/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- * vim: set ts=8 sts=2 et sw=2 tw=80: * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "jit/x64/CodeGenerator-x64.h" #include "mozilla/MathAlgorithms.h" #include "jit/CodeGenerator.h" #include "jit/MIR.h" #include "js/ScalarType.h" // js::Scalar::Type #include "jit/MacroAssembler-inl.h" #include "jit/shared/CodeGenerator-shared-inl.h" #include "vm/JSScript-inl.h" using namespace js; using namespace js::jit; using mozilla::DebugOnly; CodeGeneratorX64::CodeGeneratorX64(MIRGenerator* gen, LIRGraph* graph, MacroAssembler* masm) : CodeGeneratorX86Shared(gen, graph, masm) {} ValueOperand CodeGeneratorX64::ToValue(LInstruction* ins, size_t pos) { return ValueOperand(ToRegister(ins->getOperand(pos))); } ValueOperand CodeGeneratorX64::ToTempValue(LInstruction* ins, size_t pos) { return ValueOperand(ToRegister(ins->getTemp(pos))); } Operand CodeGeneratorX64::ToOperand64(const LInt64Allocation& a64) { const LAllocation& a = a64.value(); MOZ_ASSERT(!a.isFloatReg()); if (a.isGeneralReg()) { return Operand(a.toGeneralReg()->reg()); } return Operand(ToAddress(a)); } FrameSizeClass FrameSizeClass::FromDepth(uint32_t frameDepth) { return FrameSizeClass::None(); } FrameSizeClass FrameSizeClass::ClassLimit() { return FrameSizeClass(0); } uint32_t FrameSizeClass::frameSize() const { MOZ_CRASH("x64 does not use frame size classes"); } void CodeGenerator::visitValue(LValue* value) { ValueOperand result = ToOutValue(value); masm.moveValue(value->value(), result); } void CodeGenerator::visitBox(LBox* box) { const LAllocation* in = box->getOperand(0); ValueOperand result = ToOutValue(box); masm.moveValue(TypedOrValueRegister(box->type(), ToAnyRegister(in)), result); } void CodeGenerator::visitUnbox(LUnbox* unbox) { MUnbox* mir = unbox->mir(); Register result = ToRegister(unbox->output()); if (mir->fallible()) { const ValueOperand value = ToValue(unbox, LUnbox::Input); Label bail; switch (mir->type()) { case MIRType::Int32: masm.fallibleUnboxInt32(value, result, &bail); break; case MIRType::Boolean: masm.fallibleUnboxBoolean(value, result, &bail); break; case MIRType::Object: masm.fallibleUnboxObject(value, result, &bail); break; case MIRType::String: masm.fallibleUnboxString(value, result, &bail); break; case MIRType::Symbol: masm.fallibleUnboxSymbol(value, result, &bail); break; case MIRType::BigInt: masm.fallibleUnboxBigInt(value, result, &bail); break; default: MOZ_CRASH("Given MIRType cannot be unboxed."); } bailoutFrom(&bail, unbox->snapshot()); return; } // Infallible unbox. Operand input = ToOperand(unbox->getOperand(LUnbox::Input)); #ifdef DEBUG // Assert the types match. JSValueTag tag = MIRTypeToTag(mir->type()); Label ok; masm.splitTag(input, ScratchReg); masm.branch32(Assembler::Equal, ScratchReg, Imm32(tag), &ok); masm.assumeUnreachable("Infallible unbox type mismatch"); masm.bind(&ok); #endif switch (mir->type()) { case MIRType::Int32: masm.unboxInt32(input, result); break; case MIRType::Boolean: masm.unboxBoolean(input, result); break; case MIRType::Object: masm.unboxObject(input, result); break; case MIRType::String: masm.unboxString(input, result); break; case MIRType::Symbol: masm.unboxSymbol(input, result); break; case MIRType::BigInt: masm.unboxBigInt(input, result); break; default: MOZ_CRASH("Given MIRType cannot be unboxed."); } } void CodeGenerator::visitCompareI64(LCompareI64* lir) { MCompare* mir = lir->mir(); MOZ_ASSERT(mir->compareType() == MCompare::Compare_Int64 || mir->compareType() == MCompare::Compare_UInt64); const LInt64Allocation lhs = lir->getInt64Operand(LCompareI64::Lhs); const LInt64Allocation rhs = lir->getInt64Operand(LCompareI64::Rhs); Register lhsReg = ToRegister64(lhs).reg; Register output = ToRegister(lir->output()); if (IsConstant(rhs)) { masm.cmpPtr(lhsReg, ImmWord(ToInt64(rhs))); } else { masm.cmpPtr(lhsReg, ToOperand64(rhs)); } bool isSigned = mir->compareType() == MCompare::Compare_Int64; masm.emitSet(JSOpToCondition(lir->jsop(), isSigned), output); } void CodeGenerator::visitCompareI64AndBranch(LCompareI64AndBranch* lir) { MCompare* mir = lir->cmpMir(); MOZ_ASSERT(mir->compareType() == MCompare::Compare_Int64 || mir->compareType() == MCompare::Compare_UInt64); LInt64Allocation lhs = lir->getInt64Operand(LCompareI64::Lhs); LInt64Allocation rhs = lir->getInt64Operand(LCompareI64::Rhs); Register lhsReg = ToRegister64(lhs).reg; if (IsConstant(rhs)) { masm.cmpPtr(lhsReg, ImmWord(ToInt64(rhs))); } else { masm.cmpPtr(lhsReg, ToOperand64(rhs)); } bool isSigned = mir->compareType() == MCompare::Compare_Int64; emitBranch(JSOpToCondition(lir->jsop(), isSigned), lir->ifTrue(), lir->ifFalse()); } void CodeGenerator::visitDivOrModI64(LDivOrModI64* lir) { Register lhs = ToRegister(lir->lhs()); Register rhs = ToRegister(lir->rhs()); Register output = ToRegister(lir->output()); MOZ_ASSERT_IF(lhs != rhs, rhs != rax); MOZ_ASSERT(rhs != rdx); MOZ_ASSERT_IF(output == rax, ToRegister(lir->remainder()) == rdx); MOZ_ASSERT_IF(output == rdx, ToRegister(lir->remainder()) == rax); Label done; // Put the lhs in rax. if (lhs != rax) { masm.mov(lhs, rax); } // Handle divide by zero. if (lir->canBeDivideByZero()) { Label nonZero; masm.branchTestPtr(Assembler::NonZero, rhs, rhs, &nonZero); masm.wasmTrap(wasm::Trap::IntegerDivideByZero, lir->bytecodeOffset()); masm.bind(&nonZero); } // Handle an integer overflow exception from INT64_MIN / -1. if (lir->canBeNegativeOverflow()) { Label notOverflow; masm.branchPtr(Assembler::NotEqual, lhs, ImmWord(INT64_MIN), ¬Overflow); masm.branchPtr(Assembler::NotEqual, rhs, ImmWord(-1), ¬Overflow); if (lir->mir()->isMod()) { masm.xorl(output, output); } else { masm.wasmTrap(wasm::Trap::IntegerOverflow, lir->bytecodeOffset()); } masm.jump(&done); masm.bind(¬Overflow); } // Sign extend the lhs into rdx to make rdx:rax. masm.cqo(); masm.idivq(rhs); masm.bind(&done); } void CodeGenerator::visitUDivOrModI64(LUDivOrModI64* lir) { Register lhs = ToRegister(lir->lhs()); Register rhs = ToRegister(lir->rhs()); DebugOnly output = ToRegister(lir->output()); MOZ_ASSERT_IF(lhs != rhs, rhs != rax); MOZ_ASSERT(rhs != rdx); MOZ_ASSERT_IF(output.value == rax, ToRegister(lir->remainder()) == rdx); MOZ_ASSERT_IF(output.value == rdx, ToRegister(lir->remainder()) == rax); // Put the lhs in rax. if (lhs != rax) { masm.mov(lhs, rax); } Label done; // Prevent divide by zero. if (lir->canBeDivideByZero()) { Label nonZero; masm.branchTestPtr(Assembler::NonZero, rhs, rhs, &nonZero); masm.wasmTrap(wasm::Trap::IntegerDivideByZero, lir->bytecodeOffset()); masm.bind(&nonZero); } // Zero extend the lhs into rdx to make (rdx:rax). masm.xorl(rdx, rdx); masm.udivq(rhs); masm.bind(&done); } void CodeGeneratorX64::emitBigIntDiv(LBigIntDiv* ins, Register dividend, Register divisor, Register output, Label* fail) { // Callers handle division by zero and integer overflow. MOZ_ASSERT(dividend == rax); MOZ_ASSERT(output == rdx); // Sign extend the lhs into rdx to make rdx:rax. masm.cqo(); masm.idivq(divisor); // Create and return the result. masm.newGCBigInt(output, divisor, fail, bigIntsCanBeInNursery()); masm.initializeBigInt(output, dividend); } void CodeGeneratorX64::emitBigIntMod(LBigIntMod* ins, Register dividend, Register divisor, Register output, Label* fail) { // Callers handle division by zero and integer overflow. MOZ_ASSERT(dividend == rax); MOZ_ASSERT(output == rdx); // Sign extend the lhs into rdx to make rdx:rax. masm.cqo(); masm.idivq(divisor); // Move the remainder from rdx. masm.movq(output, dividend); // Create and return the result. masm.newGCBigInt(output, divisor, fail, bigIntsCanBeInNursery()); masm.initializeBigInt(output, dividend); } void CodeGenerator::visitWasmRegisterResult(LWasmRegisterResult* lir) { if (JitOptions.spectreIndexMasking) { if (MWasmRegisterResult* mir = lir->mir()) { if (mir->type() == MIRType::Int32) { masm.movl(ToRegister(lir->output()), ToRegister(lir->output())); } } } } void CodeGenerator::visitWasmSelectI64(LWasmSelectI64* lir) { MOZ_ASSERT(lir->mir()->type() == MIRType::Int64); Register cond = ToRegister(lir->condExpr()); Operand falseExpr = ToOperandOrRegister64(lir->falseExpr()); Register64 out = ToOutRegister64(lir); MOZ_ASSERT(ToRegister64(lir->trueExpr()) == out, "true expr is reused for input"); masm.test32(cond, cond); masm.cmovzq(falseExpr, out.reg); } void CodeGenerator::visitWasmReinterpretFromI64(LWasmReinterpretFromI64* lir) { MOZ_ASSERT(lir->mir()->type() == MIRType::Double); MOZ_ASSERT(lir->mir()->input()->type() == MIRType::Int64); masm.vmovq(ToRegister(lir->input()), ToFloatRegister(lir->output())); } void CodeGenerator::visitWasmReinterpretToI64(LWasmReinterpretToI64* lir) { MOZ_ASSERT(lir->mir()->type() == MIRType::Int64); MOZ_ASSERT(lir->mir()->input()->type() == MIRType::Double); masm.vmovq(ToFloatRegister(lir->input()), ToRegister(lir->output())); } void CodeGenerator::visitWasmUint32ToDouble(LWasmUint32ToDouble* lir) { masm.convertUInt32ToDouble(ToRegister(lir->input()), ToFloatRegister(lir->output())); } void CodeGenerator::visitWasmUint32ToFloat32(LWasmUint32ToFloat32* lir) { masm.convertUInt32ToFloat32(ToRegister(lir->input()), ToFloatRegister(lir->output())); } void CodeGeneratorX64::wasmStore(const wasm::MemoryAccessDesc& access, const LAllocation* value, Operand dstAddr) { if (value->isConstant()) { masm.memoryBarrierBefore(access.sync()); const MConstant* mir = value->toConstant(); Imm32 cst = Imm32(mir->type() == MIRType::Int32 ? mir->toInt32() : mir->toInt64()); masm.append(access, masm.size()); switch (access.type()) { case Scalar::Int8: case Scalar::Uint8: masm.movb(cst, dstAddr); break; case Scalar::Int16: case Scalar::Uint16: masm.movw(cst, dstAddr); break; case Scalar::Int32: case Scalar::Uint32: masm.movl(cst, dstAddr); break; case Scalar::Int64: case Scalar::Simd128: case Scalar::Float32: case Scalar::Float64: case Scalar::Uint8Clamped: case Scalar::BigInt64: case Scalar::BigUint64: case Scalar::MaxTypedArrayViewType: MOZ_CRASH("unexpected array type"); } masm.memoryBarrierAfter(access.sync()); } else { masm.wasmStore(access, ToAnyRegister(value), dstAddr); } } void CodeGenerator::visitWasmHeapBase(LWasmHeapBase* ins) { MOZ_ASSERT(ins->tlsPtr()->isBogus()); masm.movePtr(HeapReg, ToRegister(ins->output())); } template void CodeGeneratorX64::emitWasmLoad(T* ins) { const MWasmLoad* mir = ins->mir(); uint32_t offset = mir->access().offset(); MOZ_ASSERT(offset < masm.wasmMaxOffsetGuardLimit()); const LAllocation* ptr = ins->ptr(); Operand srcAddr = ptr->isBogus() ? Operand(HeapReg, offset) : Operand(HeapReg, ToRegister(ptr), TimesOne, offset); if (mir->type() == MIRType::Int64) { masm.wasmLoadI64(mir->access(), srcAddr, ToOutRegister64(ins)); } else { masm.wasmLoad(mir->access(), srcAddr, ToAnyRegister(ins->output())); } } void CodeGenerator::visitWasmLoad(LWasmLoad* ins) { emitWasmLoad(ins); } void CodeGenerator::visitWasmLoadI64(LWasmLoadI64* ins) { emitWasmLoad(ins); } template void CodeGeneratorX64::emitWasmStore(T* ins) { const MWasmStore* mir = ins->mir(); const wasm::MemoryAccessDesc& access = mir->access(); uint32_t offset = access.offset(); MOZ_ASSERT(offset < masm.wasmMaxOffsetGuardLimit()); const LAllocation* value = ins->getOperand(ins->ValueIndex); const LAllocation* ptr = ins->ptr(); Operand dstAddr = ptr->isBogus() ? Operand(HeapReg, offset) : Operand(HeapReg, ToRegister(ptr), TimesOne, offset); wasmStore(access, value, dstAddr); } void CodeGenerator::visitWasmStore(LWasmStore* ins) { emitWasmStore(ins); } void CodeGenerator::visitWasmStoreI64(LWasmStoreI64* ins) { MOZ_CRASH("Unused on this platform"); } void CodeGenerator::visitWasmCompareExchangeHeap( LWasmCompareExchangeHeap* ins) { MWasmCompareExchangeHeap* mir = ins->mir(); Register ptr = ToRegister(ins->ptr()); Register oldval = ToRegister(ins->oldValue()); Register newval = ToRegister(ins->newValue()); MOZ_ASSERT(ins->addrTemp()->isBogusTemp()); Scalar::Type accessType = mir->access().type(); BaseIndex srcAddr(HeapReg, ptr, TimesOne, mir->access().offset()); if (accessType == Scalar::Int64) { masm.wasmCompareExchange64(mir->access(), srcAddr, Register64(oldval), Register64(newval), ToOutRegister64(ins)); } else { masm.wasmCompareExchange(mir->access(), srcAddr, oldval, newval, ToRegister(ins->output())); } } void CodeGenerator::visitWasmAtomicExchangeHeap(LWasmAtomicExchangeHeap* ins) { MWasmAtomicExchangeHeap* mir = ins->mir(); Register ptr = ToRegister(ins->ptr()); Register value = ToRegister(ins->value()); MOZ_ASSERT(ins->addrTemp()->isBogusTemp()); Scalar::Type accessType = mir->access().type(); BaseIndex srcAddr(HeapReg, ptr, TimesOne, mir->access().offset()); if (accessType == Scalar::Int64) { masm.wasmAtomicExchange64(mir->access(), srcAddr, Register64(value), ToOutRegister64(ins)); } else { masm.wasmAtomicExchange(mir->access(), srcAddr, value, ToRegister(ins->output())); } } void CodeGenerator::visitWasmAtomicBinopHeap(LWasmAtomicBinopHeap* ins) { MWasmAtomicBinopHeap* mir = ins->mir(); MOZ_ASSERT(mir->hasUses()); Register ptr = ToRegister(ins->ptr()); const LAllocation* value = ins->value(); Register temp = ins->temp()->isBogusTemp() ? InvalidReg : ToRegister(ins->temp()); Register output = ToRegister(ins->output()); MOZ_ASSERT(ins->addrTemp()->isBogusTemp()); Scalar::Type accessType = mir->access().type(); if (accessType == Scalar::Uint32) { accessType = Scalar::Int32; } AtomicOp op = mir->operation(); BaseIndex srcAddr(HeapReg, ptr, TimesOne, mir->access().offset()); if (accessType == Scalar::Int64) { Register64 val = Register64(ToRegister(value)); Register64 out = Register64(output); Register64 tmp = Register64(temp); masm.wasmAtomicFetchOp64(mir->access(), op, val, srcAddr, tmp, out); } else if (value->isConstant()) { masm.wasmAtomicFetchOp(mir->access(), op, Imm32(ToInt32(value)), srcAddr, temp, output); } else { masm.wasmAtomicFetchOp(mir->access(), op, ToRegister(value), srcAddr, temp, output); } } void CodeGenerator::visitWasmAtomicBinopHeapForEffect( LWasmAtomicBinopHeapForEffect* ins) { MWasmAtomicBinopHeap* mir = ins->mir(); MOZ_ASSERT(!mir->hasUses()); Register ptr = ToRegister(ins->ptr()); const LAllocation* value = ins->value(); MOZ_ASSERT(ins->addrTemp()->isBogusTemp()); Scalar::Type accessType = mir->access().type(); AtomicOp op = mir->operation(); BaseIndex srcAddr(HeapReg, ptr, TimesOne, mir->access().offset()); if (accessType == Scalar::Int64) { Register64 val = Register64(ToRegister(value)); masm.wasmAtomicEffectOp64(mir->access(), op, val, srcAddr); } else if (value->isConstant()) { Imm32 c(0); if (value->toConstant()->type() == MIRType::Int64) { c = Imm32(ToInt64(value)); } else { c = Imm32(ToInt32(value)); } masm.wasmAtomicEffectOp(mir->access(), op, c, srcAddr, InvalidReg); } else { masm.wasmAtomicEffectOp(mir->access(), op, ToRegister(value), srcAddr, InvalidReg); } } void CodeGenerator::visitTruncateDToInt32(LTruncateDToInt32* ins) { FloatRegister input = ToFloatRegister(ins->input()); Register output = ToRegister(ins->output()); // On x64, branchTruncateDouble uses vcvttsd2sq. Unlike the x86 // implementation, this should handle most doubles and we can just // call a stub if it fails. emitTruncateDouble(input, output, ins->mir()); } void CodeGenerator::visitWasmBuiltinTruncateDToInt32( LWasmBuiltinTruncateDToInt32* lir) { FloatRegister input = ToFloatRegister(lir->getOperand(0)); Register output = ToRegister(lir->getDef(0)); emitTruncateDouble(input, output, lir->mir()); } void CodeGenerator::visitWasmBuiltinTruncateFToInt32( LWasmBuiltinTruncateFToInt32* lir) { FloatRegister input = ToFloatRegister(lir->getOperand(0)); Register output = ToRegister(lir->getDef(0)); emitTruncateFloat32(input, output, lir->mir()); } void CodeGenerator::visitTruncateFToInt32(LTruncateFToInt32* ins) { FloatRegister input = ToFloatRegister(ins->input()); Register output = ToRegister(ins->output()); // On x64, branchTruncateFloat32 uses vcvttss2sq. Unlike the x86 // implementation, this should handle most floats and we can just // call a stub if it fails. emitTruncateFloat32(input, output, ins->mir()); } void CodeGenerator::visitWrapInt64ToInt32(LWrapInt64ToInt32* lir) { const LAllocation* input = lir->getOperand(0); Register output = ToRegister(lir->output()); if (lir->mir()->bottomHalf()) { masm.movl(ToOperand(input), output); } else { MOZ_CRASH("Not implemented."); } } void CodeGenerator::visitExtendInt32ToInt64(LExtendInt32ToInt64* lir) { const LAllocation* input = lir->getOperand(0); Register output = ToRegister(lir->output()); if (lir->mir()->isUnsigned()) { masm.movl(ToOperand(input), output); } else { masm.movslq(ToOperand(input), output); } } void CodeGenerator::visitSignExtendInt64(LSignExtendInt64* ins) { Register64 input = ToRegister64(ins->getInt64Operand(0)); Register64 output = ToOutRegister64(ins); switch (ins->mode()) { case MSignExtendInt64::Byte: masm.movsbq(Operand(input.reg), output.reg); break; case MSignExtendInt64::Half: masm.movswq(Operand(input.reg), output.reg); break; case MSignExtendInt64::Word: masm.movslq(Operand(input.reg), output.reg); break; } } void CodeGenerator::visitWasmTruncateToInt64(LWasmTruncateToInt64* lir) { FloatRegister input = ToFloatRegister(lir->input()); Register64 output = ToOutRegister64(lir); MWasmTruncateToInt64* mir = lir->mir(); MIRType inputType = mir->input()->type(); MOZ_ASSERT(inputType == MIRType::Double || inputType == MIRType::Float32); auto* ool = new (alloc()) OutOfLineWasmTruncateCheck(mir, input, output); addOutOfLineCode(ool, mir); FloatRegister temp = mir->isUnsigned() ? ToFloatRegister(lir->temp()) : InvalidFloatReg; Label* oolEntry = ool->entry(); Label* oolRejoin = ool->rejoin(); bool isSaturating = mir->isSaturating(); if (inputType == MIRType::Double) { if (mir->isUnsigned()) { masm.wasmTruncateDoubleToUInt64(input, output, isSaturating, oolEntry, oolRejoin, temp); } else { masm.wasmTruncateDoubleToInt64(input, output, isSaturating, oolEntry, oolRejoin, temp); } } else { if (mir->isUnsigned()) { masm.wasmTruncateFloat32ToUInt64(input, output, isSaturating, oolEntry, oolRejoin, temp); } else { masm.wasmTruncateFloat32ToInt64(input, output, isSaturating, oolEntry, oolRejoin, temp); } } } void CodeGenerator::visitInt64ToFloatingPoint(LInt64ToFloatingPoint* lir) { Register64 input = ToRegister64(lir->getInt64Operand(0)); FloatRegister output = ToFloatRegister(lir->output()); MInt64ToFloatingPoint* mir = lir->mir(); bool isUnsigned = mir->isUnsigned(); MIRType outputType = mir->type(); MOZ_ASSERT(outputType == MIRType::Double || outputType == MIRType::Float32); MOZ_ASSERT(isUnsigned == !lir->getTemp(0)->isBogusTemp()); if (outputType == MIRType::Double) { if (isUnsigned) { masm.convertUInt64ToDouble(input, output, ToRegister(lir->getTemp(0))); } else { masm.convertInt64ToDouble(input, output); } } else { if (isUnsigned) { masm.convertUInt64ToFloat32(input, output, ToRegister(lir->getTemp(0))); } else { masm.convertInt64ToFloat32(input, output); } } } void CodeGenerator::visitNotI64(LNotI64* lir) { masm.cmpq(Imm32(0), ToRegister(lir->input())); masm.emitSet(Assembler::Equal, ToRegister(lir->output())); } void CodeGenerator::visitClzI64(LClzI64* lir) { Register64 input = ToRegister64(lir->getInt64Operand(0)); Register64 output = ToOutRegister64(lir); masm.clz64(input, output.reg); } void CodeGenerator::visitCtzI64(LCtzI64* lir) { Register64 input = ToRegister64(lir->getInt64Operand(0)); Register64 output = ToOutRegister64(lir); masm.ctz64(input, output.reg); } void CodeGenerator::visitTestI64AndBranch(LTestI64AndBranch* lir) { Register input = ToRegister(lir->input()); masm.testq(input, input); emitBranch(Assembler::NonZero, lir->ifTrue(), lir->ifFalse()); }