Debian Codegraph @

js/src/jit/arm/CodeGenerator-arm.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/arm/CodeGenerator-arm.h"
8
9 #include "mozilla/DebugOnly.h"
10 #include "mozilla/MathAlgorithms.h"
11 #include "mozilla/Maybe.h"
12
13 #include <iterator>
14
15 #include "jsnum.h"
16
17 #include "jit/CodeGenerator.h"
18 #include "jit/InlineScriptTree.h"
19 #include "jit/JitRuntime.h"
20 #include "jit/MIR-wasm.h"
21 #include "jit/MIR.h"
22 #include "jit/MIRGraph.h"
23 #include "js/Conversions.h"
24 #include "js/ScalarType.h" // js::Scalar::Type
25 #include "vm/JSContext.h"
26 #include "vm/Realm.h"
27 #include "vm/Shape.h"
28
29 #include "jit/MacroAssembler-inl.h"
30 #include "jit/shared/CodeGenerator-shared-inl.h"
31 #include "vm/JSScript-inl.h"
32
33 using namespace js;
34 using namespace js::jit;
35
36 using JS::GenericNaN;
37 using JS::ToInt32;
38 using mozilla::DebugOnly;
39 using mozilla::FloorLog2;
40 using mozilla::NegativeInfinity;
41
42 // shared
43 CodeGeneratorARM::CodeGeneratorARM(MIRGenerator* gen, LIRGraph* graph,
44 MacroAssembler* masm,
45 const wasm::CodeMetadata* wasmCodeMeta)
46 : CodeGeneratorShared(gen, graph, masm, wasmCodeMeta) {}
47
48 Register64 CodeGeneratorARM::ToOperandOrRegister64(
49 const LInt64Allocation& input) {
50 return ToRegister64(input);
51 }
52
53 void CodeGeneratorARM::emitBranch(Assembler::Condition cond,
54 MBasicBlock* mirTrue, MBasicBlock* mirFalse) {
55 if (isNextBlock(mirFalse->lir())) {
56 jumpToBlock(mirTrue, cond);
57 } else {
58 jumpToBlock(mirFalse, Assembler::InvertCondition(cond));
59 jumpToBlock(mirTrue);
60 }
61 }
62
63 bool CodeGeneratorARM::generateOutOfLineCode() {
64 if (!CodeGeneratorShared::generateOutOfLineCode()) {
65 return false;
66 }
67
68 if (deoptLabel_.used()) {
69 // All non-table-based bailouts will go here.
70 masm.bind(&deoptLabel_);
71
72 // Push the frame size, so the handler can recover the IonScript.
73 masm.push(Imm32(frameSize()));
74
75 TrampolinePtr handler = gen->jitRuntime()->getGenericBailoutHandler();
76 masm.jump(handler);
77 }
78
79 return !masm.oom();
80 }
81
82 void CodeGeneratorARM::emitBailoutOOL(LSnapshot* snapshot) {
83 masm.push(Imm32(snapshot->snapshotOffset()));
84 masm.ma_b(&deoptLabel_);
85 }
86
87 void CodeGeneratorARM::bailoutIf(Assembler::Condition condition,
88 LSnapshot* snapshot) {
89 encode(snapshot);
90
91 InlineScriptTree* tree = snapshot->mir()->block()->trackedTree();
92 auto* ool = new (alloc()) LambdaOutOfLineCode(
93 [=](OutOfLineCode& ool) { emitBailoutOOL(snapshot); });
94
95 // All bailout code is associated with the bytecodeSite of the block we are
96 // bailing out from.
97 addOutOfLineCode(ool,
98 new (alloc()) BytecodeSite(tree, tree->script()->code()));
99
100 masm.ma_b(ool->entry(), condition);
101 }
102
103 void CodeGeneratorARM::bailoutFrom(Label* label, LSnapshot* snapshot) {
104 MOZ_ASSERT_IF(!masm.oom(), label->used());
105 MOZ_ASSERT_IF(!masm.oom(), !label->bound());
106
107 encode(snapshot);
108
109 InlineScriptTree* tree = snapshot->mir()->block()->trackedTree();
110 auto* ool = new (alloc()) LambdaOutOfLineCode(
111 [=](OutOfLineCode& ool) { emitBailoutOOL(snapshot); });
112
113 // All bailout code is associated with the bytecodeSite of the block we are
114 // bailing out from.
115 addOutOfLineCode(ool,
116 new (alloc()) BytecodeSite(tree, tree->script()->code()));
117
118 masm.retarget(label, ool->entry());
119 }
120
121 void CodeGeneratorARM::bailout(LSnapshot* snapshot) {
122 Label label;
123 masm.ma_b(&label);
124 bailoutFrom(&label, snapshot);
125 }
126
127 void CodeGenerator::visitMinMaxD(LMinMaxD* ins) {
128 FloatRegister first = ToFloatRegister(ins->first());
129 FloatRegister second = ToFloatRegister(ins->second());
130
131 MOZ_ASSERT(first == ToFloatRegister(ins->output()));
132
133 if (ins->mir()->isMax()) {
134 masm.maxDouble(second, first, true);
135 } else {
136 masm.minDouble(second, first, true);
137 }
138 }
139
140 void CodeGenerator::visitMinMaxF(LMinMaxF* ins) {
141 FloatRegister first = ToFloatRegister(ins->first());
142 FloatRegister second = ToFloatRegister(ins->second());
143
144 MOZ_ASSERT(first == ToFloatRegister(ins->output()));
145
146 if (ins->mir()->isMax()) {
147 masm.maxFloat32(second, first, true);
148 } else {
149 masm.minFloat32(second, first, true);
150 }
151 }
152
153 void CodeGenerator::visitAddI(LAddI* ins) {
154 const LAllocation* lhs = ins->lhs();
155 const LAllocation* rhs = ins->rhs();
156 const LDefinition* dest = ins->output();
157
158 ScratchRegisterScope scratch(masm);
159
160 if (rhs->isConstant()) {
161 masm.ma_add(ToRegister(lhs), Imm32(ToInt32(rhs)), ToRegister(dest), scratch,
162 SetCC);
163 } else if (rhs->isGeneralReg()) {
164 masm.ma_add(ToRegister(lhs), ToRegister(rhs), ToRegister(dest), SetCC);
165 } else {
166 masm.ma_add(ToRegister(lhs), Operand(ToAddress(rhs)), ToRegister(dest),
167 SetCC);
168 }
169
170 if (ins->snapshot()) {
171 bailoutIf(Assembler::Overflow, ins->snapshot());
172 }
173 }
174
175 void CodeGenerator::visitAddI64(LAddI64* lir) {
176 LInt64Allocation lhs = lir->lhs();
177 LInt64Allocation rhs = lir->rhs();
178
179 MOZ_ASSERT(ToOutRegister64(lir) == ToRegister64(lhs));
180
181 if (IsConstant(rhs)) {
182 masm.add64(Imm64(ToInt64(rhs)), ToRegister64(lhs));
183 return;
184 }
185
186 masm.add64(ToOperandOrRegister64(rhs), ToRegister64(lhs));
187 }
188
189 void CodeGenerator::visitSubI(LSubI* ins) {
190 const LAllocation* lhs = ins->lhs();
191 const LAllocation* rhs = ins->rhs();
192 const LDefinition* dest = ins->output();
193
194 ScratchRegisterScope scratch(masm);
195
196 if (rhs->isConstant()) {
197 masm.ma_sub(ToRegister(lhs), Imm32(ToInt32(rhs)), ToRegister(dest), scratch,
198 SetCC);
199 } else if (rhs->isGeneralReg()) {
200 masm.ma_sub(ToRegister(lhs), ToRegister(rhs), ToRegister(dest), SetCC);
201 } else {
202 masm.ma_sub(ToRegister(lhs), Operand(ToAddress(rhs)), ToRegister(dest),
203 SetCC);
204 }
205
206 if (ins->snapshot()) {
207 bailoutIf(Assembler::Overflow, ins->snapshot());
208 }
209 }
210
211 void CodeGenerator::visitSubI64(LSubI64* lir) {
212 LInt64Allocation lhs = lir->lhs();
213 LInt64Allocation rhs = lir->rhs();
214
215 MOZ_ASSERT(ToOutRegister64(lir) == ToRegister64(lhs));
216
217 if (IsConstant(rhs)) {
218 masm.sub64(Imm64(ToInt64(rhs)), ToRegister64(lhs));
219 return;
220 }
221
222 masm.sub64(ToOperandOrRegister64(rhs), ToRegister64(lhs));
223 }
224
225 void CodeGenerator::visitMulI(LMulI* ins) {
226 const LAllocation* lhs = ins->lhs();
227 const LAllocation* rhs = ins->rhs();
228 const LDefinition* dest = ins->output();
229 MMul* mul = ins->mir();
230 MOZ_ASSERT_IF(mul->mode() == MMul::Integer,
231 !mul->canBeNegativeZero() && !mul->canOverflow());
232
233 if (rhs->isConstant()) {
234 // Bailout when this condition is met.
235 Assembler::Condition c = Assembler::Overflow;
236 // Bailout on -0.0
237 int32_t constant = ToInt32(rhs);
238 if (mul->canBeNegativeZero() && constant <= 0) {
239 Assembler::Condition bailoutCond =
240 (constant == 0) ? Assembler::LessThan : Assembler::Equal;
241 masm.as_cmp(ToRegister(lhs), Imm8(0));
242 bailoutIf(bailoutCond, ins->snapshot());
243 }
244 // TODO: move these to ma_mul.
245 switch (constant) {
246 case -1:
247 masm.as_rsb(ToRegister(dest), ToRegister(lhs), Imm8(0), SetCC);
248 break;
249 case 0:
250 masm.ma_mov(Imm32(0), ToRegister(dest));
251 return; // Escape overflow check;
252 case 1:
253 // Nop
254 masm.ma_mov(ToRegister(lhs), ToRegister(dest));
255 return; // Escape overflow check;
256 case 2:
257 masm.ma_add(ToRegister(lhs), ToRegister(lhs), ToRegister(dest), SetCC);
258 // Overflow is handled later.
259 break;
260 default: {
261 bool handled = false;
262 if (constant > 0) {
263 // Try shift and add sequences for a positive constant.
264 if (!mul->canOverflow()) {
265 // If it cannot overflow, we can do lots of optimizations.
266 Register src = ToRegister(lhs);
267 uint32_t shift = FloorLog2(constant);
268 uint32_t rest = constant - (1 << shift);
269 // See if the constant has one bit set, meaning it can be
270 // encoded as a bitshift.
271 if ((1 << shift) == constant) {
272 masm.ma_lsl(Imm32(shift), src, ToRegister(dest));
273 handled = true;
274 } else {
275 // If the constant cannot be encoded as (1 << C1), see
276 // if it can be encoded as (1 << C1) | (1 << C2), which
277 // can be computed using an add and a shift.
278 uint32_t shift_rest = FloorLog2(rest);
279 if ((1u << shift_rest) == rest) {
280 masm.as_add(ToRegister(dest), src,
281 lsl(src, shift - shift_rest));
282 if (shift_rest != 0) {
283 masm.ma_lsl(Imm32(shift_rest), ToRegister(dest),
284 ToRegister(dest));
285 }
286 handled = true;
287 }
288 }
289 } else if (ToRegister(lhs) != ToRegister(dest)) {
290 // To stay on the safe side, only optimize things that are a
291 // power of 2.
292
293 uint32_t shift = FloorLog2(constant);
294 if ((1 << shift) == constant) {
295 // dest = lhs * pow(2,shift)
296 masm.ma_lsl(Imm32(shift), ToRegister(lhs), ToRegister(dest));
297 // At runtime, check (lhs == dest >> shift), if this
298 // does not hold, some bits were lost due to overflow,
299 // and the computation should be resumed as a double.
300 masm.as_cmp(ToRegister(lhs), asr(ToRegister(dest), shift));
301 c = Assembler::NotEqual;
302 handled = true;
303 }
304 }
305 }
306
307 if (!handled) {
308 ScratchRegisterScope scratch(masm);
309 if (mul->canOverflow()) {
310 c = masm.ma_check_mul(ToRegister(lhs), Imm32(ToInt32(rhs)),
311 ToRegister(dest), scratch, c);
312 } else {
313 masm.ma_mul(ToRegister(lhs), Imm32(ToInt32(rhs)), ToRegister(dest),
314 scratch);
315 }
316 }
317 }
318 }
319 // Bailout on overflow.
320 if (mul->canOverflow()) {
321 bailoutIf(c, ins->snapshot());
322 }
323 } else {
324 Assembler::Condition c = Assembler::Overflow;
325
326 if (mul->canOverflow()) {
327 ScratchRegisterScope scratch(masm);
328 c = masm.ma_check_mul(ToRegister(lhs), ToRegister(rhs), ToRegister(dest),
329 scratch, c);
330 } else {
331 masm.ma_mul(ToRegister(lhs), ToRegister(rhs), ToRegister(dest));
332 }
333
334 // Bailout on overflow.
335 if (mul->canOverflow()) {
336 bailoutIf(c, ins->snapshot());
337 }
338
339 if (mul->canBeNegativeZero()) {
340 Label done;
341 masm.as_cmp(ToRegister(dest), Imm8(0));
342 masm.ma_b(&done, Assembler::NotEqual);
343
344 // Result is -0 if lhs or rhs is negative.
345 masm.ma_cmn(ToRegister(lhs), ToRegister(rhs));
346 bailoutIf(Assembler::Signed, ins->snapshot());
347
348 masm.bind(&done);
349 }
350 }
351 }
352
353 void CodeGenerator::visitMulI64(LMulI64* lir) {
354 LInt64Allocation lhs = lir->lhs();
355 LInt64Allocation rhs = lir->rhs();
356
357 MOZ_ASSERT(ToRegister64(lhs) == ToOutRegister64(lir));
358
359 if (IsConstant(rhs)) {
360 int64_t constant = ToInt64(rhs);
361 switch (constant) {
362 case -1:
363 masm.neg64(ToRegister64(lhs));
364 return;
365 case 0:
366 masm.xor64(ToRegister64(lhs), ToRegister64(lhs));
367 return;
368 case 1:
369 // nop
370 return;
371 case 2:
372 masm.add64(ToRegister64(lhs), ToRegister64(lhs));
373 return;
374 default:
375 if (constant > 0) {
376 // Use shift if constant is power of 2.
377 int32_t shift = mozilla::FloorLog2(constant);
378 if (int64_t(1) << shift == constant) {
379 masm.lshift64(Imm32(shift), ToRegister64(lhs));
380 return;
381 }
382 }
383 Register temp = ToTempRegisterOrInvalid(lir->temp0());
384 masm.mul64(Imm64(constant), ToRegister64(lhs), temp);
385 }
386 } else {
387 Register temp = ToTempRegisterOrInvalid(lir->temp0());
388 masm.mul64(ToOperandOrRegister64(rhs), ToRegister64(lhs), temp);
389 }
390 }
391
392 void CodeGeneratorARM::divICommon(MDiv* mir, Register lhs, Register rhs,
393 Register output, LSnapshot* snapshot,
394 Label& done) {
395 ScratchRegisterScope scratch(masm);
396
397 if (mir->canBeNegativeOverflow()) {
398 // Handle INT32_MIN / -1;
399 // The integer division will give INT32_MIN, but we want -(double)INT32_MIN.
400
401 // Sets EQ if lhs == INT32_MIN.
402 masm.ma_cmp(lhs, Imm32(INT32_MIN), scratch);
403 // If EQ (LHS == INT32_MIN), sets EQ if rhs == -1.
404 masm.ma_cmp(rhs, Imm32(-1), scratch, Assembler::Equal);
405 if (mir->canTruncateOverflow()) {
406 if (mir->trapOnError()) {
407 Label ok;
408 masm.ma_b(&ok, Assembler::NotEqual);
409 masm.wasmTrap(wasm::Trap::IntegerOverflow, mir->trapSiteDesc());
410 masm.bind(&ok);
411 } else {
412 // (-INT32_MIN)|0 = INT32_MIN
413 Label skip;
414 masm.ma_b(&skip, Assembler::NotEqual);
415 masm.ma_mov(Imm32(INT32_MIN), output);
416 masm.ma_b(&done);
417 masm.bind(&skip);
418 }
419 } else {
420 MOZ_ASSERT(mir->fallible());
421 bailoutIf(Assembler::Equal, snapshot);
422 }
423 }
424
425 // Handle divide by zero.
426 if (mir->canBeDivideByZero()) {
427 masm.as_cmp(rhs, Imm8(0));
428 if (mir->canTruncateInfinities()) {
429 if (mir->trapOnError()) {
430 Label nonZero;
431 masm.ma_b(&nonZero, Assembler::NotEqual);
432 masm.wasmTrap(wasm::Trap::IntegerDivideByZero, mir->trapSiteDesc());
433 masm.bind(&nonZero);
434 } else {
435 // Infinity|0 == 0
436 Label skip;
437 masm.ma_b(&skip, Assembler::NotEqual);
438 masm.ma_mov(Imm32(0), output);
439 masm.ma_b(&done);
440 masm.bind(&skip);
441 }
442 } else {
443 MOZ_ASSERT(mir->fallible());
444 bailoutIf(Assembler::Equal, snapshot);
445 }
446 }
447
448 // Handle negative 0.
449 if (!mir->canTruncateNegativeZero() && mir->canBeNegativeZero()) {
450 Label nonzero;
451 masm.as_cmp(lhs, Imm8(0));
452 masm.ma_b(&nonzero, Assembler::NotEqual);
453 masm.as_cmp(rhs, Imm8(0));
454 MOZ_ASSERT(mir->fallible());
455 bailoutIf(Assembler::LessThan, snapshot);
456 masm.bind(&nonzero);
457 }
458 }
459
460 void CodeGenerator::visitDivI(LDivI* ins) {
461 Register lhs = ToRegister(ins->lhs());
462 Register rhs = ToRegister(ins->rhs());
463 Register temp = ToRegister(ins->temp0());
464 Register output = ToRegister(ins->output());
465 MDiv* mir = ins->mir();
466
467 Label done;
468 divICommon(mir, lhs, rhs, output, ins->snapshot(), done);
469
470 if (mir->canTruncateRemainder()) {
471 masm.ma_sdiv(lhs, rhs, output);
472 } else {
473 {
474 ScratchRegisterScope scratch(masm);
475 masm.ma_sdiv(lhs, rhs, temp);
476 masm.ma_mul(temp, rhs, scratch);
477 masm.ma_cmp(lhs, scratch);
478 }
479 bailoutIf(Assembler::NotEqual, ins->snapshot());
480 masm.ma_mov(temp, output);
481 }
482
483 masm.bind(&done);
484 }
485
486 extern "C" {
487 extern MOZ_EXPORT int64_t __aeabi_idivmod(int, int);
488 extern MOZ_EXPORT int64_t __aeabi_uidivmod(int, int);
489 }
490
491 void CodeGenerator::visitSoftDivI(LSoftDivI* ins) {
492 Register lhs = ToRegister(ins->lhs());
493 Register rhs = ToRegister(ins->rhs());
494 Register output = ToRegister(ins->output());
495 MDiv* mir = ins->mir();
496
497 Label done;
498 divICommon(mir, lhs, rhs, output, ins->snapshot(), done);
499
500 if (gen->compilingWasm()) {
501 masm.Push(InstanceReg);
502 int32_t framePushedAfterInstance = masm.framePushed();
503 masm.setupWasmABICall();
504 masm.passABIArg(lhs);
505 masm.passABIArg(rhs);
506 int32_t instanceOffset = masm.framePushed() - framePushedAfterInstance;
507 masm.callWithABI(mir->trapSiteDesc().bytecodeOffset,
508 wasm::SymbolicAddress::aeabi_idivmod,
509 mozilla::Some(instanceOffset));
510 masm.Pop(InstanceReg);
511 } else {
512 using Fn = int64_t (*)(int, int);
513 masm.setupAlignedABICall();
514 masm.passABIArg(lhs);
515 masm.passABIArg(rhs);
516 masm.callWithABI<Fn, __aeabi_idivmod>(
517 ABIType::Int64, CheckUnsafeCallWithABI::DontCheckOther);
518 }
519
520 // idivmod returns the quotient in r0, and the remainder in r1.
521 if (!mir->canTruncateRemainder()) {
522 MOZ_ASSERT(mir->fallible());
523 masm.as_cmp(r1, Imm8(0));
524 bailoutIf(Assembler::NonZero, ins->snapshot());
525 }
526
527 masm.bind(&done);
528 }
529
530 void CodeGenerator::visitDivPowTwoI(LDivPowTwoI* ins) {
531 MDiv* mir = ins->mir();
532 Register lhs = ToRegister(ins->numerator());
533 Register output = ToRegister(ins->output());
534 int32_t shift = ins->shift();
535
536 if (shift == 0) {
537 masm.ma_mov(lhs, output);
538 return;
539 }
540
541 if (!mir->isTruncated()) {
542 // If the remainder is != 0, bailout since this must be a double.
543 {
544 // The bailout code also needs the scratch register.
545 // Here it is only used as a dummy target to set CC flags.
546 ScratchRegisterScope scratch(masm);
547 masm.as_mov(scratch, lsl(lhs, 32 - shift), SetCC);
548 }
549 bailoutIf(Assembler::NonZero, ins->snapshot());
550 }
551
552 if (!mir->canBeNegativeDividend()) {
553 // Numerator is unsigned, so needs no adjusting. Do the shift.
554 masm.as_mov(output, asr(lhs, shift));
555 return;
556 }
557
558 // Adjust the value so that shifting produces a correctly rounded result
559 // when the numerator is negative. See 10-1 "Signed Division by a Known
560 // Power of 2" in Henry S. Warren, Jr.'s Hacker's Delight.
561 ScratchRegisterScope scratch(masm);
562
563 if (shift > 1) {
564 masm.as_mov(scratch, asr(lhs, 31));
565 masm.as_add(scratch, lhs, lsr(scratch, 32 - shift));
566 } else {
567 masm.as_add(scratch, lhs, lsr(lhs, 32 - shift));
568 }
569
570 // Do the shift.
571 masm.as_mov(output, asr(scratch, shift));
572 }
573
574 void CodeGeneratorARM::modICommon(MMod* mir, Register lhs, Register rhs,
575 Register output, LSnapshot* snapshot,
576 Label& done) {
577 // X % 0 is bad because it will give garbage (or abort), when it should give
578 // NaN.
579
580 if (mir->canBeDivideByZero()) {
581 masm.as_cmp(rhs, Imm8(0));
582 if (mir->isTruncated()) {
583 Label nonZero;
584 masm.ma_b(&nonZero, Assembler::NotEqual);
585 if (mir->trapOnError()) {
586 masm.wasmTrap(wasm::Trap::IntegerDivideByZero, mir->trapSiteDesc());
587 } else {
588 // NaN|0 == 0
589 masm.ma_mov(Imm32(0), output);
590 masm.ma_b(&done);
591 }
592 masm.bind(&nonZero);
593 } else {
594 MOZ_ASSERT(mir->fallible());
595 bailoutIf(Assembler::Equal, snapshot);
596 }
597 }
598 }
599
600 void CodeGenerator::visitModI(LModI* ins) {
601 Register lhs = ToRegister(ins->lhs());
602 Register rhs = ToRegister(ins->rhs());
603 Register output = ToRegister(ins->output());
604 MMod* mir = ins->mir();
605
606 // Contrary to other architectures (notably x86) INT_MIN % -1 doesn't need to
607 // be handled separately. |ma_smod| computes the remainder using the |SDIV|
608 // and the |MLS| instructions. On overflow, |SDIV| truncates the result to
609 // 32-bit and returns INT_MIN, see ARM Architecture Reference Manual, SDIV
610 // instruction.
611 //
612 // mls(INT_MIN, sdiv(INT_MIN, -1), -1)
613 // = INT_MIN - (sdiv(INT_MIN, -1) * -1)
614 // = INT_MIN - (INT_MIN * -1)
615 // = INT_MIN - INT_MIN
616 // = 0
617 //
618 // And a zero remainder with a negative dividend is already handled below.
619
620 Label done;
621 modICommon(mir, lhs, rhs, output, ins->snapshot(), done);
622
623 {
624 ScratchRegisterScope scratch(masm);
625 masm.ma_smod(lhs, rhs, output, scratch);
626 }
627
628 // If X%Y == 0 and X < 0, then we *actually* wanted to return -0.0.
629 if (mir->canBeNegativeDividend()) {
630 if (mir->isTruncated()) {
631 // -0.0|0 == 0
632 } else {
633 MOZ_ASSERT(mir->fallible());
634 // See if X < 0
635 masm.as_cmp(output, Imm8(0));
636 masm.ma_b(&done, Assembler::NotEqual);
637 masm.as_cmp(lhs, Imm8(0));
638 bailoutIf(Assembler::Signed, ins->snapshot());
639 }
640 }
641
642 masm.bind(&done);
643 }
644
645 void CodeGenerator::visitSoftModI(LSoftModI* ins) {
646 // Extract the registers from this instruction.
647 Register lhs = ToRegister(ins->lhs());
648 Register rhs = ToRegister(ins->rhs());
649 Register output = ToRegister(ins->output());
650 Register callTemp = ToRegister(ins->temp0());
651 MMod* mir = ins->mir();
652 Label done;
653
654 // Save the lhs in case we end up with a 0 that should be a -0.0 because lhs <
655 // 0.
656 MOZ_ASSERT(callTemp != lhs);
657 MOZ_ASSERT(callTemp != rhs);
658 masm.ma_mov(lhs, callTemp);
659
660 // Prevent INT_MIN % -1.
661 //
662 // |aeabi_idivmod| is allowed to return any arbitrary value when called with
663 // |(INT_MIN, -1)|, see "Run-time ABI for the ARM architecture manual". Most
664 // implementations perform a non-trapping signed integer division and
665 // return the expected result, i.e. INT_MIN. But since we can't rely on this
666 // behavior, handle this case separately here.
667 if (mir->canBeNegativeDividend()) {
668 {
669 ScratchRegisterScope scratch(masm);
670 // Sets EQ if lhs == INT_MIN
671 masm.ma_cmp(lhs, Imm32(INT_MIN), scratch);
672 // If EQ (LHS == INT_MIN), sets EQ if rhs == -1
673 masm.ma_cmp(rhs, Imm32(-1), scratch, Assembler::Equal);
674 }
675 if (mir->isTruncated()) {
676 // (INT_MIN % -1)|0 == 0
677 Label skip;
678 masm.ma_b(&skip, Assembler::NotEqual);
679 masm.ma_mov(Imm32(0), output);
680 masm.ma_b(&done);
681 masm.bind(&skip);
682 } else {
683 MOZ_ASSERT(mir->fallible());
684 bailoutIf(Assembler::Equal, ins->snapshot());
685 }
686 }
687
688 modICommon(mir, lhs, rhs, output, ins->snapshot(), done);
689
690 if (gen->compilingWasm()) {
691 masm.Push(InstanceReg);
692 int32_t framePushedAfterInstance = masm.framePushed();
693 masm.setupWasmABICall();
694 masm.passABIArg(lhs);
695 masm.passABIArg(rhs);
696 int32_t instanceOffset = masm.framePushed() - framePushedAfterInstance;
697 masm.callWithABI(mir->trapSiteDesc().bytecodeOffset,
698 wasm::SymbolicAddress::aeabi_idivmod,
699 mozilla::Some(instanceOffset));
700 masm.Pop(InstanceReg);
701 } else {
702 using Fn = int64_t (*)(int, int);
703 masm.setupAlignedABICall();
704 masm.passABIArg(lhs);
705 masm.passABIArg(rhs);
706 masm.callWithABI<Fn, __aeabi_idivmod>(
707 ABIType::Int64, CheckUnsafeCallWithABI::DontCheckOther);
708 }
709
710 MOZ_ASSERT(r1 != output);
711 masm.move32(r1, output);
712
713 // If X%Y == 0 and X < 0, then we *actually* wanted to return -0.0
714 if (mir->canBeNegativeDividend()) {
715 if (mir->isTruncated()) {
716 // -0.0|0 == 0
717 } else {
718 MOZ_ASSERT(mir->fallible());
719 // See if X < 0
720 masm.as_cmp(output, Imm8(0));
721 masm.ma_b(&done, Assembler::NotEqual);
722 masm.as_cmp(callTemp, Imm8(0));
723 bailoutIf(Assembler::Signed, ins->snapshot());
724 }
725 }
726
727 masm.bind(&done);
728 }
729
730 void CodeGenerator::visitModPowTwoI(LModPowTwoI* ins) {
731 Register in = ToRegister(ins->input());
732 Register out = ToRegister(ins->output());
733 MMod* mir = ins->mir();
734 Label fin;
735 // bug 739870, jbramley has a different sequence that may help with speed
736 // here.
737
738 masm.ma_mov(in, out, SetCC);
739 masm.ma_b(&fin, Assembler::Zero);
740 masm.as_rsb(out, out, Imm8(0), LeaveCC, Assembler::Signed);
741 {
742 ScratchRegisterScope scratch(masm);
743 masm.ma_and(Imm32((1 << ins->shift()) - 1), out, scratch);
744 }
745 masm.as_rsb(out, out, Imm8(0), SetCC, Assembler::Signed);
746 if (mir->canBeNegativeDividend()) {
747 if (!mir->isTruncated()) {
748 MOZ_ASSERT(mir->fallible());
749 bailoutIf(Assembler::Zero, ins->snapshot());
750 } else {
751 // -0|0 == 0
752 }
753 }
754 masm.bind(&fin);
755 }
756
757 void CodeGenerator::visitModMaskI(LModMaskI* ins) {
758 Register src = ToRegister(ins->input());
759 Register dest = ToRegister(ins->output());
760 Register tmp1 = ToRegister(ins->temp0());
761 Register tmp2 = ToRegister(ins->temp1());
762 MMod* mir = ins->mir();
763
764 ScratchRegisterScope scratch(masm);
765 SecondScratchRegisterScope scratch2(masm);
766
767 masm.ma_mod_mask(src, dest, tmp1, tmp2, scratch, scratch2, ins->shift());
768
769 if (mir->canBeNegativeDividend()) {
770 if (!mir->isTruncated()) {
771 MOZ_ASSERT(mir->fallible());
772 bailoutIf(Assembler::Zero, ins->snapshot());
773 } else {
774 // -0|0 == 0
775 }
776 }
777 }
778
779 void CodeGeneratorARM::emitBigIntPtrDiv(LBigIntPtrDiv* ins, Register dividend,
780 Register divisor, Register output) {
781 // Callers handle division by zero and integer overflow.
782
783 if (ARMFlags::HasIDIV()) {
784 masm.ma_sdiv(dividend, divisor, /* result= */ output);
785 return;
786 }
787
788 // idivmod returns the quotient in r0, and the remainder in r1.
789 MOZ_ASSERT(ToRegister(ins->temp0()) == r0);
790 MOZ_ASSERT(ToRegister(ins->temp1()) == r1);
791
792 LiveRegisterSet volatileRegs = liveVolatileRegs(ins);
793 volatileRegs.takeUnchecked(output);
794
795 masm.PushRegsInMask(volatileRegs);
796
797 using Fn = int64_t (*)(int, int);
798 masm.setupUnalignedABICall(output);
799 masm.passABIArg(dividend);
800 masm.passABIArg(divisor);
801 masm.callWithABI<Fn, __aeabi_idivmod>(ABIType::Int64,
802 CheckUnsafeCallWithABI::DontCheckOther);
803 masm.move32(r0, output);
804
805 masm.PopRegsInMask(volatileRegs);
806 }
807
808 void CodeGeneratorARM::emitBigIntPtrMod(LBigIntPtrMod* ins, Register dividend,
809 Register divisor, Register output) {
810 // Callers handle division by zero and integer overflow.
811
812 if (ARMFlags::HasIDIV()) {
813 ScratchRegisterScope scratch(masm);
814 masm.ma_smod(dividend, divisor, /* result= */ output, scratch);
815 return;
816 }
817
818 // idivmod returns the quotient in r0, and the remainder in r1.
819 MOZ_ASSERT(ToRegister(ins->temp0()) == r0);
820 MOZ_ASSERT(ToRegister(ins->temp1()) == r1);
821
822 LiveRegisterSet volatileRegs = liveVolatileRegs(ins);
823 volatileRegs.takeUnchecked(output);
824
825 masm.PushRegsInMask(volatileRegs);
826
827 using Fn = int64_t (*)(int, int);
828 masm.setupUnalignedABICall(output);
829 masm.passABIArg(dividend);
830 masm.passABIArg(divisor);
831 masm.callWithABI<Fn, __aeabi_idivmod>(ABIType::Int64,
832 CheckUnsafeCallWithABI::DontCheckOther);
833 masm.move32(r1, output);
834
835 masm.PopRegsInMask(volatileRegs);
836 }
837
838 void CodeGenerator::visitBitNotI(LBitNotI* ins) {
839 const LAllocation* input = ins->input();
840 const LDefinition* dest = ins->output();
841 // This will not actually be true on arm. We can not an imm8m in order to
842 // get a wider range of numbers
843 MOZ_ASSERT(!input->isConstant());
844
845 masm.ma_mvn(ToRegister(input), ToRegister(dest));
846 }
847
848 void CodeGenerator::visitBitOpI(LBitOpI* ins) {
849 const LAllocation* lhs = ins->lhs();
850 const LAllocation* rhs = ins->rhs();
851 const LDefinition* dest = ins->output();
852
853 ScratchRegisterScope scratch(masm);
854
855 // All of these bitops should be either imm32's, or integer registers.
856 switch (ins->bitop()) {
857 case JSOp::BitOr:
858 if (rhs->isConstant()) {
859 masm.ma_orr(Imm32(ToInt32(rhs)), ToRegister(lhs), ToRegister(dest),
860 scratch);
861 } else {
862 masm.ma_orr(ToRegister(rhs), ToRegister(lhs), ToRegister(dest));
863 }
864 break;
865 case JSOp::BitXor:
866 if (rhs->isConstant()) {
867 masm.ma_eor(Imm32(ToInt32(rhs)), ToRegister(lhs), ToRegister(dest),
868 scratch);
869 } else {
870 masm.ma_eor(ToRegister(rhs), ToRegister(lhs), ToRegister(dest));
871 }
872 break;
873 case JSOp::BitAnd:
874 if (rhs->isConstant()) {
875 masm.ma_and(Imm32(ToInt32(rhs)), ToRegister(lhs), ToRegister(dest),
876 scratch);
877 } else {
878 masm.ma_and(ToRegister(rhs), ToRegister(lhs), ToRegister(dest));
879 }
880 break;
881 default:
882 MOZ_CRASH("unexpected binary opcode");
883 }
884 }
885
886 void CodeGenerator::visitShiftI(LShiftI* ins) {
887 Register lhs = ToRegister(ins->lhs());
888 const LAllocation* rhs = ins->rhs();
889 Register dest = ToRegister(ins->output());
890
891 if (rhs->isConstant()) {
892 int32_t shift = ToInt32(rhs) & 0x1F;
893 switch (ins->bitop()) {
894 case JSOp::Lsh:
895 if (shift) {
896 masm.ma_lsl(Imm32(shift), lhs, dest);
897 } else {
898 masm.ma_mov(lhs, dest);
899 }
900 break;
901 case JSOp::Rsh:
902 if (shift) {
903 masm.ma_asr(Imm32(shift), lhs, dest);
904 } else {
905 masm.ma_mov(lhs, dest);
906 }
907 break;
908 case JSOp::Ursh:
909 if (shift) {
910 masm.ma_lsr(Imm32(shift), lhs, dest);
911 } else {
912 // x >>> 0 can overflow.
913 masm.ma_mov(lhs, dest);
914 if (ins->mir()->toUrsh()->fallible()) {
915 masm.as_cmp(dest, Imm8(0));
916 bailoutIf(Assembler::LessThan, ins->snapshot());
917 }
918 }
919 break;
920 default:
921 MOZ_CRASH("Unexpected shift op");
922 }
923 } else {
924 // The shift amounts should be AND'ed into the 0-31 range since arm
925 // shifts by the lower byte of the register (it will attempt to shift by
926 // 250 if you ask it to).
927 masm.as_and(dest, ToRegister(rhs), Imm8(0x1F));
928
929 switch (ins->bitop()) {
930 case JSOp::Lsh:
931 masm.ma_lsl(dest, lhs, dest);
932 break;
933 case JSOp::Rsh:
934 masm.ma_asr(dest, lhs, dest);
935 break;
936 case JSOp::Ursh:
937 masm.ma_lsr(dest, lhs, dest);
938 if (ins->mir()->toUrsh()->fallible()) {
939 // x >>> 0 can overflow.
940 masm.as_cmp(dest, Imm8(0));
941 bailoutIf(Assembler::LessThan, ins->snapshot());
942 }
943 break;
944 default:
945 MOZ_CRASH("Unexpected shift op");
946 }
947 }
948 }
949
950 void CodeGenerator::visitUrshD(LUrshD* ins) {
951 Register lhs = ToRegister(ins->lhs());
952 Register temp = ToRegister(ins->temp0());
953
954 const LAllocation* rhs = ins->rhs();
955 FloatRegister out = ToFloatRegister(ins->output());
956
957 if (rhs->isConstant()) {
958 int32_t shift = ToInt32(rhs) & 0x1F;
959 if (shift) {
960 masm.ma_lsr(Imm32(shift), lhs, temp);
961 } else {
962 masm.ma_mov(lhs, temp);
963 }
964 } else {
965 masm.as_and(temp, ToRegister(rhs), Imm8(0x1F));
966 masm.ma_lsr(temp, lhs, temp);
967 }
968
969 masm.convertUInt32ToDouble(temp, out);
970 }
971
972 void CodeGenerator::visitPowHalfD(LPowHalfD* ins) {
973 FloatRegister input = ToFloatRegister(ins->input());
974 FloatRegister output = ToFloatRegister(ins->output());
975 ScratchDoubleScope scratch(masm);
976
977 Label done;
978
979 // Masm.pow(-Infinity, 0.5) == Infinity.
980 masm.loadConstantDouble(NegativeInfinity<double>(), scratch);
981 masm.compareDouble(input, scratch);
982 masm.ma_vneg(scratch, output, Assembler::Equal);
983 masm.ma_b(&done, Assembler::Equal);
984
985 // Math.pow(-0, 0.5) == 0 == Math.pow(0, 0.5).
986 // Adding 0 converts any -0 to 0.
987 masm.loadConstantDouble(0.0, scratch);
988 masm.ma_vadd(scratch, input, output);
989 masm.ma_vsqrt(output, output);
990
991 masm.bind(&done);
992 }
993
994 MoveOperand CodeGeneratorARM::toMoveOperand(LAllocation a) const {
995 if (a.isGeneralReg()) {
996 return MoveOperand(ToRegister(a));
997 }
998 if (a.isFloatReg()) {
999 return MoveOperand(ToFloatRegister(a));
1000 }
1001 MoveOperand::Kind kind = a.isStackArea() ? MoveOperand::Kind::EffectiveAddress
1002 : MoveOperand::Kind::Memory;
1003 Address addr = ToAddress(a);
1004 MOZ_ASSERT((addr.offset & 3) == 0);
1005 return MoveOperand(addr, kind);
1006 }
1007
1008 class js::jit::OutOfLineTableSwitch
1009 : public OutOfLineCodeBase<CodeGeneratorARM> {
1010 MTableSwitch* mir_;
1011 Vector<CodeLabel, 8, JitAllocPolicy> codeLabels_;
1012
1013 void accept(CodeGeneratorARM* codegen) override {
1014 codegen->visitOutOfLineTableSwitch(this);
1015 }
1016
1017 public:
1018 OutOfLineTableSwitch(TempAllocator& alloc, MTableSwitch* mir)
1019 : mir_(mir), codeLabels_(alloc) {}
1020
1021 MTableSwitch* mir() const { return mir_; }
1022
1023 bool addCodeLabel(CodeLabel label) { return codeLabels_.append(label); }
1024 CodeLabel codeLabel(unsigned i) { return codeLabels_[i]; }
1025 };
1026
1027 void CodeGeneratorARM::visitOutOfLineTableSwitch(OutOfLineTableSwitch* ool) {
1028 MTableSwitch* mir = ool->mir();
1029
1030 size_t numCases = mir->numCases();
1031 for (size_t i = 0; i < numCases; i++) {
1032 LBlock* caseblock =
1033 skipTrivialBlocks(mir->getCase(numCases - 1 - i))->lir();
1034 Label* caseheader = caseblock->label();
1035 uint32_t caseoffset = caseheader->offset();
1036
1037 // The entries of the jump table need to be absolute addresses and thus
1038 // must be patched after codegen is finished.
1039 CodeLabel cl = ool->codeLabel(i);
1040 cl.target()->bind(caseoffset);
1041 masm.addCodeLabel(cl);
1042 }
1043 }
1044
1045 void CodeGeneratorARM::emitTableSwitchDispatch(MTableSwitch* mir,
1046 Register index, Register base) {
1047 // The code generated by this is utter hax.
1048 // The end result looks something like:
1049 // SUBS index, input, #base
1050 // RSBSPL index, index, #max
1051 // LDRPL pc, pc, index lsl 2
1052 // B default
1053
1054 // If the range of targets in N through M, we first subtract off the lowest
1055 // case (N), which both shifts the arguments into the range 0 to (M - N)
1056 // with and sets the MInus flag if the argument was out of range on the low
1057 // end.
1058
1059 // Then we a reverse subtract with the size of the jump table, which will
1060 // reverse the order of range (It is size through 0, rather than 0 through
1061 // size). The main purpose of this is that we set the same flag as the lower
1062 // bound check for the upper bound check. Lastly, we do this conditionally
1063 // on the previous check succeeding.
1064
1065 // Then we conditionally load the pc offset by the (reversed) index (times
1066 // the address size) into the pc, which branches to the correct case. NOTE:
1067 // when we go to read the pc, the value that we get back is the pc of the
1068 // current instruction *PLUS 8*. This means that ldr foo, [pc, +0] reads
1069 // $pc+8. In other words, there is an empty word after the branch into the
1070 // switch table before the table actually starts. Since the only other
1071 // unhandled case is the default case (both out of range high and out of
1072 // range low) I then insert a branch to default case into the extra slot,
1073 // which ensures we don't attempt to execute the address table.
1074 Label* defaultcase = skipTrivialBlocks(mir->getDefault())->lir()->label();
1075
1076 ScratchRegisterScope scratch(masm);
1077
1078 int32_t cases = mir->numCases();
1079 // Lower value with low value.
1080 masm.ma_sub(index, Imm32(mir->low()), index, scratch, SetCC);
1081 masm.ma_rsb(index, Imm32(cases - 1), index, scratch, SetCC,
1082 Assembler::NotSigned);
1083 // Inhibit pools within the following sequence because we are indexing into
1084 // a pc relative table. The region will have one instruction for ma_ldr, one
1085 // for ma_b, and each table case takes one word.
1086 AutoForbidPoolsAndNops afp(&masm, 1 + 1 + cases);
1087 masm.ma_ldr(DTRAddr(pc, DtrRegImmShift(index, LSL, 2)), pc, Offset,
1088 Assembler::NotSigned);
1089 masm.ma_b(defaultcase);
1090
1091 // To fill in the CodeLabels for the case entries, we need to first generate
1092 // the case entries (we don't yet know their offsets in the instruction
1093 // stream).
1094 OutOfLineTableSwitch* ool = new (alloc()) OutOfLineTableSwitch(alloc(), mir);
1095 for (int32_t i = 0; i < cases; i++) {
1096 CodeLabel cl;
1097 masm.writeCodePointer(&cl);
1098 masm.propagateOOM(ool->addCodeLabel(cl));
1099 }
1100 addOutOfLineCode(ool, mir);
1101 }
1102
1103 void CodeGenerator::visitMathD(LMathD* math) {
1104 FloatRegister src1 = ToFloatRegister(math->lhs());
1105 FloatRegister src2 = ToFloatRegister(math->rhs());
1106 FloatRegister output = ToFloatRegister(math->output());
1107
1108 switch (math->jsop()) {
1109 case JSOp::Add:
1110 masm.ma_vadd(src1, src2, output);
1111 break;
1112 case JSOp::Sub:
1113 masm.ma_vsub(src1, src2, output);
1114 break;
1115 case JSOp::Mul:
1116 masm.ma_vmul(src1, src2, output);
1117 break;
1118 case JSOp::Div:
1119 masm.ma_vdiv(src1, src2, output);
1120 break;
1121 default:
1122 MOZ_CRASH("unexpected opcode");
1123 }
1124 }
1125
1126 void CodeGenerator::visitMathF(LMathF* math) {
1127 FloatRegister src1 = ToFloatRegister(math->lhs());
1128 FloatRegister src2 = ToFloatRegister(math->rhs());
1129 FloatRegister output = ToFloatRegister(math->output());
1130
1131 switch (math->jsop()) {
1132 case JSOp::Add:
1133 masm.ma_vadd_f32(src1, src2, output);
1134 break;
1135 case JSOp::Sub:
1136 masm.ma_vsub_f32(src1, src2, output);
1137 break;
1138 case JSOp::Mul:
1139 masm.ma_vmul_f32(src1, src2, output);
1140 break;
1141 case JSOp::Div:
1142 masm.ma_vdiv_f32(src1, src2, output);
1143 break;
1144 default:
1145 MOZ_CRASH("unexpected opcode");
1146 }
1147 }
1148
1149 void CodeGenerator::visitTruncateDToInt32(LTruncateDToInt32* ins) {
1150 emitTruncateDouble(ToFloatRegister(ins->input()), ToRegister(ins->output()),
1151 ins->mir());
1152 }
1153
1154 void CodeGenerator::visitWasmBuiltinTruncateDToInt32(
1155 LWasmBuiltinTruncateDToInt32* ins) {
1156 emitTruncateDouble(ToFloatRegister(ins->input()), ToRegister(ins->output()),
1157 ins->mir());
1158 }
1159
1160 void CodeGenerator::visitTruncateFToInt32(LTruncateFToInt32* ins) {
1161 emitTruncateFloat32(ToFloatRegister(ins->input()), ToRegister(ins->output()),
1162 ins->mir());
1163 }
1164
1165 void CodeGenerator::visitWasmBuiltinTruncateFToInt32(
1166 LWasmBuiltinTruncateFToInt32* ins) {
1167 emitTruncateFloat32(ToFloatRegister(ins->input()), ToRegister(ins->output()),
1168 ins->mir());
1169 }
1170
1171 void CodeGenerator::visitBox(LBox* box) {
1172 const LDefinition* type = box->getDef(TYPE_INDEX);
1173
1174 MOZ_ASSERT(!box->payload()->isConstant());
1175
1176 // On arm, the input operand and the output payload have the same virtual
1177 // register. All that needs to be written is the type tag for the type
1178 // definition.
1179 masm.ma_mov(Imm32(MIRTypeToTag(box->type())), ToRegister(type));
1180 }
1181
1182 void CodeGenerator::visitBoxFloatingPoint(LBoxFloatingPoint* box) {
1183 const AnyRegister in = ToAnyRegister(box->input());
1184 const ValueOperand out = ToOutValue(box);
1185
1186 masm.moveValue(TypedOrValueRegister(box->type(), in), out);
1187 }
1188
1189 void CodeGenerator::visitUnbox(LUnbox* unbox) {
1190 // Note that for unbox, the type and payload indexes are switched on the
1191 // inputs.
1192 MUnbox* mir = unbox->mir();
1193 Register type = ToRegister(unbox->type());
1194 Register payload = ToRegister(unbox->payload());
1195 Register output = ToRegister(unbox->output());
1196
1197 mozilla::Maybe<ScratchRegisterScope> scratch;
1198 scratch.emplace(masm);
1199
1200 JSValueTag tag = MIRTypeToTag(mir->type());
1201 if (mir->fallible()) {
1202 masm.ma_cmp(type, Imm32(tag), *scratch);
1203 bailoutIf(Assembler::NotEqual, unbox->snapshot());
1204 } else {
1205 #ifdef DEBUG
1206 Label ok;
1207 masm.ma_cmp(type, Imm32(tag), *scratch);
1208 masm.ma_b(&ok, Assembler::Equal);
1209 scratch.reset();
1210 masm.assumeUnreachable("Infallible unbox type mismatch");
1211 masm.bind(&ok);
1212 #endif
1213 }
1214
1215 // Note: If spectreValueMasking is disabled, then this instruction will
1216 // default to a no-op as long as the lowering allocate the same register for
1217 // the output and the payload.
1218 masm.unboxNonDouble(ValueOperand(type, payload), output,
1219 ValueTypeFromMIRType(mir->type()));
1220 }
1221
1222 void CodeGenerator::visitTestDAndBranch(LTestDAndBranch* test) {
1223 const LAllocation* opd = test->input();
1224 masm.ma_vcmpz(ToFloatRegister(opd));
1225 masm.as_vmrs(pc);
1226
1227 MBasicBlock* ifTrue = test->ifTrue();
1228 MBasicBlock* ifFalse = test->ifFalse();
1229 // If the compare set the 0 bit, then the result is definitely false.
1230 jumpToBlock(ifFalse, Assembler::Zero);
1231 // It is also false if one of the operands is NAN, which is shown as
1232 // Overflow.
1233 jumpToBlock(ifFalse, Assembler::Overflow);
1234 jumpToBlock(ifTrue);
1235 }
1236
1237 void CodeGenerator::visitTestFAndBranch(LTestFAndBranch* test) {
1238 const LAllocation* opd = test->input();
1239 masm.ma_vcmpz_f32(ToFloatRegister(opd));
1240 masm.as_vmrs(pc);
1241
1242 MBasicBlock* ifTrue = test->ifTrue();
1243 MBasicBlock* ifFalse = test->ifFalse();
1244 // If the compare set the 0 bit, then the result is definitely false.
1245 jumpToBlock(ifFalse, Assembler::Zero);
1246 // It is also false if one of the operands is NAN, which is shown as
1247 // Overflow.
1248 jumpToBlock(ifFalse, Assembler::Overflow);
1249 jumpToBlock(ifTrue);
1250 }
1251
1252 void CodeGenerator::visitCompareD(LCompareD* comp) {
1253 FloatRegister lhs = ToFloatRegister(comp->left());
1254 FloatRegister rhs = ToFloatRegister(comp->right());
1255
1256 Assembler::DoubleCondition cond = JSOpToDoubleCondition(comp->mir()->jsop());
1257 masm.compareDouble(lhs, rhs);
1258 masm.emitSet(Assembler::ConditionFromDoubleCondition(cond),
1259 ToRegister(comp->output()));
1260 }
1261
1262 void CodeGenerator::visitCompareF(LCompareF* comp) {
1263 FloatRegister lhs = ToFloatRegister(comp->left());
1264 FloatRegister rhs = ToFloatRegister(comp->right());
1265
1266 Assembler::DoubleCondition cond = JSOpToDoubleCondition(comp->mir()->jsop());
1267 masm.compareFloat(lhs, rhs);
1268 masm.emitSet(Assembler::ConditionFromDoubleCondition(cond),
1269 ToRegister(comp->output()));
1270 }
1271
1272 void CodeGenerator::visitCompareDAndBranch(LCompareDAndBranch* comp) {
1273 FloatRegister lhs = ToFloatRegister(comp->left());
1274 FloatRegister rhs = ToFloatRegister(comp->right());
1275
1276 Assembler::DoubleCondition cond =
1277 JSOpToDoubleCondition(comp->cmpMir()->jsop());
1278 masm.compareDouble(lhs, rhs);
1279 emitBranch(Assembler::ConditionFromDoubleCondition(cond), comp->ifTrue(),
1280 comp->ifFalse());
1281 }
1282
1283 void CodeGenerator::visitCompareFAndBranch(LCompareFAndBranch* comp) {
1284 FloatRegister lhs = ToFloatRegister(comp->left());
1285 FloatRegister rhs = ToFloatRegister(comp->right());
1286
1287 Assembler::DoubleCondition cond =
1288 JSOpToDoubleCondition(comp->cmpMir()->jsop());
1289 masm.compareFloat(lhs, rhs);
1290 emitBranch(Assembler::ConditionFromDoubleCondition(cond), comp->ifTrue(),
1291 comp->ifFalse());
1292 }
1293
1294 void CodeGenerator::visitWasmUint32ToDouble(LWasmUint32ToDouble* lir) {
1295 masm.convertUInt32ToDouble(ToRegister(lir->input()),
1296 ToFloatRegister(lir->output()));
1297 }
1298
1299 void CodeGenerator::visitWasmUint32ToFloat32(LWasmUint32ToFloat32* lir) {
1300 masm.convertUInt32ToFloat32(ToRegister(lir->input()),
1301 ToFloatRegister(lir->output()));
1302 }
1303
1304 void CodeGenerator::visitNotD(LNotD* ins) {
1305 // Since this operation is not, we want to set a bit if the double is
1306 // falsey, which means 0.0, -0.0 or NaN. When comparing with 0, an input of
1307 // 0 will set the Z bit (30) and NaN will set the V bit (28) of the APSR.
1308 FloatRegister opd = ToFloatRegister(ins->input());
1309 Register dest = ToRegister(ins->output());
1310
1311 // Do the compare.
1312 masm.ma_vcmpz(opd);
1313 // TODO There are three variations here to compare performance-wise.
1314 bool nocond = true;
1315 if (nocond) {
1316 // Load the value into the dest register.
1317 masm.as_vmrs(dest);
1318 masm.ma_lsr(Imm32(28), dest, dest);
1319 // 28 + 2 = 30
1320 masm.ma_alu(dest, lsr(dest, 2), dest, OpOrr);
1321 masm.as_and(dest, dest, Imm8(1));
1322 } else {
1323 masm.as_vmrs(pc);
1324 masm.ma_mov(Imm32(0), dest);
1325 masm.ma_mov(Imm32(1), dest, Assembler::Equal);
1326 masm.ma_mov(Imm32(1), dest, Assembler::Overflow);
1327 }
1328 }
1329
1330 void CodeGenerator::visitNotF(LNotF* ins) {
1331 // Since this operation is not, we want to set a bit if the double is
1332 // falsey, which means 0.0, -0.0 or NaN. When comparing with 0, an input of
1333 // 0 will set the Z bit (30) and NaN will set the V bit (28) of the APSR.
1334 FloatRegister opd = ToFloatRegister(ins->input());
1335 Register dest = ToRegister(ins->output());
1336
1337 // Do the compare.
1338 masm.ma_vcmpz_f32(opd);
1339 // TODO There are three variations here to compare performance-wise.
1340 bool nocond = true;
1341 if (nocond) {
1342 // Load the value into the dest register.
1343 masm.as_vmrs(dest);
1344 masm.ma_lsr(Imm32(28), dest, dest);
1345 // 28 + 2 = 30
1346 masm.ma_alu(dest, lsr(dest, 2), dest, OpOrr);
1347 masm.as_and(dest, dest, Imm8(1));
1348 } else {
1349 masm.as_vmrs(pc);
1350 masm.ma_mov(Imm32(0), dest);
1351 masm.ma_mov(Imm32(1), dest, Assembler::Equal);
1352 masm.ma_mov(Imm32(1), dest, Assembler::Overflow);
1353 }
1354 }
1355
1356 void CodeGeneratorARM::generateInvalidateEpilogue() {
1357 // Ensure that there is enough space in the buffer for the OsiPoint patching
1358 // to occur. Otherwise, we could overwrite the invalidation epilogue.
1359 for (size_t i = 0; i < sizeof(void*); i += Assembler::NopSize()) {
1360 masm.nop();
1361 }
1362
1363 masm.bind(&invalidate_);
1364
1365 // Push the return address of the point that we bailed out at onto the stack.
1366 masm.Push(lr);
1367
1368 // Push the Ion script onto the stack (when we determine what that pointer
1369 // is).
1370 invalidateEpilogueData_ = masm.pushWithPatch(ImmWord(uintptr_t(-1)));
1371
1372 // Jump to the invalidator which will replace the current frame.
1373 TrampolinePtr thunk = gen->jitRuntime()->getInvalidationThunk();
1374 masm.jump(thunk);
1375 }
1376
1377 void CodeGenerator::visitCompareExchangeTypedArrayElement(
1378 LCompareExchangeTypedArrayElement* lir) {
1379 Register elements = ToRegister(lir->elements());
1380 AnyRegister output = ToAnyRegister(lir->output());
1381 Register temp = ToTempRegisterOrInvalid(lir->temp0());
1382
1383 Register oldval = ToRegister(lir->oldval());
1384 Register newval = ToRegister(lir->newval());
1385
1386 Scalar::Type arrayType = lir->mir()->arrayType();
1387
1388 if (lir->index()->isConstant()) {
1389 Address dest = ToAddress(elements, lir->index(), arrayType);
1390 masm.compareExchangeJS(arrayType, Synchronization::Full(), dest, oldval,
1391 newval, temp, output);
1392 } else {
1393 BaseIndex dest(elements, ToRegister(lir->index()),
1394 ScaleFromScalarType(arrayType));
1395 masm.compareExchangeJS(arrayType, Synchronization::Full(), dest, oldval,
1396 newval, temp, output);
1397 }
1398 }
1399
1400 void CodeGenerator::visitAtomicExchangeTypedArrayElement(
1401 LAtomicExchangeTypedArrayElement* lir) {
1402 Register elements = ToRegister(lir->elements());
1403 AnyRegister output = ToAnyRegister(lir->output());
1404 Register temp = ToTempRegisterOrInvalid(lir->temp0());
1405
1406 Register value = ToRegister(lir->value());
1407
1408 Scalar::Type arrayType = lir->mir()->arrayType();
1409
1410 if (lir->index()->isConstant()) {
1411 Address dest = ToAddress(elements, lir->index(), arrayType);
1412 masm.atomicExchangeJS(arrayType, Synchronization::Full(), dest, value, temp,
1413 output);
1414 } else {
1415 BaseIndex dest(elements, ToRegister(lir->index()),
1416 ScaleFromScalarType(arrayType));
1417 masm.atomicExchangeJS(arrayType, Synchronization::Full(), dest, value, temp,
1418 output);
1419 }
1420 }
1421
1422 void CodeGenerator::visitAtomicTypedArrayElementBinop(
1423 LAtomicTypedArrayElementBinop* lir) {
1424 MOZ_ASSERT(!lir->mir()->isForEffect());
1425
1426 AnyRegister output = ToAnyRegister(lir->output());
1427 Register elements = ToRegister(lir->elements());
1428 Register flagTemp = ToRegister(lir->temp0());
1429 Register outTemp = ToTempRegisterOrInvalid(lir->temp1());
1430 Register value = ToRegister(lir->value());
1431
1432 Scalar::Type arrayType = lir->mir()->arrayType();
1433
1434 if (lir->index()->isConstant()) {
1435 Address mem = ToAddress(elements, lir->index(), arrayType);
1436 masm.atomicFetchOpJS(arrayType, Synchronization::Full(),
1437 lir->mir()->operation(), value, mem, flagTemp, outTemp,
1438 output);
1439 } else {
1440 BaseIndex mem(elements, ToRegister(lir->index()),
1441 ScaleFromScalarType(arrayType));
1442 masm.atomicFetchOpJS(arrayType, Synchronization::Full(),
1443 lir->mir()->operation(), value, mem, flagTemp, outTemp,
1444 output);
1445 }
1446 }
1447
1448 void CodeGenerator::visitAtomicTypedArrayElementBinopForEffect(
1449 LAtomicTypedArrayElementBinopForEffect* lir) {
1450 MOZ_ASSERT(lir->mir()->isForEffect());
1451
1452 Register elements = ToRegister(lir->elements());
1453 Register flagTemp = ToRegister(lir->temp0());
1454 Register value = ToRegister(lir->value());
1455 Scalar::Type arrayType = lir->mir()->arrayType();
1456
1457 if (lir->index()->isConstant()) {
1458 Address mem = ToAddress(elements, lir->index(), arrayType);
1459 masm.atomicEffectOpJS(arrayType, Synchronization::Full(),
1460 lir->mir()->operation(), value, mem, flagTemp);
1461 } else {
1462 BaseIndex mem(elements, ToRegister(lir->index()),
1463 ScaleFromScalarType(arrayType));
1464 masm.atomicEffectOpJS(arrayType, Synchronization::Full(),
1465 lir->mir()->operation(), value, mem, flagTemp);
1466 }
1467 }
1468
1469 void CodeGenerator::visitAtomicLoad64(LAtomicLoad64* lir) {
1470 Register elements = ToRegister(lir->elements());
1471 Register64 out = ToOutRegister64(lir);
1472
1473 const MLoadUnboxedScalar* mir = lir->mir();
1474
1475 Scalar::Type storageType = mir->storageType();
1476
1477 if (lir->index()->isConstant()) {
1478 Address source =
1479 ToAddress(elements, lir->index(), storageType, mir->offsetAdjustment());
1480 masm.atomicLoad64(Synchronization::Load(), source, out);
1481 } else {
1482 BaseIndex source(elements, ToRegister(lir->index()),
1483 ScaleFromScalarType(storageType), mir->offsetAdjustment());
1484 masm.atomicLoad64(Synchronization::Load(), source, out);
1485 }
1486 }
1487
1488 void CodeGenerator::visitAtomicStore64(LAtomicStore64* lir) {
1489 Register elements = ToRegister(lir->elements());
1490 Register64 value = ToRegister64(lir->value());
1491 Register64 temp = ToRegister64(lir->temp0());
1492
1493 Scalar::Type writeType = lir->mir()->writeType();
1494
1495 if (lir->index()->isConstant()) {
1496 Address dest = ToAddress(elements, lir->index(), writeType);
1497 masm.atomicStore64(Synchronization::Store(), dest, value, temp);
1498 } else {
1499 BaseIndex dest(elements, ToRegister(lir->index()),
1500 ScaleFromScalarType(writeType));
1501 masm.atomicStore64(Synchronization::Store(), dest, value, temp);
1502 }
1503 }
1504
1505 void CodeGenerator::visitCompareExchangeTypedArrayElement64(
1506 LCompareExchangeTypedArrayElement64* lir) {
1507 Register elements = ToRegister(lir->elements());
1508 Register64 oldval = ToRegister64(lir->oldval());
1509 Register64 newval = ToRegister64(lir->newval());
1510 Register64 out = ToOutRegister64(lir);
1511
1512 Scalar::Type arrayType = lir->mir()->arrayType();
1513
1514 if (lir->index()->isConstant()) {
1515 Address dest = ToAddress(elements, lir->index(), arrayType);
1516 masm.compareExchange64(Synchronization::Full(), dest, oldval, newval, out);
1517 } else {
1518 BaseIndex dest(elements, ToRegister(lir->index()),
1519 ScaleFromScalarType(arrayType));
1520 masm.compareExchange64(Synchronization::Full(), dest, oldval, newval, out);
1521 }
1522 }
1523
1524 void CodeGenerator::visitAtomicExchangeTypedArrayElement64(
1525 LAtomicExchangeTypedArrayElement64* lir) {
1526 Register elements = ToRegister(lir->elements());
1527 Register64 value = ToRegister64(lir->value());
1528 Register64 out = ToOutRegister64(lir);
1529
1530 Scalar::Type arrayType = lir->mir()->arrayType();
1531
1532 if (lir->index()->isConstant()) {
1533 Address dest = ToAddress(elements, lir->index(), arrayType);
1534 masm.atomicExchange64(Synchronization::Full(), dest, value, out);
1535 } else {
1536 BaseIndex dest(elements, ToRegister(lir->index()),
1537 ScaleFromScalarType(arrayType));
1538 masm.atomicExchange64(Synchronization::Full(), dest, value, out);
1539 }
1540 }
1541
1542 void CodeGenerator::visitAtomicTypedArrayElementBinop64(
1543 LAtomicTypedArrayElementBinop64* lir) {
1544 MOZ_ASSERT(!lir->mir()->isForEffect());
1545
1546 Register elements = ToRegister(lir->elements());
1547 Register64 value = ToRegister64(lir->value());
1548 Register64 temp = ToRegister64(lir->temp0());
1549 Register64 out = ToOutRegister64(lir);
1550
1551 Scalar::Type arrayType = lir->mir()->arrayType();
1552 AtomicOp atomicOp = lir->mir()->operation();
1553
1554 if (lir->index()->isConstant()) {
1555 Address dest = ToAddress(elements, lir->index(), arrayType);
1556 masm.atomicFetchOp64(Synchronization::Full(), atomicOp, value, dest, temp,
1557 out);
1558 } else {
1559 BaseIndex dest(elements, ToRegister(lir->index()),
1560 ScaleFromScalarType(arrayType));
1561 masm.atomicFetchOp64(Synchronization::Full(), atomicOp, value, dest, temp,
1562 out);
1563 }
1564 }
1565
1566 void CodeGenerator::visitAtomicTypedArrayElementBinopForEffect64(
1567 LAtomicTypedArrayElementBinopForEffect64* lir) {
1568 MOZ_ASSERT(lir->mir()->isForEffect());
1569
1570 Register elements = ToRegister(lir->elements());
1571 Register64 value = ToRegister64(lir->value());
1572 Register64 temp = ToRegister64(lir->temp0());
1573
1574 Scalar::Type arrayType = lir->mir()->arrayType();
1575 AtomicOp atomicOp = lir->mir()->operation();
1576
1577 if (lir->index()->isConstant()) {
1578 Address dest = ToAddress(elements, lir->index(), arrayType);
1579 masm.atomicEffectOp64(Synchronization::Full(), atomicOp, value, dest, temp);
1580 } else {
1581 BaseIndex dest(elements, ToRegister(lir->index()),
1582 ScaleFromScalarType(arrayType));
1583 masm.atomicEffectOp64(Synchronization::Full(), atomicOp, value, dest, temp);
1584 }
1585 }
1586
1587 void CodeGenerator::visitWasmSelect(LWasmSelect* ins) {
1588 MIRType mirType = ins->mir()->type();
1589
1590 Register cond = ToRegister(ins->condExpr());
1591 masm.as_cmp(cond, Imm8(0));
1592
1593 if (mirType == MIRType::Int32 || mirType == MIRType::WasmAnyRef) {
1594 Register falseExpr = ToRegister(ins->falseExpr());
1595 Register out = ToRegister(ins->output());
1596 MOZ_ASSERT(ToRegister(ins->trueExpr()) == out,
1597 "true expr input is reused for output");
1598 masm.ma_mov(falseExpr, out, LeaveCC, Assembler::Zero);
1599 return;
1600 }
1601
1602 FloatRegister out = ToFloatRegister(ins->output());
1603 MOZ_ASSERT(ToFloatRegister(ins->trueExpr()) == out,
1604 "true expr input is reused for output");
1605
1606 FloatRegister falseExpr = ToFloatRegister(ins->falseExpr());
1607
1608 if (mirType == MIRType::Double) {
1609 masm.moveDouble(falseExpr, out, Assembler::Zero);
1610 } else if (mirType == MIRType::Float32) {
1611 masm.moveFloat32(falseExpr, out, Assembler::Zero);
1612 } else {
1613 MOZ_CRASH("unhandled type in visitWasmSelect!");
1614 }
1615 }
1616
1617 // We expect to handle only the case where compare is {U,}Int32 and select is
1618 // {U,}Int32, and the "true" input is reused for the output.
1619 void CodeGenerator::visitWasmCompareAndSelect(LWasmCompareAndSelect* ins) {
1620 bool cmpIs32bit = ins->compareType() == MCompare::Compare_Int32 ||
1621 ins->compareType() == MCompare::Compare_UInt32;
1622 bool selIs32bit = ins->mir()->type() == MIRType::Int32;
1623
1624 MOZ_RELEASE_ASSERT(
1625 cmpIs32bit && selIs32bit,
1626 "CodeGenerator::visitWasmCompareAndSelect: unexpected types");
1627
1628 Register trueExprAndDest = ToRegister(ins->output());
1629 MOZ_ASSERT(ToRegister(ins->ifTrueExpr()) == trueExprAndDest,
1630 "true expr input is reused for output");
1631
1632 Assembler::Condition cond = Assembler::InvertCondition(
1633 JSOpToCondition(ins->compareType(), ins->jsop()));
1634 const LAllocation* rhs = ins->rightExpr();
1635 const LAllocation* falseExpr = ins->ifFalseExpr();
1636 Register lhs = ToRegister(ins->leftExpr());
1637
1638 masm.cmp32Move32(cond, lhs, ToRegister(rhs), ToRegister(falseExpr),
1639 trueExprAndDest);
1640 }
1641
1642 void CodeGenerator::visitAsmJSLoadHeap(LAsmJSLoadHeap* ins) {
1643 const MAsmJSLoadHeap* mir = ins->mir();
1644
1645 const LAllocation* ptr = ins->ptr();
1646 const LAllocation* boundsCheckLimit = ins->boundsCheckLimit();
1647
1648 Scalar::Type accessType = mir->access().type();
1649 bool isSigned = Scalar::isSignedIntType(accessType);
1650 int size = Scalar::byteSize(accessType) * 8;
1651 bool isFloat = Scalar::isFloatingType(accessType);
1652
1653 if (ptr->isConstant()) {
1654 MOZ_ASSERT(!mir->needsBoundsCheck());
1655 int32_t ptrImm = ptr->toConstant()->toInt32();
1656 MOZ_ASSERT(ptrImm >= 0);
1657 if (isFloat) {
1658 ScratchRegisterScope scratch(masm);
1659 VFPRegister vd(ToFloatRegister(ins->output()));
1660 if (size == 32) {
1661 masm.ma_vldr(Address(HeapReg, ptrImm), vd.singleOverlay(), scratch,
1662 Assembler::Always);
1663 } else {
1664 masm.ma_vldr(Address(HeapReg, ptrImm), vd, scratch, Assembler::Always);
1665 }
1666 } else {
1667 ScratchRegisterScope scratch(masm);
1668 masm.ma_dataTransferN(IsLoad, size, isSigned, HeapReg, Imm32(ptrImm),
1669 ToRegister(ins->output()), scratch, Offset,
1670 Assembler::Always);
1671 }
1672 } else {
1673 Register ptrReg = ToRegister(ptr);
1674 if (isFloat) {
1675 FloatRegister output = ToFloatRegister(ins->output());
1676 if (size == 32) {
1677 output = output.singleOverlay();
1678 }
1679
1680 Assembler::Condition cond = Assembler::Always;
1681 if (mir->needsBoundsCheck()) {
1682 Register boundsCheckLimitReg = ToRegister(boundsCheckLimit);
1683 masm.as_cmp(ptrReg, O2Reg(boundsCheckLimitReg));
1684 if (size == 32) {
1685 masm.ma_vimm_f32(GenericNaN(), output, Assembler::AboveOrEqual);
1686 } else {
1687 masm.ma_vimm(GenericNaN(), output, Assembler::AboveOrEqual);
1688 }
1689 cond = Assembler::Below;
1690 }
1691
1692 ScratchRegisterScope scratch(masm);
1693 masm.ma_vldr(output, HeapReg, ptrReg, scratch, 0, cond);
1694 } else {
1695 Register output = ToRegister(ins->output());
1696
1697 Assembler::Condition cond = Assembler::Always;
1698 if (mir->needsBoundsCheck()) {
1699 Register boundsCheckLimitReg = ToRegister(boundsCheckLimit);
1700 masm.as_cmp(ptrReg, O2Reg(boundsCheckLimitReg));
1701 masm.ma_mov(Imm32(0), output, Assembler::AboveOrEqual);
1702 cond = Assembler::Below;
1703 }
1704
1705 ScratchRegisterScope scratch(masm);
1706 masm.ma_dataTransferN(IsLoad, size, isSigned, HeapReg, ptrReg, output,
1707 scratch, Offset, cond);
1708 }
1709 }
1710 }
1711
1712 template <typename T>
1713 void CodeGeneratorARM::emitWasmLoad(T* lir) {
1714 const MWasmLoad* mir = lir->mir();
1715 MIRType resultType = mir->type();
1716 Register ptr;
1717 Register memoryBase = ToRegister(lir->memoryBase());
1718
1719 if (mir->access().offset32() || mir->access().type() == Scalar::Int64) {
1720 ptr = ToRegister(lir->temp0());
1721 } else {
1722 MOZ_ASSERT(lir->temp0()->isBogusTemp());
1723 ptr = ToRegister(lir->ptr());
1724 }
1725
1726 if (resultType == MIRType::Int64) {
1727 masm.wasmLoadI64(mir->access(), memoryBase, ptr, ptr, ToOutRegister64(lir));
1728 } else {
1729 masm.wasmLoad(mir->access(), memoryBase, ptr, ptr,
1730 ToAnyRegister(lir->output()));
1731 }
1732 }
1733
1734 void CodeGenerator::visitWasmLoad(LWasmLoad* lir) { emitWasmLoad(lir); }
1735
1736 void CodeGenerator::visitWasmLoadI64(LWasmLoadI64* lir) { emitWasmLoad(lir); }
1737
1738 void CodeGenerator::visitWasmAddOffset(LWasmAddOffset* lir) {
1739 MWasmAddOffset* mir = lir->mir();
1740 Register base = ToRegister(lir->base());
1741 Register out = ToRegister(lir->output());
1742
1743 ScratchRegisterScope scratch(masm);
1744 masm.ma_add(base, Imm32(mir->offset()), out, scratch, SetCC);
1745 auto* ool = new (alloc()) LambdaOutOfLineCode([=](OutOfLineCode& ool) {
1746 masm.wasmTrap(wasm::Trap::OutOfBounds, mir->trapSiteDesc());
1747 });
1748 addOutOfLineCode(ool, mir);
1749 masm.ma_b(ool->entry(), Assembler::CarrySet);
1750 }
1751
1752 void CodeGenerator::visitWasmAddOffset64(LWasmAddOffset64* lir) {
1753 MWasmAddOffset* mir = lir->mir();
1754 Register64 base = ToRegister64(lir->base());
1755 Register64 out = ToOutRegister64(lir);
1756 MOZ_ASSERT(base.low != out.high && base.high != out.low);
1757
1758 ScratchRegisterScope scratch(masm);
1759 masm.ma_add(base.low, Imm32(mir->offset()), out.low, scratch, SetCC);
1760 masm.ma_adc(base.high, Imm32(mir->offset() >> 32), out.high, scratch, SetCC);
1761 auto* ool = new (alloc()) LambdaOutOfLineCode([=](OutOfLineCode& ool) {
1762 masm.wasmTrap(wasm::Trap::OutOfBounds, mir->trapSiteDesc());
1763 });
1764 addOutOfLineCode(ool, mir);
1765 masm.ma_b(ool->entry(), Assembler::CarrySet);
1766 }
1767
1768 template <typename T>
1769 void CodeGeneratorARM::emitWasmStore(T* lir) {
1770 const MWasmStore* mir = lir->mir();
1771 Register memoryBase = ToRegister(lir->memoryBase());
1772
1773 if constexpr (std::is_same_v<T, LWasmStoreI64>) {
1774 Register64 value = ToRegister64(lir->value());
1775 Register ptr = ToRegister(lir->temp0());
1776 masm.wasmStoreI64(mir->access(), value, memoryBase, ptr, ptr);
1777 } else {
1778 // Maybe add the offset.
1779 Register ptr;
1780 if (mir->access().offset32()) {
1781 ptr = ToRegister(lir->temp0());
1782 } else {
1783 MOZ_ASSERT(lir->temp0()->isBogusTemp());
1784 ptr = ToRegister(lir->ptr());
1785 }
1786
1787 masm.wasmStore(mir->access(), ToAnyRegister(lir->value()), memoryBase, ptr,
1788 ptr);
1789 }
1790 }
1791
1792 void CodeGenerator::visitWasmStore(LWasmStore* lir) { emitWasmStore(lir); }
1793
1794 void CodeGenerator::visitWasmStoreI64(LWasmStoreI64* lir) {
1795 emitWasmStore(lir);
1796 }
1797
1798 void CodeGenerator::visitAsmJSStoreHeap(LAsmJSStoreHeap* ins) {
1799 const MAsmJSStoreHeap* mir = ins->mir();
1800
1801 const LAllocation* ptr = ins->ptr();
1802 const LAllocation* boundsCheckLimit = ins->boundsCheckLimit();
1803
1804 Scalar::Type accessType = mir->access().type();
1805 bool isSigned = accessType == Scalar::Int32 || accessType == Scalar::Uint32;
1806 int size = Scalar::byteSize(accessType) * 8;
1807 bool isFloat = Scalar::isFloatingType(accessType);
1808
1809 if (ptr->isConstant()) {
1810 MOZ_ASSERT(!mir->needsBoundsCheck());
1811 int32_t ptrImm = ptr->toConstant()->toInt32();
1812 MOZ_ASSERT(ptrImm >= 0);
1813 if (isFloat) {
1814 VFPRegister vd(ToFloatRegister(ins->value()));
1815 Address addr(HeapReg, ptrImm);
1816 if (size == 32) {
1817 masm.storeFloat32(vd, addr);
1818 } else {
1819 masm.storeDouble(vd, addr);
1820 }
1821 } else {
1822 ScratchRegisterScope scratch(masm);
1823 masm.ma_dataTransferN(IsStore, size, isSigned, HeapReg, Imm32(ptrImm),
1824 ToRegister(ins->value()), scratch, Offset,
1825 Assembler::Always);
1826 }
1827 } else {
1828 Register ptrReg = ToRegister(ptr);
1829
1830 Assembler::Condition cond = Assembler::Always;
1831 if (mir->needsBoundsCheck()) {
1832 Register boundsCheckLimitReg = ToRegister(boundsCheckLimit);
1833 masm.as_cmp(ptrReg, O2Reg(boundsCheckLimitReg));
1834 cond = Assembler::Below;
1835 }
1836
1837 if (isFloat) {
1838 ScratchRegisterScope scratch(masm);
1839 FloatRegister value = ToFloatRegister(ins->value());
1840 if (size == 32) {
1841 value = value.singleOverlay();
1842 }
1843
1844 masm.ma_vstr(value, HeapReg, ptrReg, scratch, 0, Assembler::Below);
1845 } else {
1846 ScratchRegisterScope scratch(masm);
1847 Register value = ToRegister(ins->value());
1848 masm.ma_dataTransferN(IsStore, size, isSigned, HeapReg, ptrReg, value,
1849 scratch, Offset, cond);
1850 }
1851 }
1852 }
1853
1854 void CodeGenerator::visitWasmCompareExchangeHeap(
1855 LWasmCompareExchangeHeap* ins) {
1856 MWasmCompareExchangeHeap* mir = ins->mir();
1857
1858 const LAllocation* ptr = ins->ptr();
1859 Register ptrReg = ToRegister(ptr);
1860 Register memoryBase = ToRegister(ins->memoryBase());
1861 BaseIndex srcAddr(memoryBase, ptrReg, TimesOne, mir->access().offset32());
1862
1863 Register oldval = ToRegister(ins->oldValue());
1864 Register newval = ToRegister(ins->newValue());
1865 Register out = ToRegister(ins->output());
1866
1867 masm.wasmCompareExchange(mir->access(), srcAddr, oldval, newval, out);
1868 }
1869
1870 void CodeGenerator::visitWasmAtomicExchangeHeap(LWasmAtomicExchangeHeap* ins) {
1871 MWasmAtomicExchangeHeap* mir = ins->mir();
1872
1873 Register ptrReg = ToRegister(ins->ptr());
1874 Register value = ToRegister(ins->value());
1875 Register memoryBase = ToRegister(ins->memoryBase());
1876 Register output = ToRegister(ins->output());
1877 BaseIndex srcAddr(memoryBase, ptrReg, TimesOne, mir->access().offset32());
1878
1879 masm.wasmAtomicExchange(mir->access(), srcAddr, value, output);
1880 }
1881
1882 void CodeGenerator::visitWasmAtomicBinopHeap(LWasmAtomicBinopHeap* ins) {
1883 MWasmAtomicBinopHeap* mir = ins->mir();
1884 MOZ_ASSERT(mir->hasUses());
1885
1886 Register ptrReg = ToRegister(ins->ptr());
1887 Register memoryBase = ToRegister(ins->memoryBase());
1888 Register flagTemp = ToRegister(ins->temp0());
1889 Register output = ToRegister(ins->output());
1890 const LAllocation* value = ins->value();
1891 AtomicOp op = mir->operation();
1892
1893 BaseIndex srcAddr(memoryBase, ptrReg, TimesOne, mir->access().offset32());
1894 masm.wasmAtomicFetchOp(mir->access(), op, ToRegister(value), srcAddr,
1895 flagTemp, output);
1896 }
1897
1898 void CodeGenerator::visitWasmAtomicBinopHeapForEffect(
1899 LWasmAtomicBinopHeapForEffect* ins) {
1900 MWasmAtomicBinopHeap* mir = ins->mir();
1901 MOZ_ASSERT(!mir->hasUses());
1902
1903 Register ptrReg = ToRegister(ins->ptr());
1904 Register memoryBase = ToRegister(ins->memoryBase());
1905 Register flagTemp = ToRegister(ins->temp0());
1906 const LAllocation* value = ins->value();
1907 AtomicOp op = mir->operation();
1908
1909 BaseIndex srcAddr(memoryBase, ptrReg, TimesOne, mir->access().offset32());
1910 masm.wasmAtomicEffectOp(mir->access(), op, ToRegister(value), srcAddr,
1911 flagTemp);
1912 }
1913
1914 void CodeGenerator::visitWasmStackArg(LWasmStackArg* ins) {
1915 const MWasmStackArg* mir = ins->mir();
1916 Address dst(StackPointer, mir->spOffset());
1917 ScratchRegisterScope scratch(masm);
1918 SecondScratchRegisterScope scratch2(masm);
1919
1920 if (ins->arg()->isConstant()) {
1921 masm.ma_mov(Imm32(ToInt32(ins->arg())), scratch);
1922 masm.ma_str(scratch, dst, scratch2);
1923 } else {
1924 if (ins->arg()->isGeneralReg()) {
1925 masm.ma_str(ToRegister(ins->arg()), dst, scratch);
1926 } else {
1927 masm.ma_vstr(ToFloatRegister(ins->arg()), dst, scratch);
1928 }
1929 }
1930 }
1931
1932 void CodeGenerator::visitUDiv(LUDiv* ins) {
1933 Register lhs = ToRegister(ins->lhs());
1934 Register rhs = ToRegister(ins->rhs());
1935 Register output = ToRegister(ins->output());
1936
1937 Label done;
1938 generateUDivModZeroCheck(rhs, output, &done, ins->snapshot(), ins->mir());
1939
1940 masm.ma_udiv(lhs, rhs, output);
1941
1942 // Check for large unsigned result - represent as double.
1943 if (!ins->mir()->isTruncated()) {
1944 MOZ_ASSERT(ins->mir()->fallible());
1945 masm.as_cmp(output, Imm8(0));
1946 bailoutIf(Assembler::LessThan, ins->snapshot());
1947 }
1948
1949 // Check for non-zero remainder if not truncating to int.
1950 if (!ins->mir()->canTruncateRemainder()) {
1951 MOZ_ASSERT(ins->mir()->fallible());
1952 {
1953 ScratchRegisterScope scratch(masm);
1954 masm.ma_mul(rhs, output, scratch);
1955 masm.ma_cmp(scratch, lhs);
1956 }
1957 bailoutIf(Assembler::NotEqual, ins->snapshot());
1958 }
1959
1960 if (done.used()) {
1961 masm.bind(&done);
1962 }
1963 }
1964
1965 void CodeGenerator::visitUMod(LUMod* ins) {
1966 Register lhs = ToRegister(ins->lhs());
1967 Register rhs = ToRegister(ins->rhs());
1968 Register output = ToRegister(ins->output());
1969
1970 Label done;
1971 generateUDivModZeroCheck(rhs, output, &done, ins->snapshot(), ins->mir());
1972
1973 {
1974 ScratchRegisterScope scratch(masm);
1975 masm.ma_umod(lhs, rhs, output, scratch);
1976 }
1977
1978 // Check for large unsigned result - represent as double.
1979 if (!ins->mir()->isTruncated()) {
1980 MOZ_ASSERT(ins->mir()->fallible());
1981 masm.as_cmp(output, Imm8(0));
1982 bailoutIf(Assembler::LessThan, ins->snapshot());
1983 }
1984
1985 if (done.used()) {
1986 masm.bind(&done);
1987 }
1988 }
1989
1990 template <class T>
1991 void CodeGeneratorARM::generateUDivModZeroCheck(Register rhs, Register output,
1992 Label* done,
1993 LSnapshot* snapshot, T* mir) {
1994 if (!mir) {
1995 return;
1996 }
1997 if (mir->canBeDivideByZero()) {
1998 masm.as_cmp(rhs, Imm8(0));
1999 if (mir->isTruncated()) {
2000 if (mir->trapOnError()) {
2001 Label nonZero;
2002 masm.ma_b(&nonZero, Assembler::NotEqual);
2003 masm.wasmTrap(wasm::Trap::IntegerDivideByZero, mir->trapSiteDesc());
2004 masm.bind(&nonZero);
2005 } else {
2006 Label skip;
2007 masm.ma_b(&skip, Assembler::NotEqual);
2008 // Infinity|0 == 0
2009 masm.ma_mov(Imm32(0), output);
2010 masm.ma_b(done);
2011 masm.bind(&skip);
2012 }
2013 } else {
2014 // Bailout for divide by zero
2015 MOZ_ASSERT(mir->fallible());
2016 bailoutIf(Assembler::Equal, snapshot);
2017 }
2018 }
2019 }
2020
2021 void CodeGenerator::visitSoftUDivOrMod(LSoftUDivOrMod* ins) {
2022 Register lhs = ToRegister(ins->lhs());
2023 Register rhs = ToRegister(ins->rhs());
2024 Register output = ToRegister(ins->output());
2025
2026 MOZ_ASSERT(lhs == r0);
2027 MOZ_ASSERT(rhs == r1);
2028 MOZ_ASSERT(output == r0);
2029
2030 Label done;
2031 MDiv* div = ins->mir()->isDiv() ? ins->mir()->toDiv() : nullptr;
2032 MMod* mod = !div ? ins->mir()->toMod() : nullptr;
2033
2034 generateUDivModZeroCheck(rhs, output, &done, ins->snapshot(), div);
2035 generateUDivModZeroCheck(rhs, output, &done, ins->snapshot(), mod);
2036
2037 if (gen->compilingWasm()) {
2038 masm.Push(InstanceReg);
2039 int32_t framePushedAfterInstance = masm.framePushed();
2040 masm.setupWasmABICall();
2041 masm.passABIArg(lhs);
2042 masm.passABIArg(rhs);
2043 wasm::BytecodeOffset bytecodeOffset =
2044 (div ? div->trapSiteDesc().bytecodeOffset
2045 : mod->trapSiteDesc().bytecodeOffset);
2046 int32_t instanceOffset = masm.framePushed() - framePushedAfterInstance;
2047 masm.callWithABI(bytecodeOffset, wasm::SymbolicAddress::aeabi_uidivmod,
2048 mozilla::Some(instanceOffset));
2049 masm.Pop(InstanceReg);
2050 } else {
2051 using Fn = int64_t (*)(int, int);
2052 masm.setupAlignedABICall();
2053 masm.passABIArg(lhs);
2054 masm.passABIArg(rhs);
2055 masm.callWithABI<Fn, __aeabi_uidivmod>(
2056 ABIType::Int64, CheckUnsafeCallWithABI::DontCheckOther);
2057 }
2058
2059 if (mod) {
2060 MOZ_ASSERT(output == r0, "output should not be r1 for mod");
2061 masm.move32(r1, output);
2062 }
2063
2064 // uidivmod returns the quotient in r0, and the remainder in r1.
2065 if (div && !div->canTruncateRemainder()) {
2066 MOZ_ASSERT(div->fallible());
2067 masm.as_cmp(r1, Imm8(0));
2068 bailoutIf(Assembler::NonZero, ins->snapshot());
2069 }
2070
2071 // Bailout for big unsigned results
2072 if ((div && !div->isTruncated()) || (mod && !mod->isTruncated())) {
2073 DebugOnly<bool> isFallible =
2074 (div && div->fallible()) || (mod && mod->fallible());
2075 MOZ_ASSERT(isFallible);
2076 masm.as_cmp(output, Imm8(0));
2077 bailoutIf(Assembler::LessThan, ins->snapshot());
2078 }
2079
2080 masm.bind(&done);
2081 }
2082
2083 void CodeGenerator::visitEffectiveAddress(LEffectiveAddress* ins) {
2084 const MEffectiveAddress* mir = ins->mir();
2085 Register base = ToRegister(ins->base());
2086 Register index = ToRegister(ins->index());
2087 Register output = ToRegister(ins->output());
2088
2089 ScratchRegisterScope scratch(masm);
2090
2091 masm.as_add(output, base, lsl(index, mir->scale()));
2092 masm.ma_add(Imm32(mir->displacement()), output, scratch);
2093 }
2094
2095 void CodeGenerator::visitNegI(LNegI* ins) {
2096 Register input = ToRegister(ins->input());
2097 masm.ma_neg(input, ToRegister(ins->output()));
2098 }
2099
2100 void CodeGenerator::visitNegI64(LNegI64* ins) {
2101 Register64 input = ToRegister64(ins->input());
2102 MOZ_ASSERT(input == ToOutRegister64(ins));
2103 masm.neg64(input);
2104 }
2105
2106 void CodeGenerator::visitNegD(LNegD* ins) {
2107 FloatRegister input = ToFloatRegister(ins->input());
2108 masm.ma_vneg(input, ToFloatRegister(ins->output()));
2109 }
2110
2111 void CodeGenerator::visitNegF(LNegF* ins) {
2112 FloatRegister input = ToFloatRegister(ins->input());
2113 masm.ma_vneg_f32(input, ToFloatRegister(ins->output()));
2114 }
2115
2116 void CodeGenerator::visitWasmTruncateToInt32(LWasmTruncateToInt32* lir) {
2117 auto input = ToFloatRegister(lir->input());
2118 auto output = ToRegister(lir->output());
2119
2120 MWasmTruncateToInt32* mir = lir->mir();
2121 MIRType fromType = mir->input()->type();
2122
2123 OutOfLineWasmTruncateCheck* ool = nullptr;
2124 Label* oolEntry = nullptr;
2125 if (!lir->mir()->isSaturating()) {
2126 ool = new (alloc())
2127 OutOfLineWasmTruncateCheck(mir, input, Register::Invalid());
2128 addOutOfLineCode(ool, mir);
2129 oolEntry = ool->entry();
2130 }
2131
2132 masm.wasmTruncateToInt32(input, output, fromType, mir->isUnsigned(),
2133 mir->isSaturating(), oolEntry);
2134
2135 if (!lir->mir()->isSaturating()) {
2136 masm.bind(ool->rejoin());
2137 }
2138 }
2139
2140 void CodeGenerator::visitWasmTruncateToInt64(LWasmTruncateToInt64* lir) {
2141 MOZ_ASSERT(gen->compilingWasm());
2142 MOZ_ASSERT(ToRegister(lir->instance()) == InstanceReg);
2143 masm.Push(InstanceReg);
2144 int32_t framePushedAfterInstance = masm.framePushed();
2145
2146 FloatRegister input = ToFloatRegister(lir->input());
2147 FloatRegister inputDouble = input;
2148 Register64 output = ToOutRegister64(lir);
2149
2150 MWasmBuiltinTruncateToInt64* mir = lir->mir();
2151 MIRType fromType = mir->input()->type();
2152
2153 OutOfLineWasmTruncateCheck* ool = nullptr;
2154 if (!lir->mir()->isSaturating()) {
2155 ool = new (alloc())
2156 OutOfLineWasmTruncateCheck(mir, input, Register64::Invalid());
2157 addOutOfLineCode(ool, mir);
2158 }
2159
2160 ScratchDoubleScope fpscratch(masm);
2161 if (fromType == MIRType::Float32) {
2162 inputDouble = fpscratch;
2163 masm.convertFloat32ToDouble(input, inputDouble);
2164 }
2165
2166 masm.Push(input);
2167
2168 masm.setupWasmABICall();
2169 masm.passABIArg(inputDouble, ABIType::Float64);
2170
2171 int32_t instanceOffset = masm.framePushed() - framePushedAfterInstance;
2172 if (lir->mir()->isSaturating()) {
2173 if (lir->mir()->isUnsigned()) {
2174 masm.callWithABI(mir->trapSiteDesc().bytecodeOffset,
2175 wasm::SymbolicAddress::SaturatingTruncateDoubleToUint64,
2176 mozilla::Some(instanceOffset));
2177 } else {
2178 masm.callWithABI(mir->trapSiteDesc().bytecodeOffset,
2179 wasm::SymbolicAddress::SaturatingTruncateDoubleToInt64,
2180 mozilla::Some(instanceOffset));
2181 }
2182 } else {
2183 if (lir->mir()->isUnsigned()) {
2184 masm.callWithABI(mir->trapSiteDesc().bytecodeOffset,
2185 wasm::SymbolicAddress::TruncateDoubleToUint64,
2186 mozilla::Some(instanceOffset));
2187 } else {
2188 masm.callWithABI(mir->trapSiteDesc().bytecodeOffset,
2189 wasm::SymbolicAddress::TruncateDoubleToInt64,
2190 mozilla::Some(instanceOffset));
2191 }
2192 }
2193
2194 masm.Pop(input);
2195 masm.Pop(InstanceReg);
2196
2197 // TruncateDoubleTo{UI,I}nt64 returns 0x8000000000000000 to indicate
2198 // exceptional results, so check for that and produce the appropriate
2199 // traps. The Saturating form always returns a normal value and never
2200 // needs traps.
2201 if (!lir->mir()->isSaturating()) {
2202 ScratchRegisterScope scratch(masm);
2203 masm.ma_cmp(output.high, Imm32(0x80000000), scratch);
2204 masm.as_cmp(output.low, Imm8(0x00000000), Assembler::Equal);
2205 masm.ma_b(ool->entry(), Assembler::Equal);
2206
2207 masm.bind(ool->rejoin());
2208 }
2209
2210 MOZ_ASSERT(ReturnReg64 == output);
2211 }
2212
2213 void CodeGeneratorARM::visitOutOfLineWasmTruncateCheck(
2214 OutOfLineWasmTruncateCheck* ool) {
2215 // On ARM, saturating truncation codegen handles saturating itself rather than
2216 // relying on out-of-line fixup code.
2217 if (ool->isSaturating()) {
2218 return;
2219 }
2220
2221 masm.outOfLineWasmTruncateToIntCheck(ool->input(), ool->fromType(),
2222 ool->toType(), ool->isUnsigned(),
2223 ool->rejoin(), ool->trapSiteDesc());
2224 }
2225
2226 void CodeGenerator::visitInt64ToFloatingPointCall(
2227 LInt64ToFloatingPointCall* lir) {
2228 MOZ_ASSERT(gen->compilingWasm());
2229 MOZ_ASSERT(ToRegister(lir->instance()) == InstanceReg);
2230 masm.Push(InstanceReg);
2231 int32_t framePushedAfterInstance = masm.framePushed();
2232
2233 Register64 input = ToRegister64(lir->input());
2234
2235 MBuiltinInt64ToFloatingPoint* mir = lir->mir();
2236 MIRType toType = mir->type();
2237
2238 masm.setupWasmABICall();
2239 masm.passABIArg(input.high);
2240 masm.passABIArg(input.low);
2241
2242 bool isUnsigned = mir->isUnsigned();
2243 wasm::SymbolicAddress callee =
2244 toType == MIRType::Float32
2245 ? (isUnsigned ? wasm::SymbolicAddress::Uint64ToFloat32
2246 : wasm::SymbolicAddress::Int64ToFloat32)
2247 : (isUnsigned ? wasm::SymbolicAddress::Uint64ToDouble
2248 : wasm::SymbolicAddress::Int64ToDouble);
2249
2250 int32_t instanceOffset = masm.framePushed() - framePushedAfterInstance;
2251 ABIType result =
2252 toType == MIRType::Float32 ? ABIType::Float32 : ABIType::Float64;
2253 masm.callWithABI(mir->bytecodeOffset(), callee, mozilla::Some(instanceOffset),
2254 result);
2255
2256 DebugOnly<FloatRegister> output(ToFloatRegister(lir->output()));
2257 MOZ_ASSERT_IF(toType == MIRType::Double, output.value == ReturnDoubleReg);
2258 MOZ_ASSERT_IF(toType == MIRType::Float32, output.value == ReturnFloat32Reg);
2259
2260 masm.Pop(InstanceReg);
2261 }
2262
2263 void CodeGenerator::visitCopySignF(LCopySignF* ins) {
2264 FloatRegister lhs = ToFloatRegister(ins->lhs());
2265 FloatRegister rhs = ToFloatRegister(ins->rhs());
2266 FloatRegister output = ToFloatRegister(ins->output());
2267
2268 Register lhsi = ToRegister(ins->temp0());
2269 Register rhsi = ToRegister(ins->temp1());
2270
2271 masm.ma_vxfer(lhs, lhsi);
2272 masm.ma_vxfer(rhs, rhsi);
2273
2274 ScratchRegisterScope scratch(masm);
2275
2276 // Clear lhs's sign.
2277 masm.ma_and(Imm32(INT32_MAX), lhsi, lhsi, scratch);
2278
2279 // Keep rhs's sign.
2280 masm.ma_and(Imm32(INT32_MIN), rhsi, rhsi, scratch);
2281
2282 // Combine.
2283 masm.ma_orr(lhsi, rhsi, rhsi);
2284
2285 masm.ma_vxfer(rhsi, output);
2286 }
2287
2288 void CodeGenerator::visitCopySignD(LCopySignD* ins) {
2289 FloatRegister lhs = ToFloatRegister(ins->lhs());
2290 FloatRegister rhs = ToFloatRegister(ins->rhs());
2291 FloatRegister output = ToFloatRegister(ins->output());
2292
2293 Register lhsi = ToRegister(ins->temp0());
2294 Register rhsi = ToRegister(ins->temp1());
2295
2296 // Manipulate high words of double inputs.
2297 masm.as_vxfer(lhsi, InvalidReg, lhs, Assembler::FloatToCore,
2298 Assembler::Always, 1);
2299 masm.as_vxfer(rhsi, InvalidReg, rhs, Assembler::FloatToCore,
2300 Assembler::Always, 1);
2301
2302 ScratchRegisterScope scratch(masm);
2303
2304 // Clear lhs's sign.
2305 masm.ma_and(Imm32(INT32_MAX), lhsi, lhsi, scratch);
2306
2307 // Keep rhs's sign.
2308 masm.ma_and(Imm32(INT32_MIN), rhsi, rhsi, scratch);
2309
2310 // Combine.
2311 masm.ma_orr(lhsi, rhsi, rhsi);
2312
2313 // Reconstruct the output.
2314 masm.as_vxfer(lhsi, InvalidReg, lhs, Assembler::FloatToCore,
2315 Assembler::Always, 0);
2316 masm.ma_vxfer(lhsi, rhsi, output);
2317 }
2318
2319 void CodeGenerator::visitWrapInt64ToInt32(LWrapInt64ToInt32* lir) {
2320 LInt64Allocation input = lir->input();
2321 Register output = ToRegister(lir->output());
2322
2323 if (lir->mir()->bottomHalf()) {
2324 masm.move32(ToRegister(input.low()), output);
2325 } else {
2326 masm.move32(ToRegister(input.high()), output);
2327 }
2328 }
2329
2330 void CodeGenerator::visitExtendInt32ToInt64(LExtendInt32ToInt64* lir) {
2331 Register64 output = ToOutRegister64(lir);
2332 MOZ_ASSERT(ToRegister(lir->input()) == output.low);
2333
2334 if (lir->mir()->isUnsigned()) {
2335 masm.ma_mov(Imm32(0), output.high);
2336 } else {
2337 masm.ma_asr(Imm32(31), output.low, output.high);
2338 }
2339 }
2340
2341 void CodeGenerator::visitSignExtendInt64(LSignExtendInt64* lir) {
2342 Register64 input = ToRegister64(lir->input());
2343 Register64 output = ToOutRegister64(lir);
2344 switch (lir->mir()->mode()) {
2345 case MSignExtendInt64::Byte:
2346 masm.move8SignExtend(input.low, output.low);
2347 break;
2348 case MSignExtendInt64::Half:
2349 masm.move16SignExtend(input.low, output.low);
2350 break;
2351 case MSignExtendInt64::Word:
2352 masm.move32(input.low, output.low);
2353 break;
2354 }
2355 masm.ma_asr(Imm32(31), output.low, output.high);
2356 }
2357
2358 void CodeGenerator::visitWasmExtendU32Index(LWasmExtendU32Index*) {
2359 MOZ_CRASH("64-bit only");
2360 }
2361
2362 void CodeGenerator::visitWasmWrapU32Index(LWasmWrapU32Index* lir) {
2363 // Generates no code on this platform because we just return the low part of
2364 // the input register pair.
2365 MOZ_ASSERT(ToRegister(lir->input()) == ToRegister(lir->output()));
2366 }
2367
2368 void CodeGenerator::visitDivOrModI64(LDivOrModI64* lir) {
2369 MOZ_ASSERT(gen->compilingWasm());
2370 MOZ_ASSERT(ToRegister(lir->instance()) == InstanceReg);
2371 masm.Push(InstanceReg);
2372 int32_t framePushedAfterInstance = masm.framePushed();
2373
2374 Register64 lhs = ToRegister64(lir->lhs());
2375 Register64 rhs = ToRegister64(lir->rhs());
2376 Register64 output = ToOutRegister64(lir);
2377
2378 MOZ_ASSERT(output == ReturnReg64);
2379
2380 Label done;
2381
2382 // Handle divide by zero.
2383 if (lir->canBeDivideByZero()) {
2384 Label nonZero;
2385 // We can use InstanceReg as temp register because we preserved it
2386 // before.
2387 masm.branchTest64(Assembler::NonZero, rhs, rhs, InstanceReg, &nonZero);
2388 masm.wasmTrap(wasm::Trap::IntegerDivideByZero, lir->trapSiteDesc());
2389 masm.bind(&nonZero);
2390 }
2391
2392 auto* mir = lir->mir();
2393
2394 // Handle an integer overflow exception from INT64_MIN / -1.
2395 if (lir->canBeNegativeOverflow()) {
2396 Label notmin;
2397 masm.branch64(Assembler::NotEqual, lhs, Imm64(INT64_MIN), &notmin);
2398 masm.branch64(Assembler::NotEqual, rhs, Imm64(-1), &notmin);
2399 if (mir->isWasmBuiltinModI64()) {
2400 masm.xor64(output, output);
2401 } else {
2402 masm.wasmTrap(wasm::Trap::IntegerOverflow, lir->trapSiteDesc());
2403 }
2404 masm.jump(&done);
2405 masm.bind(&notmin);
2406 }
2407
2408 masm.setupWasmABICall();
2409 masm.passABIArg(lhs.high);
2410 masm.passABIArg(lhs.low);
2411 masm.passABIArg(rhs.high);
2412 masm.passABIArg(rhs.low);
2413
2414 int32_t instanceOffset = masm.framePushed() - framePushedAfterInstance;
2415 if (mir->isWasmBuiltinModI64()) {
2416 masm.callWithABI(lir->trapSiteDesc().bytecodeOffset,
2417 wasm::SymbolicAddress::ModI64,
2418 mozilla::Some(instanceOffset));
2419 } else {
2420 masm.callWithABI(lir->trapSiteDesc().bytecodeOffset,
2421 wasm::SymbolicAddress::DivI64,
2422 mozilla::Some(instanceOffset));
2423 }
2424
2425 MOZ_ASSERT(ReturnReg64 == output);
2426
2427 masm.bind(&done);
2428 masm.Pop(InstanceReg);
2429 }
2430
2431 void CodeGenerator::visitUDivOrModI64(LUDivOrModI64* lir) {
2432 MOZ_ASSERT(gen->compilingWasm());
2433 MOZ_ASSERT(ToRegister(lir->instance()) == InstanceReg);
2434 masm.Push(InstanceReg);
2435 int32_t framePushedAfterInstance = masm.framePushed();
2436
2437 Register64 lhs = ToRegister64(lir->lhs());
2438 Register64 rhs = ToRegister64(lir->rhs());
2439
2440 MOZ_ASSERT(ToOutRegister64(lir) == ReturnReg64);
2441
2442 // Prevent divide by zero.
2443 if (lir->canBeDivideByZero()) {
2444 Label nonZero;
2445 // We can use InstanceReg as temp register because we preserved it
2446 // before.
2447 masm.branchTest64(Assembler::NonZero, rhs, rhs, InstanceReg, &nonZero);
2448 masm.wasmTrap(wasm::Trap::IntegerDivideByZero, lir->trapSiteDesc());
2449 masm.bind(&nonZero);
2450 }
2451
2452 masm.setupWasmABICall();
2453 masm.passABIArg(lhs.high);
2454 masm.passABIArg(lhs.low);
2455 masm.passABIArg(rhs.high);
2456 masm.passABIArg(rhs.low);
2457
2458 MDefinition* mir = lir->mir();
2459 int32_t instanceOffset = masm.framePushed() - framePushedAfterInstance;
2460 if (mir->isWasmBuiltinModI64()) {
2461 masm.callWithABI(lir->trapSiteDesc().bytecodeOffset,
2462 wasm::SymbolicAddress::UModI64,
2463 mozilla::Some(instanceOffset));
2464 } else {
2465 masm.callWithABI(lir->trapSiteDesc().bytecodeOffset,
2466 wasm::SymbolicAddress::UDivI64,
2467 mozilla::Some(instanceOffset));
2468 }
2469 masm.Pop(InstanceReg);
2470 }
2471
2472 void CodeGenerator::visitShiftI64(LShiftI64* lir) {
2473 LInt64Allocation lhs = lir->lhs();
2474 const LAllocation* rhs = lir->rhs();
2475
2476 MOZ_ASSERT(ToOutRegister64(lir) == ToRegister64(lhs));
2477
2478 if (rhs->isConstant()) {
2479 int32_t shift = int32_t(rhs->toConstant()->toInt64() & 0x3F);
2480 switch (lir->bitop()) {
2481 case JSOp::Lsh:
2482 if (shift) {
2483 masm.lshift64(Imm32(shift), ToRegister64(lhs));
2484 }
2485 break;
2486 case JSOp::Rsh:
2487 if (shift) {
2488 masm.rshift64Arithmetic(Imm32(shift), ToRegister64(lhs));
2489 }
2490 break;
2491 case JSOp::Ursh:
2492 if (shift) {
2493 masm.rshift64(Imm32(shift), ToRegister64(lhs));
2494 }
2495 break;
2496 default:
2497 MOZ_CRASH("Unexpected shift op");
2498 }
2499 return;
2500 }
2501
2502 switch (lir->bitop()) {
2503 case JSOp::Lsh:
2504 masm.lshift64(ToRegister(rhs), ToRegister64(lhs));
2505 break;
2506 case JSOp::Rsh:
2507 masm.rshift64Arithmetic(ToRegister(rhs), ToRegister64(lhs));
2508 break;
2509 case JSOp::Ursh:
2510 masm.rshift64(ToRegister(rhs), ToRegister64(lhs));
2511 break;
2512 default:
2513 MOZ_CRASH("Unexpected shift op");
2514 }
2515 }
2516
2517 void CodeGenerator::visitBitOpI64(LBitOpI64* lir) {
2518 LInt64Allocation lhs = lir->lhs();
2519 LInt64Allocation rhs = lir->rhs();
2520
2521 MOZ_ASSERT(ToOutRegister64(lir) == ToRegister64(lhs));
2522
2523 switch (lir->bitop()) {
2524 case JSOp::BitOr:
2525 if (IsConstant(rhs)) {
2526 masm.or64(Imm64(ToInt64(rhs)), ToRegister64(lhs));
2527 } else {
2528 masm.or64(ToOperandOrRegister64(rhs), ToRegister64(lhs));
2529 }
2530 break;
2531 case JSOp::BitXor:
2532 if (IsConstant(rhs)) {
2533 masm.xor64(Imm64(ToInt64(rhs)), ToRegister64(lhs));
2534 } else {
2535 masm.xor64(ToOperandOrRegister64(rhs), ToRegister64(lhs));
2536 }
2537 break;
2538 case JSOp::BitAnd:
2539 if (IsConstant(rhs)) {
2540 masm.and64(Imm64(ToInt64(rhs)), ToRegister64(lhs));
2541 } else {
2542 masm.and64(ToOperandOrRegister64(rhs), ToRegister64(lhs));
2543 }
2544 break;
2545 default:
2546 MOZ_CRASH("unexpected binary opcode");
2547 }
2548 }
2549
2550 void CodeGenerator::visitWasmStackArgI64(LWasmStackArgI64* ins) {
2551 const MWasmStackArg* mir = ins->mir();
2552 Address dst(StackPointer, mir->spOffset());
2553 if (IsConstant(ins->arg())) {
2554 masm.store64(Imm64(ToInt64(ins->arg())), dst);
2555 } else {
2556 masm.store64(ToRegister64(ins->arg()), dst);
2557 }
2558 }
2559
2560 void CodeGenerator::visitWasmSelectI64(LWasmSelectI64* lir) {
2561 Register cond = ToRegister(lir->condExpr());
2562 LInt64Allocation falseExpr = lir->falseExpr();
2563
2564 Register64 out = ToOutRegister64(lir);
2565 MOZ_ASSERT(ToRegister64(lir->trueExpr()) == out,
2566 "true expr is reused for input");
2567
2568 masm.as_cmp(cond, Imm8(0));
2569 if (falseExpr.low().isGeneralReg()) {
2570 masm.ma_mov(ToRegister(falseExpr.low()), out.low, LeaveCC,
2571 Assembler::Equal);
2572 masm.ma_mov(ToRegister(falseExpr.high()), out.high, LeaveCC,
2573 Assembler::Equal);
2574 } else {
2575 ScratchRegisterScope scratch(masm);
2576 masm.ma_ldr(ToAddress(falseExpr.low()), out.low, scratch, Offset,
2577 Assembler::Equal);
2578 masm.ma_ldr(ToAddress(falseExpr.high()), out.high, scratch, Offset,
2579 Assembler::Equal);
2580 }
2581 }
2582
2583 void CodeGenerator::visitBitNotI64(LBitNotI64* lir) {
2584 Register64 input = ToRegister64(lir->input());
2585 MOZ_ASSERT(input == ToOutRegister64(lir));
2586 masm.ma_mvn(input.high, input.high);
2587 masm.ma_mvn(input.low, input.low);
2588 }
2589
2590 void CodeGenerator::visitWasmAtomicLoadI64(LWasmAtomicLoadI64* lir) {
2591 Register ptr = ToRegister(lir->ptr());
2592 Register memoryBase = ToRegister(lir->memoryBase());
2593 Register64 output = ToOutRegister64(lir);
2594 Register64 tmp(InvalidReg, InvalidReg);
2595
2596 BaseIndex addr(memoryBase, ptr, TimesOne, lir->mir()->access().offset32());
2597 masm.wasmAtomicLoad64(lir->mir()->access(), addr, tmp, output);
2598 }
2599
2600 void CodeGenerator::visitWasmAtomicStoreI64(LWasmAtomicStoreI64* lir) {
2601 Register ptr = ToRegister(lir->ptr());
2602 Register memoryBase = ToRegister(lir->memoryBase());
2603 Register64 value = ToRegister64(lir->value());
2604 Register64 tmp = ToRegister64(lir->temp0());
2605
2606 BaseIndex addr(memoryBase, ptr, TimesOne, lir->mir()->access().offset32());
2607 masm.wasmAtomicExchange64(lir->mir()->access(), addr, value, tmp);
2608 }
2609
2610 void CodeGenerator::visitWasmCompareExchangeI64(LWasmCompareExchangeI64* lir) {
2611 Register ptr = ToRegister(lir->ptr());
2612 Register64 expected = ToRegister64(lir->expected());
2613 Register64 replacement = ToRegister64(lir->replacement());
2614 Register memoryBase = ToRegister(lir->memoryBase());
2615 Register64 out = ToOutRegister64(lir);
2616
2617 BaseIndex addr(memoryBase, ptr, TimesOne, lir->mir()->access().offset32());
2618 masm.wasmCompareExchange64(lir->mir()->access(), addr, expected, replacement,
2619 out);
2620 }
2621
2622 void CodeGenerator::visitWasmAtomicBinopI64(LWasmAtomicBinopI64* lir) {
2623 Register ptr = ToRegister(lir->ptr());
2624 Register64 value = ToRegister64(lir->value());
2625 Register memoryBase = ToRegister(lir->memoryBase());
2626 Register64 out = ToOutRegister64(lir);
2627
2628 BaseIndex addr(memoryBase, ptr, TimesOne, lir->access().offset32());
2629 Register64 tmp = ToRegister64(lir->temp0());
2630 masm.wasmAtomicFetchOp64(lir->access(), lir->operation(), value, addr, tmp,
2631 out);
2632 }
2633
2634 void CodeGenerator::visitWasmAtomicExchangeI64(LWasmAtomicExchangeI64* lir) {
2635 Register ptr = ToRegister(lir->ptr());
2636 Register64 value = ToRegister64(lir->value());
2637 Register memoryBase = ToRegister(lir->memoryBase());
2638 Register64 out = ToOutRegister64(lir);
2639
2640 BaseIndex addr(memoryBase, ptr, TimesOne, lir->access().offset32());
2641 masm.wasmAtomicExchange64(lir->access(), addr, value, out);
2642 }
2643
2644 void CodeGenerator::visitNearbyInt(LNearbyInt*) { MOZ_CRASH("NYI"); }
2645
2646 void CodeGenerator::visitNearbyIntF(LNearbyIntF*) { MOZ_CRASH("NYI"); }
2647
2648 void CodeGenerator::visitSimd128(LSimd128* ins) { MOZ_CRASH("No SIMD"); }
2649
2650 void CodeGenerator::visitWasmTernarySimd128(LWasmTernarySimd128* ins) {
2651 MOZ_CRASH("No SIMD");
2652 }
2653
2654 void CodeGenerator::visitWasmBinarySimd128(LWasmBinarySimd128* ins) {
2655 MOZ_CRASH("No SIMD");
2656 }
2657
2658 void CodeGenerator::visitWasmBinarySimd128WithConstant(
2659 LWasmBinarySimd128WithConstant* ins) {
2660 MOZ_CRASH("No SIMD");
2661 }
2662
2663 void CodeGenerator::visitWasmVariableShiftSimd128(
2664 LWasmVariableShiftSimd128* ins) {
2665 MOZ_CRASH("No SIMD");
2666 }
2667
2668 void CodeGenerator::visitWasmConstantShiftSimd128(
2669 LWasmConstantShiftSimd128* ins) {
2670 MOZ_CRASH("No SIMD");
2671 }
2672
2673 void CodeGenerator::visitWasmSignReplicationSimd128(
2674 LWasmSignReplicationSimd128* ins) {
2675 MOZ_CRASH("No SIMD");
2676 }
2677
2678 void CodeGenerator::visitWasmShuffleSimd128(LWasmShuffleSimd128* ins) {
2679 MOZ_CRASH("No SIMD");
2680 }
2681
2682 void CodeGenerator::visitWasmPermuteSimd128(LWasmPermuteSimd128* ins) {
2683 MOZ_CRASH("No SIMD");
2684 }
2685
2686 void CodeGenerator::visitWasmReplaceLaneSimd128(LWasmReplaceLaneSimd128* ins) {
2687 MOZ_CRASH("No SIMD");
2688 }
2689
2690 void CodeGenerator::visitWasmReplaceInt64LaneSimd128(
2691 LWasmReplaceInt64LaneSimd128* ins) {
2692 MOZ_CRASH("No SIMD");
2693 }
2694
2695 void CodeGenerator::visitWasmScalarToSimd128(LWasmScalarToSimd128* ins) {
2696 MOZ_CRASH("No SIMD");
2697 }
2698
2699 void CodeGenerator::visitWasmInt64ToSimd128(LWasmInt64ToSimd128* ins) {
2700 MOZ_CRASH("No SIMD");
2701 }
2702
2703 void CodeGenerator::visitWasmUnarySimd128(LWasmUnarySimd128* ins) {
2704 MOZ_CRASH("No SIMD");
2705 }
2706
2707 void CodeGenerator::visitWasmReduceSimd128(LWasmReduceSimd128* ins) {
2708 MOZ_CRASH("No SIMD");
2709 }
2710
2711 void CodeGenerator::visitWasmReduceAndBranchSimd128(
2712 LWasmReduceAndBranchSimd128* ins) {
2713 MOZ_CRASH("No SIMD");
2714 }
2715
2716 void CodeGenerator::visitWasmReduceSimd128ToInt64(
2717 LWasmReduceSimd128ToInt64* ins) {
2718 MOZ_CRASH("No SIMD");
2719 }
2720
2721 void CodeGenerator::visitWasmLoadLaneSimd128(LWasmLoadLaneSimd128* ins) {
2722 MOZ_CRASH("No SIMD");
2723 }
2724
2725 void CodeGenerator::visitWasmStoreLaneSimd128(LWasmStoreLaneSimd128* ins) {
2726 MOZ_CRASH("No SIMD");
2727 }