| 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_Assembler_shared_h |
| 8 |
#define jit_shared_Assembler_shared_h |
| 9 |
|
| 10 |
#include "mozilla/CheckedInt.h" |
| 11 |
#include "mozilla/DebugOnly.h" |
| 12 |
|
| 13 |
#include <limits.h> |
| 14 |
#include <utility> // std::pair |
| 15 |
|
| 16 |
#include "gc/Barrier.h" |
| 17 |
#include "jit/AtomicOp.h" |
| 18 |
#include "jit/JitAllocPolicy.h" |
| 19 |
#include "jit/JitCode.h" |
| 20 |
#include "jit/JitContext.h" |
| 21 |
#include "jit/Label.h" |
| 22 |
#include "jit/Registers.h" |
| 23 |
#include "jit/RegisterSets.h" |
| 24 |
#include "js/ScalarType.h" // js::Scalar::Type |
| 25 |
#include "vm/HelperThreads.h" |
| 26 |
#include "wasm/WasmCodegenTypes.h" |
| 27 |
#include "wasm/WasmConstants.h" |
| 28 |
|
| 29 |
#if defined(JS_CODEGEN_ARM) || defined(JS_CODEGEN_ARM64) || \ |
| 30 |
defined(JS_CODEGEN_MIPS64) || defined(JS_CODEGEN_LOONG64) || \ |
| 31 |
defined(JS_CODEGEN_WASM32) || defined(JS_CODEGEN_RISCV64) |
| 32 |
// Push return addresses callee-side. |
| 33 |
# define JS_USE_LINK_REGISTER |
| 34 |
#endif |
| 35 |
|
| 36 |
#if defined(JS_CODEGEN_MIPS64) || defined(JS_CODEGEN_ARM64) || \ |
| 37 |
defined(JS_CODEGEN_LOONG64) || defined(JS_CODEGEN_RISCV64) || \ |
| 38 |
defined(JS_CODEGEN_ARM) |
| 39 |
// JS_CODELABEL_LINKMODE gives labels additional metadata |
| 40 |
// describing how Bind() should patch them. |
| 41 |
# define JS_CODELABEL_LINKMODE |
| 42 |
#endif |
| 43 |
|
| 44 |
using js::wasm::FaultingCodeOffset; |
| 45 |
|
| 46 |
namespace js { |
| 47 |
namespace jit { |
| 48 |
|
| 49 |
enum class FrameType; |
| 50 |
enum class ExceptionResumeKind : int32_t; |
| 51 |
|
| 52 |
namespace Disassembler { |
| 53 |
class HeapAccess; |
| 54 |
} // namespace Disassembler |
| 55 |
|
| 56 |
static constexpr uint32_t Simd128DataSize = 4 * sizeof(int32_t); |
| 57 |
static_assert(Simd128DataSize == 4 * sizeof(int32_t), |
| 58 |
"SIMD data should be able to contain int32x4"); |
| 59 |
static_assert(Simd128DataSize == 4 * sizeof(float), |
| 60 |
"SIMD data should be able to contain float32x4"); |
| 61 |
static_assert(Simd128DataSize == 2 * sizeof(double), |
| 62 |
"SIMD data should be able to contain float64x2"); |
| 63 |
|
| 64 |
enum Scale { |
| 65 |
TimesOne = 0, |
| 66 |
TimesTwo = 1, |
| 67 |
TimesFour = 2, |
| 68 |
TimesEight = 3, |
| 69 |
Invalid = -1 |
| 70 |
}; |
| 71 |
|
| 72 |
static_assert(sizeof(JS::Value) == 8, |
| 73 |
"required for TimesEight and 3 below to be correct"); |
| 74 |
static const Scale ValueScale = TimesEight; |
| 75 |
static const size_t ValueShift = 3; |
| 76 |
|
| 77 |
static inline unsigned ScaleToShift(Scale scale) { return unsigned(scale); } |
| 78 |
|
| 79 |
static inline bool IsShiftInScaleRange(int i) { |
| 80 |
return i >= TimesOne && i <= TimesEight; |
| 81 |
} |
| 82 |
|
| 83 |
static inline Scale ShiftToScale(int i) { |
| 84 |
MOZ_ASSERT(IsShiftInScaleRange(i)); |
| 85 |
return Scale(i); |
| 86 |
} |
| 87 |
|
| 88 |
static inline Scale ScaleFromElemWidth(int shift) { |
| 89 |
switch (shift) { |
| 90 |
case 1: |
| 91 |
return TimesOne; |
| 92 |
case 2: |
| 93 |
return TimesTwo; |
| 94 |
case 4: |
| 95 |
return TimesFour; |
| 96 |
case 8: |
| 97 |
return TimesEight; |
| 98 |
} |
| 99 |
|
| 100 |
MOZ_CRASH("Invalid scale"); |
| 101 |
} |
| 102 |
|
| 103 |
static inline Scale ScaleFromScalarType(Scalar::Type type) { |
| 104 |
return ScaleFromElemWidth(Scalar::byteSize(type)); |
| 105 |
} |
| 106 |
|
| 107 |
#ifdef JS_JITSPEW |
| 108 |
static inline const char* StringFromScale(Scale scale) { |
| 109 |
switch (scale) { |
| 110 |
case TimesOne: |
| 111 |
return "TimesOne"; |
| 112 |
case TimesTwo: |
| 113 |
return "TimesTwo"; |
| 114 |
case TimesFour: |
| 115 |
return "TimesFour"; |
| 116 |
case TimesEight: |
| 117 |
return "TimesEight"; |
| 118 |
default: |
| 119 |
break; |
| 120 |
} |
| 121 |
MOZ_CRASH("Unknown Scale"); |
| 122 |
} |
| 123 |
#endif |
| 124 |
|
| 125 |
// Used for 32-bit immediates which do not require relocation. |
| 126 |
struct Imm32 { |
| 127 |
int32_t value; |
| 128 |
|
| 129 |
explicit Imm32(int32_t value) : value(value) {} |
| 130 |
explicit Imm32(FrameType type) : Imm32(int32_t(type)) {} |
| 131 |
explicit Imm32(ExceptionResumeKind kind) : Imm32(int32_t(kind)) {} |
| 132 |
|
| 133 |
static inline Imm32 ShiftOf(enum Scale s) { |
| 134 |
switch (s) { |
| 135 |
case TimesOne: |
| 136 |
return Imm32(0); |
| 137 |
case TimesTwo: |
| 138 |
return Imm32(1); |
| 139 |
case TimesFour: |
| 140 |
return Imm32(2); |
| 141 |
case TimesEight: |
| 142 |
return Imm32(3); |
| 143 |
default: |
| 144 |
MOZ_CRASH("Invalid scale"); |
| 145 |
}; |
| 146 |
} |
| 147 |
|
| 148 |
static inline Imm32 FactorOf(enum Scale s) { |
| 149 |
return Imm32(1 << ShiftOf(s).value); |
| 150 |
} |
| 151 |
}; |
| 152 |
|
| 153 |
// Pointer-sized integer to be embedded as an immediate in an instruction. |
| 154 |
struct ImmWord { |
| 155 |
uintptr_t value; |
| 156 |
|
| 157 |
explicit ImmWord(uintptr_t value) : value(value) {} |
| 158 |
}; |
| 159 |
|
| 160 |
// Used for 64-bit immediates which do not require relocation. |
| 161 |
struct Imm64 { |
| 162 |
uint64_t value; |
| 163 |
|
| 164 |
explicit Imm64(int64_t value) : value(value) {} |
| 165 |
|
| 166 |
Imm32 low() const { return Imm32(int32_t(value)); } |
| 167 |
|
| 168 |
Imm32 hi() const { return Imm32(int32_t(value >> 32)); } |
| 169 |
}; |
| 170 |
|
| 171 |
#ifdef DEBUG |
| 172 |
static inline bool IsCompilingWasm() { |
| 173 |
return GetJitContext()->isCompilingWasm(); |
| 174 |
} |
| 175 |
#endif |
| 176 |
|
| 177 |
// Pointer to be embedded as an immediate in an instruction. |
| 178 |
struct ImmPtr { |
| 179 |
void* value; |
| 180 |
|
| 181 |
struct NoCheckToken {}; |
| 182 |
|
| 183 |
explicit constexpr ImmPtr(std::nullptr_t) : value(nullptr) { |
| 184 |
// Explicit constructor for nullptr. This ensures ImmPtr(0) can't be called. |
| 185 |
// Either use ImmPtr(nullptr) or ImmWord(0). |
| 186 |
} |
| 187 |
|
| 188 |
explicit ImmPtr(void* value, NoCheckToken) : value(value) { |
| 189 |
// A special unchecked variant for contexts where we know it is safe to |
| 190 |
// use an immptr. This is assuming the caller knows what they're doing. |
| 191 |
} |
| 192 |
|
| 193 |
explicit ImmPtr(const void* value) : value(const_cast<void*>(value)) { |
| 194 |
// To make code serialization-safe, wasm compilation should only |
| 195 |
// compile pointer immediates using a SymbolicAddress. |
| 196 |
MOZ_ASSERT(!IsCompilingWasm()); |
| 197 |
} |
| 198 |
|
| 199 |
template <class R> |
| 200 |
explicit ImmPtr(R (*pf)()) : value(JS_FUNC_TO_DATA_PTR(void*, pf)) { |
| 201 |
MOZ_ASSERT(!IsCompilingWasm()); |
| 202 |
} |
| 203 |
|
| 204 |
template <class R, class A1> |
| 205 |
explicit ImmPtr(R (*pf)(A1)) : value(JS_FUNC_TO_DATA_PTR(void*, pf)) { |
| 206 |
MOZ_ASSERT(!IsCompilingWasm()); |
| 207 |
} |
| 208 |
|
| 209 |
template <class R, class A1, class A2> |
| 210 |
explicit ImmPtr(R (*pf)(A1, A2)) : value(JS_FUNC_TO_DATA_PTR(void*, pf)) { |
| 211 |
MOZ_ASSERT(!IsCompilingWasm()); |
| 212 |
} |
| 213 |
|
| 214 |
template <class R, class A1, class A2, class A3> |
| 215 |
explicit ImmPtr(R (*pf)(A1, A2, A3)) : value(JS_FUNC_TO_DATA_PTR(void*, pf)) { |
| 216 |
MOZ_ASSERT(!IsCompilingWasm()); |
| 217 |
} |
| 218 |
|
| 219 |
template <class R, class A1, class A2, class A3, class A4> |
| 220 |
explicit ImmPtr(R (*pf)(A1, A2, A3, A4)) |
| 221 |
: value(JS_FUNC_TO_DATA_PTR(void*, pf)) { |
| 222 |
MOZ_ASSERT(!IsCompilingWasm()); |
| 223 |
} |
| 224 |
}; |
| 225 |
|
| 226 |
// The same as ImmPtr except that the intention is to patch this |
| 227 |
// instruction. The initial value of the immediate is 'addr' and this value is |
| 228 |
// either clobbered or used in the patching process. |
| 229 |
struct PatchedImmPtr { |
| 230 |
void* value; |
| 231 |
|
| 232 |
explicit PatchedImmPtr() : value(nullptr) {} |
| 233 |
explicit PatchedImmPtr(const void* value) : value(const_cast<void*>(value)) {} |
| 234 |
}; |
| 235 |
|
| 236 |
class AssemblerShared; |
| 237 |
class ImmGCPtr; |
| 238 |
|
| 239 |
// Used for immediates which require relocation. |
| 240 |
class ImmGCPtr { |
| 241 |
public: |
| 242 |
const gc::Cell* value; |
| 243 |
|
| 244 |
explicit ImmGCPtr(const gc::Cell* ptr) : value(ptr) { |
| 245 |
// Nursery pointers can't be used if the main thread might be currently |
| 246 |
// performing a minor GC. |
| 247 |
MOZ_ASSERT_IF(ptr && !ptr->isTenured(), |
| 248 |
!CurrentThreadIsOffThreadCompiling()); |
| 249 |
|
| 250 |
// wasm shouldn't be creating GC things |
| 251 |
MOZ_ASSERT(!IsCompilingWasm()); |
| 252 |
} |
| 253 |
|
| 254 |
private: |
| 255 |
ImmGCPtr() : value(0) {} |
| 256 |
}; |
| 257 |
|
| 258 |
// Pointer to trampoline code. Trampoline code is kept alive until the runtime |
| 259 |
// is destroyed, so does not need to be traced. |
| 260 |
struct TrampolinePtr { |
| 261 |
uint8_t* value; |
| 262 |
|
| 263 |
TrampolinePtr() : value(nullptr) {} |
| 264 |
explicit TrampolinePtr(uint8_t* value) : value(value) { MOZ_ASSERT(value); } |
| 265 |
}; |
| 266 |
|
| 267 |
// Pointer to be embedded as an immediate that is loaded/stored from by an |
| 268 |
// instruction. |
| 269 |
struct AbsoluteAddress { |
| 270 |
void* addr; |
| 271 |
|
| 272 |
explicit AbsoluteAddress(const void* addr) : addr(const_cast<void*>(addr)) { |
| 273 |
MOZ_ASSERT(!IsCompilingWasm()); |
| 274 |
} |
| 275 |
|
| 276 |
AbsoluteAddress offset(ptrdiff_t delta) { |
| 277 |
return AbsoluteAddress(((uint8_t*)addr) + delta); |
| 278 |
} |
| 279 |
}; |
| 280 |
|
| 281 |
// The same as AbsoluteAddress except that the intention is to patch this |
| 282 |
// instruction. The initial value of the immediate is 'addr' and this value is |
| 283 |
// either clobbered or used in the patching process. |
| 284 |
struct PatchedAbsoluteAddress { |
| 285 |
void* addr; |
| 286 |
|
| 287 |
explicit PatchedAbsoluteAddress() : addr(nullptr) {} |
| 288 |
explicit PatchedAbsoluteAddress(const void* addr) |
| 289 |
: addr(const_cast<void*>(addr)) {} |
| 290 |
explicit PatchedAbsoluteAddress(uintptr_t addr) |
| 291 |
: addr(reinterpret_cast<void*>(addr)) {} |
| 292 |
}; |
| 293 |
|
| 294 |
// Specifies an address computed in the form of a register base and a constant, |
| 295 |
// 32-bit offset. |
| 296 |
struct Address { |
| 297 |
RegisterOrSP base; |
| 298 |
int32_t offset; |
| 299 |
|
| 300 |
Address(Register base, int32_t offset) |
| 301 |
: base(RegisterOrSP(base)), offset(offset) {} |
| 302 |
|
| 303 |
#ifdef JS_HAS_HIDDEN_SP |
| 304 |
Address(RegisterOrSP base, int32_t offset) : base(base), offset(offset) {} |
| 305 |
#endif |
| 306 |
|
| 307 |
Address() = delete; |
| 308 |
|
| 309 |
bool operator==(const Address& other) const { |
| 310 |
return base == other.base && offset == other.offset; |
| 311 |
} |
| 312 |
|
| 313 |
bool operator!=(const Address& other) const { return !(*this == other); } |
| 314 |
}; |
| 315 |
|
| 316 |
#if JS_BITS_PER_WORD == 32 |
| 317 |
|
| 318 |
static inline Address LowWord(const Address& address) { |
| 319 |
using mozilla::CheckedInt; |
| 320 |
|
| 321 |
CheckedInt<int32_t> offset = |
| 322 |
CheckedInt<int32_t>(address.offset) + INT64LOW_OFFSET; |
| 323 |
MOZ_ALWAYS_TRUE(offset.isValid()); |
| 324 |
return Address(address.base, offset.value()); |
| 325 |
} |
| 326 |
|
| 327 |
static inline Address HighWord(const Address& address) { |
| 328 |
using mozilla::CheckedInt; |
| 329 |
|
| 330 |
CheckedInt<int32_t> offset = |
| 331 |
CheckedInt<int32_t>(address.offset) + INT64HIGH_OFFSET; |
| 332 |
MOZ_ALWAYS_TRUE(offset.isValid()); |
| 333 |
return Address(address.base, offset.value()); |
| 334 |
} |
| 335 |
|
| 336 |
#endif |
| 337 |
|
| 338 |
// Specifies an address computed in the form of a register base, a register |
| 339 |
// index with a scale, and a constant, 32-bit offset. |
| 340 |
struct BaseIndex { |
| 341 |
RegisterOrSP base; |
| 342 |
Register index; |
| 343 |
Scale scale; |
| 344 |
int32_t offset; |
| 345 |
|
| 346 |
BaseIndex(Register base, Register index, Scale scale, int32_t offset = 0) |
| 347 |
: base(RegisterOrSP(base)), index(index), scale(scale), offset(offset) {} |
| 348 |
|
| 349 |
#ifdef JS_HAS_HIDDEN_SP |
| 350 |
BaseIndex(RegisterOrSP base, Register index, Scale scale, int32_t offset = 0) |
| 351 |
: base(base), index(index), scale(scale), offset(offset) {} |
| 352 |
#endif |
| 353 |
|
| 354 |
BaseIndex() = delete; |
| 355 |
}; |
| 356 |
|
| 357 |
#if JS_BITS_PER_WORD == 32 |
| 358 |
|
| 359 |
static inline BaseIndex LowWord(const BaseIndex& address) { |
| 360 |
using mozilla::CheckedInt; |
| 361 |
|
| 362 |
CheckedInt<int32_t> offset = |
| 363 |
CheckedInt<int32_t>(address.offset) + INT64LOW_OFFSET; |
| 364 |
MOZ_ALWAYS_TRUE(offset.isValid()); |
| 365 |
return BaseIndex(address.base, address.index, address.scale, offset.value()); |
| 366 |
} |
| 367 |
|
| 368 |
static inline BaseIndex HighWord(const BaseIndex& address) { |
| 369 |
using mozilla::CheckedInt; |
| 370 |
|
| 371 |
CheckedInt<int32_t> offset = |
| 372 |
CheckedInt<int32_t>(address.offset) + INT64HIGH_OFFSET; |
| 373 |
MOZ_ALWAYS_TRUE(offset.isValid()); |
| 374 |
return BaseIndex(address.base, address.index, address.scale, offset.value()); |
| 375 |
} |
| 376 |
|
| 377 |
#endif |
| 378 |
|
| 379 |
// A BaseIndex used to access Values. Note that |offset| is *not* scaled by |
| 380 |
// sizeof(Value). Use this *only* if you're indexing into a series of Values |
| 381 |
// that aren't object elements or object slots (for example, values on the |
| 382 |
// stack, values in an arguments object, &c.). If you're indexing into an |
| 383 |
// object's elements or slots, don't use this directly! Use |
| 384 |
// BaseObject{Element,Slot}Index instead. |
| 385 |
struct BaseValueIndex : BaseIndex { |
| 386 |
BaseValueIndex(Register base, Register index, int32_t offset = 0) |
| 387 |
: BaseIndex(RegisterOrSP(base), index, ValueScale, offset) {} |
| 388 |
|
| 389 |
#ifdef JS_HAS_HIDDEN_SP |
| 390 |
BaseValueIndex(RegisterOrSP base, Register index, int32_t offset = 0) |
| 391 |
: BaseIndex(base, index, ValueScale, offset) {} |
| 392 |
#endif |
| 393 |
}; |
| 394 |
|
| 395 |
// Specifies the address of an indexed Value within object elements from a |
| 396 |
// base. The index must not already be scaled by sizeof(Value)! |
| 397 |
struct BaseObjectElementIndex : BaseValueIndex { |
| 398 |
BaseObjectElementIndex(Register base, Register index, int32_t offset = 0) |
| 399 |
: BaseValueIndex(base, index, offset) {} |
| 400 |
|
| 401 |
#ifdef JS_HAS_HIDDEN_SP |
| 402 |
BaseObjectElementIndex(RegisterOrSP base, Register index, int32_t offset = 0) |
| 403 |
: BaseValueIndex(base, index, offset) {} |
| 404 |
#endif |
| 405 |
|
| 406 |
static void staticAssertions(); |
| 407 |
}; |
| 408 |
|
| 409 |
// Like BaseObjectElementIndex, except for object slots. |
| 410 |
struct BaseObjectSlotIndex : BaseValueIndex { |
| 411 |
BaseObjectSlotIndex(Register base, Register index) |
| 412 |
: BaseValueIndex(base, index) {} |
| 413 |
|
| 414 |
#ifdef JS_HAS_HIDDEN_SP |
| 415 |
BaseObjectSlotIndex(RegisterOrSP base, Register index) |
| 416 |
: BaseValueIndex(base, index) {} |
| 417 |
#endif |
| 418 |
|
| 419 |
static void staticAssertions(); |
| 420 |
}; |
| 421 |
|
| 422 |
enum class RelocationKind { |
| 423 |
// The target is immovable, so patching is only needed if the source |
| 424 |
// buffer is relocated and the reference is relative. |
| 425 |
HARDCODED, |
| 426 |
|
| 427 |
// The target is the start of a JitCode buffer, which must be traced |
| 428 |
// during garbage collection. Relocations and patching may be needed. |
| 429 |
JITCODE |
| 430 |
}; |
| 431 |
|
| 432 |
class CodeOffset { |
| 433 |
size_t offset_; |
| 434 |
|
| 435 |
static const size_t NOT_BOUND = size_t(-1); |
| 436 |
|
| 437 |
public: |
| 438 |
explicit CodeOffset(size_t offset) : offset_(offset) {} |
| 439 |
CodeOffset() : offset_(NOT_BOUND) {} |
| 440 |
|
| 441 |
size_t offset() const { |
| 442 |
MOZ_ASSERT(bound()); |
| 443 |
return offset_; |
| 444 |
} |
| 445 |
|
| 446 |
void bind(size_t offset) { |
| 447 |
MOZ_ASSERT(!bound()); |
| 448 |
offset_ = offset; |
| 449 |
MOZ_ASSERT(bound()); |
| 450 |
} |
| 451 |
bool bound() const { return offset_ != NOT_BOUND; } |
| 452 |
|
| 453 |
void offsetBy(size_t delta) { |
| 454 |
MOZ_ASSERT(bound()); |
| 455 |
MOZ_ASSERT(offset_ + delta >= offset_, "no overflow"); |
| 456 |
offset_ += delta; |
| 457 |
} |
| 458 |
}; |
| 459 |
|
| 460 |
// A code label contains an absolute reference to a point in the code. Thus, it |
| 461 |
// cannot be patched until after linking. |
| 462 |
// When the source label is resolved into a memory address, this address is |
| 463 |
// patched into the destination address. |
| 464 |
// Some need to distinguish between multiple ways of patching that address. |
| 465 |
// See JS_CODELABEL_LINKMODE. |
| 466 |
class CodeLabel { |
| 467 |
// The destination position, where the absolute reference should get |
| 468 |
// patched into. |
| 469 |
CodeOffset patchAt_; |
| 470 |
|
| 471 |
// The source label (relative) in the code to where the destination should |
| 472 |
// get patched to. |
| 473 |
CodeOffset target_; |
| 474 |
|
| 475 |
#ifdef JS_CODELABEL_LINKMODE |
| 476 |
public: |
| 477 |
enum LinkMode { Uninitialized = 0, RawPointer, MoveImmediate, JumpImmediate }; |
| 478 |
|
| 479 |
private: |
| 480 |
LinkMode linkMode_ = Uninitialized; |
| 481 |
#endif |
| 482 |
|
| 483 |
public: |
| 484 |
CodeLabel() = default; |
| 485 |
explicit CodeLabel(const CodeOffset& patchAt) : patchAt_(patchAt) {} |
| 486 |
CodeLabel(const CodeOffset& patchAt, const CodeOffset& target) |
| 487 |
: patchAt_(patchAt), target_(target) {} |
| 488 |
CodeOffset* patchAt() { return &patchAt_; } |
| 489 |
CodeOffset* target() { return &target_; } |
| 490 |
CodeOffset patchAt() const { return patchAt_; } |
| 491 |
CodeOffset target() const { return target_; } |
| 492 |
#ifdef JS_CODELABEL_LINKMODE |
| 493 |
LinkMode linkMode() const { return linkMode_; } |
| 494 |
void setLinkMode(LinkMode value) { linkMode_ = value; } |
| 495 |
#endif |
| 496 |
}; |
| 497 |
|
| 498 |
using CodeLabelVector = Vector<CodeLabel, 0, SystemAllocPolicy>; |
| 499 |
|
| 500 |
class CodeLocationLabel { |
| 501 |
uint8_t* raw_ = nullptr; |
| 502 |
|
| 503 |
public: |
| 504 |
CodeLocationLabel(JitCode* code, CodeOffset base) { |
| 505 |
MOZ_ASSERT(base.offset() < code->instructionsSize()); |
| 506 |
raw_ = code->raw() + base.offset(); |
| 507 |
} |
| 508 |
explicit CodeLocationLabel(JitCode* code) { raw_ = code->raw(); } |
| 509 |
explicit CodeLocationLabel(uint8_t* raw) { |
| 510 |
MOZ_ASSERT(raw); |
| 511 |
raw_ = raw; |
| 512 |
} |
| 513 |
|
| 514 |
ptrdiff_t operator-(const CodeLocationLabel& other) const { |
| 515 |
return raw_ - other.raw_; |
| 516 |
} |
| 517 |
|
| 518 |
uint8_t* raw() const { return raw_; } |
| 519 |
}; |
| 520 |
|
| 521 |
} // namespace jit |
| 522 |
|
| 523 |
namespace wasm { |
| 524 |
|
| 525 |
// Represents an instruction to be patched and the intended pointee. These |
| 526 |
// links are accumulated in the MacroAssembler, but patching is done outside |
| 527 |
// the MacroAssembler (in Module::staticallyLink). |
| 528 |
|
| 529 |
struct SymbolicAccess { |
| 530 |
SymbolicAccess(jit::CodeOffset patchAt, SymbolicAddress target) |
| 531 |
: patchAt(patchAt), target(target) {} |
| 532 |
|
| 533 |
jit::CodeOffset patchAt; |
| 534 |
SymbolicAddress target; |
| 535 |
}; |
| 536 |
|
| 537 |
using SymbolicAccessVector = Vector<SymbolicAccess, 0, SystemAllocPolicy>; |
| 538 |
|
| 539 |
// Describes a single wasm or asm.js memory access for the purpose of generating |
| 540 |
// code and metadata. |
| 541 |
|
| 542 |
class MemoryAccessDesc { |
| 543 |
uint32_t memoryIndex_; |
| 544 |
uint64_t offset_; |
| 545 |
uint32_t align_; |
| 546 |
Scalar::Type type_; |
| 547 |
jit::Synchronization sync_; |
| 548 |
wasm::TrapSiteDesc trapDesc_; |
| 549 |
wasm::SimdOp widenOp_; |
| 550 |
enum { Plain, ZeroExtend, Splat, Widen } loadOp_; |
| 551 |
// Used for an assertion in MacroAssembler about offset length |
| 552 |
mozilla::DebugOnly<bool> hugeMemory_; |
| 553 |
|
| 554 |
public: |
| 555 |
explicit MemoryAccessDesc( |
| 556 |
uint32_t memoryIndex, Scalar::Type type, uint32_t align, uint64_t offset, |
| 557 |
wasm::TrapSiteDesc trapDesc, mozilla::DebugOnly<bool> hugeMemory, |
| 558 |
jit::Synchronization sync = jit::Synchronization::None()) |
| 559 |
: memoryIndex_(memoryIndex), |
| 560 |
offset_(offset), |
| 561 |
align_(align), |
| 562 |
type_(type), |
| 563 |
sync_(sync), |
| 564 |
trapDesc_(trapDesc), |
| 565 |
widenOp_(wasm::SimdOp::Limit), |
| 566 |
loadOp_(Plain), |
| 567 |
hugeMemory_(hugeMemory) { |
| 568 |
MOZ_ASSERT(mozilla::IsPowerOfTwo(align)); |
| 569 |
} |
| 570 |
|
| 571 |
uint32_t memoryIndex() const { |
| 572 |
MOZ_ASSERT(memoryIndex_ != UINT32_MAX); |
| 573 |
return memoryIndex_; |
| 574 |
} |
| 575 |
|
| 576 |
// The offset is a 64-bit value because of memory64. Almost always, it will |
| 577 |
// fit in 32 bits, and therefore offset32() is used almost everywhere in the |
| 578 |
// engine. The compiler front-ends must use offset64() to bypass the check |
| 579 |
// performed by offset32(), and must resolve offsets that don't fit in 32 bits |
| 580 |
// early in the compilation pipeline so that no large offsets are observed |
| 581 |
// later. |
| 582 |
uint32_t offset32() const { |
| 583 |
MOZ_ASSERT(offset_ <= UINT32_MAX); |
| 584 |
return uint32_t(offset_); |
| 585 |
} |
| 586 |
uint64_t offset64() const { return offset_; } |
| 587 |
|
| 588 |
// The offset can be cleared without worrying about its magnitude. |
| 589 |
void clearOffset() { offset_ = 0; } |
| 590 |
|
| 591 |
// The offset can be set (after compile-time evaluation) but only to values |
| 592 |
// that fit in 32 bits. |
| 593 |
void setOffset32(uint32_t offset) { offset_ = offset; } |
| 594 |
|
| 595 |
uint32_t align() const { return align_; } |
| 596 |
Scalar::Type type() const { return type_; } |
| 597 |
unsigned byteSize() const { return Scalar::byteSize(type()); } |
| 598 |
jit::Synchronization sync() const { return sync_; } |
| 599 |
const TrapSiteDesc& trapDesc() const { return trapDesc_; } |
| 600 |
wasm::SimdOp widenSimdOp() const { |
| 601 |
MOZ_ASSERT(isWidenSimd128Load()); |
| 602 |
return widenOp_; |
| 603 |
} |
| 604 |
bool isAtomic() const { return !sync_.isNone(); } |
| 605 |
bool isZeroExtendSimd128Load() const { return loadOp_ == ZeroExtend; } |
| 606 |
bool isSplatSimd128Load() const { return loadOp_ == Splat; } |
| 607 |
bool isWidenSimd128Load() const { return loadOp_ == Widen; } |
| 608 |
|
| 609 |
mozilla::DebugOnly<bool> isHugeMemory() const { return hugeMemory_; } |
| 610 |
#ifdef DEBUG |
| 611 |
void assertOffsetInGuardPages() const; |
| 612 |
#else |
| 613 |
void assertOffsetInGuardPages() const {} |
| 614 |
#endif |
| 615 |
|
| 616 |
void setZeroExtendSimd128Load() { |
| 617 |
MOZ_ASSERT(type() == Scalar::Float32 || type() == Scalar::Float64); |
| 618 |
MOZ_ASSERT(!isAtomic()); |
| 619 |
MOZ_ASSERT(loadOp_ == Plain); |
| 620 |
loadOp_ = ZeroExtend; |
| 621 |
} |
| 622 |
|
| 623 |
void setSplatSimd128Load() { |
| 624 |
MOZ_ASSERT(type() == Scalar::Uint8 || type() == Scalar::Uint16 || |
| 625 |
type() == Scalar::Float32 || type() == Scalar::Float64); |
| 626 |
MOZ_ASSERT(!isAtomic()); |
| 627 |
MOZ_ASSERT(loadOp_ == Plain); |
| 628 |
loadOp_ = Splat; |
| 629 |
} |
| 630 |
|
| 631 |
void setWidenSimd128Load(wasm::SimdOp op) { |
| 632 |
MOZ_ASSERT(type() == Scalar::Float64); |
| 633 |
MOZ_ASSERT(!isAtomic()); |
| 634 |
MOZ_ASSERT(loadOp_ == Plain); |
| 635 |
widenOp_ = op; |
| 636 |
loadOp_ = Widen; |
| 637 |
} |
| 638 |
}; |
| 639 |
|
| 640 |
} // namespace wasm |
| 641 |
|
| 642 |
namespace jit { |
| 643 |
|
| 644 |
// The base class of all Assemblers for all archs. |
| 645 |
class AssemblerShared { |
| 646 |
wasm::InliningContext inliningContext_; |
| 647 |
wasm::CallSites callSites_; |
| 648 |
wasm::CallSiteTargetVector callSiteTargets_; |
| 649 |
wasm::TrapSites trapSites_; |
| 650 |
wasm::SymbolicAccessVector symbolicAccesses_; |
| 651 |
wasm::TryNoteVector tryNotes_; |
| 652 |
wasm::CodeRangeUnwindInfoVector codeRangesUnwind_; |
| 653 |
wasm::CallRefMetricsPatchVector callRefMetricsPatches_; |
| 654 |
wasm::AllocSitePatchVector allocSitesPatches_; |
| 655 |
|
| 656 |
#ifdef DEBUG |
| 657 |
// To facilitate figuring out which part of SM created each instruction as |
| 658 |
// shown by IONFLAGS=codegen, this maintains a stack of (notionally) |
| 659 |
// code-creating routines, which is printed in the log output every time an |
| 660 |
// entry is pushed or popped. Do not push/pop entries directly; instead use |
| 661 |
// `class AutoCreatedBy`. |
| 662 |
mozilla::Vector<const char*> creators_; |
| 663 |
#endif |
| 664 |
|
| 665 |
protected: |
| 666 |
CodeLabelVector codeLabels_; |
| 667 |
|
| 668 |
bool enoughMemory_; |
| 669 |
bool embedsNurseryPointers_; |
| 670 |
|
| 671 |
public: |
| 672 |
AssemblerShared() : enoughMemory_(true), embedsNurseryPointers_(false) {} |
| 673 |
|
| 674 |
~AssemblerShared(); |
| 675 |
|
| 676 |
#ifdef DEBUG |
| 677 |
// Do not use these directly; instead use `class AutoCreatedBy`. |
| 678 |
void pushCreator(const char*); |
| 679 |
void popCreator(); |
| 680 |
// See comment on the implementation of `hasCreator` for guidance on what to |
| 681 |
// do if you get failures of the assertion `MOZ_ASSERT(hasCreator())`, |
| 682 |
bool hasCreator() const; |
| 683 |
#endif |
| 684 |
|
| 685 |
void propagateOOM(bool success) { enoughMemory_ &= success; } |
| 686 |
|
| 687 |
void setOOM() { enoughMemory_ = false; } |
| 688 |
|
| 689 |
bool oom() const { return !enoughMemory_; } |
| 690 |
|
| 691 |
bool embedsNurseryPointers() const { return embedsNurseryPointers_; } |
| 692 |
|
| 693 |
void addCodeLabel(CodeLabel label) { |
| 694 |
propagateOOM(codeLabels_.append(label)); |
| 695 |
} |
| 696 |
size_t numCodeLabels() const { return codeLabels_.length(); } |
| 697 |
CodeLabel codeLabel(size_t i) { return codeLabels_[i]; } |
| 698 |
CodeLabelVector& codeLabels() { return codeLabels_; } |
| 699 |
|
| 700 |
// WebAssembly metadata emitted by masm operations accumulated on the |
| 701 |
// MacroAssembler, and swapped into a wasm::CompiledCode after finish(). |
| 702 |
|
| 703 |
template <typename... Args> |
| 704 |
void append(const wasm::CallSiteDesc& desc, CodeOffset retAddr, |
| 705 |
Args&&... args) { |
| 706 |
enoughMemory_ &= callSites_.append(desc, retAddr.offset()); |
| 707 |
enoughMemory_ &= callSiteTargets_.emplaceBack(std::forward<Args>(args)...); |
| 708 |
} |
| 709 |
void append(wasm::Trap trap, wasm::TrapMachineInsn insn, uint32_t pcOffset, |
| 710 |
const wasm::TrapSiteDesc& desc) { |
| 711 |
enoughMemory_ &= trapSites_.append(trap, insn, pcOffset, desc); |
| 712 |
} |
| 713 |
void append(const wasm::MemoryAccessDesc& access, wasm::TrapMachineInsn insn, |
| 714 |
FaultingCodeOffset pcOffset) { |
| 715 |
append(wasm::Trap::OutOfBounds, insn, pcOffset.get(), access.trapDesc()); |
| 716 |
} |
| 717 |
void append(wasm::SymbolicAccess access) { |
| 718 |
enoughMemory_ &= symbolicAccesses_.append(access); |
| 719 |
} |
| 720 |
// This one returns an index as the try note so that it can be looked up |
| 721 |
// later to add the end point and stack position of the try block. |
| 722 |
[[nodiscard]] bool append(wasm::TryNote tryNote, size_t* tryNoteIndex) { |
| 723 |
if (!tryNotes_.append(tryNote)) { |
| 724 |
enoughMemory_ = false; |
| 725 |
return false; |
| 726 |
} |
| 727 |
*tryNoteIndex = tryNotes_.length() - 1; |
| 728 |
return true; |
| 729 |
} |
| 730 |
|
| 731 |
void append(wasm::CodeRangeUnwindInfo::UnwindHow unwindHow, |
| 732 |
uint32_t pcOffset) { |
| 733 |
enoughMemory_ &= codeRangesUnwind_.emplaceBack(pcOffset, unwindHow); |
| 734 |
} |
| 735 |
void append(wasm::CallRefMetricsPatch patch) { |
| 736 |
enoughMemory_ &= callRefMetricsPatches_.append(patch); |
| 737 |
} |
| 738 |
void append(wasm::AllocSitePatch patch) { |
| 739 |
enoughMemory_ &= allocSitesPatches_.append(patch); |
| 740 |
} |
| 741 |
|
| 742 |
wasm::InliningContext& inliningContext() { return inliningContext_; } |
| 743 |
wasm::CallSites& callSites() { return callSites_; } |
| 744 |
wasm::CallSiteTargetVector& callSiteTargets() { return callSiteTargets_; } |
| 745 |
wasm::TrapSites& trapSites() { return trapSites_; } |
| 746 |
wasm::SymbolicAccessVector& symbolicAccesses() { return symbolicAccesses_; } |
| 747 |
wasm::TryNoteVector& tryNotes() { return tryNotes_; } |
| 748 |
wasm::CodeRangeUnwindInfoVector& codeRangeUnwindInfos() { |
| 749 |
return codeRangesUnwind_; |
| 750 |
} |
| 751 |
wasm::CallRefMetricsPatchVector& callRefMetricsPatches() { |
| 752 |
return callRefMetricsPatches_; |
| 753 |
} |
| 754 |
wasm::AllocSitePatchVector& allocSitesPatches() { return allocSitesPatches_; } |
| 755 |
}; |
| 756 |
|
| 757 |
// AutoCreatedBy pushes and later pops a who-created-these-insns? tag into the |
| 758 |
// JitSpew_Codegen output. These could be created fairly frequently, so a |
| 759 |
// dummy inlineable-out version is provided for non-debug builds. The tag |
| 760 |
// text can be completely arbitrary -- it serves only to help readers of the |
| 761 |
// output text to relate instructions back to the part(s) of SM that created |
| 762 |
// them. |
| 763 |
#ifdef DEBUG |
| 764 |
class MOZ_RAII AutoCreatedBy { |
| 765 |
private: |
| 766 |
AssemblerShared& ash_; |
| 767 |
|
| 768 |
public: |
| 769 |
AutoCreatedBy(AssemblerShared& ash, const char* who) : ash_(ash) { |
| 770 |
ash_.pushCreator(who); |
| 771 |
} |
| 772 |
~AutoCreatedBy() { ash_.popCreator(); } |
| 773 |
}; |
| 774 |
#else |
| 775 |
class MOZ_RAII AutoCreatedBy { |
| 776 |
public: |
| 777 |
inline AutoCreatedBy(AssemblerShared& ash, const char* who) {} |
| 778 |
// A user-defined constructor is necessary to stop some compilers from |
| 779 |
// complaining about unused variables. |
| 780 |
inline ~AutoCreatedBy() {} |
| 781 |
}; |
| 782 |
#endif |
| 783 |
|
| 784 |
} // namespace jit |
| 785 |
} // namespace js |
| 786 |
|
| 787 |
#endif /* jit_shared_Assembler_shared_h */ |