| 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-shared/MacroAssembler-x86-shared.h" |
| 8 |
|
| 9 |
#include "mozilla/Casting.h" |
| 10 |
|
| 11 |
#include "jsmath.h" |
| 12 |
|
| 13 |
#include "jit/JitFrames.h" |
| 14 |
#include "jit/MacroAssembler.h" |
| 15 |
#include "js/ScalarType.h" // js::Scalar::Type |
| 16 |
|
| 17 |
#include "jit/MacroAssembler-inl.h" |
| 18 |
|
| 19 |
using namespace js; |
| 20 |
using namespace js::jit; |
| 21 |
|
| 22 |
// Note: this function clobbers the input register. |
| 23 |
void MacroAssembler::clampDoubleToUint8(FloatRegister input, Register output) { |
| 24 |
ScratchDoubleScope scratch(*this); |
| 25 |
MOZ_ASSERT(input != scratch); |
| 26 |
Label positive, done; |
| 27 |
|
| 28 |
// <= 0 or NaN --> 0 |
| 29 |
zeroDouble(scratch); |
| 30 |
branchDouble(DoubleGreaterThan, input, scratch, &positive); |
| 31 |
{ |
| 32 |
move32(Imm32(0), output); |
| 33 |
jump(&done); |
| 34 |
} |
| 35 |
|
| 36 |
bind(&positive); |
| 37 |
|
| 38 |
if (HasRoundInstruction(RoundingMode::NearestTiesToEven)) { |
| 39 |
// Round input to nearest integer. |
| 40 |
nearbyIntDouble(RoundingMode::NearestTiesToEven, input, input); |
| 41 |
|
| 42 |
// Truncate to int32 and ensure the result <= 255. This relies on the |
| 43 |
// processor setting output to a value > 255 for doubles outside the int32 |
| 44 |
// range (for instance 0x80000000). |
| 45 |
vcvttsd2si(input, output); |
| 46 |
branch32(Assembler::BelowOrEqual, output, Imm32(255), &done); |
| 47 |
move32(Imm32(255), output); |
| 48 |
} else { |
| 49 |
Label outOfRange; |
| 50 |
|
| 51 |
// Truncate to int32 and ensure the result <= 255. This relies on the |
| 52 |
// processor setting output to a value > 255 for doubles outside the int32 |
| 53 |
// range (for instance 0x80000000). |
| 54 |
vcvttsd2si(input, output); |
| 55 |
branch32(Assembler::AboveOrEqual, output, Imm32(255), &outOfRange); |
| 56 |
{ |
| 57 |
// Check if we had a tie. |
| 58 |
convertInt32ToDouble(output, scratch); |
| 59 |
subDouble(scratch, input); |
| 60 |
|
| 61 |
loadConstantDouble(0.5, scratch); |
| 62 |
|
| 63 |
Label roundUp; |
| 64 |
vucomisd(scratch, input); |
| 65 |
j(Above, &roundUp); |
| 66 |
j(NotEqual, &done); |
| 67 |
|
| 68 |
// It was a tie. Round up if the output is odd. |
| 69 |
branchTest32(Zero, output, Imm32(1), &done); |
| 70 |
|
| 71 |
bind(&roundUp); |
| 72 |
add32(Imm32(1), output); |
| 73 |
jump(&done); |
| 74 |
} |
| 75 |
|
| 76 |
// > 255 --> 255 |
| 77 |
bind(&outOfRange); |
| 78 |
move32(Imm32(255), output); |
| 79 |
} |
| 80 |
|
| 81 |
bind(&done); |
| 82 |
} |
| 83 |
|
| 84 |
bool MacroAssemblerX86Shared::buildOOLFakeExitFrame(void* fakeReturnAddr) { |
| 85 |
asMasm().PushFrameDescriptor(FrameType::IonJS); |
| 86 |
asMasm().Push(ImmPtr(fakeReturnAddr)); |
| 87 |
asMasm().Push(FramePointer); |
| 88 |
return true; |
| 89 |
} |
| 90 |
|
| 91 |
void MacroAssemblerX86Shared::branchNegativeZero(FloatRegister reg, |
| 92 |
Register scratch, Label* label, |
| 93 |
bool maybeNonZero) { |
| 94 |
// Determines whether the low double contained in the XMM register reg |
| 95 |
// is equal to -0.0. |
| 96 |
|
| 97 |
#if defined(JS_CODEGEN_X86) |
| 98 |
Label nonZero; |
| 99 |
|
| 100 |
// if not already compared to zero |
| 101 |
if (maybeNonZero) { |
| 102 |
ScratchDoubleScope scratchDouble(asMasm()); |
| 103 |
|
| 104 |
// Compare to zero. Lets through {0, -0}. |
| 105 |
zeroDouble(scratchDouble); |
| 106 |
|
| 107 |
// If reg is non-zero, jump to nonZero. |
| 108 |
asMasm().branchDouble(DoubleNotEqual, reg, scratchDouble, &nonZero); |
| 109 |
} |
| 110 |
// Input register is either zero or negative zero. Retrieve sign of input. |
| 111 |
vmovmskpd(reg, scratch); |
| 112 |
|
| 113 |
// If reg is 1 or 3, input is negative zero. |
| 114 |
// If reg is 0 or 2, input is a normal zero. |
| 115 |
asMasm().branchTest32(NonZero, scratch, Imm32(1), label); |
| 116 |
|
| 117 |
bind(&nonZero); |
| 118 |
#elif defined(JS_CODEGEN_X64) |
| 119 |
vmovq(reg, scratch); |
| 120 |
cmpq(Imm32(1), scratch); |
| 121 |
j(Overflow, label); |
| 122 |
#endif |
| 123 |
} |
| 124 |
|
| 125 |
void MacroAssemblerX86Shared::branchNegativeZeroFloat32(FloatRegister reg, |
| 126 |
Register scratch, |
| 127 |
Label* label) { |
| 128 |
vmovd(reg, scratch); |
| 129 |
cmp32(scratch, Imm32(1)); |
| 130 |
j(Overflow, label); |
| 131 |
} |
| 132 |
|
| 133 |
MacroAssembler& MacroAssemblerX86Shared::asMasm() { |
| 134 |
return *static_cast<MacroAssembler*>(this); |
| 135 |
} |
| 136 |
|
| 137 |
const MacroAssembler& MacroAssemblerX86Shared::asMasm() const { |
| 138 |
return *static_cast<const MacroAssembler*>(this); |
| 139 |
} |
| 140 |
|
| 141 |
template <class T, class Map> |
| 142 |
T* MacroAssemblerX86Shared::getConstant(const typename T::Pod& value, Map& map, |
| 143 |
Vector<T, 0, SystemAllocPolicy>& vec) { |
| 144 |
using AddPtr = typename Map::AddPtr; |
| 145 |
size_t index; |
| 146 |
if (AddPtr p = map.lookupForAdd(value)) { |
| 147 |
index = p->value(); |
| 148 |
} else { |
| 149 |
index = vec.length(); |
| 150 |
enoughMemory_ &= vec.append(T(value)); |
| 151 |
if (!enoughMemory_) { |
| 152 |
return nullptr; |
| 153 |
} |
| 154 |
enoughMemory_ &= map.add(p, value, index); |
| 155 |
if (!enoughMemory_) { |
| 156 |
return nullptr; |
| 157 |
} |
| 158 |
} |
| 159 |
return &vec[index]; |
| 160 |
} |
| 161 |
|
| 162 |
MacroAssemblerX86Shared::Float* MacroAssemblerX86Shared::getFloat(float f) { |
| 163 |
return getConstant<Float, FloatMap>(f, floatMap_, floats_); |
| 164 |
} |
| 165 |
|
| 166 |
MacroAssemblerX86Shared::Double* MacroAssemblerX86Shared::getDouble(double d) { |
| 167 |
return getConstant<Double, DoubleMap>(d, doubleMap_, doubles_); |
| 168 |
} |
| 169 |
|
| 170 |
MacroAssemblerX86Shared::SimdData* MacroAssemblerX86Shared::getSimdData( |
| 171 |
const SimdConstant& v) { |
| 172 |
return getConstant<SimdData, SimdMap>(v, simdMap_, simds_); |
| 173 |
} |
| 174 |
|
| 175 |
void MacroAssemblerX86Shared::binarySimd128( |
| 176 |
const SimdConstant& rhs, FloatRegister lhsDest, |
| 177 |
void (MacroAssembler::*regOp)(const Operand&, FloatRegister, FloatRegister), |
| 178 |
void (MacroAssembler::*constOp)(const SimdConstant&, FloatRegister)) { |
| 179 |
ScratchSimd128Scope scratch(asMasm()); |
| 180 |
if (maybeInlineSimd128Int(rhs, scratch)) { |
| 181 |
(asMasm().*regOp)(Operand(scratch), lhsDest, lhsDest); |
| 182 |
} else { |
| 183 |
(asMasm().*constOp)(rhs, lhsDest); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
void MacroAssemblerX86Shared::binarySimd128( |
| 188 |
FloatRegister lhs, const SimdConstant& rhs, FloatRegister dest, |
| 189 |
void (MacroAssembler::*regOp)(const Operand&, FloatRegister, FloatRegister), |
| 190 |
void (MacroAssembler::*constOp)(const SimdConstant&, FloatRegister, |
| 191 |
FloatRegister)) { |
| 192 |
ScratchSimd128Scope scratch(asMasm()); |
| 193 |
if (maybeInlineSimd128Int(rhs, scratch)) { |
| 194 |
(asMasm().*regOp)(Operand(scratch), lhs, dest); |
| 195 |
} else { |
| 196 |
(asMasm().*constOp)(rhs, lhs, dest); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
void MacroAssemblerX86Shared::binarySimd128( |
| 201 |
const SimdConstant& rhs, FloatRegister lhs, |
| 202 |
void (MacroAssembler::*regOp)(const Operand&, FloatRegister), |
| 203 |
void (MacroAssembler::*constOp)(const SimdConstant&, FloatRegister)) { |
| 204 |
ScratchSimd128Scope scratch(asMasm()); |
| 205 |
if (maybeInlineSimd128Int(rhs, scratch)) { |
| 206 |
(asMasm().*regOp)(Operand(scratch), lhs); |
| 207 |
} else { |
| 208 |
(asMasm().*constOp)(rhs, lhs); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
void MacroAssemblerX86Shared::bitwiseTestSimd128(const SimdConstant& rhs, |
| 213 |
FloatRegister lhs) { |
| 214 |
ScratchSimd128Scope scratch(asMasm()); |
| 215 |
if (maybeInlineSimd128Int(rhs, scratch)) { |
| 216 |
vptest(scratch, lhs); |
| 217 |
} else { |
| 218 |
asMasm().vptestSimd128(rhs, lhs); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
void MacroAssemblerX86Shared::minMaxDouble(FloatRegister first, |
| 223 |
FloatRegister second, bool canBeNaN, |
| 224 |
bool isMax) { |
| 225 |
Label done, nan, minMaxInst; |
| 226 |
|
| 227 |
// Do a vucomisd to catch equality and NaNs, which both require special |
| 228 |
// handling. If the operands are ordered and inequal, we branch straight to |
| 229 |
// the min/max instruction. If we wanted, we could also branch for less-than |
| 230 |
// or greater-than here instead of using min/max, however these conditions |
| 231 |
// will sometimes be hard on the branch predictor. |
| 232 |
vucomisd(second, first); |
| 233 |
j(Assembler::NotEqual, &minMaxInst); |
| 234 |
if (canBeNaN) { |
| 235 |
j(Assembler::Parity, &nan); |
| 236 |
} |
| 237 |
|
| 238 |
// Ordered and equal. The operands are bit-identical unless they are zero |
| 239 |
// and negative zero. These instructions merge the sign bits in that |
| 240 |
// case, and are no-ops otherwise. |
| 241 |
if (isMax) { |
| 242 |
vandpd(second, first, first); |
| 243 |
} else { |
| 244 |
vorpd(second, first, first); |
| 245 |
} |
| 246 |
jump(&done); |
| 247 |
|
| 248 |
// x86's min/max are not symmetric; if either operand is a NaN, they return |
| 249 |
// the read-only operand. We need to return a NaN if either operand is a |
| 250 |
// NaN, so we explicitly check for a NaN in the read-write operand. |
| 251 |
if (canBeNaN) { |
| 252 |
bind(&nan); |
| 253 |
vucomisd(first, first); |
| 254 |
j(Assembler::Parity, &done); |
| 255 |
} |
| 256 |
|
| 257 |
// When the values are inequal, or second is NaN, x86's min and max will |
| 258 |
// return the value we need. |
| 259 |
bind(&minMaxInst); |
| 260 |
if (isMax) { |
| 261 |
vmaxsd(second, first, first); |
| 262 |
} else { |
| 263 |
vminsd(second, first, first); |
| 264 |
} |
| 265 |
|
| 266 |
bind(&done); |
| 267 |
} |
| 268 |
|
| 269 |
void MacroAssemblerX86Shared::minMaxFloat32(FloatRegister first, |
| 270 |
FloatRegister second, bool canBeNaN, |
| 271 |
bool isMax) { |
| 272 |
Label done, nan, minMaxInst; |
| 273 |
|
| 274 |
// Do a vucomiss to catch equality and NaNs, which both require special |
| 275 |
// handling. If the operands are ordered and inequal, we branch straight to |
| 276 |
// the min/max instruction. If we wanted, we could also branch for less-than |
| 277 |
// or greater-than here instead of using min/max, however these conditions |
| 278 |
// will sometimes be hard on the branch predictor. |
| 279 |
vucomiss(second, first); |
| 280 |
j(Assembler::NotEqual, &minMaxInst); |
| 281 |
if (canBeNaN) { |
| 282 |
j(Assembler::Parity, &nan); |
| 283 |
} |
| 284 |
|
| 285 |
// Ordered and equal. The operands are bit-identical unless they are zero |
| 286 |
// and negative zero. These instructions merge the sign bits in that |
| 287 |
// case, and are no-ops otherwise. |
| 288 |
if (isMax) { |
| 289 |
vandps(second, first, first); |
| 290 |
} else { |
| 291 |
vorps(second, first, first); |
| 292 |
} |
| 293 |
jump(&done); |
| 294 |
|
| 295 |
// x86's min/max are not symmetric; if either operand is a NaN, they return |
| 296 |
// the read-only operand. We need to return a NaN if either operand is a |
| 297 |
// NaN, so we explicitly check for a NaN in the read-write operand. |
| 298 |
if (canBeNaN) { |
| 299 |
bind(&nan); |
| 300 |
vucomiss(first, first); |
| 301 |
j(Assembler::Parity, &done); |
| 302 |
} |
| 303 |
|
| 304 |
// When the values are inequal, or second is NaN, x86's min and max will |
| 305 |
// return the value we need. |
| 306 |
bind(&minMaxInst); |
| 307 |
if (isMax) { |
| 308 |
vmaxss(second, first, first); |
| 309 |
} else { |
| 310 |
vminss(second, first, first); |
| 311 |
} |
| 312 |
|
| 313 |
bind(&done); |
| 314 |
} |
| 315 |
|
| 316 |
#ifdef ENABLE_WASM_SIMD |
| 317 |
bool MacroAssembler::MustMaskShiftCountSimd128(wasm::SimdOp op, int32_t* mask) { |
| 318 |
switch (op) { |
| 319 |
case wasm::SimdOp::I8x16Shl: |
| 320 |
case wasm::SimdOp::I8x16ShrU: |
| 321 |
case wasm::SimdOp::I8x16ShrS: |
| 322 |
*mask = 7; |
| 323 |
break; |
| 324 |
case wasm::SimdOp::I16x8Shl: |
| 325 |
case wasm::SimdOp::I16x8ShrU: |
| 326 |
case wasm::SimdOp::I16x8ShrS: |
| 327 |
*mask = 15; |
| 328 |
break; |
| 329 |
case wasm::SimdOp::I32x4Shl: |
| 330 |
case wasm::SimdOp::I32x4ShrU: |
| 331 |
case wasm::SimdOp::I32x4ShrS: |
| 332 |
*mask = 31; |
| 333 |
break; |
| 334 |
case wasm::SimdOp::I64x2Shl: |
| 335 |
case wasm::SimdOp::I64x2ShrU: |
| 336 |
case wasm::SimdOp::I64x2ShrS: |
| 337 |
*mask = 63; |
| 338 |
break; |
| 339 |
default: |
| 340 |
MOZ_CRASH("Unexpected shift operation"); |
| 341 |
} |
| 342 |
return true; |
| 343 |
} |
| 344 |
#endif |
| 345 |
|
| 346 |
//{{{ check_macroassembler_style |
| 347 |
// =============================================================== |
| 348 |
// MacroAssembler high-level usage. |
| 349 |
|
| 350 |
void MacroAssembler::flush() {} |
| 351 |
|
| 352 |
void MacroAssembler::comment(const char* msg) { masm.comment(msg); } |
| 353 |
|
| 354 |
// This operation really consists of five phases, in order to enforce the |
| 355 |
// restriction that on x86_shared, srcDest must be eax and edx will be |
| 356 |
// clobbered. |
| 357 |
// |
| 358 |
// Input: { rhs, lhsOutput } |
| 359 |
// |
| 360 |
// [PUSH] Preserve registers |
| 361 |
// [MOVE] Generate moves to specific registers |
| 362 |
// |
| 363 |
// [DIV] Input: { regForRhs, EAX } |
| 364 |
// [DIV] extend EAX into EDX |
| 365 |
// [DIV] x86 Division operator |
| 366 |
// [DIV] Ouptut: { EAX, EDX } |
| 367 |
// |
| 368 |
// [MOVE] Move specific registers to outputs |
| 369 |
// [POP] Restore registers |
| 370 |
// |
| 371 |
// Output: { lhsOutput, remainderOutput } |
| 372 |
void MacroAssembler::flexibleDivMod32(Register rhs, Register lhsOutput, |
| 373 |
Register remOutput, bool isUnsigned, |
| 374 |
const LiveRegisterSet&) { |
| 375 |
// Currently this helper can't handle this situation. |
| 376 |
MOZ_ASSERT(lhsOutput != rhs); |
| 377 |
MOZ_ASSERT(lhsOutput != remOutput); |
| 378 |
|
| 379 |
// Choose a register that is not edx, or eax to hold the rhs; |
| 380 |
// ebx is chosen arbitrarily, and will be preserved if necessary. |
| 381 |
Register regForRhs = (rhs == eax || rhs == edx) ? ebx : rhs; |
| 382 |
|
| 383 |
// Add registers we will be clobbering as live, but |
| 384 |
// also remove the set we do not restore. |
| 385 |
LiveRegisterSet preserve; |
| 386 |
preserve.add(edx); |
| 387 |
preserve.add(eax); |
| 388 |
preserve.add(regForRhs); |
| 389 |
|
| 390 |
preserve.takeUnchecked(lhsOutput); |
| 391 |
preserve.takeUnchecked(remOutput); |
| 392 |
|
| 393 |
PushRegsInMask(preserve); |
| 394 |
|
| 395 |
// Shuffle input into place. |
| 396 |
moveRegPair(lhsOutput, rhs, eax, regForRhs); |
| 397 |
|
| 398 |
// Sign extend eax into edx to make (edx:eax): idiv/udiv are 64-bit. |
| 399 |
if (isUnsigned) { |
| 400 |
mov(ImmWord(0), edx); |
| 401 |
udiv(regForRhs); |
| 402 |
} else { |
| 403 |
cdq(); |
| 404 |
idiv(regForRhs); |
| 405 |
} |
| 406 |
|
| 407 |
moveRegPair(eax, edx, lhsOutput, remOutput); |
| 408 |
|
| 409 |
PopRegsInMask(preserve); |
| 410 |
} |
| 411 |
|
| 412 |
void MacroAssembler::flexibleQuotient32( |
| 413 |
Register rhs, Register srcDest, bool isUnsigned, |
| 414 |
const LiveRegisterSet& volatileLiveRegs) { |
| 415 |
// Choose an arbitrary register that isn't eax, edx, rhs or srcDest; |
| 416 |
AllocatableGeneralRegisterSet regs(GeneralRegisterSet::All()); |
| 417 |
regs.takeUnchecked(eax); |
| 418 |
regs.takeUnchecked(edx); |
| 419 |
regs.takeUnchecked(rhs); |
| 420 |
regs.takeUnchecked(srcDest); |
| 421 |
|
| 422 |
Register remOut = regs.takeAny(); |
| 423 |
push(remOut); |
| 424 |
flexibleDivMod32(rhs, srcDest, remOut, isUnsigned, volatileLiveRegs); |
| 425 |
pop(remOut); |
| 426 |
} |
| 427 |
|
| 428 |
void MacroAssembler::flexibleRemainder32( |
| 429 |
Register rhs, Register srcDest, bool isUnsigned, |
| 430 |
const LiveRegisterSet& volatileLiveRegs) { |
| 431 |
// Choose an arbitrary register that isn't eax, edx, rhs or srcDest |
| 432 |
AllocatableGeneralRegisterSet regs(GeneralRegisterSet::All()); |
| 433 |
regs.takeUnchecked(eax); |
| 434 |
regs.takeUnchecked(edx); |
| 435 |
regs.takeUnchecked(rhs); |
| 436 |
regs.takeUnchecked(srcDest); |
| 437 |
|
| 438 |
Register remOut = regs.takeAny(); |
| 439 |
push(remOut); |
| 440 |
flexibleDivMod32(rhs, srcDest, remOut, isUnsigned, volatileLiveRegs); |
| 441 |
mov(remOut, srcDest); |
| 442 |
pop(remOut); |
| 443 |
} |
| 444 |
|
| 445 |
// =============================================================== |
| 446 |
// Stack manipulation functions. |
| 447 |
|
| 448 |
size_t MacroAssembler::PushRegsInMaskSizeInBytes(LiveRegisterSet set) { |
| 449 |
FloatRegisterSet fpuSet(set.fpus().reduceSetForPush()); |
| 450 |
return set.gprs().size() * sizeof(intptr_t) + fpuSet.getPushSizeInBytes(); |
| 451 |
} |
| 452 |
|
| 453 |
void MacroAssembler::PushRegsInMask(LiveRegisterSet set) { |
| 454 |
mozilla::DebugOnly<size_t> framePushedInitial = framePushed(); |
| 455 |
|
| 456 |
FloatRegisterSet fpuSet(set.fpus().reduceSetForPush()); |
| 457 |
unsigned numFpu = fpuSet.size(); |
| 458 |
int32_t diffF = fpuSet.getPushSizeInBytes(); |
| 459 |
int32_t diffG = set.gprs().size() * sizeof(intptr_t); |
| 460 |
|
| 461 |
// On x86, always use push to push the integer registers, as it's fast |
| 462 |
// on modern hardware and it's a small instruction. |
| 463 |
for (GeneralRegisterBackwardIterator iter(set.gprs()); iter.more(); ++iter) { |
| 464 |
diffG -= sizeof(intptr_t); |
| 465 |
Push(*iter); |
| 466 |
} |
| 467 |
MOZ_ASSERT(diffG == 0); |
| 468 |
(void)diffG; |
| 469 |
|
| 470 |
reserveStack(diffF); |
| 471 |
for (FloatRegisterBackwardIterator iter(fpuSet); iter.more(); ++iter) { |
| 472 |
FloatRegister reg = *iter; |
| 473 |
diffF -= reg.size(); |
| 474 |
numFpu -= 1; |
| 475 |
Address spillAddress(StackPointer, diffF); |
| 476 |
if (reg.isDouble()) { |
| 477 |
storeDouble(reg, spillAddress); |
| 478 |
} else if (reg.isSingle()) { |
| 479 |
storeFloat32(reg, spillAddress); |
| 480 |
} else if (reg.isSimd128()) { |
| 481 |
storeUnalignedSimd128(reg, spillAddress); |
| 482 |
} else { |
| 483 |
MOZ_CRASH("Unknown register type."); |
| 484 |
} |
| 485 |
} |
| 486 |
MOZ_ASSERT(numFpu == 0); |
| 487 |
(void)numFpu; |
| 488 |
|
| 489 |
// x64 padding to keep the stack aligned on uintptr_t. Keep in sync with |
| 490 |
// GetPushSizeInBytes. |
| 491 |
size_t alignExtra = ((size_t)diffF) % sizeof(uintptr_t); |
| 492 |
MOZ_ASSERT_IF(sizeof(uintptr_t) == 8, alignExtra == 0 || alignExtra == 4); |
| 493 |
MOZ_ASSERT_IF(sizeof(uintptr_t) == 4, alignExtra == 0); |
| 494 |
diffF -= alignExtra; |
| 495 |
MOZ_ASSERT(diffF == 0); |
| 496 |
|
| 497 |
// The macroassembler will keep the stack sizeof(uintptr_t)-aligned, so |
| 498 |
// we don't need to take into account `alignExtra` here. |
| 499 |
MOZ_ASSERT(framePushed() - framePushedInitial == |
| 500 |
PushRegsInMaskSizeInBytes(set)); |
| 501 |
} |
| 502 |
|
| 503 |
void MacroAssembler::storeRegsInMask(LiveRegisterSet set, Address dest, |
| 504 |
Register) { |
| 505 |
mozilla::DebugOnly<size_t> offsetInitial = dest.offset; |
| 506 |
|
| 507 |
FloatRegisterSet fpuSet(set.fpus().reduceSetForPush()); |
| 508 |
unsigned numFpu = fpuSet.size(); |
| 509 |
int32_t diffF = fpuSet.getPushSizeInBytes(); |
| 510 |
int32_t diffG = set.gprs().size() * sizeof(intptr_t); |
| 511 |
|
| 512 |
MOZ_ASSERT(dest.offset >= diffG + diffF); |
| 513 |
|
| 514 |
for (GeneralRegisterBackwardIterator iter(set.gprs()); iter.more(); ++iter) { |
| 515 |
diffG -= sizeof(intptr_t); |
| 516 |
dest.offset -= sizeof(intptr_t); |
| 517 |
storePtr(*iter, dest); |
| 518 |
} |
| 519 |
MOZ_ASSERT(diffG == 0); |
| 520 |
(void)diffG; |
| 521 |
|
| 522 |
for (FloatRegisterBackwardIterator iter(fpuSet); iter.more(); ++iter) { |
| 523 |
FloatRegister reg = *iter; |
| 524 |
diffF -= reg.size(); |
| 525 |
numFpu -= 1; |
| 526 |
dest.offset -= reg.size(); |
| 527 |
if (reg.isDouble()) { |
| 528 |
storeDouble(reg, dest); |
| 529 |
} else if (reg.isSingle()) { |
| 530 |
storeFloat32(reg, dest); |
| 531 |
} else if (reg.isSimd128()) { |
| 532 |
storeUnalignedSimd128(reg, dest); |
| 533 |
} else { |
| 534 |
MOZ_CRASH("Unknown register type."); |
| 535 |
} |
| 536 |
} |
| 537 |
MOZ_ASSERT(numFpu == 0); |
| 538 |
(void)numFpu; |
| 539 |
|
| 540 |
// x64 padding to keep the stack aligned on uintptr_t. Keep in sync with |
| 541 |
// GetPushSizeInBytes. |
| 542 |
size_t alignExtra = ((size_t)diffF) % sizeof(uintptr_t); |
| 543 |
MOZ_ASSERT_IF(sizeof(uintptr_t) == 8, alignExtra == 0 || alignExtra == 4); |
| 544 |
MOZ_ASSERT_IF(sizeof(uintptr_t) == 4, alignExtra == 0); |
| 545 |
diffF -= alignExtra; |
| 546 |
MOZ_ASSERT(diffF == 0); |
| 547 |
|
| 548 |
// What this means is: if `alignExtra` is nonzero, then the save area size |
| 549 |
// actually used is `alignExtra` bytes smaller than what |
| 550 |
// PushRegsInMaskSizeInBytes claims. Hence we need to compensate for that. |
| 551 |
MOZ_ASSERT(alignExtra + offsetInitial - dest.offset == |
| 552 |
PushRegsInMaskSizeInBytes(set)); |
| 553 |
} |
| 554 |
|
| 555 |
void MacroAssembler::PopRegsInMaskIgnore(LiveRegisterSet set, |
| 556 |
LiveRegisterSet ignore) { |
| 557 |
mozilla::DebugOnly<size_t> framePushedInitial = framePushed(); |
| 558 |
|
| 559 |
FloatRegisterSet fpuSet(set.fpus().reduceSetForPush()); |
| 560 |
unsigned numFpu = fpuSet.size(); |
| 561 |
int32_t diffG = set.gprs().size() * sizeof(intptr_t); |
| 562 |
int32_t diffF = fpuSet.getPushSizeInBytes(); |
| 563 |
const int32_t reservedG = diffG; |
| 564 |
const int32_t reservedF = diffF; |
| 565 |
|
| 566 |
for (FloatRegisterBackwardIterator iter(fpuSet); iter.more(); ++iter) { |
| 567 |
FloatRegister reg = *iter; |
| 568 |
diffF -= reg.size(); |
| 569 |
numFpu -= 1; |
| 570 |
if (ignore.has(reg)) { |
| 571 |
continue; |
| 572 |
} |
| 573 |
|
| 574 |
Address spillAddress(StackPointer, diffF); |
| 575 |
if (reg.isDouble()) { |
| 576 |
loadDouble(spillAddress, reg); |
| 577 |
} else if (reg.isSingle()) { |
| 578 |
loadFloat32(spillAddress, reg); |
| 579 |
} else if (reg.isSimd128()) { |
| 580 |
loadUnalignedSimd128(spillAddress, reg); |
| 581 |
} else { |
| 582 |
MOZ_CRASH("Unknown register type."); |
| 583 |
} |
| 584 |
} |
| 585 |
freeStack(reservedF); |
| 586 |
MOZ_ASSERT(numFpu == 0); |
| 587 |
(void)numFpu; |
| 588 |
// x64 padding to keep the stack aligned on uintptr_t. Keep in sync with |
| 589 |
// GetPushBytesInSize. |
| 590 |
diffF -= diffF % sizeof(uintptr_t); |
| 591 |
MOZ_ASSERT(diffF == 0); |
| 592 |
|
| 593 |
// On x86, use pop to pop the integer registers, if we're not going to |
| 594 |
// ignore any slots, as it's fast on modern hardware and it's a small |
| 595 |
// instruction. |
| 596 |
if (ignore.emptyGeneral()) { |
| 597 |
for (GeneralRegisterForwardIterator iter(set.gprs()); iter.more(); ++iter) { |
| 598 |
diffG -= sizeof(intptr_t); |
| 599 |
Pop(*iter); |
| 600 |
} |
| 601 |
} else { |
| 602 |
for (GeneralRegisterBackwardIterator iter(set.gprs()); iter.more(); |
| 603 |
++iter) { |
| 604 |
diffG -= sizeof(intptr_t); |
| 605 |
if (!ignore.has(*iter)) { |
| 606 |
loadPtr(Address(StackPointer, diffG), *iter); |
| 607 |
} |
| 608 |
} |
| 609 |
freeStack(reservedG); |
| 610 |
} |
| 611 |
MOZ_ASSERT(diffG == 0); |
| 612 |
|
| 613 |
MOZ_ASSERT(framePushedInitial - framePushed() == |
| 614 |
PushRegsInMaskSizeInBytes(set)); |
| 615 |
} |
| 616 |
|
| 617 |
void MacroAssembler::Push(const Operand op) { |
| 618 |
push(op); |
| 619 |
adjustFrame(sizeof(intptr_t)); |
| 620 |
} |
| 621 |
|
| 622 |
void MacroAssembler::Push(Register reg) { |
| 623 |
push(reg); |
| 624 |
adjustFrame(sizeof(intptr_t)); |
| 625 |
} |
| 626 |
|
| 627 |
void MacroAssembler::Push(const Imm32 imm) { |
| 628 |
push(imm); |
| 629 |
adjustFrame(sizeof(intptr_t)); |
| 630 |
} |
| 631 |
|
| 632 |
void MacroAssembler::Push(const ImmWord imm) { |
| 633 |
push(imm); |
| 634 |
adjustFrame(sizeof(intptr_t)); |
| 635 |
} |
| 636 |
|
| 637 |
void MacroAssembler::Push(const ImmPtr imm) { |
| 638 |
Push(ImmWord(uintptr_t(imm.value))); |
| 639 |
} |
| 640 |
|
| 641 |
void MacroAssembler::Push(const ImmGCPtr ptr) { |
| 642 |
push(ptr); |
| 643 |
adjustFrame(sizeof(intptr_t)); |
| 644 |
} |
| 645 |
|
| 646 |
void MacroAssembler::Push(FloatRegister t) { |
| 647 |
push(t); |
| 648 |
adjustFrame(sizeof(double)); |
| 649 |
} |
| 650 |
|
| 651 |
void MacroAssembler::PushFlags() { |
| 652 |
pushFlags(); |
| 653 |
adjustFrame(sizeof(intptr_t)); |
| 654 |
} |
| 655 |
|
| 656 |
void MacroAssembler::Pop(const Operand op) { |
| 657 |
pop(op); |
| 658 |
implicitPop(sizeof(intptr_t)); |
| 659 |
} |
| 660 |
|
| 661 |
void MacroAssembler::Pop(Register reg) { |
| 662 |
pop(reg); |
| 663 |
implicitPop(sizeof(intptr_t)); |
| 664 |
} |
| 665 |
|
| 666 |
void MacroAssembler::Pop(FloatRegister reg) { |
| 667 |
pop(reg); |
| 668 |
implicitPop(sizeof(double)); |
| 669 |
} |
| 670 |
|
| 671 |
void MacroAssembler::Pop(const ValueOperand& val) { |
| 672 |
popValue(val); |
| 673 |
implicitPop(sizeof(Value)); |
| 674 |
} |
| 675 |
|
| 676 |
void MacroAssembler::PopFlags() { |
| 677 |
popFlags(); |
| 678 |
implicitPop(sizeof(intptr_t)); |
| 679 |
} |
| 680 |
|
| 681 |
void MacroAssembler::PopStackPtr() { Pop(StackPointer); } |
| 682 |
|
| 683 |
void MacroAssembler::freeStackTo(uint32_t framePushed) { |
| 684 |
MOZ_ASSERT(framePushed <= framePushed_); |
| 685 |
lea(Operand(FramePointer, -int32_t(framePushed)), StackPointer); |
| 686 |
framePushed_ = framePushed; |
| 687 |
} |
| 688 |
|
| 689 |
// =============================================================== |
| 690 |
// Simple call functions. |
| 691 |
|
| 692 |
CodeOffset MacroAssembler::call(Register reg) { return Assembler::call(reg); } |
| 693 |
|
| 694 |
CodeOffset MacroAssembler::call(Label* label) { return Assembler::call(label); } |
| 695 |
|
| 696 |
void MacroAssembler::call(const Address& addr) { |
| 697 |
Assembler::call(Operand(addr.base, addr.offset)); |
| 698 |
} |
| 699 |
|
| 700 |
CodeOffset MacroAssembler::call(wasm::SymbolicAddress target) { |
| 701 |
mov(target, eax); |
| 702 |
return Assembler::call(eax); |
| 703 |
} |
| 704 |
|
| 705 |
void MacroAssembler::call(ImmWord target) { Assembler::call(target); } |
| 706 |
|
| 707 |
void MacroAssembler::call(ImmPtr target) { Assembler::call(target); } |
| 708 |
|
| 709 |
void MacroAssembler::call(JitCode* target) { Assembler::call(target); } |
| 710 |
|
| 711 |
CodeOffset MacroAssembler::callWithPatch() { |
| 712 |
return Assembler::callWithPatch(); |
| 713 |
} |
| 714 |
void MacroAssembler::patchCall(uint32_t callerOffset, uint32_t calleeOffset) { |
| 715 |
Assembler::patchCall(callerOffset, calleeOffset); |
| 716 |
} |
| 717 |
|
| 718 |
void MacroAssembler::callAndPushReturnAddress(Register reg) { call(reg); } |
| 719 |
|
| 720 |
void MacroAssembler::callAndPushReturnAddress(Label* label) { call(label); } |
| 721 |
|
| 722 |
// =============================================================== |
| 723 |
// Patchable near/far jumps. |
| 724 |
|
| 725 |
CodeOffset MacroAssembler::farJumpWithPatch() { |
| 726 |
return Assembler::farJumpWithPatch(); |
| 727 |
} |
| 728 |
|
| 729 |
void MacroAssembler::patchFarJump(CodeOffset farJump, uint32_t targetOffset) { |
| 730 |
Assembler::patchFarJump(farJump, targetOffset); |
| 731 |
} |
| 732 |
|
| 733 |
void MacroAssembler::patchFarJump(uint8_t* farJump, uint8_t* target) { |
| 734 |
Assembler::patchFarJump(farJump, target); |
| 735 |
} |
| 736 |
|
| 737 |
CodeOffset MacroAssembler::nopPatchableToCall() { |
| 738 |
masm.nop_five(); |
| 739 |
return CodeOffset(currentOffset()); |
| 740 |
} |
| 741 |
|
| 742 |
void MacroAssembler::patchNopToCall(uint8_t* callsite, uint8_t* target) { |
| 743 |
Assembler::patchFiveByteNopToCall(callsite, target); |
| 744 |
} |
| 745 |
|
| 746 |
void MacroAssembler::patchCallToNop(uint8_t* callsite) { |
| 747 |
Assembler::patchCallToFiveByteNop(callsite); |
| 748 |
} |
| 749 |
|
| 750 |
CodeOffset MacroAssembler::move32WithPatch(Register dest) { |
| 751 |
movl(Imm32(-1), dest); |
| 752 |
return CodeOffset(currentOffset()); |
| 753 |
} |
| 754 |
|
| 755 |
void MacroAssembler::patchMove32(CodeOffset offset, Imm32 n) { |
| 756 |
X86Encoding::SetInt32(masm.data() + offset.offset(), n.value); |
| 757 |
} |
| 758 |
|
| 759 |
// =============================================================== |
| 760 |
// Jit Frames. |
| 761 |
|
| 762 |
uint32_t MacroAssembler::pushFakeReturnAddress(Register scratch) { |
| 763 |
CodeLabel cl; |
| 764 |
|
| 765 |
mov(&cl, scratch); |
| 766 |
Push(scratch); |
| 767 |
bind(&cl); |
| 768 |
uint32_t retAddr = currentOffset(); |
| 769 |
|
| 770 |
addCodeLabel(cl); |
| 771 |
return retAddr; |
| 772 |
} |
| 773 |
|
| 774 |
// =============================================================== |
| 775 |
// WebAssembly |
| 776 |
|
| 777 |
FaultingCodeOffset MacroAssembler::wasmTrapInstruction() { |
| 778 |
return FaultingCodeOffset(ud2().offset()); |
| 779 |
} |
| 780 |
|
| 781 |
void MacroAssembler::wasmBoundsCheck32(Condition cond, Register index, |
| 782 |
Register boundsCheckLimit, Label* ok) { |
| 783 |
cmp32(index, boundsCheckLimit); |
| 784 |
j(cond, ok); |
| 785 |
if (JitOptions.spectreIndexMasking) { |
| 786 |
cmovCCl(cond, Operand(boundsCheckLimit), index); |
| 787 |
} |
| 788 |
} |
| 789 |
|
| 790 |
void MacroAssembler::wasmBoundsCheck32(Condition cond, Register index, |
| 791 |
Address boundsCheckLimit, Label* ok) { |
| 792 |
cmp32(index, Operand(boundsCheckLimit)); |
| 793 |
j(cond, ok); |
| 794 |
if (JitOptions.spectreIndexMasking) { |
| 795 |
cmovCCl(cond, Operand(boundsCheckLimit), index); |
| 796 |
} |
| 797 |
} |
| 798 |
|
| 799 |
// RAII class that generates the jumps to traps when it's destructed, to |
| 800 |
// prevent some code duplication in the outOfLineWasmTruncateXtoY methods. |
| 801 |
struct MOZ_RAII AutoHandleWasmTruncateToIntErrors { |
| 802 |
MacroAssembler& masm; |
| 803 |
Label inputIsNaN; |
| 804 |
Label intOverflow; |
| 805 |
const wasm::TrapSiteDesc& trapSiteDesc; |
| 806 |
|
| 807 |
explicit AutoHandleWasmTruncateToIntErrors( |
| 808 |
MacroAssembler& masm, const wasm::TrapSiteDesc& trapSiteDesc) |
| 809 |
: masm(masm), trapSiteDesc(trapSiteDesc) {} |
| 810 |
|
| 811 |
~AutoHandleWasmTruncateToIntErrors() { |
| 812 |
// Handle errors. These cases are not in arbitrary order: code will |
| 813 |
// fall through to intOverflow. |
| 814 |
masm.bind(&intOverflow); |
| 815 |
masm.wasmTrap(wasm::Trap::IntegerOverflow, trapSiteDesc); |
| 816 |
|
| 817 |
masm.bind(&inputIsNaN); |
| 818 |
masm.wasmTrap(wasm::Trap::InvalidConversionToInteger, trapSiteDesc); |
| 819 |
} |
| 820 |
}; |
| 821 |
|
| 822 |
void MacroAssembler::wasmTruncateDoubleToInt32(FloatRegister input, |
| 823 |
Register output, |
| 824 |
bool isSaturating, |
| 825 |
Label* oolEntry) { |
| 826 |
vcvttsd2si(input, output); |
| 827 |
cmp32(output, Imm32(1)); |
| 828 |
j(Assembler::Overflow, oolEntry); |
| 829 |
} |
| 830 |
|
| 831 |
void MacroAssembler::wasmTruncateFloat32ToInt32(FloatRegister input, |
| 832 |
Register output, |
| 833 |
bool isSaturating, |
| 834 |
Label* oolEntry) { |
| 835 |
vcvttss2si(input, output); |
| 836 |
cmp32(output, Imm32(1)); |
| 837 |
j(Assembler::Overflow, oolEntry); |
| 838 |
} |
| 839 |
|
| 840 |
void MacroAssembler::oolWasmTruncateCheckF64ToI32( |
| 841 |
FloatRegister input, Register output, TruncFlags flags, |
| 842 |
const wasm::TrapSiteDesc& trapSiteDesc, Label* rejoin) { |
| 843 |
bool isUnsigned = flags & TRUNC_UNSIGNED; |
| 844 |
bool isSaturating = flags & TRUNC_SATURATING; |
| 845 |
|
| 846 |
if (isSaturating) { |
| 847 |
if (isUnsigned) { |
| 848 |
// Negative overflow and NaN both are converted to 0, and the only |
| 849 |
// other case is positive overflow which is converted to |
| 850 |
// UINT32_MAX. |
| 851 |
Label nonNegative; |
| 852 |
ScratchDoubleScope fpscratch(*this); |
| 853 |
loadConstantDouble(0.0, fpscratch); |
| 854 |
branchDouble(Assembler::DoubleGreaterThanOrEqual, input, fpscratch, |
| 855 |
&nonNegative); |
| 856 |
move32(Imm32(0), output); |
| 857 |
jump(rejoin); |
| 858 |
|
| 859 |
bind(&nonNegative); |
| 860 |
move32(Imm32(UINT32_MAX), output); |
| 861 |
} else { |
| 862 |
// Negative overflow is already saturated to INT32_MIN, so we only |
| 863 |
// have to handle NaN and positive overflow here. |
| 864 |
Label notNaN; |
| 865 |
branchDouble(Assembler::DoubleOrdered, input, input, ¬NaN); |
| 866 |
move32(Imm32(0), output); |
| 867 |
jump(rejoin); |
| 868 |
|
| 869 |
bind(¬NaN); |
| 870 |
ScratchDoubleScope fpscratch(*this); |
| 871 |
loadConstantDouble(0.0, fpscratch); |
| 872 |
branchDouble(Assembler::DoubleLessThan, input, fpscratch, rejoin); |
| 873 |
sub32(Imm32(1), output); |
| 874 |
} |
| 875 |
jump(rejoin); |
| 876 |
return; |
| 877 |
} |
| 878 |
|
| 879 |
AutoHandleWasmTruncateToIntErrors traps(*this, trapSiteDesc); |
| 880 |
|
| 881 |
// Eagerly take care of NaNs. |
| 882 |
branchDouble(Assembler::DoubleUnordered, input, input, &traps.inputIsNaN); |
| 883 |
|
| 884 |
// For unsigned, fall through to intOverflow failure case. |
| 885 |
if (isUnsigned) { |
| 886 |
return; |
| 887 |
} |
| 888 |
|
| 889 |
// Handle special values. |
| 890 |
|
| 891 |
// We've used vcvttsd2si. The only valid double values that can |
| 892 |
// truncate to INT32_MIN are in ]INT32_MIN - 1; INT32_MIN]. |
| 893 |
ScratchDoubleScope fpscratch(*this); |
| 894 |
loadConstantDouble(double(INT32_MIN) - 1.0, fpscratch); |
| 895 |
branchDouble(Assembler::DoubleLessThanOrEqual, input, fpscratch, |
| 896 |
&traps.intOverflow); |
| 897 |
|
| 898 |
loadConstantDouble(0.0, fpscratch); |
| 899 |
branchDouble(Assembler::DoubleGreaterThan, input, fpscratch, |
| 900 |
&traps.intOverflow); |
| 901 |
jump(rejoin); |
| 902 |
} |
| 903 |
|
| 904 |
void MacroAssembler::oolWasmTruncateCheckF32ToI32( |
| 905 |
FloatRegister input, Register output, TruncFlags flags, |
| 906 |
const wasm::TrapSiteDesc& trapSiteDesc, Label* rejoin) { |
| 907 |
bool isUnsigned = flags & TRUNC_UNSIGNED; |
| 908 |
bool isSaturating = flags & TRUNC_SATURATING; |
| 909 |
|
| 910 |
if (isSaturating) { |
| 911 |
if (isUnsigned) { |
| 912 |
// Negative overflow and NaN both are converted to 0, and the only |
| 913 |
// other case is positive overflow which is converted to |
| 914 |
// UINT32_MAX. |
| 915 |
Label nonNegative; |
| 916 |
ScratchFloat32Scope fpscratch(*this); |
| 917 |
loadConstantFloat32(0.0f, fpscratch); |
| 918 |
branchFloat(Assembler::DoubleGreaterThanOrEqual, input, fpscratch, |
| 919 |
&nonNegative); |
| 920 |
move32(Imm32(0), output); |
| 921 |
jump(rejoin); |
| 922 |
|
| 923 |
bind(&nonNegative); |
| 924 |
move32(Imm32(UINT32_MAX), output); |
| 925 |
} else { |
| 926 |
// Negative overflow is already saturated to INT32_MIN, so we only |
| 927 |
// have to handle NaN and positive overflow here. |
| 928 |
Label notNaN; |
| 929 |
branchFloat(Assembler::DoubleOrdered, input, input, ¬NaN); |
| 930 |
move32(Imm32(0), output); |
| 931 |
jump(rejoin); |
| 932 |
|
| 933 |
bind(¬NaN); |
| 934 |
ScratchFloat32Scope fpscratch(*this); |
| 935 |
loadConstantFloat32(0.0f, fpscratch); |
| 936 |
branchFloat(Assembler::DoubleLessThan, input, fpscratch, rejoin); |
| 937 |
sub32(Imm32(1), output); |
| 938 |
} |
| 939 |
jump(rejoin); |
| 940 |
return; |
| 941 |
} |
| 942 |
|
| 943 |
AutoHandleWasmTruncateToIntErrors traps(*this, trapSiteDesc); |
| 944 |
|
| 945 |
// Eagerly take care of NaNs. |
| 946 |
branchFloat(Assembler::DoubleUnordered, input, input, &traps.inputIsNaN); |
| 947 |
|
| 948 |
// For unsigned, fall through to intOverflow failure case. |
| 949 |
if (isUnsigned) { |
| 950 |
return; |
| 951 |
} |
| 952 |
|
| 953 |
// Handle special values. |
| 954 |
|
| 955 |
// We've used vcvttss2si. Check that the input wasn't |
| 956 |
// float(INT32_MIN), which is the only legimitate input that |
| 957 |
// would truncate to INT32_MIN. |
| 958 |
ScratchFloat32Scope fpscratch(*this); |
| 959 |
loadConstantFloat32(float(INT32_MIN), fpscratch); |
| 960 |
branchFloat(Assembler::DoubleNotEqual, input, fpscratch, &traps.intOverflow); |
| 961 |
jump(rejoin); |
| 962 |
} |
| 963 |
|
| 964 |
void MacroAssembler::oolWasmTruncateCheckF64ToI64( |
| 965 |
FloatRegister input, Register64 output, TruncFlags flags, |
| 966 |
const wasm::TrapSiteDesc& trapSiteDesc, Label* rejoin) { |
| 967 |
bool isUnsigned = flags & TRUNC_UNSIGNED; |
| 968 |
bool isSaturating = flags & TRUNC_SATURATING; |
| 969 |
|
| 970 |
if (isSaturating) { |
| 971 |
if (isUnsigned) { |
| 972 |
// Negative overflow and NaN both are converted to 0, and the only |
| 973 |
// other case is positive overflow which is converted to |
| 974 |
// UINT64_MAX. |
| 975 |
Label positive; |
| 976 |
ScratchDoubleScope fpscratch(*this); |
| 977 |
loadConstantDouble(0.0, fpscratch); |
| 978 |
branchDouble(Assembler::DoubleGreaterThan, input, fpscratch, &positive); |
| 979 |
move64(Imm64(0), output); |
| 980 |
jump(rejoin); |
| 981 |
|
| 982 |
bind(&positive); |
| 983 |
move64(Imm64(UINT64_MAX), output); |
| 984 |
} else { |
| 985 |
// Negative overflow is already saturated to INT64_MIN, so we only |
| 986 |
// have to handle NaN and positive overflow here. |
| 987 |
Label notNaN; |
| 988 |
branchDouble(Assembler::DoubleOrdered, input, input, ¬NaN); |
| 989 |
move64(Imm64(0), output); |
| 990 |
jump(rejoin); |
| 991 |
|
| 992 |
bind(¬NaN); |
| 993 |
ScratchDoubleScope fpscratch(*this); |
| 994 |
loadConstantDouble(0.0, fpscratch); |
| 995 |
branchDouble(Assembler::DoubleLessThan, input, fpscratch, rejoin); |
| 996 |
sub64(Imm64(1), output); |
| 997 |
} |
| 998 |
jump(rejoin); |
| 999 |
return; |
| 1000 |
} |
| 1001 |
|
| 1002 |
AutoHandleWasmTruncateToIntErrors traps(*this, trapSiteDesc); |
| 1003 |
|
| 1004 |
// Eagerly take care of NaNs. |
| 1005 |
branchDouble(Assembler::DoubleUnordered, input, input, &traps.inputIsNaN); |
| 1006 |
|
| 1007 |
// Handle special values. |
| 1008 |
if (isUnsigned) { |
| 1009 |
ScratchDoubleScope fpscratch(*this); |
| 1010 |
loadConstantDouble(0.0, fpscratch); |
| 1011 |
branchDouble(Assembler::DoubleGreaterThan, input, fpscratch, |
| 1012 |
&traps.intOverflow); |
| 1013 |
loadConstantDouble(-1.0, fpscratch); |
| 1014 |
branchDouble(Assembler::DoubleLessThanOrEqual, input, fpscratch, |
| 1015 |
&traps.intOverflow); |
| 1016 |
jump(rejoin); |
| 1017 |
return; |
| 1018 |
} |
| 1019 |
|
| 1020 |
// We've used vcvtsd2sq. The only legit value whose i64 |
| 1021 |
// truncation is INT64_MIN is double(INT64_MIN): exponent is so |
| 1022 |
// high that the highest resolution around is much more than 1. |
| 1023 |
ScratchDoubleScope fpscratch(*this); |
| 1024 |
loadConstantDouble(double(int64_t(INT64_MIN)), fpscratch); |
| 1025 |
branchDouble(Assembler::DoubleNotEqual, input, fpscratch, &traps.intOverflow); |
| 1026 |
jump(rejoin); |
| 1027 |
} |
| 1028 |
|
| 1029 |
void MacroAssembler::oolWasmTruncateCheckF32ToI64( |
| 1030 |
FloatRegister input, Register64 output, TruncFlags flags, |
| 1031 |
const wasm::TrapSiteDesc& trapSiteDesc, Label* rejoin) { |
| 1032 |
bool isUnsigned = flags & TRUNC_UNSIGNED; |
| 1033 |
bool isSaturating = flags & TRUNC_SATURATING; |
| 1034 |
|
| 1035 |
if (isSaturating) { |
| 1036 |
if (isUnsigned) { |
| 1037 |
// Negative overflow and NaN both are converted to 0, and the only |
| 1038 |
// other case is positive overflow which is converted to |
| 1039 |
// UINT64_MAX. |
| 1040 |
Label positive; |
| 1041 |
ScratchFloat32Scope fpscratch(*this); |
| 1042 |
loadConstantFloat32(0.0f, fpscratch); |
| 1043 |
branchFloat(Assembler::DoubleGreaterThan, input, fpscratch, &positive); |
| 1044 |
move64(Imm64(0), output); |
| 1045 |
jump(rejoin); |
| 1046 |
|
| 1047 |
bind(&positive); |
| 1048 |
move64(Imm64(UINT64_MAX), output); |
| 1049 |
} else { |
| 1050 |
// Negative overflow is already saturated to INT64_MIN, so we only |
| 1051 |
// have to handle NaN and positive overflow here. |
| 1052 |
Label notNaN; |
| 1053 |
branchFloat(Assembler::DoubleOrdered, input, input, ¬NaN); |
| 1054 |
move64(Imm64(0), output); |
| 1055 |
jump(rejoin); |
| 1056 |
|
| 1057 |
bind(¬NaN); |
| 1058 |
ScratchFloat32Scope fpscratch(*this); |
| 1059 |
loadConstantFloat32(0.0f, fpscratch); |
| 1060 |
branchFloat(Assembler::DoubleLessThan, input, fpscratch, rejoin); |
| 1061 |
sub64(Imm64(1), output); |
| 1062 |
} |
| 1063 |
jump(rejoin); |
| 1064 |
return; |
| 1065 |
} |
| 1066 |
|
| 1067 |
AutoHandleWasmTruncateToIntErrors traps(*this, trapSiteDesc); |
| 1068 |
|
| 1069 |
// Eagerly take care of NaNs. |
| 1070 |
branchFloat(Assembler::DoubleUnordered, input, input, &traps.inputIsNaN); |
| 1071 |
|
| 1072 |
// Handle special values. |
| 1073 |
if (isUnsigned) { |
| 1074 |
ScratchFloat32Scope fpscratch(*this); |
| 1075 |
loadConstantFloat32(0.0f, fpscratch); |
| 1076 |
branchFloat(Assembler::DoubleGreaterThan, input, fpscratch, |
| 1077 |
&traps.intOverflow); |
| 1078 |
loadConstantFloat32(-1.0f, fpscratch); |
| 1079 |
branchFloat(Assembler::DoubleLessThanOrEqual, input, fpscratch, |
| 1080 |
&traps.intOverflow); |
| 1081 |
jump(rejoin); |
| 1082 |
return; |
| 1083 |
} |
| 1084 |
|
| 1085 |
// We've used vcvtss2sq. See comment in outOfLineWasmTruncateDoubleToInt64. |
| 1086 |
ScratchFloat32Scope fpscratch(*this); |
| 1087 |
loadConstantFloat32(float(int64_t(INT64_MIN)), fpscratch); |
| 1088 |
branchFloat(Assembler::DoubleNotEqual, input, fpscratch, &traps.intOverflow); |
| 1089 |
jump(rejoin); |
| 1090 |
} |
| 1091 |
|
| 1092 |
void MacroAssembler::enterFakeExitFrameForWasm(Register cxreg, Register scratch, |
| 1093 |
ExitFrameType type) { |
| 1094 |
enterFakeExitFrame(cxreg, scratch, type); |
| 1095 |
} |
| 1096 |
|
| 1097 |
CodeOffset MacroAssembler::sub32FromMemAndBranchIfNegativeWithPatch( |
| 1098 |
Address address, Label* label) { |
| 1099 |
// -128 is arbitrary, but makes `*address` count upwards, which may help |
| 1100 |
// to identify cases where the subsequent ::patch..() call was forgotten. |
| 1101 |
int numImmBytes = subl(Imm32(-128), Operand(address)); |
| 1102 |
// This is vitally important for patching |
| 1103 |
MOZ_RELEASE_ASSERT(numImmBytes == 1); |
| 1104 |
// Points immediately after the location to patch |
| 1105 |
CodeOffset patchPoint = CodeOffset(currentOffset()); |
| 1106 |
jSrc(Condition::Signed, label); |
| 1107 |
return patchPoint; |
| 1108 |
} |
| 1109 |
|
| 1110 |
void MacroAssembler::patchSub32FromMemAndBranchIfNegative(CodeOffset offset, |
| 1111 |
Imm32 imm) { |
| 1112 |
int32_t val = imm.value; |
| 1113 |
// Patching it to zero would make the insn pointless |
| 1114 |
MOZ_RELEASE_ASSERT(val >= 1 && val <= 127); |
| 1115 |
uint8_t* ptr = (uint8_t*)masm.data() + offset.offset() - 1; |
| 1116 |
MOZ_RELEASE_ASSERT(*ptr == uint8_t(-128)); // as created above |
| 1117 |
*ptr = uint8_t(val) & 0x7F; |
| 1118 |
} |
| 1119 |
|
| 1120 |
// ======================================================================== |
| 1121 |
// Primitive atomic operations. |
| 1122 |
|
| 1123 |
static void ExtendTo32(MacroAssembler& masm, Scalar::Type type, Register r) { |
| 1124 |
switch (type) { |
| 1125 |
case Scalar::Int8: |
| 1126 |
masm.movsbl(r, r); |
| 1127 |
break; |
| 1128 |
case Scalar::Uint8: |
| 1129 |
masm.movzbl(r, r); |
| 1130 |
break; |
| 1131 |
case Scalar::Int16: |
| 1132 |
masm.movswl(r, r); |
| 1133 |
break; |
| 1134 |
case Scalar::Uint16: |
| 1135 |
masm.movzwl(r, r); |
| 1136 |
break; |
| 1137 |
case Scalar::Int32: |
| 1138 |
case Scalar::Uint32: |
| 1139 |
break; |
| 1140 |
default: |
| 1141 |
MOZ_CRASH("unexpected type"); |
| 1142 |
} |
| 1143 |
} |
| 1144 |
|
| 1145 |
#ifdef DEBUG |
| 1146 |
static inline bool IsByteReg(Register r) { |
| 1147 |
AllocatableGeneralRegisterSet byteRegs(Registers::SingleByteRegs); |
| 1148 |
return byteRegs.has(r); |
| 1149 |
} |
| 1150 |
|
| 1151 |
static inline bool IsByteReg(Imm32 r) { |
| 1152 |
// Nothing |
| 1153 |
return true; |
| 1154 |
} |
| 1155 |
#endif |
| 1156 |
|
| 1157 |
template <typename T> |
| 1158 |
static void CompareExchange(MacroAssembler& masm, |
| 1159 |
const wasm::MemoryAccessDesc* access, |
| 1160 |
Scalar::Type type, const T& mem, Register oldval, |
| 1161 |
Register newval, Register output) { |
| 1162 |
MOZ_ASSERT(output == eax); |
| 1163 |
|
| 1164 |
if (oldval != output) { |
| 1165 |
masm.movl(oldval, output); |
| 1166 |
} |
| 1167 |
|
| 1168 |
if (access) { |
| 1169 |
masm.append(*access, wasm::TrapMachineInsn::Atomic, |
| 1170 |
FaultingCodeOffset(masm.currentOffset())); |
| 1171 |
} |
| 1172 |
|
| 1173 |
// NOTE: the generated code must match the assembly code in gen_cmpxchg in |
| 1174 |
// GenerateAtomicOperations.py |
| 1175 |
switch (Scalar::byteSize(type)) { |
| 1176 |
case 1: |
| 1177 |
MOZ_ASSERT(IsByteReg(newval)); |
| 1178 |
masm.lock_cmpxchgb(newval, Operand(mem)); |
| 1179 |
break; |
| 1180 |
case 2: |
| 1181 |
masm.lock_cmpxchgw(newval, Operand(mem)); |
| 1182 |
break; |
| 1183 |
case 4: |
| 1184 |
masm.lock_cmpxchgl(newval, Operand(mem)); |
| 1185 |
break; |
| 1186 |
default: |
| 1187 |
MOZ_CRASH("Invalid"); |
| 1188 |
} |
| 1189 |
|
| 1190 |
ExtendTo32(masm, type, output); |
| 1191 |
} |
| 1192 |
|
| 1193 |
void MacroAssembler::compareExchange(Scalar::Type type, Synchronization, |
| 1194 |
const Address& mem, Register oldval, |
| 1195 |
Register newval, Register output) { |
| 1196 |
CompareExchange(*this, nullptr, type, mem, oldval, newval, output); |
| 1197 |
} |
| 1198 |
|
| 1199 |
void MacroAssembler::compareExchange(Scalar::Type type, Synchronization, |
| 1200 |
const BaseIndex& mem, Register oldval, |
| 1201 |
Register newval, Register output) { |
| 1202 |
CompareExchange(*this, nullptr, type, mem, oldval, newval, output); |
| 1203 |
} |
| 1204 |
|
| 1205 |
void MacroAssembler::wasmCompareExchange(const wasm::MemoryAccessDesc& access, |
| 1206 |
const Address& mem, Register oldval, |
| 1207 |
Register newval, Register output) { |
| 1208 |
CompareExchange(*this, &access, access.type(), mem, oldval, newval, output); |
| 1209 |
} |
| 1210 |
|
| 1211 |
void MacroAssembler::wasmCompareExchange(const wasm::MemoryAccessDesc& access, |
| 1212 |
const BaseIndex& mem, Register oldval, |
| 1213 |
Register newval, Register output) { |
| 1214 |
CompareExchange(*this, &access, access.type(), mem, oldval, newval, output); |
| 1215 |
} |
| 1216 |
|
| 1217 |
template <typename T> |
| 1218 |
static void AtomicExchange(MacroAssembler& masm, |
| 1219 |
const wasm::MemoryAccessDesc* access, |
| 1220 |
Scalar::Type type, const T& mem, Register value, |
| 1221 |
Register output) |
| 1222 |
// NOTE: the generated code must match the assembly code in gen_exchange in |
| 1223 |
// GenerateAtomicOperations.py |
| 1224 |
{ |
| 1225 |
if (value != output) { |
| 1226 |
masm.movl(value, output); |
| 1227 |
} |
| 1228 |
|
| 1229 |
if (access) { |
| 1230 |
masm.append(*access, wasm::TrapMachineInsn::Atomic, |
| 1231 |
FaultingCodeOffset(masm.currentOffset())); |
| 1232 |
} |
| 1233 |
|
| 1234 |
switch (Scalar::byteSize(type)) { |
| 1235 |
case 1: |
| 1236 |
MOZ_ASSERT(IsByteReg(output)); |
| 1237 |
masm.xchgb(output, Operand(mem)); |
| 1238 |
break; |
| 1239 |
case 2: |
| 1240 |
masm.xchgw(output, Operand(mem)); |
| 1241 |
break; |
| 1242 |
case 4: |
| 1243 |
masm.xchgl(output, Operand(mem)); |
| 1244 |
break; |
| 1245 |
default: |
| 1246 |
MOZ_CRASH("Invalid"); |
| 1247 |
} |
| 1248 |
ExtendTo32(masm, type, output); |
| 1249 |
} |
| 1250 |
|
| 1251 |
void MacroAssembler::atomicExchange(Scalar::Type type, Synchronization, |
| 1252 |
const Address& mem, Register value, |
| 1253 |
Register output) { |
| 1254 |
AtomicExchange(*this, nullptr, type, mem, value, output); |
| 1255 |
} |
| 1256 |
|
| 1257 |
void MacroAssembler::atomicExchange(Scalar::Type type, Synchronization, |
| 1258 |
const BaseIndex& mem, Register value, |
| 1259 |
Register output) { |
| 1260 |
AtomicExchange(*this, nullptr, type, mem, value, output); |
| 1261 |
} |
| 1262 |
|
| 1263 |
void MacroAssembler::wasmAtomicExchange(const wasm::MemoryAccessDesc& access, |
| 1264 |
const Address& mem, Register value, |
| 1265 |
Register output) { |
| 1266 |
AtomicExchange(*this, &access, access.type(), mem, value, output); |
| 1267 |
} |
| 1268 |
|
| 1269 |
void MacroAssembler::wasmAtomicExchange(const wasm::MemoryAccessDesc& access, |
| 1270 |
const BaseIndex& mem, Register value, |
| 1271 |
Register output) { |
| 1272 |
AtomicExchange(*this, &access, access.type(), mem, value, output); |
| 1273 |
} |
| 1274 |
|
| 1275 |
static void SetupValue(MacroAssembler& masm, AtomicOp op, Imm32 src, |
| 1276 |
Register output) { |
| 1277 |
if (op == AtomicOp::Sub) { |
| 1278 |
masm.movl(Imm32(-src.value), output); |
| 1279 |
} else { |
| 1280 |
masm.movl(src, output); |
| 1281 |
} |
| 1282 |
} |
| 1283 |
|
| 1284 |
static void SetupValue(MacroAssembler& masm, AtomicOp op, Register src, |
| 1285 |
Register output) { |
| 1286 |
if (src != output) { |
| 1287 |
masm.movl(src, output); |
| 1288 |
} |
| 1289 |
if (op == AtomicOp::Sub) { |
| 1290 |
masm.negl(output); |
| 1291 |
} |
| 1292 |
} |
| 1293 |
|
| 1294 |
static auto WasmTrapMachineInsn(Scalar::Type arrayType, AtomicOp op) { |
| 1295 |
switch (op) { |
| 1296 |
case AtomicOp::Add: |
| 1297 |
case AtomicOp::Sub: |
| 1298 |
return wasm::TrapMachineInsn::Atomic; |
| 1299 |
case AtomicOp::And: |
| 1300 |
case AtomicOp::Or: |
| 1301 |
case AtomicOp::Xor: |
| 1302 |
switch (arrayType) { |
| 1303 |
case Scalar::Int8: |
| 1304 |
case Scalar::Uint8: |
| 1305 |
return wasm::TrapMachineInsn::Load8; |
| 1306 |
case Scalar::Int16: |
| 1307 |
case Scalar::Uint16: |
| 1308 |
return wasm::TrapMachineInsn::Load16; |
| 1309 |
case Scalar::Int32: |
| 1310 |
case Scalar::Uint32: |
| 1311 |
return wasm::TrapMachineInsn::Load32; |
| 1312 |
default: |
| 1313 |
break; |
| 1314 |
} |
| 1315 |
[[fallthrough]]; |
| 1316 |
default: |
| 1317 |
break; |
| 1318 |
} |
| 1319 |
MOZ_CRASH(); |
| 1320 |
} |
| 1321 |
|
| 1322 |
template <typename T, typename V> |
| 1323 |
static void AtomicFetchOp(MacroAssembler& masm, |
| 1324 |
const wasm::MemoryAccessDesc* access, |
| 1325 |
Scalar::Type arrayType, AtomicOp op, V value, |
| 1326 |
const T& mem, Register temp, Register output) { |
| 1327 |
// Note value can be an Imm or a Register. |
| 1328 |
|
| 1329 |
// NOTE: the generated code must match the assembly code in gen_fetchop in |
| 1330 |
// GenerateAtomicOperations.py |
| 1331 |
|
| 1332 |
// Setup the output register. |
| 1333 |
switch (op) { |
| 1334 |
case AtomicOp::Add: |
| 1335 |
case AtomicOp::Sub: |
| 1336 |
MOZ_ASSERT(temp == InvalidReg); |
| 1337 |
MOZ_ASSERT_IF(Scalar::byteSize(arrayType) == 1, |
| 1338 |
IsByteReg(output) && IsByteReg(value)); |
| 1339 |
|
| 1340 |
SetupValue(masm, op, value, output); |
| 1341 |
break; |
| 1342 |
case AtomicOp::And: |
| 1343 |
case AtomicOp::Or: |
| 1344 |
case AtomicOp::Xor: |
| 1345 |
MOZ_ASSERT(output != temp && output == eax); |
| 1346 |
MOZ_ASSERT_IF(Scalar::byteSize(arrayType) == 1, |
| 1347 |
IsByteReg(output) && IsByteReg(temp)); |
| 1348 |
|
| 1349 |
// Bitwise operations don't require any additional setup. |
| 1350 |
break; |
| 1351 |
default: |
| 1352 |
MOZ_CRASH(); |
| 1353 |
} |
| 1354 |
|
| 1355 |
auto lock_xadd = [&]() { |
| 1356 |
switch (arrayType) { |
| 1357 |
case Scalar::Int8: |
| 1358 |
case Scalar::Uint8: |
| 1359 |
masm.lock_xaddb(output, Operand(mem)); |
| 1360 |
break; |
| 1361 |
case Scalar::Int16: |
| 1362 |
case Scalar::Uint16: |
| 1363 |
masm.lock_xaddw(output, Operand(mem)); |
| 1364 |
break; |
| 1365 |
case Scalar::Int32: |
| 1366 |
case Scalar::Uint32: |
| 1367 |
masm.lock_xaddl(output, Operand(mem)); |
| 1368 |
break; |
| 1369 |
default: |
| 1370 |
MOZ_CRASH(); |
| 1371 |
} |
| 1372 |
}; |
| 1373 |
|
| 1374 |
auto load = [&]() { |
| 1375 |
switch (arrayType) { |
| 1376 |
case Scalar::Int8: |
| 1377 |
case Scalar::Uint8: |
| 1378 |
masm.movzbl(Operand(mem), eax); |
| 1379 |
break; |
| 1380 |
case Scalar::Int16: |
| 1381 |
case Scalar::Uint16: |
| 1382 |
masm.movzwl(Operand(mem), eax); |
| 1383 |
break; |
| 1384 |
case Scalar::Int32: |
| 1385 |
case Scalar::Uint32: |
| 1386 |
masm.movl(Operand(mem), eax); |
| 1387 |
break; |
| 1388 |
default: |
| 1389 |
MOZ_CRASH(); |
| 1390 |
} |
| 1391 |
}; |
| 1392 |
|
| 1393 |
auto bitwiseOp = [&]() { |
| 1394 |
switch (op) { |
| 1395 |
case AtomicOp::And: |
| 1396 |
masm.andl(value, temp); |
| 1397 |
break; |
| 1398 |
case AtomicOp::Or: |
| 1399 |
masm.orl(value, temp); |
| 1400 |
break; |
| 1401 |
case AtomicOp::Xor: |
| 1402 |
masm.xorl(value, temp); |
| 1403 |
break; |
| 1404 |
default: |
| 1405 |
MOZ_CRASH(); |
| 1406 |
} |
| 1407 |
}; |
| 1408 |
|
| 1409 |
auto lock_cmpxchg = [&]() { |
| 1410 |
switch (arrayType) { |
| 1411 |
case Scalar::Int8: |
| 1412 |
case Scalar::Uint8: |
| 1413 |
masm.lock_cmpxchgb(temp, Operand(mem)); |
| 1414 |
break; |
| 1415 |
case Scalar::Int16: |
| 1416 |
case Scalar::Uint16: |
| 1417 |
masm.lock_cmpxchgw(temp, Operand(mem)); |
| 1418 |
break; |
| 1419 |
case Scalar::Int32: |
| 1420 |
case Scalar::Uint32: |
| 1421 |
masm.lock_cmpxchgl(temp, Operand(mem)); |
| 1422 |
break; |
| 1423 |
default: |
| 1424 |
MOZ_CRASH(); |
| 1425 |
} |
| 1426 |
}; |
| 1427 |
|
| 1428 |
// Add trap instruction directly before the load. |
| 1429 |
if (access) { |
| 1430 |
masm.append(*access, WasmTrapMachineInsn(arrayType, op), |
| 1431 |
FaultingCodeOffset(masm.currentOffset())); |
| 1432 |
} |
| 1433 |
|
| 1434 |
switch (op) { |
| 1435 |
case AtomicOp::Add: |
| 1436 |
case AtomicOp::Sub: |
| 1437 |
// `add` and `sub` operations can be optimized with XADD. |
| 1438 |
lock_xadd(); |
| 1439 |
|
| 1440 |
ExtendTo32(masm, arrayType, output); |
| 1441 |
break; |
| 1442 |
|
| 1443 |
case AtomicOp::And: |
| 1444 |
case AtomicOp::Or: |
| 1445 |
case AtomicOp::Xor: { |
| 1446 |
// Bitwise operations need a CAS loop. |
| 1447 |
|
| 1448 |
// Load memory into eax. |
| 1449 |
load(); |
| 1450 |
|
| 1451 |
// Loop. |
| 1452 |
Label again; |
| 1453 |
masm.bind(&again); |
| 1454 |
masm.movl(eax, temp); |
| 1455 |
|
| 1456 |
// temp = temp <op> value. |
| 1457 |
bitwiseOp(); |
| 1458 |
|
| 1459 |
// Compare and swap `temp` with memory. |
| 1460 |
lock_cmpxchg(); |
| 1461 |
|
| 1462 |
// Repeat if the comparison failed. |
| 1463 |
masm.j(MacroAssembler::NonZero, &again); |
| 1464 |
|
| 1465 |
// Sign-extend the zero-extended load. |
| 1466 |
if (Scalar::isSignedIntType(arrayType)) { |
| 1467 |
ExtendTo32(masm, arrayType, eax); |
| 1468 |
} |
| 1469 |
break; |
| 1470 |
} |
| 1471 |
|
| 1472 |
default: |
| 1473 |
MOZ_CRASH(); |
| 1474 |
} |
| 1475 |
} |
| 1476 |
|
| 1477 |
void MacroAssembler::atomicFetchOp(Scalar::Type arrayType, Synchronization, |
| 1478 |
AtomicOp op, Register value, |
| 1479 |
const BaseIndex& mem, Register temp, |
| 1480 |
Register output) { |
| 1481 |
AtomicFetchOp(*this, nullptr, arrayType, op, value, mem, temp, output); |
| 1482 |
} |
| 1483 |
|
| 1484 |
void MacroAssembler::atomicFetchOp(Scalar::Type arrayType, Synchronization, |
| 1485 |
AtomicOp op, Register value, |
| 1486 |
const Address& mem, Register temp, |
| 1487 |
Register output) { |
| 1488 |
AtomicFetchOp(*this, nullptr, arrayType, op, value, mem, temp, output); |
| 1489 |
} |
| 1490 |
|
| 1491 |
void MacroAssembler::atomicFetchOp(Scalar::Type arrayType, Synchronization, |
| 1492 |
AtomicOp op, Imm32 value, |
| 1493 |
const BaseIndex& mem, Register temp, |
| 1494 |
Register output) { |
| 1495 |
AtomicFetchOp(*this, nullptr, arrayType, op, value, mem, temp, output); |
| 1496 |
} |
| 1497 |
|
| 1498 |
void MacroAssembler::atomicFetchOp(Scalar::Type arrayType, Synchronization, |
| 1499 |
AtomicOp op, Imm32 value, const Address& mem, |
| 1500 |
Register temp, Register output) { |
| 1501 |
AtomicFetchOp(*this, nullptr, arrayType, op, value, mem, temp, output); |
| 1502 |
} |
| 1503 |
|
| 1504 |
void MacroAssembler::wasmAtomicFetchOp(const wasm::MemoryAccessDesc& access, |
| 1505 |
AtomicOp op, Register value, |
| 1506 |
const Address& mem, Register temp, |
| 1507 |
Register output) { |
| 1508 |
AtomicFetchOp(*this, &access, access.type(), op, value, mem, temp, output); |
| 1509 |
} |
| 1510 |
|
| 1511 |
void MacroAssembler::wasmAtomicFetchOp(const wasm::MemoryAccessDesc& access, |
| 1512 |
AtomicOp op, Imm32 value, |
| 1513 |
const Address& mem, Register temp, |
| 1514 |
Register output) { |
| 1515 |
AtomicFetchOp(*this, &access, access.type(), op, value, mem, temp, output); |
| 1516 |
} |
| 1517 |
|
| 1518 |
void MacroAssembler::wasmAtomicFetchOp(const wasm::MemoryAccessDesc& access, |
| 1519 |
AtomicOp op, Register value, |
| 1520 |
const BaseIndex& mem, Register temp, |
| 1521 |
Register output) { |
| 1522 |
AtomicFetchOp(*this, &access, access.type(), op, value, mem, temp, output); |
| 1523 |
} |
| 1524 |
|
| 1525 |
void MacroAssembler::wasmAtomicFetchOp(const wasm::MemoryAccessDesc& access, |
| 1526 |
AtomicOp op, Imm32 value, |
| 1527 |
const BaseIndex& mem, Register temp, |
| 1528 |
Register output) { |
| 1529 |
AtomicFetchOp(*this, &access, access.type(), op, value, mem, temp, output); |
| 1530 |
} |
| 1531 |
|
| 1532 |
template <typename T, typename V> |
| 1533 |
static void AtomicEffectOp(MacroAssembler& masm, |
| 1534 |
const wasm::MemoryAccessDesc* access, |
| 1535 |
Scalar::Type arrayType, AtomicOp op, V value, |
| 1536 |
const T& mem) { |
| 1537 |
if (access) { |
| 1538 |
masm.append(*access, wasm::TrapMachineInsn::Atomic, |
| 1539 |
FaultingCodeOffset(masm.currentOffset())); |
| 1540 |
} |
| 1541 |
|
| 1542 |
switch (Scalar::byteSize(arrayType)) { |
| 1543 |
case 1: |
| 1544 |
switch (op) { |
| 1545 |
case AtomicOp::Add: |
| 1546 |
masm.lock_addb(value, Operand(mem)); |
| 1547 |
break; |
| 1548 |
case AtomicOp::Sub: |
| 1549 |
masm.lock_subb(value, Operand(mem)); |
| 1550 |
break; |
| 1551 |
case AtomicOp::And: |
| 1552 |
masm.lock_andb(value, Operand(mem)); |
| 1553 |
break; |
| 1554 |
case AtomicOp::Or: |
| 1555 |
masm.lock_orb(value, Operand(mem)); |
| 1556 |
break; |
| 1557 |
case AtomicOp::Xor: |
| 1558 |
masm.lock_xorb(value, Operand(mem)); |
| 1559 |
break; |
| 1560 |
default: |
| 1561 |
MOZ_CRASH(); |
| 1562 |
} |
| 1563 |
break; |
| 1564 |
case 2: |
| 1565 |
switch (op) { |
| 1566 |
case AtomicOp::Add: |
| 1567 |
masm.lock_addw(value, Operand(mem)); |
| 1568 |
break; |
| 1569 |
case AtomicOp::Sub: |
| 1570 |
masm.lock_subw(value, Operand(mem)); |
| 1571 |
break; |
| 1572 |
case AtomicOp::And: |
| 1573 |
masm.lock_andw(value, Operand(mem)); |
| 1574 |
break; |
| 1575 |
case AtomicOp::Or: |
| 1576 |
masm.lock_orw(value, Operand(mem)); |
| 1577 |
break; |
| 1578 |
case AtomicOp::Xor: |
| 1579 |
masm.lock_xorw(value, Operand(mem)); |
| 1580 |
break; |
| 1581 |
default: |
| 1582 |
MOZ_CRASH(); |
| 1583 |
} |
| 1584 |
break; |
| 1585 |
case 4: |
| 1586 |
switch (op) { |
| 1587 |
case AtomicOp::Add: |
| 1588 |
masm.lock_addl(value, Operand(mem)); |
| 1589 |
break; |
| 1590 |
case AtomicOp::Sub: |
| 1591 |
masm.lock_subl(value, Operand(mem)); |
| 1592 |
break; |
| 1593 |
case AtomicOp::And: |
| 1594 |
masm.lock_andl(value, Operand(mem)); |
| 1595 |
break; |
| 1596 |
case AtomicOp::Or: |
| 1597 |
masm.lock_orl(value, Operand(mem)); |
| 1598 |
break; |
| 1599 |
case AtomicOp::Xor: |
| 1600 |
masm.lock_xorl(value, Operand(mem)); |
| 1601 |
break; |
| 1602 |
default: |
| 1603 |
MOZ_CRASH(); |
| 1604 |
} |
| 1605 |
break; |
| 1606 |
default: |
| 1607 |
MOZ_CRASH(); |
| 1608 |
} |
| 1609 |
} |
| 1610 |
|
| 1611 |
void MacroAssembler::wasmAtomicEffectOp(const wasm::MemoryAccessDesc& access, |
| 1612 |
AtomicOp op, Register value, |
| 1613 |
const Address& mem, Register temp) { |
| 1614 |
MOZ_ASSERT(temp == InvalidReg); |
| 1615 |
AtomicEffectOp(*this, &access, access.type(), op, value, mem); |
| 1616 |
} |
| 1617 |
|
| 1618 |
void MacroAssembler::wasmAtomicEffectOp(const wasm::MemoryAccessDesc& access, |
| 1619 |
AtomicOp op, Imm32 value, |
| 1620 |
const Address& mem, Register temp) { |
| 1621 |
MOZ_ASSERT(temp == InvalidReg); |
| 1622 |
AtomicEffectOp(*this, &access, access.type(), op, value, mem); |
| 1623 |
} |
| 1624 |
|
| 1625 |
void MacroAssembler::wasmAtomicEffectOp(const wasm::MemoryAccessDesc& access, |
| 1626 |
AtomicOp op, Register value, |
| 1627 |
const BaseIndex& mem, Register temp) { |
| 1628 |
MOZ_ASSERT(temp == InvalidReg); |
| 1629 |
AtomicEffectOp(*this, &access, access.type(), op, value, mem); |
| 1630 |
} |
| 1631 |
|
| 1632 |
void MacroAssembler::wasmAtomicEffectOp(const wasm::MemoryAccessDesc& access, |
| 1633 |
AtomicOp op, Imm32 value, |
| 1634 |
const BaseIndex& mem, Register temp) { |
| 1635 |
MOZ_ASSERT(temp == InvalidReg); |
| 1636 |
AtomicEffectOp(*this, &access, access.type(), op, value, mem); |
| 1637 |
} |
| 1638 |
|
| 1639 |
// ======================================================================== |
| 1640 |
// JS atomic operations. |
| 1641 |
|
| 1642 |
template <typename T> |
| 1643 |
static void CompareExchangeJS(MacroAssembler& masm, Scalar::Type arrayType, |
| 1644 |
Synchronization sync, const T& mem, |
| 1645 |
Register oldval, Register newval, Register temp, |
| 1646 |
AnyRegister output) { |
| 1647 |
if (arrayType == Scalar::Uint32) { |
| 1648 |
masm.compareExchange(arrayType, sync, mem, oldval, newval, temp); |
| 1649 |
masm.convertUInt32ToDouble(temp, output.fpu()); |
| 1650 |
} else { |
| 1651 |
masm.compareExchange(arrayType, sync, mem, oldval, newval, output.gpr()); |
| 1652 |
} |
| 1653 |
} |
| 1654 |
|
| 1655 |
void MacroAssembler::compareExchangeJS(Scalar::Type arrayType, |
| 1656 |
Synchronization sync, const Address& mem, |
| 1657 |
Register oldval, Register newval, |
| 1658 |
Register temp, AnyRegister output) { |
| 1659 |
CompareExchangeJS(*this, arrayType, sync, mem, oldval, newval, temp, output); |
| 1660 |
} |
| 1661 |
|
| 1662 |
void MacroAssembler::compareExchangeJS(Scalar::Type arrayType, |
| 1663 |
Synchronization sync, |
| 1664 |
const BaseIndex& mem, Register oldval, |
| 1665 |
Register newval, Register temp, |
| 1666 |
AnyRegister output) { |
| 1667 |
CompareExchangeJS(*this, arrayType, sync, mem, oldval, newval, temp, output); |
| 1668 |
} |
| 1669 |
|
| 1670 |
template <typename T> |
| 1671 |
static void AtomicExchangeJS(MacroAssembler& masm, Scalar::Type arrayType, |
| 1672 |
Synchronization sync, const T& mem, Register value, |
| 1673 |
Register temp, AnyRegister output) { |
| 1674 |
if (arrayType == Scalar::Uint32) { |
| 1675 |
masm.atomicExchange(arrayType, sync, mem, value, temp); |
| 1676 |
masm.convertUInt32ToDouble(temp, output.fpu()); |
| 1677 |
} else { |
| 1678 |
masm.atomicExchange(arrayType, sync, mem, value, output.gpr()); |
| 1679 |
} |
| 1680 |
} |
| 1681 |
|
| 1682 |
void MacroAssembler::atomicExchangeJS(Scalar::Type arrayType, |
| 1683 |
Synchronization sync, const Address& mem, |
| 1684 |
Register value, Register temp, |
| 1685 |
AnyRegister output) { |
| 1686 |
AtomicExchangeJS(*this, arrayType, sync, mem, value, temp, output); |
| 1687 |
} |
| 1688 |
|
| 1689 |
void MacroAssembler::atomicExchangeJS(Scalar::Type arrayType, |
| 1690 |
Synchronization sync, |
| 1691 |
const BaseIndex& mem, Register value, |
| 1692 |
Register temp, AnyRegister output) { |
| 1693 |
AtomicExchangeJS(*this, arrayType, sync, mem, value, temp, output); |
| 1694 |
} |
| 1695 |
|
| 1696 |
template <typename T> |
| 1697 |
static void AtomicFetchOpJS(MacroAssembler& masm, Scalar::Type arrayType, |
| 1698 |
Synchronization sync, AtomicOp op, Register value, |
| 1699 |
const T& mem, Register temp1, Register temp2, |
| 1700 |
AnyRegister output) { |
| 1701 |
if (arrayType == Scalar::Uint32) { |
| 1702 |
masm.atomicFetchOp(arrayType, sync, op, value, mem, temp2, temp1); |
| 1703 |
masm.convertUInt32ToDouble(temp1, output.fpu()); |
| 1704 |
} else { |
| 1705 |
masm.atomicFetchOp(arrayType, sync, op, value, mem, temp1, output.gpr()); |
| 1706 |
} |
| 1707 |
} |
| 1708 |
|
| 1709 |
void MacroAssembler::atomicFetchOpJS(Scalar::Type arrayType, |
| 1710 |
Synchronization sync, AtomicOp op, |
| 1711 |
Register value, const Address& mem, |
| 1712 |
Register temp1, Register temp2, |
| 1713 |
AnyRegister output) { |
| 1714 |
AtomicFetchOpJS(*this, arrayType, sync, op, value, mem, temp1, temp2, output); |
| 1715 |
} |
| 1716 |
|
| 1717 |
void MacroAssembler::atomicFetchOpJS(Scalar::Type arrayType, |
| 1718 |
Synchronization sync, AtomicOp op, |
| 1719 |
Register value, const BaseIndex& mem, |
| 1720 |
Register temp1, Register temp2, |
| 1721 |
AnyRegister output) { |
| 1722 |
AtomicFetchOpJS(*this, arrayType, sync, op, value, mem, temp1, temp2, output); |
| 1723 |
} |
| 1724 |
|
| 1725 |
void MacroAssembler::atomicEffectOpJS(Scalar::Type arrayType, Synchronization, |
| 1726 |
AtomicOp op, Register value, |
| 1727 |
const BaseIndex& mem, Register temp) { |
| 1728 |
MOZ_ASSERT(temp == InvalidReg); |
| 1729 |
AtomicEffectOp(*this, nullptr, arrayType, op, value, mem); |
| 1730 |
} |
| 1731 |
|
| 1732 |
void MacroAssembler::atomicEffectOpJS(Scalar::Type arrayType, Synchronization, |
| 1733 |
AtomicOp op, Register value, |
| 1734 |
const Address& mem, Register temp) { |
| 1735 |
MOZ_ASSERT(temp == InvalidReg); |
| 1736 |
AtomicEffectOp(*this, nullptr, arrayType, op, value, mem); |
| 1737 |
} |
| 1738 |
|
| 1739 |
void MacroAssembler::atomicEffectOpJS(Scalar::Type arrayType, Synchronization, |
| 1740 |
AtomicOp op, Imm32 value, |
| 1741 |
const Address& mem, Register temp) { |
| 1742 |
MOZ_ASSERT(temp == InvalidReg); |
| 1743 |
AtomicEffectOp(*this, nullptr, arrayType, op, value, mem); |
| 1744 |
} |
| 1745 |
|
| 1746 |
void MacroAssembler::atomicEffectOpJS(Scalar::Type arrayType, |
| 1747 |
Synchronization sync, AtomicOp op, |
| 1748 |
Imm32 value, const BaseIndex& mem, |
| 1749 |
Register temp) { |
| 1750 |
MOZ_ASSERT(temp == InvalidReg); |
| 1751 |
AtomicEffectOp(*this, nullptr, arrayType, op, value, mem); |
| 1752 |
} |
| 1753 |
|
| 1754 |
template <typename T> |
| 1755 |
static void AtomicFetchOpJS(MacroAssembler& masm, Scalar::Type arrayType, |
| 1756 |
Synchronization sync, AtomicOp op, Imm32 value, |
| 1757 |
const T& mem, Register temp1, Register temp2, |
| 1758 |
AnyRegister output) { |
| 1759 |
if (arrayType == Scalar::Uint32) { |
| 1760 |
masm.atomicFetchOp(arrayType, sync, op, value, mem, temp2, temp1); |
| 1761 |
masm.convertUInt32ToDouble(temp1, output.fpu()); |
| 1762 |
} else { |
| 1763 |
masm.atomicFetchOp(arrayType, sync, op, value, mem, temp1, output.gpr()); |
| 1764 |
} |
| 1765 |
} |
| 1766 |
|
| 1767 |
void MacroAssembler::atomicFetchOpJS(Scalar::Type arrayType, |
| 1768 |
Synchronization sync, AtomicOp op, |
| 1769 |
Imm32 value, const Address& mem, |
| 1770 |
Register temp1, Register temp2, |
| 1771 |
AnyRegister output) { |
| 1772 |
AtomicFetchOpJS(*this, arrayType, sync, op, value, mem, temp1, temp2, output); |
| 1773 |
} |
| 1774 |
|
| 1775 |
void MacroAssembler::atomicFetchOpJS(Scalar::Type arrayType, |
| 1776 |
Synchronization sync, AtomicOp op, |
| 1777 |
Imm32 value, const BaseIndex& mem, |
| 1778 |
Register temp1, Register temp2, |
| 1779 |
AnyRegister output) { |
| 1780 |
AtomicFetchOpJS(*this, arrayType, sync, op, value, mem, temp1, temp2, output); |
| 1781 |
} |
| 1782 |
|
| 1783 |
void MacroAssembler::atomicPause() { masm.pause(); } |
| 1784 |
|
| 1785 |
// ======================================================================== |
| 1786 |
// Spectre Mitigations. |
| 1787 |
|
| 1788 |
void MacroAssembler::speculationBarrier() { |
| 1789 |
// Spectre mitigation recommended by Intel and AMD suggest to use lfence as |
| 1790 |
// a way to force all speculative execution of instructions to end. |
| 1791 |
MOZ_ASSERT(HasSSE2()); |
| 1792 |
masm.lfence(); |
| 1793 |
} |
| 1794 |
|
| 1795 |
void MacroAssembler::floorFloat32ToInt32(FloatRegister src, Register dest, |
| 1796 |
Label* fail) { |
| 1797 |
if (HasSSE41()) { |
| 1798 |
// Fail on negative-zero. |
| 1799 |
branchNegativeZeroFloat32(src, dest, fail); |
| 1800 |
|
| 1801 |
// Round toward -Infinity. |
| 1802 |
{ |
| 1803 |
ScratchFloat32Scope scratch(*this); |
| 1804 |
vroundss(X86Encoding::RoundDown, src, scratch); |
| 1805 |
truncateFloat32ToInt32(scratch, dest, fail); |
| 1806 |
} |
| 1807 |
} else { |
| 1808 |
Label negative, end; |
| 1809 |
|
| 1810 |
// Branch to a slow path for negative inputs. Doesn't catch NaN or -0. |
| 1811 |
{ |
| 1812 |
ScratchFloat32Scope scratch(*this); |
| 1813 |
zeroFloat32(scratch); |
| 1814 |
branchFloat(Assembler::DoubleLessThan, src, scratch, &negative); |
| 1815 |
} |
| 1816 |
|
| 1817 |
// Fail on negative-zero. |
| 1818 |
branchNegativeZeroFloat32(src, dest, fail); |
| 1819 |
|
| 1820 |
// Input is non-negative, so truncation correctly rounds. |
| 1821 |
truncateFloat32ToInt32(src, dest, fail); |
| 1822 |
jump(&end); |
| 1823 |
|
| 1824 |
// Input is negative, but isn't -0. |
| 1825 |
// Negative values go on a comparatively expensive path, since no |
| 1826 |
// native rounding mode matches JS semantics. Still better than callVM. |
| 1827 |
bind(&negative); |
| 1828 |
{ |
| 1829 |
// Truncate and round toward zero. |
| 1830 |
// This is off-by-one for everything but integer-valued inputs. |
| 1831 |
// |
| 1832 |
// Directly call vcvttss2si instead of truncateFloat32ToInt32 because we |
| 1833 |
// want to perform failure handling ourselves. |
| 1834 |
vcvttss2si(src, dest); |
| 1835 |
|
| 1836 |
// Test whether the input double was integer-valued. |
| 1837 |
{ |
| 1838 |
ScratchFloat32Scope scratch(*this); |
| 1839 |
convertInt32ToFloat32(dest, scratch); |
| 1840 |
branchFloat(Assembler::DoubleEqualOrUnordered, src, scratch, &end); |
| 1841 |
} |
| 1842 |
|
| 1843 |
// Input is not integer-valued, so we rounded off-by-one in the |
| 1844 |
// wrong direction. Correct by subtraction. |
| 1845 |
// |
| 1846 |
// Overflows if vcvttss2si returned the failure return value INT_MIN. |
| 1847 |
branchSub32(Assembler::Overflow, Imm32(1), dest, fail); |
| 1848 |
} |
| 1849 |
|
| 1850 |
bind(&end); |
| 1851 |
} |
| 1852 |
} |
| 1853 |
|
| 1854 |
void MacroAssembler::floorDoubleToInt32(FloatRegister src, Register dest, |
| 1855 |
Label* fail) { |
| 1856 |
if (HasSSE41()) { |
| 1857 |
// Fail on negative-zero. |
| 1858 |
branchNegativeZero(src, dest, fail); |
| 1859 |
|
| 1860 |
// Round toward -Infinity. |
| 1861 |
{ |
| 1862 |
ScratchDoubleScope scratch(*this); |
| 1863 |
vroundsd(X86Encoding::RoundDown, src, scratch); |
| 1864 |
truncateDoubleToInt32(scratch, dest, fail); |
| 1865 |
} |
| 1866 |
} else { |
| 1867 |
Label negative, end; |
| 1868 |
|
| 1869 |
// Branch to a slow path for negative inputs. Doesn't catch NaN or -0. |
| 1870 |
{ |
| 1871 |
ScratchDoubleScope scratch(*this); |
| 1872 |
zeroDouble(scratch); |
| 1873 |
branchDouble(Assembler::DoubleLessThan, src, scratch, &negative); |
| 1874 |
} |
| 1875 |
|
| 1876 |
// Fail on negative-zero. |
| 1877 |
branchNegativeZero(src, dest, fail); |
| 1878 |
|
| 1879 |
// Input is non-negative, so truncation correctly rounds. |
| 1880 |
truncateDoubleToInt32(src, dest, fail); |
| 1881 |
jump(&end); |
| 1882 |
|
| 1883 |
// Input is negative, but isn't -0. |
| 1884 |
// Negative values go on a comparatively expensive path, since no |
| 1885 |
// native rounding mode matches JS semantics. Still better than callVM. |
| 1886 |
bind(&negative); |
| 1887 |
{ |
| 1888 |
// Truncate and round toward zero. |
| 1889 |
// This is off-by-one for everything but integer-valued inputs. |
| 1890 |
// |
| 1891 |
// Directly call vcvttsd2si instead of truncateDoubleToInt32 because we |
| 1892 |
// want to perform failure handling ourselves. |
| 1893 |
vcvttsd2si(src, dest); |
| 1894 |
|
| 1895 |
// Test whether the input double was integer-valued. |
| 1896 |
{ |
| 1897 |
ScratchDoubleScope scratch(*this); |
| 1898 |
convertInt32ToDouble(dest, scratch); |
| 1899 |
branchDouble(Assembler::DoubleEqualOrUnordered, src, scratch, &end); |
| 1900 |
} |
| 1901 |
|
| 1902 |
// Input is not integer-valued, so we rounded off-by-one in the |
| 1903 |
// wrong direction. Correct by subtraction. |
| 1904 |
// |
| 1905 |
// Overflows if vcvttsd2si returned the failure return value INT_MIN. |
| 1906 |
branchSub32(Assembler::Overflow, Imm32(1), dest, fail); |
| 1907 |
} |
| 1908 |
|
| 1909 |
bind(&end); |
| 1910 |
} |
| 1911 |
} |
| 1912 |
|
| 1913 |
void MacroAssembler::ceilFloat32ToInt32(FloatRegister src, Register dest, |
| 1914 |
Label* fail) { |
| 1915 |
ScratchFloat32Scope scratch(*this); |
| 1916 |
|
| 1917 |
Label lessThanOrEqualMinusOne; |
| 1918 |
|
| 1919 |
// If x is in ]-1,0], ceil(x) is -0, which cannot be represented as an int32. |
| 1920 |
// Fail if x > -1 and the sign bit is set. |
| 1921 |
loadConstantFloat32(-1.f, scratch); |
| 1922 |
branchFloat(Assembler::DoubleLessThanOrEqualOrUnordered, src, scratch, |
| 1923 |
&lessThanOrEqualMinusOne); |
| 1924 |
vmovmskps(src, dest); |
| 1925 |
branchTest32(Assembler::NonZero, dest, Imm32(1), fail); |
| 1926 |
|
| 1927 |
if (HasSSE41()) { |
| 1928 |
// x <= -1 or x > -0 |
| 1929 |
bind(&lessThanOrEqualMinusOne); |
| 1930 |
// Round toward +Infinity. |
| 1931 |
vroundss(X86Encoding::RoundUp, src, scratch); |
| 1932 |
truncateFloat32ToInt32(scratch, dest, fail); |
| 1933 |
return; |
| 1934 |
} |
| 1935 |
|
| 1936 |
// No SSE4.1 |
| 1937 |
Label end; |
| 1938 |
|
| 1939 |
// x >= 0 and x is not -0.0. We can truncate integer values, and truncate and |
| 1940 |
// add 1 to non-integer values. This will also work for values >= INT_MAX + 1, |
| 1941 |
// as the truncate operation will return INT_MIN and we'll fail. |
| 1942 |
truncateFloat32ToInt32(src, dest, fail); |
| 1943 |
convertInt32ToFloat32(dest, scratch); |
| 1944 |
branchFloat(Assembler::DoubleEqualOrUnordered, src, scratch, &end); |
| 1945 |
|
| 1946 |
// Input is not integer-valued, add 1 to obtain the ceiling value. |
| 1947 |
// If input > INT_MAX, output == INT_MAX so adding 1 will overflow. |
| 1948 |
branchAdd32(Assembler::Overflow, Imm32(1), dest, fail); |
| 1949 |
jump(&end); |
| 1950 |
|
| 1951 |
// x <= -1, truncation is the way to go. |
| 1952 |
bind(&lessThanOrEqualMinusOne); |
| 1953 |
truncateFloat32ToInt32(src, dest, fail); |
| 1954 |
|
| 1955 |
bind(&end); |
| 1956 |
} |
| 1957 |
|
| 1958 |
void MacroAssembler::ceilDoubleToInt32(FloatRegister src, Register dest, |
| 1959 |
Label* fail) { |
| 1960 |
ScratchDoubleScope scratch(*this); |
| 1961 |
|
| 1962 |
Label lessThanOrEqualMinusOne; |
| 1963 |
|
| 1964 |
// If x is in ]-1,0], ceil(x) is -0, which cannot be represented as an int32. |
| 1965 |
// Fail if x > -1 and the sign bit is set. |
| 1966 |
loadConstantDouble(-1.0, scratch); |
| 1967 |
branchDouble(Assembler::DoubleLessThanOrEqualOrUnordered, src, scratch, |
| 1968 |
&lessThanOrEqualMinusOne); |
| 1969 |
vmovmskpd(src, dest); |
| 1970 |
branchTest32(Assembler::NonZero, dest, Imm32(1), fail); |
| 1971 |
|
| 1972 |
if (HasSSE41()) { |
| 1973 |
// x <= -1 or x > -0 |
| 1974 |
bind(&lessThanOrEqualMinusOne); |
| 1975 |
// Round toward +Infinity. |
| 1976 |
vroundsd(X86Encoding::RoundUp, src, scratch); |
| 1977 |
truncateDoubleToInt32(scratch, dest, fail); |
| 1978 |
return; |
| 1979 |
} |
| 1980 |
|
| 1981 |
// No SSE4.1 |
| 1982 |
Label end; |
| 1983 |
|
| 1984 |
// x >= 0 and x is not -0.0. We can truncate integer values, and truncate and |
| 1985 |
// add 1 to non-integer values. This will also work for values >= INT_MAX + 1, |
| 1986 |
// as the truncate operation will return INT_MIN and we'll fail. |
| 1987 |
truncateDoubleToInt32(src, dest, fail); |
| 1988 |
convertInt32ToDouble(dest, scratch); |
| 1989 |
branchDouble(Assembler::DoubleEqualOrUnordered, src, scratch, &end); |
| 1990 |
|
| 1991 |
// Input is not integer-valued, add 1 to obtain the ceiling value. |
| 1992 |
// If input > INT_MAX, output == INT_MAX so adding 1 will overflow. |
| 1993 |
branchAdd32(Assembler::Overflow, Imm32(1), dest, fail); |
| 1994 |
jump(&end); |
| 1995 |
|
| 1996 |
// x <= -1, truncation is the way to go. |
| 1997 |
bind(&lessThanOrEqualMinusOne); |
| 1998 |
truncateDoubleToInt32(src, dest, fail); |
| 1999 |
|
| 2000 |
bind(&end); |
| 2001 |
} |
| 2002 |
|
| 2003 |
void MacroAssembler::truncDoubleToInt32(FloatRegister src, Register dest, |
| 2004 |
Label* fail) { |
| 2005 |
Label lessThanOrEqualMinusOne; |
| 2006 |
|
| 2007 |
// Bail on ]-1; -0] range |
| 2008 |
{ |
| 2009 |
ScratchDoubleScope scratch(*this); |
| 2010 |
loadConstantDouble(-1, scratch); |
| 2011 |
branchDouble(Assembler::DoubleLessThanOrEqualOrUnordered, src, scratch, |
| 2012 |
&lessThanOrEqualMinusOne); |
| 2013 |
} |
| 2014 |
|
| 2015 |
// Test for remaining values with the sign bit set, i.e. ]-1; -0] |
| 2016 |
vmovmskpd(src, dest); |
| 2017 |
branchTest32(Assembler::NonZero, dest, Imm32(1), fail); |
| 2018 |
|
| 2019 |
// x <= -1 or x >= +0, truncation is the way to go. |
| 2020 |
bind(&lessThanOrEqualMinusOne); |
| 2021 |
truncateDoubleToInt32(src, dest, fail); |
| 2022 |
} |
| 2023 |
|
| 2024 |
void MacroAssembler::truncFloat32ToInt32(FloatRegister src, Register dest, |
| 2025 |
Label* fail) { |
| 2026 |
Label lessThanOrEqualMinusOne; |
| 2027 |
|
| 2028 |
// Bail on ]-1; -0] range |
| 2029 |
{ |
| 2030 |
ScratchFloat32Scope scratch(*this); |
| 2031 |
loadConstantFloat32(-1.f, scratch); |
| 2032 |
branchFloat(Assembler::DoubleLessThanOrEqualOrUnordered, src, scratch, |
| 2033 |
&lessThanOrEqualMinusOne); |
| 2034 |
} |
| 2035 |
|
| 2036 |
// Test for remaining values with the sign bit set, i.e. ]-1; -0] |
| 2037 |
vmovmskps(src, dest); |
| 2038 |
branchTest32(Assembler::NonZero, dest, Imm32(1), fail); |
| 2039 |
|
| 2040 |
// x <= -1 or x >= +0, truncation is the way to go. |
| 2041 |
bind(&lessThanOrEqualMinusOne); |
| 2042 |
truncateFloat32ToInt32(src, dest, fail); |
| 2043 |
} |
| 2044 |
|
| 2045 |
void MacroAssembler::roundFloat32ToInt32(FloatRegister src, Register dest, |
| 2046 |
FloatRegister temp, Label* fail) { |
| 2047 |
ScratchFloat32Scope scratch(*this); |
| 2048 |
|
| 2049 |
Label negativeOrZero, negative, end; |
| 2050 |
|
| 2051 |
// Branch to a slow path for non-positive inputs. Doesn't catch NaN. |
| 2052 |
zeroFloat32(scratch); |
| 2053 |
loadConstantFloat32(GetBiggestNumberLessThan(0.5f), temp); |
| 2054 |
branchFloat(Assembler::DoubleLessThanOrEqual, src, scratch, &negativeOrZero); |
| 2055 |
{ |
| 2056 |
// Input is strictly positive or NaN. Add the biggest float less than 0.5 |
| 2057 |
// and truncate, rounding down (because if the input is the biggest float |
| 2058 |
// less than 0.5, adding 0.5 would undesirably round up to 1). Note that we |
| 2059 |
// have to add the input to the temp register because we're not allowed to |
| 2060 |
// modify the input register. |
| 2061 |
addFloat32(src, temp); |
| 2062 |
truncateFloat32ToInt32(temp, dest, fail); |
| 2063 |
jump(&end); |
| 2064 |
} |
| 2065 |
|
| 2066 |
// Input is negative, +0 or -0. |
| 2067 |
bind(&negativeOrZero); |
| 2068 |
{ |
| 2069 |
// Branch on negative input. |
| 2070 |
j(Assembler::NotEqual, &negative); |
| 2071 |
|
| 2072 |
// Fail on negative-zero. |
| 2073 |
branchNegativeZeroFloat32(src, dest, fail); |
| 2074 |
|
| 2075 |
// Input is +0. |
| 2076 |
xor32(dest, dest); |
| 2077 |
jump(&end); |
| 2078 |
} |
| 2079 |
|
| 2080 |
// Input is negative. |
| 2081 |
bind(&negative); |
| 2082 |
{ |
| 2083 |
// Inputs in [-0.5, 0) are rounded to -0. Fail. |
| 2084 |
loadConstantFloat32(-0.5f, scratch); |
| 2085 |
branchFloat(Assembler::DoubleGreaterThanOrEqual, src, scratch, fail); |
| 2086 |
|
| 2087 |
// Other negative inputs need the biggest float less than 0.5 added. |
| 2088 |
// |
| 2089 |
// The result is stored in the temp register (currently contains the biggest |
| 2090 |
// float less than 0.5). |
| 2091 |
addFloat32(src, temp); |
| 2092 |
|
| 2093 |
if (HasSSE41()) { |
| 2094 |
// Round toward -Infinity. |
| 2095 |
vroundss(X86Encoding::RoundDown, temp, scratch); |
| 2096 |
|
| 2097 |
// Truncate. |
| 2098 |
truncateFloat32ToInt32(scratch, dest, fail); |
| 2099 |
} else { |
| 2100 |
// Round toward -Infinity without the benefit of ROUNDSS. |
| 2101 |
|
| 2102 |
// Truncate and round toward zero. |
| 2103 |
// This is off-by-one for everything but integer-valued inputs. |
| 2104 |
// |
| 2105 |
// Directly call vcvttss2si instead of truncateFloat32ToInt32 because we |
| 2106 |
// want to perform failure handling ourselves. |
| 2107 |
vcvttss2si(temp, dest); |
| 2108 |
|
| 2109 |
// Test whether the truncated float was integer-valued. |
| 2110 |
convertInt32ToFloat32(dest, scratch); |
| 2111 |
branchFloat(Assembler::DoubleEqualOrUnordered, temp, scratch, &end); |
| 2112 |
|
| 2113 |
// Input is not integer-valued, so we rounded off-by-one in the |
| 2114 |
// wrong direction. Correct by subtraction. |
| 2115 |
// |
| 2116 |
// Overflows if vcvttss2si returned the failure return value INT_MIN. |
| 2117 |
branchSub32(Assembler::Overflow, Imm32(1), dest, fail); |
| 2118 |
} |
| 2119 |
} |
| 2120 |
|
| 2121 |
bind(&end); |
| 2122 |
} |
| 2123 |
|
| 2124 |
void MacroAssembler::roundDoubleToInt32(FloatRegister src, Register dest, |
| 2125 |
FloatRegister temp, Label* fail) { |
| 2126 |
ScratchDoubleScope scratch(*this); |
| 2127 |
|
| 2128 |
Label negativeOrZero, negative, end; |
| 2129 |
|
| 2130 |
// Branch to a slow path for non-positive inputs. Doesn't catch NaN. |
| 2131 |
zeroDouble(scratch); |
| 2132 |
loadConstantDouble(GetBiggestNumberLessThan(0.5), temp); |
| 2133 |
branchDouble(Assembler::DoubleLessThanOrEqual, src, scratch, &negativeOrZero); |
| 2134 |
{ |
| 2135 |
// Input is strictly positive or NaN. Add the biggest double less than 0.5 |
| 2136 |
// and truncate, rounding down (because if the input is the biggest double |
| 2137 |
// less than 0.5, adding 0.5 would undesirably round up to 1). Note that we |
| 2138 |
// have to add the input to the temp register because we're not allowed to |
| 2139 |
// modify the input register. |
| 2140 |
addDouble(src, temp); |
| 2141 |
truncateDoubleToInt32(temp, dest, fail); |
| 2142 |
jump(&end); |
| 2143 |
} |
| 2144 |
|
| 2145 |
// Input is negative, +0 or -0. |
| 2146 |
bind(&negativeOrZero); |
| 2147 |
{ |
| 2148 |
// Branch on negative input. |
| 2149 |
j(Assembler::NotEqual, &negative); |
| 2150 |
|
| 2151 |
// Fail on negative-zero. |
| 2152 |
branchNegativeZero(src, dest, fail, /* maybeNonZero = */ false); |
| 2153 |
|
| 2154 |
// Input is +0 |
| 2155 |
xor32(dest, dest); |
| 2156 |
jump(&end); |
| 2157 |
} |
| 2158 |
|
| 2159 |
// Input is negative. |
| 2160 |
bind(&negative); |
| 2161 |
{ |
| 2162 |
// Inputs in [-0.5, 0) are rounded to -0. Fail. |
| 2163 |
loadConstantDouble(-0.5, scratch); |
| 2164 |
branchDouble(Assembler::DoubleGreaterThanOrEqual, src, scratch, fail); |
| 2165 |
|
| 2166 |
// Other negative inputs need the biggest double less than 0.5 added. |
| 2167 |
// |
| 2168 |
// The result is stored in the temp register (currently contains the biggest |
| 2169 |
// double less than 0.5). |
| 2170 |
addDouble(src, temp); |
| 2171 |
|
| 2172 |
if (HasSSE41()) { |
| 2173 |
// Round toward -Infinity. |
| 2174 |
vroundsd(X86Encoding::RoundDown, temp, scratch); |
| 2175 |
|
| 2176 |
// Truncate. |
| 2177 |
truncateDoubleToInt32(scratch, dest, fail); |
| 2178 |
} else { |
| 2179 |
// Round toward -Infinity without the benefit of ROUNDSD. |
| 2180 |
|
| 2181 |
// Truncate and round toward zero. |
| 2182 |
// This is off-by-one for everything but integer-valued inputs. |
| 2183 |
// |
| 2184 |
// Directly call vcvttsd2si instead of truncateDoubleToInt32 because we |
| 2185 |
// want to perform failure handling ourselves. |
| 2186 |
vcvttsd2si(temp, dest); |
| 2187 |
|
| 2188 |
// Test whether the truncated double was integer-valued. |
| 2189 |
convertInt32ToDouble(dest, scratch); |
| 2190 |
branchDouble(Assembler::DoubleEqualOrUnordered, temp, scratch, &end); |
| 2191 |
|
| 2192 |
// Input is not integer-valued, so we rounded off-by-one in the |
| 2193 |
// wrong direction. Correct by subtraction. |
| 2194 |
// |
| 2195 |
// Overflows if vcvttsd2si returned the failure return value INT_MIN. |
| 2196 |
branchSub32(Assembler::Overflow, Imm32(1), dest, fail); |
| 2197 |
} |
| 2198 |
} |
| 2199 |
|
| 2200 |
bind(&end); |
| 2201 |
} |
| 2202 |
|
| 2203 |
void MacroAssembler::nearbyIntDouble(RoundingMode mode, FloatRegister src, |
| 2204 |
FloatRegister dest) { |
| 2205 |
MOZ_ASSERT(HasRoundInstruction(mode)); |
| 2206 |
vroundsd(Assembler::ToX86RoundingMode(mode), src, dest); |
| 2207 |
} |
| 2208 |
|
| 2209 |
void MacroAssembler::nearbyIntFloat32(RoundingMode mode, FloatRegister src, |
| 2210 |
FloatRegister dest) { |
| 2211 |
MOZ_ASSERT(HasRoundInstruction(mode)); |
| 2212 |
vroundss(Assembler::ToX86RoundingMode(mode), src, dest); |
| 2213 |
} |
| 2214 |
|
| 2215 |
void MacroAssembler::copySignDouble(FloatRegister lhs, FloatRegister rhs, |
| 2216 |
FloatRegister output) { |
| 2217 |
ScratchDoubleScope scratch(*this); |
| 2218 |
|
| 2219 |
// TODO Support AVX2 |
| 2220 |
if (rhs == output) { |
| 2221 |
MOZ_ASSERT(lhs != rhs); |
| 2222 |
double keepSignMask = mozilla::BitwiseCast<double>(INT64_MIN); |
| 2223 |
loadConstantDouble(keepSignMask, scratch); |
| 2224 |
vandpd(scratch, rhs, output); |
| 2225 |
|
| 2226 |
double clearSignMask = mozilla::BitwiseCast<double>(INT64_MAX); |
| 2227 |
loadConstantDouble(clearSignMask, scratch); |
| 2228 |
vandpd(lhs, scratch, scratch); |
| 2229 |
} else { |
| 2230 |
double clearSignMask = mozilla::BitwiseCast<double>(INT64_MAX); |
| 2231 |
loadConstantDouble(clearSignMask, scratch); |
| 2232 |
vandpd(scratch, lhs, output); |
| 2233 |
|
| 2234 |
double keepSignMask = mozilla::BitwiseCast<double>(INT64_MIN); |
| 2235 |
loadConstantDouble(keepSignMask, scratch); |
| 2236 |
vandpd(rhs, scratch, scratch); |
| 2237 |
} |
| 2238 |
|
| 2239 |
vorpd(scratch, output, output); |
| 2240 |
} |
| 2241 |
|
| 2242 |
void MacroAssembler::copySignFloat32(FloatRegister lhs, FloatRegister rhs, |
| 2243 |
FloatRegister output) { |
| 2244 |
ScratchFloat32Scope scratch(*this); |
| 2245 |
|
| 2246 |
// TODO Support AVX2 |
| 2247 |
if (rhs == output) { |
| 2248 |
MOZ_ASSERT(lhs != rhs); |
| 2249 |
float keepSignMask = mozilla::BitwiseCast<float>(INT32_MIN); |
| 2250 |
loadConstantFloat32(keepSignMask, scratch); |
| 2251 |
vandps(scratch, output, output); |
| 2252 |
|
| 2253 |
float clearSignMask = mozilla::BitwiseCast<float>(INT32_MAX); |
| 2254 |
loadConstantFloat32(clearSignMask, scratch); |
| 2255 |
vandps(lhs, scratch, scratch); |
| 2256 |
} else { |
| 2257 |
float clearSignMask = mozilla::BitwiseCast<float>(INT32_MAX); |
| 2258 |
loadConstantFloat32(clearSignMask, scratch); |
| 2259 |
vandps(scratch, lhs, output); |
| 2260 |
|
| 2261 |
float keepSignMask = mozilla::BitwiseCast<float>(INT32_MIN); |
| 2262 |
loadConstantFloat32(keepSignMask, scratch); |
| 2263 |
vandps(rhs, scratch, scratch); |
| 2264 |
} |
| 2265 |
|
| 2266 |
vorps(scratch, output, output); |
| 2267 |
} |
| 2268 |
|
| 2269 |
void MacroAssembler::shiftIndex32AndAdd(Register indexTemp32, int shift, |
| 2270 |
Register pointer) { |
| 2271 |
if (IsShiftInScaleRange(shift)) { |
| 2272 |
computeEffectiveAddress( |
| 2273 |
BaseIndex(pointer, indexTemp32, ShiftToScale(shift)), pointer); |
| 2274 |
return; |
| 2275 |
} |
| 2276 |
lshift32(Imm32(shift), indexTemp32); |
| 2277 |
addPtr(indexTemp32, pointer); |
| 2278 |
} |
| 2279 |
|
| 2280 |
CodeOffset MacroAssembler::wasmMarkedSlowCall(const wasm::CallSiteDesc& desc, |
| 2281 |
const Register reg) { |
| 2282 |
CodeOffset offset = call(desc, reg); |
| 2283 |
wasmMarkCallAsSlow(); |
| 2284 |
return offset; |
| 2285 |
} |
| 2286 |
|
| 2287 |
//}}} check_macroassembler_style |