Debian Codegraph @

js/src/jit/x86/CodeGenerator-x86.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/x86/CodeGenerator-x86.h"
8
9 #include "mozilla/Casting.h"
10 #include "mozilla/DebugOnly.h"
11
12 #include <iterator>
13
14 #include "jsnum.h"
15
16 #include "jit/CodeGenerator.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/Shape.h"
22 #include "wasm/WasmBuiltins.h"
23 #include "wasm/WasmCodegenTypes.h"
24 #include "wasm/WasmInstanceData.h"
25
26 #include "jit/MacroAssembler-inl.h"
27 #include "jit/shared/CodeGenerator-shared-inl.h"
28 #include "vm/JSScript-inl.h"
29
30 using namespace js;
31 using namespace js::jit;
32
33 using JS::GenericNaN;
34 using mozilla::BitwiseCast;
35 using mozilla::DebugOnly;
36 using mozilla::FloatingPoint;
37
38 CodeGeneratorX86::CodeGeneratorX86(MIRGenerator* gen, LIRGraph* graph,
39 MacroAssembler* masm,
40 const wasm::CodeMetadata* wasmCodeMeta)
41 : CodeGeneratorX86Shared(gen, graph, masm, wasmCodeMeta) {}
42
43 void CodeGenerator::visitBox(LBox* box) {
44 const LDefinition* type = box->getDef(TYPE_INDEX);
45
46 MOZ_ASSERT(!box->payload()->isConstant());
47
48 // On x86, the input operand and the output payload have the same
49 // virtual register. All that needs to be written is the type tag for
50 // the type definition.
51 masm.mov(ImmWord(MIRTypeToTag(box->type())), ToRegister(type));
52 }
53
54 void CodeGenerator::visitBoxFloatingPoint(LBoxFloatingPoint* box) {
55 const AnyRegister in = ToAnyRegister(box->input());
56 const ValueOperand out = ToOutValue(box);
57
58 masm.moveValue(TypedOrValueRegister(box->type(), in), out);
59
60 if (JitOptions.spectreValueMasking) {
61 Register scratch = ToRegister(box->temp1());
62 masm.move32(Imm32(JSVAL_TAG_CLEAR), scratch);
63 masm.cmp32Move32(Assembler::Below, scratch, out.typeReg(), scratch,
64 out.typeReg());
65 }
66 }
67
68 void CodeGenerator::visitUnbox(LUnbox* unbox) {
69 // Note that for unbox, the type and payload indexes are switched on the
70 // inputs.
71 Operand type = ToOperand(unbox->type());
72 Operand payload = ToOperand(unbox->payload());
73 Register output = ToRegister(unbox->output());
74 MUnbox* mir = unbox->mir();
75
76 JSValueTag tag = MIRTypeToTag(mir->type());
77 if (mir->fallible()) {
78 masm.cmp32(type, Imm32(tag));
79 bailoutIf(Assembler::NotEqual, unbox->snapshot());
80 } else {
81 #ifdef DEBUG
82 Label ok;
83 masm.branch32(Assembler::Equal, type, Imm32(tag), &ok);
84 masm.assumeUnreachable("Infallible unbox type mismatch");
85 masm.bind(&ok);
86 #endif
87 }
88
89 // Note: If spectreValueMasking is disabled, then this instruction will
90 // default to a no-op as long as the lowering allocate the same register for
91 // the output and the payload.
92 masm.unboxNonDouble(type, payload, output, ValueTypeFromMIRType(mir->type()));
93 }
94
95 void CodeGenerator::visitAtomicLoad64(LAtomicLoad64* lir) {
96 Register elements = ToRegister(lir->elements());
97
98 MOZ_ASSERT(ToOutRegister64(lir) == Register64(edx, eax));
99 MOZ_ASSERT(ToRegister64(lir->temp0()) == Register64(ecx, ebx));
100
101 const MLoadUnboxedScalar* mir = lir->mir();
102
103 Scalar::Type storageType = mir->storageType();
104
105 if (lir->index()->isConstant()) {
106 Address source =
107 ToAddress(elements, lir->index(), storageType, mir->offsetAdjustment());
108 masm.atomicLoad64(Synchronization::Load(), source, Register64(ecx, ebx),
109 Register64(edx, eax));
110 } else {
111 BaseIndex source(elements, ToRegister(lir->index()),
112 ScaleFromScalarType(storageType), mir->offsetAdjustment());
113 masm.atomicLoad64(Synchronization::Load(), source, Register64(ecx, ebx),
114 Register64(edx, eax));
115 }
116 }
117
118 void CodeGenerator::visitAtomicStore64(LAtomicStore64* lir) {
119 Register elements = ToRegister(lir->elements());
120 Register64 value = ToRegister64(lir->value());
121 Register64 temp = ToRegister64(lir->temp0());
122
123 MOZ_ASSERT(value == Register64(ecx, ebx));
124 MOZ_ASSERT(temp == Register64(edx, eax));
125
126 Scalar::Type writeType = lir->mir()->writeType();
127
128 if (lir->index()->isConstant()) {
129 Address dest = ToAddress(elements, lir->index(), writeType);
130 masm.atomicStore64(Synchronization::Store(), dest, value, temp);
131 } else {
132 BaseIndex dest(elements, ToRegister(lir->index()),
133 ScaleFromScalarType(writeType));
134 masm.atomicStore64(Synchronization::Store(), dest, value, temp);
135 }
136 }
137
138 void CodeGenerator::visitCompareExchangeTypedArrayElement64(
139 LCompareExchangeTypedArrayElement64* lir) {
140 Register elements = ToRegister(lir->elements());
141 Register64 oldval = ToRegister64(lir->oldval());
142 Register64 newval = ToRegister64(lir->newval());
143 Register64 out = ToOutRegister64(lir);
144
145 MOZ_ASSERT(oldval == Register64(edx, eax));
146 MOZ_ASSERT(newval == Register64(ecx, ebx));
147 MOZ_ASSERT(out == oldval);
148
149 Scalar::Type arrayType = lir->mir()->arrayType();
150
151 if (lir->index()->isConstant()) {
152 Address dest = ToAddress(elements, lir->index(), arrayType);
153 masm.compareExchange64(Synchronization::Full(), dest, oldval, newval, out);
154 } else {
155 BaseIndex dest(elements, ToRegister(lir->index()),
156 ScaleFromScalarType(arrayType));
157 masm.compareExchange64(Synchronization::Full(), dest, oldval, newval, out);
158 }
159 }
160
161 void CodeGenerator::visitAtomicExchangeTypedArrayElement64(
162 LAtomicExchangeTypedArrayElement64* lir) {
163 Register elements = ToRegister(lir->elements());
164 Register64 value = ToRegister64(lir->value());
165 Register64 out = ToOutRegister64(lir);
166
167 MOZ_ASSERT(value == Register64(ecx, ebx));
168 MOZ_ASSERT(out == Register64(edx, eax));
169
170 Scalar::Type arrayType = lir->mir()->arrayType();
171
172 if (lir->index()->isConstant()) {
173 Address dest = ToAddress(elements, lir->index(), arrayType);
174 masm.atomicExchange64(Synchronization::Full(), dest, value, out);
175 } else {
176 BaseIndex dest(elements, ToRegister(lir->index()),
177 ScaleFromScalarType(arrayType));
178 masm.atomicExchange64(Synchronization::Full(), dest, value, out);
179 }
180 }
181
182 void CodeGenerator::visitAtomicTypedArrayElementBinop64(
183 LAtomicTypedArrayElementBinop64* lir) {
184 MOZ_ASSERT(!lir->mir()->isForEffect());
185
186 Register elements = ToRegister(lir->elements());
187 Register64 value = ToRegister64(lir->value());
188 Register64 out = ToOutRegister64(lir);
189
190 MOZ_ASSERT(value == Register64(ecx, ebx));
191 MOZ_ASSERT(out == Register64(edx, eax));
192
193 Scalar::Type arrayType = lir->mir()->arrayType();
194 AtomicOp atomicOp = lir->mir()->operation();
195
196 // Save |value| before it's clobbered below.
197 masm.push64(value);
198
199 Address addr(masm.getStackPointer(), 0);
200
201 if (lir->index()->isConstant()) {
202 Address dest = ToAddress(elements, lir->index(), arrayType);
203 masm.atomicFetchOp64(Synchronization::Full(), atomicOp, addr, dest, value,
204 out);
205 } else {
206 BaseIndex dest(elements, ToRegister(lir->index()),
207 ScaleFromScalarType(arrayType));
208 masm.atomicFetchOp64(Synchronization::Full(), atomicOp, addr, dest, value,
209 out);
210 }
211
212 masm.pop64(value);
213 }
214
215 void CodeGenerator::visitAtomicTypedArrayElementBinopForEffect64(
216 LAtomicTypedArrayElementBinopForEffect64* lir) {
217 MOZ_ASSERT(lir->mir()->isForEffect());
218
219 Register elements = ToRegister(lir->elements());
220 Register64 value = ToRegister64(lir->value());
221 Register64 temp = ToRegister64(lir->temp0());
222
223 MOZ_ASSERT(value == Register64(ecx, ebx));
224 MOZ_ASSERT(temp == Register64(edx, eax));
225
226 Scalar::Type arrayType = lir->mir()->arrayType();
227 AtomicOp atomicOp = lir->mir()->operation();
228
229 // Save |value| before it's clobbered below.
230 masm.push64(value);
231
232 Address addr(masm.getStackPointer(), 0);
233
234 if (lir->index()->isConstant()) {
235 Address dest = ToAddress(elements, lir->index(), arrayType);
236 masm.atomicFetchOp64(Synchronization::Full(), atomicOp, addr, dest, value,
237 temp);
238 } else {
239 BaseIndex dest(elements, ToRegister(lir->index()),
240 ScaleFromScalarType(arrayType));
241 masm.atomicFetchOp64(Synchronization::Full(), atomicOp, addr, dest, value,
242 temp);
243 }
244
245 masm.pop64(value);
246 }
247
248 void CodeGenerator::visitWasmUint32ToDouble(LWasmUint32ToDouble* lir) {
249 Register input = ToRegister(lir->input());
250 Register temp = ToRegister(lir->temp0());
251
252 if (input != temp) {
253 masm.mov(input, temp);
254 }
255
256 // Beware: convertUInt32ToDouble clobbers input.
257 masm.convertUInt32ToDouble(temp, ToFloatRegister(lir->output()));
258 }
259
260 void CodeGenerator::visitWasmUint32ToFloat32(LWasmUint32ToFloat32* lir) {
261 Register input = ToRegister(lir->input());
262 Register temp = ToRegister(lir->temp0());
263 FloatRegister output = ToFloatRegister(lir->output());
264
265 if (input != temp) {
266 masm.mov(input, temp);
267 }
268
269 // Beware: convertUInt32ToFloat32 clobbers input.
270 masm.convertUInt32ToFloat32(temp, output);
271 }
272
273 template <typename T>
274 void CodeGeneratorX86::emitWasmLoad(T* ins) {
275 const MWasmLoad* mir = ins->mir();
276
277 mir->access().assertOffsetInGuardPages();
278 uint32_t offset = mir->access().offset32();
279
280 const LAllocation* ptr = ins->ptr();
281 const LAllocation* memoryBase = ins->memoryBase();
282
283 // Lowering has set things up so that we can use a BaseIndex form if the
284 // pointer is constant and the offset is zero, or if the pointer is zero.
285
286 Operand srcAddr =
287 ptr->isBogus()
288 ? Operand(ToRegister(memoryBase),
289 offset ? offset : mir->base()->toConstant()->toInt32())
290 : Operand(ToRegister(memoryBase), ToRegister(ptr), TimesOne, offset);
291
292 if (mir->type() == MIRType::Int64) {
293 MOZ_ASSERT_IF(mir->access().isAtomic(),
294 mir->access().type() != Scalar::Int64);
295 masm.wasmLoadI64(mir->access(), srcAddr, ToOutRegister64(ins));
296 } else {
297 masm.wasmLoad(mir->access(), srcAddr, ToAnyRegister(ins->output()));
298 }
299 }
300
301 void CodeGenerator::visitWasmLoad(LWasmLoad* ins) { emitWasmLoad(ins); }
302
303 void CodeGenerator::visitWasmLoadI64(LWasmLoadI64* ins) { emitWasmLoad(ins); }
304
305 template <typename T>
306 void CodeGeneratorX86::emitWasmStore(T* ins) {
307 const MWasmStore* mir = ins->mir();
308
309 mir->access().assertOffsetInGuardPages();
310 uint32_t offset = mir->access().offset32();
311
312 const LAllocation* ptr = ins->ptr();
313 const LAllocation* memoryBase = ins->memoryBase();
314
315 // Lowering has set things up so that we can use a BaseIndex form if the
316 // pointer is constant and the offset is zero, or if the pointer is zero.
317
318 Operand dstAddr =
319 ptr->isBogus()
320 ? Operand(ToRegister(memoryBase),
321 offset ? offset : mir->base()->toConstant()->toInt32())
322 : Operand(ToRegister(memoryBase), ToRegister(ptr), TimesOne, offset);
323
324 if constexpr (std::is_same_v<T, LWasmStoreI64>) {
325 Register64 value = ToRegister64(ins->value());
326 masm.wasmStoreI64(mir->access(), value, dstAddr);
327 } else {
328 AnyRegister value = ToAnyRegister(ins->value());
329 masm.wasmStore(mir->access(), value, dstAddr);
330 }
331 }
332
333 void CodeGenerator::visitWasmStore(LWasmStore* ins) { emitWasmStore(ins); }
334
335 void CodeGenerator::visitWasmStoreI64(LWasmStoreI64* ins) {
336 emitWasmStore(ins);
337 }
338
339 void CodeGenerator::visitWasmCompareExchangeHeap(
340 LWasmCompareExchangeHeap* ins) {
341 MWasmCompareExchangeHeap* mir = ins->mir();
342
343 Register ptrReg = ToRegister(ins->ptr());
344 Register oldval = ToRegister(ins->oldValue());
345 Register newval = ToRegister(ins->newValue());
346 Register addrTemp = ToRegister(ins->temp0());
347 Register memoryBase = ToRegister(ins->memoryBase());
348 Register output = ToRegister(ins->output());
349
350 masm.leal(Operand(memoryBase, ptrReg, TimesOne, mir->access().offset32()),
351 addrTemp);
352
353 Address memAddr(addrTemp, 0);
354 masm.wasmCompareExchange(mir->access(), memAddr, oldval, newval, output);
355 }
356
357 void CodeGenerator::visitWasmAtomicExchangeHeap(LWasmAtomicExchangeHeap* ins) {
358 MWasmAtomicExchangeHeap* mir = ins->mir();
359
360 Register ptrReg = ToRegister(ins->ptr());
361 Register value = ToRegister(ins->value());
362 Register addrTemp = ToRegister(ins->temp0());
363 Register memoryBase = ToRegister(ins->memoryBase());
364 Register output = ToRegister(ins->output());
365
366 masm.leal(Operand(memoryBase, ptrReg, TimesOne, mir->access().offset32()),
367 addrTemp);
368
369 Address memAddr(addrTemp, 0);
370 masm.wasmAtomicExchange(mir->access(), memAddr, value, output);
371 }
372
373 void CodeGenerator::visitWasmAtomicBinopHeap(LWasmAtomicBinopHeap* ins) {
374 MWasmAtomicBinopHeap* mir = ins->mir();
375
376 Register ptrReg = ToRegister(ins->ptr());
377 Register temp = ToTempRegisterOrInvalid(ins->temp0());
378 Register addrTemp = ToRegister(ins->temp1());
379 Register out = ToRegister(ins->output());
380 const LAllocation* value = ins->value();
381 AtomicOp op = mir->operation();
382 Register memoryBase = ToRegister(ins->memoryBase());
383
384 masm.leal(Operand(memoryBase, ptrReg, TimesOne, mir->access().offset32()),
385 addrTemp);
386
387 Address memAddr(addrTemp, 0);
388 if (value->isConstant()) {
389 masm.wasmAtomicFetchOp(mir->access(), op, Imm32(ToInt32(value)), memAddr,
390 temp, out);
391 } else {
392 masm.wasmAtomicFetchOp(mir->access(), op, ToRegister(value), memAddr, temp,
393 out);
394 }
395 }
396
397 void CodeGenerator::visitWasmAtomicBinopHeapForEffect(
398 LWasmAtomicBinopHeapForEffect* ins) {
399 MWasmAtomicBinopHeap* mir = ins->mir();
400 MOZ_ASSERT(!mir->hasUses());
401
402 Register ptrReg = ToRegister(ins->ptr());
403 Register addrTemp = ToRegister(ins->temp0());
404 const LAllocation* value = ins->value();
405 AtomicOp op = mir->operation();
406 Register memoryBase = ToRegister(ins->memoryBase());
407
408 masm.leal(Operand(memoryBase, ptrReg, TimesOne, mir->access().offset32()),
409 addrTemp);
410
411 Address memAddr(addrTemp, 0);
412 if (value->isConstant()) {
413 masm.wasmAtomicEffectOp(mir->access(), op, Imm32(ToInt32(value)), memAddr,
414 InvalidReg);
415 } else {
416 masm.wasmAtomicEffectOp(mir->access(), op, ToRegister(value), memAddr,
417 InvalidReg);
418 }
419 }
420
421 void CodeGenerator::visitWasmAtomicLoadI64(LWasmAtomicLoadI64* ins) {
422 ins->mir()->access().assertOffsetInGuardPages();
423 uint32_t offset = ins->mir()->access().offset32();
424
425 const LAllocation* memoryBase = ins->memoryBase();
426 const LAllocation* ptr = ins->ptr();
427 BaseIndex srcAddr(ToRegister(memoryBase), ToRegister(ptr), TimesOne, offset);
428
429 MOZ_ASSERT(ToRegister64(ins->temp0()) == Register64(ecx, ebx));
430 MOZ_ASSERT(ToOutRegister64(ins) == Register64(edx, eax));
431
432 masm.wasmAtomicLoad64(ins->mir()->access(), srcAddr, Register64(ecx, ebx),
433 Register64(edx, eax));
434 }
435
436 void CodeGenerator::visitWasmCompareExchangeI64(LWasmCompareExchangeI64* ins) {
437 ins->mir()->access().assertOffsetInGuardPages();
438 uint32_t offset = ins->mir()->access().offset32();
439
440 const LAllocation* memoryBase = ins->memoryBase();
441 const LAllocation* ptr = ins->ptr();
442 Operand srcAddr(ToRegister(memoryBase), ToRegister(ptr), TimesOne, offset);
443
444 MOZ_ASSERT(ToRegister64(ins->expected()).low == eax);
445 MOZ_ASSERT(ToRegister64(ins->expected()).high == edx);
446 MOZ_ASSERT(ToRegister64(ins->replacement()).low == ebx);
447 MOZ_ASSERT(ToRegister64(ins->replacement()).high == ecx);
448 MOZ_ASSERT(ToOutRegister64(ins).low == eax);
449 MOZ_ASSERT(ToOutRegister64(ins).high == edx);
450
451 masm.append(ins->mir()->access(), wasm::TrapMachineInsn::Atomic,
452 FaultingCodeOffset(masm.currentOffset()));
453 masm.lock_cmpxchg8b(edx, eax, ecx, ebx, srcAddr);
454 }
455
456 template <typename T>
457 void CodeGeneratorX86::emitWasmStoreOrExchangeAtomicI64(
458 T* ins, const wasm::MemoryAccessDesc& access) {
459 access.assertOffsetInGuardPages();
460 const LAllocation* memoryBase = ins->memoryBase();
461 const LAllocation* ptr = ins->ptr();
462 Operand srcAddr(ToRegister(memoryBase), ToRegister(ptr), TimesOne,
463 access.offset32());
464
465 MOZ_ASSERT(ToRegister64(ins->value()) == Register64(ecx, ebx));
466
467 // eax and edx will be overwritten every time through the loop but
468 // memoryBase and ptr must remain live for a possible second iteration.
469
470 MOZ_ASSERT(ToRegister(memoryBase) != edx && ToRegister(memoryBase) != eax);
471 MOZ_ASSERT(ToRegister(ptr) != edx && ToRegister(ptr) != eax);
472
473 Label again;
474 masm.bind(&again);
475 masm.append(access, wasm::TrapMachineInsn::Atomic,
476 FaultingCodeOffset(masm.currentOffset()));
477 masm.lock_cmpxchg8b(edx, eax, ecx, ebx, srcAddr);
478 masm.j(Assembler::Condition::NonZero, &again);
479 }
480
481 void CodeGenerator::visitWasmAtomicStoreI64(LWasmAtomicStoreI64* ins) {
482 MOZ_ASSERT(ToRegister64(ins->temp0()) == Register64(edx, eax));
483
484 emitWasmStoreOrExchangeAtomicI64(ins, ins->mir()->access());
485 }
486
487 void CodeGenerator::visitWasmAtomicExchangeI64(LWasmAtomicExchangeI64* ins) {
488 MOZ_ASSERT(ToOutRegister64(ins) == Register64(edx, eax));
489
490 emitWasmStoreOrExchangeAtomicI64(ins, ins->access());
491 }
492
493 void CodeGenerator::visitWasmAtomicBinopI64(LWasmAtomicBinopI64* ins) {
494 ins->access().assertOffsetInGuardPages();
495 uint32_t offset = ins->access().offset32();
496
497 const LAllocation* memoryBase = ins->memoryBase();
498 const LAllocation* ptr = ins->ptr();
499
500 BaseIndex srcAddr(ToRegister(memoryBase), ToRegister(ptr), TimesOne, offset);
501
502 MOZ_ASSERT(ToRegister(memoryBase) == esi || ToRegister(memoryBase) == edi);
503 MOZ_ASSERT(ToRegister(ptr) == esi || ToRegister(ptr) == edi);
504
505 Register64 value = ToRegister64(ins->value());
506
507 MOZ_ASSERT(value.low == ebx);
508 MOZ_ASSERT(value.high == ecx);
509
510 Register64 output = ToOutRegister64(ins);
511
512 MOZ_ASSERT(output.low == eax);
513 MOZ_ASSERT(output.high == edx);
514
515 masm.Push(ecx);
516 masm.Push(ebx);
517
518 Address valueAddr(esp, 0);
519
520 // Here the `value` register acts as a temp, we'll restore it below.
521 masm.wasmAtomicFetchOp64(ins->access(), ins->operation(), valueAddr, srcAddr,
522 value, output);
523
524 masm.Pop(ebx);
525 masm.Pop(ecx);
526 }
527
528 namespace js {
529 namespace jit {
530
531 class OutOfLineTruncate : public OutOfLineCodeBase<CodeGeneratorX86> {
532 LInstruction* ins_;
533
534 public:
535 explicit OutOfLineTruncate(LInstruction* ins) : ins_(ins) {
536 MOZ_ASSERT(ins_->isTruncateDToInt32() ||
537 ins_->isWasmBuiltinTruncateDToInt32());
538 }
539
540 void accept(CodeGeneratorX86* codegen) override {
541 codegen->visitOutOfLineTruncate(this);
542 }
543
544 LAllocation* input() { return ins_->getOperand(0); }
545 LDefinition* output() { return ins_->getDef(0); }
546 LDefinition* tempFloat() { return ins_->getTemp(0); }
547
548 const wasm::TrapSiteDesc& trapSiteDesc() const {
549 if (ins_->isTruncateDToInt32()) {
550 return ins_->toTruncateDToInt32()->mir()->trapSiteDesc();
551 }
552
553 return ins_->toWasmBuiltinTruncateDToInt32()->mir()->trapSiteDesc();
554 }
555 };
556
557 class OutOfLineTruncateFloat32 : public OutOfLineCodeBase<CodeGeneratorX86> {
558 LInstruction* ins_;
559
560 public:
561 explicit OutOfLineTruncateFloat32(LInstruction* ins) : ins_(ins) {
562 MOZ_ASSERT(ins_->isTruncateFToInt32() ||
563 ins_->isWasmBuiltinTruncateFToInt32());
564 }
565
566 void accept(CodeGeneratorX86* codegen) override {
567 codegen->visitOutOfLineTruncateFloat32(this);
568 }
569
570 LAllocation* input() { return ins_->getOperand(0); }
571 LDefinition* output() { return ins_->getDef(0); }
572 LDefinition* tempFloat() { return ins_->getTemp(0); }
573
574 const wasm::TrapSiteDesc& trapSiteDesc() const {
575 if (ins_->isTruncateFToInt32()) {
576 return ins_->toTruncateDToInt32()->mir()->trapSiteDesc();
577 }
578
579 return ins_->toWasmBuiltinTruncateFToInt32()->mir()->trapSiteDesc();
580 }
581 };
582
583 } // namespace jit
584 } // namespace js
585
586 void CodeGenerator::visitTruncateDToInt32(LTruncateDToInt32* ins) {
587 FloatRegister input = ToFloatRegister(ins->input());
588 Register output = ToRegister(ins->output());
589
590 OutOfLineTruncate* ool = new (alloc()) OutOfLineTruncate(ins);
591 addOutOfLineCode(ool, ins->mir());
592
593 masm.branchTruncateDoubleMaybeModUint32(input, output, ool->entry());
594 masm.bind(ool->rejoin());
595 }
596
597 void CodeGenerator::visitWasmBuiltinTruncateDToInt32(
598 LWasmBuiltinTruncateDToInt32* lir) {
599 FloatRegister input = ToFloatRegister(lir->input());
600 Register output = ToRegister(lir->output());
601
602 OutOfLineTruncate* ool = new (alloc()) OutOfLineTruncate(lir);
603 addOutOfLineCode(ool, lir->mir());
604
605 masm.branchTruncateDoubleMaybeModUint32(input, output, ool->entry());
606 masm.bind(ool->rejoin());
607 }
608
609 void CodeGenerator::visitTruncateFToInt32(LTruncateFToInt32* ins) {
610 FloatRegister input = ToFloatRegister(ins->input());
611 Register output = ToRegister(ins->output());
612
613 OutOfLineTruncateFloat32* ool = new (alloc()) OutOfLineTruncateFloat32(ins);
614 addOutOfLineCode(ool, ins->mir());
615
616 masm.branchTruncateFloat32MaybeModUint32(input, output, ool->entry());
617 masm.bind(ool->rejoin());
618 }
619
620 void CodeGenerator::visitWasmBuiltinTruncateFToInt32(
621 LWasmBuiltinTruncateFToInt32* lir) {
622 FloatRegister input = ToFloatRegister(lir->input());
623 Register output = ToRegister(lir->output());
624
625 OutOfLineTruncateFloat32* ool = new (alloc()) OutOfLineTruncateFloat32(lir);
626 addOutOfLineCode(ool, lir->mir());
627
628 masm.branchTruncateFloat32MaybeModUint32(input, output, ool->entry());
629 masm.bind(ool->rejoin());
630 }
631
632 void CodeGeneratorX86::visitOutOfLineTruncate(OutOfLineTruncate* ool) {
633 FloatRegister input = ToFloatRegister(ool->input());
634 Register output = ToRegister(ool->output());
635
636 Label fail;
637
638 if (Assembler::HasSSE3()) {
639 Label failPopDouble;
640 // Push double.
641 masm.subl(Imm32(sizeof(double)), esp);
642 masm.storeDouble(input, Operand(esp, 0));
643
644 // Check exponent to avoid fp exceptions.
645 masm.branchDoubleNotInInt64Range(Address(esp, 0), output, &failPopDouble);
646
647 // Load double, perform 64-bit truncation.
648 masm.truncateDoubleToInt64(Address(esp, 0), Address(esp, 0), output);
649
650 // Load low word, pop double and jump back.
651 masm.load32(Address(esp, 0), output);
652 masm.addl(Imm32(sizeof(double)), esp);
653 masm.jump(ool->rejoin());
654
655 masm.bind(&failPopDouble);
656 masm.addl(Imm32(sizeof(double)), esp);
657 masm.jump(&fail);
658 } else {
659 FloatRegister temp = ToFloatRegister(ool->tempFloat());
660
661 // Try to convert doubles representing integers within 2^32 of a signed
662 // integer, by adding/subtracting 2^32 and then trying to convert to int32.
663 // This has to be an exact conversion, as otherwise the truncation works
664 // incorrectly on the modified value.
665 {
666 ScratchDoubleScope fpscratch(masm);
667 masm.zeroDouble(fpscratch);
668 masm.vucomisd(fpscratch, input);
669 masm.j(Assembler::Parity, &fail);
670 }
671
672 {
673 Label positive;
674 masm.j(Assembler::Above, &positive);
675
676 masm.loadConstantDouble(4294967296.0, temp);
677 Label skip;
678 masm.jmp(&skip);
679
680 masm.bind(&positive);
681 masm.loadConstantDouble(-4294967296.0, temp);
682 masm.bind(&skip);
683 }
684
685 masm.addDouble(input, temp);
686 masm.vcvttsd2si(temp, output);
687 ScratchDoubleScope fpscratch(masm);
688 masm.vcvtsi2sd(output, fpscratch, fpscratch);
689
690 masm.vucomisd(fpscratch, temp);
691 masm.j(Assembler::Parity, &fail);
692 masm.j(Assembler::Equal, ool->rejoin());
693 }
694
695 masm.bind(&fail);
696 {
697 if (gen->compilingWasm()) {
698 masm.Push(InstanceReg);
699 }
700 int32_t framePushedAfterInstance = masm.framePushed();
701
702 saveVolatile(output);
703
704 if (gen->compilingWasm()) {
705 masm.setupWasmABICall();
706 masm.passABIArg(input, ABIType::Float64);
707
708 int32_t instanceOffset = masm.framePushed() - framePushedAfterInstance;
709 masm.callWithABI(ool->trapSiteDesc().bytecodeOffset,
710 wasm::SymbolicAddress::ToInt32,
711 mozilla::Some(instanceOffset));
712 } else {
713 using Fn = int32_t (*)(double);
714 masm.setupUnalignedABICall(output);
715 masm.passABIArg(input, ABIType::Float64);
716 masm.callWithABI<Fn, JS::ToInt32>(ABIType::General,
717 CheckUnsafeCallWithABI::DontCheckOther);
718 }
719 masm.storeCallInt32Result(output);
720
721 restoreVolatile(output);
722
723 if (gen->compilingWasm()) {
724 masm.Pop(InstanceReg);
725 }
726 }
727
728 masm.jump(ool->rejoin());
729 }
730
731 void CodeGeneratorX86::visitOutOfLineTruncateFloat32(
732 OutOfLineTruncateFloat32* ool) {
733 FloatRegister input = ToFloatRegister(ool->input());
734 Register output = ToRegister(ool->output());
735
736 Label fail;
737
738 if (Assembler::HasSSE3()) {
739 Label failPopFloat;
740
741 // Push float32, but subtracts 64 bits so that the value popped by fisttp
742 // fits
743 masm.subl(Imm32(sizeof(uint64_t)), esp);
744 masm.storeFloat32(input, Operand(esp, 0));
745
746 // Check exponent to avoid fp exceptions.
747 masm.branchFloat32NotInInt64Range(Address(esp, 0), output, &failPopFloat);
748
749 // Load float, perform 32-bit truncation.
750 masm.truncateFloat32ToInt64(Address(esp, 0), Address(esp, 0), output);
751
752 // Load low word, pop 64bits and jump back.
753 masm.load32(Address(esp, 0), output);
754 masm.addl(Imm32(sizeof(uint64_t)), esp);
755 masm.jump(ool->rejoin());
756
757 masm.bind(&failPopFloat);
758 masm.addl(Imm32(sizeof(uint64_t)), esp);
759 masm.jump(&fail);
760 } else {
761 FloatRegister temp = ToFloatRegister(ool->tempFloat());
762
763 // Try to convert float32 representing integers within 2^32 of a signed
764 // integer, by adding/subtracting 2^32 and then trying to convert to int32.
765 // This has to be an exact conversion, as otherwise the truncation works
766 // incorrectly on the modified value.
767 {
768 ScratchFloat32Scope fpscratch(masm);
769 masm.zeroFloat32(fpscratch);
770 masm.vucomiss(fpscratch, input);
771 masm.j(Assembler::Parity, &fail);
772 }
773
774 {
775 Label positive;
776 masm.j(Assembler::Above, &positive);
777
778 masm.loadConstantFloat32(4294967296.f, temp);
779 Label skip;
780 masm.jmp(&skip);
781
782 masm.bind(&positive);
783 masm.loadConstantFloat32(-4294967296.f, temp);
784 masm.bind(&skip);
785 }
786
787 masm.addFloat32(input, temp);
788 masm.vcvttss2si(temp, output);
789 ScratchFloat32Scope fpscratch(masm);
790 masm.vcvtsi2ss(output, fpscratch, fpscratch);
791
792 masm.vucomiss(fpscratch, temp);
793 masm.j(Assembler::Parity, &fail);
794 masm.j(Assembler::Equal, ool->rejoin());
795 }
796
797 masm.bind(&fail);
798 {
799 if (gen->compilingWasm()) {
800 masm.Push(InstanceReg);
801 }
802 int32_t framePushedAfterInstance = masm.framePushed();
803
804 saveVolatile(output);
805
806 // Push always pushes a 64-bit double.
807 masm.Push(input.asDouble());
808
809 if (gen->compilingWasm()) {
810 masm.setupWasmABICall();
811 } else {
812 masm.setupUnalignedABICall(output);
813 }
814
815 masm.vcvtss2sd(input, input, input);
816 masm.passABIArg(input.asDouble(), ABIType::Float64);
817
818 if (gen->compilingWasm()) {
819 int32_t instanceOffset = masm.framePushed() - framePushedAfterInstance;
820 masm.callWithABI(ool->trapSiteDesc().bytecodeOffset,
821 wasm::SymbolicAddress::ToInt32,
822 mozilla::Some(instanceOffset));
823 } else {
824 using Fn = int32_t (*)(double);
825 masm.callWithABI<Fn, JS::ToInt32>(ABIType::General,
826 CheckUnsafeCallWithABI::DontCheckOther);
827 }
828
829 masm.storeCallInt32Result(output);
830 masm.Pop(input.asDouble());
831
832 restoreVolatile(output);
833
834 if (gen->compilingWasm()) {
835 masm.Pop(InstanceReg);
836 }
837 }
838
839 masm.jump(ool->rejoin());
840 }
841
842 void CodeGenerator::visitDivOrModI64(LDivOrModI64* lir) {
843 MOZ_ASSERT(gen->compilingWasm());
844 MOZ_ASSERT(ToRegister(lir->instance()) == InstanceReg);
845
846 masm.Push(InstanceReg);
847 int32_t framePushedAfterInstance = masm.framePushed();
848
849 Register64 lhs = ToRegister64(lir->lhs());
850 Register64 rhs = ToRegister64(lir->rhs());
851 Register64 output = ToOutRegister64(lir);
852
853 MOZ_ASSERT(output == ReturnReg64);
854
855 Label done;
856
857 // Handle divide by zero.
858 if (lir->canBeDivideByZero()) {
859 Label nonZero;
860 // We can use InstanceReg as temp register because we preserved it
861 // before.
862 masm.branchTest64(Assembler::NonZero, rhs, rhs, InstanceReg, &nonZero);
863 masm.wasmTrap(wasm::Trap::IntegerDivideByZero, lir->trapSiteDesc());
864 masm.bind(&nonZero);
865 }
866
867 MDefinition* mir = lir->mir();
868
869 // Handle an integer overflow exception from INT64_MIN / -1.
870 if (lir->canBeNegativeOverflow()) {
871 Label notOverflow;
872 masm.branch64(Assembler::NotEqual, lhs, Imm64(INT64_MIN), &notOverflow);
873 masm.branch64(Assembler::NotEqual, rhs, Imm64(-1), &notOverflow);
874 if (mir->isWasmBuiltinModI64()) {
875 masm.xor64(output, output);
876 } else {
877 masm.wasmTrap(wasm::Trap::IntegerOverflow, lir->trapSiteDesc());
878 }
879 masm.jump(&done);
880 masm.bind(&notOverflow);
881 }
882
883 masm.setupWasmABICall();
884 masm.passABIArg(lhs.high);
885 masm.passABIArg(lhs.low);
886 masm.passABIArg(rhs.high);
887 masm.passABIArg(rhs.low);
888
889 int32_t instanceOffset = masm.framePushed() - framePushedAfterInstance;
890 if (mir->isWasmBuiltinModI64()) {
891 masm.callWithABI(lir->trapSiteDesc().bytecodeOffset,
892 wasm::SymbolicAddress::ModI64,
893 mozilla::Some(instanceOffset));
894 } else {
895 masm.callWithABI(lir->trapSiteDesc().bytecodeOffset,
896 wasm::SymbolicAddress::DivI64,
897 mozilla::Some(instanceOffset));
898 }
899
900 // output in edx:eax, move to output register.
901 masm.movl(edx, output.high);
902 MOZ_ASSERT(eax == output.low);
903
904 masm.bind(&done);
905 masm.Pop(InstanceReg);
906 }
907
908 void CodeGenerator::visitUDivOrModI64(LUDivOrModI64* lir) {
909 MOZ_ASSERT(gen->compilingWasm());
910 MOZ_ASSERT(ToRegister(lir->instance()) == InstanceReg);
911
912 masm.Push(InstanceReg);
913 int32_t framePushedAfterInstance = masm.framePushed();
914
915 Register64 lhs = ToRegister64(lir->lhs());
916 Register64 rhs = ToRegister64(lir->rhs());
917 Register64 output = ToOutRegister64(lir);
918
919 MOZ_ASSERT(output == ReturnReg64);
920
921 // Prevent divide by zero.
922 if (lir->canBeDivideByZero()) {
923 Label nonZero;
924 // We can use InstanceReg as temp register because we preserved it
925 // before.
926 masm.branchTest64(Assembler::NonZero, rhs, rhs, InstanceReg, &nonZero);
927 masm.wasmTrap(wasm::Trap::IntegerDivideByZero, lir->trapSiteDesc());
928 masm.bind(&nonZero);
929 }
930
931 masm.setupWasmABICall();
932 masm.passABIArg(lhs.high);
933 masm.passABIArg(lhs.low);
934 masm.passABIArg(rhs.high);
935 masm.passABIArg(rhs.low);
936
937 MDefinition* mir = lir->mir();
938 int32_t instanceOffset = masm.framePushed() - framePushedAfterInstance;
939 if (mir->isWasmBuiltinModI64()) {
940 masm.callWithABI(lir->trapSiteDesc().bytecodeOffset,
941 wasm::SymbolicAddress::UModI64,
942 mozilla::Some(instanceOffset));
943 } else {
944 masm.callWithABI(lir->trapSiteDesc().bytecodeOffset,
945 wasm::SymbolicAddress::UDivI64,
946 mozilla::Some(instanceOffset));
947 }
948
949 // output in edx:eax, move to output register.
950 masm.movl(edx, output.high);
951 MOZ_ASSERT(eax == output.low);
952
953 masm.Pop(InstanceReg);
954 }
955
956 void CodeGeneratorX86::emitBigIntPtrDiv(LBigIntPtrDiv* ins, Register dividend,
957 Register divisor, Register output) {
958 // Callers handle division by zero and integer overflow.
959
960 MOZ_ASSERT(ToRegister(ins->temp0()) == edx);
961 MOZ_ASSERT(output == eax);
962
963 if (dividend != eax) {
964 masm.movePtr(dividend, eax);
965 }
966
967 // Sign extend the lhs into edx to make edx:eax.
968 masm.cdq();
969
970 masm.idiv(divisor);
971 }
972
973 void CodeGeneratorX86::emitBigIntPtrMod(LBigIntPtrMod* ins, Register dividend,
974 Register divisor, Register output) {
975 // Callers handle division by zero and integer overflow.
976
977 MOZ_ASSERT(dividend == eax);
978 MOZ_ASSERT(output == edx);
979
980 // Sign extend the lhs into edx to make edx:eax.
981 masm.cdq();
982
983 masm.idiv(divisor);
984 }
985
986 void CodeGenerator::visitWasmSelectI64(LWasmSelectI64* lir) {
987 MOZ_ASSERT(lir->mir()->type() == MIRType::Int64);
988
989 Register cond = ToRegister(lir->condExpr());
990 Register64 falseExpr = ToRegister64(lir->falseExpr());
991 Register64 out = ToOutRegister64(lir);
992
993 MOZ_ASSERT(ToRegister64(lir->trueExpr()) == out,
994 "true expr is reused for input");
995
996 Label done;
997 masm.branchTest32(Assembler::NonZero, cond, cond, &done);
998 masm.movl(falseExpr.low, out.low);
999 masm.movl(falseExpr.high, out.high);
1000 masm.bind(&done);
1001 }
1002
1003 // We expect to handle only the case where compare is {U,}Int32 and select is
1004 // {U,}Int32. Some values may be stack allocated, and the "true" input is
1005 // reused for the output.
1006 void CodeGenerator::visitWasmCompareAndSelect(LWasmCompareAndSelect* ins) {
1007 bool cmpIs32bit = ins->compareType() == MCompare::Compare_Int32 ||
1008 ins->compareType() == MCompare::Compare_UInt32;
1009 bool selIs32bit = ins->mir()->type() == MIRType::Int32;
1010
1011 MOZ_RELEASE_ASSERT(
1012 cmpIs32bit && selIs32bit,
1013 "CodeGenerator::visitWasmCompareAndSelect: unexpected types");
1014
1015 Register trueExprAndDest = ToRegister(ins->output());
1016 MOZ_ASSERT(ToRegister(ins->ifTrueExpr()) == trueExprAndDest,
1017 "true expr input is reused for output");
1018
1019 Assembler::Condition cond = Assembler::InvertCondition(
1020 JSOpToCondition(ins->compareType(), ins->jsop()));
1021 const LAllocation* rhs = ins->rightExpr();
1022 const LAllocation* falseExpr = ins->ifFalseExpr();
1023 Register lhs = ToRegister(ins->leftExpr());
1024
1025 if (rhs->isGeneralReg()) {
1026 if (falseExpr->isGeneralReg()) {
1027 masm.cmp32Move32(cond, lhs, ToRegister(rhs), ToRegister(falseExpr),
1028 trueExprAndDest);
1029 } else {
1030 masm.cmp32Load32(cond, lhs, ToRegister(rhs), ToAddress(falseExpr),
1031 trueExprAndDest);
1032 }
1033 } else {
1034 if (falseExpr->isGeneralReg()) {
1035 masm.cmp32Move32(cond, lhs, ToAddress(rhs), ToRegister(falseExpr),
1036 trueExprAndDest);
1037 } else {
1038 masm.cmp32Load32(cond, lhs, ToAddress(rhs), ToAddress(falseExpr),
1039 trueExprAndDest);
1040 }
1041 }
1042 }
1043
1044 void CodeGenerator::visitExtendInt32ToInt64(LExtendInt32ToInt64* lir) {
1045 Register64 output = ToOutRegister64(lir);
1046 Register input = ToRegister(lir->input());
1047
1048 if (lir->mir()->isUnsigned()) {
1049 if (output.low != input) {
1050 masm.movl(input, output.low);
1051 }
1052 masm.xorl(output.high, output.high);
1053 } else {
1054 MOZ_ASSERT(output.low == input);
1055 MOZ_ASSERT(output.low == eax);
1056 MOZ_ASSERT(output.high == edx);
1057 masm.cdq();
1058 }
1059 }
1060
1061 void CodeGenerator::visitSignExtendInt64(LSignExtendInt64* lir) {
1062 #ifdef DEBUG
1063 Register64 input = ToRegister64(lir->input());
1064 Register64 output = ToOutRegister64(lir);
1065 MOZ_ASSERT(input.low == eax);
1066 MOZ_ASSERT(output.low == eax);
1067 MOZ_ASSERT(input.high == edx);
1068 MOZ_ASSERT(output.high == edx);
1069 #endif
1070 switch (lir->mir()->mode()) {
1071 case MSignExtendInt64::Byte:
1072 masm.move8SignExtend(eax, eax);
1073 break;
1074 case MSignExtendInt64::Half:
1075 masm.move16SignExtend(eax, eax);
1076 break;
1077 case MSignExtendInt64::Word:
1078 break;
1079 }
1080 masm.cdq();
1081 }
1082
1083 void CodeGenerator::visitWrapInt64ToInt32(LWrapInt64ToInt32* lir) {
1084 LInt64Allocation input = lir->input();
1085 Register output = ToRegister(lir->output());
1086
1087 if (lir->mir()->bottomHalf()) {
1088 masm.move32(ToRegister(input.low()), output);
1089 } else {
1090 masm.move32(ToRegister(input.high()), output);
1091 }
1092 }
1093
1094 void CodeGenerator::visitWasmExtendU32Index(LWasmExtendU32Index*) {
1095 MOZ_CRASH("64-bit only");
1096 }
1097
1098 void CodeGenerator::visitWasmWrapU32Index(LWasmWrapU32Index* lir) {
1099 // Generates no code on this platform because we just return the low part of
1100 // the input register pair.
1101 MOZ_ASSERT(ToRegister(lir->input()) == ToRegister(lir->output()));
1102 }
1103
1104 void CodeGenerator::visitWasmTruncateToInt64(LWasmTruncateToInt64* lir) {
1105 FloatRegister input = ToFloatRegister(lir->input());
1106 Register64 output = ToOutRegister64(lir);
1107
1108 MWasmTruncateToInt64* mir = lir->mir();
1109 FloatRegister floatTemp = ToFloatRegister(lir->temp0());
1110
1111 Label fail, convert;
1112
1113 MOZ_ASSERT(mir->input()->type() == MIRType::Double ||
1114 mir->input()->type() == MIRType::Float32);
1115
1116 auto* ool = new (alloc()) OutOfLineWasmTruncateCheck(mir, input, output);
1117 addOutOfLineCode(ool, mir);
1118
1119 bool isSaturating = mir->isSaturating();
1120 if (mir->input()->type() == MIRType::Float32) {
1121 if (mir->isUnsigned()) {
1122 masm.wasmTruncateFloat32ToUInt64(input, output, isSaturating,
1123 ool->entry(), ool->rejoin(), floatTemp);
1124 } else {
1125 masm.wasmTruncateFloat32ToInt64(input, output, isSaturating, ool->entry(),
1126 ool->rejoin(), floatTemp);
1127 }
1128 } else {
1129 if (mir->isUnsigned()) {
1130 masm.wasmTruncateDoubleToUInt64(input, output, isSaturating, ool->entry(),
1131 ool->rejoin(), floatTemp);
1132 } else {
1133 masm.wasmTruncateDoubleToInt64(input, output, isSaturating, ool->entry(),
1134 ool->rejoin(), floatTemp);
1135 }
1136 }
1137 }
1138
1139 void CodeGenerator::visitInt64ToFloatingPoint(LInt64ToFloatingPoint* lir) {
1140 Register64 input = ToRegister64(lir->input());
1141 FloatRegister output = ToFloatRegister(lir->output());
1142 Register temp = ToTempRegisterOrInvalid(lir->temp0());
1143
1144 MIRType outputType = lir->mir()->type();
1145 MOZ_ASSERT(outputType == MIRType::Double || outputType == MIRType::Float32);
1146
1147 if (outputType == MIRType::Double) {
1148 if (lir->mir()->isUnsigned()) {
1149 masm.convertUInt64ToDouble(input, output, temp);
1150 } else {
1151 masm.convertInt64ToDouble(input, output);
1152 }
1153 } else {
1154 if (lir->mir()->isUnsigned()) {
1155 masm.convertUInt64ToFloat32(input, output, temp);
1156 } else {
1157 masm.convertInt64ToFloat32(input, output);
1158 }
1159 }
1160 }
1161
1162 void CodeGenerator::visitBitNotI64(LBitNotI64* ins) {
1163 LInt64Allocation input = ins->input();
1164 Register64 inputR = ToRegister64(input);
1165 MOZ_ASSERT(inputR == ToOutRegister64(ins));
1166 masm.notl(inputR.high);
1167 masm.notl(inputR.low);
1168 }