| 1 | // Copyright (C) 2017 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | // Qt-Security score:critical reason:jit |
| 4 | |
| 5 | #include <QBuffer> |
| 6 | #include <QFile> |
| 7 | |
| 8 | #include "qv4engine_p.h" |
| 9 | #include "qv4baselineassembler_p.h" |
| 10 | #include "qv4assemblercommon_p.h" |
| 11 | #include <private/qv4function_p.h> |
| 12 | #include <private/qv4runtime_p.h> |
| 13 | #include <private/qv4stackframe_p.h> |
| 14 | |
| 15 | #include <wtf/Vector.h> |
| 16 | #include <assembler/MacroAssembler.h> |
| 17 | #include <assembler/MacroAssemblerCodeRef.h> |
| 18 | #include <assembler/LinkBuffer.h> |
| 19 | #include <WTFStubs.h> |
| 20 | |
| 21 | #undef ENABLE_ALL_ASSEMBLERS_FOR_REFACTORING_PURPOSES |
| 22 | |
| 23 | #if QT_CONFIG(qml_jit) |
| 24 | |
| 25 | QT_BEGIN_NAMESPACE |
| 26 | namespace QV4 { |
| 27 | namespace JIT { |
| 28 | |
| 29 | #define ASM_GENERATE_RUNTIME_CALL(function, destination) \ |
| 30 | pasm()->GENERATE_RUNTIME_CALL(function, destination) |
| 31 | #define callHelper(x) \ |
| 32 | PlatformAssemblerCommon::callRuntimeUnchecked(reinterpret_cast<void *>(&x), #x) |
| 33 | |
| 34 | const QV4::Value::ValueTypeInternal IntegerTag = QV4::Value::ValueTypeInternal::Integer; |
| 35 | |
| 36 | static ReturnedValue toNumberHelper(ReturnedValue v) |
| 37 | { |
| 38 | return Encode(Value::fromReturnedValue(val: v).toNumber()); |
| 39 | } |
| 40 | |
| 41 | static ReturnedValue toInt32Helper(ReturnedValue v) |
| 42 | { |
| 43 | return Encode(Value::fromReturnedValue(val: v).toInt32()); |
| 44 | } |
| 45 | |
| 46 | #if QT_POINTER_SIZE == 8 || defined(ENABLE_ALL_ASSEMBLERS_FOR_REFACTORING_PURPOSES) |
| 47 | class PlatformAssembler64 : public PlatformAssemblerCommon |
| 48 | { |
| 49 | public: |
| 50 | PlatformAssembler64(const Value *constantTable) |
| 51 | : PlatformAssemblerCommon(constantTable) |
| 52 | {} |
| 53 | |
| 54 | void callRuntime(const void *funcPtr, CallResultDestination dest) |
| 55 | { |
| 56 | PlatformAssemblerCommon::callRuntime(funcPtr); |
| 57 | if (dest == CallResultDestination::InAccumulator) |
| 58 | saveReturnValueInAccumulator(); |
| 59 | else if (AccumulatorRegister == ReturnValueRegister) |
| 60 | loadUndefined(); |
| 61 | } |
| 62 | |
| 63 | void saveReturnValueInAccumulator() |
| 64 | { |
| 65 | move(src: ReturnValueRegister, dest: AccumulatorRegister); |
| 66 | } |
| 67 | |
| 68 | void loadUndefined(RegisterID dest = AccumulatorRegister) |
| 69 | { |
| 70 | move(imm: TrustedImm64(0), dest); |
| 71 | } |
| 72 | |
| 73 | void copyConst(int constIndex, Address dest) |
| 74 | { |
| 75 | //### |
| 76 | if (constant(idx: constIndex).isUndefined()) { |
| 77 | loadUndefined(dest: ScratchRegister); |
| 78 | } else { |
| 79 | load64(address: loadConstAddress(constIndex, baseReg: ScratchRegister), dest: ScratchRegister); |
| 80 | } |
| 81 | store64(src: ScratchRegister, address: dest); |
| 82 | } |
| 83 | |
| 84 | void copyReg(Address src, Address dst) |
| 85 | { |
| 86 | load64(address: src, dest: ScratchRegister); |
| 87 | store64(src: ScratchRegister, address: dst); |
| 88 | } |
| 89 | |
| 90 | void loadPointerFromValue(Address addr, RegisterID dest = AccumulatorRegister) |
| 91 | { |
| 92 | load64(address: addr, dest); |
| 93 | } |
| 94 | |
| 95 | void loadAccumulator(Address addr) |
| 96 | { |
| 97 | load64(address: addr, dest: AccumulatorRegister); |
| 98 | } |
| 99 | |
| 100 | void storeAccumulator(Address addr) |
| 101 | { |
| 102 | store64(src: AccumulatorRegister, address: addr); |
| 103 | } |
| 104 | |
| 105 | void moveReg(Address sourceRegAddress, Address destRegAddress) |
| 106 | { |
| 107 | load64(address: sourceRegAddress, dest: ScratchRegister); |
| 108 | store64(src: ScratchRegister, address: destRegAddress); |
| 109 | } |
| 110 | |
| 111 | void loadString(int stringId) |
| 112 | { |
| 113 | loadAccumulator(addr: loadStringAddress(stringId)); |
| 114 | } |
| 115 | |
| 116 | void loadValue(ReturnedValue value) |
| 117 | { |
| 118 | move(imm: TrustedImm64(value), dest: AccumulatorRegister); |
| 119 | } |
| 120 | |
| 121 | void storeHeapObject(RegisterID source, Address addr) |
| 122 | { |
| 123 | store64(src: source, address: addr); |
| 124 | } |
| 125 | |
| 126 | void generateCatchTrampoline() |
| 127 | { |
| 128 | PlatformAssemblerCommon::generateCatchTrampoline(loadUndefined: [this](){loadUndefined();}); |
| 129 | } |
| 130 | |
| 131 | void jumpNotUndefined(int offset) |
| 132 | { |
| 133 | auto jump = branch64(cond: NotEqual, left: AccumulatorRegister, right: TrustedImm64(0)); |
| 134 | addJumpToOffset(jump, offset); |
| 135 | } |
| 136 | |
| 137 | Jump jumpEmpty() |
| 138 | { |
| 139 | return branch64(cond: Equal, left: AccumulatorRegister, right: TrustedImm64(Value::emptyValue().asReturnedValue())); |
| 140 | } |
| 141 | |
| 142 | Jump jumpNotEmpty() |
| 143 | { |
| 144 | return branch64(cond: NotEqual, left: AccumulatorRegister, right: TrustedImm64(Value::emptyValue().asReturnedValue())); |
| 145 | } |
| 146 | |
| 147 | void toBoolean(std::function<void(RegisterID)> continuation) |
| 148 | { |
| 149 | urshift64(src: AccumulatorRegister, imm: TrustedImm32(Value::IsIntegerConvertible_Shift), dest: ScratchRegister); |
| 150 | auto needsConversion = branch32( |
| 151 | cond: NotEqual, left: TrustedImm32(Value::IsIntegerConvertible_Value), right: ScratchRegister); |
| 152 | |
| 153 | continuation(AccumulatorRegister); |
| 154 | Jump done = jump(); |
| 155 | |
| 156 | // slow path: |
| 157 | needsConversion.link(masm: this); |
| 158 | push(src: AccumulatorRegister); |
| 159 | move(src: AccumulatorRegister, dest: registerForArg(arg: 0)); |
| 160 | callHelper(Value::toBooleanImpl); |
| 161 | and32(imm: TrustedImm32(1), src: ReturnValueRegister, dest: ScratchRegister); |
| 162 | pop(dest: AccumulatorRegister); |
| 163 | continuation(ScratchRegister); |
| 164 | |
| 165 | done.link(masm: this); |
| 166 | } |
| 167 | |
| 168 | void toNumber() |
| 169 | { |
| 170 | move(imm: TrustedImm64(Value::NumberMask), dest: ScratchRegister); |
| 171 | and64(src: AccumulatorRegister, dest: ScratchRegister); |
| 172 | move(imm: TrustedImm64(Value::NumberDiscriminator), dest: ScratchRegister2); |
| 173 | auto isNumber = branch64(cond: GreaterThanOrEqual, left: ScratchRegister, right: ScratchRegister2); |
| 174 | |
| 175 | move(src: AccumulatorRegister, dest: registerForArg(arg: 0)); |
| 176 | callHelper(toNumberHelper); |
| 177 | saveReturnValueInAccumulator(); |
| 178 | |
| 179 | isNumber.link(masm: this); |
| 180 | } |
| 181 | |
| 182 | // this converts both the lhs and the accumulator to int32 |
| 183 | void toInt32LhsAcc(Address lhs, RegisterID lhsTarget) |
| 184 | { |
| 185 | load64(address: lhs, dest: lhsTarget); |
| 186 | urshift64(src: lhsTarget, imm: TrustedImm32(Value::QuickType_Shift), dest: ScratchRegister2); |
| 187 | auto lhsIsInt = branch32(cond: Equal, left: TrustedImm32(Value::QT_Int), right: ScratchRegister2); |
| 188 | |
| 189 | const Address accumulatorStackAddress(JSStackFrameRegister, |
| 190 | offsetof(CallData, accumulator)); |
| 191 | storeAccumulator(addr: accumulatorStackAddress); |
| 192 | move(src: lhsTarget, dest: registerForArg(arg: 0)); |
| 193 | callHelper(toInt32Helper); |
| 194 | move(src: ReturnValueRegister, dest: lhsTarget); |
| 195 | loadAccumulator(addr: accumulatorStackAddress); |
| 196 | |
| 197 | lhsIsInt.link(masm: this); |
| 198 | urshift64(src: AccumulatorRegister, imm: TrustedImm32(Value::QuickType_Shift), dest: ScratchRegister2); |
| 199 | auto isInt = branch32(cond: Equal, left: TrustedImm32(Value::QT_Int), right: ScratchRegister2); |
| 200 | |
| 201 | pushAligned(reg: lhsTarget); |
| 202 | move(src: AccumulatorRegister, dest: registerForArg(arg: 0)); |
| 203 | callHelper(toInt32Helper); |
| 204 | saveReturnValueInAccumulator(); |
| 205 | popAligned(reg: lhsTarget); |
| 206 | |
| 207 | isInt.link(masm: this); |
| 208 | } |
| 209 | |
| 210 | void toInt32() |
| 211 | { |
| 212 | urshift64(src: AccumulatorRegister, imm: TrustedImm32(Value::QuickType_Shift), dest: ScratchRegister2); |
| 213 | auto isInt = branch32(cond: Equal, left: TrustedImm32(Value::QT_Int), right: ScratchRegister2); |
| 214 | |
| 215 | move(src: AccumulatorRegister, dest: registerForArg(arg: 0)); |
| 216 | callHelper(toInt32Helper); |
| 217 | saveReturnValueInAccumulator(); |
| 218 | |
| 219 | isInt.link(masm: this); |
| 220 | } |
| 221 | |
| 222 | void regToInt32(Address srcReg, RegisterID targetReg) |
| 223 | { |
| 224 | load64(address: srcReg, dest: targetReg); |
| 225 | urshift64(src: targetReg, imm: TrustedImm32(Value::QuickType_Shift), dest: ScratchRegister2); |
| 226 | auto isInt = branch32(cond: Equal, left: TrustedImm32(Value::QT_Int), right: ScratchRegister2); |
| 227 | |
| 228 | pushAligned(reg: AccumulatorRegister); |
| 229 | move(src: targetReg, dest: registerForArg(arg: 0)); |
| 230 | callHelper(toInt32Helper); |
| 231 | move(src: ReturnValueRegister, dest: targetReg); |
| 232 | popAligned(reg: AccumulatorRegister); |
| 233 | |
| 234 | isInt.link(masm: this); |
| 235 | } |
| 236 | |
| 237 | void isNullOrUndefined() |
| 238 | { |
| 239 | move(src: AccumulatorRegister, dest: ScratchRegister); |
| 240 | compare64(cond: Equal, left: ScratchRegister, right: TrustedImm32(0), dest: AccumulatorRegister); |
| 241 | Jump isUndef = branch32(cond: NotEqual, left: TrustedImm32(0), right: AccumulatorRegister); |
| 242 | |
| 243 | // not undefined |
| 244 | rshift64(imm: TrustedImm32(32), dest: ScratchRegister); |
| 245 | compare32(cond: Equal, left: ScratchRegister, right: TrustedImm32(int(QV4::Value::ValueTypeInternal::Null)), |
| 246 | dest: AccumulatorRegister); |
| 247 | |
| 248 | isUndef.link(masm: this); |
| 249 | } |
| 250 | |
| 251 | Jump isIntOrBool() |
| 252 | { |
| 253 | urshift64(src: AccumulatorRegister, imm: TrustedImm32(Value::IsIntegerOrBool_Shift), dest: ScratchRegister); |
| 254 | return branch32(cond: Equal, left: TrustedImm32(Value::IsIntegerOrBool_Value), right: ScratchRegister); |
| 255 | } |
| 256 | |
| 257 | void jumpStrictEqualStackSlotInt(int lhs, int rhs, int offset) |
| 258 | { |
| 259 | Address lhsAddr(JSStackFrameRegister, lhs * int(sizeof(Value))); |
| 260 | load64(address: lhsAddr, dest: ScratchRegister); |
| 261 | Jump isUndef = branch64(cond: Equal, left: ScratchRegister, right: TrustedImm64(0)); |
| 262 | Jump equal = branch32(cond: Equal, left: TrustedImm32(rhs), right: ScratchRegister); |
| 263 | addJumpToOffset(jump: equal, offset); |
| 264 | isUndef.link(masm: this); |
| 265 | } |
| 266 | |
| 267 | void jumpStrictNotEqualStackSlotInt(int lhs, int rhs, int offset) |
| 268 | { |
| 269 | Address lhsAddr(JSStackFrameRegister, lhs * int(sizeof(Value))); |
| 270 | load64(address: lhsAddr, dest: ScratchRegister); |
| 271 | Jump isUndef = branch64(cond: Equal, left: ScratchRegister, right: TrustedImm64(0)); |
| 272 | addJumpToOffset(jump: isUndef, offset); |
| 273 | Jump notEqual = branch32(cond: NotEqual, left: TrustedImm32(rhs), right: ScratchRegister); |
| 274 | addJumpToOffset(jump: notEqual, offset); |
| 275 | } |
| 276 | |
| 277 | void setAccumulatorTag(QV4::Value::ValueTypeInternal tag, RegisterID sourceReg = NoRegister) |
| 278 | { |
| 279 | if (sourceReg == NoRegister) |
| 280 | or64(imm: TrustedImm64(int64_t(tag) << 32), dest: AccumulatorRegister); |
| 281 | else |
| 282 | or64(imm: TrustedImm64(int64_t(tag) << 32), src: sourceReg, dest: AccumulatorRegister); |
| 283 | } |
| 284 | |
| 285 | void encodeDoubleIntoAccumulator(FPRegisterID src) |
| 286 | { |
| 287 | moveDoubleTo64(src, dest: AccumulatorRegister); |
| 288 | move(imm: TrustedImm64(Value::EncodeMask), dest: ScratchRegister); |
| 289 | xor64(src: ScratchRegister, dest: AccumulatorRegister); |
| 290 | } |
| 291 | |
| 292 | void pushValueAligned(ReturnedValue v) |
| 293 | { |
| 294 | loadValue(value: v); |
| 295 | pushAligned(reg: AccumulatorRegister); |
| 296 | } |
| 297 | |
| 298 | void popValueAligned() |
| 299 | { |
| 300 | addPtr(imm: TrustedImm32(2 * PointerSize), srcDest: StackPointerRegister); |
| 301 | } |
| 302 | |
| 303 | Jump binopBothIntPath(Address lhsAddr, std::function<Jump(void)> fastPath) |
| 304 | { |
| 305 | urshift64(src: AccumulatorRegister, imm: TrustedImm32(32), dest: ScratchRegister); |
| 306 | Jump accNotInt = branch32(cond: NotEqual, left: TrustedImm32(int(IntegerTag)), right: ScratchRegister); |
| 307 | load64(address: lhsAddr, dest: ScratchRegister); |
| 308 | urshift64(src: ScratchRegister, imm: TrustedImm32(32), dest: ScratchRegister2); |
| 309 | Jump lhsNotInt = branch32(cond: NotEqual, left: TrustedImm32(int(IntegerTag)), right: ScratchRegister2); |
| 310 | |
| 311 | // both integer |
| 312 | Jump failure = fastPath(); |
| 313 | Jump done = jump(); |
| 314 | |
| 315 | // all other cases |
| 316 | if (failure.isSet()) |
| 317 | failure.link(masm: this); |
| 318 | accNotInt.link(masm: this); |
| 319 | lhsNotInt.link(masm: this); |
| 320 | |
| 321 | return done; |
| 322 | } |
| 323 | |
| 324 | Jump unopIntPath(std::function<Jump(void)> fastPath) |
| 325 | { |
| 326 | urshift64(src: AccumulatorRegister, imm: TrustedImm32(Value::IsIntegerConvertible_Shift), dest: ScratchRegister); |
| 327 | Jump accNotIntConvertible = branch32( |
| 328 | cond: NotEqual, left: TrustedImm32(Value::IsIntegerConvertible_Value), right: ScratchRegister); |
| 329 | |
| 330 | // both integer |
| 331 | Jump failure = fastPath(); |
| 332 | Jump done = jump(); |
| 333 | |
| 334 | // all other cases |
| 335 | if (failure.isSet()) |
| 336 | failure.link(masm: this); |
| 337 | accNotIntConvertible.link(masm: this); |
| 338 | |
| 339 | return done; |
| 340 | } |
| 341 | |
| 342 | void callWithAccumulatorByValueAsFirstArgument(std::function<void()> doCall) |
| 343 | { |
| 344 | passAsArg(src: AccumulatorRegister, arg: 0); |
| 345 | doCall(); |
| 346 | } |
| 347 | }; |
| 348 | |
| 349 | typedef PlatformAssembler64 PlatformAssembler; |
| 350 | #endif |
| 351 | |
| 352 | #if QT_POINTER_SIZE == 4 || defined(ENABLE_ALL_ASSEMBLERS_FOR_REFACTORING_PURPOSES) |
| 353 | class PlatformAssembler32 : public PlatformAssemblerCommon |
| 354 | { |
| 355 | public: |
| 356 | PlatformAssembler32(const Value *constantTable) |
| 357 | : PlatformAssemblerCommon(constantTable) |
| 358 | {} |
| 359 | |
| 360 | void callRuntime(const void *funcPtr, CallResultDestination dest) |
| 361 | { |
| 362 | PlatformAssemblerCommon::callRuntime(funcPtr); |
| 363 | if (dest == CallResultDestination::InAccumulator) |
| 364 | saveReturnValueInAccumulator(); |
| 365 | else if (AccumulatorRegisterValue == ReturnValueRegisterValue) |
| 366 | loadUndefined(); |
| 367 | } |
| 368 | |
| 369 | void saveReturnValueInAccumulator() |
| 370 | { |
| 371 | move(ReturnValueRegisterValue, AccumulatorRegisterValue); |
| 372 | move(ReturnValueRegisterTag, AccumulatorRegisterTag); |
| 373 | } |
| 374 | |
| 375 | void loadUndefined() |
| 376 | { |
| 377 | move(TrustedImm32(0), AccumulatorRegisterValue); |
| 378 | move(TrustedImm32(0), AccumulatorRegisterTag); |
| 379 | } |
| 380 | |
| 381 | void copyConst(int constIndex, Address destRegAddr) |
| 382 | { |
| 383 | //### |
| 384 | if (constant(constIndex).isUndefined()) { |
| 385 | move(TrustedImm32(0), ScratchRegister); |
| 386 | store32(ScratchRegister, destRegAddr); |
| 387 | destRegAddr.offset += 4; |
| 388 | store32(ScratchRegister, destRegAddr); |
| 389 | } else { |
| 390 | Address src = loadConstAddress(constIndex); |
| 391 | loadDouble(src, FPScratchRegister); |
| 392 | storeDouble(FPScratchRegister, destRegAddr); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | void copyReg(Address src, Address dest) |
| 397 | { |
| 398 | loadDouble(src, FPScratchRegister); |
| 399 | storeDouble(FPScratchRegister, dest); |
| 400 | } |
| 401 | |
| 402 | void loadPointerFromValue(Address addr, RegisterID dest = AccumulatorRegisterValue) |
| 403 | { |
| 404 | load32(addr, dest); |
| 405 | } |
| 406 | |
| 407 | void loadAccumulator(Address src) |
| 408 | { |
| 409 | load32(src, AccumulatorRegisterValue); |
| 410 | src.offset += 4; |
| 411 | load32(src, AccumulatorRegisterTag); |
| 412 | } |
| 413 | |
| 414 | void storeAccumulator(Address addr) |
| 415 | { |
| 416 | store32(AccumulatorRegisterValue, addr); |
| 417 | addr.offset += 4; |
| 418 | store32(AccumulatorRegisterTag, addr); |
| 419 | } |
| 420 | |
| 421 | void moveReg(Address sourceRegAddress, Address destRegAddress) |
| 422 | { |
| 423 | load32(sourceRegAddress, ScratchRegister); |
| 424 | store32(ScratchRegister, destRegAddress); |
| 425 | sourceRegAddress.offset += 4; |
| 426 | destRegAddress.offset += 4; |
| 427 | load32(sourceRegAddress, ScratchRegister); |
| 428 | store32(ScratchRegister, destRegAddress); |
| 429 | } |
| 430 | |
| 431 | void loadString(int stringId) |
| 432 | { |
| 433 | load32(loadStringAddress(stringId), AccumulatorRegisterValue); |
| 434 | move(TrustedImm32(0), AccumulatorRegisterTag); |
| 435 | } |
| 436 | |
| 437 | void loadValue(ReturnedValue value) |
| 438 | { |
| 439 | move(TrustedImm32(Value::fromReturnedValue(value).value()), AccumulatorRegisterValue); |
| 440 | move(TrustedImm32(Value::fromReturnedValue(value).tag()), AccumulatorRegisterTag); |
| 441 | } |
| 442 | |
| 443 | void storeHeapObject(RegisterID source, Address addr) |
| 444 | { |
| 445 | store32(source, addr); |
| 446 | addr.offset += 4; |
| 447 | store32(TrustedImm32(0), addr); |
| 448 | } |
| 449 | |
| 450 | |
| 451 | void generateCatchTrampoline() |
| 452 | { |
| 453 | PlatformAssemblerCommon::generateCatchTrampoline([this](){loadUndefined();}); |
| 454 | } |
| 455 | |
| 456 | void toNumber() |
| 457 | { |
| 458 | and32(TrustedImm32(Value::NumberMask >> Value::Tag_Shift), |
| 459 | AccumulatorRegisterTag, ScratchRegister); |
| 460 | auto isNumber = branch32( |
| 461 | GreaterThanOrEqual, ScratchRegister, |
| 462 | TrustedImm32(Value::NumberDiscriminator >> Value::Tag_Shift)); |
| 463 | |
| 464 | |
| 465 | if (ArgInRegCount < 2) { |
| 466 | subPtr(TrustedImm32(2 * PointerSize), StackPointerRegister); // stack alignment |
| 467 | push(AccumulatorRegisterTag); |
| 468 | push(AccumulatorRegisterValue); |
| 469 | } else { |
| 470 | move(AccumulatorRegisterValue, registerForArg(0)); |
| 471 | move(AccumulatorRegisterTag, registerForArg(1)); |
| 472 | } |
| 473 | callHelper(toNumberHelper); |
| 474 | saveReturnValueInAccumulator(); |
| 475 | if (ArgInRegCount < 2) |
| 476 | addPtr(TrustedImm32(4 * PointerSize), StackPointerRegister); |
| 477 | |
| 478 | isNumber.link(this); |
| 479 | } |
| 480 | |
| 481 | // this converts both the lhs and the accumulator to int32 |
| 482 | void toInt32LhsAcc(Address lhs, RegisterID lhsTarget) |
| 483 | { |
| 484 | bool accumulatorNeedsSaving = AccumulatorRegisterValue == ReturnValueRegisterValue |
| 485 | || AccumulatorRegisterTag == ReturnValueRegisterTag; |
| 486 | lhs.offset += 4; |
| 487 | load32(lhs, lhsTarget); |
| 488 | lhs.offset -= 4; |
| 489 | auto lhsIsNotInt = branch32(NotEqual, TrustedImm32(int(IntegerTag)), lhsTarget); |
| 490 | load32(lhs, lhsTarget); |
| 491 | auto lhsIsInt = jump(); |
| 492 | |
| 493 | lhsIsNotInt.link(this); |
| 494 | |
| 495 | // Save accumulator from being garbage collected, no matter if we will reuse the register. |
| 496 | const Address accumulatorStackAddress(JSStackFrameRegister, |
| 497 | offsetof(CallData, accumulator)); |
| 498 | storeAccumulator(accumulatorStackAddress); |
| 499 | |
| 500 | if (ArgInRegCount < 2) { |
| 501 | subPtr(TrustedImm32(2 * PointerSize), StackPointerRegister); |
| 502 | push(lhsTarget); |
| 503 | load32(lhs, lhsTarget); |
| 504 | push(lhsTarget); |
| 505 | } else { |
| 506 | move(lhsTarget, registerForArg(1)); |
| 507 | load32(lhs, registerForArg(0)); |
| 508 | } |
| 509 | callHelper(toInt32Helper); |
| 510 | move(ReturnValueRegisterValue, lhsTarget); |
| 511 | if (ArgInRegCount < 2) |
| 512 | addPtr(TrustedImm32(4 * PointerSize), StackPointerRegister); |
| 513 | |
| 514 | if (accumulatorNeedsSaving) // otherwise it's still the same |
| 515 | loadAccumulator(accumulatorStackAddress); |
| 516 | |
| 517 | lhsIsInt.link(this); |
| 518 | |
| 519 | auto rhsIsInt = branch32(Equal, TrustedImm32(int(IntegerTag)), AccumulatorRegisterTag); |
| 520 | |
| 521 | pushAligned(lhsTarget); |
| 522 | if (ArgInRegCount < 2) { |
| 523 | subPtr(TrustedImm32(2 * PointerSize), StackPointerRegister); |
| 524 | push(AccumulatorRegisterTag); |
| 525 | push(AccumulatorRegisterValue); |
| 526 | } else { |
| 527 | move(AccumulatorRegisterValue, registerForArg(0)); |
| 528 | move(AccumulatorRegisterTag, registerForArg(1)); |
| 529 | } |
| 530 | callHelper(toInt32Helper); |
| 531 | saveReturnValueInAccumulator(); |
| 532 | if (ArgInRegCount < 2) |
| 533 | addPtr(TrustedImm32(4 * PointerSize), StackPointerRegister); |
| 534 | popAligned(lhsTarget); |
| 535 | |
| 536 | rhsIsInt.link(this); |
| 537 | } |
| 538 | |
| 539 | void toInt32() |
| 540 | { |
| 541 | urshift32(AccumulatorRegisterTag, TrustedImm32(Value::QuickType_Shift - 32), ScratchRegister); |
| 542 | auto isInt = branch32(Equal, TrustedImm32(Value::QT_Int), ScratchRegister); |
| 543 | |
| 544 | if (ArgInRegCount < 2) { |
| 545 | subPtr(TrustedImm32(2 * PointerSize), StackPointerRegister); // align the stack on a 16-byte boundary |
| 546 | push(AccumulatorRegisterTag); |
| 547 | push(AccumulatorRegisterValue); |
| 548 | } else { |
| 549 | move(AccumulatorRegisterValue, registerForArg(0)); |
| 550 | move(AccumulatorRegisterTag, registerForArg(1)); |
| 551 | } |
| 552 | callHelper(toInt32Helper); |
| 553 | saveReturnValueInAccumulator(); |
| 554 | if (ArgInRegCount < 2) |
| 555 | addPtr(TrustedImm32(4 * PointerSize), StackPointerRegister); |
| 556 | |
| 557 | isInt.link(this); |
| 558 | } |
| 559 | |
| 560 | void regToInt32(Address srcReg, RegisterID targetReg) |
| 561 | { |
| 562 | bool accumulatorNeedsSaving = AccumulatorRegisterValue == ReturnValueRegisterValue |
| 563 | || AccumulatorRegisterTag == ReturnValueRegisterTag; |
| 564 | if (accumulatorNeedsSaving) { |
| 565 | push(AccumulatorRegisterTag); |
| 566 | push(AccumulatorRegisterValue); |
| 567 | } |
| 568 | if (ArgInRegCount < 2) { |
| 569 | if (!accumulatorNeedsSaving) |
| 570 | subPtr(TrustedImm32(2 * PointerSize), StackPointerRegister); |
| 571 | srcReg.offset += 4; |
| 572 | load32(srcReg, targetReg); |
| 573 | push(targetReg); |
| 574 | srcReg.offset -= 4; |
| 575 | load32(srcReg, targetReg); |
| 576 | push(targetReg); |
| 577 | } else { |
| 578 | if (accumulatorNeedsSaving) |
| 579 | subPtr(TrustedImm32(2 * PointerSize), StackPointerRegister); |
| 580 | load32(srcReg, registerForArg(0)); |
| 581 | srcReg.offset += 4; |
| 582 | load32(srcReg, registerForArg(1)); |
| 583 | } |
| 584 | callHelper(toInt32Helper); |
| 585 | move(ReturnValueRegisterValue, targetReg); |
| 586 | if (accumulatorNeedsSaving) { |
| 587 | addPtr(TrustedImm32(2 * PointerSize), StackPointerRegister); |
| 588 | pop(AccumulatorRegisterValue); |
| 589 | pop(AccumulatorRegisterTag); |
| 590 | } else if (ArgInRegCount < 2) { |
| 591 | addPtr(TrustedImm32(4 * PointerSize), StackPointerRegister); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | void isNullOrUndefined() |
| 596 | { |
| 597 | Jump notUndefOrPtr = branch32(NotEqual, TrustedImm32(0), AccumulatorRegisterTag); |
| 598 | compare32(Equal, AccumulatorRegisterValue, TrustedImm32(0), AccumulatorRegisterValue); |
| 599 | auto done = jump(); |
| 600 | |
| 601 | // not undefined or managed |
| 602 | notUndefOrPtr.link(this); |
| 603 | compare32(Equal, AccumulatorRegisterTag, TrustedImm32(int(QV4::Value::ValueTypeInternal::Null)), |
| 604 | AccumulatorRegisterValue); |
| 605 | |
| 606 | done.link(this); |
| 607 | } |
| 608 | |
| 609 | Jump isIntOrBool() |
| 610 | { |
| 611 | urshift32(AccumulatorRegisterTag, TrustedImm32(Value::IsIntegerOrBool_Shift - 32), ScratchRegister); |
| 612 | return branch32(Equal, TrustedImm32(Value::IsIntegerOrBool_Value), ScratchRegister); |
| 613 | } |
| 614 | |
| 615 | void pushValue(ReturnedValue v) |
| 616 | { |
| 617 | push(TrustedImm32(v >> 32)); |
| 618 | push(TrustedImm32(v)); |
| 619 | } |
| 620 | |
| 621 | void jumpNotUndefined(int offset) |
| 622 | { |
| 623 | move(AccumulatorRegisterTag, ScratchRegister); |
| 624 | or32(AccumulatorRegisterValue, ScratchRegister); |
| 625 | auto jump = branch32(NotEqual, ScratchRegister, TrustedImm32(0)); |
| 626 | addJumpToOffset(jump, offset); |
| 627 | } |
| 628 | |
| 629 | Jump jumpEmpty() |
| 630 | { |
| 631 | return branch32(Equal, AccumulatorRegisterTag, TrustedImm32(Value::emptyValue().asReturnedValue() >> 32)); |
| 632 | } |
| 633 | |
| 634 | Jump jumpNotEmpty() |
| 635 | { |
| 636 | return branch32(NotEqual, AccumulatorRegisterTag, TrustedImm32(Value::emptyValue().asReturnedValue() >> 32)); |
| 637 | } |
| 638 | |
| 639 | void toBoolean(std::function<void(RegisterID)> continuation) |
| 640 | { |
| 641 | urshift32(AccumulatorRegisterTag, TrustedImm32(Value::IsIntegerConvertible_Shift - 32), |
| 642 | ScratchRegister); |
| 643 | auto needsConversion = branch32( |
| 644 | NotEqual, TrustedImm32(Value::IsIntegerConvertible_Value), ScratchRegister); |
| 645 | continuation(AccumulatorRegisterValue); |
| 646 | Jump done = jump(); |
| 647 | |
| 648 | // slow path: |
| 649 | needsConversion.link(this); |
| 650 | |
| 651 | bool accumulatorNeedsSaving = AccumulatorRegisterValue == ReturnValueRegisterValue |
| 652 | || AccumulatorRegisterTag == ReturnValueRegisterTag; |
| 653 | if (accumulatorNeedsSaving) { |
| 654 | push(AccumulatorRegisterTag); |
| 655 | push(AccumulatorRegisterValue); |
| 656 | } |
| 657 | |
| 658 | if (ArgInRegCount < 2) { |
| 659 | if (!accumulatorNeedsSaving) |
| 660 | subPtr(TrustedImm32(2 * PointerSize), StackPointerRegister); |
| 661 | push(AccumulatorRegisterTag); |
| 662 | push(AccumulatorRegisterValue); |
| 663 | } else { |
| 664 | if (accumulatorNeedsSaving) |
| 665 | subPtr(TrustedImm32(2 * PointerSize), StackPointerRegister); |
| 666 | move(AccumulatorRegisterValue, registerForArg(0)); |
| 667 | move(AccumulatorRegisterTag, registerForArg(1)); |
| 668 | } |
| 669 | callHelper(Value::toBooleanImpl); |
| 670 | and32(TrustedImm32(1), ReturnValueRegisterValue, ScratchRegister); |
| 671 | if (accumulatorNeedsSaving) { |
| 672 | addPtr(TrustedImm32(2 * PointerSize), StackPointerRegister); |
| 673 | pop(AccumulatorRegisterValue); |
| 674 | pop(AccumulatorRegisterTag); |
| 675 | } else if (ArgInRegCount < 2) { |
| 676 | addPtr(TrustedImm32(4 * PointerSize), StackPointerRegister); |
| 677 | } |
| 678 | continuation(ScratchRegister); |
| 679 | |
| 680 | done.link(this); |
| 681 | } |
| 682 | |
| 683 | void jumpStrictEqualStackSlotInt(int lhs, int rhs, int offset) |
| 684 | { |
| 685 | Address lhsAddr(JSStackFrameRegister, lhs * int(sizeof(Value))); |
| 686 | load32(lhsAddr, ScratchRegister); |
| 687 | Jump notEqInt = branch32(NotEqual, ScratchRegister, TrustedImm32(rhs)); |
| 688 | Jump notEqUndefVal = branch32(NotEqual, ScratchRegister, TrustedImm32(0)); |
| 689 | addJumpToOffset(notEqUndefVal, offset); |
| 690 | lhsAddr.offset += 4; |
| 691 | load32(lhsAddr, ScratchRegister); |
| 692 | Jump notEqUndefTag = branch32(NotEqual, ScratchRegister, TrustedImm32(0)); |
| 693 | addJumpToOffset(notEqUndefTag, offset); |
| 694 | notEqInt.link(this); |
| 695 | } |
| 696 | |
| 697 | void jumpStrictNotEqualStackSlotInt(int lhs, int rhs, int offset) |
| 698 | { |
| 699 | Address lhsAddr(JSStackFrameRegister, lhs * int(sizeof(Value))); |
| 700 | load32(lhsAddr, ScratchRegister); |
| 701 | Jump notEqual = branch32(NotEqual, TrustedImm32(rhs), ScratchRegister); |
| 702 | addJumpToOffset(notEqual, offset); |
| 703 | Jump notUndefValue = branch32(NotEqual, TrustedImm32(0), ScratchRegister); |
| 704 | lhsAddr.offset += 4; |
| 705 | load32(lhsAddr, ScratchRegister); |
| 706 | Jump equalUndef = branch32(Equal, TrustedImm32(0), ScratchRegister); |
| 707 | addJumpToOffset(equalUndef, offset); |
| 708 | notUndefValue.link(this); |
| 709 | } |
| 710 | |
| 711 | void setAccumulatorTag(QV4::Value::ValueTypeInternal tag, RegisterID sourceReg = NoRegister) |
| 712 | { |
| 713 | if (sourceReg != NoRegister) |
| 714 | move(sourceReg, AccumulatorRegisterValue); |
| 715 | move(TrustedImm32(int(tag)), AccumulatorRegisterTag); |
| 716 | } |
| 717 | |
| 718 | void encodeDoubleIntoAccumulator(FPRegisterID src) |
| 719 | { |
| 720 | moveDoubleToInts(src, AccumulatorRegisterValue, AccumulatorRegisterTag); |
| 721 | xor32(TrustedImm32(Value::EncodeMask >> 32), AccumulatorRegisterTag); |
| 722 | } |
| 723 | |
| 724 | void pushValueAligned(ReturnedValue v) |
| 725 | { |
| 726 | pushValue(v); |
| 727 | } |
| 728 | |
| 729 | void popValueAligned() |
| 730 | { |
| 731 | popValue(); |
| 732 | } |
| 733 | |
| 734 | Jump binopBothIntPath(Address lhsAddr, std::function<Jump(void)> fastPath) |
| 735 | { |
| 736 | Jump accNotInt = branch32(NotEqual, TrustedImm32(int(IntegerTag)), AccumulatorRegisterTag); |
| 737 | Address lhsAddrTag = lhsAddr; lhsAddrTag.offset += Value::tagOffset(); |
| 738 | load32(lhsAddrTag, ScratchRegister); |
| 739 | Jump lhsNotInt = branch32(NotEqual, TrustedImm32(int(IntegerTag)), ScratchRegister); |
| 740 | |
| 741 | // both integer |
| 742 | Address lhsAddrValue = lhsAddr; lhsAddrValue.offset += Value::valueOffset(); |
| 743 | load32(lhsAddrValue, ScratchRegister); |
| 744 | Jump failure = fastPath(); |
| 745 | Jump done = jump(); |
| 746 | |
| 747 | // all other cases |
| 748 | if (failure.isSet()) |
| 749 | failure.link(this); |
| 750 | accNotInt.link(this); |
| 751 | lhsNotInt.link(this); |
| 752 | |
| 753 | return done; |
| 754 | } |
| 755 | |
| 756 | Jump unopIntPath(std::function<Jump(void)> fastPath) |
| 757 | { |
| 758 | Jump accNotInt = branch32(NotEqual, TrustedImm32(int(IntegerTag)), AccumulatorRegisterTag); |
| 759 | |
| 760 | // both integer |
| 761 | Jump failure = fastPath(); |
| 762 | Jump done = jump(); |
| 763 | |
| 764 | // all other cases |
| 765 | if (failure.isSet()) |
| 766 | failure.link(this); |
| 767 | accNotInt.link(this); |
| 768 | |
| 769 | return done; |
| 770 | } |
| 771 | |
| 772 | void callWithAccumulatorByValueAsFirstArgument(std::function<void()> doCall) |
| 773 | { |
| 774 | if (ArgInRegCount < 2) { |
| 775 | subPtr(TrustedImm32(2 * PointerSize), StackPointerRegister); |
| 776 | push(AccumulatorRegisterTag); |
| 777 | push(AccumulatorRegisterValue); |
| 778 | } else { |
| 779 | move(AccumulatorRegisterValue, registerForArg(0)); |
| 780 | move(AccumulatorRegisterTag, registerForArg(1)); |
| 781 | } |
| 782 | doCall(); |
| 783 | if (ArgInRegCount < 2) |
| 784 | addPtr(TrustedImm32(4 * PointerSize), StackPointerRegister); |
| 785 | } |
| 786 | }; |
| 787 | |
| 788 | typedef PlatformAssembler32 PlatformAssembler; |
| 789 | #endif |
| 790 | |
| 791 | #define pasm() reinterpret_cast<PlatformAssembler *>(this->d) |
| 792 | |
| 793 | typedef PlatformAssembler::TrustedImmPtr TrustedImmPtr; |
| 794 | typedef PlatformAssembler::TrustedImm32 TrustedImm32; |
| 795 | typedef PlatformAssembler::TrustedImm64 TrustedImm64; |
| 796 | typedef PlatformAssembler::Address Address; |
| 797 | typedef PlatformAssembler::RegisterID RegisterID; |
| 798 | typedef PlatformAssembler::FPRegisterID FPRegisterID; |
| 799 | |
| 800 | static Address regAddr(int reg) |
| 801 | { |
| 802 | return Address(PlatformAssembler::JSStackFrameRegister, reg * int(sizeof(QV4::Value))); |
| 803 | } |
| 804 | |
| 805 | BaselineAssembler::BaselineAssembler(const Value *constantTable) |
| 806 | : d(new PlatformAssembler(constantTable)) |
| 807 | { |
| 808 | } |
| 809 | |
| 810 | BaselineAssembler::~BaselineAssembler() |
| 811 | { |
| 812 | delete pasm(); |
| 813 | } |
| 814 | |
| 815 | void BaselineAssembler::generatePrologue() |
| 816 | { |
| 817 | pasm()->generateFunctionEntry(); |
| 818 | } |
| 819 | |
| 820 | void BaselineAssembler::generateEpilogue() |
| 821 | { |
| 822 | pasm()->generateCatchTrampoline(); |
| 823 | } |
| 824 | |
| 825 | void BaselineAssembler::link(Function *function) |
| 826 | { |
| 827 | pasm()->link(function, jitKind: "BaselineJIT" ); |
| 828 | } |
| 829 | |
| 830 | void BaselineAssembler::addLabel(int offset) |
| 831 | { |
| 832 | pasm()->addLabelForOffset(offset); |
| 833 | } |
| 834 | |
| 835 | void BaselineAssembler::loadConst(int constIndex) |
| 836 | { |
| 837 | //### |
| 838 | if (pasm()->constant(idx: constIndex).isUndefined()) { |
| 839 | pasm()->loadUndefined(); |
| 840 | } else { |
| 841 | pasm()->loadAccumulator(pasm()->loadConstAddress(constIndex)); |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | void BaselineAssembler::copyConst(int constIndex, int destReg) |
| 846 | { |
| 847 | pasm()->copyConst(constIndex, dest: regAddr(reg: destReg)); |
| 848 | } |
| 849 | |
| 850 | void BaselineAssembler::loadReg(int reg) |
| 851 | { |
| 852 | pasm()->loadAccumulator(addr: regAddr(reg)); |
| 853 | } |
| 854 | |
| 855 | void JIT::BaselineAssembler::moveReg(int sourceReg, int destReg) |
| 856 | { |
| 857 | pasm()->moveReg(sourceRegAddress: regAddr(reg: sourceReg), destRegAddress: regAddr(reg: destReg)); |
| 858 | } |
| 859 | |
| 860 | void BaselineAssembler::storeReg(int reg) |
| 861 | { |
| 862 | pasm()->storeAccumulator(addr: regAddr(reg)); |
| 863 | } |
| 864 | |
| 865 | void BaselineAssembler::loadLocal(int index, int level) |
| 866 | { |
| 867 | Heap::CallContext ctx; |
| 868 | Q_UNUSED(ctx); |
| 869 | pasm()->loadPointerFromValue(addr: regAddr(reg: CallData::Context), dest: PlatformAssembler::ScratchRegister); |
| 870 | while (level) { |
| 871 | pasm()->loadPtr(address: Address(PlatformAssembler::ScratchRegister, ctx.outer.offset), dest: PlatformAssembler::ScratchRegister); |
| 872 | --level; |
| 873 | } |
| 874 | pasm()->loadAccumulator(addr: Address(PlatformAssembler::ScratchRegister, ctx.locals.offset + offsetof(ValueArray<0>, values) + sizeof(Value)*index)); |
| 875 | } |
| 876 | |
| 877 | void BaselineAssembler::storeLocal(int index, int level) |
| 878 | { |
| 879 | Heap::CallContext ctx; |
| 880 | Q_UNUSED(ctx); |
| 881 | pasm()->loadPtr(address: regAddr(reg: CallData::Context), dest: PlatformAssembler::ScratchRegister); |
| 882 | while (level) { |
| 883 | pasm()->loadPtr(address: Address(PlatformAssembler::ScratchRegister, ctx.outer.offset), dest: PlatformAssembler::ScratchRegister); |
| 884 | --level; |
| 885 | } |
| 886 | pasm()->storeAccumulator(addr: Address(PlatformAssembler::ScratchRegister, ctx.locals.offset + offsetof(ValueArray<0>, values) + sizeof(Value)*index)); |
| 887 | // check if we need a write barrier |
| 888 | auto skipBarrier = pasm()->branch8( |
| 889 | cond: PlatformAssembler::Equal, |
| 890 | left: PlatformAssembler::Address(PlatformAssembler::EngineRegister, |
| 891 | offsetof(EngineBase, isGCOngoing)), |
| 892 | right: TrustedImm32(0)); |
| 893 | saveAccumulatorInFrame(); |
| 894 | // if so, do a runtime call |
| 895 | pasm()->prepareCallWithArgCount(argc: 1); |
| 896 | pasm()->passAccumulatorAsArg(arg: 0); |
| 897 | pasm()->callRuntime(funcPtr: (void*)Runtime::MarkCustom::call, dest: CallResultDestination::Ignore); |
| 898 | loadAccumulatorFromFrame(); |
| 899 | skipBarrier.link(pasm()); |
| 900 | |
| 901 | } |
| 902 | |
| 903 | void BaselineAssembler::loadString(int stringId) |
| 904 | { |
| 905 | pasm()->loadString(stringId); |
| 906 | } |
| 907 | |
| 908 | void BaselineAssembler::loadValue(ReturnedValue value) |
| 909 | { |
| 910 | pasm()->loadValue(value); |
| 911 | } |
| 912 | |
| 913 | void BaselineAssembler::storeHeapObject(int reg) |
| 914 | { |
| 915 | pasm()->storeHeapObject(source: PlatformAssembler::ReturnValueRegisterValue, addr: regAddr(reg)); |
| 916 | } |
| 917 | |
| 918 | void BaselineAssembler::loadImport(int index) |
| 919 | { |
| 920 | Address addr = pasm()->loadCompilationUnitPtr(target: PlatformAssembler::ScratchRegister); |
| 921 | addr.offset = offsetof(QV4::CompilationUnitRuntimeData, imports); |
| 922 | pasm()->loadPtr(address: addr, dest: PlatformAssembler::ScratchRegister); |
| 923 | addr.offset = index * int(sizeof(QV4::Value*)); |
| 924 | pasm()->loadPtr(address: addr, dest: PlatformAssembler::ScratchRegister); |
| 925 | pasm()->loadAccumulator(addr: Address(PlatformAssembler::ScratchRegister)); |
| 926 | } |
| 927 | |
| 928 | void BaselineAssembler::toNumber() |
| 929 | { |
| 930 | pasm()->toNumber(); |
| 931 | } |
| 932 | |
| 933 | void BaselineAssembler::uminus() |
| 934 | { |
| 935 | saveAccumulatorInFrame(); |
| 936 | pasm()->prepareCallWithArgCount(argc: 1); |
| 937 | pasm()->passAccumulatorAsArg(arg: 0); |
| 938 | ASM_GENERATE_RUNTIME_CALL(UMinus, CallResultDestination::InAccumulator); |
| 939 | checkException(); |
| 940 | } |
| 941 | |
| 942 | void BaselineAssembler::ucompl() |
| 943 | { |
| 944 | pasm()->toInt32(); |
| 945 | pasm()->xor32(imm: TrustedImm32(-1), dest: PlatformAssembler::AccumulatorRegisterValue); |
| 946 | pasm()->setAccumulatorTag(tag: IntegerTag); |
| 947 | } |
| 948 | |
| 949 | static ReturnedValue incHelper(const Value v) |
| 950 | { |
| 951 | double d; |
| 952 | if (Q_LIKELY(v.isDouble())) |
| 953 | d = v.doubleValue(); |
| 954 | else |
| 955 | d = v.toNumberImpl(); |
| 956 | return Encode(d + 1.); |
| 957 | } |
| 958 | |
| 959 | void BaselineAssembler::inc() |
| 960 | { |
| 961 | auto done = pasm()->unopIntPath(fastPath: [this](){ |
| 962 | auto overflowed = pasm()->branchAdd32(cond: PlatformAssembler::Overflow, |
| 963 | src: PlatformAssembler::AccumulatorRegisterValue, |
| 964 | imm: TrustedImm32(1), |
| 965 | dest: PlatformAssembler::ScratchRegister); |
| 966 | pasm()->setAccumulatorTag(tag: IntegerTag, sourceReg: PlatformAssembler::ScratchRegister); |
| 967 | return overflowed; |
| 968 | }); |
| 969 | |
| 970 | // slow path: |
| 971 | pasm()->callWithAccumulatorByValueAsFirstArgument(doCall: [this]() { |
| 972 | pasm()->callHelper(incHelper); |
| 973 | pasm()->saveReturnValueInAccumulator(); |
| 974 | }); |
| 975 | checkException(); |
| 976 | |
| 977 | // done. |
| 978 | done.link(pasm()); |
| 979 | } |
| 980 | |
| 981 | static ReturnedValue decHelper(const Value v) |
| 982 | { |
| 983 | double d; |
| 984 | if (Q_LIKELY(v.isDouble())) |
| 985 | d = v.doubleValue(); |
| 986 | else |
| 987 | d = v.toNumberImpl(); |
| 988 | return Encode(d - 1.); |
| 989 | } |
| 990 | |
| 991 | void BaselineAssembler::dec() |
| 992 | { |
| 993 | auto done = pasm()->unopIntPath(fastPath: [this](){ |
| 994 | auto overflowed = pasm()->branchSub32(cond: PlatformAssembler::Overflow, |
| 995 | src1: PlatformAssembler::AccumulatorRegisterValue, |
| 996 | src2: TrustedImm32(1), |
| 997 | dest: PlatformAssembler::ScratchRegister); |
| 998 | pasm()->setAccumulatorTag(tag: IntegerTag, sourceReg: PlatformAssembler::ScratchRegister); |
| 999 | return overflowed; |
| 1000 | }); |
| 1001 | |
| 1002 | // slow path: |
| 1003 | pasm()->callWithAccumulatorByValueAsFirstArgument(doCall: [this]() { |
| 1004 | pasm()->callHelper(decHelper); |
| 1005 | pasm()->saveReturnValueInAccumulator(); |
| 1006 | }); |
| 1007 | checkException(); |
| 1008 | |
| 1009 | // done. |
| 1010 | done.link(pasm()); |
| 1011 | } |
| 1012 | |
| 1013 | void BaselineAssembler::unot() |
| 1014 | { |
| 1015 | pasm()->toBoolean(continuation: [this](PlatformAssembler::RegisterID resultReg){ |
| 1016 | pasm()->compare32(cond: PlatformAssembler::Equal, left: resultReg, |
| 1017 | right: TrustedImm32(0), dest: PlatformAssembler::AccumulatorRegisterValue); |
| 1018 | pasm()->setAccumulatorTag(tag: QV4::Value::ValueTypeInternal::Boolean); |
| 1019 | }); |
| 1020 | } |
| 1021 | |
| 1022 | void BaselineAssembler::add(int lhs) |
| 1023 | { |
| 1024 | auto done = pasm()->binopBothIntPath(lhsAddr: regAddr(reg: lhs), fastPath: [this](){ |
| 1025 | auto overflowed = pasm()->branchAdd32(cond: PlatformAssembler::Overflow, |
| 1026 | src: PlatformAssembler::AccumulatorRegisterValue, |
| 1027 | dest: PlatformAssembler::ScratchRegister); |
| 1028 | pasm()->setAccumulatorTag(tag: IntegerTag, |
| 1029 | sourceReg: PlatformAssembler::ScratchRegister); |
| 1030 | return overflowed; |
| 1031 | }); |
| 1032 | |
| 1033 | // slow path: |
| 1034 | saveAccumulatorInFrame(); |
| 1035 | pasm()->prepareCallWithArgCount(argc: 3); |
| 1036 | pasm()->passAccumulatorAsArg(arg: 2); |
| 1037 | pasm()->passJSSlotAsArg(reg: lhs, arg: 1); |
| 1038 | pasm()->passEngineAsArg(arg: 0); |
| 1039 | ASM_GENERATE_RUNTIME_CALL(Add, CallResultDestination::InAccumulator); |
| 1040 | checkException(); |
| 1041 | |
| 1042 | // done. |
| 1043 | done.link(pasm()); |
| 1044 | } |
| 1045 | |
| 1046 | void BaselineAssembler::bitAnd(int lhs) |
| 1047 | { |
| 1048 | PlatformAssembler::Address lhsAddr = regAddr(reg: lhs); |
| 1049 | pasm()->toInt32LhsAcc(lhs: lhsAddr, lhsTarget: PlatformAssembler::ScratchRegister); |
| 1050 | pasm()->and32(src: PlatformAssembler::ScratchRegister, dest: PlatformAssembler::AccumulatorRegisterValue); |
| 1051 | pasm()->setAccumulatorTag(tag: IntegerTag); |
| 1052 | } |
| 1053 | |
| 1054 | void BaselineAssembler::bitOr(int lhs) |
| 1055 | { |
| 1056 | PlatformAssembler::Address lhsAddr = regAddr(reg: lhs); |
| 1057 | pasm()->toInt32LhsAcc(lhs: lhsAddr, lhsTarget: PlatformAssembler::ScratchRegister); |
| 1058 | pasm()->or32(src: PlatformAssembler::ScratchRegister, dest: PlatformAssembler::AccumulatorRegisterValue); |
| 1059 | pasm()->setAccumulatorTag(tag: IntegerTag); |
| 1060 | } |
| 1061 | |
| 1062 | void BaselineAssembler::bitXor(int lhs) |
| 1063 | { |
| 1064 | PlatformAssembler::Address lhsAddr = regAddr(reg: lhs); |
| 1065 | pasm()->toInt32LhsAcc(lhs: lhsAddr, lhsTarget: PlatformAssembler::ScratchRegister); |
| 1066 | pasm()->xor32(src: PlatformAssembler::ScratchRegister, dest: PlatformAssembler::AccumulatorRegisterValue); |
| 1067 | pasm()->setAccumulatorTag(tag: IntegerTag); |
| 1068 | } |
| 1069 | |
| 1070 | void BaselineAssembler::ushr(int lhs) |
| 1071 | { |
| 1072 | PlatformAssembler::Address lhsAddr = regAddr(reg: lhs); |
| 1073 | pasm()->toInt32LhsAcc(lhs: lhsAddr, lhsTarget: PlatformAssembler::ScratchRegister); |
| 1074 | pasm()->and32(imm: TrustedImm32(0x1f), dest: PlatformAssembler::AccumulatorRegisterValue); |
| 1075 | pasm()->urshift32(shift_amount: PlatformAssembler::AccumulatorRegisterValue, dest: PlatformAssembler::ScratchRegister); |
| 1076 | pasm()->move(src: PlatformAssembler::ScratchRegister, dest: PlatformAssembler::AccumulatorRegisterValue); |
| 1077 | auto doubleEncode = pasm()->branch32(cond: PlatformAssembler::LessThan, |
| 1078 | left: PlatformAssembler::AccumulatorRegisterValue, |
| 1079 | right: TrustedImm32(0)); |
| 1080 | pasm()->setAccumulatorTag(tag: IntegerTag); |
| 1081 | auto done = pasm()->jump(); |
| 1082 | |
| 1083 | doubleEncode.link(pasm()); |
| 1084 | pasm()->convertUInt32ToDouble(src: PlatformAssembler::AccumulatorRegisterValue, |
| 1085 | dest: PlatformAssembler::FPScratchRegister, |
| 1086 | PlatformAssembler::ScratchRegister); |
| 1087 | pasm()->encodeDoubleIntoAccumulator(src: PlatformAssembler::FPScratchRegister); |
| 1088 | done.link(pasm()); |
| 1089 | } |
| 1090 | |
| 1091 | void BaselineAssembler::shr(int lhs) |
| 1092 | { |
| 1093 | PlatformAssembler::Address lhsAddr = regAddr(reg: lhs); |
| 1094 | pasm()->toInt32LhsAcc(lhs: lhsAddr, lhsTarget: PlatformAssembler::ScratchRegister); |
| 1095 | pasm()->and32(imm: TrustedImm32(0x1f), dest: PlatformAssembler::AccumulatorRegisterValue); |
| 1096 | pasm()->rshift32(shift_amount: PlatformAssembler::AccumulatorRegisterValue, dest: PlatformAssembler::ScratchRegister); |
| 1097 | pasm()->move(src: PlatformAssembler::ScratchRegister, dest: PlatformAssembler::AccumulatorRegisterValue); |
| 1098 | pasm()->setAccumulatorTag(tag: IntegerTag); |
| 1099 | } |
| 1100 | |
| 1101 | void BaselineAssembler::shl(int lhs) |
| 1102 | { |
| 1103 | PlatformAssembler::Address lhsAddr = regAddr(reg: lhs); |
| 1104 | pasm()->toInt32LhsAcc(lhs: lhsAddr, lhsTarget: PlatformAssembler::ScratchRegister); |
| 1105 | pasm()->and32(imm: TrustedImm32(0x1f), dest: PlatformAssembler::AccumulatorRegisterValue); |
| 1106 | pasm()->lshift32(shift_amount: PlatformAssembler::AccumulatorRegisterValue, dest: PlatformAssembler::ScratchRegister); |
| 1107 | pasm()->move(src: PlatformAssembler::ScratchRegister, dest: PlatformAssembler::AccumulatorRegisterValue); |
| 1108 | pasm()->setAccumulatorTag(tag: IntegerTag); |
| 1109 | } |
| 1110 | |
| 1111 | void BaselineAssembler::bitAndConst(int rhs) |
| 1112 | { |
| 1113 | pasm()->toInt32(); |
| 1114 | pasm()->and32(imm: TrustedImm32(rhs), dest: PlatformAssembler::AccumulatorRegisterValue); |
| 1115 | pasm()->setAccumulatorTag(tag: IntegerTag); |
| 1116 | } |
| 1117 | |
| 1118 | void BaselineAssembler::bitOrConst(int rhs) |
| 1119 | { |
| 1120 | pasm()->toInt32(); |
| 1121 | pasm()->or32(imm: TrustedImm32(rhs), dest: PlatformAssembler::AccumulatorRegisterValue); |
| 1122 | pasm()->setAccumulatorTag(tag: IntegerTag); |
| 1123 | } |
| 1124 | |
| 1125 | void BaselineAssembler::bitXorConst(int rhs) |
| 1126 | { |
| 1127 | pasm()->toInt32(); |
| 1128 | pasm()->xor32(imm: TrustedImm32(rhs), dest: PlatformAssembler::AccumulatorRegisterValue); |
| 1129 | pasm()->setAccumulatorTag(tag: IntegerTag); |
| 1130 | } |
| 1131 | |
| 1132 | void BaselineAssembler::ushrConst(int rhs) |
| 1133 | { |
| 1134 | rhs &= 0x1f; |
| 1135 | pasm()->toInt32(); |
| 1136 | if (rhs) { |
| 1137 | // a non zero shift will always give a number encodable as an int |
| 1138 | pasm()->urshift32(imm: TrustedImm32(rhs), dest: PlatformAssembler::AccumulatorRegisterValue); |
| 1139 | pasm()->setAccumulatorTag(tag: IntegerTag); |
| 1140 | } else { |
| 1141 | // shift with 0 can lead to a negative result |
| 1142 | auto doubleEncode = pasm()->branch32(cond: PlatformAssembler::LessThan, |
| 1143 | left: PlatformAssembler::AccumulatorRegisterValue, |
| 1144 | right: TrustedImm32(0)); |
| 1145 | pasm()->setAccumulatorTag(tag: IntegerTag); |
| 1146 | auto done = pasm()->jump(); |
| 1147 | |
| 1148 | doubleEncode.link(pasm()); |
| 1149 | pasm()->convertUInt32ToDouble(src: PlatformAssembler::AccumulatorRegisterValue, |
| 1150 | dest: PlatformAssembler::FPScratchRegister, |
| 1151 | PlatformAssembler::ScratchRegister); |
| 1152 | pasm()->encodeDoubleIntoAccumulator(src: PlatformAssembler::FPScratchRegister); |
| 1153 | done.link(pasm()); |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | void BaselineAssembler::shrConst(int rhs) |
| 1158 | { |
| 1159 | rhs &= 0x1f; |
| 1160 | pasm()->toInt32(); |
| 1161 | if (rhs) |
| 1162 | pasm()->rshift32(imm: TrustedImm32(rhs), dest: PlatformAssembler::AccumulatorRegisterValue); |
| 1163 | pasm()->setAccumulatorTag(tag: IntegerTag); |
| 1164 | } |
| 1165 | |
| 1166 | void BaselineAssembler::shlConst(int rhs) |
| 1167 | { |
| 1168 | rhs &= 0x1f; |
| 1169 | pasm()->toInt32(); |
| 1170 | if (rhs) |
| 1171 | pasm()->lshift32(imm: TrustedImm32(rhs), dest: PlatformAssembler::AccumulatorRegisterValue); |
| 1172 | pasm()->setAccumulatorTag(tag: IntegerTag); |
| 1173 | } |
| 1174 | |
| 1175 | void BaselineAssembler::mul(int lhs) |
| 1176 | { |
| 1177 | auto done = pasm()->binopBothIntPath(lhsAddr: regAddr(reg: lhs), fastPath: [this](){ |
| 1178 | auto overflowed = pasm()->branchMul32(cond: PlatformAssembler::Overflow, |
| 1179 | src: PlatformAssembler::AccumulatorRegisterValue, |
| 1180 | dest: PlatformAssembler::ScratchRegister); |
| 1181 | pasm()->setAccumulatorTag(tag: IntegerTag, |
| 1182 | sourceReg: PlatformAssembler::ScratchRegister); |
| 1183 | return overflowed; |
| 1184 | }); |
| 1185 | |
| 1186 | // slow path: |
| 1187 | saveAccumulatorInFrame(); |
| 1188 | pasm()->prepareCallWithArgCount(argc: 2); |
| 1189 | pasm()->passAccumulatorAsArg(arg: 1); |
| 1190 | pasm()->passJSSlotAsArg(reg: lhs, arg: 0); |
| 1191 | ASM_GENERATE_RUNTIME_CALL(Mul, CallResultDestination::InAccumulator); |
| 1192 | checkException(); |
| 1193 | |
| 1194 | // done. |
| 1195 | done.link(pasm()); |
| 1196 | } |
| 1197 | |
| 1198 | void BaselineAssembler::div(int lhs) |
| 1199 | { |
| 1200 | saveAccumulatorInFrame(); |
| 1201 | pasm()->prepareCallWithArgCount(argc: 2); |
| 1202 | pasm()->passAccumulatorAsArg(arg: 1); |
| 1203 | pasm()->passJSSlotAsArg(reg: lhs, arg: 0); |
| 1204 | ASM_GENERATE_RUNTIME_CALL(Div, CallResultDestination::InAccumulator); |
| 1205 | checkException(); |
| 1206 | } |
| 1207 | |
| 1208 | void BaselineAssembler::mod(int lhs) |
| 1209 | { |
| 1210 | saveAccumulatorInFrame(); |
| 1211 | pasm()->prepareCallWithArgCount(argc: 2); |
| 1212 | pasm()->passAccumulatorAsArg(arg: 1); |
| 1213 | pasm()->passJSSlotAsArg(reg: lhs, arg: 0); |
| 1214 | ASM_GENERATE_RUNTIME_CALL(Mod, CallResultDestination::InAccumulator); |
| 1215 | checkException(); |
| 1216 | } |
| 1217 | |
| 1218 | void BaselineAssembler::sub(int lhs) |
| 1219 | { |
| 1220 | auto done = pasm()->binopBothIntPath(lhsAddr: regAddr(reg: lhs), fastPath: [this](){ |
| 1221 | auto overflowed = pasm()->branchSub32(cond: PlatformAssembler::Overflow, |
| 1222 | src: PlatformAssembler::AccumulatorRegisterValue, |
| 1223 | dest: PlatformAssembler::ScratchRegister); |
| 1224 | pasm()->setAccumulatorTag(tag: IntegerTag, |
| 1225 | sourceReg: PlatformAssembler::ScratchRegister); |
| 1226 | return overflowed; |
| 1227 | }); |
| 1228 | |
| 1229 | // slow path: |
| 1230 | saveAccumulatorInFrame(); |
| 1231 | pasm()->prepareCallWithArgCount(argc: 2); |
| 1232 | pasm()->passAccumulatorAsArg(arg: 1); |
| 1233 | pasm()->passJSSlotAsArg(reg: lhs, arg: 0); |
| 1234 | ASM_GENERATE_RUNTIME_CALL(Sub, CallResultDestination::InAccumulator); |
| 1235 | checkException(); |
| 1236 | |
| 1237 | // done. |
| 1238 | done.link(pasm()); |
| 1239 | } |
| 1240 | |
| 1241 | void BaselineAssembler::cmpeqNull() |
| 1242 | { |
| 1243 | pasm()->isNullOrUndefined(); |
| 1244 | pasm()->setAccumulatorTag(tag: QV4::Value::ValueTypeInternal::Boolean); |
| 1245 | } |
| 1246 | |
| 1247 | void BaselineAssembler::cmpneNull() |
| 1248 | { |
| 1249 | pasm()->isNullOrUndefined(); |
| 1250 | pasm()->xor32(imm: TrustedImm32(1), dest: PlatformAssembler::AccumulatorRegisterValue); |
| 1251 | pasm()->setAccumulatorTag(tag: QV4::Value::ValueTypeInternal::Boolean); |
| 1252 | } |
| 1253 | |
| 1254 | void BaselineAssembler::cmpeqInt(int lhs) |
| 1255 | { |
| 1256 | auto isIntOrBool = pasm()->isIntOrBool(); |
| 1257 | saveAccumulatorInFrame(); |
| 1258 | pasm()->pushValueAligned(v: Encode(lhs)); |
| 1259 | if (PlatformAssembler::ArgInRegCount < 2) |
| 1260 | pasm()->push(src: PlatformAssembler::StackPointerRegister); |
| 1261 | else |
| 1262 | pasm()->move(src: PlatformAssembler::StackPointerRegister, pasm()->registerForArg(arg: 1)); |
| 1263 | pasm()->pushAccumulatorAsArg(arg: 0); |
| 1264 | pasm()->callRuntimeUnchecked(funcPtr: (void*)Runtime::Equal::call); |
| 1265 | pasm()->saveReturnValueInAccumulator(); |
| 1266 | if (PlatformAssembler::ArgInRegCount < 2) |
| 1267 | pasm()->addPtr(imm: TrustedImm32(2 * PlatformAssembler::PointerSize), srcDest: PlatformAssembler::StackPointerRegister); |
| 1268 | pasm()->popValueAligned(); |
| 1269 | auto done = pasm()->jump(); |
| 1270 | isIntOrBool.link(pasm()); |
| 1271 | pasm()->compare32(cond: PlatformAssembler::Equal, left: PlatformAssembler::AccumulatorRegisterValue, |
| 1272 | right: TrustedImm32(lhs), |
| 1273 | dest: PlatformAssembler::AccumulatorRegisterValue); |
| 1274 | pasm()->setAccumulatorTag(tag: QV4::Value::ValueTypeInternal::Boolean); |
| 1275 | done.link(pasm()); |
| 1276 | } |
| 1277 | |
| 1278 | void BaselineAssembler::cmpneInt(int lhs) |
| 1279 | { |
| 1280 | auto isIntOrBool = pasm()->isIntOrBool(); |
| 1281 | saveAccumulatorInFrame(); |
| 1282 | pasm()->pushValueAligned(v: Encode(lhs)); |
| 1283 | if (PlatformAssembler::ArgInRegCount < 2) |
| 1284 | pasm()->push(src: PlatformAssembler::StackPointerRegister); |
| 1285 | else |
| 1286 | pasm()->move(src: PlatformAssembler::StackPointerRegister, pasm()->registerForArg(arg: 1)); |
| 1287 | pasm()->pushAccumulatorAsArg(arg: 0); |
| 1288 | pasm()->callRuntimeUnchecked(funcPtr: (void*)Runtime::NotEqual::call); |
| 1289 | pasm()->saveReturnValueInAccumulator(); |
| 1290 | if (PlatformAssembler::ArgInRegCount < 2) |
| 1291 | pasm()->addPtr(imm: TrustedImm32(2 * PlatformAssembler::PointerSize), srcDest: PlatformAssembler::StackPointerRegister); |
| 1292 | pasm()->popValueAligned(); |
| 1293 | auto done = pasm()->jump(); |
| 1294 | isIntOrBool.link(pasm()); |
| 1295 | pasm()->compare32(cond: PlatformAssembler::NotEqual, left: PlatformAssembler::AccumulatorRegisterValue, |
| 1296 | right: TrustedImm32(lhs), |
| 1297 | dest: PlatformAssembler::AccumulatorRegisterValue); |
| 1298 | pasm()->setAccumulatorTag(tag: QV4::Value::ValueTypeInternal::Boolean); |
| 1299 | done.link(pasm()); |
| 1300 | } |
| 1301 | |
| 1302 | void BaselineAssembler::cmp(int cond, CmpFunc function, int lhs) |
| 1303 | { |
| 1304 | auto c = static_cast<PlatformAssembler::RelationalCondition>(cond); |
| 1305 | auto done = pasm()->binopBothIntPath(lhsAddr: regAddr(reg: lhs), fastPath: [this, c](){ |
| 1306 | pasm()->compare32(cond: c, left: PlatformAssembler::ScratchRegister, |
| 1307 | right: PlatformAssembler::AccumulatorRegisterValue, |
| 1308 | dest: PlatformAssembler::AccumulatorRegisterValue); |
| 1309 | return PlatformAssembler::Jump(); |
| 1310 | }); |
| 1311 | |
| 1312 | // slow path: |
| 1313 | saveAccumulatorInFrame(); |
| 1314 | pasm()->prepareCallWithArgCount(argc: 2); |
| 1315 | pasm()->passAccumulatorAsArg(arg: 1); |
| 1316 | pasm()->passJSSlotAsArg(reg: lhs, arg: 0); |
| 1317 | |
| 1318 | callRuntime(funcPtr: reinterpret_cast<void*>(function), dest: CallResultDestination::InAccumulator); |
| 1319 | checkException(); |
| 1320 | |
| 1321 | // done. |
| 1322 | done.link(pasm()); |
| 1323 | pasm()->setAccumulatorTag(tag: QV4::Value::ValueTypeInternal::Boolean); |
| 1324 | } |
| 1325 | |
| 1326 | void BaselineAssembler::cmpeq(int lhs) |
| 1327 | { |
| 1328 | cmp(cond: PlatformAssembler::Equal, function: &Runtime::CompareEqual::call, lhs); |
| 1329 | } |
| 1330 | |
| 1331 | void BaselineAssembler::cmpne(int lhs) |
| 1332 | { |
| 1333 | cmp(cond: PlatformAssembler::NotEqual, function: &Runtime::CompareNotEqual::call, lhs); |
| 1334 | } |
| 1335 | |
| 1336 | void BaselineAssembler::cmpgt(int lhs) |
| 1337 | { |
| 1338 | cmp(cond: PlatformAssembler::GreaterThan, function: &Runtime::CompareGreaterThan::call, lhs); |
| 1339 | } |
| 1340 | |
| 1341 | void BaselineAssembler::cmpge(int lhs) |
| 1342 | { |
| 1343 | cmp(cond: PlatformAssembler::GreaterThanOrEqual, function: &Runtime::CompareGreaterEqual::call, lhs); |
| 1344 | } |
| 1345 | |
| 1346 | void BaselineAssembler::cmplt(int lhs) |
| 1347 | { |
| 1348 | cmp(cond: PlatformAssembler::LessThan, function: &Runtime::CompareLessThan::call, lhs); |
| 1349 | } |
| 1350 | |
| 1351 | void BaselineAssembler::cmple(int lhs) |
| 1352 | { |
| 1353 | cmp(cond: PlatformAssembler::LessThanOrEqual, function: &Runtime::CompareLessEqual::call, lhs); |
| 1354 | } |
| 1355 | |
| 1356 | void BaselineAssembler::cmpStrictEqual(int lhs) |
| 1357 | { |
| 1358 | cmp(cond: PlatformAssembler::Equal, function: &Runtime::CompareStrictEqual::call, lhs); |
| 1359 | } |
| 1360 | |
| 1361 | void BaselineAssembler::cmpStrictNotEqual(int lhs) |
| 1362 | { |
| 1363 | cmp(cond: PlatformAssembler::NotEqual, function: &Runtime::CompareStrictNotEqual::call, lhs); |
| 1364 | } |
| 1365 | |
| 1366 | int BaselineAssembler::jump(int offset) |
| 1367 | { |
| 1368 | pasm()->addJumpToOffset(pasm()->jump(), offset); |
| 1369 | return offset; |
| 1370 | } |
| 1371 | |
| 1372 | int BaselineAssembler::jumpTrue(int offset) |
| 1373 | { |
| 1374 | pasm()->toBoolean(continuation: [this, offset](PlatformAssembler::RegisterID resultReg) { |
| 1375 | auto jump = pasm()->branch32(cond: PlatformAssembler::NotEqual, left: TrustedImm32(0), right: resultReg); |
| 1376 | pasm()->addJumpToOffset(jump, offset); |
| 1377 | }); |
| 1378 | return offset; |
| 1379 | } |
| 1380 | |
| 1381 | int BaselineAssembler::jumpFalse(int offset) |
| 1382 | { |
| 1383 | pasm()->toBoolean(continuation: [this, offset](PlatformAssembler::RegisterID resultReg) { |
| 1384 | auto jump = pasm()->branch32(cond: PlatformAssembler::Equal, left: TrustedImm32(0), right: resultReg); |
| 1385 | pasm()->addJumpToOffset(jump, offset); |
| 1386 | }); |
| 1387 | return offset; |
| 1388 | } |
| 1389 | |
| 1390 | int BaselineAssembler::jumpNoException(int offset) |
| 1391 | { |
| 1392 | auto jump = pasm()->branch32( |
| 1393 | cond: PlatformAssembler::Equal, |
| 1394 | left: PlatformAssembler::Address(PlatformAssembler::EngineRegister, |
| 1395 | offsetof(EngineBase, hasException)), |
| 1396 | right: TrustedImm32(0)); |
| 1397 | pasm()->addJumpToOffset(jump, offset); |
| 1398 | return offset; |
| 1399 | } |
| 1400 | |
| 1401 | int BaselineAssembler::jumpNotUndefined(int offset) |
| 1402 | { |
| 1403 | pasm()->jumpNotUndefined(offset); |
| 1404 | return offset; |
| 1405 | } |
| 1406 | |
| 1407 | int BaselineAssembler::jumpEqNull(int offset) |
| 1408 | { |
| 1409 | saveAccumulatorInFrame(); |
| 1410 | cmpeqNull(); |
| 1411 | |
| 1412 | pasm()->toBoolean(continuation: [this, offset](PlatformAssembler::RegisterID resultReg) { |
| 1413 | auto isFalse = pasm()->branch32(cond: PlatformAssembler::Equal, left: TrustedImm32(0), right: resultReg); |
| 1414 | loadValue(value: Encode::undefined()); |
| 1415 | pasm()->addJumpToOffset(pasm()->jump(), offset); |
| 1416 | isFalse.link(pasm()); |
| 1417 | loadAccumulatorFromFrame(); |
| 1418 | }); |
| 1419 | |
| 1420 | return offset; |
| 1421 | } |
| 1422 | |
| 1423 | |
| 1424 | void BaselineAssembler::prepareCallWithArgCount(int argc) |
| 1425 | { |
| 1426 | pasm()->prepareCallWithArgCount(argc); |
| 1427 | } |
| 1428 | |
| 1429 | void BaselineAssembler::storeInstructionPointer(int instructionOffset) |
| 1430 | { |
| 1431 | pasm()->storeInstructionPointer(instructionOffset); |
| 1432 | } |
| 1433 | |
| 1434 | void BaselineAssembler::passAccumulatorAsArg(int arg) |
| 1435 | { |
| 1436 | pasm()->passAccumulatorAsArg(arg); |
| 1437 | } |
| 1438 | |
| 1439 | void BaselineAssembler::passFunctionAsArg(int arg) |
| 1440 | { |
| 1441 | pasm()->passFunctionAsArg(arg); |
| 1442 | } |
| 1443 | |
| 1444 | void BaselineAssembler::passEngineAsArg(int arg) |
| 1445 | { |
| 1446 | pasm()->passEngineAsArg(arg); |
| 1447 | } |
| 1448 | |
| 1449 | void BaselineAssembler::passJSSlotAsArg(int reg, int arg) |
| 1450 | { |
| 1451 | pasm()->passJSSlotAsArg(reg, arg); |
| 1452 | } |
| 1453 | |
| 1454 | void BaselineAssembler::passCppFrameAsArg(int arg) |
| 1455 | { |
| 1456 | pasm()->passCppFrameAsArg(arg); |
| 1457 | } |
| 1458 | |
| 1459 | void BaselineAssembler::passInt32AsArg(int value, int arg) |
| 1460 | { |
| 1461 | pasm()->passInt32AsArg(value, arg); |
| 1462 | } |
| 1463 | |
| 1464 | void BaselineAssembler::passPointerAsArg(void *ptr, int arg) |
| 1465 | { |
| 1466 | pasm()->passPointerAsArg(ptr, arg); |
| 1467 | } |
| 1468 | |
| 1469 | void BaselineAssembler::callRuntime(const void *funcPtr, CallResultDestination dest) |
| 1470 | { |
| 1471 | pasm()->callRuntime(funcPtr, dest); |
| 1472 | } |
| 1473 | |
| 1474 | void BaselineAssembler::saveAccumulatorInFrame() |
| 1475 | { |
| 1476 | pasm()->storeAccumulator(addr: PlatformAssembler::Address(PlatformAssembler::JSStackFrameRegister, |
| 1477 | offsetof(CallData, accumulator))); |
| 1478 | } |
| 1479 | |
| 1480 | void BaselineAssembler::loadAccumulatorFromFrame() |
| 1481 | { |
| 1482 | pasm()->loadAccumulator(addr: PlatformAssembler::Address(PlatformAssembler::JSStackFrameRegister, |
| 1483 | offsetof(CallData, accumulator))); |
| 1484 | } |
| 1485 | |
| 1486 | static ReturnedValue TheJitIs__Tail_Calling__ToTheRuntimeSoTheJitFrameIsMissing(JSTypesStackFrame *frame, ExecutionEngine *engine) |
| 1487 | { |
| 1488 | return Runtime::TailCall::call(frame, engine); |
| 1489 | } |
| 1490 | |
| 1491 | void BaselineAssembler::jsTailCall(int func, int thisObject, int argc, int argv) |
| 1492 | { |
| 1493 | Address tos = pasm()->jsAlloca(slotCount: 4); |
| 1494 | |
| 1495 | int32_t argcOffset = tos.offset + int32_t(sizeof(Value)) * Runtime::StackOffsets::tailCall_argc; |
| 1496 | int32_t argvOffset = tos.offset + int32_t(sizeof(Value)) * Runtime::StackOffsets::tailCall_argv; |
| 1497 | int32_t thisOffset = tos.offset + int32_t(sizeof(Value)) * Runtime::StackOffsets::tailCall_thisObject; |
| 1498 | int32_t funcOffset = tos.offset + int32_t(sizeof(Value)) * Runtime::StackOffsets::tailCall_function; |
| 1499 | |
| 1500 | pasm()->storeInt32AsValue(srcInt: argc, destAddr: Address(tos.base, argcOffset)); |
| 1501 | pasm()->storeInt32AsValue(srcInt: argv, destAddr: Address(tos.base, argvOffset)); |
| 1502 | pasm()->moveReg(sourceRegAddress: regAddr(reg: thisObject), destRegAddress: Address(tos.base, thisOffset)); |
| 1503 | pasm()->moveReg(sourceRegAddress: regAddr(reg: func), destRegAddress: Address(tos.base, funcOffset)); |
| 1504 | pasm()->tailCallRuntime( |
| 1505 | funcPtr: reinterpret_cast<void *>(TheJitIs__Tail_Calling__ToTheRuntimeSoTheJitFrameIsMissing), |
| 1506 | functionName: "TheJitIs__Tail_Calling__ToTheRuntimeSoTheJitFrameIsMissing" ); |
| 1507 | } |
| 1508 | |
| 1509 | void BaselineAssembler::checkException() |
| 1510 | { |
| 1511 | pasm()->checkException(); |
| 1512 | } |
| 1513 | |
| 1514 | void BaselineAssembler::gotoCatchException() |
| 1515 | { |
| 1516 | pasm()->addCatchyJump(pasm()->jump()); |
| 1517 | } |
| 1518 | |
| 1519 | void BaselineAssembler::getException() |
| 1520 | { |
| 1521 | Q_STATIC_ASSERT(sizeof(QV4::EngineBase::hasException) == 1); |
| 1522 | |
| 1523 | Address hasExceptionAddr(PlatformAssembler::EngineRegister, |
| 1524 | offsetof(EngineBase, hasException)); |
| 1525 | PlatformAssembler::Jump nope = pasm()->branch8(cond: PlatformAssembler::Equal, |
| 1526 | left: hasExceptionAddr, |
| 1527 | right: TrustedImm32(0)); |
| 1528 | pasm()->loadPtr(address: Address(PlatformAssembler::EngineRegister, |
| 1529 | offsetof(EngineBase, exceptionValue)), |
| 1530 | dest: PlatformAssembler::ScratchRegister); |
| 1531 | pasm()->loadAccumulator(addr: Address(PlatformAssembler::ScratchRegister)); |
| 1532 | pasm()->store8(imm: TrustedImm32(0), address: hasExceptionAddr); |
| 1533 | auto done = pasm()->jump(); |
| 1534 | nope.link(pasm()); |
| 1535 | pasm()->loadValue(value: Value::emptyValue().asReturnedValue()); |
| 1536 | |
| 1537 | done.link(pasm()); |
| 1538 | } |
| 1539 | |
| 1540 | void BaselineAssembler::setException() |
| 1541 | { |
| 1542 | auto noException = pasm()->jumpEmpty(); |
| 1543 | Address addr(PlatformAssembler::EngineRegister, offsetof(EngineBase, exceptionValue)); |
| 1544 | pasm()->loadPtr(address: addr, dest: PlatformAssembler::ScratchRegister); |
| 1545 | pasm()->storeAccumulator(addr: Address(PlatformAssembler::ScratchRegister)); |
| 1546 | addr.offset = offsetof(EngineBase, hasException); |
| 1547 | Q_STATIC_ASSERT(sizeof(QV4::EngineBase::hasException) == 1); |
| 1548 | pasm()->store8(imm: TrustedImm32(1), address: addr); |
| 1549 | noException.link(pasm()); |
| 1550 | } |
| 1551 | |
| 1552 | int BaselineAssembler::setUnwindHandler(int offset) |
| 1553 | { |
| 1554 | auto l = pasm()->storePtrWithPatch(initialValue: TrustedImmPtr(nullptr), pasm()->exceptionHandlerAddress()); |
| 1555 | pasm()->addEHTarget(label: l, offset); |
| 1556 | return offset; |
| 1557 | } |
| 1558 | |
| 1559 | |
| 1560 | void BaselineAssembler::clearUnwindHandler() |
| 1561 | { |
| 1562 | pasm()->storePtr(imm: TrustedImmPtr(nullptr), pasm()->exceptionHandlerAddress()); |
| 1563 | } |
| 1564 | |
| 1565 | void JIT::BaselineAssembler::unwindDispatch() |
| 1566 | { |
| 1567 | checkException(); |
| 1568 | pasm()->load32(address: Address(PlatformAssembler::CppStackFrameRegister, offsetof(JSTypesStackFrame, unwindLevel)), dest: PlatformAssembler::ScratchRegister); |
| 1569 | auto noUnwind = pasm()->branch32(cond: PlatformAssembler::Equal, left: PlatformAssembler::ScratchRegister, right: TrustedImm32(0)); |
| 1570 | pasm()->sub32(imm: TrustedImm32(1), dest: PlatformAssembler::ScratchRegister); |
| 1571 | pasm()->store32(src: PlatformAssembler::ScratchRegister, address: Address(PlatformAssembler::CppStackFrameRegister, offsetof(JSTypesStackFrame, unwindLevel))); |
| 1572 | auto jump = pasm()->branch32(cond: PlatformAssembler::Equal, left: PlatformAssembler::ScratchRegister, right: TrustedImm32(0)); |
| 1573 | gotoCatchException(); |
| 1574 | jump.link(pasm()); |
| 1575 | |
| 1576 | pasm()->loadPtr(address: Address(PlatformAssembler::CppStackFrameRegister, offsetof(JSTypesStackFrame, unwindLabel)), dest: PlatformAssembler::ScratchRegister); |
| 1577 | pasm()->jump(target: PlatformAssembler::ScratchRegister); |
| 1578 | |
| 1579 | noUnwind.link(pasm()); |
| 1580 | } |
| 1581 | |
| 1582 | int JIT::BaselineAssembler::unwindToLabel(int level, int offset) |
| 1583 | { |
| 1584 | auto l = pasm()->storePtrWithPatch(initialValue: TrustedImmPtr(nullptr), address: Address(PlatformAssembler::CppStackFrameRegister, offsetof(JSTypesStackFrame, unwindLabel))); |
| 1585 | pasm()->addEHTarget(label: l, offset); |
| 1586 | pasm()->store32(imm: TrustedImm32(level), address: Address(PlatformAssembler::CppStackFrameRegister, offsetof(JSTypesStackFrame, unwindLevel))); |
| 1587 | gotoCatchException(); |
| 1588 | return offset; |
| 1589 | } |
| 1590 | |
| 1591 | void BaselineAssembler::pushCatchContext(int index, int name) |
| 1592 | { |
| 1593 | pasm()->prepareCallWithArgCount(argc: 3); |
| 1594 | pasm()->passInt32AsArg(value: name, arg: 2); |
| 1595 | pasm()->passInt32AsArg(value: index, arg: 1); |
| 1596 | pasm()->passEngineAsArg(arg: 0); |
| 1597 | ASM_GENERATE_RUNTIME_CALL(PushCatchContext, CallResultDestination::Ignore); |
| 1598 | } |
| 1599 | |
| 1600 | void BaselineAssembler::popContext() |
| 1601 | { |
| 1602 | Heap::CallContext ctx; |
| 1603 | Q_UNUSED(ctx); |
| 1604 | pasm()->loadPointerFromValue(addr: regAddr(reg: CallData::Context), dest: PlatformAssembler::ScratchRegister); |
| 1605 | pasm()->loadPtr(address: Address(PlatformAssembler::ScratchRegister, ctx.outer.offset), dest: PlatformAssembler::ScratchRegister); |
| 1606 | pasm()->storeHeapObject(source: PlatformAssembler::ScratchRegister, addr: regAddr(reg: CallData::Context)); |
| 1607 | } |
| 1608 | |
| 1609 | void BaselineAssembler::deadTemporalZoneCheck(int offsetForSavedIP, int variableName) |
| 1610 | { |
| 1611 | auto valueIsAliveJump = pasm()->jumpNotEmpty(); |
| 1612 | storeInstructionPointer(instructionOffset: offsetForSavedIP); |
| 1613 | prepareCallWithArgCount(argc: 2); |
| 1614 | passInt32AsArg(value: variableName, arg: 1); |
| 1615 | passEngineAsArg(arg: 0); |
| 1616 | ASM_GENERATE_RUNTIME_CALL(ThrowReferenceError, CallResultDestination::Ignore); |
| 1617 | gotoCatchException(); |
| 1618 | valueIsAliveJump.link(pasm()); |
| 1619 | } |
| 1620 | |
| 1621 | void BaselineAssembler::ret() |
| 1622 | { |
| 1623 | pasm()->generateFunctionExit(); |
| 1624 | } |
| 1625 | |
| 1626 | } // JIT namespace |
| 1627 | } // QV4 namepsace |
| 1628 | |
| 1629 | QT_END_NAMESPACE |
| 1630 | |
| 1631 | #endif // QT_CONFIG(qml_jit) |
| 1632 | |