Debian Codegraph @

js/src/jit/arm64/MacroAssembler-arm64.cpp

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "jit/arm64/MacroAssembler-arm64.h"
8
9 #include "mozilla/MathAlgorithms.h"
10 #include "mozilla/Maybe.h"
11
12 #include "jsmath.h"
13
14 #include "jit/arm64/MoveEmitter-arm64.h"
15 #include "jit/arm64/SharedICRegisters-arm64.h"
16 #include "jit/Bailouts.h"
17 #include "jit/BaselineFrame.h"
18 #include "jit/JitRuntime.h"
19 #include "jit/MacroAssembler.h"
20 #include "jit/ProcessExecutableMemory.h"
21 #include "util/Memory.h"
22 #include "vm/BigIntType.h"
23 #include "vm/JitActivation.h" // js::jit::JitActivation
24 #include "vm/JSContext.h"
25 #include "vm/StringType.h"
26 #include "wasm/WasmStubs.h"
27
28 #include "jit/MacroAssembler-inl.h"
29
30 namespace js {
31 namespace jit {
32
33 enum class Width { _32 = 32, _64 = 64 };
34
35 static inline ARMRegister X(Register r) { return ARMRegister(r, 64); }
36
37 static inline ARMRegister X(MacroAssembler& masm, RegisterOrSP r) {
38 return masm.toARMRegister(r, 64);
39 }
40
41 static inline ARMRegister W(Register r) { return ARMRegister(r, 32); }
42
43 static inline ARMRegister R(Register r, Width w) {
44 return ARMRegister(r, unsigned(w));
45 }
46
47 void MacroAssemblerCompat::boxValue(JSValueType type, Register src,
48 Register dest) {
49 #ifdef DEBUG
50 if (type == JSVAL_TYPE_INT32 || type == JSVAL_TYPE_BOOLEAN) {
51 Label upper32BitsZeroed;
52 movePtr(ImmWord(UINT32_MAX), dest);
53 asMasm().branchPtr(Assembler::BelowOrEqual, src, dest, &upper32BitsZeroed);
54 breakpoint();
55 bind(&upper32BitsZeroed);
56 }
57 #endif
58 Orr(ARMRegister(dest, 64), ARMRegister(src, 64),
59 Operand(ImmShiftedTag(type).value));
60 }
61
62 #ifdef ENABLE_WASM_SIMD
63 bool MacroAssembler::MustMaskShiftCountSimd128(wasm::SimdOp op, int32_t* mask) {
64 switch (op) {
65 case wasm::SimdOp::I8x16Shl:
66 case wasm::SimdOp::I8x16ShrU:
67 case wasm::SimdOp::I8x16ShrS:
68 *mask = 7;
69 break;
70 case wasm::SimdOp::I16x8Shl:
71 case wasm::SimdOp::I16x8ShrU:
72 case wasm::SimdOp::I16x8ShrS:
73 *mask = 15;
74 break;
75 case wasm::SimdOp::I32x4Shl:
76 case wasm::SimdOp::I32x4ShrU:
77 case wasm::SimdOp::I32x4ShrS:
78 *mask = 31;
79 break;
80 case wasm::SimdOp::I64x2Shl:
81 case wasm::SimdOp::I64x2ShrU:
82 case wasm::SimdOp::I64x2ShrS:
83 *mask = 63;
84 break;
85 default:
86 MOZ_CRASH("Unexpected shift operation");
87 }
88 return true;
89 }
90 #endif
91
92 void MacroAssembler::clampDoubleToUint8(FloatRegister input, Register output) {
93 ARMRegister dest(output, 32);
94 Fcvtns(dest, ARMFPRegister(input, 64));
95
96 {
97 vixl::UseScratchRegisterScope temps(this);
98 const ARMRegister scratch32 = temps.AcquireW();
99
100 Mov(scratch32, Operand(0xff));
101 Cmp(dest, scratch32);
102 Csel(dest, dest, scratch32, LessThan);
103 }
104
105 Cmp(dest, Operand(0));
106 Csel(dest, dest, wzr, GreaterThan);
107 }
108
109 js::jit::MacroAssembler& MacroAssemblerCompat::asMasm() {
110 return *static_cast<js::jit::MacroAssembler*>(this);
111 }
112
113 const js::jit::MacroAssembler& MacroAssemblerCompat::asMasm() const {
114 return *static_cast<const js::jit::MacroAssembler*>(this);
115 }
116
117 vixl::MacroAssembler& MacroAssemblerCompat::asVIXL() {
118 return *static_cast<vixl::MacroAssembler*>(this);
119 }
120
121 const vixl::MacroAssembler& MacroAssemblerCompat::asVIXL() const {
122 return *static_cast<const vixl::MacroAssembler*>(this);
123 }
124
125 void MacroAssemblerCompat::mov(CodeLabel* label, Register dest) {
126 BufferOffset bo = movePatchablePtr(ImmWord(/* placeholder */ 0), dest);
127 label->patchAt()->bind(bo.getOffset());
128 label->setLinkMode(CodeLabel::MoveImmediate);
129 }
130
131 BufferOffset MacroAssemblerCompat::movePatchablePtr(ImmPtr ptr, Register dest) {
132 const size_t numInst = 1; // Inserting one load instruction.
133 const unsigned numPoolEntries = 2; // Every pool entry is 4 bytes.
134 uint8_t* literalAddr = (uint8_t*)(&ptr.value); // TODO: Should be const.
135
136 // Scratch space for generating the load instruction.
137 //
138 // allocLiteralLoadEntry() will use InsertIndexIntoTag() to store a temporary
139 // index to the corresponding PoolEntry in the instruction itself.
140 //
141 // That index will be fixed up later when finishPool()
142 // walks over all marked loads and calls PatchConstantPoolLoad().
143 uint32_t instructionScratch = 0;
144
145 // Emit the instruction mask in the scratch space.
146 // The offset doesn't matter: it will be fixed up later.
147 vixl::Assembler::ldr((Instruction*)&instructionScratch, ARMRegister(dest, 64),
148 0);
149
150 // Add the entry to the pool, fix up the LDR imm19 offset,
151 // and add the completed instruction to the buffer.
152 return allocLiteralLoadEntry(numInst, numPoolEntries,
153 (uint8_t*)&instructionScratch, literalAddr);
154 }
155
156 BufferOffset MacroAssemblerCompat::movePatchablePtr(ImmWord ptr,
157 Register dest) {
158 const size_t numInst = 1; // Inserting one load instruction.
159 const unsigned numPoolEntries = 2; // Every pool entry is 4 bytes.
160 uint8_t* literalAddr = (uint8_t*)(&ptr.value);
161
162 // Scratch space for generating the load instruction.
163 //
164 // allocLiteralLoadEntry() will use InsertIndexIntoTag() to store a temporary
165 // index to the corresponding PoolEntry in the instruction itself.
166 //
167 // That index will be fixed up later when finishPool()
168 // walks over all marked loads and calls PatchConstantPoolLoad().
169 uint32_t instructionScratch = 0;
170
171 // Emit the instruction mask in the scratch space.
172 // The offset doesn't matter: it will be fixed up later.
173 vixl::Assembler::ldr((Instruction*)&instructionScratch, ARMRegister(dest, 64),
174 0);
175
176 // Add the entry to the pool, fix up the LDR imm19 offset,
177 // and add the completed instruction to the buffer.
178 return allocLiteralLoadEntry(numInst, numPoolEntries,
179 (uint8_t*)&instructionScratch, literalAddr);
180 }
181
182 void MacroAssemblerCompat::loadPrivate(const Address& src, Register dest) {
183 loadPtr(src, dest);
184 }
185
186 void MacroAssemblerCompat::handleFailureWithHandlerTail(
187 Label* profilerExitTail, Label* bailoutTail,
188 uint32_t* returnValueCheckOffset) {
189 // Fail rather than silently create wrong code.
190 MOZ_RELEASE_ASSERT(GetStackPointer64().Is(PseudoStackPointer64));
191
192 // Reserve space for exception information.
193 int64_t size = (sizeof(ResumeFromException) + 7) & ~7;
194 Sub(PseudoStackPointer64, PseudoStackPointer64, Operand(size));
195 syncStackPtr();
196
197 MOZ_ASSERT(!x0.Is(PseudoStackPointer64));
198 Mov(x0, PseudoStackPointer64);
199
200 // Call the handler.
201 using Fn = void (*)(ResumeFromException* rfe);
202 asMasm().setupUnalignedABICall(r1);
203 asMasm().passABIArg(r0);
204 asMasm().callWithABI<Fn, HandleException>(
205 ABIType::General, CheckUnsafeCallWithABI::DontCheckHasExitFrame);
206
207 *returnValueCheckOffset = asMasm().currentOffset();
208
209 Label entryFrame;
210 Label catch_;
211 Label finally;
212 Label returnBaseline;
213 Label returnIon;
214 Label bailout;
215 Label wasmInterpEntry;
216 Label wasmCatch;
217
218 // Check the `asMasm` calls above didn't mess with the StackPointer identity.
219 MOZ_ASSERT(GetStackPointer64().Is(PseudoStackPointer64));
220
221 loadPtr(Address(PseudoStackPointer, ResumeFromException::offsetOfKind()), r0);
222 asMasm().branch32(Assembler::Equal, r0,
223 Imm32(ExceptionResumeKind::EntryFrame), &entryFrame);
224 asMasm().branch32(Assembler::Equal, r0, Imm32(ExceptionResumeKind::Catch),
225 &catch_);
226 asMasm().branch32(Assembler::Equal, r0, Imm32(ExceptionResumeKind::Finally),
227 &finally);
228 asMasm().branch32(Assembler::Equal, r0,
229 Imm32(ExceptionResumeKind::ForcedReturnBaseline),
230 &returnBaseline);
231 asMasm().branch32(Assembler::Equal, r0,
232 Imm32(ExceptionResumeKind::ForcedReturnIon), &returnIon);
233 asMasm().branch32(Assembler::Equal, r0, Imm32(ExceptionResumeKind::Bailout),
234 &bailout);
235 asMasm().branch32(Assembler::Equal, r0,
236 Imm32(ExceptionResumeKind::WasmInterpEntry),
237 &wasmInterpEntry);
238 asMasm().branch32(Assembler::Equal, r0, Imm32(ExceptionResumeKind::WasmCatch),
239 &wasmCatch);
240
241 breakpoint(); // Invalid kind.
242
243 // No exception handler. Load the error value, restore state and return from
244 // the entry frame.
245 bind(&entryFrame);
246 moveValue(MagicValue(JS_ION_ERROR), JSReturnOperand);
247 loadPtr(
248 Address(PseudoStackPointer, ResumeFromException::offsetOfFramePointer()),
249 FramePointer);
250 loadPtr(
251 Address(PseudoStackPointer, ResumeFromException::offsetOfStackPointer()),
252 PseudoStackPointer);
253
254 // `retn` does indeed sync the stack pointer, but before doing that it reads
255 // from the stack. Consequently, if we remove this call to syncStackPointer
256 // then we take on the requirement to prove that the immediately preceding
257 // loadPtr produces a value for PSP which maintains the SP <= PSP invariant.
258 // That's a proof burden we don't want to take on. In general it would be
259 // good to move (at some time in the future, not now) to a world where
260 // *every* assignment to PSP or SP is followed immediately by a copy into
261 // the other register. That would make all required correctness proofs
262 // trivial in the sense that it requires only local inspection of code
263 // immediately following (dominated by) any such assignment.
264 syncStackPtr();
265 retn(Imm32(1 * sizeof(void*))); // Pop from stack and return.
266
267 // If we found a catch handler, this must be a baseline frame. Restore state
268 // and jump to the catch block.
269 bind(&catch_);
270 loadPtr(Address(PseudoStackPointer, ResumeFromException::offsetOfTarget()),
271 r0);
272 loadPtr(
273 Address(PseudoStackPointer, ResumeFromException::offsetOfFramePointer()),
274 FramePointer);
275 loadPtr(
276 Address(PseudoStackPointer, ResumeFromException::offsetOfStackPointer()),
277 PseudoStackPointer);
278 syncStackPtr();
279 Br(x0);
280
281 // If we found a finally block, this must be a baseline frame. Push three
282 // values expected by the finally block: the exception, the exception stack,
283 // and BooleanValue(true).
284 bind(&finally);
285 ARMRegister exception = x1;
286 Ldr(exception, MemOperand(PseudoStackPointer64,
287 ResumeFromException::offsetOfException()));
288
289 ARMRegister exceptionStack = x2;
290 Ldr(exceptionStack,
291 MemOperand(PseudoStackPointer64,
292 ResumeFromException::offsetOfExceptionStack()));
293
294 Ldr(x0,
295 MemOperand(PseudoStackPointer64, ResumeFromException::offsetOfTarget()));
296 Ldr(ARMRegister(FramePointer, 64),
297 MemOperand(PseudoStackPointer64,
298 ResumeFromException::offsetOfFramePointer()));
299 Ldr(PseudoStackPointer64,
300 MemOperand(PseudoStackPointer64,
301 ResumeFromException::offsetOfStackPointer()));
302 syncStackPtr();
303 push(exception);
304 push(exceptionStack);
305 pushValue(BooleanValue(true));
306 Br(x0);
307
308 // Return BaselineFrame->returnValue() to the caller.
309 // Used in debug mode and for GeneratorReturn.
310 Label profilingInstrumentation;
311 bind(&returnBaseline);
312 loadPtr(
313 Address(PseudoStackPointer, ResumeFromException::offsetOfFramePointer()),
314 FramePointer);
315 loadPtr(
316 Address(PseudoStackPointer, ResumeFromException::offsetOfStackPointer()),
317 PseudoStackPointer);
318 // See comment further up beginning "`retn` does indeed sync the stack
319 // pointer". That comment applies here too.
320 syncStackPtr();
321 loadValue(Address(FramePointer, BaselineFrame::reverseOffsetOfReturnValue()),
322 JSReturnOperand);
323 jump(&profilingInstrumentation);
324
325 // Return the given value to the caller.
326 bind(&returnIon);
327 loadValue(
328 Address(PseudoStackPointer, ResumeFromException::offsetOfException()),
329 JSReturnOperand);
330 loadPtr(
331 Address(PseudoStackPointer, offsetof(ResumeFromException, framePointer)),
332 FramePointer);
333 loadPtr(
334 Address(PseudoStackPointer, offsetof(ResumeFromException, stackPointer)),
335 PseudoStackPointer);
336 syncStackPtr();
337
338 // If profiling is enabled, then update the lastProfilingFrame to refer to
339 // caller frame before returning. This code is shared by ForcedReturnIon
340 // and ForcedReturnBaseline.
341 bind(&profilingInstrumentation);
342 {
343 Label skipProfilingInstrumentation;
344 AbsoluteAddress addressOfEnabled(
345 asMasm().runtime()->geckoProfiler().addressOfEnabled());
346 asMasm().branch32(Assembler::Equal, addressOfEnabled, Imm32(0),
347 &skipProfilingInstrumentation);
348 jump(profilerExitTail);
349 bind(&skipProfilingInstrumentation);
350 }
351
352 movePtr(FramePointer, PseudoStackPointer);
353 syncStackPtr();
354 vixl::MacroAssembler::Pop(ARMRegister(FramePointer, 64));
355
356 vixl::MacroAssembler::Pop(vixl::lr);
357 syncStackPtr();
358 vixl::MacroAssembler::Ret(vixl::lr);
359
360 // If we are bailing out to baseline to handle an exception, jump to the
361 // bailout tail stub. Load 1 (true) in x0 (ReturnReg) to indicate success.
362 bind(&bailout);
363 Ldr(x2, MemOperand(PseudoStackPointer64,
364 ResumeFromException::offsetOfBailoutInfo()));
365 Ldr(PseudoStackPointer64,
366 MemOperand(PseudoStackPointer64,
367 ResumeFromException::offsetOfStackPointer()));
368 syncStackPtr();
369 Mov(x0, 1);
370 jump(bailoutTail);
371
372 // Reset SP and FP; SP is pointing to the unwound return address to the wasm
373 // interpreter entry, so we can just ret().
374 bind(&wasmInterpEntry);
375 Ldr(x29, MemOperand(PseudoStackPointer64,
376 ResumeFromException::offsetOfFramePointer()));
377 Ldr(PseudoStackPointer64,
378 MemOperand(PseudoStackPointer64,
379 ResumeFromException::offsetOfStackPointer()));
380 syncStackPtr();
381 Mov(x23, int64_t(wasm::InterpFailInstanceReg));
382 ret();
383
384 // Found a wasm catch handler, restore state and jump to it.
385 bind(&wasmCatch);
386 wasm::GenerateJumpToCatchHandler(asMasm(), PseudoStackPointer, r0, r1);
387
388 MOZ_ASSERT(GetStackPointer64().Is(PseudoStackPointer64));
389 }
390
391 void MacroAssemblerCompat::profilerEnterFrame(Register framePtr,
392 Register scratch) {
393 asMasm().loadJSContext(scratch);
394 loadPtr(Address(scratch, offsetof(JSContext, profilingActivation_)), scratch);
395 storePtr(framePtr,
396 Address(scratch, JitActivation::offsetOfLastProfilingFrame()));
397 storePtr(ImmPtr(nullptr),
398 Address(scratch, JitActivation::offsetOfLastProfilingCallSite()));
399 }
400
401 void MacroAssemblerCompat::profilerExitFrame() {
402 jump(asMasm().runtime()->jitRuntime()->getProfilerExitFrameTail());
403 }
404
405 Assembler::Condition MacroAssemblerCompat::testStringTruthy(
406 bool truthy, const ValueOperand& value) {
407 vixl::UseScratchRegisterScope temps(this);
408 const Register scratch = temps.AcquireX().asUnsized();
409 const ARMRegister scratch32(scratch, 32);
410 const ARMRegister scratch64(scratch, 64);
411
412 MOZ_ASSERT(value.valueReg() != scratch);
413
414 unboxString(value, scratch);
415 Ldr(scratch32, MemOperand(scratch64, JSString::offsetOfLength()));
416 Cmp(scratch32, Operand(0));
417 return truthy ? Condition::NonZero : Condition::Zero;
418 }
419
420 Assembler::Condition MacroAssemblerCompat::testBigIntTruthy(
421 bool truthy, const ValueOperand& value) {
422 vixl::UseScratchRegisterScope temps(this);
423 const Register scratch = temps.AcquireX().asUnsized();
424
425 MOZ_ASSERT(value.valueReg() != scratch);
426
427 unboxBigInt(value, scratch);
428 load32(Address(scratch, BigInt::offsetOfDigitLength()), scratch);
429 cmp32(scratch, Imm32(0));
430 return truthy ? Condition::NonZero : Condition::Zero;
431 }
432
433 void MacroAssemblerCompat::breakpoint() {
434 // Note, other payloads are possible, but GDB is known to misinterpret them
435 // sometimes and iloop on the breakpoint instead of stopping properly.
436 Brk(0xf000);
437 }
438
439 // Either `any` is valid or `sixtyfour` is valid. Return a 32-bit ARMRegister
440 // in the first case and an ARMRegister of the desired size in the latter case.
441
442 static inline ARMRegister SelectGPReg(AnyRegister any, Register64 sixtyfour,
443 unsigned size = 64) {
444 MOZ_ASSERT(any.isValid() != (sixtyfour != Register64::Invalid()));
445
446 if (sixtyfour == Register64::Invalid()) {
447 return ARMRegister(any.gpr(), 32);
448 }
449
450 return ARMRegister(sixtyfour.reg, size);
451 }
452
453 // Assert that `sixtyfour` is invalid and then return an FP register from `any`
454 // of the desired size.
455
456 static inline ARMFPRegister SelectFPReg(AnyRegister any, Register64 sixtyfour,
457 unsigned size) {
458 MOZ_ASSERT(sixtyfour == Register64::Invalid());
459 return ARMFPRegister(any.fpu(), size);
460 }
461
462 void MacroAssemblerCompat::wasmLoadImpl(const wasm::MemoryAccessDesc& access,
463 Register memoryBase_, Register ptr_,
464 AnyRegister outany, Register64 out64) {
465 access.assertOffsetInGuardPages();
466 uint32_t offset = access.offset32();
467
468 MOZ_ASSERT(memoryBase_ != ptr_);
469
470 ARMRegister memoryBase(memoryBase_, 64);
471 ARMRegister ptr(ptr_, 64);
472 if (offset) {
473 vixl::UseScratchRegisterScope temps(this);
474 ARMRegister scratch = temps.AcquireX();
475 Add(scratch, ptr, Operand(offset));
476 MemOperand srcAddr(memoryBase, scratch);
477 wasmLoadImpl(access, srcAddr, outany, out64);
478 } else {
479 MemOperand srcAddr(memoryBase, ptr);
480 wasmLoadImpl(access, srcAddr, outany, out64);
481 }
482 }
483
484 void MacroAssemblerCompat::wasmLoadImpl(const wasm::MemoryAccessDesc& access,
485 MemOperand srcAddr, AnyRegister outany,
486 Register64 out64) {
487 MOZ_ASSERT_IF(access.isSplatSimd128Load() || access.isWidenSimd128Load(),
488 access.type() == Scalar::Float64);
489
490 // NOTE: the generated code must match the assembly code in gen_load in
491 // GenerateAtomicOperations.py
492 asMasm().memoryBarrierBefore(access.sync());
493
494 FaultingCodeOffset fco;
495 switch (access.type()) {
496 case Scalar::Int8:
497 fco = Ldrsb(SelectGPReg(outany, out64), srcAddr);
498 break;
499 case Scalar::Uint8:
500 fco = Ldrb(SelectGPReg(outany, out64), srcAddr);
501 break;
502 case Scalar::Int16:
503 fco = Ldrsh(SelectGPReg(outany, out64), srcAddr);
504 break;
505 case Scalar::Uint16:
506 fco = Ldrh(SelectGPReg(outany, out64), srcAddr);
507 break;
508 case Scalar::Int32:
509 if (out64 != Register64::Invalid()) {
510 fco = Ldrsw(SelectGPReg(outany, out64), srcAddr);
511 } else {
512 fco = Ldr(SelectGPReg(outany, out64, 32), srcAddr);
513 }
514 break;
515 case Scalar::Uint32:
516 fco = Ldr(SelectGPReg(outany, out64, 32), srcAddr);
517 break;
518 case Scalar::Int64:
519 fco = Ldr(SelectGPReg(outany, out64), srcAddr);
520 break;
521 case Scalar::Float32:
522 // LDR does the right thing also for access.isZeroExtendSimd128Load()
523 fco = Ldr(SelectFPReg(outany, out64, 32), srcAddr);
524 break;
525 case Scalar::Float64:
526 if (access.isSplatSimd128Load() || access.isWidenSimd128Load()) {
527 ScratchSimd128Scope scratch_(asMasm());
528 ARMFPRegister scratch = Simd1D(scratch_);
529 fco = Ldr(scratch, srcAddr);
530 if (access.isSplatSimd128Load()) {
531 Dup(SelectFPReg(outany, out64, 128).V2D(), scratch, 0);
532 } else {
533 MOZ_ASSERT(access.isWidenSimd128Load());
534 switch (access.widenSimdOp()) {
535 case wasm::SimdOp::V128Load8x8S:
536 Sshll(SelectFPReg(outany, out64, 128).V8H(), scratch.V8B(), 0);
537 break;
538 case wasm::SimdOp::V128Load8x8U:
539 Ushll(SelectFPReg(outany, out64, 128).V8H(), scratch.V8B(), 0);
540 break;
541 case wasm::SimdOp::V128Load16x4S:
542 Sshll(SelectFPReg(outany, out64, 128).V4S(), scratch.V4H(), 0);
543 break;
544 case wasm::SimdOp::V128Load16x4U:
545 Ushll(SelectFPReg(outany, out64, 128).V4S(), scratch.V4H(), 0);
546 break;
547 case wasm::SimdOp::V128Load32x2S:
548 Sshll(SelectFPReg(outany, out64, 128).V2D(), scratch.V2S(), 0);
549 break;
550 case wasm::SimdOp::V128Load32x2U:
551 Ushll(SelectFPReg(outany, out64, 128).V2D(), scratch.V2S(), 0);
552 break;
553 default:
554 MOZ_CRASH("Unexpected widening op for wasmLoad");
555 }
556 }
557 } else {
558 // LDR does the right thing also for access.isZeroExtendSimd128Load()
559 fco = Ldr(SelectFPReg(outany, out64, 64), srcAddr);
560 }
561 break;
562 case Scalar::Simd128:
563 fco = Ldr(SelectFPReg(outany, out64, 128), srcAddr);
564 break;
565 case Scalar::Uint8Clamped:
566 case Scalar::BigInt64:
567 case Scalar::BigUint64:
568 case Scalar::Float16:
569 case Scalar::MaxTypedArrayViewType:
570 MOZ_CRASH("unexpected array type");
571 }
572
573 append(access, wasm::TrapMachineInsnForLoad(byteSize(access.type())), fco);
574
575 asMasm().memoryBarrierAfter(access.sync());
576 }
577
578 // Return true if `address` can be represented as an immediate (possibly scaled
579 // by the access size) in an LDR/STR type instruction.
580 //
581 // For more about the logic here, see vixl::MacroAssembler::LoadStoreMacro().
582 static bool IsLSImmediateOffset(uint64_t address, size_t accessByteSize) {
583 // The predicates below operate on signed values only.
584 if (address > INT64_MAX) {
585 return false;
586 }
587
588 // The access size is always a power of 2, so computing the log amounts to
589 // counting trailing zeroes.
590 unsigned logAccessSize = mozilla::CountTrailingZeroes32(accessByteSize);
591 return (MacroAssemblerCompat::IsImmLSUnscaled(int64_t(address)) ||
592 MacroAssemblerCompat::IsImmLSScaled(int64_t(address), logAccessSize));
593 }
594
595 void MacroAssemblerCompat::wasmLoadAbsolute(
596 const wasm::MemoryAccessDesc& access, Register memoryBase, uint64_t address,
597 AnyRegister output, Register64 out64) {
598 if (!IsLSImmediateOffset(address, access.byteSize())) {
599 // The access will require the constant to be loaded into a temp register.
600 // Do so here, to keep the logic in wasmLoadImpl() tractable wrt emitting
601 // trap information.
602 //
603 // Almost all constant addresses will in practice be handled by a single MOV
604 // so do not worry about additional optimizations here.
605 vixl::UseScratchRegisterScope temps(this);
606 ARMRegister scratch = temps.AcquireX();
607 Mov(scratch, address);
608 MemOperand srcAddr(X(memoryBase), scratch);
609 wasmLoadImpl(access, srcAddr, output, out64);
610 } else {
611 MemOperand srcAddr(X(memoryBase), address);
612 wasmLoadImpl(access, srcAddr, output, out64);
613 }
614 }
615
616 void MacroAssemblerCompat::wasmStoreImpl(const wasm::MemoryAccessDesc& access,
617 AnyRegister valany, Register64 val64,
618 Register memoryBase_, Register ptr_) {
619 access.assertOffsetInGuardPages();
620 uint32_t offset = access.offset32();
621
622 ARMRegister memoryBase(memoryBase_, 64);
623 ARMRegister ptr(ptr_, 64);
624 if (offset) {
625 vixl::UseScratchRegisterScope temps(this);
626 ARMRegister scratch = temps.AcquireX();
627 Add(scratch, ptr, Operand(offset));
628 MemOperand destAddr(memoryBase, scratch);
629 wasmStoreImpl(access, destAddr, valany, val64);
630 } else {
631 MemOperand destAddr(memoryBase, ptr);
632 wasmStoreImpl(access, destAddr, valany, val64);
633 }
634 }
635
636 void MacroAssemblerCompat::wasmStoreImpl(const wasm::MemoryAccessDesc& access,
637 MemOperand dstAddr, AnyRegister valany,
638 Register64 val64) {
639 // NOTE: the generated code must match the assembly code in gen_store in
640 // GenerateAtomicOperations.py
641 asMasm().memoryBarrierBefore(access.sync());
642
643 FaultingCodeOffset fco;
644 switch (access.type()) {
645 case Scalar::Int8:
646 case Scalar::Uint8:
647 fco = Strb(SelectGPReg(valany, val64), dstAddr);
648 break;
649 case Scalar::Int16:
650 case Scalar::Uint16:
651 fco = Strh(SelectGPReg(valany, val64), dstAddr);
652 break;
653 case Scalar::Int32:
654 case Scalar::Uint32:
655 fco = Str(SelectGPReg(valany, val64), dstAddr);
656 break;
657 case Scalar::Int64:
658 fco = Str(SelectGPReg(valany, val64), dstAddr);
659 break;
660 case Scalar::Float32:
661 fco = Str(SelectFPReg(valany, val64, 32), dstAddr);
662 break;
663 case Scalar::Float64:
664 fco = Str(SelectFPReg(valany, val64, 64), dstAddr);
665 break;
666 case Scalar::Simd128:
667 fco = Str(SelectFPReg(valany, val64, 128), dstAddr);
668 break;
669 case Scalar::Uint8Clamped:
670 case Scalar::BigInt64:
671 case Scalar::BigUint64:
672 case Scalar::Float16:
673 case Scalar::MaxTypedArrayViewType:
674 MOZ_CRASH("unexpected array type");
675 }
676
677 append(access, wasm::TrapMachineInsnForStore(byteSize(access.type())), fco);
678
679 asMasm().memoryBarrierAfter(access.sync());
680 }
681
682 void MacroAssemblerCompat::wasmStoreAbsolute(
683 const wasm::MemoryAccessDesc& access, AnyRegister value, Register64 value64,
684 Register memoryBase, uint64_t address) {
685 // See comments in wasmLoadAbsolute.
686 unsigned logAccessSize = mozilla::CountTrailingZeroes32(access.byteSize());
687 if (address > INT64_MAX || !(IsImmLSScaled(int64_t(address), logAccessSize) ||
688 IsImmLSUnscaled(int64_t(address)))) {
689 vixl::UseScratchRegisterScope temps(this);
690 ARMRegister scratch = temps.AcquireX();
691 Mov(scratch, address);
692 MemOperand destAddr(X(memoryBase), scratch);
693 wasmStoreImpl(access, destAddr, value, value64);
694 } else {
695 MemOperand destAddr(X(memoryBase), address);
696 wasmStoreImpl(access, destAddr, value, value64);
697 }
698 }
699
700 void MacroAssemblerCompat::compareSimd128Int(Assembler::Condition cond,
701 ARMFPRegister dest,
702 ARMFPRegister lhs,
703 ARMFPRegister rhs) {
704 switch (cond) {
705 case Assembler::Equal:
706 Cmeq(dest, lhs, rhs);
707 break;
708 case Assembler::NotEqual:
709 Cmeq(dest, lhs, rhs);
710 Mvn(dest, dest);
711 break;
712 case Assembler::GreaterThan:
713 Cmgt(dest, lhs, rhs);
714 break;
715 case Assembler::GreaterThanOrEqual:
716 Cmge(dest, lhs, rhs);
717 break;
718 case Assembler::LessThan:
719 Cmgt(dest, rhs, lhs);
720 break;
721 case Assembler::LessThanOrEqual:
722 Cmge(dest, rhs, lhs);
723 break;
724 case Assembler::Above:
725 Cmhi(dest, lhs, rhs);
726 break;
727 case Assembler::AboveOrEqual:
728 Cmhs(dest, lhs, rhs);
729 break;
730 case Assembler::Below:
731 Cmhi(dest, rhs, lhs);
732 break;
733 case Assembler::BelowOrEqual:
734 Cmhs(dest, rhs, lhs);
735 break;
736 default:
737 MOZ_CRASH("Unexpected SIMD integer condition");
738 }
739 }
740
741 void MacroAssemblerCompat::compareSimd128Float(Assembler::Condition cond,
742 ARMFPRegister dest,
743 ARMFPRegister lhs,
744 ARMFPRegister rhs) {
745 switch (cond) {
746 case Assembler::Equal:
747 Fcmeq(dest, lhs, rhs);
748 break;
749 case Assembler::NotEqual:
750 Fcmeq(dest, lhs, rhs);
751 Mvn(dest, dest);
752 break;
753 case Assembler::GreaterThan:
754 Fcmgt(dest, lhs, rhs);
755 break;
756 case Assembler::GreaterThanOrEqual:
757 Fcmge(dest, lhs, rhs);
758 break;
759 case Assembler::LessThan:
760 Fcmgt(dest, rhs, lhs);
761 break;
762 case Assembler::LessThanOrEqual:
763 Fcmge(dest, rhs, lhs);
764 break;
765 default:
766 MOZ_CRASH("Unexpected SIMD integer condition");
767 }
768 }
769
770 void MacroAssemblerCompat::rightShiftInt8x16(FloatRegister lhs, Register rhs,
771 FloatRegister dest,
772 bool isUnsigned) {
773 ScratchSimd128Scope scratch_(asMasm());
774 ARMFPRegister shift = Simd16B(scratch_);
775
776 Dup(shift, ARMRegister(rhs, 32));
777 Neg(shift, shift);
778
779 if (isUnsigned) {
780 Ushl(Simd16B(dest), Simd16B(lhs), shift);
781 } else {
782 Sshl(Simd16B(dest), Simd16B(lhs), shift);
783 }
784 }
785
786 void MacroAssemblerCompat::rightShiftInt16x8(FloatRegister lhs, Register rhs,
787 FloatRegister dest,
788 bool isUnsigned) {
789 ScratchSimd128Scope scratch_(asMasm());
790 ARMFPRegister shift = Simd8H(scratch_);
791
792 Dup(shift, ARMRegister(rhs, 32));
793 Neg(shift, shift);
794
795 if (isUnsigned) {
796 Ushl(Simd8H(dest), Simd8H(lhs), shift);
797 } else {
798 Sshl(Simd8H(dest), Simd8H(lhs), shift);
799 }
800 }
801
802 void MacroAssemblerCompat::rightShiftInt32x4(FloatRegister lhs, Register rhs,
803 FloatRegister dest,
804 bool isUnsigned) {
805 ScratchSimd128Scope scratch_(asMasm());
806 ARMFPRegister shift = Simd4S(scratch_);
807
808 Dup(shift, ARMRegister(rhs, 32));
809 Neg(shift, shift);
810
811 if (isUnsigned) {
812 Ushl(Simd4S(dest), Simd4S(lhs), shift);
813 } else {
814 Sshl(Simd4S(dest), Simd4S(lhs), shift);
815 }
816 }
817
818 void MacroAssemblerCompat::rightShiftInt64x2(FloatRegister lhs, Register rhs,
819 FloatRegister dest,
820 bool isUnsigned) {
821 ScratchSimd128Scope scratch_(asMasm());
822 ARMFPRegister shift = Simd2D(scratch_);
823
824 Dup(shift, ARMRegister(rhs, 64));
825 Neg(shift, shift);
826
827 if (isUnsigned) {
828 Ushl(Simd2D(dest), Simd2D(lhs), shift);
829 } else {
830 Sshl(Simd2D(dest), Simd2D(lhs), shift);
831 }
832 }
833
834 void MacroAssembler::reserveStack(uint32_t amount) {
835 // TODO: This bumps |sp| every time we reserve using a second register.
836 // It would save some instructions if we had a fixed frame size.
837 vixl::MacroAssembler::Claim(Operand(amount));
838 adjustFrame(amount);
839 }
840
841 void MacroAssembler::Push(RegisterOrSP reg) {
842 if (IsHiddenSP(reg)) {
843 push(sp);
844 } else {
845 push(AsRegister(reg));
846 }
847 adjustFrame(sizeof(intptr_t));
848 }
849
850 //{{{ check_macroassembler_style
851 // ===============================================================
852 // MacroAssembler high-level usage.
853
854 void MacroAssembler::flush() { Assembler::flush(); }
855
856 // ===============================================================
857 // Stack manipulation functions.
858
859 // Routines for saving/restoring registers on the stack. The format is:
860 //
861 // (highest address)
862 //
863 // integer (X) regs in any order size: 8 * # int regs
864 //
865 // if # int regs is odd,
866 // then an 8 byte alignment hole size: 0 or 8
867 //
868 // double (D) regs in any order size: 8 * # double regs
869 //
870 // if # double regs is odd,
871 // then an 8 byte alignment hole size: 0 or 8
872 //
873 // vector (Q) regs in any order size: 16 * # vector regs
874 //
875 // (lowest address)
876 //
877 // Hence the size of the save area is 0 % 16. And, provided that the base
878 // (highest) address is 16-aligned, then the vector reg save/restore accesses
879 // will also be 16-aligned, as will pairwise operations for the double regs.
880 //
881 // Implied by this is that the format of the double and vector dump area
882 // corresponds with what FloatRegister::GetPushSizeInBytes computes.
883 // See block comment in MacroAssembler.h for more details.
884
885 size_t MacroAssembler::PushRegsInMaskSizeInBytes(LiveRegisterSet set) {
886 size_t numIntRegs = set.gprs().size();
887 return ((numIntRegs + 1) & ~1) * sizeof(intptr_t) +
888 FloatRegister::GetPushSizeInBytes(set.fpus());
889 }
890
891 // Generate code to dump the values in `set`, either on the stack if `dest` is
892 // `Nothing` or working backwards from the address denoted by `dest` if it is
893 // `Some`. These two cases are combined so as to minimise the chance of
894 // mistakenly generating different formats for the same `set`, given that the
895 // `Some` `dest` case is used extremely rarely.
896 static void PushOrStoreRegsInMask(MacroAssembler* masm, LiveRegisterSet set,
897 mozilla::Maybe<Address> dest) {
898 static_assert(sizeof(FloatRegisters::RegisterContent) == 16);
899
900 // If we're saving to arbitrary memory, check the destination is big enough.
901 if (dest) {
902 mozilla::DebugOnly<size_t> bytesRequired =
903 MacroAssembler::PushRegsInMaskSizeInBytes(set);
904 MOZ_ASSERT(dest->offset >= 0);
905 MOZ_ASSERT(((size_t)dest->offset) >= bytesRequired);
906 }
907
908 // Note the high limit point; we'll check it again later.
909 mozilla::DebugOnly<size_t> maxExtentInitial =
910 dest ? dest->offset : masm->framePushed();
911
912 // Gather up the integer registers in groups of four, and either push each
913 // group as a single transfer so as to minimise the number of stack pointer
914 // changes, or write them individually to memory. Take care to ensure the
915 // space used remains 16-aligned.
916 for (GeneralRegisterBackwardIterator iter(set.gprs()); iter.more();) {
917 vixl::CPURegister src[4] = {vixl::NoCPUReg, vixl::NoCPUReg, vixl::NoCPUReg,
918 vixl::NoCPUReg};
919 size_t i;
920 for (i = 0; i < 4 && iter.more(); i++) {
921 src[i] = ARMRegister(*iter, 64);
922 ++iter;
923 }
924 MOZ_ASSERT(i > 0);
925
926 if (i == 1 || i == 3) {
927 // Ensure the stack remains 16-aligned
928 MOZ_ASSERT(!iter.more());
929 src[i] = vixl::xzr;
930 i++;
931 }
932 MOZ_ASSERT(i == 2 || i == 4);
933
934 if (dest) {
935 for (size_t j = 0; j < i; j++) {
936 Register ireg = Register::FromCode(src[j].IsZero() ? Registers::xzr
937 : src[j].code());
938 dest->offset -= sizeof(intptr_t);
939 masm->storePtr(ireg, *dest);
940 }
941 } else {
942 masm->adjustFrame(i * 8);
943 masm->vixl::MacroAssembler::Push(src[0], src[1], src[2], src[3]);
944 }
945 }
946
947 // Now the same for the FP double registers. Note that because of how
948 // ReduceSetForPush works, an underlying AArch64 SIMD/FP register can either
949 // be present as a double register, or as a V128 register, but not both.
950 // Firstly, round up the registers to be pushed.
951
952 FloatRegisterSet fpuSet(set.fpus().reduceSetForPush());
953 vixl::CPURegister allSrcs[FloatRegisters::TotalPhys];
954 size_t numAllSrcs = 0;
955
956 for (FloatRegisterBackwardIterator iter(fpuSet); iter.more(); ++iter) {
957 FloatRegister reg = *iter;
958 if (reg.isDouble()) {
959 MOZ_RELEASE_ASSERT(numAllSrcs < FloatRegisters::TotalPhys);
960 allSrcs[numAllSrcs] = ARMFPRegister(reg, 64);
961 numAllSrcs++;
962 } else {
963 MOZ_ASSERT(reg.isSimd128());
964 }
965 }
966 MOZ_RELEASE_ASSERT(numAllSrcs <= FloatRegisters::TotalPhys);
967
968 if ((numAllSrcs & 1) == 1) {
969 // We've got an odd number of doubles. In order to maintain 16-alignment,
970 // push the last register twice. We'll skip over the duplicate in
971 // PopRegsInMaskIgnore.
972 allSrcs[numAllSrcs] = allSrcs[numAllSrcs - 1];
973 numAllSrcs++;
974 }
975 MOZ_RELEASE_ASSERT(numAllSrcs <= FloatRegisters::TotalPhys);
976 MOZ_RELEASE_ASSERT((numAllSrcs & 1) == 0);
977
978 // And now generate the transfers.
979 size_t i;
980 if (dest) {
981 for (i = 0; i < numAllSrcs; i++) {
982 FloatRegister freg =
983 FloatRegister(FloatRegisters::FPRegisterID(allSrcs[i].code()),
984 FloatRegisters::Kind::Double);
985 dest->offset -= sizeof(double);
986 masm->storeDouble(freg, *dest);
987 }
988 } else {
989 i = 0;
990 while (i < numAllSrcs) {
991 vixl::CPURegister src[4] = {vixl::NoCPUReg, vixl::NoCPUReg,
992 vixl::NoCPUReg, vixl::NoCPUReg};
993 size_t j;
994 for (j = 0; j < 4 && j + i < numAllSrcs; j++) {
995 src[j] = allSrcs[j + i];
996 }
997 masm->adjustFrame(8 * j);
998 masm->vixl::MacroAssembler::Push(src[0], src[1], src[2], src[3]);
999 i += j;
1000 }
1001 }
1002 MOZ_ASSERT(i == numAllSrcs);
1003
1004 // Finally, deal with the SIMD (V128) registers. This is a bit simpler
1005 // as there's no need for special-casing to maintain 16-alignment.
1006
1007 numAllSrcs = 0;
1008 for (FloatRegisterBackwardIterator iter(fpuSet); iter.more(); ++iter) {
1009 FloatRegister reg = *iter;
1010 if (reg.isSimd128()) {
1011 MOZ_RELEASE_ASSERT(numAllSrcs < FloatRegisters::TotalPhys);
1012 allSrcs[numAllSrcs] = ARMFPRegister(reg, 128);
1013 numAllSrcs++;
1014 }
1015 }
1016 MOZ_RELEASE_ASSERT(numAllSrcs <= FloatRegisters::TotalPhys);
1017
1018 // Generate the transfers.
1019 if (dest) {
1020 for (i = 0; i < numAllSrcs; i++) {
1021 FloatRegister freg =
1022 FloatRegister(FloatRegisters::FPRegisterID(allSrcs[i].code()),
1023 FloatRegisters::Kind::Simd128);
1024 dest->offset -= FloatRegister::SizeOfSimd128;
1025 masm->storeUnalignedSimd128(freg, *dest);
1026 }
1027 } else {
1028 i = 0;
1029 while (i < numAllSrcs) {
1030 vixl::CPURegister src[4] = {vixl::NoCPUReg, vixl::NoCPUReg,
1031 vixl::NoCPUReg, vixl::NoCPUReg};
1032 size_t j;
1033 for (j = 0; j < 4 && j + i < numAllSrcs; j++) {
1034 src[j] = allSrcs[j + i];
1035 }
1036 masm->adjustFrame(16 * j);
1037 masm->vixl::MacroAssembler::Push(src[0], src[1], src[2], src[3]);
1038 i += j;
1039 }
1040 }
1041 MOZ_ASSERT(i == numAllSrcs);
1042
1043 // Final overrun check.
1044 if (dest) {
1045 MOZ_ASSERT(maxExtentInitial - dest->offset ==
1046 MacroAssembler::PushRegsInMaskSizeInBytes(set));
1047 } else {
1048 MOZ_ASSERT(masm->framePushed() - maxExtentInitial ==
1049 MacroAssembler::PushRegsInMaskSizeInBytes(set));
1050 }
1051 }
1052
1053 void MacroAssembler::PushRegsInMask(LiveRegisterSet set) {
1054 PushOrStoreRegsInMask(this, set, mozilla::Nothing());
1055 }
1056
1057 void MacroAssembler::storeRegsInMask(LiveRegisterSet set, Address dest,
1058 Register scratch) {
1059 PushOrStoreRegsInMask(this, set, mozilla::Some(dest));
1060 }
1061
1062 // This is a helper function for PopRegsInMaskIgnore below. It emits the
1063 // loads described by dests[0] and [1] and offsets[0] and [1], generating a
1064 // load-pair if it can.
1065 static void GeneratePendingLoadsThenFlush(MacroAssembler* masm,
1066 vixl::CPURegister* dests,
1067 uint32_t* offsets,
1068 uint32_t transactionSize) {
1069 // Generate the loads ..
1070 if (!dests[0].IsNone()) {
1071 if (!dests[1].IsNone()) {
1072 // [0] and [1] both present.
1073 if (offsets[0] + transactionSize == offsets[1]) {
1074 masm->Ldp(dests[0], dests[1],
1075 MemOperand(masm->GetStackPointer64(), offsets[0]));
1076 } else {
1077 // Theoretically we could check for a load-pair with the destinations
1078 // switched, but our callers will never generate that. Hence there's
1079 // no loss in giving up at this point and generating two loads.
1080 masm->Ldr(dests[0], MemOperand(masm->GetStackPointer64(), offsets[0]));
1081 masm->Ldr(dests[1], MemOperand(masm->GetStackPointer64(), offsets[1]));
1082 }
1083 } else {
1084 // [0] only.
1085 masm->Ldr(dests[0], MemOperand(masm->GetStackPointer64(), offsets[0]));
1086 }
1087 } else {
1088 if (!dests[1].IsNone()) {
1089 // [1] only. Can't happen because callers always fill [0] before [1].
1090 MOZ_CRASH("GenerateLoadsThenFlush");
1091 } else {
1092 // Neither entry valid. This can happen.
1093 }
1094 }
1095
1096 // .. and flush.
1097 dests[0] = dests[1] = vixl::NoCPUReg;
1098 offsets[0] = offsets[1] = 0;
1099 }
1100
1101 void MacroAssembler::PopRegsInMaskIgnore(LiveRegisterSet set,
1102 LiveRegisterSet ignore) {
1103 mozilla::DebugOnly<size_t> framePushedInitial = framePushed();
1104
1105 // The offset of the data from the stack pointer.
1106 uint32_t offset = 0;
1107
1108 // The set of FP/SIMD registers we need to restore.
1109 FloatRegisterSet fpuSet(set.fpus().reduceSetForPush());
1110
1111 // The set of registers to ignore. BroadcastToAllSizes() is used to avoid
1112 // any ambiguities arising from (eg) `fpuSet` containing q17 but `ignore`
1113 // containing d17.
1114 FloatRegisterSet ignoreFpusBroadcasted(
1115 FloatRegister::BroadcastToAllSizes(ignore.fpus()));
1116
1117 // First recover the SIMD (V128) registers. This is straightforward in that
1118 // we don't need to think about alignment holes.
1119
1120 // These three form a two-entry queue that holds loads that we know we
1121 // need, but which we haven't yet emitted.
1122 vixl::CPURegister pendingDests[2] = {vixl::NoCPUReg, vixl::NoCPUReg};
1123 uint32_t pendingOffsets[2] = {0, 0};
1124 size_t nPending = 0;
1125
1126 for (FloatRegisterIterator iter(fpuSet); iter.more(); ++iter) {
1127 FloatRegister reg = *iter;
1128 if (reg.isDouble()) {
1129 continue;
1130 }
1131 MOZ_RELEASE_ASSERT(reg.isSimd128());
1132
1133 uint32_t offsetForReg = offset;
1134 offset += FloatRegister::SizeOfSimd128;
1135
1136 if (ignoreFpusBroadcasted.hasRegisterIndex(reg)) {
1137 continue;
1138 }
1139
1140 MOZ_ASSERT(nPending <= 2);
1141 if (nPending == 2) {
1142 GeneratePendingLoadsThenFlush(this, pendingDests, pendingOffsets, 16);
1143 nPending = 0;
1144 }
1145 pendingDests[nPending] = ARMFPRegister(reg, 128);
1146 pendingOffsets[nPending] = offsetForReg;
1147 nPending++;
1148 }
1149 GeneratePendingLoadsThenFlush(this, pendingDests, pendingOffsets, 16);
1150 nPending = 0;
1151
1152 MOZ_ASSERT((offset % 16) == 0);
1153
1154 // Now recover the FP double registers. This is more tricky in that we need
1155 // to skip over the lowest-addressed of them if the number of them was odd.
1156
1157 if ((((fpuSet.bits() & FloatRegisters::AllDoubleMask).size()) & 1) == 1) {
1158 offset += sizeof(double);
1159 }
1160
1161 for (FloatRegisterIterator iter(fpuSet); iter.more(); ++iter) {
1162 FloatRegister reg = *iter;
1163 if (reg.isSimd128()) {
1164 continue;
1165 }
1166 /* true but redundant, per loop above: MOZ_RELEASE_ASSERT(reg.isDouble()) */
1167
1168 uint32_t offsetForReg = offset;
1169 offset += sizeof(double);
1170
1171 if (ignoreFpusBroadcasted.hasRegisterIndex(reg)) {
1172 continue;
1173 }
1174
1175 MOZ_ASSERT(nPending <= 2);
1176 if (nPending == 2) {
1177 GeneratePendingLoadsThenFlush(this, pendingDests, pendingOffsets, 8);
1178 nPending = 0;
1179 }
1180 pendingDests[nPending] = ARMFPRegister(reg, 64);
1181 pendingOffsets[nPending] = offsetForReg;
1182 nPending++;
1183 }
1184 GeneratePendingLoadsThenFlush(this, pendingDests, pendingOffsets, 8);
1185 nPending = 0;
1186
1187 MOZ_ASSERT((offset % 16) == 0);
1188 MOZ_ASSERT(offset == set.fpus().getPushSizeInBytes());
1189
1190 // And finally recover the integer registers, again skipping an alignment
1191 // hole if it exists.
1192
1193 if ((set.gprs().size() & 1) == 1) {
1194 offset += sizeof(uint64_t);
1195 }
1196
1197 for (GeneralRegisterIterator iter(set.gprs()); iter.more(); ++iter) {
1198 Register reg = *iter;
1199
1200 uint32_t offsetForReg = offset;
1201 offset += sizeof(uint64_t);
1202
1203 if (ignore.has(reg)) {
1204 continue;
1205 }
1206
1207 MOZ_ASSERT(nPending <= 2);
1208 if (nPending == 2) {
1209 GeneratePendingLoadsThenFlush(this, pendingDests, pendingOffsets, 8);
1210 nPending = 0;
1211 }
1212 pendingDests[nPending] = ARMRegister(reg, 64);
1213 pendingOffsets[nPending] = offsetForReg;
1214 nPending++;
1215 }
1216 GeneratePendingLoadsThenFlush(this, pendingDests, pendingOffsets, 8);
1217
1218 MOZ_ASSERT((offset % 16) == 0);
1219
1220 size_t bytesPushed = PushRegsInMaskSizeInBytes(set);
1221 MOZ_ASSERT(offset == bytesPushed);
1222 freeStack(bytesPushed);
1223 }
1224
1225 void MacroAssembler::Push(Register reg) {
1226 push(reg);
1227 adjustFrame(sizeof(intptr_t));
1228 }
1229
1230 void MacroAssembler::Push(Register reg1, Register reg2, Register reg3,
1231 Register reg4) {
1232 push(reg1, reg2, reg3, reg4);
1233 adjustFrame(4 * sizeof(intptr_t));
1234 }
1235
1236 void MacroAssembler::Push(const Imm32 imm) {
1237 push(imm);
1238 adjustFrame(sizeof(intptr_t));
1239 }
1240
1241 void MacroAssembler::Push(const ImmWord imm) {
1242 push(imm);
1243 adjustFrame(sizeof(intptr_t));
1244 }
1245
1246 void MacroAssembler::Push(const ImmPtr imm) {
1247 push(imm);
1248 adjustFrame(sizeof(intptr_t));
1249 }
1250
1251 void MacroAssembler::Push(const ImmGCPtr ptr) {
1252 push(ptr);
1253 adjustFrame(sizeof(intptr_t));
1254 }
1255
1256 void MacroAssembler::Push(FloatRegister f) {
1257 push(f);
1258 adjustFrame(sizeof(double));
1259 }
1260
1261 void MacroAssembler::PushBoxed(FloatRegister reg) {
1262 subFromStackPtr(Imm32(sizeof(double)));
1263 boxDouble(reg, Address(getStackPointer(), 0));
1264 adjustFrame(sizeof(double));
1265 }
1266
1267 void MacroAssembler::Pop(Register reg) {
1268 pop(reg);
1269 adjustFrame(-1 * int64_t(sizeof(int64_t)));
1270 }
1271
1272 void MacroAssembler::Pop(FloatRegister f) {
1273 loadDouble(Address(getStackPointer(), 0), f);
1274 freeStack(sizeof(double));
1275 }
1276
1277 void MacroAssembler::Pop(const ValueOperand& val) {
1278 pop(val);
1279 adjustFrame(-1 * int64_t(sizeof(int64_t)));
1280 }
1281
1282 void MacroAssembler::freeStackTo(uint32_t framePushed) {
1283 MOZ_ASSERT(framePushed <= framePushed_);
1284 Sub(GetStackPointer64(), X(FramePointer), Operand(int32_t(framePushed)));
1285 syncStackPtr();
1286 framePushed_ = framePushed;
1287 }
1288
1289 // ===============================================================
1290 // Simple call functions.
1291
1292 CodeOffset MacroAssembler::call(Register reg) {
1293 // This sync has been observed (and is expected) to be necessary.
1294 // eg testcase: tests/debug/bug1107525.js
1295 syncStackPtr();
1296 Blr(ARMRegister(reg, 64));
1297 return CodeOffset(currentOffset());
1298 }
1299
1300 CodeOffset MacroAssembler::call(Label* label) {
1301 // This sync has been observed (and is expected) to be necessary.
1302 // eg testcase: tests/basic/testBug504520Harder.js
1303 syncStackPtr();
1304 Bl(label);
1305 return CodeOffset(currentOffset());
1306 }
1307
1308 void MacroAssembler::call(ImmPtr imm) {
1309 // This sync has been observed (and is expected) to be necessary.
1310 // eg testcase: asm.js/testTimeout5.js
1311 syncStackPtr();
1312 vixl::UseScratchRegisterScope temps(this);
1313 MOZ_ASSERT(temps.IsAvailable(ScratchReg64)); // ip0
1314 temps.Exclude(ScratchReg64);
1315 movePtr(imm, ScratchReg64.asUnsized());
1316 Blr(ScratchReg64);
1317 }
1318
1319 void MacroAssembler::call(ImmWord imm) { call(ImmPtr((void*)imm.value)); }
1320
1321 CodeOffset MacroAssembler::call(wasm::SymbolicAddress imm) {
1322 vixl::UseScratchRegisterScope temps(this);
1323 const Register scratch = temps.AcquireX().asUnsized();
1324 // This sync is believed to be necessary, although no case in jit-test/tests
1325 // has been observed to cause SP != PSP here.
1326 syncStackPtr();
1327 movePtr(imm, scratch);
1328 Blr(ARMRegister(scratch, 64));
1329 return CodeOffset(currentOffset());
1330 }
1331
1332 void MacroAssembler::call(const Address& addr) {
1333 vixl::UseScratchRegisterScope temps(this);
1334 const Register scratch = temps.AcquireX().asUnsized();
1335 // This sync has been observed (and is expected) to be necessary.
1336 // eg testcase: tests/backup-point-bug1315634.js
1337 syncStackPtr();
1338 loadPtr(addr, scratch);
1339 Blr(ARMRegister(scratch, 64));
1340 }
1341
1342 void MacroAssembler::call(JitCode* c) {
1343 vixl::UseScratchRegisterScope temps(this);
1344 const ARMRegister scratch64 = temps.AcquireX();
1345 // This sync has been observed (and is expected) to be necessary.
1346 // eg testcase: arrays/new-array-undefined-undefined-more-args-2.js
1347 syncStackPtr();
1348 BufferOffset off = immPool64(scratch64, uint64_t(c->raw()));
1349 addPendingJump(off, ImmPtr(c->raw()), RelocationKind::JITCODE);
1350 blr(scratch64);
1351 }
1352
1353 CodeOffset MacroAssembler::callWithPatch() {
1354 // This needs to sync. Wasm goes through this one for intramodule calls.
1355 //
1356 // In other cases, wasm goes through masm.wasmCallImport(),
1357 // masm.wasmCallBuiltinInstanceMethod, masm.wasmCallIndirect, all of which
1358 // sync.
1359 //
1360 // This sync is believed to be necessary, although no case in jit-test/tests
1361 // has been observed to cause SP != PSP here.
1362 syncStackPtr();
1363 bl(0, LabelDoc());
1364 return CodeOffset(currentOffset());
1365 }
1366 void MacroAssembler::patchCall(uint32_t callerOffset, uint32_t calleeOffset) {
1367 Instruction* inst = getInstructionAt(BufferOffset(callerOffset - 4));
1368 MOZ_ASSERT(inst->IsBL());
1369 ptrdiff_t relTarget = (int)calleeOffset - ((int)callerOffset - 4);
1370 ptrdiff_t relTarget00 = relTarget >> 2;
1371 MOZ_RELEASE_ASSERT((relTarget & 0x3) == 0);
1372 MOZ_RELEASE_ASSERT(vixl::IsInt26(relTarget00));
1373 bl(inst, relTarget00);
1374 }
1375
1376 CodeOffset MacroAssembler::farJumpWithPatch() {
1377 vixl::UseScratchRegisterScope temps(this);
1378 const ARMRegister scratch = temps.AcquireX();
1379 const ARMRegister scratch2 = temps.AcquireX();
1380
1381 AutoForbidPoolsAndNops afp(this,
1382 /* max number of instructions in scope = */ 7);
1383
1384 mozilla::DebugOnly<uint32_t> before = currentOffset();
1385
1386 align(8); // At most one nop
1387
1388 Label branch;
1389 adr(scratch2, &branch);
1390 ldr(scratch, vixl::MemOperand(scratch2, 4));
1391 add(scratch2, scratch2, scratch);
1392 CodeOffset offs(currentOffset());
1393 bind(&branch);
1394 br(scratch2);
1395 Emit(UINT32_MAX);
1396 Emit(UINT32_MAX);
1397
1398 mozilla::DebugOnly<uint32_t> after = currentOffset();
1399
1400 MOZ_ASSERT_IF(!oom(), after - before == 24 || after - before == 28);
1401
1402 return offs;
1403 }
1404
1405 void MacroAssembler::patchFarJump(CodeOffset farJump, uint32_t targetOffset) {
1406 Instruction* inst1 = getInstructionAt(BufferOffset(farJump.offset() + 4));
1407 Instruction* inst2 = getInstructionAt(BufferOffset(farJump.offset() + 8));
1408
1409 int64_t distance = (int64_t)targetOffset - (int64_t)farJump.offset();
1410
1411 MOZ_ASSERT(inst1->InstructionBits() == UINT32_MAX);
1412 MOZ_ASSERT(inst2->InstructionBits() == UINT32_MAX);
1413
1414 inst1->SetInstructionBits((uint32_t)distance);
1415 inst2->SetInstructionBits((uint32_t)(distance >> 32));
1416 }
1417
1418 void MacroAssembler::patchFarJump(uint8_t* farJump, uint8_t* target) {
1419 Instruction* inst1 = (Instruction*)(farJump + 4);
1420 Instruction* inst2 = (Instruction*)(farJump + 8);
1421
1422 int64_t distance = (int64_t)target - (int64_t)farJump;
1423 MOZ_RELEASE_ASSERT(mozilla::Abs(distance) <=
1424 (intptr_t)jit::MaxCodeBytesPerProcess);
1425
1426 MOZ_ASSERT(inst1->InstructionBits() == UINT32_MAX);
1427 MOZ_ASSERT(inst2->InstructionBits() == UINT32_MAX);
1428
1429 inst1->SetInstructionBits((uint32_t)distance);
1430 inst2->SetInstructionBits((uint32_t)(distance >> 32));
1431 }
1432
1433 CodeOffset MacroAssembler::nopPatchableToCall() {
1434 AutoForbidPoolsAndNops afp(this,
1435 /* max number of instructions in scope = */ 1);
1436 Nop();
1437 return CodeOffset(currentOffset());
1438 }
1439
1440 void MacroAssembler::patchNopToCall(uint8_t* call, uint8_t* target) {
1441 uint8_t* inst = call - 4;
1442 Instruction* instr = reinterpret_cast<Instruction*>(inst);
1443 MOZ_ASSERT(instr->IsBL() || instr->IsNOP());
1444 bl(instr, (target - inst) >> 2);
1445 }
1446
1447 void MacroAssembler::patchCallToNop(uint8_t* call) {
1448 uint8_t* inst = call - 4;
1449 Instruction* instr = reinterpret_cast<Instruction*>(inst);
1450 MOZ_ASSERT(instr->IsBL() || instr->IsNOP());
1451 nop(instr);
1452 }
1453
1454 CodeOffset MacroAssembler::move32WithPatch(Register dest) {
1455 AutoForbidPoolsAndNops afp(this,
1456 /* max number of instructions in scope = */ 3);
1457 CodeOffset offs = CodeOffset(currentOffset());
1458 movz(ARMRegister(dest, 64), 0, 0);
1459 movk(ARMRegister(dest, 64), 0, 16);
1460 return offs;
1461 }
1462
1463 void MacroAssembler::patchMove32(CodeOffset offset, Imm32 n) {
1464 Instruction* i1 = getInstructionAt(BufferOffset(offset.offset()));
1465 MOZ_ASSERT(i1->IsMovz());
1466 i1->SetInstructionBits(i1->InstructionBits() | ImmMoveWide(n.value & 0xFFFF));
1467
1468 Instruction* i2 = getInstructionAt(BufferOffset(offset.offset() + 4));
1469 MOZ_ASSERT(i2->IsMovk());
1470 i2->SetInstructionBits(i2->InstructionBits() |
1471 ImmMoveWide((n.value >> 16) & 0xFFFF));
1472 }
1473
1474 void MacroAssembler::pushReturnAddress() {
1475 MOZ_RELEASE_ASSERT(!sp.Is(GetStackPointer64()), "Not valid");
1476 push(lr);
1477 }
1478
1479 void MacroAssembler::popReturnAddress() {
1480 MOZ_RELEASE_ASSERT(!sp.Is(GetStackPointer64()), "Not valid");
1481 pop(lr);
1482 }
1483
1484 // ===============================================================
1485 // ABI function calls.
1486
1487 void MacroAssembler::setupUnalignedABICall(Register scratch) {
1488 // Because wasm operates without the need for dynamic alignment of SP, it is
1489 // implied that this routine should never be called when generating wasm.
1490 MOZ_ASSERT(!IsCompilingWasm());
1491
1492 // The following won't work for SP -- needs slightly different logic.
1493 MOZ_RELEASE_ASSERT(GetStackPointer64().Is(PseudoStackPointer64));
1494
1495 setupNativeABICall();
1496 dynamicAlignment_ = true;
1497
1498 int64_t alignment = ~(int64_t(ABIStackAlignment) - 1);
1499 ARMRegister scratch64(scratch, 64);
1500 MOZ_ASSERT(!scratch64.Is(PseudoStackPointer64));
1501
1502 // Always save LR -- Baseline ICs assume that LR isn't modified.
1503 push(lr);
1504
1505 // Remember the stack address on entry. This is reloaded in callWithABIPost
1506 // below.
1507 Mov(scratch64, PseudoStackPointer64);
1508
1509 // Make alignment, including the effective push of the previous sp.
1510 Sub(PseudoStackPointer64, PseudoStackPointer64, Operand(8));
1511 And(PseudoStackPointer64, PseudoStackPointer64, Operand(alignment));
1512 syncStackPtr();
1513
1514 // Store previous sp to the top of the stack, aligned. This is also
1515 // reloaded in callWithABIPost.
1516 Str(scratch64, MemOperand(PseudoStackPointer64, 0));
1517 }
1518
1519 void MacroAssembler::callWithABIPre(uint32_t* stackAdjust, bool callFromWasm) {
1520 // wasm operates without the need for dynamic alignment of SP.
1521 MOZ_ASSERT(!(dynamicAlignment_ && callFromWasm));
1522
1523 MOZ_ASSERT(inCall_);
1524 uint32_t stackForCall = abiArgs_.stackBytesConsumedSoFar();
1525
1526 // ARM64 *really* wants SP to always be 16-aligned, so ensure this now.
1527 if (dynamicAlignment_) {
1528 stackForCall += ComputeByteAlignment(stackForCall, StackAlignment);
1529 } else {
1530 // This can happen when we attach out-of-line stubs for rare cases. For
1531 // example CodeGenerator::visitWasmTruncateToInt32 adds an out-of-line
1532 // chunk.
1533 uint32_t alignmentAtPrologue = callFromWasm ? sizeof(wasm::Frame) : 0;
1534 stackForCall += ComputeByteAlignment(
1535 stackForCall + framePushed() + alignmentAtPrologue, ABIStackAlignment);
1536 }
1537
1538 *stackAdjust = stackForCall;
1539 reserveStack(*stackAdjust);
1540 {
1541 enoughMemory_ &= moveResolver_.resolve();
1542 if (!enoughMemory_) {
1543 return;
1544 }
1545 MoveEmitter emitter(*this);
1546 emitter.emit(moveResolver_);
1547 emitter.finish();
1548 }
1549
1550 assertStackAlignment(ABIStackAlignment);
1551 }
1552
1553 void MacroAssembler::callWithABIPost(uint32_t stackAdjust, ABIType result,
1554 bool callFromWasm) {
1555 // wasm operates without the need for dynamic alignment of SP.
1556 MOZ_ASSERT(!(dynamicAlignment_ && callFromWasm));
1557
1558 // Call boundaries communicate stack via SP, so we must resync PSP now.
1559 initPseudoStackPtr();
1560
1561 freeStack(stackAdjust);
1562
1563 if (dynamicAlignment_) {
1564 // This then-clause makes more sense if you first read
1565 // setupUnalignedABICall above.
1566 //
1567 // Restore the stack pointer from entry. The stack pointer will have been
1568 // saved by setupUnalignedABICall. This is fragile in that it assumes
1569 // that uses of this routine (callWithABIPost) with `dynamicAlignment_ ==
1570 // true` are preceded by matching calls to setupUnalignedABICall. But
1571 // there's nothing that enforce that mechanically. If we really want to
1572 // enforce this, we could add a debug-only CallWithABIState enum to the
1573 // MacroAssembler and assert that setupUnalignedABICall updates it before
1574 // we get here, then reset it to its initial state.
1575 Ldr(GetStackPointer64(), MemOperand(GetStackPointer64(), 0));
1576 syncStackPtr();
1577
1578 // Restore LR. This restores LR to the value stored by
1579 // setupUnalignedABICall, which should have been called just before
1580 // callWithABIPre. This is, per the above comment, also fragile.
1581 pop(lr);
1582
1583 // SP may be < PSP now. That is expected from the behaviour of `pop`. It
1584 // is not clear why the following `syncStackPtr` is necessary, but it is:
1585 // without it, the following test segfaults:
1586 // tests/backup-point-bug1315634.js
1587 syncStackPtr();
1588 }
1589
1590 // If the ABI's return regs are where ION is expecting them, then
1591 // no other work needs to be done.
1592
1593 #ifdef DEBUG
1594 MOZ_ASSERT(inCall_);
1595 inCall_ = false;
1596 #endif
1597 }
1598
1599 void MacroAssembler::callWithABINoProfiler(Register fun, ABIType result) {
1600 vixl::UseScratchRegisterScope temps(this);
1601 const Register scratch = temps.AcquireX().asUnsized();
1602 movePtr(fun, scratch);
1603
1604 uint32_t stackAdjust;
1605 callWithABIPre(&stackAdjust);
1606 call(scratch);
1607 callWithABIPost(stackAdjust, result);
1608 }
1609
1610 void MacroAssembler::callWithABINoProfiler(const Address& fun, ABIType result) {
1611 vixl::UseScratchRegisterScope temps(this);
1612 const Register scratch = temps.AcquireX().asUnsized();
1613 loadPtr(fun, scratch);
1614
1615 uint32_t stackAdjust;
1616 callWithABIPre(&stackAdjust);
1617 call(scratch);
1618 callWithABIPost(stackAdjust, result);
1619 }
1620
1621 // ===============================================================
1622 // Jit Frames.
1623
1624 uint32_t MacroAssembler::pushFakeReturnAddress(Register scratch) {
1625 enterNoPool(3);
1626 Label fakeCallsite;
1627
1628 Adr(ARMRegister(scratch, 64), &fakeCallsite);
1629 Push(scratch);
1630 bind(&fakeCallsite);
1631 uint32_t pseudoReturnOffset = currentOffset();
1632
1633 leaveNoPool();
1634 return pseudoReturnOffset;
1635 }
1636
1637 bool MacroAssemblerCompat::buildOOLFakeExitFrame(void* fakeReturnAddr) {
1638 asMasm().PushFrameDescriptor(FrameType::IonJS);
1639 asMasm().Push(ImmPtr(fakeReturnAddr));
1640 asMasm().Push(FramePointer);
1641 return true;
1642 }
1643
1644 // ===============================================================
1645 // Move instructions
1646
1647 void MacroAssembler::moveValue(const ValueOperand& src,
1648 const ValueOperand& dest) {
1649 if (src == dest) {
1650 return;
1651 }
1652 movePtr(src.valueReg(), dest.valueReg());
1653 }
1654
1655 void MacroAssembler::moveValue(const Value& src, const ValueOperand& dest) {
1656 if (!src.isGCThing()) {
1657 movePtr(ImmWord(src.asRawBits()), dest.valueReg());
1658 return;
1659 }
1660
1661 BufferOffset load =
1662 movePatchablePtr(ImmPtr(src.bitsAsPunboxPointer()), dest.valueReg());
1663 writeDataRelocation(src, load);
1664 }
1665
1666 // ===============================================================
1667 // Branch functions
1668
1669 void MacroAssembler::loadStoreBuffer(Register ptr, Register buffer) {
1670 And(ARMRegister(buffer, 64), ARMRegister(ptr, 64),
1671 Operand(int32_t(~gc::ChunkMask)));
1672 loadPtr(Address(buffer, gc::ChunkStoreBufferOffset), buffer);
1673 }
1674
1675 void MacroAssembler::branchPtrInNurseryChunk(Condition cond, Register ptr,
1676 Register temp, Label* label) {
1677 MOZ_ASSERT(cond == Assembler::Equal || cond == Assembler::NotEqual);
1678 MOZ_ASSERT(ptr != temp);
1679 MOZ_ASSERT(ptr != ScratchReg &&
1680 ptr != ScratchReg2); // Both may be used internally.
1681 MOZ_ASSERT(temp != ScratchReg && temp != ScratchReg2);
1682
1683 And(ARMRegister(temp, 64), ARMRegister(ptr, 64),
1684 Operand(int32_t(~gc::ChunkMask)));
1685 branchPtr(InvertCondition(cond), Address(temp, gc::ChunkStoreBufferOffset),
1686 ImmWord(0), label);
1687 }
1688
1689 void MacroAssembler::branchValueIsNurseryCell(Condition cond,
1690 const Address& address,
1691 Register temp, Label* label) {
1692 branchValueIsNurseryCellImpl(cond, address, temp, label);
1693 }
1694
1695 void MacroAssembler::branchValueIsNurseryCell(Condition cond,
1696 ValueOperand value, Register temp,
1697 Label* label) {
1698 branchValueIsNurseryCellImpl(cond, value, temp, label);
1699 }
1700 template <typename T>
1701 void MacroAssembler::branchValueIsNurseryCellImpl(Condition cond,
1702 const T& value, Register temp,
1703 Label* label) {
1704 MOZ_ASSERT(cond == Assembler::Equal || cond == Assembler::NotEqual);
1705 MOZ_ASSERT(temp != ScratchReg &&
1706 temp != ScratchReg2); // Both may be used internally.
1707
1708 Label done;
1709 branchTestGCThing(Assembler::NotEqual, value,
1710 cond == Assembler::Equal ? &done : label);
1711
1712 getGCThingValueChunk(value, temp);
1713 branchPtr(InvertCondition(cond), Address(temp, gc::ChunkStoreBufferOffset),
1714 ImmWord(0), label);
1715
1716 bind(&done);
1717 }
1718
1719 void MacroAssembler::branchTestValue(Condition cond, const ValueOperand& lhs,
1720 const Value& rhs, Label* label) {
1721 MOZ_ASSERT(cond == Equal || cond == NotEqual);
1722 MOZ_ASSERT(!rhs.isNaN());
1723 vixl::UseScratchRegisterScope temps(this);
1724 const ARMRegister scratch64 = temps.AcquireX();
1725 MOZ_ASSERT(scratch64.asUnsized() != lhs.valueReg());
1726 moveValue(rhs, ValueOperand(scratch64.asUnsized()));
1727 Cmp(ARMRegister(lhs.valueReg(), 64), scratch64);
1728 B(label, cond);
1729 }
1730
1731 void MacroAssembler::branchTestNaNValue(Condition cond, const ValueOperand& val,
1732 Register temp, Label* label) {
1733 MOZ_ASSERT(cond == Equal || cond == NotEqual);
1734 vixl::UseScratchRegisterScope temps(this);
1735 const ARMRegister scratch64 = temps.AcquireX();
1736 MOZ_ASSERT(scratch64.asUnsized() != val.valueReg());
1737
1738 // When testing for NaN, we want to ignore the sign bit.
1739 And(ARMRegister(temp, 64), ARMRegister(val.valueReg(), 64),
1740 Operand(~mozilla::FloatingPoint<double>::kSignBit));
1741
1742 // Compare against a NaN with sign bit 0.
1743 static_assert(JS::detail::CanonicalizedNaNSignBit == 0);
1744 moveValue(DoubleValue(JS::GenericNaN()), ValueOperand(scratch64.asUnsized()));
1745 Cmp(ARMRegister(temp, 64), scratch64);
1746 B(label, cond);
1747 }
1748
1749 // ========================================================================
1750 // Memory access primitives.
1751 template <typename T>
1752 void MacroAssembler::storeUnboxedValue(const ConstantOrRegister& value,
1753 MIRType valueType, const T& dest) {
1754 MOZ_ASSERT(valueType < MIRType::Value);
1755
1756 if (valueType == MIRType::Double) {
1757 boxDouble(value.reg().typedReg().fpu(), dest);
1758 return;
1759 }
1760
1761 if (value.constant()) {
1762 storeValue(value.value(), dest);
1763 } else {
1764 storeValue(ValueTypeFromMIRType(valueType), value.reg().typedReg().gpr(),
1765 dest);
1766 }
1767 }
1768
1769 template void MacroAssembler::storeUnboxedValue(const ConstantOrRegister& value,
1770 MIRType valueType,
1771 const Address& dest);
1772 template void MacroAssembler::storeUnboxedValue(
1773 const ConstantOrRegister& value, MIRType valueType,
1774 const BaseObjectElementIndex& dest);
1775
1776 void MacroAssembler::comment(const char* msg) { Assembler::comment(msg); }
1777
1778 // ========================================================================
1779 // wasm support
1780
1781 FaultingCodeOffset MacroAssembler::wasmTrapInstruction() {
1782 AutoForbidPoolsAndNops afp(this,
1783 /* max number of instructions in scope = */ 1);
1784 FaultingCodeOffset fco = FaultingCodeOffset(currentOffset());
1785 Unreachable();
1786 return fco;
1787 }
1788
1789 void MacroAssembler::wasmBoundsCheck32(Condition cond, Register index,
1790 Register boundsCheckLimit, Label* ok) {
1791 branch32(cond, index, boundsCheckLimit, ok);
1792 if (JitOptions.spectreIndexMasking) {
1793 csel(ARMRegister(index, 32), vixl::wzr, ARMRegister(index, 32), cond);
1794 }
1795 }
1796
1797 void MacroAssembler::wasmBoundsCheck32(Condition cond, Register index,
1798 Address boundsCheckLimit, Label* ok) {
1799 branch32(cond, index, boundsCheckLimit, ok);
1800 if (JitOptions.spectreIndexMasking) {
1801 csel(ARMRegister(index, 32), vixl::wzr, ARMRegister(index, 32), cond);
1802 }
1803 }
1804
1805 void MacroAssembler::wasmBoundsCheck64(Condition cond, Register64 index,
1806 Register64 boundsCheckLimit, Label* ok) {
1807 branchPtr(cond, index.reg, boundsCheckLimit.reg, ok);
1808 if (JitOptions.spectreIndexMasking) {
1809 csel(ARMRegister(index.reg, 64), vixl::xzr, ARMRegister(index.reg, 64),
1810 cond);
1811 }
1812 }
1813
1814 void MacroAssembler::wasmBoundsCheck64(Condition cond, Register64 index,
1815 Address boundsCheckLimit, Label* ok) {
1816 branchPtr(InvertCondition(cond), boundsCheckLimit, index.reg, ok);
1817 if (JitOptions.spectreIndexMasking) {
1818 csel(ARMRegister(index.reg, 64), vixl::xzr, ARMRegister(index.reg, 64),
1819 cond);
1820 }
1821 }
1822
1823 // FCVTZU behaves as follows:
1824 //
1825 // on NaN it produces zero
1826 // on too large it produces UINT_MAX (for appropriate type)
1827 // on too small it produces zero
1828 //
1829 // FCVTZS behaves as follows:
1830 //
1831 // on NaN it produces zero
1832 // on too large it produces INT_MAX (for appropriate type)
1833 // on too small it produces INT_MIN (ditto)
1834
1835 void MacroAssembler::wasmTruncateDoubleToUInt32(FloatRegister input_,
1836 Register output_,
1837 bool isSaturating,
1838 Label* oolEntry) {
1839 ARMRegister output(output_, 32);
1840 ARMFPRegister input(input_, 64);
1841 Fcvtzu(output, input);
1842 if (!isSaturating) {
1843 Cmp(output, 0);
1844 Ccmp(output, -1, vixl::ZFlag, Assembler::NotEqual);
1845 B(oolEntry, Assembler::Equal);
1846 }
1847 }
1848
1849 void MacroAssembler::wasmTruncateFloat32ToUInt32(FloatRegister input_,
1850 Register output_,
1851 bool isSaturating,
1852 Label* oolEntry) {
1853 ARMRegister output(output_, 32);
1854 ARMFPRegister input(input_, 32);
1855 Fcvtzu(output, input);
1856 if (!isSaturating) {
1857 Cmp(output, 0);
1858 Ccmp(output, -1, vixl::ZFlag, Assembler::NotEqual);
1859 B(oolEntry, Assembler::Equal);
1860 }
1861 }
1862
1863 void MacroAssembler::wasmTruncateDoubleToInt32(FloatRegister input_,
1864 Register output_,
1865 bool isSaturating,
1866 Label* oolEntry) {
1867 ARMRegister output(output_, 32);
1868 ARMFPRegister input(input_, 64);
1869 Fcvtzs(output, input);
1870 if (!isSaturating) {
1871 Cmp(output, 0);
1872 Ccmp(output, INT32_MAX, vixl::ZFlag, Assembler::NotEqual);
1873 Ccmp(output, INT32_MIN, vixl::ZFlag, Assembler::NotEqual);
1874 B(oolEntry, Assembler::Equal);
1875 }
1876 }
1877
1878 void MacroAssembler::wasmTruncateFloat32ToInt32(FloatRegister input_,
1879 Register output_,
1880 bool isSaturating,
1881 Label* oolEntry) {
1882 ARMRegister output(output_, 32);
1883 ARMFPRegister input(input_, 32);
1884 Fcvtzs(output, input);
1885 if (!isSaturating) {
1886 Cmp(output, 0);
1887 Ccmp(output, INT32_MAX, vixl::ZFlag, Assembler::NotEqual);
1888 Ccmp(output, INT32_MIN, vixl::ZFlag, Assembler::NotEqual);
1889 B(oolEntry, Assembler::Equal);
1890 }
1891 }
1892
1893 void MacroAssembler::wasmTruncateDoubleToUInt64(
1894 FloatRegister input_, Register64 output_, bool isSaturating,
1895 Label* oolEntry, Label* oolRejoin, FloatRegister tempDouble) {
1896 MOZ_ASSERT(tempDouble.isInvalid());
1897
1898 ARMRegister output(output_.reg, 64);
1899 ARMFPRegister input(input_, 64);
1900 Fcvtzu(output, input);
1901 if (!isSaturating) {
1902 Cmp(output, 0);
1903 Ccmp(output, -1, vixl::ZFlag, Assembler::NotEqual);
1904 B(oolEntry, Assembler::Equal);
1905 bind(oolRejoin);
1906 }
1907 }
1908
1909 void MacroAssembler::wasmTruncateFloat32ToUInt64(
1910 FloatRegister input_, Register64 output_, bool isSaturating,
1911 Label* oolEntry, Label* oolRejoin, FloatRegister tempDouble) {
1912 MOZ_ASSERT(tempDouble.isInvalid());
1913
1914 ARMRegister output(output_.reg, 64);
1915 ARMFPRegister input(input_, 32);
1916 Fcvtzu(output, input);
1917 if (!isSaturating) {
1918 Cmp(output, 0);
1919 Ccmp(output, -1, vixl::ZFlag, Assembler::NotEqual);
1920 B(oolEntry, Assembler::Equal);
1921 bind(oolRejoin);
1922 }
1923 }
1924
1925 void MacroAssembler::wasmTruncateDoubleToInt64(
1926 FloatRegister input_, Register64 output_, bool isSaturating,
1927 Label* oolEntry, Label* oolRejoin, FloatRegister tempDouble) {
1928 MOZ_ASSERT(tempDouble.isInvalid());
1929
1930 ARMRegister output(output_.reg, 64);
1931 ARMFPRegister input(input_, 64);
1932 Fcvtzs(output, input);
1933 if (!isSaturating) {
1934 Cmp(output, 0);
1935 Ccmp(output, INT64_MAX, vixl::ZFlag, Assembler::NotEqual);
1936 Ccmp(output, INT64_MIN, vixl::ZFlag, Assembler::NotEqual);
1937 B(oolEntry, Assembler::Equal);
1938 bind(oolRejoin);
1939 }
1940 }
1941
1942 void MacroAssembler::wasmTruncateFloat32ToInt64(
1943 FloatRegister input_, Register64 output_, bool isSaturating,
1944 Label* oolEntry, Label* oolRejoin, FloatRegister tempDouble) {
1945 ARMRegister output(output_.reg, 64);
1946 ARMFPRegister input(input_, 32);
1947 Fcvtzs(output, input);
1948 if (!isSaturating) {
1949 Cmp(output, 0);
1950 Ccmp(output, INT64_MAX, vixl::ZFlag, Assembler::NotEqual);
1951 Ccmp(output, INT64_MIN, vixl::ZFlag, Assembler::NotEqual);
1952 B(oolEntry, Assembler::Equal);
1953 bind(oolRejoin);
1954 }
1955 }
1956
1957 void MacroAssembler::oolWasmTruncateCheckF32ToI32(
1958 FloatRegister input, Register output, TruncFlags flags,
1959 const wasm::TrapSiteDesc& trapSiteDesc, Label* rejoin) {
1960 Label notNaN;
1961 branchFloat(Assembler::DoubleOrdered, input, input, &notNaN);
1962 wasmTrap(wasm::Trap::InvalidConversionToInteger, trapSiteDesc);
1963 bind(&notNaN);
1964
1965 Label isOverflow;
1966 const float two_31 = -float(INT32_MIN);
1967 ScratchFloat32Scope fpscratch(*this);
1968 if (flags & TRUNC_UNSIGNED) {
1969 loadConstantFloat32(two_31 * 2, fpscratch);
1970 branchFloat(Assembler::DoubleGreaterThanOrEqual, input, fpscratch,
1971 &isOverflow);
1972 loadConstantFloat32(-1.0f, fpscratch);
1973 branchFloat(Assembler::DoubleGreaterThan, input, fpscratch, rejoin);
1974 } else {
1975 loadConstantFloat32(two_31, fpscratch);
1976 branchFloat(Assembler::DoubleGreaterThanOrEqual, input, fpscratch,
1977 &isOverflow);
1978 loadConstantFloat32(-two_31, fpscratch);
1979 branchFloat(Assembler::DoubleGreaterThanOrEqual, input, fpscratch, rejoin);
1980 }
1981 bind(&isOverflow);
1982 wasmTrap(wasm::Trap::IntegerOverflow, trapSiteDesc);
1983 }
1984
1985 void MacroAssembler::oolWasmTruncateCheckF64ToI32(
1986 FloatRegister input, Register output, TruncFlags flags,
1987 const wasm::TrapSiteDesc& trapSiteDesc, Label* rejoin) {
1988 Label notNaN;
1989 branchDouble(Assembler::DoubleOrdered, input, input, &notNaN);
1990 wasmTrap(wasm::Trap::InvalidConversionToInteger, trapSiteDesc);
1991 bind(&notNaN);
1992
1993 Label isOverflow;
1994 const double two_31 = -double(INT32_MIN);
1995 ScratchDoubleScope fpscratch(*this);
1996 if (flags & TRUNC_UNSIGNED) {
1997 loadConstantDouble(two_31 * 2, fpscratch);
1998 branchDouble(Assembler::DoubleGreaterThanOrEqual, input, fpscratch,
1999 &isOverflow);
2000 loadConstantDouble(-1.0, fpscratch);
2001 branchDouble(Assembler::DoubleGreaterThan, input, fpscratch, rejoin);
2002 } else {
2003 loadConstantDouble(two_31, fpscratch);
2004 branchDouble(Assembler::DoubleGreaterThanOrEqual, input, fpscratch,
2005 &isOverflow);
2006 loadConstantDouble(-two_31 - 1, fpscratch);
2007 branchDouble(Assembler::DoubleGreaterThan, input, fpscratch, rejoin);
2008 }
2009 bind(&isOverflow);
2010 wasmTrap(wasm::Trap::IntegerOverflow, trapSiteDesc);
2011 }
2012
2013 void MacroAssembler::oolWasmTruncateCheckF32ToI64(
2014 FloatRegister input, Register64 output, TruncFlags flags,
2015 const wasm::TrapSiteDesc& trapSiteDesc, Label* rejoin) {
2016 Label notNaN;
2017 branchFloat(Assembler::DoubleOrdered, input, input, &notNaN);
2018 wasmTrap(wasm::Trap::InvalidConversionToInteger, trapSiteDesc);
2019 bind(&notNaN);
2020
2021 Label isOverflow;
2022 const float two_63 = -float(INT64_MIN);
2023 ScratchFloat32Scope fpscratch(*this);
2024 if (flags & TRUNC_UNSIGNED) {
2025 loadConstantFloat32(two_63 * 2, fpscratch);
2026 branchFloat(Assembler::DoubleGreaterThanOrEqual, input, fpscratch,
2027 &isOverflow);
2028 loadConstantFloat32(-1.0f, fpscratch);
2029 branchFloat(Assembler::DoubleGreaterThan, input, fpscratch, rejoin);
2030 } else {
2031 loadConstantFloat32(two_63, fpscratch);
2032 branchFloat(Assembler::DoubleGreaterThanOrEqual, input, fpscratch,
2033 &isOverflow);
2034 loadConstantFloat32(-two_63, fpscratch);
2035 branchFloat(Assembler::DoubleGreaterThanOrEqual, input, fpscratch, rejoin);
2036 }
2037 bind(&isOverflow);
2038 wasmTrap(wasm::Trap::IntegerOverflow, trapSiteDesc);
2039 }
2040
2041 void MacroAssembler::oolWasmTruncateCheckF64ToI64(
2042 FloatRegister input, Register64 output, TruncFlags flags,
2043 const wasm::TrapSiteDesc& trapSiteDesc, Label* rejoin) {
2044 Label notNaN;
2045 branchDouble(Assembler::DoubleOrdered, input, input, &notNaN);
2046 wasmTrap(wasm::Trap::InvalidConversionToInteger, trapSiteDesc);
2047 bind(&notNaN);
2048
2049 Label isOverflow;
2050 const double two_63 = -double(INT64_MIN);
2051 ScratchDoubleScope fpscratch(*this);
2052 if (flags & TRUNC_UNSIGNED) {
2053 loadConstantDouble(two_63 * 2, fpscratch);
2054 branchDouble(Assembler::DoubleGreaterThanOrEqual, input, fpscratch,
2055 &isOverflow);
2056 loadConstantDouble(-1.0, fpscratch);
2057 branchDouble(Assembler::DoubleGreaterThan, input, fpscratch, rejoin);
2058 } else {
2059 loadConstantDouble(two_63, fpscratch);
2060 branchDouble(Assembler::DoubleGreaterThanOrEqual, input, fpscratch,
2061 &isOverflow);
2062 loadConstantDouble(-two_63, fpscratch);
2063 branchDouble(Assembler::DoubleGreaterThanOrEqual, input, fpscratch, rejoin);
2064 }
2065 bind(&isOverflow);
2066 wasmTrap(wasm::Trap::IntegerOverflow, trapSiteDesc);
2067 }
2068
2069 void MacroAssembler::wasmLoad(const wasm::MemoryAccessDesc& access,
2070 Register memoryBase, Register ptr,
2071 AnyRegister output) {
2072 wasmLoadImpl(access, memoryBase, ptr, output, Register64::Invalid());
2073 }
2074
2075 void MacroAssembler::wasmLoadI64(const wasm::MemoryAccessDesc& access,
2076 Register memoryBase, Register ptr,
2077 Register64 output) {
2078 wasmLoadImpl(access, memoryBase, ptr, AnyRegister(), output);
2079 }
2080
2081 void MacroAssembler::wasmStore(const wasm::MemoryAccessDesc& access,
2082 AnyRegister value, Register memoryBase,
2083 Register ptr) {
2084 wasmStoreImpl(access, value, Register64::Invalid(), memoryBase, ptr);
2085 }
2086
2087 void MacroAssembler::wasmStoreI64(const wasm::MemoryAccessDesc& access,
2088 Register64 value, Register memoryBase,
2089 Register ptr) {
2090 wasmStoreImpl(access, AnyRegister(), value, memoryBase, ptr);
2091 }
2092
2093 void MacroAssembler::enterFakeExitFrameForWasm(Register cxreg, Register scratch,
2094 ExitFrameType type) {
2095 // Wasm stubs use the native SP, not the PSP.
2096
2097 linkExitFrame(cxreg, scratch);
2098
2099 MOZ_RELEASE_ASSERT(sp.Is(GetStackPointer64()));
2100
2101 // SP has to be 16-byte aligned when we do a load/store, so push |type| twice
2102 // and then add 8 bytes to SP. This leaves SP unaligned.
2103 move32(Imm32(int32_t(type)), scratch);
2104 push(scratch, scratch);
2105 Add(sp, sp, 8);
2106
2107 // Despite the above assertion, it is possible for control to flow from here
2108 // to the code generated by
2109 // MacroAssemblerCompat::handleFailureWithHandlerTail without any
2110 // intervening assignment to PSP. But handleFailureWithHandlerTail assumes
2111 // that PSP is the active stack pointer. Hence the following is necessary
2112 // for safety. Note we can't use initPseudoStackPtr here as that would
2113 // generate no instructions.
2114 Mov(PseudoStackPointer64, sp);
2115 }
2116
2117 void MacroAssembler::widenInt32(Register r) {
2118 move32To64ZeroExtend(r, Register64(r));
2119 }
2120
2121 CodeOffset MacroAssembler::sub32FromMemAndBranchIfNegativeWithPatch(
2122 Address address, Label* label) {
2123 vixl::UseScratchRegisterScope temps(this);
2124 const ARMRegister value32 = temps.AcquireW();
2125 MOZ_ASSERT(value32.asUnsized() != address.base);
2126 Ldr(value32, toMemOperand(address));
2127 // -128 is arbitrary, but makes `*address` count upwards, which may help
2128 // to identify cases where the subsequent ::patch..() call was forgotten.
2129 Subs(value32, value32, Operand(-128));
2130 // Points immediately after the insn to patch
2131 CodeOffset patchPoint = CodeOffset(currentOffset());
2132 // This assumes that Str does not change the condition codes.
2133 Str(value32, toMemOperand(address));
2134 B(label, Assembler::Signed);
2135 return patchPoint;
2136 }
2137
2138 void MacroAssembler::patchSub32FromMemAndBranchIfNegative(CodeOffset offset,
2139 Imm32 imm) {
2140 int32_t val = imm.value;
2141 // Patching it to zero would make the insn pointless
2142 MOZ_RELEASE_ASSERT(val >= 1 && val <= 127);
2143 Instruction* instrPtr = getInstructionAt(BufferOffset(offset.offset() - 4));
2144 // 31 27 23 21 9 4
2145 // | | | | | |
2146 // 0011 0001 00 imm12 Rn Rd = ADDS Wd, Wn|WSP, #imm12 // (expected)
2147 // 0111 0001 00 imm12 Rn Rd = SUBS Wd, Wn|WSP, #imm12 // (replacement)
2148 vixl::Instr oldInstr = instrPtr->InstructionBits();
2149 // Check opcode bits and imm field are as expected
2150 MOZ_ASSERT((oldInstr & 0b1111'1111'11'000000000000'00000'00000U) ==
2151 0b0011'0001'00'000000000000'00000'00000U);
2152 MOZ_RELEASE_ASSERT((oldInstr & 0b0000'0000'00'111111111111'00000'00000U) ==
2153 (128 << 10)); // 128 as created above
2154 vixl::Instr newInstr =
2155 0b0111'0001'00'000000000000'00000'00000U | // opcode bits
2156 (oldInstr & 0b11111'11111) | // existing register fields
2157 ((val & 0b111111111111) << 10); // #val
2158 instrPtr->SetInstructionBits(newInstr);
2159 }
2160
2161 // ========================================================================
2162 // Convert floating point.
2163
2164 bool MacroAssembler::convertUInt64ToDoubleNeedsTemp() { return false; }
2165
2166 void MacroAssembler::convertUInt64ToDouble(Register64 src, FloatRegister dest,
2167 Register temp) {
2168 MOZ_ASSERT(temp == Register::Invalid());
2169 Ucvtf(ARMFPRegister(dest, 64), ARMRegister(src.reg, 64));
2170 }
2171
2172 void MacroAssembler::convertInt64ToDouble(Register64 src, FloatRegister dest) {
2173 Scvtf(ARMFPRegister(dest, 64), ARMRegister(src.reg, 64));
2174 }
2175
2176 void MacroAssembler::convertUInt64ToFloat32(Register64 src, FloatRegister dest,
2177 Register temp) {
2178 MOZ_ASSERT(temp == Register::Invalid());
2179 Ucvtf(ARMFPRegister(dest, 32), ARMRegister(src.reg, 64));
2180 }
2181
2182 void MacroAssembler::convertInt64ToFloat32(Register64 src, FloatRegister dest) {
2183 Scvtf(ARMFPRegister(dest, 32), ARMRegister(src.reg, 64));
2184 }
2185
2186 void MacroAssembler::convertIntPtrToDouble(Register src, FloatRegister dest) {
2187 convertInt64ToDouble(Register64(src), dest);
2188 }
2189
2190 // ========================================================================
2191 // Primitive atomic operations.
2192
2193 // The computed MemOperand must be Reg+0 because the load/store exclusive
2194 // instructions only take a single pointer register.
2195
2196 static MemOperand ComputePointerForAtomic(MacroAssembler& masm,
2197 const Address& address,
2198 Register scratch) {
2199 if (address.offset == 0) {
2200 return MemOperand(X(masm, address.base), 0);
2201 }
2202
2203 masm.Add(X(scratch), X(masm, address.base), address.offset);
2204 return MemOperand(X(scratch), 0);
2205 }
2206
2207 static MemOperand ComputePointerForAtomic(MacroAssembler& masm,
2208 const BaseIndex& address,
2209 Register scratch) {
2210 masm.Add(X(scratch), X(masm, address.base),
2211 Operand(X(address.index), vixl::LSL, address.scale));
2212 if (address.offset) {
2213 masm.Add(X(scratch), X(scratch), address.offset);
2214 }
2215 return MemOperand(X(scratch), 0);
2216 }
2217
2218 // This sign extends to targetWidth and leaves any higher bits zero.
2219
2220 static void SignOrZeroExtend(MacroAssembler& masm, Scalar::Type srcType,
2221 Width targetWidth, Register src, Register dest) {
2222 bool signExtend = Scalar::isSignedIntType(srcType);
2223
2224 switch (Scalar::byteSize(srcType)) {
2225 case 1:
2226 if (signExtend) {
2227 masm.Sbfm(R(dest, targetWidth), R(src, targetWidth), 0, 7);
2228 } else {
2229 masm.Ubfm(R(dest, targetWidth), R(src, targetWidth), 0, 7);
2230 }
2231 break;
2232 case 2:
2233 if (signExtend) {
2234 masm.Sbfm(R(dest, targetWidth), R(src, targetWidth), 0, 15);
2235 } else {
2236 masm.Ubfm(R(dest, targetWidth), R(src, targetWidth), 0, 15);
2237 }
2238 break;
2239 case 4:
2240 if (targetWidth == Width::_64) {
2241 if (signExtend) {
2242 masm.Sbfm(X(dest), X(src), 0, 31);
2243 } else {
2244 masm.Ubfm(X(dest), X(src), 0, 31);
2245 }
2246 } else if (src != dest) {
2247 masm.Mov(R(dest, targetWidth), R(src, targetWidth));
2248 }
2249 break;
2250 case 8:
2251 if (src != dest) {
2252 masm.Mov(R(dest, targetWidth), R(src, targetWidth));
2253 }
2254 break;
2255 default:
2256 MOZ_CRASH();
2257 }
2258 }
2259
2260 // Exclusive-loads zero-extend their values to the full width of the X register.
2261 //
2262 // Note, we've promised to leave the high bits of the 64-bit register clear if
2263 // the targetWidth is 32.
2264
2265 static void LoadExclusive(MacroAssembler& masm,
2266 const wasm::MemoryAccessDesc* access,
2267 Scalar::Type srcType, Width targetWidth,
2268 MemOperand ptr, Register dest) {
2269 bool signExtend = Scalar::isSignedIntType(srcType);
2270
2271 // With this address form, a single native ldxr* will be emitted, and the
2272 // AutoForbidPoolsAndNops ensures that the metadata is emitted at the
2273 // address of the ldxr*. Note that the use of AutoForbidPoolsAndNops is now
2274 // a "second class" solution; the right way to do this would be to have the
2275 // masm.<LoadInsn> calls produce an FaultingCodeOffset, and hand that value to
2276 // `masm.append`.
2277 MOZ_ASSERT(ptr.IsImmediateOffset() && ptr.offset() == 0);
2278
2279 switch (Scalar::byteSize(srcType)) {
2280 case 1: {
2281 {
2282 AutoForbidPoolsAndNops afp(
2283 &masm,
2284 /* max number of instructions in scope = */ 1);
2285 if (access) {
2286 masm.append(*access, wasm::TrapMachineInsn::Load8,
2287 FaultingCodeOffset(masm.currentOffset()));
2288 }
2289 masm.Ldxrb(W(dest), ptr);
2290 }
2291 if (signExtend) {
2292 masm.Sbfm(R(dest, targetWidth), R(dest, targetWidth), 0, 7);
2293 }
2294 break;
2295 }
2296 case 2: {
2297 {
2298 AutoForbidPoolsAndNops afp(
2299 &masm,
2300 /* max number of instructions in scope = */ 1);
2301 if (access) {
2302 masm.append(*access, wasm::TrapMachineInsn::Load16,
2303 FaultingCodeOffset(masm.currentOffset()));
2304 }
2305 masm.Ldxrh(W(dest), ptr);
2306 }
2307 if (signExtend) {
2308 masm.Sbfm(R(dest, targetWidth), R(dest, targetWidth), 0, 15);
2309 }
2310 break;
2311 }
2312 case 4: {
2313 {
2314 AutoForbidPoolsAndNops afp(
2315 &masm,
2316 /* max number of instructions in scope = */ 1);
2317 if (access) {
2318 masm.append(*access, wasm::TrapMachineInsn::Load32,
2319 FaultingCodeOffset(masm.currentOffset()));
2320 }
2321 masm.Ldxr(W(dest), ptr);
2322 }
2323 if (targetWidth == Width::_64 && signExtend) {
2324 masm.Sbfm(X(dest), X(dest), 0, 31);
2325 }
2326 break;
2327 }
2328 case 8: {
2329 {
2330 AutoForbidPoolsAndNops afp(
2331 &masm,
2332 /* max number of instructions in scope = */ 1);
2333 if (access) {
2334 masm.append(*access, wasm::TrapMachineInsn::Load64,
2335 FaultingCodeOffset(masm.currentOffset()));
2336 }
2337 masm.Ldxr(X(dest), ptr);
2338 }
2339 break;
2340 }
2341 default: {
2342 MOZ_CRASH();
2343 }
2344 }
2345 }
2346
2347 static void StoreExclusive(MacroAssembler& masm, Scalar::Type type,
2348 Register status, Register src, MemOperand ptr) {
2349 // Note, these are not decorated with a TrapSite only because they are
2350 // assumed to be preceded by a LoadExclusive to the same address, of the
2351 // same width, so that will always take the page fault if the address is bad.
2352 switch (Scalar::byteSize(type)) {
2353 case 1:
2354 masm.Stxrb(W(status), W(src), ptr);
2355 break;
2356 case 2:
2357 masm.Stxrh(W(status), W(src), ptr);
2358 break;
2359 case 4:
2360 masm.Stxr(W(status), W(src), ptr);
2361 break;
2362 case 8:
2363 masm.Stxr(W(status), X(src), ptr);
2364 break;
2365 }
2366 }
2367
2368 static bool HasAtomicInstructions(MacroAssembler& masm) {
2369 return masm.asVIXL().GetCPUFeatures()->Has(vixl::CPUFeatures::kAtomics);
2370 }
2371
2372 static inline bool SupportedAtomicInstructionOperands(Scalar::Type type,
2373 Width targetWidth) {
2374 if (targetWidth == Width::_32) {
2375 return byteSize(type) <= 4;
2376 }
2377 if (targetWidth == Width::_64) {
2378 return byteSize(type) == 8;
2379 }
2380 return false;
2381 }
2382
2383 template <typename T>
2384 static void CompareExchange(MacroAssembler& masm,
2385 const wasm::MemoryAccessDesc* access,
2386 Scalar::Type type, Width targetWidth,
2387 Synchronization sync, const T& mem, Register oldval,
2388 Register newval, Register output) {
2389 MOZ_ASSERT(oldval != output && newval != output);
2390
2391 vixl::UseScratchRegisterScope temps(&masm);
2392
2393 Register ptrScratch = temps.AcquireX().asUnsized();
2394 MemOperand ptr = ComputePointerForAtomic(masm, mem, ptrScratch);
2395
2396 MOZ_ASSERT(ptr.base().asUnsized() != output);
2397
2398 if (HasAtomicInstructions(masm) &&
2399 SupportedAtomicInstructionOperands(type, targetWidth)) {
2400 masm.Mov(X(output), X(oldval));
2401 // Capal is using same atomic mechanism as Ldxr/Stxr, and
2402 // consider it is the same for "Inner Shareable" domain.
2403 // Not updated gen_cmpxchg in GenerateAtomicOperations.py.
2404 masm.memoryBarrierBefore(sync);
2405 {
2406 AutoForbidPoolsAndNops afp(&masm, /* number of insns = */ 1);
2407 if (access) {
2408 masm.append(*access, wasm::TrapMachineInsn::Atomic,
2409 FaultingCodeOffset(masm.currentOffset()));
2410 }
2411 switch (byteSize(type)) {
2412 case 1:
2413 masm.Casalb(R(output, targetWidth), R(newval, targetWidth), ptr);
2414 break;
2415 case 2:
2416 masm.Casalh(R(output, targetWidth), R(newval, targetWidth), ptr);
2417 break;
2418 case 4:
2419 case 8:
2420 masm.Casal(R(output, targetWidth), R(newval, targetWidth), ptr);
2421 break;
2422 default:
2423 MOZ_CRASH("CompareExchange unsupported type");
2424 }
2425 }
2426 masm.memoryBarrierAfter(sync);
2427 SignOrZeroExtend(masm, type, targetWidth, output, output);
2428 return;
2429 }
2430
2431 // The target doesn't support atomics, so generate a LL-SC loop. This requires
2432 // only AArch64 v8.0.
2433 Label again;
2434 Label done;
2435
2436 // NOTE: the generated code must match the assembly code in gen_cmpxchg in
2437 // GenerateAtomicOperations.py
2438 masm.memoryBarrierBefore(sync);
2439
2440 Register scratch = temps.AcquireX().asUnsized();
2441
2442 masm.bind(&again);
2443 SignOrZeroExtend(masm, type, targetWidth, oldval, scratch);
2444 LoadExclusive(masm, access, type, targetWidth, ptr, output);
2445 masm.Cmp(R(output, targetWidth), R(scratch, targetWidth));
2446 masm.B(&done, MacroAssembler::NotEqual);
2447 StoreExclusive(masm, type, scratch, newval, ptr);
2448 masm.Cbnz(W(scratch), &again);
2449 masm.bind(&done);
2450
2451 masm.memoryBarrierAfter(sync);
2452 }
2453
2454 template <typename T>
2455 static void AtomicExchange(MacroAssembler& masm,
2456 const wasm::MemoryAccessDesc* access,
2457 Scalar::Type type, Width targetWidth,
2458 Synchronization sync, const T& mem, Register value,
2459 Register output) {
2460 MOZ_ASSERT(value != output);
2461
2462 vixl::UseScratchRegisterScope temps(&masm);
2463
2464 Register ptrScratch = temps.AcquireX().asUnsized();
2465 MemOperand ptr = ComputePointerForAtomic(masm, mem, ptrScratch);
2466
2467 if (HasAtomicInstructions(masm) &&
2468 SupportedAtomicInstructionOperands(type, targetWidth)) {
2469 // Swpal is using same atomic mechanism as Ldxr/Stxr, and
2470 // consider it is the same for "Inner Shareable" domain.
2471 // Not updated gen_exchange in GenerateAtomicOperations.py.
2472 masm.memoryBarrierBefore(sync);
2473 {
2474 AutoForbidPoolsAndNops afp(&masm, /* number of insns = */ 1);
2475 if (access) {
2476 masm.append(*access, wasm::TrapMachineInsn::Atomic,
2477 FaultingCodeOffset(masm.currentOffset()));
2478 }
2479 switch (byteSize(type)) {
2480 case 1:
2481 masm.Swpalb(R(value, targetWidth), R(output, targetWidth), ptr);
2482 break;
2483 case 2:
2484 masm.Swpalh(R(value, targetWidth), R(output, targetWidth), ptr);
2485 break;
2486 case 4:
2487 case 8:
2488 masm.Swpal(R(value, targetWidth), R(output, targetWidth), ptr);
2489 break;
2490 default:
2491 MOZ_CRASH("AtomicExchange unsupported type");
2492 }
2493 }
2494 masm.memoryBarrierAfter(sync);
2495 SignOrZeroExtend(masm, type, targetWidth, output, output);
2496 return;
2497 }
2498
2499 // The target doesn't support atomics, so generate a LL-SC loop. This requires
2500 // only AArch64 v8.0.
2501 Label again;
2502
2503 // NOTE: the generated code must match the assembly code in gen_exchange in
2504 // GenerateAtomicOperations.py
2505 masm.memoryBarrierBefore(sync);
2506
2507 Register scratch = temps.AcquireX().asUnsized();
2508
2509 masm.bind(&again);
2510 LoadExclusive(masm, access, type, targetWidth, ptr, output);
2511 StoreExclusive(masm, type, scratch, value, ptr);
2512 masm.Cbnz(W(scratch), &again);
2513
2514 masm.memoryBarrierAfter(sync);
2515 }
2516
2517 template <bool wantResult, typename T>
2518 static void AtomicFetchOp(MacroAssembler& masm,
2519 const wasm::MemoryAccessDesc* access,
2520 Scalar::Type type, Width targetWidth,
2521 Synchronization sync, AtomicOp op, const T& mem,
2522 Register value, Register temp, Register output) {
2523 MOZ_ASSERT(value != output);
2524 MOZ_ASSERT(value != temp);
2525 MOZ_ASSERT_IF(wantResult, output != temp);
2526
2527 vixl::UseScratchRegisterScope temps(&masm);
2528
2529 Register ptrScratch = temps.AcquireX().asUnsized();
2530 MemOperand ptr = ComputePointerForAtomic(masm, mem, ptrScratch);
2531
2532 if (HasAtomicInstructions(masm) &&
2533 SupportedAtomicInstructionOperands(type, targetWidth) &&
2534 !isFloatingType(type)) {
2535 // LdXXXal/StXXXl is using same atomic mechanism as Ldxr/Stxr, and
2536 // consider it is the same for "Inner Shareable" domain.
2537 // Not updated gen_fetchop in GenerateAtomicOperations.py.
2538 masm.memoryBarrierBefore(sync);
2539
2540 #define FETCH_OP_CASE(op, arg) \
2541 { \
2542 AutoForbidPoolsAndNops afp(&masm, /* num insns = */ 1); \
2543 if (access) { \
2544 masm.append(*access, wasm::TrapMachineInsn::Atomic, \
2545 FaultingCodeOffset(masm.currentOffset())); \
2546 } \
2547 switch (byteSize(type)) { \
2548 case 1: \
2549 if (wantResult) { \
2550 masm.Ld##op##alb(R(arg, targetWidth), R(output, targetWidth), ptr); \
2551 } else { \
2552 masm.St##op##lb(R(arg, targetWidth), ptr); \
2553 } \
2554 break; \
2555 case 2: \
2556 if (wantResult) { \
2557 masm.Ld##op##alh(R(arg, targetWidth), R(output, targetWidth), ptr); \
2558 } else { \
2559 masm.St##op##lh(R(arg, targetWidth), ptr); \
2560 } \
2561 break; \
2562 case 4: \
2563 case 8: \
2564 if (wantResult) { \
2565 masm.Ld##op##al(R(arg, targetWidth), R(output, targetWidth), ptr); \
2566 } else { \
2567 masm.St##op##l(R(arg, targetWidth), ptr); \
2568 } \
2569 break; \
2570 default: \
2571 MOZ_CRASH("AtomicFetchOp unsupported type"); \
2572 } \
2573 }
2574
2575 switch (op) {
2576 case AtomicOp::Add:
2577 FETCH_OP_CASE(add, value);
2578 break;
2579 case AtomicOp::Sub: {
2580 Register scratch = temps.AcquireX().asUnsized();
2581 masm.Neg(X(scratch), X(value));
2582 FETCH_OP_CASE(add, scratch);
2583 break;
2584 }
2585 case AtomicOp::And: {
2586 Register scratch = temps.AcquireX().asUnsized();
2587 masm.Eor(X(scratch), X(value), Operand(~0));
2588 FETCH_OP_CASE(clr, scratch);
2589 break;
2590 }
2591 case AtomicOp::Or:
2592 FETCH_OP_CASE(set, value);
2593 break;
2594 case AtomicOp::Xor:
2595 FETCH_OP_CASE(eor, value);
2596 break;
2597 }
2598 masm.memoryBarrierAfter(sync);
2599 if (wantResult) {
2600 SignOrZeroExtend(masm, type, targetWidth, output, output);
2601 }
2602 return;
2603 }
2604
2605 #undef FETCH_OP_CASE
2606
2607 // The target doesn't support atomics, so generate a LL-SC loop. This requires
2608 // only AArch64 v8.0.
2609 Label again;
2610
2611 // NOTE: the generated code must match the assembly code in gen_fetchop in
2612 // GenerateAtomicOperations.py
2613 masm.memoryBarrierBefore(sync);
2614
2615 Register scratch = temps.AcquireX().asUnsized();
2616
2617 masm.bind(&again);
2618 LoadExclusive(masm, access, type, targetWidth, ptr, output);
2619 switch (op) {
2620 case AtomicOp::Add:
2621 masm.Add(X(temp), X(output), X(value));
2622 break;
2623 case AtomicOp::Sub:
2624 masm.Sub(X(temp), X(output), X(value));
2625 break;
2626 case AtomicOp::And:
2627 masm.And(X(temp), X(output), X(value));
2628 break;
2629 case AtomicOp::Or:
2630 masm.Orr(X(temp), X(output), X(value));
2631 break;
2632 case AtomicOp::Xor:
2633 masm.Eor(X(temp), X(output), X(value));
2634 break;
2635 }
2636 StoreExclusive(masm, type, scratch, temp, ptr);
2637 masm.Cbnz(W(scratch), &again);
2638 if (wantResult) {
2639 SignOrZeroExtend(masm, type, targetWidth, output, output);
2640 }
2641
2642 masm.memoryBarrierAfter(sync);
2643 }
2644
2645 void MacroAssembler::compareExchange(Scalar::Type type, Synchronization sync,
2646 const Address& mem, Register oldval,
2647 Register newval, Register output) {
2648 CompareExchange(*this, nullptr, type, Width::_32, sync, mem, oldval, newval,
2649 output);
2650 }
2651
2652 void MacroAssembler::compareExchange(Scalar::Type type, Synchronization sync,
2653 const BaseIndex& mem, Register oldval,
2654 Register newval, Register output) {
2655 CompareExchange(*this, nullptr, type, Width::_32, sync, mem, oldval, newval,
2656 output);
2657 }
2658
2659 void MacroAssembler::compareExchange64(Synchronization sync, const Address& mem,
2660 Register64 expect, Register64 replace,
2661 Register64 output) {
2662 CompareExchange(*this, nullptr, Scalar::Int64, Width::_64, sync, mem,
2663 expect.reg, replace.reg, output.reg);
2664 }
2665
2666 void MacroAssembler::compareExchange64(Synchronization sync,
2667 const BaseIndex& mem, Register64 expect,
2668 Register64 replace, Register64 output) {
2669 CompareExchange(*this, nullptr, Scalar::Int64, Width::_64, sync, mem,
2670 expect.reg, replace.reg, output.reg);
2671 }
2672
2673 void MacroAssembler::atomicExchange64(Synchronization sync, const Address& mem,
2674 Register64 value, Register64 output) {
2675 AtomicExchange(*this, nullptr, Scalar::Int64, Width::_64, sync, mem,
2676 value.reg, output.reg);
2677 }
2678
2679 void MacroAssembler::atomicExchange64(Synchronization sync,
2680 const BaseIndex& mem, Register64 value,
2681 Register64 output) {
2682 AtomicExchange(*this, nullptr, Scalar::Int64, Width::_64, sync, mem,
2683 value.reg, output.reg);
2684 }
2685
2686 void MacroAssembler::atomicFetchOp64(Synchronization sync, AtomicOp op,
2687 Register64 value, const Address& mem,
2688 Register64 temp, Register64 output) {
2689 AtomicFetchOp<true>(*this, nullptr, Scalar::Int64, Width::_64, sync, op, mem,
2690 value.reg, temp.reg, output.reg);
2691 }
2692
2693 void MacroAssembler::atomicFetchOp64(Synchronization sync, AtomicOp op,
2694 Register64 value, const BaseIndex& mem,
2695 Register64 temp, Register64 output) {
2696 AtomicFetchOp<true>(*this, nullptr, Scalar::Int64, Width::_64, sync, op, mem,
2697 value.reg, temp.reg, output.reg);
2698 }
2699
2700 void MacroAssembler::atomicEffectOp64(Synchronization sync, AtomicOp op,
2701 Register64 value, const Address& mem,
2702 Register64 temp) {
2703 AtomicFetchOp<false>(*this, nullptr, Scalar::Int64, Width::_64, sync, op, mem,
2704 value.reg, temp.reg, temp.reg);
2705 }
2706
2707 void MacroAssembler::atomicEffectOp64(Synchronization sync, AtomicOp op,
2708 Register64 value, const BaseIndex& mem,
2709 Register64 temp) {
2710 AtomicFetchOp<false>(*this, nullptr, Scalar::Int64, Width::_64, sync, op, mem,
2711 value.reg, temp.reg, temp.reg);
2712 }
2713
2714 void MacroAssembler::wasmCompareExchange(const wasm::MemoryAccessDesc& access,
2715 const Address& mem, Register oldval,
2716 Register newval, Register output) {
2717 CompareExchange(*this, &access, access.type(), Width::_32, access.sync(), mem,
2718 oldval, newval, output);
2719 }
2720
2721 void MacroAssembler::wasmCompareExchange(const wasm::MemoryAccessDesc& access,
2722 const BaseIndex& mem, Register oldval,
2723 Register newval, Register output) {
2724 CompareExchange(*this, &access, access.type(), Width::_32, access.sync(), mem,
2725 oldval, newval, output);
2726 }
2727
2728 void MacroAssembler::atomicExchange(Scalar::Type type, Synchronization sync,
2729 const Address& mem, Register value,
2730 Register output) {
2731 AtomicExchange(*this, nullptr, type, Width::_32, sync, mem, value, output);
2732 }
2733
2734 void MacroAssembler::atomicExchange(Scalar::Type type, Synchronization sync,
2735 const BaseIndex& mem, Register value,
2736 Register output) {
2737 AtomicExchange(*this, nullptr, type, Width::_32, sync, mem, value, output);
2738 }
2739
2740 void MacroAssembler::wasmAtomicExchange(const wasm::MemoryAccessDesc& access,
2741 const Address& mem, Register value,
2742 Register output) {
2743 AtomicExchange(*this, &access, access.type(), Width::_32, access.sync(), mem,
2744 value, output);
2745 }
2746
2747 void MacroAssembler::wasmAtomicExchange(const wasm::MemoryAccessDesc& access,
2748 const BaseIndex& mem, Register value,
2749 Register output) {
2750 AtomicExchange(*this, &access, access.type(), Width::_32, access.sync(), mem,
2751 value, output);
2752 }
2753
2754 void MacroAssembler::atomicFetchOp(Scalar::Type type, Synchronization sync,
2755 AtomicOp op, Register value,
2756 const Address& mem, Register temp,
2757 Register output) {
2758 AtomicFetchOp<true>(*this, nullptr, type, Width::_32, sync, op, mem, value,
2759 temp, output);
2760 }
2761
2762 void MacroAssembler::atomicFetchOp(Scalar::Type type, Synchronization sync,
2763 AtomicOp op, Register value,
2764 const BaseIndex& mem, Register temp,
2765 Register output) {
2766 AtomicFetchOp<true>(*this, nullptr, type, Width::_32, sync, op, mem, value,
2767 temp, output);
2768 }
2769
2770 void MacroAssembler::wasmAtomicFetchOp(const wasm::MemoryAccessDesc& access,
2771 AtomicOp op, Register value,
2772 const Address& mem, Register temp,
2773 Register output) {
2774 AtomicFetchOp<true>(*this, &access, access.type(), Width::_32, access.sync(),
2775 op, mem, value, temp, output);
2776 }
2777
2778 void MacroAssembler::wasmAtomicFetchOp(const wasm::MemoryAccessDesc& access,
2779 AtomicOp op, Register value,
2780 const BaseIndex& mem, Register temp,
2781 Register output) {
2782 AtomicFetchOp<true>(*this, &access, access.type(), Width::_32, access.sync(),
2783 op, mem, value, temp, output);
2784 }
2785
2786 void MacroAssembler::wasmAtomicEffectOp(const wasm::MemoryAccessDesc& access,
2787 AtomicOp op, Register value,
2788 const Address& mem, Register temp) {
2789 AtomicFetchOp<false>(*this, &access, access.type(), Width::_32, access.sync(),
2790 op, mem, value, temp, temp);
2791 }
2792
2793 void MacroAssembler::wasmAtomicEffectOp(const wasm::MemoryAccessDesc& access,
2794 AtomicOp op, Register value,
2795 const BaseIndex& mem, Register temp) {
2796 AtomicFetchOp<false>(*this, &access, access.type(), Width::_32, access.sync(),
2797 op, mem, value, temp, temp);
2798 }
2799
2800 void MacroAssembler::wasmCompareExchange64(const wasm::MemoryAccessDesc& access,
2801 const Address& mem,
2802 Register64 expect,
2803 Register64 replace,
2804 Register64 output) {
2805 CompareExchange(*this, &access, Scalar::Int64, Width::_64, access.sync(), mem,
2806 expect.reg, replace.reg, output.reg);
2807 }
2808
2809 void MacroAssembler::wasmCompareExchange64(const wasm::MemoryAccessDesc& access,
2810 const BaseIndex& mem,
2811 Register64 expect,
2812 Register64 replace,
2813 Register64 output) {
2814 CompareExchange(*this, &access, Scalar::Int64, Width::_64, access.sync(), mem,
2815 expect.reg, replace.reg, output.reg);
2816 }
2817
2818 void MacroAssembler::wasmAtomicExchange64(const wasm::MemoryAccessDesc& access,
2819 const Address& mem, Register64 value,
2820 Register64 output) {
2821 AtomicExchange(*this, &access, Scalar::Int64, Width::_64, access.sync(), mem,
2822 value.reg, output.reg);
2823 }
2824
2825 void MacroAssembler::wasmAtomicExchange64(const wasm::MemoryAccessDesc& access,
2826 const BaseIndex& mem,
2827 Register64 value, Register64 output) {
2828 AtomicExchange(*this, &access, Scalar::Int64, Width::_64, access.sync(), mem,
2829 value.reg, output.reg);
2830 }
2831
2832 void MacroAssembler::wasmAtomicFetchOp64(const wasm::MemoryAccessDesc& access,
2833 AtomicOp op, Register64 value,
2834 const Address& mem, Register64 temp,
2835 Register64 output) {
2836 AtomicFetchOp<true>(*this, &access, Scalar::Int64, Width::_64, access.sync(),
2837 op, mem, value.reg, temp.reg, output.reg);
2838 }
2839
2840 void MacroAssembler::wasmAtomicFetchOp64(const wasm::MemoryAccessDesc& access,
2841 AtomicOp op, Register64 value,
2842 const BaseIndex& mem, Register64 temp,
2843 Register64 output) {
2844 AtomicFetchOp<true>(*this, &access, Scalar::Int64, Width::_64, access.sync(),
2845 op, mem, value.reg, temp.reg, output.reg);
2846 }
2847
2848 void MacroAssembler::wasmAtomicEffectOp64(const wasm::MemoryAccessDesc& access,
2849 AtomicOp op, Register64 value,
2850 const BaseIndex& mem,
2851 Register64 temp) {
2852 AtomicFetchOp<false>(*this, &access, Scalar::Int64, Width::_64, access.sync(),
2853 op, mem, value.reg, temp.reg, temp.reg);
2854 }
2855
2856 // ========================================================================
2857 // JS atomic operations.
2858
2859 template <typename T>
2860 static void CompareExchangeJS(MacroAssembler& masm, Scalar::Type arrayType,
2861 Synchronization sync, const T& mem,
2862 Register oldval, Register newval, Register temp,
2863 AnyRegister output) {
2864 if (arrayType == Scalar::Uint32) {
2865 masm.compareExchange(arrayType, sync, mem, oldval, newval, temp);
2866 masm.convertUInt32ToDouble(temp, output.fpu());
2867 } else {
2868 masm.compareExchange(arrayType, sync, mem, oldval, newval, output.gpr());
2869 }
2870 }
2871
2872 void MacroAssembler::compareExchangeJS(Scalar::Type arrayType,
2873 Synchronization sync, const Address& mem,
2874 Register oldval, Register newval,
2875 Register temp, AnyRegister output) {
2876 CompareExchangeJS(*this, arrayType, sync, mem, oldval, newval, temp, output);
2877 }
2878
2879 void MacroAssembler::compareExchangeJS(Scalar::Type arrayType,
2880 Synchronization sync,
2881 const BaseIndex& mem, Register oldval,
2882 Register newval, Register temp,
2883 AnyRegister output) {
2884 CompareExchangeJS(*this, arrayType, sync, mem, oldval, newval, temp, output);
2885 }
2886
2887 template <typename T>
2888 static void AtomicExchangeJS(MacroAssembler& masm, Scalar::Type arrayType,
2889 Synchronization sync, const T& mem, Register value,
2890 Register temp, AnyRegister output) {
2891 if (arrayType == Scalar::Uint32) {
2892 masm.atomicExchange(arrayType, sync, mem, value, temp);
2893 masm.convertUInt32ToDouble(temp, output.fpu());
2894 } else {
2895 masm.atomicExchange(arrayType, sync, mem, value, output.gpr());
2896 }
2897 }
2898
2899 void MacroAssembler::atomicExchangeJS(Scalar::Type arrayType,
2900 Synchronization sync, const Address& mem,
2901 Register value, Register temp,
2902 AnyRegister output) {
2903 AtomicExchangeJS(*this, arrayType, sync, mem, value, temp, output);
2904 }
2905
2906 void MacroAssembler::atomicExchangeJS(Scalar::Type arrayType,
2907 Synchronization sync,
2908 const BaseIndex& mem, Register value,
2909 Register temp, AnyRegister output) {
2910 AtomicExchangeJS(*this, arrayType, sync, mem, value, temp, output);
2911 }
2912
2913 template <typename T>
2914 static void AtomicFetchOpJS(MacroAssembler& masm, Scalar::Type arrayType,
2915 Synchronization sync, AtomicOp op, Register value,
2916 const T& mem, Register temp1, Register temp2,
2917 AnyRegister output) {
2918 if (arrayType == Scalar::Uint32) {
2919 masm.atomicFetchOp(arrayType, sync, op, value, mem, temp2, temp1);
2920 masm.convertUInt32ToDouble(temp1, output.fpu());
2921 } else {
2922 masm.atomicFetchOp(arrayType, sync, op, value, mem, temp1, output.gpr());
2923 }
2924 }
2925
2926 void MacroAssembler::atomicFetchOpJS(Scalar::Type arrayType,
2927 Synchronization sync, AtomicOp op,
2928 Register value, const Address& mem,
2929 Register temp1, Register temp2,
2930 AnyRegister output) {
2931 AtomicFetchOpJS(*this, arrayType, sync, op, value, mem, temp1, temp2, output);
2932 }
2933
2934 void MacroAssembler::atomicFetchOpJS(Scalar::Type arrayType,
2935 Synchronization sync, AtomicOp op,
2936 Register value, const BaseIndex& mem,
2937 Register temp1, Register temp2,
2938 AnyRegister output) {
2939 AtomicFetchOpJS(*this, arrayType, sync, op, value, mem, temp1, temp2, output);
2940 }
2941
2942 void MacroAssembler::atomicEffectOpJS(Scalar::Type arrayType,
2943 Synchronization sync, AtomicOp op,
2944 Register value, const BaseIndex& mem,
2945 Register temp) {
2946 AtomicFetchOp<false>(*this, nullptr, arrayType, Width::_32, sync, op, mem,
2947 value, temp, temp);
2948 }
2949
2950 void MacroAssembler::atomicEffectOpJS(Scalar::Type arrayType,
2951 Synchronization sync, AtomicOp op,
2952 Register value, const Address& mem,
2953 Register temp) {
2954 AtomicFetchOp<false>(*this, nullptr, arrayType, Width::_32, sync, op, mem,
2955 value, temp, temp);
2956 }
2957
2958 void MacroAssembler::atomicPause() { Isb(); }
2959
2960 void MacroAssembler::flexibleQuotient32(Register rhs, Register srcDest,
2961 bool isUnsigned,
2962 const LiveRegisterSet&) {
2963 quotient32(rhs, srcDest, isUnsigned);
2964 }
2965
2966 void MacroAssembler::flexibleQuotientPtr(
2967 Register rhs, Register srcDest, bool isUnsigned,
2968 const LiveRegisterSet& volatileLiveRegs) {
2969 quotient64(rhs, srcDest, isUnsigned);
2970 }
2971
2972 void MacroAssembler::flexibleRemainder32(Register rhs, Register srcDest,
2973 bool isUnsigned,
2974 const LiveRegisterSet&) {
2975 remainder32(rhs, srcDest, isUnsigned);
2976 }
2977
2978 void MacroAssembler::flexibleRemainderPtr(
2979 Register rhs, Register srcDest, bool isUnsigned,
2980 const LiveRegisterSet& volatileLiveRegs) {
2981 remainder64(rhs, srcDest, isUnsigned);
2982 }
2983
2984 void MacroAssembler::flexibleDivMod32(Register rhs, Register srcDest,
2985 Register remOutput, bool isUnsigned,
2986 const LiveRegisterSet&) {
2987 vixl::UseScratchRegisterScope temps(this);
2988 ARMRegister src = temps.AcquireW();
2989
2990 // Preserve src for remainder computation
2991 Mov(src, ARMRegister(srcDest, 32));
2992
2993 if (isUnsigned) {
2994 Udiv(ARMRegister(srcDest, 32), src, ARMRegister(rhs, 32));
2995 } else {
2996 Sdiv(ARMRegister(srcDest, 32), src, ARMRegister(rhs, 32));
2997 }
2998
2999 // Compute the remainder: remOutput = src - (srcDest * rhs).
3000 Msub(/* result= */ ARMRegister(remOutput, 32), ARMRegister(srcDest, 32),
3001 ARMRegister(rhs, 32), src);
3002 }
3003
3004 CodeOffset MacroAssembler::moveNearAddressWithPatch(Register dest) {
3005 AutoForbidPoolsAndNops afp(this,
3006 /* max number of instructions in scope = */ 1);
3007 CodeOffset offset(currentOffset());
3008 adr(ARMRegister(dest, 64), 0, LabelDoc());
3009 return offset;
3010 }
3011
3012 void MacroAssembler::patchNearAddressMove(CodeLocationLabel loc,
3013 CodeLocationLabel target) {
3014 ptrdiff_t off = target - loc;
3015 MOZ_RELEASE_ASSERT(vixl::IsInt21(off));
3016
3017 Instruction* cur = reinterpret_cast<Instruction*>(loc.raw());
3018 MOZ_ASSERT(cur->IsADR());
3019
3020 vixl::Register rd = vixl::Register::XRegFromCode(cur->Rd());
3021 adr(cur, rd, off);
3022 }
3023
3024 // ========================================================================
3025 // Spectre Mitigations.
3026
3027 void MacroAssembler::speculationBarrier() {
3028 // Conditional speculation barrier.
3029 csdb();
3030 }
3031
3032 void MacroAssembler::floorFloat32ToInt32(FloatRegister src, Register dest,
3033 Label* fail) {
3034 ARMFPRegister iFlt(src, 32);
3035 ARMRegister o64(dest, 64);
3036 ARMRegister o32(dest, 32);
3037
3038 Label handleZero;
3039 Label fin;
3040
3041 // Handle ±0 and NaN first.
3042 Fcmp(iFlt, 0.0);
3043 B(Assembler::Equal, &handleZero);
3044 // NaN is always a bail condition, just bail directly.
3045 B(Assembler::Overflow, fail);
3046
3047 // Round towards negative infinity.
3048 Fcvtms(o64, iFlt);
3049
3050 // Sign extend lower 32 bits to test if the result isn't an Int32.
3051 Cmp(o64, Operand(o64, vixl::SXTW));
3052 B(NotEqual, fail);
3053
3054 // Clear upper 32 bits.
3055 Uxtw(o64, o64);
3056 B(&fin);
3057
3058 bind(&handleZero);
3059 // Move the top word of the float into the output reg, if it is non-zero,
3060 // then the original value was -0.0.
3061 Fmov(o32, iFlt);
3062 Cbnz(o32, fail);
3063 bind(&fin);
3064 }
3065
3066 void MacroAssembler::floorDoubleToInt32(FloatRegister src, Register dest,
3067 Label* fail) {
3068 ARMFPRegister iDbl(src, 64);
3069 ARMRegister o64(dest, 64);
3070 ARMRegister o32(dest, 32);
3071
3072 Label handleZero;
3073 Label fin;
3074
3075 // Handle ±0 and NaN first.
3076 Fcmp(iDbl, 0.0);
3077 B(Assembler::Equal, &handleZero);
3078 // NaN is always a bail condition, just bail directly.
3079 B(Assembler::Overflow, fail);
3080
3081 // Round towards negative infinity.
3082 Fcvtms(o64, iDbl);
3083
3084 // Sign extend lower 32 bits to test if the result isn't an Int32.
3085 Cmp(o64, Operand(o64, vixl::SXTW));
3086 B(NotEqual, fail);
3087
3088 // Clear upper 32 bits.
3089 Uxtw(o64, o64);
3090 B(&fin);
3091
3092 bind(&handleZero);
3093 // Move the top word of the double into the output reg, if it is non-zero,
3094 // then the original value was -0.0.
3095 Fmov(o64, iDbl);
3096 Cbnz(o64, fail);
3097 bind(&fin);
3098 }
3099
3100 void MacroAssembler::ceilFloat32ToInt32(FloatRegister src, Register dest,
3101 Label* fail) {
3102 ARMFPRegister iFlt(src, 32);
3103 ARMRegister o64(dest, 64);
3104 ARMRegister o32(dest, 32);
3105
3106 Label handleZero;
3107 Label fin;
3108
3109 // Round towards positive infinity.
3110 Fcvtps(o64, iFlt);
3111
3112 // Sign extend lower 32 bits to test if the result isn't an Int32.
3113 Cmp(o64, Operand(o64, vixl::SXTW));
3114 B(NotEqual, fail);
3115
3116 // We have to check for (-1, -0] and NaN when the result is zero.
3117 Cbz(o64, &handleZero);
3118
3119 // Clear upper 32 bits.
3120 Uxtw(o64, o64);
3121 B(&fin);
3122
3123 // Bail if the input is in (-1, -0] or NaN.
3124 bind(&handleZero);
3125 // Move the top word of the float into the output reg, if it is non-zero,
3126 // then the original value wasn't +0.0.
3127 Fmov(o32, iFlt);
3128 Cbnz(o32, fail);
3129 bind(&fin);
3130 }
3131
3132 void MacroAssembler::ceilDoubleToInt32(FloatRegister src, Register dest,
3133 Label* fail) {
3134 ARMFPRegister iDbl(src, 64);
3135 ARMRegister o64(dest, 64);
3136 ARMRegister o32(dest, 32);
3137
3138 Label handleZero;
3139 Label fin;
3140
3141 // Round towards positive infinity.
3142 Fcvtps(o64, iDbl);
3143
3144 // Sign extend lower 32 bits to test if the result isn't an Int32.
3145 Cmp(o64, Operand(o64, vixl::SXTW));
3146 B(NotEqual, fail);
3147
3148 // We have to check for (-1, -0] and NaN when the result is zero.
3149 Cbz(o64, &handleZero);
3150
3151 // Clear upper 32 bits.
3152 Uxtw(o64, o64);
3153 B(&fin);
3154
3155 // Bail if the input is in (-1, -0] or NaN.
3156 bind(&handleZero);
3157 // Move the top word of the double into the output reg, if it is non-zero,
3158 // then the original value wasn't +0.0.
3159 Fmov(o64, iDbl);
3160 Cbnz(o64, fail);
3161 bind(&fin);
3162 }
3163
3164 void MacroAssembler::truncFloat32ToInt32(FloatRegister src, Register dest,
3165 Label* fail) {
3166 ARMFPRegister src32(src, 32);
3167 ARMRegister dest32(dest, 32);
3168 ARMRegister dest64(dest, 64);
3169
3170 Label done, zeroCase;
3171
3172 // Convert scalar to signed 64-bit fixed-point, rounding toward zero.
3173 // In the case of overflow, the output is saturated.
3174 // In the case of NaN and -0, the output is zero.
3175 Fcvtzs(dest64, src32);
3176
3177 // If the output was zero, worry about special cases.
3178 Cbz(dest64, &zeroCase);
3179
3180 // Sign extend lower 32 bits to test if the result isn't an Int32.
3181 Cmp(dest64, Operand(dest64, vixl::SXTW));
3182 B(NotEqual, fail);
3183
3184 // Clear upper 32 bits.
3185 Uxtw(dest64, dest64);
3186
3187 // If the output was non-zero and wasn't saturated, just return it.
3188 B(&done);
3189
3190 // Handle the case of a zero output:
3191 // 1. The input may have been NaN, requiring a failure.
3192 // 2. The input may have been in (-1,-0], requiring a failure.
3193 {
3194 bind(&zeroCase);
3195
3196 // Combine test for negative and NaN values using a single bitwise
3197 // operation.
3198 //
3199 // | Decimal number | Bitwise representation |
3200 // |----------------|------------------------|
3201 // | -0 | 8000'0000 |
3202 // | +0 | 0000'0000 |
3203 // | +1 | 3f80'0000 |
3204 // | NaN (or +Inf) | 7fyx'xxxx, y >= 8 |
3205 // | -NaN (or -Inf) | ffyx'xxxx, y >= 8 |
3206 //
3207 // If any of two most significant bits is set, the number isn't in [0, 1).
3208 // (Recall that floating point numbers, except for NaN, are strictly ordered
3209 // when comparing their bitwise representation as signed integers.)
3210
3211 Fmov(dest32, src32);
3212 Lsr(dest32, dest32, 30);
3213 Cbnz(dest32, fail);
3214 }
3215
3216 bind(&done);
3217 }
3218
3219 void MacroAssembler::truncDoubleToInt32(FloatRegister src, Register dest,
3220 Label* fail) {
3221 ARMFPRegister src64(src, 64);
3222 ARMRegister dest64(dest, 64);
3223 ARMRegister dest32(dest, 32);
3224
3225 Label done, zeroCase;
3226
3227 // Convert scalar to signed 64-bit fixed-point, rounding toward zero.
3228 // In the case of overflow, the output is saturated.
3229 // In the case of NaN and -0, the output is zero.
3230 Fcvtzs(dest64, src64);
3231
3232 // If the output was zero, worry about special cases.
3233 Cbz(dest64, &zeroCase);
3234
3235 // Sign extend lower 32 bits to test if the result isn't an Int32.
3236 Cmp(dest64, Operand(dest64, vixl::SXTW));
3237 B(NotEqual, fail);
3238
3239 // Clear upper 32 bits.
3240 Uxtw(dest64, dest64);
3241
3242 // If the output was non-zero and wasn't saturated, just return it.
3243 B(&done);
3244
3245 // Handle the case of a zero output:
3246 // 1. The input may have been NaN, requiring a failure.
3247 // 2. The input may have been in (-1,-0], requiring a failure.
3248 {
3249 bind(&zeroCase);
3250
3251 // Combine test for negative and NaN values using a single bitwise
3252 // operation.
3253 //
3254 // | Decimal number | Bitwise representation |
3255 // |----------------|------------------------|
3256 // | -0 | 8000'0000'0000'0000 |
3257 // | +0 | 0000'0000'0000'0000 |
3258 // | +1 | 3ff0'0000'0000'0000 |
3259 // | NaN (or +Inf) | 7ffx'xxxx'xxxx'xxxx |
3260 // | -NaN (or -Inf) | fffx'xxxx'xxxx'xxxx |
3261 //
3262 // If any of two most significant bits is set, the number isn't in [0, 1).
3263 // (Recall that floating point numbers, except for NaN, are strictly ordered
3264 // when comparing their bitwise representation as signed integers.)
3265
3266 Fmov(dest64, src64);
3267 Lsr(dest64, dest64, 62);
3268 Cbnz(dest64, fail);
3269 }
3270
3271 bind(&done);
3272 }
3273
3274 void MacroAssembler::roundFloat32ToInt32(FloatRegister src, Register dest,
3275 FloatRegister temp, Label* fail) {
3276 ARMFPRegister src32(src, 32);
3277 ARMRegister dest32(dest, 32);
3278 ARMRegister dest64(dest, 64);
3279
3280 Label negative, saturated, done;
3281
3282 // Branch to a slow path if input < 0.0 due to complicated rounding rules.
3283 // Note that Fcmp with NaN unsets the negative flag.
3284 Fcmp(src32, 0.0);
3285 B(&negative, Assembler::Condition::lo);
3286
3287 // Handle the simple case of a positive input, and also -0 and NaN.
3288 // Rounding proceeds with consideration of the fractional part of the input:
3289 // 1. If > 0.5, round to integer with higher absolute value (so, up).
3290 // 2. If < 0.5, round to integer with lower absolute value (so, down).
3291 // 3. If = 0.5, round to +Infinity (so, up).
3292 {
3293 // Convert to signed 64-bit integer, rounding halfway cases away from zero.
3294 // In the case of overflow, the output is saturated.
3295 // In the case of NaN and -0, the output is zero.
3296 Fcvtas(dest64, src32);
3297
3298 // In the case of zero, the input may have been NaN or -0, which must bail.
3299 Cbnz(dest64, &saturated);
3300
3301 // Combine test for -0 and NaN values using a single bitwise operation.
3302 // See truncFloat32ToInt32 for an explanation.
3303 Fmov(dest32, src32);
3304 Lsr(dest32, dest32, 30);
3305 Cbnz(dest32, fail);
3306
3307 B(&done);
3308 }
3309
3310 // Handle the complicated case of a negative input.
3311 // Rounding proceeds with consideration of the fractional part of the input:
3312 // 1. If > 0.5, round to integer with higher absolute value (so, down).
3313 // 2. If < 0.5, round to integer with lower absolute value (so, up).
3314 // 3. If = 0.5, round to +Infinity (so, up).
3315 bind(&negative);
3316 {
3317 // Inputs in [-0.5, 0) are rounded to -0. Fail.
3318 loadConstantFloat32(-0.5f, temp);
3319 branchFloat(Assembler::DoubleGreaterThanOrEqual, src, temp, fail);
3320
3321 // Other negative inputs need the biggest double less than 0.5 added.
3322 loadConstantFloat32(GetBiggestNumberLessThan(0.5f), temp);
3323 addFloat32(src, temp);
3324
3325 // Round all values toward -Infinity.
3326 // In the case of overflow, the output is saturated.
3327 // NaN and -0 are already handled by the "positive number" path above.
3328 Fcvtms(dest64, temp);
3329 }
3330
3331 bind(&saturated);
3332
3333 // Sign extend lower 32 bits to test if the result isn't an Int32.
3334 Cmp(dest64, Operand(dest64, vixl::SXTW));
3335 B(NotEqual, fail);
3336
3337 // Clear upper 32 bits.
3338 Uxtw(dest64, dest64);
3339
3340 bind(&done);
3341 }
3342
3343 void MacroAssembler::roundDoubleToInt32(FloatRegister src, Register dest,
3344 FloatRegister temp, Label* fail) {
3345 ARMFPRegister src64(src, 64);
3346 ARMRegister dest64(dest, 64);
3347 ARMRegister dest32(dest, 32);
3348
3349 Label negative, saturated, done;
3350
3351 // Branch to a slow path if input < 0.0 due to complicated rounding rules.
3352 // Note that Fcmp with NaN unsets the negative flag.
3353 Fcmp(src64, 0.0);
3354 B(&negative, Assembler::Condition::lo);
3355
3356 // Handle the simple case of a positive input, and also -0 and NaN.
3357 // Rounding proceeds with consideration of the fractional part of the input:
3358 // 1. If > 0.5, round to integer with higher absolute value (so, up).
3359 // 2. If < 0.5, round to integer with lower absolute value (so, down).
3360 // 3. If = 0.5, round to +Infinity (so, up).
3361 {
3362 // Convert to signed 64-bit integer, rounding halfway cases away from zero.
3363 // In the case of overflow, the output is saturated.
3364 // In the case of NaN and -0, the output is zero.
3365 Fcvtas(dest64, src64);
3366
3367 // In the case of zero, the input may have been NaN or -0, which must bail.
3368 Cbnz(dest64, &saturated);
3369
3370 // Combine test for -0 and NaN values using a single bitwise operation.
3371 // See truncDoubleToInt32 for an explanation.
3372 Fmov(dest64, src64);
3373 Lsr(dest64, dest64, 62);
3374 Cbnz(dest64, fail);
3375
3376 B(&done);
3377 }
3378
3379 // Handle the complicated case of a negative input.
3380 // Rounding proceeds with consideration of the fractional part of the input:
3381 // 1. If > 0.5, round to integer with higher absolute value (so, down).
3382 // 2. If < 0.5, round to integer with lower absolute value (so, up).
3383 // 3. If = 0.5, round to +Infinity (so, up).
3384 bind(&negative);
3385 {
3386 // Inputs in [-0.5, 0) are rounded to -0. Fail.
3387 loadConstantDouble(-0.5, temp);
3388 branchDouble(Assembler::DoubleGreaterThanOrEqual, src, temp, fail);
3389
3390 // Other negative inputs need the biggest double less than 0.5 added.
3391 loadConstantDouble(GetBiggestNumberLessThan(0.5), temp);
3392 addDouble(src, temp);
3393
3394 // Round all values toward -Infinity.
3395 // In the case of overflow, the output is saturated.
3396 // NaN and -0 are already handled by the "positive number" path above.
3397 Fcvtms(dest64, temp);
3398 }
3399
3400 bind(&saturated);
3401
3402 // Sign extend lower 32 bits to test if the result isn't an Int32.
3403 Cmp(dest64, Operand(dest64, vixl::SXTW));
3404 B(NotEqual, fail);
3405
3406 // Clear upper 32 bits.
3407 Uxtw(dest64, dest64);
3408
3409 bind(&done);
3410 }
3411
3412 void MacroAssembler::nearbyIntDouble(RoundingMode mode, FloatRegister src,
3413 FloatRegister dest) {
3414 switch (mode) {
3415 case RoundingMode::Up:
3416 frintp(ARMFPRegister(dest, 64), ARMFPRegister(src, 64));
3417 return;
3418 case RoundingMode::Down:
3419 frintm(ARMFPRegister(dest, 64), ARMFPRegister(src, 64));
3420 return;
3421 case RoundingMode::NearestTiesToEven:
3422 frintn(ARMFPRegister(dest, 64), ARMFPRegister(src, 64));
3423 return;
3424 case RoundingMode::TowardsZero:
3425 frintz(ARMFPRegister(dest, 64), ARMFPRegister(src, 64));
3426 return;
3427 }
3428 MOZ_CRASH("unexpected mode");
3429 }
3430
3431 void MacroAssembler::nearbyIntFloat32(RoundingMode mode, FloatRegister src,
3432 FloatRegister dest) {
3433 switch (mode) {
3434 case RoundingMode::Up:
3435 frintp(ARMFPRegister(dest, 32), ARMFPRegister(src, 32));
3436 return;
3437 case RoundingMode::Down:
3438 frintm(ARMFPRegister(dest, 32), ARMFPRegister(src, 32));
3439 return;
3440 case RoundingMode::NearestTiesToEven:
3441 frintn(ARMFPRegister(dest, 32), ARMFPRegister(src, 32));
3442 return;
3443 case RoundingMode::TowardsZero:
3444 frintz(ARMFPRegister(dest, 32), ARMFPRegister(src, 32));
3445 return;
3446 }
3447 MOZ_CRASH("unexpected mode");
3448 }
3449
3450 void MacroAssembler::copySignDouble(FloatRegister lhs, FloatRegister rhs,
3451 FloatRegister output) {
3452 ScratchDoubleScope scratch(*this);
3453
3454 // Double with only the sign bit set
3455 loadConstantDouble(-0.0, scratch);
3456
3457 if (lhs != output) {
3458 moveDouble(lhs, output);
3459 }
3460
3461 bit(ARMFPRegister(output.encoding(), vixl::VectorFormat::kFormat8B),
3462 ARMFPRegister(rhs.encoding(), vixl::VectorFormat::kFormat8B),
3463 ARMFPRegister(scratch.encoding(), vixl::VectorFormat::kFormat8B));
3464 }
3465
3466 void MacroAssembler::copySignFloat32(FloatRegister lhs, FloatRegister rhs,
3467 FloatRegister output) {
3468 ScratchFloat32Scope scratch(*this);
3469
3470 // Float with only the sign bit set
3471 loadConstantFloat32(-0.0f, scratch);
3472
3473 if (lhs != output) {
3474 moveFloat32(lhs, output);
3475 }
3476
3477 bit(ARMFPRegister(output.encoding(), vixl::VectorFormat::kFormat8B),
3478 ARMFPRegister(rhs.encoding(), vixl::VectorFormat::kFormat8B),
3479 ARMFPRegister(scratch.encoding(), vixl::VectorFormat::kFormat8B));
3480 }
3481
3482 void MacroAssembler::shiftIndex32AndAdd(Register indexTemp32, int shift,
3483 Register pointer) {
3484 Add(ARMRegister(pointer, 64), ARMRegister(pointer, 64),
3485 Operand(ARMRegister(indexTemp32, 64), vixl::LSL, shift));
3486 }
3487
3488 void MacroAssembler::wasmMarkCallAsSlow() { Mov(x28, x28); }
3489
3490 const int32_t SlowCallMarker = 0xaa1c03fc;
3491
3492 void MacroAssembler::wasmCheckSlowCallsite(Register ra, Label* notSlow,
3493 Register temp1, Register temp2) {
3494 MOZ_ASSERT(ra != temp2);
3495 Ldr(W(temp2), MemOperand(X(ra), 0));
3496 Cmp(W(temp2), Operand(SlowCallMarker));
3497 B(Assembler::NotEqual, notSlow);
3498 }
3499
3500 CodeOffset MacroAssembler::wasmMarkedSlowCall(const wasm::CallSiteDesc& desc,
3501 const Register reg) {
3502 AutoForbidPoolsAndNops afp(this, !GetStackPointer64().Is(vixl::sp) ? 3 : 2);
3503 CodeOffset offset = call(desc, reg);
3504 wasmMarkCallAsSlow();
3505 return offset;
3506 }
3507
3508 //}}} check_macroassembler_style
3509
3510 } // namespace jit
3511 } // namespace js