| 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 |
* |
| 4 |
* Copyright 2016 Mozilla Foundation |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "wasm/WasmBCClass.h" |
| 20 |
#include "wasm/WasmBCDefs.h" |
| 21 |
#include "wasm/WasmBCRegDefs.h" |
| 22 |
#include "wasm/WasmConstants.h" |
| 23 |
#include "wasm/WasmMemory.h" |
| 24 |
|
| 25 |
#include "jit/MacroAssembler-inl.h" |
| 26 |
|
| 27 |
#include "wasm/WasmBCClass-inl.h" |
| 28 |
#include "wasm/WasmBCCodegen-inl.h" |
| 29 |
#include "wasm/WasmBCRegDefs-inl.h" |
| 30 |
#include "wasm/WasmBCRegMgmt-inl.h" |
| 31 |
#include "wasm/WasmBCStkMgmt-inl.h" |
| 32 |
|
| 33 |
namespace js { |
| 34 |
namespace wasm { |
| 35 |
|
| 36 |
using mozilla::Nothing; |
| 37 |
|
| 38 |
////////////////////////////////////////////////////////////////////////////// |
| 39 |
// |
| 40 |
// Heap access subroutines. |
| 41 |
|
| 42 |
// Bounds check elimination. |
| 43 |
// |
| 44 |
// We perform BCE on two kinds of address expressions: on constant heap pointers |
| 45 |
// that are known to be in the heap or will be handled by the out-of-bounds trap |
| 46 |
// handler; and on local variables that have been checked in dominating code |
| 47 |
// without being updated since. |
| 48 |
// |
| 49 |
// For an access through a constant heap pointer + an offset we can eliminate |
| 50 |
// the bounds check if the sum of the address and offset is below the sum of the |
| 51 |
// minimum memory length and the offset guard length. |
| 52 |
// |
| 53 |
// For an access through a local variable + an offset we can eliminate the |
| 54 |
// bounds check if the local variable has already been checked and has not been |
| 55 |
// updated since, and the offset is less than the guard limit. |
| 56 |
// |
| 57 |
// To track locals for which we can eliminate checks we use a bit vector |
| 58 |
// bceSafe_ that has a bit set for those locals whose bounds have been checked |
| 59 |
// and which have not subsequently been set. Initially this vector is zero. |
| 60 |
// |
| 61 |
// In straight-line code a bit is set when we perform a bounds check on an |
| 62 |
// access via the local and is reset when the variable is updated. |
| 63 |
// |
| 64 |
// In control flow, the bit vector is manipulated as follows. Each ControlItem |
| 65 |
// has a value bceSafeOnEntry, which is the value of bceSafe_ on entry to the |
| 66 |
// item, and a value bceSafeOnExit, which is initially ~0. On a branch (br, |
| 67 |
// brIf, brTable), we always AND the branch target's bceSafeOnExit with the |
| 68 |
// value of bceSafe_ at the branch point. On exiting an item by falling out of |
| 69 |
// it, provided we're not in dead code, we AND the current value of bceSafe_ |
| 70 |
// into the item's bceSafeOnExit. Additional processing depends on the item |
| 71 |
// type: |
| 72 |
// |
| 73 |
// - After a block, set bceSafe_ to the block's bceSafeOnExit. |
| 74 |
// |
| 75 |
// - On loop entry, after pushing the ControlItem, set bceSafe_ to zero; the |
| 76 |
// back edges would otherwise require us to iterate to a fixedpoint. |
| 77 |
// |
| 78 |
// - After a loop, the bceSafe_ is left unchanged, because only fallthrough |
| 79 |
// control flow will reach that point and the bceSafe_ value represents the |
| 80 |
// correct state of the fallthrough path. |
| 81 |
// |
| 82 |
// - Set bceSafe_ to the ControlItem's bceSafeOnEntry at both the 'then' branch |
| 83 |
// and the 'else' branch. |
| 84 |
// |
| 85 |
// - After an if-then-else, set bceSafe_ to the if-then-else's bceSafeOnExit. |
| 86 |
// |
| 87 |
// - After an if-then, set bceSafe_ to the if-then's bceSafeOnExit AND'ed with |
| 88 |
// the if-then's bceSafeOnEntry. |
| 89 |
// |
| 90 |
// Finally, when the debugger allows locals to be mutated we must disable BCE |
| 91 |
// for references via a local, by returning immediately from bceCheckLocal if |
| 92 |
// compilerEnv_.debugEnabled() is true. |
| 93 |
|
| 94 |
void BaseCompiler::bceCheckLocal(MemoryAccessDesc* access, AccessCheck* check, |
| 95 |
uint32_t local) { |
| 96 |
// We only eliminate bounds checks for memory 0 |
| 97 |
if (access->memoryIndex() != 0) { |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
if (local >= sizeof(BCESet) * 8) { |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
uint64_t offsetGuardLimit = |
| 106 |
GetMaxOffsetGuardLimit(codeMeta_.hugeMemoryEnabled(0)); |
| 107 |
|
| 108 |
if ((bceSafe_ & (BCESet(1) << local)) && |
| 109 |
access->offset64() < offsetGuardLimit) { |
| 110 |
check->omitBoundsCheck = true; |
| 111 |
} |
| 112 |
|
| 113 |
// The local becomes safe even if the offset is beyond the guard limit. |
| 114 |
bceSafe_ |= (BCESet(1) << local); |
| 115 |
} |
| 116 |
|
| 117 |
void BaseCompiler::bceLocalIsUpdated(uint32_t local) { |
| 118 |
if (local >= sizeof(BCESet) * 8) { |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
bceSafe_ &= ~(BCESet(1) << local); |
| 123 |
} |
| 124 |
|
| 125 |
// Alignment check elimination. |
| 126 |
// |
| 127 |
// Alignment checks for atomic operations can be omitted if the pointer is a |
| 128 |
// constant and the pointer + offset is aligned. Alignment checking that can't |
| 129 |
// be omitted can still be simplified by checking only the pointer if the offset |
| 130 |
// is aligned. |
| 131 |
// |
| 132 |
// (In addition, alignment checking of the pointer can be omitted if the pointer |
| 133 |
// has been checked in dominating code, but we don't do that yet.) |
| 134 |
|
| 135 |
template <> |
| 136 |
RegI32 BaseCompiler::popConstMemoryAccess<RegI32>(MemoryAccessDesc* access, |
| 137 |
AccessCheck* check) { |
| 138 |
MOZ_ASSERT(isMem32(access->memoryIndex())); |
| 139 |
|
| 140 |
int32_t addrTemp; |
| 141 |
MOZ_ALWAYS_TRUE(popConst(&addrTemp)); |
| 142 |
uint32_t addr = addrTemp; |
| 143 |
|
| 144 |
uint64_t offsetGuardLimit = GetMaxOffsetGuardLimit( |
| 145 |
codeMeta_.hugeMemoryEnabled(access->memoryIndex())); |
| 146 |
|
| 147 |
// Validation ensures that the offset is in 32-bit range, and the calculation |
| 148 |
// of the limit cannot overflow due to our choice of HugeOffsetGuardLimit. |
| 149 |
#ifdef WASM_SUPPORTS_HUGE_MEMORY |
| 150 |
static_assert(MaxMemory32PagesValidation * PageSize <= |
| 151 |
UINT64_MAX - HugeOffsetGuardLimit); |
| 152 |
#endif |
| 153 |
uint64_t ea = uint64_t(addr) + uint64_t(access->offset32()); |
| 154 |
uint64_t limit = codeMeta_.memories[access->memoryIndex()].initialLength32() + |
| 155 |
offsetGuardLimit; |
| 156 |
|
| 157 |
check->omitBoundsCheck = ea < limit; |
| 158 |
check->omitAlignmentCheck = (ea & (access->byteSize() - 1)) == 0; |
| 159 |
|
| 160 |
// Fold the offset into the pointer if we can, as this is always |
| 161 |
// beneficial. |
| 162 |
if (ea <= UINT32_MAX) { |
| 163 |
addr = uint32_t(ea); |
| 164 |
access->clearOffset(); |
| 165 |
} |
| 166 |
|
| 167 |
RegI32 r = needI32(); |
| 168 |
moveImm32(int32_t(addr), r); |
| 169 |
return r; |
| 170 |
} |
| 171 |
|
| 172 |
#ifdef ENABLE_WASM_MEMORY64 |
| 173 |
template <> |
| 174 |
RegI64 BaseCompiler::popConstMemoryAccess<RegI64>(MemoryAccessDesc* access, |
| 175 |
AccessCheck* check) { |
| 176 |
MOZ_ASSERT(isMem64(access->memoryIndex())); |
| 177 |
|
| 178 |
int64_t addrTemp; |
| 179 |
MOZ_ALWAYS_TRUE(popConst(&addrTemp)); |
| 180 |
uint64_t addr = addrTemp; |
| 181 |
|
| 182 |
uint64_t offsetGuardLimit = GetMaxOffsetGuardLimit( |
| 183 |
codeMeta_.hugeMemoryEnabled(access->memoryIndex())); |
| 184 |
|
| 185 |
mozilla::CheckedUint64 ea(addr); |
| 186 |
ea += access->offset64(); |
| 187 |
mozilla::CheckedUint64 limit( |
| 188 |
codeMeta_.memories[access->memoryIndex()].initialLength64()); |
| 189 |
limit += offsetGuardLimit; |
| 190 |
|
| 191 |
if (ea.isValid() && limit.isValid()) { |
| 192 |
check->omitBoundsCheck = ea.value() < limit.value(); |
| 193 |
check->omitAlignmentCheck = (ea.value() & (access->byteSize() - 1)) == 0; |
| 194 |
|
| 195 |
// Fold the offset into the pointer if we can, as this is always |
| 196 |
// beneficial. |
| 197 |
addr = ea.value(); |
| 198 |
access->clearOffset(); |
| 199 |
} |
| 200 |
|
| 201 |
RegI64 r = needI64(); |
| 202 |
moveImm64(int64_t(addr), r); |
| 203 |
return r; |
| 204 |
} |
| 205 |
#endif |
| 206 |
|
| 207 |
template <typename RegType> |
| 208 |
RegType BaseCompiler::popMemoryAccess(MemoryAccessDesc* access, |
| 209 |
AccessCheck* check) { |
| 210 |
check->onlyPointerAlignment = |
| 211 |
(access->offset64() & (access->byteSize() - 1)) == 0; |
| 212 |
|
| 213 |
// If there's a constant it will have the correct type for RegType. |
| 214 |
if (hasConst()) { |
| 215 |
return popConstMemoryAccess<RegType>(access, check); |
| 216 |
} |
| 217 |
|
| 218 |
// If there's a local it will have the correct type for RegType. |
| 219 |
uint32_t local; |
| 220 |
if (peekLocal(&local)) { |
| 221 |
bceCheckLocal(access, check, local); |
| 222 |
} |
| 223 |
|
| 224 |
return pop<RegType>(); |
| 225 |
} |
| 226 |
|
| 227 |
#ifdef JS_64BIT |
| 228 |
static inline RegI64 RegPtrToRegIntptr(RegPtr r) { |
| 229 |
return RegI64(Register64(Register(r))); |
| 230 |
} |
| 231 |
|
| 232 |
# ifndef WASM_HAS_HEAPREG |
| 233 |
static inline RegPtr RegIntptrToRegPtr(RegI64 r) { |
| 234 |
return RegPtr(Register64(r).reg); |
| 235 |
} |
| 236 |
# endif |
| 237 |
#else |
| 238 |
static inline RegI32 RegPtrToRegIntptr(RegPtr r) { return RegI32(Register(r)); } |
| 239 |
|
| 240 |
# ifndef WASM_HAS_HEAPREG |
| 241 |
static inline RegPtr RegIntptrToRegPtr(RegI32 r) { return RegPtr(Register(r)); } |
| 242 |
# endif |
| 243 |
#endif |
| 244 |
|
| 245 |
void BaseCompiler::pushHeapBase(uint32_t memoryIndex) { |
| 246 |
MOZ_ASSERT(memoryIndex < codeMeta_.memories.length()); |
| 247 |
|
| 248 |
RegPtr heapBase = need<RegPtr>(); |
| 249 |
|
| 250 |
#ifdef WASM_HAS_HEAPREG |
| 251 |
if (memoryIndex == 0) { |
| 252 |
move(RegPtr(HeapReg), heapBase); |
| 253 |
push(RegPtrToRegIntptr(heapBase)); |
| 254 |
return; |
| 255 |
} |
| 256 |
#endif |
| 257 |
|
| 258 |
#ifdef RABALDR_PIN_INSTANCE |
| 259 |
movePtr(RegPtr(InstanceReg), heapBase); |
| 260 |
#else |
| 261 |
fr.loadInstancePtr(heapBase); |
| 262 |
#endif |
| 263 |
|
| 264 |
uint32_t offset = instanceOffsetOfMemoryBase(memoryIndex); |
| 265 |
masm.loadPtr(Address(heapBase, offset), heapBase); |
| 266 |
push(RegPtrToRegIntptr(heapBase)); |
| 267 |
} |
| 268 |
|
| 269 |
void BaseCompiler::branchAddNoOverflow(uint64_t offset, RegI32 ptr, Label* ok) { |
| 270 |
// The invariant holds because ptr is RegI32 - this is m32. |
| 271 |
MOZ_ASSERT(offset <= UINT32_MAX); |
| 272 |
masm.branchAdd32(Assembler::CarryClear, Imm32(uint32_t(offset)), ptr, ok); |
| 273 |
} |
| 274 |
|
| 275 |
#ifdef ENABLE_WASM_MEMORY64 |
| 276 |
void BaseCompiler::branchAddNoOverflow(uint64_t offset, RegI64 ptr, Label* ok) { |
| 277 |
# if defined(JS_64BIT) |
| 278 |
masm.branchAddPtr(Assembler::CarryClear, ImmWord(offset), Register64(ptr).reg, |
| 279 |
ok); |
| 280 |
# else |
| 281 |
masm.branchAdd64(Assembler::CarryClear, Imm64(offset), ptr, ok); |
| 282 |
# endif |
| 283 |
} |
| 284 |
#endif |
| 285 |
|
| 286 |
void BaseCompiler::branchTestLowZero(RegI32 ptr, Imm32 mask, Label* ok) { |
| 287 |
masm.branchTest32(Assembler::Zero, ptr, mask, ok); |
| 288 |
} |
| 289 |
|
| 290 |
#ifdef ENABLE_WASM_MEMORY64 |
| 291 |
void BaseCompiler::branchTestLowZero(RegI64 ptr, Imm32 mask, Label* ok) { |
| 292 |
# ifdef JS_64BIT |
| 293 |
masm.branchTestPtr(Assembler::Zero, Register64(ptr).reg, mask, ok); |
| 294 |
# else |
| 295 |
masm.branchTestPtr(Assembler::Zero, ptr.low, mask, ok); |
| 296 |
# endif |
| 297 |
} |
| 298 |
#endif |
| 299 |
|
| 300 |
void BaseCompiler::boundsCheck4GBOrLargerAccess(uint32_t memoryIndex, |
| 301 |
RegPtr instance, RegI32 ptr, |
| 302 |
Label* ok) { |
| 303 |
#ifdef JS_64BIT |
| 304 |
// Extend the value to 64 bits, check the 64-bit value against the 64-bit |
| 305 |
// bound, then chop back to 32 bits. On most platform the extending and |
| 306 |
// chopping are no-ops. It's important that the value we end up with has |
| 307 |
// flowed through the Spectre mask |
| 308 |
|
| 309 |
// Note, ptr and ptr64 are the same register. |
| 310 |
RegI64 ptr64 = fromI32(ptr); |
| 311 |
|
| 312 |
// In principle there may be non-zero bits in the upper bits of the |
| 313 |
// register; clear them. |
| 314 |
# ifdef RABALDR_ZERO_EXTENDS |
| 315 |
masm.debugAssertCanonicalInt32(ptr); |
| 316 |
# else |
| 317 |
masm.move32To64ZeroExtend(ptr, ptr64); |
| 318 |
# endif |
| 319 |
|
| 320 |
boundsCheck4GBOrLargerAccess(memoryIndex, instance, ptr64, ok); |
| 321 |
|
| 322 |
// Restore the value to the canonical form for a 32-bit value in a |
| 323 |
// 64-bit register and/or the appropriate form for further use in the |
| 324 |
// indexing instruction. |
| 325 |
# ifdef RABALDR_ZERO_EXTENDS |
| 326 |
// The canonical value is zero-extended; we already have that. |
| 327 |
# else |
| 328 |
masm.move64To32(ptr64, ptr); |
| 329 |
# endif |
| 330 |
#else |
| 331 |
// No support needed, we have max 2GB heap on 32-bit |
| 332 |
MOZ_CRASH("No 32-bit support"); |
| 333 |
#endif |
| 334 |
} |
| 335 |
|
| 336 |
void BaseCompiler::boundsCheckBelow4GBAccess(uint32_t memoryIndex, |
| 337 |
RegPtr instance, RegI32 ptr, |
| 338 |
Label* ok) { |
| 339 |
// If the memory's max size is known to be smaller than 64K pages exactly, |
| 340 |
// we can use a 32-bit check and avoid extension and wrapping. |
| 341 |
masm.wasmBoundsCheck32( |
| 342 |
Assembler::Below, ptr, |
| 343 |
Address(instance, instanceOffsetOfBoundsCheckLimit(memoryIndex)), ok); |
| 344 |
} |
| 345 |
|
| 346 |
void BaseCompiler::boundsCheck4GBOrLargerAccess(uint32_t memoryIndex, |
| 347 |
RegPtr instance, RegI64 ptr, |
| 348 |
Label* ok) { |
| 349 |
// Any Spectre mitigation will appear to update the ptr64 register. |
| 350 |
masm.wasmBoundsCheck64( |
| 351 |
Assembler::Below, ptr, |
| 352 |
Address(instance, instanceOffsetOfBoundsCheckLimit(memoryIndex)), ok); |
| 353 |
} |
| 354 |
|
| 355 |
void BaseCompiler::boundsCheckBelow4GBAccess(uint32_t memoryIndex, |
| 356 |
RegPtr instance, RegI64 ptr, |
| 357 |
Label* ok) { |
| 358 |
// The bounds check limit is valid to 64 bits, so there's no sense in doing |
| 359 |
// anything complicated here. There may be optimization paths here in the |
| 360 |
// future and they may differ on 32-bit and 64-bit. |
| 361 |
boundsCheck4GBOrLargerAccess(memoryIndex, instance, ptr, ok); |
| 362 |
} |
| 363 |
|
| 364 |
// Make sure the ptr could be used as an index register. |
| 365 |
static inline void ToValidIndex(MacroAssembler& masm, RegI32 ptr) { |
| 366 |
#if defined(JS_CODEGEN_MIPS64) || defined(JS_CODEGEN_LOONG64) || \ |
| 367 |
defined(JS_CODEGEN_RISCV64) |
| 368 |
// When ptr is used as an index, it will be added to a 64-bit register. |
| 369 |
// So we should explicitly promote ptr to 64-bit. Since now ptr holds a |
| 370 |
// unsigned 32-bit value, we zero-extend it to 64-bit here. |
| 371 |
masm.move32To64ZeroExtend(ptr, Register64(ptr)); |
| 372 |
#endif |
| 373 |
} |
| 374 |
|
| 375 |
#if defined(ENABLE_WASM_MEMORY64) |
| 376 |
static inline void ToValidIndex(MacroAssembler& masm, RegI64 ptr) {} |
| 377 |
#endif |
| 378 |
|
| 379 |
// RegAddressType is RegI32 for Memory32 and RegI64 for Memory64. |
| 380 |
template <typename RegAddressType> |
| 381 |
void BaseCompiler::prepareMemoryAccess(MemoryAccessDesc* access, |
| 382 |
AccessCheck* check, RegPtr instance, |
| 383 |
RegAddressType ptr) { |
| 384 |
uint64_t offsetGuardLimit = GetMaxOffsetGuardLimit( |
| 385 |
codeMeta_.hugeMemoryEnabled(access->memoryIndex())); |
| 386 |
|
| 387 |
// Fold offset if necessary for further computations. |
| 388 |
if (access->offset64() >= offsetGuardLimit || |
| 389 |
access->offset64() > UINT32_MAX || |
| 390 |
(access->isAtomic() && !check->omitAlignmentCheck && |
| 391 |
!check->onlyPointerAlignment)) { |
| 392 |
Label ok; |
| 393 |
branchAddNoOverflow(access->offset64(), ptr, &ok); |
| 394 |
trap(Trap::OutOfBounds); |
| 395 |
masm.bind(&ok); |
| 396 |
access->clearOffset(); |
| 397 |
check->onlyPointerAlignment = true; |
| 398 |
} |
| 399 |
|
| 400 |
// Alignment check if required. |
| 401 |
|
| 402 |
if (access->isAtomic() && !check->omitAlignmentCheck) { |
| 403 |
MOZ_ASSERT(check->onlyPointerAlignment); |
| 404 |
// We only care about the low pointer bits here. |
| 405 |
Label ok; |
| 406 |
branchTestLowZero(ptr, Imm32(access->byteSize() - 1), &ok); |
| 407 |
trap(Trap::UnalignedAccess); |
| 408 |
masm.bind(&ok); |
| 409 |
} |
| 410 |
|
| 411 |
// Ensure no instance if we don't need it. |
| 412 |
|
| 413 |
if (codeMeta_.hugeMemoryEnabled(access->memoryIndex()) && |
| 414 |
access->memoryIndex() == 0) { |
| 415 |
// We have HeapReg and no bounds checking and need load neither |
| 416 |
// memoryBase nor boundsCheckLimit from instance. |
| 417 |
MOZ_ASSERT_IF(check->omitBoundsCheck, instance.isInvalid()); |
| 418 |
} |
| 419 |
#ifdef WASM_HAS_HEAPREG |
| 420 |
// We have HeapReg and don't need to load the memoryBase from instance. |
| 421 |
MOZ_ASSERT_IF(check->omitBoundsCheck && access->memoryIndex() == 0, |
| 422 |
instance.isInvalid()); |
| 423 |
#endif |
| 424 |
|
| 425 |
// Bounds check if required. |
| 426 |
|
| 427 |
if (!codeMeta_.hugeMemoryEnabled(access->memoryIndex()) && |
| 428 |
!check->omitBoundsCheck) { |
| 429 |
Label ok; |
| 430 |
#ifdef JS_64BIT |
| 431 |
// The checking depends on how many bits are in the pointer and how many |
| 432 |
// bits are in the bound. |
| 433 |
static_assert(0x100000000 % PageSize == 0); |
| 434 |
if (!codeMeta_.memories[access->memoryIndex()].boundsCheckLimitIs32Bits() && |
| 435 |
MaxMemoryPages( |
| 436 |
codeMeta_.memories[access->memoryIndex()].addressType()) >= |
| 437 |
Pages(0x100000000 / PageSize)) { |
| 438 |
boundsCheck4GBOrLargerAccess(access->memoryIndex(), instance, ptr, &ok); |
| 439 |
} else { |
| 440 |
boundsCheckBelow4GBAccess(access->memoryIndex(), instance, ptr, &ok); |
| 441 |
} |
| 442 |
#else |
| 443 |
boundsCheckBelow4GBAccess(access->memoryIndex(), instance, ptr, &ok); |
| 444 |
#endif |
| 445 |
trap(Trap::OutOfBounds); |
| 446 |
masm.bind(&ok); |
| 447 |
} |
| 448 |
|
| 449 |
ToValidIndex(masm, ptr); |
| 450 |
} |
| 451 |
|
| 452 |
template <typename RegAddressType> |
| 453 |
void BaseCompiler::computeEffectiveAddress(MemoryAccessDesc* access) { |
| 454 |
if (access->offset64()) { |
| 455 |
Label ok; |
| 456 |
RegAddressType ptr = pop<RegAddressType>(); |
| 457 |
branchAddNoOverflow(access->offset64(), ptr, &ok); |
| 458 |
trap(Trap::OutOfBounds); |
| 459 |
masm.bind(&ok); |
| 460 |
access->clearOffset(); |
| 461 |
push(ptr); |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
RegPtr BaseCompiler::maybeLoadMemoryBaseForAccess( |
| 466 |
RegPtr instance, const MemoryAccessDesc* access) { |
| 467 |
#ifdef JS_CODEGEN_X86 |
| 468 |
// x86 adds the memory base to the wasm pointer directly using an addressing |
| 469 |
// mode and doesn't require the memory base to be loaded to a register. |
| 470 |
return RegPtr(); |
| 471 |
#endif |
| 472 |
|
| 473 |
#ifdef WASM_HAS_HEAPREG |
| 474 |
if (access->memoryIndex() == 0) { |
| 475 |
return RegPtr(HeapReg); |
| 476 |
} |
| 477 |
#endif |
| 478 |
RegPtr memoryBase = needPtr(); |
| 479 |
uint32_t offset = instanceOffsetOfMemoryBase(access->memoryIndex()); |
| 480 |
masm.loadPtr(Address(instance, offset), memoryBase); |
| 481 |
return memoryBase; |
| 482 |
} |
| 483 |
|
| 484 |
bool BaseCompiler::needInstanceForAccess(const MemoryAccessDesc* access, |
| 485 |
const AccessCheck& check) { |
| 486 |
#ifndef WASM_HAS_HEAPREG |
| 487 |
// Platform requires instance for memory base. |
| 488 |
return true; |
| 489 |
#else |
| 490 |
if (access->memoryIndex() != 0) { |
| 491 |
// Need instance to load the memory base |
| 492 |
return true; |
| 493 |
} |
| 494 |
return !codeMeta_.hugeMemoryEnabled(access->memoryIndex()) && |
| 495 |
!check.omitBoundsCheck; |
| 496 |
#endif |
| 497 |
} |
| 498 |
|
| 499 |
RegPtr BaseCompiler::maybeLoadInstanceForAccess(const MemoryAccessDesc* access, |
| 500 |
const AccessCheck& check) { |
| 501 |
if (needInstanceForAccess(access, check)) { |
| 502 |
#ifdef RABALDR_PIN_INSTANCE |
| 503 |
// NOTE, returning InstanceReg here depends for correctness on *ALL* |
| 504 |
// clients not attempting to free this register and not push it on the value |
| 505 |
// stack. |
| 506 |
// |
| 507 |
// We have assertions in place to guard against that, so the risk of the |
| 508 |
// leaky abstraction is acceptable. performRegisterLeakCheck() will ensure |
| 509 |
// that after every bytecode, the union of available registers from the |
| 510 |
// regalloc and used registers from the stack equals the set of allocatable |
| 511 |
// registers at startup. Thus if the instance is freed incorrectly it will |
| 512 |
// end up in that union via the regalloc, and if it is pushed incorrectly it |
| 513 |
// will end up in the union via the stack. |
| 514 |
return RegPtr(InstanceReg); |
| 515 |
#else |
| 516 |
RegPtr instance = need<RegPtr>(); |
| 517 |
fr.loadInstancePtr(instance); |
| 518 |
return instance; |
| 519 |
#endif |
| 520 |
} |
| 521 |
return RegPtr::Invalid(); |
| 522 |
} |
| 523 |
|
| 524 |
RegPtr BaseCompiler::maybeLoadInstanceForAccess(const MemoryAccessDesc* access, |
| 525 |
const AccessCheck& check, |
| 526 |
RegPtr specific) { |
| 527 |
if (needInstanceForAccess(access, check)) { |
| 528 |
#ifdef RABALDR_PIN_INSTANCE |
| 529 |
movePtr(RegPtr(InstanceReg), specific); |
| 530 |
#else |
| 531 |
fr.loadInstancePtr(specific); |
| 532 |
#endif |
| 533 |
return specific; |
| 534 |
} |
| 535 |
return RegPtr::Invalid(); |
| 536 |
} |
| 537 |
|
| 538 |
////////////////////////////////////////////////////////////////////////////// |
| 539 |
// |
| 540 |
// Load and store. |
| 541 |
|
| 542 |
void BaseCompiler::executeLoad(MemoryAccessDesc* access, AccessCheck* check, |
| 543 |
RegPtr instance, RegPtr memoryBase, RegI32 ptr, |
| 544 |
AnyReg dest, RegI32 temp) { |
| 545 |
// Emit the load. At this point, 64-bit offsets will have been folded away by |
| 546 |
// prepareMemoryAccess. |
| 547 |
#if defined(JS_CODEGEN_X64) |
| 548 |
MOZ_ASSERT(temp.isInvalid()); |
| 549 |
Operand srcAddr(memoryBase, ptr, TimesOne, access->offset32()); |
| 550 |
|
| 551 |
if (dest.tag == AnyReg::I64) { |
| 552 |
masm.wasmLoadI64(*access, srcAddr, dest.i64()); |
| 553 |
} else { |
| 554 |
masm.wasmLoad(*access, srcAddr, dest.any()); |
| 555 |
} |
| 556 |
#elif defined(JS_CODEGEN_X86) |
| 557 |
MOZ_ASSERT(memoryBase.isInvalid() && temp.isInvalid()); |
| 558 |
masm.addPtr( |
| 559 |
Address(instance, instanceOffsetOfMemoryBase(access->memoryIndex())), |
| 560 |
ptr); |
| 561 |
Operand srcAddr(ptr, access->offset32()); |
| 562 |
|
| 563 |
if (dest.tag == AnyReg::I64) { |
| 564 |
MOZ_ASSERT(dest.i64() == specific_.abiReturnRegI64); |
| 565 |
masm.wasmLoadI64(*access, srcAddr, dest.i64()); |
| 566 |
} else { |
| 567 |
// For 8 bit loads, this will generate movsbl or movzbl, so |
| 568 |
// there's no constraint on what the output register may be. |
| 569 |
masm.wasmLoad(*access, srcAddr, dest.any()); |
| 570 |
} |
| 571 |
#elif defined(JS_CODEGEN_MIPS64) |
| 572 |
if (IsUnaligned(*access)) { |
| 573 |
switch (dest.tag) { |
| 574 |
case AnyReg::I64: |
| 575 |
masm.wasmUnalignedLoadI64(*access, memoryBase, ptr, ptr, dest.i64(), |
| 576 |
temp); |
| 577 |
break; |
| 578 |
case AnyReg::F32: |
| 579 |
masm.wasmUnalignedLoadFP(*access, memoryBase, ptr, ptr, dest.f32(), |
| 580 |
temp); |
| 581 |
break; |
| 582 |
case AnyReg::F64: |
| 583 |
masm.wasmUnalignedLoadFP(*access, memoryBase, ptr, ptr, dest.f64(), |
| 584 |
temp); |
| 585 |
break; |
| 586 |
case AnyReg::I32: |
| 587 |
masm.wasmUnalignedLoad(*access, memoryBase, ptr, ptr, dest.i32(), temp); |
| 588 |
break; |
| 589 |
default: |
| 590 |
MOZ_CRASH("Unexpected type"); |
| 591 |
} |
| 592 |
} else { |
| 593 |
if (dest.tag == AnyReg::I64) { |
| 594 |
masm.wasmLoadI64(*access, memoryBase, ptr, ptr, dest.i64()); |
| 595 |
} else { |
| 596 |
masm.wasmLoad(*access, memoryBase, ptr, ptr, dest.any()); |
| 597 |
} |
| 598 |
} |
| 599 |
#elif defined(JS_CODEGEN_ARM) |
| 600 |
MOZ_ASSERT(temp.isInvalid()); |
| 601 |
if (dest.tag == AnyReg::I64) { |
| 602 |
masm.wasmLoadI64(*access, memoryBase, ptr, ptr, dest.i64()); |
| 603 |
} else { |
| 604 |
masm.wasmLoad(*access, memoryBase, ptr, ptr, dest.any()); |
| 605 |
} |
| 606 |
#elif defined(JS_CODEGEN_ARM64) |
| 607 |
MOZ_ASSERT(temp.isInvalid()); |
| 608 |
if (dest.tag == AnyReg::I64) { |
| 609 |
masm.wasmLoadI64(*access, memoryBase, ptr, dest.i64()); |
| 610 |
} else { |
| 611 |
masm.wasmLoad(*access, memoryBase, ptr, dest.any()); |
| 612 |
} |
| 613 |
#elif defined(JS_CODEGEN_LOONG64) |
| 614 |
MOZ_ASSERT(temp.isInvalid()); |
| 615 |
if (dest.tag == AnyReg::I64) { |
| 616 |
masm.wasmLoadI64(*access, memoryBase, ptr, ptr, dest.i64()); |
| 617 |
} else { |
| 618 |
masm.wasmLoad(*access, memoryBase, ptr, ptr, dest.any()); |
| 619 |
} |
| 620 |
#elif defined(JS_CODEGEN_RISCV64) |
| 621 |
MOZ_ASSERT(temp.isInvalid()); |
| 622 |
if (dest.tag == AnyReg::I64) { |
| 623 |
masm.wasmLoadI64(*access, memoryBase, ptr, ptr, dest.i64()); |
| 624 |
} else { |
| 625 |
masm.wasmLoad(*access, memoryBase, ptr, ptr, dest.any()); |
| 626 |
} |
| 627 |
#else |
| 628 |
MOZ_CRASH("BaseCompiler platform hook: load"); |
| 629 |
#endif |
| 630 |
} |
| 631 |
|
| 632 |
// ptr and dest may be the same iff dest is I32. |
| 633 |
// This may destroy ptr even if ptr and dest are not the same. |
| 634 |
void BaseCompiler::load(MemoryAccessDesc* access, AccessCheck* check, |
| 635 |
RegPtr instance, RegPtr memoryBase, RegI32 ptr, |
| 636 |
AnyReg dest, RegI32 temp) { |
| 637 |
prepareMemoryAccess(access, check, instance, ptr); |
| 638 |
executeLoad(access, check, instance, memoryBase, ptr, dest, temp); |
| 639 |
} |
| 640 |
|
| 641 |
#ifdef ENABLE_WASM_MEMORY64 |
| 642 |
void BaseCompiler::load(MemoryAccessDesc* access, AccessCheck* check, |
| 643 |
RegPtr instance, RegPtr memoryBase, RegI64 ptr, |
| 644 |
AnyReg dest, RegI64 temp) { |
| 645 |
prepareMemoryAccess(access, check, instance, ptr); |
| 646 |
|
| 647 |
# if !defined(JS_64BIT) |
| 648 |
// On 32-bit systems we have a maximum 2GB heap and bounds checking has |
| 649 |
// been applied to ensure that the 64-bit pointer is valid. |
| 650 |
return executeLoad(access, check, instance, memoryBase, RegI32(ptr.low), dest, |
| 651 |
maybeFromI64(temp)); |
| 652 |
# elif defined(JS_CODEGEN_X64) || defined(JS_CODEGEN_ARM64) |
| 653 |
// On x64 and arm64 the 32-bit code simply assumes that the high bits of the |
| 654 |
// 64-bit pointer register are zero and performs a 64-bit add. Thus the code |
| 655 |
// generated is the same for the 64-bit and the 32-bit case. |
| 656 |
return executeLoad(access, check, instance, memoryBase, RegI32(ptr.reg), dest, |
| 657 |
maybeFromI64(temp)); |
| 658 |
# elif defined(JS_CODEGEN_MIPS64) || defined(JS_CODEGEN_LOONG64) |
| 659 |
// On mips64 and loongarch64, the 'prepareMemoryAccess' function will make |
| 660 |
// sure that ptr holds a valid 64-bit index value. Thus the code generated in |
| 661 |
// 'executeLoad' is the same for the 64-bit and the 32-bit case. |
| 662 |
return executeLoad(access, check, instance, memoryBase, RegI32(ptr.reg), dest, |
| 663 |
maybeFromI64(temp)); |
| 664 |
# elif defined(JS_CODEGEN_RISCV64) |
| 665 |
// RISCV the 'prepareMemoryAccess' function will make |
| 666 |
// sure that ptr holds a valid 64-bit index value. Thus the code generated in |
| 667 |
// 'executeLoad' is the same for the 64-bit and the 32-bit case. |
| 668 |
return executeLoad(access, check, instance, memoryBase, RegI32(ptr.reg), dest, |
| 669 |
maybeFromI64(temp)); |
| 670 |
# else |
| 671 |
MOZ_CRASH("Missing platform hook"); |
| 672 |
# endif |
| 673 |
} |
| 674 |
#endif |
| 675 |
|
| 676 |
void BaseCompiler::executeStore(MemoryAccessDesc* access, AccessCheck* check, |
| 677 |
RegPtr instance, RegPtr memoryBase, RegI32 ptr, |
| 678 |
AnyReg src, RegI32 temp) { |
| 679 |
// Emit the store. At this point, 64-bit offsets will have been folded away by |
| 680 |
// prepareMemoryAccess. |
| 681 |
#if defined(JS_CODEGEN_X64) |
| 682 |
MOZ_ASSERT(temp.isInvalid()); |
| 683 |
Operand dstAddr(memoryBase, ptr, TimesOne, access->offset32()); |
| 684 |
|
| 685 |
masm.wasmStore(*access, src.any(), dstAddr); |
| 686 |
#elif defined(JS_CODEGEN_X86) |
| 687 |
MOZ_ASSERT(memoryBase.isInvalid() && temp.isInvalid()); |
| 688 |
masm.addPtr( |
| 689 |
Address(instance, instanceOffsetOfMemoryBase(access->memoryIndex())), |
| 690 |
ptr); |
| 691 |
Operand dstAddr(ptr, access->offset32()); |
| 692 |
|
| 693 |
if (access->type() == Scalar::Int64) { |
| 694 |
masm.wasmStoreI64(*access, src.i64(), dstAddr); |
| 695 |
} else { |
| 696 |
AnyRegister value; |
| 697 |
ScratchI8 scratch(*this); |
| 698 |
if (src.tag == AnyReg::I64) { |
| 699 |
if (access->byteSize() == 1 && !ra.isSingleByteI32(src.i64().low)) { |
| 700 |
masm.mov(src.i64().low, scratch); |
| 701 |
value = AnyRegister(scratch); |
| 702 |
} else { |
| 703 |
value = AnyRegister(src.i64().low); |
| 704 |
} |
| 705 |
} else if (access->byteSize() == 1 && !ra.isSingleByteI32(src.i32())) { |
| 706 |
masm.mov(src.i32(), scratch); |
| 707 |
value = AnyRegister(scratch); |
| 708 |
} else { |
| 709 |
value = src.any(); |
| 710 |
} |
| 711 |
|
| 712 |
masm.wasmStore(*access, value, dstAddr); |
| 713 |
} |
| 714 |
#elif defined(JS_CODEGEN_ARM) |
| 715 |
MOZ_ASSERT(temp.isInvalid()); |
| 716 |
if (access->type() == Scalar::Int64) { |
| 717 |
masm.wasmStoreI64(*access, src.i64(), memoryBase, ptr, ptr); |
| 718 |
} else if (src.tag == AnyReg::I64) { |
| 719 |
masm.wasmStore(*access, AnyRegister(src.i64().low), memoryBase, ptr, ptr); |
| 720 |
} else { |
| 721 |
masm.wasmStore(*access, src.any(), memoryBase, ptr, ptr); |
| 722 |
} |
| 723 |
#elif defined(JS_CODEGEN_MIPS64) |
| 724 |
if (IsUnaligned(*access)) { |
| 725 |
switch (src.tag) { |
| 726 |
case AnyReg::I64: |
| 727 |
masm.wasmUnalignedStoreI64(*access, src.i64(), memoryBase, ptr, ptr, |
| 728 |
temp); |
| 729 |
break; |
| 730 |
case AnyReg::F32: |
| 731 |
masm.wasmUnalignedStoreFP(*access, src.f32(), memoryBase, ptr, ptr, |
| 732 |
temp); |
| 733 |
break; |
| 734 |
case AnyReg::F64: |
| 735 |
masm.wasmUnalignedStoreFP(*access, src.f64(), memoryBase, ptr, ptr, |
| 736 |
temp); |
| 737 |
break; |
| 738 |
case AnyReg::I32: |
| 739 |
masm.wasmUnalignedStore(*access, src.i32(), memoryBase, ptr, ptr, temp); |
| 740 |
break; |
| 741 |
default: |
| 742 |
MOZ_CRASH("Unexpected type"); |
| 743 |
} |
| 744 |
} else { |
| 745 |
if (src.tag == AnyReg::I64) { |
| 746 |
masm.wasmStoreI64(*access, src.i64(), memoryBase, ptr, ptr); |
| 747 |
} else { |
| 748 |
masm.wasmStore(*access, src.any(), memoryBase, ptr, ptr); |
| 749 |
} |
| 750 |
} |
| 751 |
#elif defined(JS_CODEGEN_ARM64) |
| 752 |
MOZ_ASSERT(temp.isInvalid()); |
| 753 |
if (access->type() == Scalar::Int64) { |
| 754 |
masm.wasmStoreI64(*access, src.i64(), memoryBase, ptr); |
| 755 |
} else { |
| 756 |
masm.wasmStore(*access, src.any(), memoryBase, ptr); |
| 757 |
} |
| 758 |
#elif defined(JS_CODEGEN_LOONG64) |
| 759 |
MOZ_ASSERT(temp.isInvalid()); |
| 760 |
if (access->type() == Scalar::Int64) { |
| 761 |
masm.wasmStoreI64(*access, src.i64(), memoryBase, ptr, ptr); |
| 762 |
} else { |
| 763 |
masm.wasmStore(*access, src.any(), memoryBase, ptr, ptr); |
| 764 |
} |
| 765 |
#elif defined(JS_CODEGEN_RISCV64) |
| 766 |
MOZ_ASSERT(temp.isInvalid()); |
| 767 |
if (access->type() == Scalar::Int64) { |
| 768 |
masm.wasmStoreI64(*access, src.i64(), memoryBase, ptr, ptr); |
| 769 |
} else { |
| 770 |
masm.wasmStore(*access, src.any(), memoryBase, ptr, ptr); |
| 771 |
} |
| 772 |
#else |
| 773 |
MOZ_CRASH("BaseCompiler platform hook: store"); |
| 774 |
#endif |
| 775 |
} |
| 776 |
|
| 777 |
// ptr and src must not be the same register. |
| 778 |
// This may destroy ptr and src. |
| 779 |
void BaseCompiler::store(MemoryAccessDesc* access, AccessCheck* check, |
| 780 |
RegPtr instance, RegPtr memoryBase, RegI32 ptr, |
| 781 |
AnyReg src, RegI32 temp) { |
| 782 |
prepareMemoryAccess(access, check, instance, ptr); |
| 783 |
executeStore(access, check, instance, memoryBase, ptr, src, temp); |
| 784 |
} |
| 785 |
|
| 786 |
#ifdef ENABLE_WASM_MEMORY64 |
| 787 |
void BaseCompiler::store(MemoryAccessDesc* access, AccessCheck* check, |
| 788 |
RegPtr instance, RegPtr memoryBase, RegI64 ptr, |
| 789 |
AnyReg src, RegI64 temp) { |
| 790 |
prepareMemoryAccess(access, check, instance, ptr); |
| 791 |
// See comments in load() |
| 792 |
# if !defined(JS_64BIT) |
| 793 |
return executeStore(access, check, instance, memoryBase, RegI32(ptr.low), src, |
| 794 |
maybeFromI64(temp)); |
| 795 |
# elif defined(JS_CODEGEN_X64) || defined(JS_CODEGEN_ARM64) || \ |
| 796 |
defined(JS_CODEGEN_MIPS64) || defined(JS_CODEGEN_LOONG64) || \ |
| 797 |
defined(JS_CODEGEN_RISCV64) |
| 798 |
return executeStore(access, check, instance, memoryBase, RegI32(ptr.reg), src, |
| 799 |
maybeFromI64(temp)); |
| 800 |
# else |
| 801 |
MOZ_CRASH("Missing platform hook"); |
| 802 |
# endif |
| 803 |
} |
| 804 |
#endif |
| 805 |
|
| 806 |
template <typename RegType> |
| 807 |
void BaseCompiler::doLoadCommon(MemoryAccessDesc* access, AccessCheck check, |
| 808 |
ValType type) { |
| 809 |
RegPtr instance; |
| 810 |
RegPtr memoryBase; |
| 811 |
RegType temp; |
| 812 |
#if defined(JS_CODEGEN_MIPS64) |
| 813 |
temp = need<RegType>(); |
| 814 |
#endif |
| 815 |
|
| 816 |
switch (type.kind()) { |
| 817 |
case ValType::I32: { |
| 818 |
RegType rp = popMemoryAccess<RegType>(access, &check); |
| 819 |
RegI32 rv = needI32(); |
| 820 |
instance = maybeLoadInstanceForAccess(access, check); |
| 821 |
memoryBase = maybeLoadMemoryBaseForAccess(instance, access); |
| 822 |
load(access, &check, instance, memoryBase, rp, AnyReg(rv), temp); |
| 823 |
push(rv); |
| 824 |
free(rp); |
| 825 |
break; |
| 826 |
} |
| 827 |
case ValType::I64: { |
| 828 |
RegI64 rv; |
| 829 |
RegType rp; |
| 830 |
#ifdef JS_CODEGEN_X86 |
| 831 |
rv = specific_.abiReturnRegI64; |
| 832 |
needI64(rv); |
| 833 |
rp = popMemoryAccess<RegType>(access, &check); |
| 834 |
#else |
| 835 |
rp = popMemoryAccess<RegType>(access, &check); |
| 836 |
rv = needI64(); |
| 837 |
#endif |
| 838 |
instance = maybeLoadInstanceForAccess(access, check); |
| 839 |
memoryBase = maybeLoadMemoryBaseForAccess(instance, access); |
| 840 |
load(access, &check, instance, memoryBase, rp, AnyReg(rv), temp); |
| 841 |
push(rv); |
| 842 |
free(rp); |
| 843 |
break; |
| 844 |
} |
| 845 |
case ValType::F32: { |
| 846 |
RegType rp = popMemoryAccess<RegType>(access, &check); |
| 847 |
RegF32 rv = needF32(); |
| 848 |
instance = maybeLoadInstanceForAccess(access, check); |
| 849 |
memoryBase = maybeLoadMemoryBaseForAccess(instance, access); |
| 850 |
load(access, &check, instance, memoryBase, rp, AnyReg(rv), temp); |
| 851 |
push(rv); |
| 852 |
free(rp); |
| 853 |
break; |
| 854 |
} |
| 855 |
case ValType::F64: { |
| 856 |
RegType rp = popMemoryAccess<RegType>(access, &check); |
| 857 |
RegF64 rv = needF64(); |
| 858 |
instance = maybeLoadInstanceForAccess(access, check); |
| 859 |
memoryBase = maybeLoadMemoryBaseForAccess(instance, access); |
| 860 |
load(access, &check, instance, memoryBase, rp, AnyReg(rv), temp); |
| 861 |
push(rv); |
| 862 |
free(rp); |
| 863 |
break; |
| 864 |
} |
| 865 |
#ifdef ENABLE_WASM_SIMD |
| 866 |
case ValType::V128: { |
| 867 |
RegType rp = popMemoryAccess<RegType>(access, &check); |
| 868 |
RegV128 rv = needV128(); |
| 869 |
instance = maybeLoadInstanceForAccess(access, check); |
| 870 |
memoryBase = maybeLoadMemoryBaseForAccess(instance, access); |
| 871 |
load(access, &check, instance, memoryBase, rp, AnyReg(rv), temp); |
| 872 |
push(rv); |
| 873 |
free(rp); |
| 874 |
break; |
| 875 |
} |
| 876 |
#endif |
| 877 |
default: |
| 878 |
MOZ_CRASH("load type"); |
| 879 |
break; |
| 880 |
} |
| 881 |
|
| 882 |
#ifndef RABALDR_PIN_INSTANCE |
| 883 |
maybeFree(instance); |
| 884 |
#endif |
| 885 |
#ifdef WASM_HAS_HEAPREG |
| 886 |
if (memoryBase != HeapReg) { |
| 887 |
maybeFree(memoryBase); |
| 888 |
} |
| 889 |
#else |
| 890 |
maybeFree(memoryBase); |
| 891 |
#endif |
| 892 |
maybeFree(temp); |
| 893 |
} |
| 894 |
|
| 895 |
void BaseCompiler::loadCommon(MemoryAccessDesc* access, AccessCheck check, |
| 896 |
ValType type) { |
| 897 |
if (isMem32(access->memoryIndex())) { |
| 898 |
doLoadCommon<RegI32>(access, check, type); |
| 899 |
} else { |
| 900 |
#ifdef ENABLE_WASM_MEMORY64 |
| 901 |
doLoadCommon<RegI64>(access, check, type); |
| 902 |
#else |
| 903 |
MOZ_CRASH("Memory64 not enabled / supported on this platform"); |
| 904 |
#endif |
| 905 |
} |
| 906 |
} |
| 907 |
|
| 908 |
template <typename RegType> |
| 909 |
void BaseCompiler::doStoreCommon(MemoryAccessDesc* access, AccessCheck check, |
| 910 |
ValType resultType) { |
| 911 |
RegPtr instance; |
| 912 |
RegPtr memoryBase; |
| 913 |
RegType temp; |
| 914 |
#if defined(JS_CODEGEN_MIPS64) |
| 915 |
temp = need<RegType>(); |
| 916 |
#endif |
| 917 |
|
| 918 |
switch (resultType.kind()) { |
| 919 |
case ValType::I32: { |
| 920 |
RegI32 rv = popI32(); |
| 921 |
RegType rp = popMemoryAccess<RegType>(access, &check); |
| 922 |
instance = maybeLoadInstanceForAccess(access, check); |
| 923 |
memoryBase = maybeLoadMemoryBaseForAccess(instance, access); |
| 924 |
store(access, &check, instance, memoryBase, rp, AnyReg(rv), temp); |
| 925 |
free(rp); |
| 926 |
free(rv); |
| 927 |
break; |
| 928 |
} |
| 929 |
case ValType::I64: { |
| 930 |
RegI64 rv = popI64(); |
| 931 |
RegType rp = popMemoryAccess<RegType>(access, &check); |
| 932 |
instance = maybeLoadInstanceForAccess(access, check); |
| 933 |
memoryBase = maybeLoadMemoryBaseForAccess(instance, access); |
| 934 |
store(access, &check, instance, memoryBase, rp, AnyReg(rv), temp); |
| 935 |
free(rp); |
| 936 |
free(rv); |
| 937 |
break; |
| 938 |
} |
| 939 |
case ValType::F32: { |
| 940 |
RegF32 rv = popF32(); |
| 941 |
RegType rp = popMemoryAccess<RegType>(access, &check); |
| 942 |
instance = maybeLoadInstanceForAccess(access, check); |
| 943 |
memoryBase = maybeLoadMemoryBaseForAccess(instance, access); |
| 944 |
store(access, &check, instance, memoryBase, rp, AnyReg(rv), temp); |
| 945 |
free(rp); |
| 946 |
free(rv); |
| 947 |
break; |
| 948 |
} |
| 949 |
case ValType::F64: { |
| 950 |
RegF64 rv = popF64(); |
| 951 |
RegType rp = popMemoryAccess<RegType>(access, &check); |
| 952 |
instance = maybeLoadInstanceForAccess(access, check); |
| 953 |
memoryBase = maybeLoadMemoryBaseForAccess(instance, access); |
| 954 |
store(access, &check, instance, memoryBase, rp, AnyReg(rv), temp); |
| 955 |
free(rp); |
| 956 |
free(rv); |
| 957 |
break; |
| 958 |
} |
| 959 |
#ifdef ENABLE_WASM_SIMD |
| 960 |
case ValType::V128: { |
| 961 |
RegV128 rv = popV128(); |
| 962 |
RegType rp = popMemoryAccess<RegType>(access, &check); |
| 963 |
instance = maybeLoadInstanceForAccess(access, check); |
| 964 |
memoryBase = maybeLoadMemoryBaseForAccess(instance, access); |
| 965 |
store(access, &check, instance, memoryBase, rp, AnyReg(rv), temp); |
| 966 |
free(rp); |
| 967 |
free(rv); |
| 968 |
break; |
| 969 |
} |
| 970 |
#endif |
| 971 |
default: |
| 972 |
MOZ_CRASH("store type"); |
| 973 |
break; |
| 974 |
} |
| 975 |
|
| 976 |
#ifndef RABALDR_PIN_INSTANCE |
| 977 |
maybeFree(instance); |
| 978 |
#endif |
| 979 |
#ifdef WASM_HAS_HEAPREG |
| 980 |
if (memoryBase != HeapReg) { |
| 981 |
maybeFree(memoryBase); |
| 982 |
} |
| 983 |
#else |
| 984 |
maybeFree(memoryBase); |
| 985 |
#endif |
| 986 |
maybeFree(temp); |
| 987 |
} |
| 988 |
|
| 989 |
void BaseCompiler::storeCommon(MemoryAccessDesc* access, AccessCheck check, |
| 990 |
ValType type) { |
| 991 |
if (isMem32(access->memoryIndex())) { |
| 992 |
doStoreCommon<RegI32>(access, check, type); |
| 993 |
} else { |
| 994 |
#ifdef ENABLE_WASM_MEMORY64 |
| 995 |
doStoreCommon<RegI64>(access, check, type); |
| 996 |
#else |
| 997 |
MOZ_CRASH("Memory64 not enabled / supported on this platform"); |
| 998 |
#endif |
| 999 |
} |
| 1000 |
} |
| 1001 |
|
| 1002 |
// Convert something that may contain a heap index into a Register that can be |
| 1003 |
// used in an access. |
| 1004 |
|
| 1005 |
static inline Register ToRegister(RegI32 r) { return Register(r); } |
| 1006 |
#ifdef ENABLE_WASM_MEMORY64 |
| 1007 |
# ifdef JS_PUNBOX64 |
| 1008 |
static inline Register ToRegister(RegI64 r) { return r.reg; } |
| 1009 |
# else |
| 1010 |
static inline Register ToRegister(RegI64 r) { return r.low; } |
| 1011 |
# endif |
| 1012 |
#endif |
| 1013 |
|
| 1014 |
////////////////////////////////////////////////////////////////////////////// |
| 1015 |
// |
| 1016 |
// Atomic operations. |
| 1017 |
// |
| 1018 |
// The atomic operations have very diverse per-platform needs for register |
| 1019 |
// allocation and temps. To handle that, the implementations are structured as |
| 1020 |
// a per-operation framework method that calls into platform-specific helpers |
| 1021 |
// (usually called PopAndAllocate, Perform, and Deallocate) in a per-operation |
| 1022 |
// namespace. This structure results in a little duplication and boilerplate |
| 1023 |
// but is otherwise clean and flexible and keeps code and supporting definitions |
| 1024 |
// entirely co-located. |
| 1025 |
|
| 1026 |
// Some consumers depend on the returned Address not incorporating instance, as |
| 1027 |
// instance may be the scratch register. |
| 1028 |
// |
| 1029 |
// RegAddressType is RegI32 for Memory32 and RegI64 for Memory64. |
| 1030 |
template <typename RegAddressType> |
| 1031 |
Address BaseCompiler::prepareAtomicMemoryAccess(MemoryAccessDesc* access, |
| 1032 |
AccessCheck* check, |
| 1033 |
RegPtr instance, |
| 1034 |
RegAddressType ptr) { |
| 1035 |
MOZ_ASSERT(needInstanceForAccess(access, *check) == instance.isValid()); |
| 1036 |
prepareMemoryAccess(access, check, instance, ptr); |
| 1037 |
|
| 1038 |
#ifdef WASM_HAS_HEAPREG |
| 1039 |
if (access->memoryIndex() == 0) { |
| 1040 |
masm.addPtr(HeapReg, ToRegister(ptr)); |
| 1041 |
} else { |
| 1042 |
masm.addPtr( |
| 1043 |
Address(instance, instanceOffsetOfMemoryBase(access->memoryIndex())), |
| 1044 |
ToRegister(ptr)); |
| 1045 |
} |
| 1046 |
#else |
| 1047 |
masm.addPtr( |
| 1048 |
Address(instance, instanceOffsetOfMemoryBase(access->memoryIndex())), |
| 1049 |
ToRegister(ptr)); |
| 1050 |
#endif |
| 1051 |
|
| 1052 |
// At this point, 64-bit offsets will have been folded away by |
| 1053 |
// prepareMemoryAccess. |
| 1054 |
return Address(ToRegister(ptr), access->offset32()); |
| 1055 |
} |
| 1056 |
|
| 1057 |
#ifndef WASM_HAS_HEAPREG |
| 1058 |
# ifdef JS_CODEGEN_X86 |
| 1059 |
using ScratchAtomicNoHeapReg = ScratchEBX; |
| 1060 |
# else |
| 1061 |
# error "Unimplemented porting interface" |
| 1062 |
# endif |
| 1063 |
#endif |
| 1064 |
|
| 1065 |
////////////////////////////////////////////////////////////////////////////// |
| 1066 |
// |
| 1067 |
// Atomic load and store. |
| 1068 |
|
| 1069 |
namespace atomic_load64 { |
| 1070 |
|
| 1071 |
#ifdef JS_CODEGEN_ARM |
| 1072 |
|
| 1073 |
static void Allocate(BaseCompiler* bc, RegI64* rd, RegI64*) { |
| 1074 |
*rd = bc->needI64Pair(); |
| 1075 |
} |
| 1076 |
|
| 1077 |
static void Deallocate(BaseCompiler* bc, RegI64) {} |
| 1078 |
|
| 1079 |
#elif defined JS_CODEGEN_X86 |
| 1080 |
|
| 1081 |
static void Allocate(BaseCompiler* bc, RegI64* rd, RegI64* temp) { |
| 1082 |
// The result is in edx:eax, and we need ecx:ebx as a temp. But ebx will also |
| 1083 |
// be used as a scratch, so don't manage that here. |
| 1084 |
bc->needI32(bc->specific_.ecx); |
| 1085 |
*temp = bc->specific_.ecx_ebx; |
| 1086 |
bc->needI64(bc->specific_.edx_eax); |
| 1087 |
*rd = bc->specific_.edx_eax; |
| 1088 |
} |
| 1089 |
|
| 1090 |
static void Deallocate(BaseCompiler* bc, RegI64 temp) { |
| 1091 |
// See comment above. |
| 1092 |
MOZ_ASSERT(temp.high == js::jit::ecx); |
| 1093 |
bc->freeI32(bc->specific_.ecx); |
| 1094 |
} |
| 1095 |
|
| 1096 |
#elif defined(__wasi__) || (defined(JS_CODEGEN_NONE) && !defined(JS_64BIT)) |
| 1097 |
|
| 1098 |
static void Allocate(BaseCompiler*, RegI64*, RegI64*) {} |
| 1099 |
static void Deallocate(BaseCompiler*, RegI64) {} |
| 1100 |
|
| 1101 |
#endif |
| 1102 |
|
| 1103 |
} // namespace atomic_load64 |
| 1104 |
|
| 1105 |
#if !defined(JS_64BIT) |
| 1106 |
template <typename RegAddressType> |
| 1107 |
void BaseCompiler::atomicLoad64(MemoryAccessDesc* access) { |
| 1108 |
RegI64 rd, temp; |
| 1109 |
atomic_load64::Allocate(this, &rd, &temp); |
| 1110 |
|
| 1111 |
AccessCheck check; |
| 1112 |
RegAddressType rp = popMemoryAccess<RegAddressType>(access, &check); |
| 1113 |
|
| 1114 |
# ifdef WASM_HAS_HEAPREG |
| 1115 |
RegPtr instance = maybeLoadInstanceForAccess(access, check); |
| 1116 |
auto memaddr = prepareAtomicMemoryAccess(access, &check, instance, rp); |
| 1117 |
masm.wasmAtomicLoad64(*access, memaddr, temp, rd); |
| 1118 |
# ifndef RABALDR_PIN_INSTANCE |
| 1119 |
maybeFree(instance); |
| 1120 |
# endif |
| 1121 |
# else |
| 1122 |
ScratchAtomicNoHeapReg scratch(*this); |
| 1123 |
RegPtr instance = |
| 1124 |
maybeLoadInstanceForAccess(access, check, RegIntptrToRegPtr(scratch)); |
| 1125 |
auto memaddr = prepareAtomicMemoryAccess(access, &check, instance, rp); |
| 1126 |
masm.wasmAtomicLoad64(*access, memaddr, temp, rd); |
| 1127 |
MOZ_ASSERT(instance == scratch); |
| 1128 |
# endif |
| 1129 |
|
| 1130 |
free(rp); |
| 1131 |
atomic_load64::Deallocate(this, temp); |
| 1132 |
pushI64(rd); |
| 1133 |
} |
| 1134 |
#endif |
| 1135 |
|
| 1136 |
void BaseCompiler::atomicLoad(MemoryAccessDesc* access, ValType type) { |
| 1137 |
Scalar::Type viewType = access->type(); |
| 1138 |
if (Scalar::byteSize(viewType) <= sizeof(void*)) { |
| 1139 |
loadCommon(access, AccessCheck(), type); |
| 1140 |
return; |
| 1141 |
} |
| 1142 |
|
| 1143 |
MOZ_ASSERT(type == ValType::I64 && Scalar::byteSize(viewType) == 8); |
| 1144 |
|
| 1145 |
#if !defined(JS_64BIT) |
| 1146 |
if (isMem32(access->memoryIndex())) { |
| 1147 |
atomicLoad64<RegI32>(access); |
| 1148 |
} else { |
| 1149 |
# ifdef ENABLE_WASM_MEMORY64 |
| 1150 |
atomicLoad64<RegI64>(access); |
| 1151 |
# else |
| 1152 |
MOZ_CRASH("Memory64 not enabled / supported on this platform"); |
| 1153 |
# endif |
| 1154 |
} |
| 1155 |
#else |
| 1156 |
MOZ_CRASH("Should not happen"); |
| 1157 |
#endif |
| 1158 |
} |
| 1159 |
|
| 1160 |
void BaseCompiler::atomicStore(MemoryAccessDesc* access, ValType type) { |
| 1161 |
Scalar::Type viewType = access->type(); |
| 1162 |
|
| 1163 |
if (Scalar::byteSize(viewType) <= sizeof(void*)) { |
| 1164 |
storeCommon(access, AccessCheck(), type); |
| 1165 |
return; |
| 1166 |
} |
| 1167 |
|
| 1168 |
MOZ_ASSERT(type == ValType::I64 && Scalar::byteSize(viewType) == 8); |
| 1169 |
|
| 1170 |
#if !defined(JS_64BIT) |
| 1171 |
if (isMem32(access->memoryIndex())) { |
| 1172 |
atomicXchg64<RegI32>(access, WantResult(false)); |
| 1173 |
} else { |
| 1174 |
# ifdef ENABLE_WASM_MEMORY64 |
| 1175 |
atomicXchg64<RegI64>(access, WantResult(false)); |
| 1176 |
# else |
| 1177 |
MOZ_CRASH("Memory64 not enabled / supported on this platform"); |
| 1178 |
# endif |
| 1179 |
} |
| 1180 |
#else |
| 1181 |
MOZ_CRASH("Should not happen"); |
| 1182 |
#endif |
| 1183 |
} |
| 1184 |
|
| 1185 |
////////////////////////////////////////////////////////////////////////////// |
| 1186 |
// |
| 1187 |
// Atomic RMW op= operations. |
| 1188 |
|
| 1189 |
void BaseCompiler::atomicRMW(MemoryAccessDesc* access, ValType type, |
| 1190 |
AtomicOp op) { |
| 1191 |
Scalar::Type viewType = access->type(); |
| 1192 |
if (Scalar::byteSize(viewType) <= 4) { |
| 1193 |
if (isMem32(access->memoryIndex())) { |
| 1194 |
atomicRMW32<RegI32>(access, type, op); |
| 1195 |
} else { |
| 1196 |
#ifdef ENABLE_WASM_MEMORY64 |
| 1197 |
atomicRMW32<RegI64>(access, type, op); |
| 1198 |
#else |
| 1199 |
MOZ_CRASH("Memory64 not enabled / supported on this platform"); |
| 1200 |
#endif |
| 1201 |
} |
| 1202 |
} else { |
| 1203 |
MOZ_ASSERT(type == ValType::I64 && Scalar::byteSize(viewType) == 8); |
| 1204 |
if (isMem32(access->memoryIndex())) { |
| 1205 |
atomicRMW64<RegI32>(access, type, op); |
| 1206 |
} else { |
| 1207 |
#ifdef ENABLE_WASM_MEMORY64 |
| 1208 |
atomicRMW64<RegI64>(access, type, op); |
| 1209 |
#else |
| 1210 |
MOZ_CRASH("Memory64 not enabled / supported on this platform"); |
| 1211 |
#endif |
| 1212 |
} |
| 1213 |
} |
| 1214 |
} |
| 1215 |
|
| 1216 |
namespace atomic_rmw32 { |
| 1217 |
|
| 1218 |
#if defined(JS_CODEGEN_X64) || defined(JS_CODEGEN_X86) |
| 1219 |
|
| 1220 |
struct Temps { |
| 1221 |
// On x86 we use the ScratchI32 for the temp, otherwise we'd run out of |
| 1222 |
// registers for 64-bit operations. |
| 1223 |
# if defined(JS_CODEGEN_X64) |
| 1224 |
RegI32 t0; |
| 1225 |
# endif |
| 1226 |
}; |
| 1227 |
|
| 1228 |
static void PopAndAllocate(BaseCompiler* bc, ValType type, |
| 1229 |
Scalar::Type viewType, AtomicOp op, RegI32* rd, |
| 1230 |
RegI32* rv, Temps* temps) { |
| 1231 |
bc->needI32(bc->specific_.eax); |
| 1232 |
if (op == AtomicOp::Add || op == AtomicOp::Sub) { |
| 1233 |
// We use xadd, so source and destination are the same. Using |
| 1234 |
// eax here is overconstraining, but for byte operations on x86 |
| 1235 |
// we do need something with a byte register. |
| 1236 |
if (type == ValType::I64) { |
| 1237 |
*rv = bc->popI64ToSpecificI32(bc->specific_.eax); |
| 1238 |
} else { |
| 1239 |
*rv = bc->popI32ToSpecific(bc->specific_.eax); |
| 1240 |
} |
| 1241 |
*rd = *rv; |
| 1242 |
} else { |
| 1243 |
// We use a cmpxchg loop. The output must be eax; the input |
| 1244 |
// must be in a separate register since it may be used several |
| 1245 |
// times. |
| 1246 |
if (type == ValType::I64) { |
| 1247 |
*rv = bc->popI64ToI32(); |
| 1248 |
} else { |
| 1249 |
*rv = bc->popI32(); |
| 1250 |
} |
| 1251 |
*rd = bc->specific_.eax; |
| 1252 |
# ifdef JS_CODEGEN_X64 |
| 1253 |
temps->t0 = bc->needI32(); |
| 1254 |
# endif |
| 1255 |
} |
| 1256 |
} |
| 1257 |
|
| 1258 |
template <typename T> |
| 1259 |
static void Perform(BaseCompiler* bc, const MemoryAccessDesc& access, T srcAddr, |
| 1260 |
AtomicOp op, RegI32 rv, RegI32 rd, const Temps& temps) { |
| 1261 |
# ifdef JS_CODEGEN_X64 |
| 1262 |
RegI32 temp = temps.t0; |
| 1263 |
# else |
| 1264 |
RegI32 temp; |
| 1265 |
ScratchI32 scratch(*bc); |
| 1266 |
if (op != AtomicOp::Add && op != AtomicOp::Sub) { |
| 1267 |
temp = scratch; |
| 1268 |
} |
| 1269 |
# endif |
| 1270 |
bc->masm.wasmAtomicFetchOp(access, op, rv, srcAddr, temp, rd); |
| 1271 |
} |
| 1272 |
|
| 1273 |
static void Deallocate(BaseCompiler* bc, RegI32 rv, const Temps& temps) { |
| 1274 |
if (rv != bc->specific_.eax) { |
| 1275 |
bc->freeI32(rv); |
| 1276 |
} |
| 1277 |
# ifdef JS_CODEGEN_X64 |
| 1278 |
bc->maybeFree(temps.t0); |
| 1279 |
# endif |
| 1280 |
} |
| 1281 |
|
| 1282 |
#elif defined(JS_CODEGEN_ARM) || defined(JS_CODEGEN_ARM64) |
| 1283 |
|
| 1284 |
struct Temps { |
| 1285 |
RegI32 t0; |
| 1286 |
}; |
| 1287 |
|
| 1288 |
static void PopAndAllocate(BaseCompiler* bc, ValType type, |
| 1289 |
Scalar::Type viewType, AtomicOp op, RegI32* rd, |
| 1290 |
RegI32* rv, Temps* temps) { |
| 1291 |
*rv = type == ValType::I64 ? bc->popI64ToI32() : bc->popI32(); |
| 1292 |
temps->t0 = bc->needI32(); |
| 1293 |
*rd = bc->needI32(); |
| 1294 |
} |
| 1295 |
|
| 1296 |
static void Perform(BaseCompiler* bc, const MemoryAccessDesc& access, |
| 1297 |
Address srcAddr, AtomicOp op, RegI32 rv, RegI32 rd, |
| 1298 |
const Temps& temps) { |
| 1299 |
bc->masm.wasmAtomicFetchOp(access, op, rv, srcAddr, temps.t0, rd); |
| 1300 |
} |
| 1301 |
|
| 1302 |
static void Deallocate(BaseCompiler* bc, RegI32 rv, const Temps& temps) { |
| 1303 |
bc->freeI32(rv); |
| 1304 |
bc->freeI32(temps.t0); |
| 1305 |
} |
| 1306 |
|
| 1307 |
#elif defined(JS_CODEGEN_MIPS64) || defined(JS_CODEGEN_LOONG64) |
| 1308 |
|
| 1309 |
struct Temps { |
| 1310 |
RegI32 t0, t1, t2; |
| 1311 |
}; |
| 1312 |
|
| 1313 |
static void PopAndAllocate(BaseCompiler* bc, ValType type, |
| 1314 |
Scalar::Type viewType, AtomicOp op, RegI32* rd, |
| 1315 |
RegI32* rv, Temps* temps) { |
| 1316 |
*rv = type == ValType::I64 ? bc->popI64ToI32() : bc->popI32(); |
| 1317 |
if (Scalar::byteSize(viewType) < 4) { |
| 1318 |
temps->t0 = bc->needI32(); |
| 1319 |
temps->t1 = bc->needI32(); |
| 1320 |
temps->t2 = bc->needI32(); |
| 1321 |
} |
| 1322 |
*rd = bc->needI32(); |
| 1323 |
} |
| 1324 |
|
| 1325 |
static void Perform(BaseCompiler* bc, const MemoryAccessDesc& access, |
| 1326 |
Address srcAddr, AtomicOp op, RegI32 rv, RegI32 rd, |
| 1327 |
const Temps& temps) { |
| 1328 |
bc->masm.wasmAtomicFetchOp(access, op, rv, srcAddr, temps.t0, temps.t1, |
| 1329 |
temps.t2, rd); |
| 1330 |
} |
| 1331 |
|
| 1332 |
static void Deallocate(BaseCompiler* bc, RegI32 rv, const Temps& temps) { |
| 1333 |
bc->freeI32(rv); |
| 1334 |
bc->maybeFree(temps.t0); |
| 1335 |
bc->maybeFree(temps.t1); |
| 1336 |
bc->maybeFree(temps.t2); |
| 1337 |
} |
| 1338 |
|
| 1339 |
#elif defined(JS_CODEGEN_RISCV64) |
| 1340 |
|
| 1341 |
struct Temps { |
| 1342 |
RegI32 t0, t1, t2; |
| 1343 |
}; |
| 1344 |
|
| 1345 |
static void PopAndAllocate(BaseCompiler* bc, ValType type, |
| 1346 |
Scalar::Type viewType, AtomicOp op, RegI32* rd, |
| 1347 |
RegI32* rv, Temps* temps) { |
| 1348 |
*rv = type == ValType::I64 ? bc->popI64ToI32() : bc->popI32(); |
| 1349 |
if (Scalar::byteSize(viewType) < 4) { |
| 1350 |
temps->t0 = bc->needI32(); |
| 1351 |
temps->t1 = bc->needI32(); |
| 1352 |
temps->t2 = bc->needI32(); |
| 1353 |
} |
| 1354 |
*rd = bc->needI32(); |
| 1355 |
} |
| 1356 |
|
| 1357 |
static void Perform(BaseCompiler* bc, const MemoryAccessDesc& access, |
| 1358 |
Address srcAddr, AtomicOp op, RegI32 rv, RegI32 rd, |
| 1359 |
const Temps& temps) { |
| 1360 |
bc->masm.wasmAtomicFetchOp(access, op, rv, srcAddr, temps.t0, temps.t1, |
| 1361 |
temps.t2, rd); |
| 1362 |
} |
| 1363 |
|
| 1364 |
static void Deallocate(BaseCompiler* bc, RegI32 rv, const Temps& temps) { |
| 1365 |
bc->freeI32(rv); |
| 1366 |
bc->maybeFree(temps.t0); |
| 1367 |
bc->maybeFree(temps.t1); |
| 1368 |
bc->maybeFree(temps.t2); |
| 1369 |
} |
| 1370 |
|
| 1371 |
#elif defined(JS_CODEGEN_NONE) || defined(JS_CODEGEN_WASM32) |
| 1372 |
|
| 1373 |
using Temps = Nothing; |
| 1374 |
|
| 1375 |
static void PopAndAllocate(BaseCompiler*, ValType, Scalar::Type, AtomicOp, |
| 1376 |
RegI32*, RegI32*, Temps*) {} |
| 1377 |
|
| 1378 |
static void Perform(BaseCompiler*, const MemoryAccessDesc&, Address, AtomicOp, |
| 1379 |
RegI32, RegI32, const Temps&) {} |
| 1380 |
|
| 1381 |
static void Deallocate(BaseCompiler*, RegI32, const Temps&) {} |
| 1382 |
|
| 1383 |
#endif |
| 1384 |
|
| 1385 |
} // namespace atomic_rmw32 |
| 1386 |
|
| 1387 |
template <typename RegAddressType> |
| 1388 |
void BaseCompiler::atomicRMW32(MemoryAccessDesc* access, ValType type, |
| 1389 |
AtomicOp op) { |
| 1390 |
Scalar::Type viewType = access->type(); |
| 1391 |
RegI32 rd, rv; |
| 1392 |
atomic_rmw32::Temps temps; |
| 1393 |
atomic_rmw32::PopAndAllocate(this, type, viewType, op, &rd, &rv, &temps); |
| 1394 |
|
| 1395 |
AccessCheck check; |
| 1396 |
RegAddressType rp = popMemoryAccess<RegAddressType>(access, &check); |
| 1397 |
RegPtr instance = maybeLoadInstanceForAccess(access, check); |
| 1398 |
|
| 1399 |
auto memaddr = prepareAtomicMemoryAccess(access, &check, instance, rp); |
| 1400 |
atomic_rmw32::Perform(this, *access, memaddr, op, rv, rd, temps); |
| 1401 |
|
| 1402 |
#ifndef RABALDR_PIN_INSTANCE |
| 1403 |
maybeFree(instance); |
| 1404 |
#endif |
| 1405 |
atomic_rmw32::Deallocate(this, rv, temps); |
| 1406 |
free(rp); |
| 1407 |
|
| 1408 |
if (type == ValType::I64) { |
| 1409 |
pushU32AsI64(rd); |
| 1410 |
} else { |
| 1411 |
pushI32(rd); |
| 1412 |
} |
| 1413 |
} |
| 1414 |
|
| 1415 |
namespace atomic_rmw64 { |
| 1416 |
|
| 1417 |
#if defined(JS_CODEGEN_X64) |
| 1418 |
|
| 1419 |
static void PopAndAllocate(BaseCompiler* bc, AtomicOp op, RegI64* rd, |
| 1420 |
RegI64* rv, RegI64* temp) { |
| 1421 |
if (op == AtomicOp::Add || op == AtomicOp::Sub) { |
| 1422 |
// We use xaddq, so input and output must be the same register. |
| 1423 |
*rv = bc->popI64(); |
| 1424 |
*rd = *rv; |
| 1425 |
} else { |
| 1426 |
// We use a cmpxchgq loop, so the output must be rax and we need a temp. |
| 1427 |
bc->needI64(bc->specific_.rax); |
| 1428 |
*rd = bc->specific_.rax; |
| 1429 |
*rv = bc->popI64(); |
| 1430 |
*temp = bc->needI64(); |
| 1431 |
} |
| 1432 |
} |
| 1433 |
|
| 1434 |
static void Perform(BaseCompiler* bc, const MemoryAccessDesc& access, |
| 1435 |
Address srcAddr, AtomicOp op, RegI64 rv, RegI64 temp, |
| 1436 |
RegI64 rd) { |
| 1437 |
bc->masm.wasmAtomicFetchOp64(access, op, rv, srcAddr, temp, rd); |
| 1438 |
} |
| 1439 |
|
| 1440 |
static void Deallocate(BaseCompiler* bc, AtomicOp op, RegI64 rv, RegI64 temp) { |
| 1441 |
bc->maybeFree(temp); |
| 1442 |
if (op != AtomicOp::Add && op != AtomicOp::Sub) { |
| 1443 |
bc->freeI64(rv); |
| 1444 |
} |
| 1445 |
} |
| 1446 |
|
| 1447 |
#elif defined(JS_CODEGEN_X86) |
| 1448 |
|
| 1449 |
// Register allocation is tricky, see comments at atomic_xchg64 below. |
| 1450 |
// |
| 1451 |
// - Initially rv=ecx:edx and eax is reserved, rd=unallocated. |
| 1452 |
// - Then rp is popped into esi+edi because those are the only available. |
| 1453 |
// - The Setup operation makes rd=edx:eax. |
| 1454 |
// - Deallocation then frees only the ecx part of rv. |
| 1455 |
// |
| 1456 |
// The temp is unused here. |
| 1457 |
|
| 1458 |
static void PopAndAllocate(BaseCompiler* bc, AtomicOp op, RegI64* rd, |
| 1459 |
RegI64* rv, RegI64*) { |
| 1460 |
bc->needI32(bc->specific_.eax); |
| 1461 |
bc->needI32(bc->specific_.ecx); |
| 1462 |
bc->needI32(bc->specific_.edx); |
| 1463 |
*rv = RegI64(Register64(bc->specific_.ecx, bc->specific_.edx)); |
| 1464 |
bc->popI64ToSpecific(*rv); |
| 1465 |
} |
| 1466 |
|
| 1467 |
static void Setup(BaseCompiler* bc, RegI64* rd) { *rd = bc->specific_.edx_eax; } |
| 1468 |
|
| 1469 |
static void Perform(BaseCompiler* bc, const MemoryAccessDesc& access, |
| 1470 |
Address srcAddr, AtomicOp op, RegI64 rv, RegI64, RegI64 rd, |
| 1471 |
const ScratchAtomicNoHeapReg& scratch) { |
| 1472 |
MOZ_ASSERT(rv.high == bc->specific_.ecx); |
| 1473 |
MOZ_ASSERT(Register(scratch) == js::jit::ebx); |
| 1474 |
|
| 1475 |
bc->fr.pushGPR(rv.high); |
| 1476 |
bc->fr.pushGPR(rv.low); |
| 1477 |
Address value(StackPointer, 0); |
| 1478 |
|
| 1479 |
bc->masm.wasmAtomicFetchOp64(access, op, value, srcAddr, |
| 1480 |
bc->specific_.ecx_ebx, rd); |
| 1481 |
|
| 1482 |
bc->fr.popBytes(8); |
| 1483 |
} |
| 1484 |
|
| 1485 |
static void Deallocate(BaseCompiler* bc, AtomicOp, RegI64, RegI64) { |
| 1486 |
bc->freeI32(bc->specific_.ecx); |
| 1487 |
} |
| 1488 |
|
| 1489 |
#elif defined(JS_CODEGEN_ARM) |
| 1490 |
|
| 1491 |
static void PopAndAllocate(BaseCompiler* bc, AtomicOp op, RegI64* rd, |
| 1492 |
RegI64* rv, RegI64* temp) { |
| 1493 |
// We use a ldrex/strexd loop so the temp and the output must be |
| 1494 |
// odd/even pairs. |
| 1495 |
*rv = bc->popI64(); |
| 1496 |
*temp = bc->needI64Pair(); |
| 1497 |
*rd = bc->needI64Pair(); |
| 1498 |
} |
| 1499 |
|
| 1500 |
static void Perform(BaseCompiler* bc, const MemoryAccessDesc& access, |
| 1501 |
Address srcAddr, AtomicOp op, RegI64 rv, RegI64 temp, |
| 1502 |
RegI64 rd) { |
| 1503 |
bc->masm.wasmAtomicFetchOp64(access, op, rv, srcAddr, temp, rd); |
| 1504 |
} |
| 1505 |
|
| 1506 |
static void Deallocate(BaseCompiler* bc, AtomicOp op, RegI64 rv, RegI64 temp) { |
| 1507 |
bc->freeI64(rv); |
| 1508 |
bc->freeI64(temp); |
| 1509 |
} |
| 1510 |
|
| 1511 |
#elif defined(JS_CODEGEN_ARM64) || defined(JS_CODEGEN_MIPS64) || \ |
| 1512 |
defined(JS_CODEGEN_LOONG64) |
| 1513 |
|
| 1514 |
static void PopAndAllocate(BaseCompiler* bc, AtomicOp op, RegI64* rd, |
| 1515 |
RegI64* rv, RegI64* temp) { |
| 1516 |
*rv = bc->popI64(); |
| 1517 |
*temp = bc->needI64(); |
| 1518 |
*rd = bc->needI64(); |
| 1519 |
} |
| 1520 |
|
| 1521 |
static void Perform(BaseCompiler* bc, const MemoryAccessDesc& access, |
| 1522 |
Address srcAddr, AtomicOp op, RegI64 rv, RegI64 temp, |
| 1523 |
RegI64 rd) { |
| 1524 |
bc->masm.wasmAtomicFetchOp64(access, op, rv, srcAddr, temp, rd); |
| 1525 |
} |
| 1526 |
|
| 1527 |
static void Deallocate(BaseCompiler* bc, AtomicOp op, RegI64 rv, RegI64 temp) { |
| 1528 |
bc->freeI64(rv); |
| 1529 |
bc->freeI64(temp); |
| 1530 |
} |
| 1531 |
#elif defined(JS_CODEGEN_RISCV64) |
| 1532 |
|
| 1533 |
static void PopAndAllocate(BaseCompiler* bc, AtomicOp op, RegI64* rd, |
| 1534 |
RegI64* rv, RegI64* temp) { |
| 1535 |
*rv = bc->popI64(); |
| 1536 |
*temp = bc->needI64(); |
| 1537 |
*rd = bc->needI64(); |
| 1538 |
} |
| 1539 |
|
| 1540 |
static void Perform(BaseCompiler* bc, const MemoryAccessDesc& access, |
| 1541 |
Address srcAddr, AtomicOp op, RegI64 rv, RegI64 temp, |
| 1542 |
RegI64 rd) { |
| 1543 |
bc->masm.wasmAtomicFetchOp64(access, op, rv, srcAddr, temp, rd); |
| 1544 |
} |
| 1545 |
|
| 1546 |
static void Deallocate(BaseCompiler* bc, AtomicOp op, RegI64 rv, RegI64 temp) { |
| 1547 |
bc->freeI64(rv); |
| 1548 |
bc->freeI64(temp); |
| 1549 |
} |
| 1550 |
|
| 1551 |
#elif defined(JS_CODEGEN_NONE) || defined(JS_CODEGEN_WASM32) |
| 1552 |
|
| 1553 |
static void PopAndAllocate(BaseCompiler*, AtomicOp, RegI64*, RegI64*, RegI64*) { |
| 1554 |
} |
| 1555 |
|
| 1556 |
static void Perform(BaseCompiler*, const MemoryAccessDesc&, Address, |
| 1557 |
AtomicOp op, RegI64, RegI64, RegI64) {} |
| 1558 |
|
| 1559 |
static void Deallocate(BaseCompiler*, AtomicOp, RegI64, RegI64) {} |
| 1560 |
|
| 1561 |
#endif |
| 1562 |
|
| 1563 |
} // namespace atomic_rmw64 |
| 1564 |
|
| 1565 |
template <typename RegAddressType> |
| 1566 |
void BaseCompiler::atomicRMW64(MemoryAccessDesc* access, ValType type, |
| 1567 |
AtomicOp op) { |
| 1568 |
RegI64 rd, rv, temp; |
| 1569 |
atomic_rmw64::PopAndAllocate(this, op, &rd, &rv, &temp); |
| 1570 |
|
| 1571 |
AccessCheck check; |
| 1572 |
RegAddressType rp = popMemoryAccess<RegAddressType>(access, &check); |
| 1573 |
|
| 1574 |
#if defined(WASM_HAS_HEAPREG) |
| 1575 |
RegPtr instance = maybeLoadInstanceForAccess(access, check); |
| 1576 |
auto memaddr = prepareAtomicMemoryAccess(access, &check, instance, rp); |
| 1577 |
atomic_rmw64::Perform(this, *access, memaddr, op, rv, temp, rd); |
| 1578 |
# ifndef RABALDR_PIN_INSTANCE |
| 1579 |
maybeFree(instance); |
| 1580 |
# endif |
| 1581 |
#else |
| 1582 |
ScratchAtomicNoHeapReg scratch(*this); |
| 1583 |
RegPtr instance = |
| 1584 |
maybeLoadInstanceForAccess(access, check, RegIntptrToRegPtr(scratch)); |
| 1585 |
auto memaddr = prepareAtomicMemoryAccess(access, &check, instance, rp); |
| 1586 |
atomic_rmw64::Setup(this, &rd); |
| 1587 |
atomic_rmw64::Perform(this, *access, memaddr, op, rv, temp, rd, scratch); |
| 1588 |
MOZ_ASSERT(instance == scratch); |
| 1589 |
#endif |
| 1590 |
|
| 1591 |
free(rp); |
| 1592 |
atomic_rmw64::Deallocate(this, op, rv, temp); |
| 1593 |
|
| 1594 |
pushI64(rd); |
| 1595 |
} |
| 1596 |
|
| 1597 |
////////////////////////////////////////////////////////////////////////////// |
| 1598 |
// |
| 1599 |
// Atomic exchange (also used for atomic store in some cases). |
| 1600 |
|
| 1601 |
void BaseCompiler::atomicXchg(MemoryAccessDesc* access, ValType type) { |
| 1602 |
Scalar::Type viewType = access->type(); |
| 1603 |
if (Scalar::byteSize(viewType) <= 4) { |
| 1604 |
if (isMem32(access->memoryIndex())) { |
| 1605 |
atomicXchg32<RegI32>(access, type); |
| 1606 |
} else { |
| 1607 |
#ifdef ENABLE_WASM_MEMORY64 |
| 1608 |
atomicXchg32<RegI64>(access, type); |
| 1609 |
#else |
| 1610 |
MOZ_CRASH("Memory64 not enabled / supported on this platform"); |
| 1611 |
#endif |
| 1612 |
} |
| 1613 |
} else { |
| 1614 |
MOZ_ASSERT(type == ValType::I64 && Scalar::byteSize(viewType) == 8); |
| 1615 |
if (isMem32(access->memoryIndex())) { |
| 1616 |
atomicXchg64<RegI32>(access, WantResult(true)); |
| 1617 |
} else { |
| 1618 |
#ifdef ENABLE_WASM_MEMORY64 |
| 1619 |
atomicXchg64<RegI64>(access, WantResult(true)); |
| 1620 |
#else |
| 1621 |
MOZ_CRASH("Memory64 not enabled / supported on this platform"); |
| 1622 |
#endif |
| 1623 |
} |
| 1624 |
} |
| 1625 |
} |
| 1626 |
|
| 1627 |
namespace atomic_xchg32 { |
| 1628 |
|
| 1629 |
#if defined(JS_CODEGEN_X64) |
| 1630 |
|
| 1631 |
using Temps = Nothing; |
| 1632 |
|
| 1633 |
static void PopAndAllocate(BaseCompiler* bc, ValType type, |
| 1634 |
Scalar::Type viewType, RegI32* rd, RegI32* rv, |
| 1635 |
Temps*) { |
| 1636 |
// The xchg instruction reuses rv as rd. |
| 1637 |
*rv = (type == ValType::I64) ? bc->popI64ToI32() : bc->popI32(); |
| 1638 |
*rd = *rv; |
| 1639 |
} |
| 1640 |
|
| 1641 |
static void Perform(BaseCompiler* bc, const MemoryAccessDesc& access, |
| 1642 |
Address srcAddr, RegI32 rv, RegI32 rd, const Temps&) { |
| 1643 |
bc->masm.wasmAtomicExchange(access, srcAddr, rv, rd); |
| 1644 |
} |
| 1645 |
|
| 1646 |
static void Deallocate(BaseCompiler* bc, RegI32, const Temps&) {} |
| 1647 |
|
| 1648 |
#elif defined(JS_CODEGEN_X86) |
| 1649 |
|
| 1650 |
using Temps = Nothing; |
| 1651 |
|
| 1652 |
static void PopAndAllocate(BaseCompiler* bc, ValType type, |
| 1653 |
Scalar::Type viewType, RegI32* rd, RegI32* rv, |
| 1654 |
Temps*) { |
| 1655 |
// The xchg instruction reuses rv as rd. |
| 1656 |
*rv = (type == ValType::I64) ? bc->popI64ToI32() : bc->popI32(); |
| 1657 |
*rd = *rv; |
| 1658 |
} |
| 1659 |
|
| 1660 |
static void Perform(BaseCompiler* bc, const MemoryAccessDesc& access, |
| 1661 |
Address srcAddr, RegI32 rv, RegI32 rd, const Temps&) { |
| 1662 |
if (access.type() == Scalar::Uint8 && !bc->ra.isSingleByteI32(rd)) { |
| 1663 |
ScratchI8 scratch(*bc); |
| 1664 |
// The output register must have a byte persona. |
| 1665 |
bc->masm.wasmAtomicExchange(access, srcAddr, rv, scratch); |
| 1666 |
bc->masm.movl(scratch, rd); |
| 1667 |
} else { |
| 1668 |
bc->masm.wasmAtomicExchange(access, srcAddr, rv, rd); |
| 1669 |
} |
| 1670 |
} |
| 1671 |
|
| 1672 |
static void Deallocate(BaseCompiler* bc, RegI32, const Temps&) {} |
| 1673 |
|
| 1674 |
#elif defined(JS_CODEGEN_ARM) || defined(JS_CODEGEN_ARM64) |
| 1675 |
|
| 1676 |
using Temps = Nothing; |
| 1677 |
|
| 1678 |
static void PopAndAllocate(BaseCompiler* bc, ValType type, |
| 1679 |
Scalar::Type viewType, RegI32* rd, RegI32* rv, |
| 1680 |
Temps*) { |
| 1681 |
*rv = (type == ValType::I64) ? bc->popI64ToI32() : bc->popI32(); |
| 1682 |
*rd = bc->needI32(); |
| 1683 |
} |
| 1684 |
|
| 1685 |
static void Perform(BaseCompiler* bc, const MemoryAccessDesc& access, |
| 1686 |
Address srcAddr, RegI32 rv, RegI32 rd, const Temps&) { |
| 1687 |
bc->masm.wasmAtomicExchange(access, srcAddr, rv, rd); |
| 1688 |
} |
| 1689 |
|
| 1690 |
static void Deallocate(BaseCompiler* bc, RegI32 rv, const Temps&) { |
| 1691 |
bc->freeI32(rv); |
| 1692 |
} |
| 1693 |
|
| 1694 |
#elif defined(JS_CODEGEN_MIPS64) || defined(JS_CODEGEN_LOONG64) |
| 1695 |
|
| 1696 |
struct Temps { |
| 1697 |
RegI32 t0, t1, t2; |
| 1698 |
}; |
| 1699 |
|
| 1700 |
static void PopAndAllocate(BaseCompiler* bc, ValType type, |
| 1701 |
Scalar::Type viewType, RegI32* rd, RegI32* rv, |
| 1702 |
Temps* temps) { |
| 1703 |
*rv = (type == ValType::I64) ? bc->popI64ToI32() : bc->popI32(); |
| 1704 |
if (Scalar::byteSize(viewType) < 4) { |
| 1705 |
temps->t0 = bc->needI32(); |
| 1706 |
temps->t1 = bc->needI32(); |
| 1707 |
temps->t2 = bc->needI32(); |
| 1708 |
} |
| 1709 |
*rd = bc->needI32(); |
| 1710 |
} |
| 1711 |
|
| 1712 |
static void Perform(BaseCompiler* bc, const MemoryAccessDesc& access, |
| 1713 |
Address srcAddr, RegI32 rv, RegI32 rd, const Temps& temps) { |
| 1714 |
bc->masm.wasmAtomicExchange(access, srcAddr, rv, temps.t0, temps.t1, temps.t2, |
| 1715 |
rd); |
| 1716 |
} |
| 1717 |
|
| 1718 |
static void Deallocate(BaseCompiler* bc, RegI32 rv, const Temps& temps) { |
| 1719 |
bc->freeI32(rv); |
| 1720 |
bc->maybeFree(temps.t0); |
| 1721 |
bc->maybeFree(temps.t1); |
| 1722 |
bc->maybeFree(temps.t2); |
| 1723 |
} |
| 1724 |
|
| 1725 |
#elif defined(JS_CODEGEN_RISCV64) |
| 1726 |
|
| 1727 |
struct Temps { |
| 1728 |
RegI32 t0, t1, t2; |
| 1729 |
}; |
| 1730 |
|
| 1731 |
static void PopAndAllocate(BaseCompiler* bc, ValType type, |
| 1732 |
Scalar::Type viewType, RegI32* rd, RegI32* rv, |
| 1733 |
Temps* temps) { |
| 1734 |
*rv = (type == ValType::I64) ? bc->popI64ToI32() : bc->popI32(); |
| 1735 |
if (Scalar::byteSize(viewType) < 4) { |
| 1736 |
temps->t0 = bc->needI32(); |
| 1737 |
temps->t1 = bc->needI32(); |
| 1738 |
temps->t2 = bc->needI32(); |
| 1739 |
} |
| 1740 |
*rd = bc->needI32(); |
| 1741 |
} |
| 1742 |
|
| 1743 |
static void Perform(BaseCompiler* bc, const MemoryAccessDesc& access, |
| 1744 |
Address srcAddr, RegI32 rv, RegI32 rd, const Temps& temps) { |
| 1745 |
bc->masm.wasmAtomicExchange(access, srcAddr, rv, temps.t0, temps.t1, temps.t2, |
| 1746 |
rd); |
| 1747 |
} |
| 1748 |
|
| 1749 |
static void Deallocate(BaseCompiler* bc, RegI32 rv, const Temps& temps) { |
| 1750 |
bc->freeI32(rv); |
| 1751 |
bc->maybeFree(temps.t0); |
| 1752 |
bc->maybeFree(temps.t1); |
| 1753 |
bc->maybeFree(temps.t2); |
| 1754 |
} |
| 1755 |
|
| 1756 |
#elif defined(JS_CODEGEN_NONE) || defined(JS_CODEGEN_WASM32) |
| 1757 |
|
| 1758 |
using Temps = Nothing; |
| 1759 |
|
| 1760 |
static void PopAndAllocate(BaseCompiler*, ValType, Scalar::Type, RegI32*, |
| 1761 |
RegI32*, Temps*) {} |
| 1762 |
static void Perform(BaseCompiler*, const MemoryAccessDesc&, Address, RegI32, |
| 1763 |
RegI32, const Temps&) {} |
| 1764 |
static void Deallocate(BaseCompiler*, RegI32, const Temps&) {} |
| 1765 |
|
| 1766 |
#endif |
| 1767 |
|
| 1768 |
} // namespace atomic_xchg32 |
| 1769 |
|
| 1770 |
template <typename RegAddressType> |
| 1771 |
void BaseCompiler::atomicXchg32(MemoryAccessDesc* access, ValType type) { |
| 1772 |
Scalar::Type viewType = access->type(); |
| 1773 |
|
| 1774 |
RegI32 rd, rv; |
| 1775 |
atomic_xchg32::Temps temps; |
| 1776 |
atomic_xchg32::PopAndAllocate(this, type, viewType, &rd, &rv, &temps); |
| 1777 |
|
| 1778 |
AccessCheck check; |
| 1779 |
|
| 1780 |
RegAddressType rp = popMemoryAccess<RegAddressType>(access, &check); |
| 1781 |
RegPtr instance = maybeLoadInstanceForAccess(access, check); |
| 1782 |
|
| 1783 |
auto memaddr = prepareAtomicMemoryAccess(access, &check, instance, rp); |
| 1784 |
atomic_xchg32::Perform(this, *access, memaddr, rv, rd, temps); |
| 1785 |
|
| 1786 |
#ifndef RABALDR_PIN_INSTANCE |
| 1787 |
maybeFree(instance); |
| 1788 |
#endif |
| 1789 |
free(rp); |
| 1790 |
atomic_xchg32::Deallocate(this, rv, temps); |
| 1791 |
|
| 1792 |
if (type == ValType::I64) { |
| 1793 |
pushU32AsI64(rd); |
| 1794 |
} else { |
| 1795 |
pushI32(rd); |
| 1796 |
} |
| 1797 |
} |
| 1798 |
|
| 1799 |
namespace atomic_xchg64 { |
| 1800 |
|
| 1801 |
#if defined(JS_CODEGEN_X64) |
| 1802 |
|
| 1803 |
static void PopAndAllocate(BaseCompiler* bc, RegI64* rd, RegI64* rv) { |
| 1804 |
*rv = bc->popI64(); |
| 1805 |
*rd = *rv; |
| 1806 |
} |
| 1807 |
|
| 1808 |
static void Deallocate(BaseCompiler* bc, RegI64 rd, RegI64) { |
| 1809 |
bc->maybeFree(rd); |
| 1810 |
} |
| 1811 |
|
| 1812 |
#elif defined(JS_CODEGEN_X86) |
| 1813 |
|
| 1814 |
// Register allocation is tricky in several ways. |
| 1815 |
// |
| 1816 |
// - For a 64-bit access on memory64 we need six registers for rd, rv, and rp, |
| 1817 |
// but have only five (as the temp ebx is needed too), so we target all |
| 1818 |
// registers explicitly to make sure there's space. |
| 1819 |
// |
| 1820 |
// - We'll be using cmpxchg8b, and when we do the operation, rv must be in |
| 1821 |
// ecx:ebx, and rd must be edx:eax. We can't use ebx for rv initially because |
| 1822 |
// we need ebx for a scratch also, so use a separate temp and move the value |
| 1823 |
// to ebx just before the operation. |
| 1824 |
// |
| 1825 |
// In sum: |
| 1826 |
// |
| 1827 |
// - Initially rv=ecx:edx and eax is reserved, rd=unallocated. |
| 1828 |
// - Then rp is popped into esi+edi because those are the only available. |
| 1829 |
// - The Setup operation makes rv=ecx:ebx and rd=edx:eax and moves edx->ebx. |
| 1830 |
// - Deallocation then frees only the ecx part of rv. |
| 1831 |
|
| 1832 |
static void PopAndAllocate(BaseCompiler* bc, RegI64* rd, RegI64* rv) { |
| 1833 |
bc->needI32(bc->specific_.ecx); |
| 1834 |
bc->needI32(bc->specific_.edx); |
| 1835 |
bc->needI32(bc->specific_.eax); |
| 1836 |
*rv = RegI64(Register64(bc->specific_.ecx, bc->specific_.edx)); |
| 1837 |
bc->popI64ToSpecific(*rv); |
| 1838 |
} |
| 1839 |
|
| 1840 |
static void Setup(BaseCompiler* bc, RegI64* rv, RegI64* rd, |
| 1841 |
const ScratchAtomicNoHeapReg& scratch) { |
| 1842 |
MOZ_ASSERT(rv->high == bc->specific_.ecx); |
| 1843 |
MOZ_ASSERT(Register(scratch) == js::jit::ebx); |
| 1844 |
bc->masm.move32(rv->low, scratch); |
| 1845 |
*rv = bc->specific_.ecx_ebx; |
| 1846 |
*rd = bc->specific_.edx_eax; |
| 1847 |
} |
| 1848 |
|
| 1849 |
static void Deallocate(BaseCompiler* bc, RegI64 rd, RegI64 rv) { |
| 1850 |
MOZ_ASSERT(rd == bc->specific_.edx_eax || rd == RegI64::Invalid()); |
| 1851 |
bc->maybeFree(rd); |
| 1852 |
bc->freeI32(bc->specific_.ecx); |
| 1853 |
} |
| 1854 |
|
| 1855 |
#elif defined(JS_CODEGEN_ARM64) || defined(JS_CODEGEN_MIPS64) || \ |
| 1856 |
defined(JS_CODEGEN_LOONG64) |
| 1857 |
|
| 1858 |
static void PopAndAllocate(BaseCompiler* bc, RegI64* rd, RegI64* rv) { |
| 1859 |
*rv = bc->popI64(); |
| 1860 |
*rd = bc->needI64(); |
| 1861 |
} |
| 1862 |
|
| 1863 |
static void Deallocate(BaseCompiler* bc, RegI64 rd, RegI64 rv) { |
| 1864 |
bc->freeI64(rv); |
| 1865 |
bc->maybeFree(rd); |
| 1866 |
} |
| 1867 |
|
| 1868 |
#elif defined(JS_CODEGEN_ARM) |
| 1869 |
|
| 1870 |
static void PopAndAllocate(BaseCompiler* bc, RegI64* rd, RegI64* rv) { |
| 1871 |
// Both rv and rd must be odd/even pairs. |
| 1872 |
*rv = bc->popI64ToSpecific(bc->needI64Pair()); |
| 1873 |
*rd = bc->needI64Pair(); |
| 1874 |
} |
| 1875 |
|
| 1876 |
static void Deallocate(BaseCompiler* bc, RegI64 rd, RegI64 rv) { |
| 1877 |
bc->freeI64(rv); |
| 1878 |
bc->maybeFree(rd); |
| 1879 |
} |
| 1880 |
|
| 1881 |
#elif defined(JS_CODEGEN_RISCV64) |
| 1882 |
|
| 1883 |
static void PopAndAllocate(BaseCompiler* bc, RegI64* rd, RegI64* rv) { |
| 1884 |
*rv = bc->popI64(); |
| 1885 |
*rd = bc->needI64(); |
| 1886 |
} |
| 1887 |
|
| 1888 |
static void Deallocate(BaseCompiler* bc, RegI64 rd, RegI64 rv) { |
| 1889 |
bc->freeI64(rv); |
| 1890 |
bc->maybeFree(rd); |
| 1891 |
} |
| 1892 |
#elif defined(JS_CODEGEN_NONE) || defined(JS_CODEGEN_WASM32) |
| 1893 |
|
| 1894 |
static void PopAndAllocate(BaseCompiler*, RegI64*, RegI64*) {} |
| 1895 |
static void Deallocate(BaseCompiler*, RegI64, RegI64) {} |
| 1896 |
|
| 1897 |
#endif |
| 1898 |
|
| 1899 |
} // namespace atomic_xchg64 |
| 1900 |
|
| 1901 |
template <typename RegAddressType> |
| 1902 |
void BaseCompiler::atomicXchg64(MemoryAccessDesc* access, |
| 1903 |
WantResult wantResult) { |
| 1904 |
RegI64 rd, rv; |
| 1905 |
atomic_xchg64::PopAndAllocate(this, &rd, &rv); |
| 1906 |
|
| 1907 |
AccessCheck check; |
| 1908 |
RegAddressType rp = popMemoryAccess<RegAddressType>(access, &check); |
| 1909 |
|
| 1910 |
#ifdef WASM_HAS_HEAPREG |
| 1911 |
RegPtr instance = maybeLoadInstanceForAccess(access, check); |
| 1912 |
auto memaddr = |
| 1913 |
prepareAtomicMemoryAccess<RegAddressType>(access, &check, instance, rp); |
| 1914 |
masm.wasmAtomicExchange64(*access, memaddr, rv, rd); |
| 1915 |
# ifndef RABALDR_PIN_INSTANCE |
| 1916 |
maybeFree(instance); |
| 1917 |
# endif |
| 1918 |
#else |
| 1919 |
ScratchAtomicNoHeapReg scratch(*this); |
| 1920 |
RegPtr instance = |
| 1921 |
maybeLoadInstanceForAccess(access, check, RegIntptrToRegPtr(scratch)); |
| 1922 |
Address memaddr = prepareAtomicMemoryAccess(access, &check, instance, rp); |
| 1923 |
atomic_xchg64::Setup(this, &rv, &rd, scratch); |
| 1924 |
masm.wasmAtomicExchange64(*access, memaddr, rv, rd); |
| 1925 |
MOZ_ASSERT(instance == scratch); |
| 1926 |
#endif |
| 1927 |
|
| 1928 |
free(rp); |
| 1929 |
if (wantResult) { |
| 1930 |
pushI64(rd); |
| 1931 |
rd = RegI64::Invalid(); |
| 1932 |
} |
| 1933 |
atomic_xchg64::Deallocate(this, rd, rv); |
| 1934 |
} |
| 1935 |
|
| 1936 |
////////////////////////////////////////////////////////////////////////////// |
| 1937 |
// |
| 1938 |
// Atomic compare-exchange. |
| 1939 |
|
| 1940 |
void BaseCompiler::atomicCmpXchg(MemoryAccessDesc* access, ValType type) { |
| 1941 |
Scalar::Type viewType = access->type(); |
| 1942 |
if (Scalar::byteSize(viewType) <= 4) { |
| 1943 |
if (isMem32(access->memoryIndex())) { |
| 1944 |
atomicCmpXchg32<RegI32>(access, type); |
| 1945 |
} else { |
| 1946 |
#ifdef ENABLE_WASM_MEMORY64 |
| 1947 |
atomicCmpXchg32<RegI64>(access, type); |
| 1948 |
#else |
| 1949 |
MOZ_CRASH("Memory64 not enabled / supported on this platform"); |
| 1950 |
#endif |
| 1951 |
} |
| 1952 |
} else { |
| 1953 |
MOZ_ASSERT(type == ValType::I64 && Scalar::byteSize(viewType) == 8); |
| 1954 |
if (isMem32(access->memoryIndex())) { |
| 1955 |
atomicCmpXchg64<RegI32>(access, type); |
| 1956 |
} else { |
| 1957 |
#ifdef ENABLE_WASM_MEMORY64 |
| 1958 |
atomicCmpXchg64<RegI64>(access, type); |
| 1959 |
#else |
| 1960 |
MOZ_CRASH("Memory64 not enabled / supported on this platform"); |
| 1961 |
#endif |
| 1962 |
} |
| 1963 |
} |
| 1964 |
} |
| 1965 |
|
| 1966 |
namespace atomic_cmpxchg32 { |
| 1967 |
|
| 1968 |
#if defined(JS_CODEGEN_X64) || defined(JS_CODEGEN_X86) |
| 1969 |
|
| 1970 |
using Temps = Nothing; |
| 1971 |
|
| 1972 |
static void PopAndAllocate(BaseCompiler* bc, ValType type, |
| 1973 |
Scalar::Type viewType, RegI32* rexpect, RegI32* rnew, |
| 1974 |
RegI32* rd, Temps*) { |
| 1975 |
// For cmpxchg, the expected value and the result are both in eax. |
| 1976 |
bc->needI32(bc->specific_.eax); |
| 1977 |
if (type == ValType::I64) { |
| 1978 |
*rnew = bc->popI64ToI32(); |
| 1979 |
*rexpect = bc->popI64ToSpecificI32(bc->specific_.eax); |
| 1980 |
} else { |
| 1981 |
*rnew = bc->popI32(); |
| 1982 |
*rexpect = bc->popI32ToSpecific(bc->specific_.eax); |
| 1983 |
} |
| 1984 |
*rd = *rexpect; |
| 1985 |
} |
| 1986 |
|
| 1987 |
template <typename T> |
| 1988 |
static void Perform(BaseCompiler* bc, const MemoryAccessDesc& access, T srcAddr, |
| 1989 |
RegI32 rexpect, RegI32 rnew, RegI32 rd, const Temps&) { |
| 1990 |
# if defined(JS_CODEGEN_X86) |
| 1991 |
ScratchI8 scratch(*bc); |
| 1992 |
if (access.type() == Scalar::Uint8) { |
| 1993 |
MOZ_ASSERT(rd == bc->specific_.eax); |
| 1994 |
if (!bc->ra.isSingleByteI32(rnew)) { |
| 1995 |
// The replacement value must have a byte persona. |
| 1996 |
bc->masm.movl(rnew, scratch); |
| 1997 |
rnew = scratch; |
| 1998 |
} |
| 1999 |
} |
| 2000 |
# endif |
| 2001 |
bc->masm.wasmCompareExchange(access, srcAddr, rexpect, rnew, rd); |
| 2002 |
} |
| 2003 |
|
| 2004 |
static void Deallocate(BaseCompiler* bc, RegI32, RegI32 rnew, const Temps&) { |
| 2005 |
bc->freeI32(rnew); |
| 2006 |
} |
| 2007 |
|
| 2008 |
#elif defined(JS_CODEGEN_ARM) || defined(JS_CODEGEN_ARM64) |
| 2009 |
|
| 2010 |
using Temps = Nothing; |
| 2011 |
|
| 2012 |
static void PopAndAllocate(BaseCompiler* bc, ValType type, |
| 2013 |
Scalar::Type viewType, RegI32* rexpect, RegI32* rnew, |
| 2014 |
RegI32* rd, Temps*) { |
| 2015 |
if (type == ValType::I64) { |
| 2016 |
*rnew = bc->popI64ToI32(); |
| 2017 |
*rexpect = bc->popI64ToI32(); |
| 2018 |
} else { |
| 2019 |
*rnew = bc->popI32(); |
| 2020 |
*rexpect = bc->popI32(); |
| 2021 |
} |
| 2022 |
*rd = bc->needI32(); |
| 2023 |
} |
| 2024 |
|
| 2025 |
static void Perform(BaseCompiler* bc, const MemoryAccessDesc& access, |
| 2026 |
Address srcAddr, RegI32 rexpect, RegI32 rnew, RegI32 rd, |
| 2027 |
const Temps&) { |
| 2028 |
bc->masm.wasmCompareExchange(access, srcAddr, rexpect, rnew, rd); |
| 2029 |
} |
| 2030 |
|
| 2031 |
static void Deallocate(BaseCompiler* bc, RegI32 rexpect, RegI32 rnew, |
| 2032 |
const Temps&) { |
| 2033 |
bc->freeI32(rnew); |
| 2034 |
bc->freeI32(rexpect); |
| 2035 |
} |
| 2036 |
|
| 2037 |
#elif defined(JS_CODEGEN_MIPS64) || defined(JS_CODEGEN_LOONG64) |
| 2038 |
|
| 2039 |
struct Temps { |
| 2040 |
RegI32 t0, t1, t2; |
| 2041 |
}; |
| 2042 |
|
| 2043 |
static void PopAndAllocate(BaseCompiler* bc, ValType type, |
| 2044 |
Scalar::Type viewType, RegI32* rexpect, RegI32* rnew, |
| 2045 |
RegI32* rd, Temps* temps) { |
| 2046 |
if (type == ValType::I64) { |
| 2047 |
*rnew = bc->popI64ToI32(); |
| 2048 |
*rexpect = bc->popI64ToI32(); |
| 2049 |
} else { |
| 2050 |
*rnew = bc->popI32(); |
| 2051 |
*rexpect = bc->popI32(); |
| 2052 |
} |
| 2053 |
if (Scalar::byteSize(viewType) < 4) { |
| 2054 |
temps->t0 = bc->needI32(); |
| 2055 |
temps->t1 = bc->needI32(); |
| 2056 |
temps->t2 = bc->needI32(); |
| 2057 |
} |
| 2058 |
*rd = bc->needI32(); |
| 2059 |
} |
| 2060 |
|
| 2061 |
static void Perform(BaseCompiler* bc, const MemoryAccessDesc& access, |
| 2062 |
Address srcAddr, RegI32 rexpect, RegI32 rnew, RegI32 rd, |
| 2063 |
const Temps& temps) { |
| 2064 |
bc->masm.wasmCompareExchange(access, srcAddr, rexpect, rnew, temps.t0, |
| 2065 |
temps.t1, temps.t2, rd); |
| 2066 |
} |
| 2067 |
|
| 2068 |
static void Deallocate(BaseCompiler* bc, RegI32 rexpect, RegI32 rnew, |
| 2069 |
const Temps& temps) { |
| 2070 |
bc->freeI32(rnew); |
| 2071 |
bc->freeI32(rexpect); |
| 2072 |
bc->maybeFree(temps.t0); |
| 2073 |
bc->maybeFree(temps.t1); |
| 2074 |
bc->maybeFree(temps.t2); |
| 2075 |
} |
| 2076 |
|
| 2077 |
#elif defined(JS_CODEGEN_RISCV64) |
| 2078 |
|
| 2079 |
struct Temps { |
| 2080 |
RegI32 t0, t1, t2; |
| 2081 |
}; |
| 2082 |
|
| 2083 |
static void PopAndAllocate(BaseCompiler* bc, ValType type, |
| 2084 |
Scalar::Type viewType, RegI32* rexpect, RegI32* rnew, |
| 2085 |
RegI32* rd, Temps* temps) { |
| 2086 |
if (type == ValType::I64) { |
| 2087 |
*rnew = bc->popI64ToI32(); |
| 2088 |
*rexpect = bc->popI64ToI32(); |
| 2089 |
} else { |
| 2090 |
*rnew = bc->popI32(); |
| 2091 |
*rexpect = bc->popI32(); |
| 2092 |
} |
| 2093 |
if (Scalar::byteSize(viewType) < 4) { |
| 2094 |
temps->t0 = bc->needI32(); |
| 2095 |
temps->t1 = bc->needI32(); |
| 2096 |
temps->t2 = bc->needI32(); |
| 2097 |
} |
| 2098 |
*rd = bc->needI32(); |
| 2099 |
} |
| 2100 |
|
| 2101 |
static void Perform(BaseCompiler* bc, const MemoryAccessDesc& access, |
| 2102 |
Address srcAddr, RegI32 rexpect, RegI32 rnew, RegI32 rd, |
| 2103 |
const Temps& temps) { |
| 2104 |
bc->masm.wasmCompareExchange(access, srcAddr, rexpect, rnew, temps.t0, |
| 2105 |
temps.t1, temps.t2, rd); |
| 2106 |
} |
| 2107 |
|
| 2108 |
static void Deallocate(BaseCompiler* bc, RegI32 rexpect, RegI32 rnew, |
| 2109 |
const Temps& temps) { |
| 2110 |
bc->freeI32(rnew); |
| 2111 |
bc->freeI32(rexpect); |
| 2112 |
bc->maybeFree(temps.t0); |
| 2113 |
bc->maybeFree(temps.t1); |
| 2114 |
bc->maybeFree(temps.t2); |
| 2115 |
} |
| 2116 |
|
| 2117 |
#elif defined(JS_CODEGEN_NONE) || defined(JS_CODEGEN_WASM32) |
| 2118 |
|
| 2119 |
using Temps = Nothing; |
| 2120 |
|
| 2121 |
static void PopAndAllocate(BaseCompiler*, ValType, Scalar::Type, RegI32*, |
| 2122 |
RegI32*, RegI32*, Temps*) {} |
| 2123 |
|
| 2124 |
static void Perform(BaseCompiler*, const MemoryAccessDesc&, Address, RegI32, |
| 2125 |
RegI32, RegI32, const Temps& temps) {} |
| 2126 |
|
| 2127 |
static void Deallocate(BaseCompiler*, RegI32, RegI32, const Temps&) {} |
| 2128 |
|
| 2129 |
#endif |
| 2130 |
|
| 2131 |
} // namespace atomic_cmpxchg32 |
| 2132 |
|
| 2133 |
template <typename RegAddressType> |
| 2134 |
void BaseCompiler::atomicCmpXchg32(MemoryAccessDesc* access, ValType type) { |
| 2135 |
Scalar::Type viewType = access->type(); |
| 2136 |
RegI32 rexpect, rnew, rd; |
| 2137 |
atomic_cmpxchg32::Temps temps; |
| 2138 |
atomic_cmpxchg32::PopAndAllocate(this, type, viewType, &rexpect, &rnew, &rd, |
| 2139 |
&temps); |
| 2140 |
|
| 2141 |
AccessCheck check; |
| 2142 |
RegAddressType rp = popMemoryAccess<RegAddressType>(access, &check); |
| 2143 |
RegPtr instance = maybeLoadInstanceForAccess(access, check); |
| 2144 |
|
| 2145 |
auto memaddr = prepareAtomicMemoryAccess(access, &check, instance, rp); |
| 2146 |
atomic_cmpxchg32::Perform(this, *access, memaddr, rexpect, rnew, rd, temps); |
| 2147 |
|
| 2148 |
#ifndef RABALDR_PIN_INSTANCE |
| 2149 |
maybeFree(instance); |
| 2150 |
#endif |
| 2151 |
free(rp); |
| 2152 |
atomic_cmpxchg32::Deallocate(this, rexpect, rnew, temps); |
| 2153 |
|
| 2154 |
if (type == ValType::I64) { |
| 2155 |
pushU32AsI64(rd); |
| 2156 |
} else { |
| 2157 |
pushI32(rd); |
| 2158 |
} |
| 2159 |
} |
| 2160 |
|
| 2161 |
namespace atomic_cmpxchg64 { |
| 2162 |
|
| 2163 |
// The templates are needed for x86 code generation, which needs complicated |
| 2164 |
// register allocation for memory64. |
| 2165 |
|
| 2166 |
template <typename RegAddressType> |
| 2167 |
static void PopAndAllocate(BaseCompiler* bc, RegI64* rexpect, RegI64* rnew, |
| 2168 |
RegI64* rd); |
| 2169 |
|
| 2170 |
template <typename RegAddressType> |
| 2171 |
static void Deallocate(BaseCompiler* bc, RegI64 rexpect, RegI64 rnew); |
| 2172 |
|
| 2173 |
#if defined(JS_CODEGEN_X64) |
| 2174 |
|
| 2175 |
template <typename RegAddressType> |
| 2176 |
static void PopAndAllocate(BaseCompiler* bc, RegI64* rexpect, RegI64* rnew, |
| 2177 |
RegI64* rd) { |
| 2178 |
// For cmpxchg, the expected value and the result are both in rax. |
| 2179 |
bc->needI64(bc->specific_.rax); |
| 2180 |
*rnew = bc->popI64(); |
| 2181 |
*rexpect = bc->popI64ToSpecific(bc->specific_.rax); |
| 2182 |
*rd = *rexpect; |
| 2183 |
} |
| 2184 |
|
| 2185 |
static void Perform(BaseCompiler* bc, const MemoryAccessDesc& access, |
| 2186 |
Address srcAddr, RegI64 rexpect, RegI64 rnew, RegI64 rd) { |
| 2187 |
bc->masm.wasmCompareExchange64(access, srcAddr, rexpect, rnew, rd); |
| 2188 |
} |
| 2189 |
|
| 2190 |
template <typename RegAddressType> |
| 2191 |
static void Deallocate(BaseCompiler* bc, RegI64 rexpect, RegI64 rnew) { |
| 2192 |
bc->freeI64(rnew); |
| 2193 |
} |
| 2194 |
|
| 2195 |
#elif defined(JS_CODEGEN_X86) |
| 2196 |
|
| 2197 |
template <typename RegAddressType> |
| 2198 |
static void Perform(BaseCompiler* bc, const MemoryAccessDesc& access, |
| 2199 |
Address srcAddr, RegI64 rexpect, RegI64 rnew, RegI64 rd, |
| 2200 |
ScratchAtomicNoHeapReg& scratch); |
| 2201 |
|
| 2202 |
// Memory32: For cmpxchg8b, the expected value and the result are both in |
| 2203 |
// edx:eax, and the replacement value is in ecx:ebx. But we can't allocate ebx |
| 2204 |
// initially because we need it later for a scratch, so instead we allocate a |
| 2205 |
// temp to hold the low word of 'new'. |
| 2206 |
|
| 2207 |
template <> |
| 2208 |
void PopAndAllocate<RegI32>(BaseCompiler* bc, RegI64* rexpect, RegI64* rnew, |
| 2209 |
RegI64* rd) { |
| 2210 |
bc->needI64(bc->specific_.edx_eax); |
| 2211 |
bc->needI32(bc->specific_.ecx); |
| 2212 |
RegI32 tmp = bc->needI32(); |
| 2213 |
*rnew = bc->popI64ToSpecific(RegI64(Register64(bc->specific_.ecx, tmp))); |
| 2214 |
*rexpect = bc->popI64ToSpecific(bc->specific_.edx_eax); |
| 2215 |
*rd = *rexpect; |
| 2216 |
} |
| 2217 |
|
| 2218 |
template <> |
| 2219 |
void Perform<RegI32>(BaseCompiler* bc, const MemoryAccessDesc& access, |
| 2220 |
Address srcAddr, RegI64 rexpect, RegI64 rnew, RegI64 rd, |
| 2221 |
ScratchAtomicNoHeapReg& scratch) { |
| 2222 |
MOZ_ASSERT(Register(scratch) == js::jit::ebx); |
| 2223 |
MOZ_ASSERT(rnew.high == bc->specific_.ecx); |
| 2224 |
bc->masm.move32(rnew.low, ebx); |
| 2225 |
bc->masm.wasmCompareExchange64(access, srcAddr, rexpect, |
| 2226 |
bc->specific_.ecx_ebx, rd); |
| 2227 |
} |
| 2228 |
|
| 2229 |
template <> |
| 2230 |
void Deallocate<RegI32>(BaseCompiler* bc, RegI64 rexpect, RegI64 rnew) { |
| 2231 |
bc->freeI64(rnew); |
| 2232 |
} |
| 2233 |
|
| 2234 |
// Memory64: Register allocation is particularly hairy here. With memory64, we |
| 2235 |
// have up to seven live values: i64 expected-value, i64 new-value, i64 pointer, |
| 2236 |
// and instance. The instance can use the scratch but there's no avoiding that |
| 2237 |
// we'll run out of registers. |
| 2238 |
// |
| 2239 |
// Unlike for the rmw ops, we can't use edx as the rnew.low since it's used |
| 2240 |
// for the rexpect.high. And we can't push anything onto the stack while we're |
| 2241 |
// popping the memory address because the memory address may be on the stack. |
| 2242 |
|
| 2243 |
# ifdef ENABLE_WASM_MEMORY64 |
| 2244 |
template <> |
| 2245 |
void PopAndAllocate<RegI64>(BaseCompiler* bc, RegI64* rexpect, RegI64* rnew, |
| 2246 |
RegI64* rd) { |
| 2247 |
// We reserve these (and ebx). The 64-bit pointer will end up in esi+edi. |
| 2248 |
bc->needI32(bc->specific_.eax); |
| 2249 |
bc->needI32(bc->specific_.ecx); |
| 2250 |
bc->needI32(bc->specific_.edx); |
| 2251 |
|
| 2252 |
// Pop the 'new' value and stash it in the instance scratch area. Do not |
| 2253 |
// initialize *rnew to anything. |
| 2254 |
RegI64 tmp(Register64(bc->specific_.ecx, bc->specific_.edx)); |
| 2255 |
bc->popI64ToSpecific(tmp); |
| 2256 |
{ |
| 2257 |
ScratchPtr instanceScratch(*bc); |
| 2258 |
bc->stashI64(instanceScratch, tmp); |
| 2259 |
} |
| 2260 |
|
| 2261 |
*rexpect = bc->popI64ToSpecific(bc->specific_.edx_eax); |
| 2262 |
*rd = *rexpect; |
| 2263 |
} |
| 2264 |
|
| 2265 |
template <> |
| 2266 |
void Perform<RegI64>(BaseCompiler* bc, const MemoryAccessDesc& access, |
| 2267 |
Address srcAddr, RegI64 rexpect, RegI64 rnew, RegI64 rd, |
| 2268 |
ScratchAtomicNoHeapReg& scratch) { |
| 2269 |
MOZ_ASSERT(rnew.isInvalid()); |
| 2270 |
rnew = bc->specific_.ecx_ebx; |
| 2271 |
|
| 2272 |
bc->unstashI64(RegPtr(Register(bc->specific_.ecx)), rnew); |
| 2273 |
bc->masm.wasmCompareExchange64(access, srcAddr, rexpect, rnew, rd); |
| 2274 |
} |
| 2275 |
|
| 2276 |
template <> |
| 2277 |
void Deallocate<RegI64>(BaseCompiler* bc, RegI64 rexpect, RegI64 rnew) { |
| 2278 |
// edx:ebx have been pushed as the result, and the pointer was freed |
| 2279 |
// separately in the caller, so just free ecx. |
| 2280 |
bc->free(bc->specific_.ecx); |
| 2281 |
} |
| 2282 |
# endif |
| 2283 |
|
| 2284 |
#elif defined(JS_CODEGEN_ARM) |
| 2285 |
|
| 2286 |
template <typename RegAddressType> |
| 2287 |
static void PopAndAllocate(BaseCompiler* bc, RegI64* rexpect, RegI64* rnew, |
| 2288 |
RegI64* rd) { |
| 2289 |
// The replacement value and the result must both be odd/even pairs. |
| 2290 |
*rnew = bc->popI64Pair(); |
| 2291 |
*rexpect = bc->popI64(); |
| 2292 |
*rd = bc->needI64Pair(); |
| 2293 |
} |
| 2294 |
|
| 2295 |
static void Perform(BaseCompiler* bc, const MemoryAccessDesc& access, |
| 2296 |
Address srcAddr, RegI64 rexpect, RegI64 rnew, RegI64 rd) { |
| 2297 |
bc->masm.wasmCompareExchange64(access, srcAddr, rexpect, rnew, rd); |
| 2298 |
} |
| 2299 |
|
| 2300 |
template <typename RegAddressType> |
| 2301 |
static void Deallocate(BaseCompiler* bc, RegI64 rexpect, RegI64 rnew) { |
| 2302 |
bc->freeI64(rexpect); |
| 2303 |
bc->freeI64(rnew); |
| 2304 |
} |
| 2305 |
|
| 2306 |
#elif defined(JS_CODEGEN_ARM64) || defined(JS_CODEGEN_MIPS64) || \ |
| 2307 |
defined(JS_CODEGEN_LOONG64) |
| 2308 |
|
| 2309 |
template <typename RegAddressType> |
| 2310 |
static void PopAndAllocate(BaseCompiler* bc, RegI64* rexpect, RegI64* rnew, |
| 2311 |
RegI64* rd) { |
| 2312 |
*rnew = bc->popI64(); |
| 2313 |
*rexpect = bc->popI64(); |
| 2314 |
*rd = bc->needI64(); |
| 2315 |
} |
| 2316 |
|
| 2317 |
static void Perform(BaseCompiler* bc, const MemoryAccessDesc& access, |
| 2318 |
Address srcAddr, RegI64 rexpect, RegI64 rnew, RegI64 rd) { |
| 2319 |
bc->masm.wasmCompareExchange64(access, srcAddr, rexpect, rnew, rd); |
| 2320 |
} |
| 2321 |
|
| 2322 |
template <typename RegAddressType> |
| 2323 |
static void Deallocate(BaseCompiler* bc, RegI64 rexpect, RegI64 rnew) { |
| 2324 |
bc->freeI64(rexpect); |
| 2325 |
bc->freeI64(rnew); |
| 2326 |
} |
| 2327 |
|
| 2328 |
#elif defined(JS_CODEGEN_RISCV64) |
| 2329 |
|
| 2330 |
template <typename RegAddressType> |
| 2331 |
static void PopAndAllocate(BaseCompiler* bc, RegI64* rexpect, RegI64* rnew, |
| 2332 |
RegI64* rd) { |
| 2333 |
*rnew = bc->popI64(); |
| 2334 |
*rexpect = bc->popI64(); |
| 2335 |
*rd = bc->needI64(); |
| 2336 |
} |
| 2337 |
|
| 2338 |
static void Perform(BaseCompiler* bc, const MemoryAccessDesc& access, |
| 2339 |
Address srcAddr, RegI64 rexpect, RegI64 rnew, RegI64 rd) { |
| 2340 |
bc->masm.wasmCompareExchange64(access, srcAddr, rexpect, rnew, rd); |
| 2341 |
} |
| 2342 |
|
| 2343 |
template <typename RegAddressType> |
| 2344 |
static void Deallocate(BaseCompiler* bc, RegI64 rexpect, RegI64 rnew) { |
| 2345 |
bc->freeI64(rexpect); |
| 2346 |
bc->freeI64(rnew); |
| 2347 |
} |
| 2348 |
|
| 2349 |
#elif defined(JS_CODEGEN_NONE) || defined(JS_CODEGEN_WASM32) |
| 2350 |
|
| 2351 |
template <typename RegAddressType> |
| 2352 |
static void PopAndAllocate(BaseCompiler* bc, RegI64* rexpect, RegI64* rnew, |
| 2353 |
RegI64* rd) {} |
| 2354 |
static void Perform(BaseCompiler* bc, const MemoryAccessDesc& access, |
| 2355 |
Address srcAddr, RegI64 rexpect, RegI64 rnew, RegI64 rd) {} |
| 2356 |
template <typename RegAddressType> |
| 2357 |
static void Deallocate(BaseCompiler* bc, RegI64 rexpect, RegI64 rnew) {} |
| 2358 |
|
| 2359 |
#endif |
| 2360 |
|
| 2361 |
} // namespace atomic_cmpxchg64 |
| 2362 |
|
| 2363 |
template <typename RegAddressType> |
| 2364 |
void BaseCompiler::atomicCmpXchg64(MemoryAccessDesc* access, ValType type) { |
| 2365 |
RegI64 rexpect, rnew, rd; |
| 2366 |
atomic_cmpxchg64::PopAndAllocate<RegAddressType>(this, &rexpect, &rnew, &rd); |
| 2367 |
|
| 2368 |
AccessCheck check; |
| 2369 |
RegAddressType rp = popMemoryAccess<RegAddressType>(access, &check); |
| 2370 |
|
| 2371 |
#ifdef WASM_HAS_HEAPREG |
| 2372 |
RegPtr instance = maybeLoadInstanceForAccess(access, check); |
| 2373 |
auto memaddr = prepareAtomicMemoryAccess(access, &check, instance, rp); |
| 2374 |
atomic_cmpxchg64::Perform(this, *access, memaddr, rexpect, rnew, rd); |
| 2375 |
# ifndef RABALDR_PIN_INSTANCE |
| 2376 |
maybeFree(instance); |
| 2377 |
# endif |
| 2378 |
#else |
| 2379 |
ScratchAtomicNoHeapReg scratch(*this); |
| 2380 |
RegPtr instance = |
| 2381 |
maybeLoadInstanceForAccess(access, check, RegIntptrToRegPtr(scratch)); |
| 2382 |
Address memaddr = prepareAtomicMemoryAccess(access, &check, instance, rp); |
| 2383 |
atomic_cmpxchg64::Perform<RegAddressType>(this, *access, memaddr, rexpect, |
| 2384 |
rnew, rd, scratch); |
| 2385 |
MOZ_ASSERT(instance == scratch); |
| 2386 |
#endif |
| 2387 |
|
| 2388 |
free(rp); |
| 2389 |
atomic_cmpxchg64::Deallocate<RegAddressType>(this, rexpect, rnew); |
| 2390 |
|
| 2391 |
pushI64(rd); |
| 2392 |
} |
| 2393 |
|
| 2394 |
////////////////////////////////////////////////////////////////////////////// |
| 2395 |
// |
| 2396 |
// Synchronization. |
| 2397 |
|
| 2398 |
bool BaseCompiler::atomicWait(ValType type, MemoryAccessDesc* access) { |
| 2399 |
switch (type.kind()) { |
| 2400 |
case ValType::I32: { |
| 2401 |
RegI64 timeout = popI64(); |
| 2402 |
RegI32 val = popI32(); |
| 2403 |
|
| 2404 |
if (isMem32(access->memoryIndex())) { |
| 2405 |
computeEffectiveAddress<RegI32>(access); |
| 2406 |
} else { |
| 2407 |
#ifdef ENABLE_WASM_MEMORY64 |
| 2408 |
computeEffectiveAddress<RegI64>(access); |
| 2409 |
#else |
| 2410 |
MOZ_CRASH("Memory64 not enabled / supported on this platform"); |
| 2411 |
#endif |
| 2412 |
} |
| 2413 |
|
| 2414 |
pushI32(val); |
| 2415 |
pushI64(timeout); |
| 2416 |
pushI32(access->memoryIndex()); |
| 2417 |
|
| 2418 |
if (!emitInstanceCall(isMem32(access->memoryIndex()) ? SASigWaitI32M32 |
| 2419 |
: SASigWaitI32M64)) { |
| 2420 |
return false; |
| 2421 |
} |
| 2422 |
break; |
| 2423 |
} |
| 2424 |
case ValType::I64: { |
| 2425 |
RegI64 timeout = popI64(); |
| 2426 |
RegI64 val = popI64(); |
| 2427 |
|
| 2428 |
if (isMem32(access->memoryIndex())) { |
| 2429 |
computeEffectiveAddress<RegI32>(access); |
| 2430 |
} else { |
| 2431 |
#ifdef ENABLE_WASM_MEMORY64 |
| 2432 |
# ifdef JS_CODEGEN_X86 |
| 2433 |
{ |
| 2434 |
ScratchPtr scratch(*this); |
| 2435 |
stashI64(scratch, val); |
| 2436 |
freeI64(val); |
| 2437 |
} |
| 2438 |
# endif |
| 2439 |
computeEffectiveAddress<RegI64>(access); |
| 2440 |
# ifdef JS_CODEGEN_X86 |
| 2441 |
{ |
| 2442 |
ScratchPtr scratch(*this); |
| 2443 |
val = needI64(); |
| 2444 |
unstashI64(scratch, val); |
| 2445 |
} |
| 2446 |
# endif |
| 2447 |
#else |
| 2448 |
MOZ_CRASH("Memory64 not enabled / supported on this platform"); |
| 2449 |
#endif |
| 2450 |
} |
| 2451 |
|
| 2452 |
pushI64(val); |
| 2453 |
pushI64(timeout); |
| 2454 |
pushI32(access->memoryIndex()); |
| 2455 |
|
| 2456 |
if (!emitInstanceCall(isMem32(access->memoryIndex()) ? SASigWaitI64M32 |
| 2457 |
: SASigWaitI64M64)) { |
| 2458 |
return false; |
| 2459 |
} |
| 2460 |
break; |
| 2461 |
} |
| 2462 |
default: |
| 2463 |
MOZ_CRASH(); |
| 2464 |
} |
| 2465 |
|
| 2466 |
return true; |
| 2467 |
} |
| 2468 |
|
| 2469 |
bool BaseCompiler::atomicNotify(MemoryAccessDesc* access) { |
| 2470 |
RegI32 count = popI32(); |
| 2471 |
|
| 2472 |
if (isMem32(access->memoryIndex())) { |
| 2473 |
computeEffectiveAddress<RegI32>(access); |
| 2474 |
RegI32 byteOffset = popI32(); |
| 2475 |
pushI32(byteOffset); |
| 2476 |
} else { |
| 2477 |
#ifdef ENABLE_WASM_MEMORY64 |
| 2478 |
computeEffectiveAddress<RegI64>(access); |
| 2479 |
RegI64 byteOffset = popI64(); |
| 2480 |
pushI64(byteOffset); |
| 2481 |
#else |
| 2482 |
MOZ_CRASH("Memory64 not enabled / supported on this platform"); |
| 2483 |
#endif |
| 2484 |
} |
| 2485 |
|
| 2486 |
pushI32(count); |
| 2487 |
pushI32(access->memoryIndex()); |
| 2488 |
return emitInstanceCall(isMem32(access->memoryIndex()) ? SASigWakeM32 |
| 2489 |
: SASigWakeM64); |
| 2490 |
} |
| 2491 |
|
| 2492 |
////////////////////////////////////////////////////////////////////////////// |
| 2493 |
// |
| 2494 |
// Bulk memory. |
| 2495 |
|
| 2496 |
void BaseCompiler::memCopyInlineM32() { |
| 2497 |
MOZ_ASSERT(MaxInlineMemoryCopyLength != 0); |
| 2498 |
|
| 2499 |
// This function assumes a memory index of zero |
| 2500 |
uint32_t memoryIndex = 0; |
| 2501 |
int32_t signedLength; |
| 2502 |
MOZ_ALWAYS_TRUE(popConst(&signedLength)); |
| 2503 |
uint32_t length = signedLength; |
| 2504 |
MOZ_ASSERT(length != 0 && length <= MaxInlineMemoryCopyLength); |
| 2505 |
|
| 2506 |
RegI32 src = popI32(); |
| 2507 |
RegI32 dest = popI32(); |
| 2508 |
|
| 2509 |
// Compute the number of copies of each width we will need to do |
| 2510 |
size_t remainder = length; |
| 2511 |
#ifdef ENABLE_WASM_SIMD |
| 2512 |
size_t numCopies16 = 0; |
| 2513 |
if (MacroAssembler::SupportsFastUnalignedFPAccesses()) { |
| 2514 |
numCopies16 = remainder / sizeof(V128); |
| 2515 |
remainder %= sizeof(V128); |
| 2516 |
} |
| 2517 |
#endif |
| 2518 |
#ifdef JS_64BIT |
| 2519 |
size_t numCopies8 = remainder / sizeof(uint64_t); |
| 2520 |
remainder %= sizeof(uint64_t); |
| 2521 |
#endif |
| 2522 |
size_t numCopies4 = remainder / sizeof(uint32_t); |
| 2523 |
remainder %= sizeof(uint32_t); |
| 2524 |
size_t numCopies2 = remainder / sizeof(uint16_t); |
| 2525 |
remainder %= sizeof(uint16_t); |
| 2526 |
size_t numCopies1 = remainder; |
| 2527 |
|
| 2528 |
// Load all source bytes onto the value stack from low to high using the |
| 2529 |
// widest transfer width we can for the system. We will trap without writing |
| 2530 |
// anything if any source byte is out-of-bounds. |
| 2531 |
bool omitBoundsCheck = false; |
| 2532 |
size_t offset = 0; |
| 2533 |
|
| 2534 |
#ifdef ENABLE_WASM_SIMD |
| 2535 |
for (uint32_t i = 0; i < numCopies16; i++) { |
| 2536 |
RegI32 temp = needI32(); |
| 2537 |
moveI32(src, temp); |
| 2538 |
pushI32(temp); |
| 2539 |
|
| 2540 |
MemoryAccessDesc access(memoryIndex, Scalar::Simd128, 1, offset, |
| 2541 |
trapSiteDesc(), hugeMemoryEnabled(memoryIndex)); |
| 2542 |
AccessCheck check; |
| 2543 |
check.omitBoundsCheck = omitBoundsCheck; |
| 2544 |
loadCommon(&access, check, ValType::V128); |
| 2545 |
|
| 2546 |
offset += sizeof(V128); |
| 2547 |
omitBoundsCheck = true; |
| 2548 |
} |
| 2549 |
#endif |
| 2550 |
|
| 2551 |
#ifdef JS_64BIT |
| 2552 |
for (uint32_t i = 0; i < numCopies8; i++) { |
| 2553 |
RegI32 temp = needI32(); |
| 2554 |
moveI32(src, temp); |
| 2555 |
pushI32(temp); |
| 2556 |
|
| 2557 |
MemoryAccessDesc access(memoryIndex, Scalar::Int64, 1, offset, |
| 2558 |
trapSiteDesc(), hugeMemoryEnabled(memoryIndex)); |
| 2559 |
AccessCheck check; |
| 2560 |
check.omitBoundsCheck = omitBoundsCheck; |
| 2561 |
loadCommon(&access, check, ValType::I64); |
| 2562 |
|
| 2563 |
offset += sizeof(uint64_t); |
| 2564 |
omitBoundsCheck = true; |
| 2565 |
} |
| 2566 |
#endif |
| 2567 |
|
| 2568 |
for (uint32_t i = 0; i < numCopies4; i++) { |
| 2569 |
RegI32 temp = needI32(); |
| 2570 |
moveI32(src, temp); |
| 2571 |
pushI32(temp); |
| 2572 |
|
| 2573 |
MemoryAccessDesc access(memoryIndex, Scalar::Uint32, 1, offset, |
| 2574 |
trapSiteDesc(), hugeMemoryEnabled(memoryIndex)); |
| 2575 |
AccessCheck check; |
| 2576 |
check.omitBoundsCheck = omitBoundsCheck; |
| 2577 |
loadCommon(&access, check, ValType::I32); |
| 2578 |
|
| 2579 |
offset += sizeof(uint32_t); |
| 2580 |
omitBoundsCheck = true; |
| 2581 |
} |
| 2582 |
|
| 2583 |
if (numCopies2) { |
| 2584 |
RegI32 temp = needI32(); |
| 2585 |
moveI32(src, temp); |
| 2586 |
pushI32(temp); |
| 2587 |
|
| 2588 |
MemoryAccessDesc access(memoryIndex, Scalar::Uint16, 1, offset, |
| 2589 |
trapSiteDesc(), hugeMemoryEnabled(memoryIndex)); |
| 2590 |
AccessCheck check; |
| 2591 |
check.omitBoundsCheck = omitBoundsCheck; |
| 2592 |
loadCommon(&access, check, ValType::I32); |
| 2593 |
|
| 2594 |
offset += sizeof(uint16_t); |
| 2595 |
omitBoundsCheck = true; |
| 2596 |
} |
| 2597 |
|
| 2598 |
if (numCopies1) { |
| 2599 |
RegI32 temp = needI32(); |
| 2600 |
moveI32(src, temp); |
| 2601 |
pushI32(temp); |
| 2602 |
|
| 2603 |
MemoryAccessDesc access(memoryIndex, Scalar::Uint8, 1, offset, |
| 2604 |
trapSiteDesc(), hugeMemoryEnabled(memoryIndex)); |
| 2605 |
AccessCheck check; |
| 2606 |
check.omitBoundsCheck = omitBoundsCheck; |
| 2607 |
loadCommon(&access, check, ValType::I32); |
| 2608 |
} |
| 2609 |
|
| 2610 |
// Store all source bytes from the value stack to the destination from |
| 2611 |
// high to low. We will trap without writing anything on the first store |
| 2612 |
// if any dest byte is out-of-bounds. |
| 2613 |
offset = length; |
| 2614 |
omitBoundsCheck = false; |
| 2615 |
|
| 2616 |
if (numCopies1) { |
| 2617 |
offset -= sizeof(uint8_t); |
| 2618 |
|
| 2619 |
RegI32 value = popI32(); |
| 2620 |
RegI32 temp = needI32(); |
| 2621 |
moveI32(dest, temp); |
| 2622 |
pushI32(temp); |
| 2623 |
pushI32(value); |
| 2624 |
|
| 2625 |
MemoryAccessDesc access(memoryIndex, Scalar::Uint8, 1, offset, |
| 2626 |
trapSiteDesc(), hugeMemoryEnabled(memoryIndex)); |
| 2627 |
AccessCheck check; |
| 2628 |
storeCommon(&access, check, ValType::I32); |
| 2629 |
|
| 2630 |
omitBoundsCheck = true; |
| 2631 |
} |
| 2632 |
|
| 2633 |
if (numCopies2) { |
| 2634 |
offset -= sizeof(uint16_t); |
| 2635 |
|
| 2636 |
RegI32 value = popI32(); |
| 2637 |
RegI32 temp = needI32(); |
| 2638 |
moveI32(dest, temp); |
| 2639 |
pushI32(temp); |
| 2640 |
pushI32(value); |
| 2641 |
|
| 2642 |
MemoryAccessDesc access(memoryIndex, Scalar::Uint16, 1, offset, |
| 2643 |
trapSiteDesc(), hugeMemoryEnabled(memoryIndex)); |
| 2644 |
AccessCheck check; |
| 2645 |
check.omitBoundsCheck = omitBoundsCheck; |
| 2646 |
storeCommon(&access, check, ValType::I32); |
| 2647 |
|
| 2648 |
omitBoundsCheck = true; |
| 2649 |
} |
| 2650 |
|
| 2651 |
for (uint32_t i = 0; i < numCopies4; i++) { |
| 2652 |
offset -= sizeof(uint32_t); |
| 2653 |
|
| 2654 |
RegI32 value = popI32(); |
| 2655 |
RegI32 temp = needI32(); |
| 2656 |
moveI32(dest, temp); |
| 2657 |
pushI32(temp); |
| 2658 |
pushI32(value); |
| 2659 |
|
| 2660 |
MemoryAccessDesc access(memoryIndex, Scalar::Uint32, 1, offset, |
| 2661 |
trapSiteDesc(), hugeMemoryEnabled(memoryIndex)); |
| 2662 |
AccessCheck check; |
| 2663 |
check.omitBoundsCheck = omitBoundsCheck; |
| 2664 |
storeCommon(&access, check, ValType::I32); |
| 2665 |
|
| 2666 |
omitBoundsCheck = true; |
| 2667 |
} |
| 2668 |
|
| 2669 |
#ifdef JS_64BIT |
| 2670 |
for (uint32_t i = 0; i < numCopies8; i++) { |
| 2671 |
offset -= sizeof(uint64_t); |
| 2672 |
|
| 2673 |
RegI64 value = popI64(); |
| 2674 |
RegI32 temp = needI32(); |
| 2675 |
moveI32(dest, temp); |
| 2676 |
pushI32(temp); |
| 2677 |
pushI64(value); |
| 2678 |
|
| 2679 |
MemoryAccessDesc access(memoryIndex, Scalar::Int64, 1, offset, |
| 2680 |
trapSiteDesc(), hugeMemoryEnabled(memoryIndex)); |
| 2681 |
AccessCheck check; |
| 2682 |
check.omitBoundsCheck = omitBoundsCheck; |
| 2683 |
storeCommon(&access, check, ValType::I64); |
| 2684 |
|
| 2685 |
omitBoundsCheck = true; |
| 2686 |
} |
| 2687 |
#endif |
| 2688 |
|
| 2689 |
#ifdef ENABLE_WASM_SIMD |
| 2690 |
for (uint32_t i = 0; i < numCopies16; i++) { |
| 2691 |
offset -= sizeof(V128); |
| 2692 |
|
| 2693 |
RegV128 value = popV128(); |
| 2694 |
RegI32 temp = needI32(); |
| 2695 |
moveI32(dest, temp); |
| 2696 |
pushI32(temp); |
| 2697 |
pushV128(value); |
| 2698 |
|
| 2699 |
MemoryAccessDesc access(memoryIndex, Scalar::Simd128, 1, offset, |
| 2700 |
trapSiteDesc(), hugeMemoryEnabled(memoryIndex)); |
| 2701 |
AccessCheck check; |
| 2702 |
check.omitBoundsCheck = omitBoundsCheck; |
| 2703 |
storeCommon(&access, check, ValType::V128); |
| 2704 |
|
| 2705 |
omitBoundsCheck = true; |
| 2706 |
} |
| 2707 |
#endif |
| 2708 |
|
| 2709 |
freeI32(dest); |
| 2710 |
freeI32(src); |
| 2711 |
} |
| 2712 |
|
| 2713 |
void BaseCompiler::memFillInlineM32() { |
| 2714 |
MOZ_ASSERT(MaxInlineMemoryFillLength != 0); |
| 2715 |
|
| 2716 |
// This function assumes a memory index of zero |
| 2717 |
uint32_t memoryIndex = 0; |
| 2718 |
int32_t signedLength; |
| 2719 |
int32_t signedValue; |
| 2720 |
MOZ_ALWAYS_TRUE(popConst(&signedLength)); |
| 2721 |
MOZ_ALWAYS_TRUE(popConst(&signedValue)); |
| 2722 |
uint32_t length = uint32_t(signedLength); |
| 2723 |
uint32_t value = uint32_t(signedValue); |
| 2724 |
MOZ_ASSERT(length != 0 && length <= MaxInlineMemoryFillLength); |
| 2725 |
|
| 2726 |
RegI32 dest = popI32(); |
| 2727 |
|
| 2728 |
// Compute the number of copies of each width we will need to do |
| 2729 |
size_t remainder = length; |
| 2730 |
#ifdef ENABLE_WASM_SIMD |
| 2731 |
size_t numCopies16 = 0; |
| 2732 |
if (MacroAssembler::SupportsFastUnalignedFPAccesses()) { |
| 2733 |
numCopies16 = remainder / sizeof(V128); |
| 2734 |
remainder %= sizeof(V128); |
| 2735 |
} |
| 2736 |
#endif |
| 2737 |
#ifdef JS_64BIT |
| 2738 |
size_t numCopies8 = remainder / sizeof(uint64_t); |
| 2739 |
remainder %= sizeof(uint64_t); |
| 2740 |
#endif |
| 2741 |
size_t numCopies4 = remainder / sizeof(uint32_t); |
| 2742 |
remainder %= sizeof(uint32_t); |
| 2743 |
size_t numCopies2 = remainder / sizeof(uint16_t); |
| 2744 |
remainder %= sizeof(uint16_t); |
| 2745 |
size_t numCopies1 = remainder; |
| 2746 |
|
| 2747 |
MOZ_ASSERT(numCopies2 <= 1 && numCopies1 <= 1); |
| 2748 |
|
| 2749 |
// Generate splatted definitions for wider fills as needed |
| 2750 |
#ifdef ENABLE_WASM_SIMD |
| 2751 |
V128 val16(value); |
| 2752 |
#endif |
| 2753 |
#ifdef JS_64BIT |
| 2754 |
uint64_t val8 = SplatByteToUInt<uint64_t>(value, 8); |
| 2755 |
#endif |
| 2756 |
uint32_t val4 = SplatByteToUInt<uint32_t>(value, 4); |
| 2757 |
uint32_t val2 = SplatByteToUInt<uint32_t>(value, 2); |
| 2758 |
uint32_t val1 = value; |
| 2759 |
|
| 2760 |
// Store the fill value to the destination from high to low. We will trap |
| 2761 |
// without writing anything on the first store if any dest byte is |
| 2762 |
// out-of-bounds. |
| 2763 |
size_t offset = length; |
| 2764 |
bool omitBoundsCheck = false; |
| 2765 |
|
| 2766 |
if (numCopies1) { |
| 2767 |
offset -= sizeof(uint8_t); |
| 2768 |
|
| 2769 |
RegI32 temp = needI32(); |
| 2770 |
moveI32(dest, temp); |
| 2771 |
pushI32(temp); |
| 2772 |
pushI32(val1); |
| 2773 |
|
| 2774 |
MemoryAccessDesc access(memoryIndex, Scalar::Uint8, 1, offset, |
| 2775 |
trapSiteDesc(), hugeMemoryEnabled(memoryIndex)); |
| 2776 |
AccessCheck check; |
| 2777 |
storeCommon(&access, check, ValType::I32); |
| 2778 |
|
| 2779 |
omitBoundsCheck = true; |
| 2780 |
} |
| 2781 |
|
| 2782 |
if (numCopies2) { |
| 2783 |
offset -= sizeof(uint16_t); |
| 2784 |
|
| 2785 |
RegI32 temp = needI32(); |
| 2786 |
moveI32(dest, temp); |
| 2787 |
pushI32(temp); |
| 2788 |
pushI32(val2); |
| 2789 |
|
| 2790 |
MemoryAccessDesc access(memoryIndex, Scalar::Uint16, 1, offset, |
| 2791 |
trapSiteDesc(), hugeMemoryEnabled(memoryIndex)); |
| 2792 |
AccessCheck check; |
| 2793 |
check.omitBoundsCheck = omitBoundsCheck; |
| 2794 |
storeCommon(&access, check, ValType::I32); |
| 2795 |
|
| 2796 |
omitBoundsCheck = true; |
| 2797 |
} |
| 2798 |
|
| 2799 |
for (uint32_t i = 0; i < numCopies4; i++) { |
| 2800 |
offset -= sizeof(uint32_t); |
| 2801 |
|
| 2802 |
RegI32 temp = needI32(); |
| 2803 |
moveI32(dest, temp); |
| 2804 |
pushI32(temp); |
| 2805 |
pushI32(val4); |
| 2806 |
|
| 2807 |
MemoryAccessDesc access(memoryIndex, Scalar::Uint32, 1, offset, |
| 2808 |
trapSiteDesc(), hugeMemoryEnabled(memoryIndex)); |
| 2809 |
AccessCheck check; |
| 2810 |
check.omitBoundsCheck = omitBoundsCheck; |
| 2811 |
storeCommon(&access, check, ValType::I32); |
| 2812 |
|
| 2813 |
omitBoundsCheck = true; |
| 2814 |
} |
| 2815 |
|
| 2816 |
#ifdef JS_64BIT |
| 2817 |
for (uint32_t i = 0; i < numCopies8; i++) { |
| 2818 |
offset -= sizeof(uint64_t); |
| 2819 |
|
| 2820 |
RegI32 temp = needI32(); |
| 2821 |
moveI32(dest, temp); |
| 2822 |
pushI32(temp); |
| 2823 |
pushI64(val8); |
| 2824 |
|
| 2825 |
MemoryAccessDesc access(memoryIndex, Scalar::Int64, 1, offset, |
| 2826 |
trapSiteDesc(), hugeMemoryEnabled(memoryIndex)); |
| 2827 |
AccessCheck check; |
| 2828 |
check.omitBoundsCheck = omitBoundsCheck; |
| 2829 |
storeCommon(&access, check, ValType::I64); |
| 2830 |
|
| 2831 |
omitBoundsCheck = true; |
| 2832 |
} |
| 2833 |
#endif |
| 2834 |
|
| 2835 |
#ifdef ENABLE_WASM_SIMD |
| 2836 |
for (uint32_t i = 0; i < numCopies16; i++) { |
| 2837 |
offset -= sizeof(V128); |
| 2838 |
|
| 2839 |
RegI32 temp = needI32(); |
| 2840 |
moveI32(dest, temp); |
| 2841 |
pushI32(temp); |
| 2842 |
pushV128(val16); |
| 2843 |
|
| 2844 |
MemoryAccessDesc access(memoryIndex, Scalar::Simd128, 1, offset, |
| 2845 |
trapSiteDesc(), hugeMemoryEnabled(memoryIndex)); |
| 2846 |
AccessCheck check; |
| 2847 |
check.omitBoundsCheck = omitBoundsCheck; |
| 2848 |
storeCommon(&access, check, ValType::V128); |
| 2849 |
|
| 2850 |
omitBoundsCheck = true; |
| 2851 |
} |
| 2852 |
#endif |
| 2853 |
|
| 2854 |
freeI32(dest); |
| 2855 |
} |
| 2856 |
|
| 2857 |
////////////////////////////////////////////////////////////////////////////// |
| 2858 |
// |
| 2859 |
// SIMD and Relaxed SIMD. |
| 2860 |
|
| 2861 |
#ifdef ENABLE_WASM_SIMD |
| 2862 |
void BaseCompiler::loadSplat(MemoryAccessDesc* access) { |
| 2863 |
// We can implement loadSplat mostly as load + splat because the push of the |
| 2864 |
// result onto the value stack in loadCommon normally will not generate any |
| 2865 |
// code, it will leave the value in a register which we will consume. |
| 2866 |
|
| 2867 |
// We use uint types when we can on the general assumption that unsigned loads |
| 2868 |
// might be smaller/faster on some platforms, because no sign extension needs |
| 2869 |
// to be done after the sub-register load. |
| 2870 |
RegV128 rd = needV128(); |
| 2871 |
switch (access->type()) { |
| 2872 |
case Scalar::Uint8: { |
| 2873 |
loadCommon(access, AccessCheck(), ValType::I32); |
| 2874 |
RegI32 rs = popI32(); |
| 2875 |
masm.splatX16(rs, rd); |
| 2876 |
free(rs); |
| 2877 |
break; |
| 2878 |
} |
| 2879 |
case Scalar::Uint16: { |
| 2880 |
loadCommon(access, AccessCheck(), ValType::I32); |
| 2881 |
RegI32 rs = popI32(); |
| 2882 |
masm.splatX8(rs, rd); |
| 2883 |
free(rs); |
| 2884 |
break; |
| 2885 |
} |
| 2886 |
case Scalar::Uint32: { |
| 2887 |
loadCommon(access, AccessCheck(), ValType::I32); |
| 2888 |
RegI32 rs = popI32(); |
| 2889 |
masm.splatX4(rs, rd); |
| 2890 |
free(rs); |
| 2891 |
break; |
| 2892 |
} |
| 2893 |
case Scalar::Int64: { |
| 2894 |
loadCommon(access, AccessCheck(), ValType::I64); |
| 2895 |
RegI64 rs = popI64(); |
| 2896 |
masm.splatX2(rs, rd); |
| 2897 |
free(rs); |
| 2898 |
break; |
| 2899 |
} |
| 2900 |
default: |
| 2901 |
MOZ_CRASH(); |
| 2902 |
} |
| 2903 |
pushV128(rd); |
| 2904 |
} |
| 2905 |
|
| 2906 |
void BaseCompiler::loadZero(MemoryAccessDesc* access) { |
| 2907 |
access->setZeroExtendSimd128Load(); |
| 2908 |
loadCommon(access, AccessCheck(), ValType::V128); |
| 2909 |
} |
| 2910 |
|
| 2911 |
void BaseCompiler::loadExtend(MemoryAccessDesc* access, Scalar::Type viewType) { |
| 2912 |
loadCommon(access, AccessCheck(), ValType::I64); |
| 2913 |
|
| 2914 |
RegI64 rs = popI64(); |
| 2915 |
RegV128 rd = needV128(); |
| 2916 |
masm.moveGPR64ToDouble(rs, rd); |
| 2917 |
switch (viewType) { |
| 2918 |
case Scalar::Int8: |
| 2919 |
masm.widenLowInt8x16(rd, rd); |
| 2920 |
break; |
| 2921 |
case Scalar::Uint8: |
| 2922 |
masm.unsignedWidenLowInt8x16(rd, rd); |
| 2923 |
break; |
| 2924 |
case Scalar::Int16: |
| 2925 |
masm.widenLowInt16x8(rd, rd); |
| 2926 |
break; |
| 2927 |
case Scalar::Uint16: |
| 2928 |
masm.unsignedWidenLowInt16x8(rd, rd); |
| 2929 |
break; |
| 2930 |
case Scalar::Int32: |
| 2931 |
masm.widenLowInt32x4(rd, rd); |
| 2932 |
break; |
| 2933 |
case Scalar::Uint32: |
| 2934 |
masm.unsignedWidenLowInt32x4(rd, rd); |
| 2935 |
break; |
| 2936 |
default: |
| 2937 |
MOZ_CRASH(); |
| 2938 |
} |
| 2939 |
freeI64(rs); |
| 2940 |
pushV128(rd); |
| 2941 |
} |
| 2942 |
|
| 2943 |
void BaseCompiler::loadLane(MemoryAccessDesc* access, uint32_t laneIndex) { |
| 2944 |
ValType type = access->type() == Scalar::Int64 ? ValType::I64 : ValType::I32; |
| 2945 |
|
| 2946 |
RegV128 rsd = popV128(); |
| 2947 |
loadCommon(access, AccessCheck(), type); |
| 2948 |
|
| 2949 |
if (type == ValType::I32) { |
| 2950 |
RegI32 rs = popI32(); |
| 2951 |
switch (access->type()) { |
| 2952 |
case Scalar::Uint8: |
| 2953 |
masm.replaceLaneInt8x16(laneIndex, rs, rsd); |
| 2954 |
break; |
| 2955 |
case Scalar::Uint16: |
| 2956 |
masm.replaceLaneInt16x8(laneIndex, rs, rsd); |
| 2957 |
break; |
| 2958 |
case Scalar::Int32: |
| 2959 |
masm.replaceLaneInt32x4(laneIndex, rs, rsd); |
| 2960 |
break; |
| 2961 |
default: |
| 2962 |
MOZ_CRASH("unsupported access type"); |
| 2963 |
} |
| 2964 |
freeI32(rs); |
| 2965 |
} else { |
| 2966 |
MOZ_ASSERT(type == ValType::I64); |
| 2967 |
RegI64 rs = popI64(); |
| 2968 |
masm.replaceLaneInt64x2(laneIndex, rs, rsd); |
| 2969 |
freeI64(rs); |
| 2970 |
} |
| 2971 |
|
| 2972 |
pushV128(rsd); |
| 2973 |
} |
| 2974 |
|
| 2975 |
void BaseCompiler::storeLane(MemoryAccessDesc* access, uint32_t laneIndex) { |
| 2976 |
ValType type = access->type() == Scalar::Int64 ? ValType::I64 : ValType::I32; |
| 2977 |
|
| 2978 |
RegV128 rs = popV128(); |
| 2979 |
if (type == ValType::I32) { |
| 2980 |
RegI32 tmp = needI32(); |
| 2981 |
switch (access->type()) { |
| 2982 |
case Scalar::Uint8: |
| 2983 |
masm.extractLaneInt8x16(laneIndex, rs, tmp); |
| 2984 |
break; |
| 2985 |
case Scalar::Uint16: |
| 2986 |
masm.extractLaneInt16x8(laneIndex, rs, tmp); |
| 2987 |
break; |
| 2988 |
case Scalar::Int32: |
| 2989 |
masm.extractLaneInt32x4(laneIndex, rs, tmp); |
| 2990 |
break; |
| 2991 |
default: |
| 2992 |
MOZ_CRASH("unsupported laneSize"); |
| 2993 |
} |
| 2994 |
pushI32(tmp); |
| 2995 |
} else { |
| 2996 |
MOZ_ASSERT(type == ValType::I64); |
| 2997 |
RegI64 tmp = needI64(); |
| 2998 |
masm.extractLaneInt64x2(laneIndex, rs, tmp); |
| 2999 |
pushI64(tmp); |
| 3000 |
} |
| 3001 |
freeV128(rs); |
| 3002 |
|
| 3003 |
storeCommon(access, AccessCheck(), type); |
| 3004 |
} |
| 3005 |
#endif // ENABLE_WASM_SIMD |
| 3006 |
|
| 3007 |
} // namespace wasm |
| 3008 |
} // namespace js |