summaryrefslogtreecommitdiffstats
path: root/js/src/jit/shared/IonAssemblerBuffer.h
blob: b6af126bc882f5ca4788e9ce4283a8a6ee64b5aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef jit_shared_IonAssemblerBuffer_h
#define jit_shared_IonAssemblerBuffer_h

#include "mozilla/Assertions.h"
#include "mozilla/MathAlgorithms.h"

#include "jit/shared/Assembler-shared.h"

namespace js {
namespace jit {

// The offset into a buffer, in bytes.
class BufferOffset
{
    int offset;

  public:
    friend BufferOffset nextOffset();

    BufferOffset()
      : offset(INT_MIN)
    { }

    explicit BufferOffset(int offset_)
      : offset(offset_)
    { }

    explicit BufferOffset(Label* l)
      : offset(l->offset())
    { }

    explicit BufferOffset(RepatchLabel* l)
      : offset(l->offset())
    { }

    int getOffset() const { return offset; }
    bool assigned() const { return offset != INT_MIN; }

    // A BOffImm is a Branch Offset Immediate. It is an architecture-specific
    // structure that holds the immediate for a pc relative branch. diffB takes
    // the label for the destination of the branch, and encodes the immediate
    // for the branch. This will need to be fixed up later, since A pool may be
    // inserted between the branch and its destination.
    template <class BOffImm>
    BOffImm diffB(BufferOffset other) const {
        if (!BOffImm::IsInRange(offset - other.offset))
            return BOffImm();
        return BOffImm(offset - other.offset);
    }

    template <class BOffImm>
    BOffImm diffB(Label* other) const {
        MOZ_ASSERT(other->bound());
        if (!BOffImm::IsInRange(offset - other->offset()))
            return BOffImm();
        return BOffImm(offset - other->offset());
    }
};

inline bool
operator<(BufferOffset a, BufferOffset b)
{
    return a.getOffset() < b.getOffset();
}

inline bool
operator>(BufferOffset a, BufferOffset b)
{
    return a.getOffset() > b.getOffset();
}

inline bool
operator<=(BufferOffset a, BufferOffset b)
{
    return a.getOffset() <= b.getOffset();
}

inline bool
operator>=(BufferOffset a, BufferOffset b)
{
    return a.getOffset() >= b.getOffset();
}

inline bool
operator==(BufferOffset a, BufferOffset b)
{
    return a.getOffset() == b.getOffset();
}

inline bool
operator!=(BufferOffset a, BufferOffset b)
{
    return a.getOffset() != b.getOffset();
}

template<int SliceSize>
class BufferSlice
{
  protected:
    BufferSlice<SliceSize>* prev_;
    BufferSlice<SliceSize>* next_;

    size_t bytelength_;

  public:
    mozilla::Array<uint8_t, SliceSize> instructions;

  public:
    explicit BufferSlice()
      : prev_(nullptr), next_(nullptr), bytelength_(0)
    { }

    size_t length() const { return bytelength_; }
    static inline size_t Capacity() { return SliceSize; }

    BufferSlice* getNext() const { return next_; }
    BufferSlice* getPrev() const { return prev_; }

    void setNext(BufferSlice<SliceSize>* next) {
        MOZ_ASSERT(next_ == nullptr);
        MOZ_ASSERT(next->prev_ == nullptr);
        next_ = next;
        next->prev_ = this;
    }

    void putBytes(size_t numBytes, const void* source) {
        MOZ_ASSERT(bytelength_ + numBytes <= SliceSize);
        if (source)
            memcpy(&instructions[length()], source, numBytes);
        bytelength_ += numBytes;
    }
};

template<int SliceSize, class Inst>
class AssemblerBuffer
{
  protected:
    typedef BufferSlice<SliceSize> Slice;
    typedef AssemblerBuffer<SliceSize, Inst> AssemblerBuffer_;

    // Doubly-linked list of BufferSlices, with the most recent in tail position.
    Slice* head;
    Slice* tail;

    bool m_oom;
    bool m_bail;

    // How many bytes has been committed to the buffer thus far.
    // Does not include tail.
    uint32_t bufferSize;

    // Finger for speeding up accesses.
    Slice* finger;
    int finger_offset;

    LifoAlloc lifoAlloc_;

  public:
    explicit AssemblerBuffer()
      : head(nullptr),
        tail(nullptr),
        m_oom(false),
        m_bail(false),
        bufferSize(0),
        finger(nullptr),
        finger_offset(0),
        lifoAlloc_(8192)
    { }

  public:
    bool isAligned(size_t alignment) const {
        MOZ_ASSERT(mozilla::IsPowerOfTwo(alignment));
        return !(size() & (alignment - 1));
    }

  protected:
    virtual Slice* newSlice(LifoAlloc& a) {
        Slice* tmp = static_cast<Slice*>(a.alloc(sizeof(Slice)));
        if (!tmp) {
            fail_oom();
            return nullptr;
        }
        return new (tmp) Slice;
    }

  public:
    bool ensureSpace(size_t size) {
        // Space can exist in the most recent Slice.
        if (tail && tail->length() + size <= tail->Capacity()) {
            // Simulate allocation failure even when we don't need a new slice.
            if (js::oom::ShouldFailWithOOM())
                return fail_oom();

            return true;
        }

        // Otherwise, a new Slice must be added.
        Slice* slice = newSlice(lifoAlloc_);
        if (slice == nullptr)
            return fail_oom();

        // If this is the first Slice in the buffer, add to head position.
        if (!head) {
            head = slice;
            finger = slice;
            finger_offset = 0;
        }

        // Finish the last Slice and add the new Slice to the linked list.
        if (tail) {
            bufferSize += tail->length();
            tail->setNext(slice);
        }
        tail = slice;

        return true;
    }

    BufferOffset putByte(uint8_t value) {
        return putBytes(sizeof(value), &value);
    }

    BufferOffset putShort(uint16_t value) {
        return putBytes(sizeof(value), &value);
    }

    BufferOffset putInt(uint32_t value) {
        return putBytes(sizeof(value), &value);
    }

    // Add numBytes bytes to this buffer.
    // The data must fit in a single slice.
    BufferOffset putBytes(size_t numBytes, const void* inst) {
        if (!ensureSpace(numBytes))
            return BufferOffset();

        BufferOffset ret = nextOffset();
        tail->putBytes(numBytes, inst);
        return ret;
    }

    // Add a potentially large amount of data to this buffer.
    // The data may be distrubuted across multiple slices.
    // Return the buffer offset of the first added byte.
    BufferOffset putBytesLarge(size_t numBytes, const void* data)
    {
        BufferOffset ret = nextOffset();
        while (numBytes > 0) {
            if (!ensureSpace(1))
                return BufferOffset();
            size_t avail = tail->Capacity() - tail->length();
            size_t xfer = numBytes < avail ? numBytes : avail;
            MOZ_ASSERT(xfer > 0, "ensureSpace should have allocated a slice");
            tail->putBytes(xfer, data);
            data = (const uint8_t*)data + xfer;
            numBytes -= xfer;
        }
        return ret;
    }

    unsigned int size() const {
        if (tail)
            return bufferSize + tail->length();
        return bufferSize;
    }

    bool oom() const { return m_oom || m_bail; }
    bool bail() const { return m_bail; }

    bool fail_oom() {
        m_oom = true;
        return false;
    }
    bool fail_bail() {
        m_bail = true;
        return false;
    }

  private:
    void update_finger(Slice* finger_, int fingerOffset_) {
        finger = finger_;
        finger_offset = fingerOffset_;
    }

    static const unsigned SliceDistanceRequiringFingerUpdate = 3;

    Inst* getInstForwards(BufferOffset off, Slice* start, int startOffset, bool updateFinger = false) {
        const int offset = off.getOffset();

        int cursor = startOffset;
        unsigned slicesSkipped = 0;

        MOZ_ASSERT(offset >= cursor);

        for (Slice *slice = start; slice != nullptr; slice = slice->getNext()) {
            const int slicelen = slice->length();

            // Is the offset within the bounds of this slice?
            if (offset < cursor + slicelen) {
                if (updateFinger || slicesSkipped >= SliceDistanceRequiringFingerUpdate)
                    update_finger(slice, cursor);

                MOZ_ASSERT(offset - cursor < (int)slice->length());
                return (Inst*)&slice->instructions[offset - cursor];
            }

            cursor += slicelen;
            slicesSkipped++;
        }

        MOZ_CRASH("Invalid instruction cursor.");
    }

    Inst* getInstBackwards(BufferOffset off, Slice* start, int startOffset, bool updateFinger = false) {
        const int offset = off.getOffset();

        int cursor = startOffset; // First (lowest) offset in the start Slice.
        unsigned slicesSkipped = 0;

        MOZ_ASSERT(offset < int(cursor + start->length()));

        for (Slice* slice = start; slice != nullptr; ) {
            // Is the offset within the bounds of this slice?
            if (offset >= cursor) {
                if (updateFinger || slicesSkipped >= SliceDistanceRequiringFingerUpdate)
                    update_finger(slice, cursor);

                MOZ_ASSERT(offset - cursor < (int)slice->length());
                return (Inst*)&slice->instructions[offset - cursor];
            }

            // Move the cursor to the start of the previous slice.
            Slice* prev = slice->getPrev();
            cursor -= prev->length();

            slice = prev;
            slicesSkipped++;
        }

        MOZ_CRASH("Invalid instruction cursor.");
    }

  public:
    Inst* getInstOrNull(BufferOffset off) {
        if (!off.assigned())
            return nullptr;
        return getInst(off);
    }

    // Get a pointer to the instruction at offset |off| which must be within the
    // bounds of the buffer. Use |getInstOrNull()| if |off| may be unassigned.
    Inst* getInst(BufferOffset off) {
        const int offset = off.getOffset();
        MOZ_RELEASE_ASSERT(off.assigned() && offset >= 0 && (unsigned)offset < size());

        // Is the instruction in the last slice?
        if (offset >= int(bufferSize))
            return (Inst*)&tail->instructions[offset - bufferSize];

        // How close is this offset to the previous one we looked up?
        // If it is sufficiently far from the start and end of the buffer,
        // use the finger to start midway through the list.
        int finger_dist = abs(offset - finger_offset);
        if (finger_dist < Min(offset, int(bufferSize - offset))) {
            if (finger_offset < offset)
                return getInstForwards(off, finger, finger_offset, true);
            return getInstBackwards(off, finger, finger_offset, true);
        }

        // Is the instruction closer to the start or to the end?
        if (offset < int(bufferSize - offset))
            return getInstForwards(off, head, 0);

        // The last slice was already checked above, so start at the
        // second-to-last.
        Slice* prev = tail->getPrev();
        return getInstBackwards(off, prev, bufferSize - prev->length());
    }

    BufferOffset nextOffset() const {
        if (tail)
            return BufferOffset(bufferSize + tail->length());
        return BufferOffset(bufferSize);
    }

    class AssemblerBufferInstIterator
    {
        BufferOffset bo;
        AssemblerBuffer_* m_buffer;

      public:
        explicit AssemblerBufferInstIterator(BufferOffset off, AssemblerBuffer_* buffer)
          : bo(off), m_buffer(buffer)
        { }

        Inst* next() {
            Inst* i = m_buffer->getInst(bo);
            bo = BufferOffset(bo.getOffset() + i->size());
            return cur();
        }

        Inst* cur() {
            return m_buffer->getInst(bo);
        }
    };
};

} // namespace ion
} // namespace js

#endif // jit_shared_IonAssemblerBuffer_h