blob: cfbc75b7c8e99f79504b058f340bb9bd6befc333 [file] [log] [blame]
oliver@apple.comea771492013-07-25 03:58:38 +00001/*
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.comea92c202013-10-10 04:24:57 +000031#include "AssemblyHelpers.h"
sbarati@apple.coma582c182015-12-05 00:04:01 +000032#include "DFGOSRExitCompilerCommon.h"
fpizlo@apple.comcd8eb2c2013-09-20 18:42:41 +000033#include "FPRInfo.h"
oliver@apple.comea771492013-07-25 03:58:38 +000034#include "FTLOSRExitCompiler.h"
fpizlo@apple.com6e697962015-10-12 17:56:26 +000035#include "FTLOperations.h"
fpizlo@apple.comea92c202013-10-10 04:24:57 +000036#include "FTLSaveRestore.h"
fpizlo@apple.comcd8eb2c2013-09-20 18:42:41 +000037#include "GPRInfo.h"
oliver@apple.comea771492013-07-25 03:58:38 +000038#include "LinkBuffer.h"
oliver@apple.comea771492013-07-25 03:58:38 +000039
40namespace JSC { namespace FTL {
41
42using namespace DFG;
43
sbarati@apple.coma582c182015-12-05 00:04:01 +000044enum class FrameAndStackAdjustmentRequirement {
45 Needed,
46 NotNeeded
47};
48
fpizlo@apple.com6e697962015-10-12 17:56:26 +000049static MacroAssemblerCodeRef genericGenerationThunkGenerator(
sbarati@apple.coma582c182015-12-05 00:04:01 +000050 VM* vm, FunctionPtr generationFunction, const char* name, unsigned extraPopsToRestore, FrameAndStackAdjustmentRequirement frameAndStackAdjustmentRequirement)
fpizlo@apple.com50cd41c2013-10-10 22:57:10 +000051{
msaboff@apple.com95894332014-01-29 19:18:54 +000052 AssemblyHelpers jit(vm, 0);
sbarati@apple.coma582c182015-12-05 00:04:01 +000053
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.com50cd41c2013-10-10 22:57:10 +000058
fpizlo@apple.com6e697962015-10-12 17:56:26 +000059 // Note that the "return address" will be the ID that we pass to the generation function.
fpizlo@apple.com50cd41c2013-10-10 22:57:10 +000060
fpizlo@apple.com720e05a2014-02-17 18:59:13 +000061 ptrdiff_t stackMisalignment = MacroAssembler::pushToSaveByteOffset();
62
fpizlo@apple.com50cd41c2013-10-10 22:57:10 +000063 // Pretend that we're a C call frame.
fpizlo@apple.com720e05a2014-02-17 18:59:13 +000064 jit.pushToSave(MacroAssembler::framePointerRegister);
fpizlo@apple.com50cd41c2013-10-10 22:57:10 +000065 jit.move(MacroAssembler::stackPointerRegister, MacroAssembler::framePointerRegister);
fpizlo@apple.com720e05a2014-02-17 18:59:13 +000066 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.com50cd41c2013-10-10 22:57:10 +000075
msaboff@apple.com95894332014-01-29 19:18:54 +000076 ScratchBuffer* scratchBuffer = vm->scratchBufferForSize(requiredScratchMemorySizeInBytes());
fpizlo@apple.com50cd41c2013-10-10 22:57:10 +000077 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.com02085462015-09-10 17:47:16 +000082 jit.move(MacroAssembler::TrustedImmPtr(scratchBuffer->activeLengthPtr()), GPRInfo::nonArgGPR0);
83 jit.storePtr(MacroAssembler::TrustedImmPtr(requiredScratchMemorySizeInBytes()), GPRInfo::nonArgGPR0);
fpizlo@apple.com50cd41c2013-10-10 22:57:10 +000084
msaboff@apple.com95894332014-01-29 19:18:54 +000085 jit.loadPtr(GPRInfo::callFrameRegister, GPRInfo::argumentGPR0);
fpizlo@apple.com41bed902014-02-25 22:18:21 +000086 jit.peek(
87 GPRInfo::argumentGPR1,
88 (stackMisalignment - MacroAssembler::pushToSaveByteOffset()) / sizeof(void*));
fpizlo@apple.com50cd41c2013-10-10 22:57:10 +000089 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.com95894332014-01-29 19:18:54 +000098 // 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.com50cd41c2013-10-10 22:57:10 +0000102 // Prepare for tail call.
fpizlo@apple.com720e05a2014-02-17 18:59:13 +0000103 while (numberOfRequiredPops--)
104 jit.popToRestore(GPRInfo::regT1);
105 jit.popToRestore(MacroAssembler::framePointerRegister);
fpizlo@apple.com6e697962015-10-12 17:56:26 +0000106
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.com50cd41c2013-10-10 22:57:10 +0000114 jit.restoreReturnAddressBeforeReturn(GPRInfo::regT0);
msaboff@apple.com95894332014-01-29 19:18:54 +0000115
fpizlo@apple.com50cd41c2013-10-10 22:57:10 +0000116 restoreAllRegisters(jit, buffer);
117
118 jit.ret();
119
benjamin@webkit.orgf766fd92014-07-08 04:23:30 +0000120 LinkBuffer patchBuffer(*vm, jit, GLOBAL_THUNK_ID);
fpizlo@apple.com6e697962015-10-12 17:56:26 +0000121 patchBuffer.link(functionCall, generationFunction);
122 return FINALIZE_CODE(patchBuffer, ("%s", name));
123}
124
125MacroAssemblerCodeRef osrExitGenerationThunkGenerator(VM* vm)
126{
127 unsigned extraPopsToRestore = 0;
128 return genericGenerationThunkGenerator(
sbarati@apple.coma582c182015-12-05 00:04:01 +0000129 vm, compileFTLOSRExit, "FTL OSR exit generation thunk", extraPopsToRestore, FrameAndStackAdjustmentRequirement::Needed);
fpizlo@apple.com6e697962015-10-12 17:56:26 +0000130}
131
132MacroAssemblerCodeRef lazySlowPathGenerationThunkGenerator(VM* vm)
133{
134 unsigned extraPopsToRestore = 1;
135 return genericGenerationThunkGenerator(
sbarati@apple.coma582c182015-12-05 00:04:01 +0000136 vm, compileFTLLazySlowPath, "FTL lazy slow path generation thunk", extraPopsToRestore, FrameAndStackAdjustmentRequirement::NotNeeded);
fpizlo@apple.com50cd41c2013-10-10 22:57:10 +0000137}
138
fpizlo@apple.coma502abe2014-03-30 18:43:41 +0000139static 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.com2e7ada02013-10-23 18:22:09 +0000167MacroAssemblerCodeRef 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.coma502abe2014-03-30 18:43:41 +0000198 registerClobberCheck(jit, key.argumentRegisters());
199
fpizlo@apple.comb426f862014-02-10 02:51:13 +0000200 AssemblyHelpers::Call call = jit.call();
msaboff@apple.com95894332014-01-29 19:18:54 +0000201
fpizlo@apple.com2e7ada02013-10-23 18:22:09 +0000202 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.orgf766fd92014-07-08 04:23:30 +0000225 LinkBuffer patchBuffer(vm, jit, GLOBAL_THUNK_ID);
fpizlo@apple.com2e7ada02013-10-23 18:22:09 +0000226 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.comea771492013-07-25 03:58:38 +0000230} } // namespace JSC::FTL
231
232#endif // ENABLE(FTL_JIT)
233