Debian Codegraph @

js/src/jit/mips-shared/CodeGenerator-mips-shared.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/mips-shared/CodeGenerator-mips-shared.h"
8
9 #include "mozilla/DebugOnly.h"
10 #include "mozilla/MathAlgorithms.h"
11
12 #include "jsnum.h"
13
14 #include "jit/CodeGenerator.h"
15 #include "jit/InlineScriptTree.h"
16 #include "jit/JitRuntime.h"
17 #include "jit/MIR-wasm.h"
18 #include "jit/MIR.h"
19 #include "jit/MIRGraph.h"
20 #include "js/Conversions.h"
21 #include "vm/JSContext.h"
22 #include "vm/Realm.h"
23 #include "vm/Shape.h"
24
25 #include "jit/MacroAssembler-inl.h"
26 #include "jit/shared/CodeGenerator-shared-inl.h"
27 #include "vm/JSScript-inl.h"
28
29 using namespace js;
30 using namespace js::jit;
31
32 using JS::GenericNaN;
33 using JS::ToInt32;
34 using mozilla::DebugOnly;
35 using mozilla::FloorLog2;
36 using mozilla::NegativeInfinity;
37
38 // shared
39 CodeGeneratorMIPSShared::CodeGeneratorMIPSShared(
40 MIRGenerator* gen, LIRGraph* graph, MacroAssembler* masm,
41 const wasm::CodeMetadata* wasmCodeMeta)
42 : CodeGeneratorShared(gen, graph, masm, wasmCodeMeta) {}
43
44 Operand CodeGeneratorMIPSShared::ToOperand(const LAllocation& a) {
45 if (a.isGeneralReg()) {
46 return Operand(a.toGeneralReg()->reg());
47 }
48 if (a.isFloatReg()) {
49 return Operand(a.toFloatReg()->reg());
50 }
51 return Operand(ToAddress(a));
52 }
53
54 Operand CodeGeneratorMIPSShared::ToOperand(const LAllocation* a) {
55 return ToOperand(*a);
56 }
57
58 Operand CodeGeneratorMIPSShared::ToOperand(const LDefinition* def) {
59 return ToOperand(def->output());
60 }
61
62 Operand CodeGeneratorMIPSShared::ToOperandOrRegister64(
63 const LInt64Allocation& input) {
64 return ToOperand(input.value());
65 }
66
67 void CodeGeneratorMIPSShared::branchToBlock(Assembler::FloatFormat fmt,
68 FloatRegister lhs,
69 FloatRegister rhs, MBasicBlock* mir,
70 Assembler::DoubleCondition cond) {
71 // Skip past trivial blocks.
72 Label* label = skipTrivialBlocks(mir)->lir()->label();
73 if (fmt == Assembler::DoubleFloat) {
74 masm.branchDouble(cond, lhs, rhs, label);
75 } else {
76 masm.branchFloat(cond, lhs, rhs, label);
77 }
78 }
79
80 bool CodeGeneratorMIPSShared::generateOutOfLineCode() {
81 if (!CodeGeneratorShared::generateOutOfLineCode()) {
82 return false;
83 }
84
85 if (deoptLabel_.used()) {
86 // All non-table-based bailouts will go here.
87 masm.bind(&deoptLabel_);
88
89 // Push the frame size, so the handler can recover the IonScript.
90 // Frame size is stored in 'ra' and pushed by GenerateBailoutThunk
91 // We have to use 'ra' because generateBailoutTable will implicitly do
92 // the same.
93 masm.move32(Imm32(frameSize()), ra);
94
95 TrampolinePtr handler = gen->jitRuntime()->getGenericBailoutHandler();
96 masm.jump(handler);
97 }
98
99 return !masm.oom();
100 }
101
102 void CodeGeneratorMIPSShared::bailoutFrom(Label* label, LSnapshot* snapshot) {
103 MOZ_ASSERT_IF(!masm.oom(), label->used());
104 MOZ_ASSERT_IF(!masm.oom(), !label->bound());
105
106 encode(snapshot);
107
108 InlineScriptTree* tree = snapshot->mir()->block()->trackedTree();
109 auto* ool = new (alloc()) LambdaOutOfLineCode([=](OutOfLineCode& ool) {
110 // Push snapshotOffset and make sure stack is aligned.
111 masm.subPtr(Imm32(sizeof(Value)), StackPointer);
112 masm.storePtr(ImmWord(snapshot->snapshotOffset()),
113 Address(StackPointer, 0));
114
115 masm.jump(&deoptLabel_);
116 });
117 addOutOfLineCode(ool,
118 new (alloc()) BytecodeSite(tree, tree->script()->code()));
119
120 masm.retarget(label, ool->entry());
121 }
122
123 void CodeGeneratorMIPSShared::bailout(LSnapshot* snapshot) {
124 Label label;
125 masm.jump(&label);
126 bailoutFrom(&label, snapshot);
127 }
128
129 void CodeGenerator::visitMinMaxD(LMinMaxD* ins) {
130 FloatRegister first = ToFloatRegister(ins->first());
131 FloatRegister second = ToFloatRegister(ins->second());
132
133 MOZ_ASSERT(first == ToFloatRegister(ins->output()));
134
135 if (ins->mir()->isMax()) {
136 masm.maxDouble(second, first, true);
137 } else {
138 masm.minDouble(second, first, true);
139 }
140 }
141
142 void CodeGenerator::visitMinMaxF(LMinMaxF* ins) {
143 FloatRegister first = ToFloatRegister(ins->first());
144 FloatRegister second = ToFloatRegister(ins->second());
145
146 MOZ_ASSERT(first == ToFloatRegister(ins->output()));
147
148 if (ins->mir()->isMax()) {
149 masm.maxFloat32(second, first, true);
150 } else {
151 masm.minFloat32(second, first, true);
152 }
153 }
154
155 void CodeGenerator::visitAddI(LAddI* ins) {
156 const LAllocation* lhs = ins->lhs();
157 const LAllocation* rhs = ins->rhs();
158 const LDefinition* dest = ins->output();
159
160 MOZ_ASSERT(rhs->isConstant() || rhs->isGeneralReg());
161
162 // If there is no snapshot, we don't need to check for overflow
163 if (!ins->snapshot()) {
164 if (rhs->isConstant()) {
165 masm.ma_addu(ToRegister(dest), ToRegister(lhs), Imm32(ToInt32(rhs)));
166 } else {
167 masm.as_addu(ToRegister(dest), ToRegister(lhs), ToRegister(rhs));
168 }
169 return;
170 }
171
172 Label overflow;
173 if (rhs->isConstant()) {
174 masm.ma_add32TestOverflow(ToRegister(dest), ToRegister(lhs),
175 Imm32(ToInt32(rhs)), &overflow);
176 } else {
177 masm.ma_add32TestOverflow(ToRegister(dest), ToRegister(lhs),
178 ToRegister(rhs), &overflow);
179 }
180
181 bailoutFrom(&overflow, ins->snapshot());
182 }
183
184 void CodeGenerator::visitAddI64(LAddI64* lir) {
185 LInt64Allocation lhs = lir->lhs();
186 LInt64Allocation rhs = lir->rhs();
187
188 MOZ_ASSERT(ToOutRegister64(lir) == ToRegister64(lhs));
189
190 if (IsConstant(rhs)) {
191 masm.add64(Imm64(ToInt64(rhs)), ToRegister64(lhs));
192 return;
193 }
194
195 masm.add64(ToOperandOrRegister64(rhs), ToRegister64(lhs));
196 }
197
198 void CodeGenerator::visitSubI(LSubI* ins) {
199 const LAllocation* lhs = ins->lhs();
200 const LAllocation* rhs = ins->rhs();
201 const LDefinition* dest = ins->output();
202
203 MOZ_ASSERT(rhs->isConstant() || rhs->isGeneralReg());
204
205 // If there is no snapshot, we don't need to check for overflow
206 if (!ins->snapshot()) {
207 if (rhs->isConstant()) {
208 masm.ma_subu(ToRegister(dest), ToRegister(lhs), Imm32(ToInt32(rhs)));
209 } else {
210 masm.as_subu(ToRegister(dest), ToRegister(lhs), ToRegister(rhs));
211 }
212 return;
213 }
214
215 Label overflow;
216 if (rhs->isConstant()) {
217 masm.ma_sub32TestOverflow(ToRegister(dest), ToRegister(lhs),
218 Imm32(ToInt32(rhs)), &overflow);
219 } else {
220 masm.ma_sub32TestOverflow(ToRegister(dest), ToRegister(lhs),
221 ToRegister(rhs), &overflow);
222 }
223
224 bailoutFrom(&overflow, ins->snapshot());
225 }
226
227 void CodeGenerator::visitSubI64(LSubI64* lir) {
228 LInt64Allocation lhs = lir->lhs();
229 LInt64Allocation rhs = lir->rhs();
230
231 MOZ_ASSERT(ToOutRegister64(lir) == ToRegister64(lhs));
232
233 if (IsConstant(rhs)) {
234 masm.sub64(Imm64(ToInt64(rhs)), ToRegister64(lhs));
235 return;
236 }
237
238 masm.sub64(ToOperandOrRegister64(rhs), ToRegister64(lhs));
239 }
240
241 void CodeGenerator::visitMulI(LMulI* ins) {
242 const LAllocation* lhs = ins->lhs();
243 const LAllocation* rhs = ins->rhs();
244 Register dest = ToRegister(ins->output());
245 MMul* mul = ins->mir();
246
247 MOZ_ASSERT_IF(mul->mode() == MMul::Integer,
248 !mul->canBeNegativeZero() && !mul->canOverflow());
249
250 if (rhs->isConstant()) {
251 int32_t constant = ToInt32(rhs);
252 Register src = ToRegister(lhs);
253
254 // Bailout on -0.0
255 if (mul->canBeNegativeZero() && constant <= 0) {
256 Assembler::Condition cond =
257 (constant == 0) ? Assembler::LessThan : Assembler::Equal;
258 bailoutCmp32(cond, src, Imm32(0), ins->snapshot());
259 }
260
261 switch (constant) {
262 case -1:
263 if (mul->canOverflow()) {
264 bailoutCmp32(Assembler::Equal, src, Imm32(INT32_MIN),
265 ins->snapshot());
266 }
267
268 masm.ma_negu(dest, src);
269 break;
270 case 0:
271 masm.move32(Imm32(0), dest);
272 break;
273 case 1:
274 masm.move32(src, dest);
275 break;
276 case 2:
277 if (mul->canOverflow()) {
278 Label mulTwoOverflow;
279 masm.ma_add32TestOverflow(dest, src, src, &mulTwoOverflow);
280
281 bailoutFrom(&mulTwoOverflow, ins->snapshot());
282 } else {
283 masm.as_addu(dest, src, src);
284 }
285 break;
286 default:
287 uint32_t shift = FloorLog2(constant);
288
289 if (!mul->canOverflow() && (constant > 0)) {
290 // If it cannot overflow, we can do lots of optimizations.
291 uint32_t rest = constant - (1 << shift);
292
293 // See if the constant has one bit set, meaning it can be
294 // encoded as a bitshift.
295 if ((1 << shift) == constant) {
296 masm.ma_sll(dest, src, Imm32(shift));
297 return;
298 }
299
300 // If the constant cannot be encoded as (1<<C1), see if it can
301 // be encoded as (1<<C1) | (1<<C2), which can be computed
302 // using an add and a shift.
303 uint32_t shift_rest = FloorLog2(rest);
304 if (src != dest && (1u << shift_rest) == rest) {
305 masm.ma_sll(dest, src, Imm32(shift - shift_rest));
306 masm.add32(src, dest);
307 if (shift_rest != 0) {
308 masm.ma_sll(dest, dest, Imm32(shift_rest));
309 }
310 return;
311 }
312 }
313
314 if (mul->canOverflow() && (constant > 0) && (src != dest)) {
315 // To stay on the safe side, only optimize things that are a
316 // power of 2.
317
318 if ((1 << shift) == constant) {
319 // dest = lhs * pow(2, shift)
320 masm.ma_sll(dest, src, Imm32(shift));
321 // At runtime, check (lhs == dest >> shift), if this does
322 // not hold, some bits were lost due to overflow, and the
323 // computation should be resumed as a double.
324 masm.ma_sra(ScratchRegister, dest, Imm32(shift));
325 bailoutCmp32(Assembler::NotEqual, src, ScratchRegister,
326 ins->snapshot());
327 return;
328 }
329 }
330
331 if (mul->canOverflow()) {
332 Label mulConstOverflow;
333 masm.ma_mul32TestOverflow(dest, ToRegister(lhs), Imm32(ToInt32(rhs)),
334 &mulConstOverflow);
335
336 bailoutFrom(&mulConstOverflow, ins->snapshot());
337 } else {
338 masm.ma_mul(dest, src, Imm32(ToInt32(rhs)));
339 }
340 break;
341 }
342 } else {
343 Label multRegOverflow;
344
345 if (mul->canOverflow()) {
346 masm.ma_mul32TestOverflow(dest, ToRegister(lhs), ToRegister(rhs),
347 &multRegOverflow);
348 bailoutFrom(&multRegOverflow, ins->snapshot());
349 } else {
350 masm.as_mul(dest, ToRegister(lhs), ToRegister(rhs));
351 }
352
353 if (mul->canBeNegativeZero()) {
354 Label done;
355 masm.ma_b(dest, dest, &done, Assembler::NonZero, ShortJump);
356
357 // Result is -0 if lhs or rhs is negative.
358 // In that case result must be double value so bailout
359 Register scratch = SecondScratchReg;
360 masm.as_or(scratch, ToRegister(lhs), ToRegister(rhs));
361 bailoutCmp32(Assembler::Signed, scratch, scratch, ins->snapshot());
362
363 masm.bind(&done);
364 }
365 }
366 }
367
368 void CodeGenerator::visitMulI64(LMulI64* lir) {
369 LInt64Allocation lhs = lir->lhs();
370 LInt64Allocation rhs = lir->rhs();
371 Register64 output = ToOutRegister64(lir);
372 MOZ_ASSERT(ToRegister64(lhs) == output);
373
374 if (IsConstant(rhs)) {
375 int64_t constant = ToInt64(rhs);
376 switch (constant) {
377 case -1:
378 masm.neg64(ToRegister64(lhs));
379 return;
380 case 0:
381 masm.xor64(ToRegister64(lhs), ToRegister64(lhs));
382 return;
383 case 1:
384 // nop
385 return;
386 case 2:
387 masm.add64(ToRegister64(lhs), ToRegister64(lhs));
388 return;
389 default:
390 if (constant > 0) {
391 if (mozilla::IsPowerOfTwo(static_cast<uint64_t>(constant + 1))) {
392 ScratchRegisterScope scratch(masm);
393 Register64 scratch64(scratch);
394 masm.move64(ToRegister64(lhs), scratch64);
395 masm.lshift64(Imm32(FloorLog2(constant + 1)), output);
396 masm.sub64(scratch64, output);
397 return;
398 } else if (mozilla::IsPowerOfTwo(
399 static_cast<uint64_t>(constant - 1))) {
400 ScratchRegisterScope scratch(masm);
401 Register64 scratch64(scratch);
402 masm.move64(ToRegister64(lhs), scratch64);
403 masm.lshift64(Imm32(FloorLog2(constant - 1u)), output);
404 masm.add64(scratch64, output);
405 return;
406 }
407 // Use shift if constant is power of 2.
408 int32_t shift = mozilla::FloorLog2(constant);
409 if (int64_t(1) << shift == constant) {
410 masm.lshift64(Imm32(shift), ToRegister64(lhs));
411 return;
412 }
413 }
414 Register temp = ToTempRegisterOrInvalid(lir->temp0());
415 masm.mul64(Imm64(constant), ToRegister64(lhs), temp);
416 }
417 } else {
418 Register temp = ToTempRegisterOrInvalid(lir->temp0());
419 masm.mul64(ToOperandOrRegister64(rhs), ToRegister64(lhs), temp);
420 }
421 }
422
423 void CodeGenerator::visitDivI(LDivI* ins) {
424 // Extract the registers from this instruction
425 Register lhs = ToRegister(ins->lhs());
426 Register rhs = ToRegister(ins->rhs());
427 Register dest = ToRegister(ins->output());
428 Register temp = ToRegister(ins->temp0());
429 MDiv* mir = ins->mir();
430
431 Label done;
432
433 // Handle divide by zero.
434 if (mir->canBeDivideByZero()) {
435 if (mir->trapOnError()) {
436 Label nonZero;
437 masm.ma_b(rhs, rhs, &nonZero, Assembler::NonZero);
438 masm.wasmTrap(wasm::Trap::IntegerDivideByZero, mir->trapSiteDesc());
439 masm.bind(&nonZero);
440 } else if (mir->canTruncateInfinities()) {
441 // Truncated division by zero is zero (Infinity|0 == 0)
442 Label notzero;
443 masm.ma_b(rhs, rhs, &notzero, Assembler::NonZero, ShortJump);
444 masm.move32(Imm32(0), dest);
445 masm.ma_b(&done, ShortJump);
446 masm.bind(&notzero);
447 } else {
448 MOZ_ASSERT(mir->fallible());
449 bailoutCmp32(Assembler::Zero, rhs, rhs, ins->snapshot());
450 }
451 }
452
453 // Handle an integer overflow exception from -2147483648 / -1.
454 if (mir->canBeNegativeOverflow()) {
455 Label notMinInt;
456 masm.move32(Imm32(INT32_MIN), temp);
457 masm.ma_b(lhs, temp, &notMinInt, Assembler::NotEqual, ShortJump);
458
459 masm.move32(Imm32(-1), temp);
460 if (mir->trapOnError()) {
461 Label ok;
462 masm.ma_b(rhs, temp, &ok, Assembler::NotEqual);
463 masm.wasmTrap(wasm::Trap::IntegerOverflow, mir->trapSiteDesc());
464 masm.bind(&ok);
465 } else if (mir->canTruncateOverflow()) {
466 // (-INT32_MIN)|0 == INT32_MIN
467 Label skip;
468 masm.ma_b(rhs, temp, &skip, Assembler::NotEqual, ShortJump);
469 masm.move32(Imm32(INT32_MIN), dest);
470 masm.ma_b(&done, ShortJump);
471 masm.bind(&skip);
472 } else {
473 MOZ_ASSERT(mir->fallible());
474 bailoutCmp32(Assembler::Equal, rhs, temp, ins->snapshot());
475 }
476 masm.bind(&notMinInt);
477 }
478
479 // Handle negative 0. (0/-Y)
480 if (!mir->canTruncateNegativeZero() && mir->canBeNegativeZero()) {
481 Label nonzero;
482 masm.ma_b(lhs, lhs, &nonzero, Assembler::NonZero, ShortJump);
483 bailoutCmp32(Assembler::LessThan, rhs, Imm32(0), ins->snapshot());
484 masm.bind(&nonzero);
485 }
486 // Note: above safety checks could not be verified as Ion seems to be
487 // smarter and requires double arithmetic in such cases.
488
489 // All regular. Lets call div.
490 if (mir->canTruncateRemainder()) {
491 #ifdef MIPSR6
492 masm.as_div(dest, lhs, rhs);
493 #else
494 masm.as_div(lhs, rhs);
495 masm.as_mflo(dest);
496 #endif
497 } else {
498 MOZ_ASSERT(mir->fallible());
499
500 Label remainderNonZero;
501 masm.ma_div_branch_overflow(dest, lhs, rhs, &remainderNonZero);
502 bailoutFrom(&remainderNonZero, ins->snapshot());
503 }
504
505 masm.bind(&done);
506 }
507
508 void CodeGenerator::visitDivPowTwoI(LDivPowTwoI* ins) {
509 Register lhs = ToRegister(ins->numerator());
510 Register dest = ToRegister(ins->output());
511 Register tmp = ToRegister(ins->temp0());
512 int32_t shift = ins->shift();
513
514 if (shift != 0) {
515 MDiv* mir = ins->mir();
516 if (!mir->isTruncated()) {
517 // If the remainder is going to be != 0, bailout since this must
518 // be a double.
519 masm.ma_sll(tmp, lhs, Imm32(32 - shift));
520 bailoutCmp32(Assembler::NonZero, tmp, tmp, ins->snapshot());
521 }
522
523 if (!mir->canBeNegativeDividend()) {
524 // Numerator is unsigned, so needs no adjusting. Do the shift.
525 masm.ma_sra(dest, lhs, Imm32(shift));
526 return;
527 }
528
529 // Adjust the value so that shifting produces a correctly rounded result
530 // when the numerator is negative. See 10-1 "Signed Division by a Known
531 // Power of 2" in Henry S. Warren, Jr.'s Hacker's Delight.
532 if (shift > 1) {
533 masm.ma_sra(tmp, lhs, Imm32(31));
534 masm.ma_srl(tmp, tmp, Imm32(32 - shift));
535 masm.add32(lhs, tmp);
536 } else {
537 masm.ma_srl(tmp, lhs, Imm32(32 - shift));
538 masm.add32(lhs, tmp);
539 }
540
541 // Do the shift.
542 masm.ma_sra(dest, tmp, Imm32(shift));
543 } else {
544 masm.move32(lhs, dest);
545 }
546 }
547
548 void CodeGenerator::visitModI(LModI* ins) {
549 // Extract the registers from this instruction
550 Register lhs = ToRegister(ins->lhs());
551 Register rhs = ToRegister(ins->rhs());
552 Register dest = ToRegister(ins->output());
553 Register callTemp = ToRegister(ins->temp0());
554 MMod* mir = ins->mir();
555 Label done;
556
557 masm.move32(lhs, callTemp);
558
559 // Prevent X % 0.
560 // For X % Y, Compare Y with 0.
561 // There are two cases: (Y == 0) and (Y != 0)
562 // If (Y == 0), then we want to bail.
563 // if (Y != 0), we don't bail.
564
565 if (mir->canBeDivideByZero()) {
566 if (mir->isTruncated()) {
567 if (mir->trapOnError()) {
568 Label nonZero;
569 masm.ma_b(rhs, rhs, &nonZero, Assembler::NonZero);
570 masm.wasmTrap(wasm::Trap::IntegerDivideByZero, mir->trapSiteDesc());
571 masm.bind(&nonZero);
572 } else {
573 Label skip;
574 masm.ma_b(rhs, Imm32(0), &skip, Assembler::NotEqual, ShortJump);
575 // (X % 0) | 0 == 0
576 masm.move32(Imm32(0), dest);
577 masm.ma_b(&done, ShortJump);
578 masm.bind(&skip);
579 }
580 } else {
581 MOZ_ASSERT(mir->fallible());
582 bailoutCmp32(Assembler::Equal, rhs, Imm32(0), ins->snapshot());
583 }
584 }
585
586 #ifdef MIPSR6
587 masm.as_mod(dest, lhs, rhs);
588 #else
589 masm.as_div(lhs, rhs);
590 masm.as_mfhi(dest);
591 #endif
592
593 // If X%Y == 0 and X < 0, then we *actually* wanted to return -0.0
594 // This also catches INT_MIN % -1 since we want -0.
595 if (mir->canBeNegativeDividend()) {
596 if (mir->isTruncated()) {
597 // -0.0|0 == 0
598 } else {
599 MOZ_ASSERT(mir->fallible());
600 // See if X < 0
601 masm.ma_b(dest, Imm32(0), &done, Assembler::NotEqual, ShortJump);
602 bailoutCmp32(Assembler::Signed, callTemp, Imm32(0), ins->snapshot());
603 }
604 }
605 masm.bind(&done);
606 }
607
608 void CodeGenerator::visitModPowTwoI(LModPowTwoI* ins) {
609 Register in = ToRegister(ins->input());
610 Register out = ToRegister(ins->output());
611 MMod* mir = ins->mir();
612 Label negative, done;
613
614 masm.move32(in, out);
615 masm.ma_b(in, in, &done, Assembler::Zero, ShortJump);
616 // Switch based on sign of the lhs.
617 // Positive numbers are just a bitmask
618 masm.ma_b(in, in, &negative, Assembler::Signed, ShortJump);
619 {
620 masm.and32(Imm32((1 << ins->shift()) - 1), out);
621 masm.ma_b(&done, ShortJump);
622 }
623
624 // Negative numbers need a negate, bitmask, negate
625 {
626 masm.bind(&negative);
627 masm.neg32(out);
628 masm.and32(Imm32((1 << ins->shift()) - 1), out);
629 masm.neg32(out);
630 }
631 if (mir->canBeNegativeDividend()) {
632 if (!mir->isTruncated()) {
633 MOZ_ASSERT(mir->fallible());
634 bailoutCmp32(Assembler::Equal, out, zero, ins->snapshot());
635 } else {
636 // -0|0 == 0
637 }
638 }
639 masm.bind(&done);
640 }
641
642 void CodeGenerator::visitModMaskI(LModMaskI* ins) {
643 Register src = ToRegister(ins->input());
644 Register dest = ToRegister(ins->output());
645 Register tmp0 = ToRegister(ins->temp0());
646 Register tmp1 = ToRegister(ins->temp1());
647 MMod* mir = ins->mir();
648
649 if (!mir->isTruncated() && mir->canBeNegativeDividend()) {
650 MOZ_ASSERT(mir->fallible());
651
652 Label bail;
653 masm.ma_mod_mask(src, dest, tmp0, tmp1, ins->shift(), &bail);
654 bailoutFrom(&bail, ins->snapshot());
655 } else {
656 masm.ma_mod_mask(src, dest, tmp0, tmp1, ins->shift(), nullptr);
657 }
658 }
659
660 void CodeGenerator::visitBitNotI(LBitNotI* ins) {
661 const LAllocation* input = ins->input();
662 const LDefinition* dest = ins->output();
663 MOZ_ASSERT(!input->isConstant());
664
665 masm.ma_not(ToRegister(dest), ToRegister(input));
666 }
667
668 void CodeGenerator::visitBitOpI(LBitOpI* ins) {
669 const LAllocation* lhs = ins->lhs();
670 const LAllocation* rhs = ins->rhs();
671 const LDefinition* dest = ins->output();
672 // all of these bitops should be either imm32's, or integer registers.
673 switch (ins->bitop()) {
674 case JSOp::BitOr:
675 if (rhs->isConstant()) {
676 masm.ma_or(ToRegister(dest), ToRegister(lhs), Imm32(ToInt32(rhs)));
677 } else {
678 masm.as_or(ToRegister(dest), ToRegister(lhs), ToRegister(rhs));
679 }
680 break;
681 case JSOp::BitXor:
682 if (rhs->isConstant()) {
683 masm.ma_xor(ToRegister(dest), ToRegister(lhs), Imm32(ToInt32(rhs)));
684 } else {
685 masm.as_xor(ToRegister(dest), ToRegister(lhs), ToRegister(rhs));
686 }
687 break;
688 case JSOp::BitAnd:
689 if (rhs->isConstant()) {
690 masm.ma_and(ToRegister(dest), ToRegister(lhs), Imm32(ToInt32(rhs)));
691 } else {
692 masm.as_and(ToRegister(dest), ToRegister(lhs), ToRegister(rhs));
693 }
694 break;
695 default:
696 MOZ_CRASH("unexpected binary opcode");
697 }
698 }
699
700 void CodeGenerator::visitBitOpI64(LBitOpI64* lir) {
701 LInt64Allocation lhs = lir->lhs();
702 LInt64Allocation rhs = lir->rhs();
703
704 MOZ_ASSERT(ToOutRegister64(lir) == ToRegister64(lhs));
705
706 switch (lir->bitop()) {
707 case JSOp::BitOr:
708 if (IsConstant(rhs)) {
709 masm.or64(Imm64(ToInt64(rhs)), ToRegister64(lhs));
710 } else {
711 masm.or64(ToOperandOrRegister64(rhs), ToRegister64(lhs));
712 }
713 break;
714 case JSOp::BitXor:
715 if (IsConstant(rhs)) {
716 masm.xor64(Imm64(ToInt64(rhs)), ToRegister64(lhs));
717 } else {
718 masm.xor64(ToOperandOrRegister64(rhs), ToRegister64(lhs));
719 }
720 break;
721 case JSOp::BitAnd:
722 if (IsConstant(rhs)) {
723 masm.and64(Imm64(ToInt64(rhs)), ToRegister64(lhs));
724 } else {
725 masm.and64(ToOperandOrRegister64(rhs), ToRegister64(lhs));
726 }
727 break;
728 default:
729 MOZ_CRASH("unexpected binary opcode");
730 }
731 }
732
733 void CodeGenerator::visitShiftI(LShiftI* ins) {
734 Register lhs = ToRegister(ins->lhs());
735 const LAllocation* rhs = ins->rhs();
736 Register dest = ToRegister(ins->output());
737
738 if (rhs->isConstant()) {
739 int32_t shift = ToInt32(rhs) & 0x1F;
740 switch (ins->bitop()) {
741 case JSOp::Lsh:
742 if (shift) {
743 masm.ma_sll(dest, lhs, Imm32(shift));
744 } else {
745 masm.move32(lhs, dest);
746 }
747 break;
748 case JSOp::Rsh:
749 if (shift) {
750 masm.ma_sra(dest, lhs, Imm32(shift));
751 } else {
752 masm.move32(lhs, dest);
753 }
754 break;
755 case JSOp::Ursh:
756 if (shift) {
757 masm.ma_srl(dest, lhs, Imm32(shift));
758 } else {
759 // x >>> 0 can overflow.
760 if (ins->mir()->toUrsh()->fallible()) {
761 bailoutCmp32(Assembler::LessThan, lhs, Imm32(0), ins->snapshot());
762 }
763 masm.move32(lhs, dest);
764 }
765 break;
766 default:
767 MOZ_CRASH("Unexpected shift op");
768 }
769 } else {
770 // The shift amounts should be AND'ed into the 0-31 range
771 masm.ma_and(dest, ToRegister(rhs), Imm32(0x1F));
772
773 switch (ins->bitop()) {
774 case JSOp::Lsh:
775 masm.ma_sll(dest, lhs, dest);
776 break;
777 case JSOp::Rsh:
778 masm.ma_sra(dest, lhs, dest);
779 break;
780 case JSOp::Ursh:
781 masm.ma_srl(dest, lhs, dest);
782 if (ins->mir()->toUrsh()->fallible()) {
783 // x >>> 0 can overflow.
784 bailoutCmp32(Assembler::LessThan, dest, Imm32(0), ins->snapshot());
785 }
786 break;
787 default:
788 MOZ_CRASH("Unexpected shift op");
789 }
790 }
791 }
792
793 void CodeGenerator::visitShiftI64(LShiftI64* lir) {
794 LInt64Allocation lhs = lir->lhs();
795 const LAllocation* rhs = lir->rhs();
796
797 MOZ_ASSERT(ToOutRegister64(lir) == ToRegister64(lhs));
798
799 if (rhs->isConstant()) {
800 int32_t shift = int32_t(rhs->toConstant()->toInt64() & 0x3F);
801 switch (lir->bitop()) {
802 case JSOp::Lsh:
803 if (shift) {
804 masm.lshift64(Imm32(shift), ToRegister64(lhs));
805 }
806 break;
807 case JSOp::Rsh:
808 if (shift) {
809 masm.rshift64Arithmetic(Imm32(shift), ToRegister64(lhs));
810 }
811 break;
812 case JSOp::Ursh:
813 if (shift) {
814 masm.rshift64(Imm32(shift), ToRegister64(lhs));
815 }
816 break;
817 default:
818 MOZ_CRASH("Unexpected shift op");
819 }
820 return;
821 }
822
823 switch (lir->bitop()) {
824 case JSOp::Lsh:
825 masm.lshift64(ToRegister(rhs), ToRegister64(lhs));
826 break;
827 case JSOp::Rsh:
828 masm.rshift64Arithmetic(ToRegister(rhs), ToRegister64(lhs));
829 break;
830 case JSOp::Ursh:
831 masm.rshift64(ToRegister(rhs), ToRegister64(lhs));
832 break;
833 default:
834 MOZ_CRASH("Unexpected shift op");
835 }
836 }
837
838 void CodeGenerator::visitUrshD(LUrshD* ins) {
839 Register lhs = ToRegister(ins->lhs());
840 Register temp = ToRegister(ins->temp0());
841
842 const LAllocation* rhs = ins->rhs();
843 FloatRegister out = ToFloatRegister(ins->output());
844
845 if (rhs->isConstant()) {
846 masm.ma_srl(temp, lhs, Imm32(ToInt32(rhs)));
847 } else {
848 masm.ma_srl(temp, lhs, ToRegister(rhs));
849 }
850
851 masm.convertUInt32ToDouble(temp, out);
852 }
853
854 void CodeGenerator::visitPowHalfD(LPowHalfD* ins) {
855 FloatRegister input = ToFloatRegister(ins->input());
856 FloatRegister output = ToFloatRegister(ins->output());
857
858 Label done, skip;
859
860 // Masm.pow(-Infinity, 0.5) == Infinity.
861 masm.loadConstantDouble(NegativeInfinity<double>(), ScratchDoubleReg);
862 masm.ma_bc1d(input, ScratchDoubleReg, &skip,
863 Assembler::DoubleNotEqualOrUnordered, ShortJump);
864 masm.as_negd(output, ScratchDoubleReg);
865 masm.ma_b(&done, ShortJump);
866
867 masm.bind(&skip);
868 // Math.pow(-0, 0.5) == 0 == Math.pow(0, 0.5).
869 // Adding 0 converts any -0 to 0.
870 masm.loadConstantDouble(0.0, ScratchDoubleReg);
871 masm.as_addd(output, input, ScratchDoubleReg);
872 masm.as_sqrtd(output, output);
873
874 masm.bind(&done);
875 }
876
877 MoveOperand CodeGeneratorMIPSShared::toMoveOperand(LAllocation a) const {
878 if (a.isGeneralReg()) {
879 return MoveOperand(ToRegister(a));
880 }
881 if (a.isFloatReg()) {
882 return MoveOperand(ToFloatRegister(a));
883 }
884 MoveOperand::Kind kind = a.isStackArea() ? MoveOperand::Kind::EffectiveAddress
885 : MoveOperand::Kind::Memory;
886 Address address = ToAddress(a);
887 MOZ_ASSERT((address.offset & 3) == 0);
888 return MoveOperand(address, kind);
889 }
890
891 void CodeGenerator::visitMathD(LMathD* math) {
892 FloatRegister src1 = ToFloatRegister(math->lhs());
893 FloatRegister src2 = ToFloatRegister(math->rhs());
894 FloatRegister output = ToFloatRegister(math->output());
895
896 switch (math->jsop()) {
897 case JSOp::Add:
898 masm.as_addd(output, src1, src2);
899 break;
900 case JSOp::Sub:
901 masm.as_subd(output, src1, src2);
902 break;
903 case JSOp::Mul:
904 masm.as_muld(output, src1, src2);
905 break;
906 case JSOp::Div:
907 masm.as_divd(output, src1, src2);
908 break;
909 default:
910 MOZ_CRASH("unexpected opcode");
911 }
912 }
913
914 void CodeGenerator::visitMathF(LMathF* math) {
915 FloatRegister src1 = ToFloatRegister(math->lhs());
916 FloatRegister src2 = ToFloatRegister(math->rhs());
917 FloatRegister output = ToFloatRegister(math->output());
918
919 switch (math->jsop()) {
920 case JSOp::Add:
921 masm.as_adds(output, src1, src2);
922 break;
923 case JSOp::Sub:
924 masm.as_subs(output, src1, src2);
925 break;
926 case JSOp::Mul:
927 masm.as_muls(output, src1, src2);
928 break;
929 case JSOp::Div:
930 masm.as_divs(output, src1, src2);
931 break;
932 default:
933 MOZ_CRASH("unexpected opcode");
934 }
935 }
936
937 void CodeGenerator::visitTruncateDToInt32(LTruncateDToInt32* ins) {
938 emitTruncateDouble(ToFloatRegister(ins->input()), ToRegister(ins->output()),
939 ins->mir());
940 }
941
942 void CodeGenerator::visitTruncateFToInt32(LTruncateFToInt32* ins) {
943 emitTruncateFloat32(ToFloatRegister(ins->input()), ToRegister(ins->output()),
944 ins->mir());
945 }
946
947 void CodeGenerator::visitWasmBuiltinTruncateDToInt32(
948 LWasmBuiltinTruncateDToInt32* lir) {
949 emitTruncateDouble(ToFloatRegister(lir->input()), ToRegister(lir->output()),
950 lir->mir());
951 }
952
953 void CodeGenerator::visitWasmBuiltinTruncateFToInt32(
954 LWasmBuiltinTruncateFToInt32* lir) {
955 emitTruncateFloat32(ToFloatRegister(lir->input()), ToRegister(lir->output()),
956 lir->mir());
957 }
958
959 void CodeGenerator::visitWasmTruncateToInt32(LWasmTruncateToInt32* lir) {
960 auto input = ToFloatRegister(lir->input());
961 auto output = ToRegister(lir->output());
962
963 MWasmTruncateToInt32* mir = lir->mir();
964 MIRType fromType = mir->input()->type();
965
966 MOZ_ASSERT(fromType == MIRType::Double || fromType == MIRType::Float32);
967
968 auto* ool = new (alloc()) OutOfLineWasmTruncateCheck(mir, input, output);
969 addOutOfLineCode(ool, mir);
970
971 Label* oolEntry = ool->entry();
972 if (mir->isUnsigned()) {
973 if (fromType == MIRType::Double) {
974 masm.wasmTruncateDoubleToUInt32(input, output, mir->isSaturating(),
975 oolEntry);
976 } else if (fromType == MIRType::Float32) {
977 masm.wasmTruncateFloat32ToUInt32(input, output, mir->isSaturating(),
978 oolEntry);
979 } else {
980 MOZ_CRASH("unexpected type");
981 }
982
983 masm.bind(ool->rejoin());
984 return;
985 }
986
987 if (fromType == MIRType::Double) {
988 masm.wasmTruncateDoubleToInt32(input, output, mir->isSaturating(),
989 oolEntry);
990 } else if (fromType == MIRType::Float32) {
991 masm.wasmTruncateFloat32ToInt32(input, output, mir->isSaturating(),
992 oolEntry);
993 } else {
994 MOZ_CRASH("unexpected type");
995 }
996
997 masm.bind(ool->rejoin());
998 }
999
1000 void CodeGeneratorMIPSShared::visitOutOfLineWasmTruncateCheck(
1001 OutOfLineWasmTruncateCheck* ool) {
1002 if (ool->toType() == MIRType::Int32) {
1003 masm.outOfLineWasmTruncateToInt32Check(ool->input(), ool->output(),
1004 ool->fromType(), ool->flags(),
1005 ool->rejoin(), ool->trapSiteDesc());
1006 } else {
1007 MOZ_ASSERT(ool->toType() == MIRType::Int64);
1008 masm.outOfLineWasmTruncateToInt64Check(ool->input(), ool->output64(),
1009 ool->fromType(), ool->flags(),
1010 ool->rejoin(), ool->trapSiteDesc());
1011 }
1012 }
1013
1014 void CodeGenerator::visitCopySignF(LCopySignF* ins) {
1015 FloatRegister lhs = ToFloatRegister(ins->lhs());
1016 FloatRegister rhs = ToFloatRegister(ins->rhs());
1017 FloatRegister output = ToFloatRegister(ins->output());
1018
1019 Register lhsi = ToRegister(ins->temp0());
1020 Register rhsi = ToRegister(ins->temp1());
1021
1022 masm.moveFromFloat32(lhs, lhsi);
1023 masm.moveFromFloat32(rhs, rhsi);
1024
1025 // Combine.
1026 masm.ma_ins(rhsi, lhsi, 0, 31);
1027
1028 masm.moveToFloat32(rhsi, output);
1029 }
1030
1031 void CodeGenerator::visitCopySignD(LCopySignD* ins) {
1032 FloatRegister lhs = ToFloatRegister(ins->lhs());
1033 FloatRegister rhs = ToFloatRegister(ins->rhs());
1034 FloatRegister output = ToFloatRegister(ins->output());
1035
1036 Register lhsi = ToRegister(ins->temp0());
1037 Register rhsi = ToRegister(ins->temp1());
1038
1039 // Manipulate high words of double inputs.
1040 masm.moveFromDoubleHi(lhs, lhsi);
1041 masm.moveFromDoubleHi(rhs, rhsi);
1042
1043 // Combine.
1044 masm.ma_ins(rhsi, lhsi, 0, 31);
1045
1046 masm.moveToDoubleHi(rhsi, output);
1047 }
1048
1049 void CodeGenerator::visitTestDAndBranch(LTestDAndBranch* test) {
1050 FloatRegister input = ToFloatRegister(test->input());
1051
1052 MBasicBlock* ifTrue = test->ifTrue();
1053 MBasicBlock* ifFalse = test->ifFalse();
1054
1055 masm.loadConstantDouble(0.0, ScratchDoubleReg);
1056 // If 0, or NaN, the result is false.
1057
1058 if (isNextBlock(ifFalse->lir())) {
1059 branchToBlock(Assembler::DoubleFloat, input, ScratchDoubleReg, ifTrue,
1060 Assembler::DoubleNotEqual);
1061 } else {
1062 branchToBlock(Assembler::DoubleFloat, input, ScratchDoubleReg, ifFalse,
1063 Assembler::DoubleEqualOrUnordered);
1064 jumpToBlock(ifTrue);
1065 }
1066 }
1067
1068 void CodeGenerator::visitTestFAndBranch(LTestFAndBranch* test) {
1069 FloatRegister input = ToFloatRegister(test->input());
1070
1071 MBasicBlock* ifTrue = test->ifTrue();
1072 MBasicBlock* ifFalse = test->ifFalse();
1073
1074 masm.loadConstantFloat32(0.0f, ScratchFloat32Reg);
1075 // If 0, or NaN, the result is false.
1076
1077 if (isNextBlock(ifFalse->lir())) {
1078 branchToBlock(Assembler::SingleFloat, input, ScratchFloat32Reg, ifTrue,
1079 Assembler::DoubleNotEqual);
1080 } else {
1081 branchToBlock(Assembler::SingleFloat, input, ScratchFloat32Reg, ifFalse,
1082 Assembler::DoubleEqualOrUnordered);
1083 jumpToBlock(ifTrue);
1084 }
1085 }
1086
1087 void CodeGenerator::visitCompareD(LCompareD* comp) {
1088 FloatRegister lhs = ToFloatRegister(comp->left());
1089 FloatRegister rhs = ToFloatRegister(comp->right());
1090 Register dest = ToRegister(comp->output());
1091
1092 Assembler::DoubleCondition cond = JSOpToDoubleCondition(comp->mir()->jsop());
1093 masm.ma_cmp_set_double(dest, lhs, rhs, cond);
1094 }
1095
1096 void CodeGenerator::visitCompareF(LCompareF* comp) {
1097 FloatRegister lhs = ToFloatRegister(comp->left());
1098 FloatRegister rhs = ToFloatRegister(comp->right());
1099 Register dest = ToRegister(comp->output());
1100
1101 Assembler::DoubleCondition cond = JSOpToDoubleCondition(comp->mir()->jsop());
1102 masm.ma_cmp_set_float32(dest, lhs, rhs, cond);
1103 }
1104
1105 void CodeGenerator::visitCompareDAndBranch(LCompareDAndBranch* comp) {
1106 FloatRegister lhs = ToFloatRegister(comp->left());
1107 FloatRegister rhs = ToFloatRegister(comp->right());
1108
1109 Assembler::DoubleCondition cond =
1110 JSOpToDoubleCondition(comp->cmpMir()->jsop());
1111 MBasicBlock* ifTrue = comp->ifTrue();
1112 MBasicBlock* ifFalse = comp->ifFalse();
1113
1114 if (isNextBlock(ifFalse->lir())) {
1115 branchToBlock(Assembler::DoubleFloat, lhs, rhs, ifTrue, cond);
1116 } else {
1117 branchToBlock(Assembler::DoubleFloat, lhs, rhs, ifFalse,
1118 Assembler::InvertCondition(cond));
1119 jumpToBlock(ifTrue);
1120 }
1121 }
1122
1123 void CodeGenerator::visitCompareFAndBranch(LCompareFAndBranch* comp) {
1124 FloatRegister lhs = ToFloatRegister(comp->left());
1125 FloatRegister rhs = ToFloatRegister(comp->right());
1126
1127 Assembler::DoubleCondition cond =
1128 JSOpToDoubleCondition(comp->cmpMir()->jsop());
1129 MBasicBlock* ifTrue = comp->ifTrue();
1130 MBasicBlock* ifFalse = comp->ifFalse();
1131
1132 if (isNextBlock(ifFalse->lir())) {
1133 branchToBlock(Assembler::SingleFloat, lhs, rhs, ifTrue, cond);
1134 } else {
1135 branchToBlock(Assembler::SingleFloat, lhs, rhs, ifFalse,
1136 Assembler::InvertCondition(cond));
1137 jumpToBlock(ifTrue);
1138 }
1139 }
1140
1141 void CodeGenerator::visitWasmUint32ToDouble(LWasmUint32ToDouble* lir) {
1142 masm.convertUInt32ToDouble(ToRegister(lir->input()),
1143 ToFloatRegister(lir->output()));
1144 }
1145
1146 void CodeGenerator::visitWasmUint32ToFloat32(LWasmUint32ToFloat32* lir) {
1147 masm.convertUInt32ToFloat32(ToRegister(lir->input()),
1148 ToFloatRegister(lir->output()));
1149 }
1150
1151 void CodeGenerator::visitNotD(LNotD* ins) {
1152 // Since this operation is not, we want to set a bit if
1153 // the double is falsey, which means 0.0, -0.0 or NaN.
1154 FloatRegister in = ToFloatRegister(ins->input());
1155 Register dest = ToRegister(ins->output());
1156
1157 masm.loadConstantDouble(0.0, ScratchDoubleReg);
1158 masm.ma_cmp_set_double(dest, in, ScratchDoubleReg,
1159 Assembler::DoubleEqualOrUnordered);
1160 }
1161
1162 void CodeGenerator::visitNotF(LNotF* ins) {
1163 // Since this operation is not, we want to set a bit if
1164 // the float32 is falsey, which means 0.0, -0.0 or NaN.
1165 FloatRegister in = ToFloatRegister(ins->input());
1166 Register dest = ToRegister(ins->output());
1167
1168 masm.loadConstantFloat32(0.0f, ScratchFloat32Reg);
1169 masm.ma_cmp_set_float32(dest, in, ScratchFloat32Reg,
1170 Assembler::DoubleEqualOrUnordered);
1171 }
1172
1173 void CodeGeneratorMIPSShared::generateInvalidateEpilogue() {
1174 // Ensure that there is enough space in the buffer for the OsiPoint
1175 // patching to occur. Otherwise, we could overwrite the invalidation
1176 // epilogue.
1177 for (size_t i = 0; i < sizeof(void*); i += Assembler::NopSize()) {
1178 masm.nop();
1179 }
1180
1181 masm.bind(&invalidate_);
1182
1183 // Push the return address of the point that we bailed out at to the stack
1184 masm.Push(ra);
1185
1186 // Push the Ion script onto the stack (when we determine what that
1187 // pointer is).
1188 invalidateEpilogueData_ = masm.pushWithPatch(ImmWord(uintptr_t(-1)));
1189
1190 // Jump to the invalidator which will replace the current frame.
1191 TrampolinePtr thunk = gen->jitRuntime()->getInvalidationThunk();
1192 masm.jump(thunk);
1193 }
1194
1195 class js::jit::OutOfLineTableSwitch
1196 : public OutOfLineCodeBase<CodeGeneratorMIPSShared> {
1197 MTableSwitch* mir_;
1198 CodeLabel jumpLabel_;
1199
1200 void accept(CodeGeneratorMIPSShared* codegen) {
1201 codegen->visitOutOfLineTableSwitch(this);
1202 }
1203
1204 public:
1205 OutOfLineTableSwitch(MTableSwitch* mir) : mir_(mir) {}
1206
1207 MTableSwitch* mir() const { return mir_; }
1208
1209 CodeLabel* jumpLabel() { return &jumpLabel_; }
1210 };
1211
1212 void CodeGeneratorMIPSShared::visitOutOfLineTableSwitch(
1213 OutOfLineTableSwitch* ool) {
1214 MTableSwitch* mir = ool->mir();
1215
1216 masm.haltingAlign(sizeof(void*));
1217 masm.bind(ool->jumpLabel());
1218 masm.addCodeLabel(*ool->jumpLabel());
1219
1220 for (size_t i = 0; i < mir->numCases(); i++) {
1221 LBlock* caseblock = skipTrivialBlocks(mir->getCase(i))->lir();
1222 Label* caseheader = caseblock->label();
1223 uint32_t caseoffset = caseheader->offset();
1224
1225 // The entries of the jump table need to be absolute addresses and thus
1226 // must be patched after codegen is finished.
1227 CodeLabel cl;
1228 masm.writeCodePointer(&cl);
1229 cl.target()->bind(caseoffset);
1230 masm.addCodeLabel(cl);
1231 }
1232 }
1233
1234 void CodeGeneratorMIPSShared::emitTableSwitchDispatch(MTableSwitch* mir,
1235 Register index,
1236 Register base) {
1237 Label* defaultcase = skipTrivialBlocks(mir->getDefault())->lir()->label();
1238
1239 // Lower value with low value
1240 if (mir->low() != 0) {
1241 masm.subPtr(Imm32(mir->low()), index);
1242 }
1243
1244 // Jump to default case if input is out of range
1245 int32_t cases = mir->numCases();
1246 masm.branchPtr(Assembler::AboveOrEqual, index, ImmWord(cases), defaultcase);
1247
1248 // To fill in the CodeLabels for the case entries, we need to first
1249 // generate the case entries (we don't yet know their offsets in the
1250 // instruction stream).
1251 OutOfLineTableSwitch* ool = new (alloc()) OutOfLineTableSwitch(mir);
1252 addOutOfLineCode(ool, mir);
1253
1254 // Compute the position where a pointer to the right case stands.
1255 masm.ma_li(base, ool->jumpLabel());
1256
1257 BaseIndex pointer(base, index, ScalePointer);
1258
1259 // Jump to the right case
1260 masm.branchToComputedAddress(pointer);
1261 }
1262
1263 template <typename T>
1264 void CodeGeneratorMIPSShared::emitWasmLoad(T* lir) {
1265 const MWasmLoad* mir = lir->mir();
1266 SecondScratchRegisterScope scratch2(masm);
1267
1268 Register memoryBase = ToRegister(lir->memoryBase());
1269 Register ptr = ToRegister(lir->ptr());
1270 Register ptrScratch = ToTempRegisterOrInvalid(lir->temp0());
1271
1272 if (mir->base()->type() == MIRType::Int32) {
1273 masm.move32To64ZeroExtend(ptr, Register64(scratch2));
1274 ptr = scratch2;
1275 ptrScratch = ptrScratch != InvalidReg ? scratch2 : InvalidReg;
1276 }
1277
1278 if constexpr (std::is_same_v<T, LWasmUnalignedLoad>) {
1279 MOZ_ASSERT(IsUnaligned(mir->access()));
1280 if (IsFloatingPointType(mir->type())) {
1281 masm.wasmUnalignedLoadFP(mir->access(), memoryBase, ptr, ptrScratch,
1282 ToFloatRegister(lir->output()),
1283 ToRegister(lir->temp1()));
1284 } else {
1285 masm.wasmUnalignedLoad(mir->access(), memoryBase, ptr, ptrScratch,
1286 ToRegister(lir->output()),
1287 ToRegister(lir->temp1()));
1288 }
1289 } else {
1290 MOZ_ASSERT(!IsUnaligned(mir->access()));
1291 masm.wasmLoad(mir->access(), memoryBase, ptr, ptrScratch,
1292 ToAnyRegister(lir->output()));
1293 }
1294 }
1295
1296 void CodeGenerator::visitWasmLoad(LWasmLoad* lir) { emitWasmLoad(lir); }
1297
1298 void CodeGenerator::visitWasmUnalignedLoad(LWasmUnalignedLoad* lir) {
1299 emitWasmLoad(lir);
1300 }
1301
1302 template <typename T>
1303 void CodeGeneratorMIPSShared::emitWasmStore(T* lir) {
1304 const MWasmStore* mir = lir->mir();
1305 SecondScratchRegisterScope scratch2(masm);
1306
1307 Register memoryBase = ToRegister(lir->memoryBase());
1308 Register ptr = ToRegister(lir->ptr());
1309 Register ptrScratch = ToTempRegisterOrInvalid(lir->temp0());
1310
1311 if (mir->base()->type() == MIRType::Int32) {
1312 masm.move32To64ZeroExtend(ptr, Register64(scratch2));
1313 ptr = scratch2;
1314 ptrScratch = ptrScratch != InvalidReg ? scratch2 : InvalidReg;
1315 }
1316
1317 if constexpr (std::is_same_v<T, LWasmUnalignedStore>) {
1318 MOZ_ASSERT(IsUnaligned(mir->access()));
1319 if (mir->access().type() == Scalar::Float32 ||
1320 mir->access().type() == Scalar::Float64) {
1321 masm.wasmUnalignedStoreFP(mir->access(), ToFloatRegister(lir->value()),
1322 memoryBase, ptr, ptrScratch,
1323 ToRegister(lir->temp1()));
1324 } else {
1325 masm.wasmUnalignedStore(mir->access(), ToRegister(lir->value()),
1326 memoryBase, ptr, ptrScratch,
1327 ToRegister(lir->temp1()));
1328 }
1329 } else {
1330 MOZ_ASSERT(!IsUnaligned(mir->access()));
1331 masm.wasmStore(mir->access(), ToAnyRegister(lir->value()), memoryBase, ptr,
1332 ptrScratch);
1333 }
1334 }
1335
1336 void CodeGenerator::visitWasmStore(LWasmStore* lir) { emitWasmStore(lir); }
1337
1338 void CodeGenerator::visitWasmUnalignedStore(LWasmUnalignedStore* lir) {
1339 emitWasmStore(lir);
1340 }
1341
1342 void CodeGenerator::visitAsmJSLoadHeap(LAsmJSLoadHeap* ins) {
1343 const MAsmJSLoadHeap* mir = ins->mir();
1344 const LAllocation* ptr = ins->ptr();
1345 const LDefinition* out = ins->output();
1346 const LAllocation* boundsCheckLimit = ins->boundsCheckLimit();
1347
1348 Scalar::Type accessType = mir->access().type();
1349 bool isSigned = Scalar::isSignedIntType(accessType);
1350 int size = Scalar::byteSize(accessType) * 8;
1351 bool isFloat = Scalar::isFloatingType(accessType);
1352
1353 if (ptr->isConstant()) {
1354 MOZ_ASSERT(!mir->needsBoundsCheck());
1355 int32_t ptrImm = ptr->toConstant()->toInt32();
1356 MOZ_ASSERT(ptrImm >= 0);
1357 if (isFloat) {
1358 if (size == 32) {
1359 masm.loadFloat32(Address(HeapReg, ptrImm), ToFloatRegister(out));
1360 } else {
1361 masm.loadDouble(Address(HeapReg, ptrImm), ToFloatRegister(out));
1362 }
1363 } else {
1364 masm.ma_load(ToRegister(out), Address(HeapReg, ptrImm),
1365 static_cast<LoadStoreSize>(size),
1366 isSigned ? SignExtend : ZeroExtend);
1367 }
1368 return;
1369 }
1370
1371 Register ptrReg = ToRegister(ptr);
1372
1373 if (!mir->needsBoundsCheck()) {
1374 if (isFloat) {
1375 if (size == 32) {
1376 masm.loadFloat32(BaseIndex(HeapReg, ptrReg, TimesOne),
1377 ToFloatRegister(out));
1378 } else {
1379 masm.loadDouble(BaseIndex(HeapReg, ptrReg, TimesOne),
1380 ToFloatRegister(out));
1381 }
1382 } else {
1383 masm.ma_load(ToRegister(out), BaseIndex(HeapReg, ptrReg, TimesOne),
1384 static_cast<LoadStoreSize>(size),
1385 isSigned ? SignExtend : ZeroExtend);
1386 }
1387 return;
1388 }
1389
1390 Label done, outOfRange;
1391 masm.wasmBoundsCheck32(Assembler::AboveOrEqual, ptrReg,
1392 ToRegister(boundsCheckLimit), &outOfRange);
1393 // Offset is ok, let's load value.
1394 if (isFloat) {
1395 if (size == 32) {
1396 masm.loadFloat32(BaseIndex(HeapReg, ptrReg, TimesOne),
1397 ToFloatRegister(out));
1398 } else {
1399 masm.loadDouble(BaseIndex(HeapReg, ptrReg, TimesOne),
1400 ToFloatRegister(out));
1401 }
1402 } else {
1403 masm.ma_load(ToRegister(out), BaseIndex(HeapReg, ptrReg, TimesOne),
1404 static_cast<LoadStoreSize>(size),
1405 isSigned ? SignExtend : ZeroExtend);
1406 }
1407 masm.ma_b(&done, ShortJump);
1408 masm.bind(&outOfRange);
1409 // Offset is out of range. Load default values.
1410 if (isFloat) {
1411 if (size == 32) {
1412 masm.loadConstantFloat32(float(GenericNaN()), ToFloatRegister(out));
1413 } else {
1414 masm.loadConstantDouble(GenericNaN(), ToFloatRegister(out));
1415 }
1416 } else {
1417 masm.move32(Imm32(0), ToRegister(out));
1418 }
1419 masm.bind(&done);
1420 }
1421
1422 void CodeGenerator::visitAsmJSStoreHeap(LAsmJSStoreHeap* ins) {
1423 const MAsmJSStoreHeap* mir = ins->mir();
1424 const LAllocation* value = ins->value();
1425 const LAllocation* ptr = ins->ptr();
1426 const LAllocation* boundsCheckLimit = ins->boundsCheckLimit();
1427
1428 Scalar::Type accessType = mir->access().type();
1429 bool isSigned = Scalar::isSignedIntType(accessType);
1430 int size = Scalar::byteSize(accessType) * 8;
1431 bool isFloat = Scalar::isFloatingType(accessType);
1432
1433 if (ptr->isConstant()) {
1434 MOZ_ASSERT(!mir->needsBoundsCheck());
1435 int32_t ptrImm = ptr->toConstant()->toInt32();
1436 MOZ_ASSERT(ptrImm >= 0);
1437
1438 if (isFloat) {
1439 FloatRegister freg = ToFloatRegister(value);
1440 Address addr(HeapReg, ptrImm);
1441 if (size == 32) {
1442 masm.storeFloat32(freg, addr);
1443 } else {
1444 masm.storeDouble(freg, addr);
1445 }
1446 } else {
1447 masm.ma_store(ToRegister(value), Address(HeapReg, ptrImm),
1448 static_cast<LoadStoreSize>(size),
1449 isSigned ? SignExtend : ZeroExtend);
1450 }
1451 return;
1452 }
1453
1454 Register ptrReg = ToRegister(ptr);
1455 Address dstAddr(ptrReg, 0);
1456
1457 if (!mir->needsBoundsCheck()) {
1458 if (isFloat) {
1459 FloatRegister freg = ToFloatRegister(value);
1460 BaseIndex bi(HeapReg, ptrReg, TimesOne);
1461 if (size == 32) {
1462 masm.storeFloat32(freg, bi);
1463 } else {
1464 masm.storeDouble(freg, bi);
1465 }
1466 } else {
1467 masm.ma_store(ToRegister(value), BaseIndex(HeapReg, ptrReg, TimesOne),
1468 static_cast<LoadStoreSize>(size),
1469 isSigned ? SignExtend : ZeroExtend);
1470 }
1471 return;
1472 }
1473
1474 Label outOfRange;
1475 masm.wasmBoundsCheck32(Assembler::AboveOrEqual, ptrReg,
1476 ToRegister(boundsCheckLimit), &outOfRange);
1477
1478 // Offset is ok, let's store value.
1479 if (isFloat) {
1480 if (size == 32) {
1481 masm.storeFloat32(ToFloatRegister(value),
1482 BaseIndex(HeapReg, ptrReg, TimesOne));
1483 } else
1484 masm.storeDouble(ToFloatRegister(value),
1485 BaseIndex(HeapReg, ptrReg, TimesOne));
1486 } else {
1487 masm.ma_store(ToRegister(value), BaseIndex(HeapReg, ptrReg, TimesOne),
1488 static_cast<LoadStoreSize>(size),
1489 isSigned ? SignExtend : ZeroExtend);
1490 }
1491
1492 masm.bind(&outOfRange);
1493 }
1494
1495 void CodeGenerator::visitWasmCompareExchangeHeap(
1496 LWasmCompareExchangeHeap* ins) {
1497 MWasmCompareExchangeHeap* mir = ins->mir();
1498 Register memoryBase = ToRegister(ins->memoryBase());
1499 Register ptrReg = ToRegister(ins->ptr());
1500 BaseIndex srcAddr(memoryBase, ptrReg, TimesOne, mir->access().offset32());
1501
1502 Register oldval = ToRegister(ins->oldValue());
1503 Register newval = ToRegister(ins->newValue());
1504 Register valueTemp = ToTempRegisterOrInvalid(ins->temp0());
1505 Register offsetTemp = ToTempRegisterOrInvalid(ins->temp1());
1506 Register maskTemp = ToTempRegisterOrInvalid(ins->temp2());
1507
1508 masm.wasmCompareExchange(mir->access(), srcAddr, oldval, newval, valueTemp,
1509 offsetTemp, maskTemp, ToRegister(ins->output()));
1510 }
1511
1512 void CodeGenerator::visitWasmAtomicExchangeHeap(LWasmAtomicExchangeHeap* ins) {
1513 MWasmAtomicExchangeHeap* mir = ins->mir();
1514 Register memoryBase = ToRegister(ins->memoryBase());
1515 Register ptrReg = ToRegister(ins->ptr());
1516 Register value = ToRegister(ins->value());
1517 BaseIndex srcAddr(memoryBase, ptrReg, TimesOne, mir->access().offset32());
1518
1519 Register valueTemp = ToTempRegisterOrInvalid(ins->temp0());
1520 Register offsetTemp = ToTempRegisterOrInvalid(ins->temp1());
1521 Register maskTemp = ToTempRegisterOrInvalid(ins->temp2());
1522
1523 masm.wasmAtomicExchange(mir->access(), srcAddr, value, valueTemp, offsetTemp,
1524 maskTemp, ToRegister(ins->output()));
1525 }
1526
1527 void CodeGenerator::visitWasmAtomicBinopHeap(LWasmAtomicBinopHeap* ins) {
1528 MOZ_ASSERT(ins->mir()->hasUses());
1529
1530 MWasmAtomicBinopHeap* mir = ins->mir();
1531 Register memoryBase = ToRegister(ins->memoryBase());
1532 Register ptrReg = ToRegister(ins->ptr());
1533 Register valueTemp = ToTempRegisterOrInvalid(ins->temp0());
1534 Register offsetTemp = ToTempRegisterOrInvalid(ins->temp1());
1535 Register maskTemp = ToTempRegisterOrInvalid(ins->temp2());
1536
1537 BaseIndex srcAddr(memoryBase, ptrReg, TimesOne, mir->access().offset32());
1538
1539 masm.wasmAtomicFetchOp(mir->access(), mir->operation(),
1540 ToRegister(ins->value()), srcAddr, valueTemp,
1541 offsetTemp, maskTemp, ToRegister(ins->output()));
1542 }
1543
1544 void CodeGenerator::visitWasmAtomicBinopHeapForEffect(
1545 LWasmAtomicBinopHeapForEffect* ins) {
1546 MOZ_ASSERT(!ins->mir()->hasUses());
1547
1548 MWasmAtomicBinopHeap* mir = ins->mir();
1549 Register memoryBase = ToRegister(ins->memoryBase());
1550 Register ptrReg = ToRegister(ins->ptr());
1551 Register valueTemp = ToTempRegisterOrInvalid(ins->temp0());
1552 Register offsetTemp = ToTempRegisterOrInvalid(ins->temp1());
1553 Register maskTemp = ToTempRegisterOrInvalid(ins->temp2());
1554
1555 BaseIndex srcAddr(memoryBase, ptrReg, TimesOne, mir->access().offset32());
1556 masm.wasmAtomicEffectOp(mir->access(), mir->operation(),
1557 ToRegister(ins->value()), srcAddr, valueTemp,
1558 offsetTemp, maskTemp);
1559 }
1560
1561 void CodeGenerator::visitWasmStackArg(LWasmStackArg* ins) {
1562 const MWasmStackArg* mir = ins->mir();
1563 if (ins->arg()->isConstant()) {
1564 masm.storePtr(ImmWord(ToInt32(ins->arg())),
1565 Address(StackPointer, mir->spOffset()));
1566 } else {
1567 if (ins->arg()->isGeneralReg()) {
1568 masm.storePtr(ToRegister(ins->arg()),
1569 Address(StackPointer, mir->spOffset()));
1570 } else if (mir->input()->type() == MIRType::Double) {
1571 masm.storeDouble(ToFloatRegister(ins->arg()).doubleOverlay(),
1572 Address(StackPointer, mir->spOffset()));
1573 } else {
1574 masm.storeFloat32(ToFloatRegister(ins->arg()),
1575 Address(StackPointer, mir->spOffset()));
1576 }
1577 }
1578 }
1579
1580 void CodeGenerator::visitWasmStackArgI64(LWasmStackArgI64* ins) {
1581 const MWasmStackArg* mir = ins->mir();
1582 Address dst(StackPointer, mir->spOffset());
1583 if (IsConstant(ins->arg())) {
1584 masm.store64(Imm64(ToInt64(ins->arg())), dst);
1585 } else {
1586 masm.store64(ToRegister64(ins->arg()), dst);
1587 }
1588 }
1589
1590 void CodeGenerator::visitWasmSelect(LWasmSelect* ins) {
1591 MIRType mirType = ins->mir()->type();
1592
1593 Register cond = ToRegister(ins->condExpr());
1594 const LAllocation* falseExpr = ins->falseExpr();
1595
1596 if (mirType == MIRType::Int32 || mirType == MIRType::WasmAnyRef) {
1597 Register out = ToRegister(ins->output());
1598 MOZ_ASSERT(ToRegister(ins->trueExpr()) == out,
1599 "true expr input is reused for output");
1600 if (falseExpr->isGeneralReg()) {
1601 masm.as_movz(out, ToRegister(falseExpr), cond);
1602 } else {
1603 masm.cmp32Load32(Assembler::Zero, cond, cond, ToAddress(falseExpr), out);
1604 }
1605 return;
1606 }
1607
1608 FloatRegister out = ToFloatRegister(ins->output());
1609 MOZ_ASSERT(ToFloatRegister(ins->trueExpr()) == out,
1610 "true expr input is reused for output");
1611
1612 if (falseExpr->isFloatReg()) {
1613 if (mirType == MIRType::Float32) {
1614 masm.as_movz(Assembler::SingleFloat, out, ToFloatRegister(falseExpr),
1615 cond);
1616 } else if (mirType == MIRType::Double) {
1617 masm.as_movz(Assembler::DoubleFloat, out, ToFloatRegister(falseExpr),
1618 cond);
1619 } else {
1620 MOZ_CRASH("unhandled type in visitWasmSelect!");
1621 }
1622 } else {
1623 Label done;
1624 masm.ma_b(cond, cond, &done, Assembler::NonZero, ShortJump);
1625
1626 if (mirType == MIRType::Float32) {
1627 masm.loadFloat32(ToAddress(falseExpr), out);
1628 } else if (mirType == MIRType::Double) {
1629 masm.loadDouble(ToAddress(falseExpr), out);
1630 } else {
1631 MOZ_CRASH("unhandled type in visitWasmSelect!");
1632 }
1633
1634 masm.bind(&done);
1635 }
1636 }
1637
1638 // We expect to handle only the case where compare is {U,}Int32 and select is
1639 // {U,}Int32, and the "true" input is reused for the output.
1640 void CodeGenerator::visitWasmCompareAndSelect(LWasmCompareAndSelect* ins) {
1641 bool cmpIs32bit = ins->compareType() == MCompare::Compare_Int32 ||
1642 ins->compareType() == MCompare::Compare_UInt32;
1643 bool selIs32bit = ins->mir()->type() == MIRType::Int32;
1644
1645 MOZ_RELEASE_ASSERT(
1646 cmpIs32bit && selIs32bit,
1647 "CodeGenerator::visitWasmCompareAndSelect: unexpected types");
1648
1649 Register trueExprAndDest = ToRegister(ins->output());
1650 MOZ_ASSERT(ToRegister(ins->ifTrueExpr()) == trueExprAndDest,
1651 "true expr input is reused for output");
1652
1653 Assembler::Condition cond = Assembler::InvertCondition(
1654 JSOpToCondition(ins->compareType(), ins->jsop()));
1655 const LAllocation* rhs = ins->rightExpr();
1656 const LAllocation* falseExpr = ins->ifFalseExpr();
1657 Register lhs = ToRegister(ins->leftExpr());
1658
1659 masm.cmp32Move32(cond, lhs, ToRegister(rhs), ToRegister(falseExpr),
1660 trueExprAndDest);
1661 }
1662
1663 void CodeGenerator::visitUDivOrMod(LUDivOrMod* ins) {
1664 Register lhs = ToRegister(ins->lhs());
1665 Register rhs = ToRegister(ins->rhs());
1666 Register output = ToRegister(ins->output());
1667 Label done;
1668
1669 // Prevent divide by zero.
1670 if (ins->canBeDivideByZero()) {
1671 if (ins->mir()->isTruncated()) {
1672 if (ins->trapOnError()) {
1673 Label nonZero;
1674 masm.ma_b(rhs, rhs, &nonZero, Assembler::NonZero);
1675 masm.wasmTrap(wasm::Trap::IntegerDivideByZero, ins->trapSiteDesc());
1676 masm.bind(&nonZero);
1677 } else {
1678 // Infinity|0 == 0
1679 Label notzero;
1680 masm.ma_b(rhs, rhs, &notzero, Assembler::NonZero, ShortJump);
1681 masm.move32(Imm32(0), output);
1682 masm.ma_b(&done, ShortJump);
1683 masm.bind(&notzero);
1684 }
1685 } else {
1686 bailoutCmp32(Assembler::Equal, rhs, Imm32(0), ins->snapshot());
1687 }
1688 }
1689
1690 #ifdef MIPSR6
1691 masm.as_modu(output, lhs, rhs);
1692 #else
1693 masm.as_divu(lhs, rhs);
1694 masm.as_mfhi(output);
1695 #endif
1696
1697 // If the remainder is > 0, bailout since this must be a double.
1698 if (ins->mir()->isDiv()) {
1699 if (!ins->mir()->toDiv()->canTruncateRemainder()) {
1700 bailoutCmp32(Assembler::NonZero, output, output, ins->snapshot());
1701 }
1702 // Get quotient
1703 #ifdef MIPSR6
1704 masm.as_divu(output, lhs, rhs);
1705 #else
1706 masm.as_mflo(output);
1707 #endif
1708 }
1709
1710 if (!ins->mir()->isTruncated()) {
1711 bailoutCmp32(Assembler::LessThan, output, Imm32(0), ins->snapshot());
1712 }
1713
1714 masm.bind(&done);
1715 }
1716
1717 void CodeGenerator::visitEffectiveAddress(LEffectiveAddress* ins) {
1718 const MEffectiveAddress* mir = ins->mir();
1719 Register base = ToRegister(ins->base());
1720 Register index = ToRegister(ins->index());
1721 Register output = ToRegister(ins->output());
1722
1723 BaseIndex address(base, index, mir->scale(), mir->displacement());
1724 masm.computeEffectiveAddress(address, output);
1725 }
1726
1727 void CodeGenerator::visitNegI(LNegI* ins) {
1728 Register input = ToRegister(ins->input());
1729 Register output = ToRegister(ins->output());
1730
1731 masm.ma_negu(output, input);
1732 }
1733
1734 void CodeGenerator::visitNegI64(LNegI64* ins) {
1735 Register64 input = ToRegister64(ins->input());
1736 MOZ_ASSERT(input == ToOutRegister64(ins));
1737 masm.neg64(input);
1738 }
1739
1740 void CodeGenerator::visitNegD(LNegD* ins) {
1741 FloatRegister input = ToFloatRegister(ins->input());
1742 FloatRegister output = ToFloatRegister(ins->output());
1743
1744 masm.as_negd(output, input);
1745 }
1746
1747 void CodeGenerator::visitNegF(LNegF* ins) {
1748 FloatRegister input = ToFloatRegister(ins->input());
1749 FloatRegister output = ToFloatRegister(ins->output());
1750
1751 masm.as_negs(output, input);
1752 }
1753
1754 void CodeGenerator::visitWasmAddOffset(LWasmAddOffset* lir) {
1755 MWasmAddOffset* mir = lir->mir();
1756 Register base = ToRegister(lir->base());
1757 Register out = ToRegister(lir->output());
1758
1759 Label ok;
1760 masm.ma_add32TestCarry(Assembler::CarryClear, out, base, Imm32(mir->offset()),
1761 &ok);
1762 masm.wasmTrap(wasm::Trap::OutOfBounds, mir->trapSiteDesc());
1763 masm.bind(&ok);
1764 }
1765
1766 void CodeGenerator::visitWasmAddOffset64(LWasmAddOffset64* lir) {
1767 MWasmAddOffset* mir = lir->mir();
1768 Register64 base = ToRegister64(lir->base());
1769 Register64 out = ToOutRegister64(lir);
1770
1771 Label ok;
1772 masm.ma_addPtrTestCarry(Assembler::CarryClear, out.reg, base.reg,
1773 ImmWord(mir->offset()), &ok);
1774 masm.wasmTrap(wasm::Trap::OutOfBounds, mir->trapSiteDesc());
1775 masm.bind(&ok);
1776 }
1777
1778 void CodeGenerator::visitAtomicTypedArrayElementBinop(
1779 LAtomicTypedArrayElementBinop* lir) {
1780 MOZ_ASSERT(!lir->mir()->isForEffect());
1781
1782 AnyRegister output = ToAnyRegister(lir->output());
1783 Register elements = ToRegister(lir->elements());
1784 Register outTemp = ToTempRegisterOrInvalid(lir->temp0());
1785 Register valueTemp = ToTempRegisterOrInvalid(lir->temp1());
1786 Register offsetTemp = ToTempRegisterOrInvalid(lir->temp2());
1787 Register maskTemp = ToTempRegisterOrInvalid(lir->temp3());
1788 Register value = ToRegister(lir->value());
1789
1790 Scalar::Type arrayType = lir->mir()->arrayType();
1791
1792 if (lir->index()->isConstant()) {
1793 Address mem = ToAddress(elements, lir->index(), arrayType);
1794 masm.atomicFetchOpJS(arrayType, Synchronization::Full(),
1795 lir->mir()->operation(), value, mem, valueTemp,
1796 offsetTemp, maskTemp, outTemp, output);
1797 } else {
1798 BaseIndex mem(elements, ToRegister(lir->index()),
1799 ScaleFromScalarType(arrayType));
1800 masm.atomicFetchOpJS(arrayType, Synchronization::Full(),
1801 lir->mir()->operation(), value, mem, valueTemp,
1802 offsetTemp, maskTemp, outTemp, output);
1803 }
1804 }
1805
1806 void CodeGenerator::visitAtomicTypedArrayElementBinopForEffect(
1807 LAtomicTypedArrayElementBinopForEffect* lir) {
1808 MOZ_ASSERT(lir->mir()->isForEffect());
1809
1810 Register elements = ToRegister(lir->elements());
1811 Register valueTemp = ToTempRegisterOrInvalid(lir->temp0());
1812 Register offsetTemp = ToTempRegisterOrInvalid(lir->temp1());
1813 Register maskTemp = ToTempRegisterOrInvalid(lir->temp2());
1814 Register value = ToRegister(lir->value());
1815 Scalar::Type arrayType = lir->mir()->arrayType();
1816
1817 if (lir->index()->isConstant()) {
1818 Address mem = ToAddress(elements, lir->index(), arrayType);
1819 masm.atomicEffectOpJS(arrayType, Synchronization::Full(),
1820 lir->mir()->operation(), value, mem, valueTemp,
1821 offsetTemp, maskTemp);
1822 } else {
1823 BaseIndex mem(elements, ToRegister(lir->index()),
1824 ScaleFromScalarType(arrayType));
1825 masm.atomicEffectOpJS(arrayType, Synchronization::Full(),
1826 lir->mir()->operation(), value, mem, valueTemp,
1827 offsetTemp, maskTemp);
1828 }
1829 }
1830
1831 void CodeGenerator::visitCompareExchangeTypedArrayElement(
1832 LCompareExchangeTypedArrayElement* lir) {
1833 Register elements = ToRegister(lir->elements());
1834 AnyRegister output = ToAnyRegister(lir->output());
1835 Register outTemp = ToTempRegisterOrInvalid(lir->temp0());
1836
1837 Register oldval = ToRegister(lir->oldval());
1838 Register newval = ToRegister(lir->newval());
1839 Register valueTemp = ToTempRegisterOrInvalid(lir->temp1());
1840 Register offsetTemp = ToTempRegisterOrInvalid(lir->temp2());
1841 Register maskTemp = ToTempRegisterOrInvalid(lir->temp3());
1842
1843 Scalar::Type arrayType = lir->mir()->arrayType();
1844
1845 if (lir->index()->isConstant()) {
1846 Address dest = ToAddress(elements, lir->index(), arrayType);
1847 masm.compareExchangeJS(arrayType, Synchronization::Full(), dest, oldval,
1848 newval, valueTemp, offsetTemp, maskTemp, outTemp,
1849 output);
1850 } else {
1851 BaseIndex dest(elements, ToRegister(lir->index()),
1852 ScaleFromScalarType(arrayType));
1853 masm.compareExchangeJS(arrayType, Synchronization::Full(), dest, oldval,
1854 newval, valueTemp, offsetTemp, maskTemp, outTemp,
1855 output);
1856 }
1857 }
1858
1859 void CodeGenerator::visitAtomicExchangeTypedArrayElement(
1860 LAtomicExchangeTypedArrayElement* lir) {
1861 Register elements = ToRegister(lir->elements());
1862 AnyRegister output = ToAnyRegister(lir->output());
1863 Register outTemp = ToTempRegisterOrInvalid(lir->temp0());
1864
1865 Register value = ToRegister(lir->value());
1866 Register valueTemp = ToTempRegisterOrInvalid(lir->temp1());
1867 Register offsetTemp = ToTempRegisterOrInvalid(lir->temp2());
1868 Register maskTemp = ToTempRegisterOrInvalid(lir->temp3());
1869
1870 Scalar::Type arrayType = lir->mir()->arrayType();
1871
1872 if (lir->index()->isConstant()) {
1873 Address dest = ToAddress(elements, lir->index(), arrayType);
1874 masm.atomicExchangeJS(arrayType, Synchronization::Full(), dest, value,
1875 valueTemp, offsetTemp, maskTemp, outTemp, output);
1876 } else {
1877 BaseIndex dest(elements, ToRegister(lir->index()),
1878 ScaleFromScalarType(arrayType));
1879 masm.atomicExchangeJS(arrayType, Synchronization::Full(), dest, value,
1880 valueTemp, offsetTemp, maskTemp, outTemp, output);
1881 }
1882 }
1883
1884 void CodeGenerator::visitCompareExchangeTypedArrayElement64(
1885 LCompareExchangeTypedArrayElement64* lir) {
1886 Register elements = ToRegister(lir->elements());
1887 Register64 oldval = ToRegister64(lir->oldval());
1888 Register64 newval = ToRegister64(lir->newval());
1889 Register64 out = ToOutRegister64(lir);
1890
1891 Scalar::Type arrayType = lir->mir()->arrayType();
1892
1893 if (lir->index()->isConstant()) {
1894 Address dest = ToAddress(elements, lir->index(), arrayType);
1895 masm.compareExchange64(Synchronization::Full(), dest, oldval, newval, out);
1896 } else {
1897 BaseIndex dest(elements, ToRegister(lir->index()),
1898 ScaleFromScalarType(arrayType));
1899 masm.compareExchange64(Synchronization::Full(), dest, oldval, newval, out);
1900 }
1901 }
1902
1903 void CodeGenerator::visitAtomicExchangeTypedArrayElement64(
1904 LAtomicExchangeTypedArrayElement64* lir) {
1905 Register elements = ToRegister(lir->elements());
1906 Register64 value = ToRegister64(lir->value());
1907 Register64 out = ToOutRegister64(lir);
1908
1909 Scalar::Type arrayType = lir->mir()->arrayType();
1910
1911 if (lir->index()->isConstant()) {
1912 Address dest = ToAddress(elements, lir->index(), arrayType);
1913 masm.atomicExchange64(Synchronization::Full(), dest, value, out);
1914 } else {
1915 BaseIndex dest(elements, ToRegister(lir->index()),
1916 ScaleFromScalarType(arrayType));
1917 masm.atomicExchange64(Synchronization::Full(), dest, value, out);
1918 }
1919 }
1920
1921 void CodeGenerator::visitAtomicTypedArrayElementBinop64(
1922 LAtomicTypedArrayElementBinop64* lir) {
1923 MOZ_ASSERT(lir->mir()->hasUses());
1924
1925 Register elements = ToRegister(lir->elements());
1926 Register64 value = ToRegister64(lir->value());
1927 Register64 temp = ToRegister64(lir->temp0());
1928 Register64 out = ToOutRegister64(lir);
1929
1930 Scalar::Type arrayType = lir->mir()->arrayType();
1931 AtomicOp atomicOp = lir->mir()->operation();
1932
1933 if (lir->index()->isConstant()) {
1934 Address dest = ToAddress(elements, lir->index(), arrayType);
1935 masm.atomicFetchOp64(Synchronization::Full(), atomicOp, value, dest, temp,
1936 out);
1937 } else {
1938 BaseIndex dest(elements, ToRegister(lir->index()),
1939 ScaleFromScalarType(arrayType));
1940 masm.atomicFetchOp64(Synchronization::Full(), atomicOp, value, dest, temp,
1941 out);
1942 }
1943 }
1944
1945 void CodeGenerator::visitAtomicTypedArrayElementBinopForEffect64(
1946 LAtomicTypedArrayElementBinopForEffect64* lir) {
1947 MOZ_ASSERT(!lir->mir()->hasUses());
1948
1949 Register elements = ToRegister(lir->elements());
1950 Register64 value = ToRegister64(lir->value());
1951 Register64 temp = ToRegister64(lir->temp0());
1952
1953 Scalar::Type arrayType = lir->mir()->arrayType();
1954 AtomicOp atomicOp = lir->mir()->operation();
1955
1956 if (lir->index()->isConstant()) {
1957 Address dest = ToAddress(elements, lir->index(), arrayType);
1958 masm.atomicEffectOp64(Synchronization::Full(), atomicOp, value, dest, temp);
1959 } else {
1960 BaseIndex dest(elements, ToRegister(lir->index()),
1961 ScaleFromScalarType(arrayType));
1962 masm.atomicEffectOp64(Synchronization::Full(), atomicOp, value, dest, temp);
1963 }
1964 }
1965
1966 void CodeGenerator::visitWasmCompareExchangeI64(LWasmCompareExchangeI64* lir) {
1967 Register memoryBase = ToRegister(lir->memoryBase());
1968 Register ptr = ToRegister(lir->ptr());
1969 Register64 oldValue = ToRegister64(lir->oldValue());
1970 Register64 newValue = ToRegister64(lir->newValue());
1971 Register64 output = ToOutRegister64(lir);
1972 uint32_t offset = lir->mir()->access().offset32();
1973
1974 BaseIndex addr(memoryBase, ptr, TimesOne, offset);
1975 masm.wasmCompareExchange64(lir->mir()->access(), addr, oldValue, newValue,
1976 output);
1977 }
1978
1979 void CodeGenerator::visitWasmAtomicExchangeI64(LWasmAtomicExchangeI64* lir) {
1980 Register memoryBase = ToRegister(lir->memoryBase());
1981 Register ptr = ToRegister(lir->ptr());
1982 Register64 value = ToRegister64(lir->value());
1983 Register64 output = ToOutRegister64(lir);
1984 uint32_t offset = lir->mir()->access().offset32();
1985
1986 BaseIndex addr(memoryBase, ptr, TimesOne, offset);
1987 masm.wasmAtomicExchange64(lir->mir()->access(), addr, value, output);
1988 }
1989
1990 void CodeGenerator::visitWasmAtomicBinopI64(LWasmAtomicBinopI64* lir) {
1991 Register memoryBase = ToRegister(lir->memoryBase());
1992 Register ptr = ToRegister(lir->ptr());
1993 Register64 value = ToRegister64(lir->value());
1994 Register64 output = ToOutRegister64(lir);
1995 Register64 temp = ToRegister64(lir->temp0());
1996 uint32_t offset = lir->mir()->access().offset32();
1997
1998 BaseIndex addr(memoryBase, ptr, TimesOne, offset);
1999
2000 masm.wasmAtomicFetchOp64(lir->mir()->access(), lir->mir()->operation(), value,
2001 addr, temp, output);
2002 }
2003
2004 void CodeGenerator::visitNearbyInt(LNearbyInt*) { MOZ_CRASH("NYI"); }
2005
2006 void CodeGenerator::visitNearbyIntF(LNearbyIntF*) { MOZ_CRASH("NYI"); }
2007
2008 void CodeGenerator::visitSimd128(LSimd128* ins) { MOZ_CRASH("No SIMD"); }
2009
2010 void CodeGenerator::visitWasmTernarySimd128(LWasmTernarySimd128* ins) {
2011 MOZ_CRASH("No SIMD");
2012 }
2013
2014 void CodeGenerator::visitWasmBinarySimd128(LWasmBinarySimd128* ins) {
2015 MOZ_CRASH("No SIMD");
2016 }
2017
2018 void CodeGenerator::visitWasmBinarySimd128WithConstant(
2019 LWasmBinarySimd128WithConstant* ins) {
2020 MOZ_CRASH("No SIMD");
2021 }
2022
2023 void CodeGenerator::visitWasmVariableShiftSimd128(
2024 LWasmVariableShiftSimd128* ins) {
2025 MOZ_CRASH("No SIMD");
2026 }
2027
2028 void CodeGenerator::visitWasmConstantShiftSimd128(
2029 LWasmConstantShiftSimd128* ins) {
2030 MOZ_CRASH("No SIMD");
2031 }
2032
2033 void CodeGenerator::visitWasmSignReplicationSimd128(
2034 LWasmSignReplicationSimd128* ins) {
2035 MOZ_CRASH("No SIMD");
2036 }
2037
2038 void CodeGenerator::visitWasmShuffleSimd128(LWasmShuffleSimd128* ins) {
2039 MOZ_CRASH("No SIMD");
2040 }
2041
2042 void CodeGenerator::visitWasmPermuteSimd128(LWasmPermuteSimd128* ins) {
2043 MOZ_CRASH("No SIMD");
2044 }
2045
2046 void CodeGenerator::visitWasmReplaceLaneSimd128(LWasmReplaceLaneSimd128* ins) {
2047 MOZ_CRASH("No SIMD");
2048 }
2049
2050 void CodeGenerator::visitWasmReplaceInt64LaneSimd128(
2051 LWasmReplaceInt64LaneSimd128* ins) {
2052 MOZ_CRASH("No SIMD");
2053 }
2054
2055 void CodeGenerator::visitWasmScalarToSimd128(LWasmScalarToSimd128* ins) {
2056 MOZ_CRASH("No SIMD");
2057 }
2058
2059 void CodeGenerator::visitWasmInt64ToSimd128(LWasmInt64ToSimd128* ins) {
2060 MOZ_CRASH("No SIMD");
2061 }
2062
2063 void CodeGenerator::visitWasmUnarySimd128(LWasmUnarySimd128* ins) {
2064 MOZ_CRASH("No SIMD");
2065 }
2066
2067 void CodeGenerator::visitWasmReduceSimd128(LWasmReduceSimd128* ins) {
2068 MOZ_CRASH("No SIMD");
2069 }
2070
2071 void CodeGenerator::visitWasmReduceAndBranchSimd128(
2072 LWasmReduceAndBranchSimd128* ins) {
2073 MOZ_CRASH("No SIMD");
2074 }
2075
2076 void CodeGenerator::visitWasmReduceSimd128ToInt64(
2077 LWasmReduceSimd128ToInt64* ins) {
2078 MOZ_CRASH("No SIMD");
2079 }
2080
2081 void CodeGenerator::visitWasmLoadLaneSimd128(LWasmLoadLaneSimd128* ins) {
2082 MOZ_CRASH("No SIMD");
2083 }
2084
2085 void CodeGenerator::visitWasmStoreLaneSimd128(LWasmStoreLaneSimd128* ins) {
2086 MOZ_CRASH("No SIMD");
2087 }