oliver@apple.com | ea77149 | 2013-07-25 03:58:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "FTLThunks.h" |
| 28 | |
| 29 | #if ENABLE(FTL_JIT) |
| 30 | |
fpizlo@apple.com | ea92c20 | 2013-10-10 04:24:57 +0000 | [diff] [blame] | 31 | #include "AssemblyHelpers.h" |
sbarati@apple.com | a582c18 | 2015-12-05 00:04:01 +0000 | [diff] [blame] | 32 | #include "DFGOSRExitCompilerCommon.h" |
fpizlo@apple.com | cd8eb2c | 2013-09-20 18:42:41 +0000 | [diff] [blame] | 33 | #include "FPRInfo.h" |
oliver@apple.com | ea77149 | 2013-07-25 03:58:38 +0000 | [diff] [blame] | 34 | #include "FTLOSRExitCompiler.h" |
fpizlo@apple.com | 6e69796 | 2015-10-12 17:56:26 +0000 | [diff] [blame] | 35 | #include "FTLOperations.h" |
fpizlo@apple.com | ea92c20 | 2013-10-10 04:24:57 +0000 | [diff] [blame] | 36 | #include "FTLSaveRestore.h" |
fpizlo@apple.com | cd8eb2c | 2013-09-20 18:42:41 +0000 | [diff] [blame] | 37 | #include "GPRInfo.h" |
oliver@apple.com | ea77149 | 2013-07-25 03:58:38 +0000 | [diff] [blame] | 38 | #include "LinkBuffer.h" |
oliver@apple.com | ea77149 | 2013-07-25 03:58:38 +0000 | [diff] [blame] | 39 | |
| 40 | namespace JSC { namespace FTL { |
| 41 | |
| 42 | using namespace DFG; |
| 43 | |
sbarati@apple.com | a582c18 | 2015-12-05 00:04:01 +0000 | [diff] [blame] | 44 | enum class FrameAndStackAdjustmentRequirement { |
| 45 | Needed, |
| 46 | NotNeeded |
| 47 | }; |
| 48 | |
fpizlo@apple.com | 6e69796 | 2015-10-12 17:56:26 +0000 | [diff] [blame] | 49 | static MacroAssemblerCodeRef genericGenerationThunkGenerator( |
sbarati@apple.com | a582c18 | 2015-12-05 00:04:01 +0000 | [diff] [blame] | 50 | VM* vm, FunctionPtr generationFunction, const char* name, unsigned extraPopsToRestore, FrameAndStackAdjustmentRequirement frameAndStackAdjustmentRequirement) |
fpizlo@apple.com | 50cd41c | 2013-10-10 22:57:10 +0000 | [diff] [blame] | 51 | { |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 52 | AssemblyHelpers jit(vm, 0); |
sbarati@apple.com | a582c18 | 2015-12-05 00:04:01 +0000 | [diff] [blame] | 53 | |
| 54 | if (frameAndStackAdjustmentRequirement == FrameAndStackAdjustmentRequirement::Needed) { |
| 55 | // This needs to happen before we use the scratch buffer because this function also uses the scratch buffer. |
| 56 | adjustFrameAndStackInOSRExitCompilerThunk<FTL::JITCode>(jit, vm, JITCode::FTLJIT); |
| 57 | } |
fpizlo@apple.com | 50cd41c | 2013-10-10 22:57:10 +0000 | [diff] [blame] | 58 | |
fpizlo@apple.com | 6e69796 | 2015-10-12 17:56:26 +0000 | [diff] [blame] | 59 | // Note that the "return address" will be the ID that we pass to the generation function. |
fpizlo@apple.com | 50cd41c | 2013-10-10 22:57:10 +0000 | [diff] [blame] | 60 | |
fpizlo@apple.com | 720e05a | 2014-02-17 18:59:13 +0000 | [diff] [blame] | 61 | ptrdiff_t stackMisalignment = MacroAssembler::pushToSaveByteOffset(); |
| 62 | |
fpizlo@apple.com | 50cd41c | 2013-10-10 22:57:10 +0000 | [diff] [blame] | 63 | // Pretend that we're a C call frame. |
fpizlo@apple.com | 720e05a | 2014-02-17 18:59:13 +0000 | [diff] [blame] | 64 | jit.pushToSave(MacroAssembler::framePointerRegister); |
fpizlo@apple.com | 50cd41c | 2013-10-10 22:57:10 +0000 | [diff] [blame] | 65 | jit.move(MacroAssembler::stackPointerRegister, MacroAssembler::framePointerRegister); |
fpizlo@apple.com | 720e05a | 2014-02-17 18:59:13 +0000 | [diff] [blame] | 66 | stackMisalignment += MacroAssembler::pushToSaveByteOffset(); |
| 67 | |
| 68 | // Now create ourselves enough stack space to give saveAllRegisters() a scratch slot. |
| 69 | unsigned numberOfRequiredPops = 0; |
| 70 | do { |
| 71 | jit.pushToSave(GPRInfo::regT0); |
| 72 | stackMisalignment += MacroAssembler::pushToSaveByteOffset(); |
| 73 | numberOfRequiredPops++; |
| 74 | } while (stackMisalignment % stackAlignmentBytes()); |
fpizlo@apple.com | 50cd41c | 2013-10-10 22:57:10 +0000 | [diff] [blame] | 75 | |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 76 | ScratchBuffer* scratchBuffer = vm->scratchBufferForSize(requiredScratchMemorySizeInBytes()); |
fpizlo@apple.com | 50cd41c | 2013-10-10 22:57:10 +0000 | [diff] [blame] | 77 | char* buffer = static_cast<char*>(scratchBuffer->dataBuffer()); |
| 78 | |
| 79 | saveAllRegisters(jit, buffer); |
| 80 | |
| 81 | // Tell GC mark phase how much of the scratch buffer is active during call. |
msaboff@apple.com | 0208546 | 2015-09-10 17:47:16 +0000 | [diff] [blame] | 82 | jit.move(MacroAssembler::TrustedImmPtr(scratchBuffer->activeLengthPtr()), GPRInfo::nonArgGPR0); |
| 83 | jit.storePtr(MacroAssembler::TrustedImmPtr(requiredScratchMemorySizeInBytes()), GPRInfo::nonArgGPR0); |
fpizlo@apple.com | 50cd41c | 2013-10-10 22:57:10 +0000 | [diff] [blame] | 84 | |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 85 | jit.loadPtr(GPRInfo::callFrameRegister, GPRInfo::argumentGPR0); |
fpizlo@apple.com | 41bed90 | 2014-02-25 22:18:21 +0000 | [diff] [blame] | 86 | jit.peek( |
| 87 | GPRInfo::argumentGPR1, |
| 88 | (stackMisalignment - MacroAssembler::pushToSaveByteOffset()) / sizeof(void*)); |
fpizlo@apple.com | 50cd41c | 2013-10-10 22:57:10 +0000 | [diff] [blame] | 89 | MacroAssembler::Call functionCall = jit.call(); |
| 90 | |
| 91 | // At this point we want to make a tail call to what was returned to us in the |
| 92 | // returnValueGPR. But at the same time as we do this, we must restore all registers. |
| 93 | // The way we will accomplish this is by arranging to have the tail call target in the |
| 94 | // return address "slot" (be it a register or the stack). |
| 95 | |
| 96 | jit.move(GPRInfo::returnValueGPR, GPRInfo::regT0); |
| 97 | |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 98 | // Make sure we tell the GC that we're not using the scratch buffer anymore. |
| 99 | jit.move(MacroAssembler::TrustedImmPtr(scratchBuffer->activeLengthPtr()), GPRInfo::regT1); |
| 100 | jit.storePtr(MacroAssembler::TrustedImmPtr(0), GPRInfo::regT1); |
| 101 | |
fpizlo@apple.com | 50cd41c | 2013-10-10 22:57:10 +0000 | [diff] [blame] | 102 | // Prepare for tail call. |
fpizlo@apple.com | 720e05a | 2014-02-17 18:59:13 +0000 | [diff] [blame] | 103 | while (numberOfRequiredPops--) |
| 104 | jit.popToRestore(GPRInfo::regT1); |
| 105 | jit.popToRestore(MacroAssembler::framePointerRegister); |
fpizlo@apple.com | 6e69796 | 2015-10-12 17:56:26 +0000 | [diff] [blame] | 106 | |
| 107 | // When we came in here, there was an additional thing pushed to the stack. Some clients want it |
| 108 | // popped before proceeding. |
| 109 | while (extraPopsToRestore--) |
| 110 | jit.popToRestore(GPRInfo::regT1); |
| 111 | |
| 112 | // Put the return address wherever the return instruction wants it. On all platforms, this |
| 113 | // ensures that the return address is out of the way of register restoration. |
fpizlo@apple.com | 50cd41c | 2013-10-10 22:57:10 +0000 | [diff] [blame] | 114 | jit.restoreReturnAddressBeforeReturn(GPRInfo::regT0); |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 115 | |
fpizlo@apple.com | 50cd41c | 2013-10-10 22:57:10 +0000 | [diff] [blame] | 116 | restoreAllRegisters(jit, buffer); |
| 117 | |
| 118 | jit.ret(); |
| 119 | |
benjamin@webkit.org | f766fd9 | 2014-07-08 04:23:30 +0000 | [diff] [blame] | 120 | LinkBuffer patchBuffer(*vm, jit, GLOBAL_THUNK_ID); |
fpizlo@apple.com | 6e69796 | 2015-10-12 17:56:26 +0000 | [diff] [blame] | 121 | patchBuffer.link(functionCall, generationFunction); |
| 122 | return FINALIZE_CODE(patchBuffer, ("%s", name)); |
| 123 | } |
| 124 | |
| 125 | MacroAssemblerCodeRef osrExitGenerationThunkGenerator(VM* vm) |
| 126 | { |
| 127 | unsigned extraPopsToRestore = 0; |
| 128 | return genericGenerationThunkGenerator( |
sbarati@apple.com | a582c18 | 2015-12-05 00:04:01 +0000 | [diff] [blame] | 129 | vm, compileFTLOSRExit, "FTL OSR exit generation thunk", extraPopsToRestore, FrameAndStackAdjustmentRequirement::Needed); |
fpizlo@apple.com | 6e69796 | 2015-10-12 17:56:26 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | MacroAssemblerCodeRef lazySlowPathGenerationThunkGenerator(VM* vm) |
| 133 | { |
| 134 | unsigned extraPopsToRestore = 1; |
| 135 | return genericGenerationThunkGenerator( |
sbarati@apple.com | a582c18 | 2015-12-05 00:04:01 +0000 | [diff] [blame] | 136 | vm, compileFTLLazySlowPath, "FTL lazy slow path generation thunk", extraPopsToRestore, FrameAndStackAdjustmentRequirement::NotNeeded); |
fpizlo@apple.com | 50cd41c | 2013-10-10 22:57:10 +0000 | [diff] [blame] | 137 | } |
| 138 | |
fpizlo@apple.com | a502abe | 2014-03-30 18:43:41 +0000 | [diff] [blame] | 139 | static void registerClobberCheck(AssemblyHelpers& jit, RegisterSet dontClobber) |
| 140 | { |
| 141 | if (!Options::clobberAllRegsInFTLICSlowPath()) |
| 142 | return; |
| 143 | |
| 144 | RegisterSet clobber = RegisterSet::allRegisters(); |
| 145 | clobber.exclude(RegisterSet::reservedHardwareRegisters()); |
| 146 | clobber.exclude(RegisterSet::stackRegisters()); |
| 147 | clobber.exclude(RegisterSet::calleeSaveRegisters()); |
| 148 | clobber.exclude(dontClobber); |
| 149 | |
| 150 | GPRReg someGPR; |
| 151 | for (Reg reg = Reg::first(); reg <= Reg::last(); reg = reg.next()) { |
| 152 | if (!clobber.get(reg) || !reg.isGPR()) |
| 153 | continue; |
| 154 | |
| 155 | jit.move(AssemblyHelpers::TrustedImm32(0x1337beef), reg.gpr()); |
| 156 | someGPR = reg.gpr(); |
| 157 | } |
| 158 | |
| 159 | for (Reg reg = Reg::first(); reg <= Reg::last(); reg = reg.next()) { |
| 160 | if (!clobber.get(reg) || !reg.isFPR()) |
| 161 | continue; |
| 162 | |
| 163 | jit.move64ToDouble(someGPR, reg.fpr()); |
| 164 | } |
| 165 | } |
| 166 | |
fpizlo@apple.com | 2e7ada0 | 2013-10-23 18:22:09 +0000 | [diff] [blame] | 167 | MacroAssemblerCodeRef slowPathCallThunkGenerator(VM& vm, const SlowPathCallKey& key) |
| 168 | { |
| 169 | AssemblyHelpers jit(&vm, 0); |
| 170 | |
| 171 | // We want to save the given registers at the given offset, then we want to save the |
| 172 | // old return address somewhere past that offset, and then finally we want to make the |
| 173 | // call. |
| 174 | |
| 175 | size_t currentOffset = key.offset() + sizeof(void*); |
| 176 | |
| 177 | #if CPU(X86) || CPU(X86_64) |
| 178 | currentOffset += sizeof(void*); |
| 179 | #endif |
| 180 | |
| 181 | for (MacroAssembler::RegisterID reg = MacroAssembler::firstRegister(); reg <= MacroAssembler::lastRegister(); reg = static_cast<MacroAssembler::RegisterID>(reg + 1)) { |
| 182 | if (!key.usedRegisters().get(reg)) |
| 183 | continue; |
| 184 | jit.storePtr(reg, AssemblyHelpers::Address(MacroAssembler::stackPointerRegister, currentOffset)); |
| 185 | currentOffset += sizeof(void*); |
| 186 | } |
| 187 | |
| 188 | for (MacroAssembler::FPRegisterID reg = MacroAssembler::firstFPRegister(); reg <= MacroAssembler::lastFPRegister(); reg = static_cast<MacroAssembler::FPRegisterID>(reg + 1)) { |
| 189 | if (!key.usedRegisters().get(reg)) |
| 190 | continue; |
| 191 | jit.storeDouble(reg, AssemblyHelpers::Address(MacroAssembler::stackPointerRegister, currentOffset)); |
| 192 | currentOffset += sizeof(double); |
| 193 | } |
| 194 | |
| 195 | jit.preserveReturnAddressAfterCall(GPRInfo::nonArgGPR0); |
| 196 | jit.storePtr(GPRInfo::nonArgGPR0, AssemblyHelpers::Address(MacroAssembler::stackPointerRegister, key.offset())); |
| 197 | |
fpizlo@apple.com | a502abe | 2014-03-30 18:43:41 +0000 | [diff] [blame] | 198 | registerClobberCheck(jit, key.argumentRegisters()); |
| 199 | |
fpizlo@apple.com | b426f86 | 2014-02-10 02:51:13 +0000 | [diff] [blame] | 200 | AssemblyHelpers::Call call = jit.call(); |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 201 | |
fpizlo@apple.com | 2e7ada0 | 2013-10-23 18:22:09 +0000 | [diff] [blame] | 202 | jit.loadPtr(AssemblyHelpers::Address(MacroAssembler::stackPointerRegister, key.offset()), GPRInfo::nonPreservedNonReturnGPR); |
| 203 | jit.restoreReturnAddressBeforeReturn(GPRInfo::nonPreservedNonReturnGPR); |
| 204 | |
| 205 | for (MacroAssembler::FPRegisterID reg = MacroAssembler::lastFPRegister(); ; reg = static_cast<MacroAssembler::FPRegisterID>(reg - 1)) { |
| 206 | if (key.usedRegisters().get(reg)) { |
| 207 | currentOffset -= sizeof(double); |
| 208 | jit.loadDouble(AssemblyHelpers::Address(MacroAssembler::stackPointerRegister, currentOffset), reg); |
| 209 | } |
| 210 | if (reg == MacroAssembler::firstFPRegister()) |
| 211 | break; |
| 212 | } |
| 213 | |
| 214 | for (MacroAssembler::RegisterID reg = MacroAssembler::lastRegister(); ; reg = static_cast<MacroAssembler::RegisterID>(reg - 1)) { |
| 215 | if (key.usedRegisters().get(reg)) { |
| 216 | currentOffset -= sizeof(void*); |
| 217 | jit.loadPtr(AssemblyHelpers::Address(MacroAssembler::stackPointerRegister, currentOffset), reg); |
| 218 | } |
| 219 | if (reg == MacroAssembler::firstRegister()) |
| 220 | break; |
| 221 | } |
| 222 | |
| 223 | jit.ret(); |
| 224 | |
benjamin@webkit.org | f766fd9 | 2014-07-08 04:23:30 +0000 | [diff] [blame] | 225 | LinkBuffer patchBuffer(vm, jit, GLOBAL_THUNK_ID); |
fpizlo@apple.com | 2e7ada0 | 2013-10-23 18:22:09 +0000 | [diff] [blame] | 226 | patchBuffer.link(call, FunctionPtr(key.callTarget())); |
| 227 | return FINALIZE_CODE(patchBuffer, ("FTL slow path call thunk for %s", toCString(key).data())); |
| 228 | } |
| 229 | |
oliver@apple.com | ea77149 | 2013-07-25 03:58:38 +0000 | [diff] [blame] | 230 | } } // namespace JSC::FTL |
| 231 | |
| 232 | #endif // ENABLE(FTL_JIT) |
| 233 | |