| 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/mips64/CodeGenerator-mips64.h" |
| 8 |
|
| 9 |
#include "mozilla/MathAlgorithms.h" |
| 10 |
|
| 11 |
#include "jit/CodeGenerator.h" |
| 12 |
#include "jit/MIR-wasm.h" |
| 13 |
#include "jit/MIR.h" |
| 14 |
#include "jit/MIRGraph.h" |
| 15 |
#include "js/Conversions.h" |
| 16 |
#include "vm/Shape.h" |
| 17 |
|
| 18 |
#include "jit/MacroAssembler-inl.h" |
| 19 |
#include "jit/shared/CodeGenerator-shared-inl.h" |
| 20 |
|
| 21 |
using namespace js; |
| 22 |
using namespace js::jit; |
| 23 |
|
| 24 |
void CodeGenerator::visitBox(LBox* box) { |
| 25 |
const LAllocation* in = box->payload(); |
| 26 |
ValueOperand result = ToOutValue(box); |
| 27 |
|
| 28 |
masm.moveValue(TypedOrValueRegister(box->type(), ToAnyRegister(in)), result); |
| 29 |
} |
| 30 |
|
| 31 |
void CodeGenerator::visitUnbox(LUnbox* unbox) { |
| 32 |
MUnbox* mir = unbox->mir(); |
| 33 |
|
| 34 |
Register result = ToRegister(unbox->output()); |
| 35 |
|
| 36 |
if (mir->fallible()) { |
| 37 |
ValueOperand value = ToValue(unbox->input()); |
| 38 |
Label bail; |
| 39 |
switch (mir->type()) { |
| 40 |
case MIRType::Int32: |
| 41 |
masm.fallibleUnboxInt32(value, result, &bail); |
| 42 |
break; |
| 43 |
case MIRType::Boolean: |
| 44 |
masm.fallibleUnboxBoolean(value, result, &bail); |
| 45 |
break; |
| 46 |
case MIRType::Object: |
| 47 |
masm.fallibleUnboxObject(value, result, &bail); |
| 48 |
break; |
| 49 |
case MIRType::String: |
| 50 |
masm.fallibleUnboxString(value, result, &bail); |
| 51 |
break; |
| 52 |
case MIRType::Symbol: |
| 53 |
masm.fallibleUnboxSymbol(value, result, &bail); |
| 54 |
break; |
| 55 |
case MIRType::BigInt: |
| 56 |
masm.fallibleUnboxBigInt(value, result, &bail); |
| 57 |
break; |
| 58 |
default: |
| 59 |
MOZ_CRASH("Given MIRType cannot be unboxed."); |
| 60 |
} |
| 61 |
bailoutFrom(&bail, unbox->snapshot()); |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
LAllocation* input = unbox->getOperand(LUnbox::Input); |
| 66 |
if (input->isGeneralReg()) { |
| 67 |
Register inputReg = ToRegister(input); |
| 68 |
switch (mir->type()) { |
| 69 |
case MIRType::Int32: |
| 70 |
masm.unboxInt32(inputReg, result); |
| 71 |
break; |
| 72 |
case MIRType::Boolean: |
| 73 |
masm.unboxBoolean(inputReg, result); |
| 74 |
break; |
| 75 |
case MIRType::Object: |
| 76 |
masm.unboxObject(inputReg, result); |
| 77 |
break; |
| 78 |
case MIRType::String: |
| 79 |
masm.unboxString(inputReg, result); |
| 80 |
break; |
| 81 |
case MIRType::Symbol: |
| 82 |
masm.unboxSymbol(inputReg, result); |
| 83 |
break; |
| 84 |
case MIRType::BigInt: |
| 85 |
masm.unboxBigInt(inputReg, result); |
| 86 |
break; |
| 87 |
default: |
| 88 |
MOZ_CRASH("Given MIRType cannot be unboxed."); |
| 89 |
} |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
Address inputAddr = ToAddress(input); |
| 94 |
switch (mir->type()) { |
| 95 |
case MIRType::Int32: |
| 96 |
masm.unboxInt32(inputAddr, result); |
| 97 |
break; |
| 98 |
case MIRType::Boolean: |
| 99 |
masm.unboxBoolean(inputAddr, result); |
| 100 |
break; |
| 101 |
case MIRType::Object: |
| 102 |
masm.unboxObject(inputAddr, result); |
| 103 |
break; |
| 104 |
case MIRType::String: |
| 105 |
masm.unboxString(inputAddr, result); |
| 106 |
break; |
| 107 |
case MIRType::Symbol: |
| 108 |
masm.unboxSymbol(inputAddr, result); |
| 109 |
break; |
| 110 |
case MIRType::BigInt: |
| 111 |
masm.unboxBigInt(inputAddr, result); |
| 112 |
break; |
| 113 |
default: |
| 114 |
MOZ_CRASH("Given MIRType cannot be unboxed."); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
void CodeGenerator::visitDivOrModI64(LDivOrModI64* lir) { |
| 119 |
Register lhs = ToRegister(lir->lhs()); |
| 120 |
Register rhs = ToRegister(lir->rhs()); |
| 121 |
Register output = ToRegister(lir->output()); |
| 122 |
|
| 123 |
Label done; |
| 124 |
|
| 125 |
// Handle divide by zero. |
| 126 |
if (lir->canBeDivideByZero()) { |
| 127 |
Label nonZero; |
| 128 |
masm.ma_b(rhs, rhs, &nonZero, Assembler::NonZero); |
| 129 |
masm.wasmTrap(wasm::Trap::IntegerDivideByZero, lir->trapSiteDesc()); |
| 130 |
masm.bind(&nonZero); |
| 131 |
} |
| 132 |
|
| 133 |
// Handle an integer overflow exception from INT64_MIN / -1. |
| 134 |
if (lir->canBeNegativeOverflow()) { |
| 135 |
Label notOverflow; |
| 136 |
masm.branchPtr(Assembler::NotEqual, lhs, ImmWord(INT64_MIN), ¬Overflow); |
| 137 |
masm.branchPtr(Assembler::NotEqual, rhs, ImmWord(-1), ¬Overflow); |
| 138 |
if (lir->mir()->isMod()) { |
| 139 |
masm.ma_xor(output, output); |
| 140 |
} else { |
| 141 |
masm.wasmTrap(wasm::Trap::IntegerOverflow, lir->trapSiteDesc()); |
| 142 |
} |
| 143 |
masm.jump(&done); |
| 144 |
masm.bind(¬Overflow); |
| 145 |
} |
| 146 |
|
| 147 |
#ifdef MIPSR6 |
| 148 |
if (lir->mir()->isMod()) { |
| 149 |
masm.as_dmod(output, lhs, rhs); |
| 150 |
} else { |
| 151 |
masm.as_ddiv(output, lhs, rhs); |
| 152 |
} |
| 153 |
#else |
| 154 |
masm.as_ddiv(lhs, rhs); |
| 155 |
if (lir->mir()->isMod()) { |
| 156 |
masm.as_mfhi(output); |
| 157 |
} else { |
| 158 |
masm.as_mflo(output); |
| 159 |
} |
| 160 |
#endif |
| 161 |
masm.bind(&done); |
| 162 |
} |
| 163 |
|
| 164 |
void CodeGenerator::visitUDivOrModI64(LUDivOrModI64* lir) { |
| 165 |
Register lhs = ToRegister(lir->lhs()); |
| 166 |
Register rhs = ToRegister(lir->rhs()); |
| 167 |
Register output = ToRegister(lir->output()); |
| 168 |
|
| 169 |
Label done; |
| 170 |
|
| 171 |
// Prevent divide by zero. |
| 172 |
if (lir->canBeDivideByZero()) { |
| 173 |
Label nonZero; |
| 174 |
masm.ma_b(rhs, rhs, &nonZero, Assembler::NonZero); |
| 175 |
masm.wasmTrap(wasm::Trap::IntegerDivideByZero, lir->trapSiteDesc()); |
| 176 |
masm.bind(&nonZero); |
| 177 |
} |
| 178 |
|
| 179 |
#ifdef MIPSR6 |
| 180 |
if (lir->mir()->isMod()) { |
| 181 |
masm.as_dmodu(output, lhs, rhs); |
| 182 |
} else { |
| 183 |
masm.as_ddivu(output, lhs, rhs); |
| 184 |
} |
| 185 |
#else |
| 186 |
masm.as_ddivu(lhs, rhs); |
| 187 |
if (lir->mir()->isMod()) { |
| 188 |
masm.as_mfhi(output); |
| 189 |
} else { |
| 190 |
masm.as_mflo(output); |
| 191 |
} |
| 192 |
#endif |
| 193 |
masm.bind(&done); |
| 194 |
} |
| 195 |
|
| 196 |
template <typename T> |
| 197 |
void CodeGeneratorMIPS64::emitWasmLoadI64(T* lir) { |
| 198 |
const MWasmLoad* mir = lir->mir(); |
| 199 |
|
| 200 |
Register memoryBase = ToRegister(lir->memoryBase()); |
| 201 |
Register ptrScratch = ToTempRegisterOrInvalid(lir->temp0()); |
| 202 |
|
| 203 |
Register ptrReg = ToRegister(lir->ptr()); |
| 204 |
if (mir->base()->type() == MIRType::Int32) { |
| 205 |
// See comment in visitWasmLoad re the type of 'base'. |
| 206 |
masm.move32ZeroExtendToPtr(ptrReg, ptrReg); |
| 207 |
} |
| 208 |
|
| 209 |
if constexpr (std::is_same_v<T, LWasmUnalignedLoadI64>) { |
| 210 |
MOZ_ASSERT(IsUnaligned(mir->access())); |
| 211 |
masm.wasmUnalignedLoadI64(mir->access(), memoryBase, ptrReg, ptrScratch, |
| 212 |
ToOutRegister64(lir), ToRegister(lir->temp1())); |
| 213 |
} else { |
| 214 |
MOZ_ASSERT(!IsUnaligned(mir->access())); |
| 215 |
masm.wasmLoadI64(mir->access(), memoryBase, ptrReg, ptrScratch, |
| 216 |
ToOutRegister64(lir)); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
void CodeGenerator::visitWasmLoadI64(LWasmLoadI64* lir) { |
| 221 |
emitWasmLoadI64(lir); |
| 222 |
} |
| 223 |
|
| 224 |
void CodeGenerator::visitWasmUnalignedLoadI64(LWasmUnalignedLoadI64* lir) { |
| 225 |
emitWasmLoadI64(lir); |
| 226 |
} |
| 227 |
|
| 228 |
template <typename T> |
| 229 |
void CodeGeneratorMIPS64::emitWasmStoreI64(T* lir) { |
| 230 |
const MWasmStore* mir = lir->mir(); |
| 231 |
|
| 232 |
Register memoryBase = ToRegister(lir->memoryBase()); |
| 233 |
Register ptrScratch = ToTempRegisterOrInvalid(lir->temp0()); |
| 234 |
|
| 235 |
Register ptrReg = ToRegister(lir->ptr()); |
| 236 |
if (mir->base()->type() == MIRType::Int32) { |
| 237 |
// See comment in visitWasmLoad re the type of 'base'. |
| 238 |
masm.move32ZeroExtendToPtr(ptrReg, ptrReg); |
| 239 |
} |
| 240 |
|
| 241 |
if constexpr (std::is_same_v<T, LWasmUnalignedStoreI64>) { |
| 242 |
MOZ_ASSERT(IsUnaligned(mir->access())); |
| 243 |
masm.wasmUnalignedStoreI64(mir->access(), ToRegister64(lir->value()), |
| 244 |
memoryBase, ptrReg, ptrScratch, |
| 245 |
ToRegister(lir->temp1())); |
| 246 |
} else { |
| 247 |
MOZ_ASSERT(!IsUnaligned(mir->access())); |
| 248 |
masm.wasmStoreI64(mir->access(), ToRegister64(lir->value()), memoryBase, |
| 249 |
ptrReg, ptrScratch); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
void CodeGenerator::visitWasmStoreI64(LWasmStoreI64* lir) { |
| 254 |
emitWasmStoreI64(lir); |
| 255 |
} |
| 256 |
|
| 257 |
void CodeGenerator::visitWasmUnalignedStoreI64(LWasmUnalignedStoreI64* lir) { |
| 258 |
emitWasmStoreI64(lir); |
| 259 |
} |
| 260 |
|
| 261 |
void CodeGenerator::visitWasmSelectI64(LWasmSelectI64* lir) { |
| 262 |
MOZ_ASSERT(lir->mir()->type() == MIRType::Int64); |
| 263 |
|
| 264 |
Register cond = ToRegister(lir->condExpr()); |
| 265 |
LInt64Allocation falseExpr = lir->falseExpr(); |
| 266 |
|
| 267 |
Register64 out = ToOutRegister64(lir); |
| 268 |
MOZ_ASSERT(ToRegister64(lir->trueExpr()) == out, |
| 269 |
"true expr is reused for input"); |
| 270 |
|
| 271 |
if (falseExpr.value().isGeneralReg()) { |
| 272 |
masm.as_movz(out.reg, ToRegister(falseExpr.value()), cond); |
| 273 |
} else { |
| 274 |
Label done; |
| 275 |
masm.ma_b(cond, cond, &done, Assembler::NonZero, ShortJump); |
| 276 |
masm.loadPtr(ToAddress(falseExpr.value()), out.reg); |
| 277 |
masm.bind(&done); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
void CodeGenerator::visitExtendInt32ToInt64(LExtendInt32ToInt64* lir) { |
| 282 |
const LAllocation* input = lir->input(); |
| 283 |
Register output = ToRegister(lir->output()); |
| 284 |
|
| 285 |
if (lir->mir()->isUnsigned()) { |
| 286 |
masm.ma_dext(output, ToRegister(input), Imm32(0), Imm32(32)); |
| 287 |
} else { |
| 288 |
masm.ma_sll(output, ToRegister(input), Imm32(0)); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
void CodeGenerator::visitWrapInt64ToInt32(LWrapInt64ToInt32* lir) { |
| 293 |
LInt64Allocation input = lir->input(); |
| 294 |
Register output = ToRegister(lir->output()); |
| 295 |
|
| 296 |
if (lir->mir()->bottomHalf()) { |
| 297 |
if (input.value().isMemory()) { |
| 298 |
masm.load32(ToAddress(input), output); |
| 299 |
} else { |
| 300 |
masm.move64To32(ToRegister64(input), output); |
| 301 |
} |
| 302 |
} else { |
| 303 |
MOZ_CRASH("Not implemented."); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
void CodeGenerator::visitSignExtendInt64(LSignExtendInt64* lir) { |
| 308 |
Register64 input = ToRegister64(lir->input()); |
| 309 |
Register64 output = ToOutRegister64(lir); |
| 310 |
switch (lir->mir()->mode()) { |
| 311 |
case MSignExtendInt64::Byte: |
| 312 |
masm.move32To64SignExtend(input.reg, output); |
| 313 |
masm.move8SignExtend(output.reg, output.reg); |
| 314 |
break; |
| 315 |
case MSignExtendInt64::Half: |
| 316 |
masm.move32To64SignExtend(input.reg, output); |
| 317 |
masm.move16SignExtend(output.reg, output.reg); |
| 318 |
break; |
| 319 |
case MSignExtendInt64::Word: |
| 320 |
masm.move32To64SignExtend(input.reg, output); |
| 321 |
break; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
void CodeGenerator::visitWasmExtendU32Index(LWasmExtendU32Index* lir) { |
| 326 |
Register input = ToRegister(lir->input()); |
| 327 |
Register output = ToRegister(lir->output()); |
| 328 |
MOZ_ASSERT(input == output); |
| 329 |
masm.move32To64ZeroExtend(input, Register64(output)); |
| 330 |
} |
| 331 |
|
| 332 |
void CodeGenerator::visitWasmWrapU32Index(LWasmWrapU32Index* lir) { |
| 333 |
Register input = ToRegister(lir->input()); |
| 334 |
Register output = ToRegister(lir->output()); |
| 335 |
MOZ_ASSERT(input == output); |
| 336 |
masm.move64To32(Register64(input), output); |
| 337 |
} |
| 338 |
|
| 339 |
void CodeGenerator::visitBitNotI64(LBitNotI64* ins) { |
| 340 |
LInt64Allocation input = ins->input(); |
| 341 |
MOZ_ASSERT(!IsConstant(input)); |
| 342 |
Register64 inputReg = ToRegister64(input); |
| 343 |
MOZ_ASSERT(inputReg == ToOutRegister64(ins)); |
| 344 |
masm.ma_not(inputReg.reg, inputReg.reg); |
| 345 |
} |
| 346 |
|
| 347 |
void CodeGenerator::visitWasmTruncateToInt64(LWasmTruncateToInt64* lir) { |
| 348 |
FloatRegister input = ToFloatRegister(lir->input()); |
| 349 |
Register64 output = ToOutRegister64(lir); |
| 350 |
|
| 351 |
MWasmTruncateToInt64* mir = lir->mir(); |
| 352 |
MIRType fromType = mir->input()->type(); |
| 353 |
|
| 354 |
MOZ_ASSERT(fromType == MIRType::Double || fromType == MIRType::Float32); |
| 355 |
|
| 356 |
auto* ool = new (alloc()) OutOfLineWasmTruncateCheck(mir, input, output); |
| 357 |
addOutOfLineCode(ool, mir); |
| 358 |
|
| 359 |
Label* oolEntry = ool->entry(); |
| 360 |
Label* oolRejoin = ool->rejoin(); |
| 361 |
bool isSaturating = mir->isSaturating(); |
| 362 |
|
| 363 |
if (fromType == MIRType::Double) { |
| 364 |
if (mir->isUnsigned()) { |
| 365 |
masm.wasmTruncateDoubleToUInt64(input, output, isSaturating, oolEntry, |
| 366 |
oolRejoin, InvalidFloatReg); |
| 367 |
} else { |
| 368 |
masm.wasmTruncateDoubleToInt64(input, output, isSaturating, oolEntry, |
| 369 |
oolRejoin, InvalidFloatReg); |
| 370 |
} |
| 371 |
} else { |
| 372 |
if (mir->isUnsigned()) { |
| 373 |
masm.wasmTruncateFloat32ToUInt64(input, output, isSaturating, oolEntry, |
| 374 |
oolRejoin, InvalidFloatReg); |
| 375 |
} else { |
| 376 |
masm.wasmTruncateFloat32ToInt64(input, output, isSaturating, oolEntry, |
| 377 |
oolRejoin, InvalidFloatReg); |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
void CodeGenerator::visitInt64ToFloatingPoint(LInt64ToFloatingPoint* lir) { |
| 383 |
Register64 input = ToRegister64(lir->input()); |
| 384 |
FloatRegister output = ToFloatRegister(lir->output()); |
| 385 |
|
| 386 |
MIRType outputType = lir->mir()->type(); |
| 387 |
MOZ_ASSERT(outputType == MIRType::Double || outputType == MIRType::Float32); |
| 388 |
|
| 389 |
if (outputType == MIRType::Double) { |
| 390 |
if (lir->mir()->isUnsigned()) { |
| 391 |
masm.convertUInt64ToDouble(input, output, Register::Invalid()); |
| 392 |
} else { |
| 393 |
masm.convertInt64ToDouble(input, output); |
| 394 |
} |
| 395 |
} else { |
| 396 |
if (lir->mir()->isUnsigned()) { |
| 397 |
masm.convertUInt64ToFloat32(input, output, Register::Invalid()); |
| 398 |
} else { |
| 399 |
masm.convertInt64ToFloat32(input, output); |
| 400 |
} |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
void CodeGenerator::visitAtomicLoad64(LAtomicLoad64* lir) { |
| 405 |
Register elements = ToRegister(lir->elements()); |
| 406 |
Register64 out = ToOutRegister64(lir); |
| 407 |
const MLoadUnboxedScalar* mir = lir->mir(); |
| 408 |
|
| 409 |
Scalar::Type storageType = mir->storageType(); |
| 410 |
|
| 411 |
auto sync = Synchronization::Load(); |
| 412 |
masm.memoryBarrierBefore(sync); |
| 413 |
if (lir->index()->isConstant()) { |
| 414 |
Address source = |
| 415 |
ToAddress(elements, lir->index(), storageType, mir->offsetAdjustment()); |
| 416 |
masm.load64(source, out); |
| 417 |
} else { |
| 418 |
BaseIndex source(elements, ToRegister(lir->index()), |
| 419 |
ScaleFromScalarType(storageType), mir->offsetAdjustment()); |
| 420 |
masm.load64(source, out); |
| 421 |
} |
| 422 |
masm.memoryBarrierAfter(sync); |
| 423 |
} |
| 424 |
|
| 425 |
void CodeGenerator::visitAtomicStore64(LAtomicStore64* lir) { |
| 426 |
Register elements = ToRegister(lir->elements()); |
| 427 |
Register64 value = ToRegister64(lir->value()); |
| 428 |
|
| 429 |
Scalar::Type writeType = lir->mir()->writeType(); |
| 430 |
|
| 431 |
auto sync = Synchronization::Store(); |
| 432 |
masm.memoryBarrierBefore(sync); |
| 433 |
if (lir->index()->isConstant()) { |
| 434 |
Address dest = ToAddress(elements, lir->index(), writeType); |
| 435 |
masm.store64(value, dest); |
| 436 |
} else { |
| 437 |
BaseIndex dest(elements, ToRegister(lir->index()), |
| 438 |
ScaleFromScalarType(writeType)); |
| 439 |
masm.store64(value, dest); |
| 440 |
} |
| 441 |
masm.memoryBarrierAfter(sync); |
| 442 |
} |
| 443 |
|
| 444 |
void CodeGeneratorMIPS64::emitBigIntPtrDiv(LBigIntPtrDiv* ins, |
| 445 |
Register dividend, Register divisor, |
| 446 |
Register output) { |
| 447 |
// Callers handle division by zero and integer overflow. |
| 448 |
#ifdef MIPSR6 |
| 449 |
masm.as_ddiv(/* result= */ output, dividend, divisor); |
| 450 |
#else |
| 451 |
masm.as_ddiv(dividend, divisor); |
| 452 |
masm.as_mflo(/* result= */ output); |
| 453 |
#endif |
| 454 |
} |
| 455 |
|
| 456 |
void CodeGeneratorMIPS64::emitBigIntPtrMod(LBigIntPtrMod* ins, |
| 457 |
Register dividend, Register divisor, |
| 458 |
Register output) { |
| 459 |
// Callers handle division by zero and integer overflow. |
| 460 |
#ifdef MIPSR6 |
| 461 |
masm.as_dmod(/* result= */ output, dividend, divisor); |
| 462 |
#else |
| 463 |
masm.as_ddiv(dividend, divisor); |
| 464 |
masm.as_mfhi(/* result= */ output); |
| 465 |
#endif |
| 466 |
} |