| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#ifndef jit_shared_IonAssemblerBuffer_h |
| 8 |
#define jit_shared_IonAssemblerBuffer_h |
| 9 |
|
| 10 |
#include "mozilla/Assertions.h" |
| 11 |
#include "mozilla/MathAlgorithms.h" |
| 12 |
|
| 13 |
#include <algorithm> |
| 14 |
|
| 15 |
#include "jit/ProcessExecutableMemory.h" |
| 16 |
#include "jit/shared/Assembler-shared.h" |
| 17 |
|
| 18 |
namespace js { |
| 19 |
namespace jit { |
| 20 |
|
| 21 |
// The offset into a buffer, in bytes. |
| 22 |
class BufferOffset { |
| 23 |
int offset; |
| 24 |
|
| 25 |
public: |
| 26 |
friend BufferOffset nextOffset(); |
| 27 |
|
| 28 |
BufferOffset() : offset(INT_MIN) {} |
| 29 |
|
| 30 |
explicit BufferOffset(int offset_) : offset(offset_) { |
| 31 |
MOZ_ASSERT(offset >= 0); |
| 32 |
} |
| 33 |
|
| 34 |
explicit BufferOffset(Label* l) : offset(l->offset()) { |
| 35 |
MOZ_ASSERT(offset >= 0); |
| 36 |
} |
| 37 |
|
| 38 |
int getOffset() const { return offset; } |
| 39 |
bool assigned() const { return offset != INT_MIN; } |
| 40 |
|
| 41 |
// A BOffImm is a Branch Offset Immediate. It is an architecture-specific |
| 42 |
// structure that holds the immediate for a pc relative branch. diffB takes |
| 43 |
// the label for the destination of the branch, and encodes the immediate |
| 44 |
// for the branch. This will need to be fixed up later, since A pool may be |
| 45 |
// inserted between the branch and its destination. |
| 46 |
template <class BOffImm> |
| 47 |
BOffImm diffB(BufferOffset other) const { |
| 48 |
if (!BOffImm::IsInRange(offset - other.offset)) { |
| 49 |
return BOffImm(); |
| 50 |
} |
| 51 |
return BOffImm(offset - other.offset); |
| 52 |
} |
| 53 |
|
| 54 |
template <class BOffImm> |
| 55 |
BOffImm diffB(Label* other) const { |
| 56 |
MOZ_ASSERT(other->bound()); |
| 57 |
if (!BOffImm::IsInRange(offset - other->offset())) { |
| 58 |
return BOffImm(); |
| 59 |
} |
| 60 |
return BOffImm(offset - other->offset()); |
| 61 |
} |
| 62 |
}; |
| 63 |
|
| 64 |
inline bool operator<(BufferOffset a, BufferOffset b) { |
| 65 |
return a.getOffset() < b.getOffset(); |
| 66 |
} |
| 67 |
|
| 68 |
inline bool operator>(BufferOffset a, BufferOffset b) { |
| 69 |
return a.getOffset() > b.getOffset(); |
| 70 |
} |
| 71 |
|
| 72 |
inline bool operator<=(BufferOffset a, BufferOffset b) { |
| 73 |
return a.getOffset() <= b.getOffset(); |
| 74 |
} |
| 75 |
|
| 76 |
inline bool operator>=(BufferOffset a, BufferOffset b) { |
| 77 |
return a.getOffset() >= b.getOffset(); |
| 78 |
} |
| 79 |
|
| 80 |
inline bool operator==(BufferOffset a, BufferOffset b) { |
| 81 |
return a.getOffset() == b.getOffset(); |
| 82 |
} |
| 83 |
|
| 84 |
inline bool operator!=(BufferOffset a, BufferOffset b) { |
| 85 |
return a.getOffset() != b.getOffset(); |
| 86 |
} |
| 87 |
|
| 88 |
template <int SliceSize> |
| 89 |
class BufferSlice { |
| 90 |
protected: |
| 91 |
BufferSlice<SliceSize>* prev_; |
| 92 |
BufferSlice<SliceSize>* next_; |
| 93 |
|
| 94 |
size_t bytelength_; |
| 95 |
|
| 96 |
public: |
| 97 |
mozilla::Array<uint8_t, SliceSize> instructions; |
| 98 |
|
| 99 |
public: |
| 100 |
explicit BufferSlice() : prev_(nullptr), next_(nullptr), bytelength_(0) {} |
| 101 |
|
| 102 |
size_t length() const { return bytelength_; } |
| 103 |
static inline size_t Capacity() { return SliceSize; } |
| 104 |
|
| 105 |
BufferSlice* getNext() const { return next_; } |
| 106 |
BufferSlice* getPrev() const { return prev_; } |
| 107 |
|
| 108 |
void setNext(BufferSlice<SliceSize>* next) { |
| 109 |
MOZ_ASSERT(next_ == nullptr); |
| 110 |
MOZ_ASSERT(next->prev_ == nullptr); |
| 111 |
next_ = next; |
| 112 |
next->prev_ = this; |
| 113 |
} |
| 114 |
|
| 115 |
void putBytes(size_t numBytes, const void* source) { |
| 116 |
MOZ_ASSERT(bytelength_ + numBytes <= SliceSize); |
| 117 |
if (source) { |
| 118 |
memcpy(&instructions[length()], source, numBytes); |
| 119 |
} |
| 120 |
bytelength_ += numBytes; |
| 121 |
} |
| 122 |
|
| 123 |
MOZ_ALWAYS_INLINE |
| 124 |
void putU32Aligned(uint32_t value) { |
| 125 |
MOZ_ASSERT(bytelength_ + 4 <= SliceSize); |
| 126 |
MOZ_ASSERT((bytelength_ & 3) == 0); |
| 127 |
MOZ_ASSERT((uintptr_t(&instructions[0]) & 3) == 0); |
| 128 |
*reinterpret_cast<uint32_t*>(&instructions[bytelength_]) = value; |
| 129 |
bytelength_ += 4; |
| 130 |
} |
| 131 |
}; |
| 132 |
|
| 133 |
template <int SliceSize, class Inst> |
| 134 |
class AssemblerBuffer { |
| 135 |
protected: |
| 136 |
using Slice = BufferSlice<SliceSize>; |
| 137 |
|
| 138 |
// Doubly-linked list of BufferSlices, with the most recent in tail position. |
| 139 |
Slice* head; |
| 140 |
Slice* tail; |
| 141 |
|
| 142 |
bool m_oom; |
| 143 |
|
| 144 |
// How many bytes has been committed to the buffer thus far. |
| 145 |
// Does not include tail. |
| 146 |
uint32_t bufferSize; |
| 147 |
|
| 148 |
// How many bytes can be in the buffer. Normally this is |
| 149 |
// MaxCodeBytesPerBuffer, but for pasteup buffers where we handle far jumps |
| 150 |
// explicitly it can be larger. |
| 151 |
uint32_t maxSize; |
| 152 |
|
| 153 |
// Finger for speeding up accesses. |
| 154 |
Slice* finger; |
| 155 |
int finger_offset; |
| 156 |
|
| 157 |
LifoAlloc lifoAlloc_; |
| 158 |
|
| 159 |
public: |
| 160 |
explicit AssemblerBuffer() |
| 161 |
: head(nullptr), |
| 162 |
tail(nullptr), |
| 163 |
m_oom(false), |
| 164 |
bufferSize(0), |
| 165 |
maxSize(MaxCodeBytesPerBuffer), |
| 166 |
finger(nullptr), |
| 167 |
finger_offset(0), |
| 168 |
lifoAlloc_(8192, js::BackgroundMallocArena) {} |
| 169 |
|
| 170 |
public: |
| 171 |
bool isAligned(size_t alignment) const { |
| 172 |
MOZ_ASSERT(mozilla::IsPowerOfTwo(alignment)); |
| 173 |
return !(size() & (alignment - 1)); |
| 174 |
} |
| 175 |
|
| 176 |
void setUnlimited() { maxSize = MaxCodeBytesPerProcess; } |
| 177 |
|
| 178 |
private: |
| 179 |
Slice* newSlice(LifoAlloc& a) { |
| 180 |
if (size() > maxSize - sizeof(Slice)) { |
| 181 |
fail_oom(); |
| 182 |
return nullptr; |
| 183 |
} |
| 184 |
Slice* tmp = static_cast<Slice*>(a.alloc(sizeof(Slice))); |
| 185 |
if (!tmp) { |
| 186 |
fail_oom(); |
| 187 |
return nullptr; |
| 188 |
} |
| 189 |
return new (tmp) Slice; |
| 190 |
} |
| 191 |
|
| 192 |
public: |
| 193 |
bool ensureSpace(size_t size) { |
| 194 |
// Space can exist in the most recent Slice. |
| 195 |
if (tail && tail->length() + size <= tail->Capacity()) { |
| 196 |
// Simulate allocation failure even when we don't need a new slice. |
| 197 |
if (js::oom::ShouldFailWithOOM()) { |
| 198 |
return fail_oom(); |
| 199 |
} |
| 200 |
|
| 201 |
return true; |
| 202 |
} |
| 203 |
|
| 204 |
// Otherwise, a new Slice must be added. |
| 205 |
Slice* slice = newSlice(lifoAlloc_); |
| 206 |
if (slice == nullptr) { |
| 207 |
return fail_oom(); |
| 208 |
} |
| 209 |
|
| 210 |
// If this is the first Slice in the buffer, add to head position. |
| 211 |
if (!head) { |
| 212 |
head = slice; |
| 213 |
finger = slice; |
| 214 |
finger_offset = 0; |
| 215 |
} |
| 216 |
|
| 217 |
// Finish the last Slice and add the new Slice to the linked list. |
| 218 |
if (tail) { |
| 219 |
bufferSize += tail->length(); |
| 220 |
tail->setNext(slice); |
| 221 |
} |
| 222 |
tail = slice; |
| 223 |
|
| 224 |
return true; |
| 225 |
} |
| 226 |
|
| 227 |
BufferOffset putByte(uint8_t value) { |
| 228 |
return putBytes(sizeof(value), &value); |
| 229 |
} |
| 230 |
|
| 231 |
BufferOffset putShort(uint16_t value) { |
| 232 |
return putBytes(sizeof(value), &value); |
| 233 |
} |
| 234 |
|
| 235 |
BufferOffset putInt(uint32_t value) { |
| 236 |
return putBytes(sizeof(value), &value); |
| 237 |
} |
| 238 |
|
| 239 |
MOZ_ALWAYS_INLINE |
| 240 |
BufferOffset putU32Aligned(uint32_t value) { |
| 241 |
if (!ensureSpace(sizeof(value))) { |
| 242 |
return BufferOffset(); |
| 243 |
} |
| 244 |
|
| 245 |
BufferOffset ret = nextOffset(); |
| 246 |
tail->putU32Aligned(value); |
| 247 |
return ret; |
| 248 |
} |
| 249 |
|
| 250 |
// Add numBytes bytes to this buffer. |
| 251 |
// The data must fit in a single slice. |
| 252 |
BufferOffset putBytes(size_t numBytes, const void* inst) { |
| 253 |
if (!ensureSpace(numBytes)) { |
| 254 |
return BufferOffset(); |
| 255 |
} |
| 256 |
|
| 257 |
BufferOffset ret = nextOffset(); |
| 258 |
tail->putBytes(numBytes, inst); |
| 259 |
return ret; |
| 260 |
} |
| 261 |
|
| 262 |
// Add a potentially large amount of data to this buffer. |
| 263 |
// The data may be distrubuted across multiple slices. |
| 264 |
// Return the buffer offset of the first added byte. |
| 265 |
BufferOffset putBytesLarge(size_t numBytes, const void* data) { |
| 266 |
BufferOffset ret = nextOffset(); |
| 267 |
while (numBytes > 0) { |
| 268 |
if (!ensureSpace(1)) { |
| 269 |
return BufferOffset(); |
| 270 |
} |
| 271 |
size_t avail = tail->Capacity() - tail->length(); |
| 272 |
size_t xfer = numBytes < avail ? numBytes : avail; |
| 273 |
MOZ_ASSERT(xfer > 0, "ensureSpace should have allocated a slice"); |
| 274 |
tail->putBytes(xfer, data); |
| 275 |
data = (const uint8_t*)data + xfer; |
| 276 |
numBytes -= xfer; |
| 277 |
} |
| 278 |
return ret; |
| 279 |
} |
| 280 |
|
| 281 |
unsigned int size() const { |
| 282 |
if (tail) { |
| 283 |
return bufferSize + tail->length(); |
| 284 |
} |
| 285 |
return bufferSize; |
| 286 |
} |
| 287 |
BufferOffset nextOffset() const { return BufferOffset(size()); } |
| 288 |
|
| 289 |
bool oom() const { return m_oom; } |
| 290 |
|
| 291 |
bool fail_oom() { |
| 292 |
m_oom = true; |
| 293 |
#ifdef DEBUG |
| 294 |
JitContext* context = MaybeGetJitContext(); |
| 295 |
if (context) { |
| 296 |
context->setOOM(); |
| 297 |
} |
| 298 |
#endif |
| 299 |
return false; |
| 300 |
} |
| 301 |
|
| 302 |
private: |
| 303 |
void update_finger(Slice* finger_, int fingerOffset_) { |
| 304 |
finger = finger_; |
| 305 |
finger_offset = fingerOffset_; |
| 306 |
} |
| 307 |
|
| 308 |
static const unsigned SliceDistanceRequiringFingerUpdate = 3; |
| 309 |
|
| 310 |
Inst* getInstForwards(BufferOffset off, Slice* start, int startOffset, |
| 311 |
bool updateFinger = false) { |
| 312 |
const int offset = off.getOffset(); |
| 313 |
|
| 314 |
int cursor = startOffset; |
| 315 |
unsigned slicesSkipped = 0; |
| 316 |
|
| 317 |
MOZ_ASSERT(offset >= cursor); |
| 318 |
|
| 319 |
for (Slice* slice = start; slice != nullptr; slice = slice->getNext()) { |
| 320 |
const int slicelen = slice->length(); |
| 321 |
|
| 322 |
// Is the offset within the bounds of this slice? |
| 323 |
if (offset < cursor + slicelen) { |
| 324 |
if (updateFinger || |
| 325 |
slicesSkipped >= SliceDistanceRequiringFingerUpdate) { |
| 326 |
update_finger(slice, cursor); |
| 327 |
} |
| 328 |
|
| 329 |
MOZ_ASSERT(offset - cursor < (int)slice->length()); |
| 330 |
return (Inst*)&slice->instructions[offset - cursor]; |
| 331 |
} |
| 332 |
|
| 333 |
cursor += slicelen; |
| 334 |
slicesSkipped++; |
| 335 |
} |
| 336 |
|
| 337 |
MOZ_CRASH("Invalid instruction cursor."); |
| 338 |
} |
| 339 |
|
| 340 |
Inst* getInstBackwards(BufferOffset off, Slice* start, int startOffset, |
| 341 |
bool updateFinger = false) { |
| 342 |
const int offset = off.getOffset(); |
| 343 |
|
| 344 |
int cursor = startOffset; // First (lowest) offset in the start Slice. |
| 345 |
unsigned slicesSkipped = 0; |
| 346 |
|
| 347 |
MOZ_ASSERT(offset < int(cursor + start->length())); |
| 348 |
|
| 349 |
for (Slice* slice = start; slice != nullptr;) { |
| 350 |
// Is the offset within the bounds of this slice? |
| 351 |
if (offset >= cursor) { |
| 352 |
if (updateFinger || |
| 353 |
slicesSkipped >= SliceDistanceRequiringFingerUpdate) { |
| 354 |
update_finger(slice, cursor); |
| 355 |
} |
| 356 |
|
| 357 |
MOZ_ASSERT(offset - cursor < (int)slice->length()); |
| 358 |
return (Inst*)&slice->instructions[offset - cursor]; |
| 359 |
} |
| 360 |
|
| 361 |
// Move the cursor to the start of the previous slice. |
| 362 |
Slice* prev = slice->getPrev(); |
| 363 |
cursor -= prev->length(); |
| 364 |
|
| 365 |
slice = prev; |
| 366 |
slicesSkipped++; |
| 367 |
} |
| 368 |
|
| 369 |
MOZ_CRASH("Invalid instruction cursor."); |
| 370 |
} |
| 371 |
|
| 372 |
public: |
| 373 |
Inst* getInstOrNull(BufferOffset off) { |
| 374 |
if (!off.assigned()) { |
| 375 |
return nullptr; |
| 376 |
} |
| 377 |
return getInst(off); |
| 378 |
} |
| 379 |
|
| 380 |
// Get a pointer to the instruction at offset |off| which must be within the |
| 381 |
// bounds of the buffer. Use |getInstOrNull()| if |off| may be unassigned. |
| 382 |
Inst* getInst(BufferOffset off) { |
| 383 |
const int offset = off.getOffset(); |
| 384 |
// This function is hot, do not make the next line a RELEASE_ASSERT. |
| 385 |
MOZ_ASSERT(off.assigned() && offset >= 0 && unsigned(offset) < size()); |
| 386 |
|
| 387 |
// Is the instruction in the last slice? |
| 388 |
if (offset >= int(bufferSize)) { |
| 389 |
return (Inst*)&tail->instructions[offset - bufferSize]; |
| 390 |
} |
| 391 |
|
| 392 |
// How close is this offset to the previous one we looked up? |
| 393 |
// If it is sufficiently far from the start and end of the buffer, |
| 394 |
// use the finger to start midway through the list. |
| 395 |
int finger_dist = abs(offset - finger_offset); |
| 396 |
if (finger_dist < std::min(offset, int(bufferSize - offset))) { |
| 397 |
if (finger_offset < offset) { |
| 398 |
return getInstForwards(off, finger, finger_offset, true); |
| 399 |
} |
| 400 |
return getInstBackwards(off, finger, finger_offset, true); |
| 401 |
} |
| 402 |
|
| 403 |
// Is the instruction closer to the start or to the end? |
| 404 |
if (offset < int(bufferSize - offset)) { |
| 405 |
return getInstForwards(off, head, 0); |
| 406 |
} |
| 407 |
|
| 408 |
// The last slice was already checked above, so start at the |
| 409 |
// second-to-last. |
| 410 |
Slice* prev = tail->getPrev(); |
| 411 |
return getInstBackwards(off, prev, bufferSize - prev->length()); |
| 412 |
} |
| 413 |
|
| 414 |
using ThisClass = AssemblerBuffer<SliceSize, Inst>; |
| 415 |
|
| 416 |
class AssemblerBufferInstIterator { |
| 417 |
BufferOffset bo_; |
| 418 |
ThisClass* buffer_; |
| 419 |
|
| 420 |
public: |
| 421 |
explicit AssemblerBufferInstIterator(BufferOffset bo, ThisClass* buffer) |
| 422 |
: bo_(bo), buffer_(buffer) {} |
| 423 |
void advance(int offset) { bo_ = BufferOffset(bo_.getOffset() + offset); } |
| 424 |
Inst* next() { |
| 425 |
advance(cur()->size()); |
| 426 |
return cur(); |
| 427 |
} |
| 428 |
Inst* peek() { |
| 429 |
return buffer_->getInst(BufferOffset(bo_.getOffset() + cur()->size())); |
| 430 |
} |
| 431 |
Inst* cur() const { return buffer_->getInst(bo_); } |
| 432 |
}; |
| 433 |
}; |
| 434 |
|
| 435 |
} // namespace jit |
| 436 |
} // namespace js |
| 437 |
|
| 438 |
#endif // jit_shared_IonAssemblerBuffer_h |