blob: 597ab9725f7db20ed934078cb7daf8880ca7b48d [file] [log] [blame]
oliver@apple.comea771492013-07-25 03:58:38 +00001/*
msaboff@apple.com95894332014-01-29 19:18:54 +00002 * Copyright (C) 2013, 2014 Apple Inc. All rights reserved.
oliver@apple.comea771492013-07-25 03:58:38 +00003 *
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 "FTLOSRExitCompiler.h"
28
29#if ENABLE(FTL_JIT)
30
31#include "DFGOSRExitCompilerCommon.h"
32#include "DFGOSRExitPreparation.h"
oliver@apple.comea771492013-07-25 03:58:38 +000033#include "FTLExitArgumentForOperand.h"
34#include "FTLJITCode.h"
35#include "FTLOSRExit.h"
fpizlo@apple.comfc70ba62014-09-26 03:59:33 +000036#include "FTLOperations.h"
fpizlo@apple.com40750682014-03-13 22:18:50 +000037#include "FTLState.h"
fpizlo@apple.comea92c202013-10-10 04:24:57 +000038#include "FTLSaveRestore.h"
fpizlo@apple.comb426f862014-02-10 02:51:13 +000039#include "LinkBuffer.h"
msaboff@apple.com95894332014-01-29 19:18:54 +000040#include "MaxFrameExtentForSlowPathCall.h"
commit-queue@webkit.orga936a262014-01-14 15:20:19 +000041#include "OperandsInlines.h"
fpizlo@apple.comfb7eff22014-02-11 01:45:50 +000042#include "JSCInlines.h"
msaboff@apple.com95894332014-01-29 19:18:54 +000043#include "RegisterPreservationWrapperGenerator.h"
oliver@apple.comea771492013-07-25 03:58:38 +000044#include "RepatchBuffer.h"
45
46namespace JSC { namespace FTL {
47
48using namespace DFG;
49
fpizlo@apple.comfc70ba62014-09-26 03:59:33 +000050static void compileRecovery(
51 CCallHelpers& jit, const ExitValue& value, StackMaps::Record* record, StackMaps& stackmaps,
52 char* registerScratch,
53 const HashMap<ExitTimeObjectMaterialization*, EncodedJSValue*>& materializationToPointer)
54{
55 switch (value.kind()) {
56 case ExitValueDead:
57 jit.move(MacroAssembler::TrustedImm64(JSValue::encode(jsUndefined())), GPRInfo::regT0);
58 break;
59
60 case ExitValueConstant:
61 jit.move(MacroAssembler::TrustedImm64(JSValue::encode(value.constant())), GPRInfo::regT0);
62 break;
63
64 case ExitValueArgument:
65 record->locations[value.exitArgument().argument()].restoreInto(
66 jit, stackmaps, registerScratch, GPRInfo::regT0);
67 break;
68
69 case ExitValueInJSStack:
70 case ExitValueInJSStackAsInt32:
71 case ExitValueInJSStackAsInt52:
72 case ExitValueInJSStackAsDouble:
73 jit.load64(AssemblyHelpers::addressFor(value.virtualRegister()), GPRInfo::regT0);
74 break;
75
76 case ExitValueArgumentsObjectThatWasNotCreated:
77 jit.move(MacroAssembler::TrustedImm64(JSValue::encode(JSValue())), GPRInfo::regT0);
78 break;
79
80 case ExitValueRecovery:
81 record->locations[value.rightRecoveryArgument()].restoreInto(
82 jit, stackmaps, registerScratch, GPRInfo::regT1);
83 record->locations[value.leftRecoveryArgument()].restoreInto(
84 jit, stackmaps, registerScratch, GPRInfo::regT0);
85 switch (value.recoveryOpcode()) {
86 case AddRecovery:
87 switch (value.recoveryFormat()) {
88 case ValueFormatInt32:
89 jit.add32(GPRInfo::regT1, GPRInfo::regT0);
90 break;
91 case ValueFormatInt52:
92 jit.add64(GPRInfo::regT1, GPRInfo::regT0);
93 break;
94 default:
95 RELEASE_ASSERT_NOT_REACHED();
96 break;
97 }
98 break;
99 case SubRecovery:
100 switch (value.recoveryFormat()) {
101 case ValueFormatInt32:
102 jit.sub32(GPRInfo::regT1, GPRInfo::regT0);
103 break;
104 case ValueFormatInt52:
105 jit.sub64(GPRInfo::regT1, GPRInfo::regT0);
106 break;
107 default:
108 RELEASE_ASSERT_NOT_REACHED();
109 break;
110 }
111 break;
112 default:
113 RELEASE_ASSERT_NOT_REACHED();
114 break;
115 }
116 break;
117
118 case ExitValueMaterializeNewObject:
119 jit.loadPtr(materializationToPointer.get(value.objectMaterialization()), GPRInfo::regT0);
120 break;
121
122 default:
123 RELEASE_ASSERT_NOT_REACHED();
124 break;
125 }
126
127 reboxAccordingToFormat(
128 value.valueFormat(), jit, GPRInfo::regT0, GPRInfo::regT1, GPRInfo::regT2);
129}
130
fpizlo@apple.com6bf11982013-11-03 18:24:20 +0000131static void compileStub(
fpizlo@apple.comea92c202013-10-10 04:24:57 +0000132 unsigned exitID, JITCode* jitCode, OSRExit& exit, VM* vm, CodeBlock* codeBlock)
133{
llango.u-szeged@partner.samsung.comc10c9cb2014-06-27 07:28:49 +0000134 StackMaps::Record* record = nullptr;
fpizlo@apple.comea92c202013-10-10 04:24:57 +0000135
136 for (unsigned i = jitCode->stackmaps.records.size(); i--;) {
137 record = &jitCode->stackmaps.records[i];
138 if (record->patchpointID == exit.m_stackmapID)
139 break;
140 }
141
142 RELEASE_ASSERT(record->patchpointID == exit.m_stackmapID);
143
msaboff@apple.comcb9adb02013-11-07 23:45:56 +0000144 // This code requires framePointerRegister is the same as callFrameRegister
145 static_assert(MacroAssembler::framePointerRegister == GPRInfo::callFrameRegister, "MacroAssembler::framePointerRegister and GPRInfo::callFrameRegister must be the same");
146
fpizlo@apple.comea92c202013-10-10 04:24:57 +0000147 CCallHelpers jit(vm, codeBlock);
148
fpizlo@apple.comfc70ba62014-09-26 03:59:33 +0000149 // We need scratch space to save all registers, to build up the JS stack, to deal with unwind
150 // fixup, pointers to all of the objects we materialize, and the elements inside those objects
151 // that we materialize.
152
153 // Figure out how much space we need for those object allocations.
154 unsigned numMaterializations = 0;
155 size_t maxMaterializationNumArguments = 0;
156 for (ExitTimeObjectMaterialization* materialization : exit.m_materializations) {
157 numMaterializations++;
158
159 maxMaterializationNumArguments = std::max(
160 maxMaterializationNumArguments,
161 materialization->properties().size());
162 }
163
164 ScratchBuffer* scratchBuffer = vm->scratchBufferForSize(
165 sizeof(EncodedJSValue) * (
166 exit.m_values.size() + numMaterializations + maxMaterializationNumArguments) +
167 requiredScratchMemorySizeInBytes() +
168 jitCode->unwindInfo.m_registers.size() * sizeof(uint64_t));
fpizlo@apple.comea92c202013-10-10 04:24:57 +0000169 EncodedJSValue* scratch = scratchBuffer ? static_cast<EncodedJSValue*>(scratchBuffer->dataBuffer()) : 0;
fpizlo@apple.comfc70ba62014-09-26 03:59:33 +0000170 EncodedJSValue* materializationPointers = scratch + exit.m_values.size();
171 EncodedJSValue* materializationArguments = materializationPointers + numMaterializations;
172 char* registerScratch = bitwise_cast<char*>(materializationArguments + maxMaterializationNumArguments);
msaboff@apple.com95894332014-01-29 19:18:54 +0000173 uint64_t* unwindScratch = bitwise_cast<uint64_t*>(registerScratch + requiredScratchMemorySizeInBytes());
fpizlo@apple.comea92c202013-10-10 04:24:57 +0000174
fpizlo@apple.comfc70ba62014-09-26 03:59:33 +0000175 HashMap<ExitTimeObjectMaterialization*, EncodedJSValue*> materializationToPointer;
176 unsigned materializationCount = 0;
177 for (ExitTimeObjectMaterialization* materialization : exit.m_materializations) {
178 materializationToPointer.add(
179 materialization, materializationPointers + materializationCount++);
180 }
181
fpizlo@apple.com720e05a2014-02-17 18:59:13 +0000182 // Note that we come in here, the stack used to be as LLVM left it except that someone called pushToSave().
183 // We don't care about the value they saved. But, we do appreciate the fact that they did it, because we use
184 // that slot for saveAllRegisters().
185
fpizlo@apple.comea92c202013-10-10 04:24:57 +0000186 saveAllRegisters(jit, registerScratch);
187
fpizlo@apple.com68952692014-03-23 18:56:56 +0000188 // Bring the stack back into a sane form and assert that it's sane.
fpizlo@apple.com720e05a2014-02-17 18:59:13 +0000189 jit.popToRestore(GPRInfo::regT0);
fpizlo@apple.com68952692014-03-23 18:56:56 +0000190 jit.checkStackPointerAlignment();
fpizlo@apple.come5192ec2013-10-12 01:33:16 +0000191
msaboff@apple.com95894332014-01-29 19:18:54 +0000192 if (vm->m_perBytecodeProfiler && codeBlock->jitCode()->dfgCommon()->compilation) {
193 Profiler::Database& database = *vm->m_perBytecodeProfiler;
194 Profiler::Compilation* compilation = codeBlock->jitCode()->dfgCommon()->compilation.get();
195
196 Profiler::OSRExit* profilerExit = compilation->addOSRExit(
197 exitID, Profiler::OriginStack(database, codeBlock, exit.m_codeOrigin),
fpizlo@apple.comb41e6822014-07-25 20:55:17 +0000198 exit.m_kind, exit.m_kind == UncountableInvalidation);
msaboff@apple.com95894332014-01-29 19:18:54 +0000199 jit.add64(CCallHelpers::TrustedImm32(1), CCallHelpers::AbsoluteAddress(profilerExit->counterAddress()));
200 }
201
fpizlo@apple.come5192ec2013-10-12 01:33:16 +0000202 // The remaining code assumes that SP/FP are in the same state that they were in the FTL's
203 // call frame.
fpizlo@apple.comea92c202013-10-10 04:24:57 +0000204
205 // Get the call frame and tag thingies.
msaboff@apple.comcb9adb02013-11-07 23:45:56 +0000206 // Restore the exiting function's callFrame value into a regT4
fpizlo@apple.comea92c202013-10-10 04:24:57 +0000207 jit.move(MacroAssembler::TrustedImm64(TagTypeNumber), GPRInfo::tagTypeNumberRegister);
208 jit.move(MacroAssembler::TrustedImm64(TagMask), GPRInfo::tagMaskRegister);
209
210 // Do some value profiling.
211 if (exit.m_profileValueFormat != InvalidValueFormat) {
msaboff@apple.com95894332014-01-29 19:18:54 +0000212 record->locations[0].restoreInto(jit, jitCode->stackmaps, registerScratch, GPRInfo::regT0);
fpizlo@apple.comea92c202013-10-10 04:24:57 +0000213 reboxAccordingToFormat(
214 exit.m_profileValueFormat, jit, GPRInfo::regT0, GPRInfo::regT1, GPRInfo::regT2);
215
216 if (exit.m_kind == BadCache || exit.m_kind == BadIndexingType) {
217 CodeOrigin codeOrigin = exit.m_codeOriginForExitProfile;
218 if (ArrayProfile* arrayProfile = jit.baselineCodeBlockFor(codeOrigin)->getArrayProfile(codeOrigin.bytecodeIndex)) {
mhahnenberg@apple.comb6f85192014-02-27 01:27:18 +0000219 jit.load32(MacroAssembler::Address(GPRInfo::regT0, JSCell::structureIDOffset()), GPRInfo::regT1);
220 jit.store32(GPRInfo::regT1, arrayProfile->addressOfLastSeenStructureID());
221 jit.load8(MacroAssembler::Address(GPRInfo::regT0, JSCell::indexingTypeOffset()), GPRInfo::regT1);
fpizlo@apple.comea92c202013-10-10 04:24:57 +0000222 jit.move(MacroAssembler::TrustedImm32(1), GPRInfo::regT2);
223 jit.lshift32(GPRInfo::regT1, GPRInfo::regT2);
224 jit.or32(GPRInfo::regT2, MacroAssembler::AbsoluteAddress(arrayProfile->addressOfArrayModes()));
225 }
226 }
227
228 if (!!exit.m_valueProfile)
229 jit.store64(GPRInfo::regT0, exit.m_valueProfile.getSpecFailBucket(0));
230 }
fpizlo@apple.comfc70ba62014-09-26 03:59:33 +0000231
232 // Materialize all objects. Don't materialize an object until all of the objects it needs
233 // have been materialized.
234 HashSet<ExitTimeObjectMaterialization*> toMaterialize;
235 for (ExitTimeObjectMaterialization* materialization : exit.m_materializations)
236 toMaterialize.add(materialization);
237
238 while (!toMaterialize.isEmpty()) {
commit-queue@webkit.org877bfc32015-03-10 21:54:21 +0000239 unsigned previousToMaterializeSize = toMaterialize.size();
fpizlo@apple.comfc70ba62014-09-26 03:59:33 +0000240
241 Vector<ExitTimeObjectMaterialization*> worklist;
242 worklist.appendRange(toMaterialize.begin(), toMaterialize.end());
243 for (ExitTimeObjectMaterialization* materialization : worklist) {
244 // Check if we can do anything about this right now.
245 bool allGood = true;
246 for (ExitPropertyValue value : materialization->properties()) {
247 if (!value.value().isObjectMaterialization())
248 continue;
249 if (toMaterialize.contains(value.value().objectMaterialization())) {
250 // Gotta skip this one, since one of its fields points to a materialization
251 // that hasn't been materialized.
252 allGood = false;
253 break;
254 }
255 }
256 if (!allGood)
257 continue;
258
259 // All systems go for materializing the object. First we recover the values of all of
260 // its fields and then we call a function to actually allocate the beast.
261 for (unsigned propertyIndex = materialization->properties().size(); propertyIndex--;) {
262 const ExitValue& value = materialization->properties()[propertyIndex].value();
263 compileRecovery(
264 jit, value, record, jitCode->stackmaps, registerScratch,
265 materializationToPointer);
266 jit.storePtr(GPRInfo::regT0, materializationArguments + propertyIndex);
267 }
268
269 // This call assumes that we don't pass arguments on the stack.
270 jit.setupArgumentsWithExecState(
271 CCallHelpers::TrustedImmPtr(materialization),
272 CCallHelpers::TrustedImmPtr(materializationArguments));
273 jit.move(CCallHelpers::TrustedImmPtr(bitwise_cast<void*>(operationMaterializeObjectInOSR)), GPRInfo::nonArgGPR0);
274 jit.call(GPRInfo::nonArgGPR0);
275 jit.storePtr(GPRInfo::returnValueGPR, materializationToPointer.get(materialization));
276
277 // Let everyone know that we're done.
278 toMaterialize.remove(materialization);
279 }
280
281 // We expect progress! This ensures that we crash rather than looping infinitely if there
282 // is something broken about this fixpoint. Or, this could happen if we ever violate the
283 // "materializations form a DAG" rule.
284 RELEASE_ASSERT(toMaterialize.size() < previousToMaterializeSize);
285 }
fpizlo@apple.comea92c202013-10-10 04:24:57 +0000286
287 // Save all state from wherever the exit data tells us it was, into the appropriate place in
fpizlo@apple.com2a69df42014-09-20 18:45:54 +0000288 // the scratch buffer. This also does the reboxing.
fpizlo@apple.comea92c202013-10-10 04:24:57 +0000289
290 for (unsigned index = exit.m_values.size(); index--;) {
fpizlo@apple.comfc70ba62014-09-26 03:59:33 +0000291 compileRecovery(
292 jit, exit.m_values[index], record, jitCode->stackmaps, registerScratch,
293 materializationToPointer);
fpizlo@apple.comea92c202013-10-10 04:24:57 +0000294 jit.store64(GPRInfo::regT0, scratch + index);
295 }
296
msaboff@apple.com95894332014-01-29 19:18:54 +0000297 // Henceforth we make it look like the exiting function was called through a register
298 // preservation wrapper. This implies that FP must be nudged down by a certain amount. Then
299 // we restore the various things according to either exit.m_values or by copying from the
300 // old frame, and finally we save the various callee-save registers into where the
301 // restoration thunk would restore them from.
302
303 ptrdiff_t offset = registerPreservationOffset();
304 RegisterSet toSave = registersToPreserve();
305
306 // Before we start messing with the frame, we need to set aside any registers that the
307 // FTL code was preserving.
308 for (unsigned i = jitCode->unwindInfo.m_registers.size(); i--;) {
309 RegisterAtOffset entry = jitCode->unwindInfo.m_registers[i];
310 jit.load64(
311 MacroAssembler::Address(MacroAssembler::framePointerRegister, entry.offset()),
312 GPRInfo::regT0);
313 jit.store64(GPRInfo::regT0, unwindScratch + i);
314 }
315
316 jit.load32(CCallHelpers::payloadFor(JSStack::ArgumentCount), GPRInfo::regT2);
317
318 // Let's say that the FTL function had failed its arity check. In that case, the stack will
319 // contain some extra stuff.
320 //
321 // First we compute the padded stack space:
322 //
323 // paddedStackSpace = roundUp(codeBlock->numParameters - regT2 + 1)
324 //
325 // The stack will have regT2 + CallFrameHeaderSize stuff, but above it there will be
326 // paddedStackSpace gunk used by the arity check fail restoration thunk. When that happens
327 // we want to make the stack look like this, from higher addresses down:
328 //
329 // - register preservation return PC
330 // - preserved registers
331 // - arity check fail return PC
332 // - argument padding
333 // - actual arguments
334 // - call frame header
335 //
336 // So that the actual call frame header appears to return to the arity check fail return
337 // PC, and that then returns to the register preservation thunk. The arity check thunk that
338 // we return to will have the padding size encoded into it. It will then know to return
339 // into the register preservation thunk, which uses the argument count to figure out where
340 // registers are preserved.
341
342 // This code assumes that we're dealing with FunctionCode.
343 RELEASE_ASSERT(codeBlock->codeType() == FunctionCode);
344
345 jit.add32(
346 MacroAssembler::TrustedImm32(-codeBlock->numParameters()), GPRInfo::regT2,
347 GPRInfo::regT3);
fpizlo@apple.com0cec6dea2014-03-04 21:27:37 +0000348 MacroAssembler::Jump arityIntact = jit.branch32(
349 MacroAssembler::GreaterThanOrEqual, GPRInfo::regT3, MacroAssembler::TrustedImm32(0));
msaboff@apple.com95894332014-01-29 19:18:54 +0000350 jit.neg32(GPRInfo::regT3);
351 jit.add32(MacroAssembler::TrustedImm32(1 + stackAlignmentRegisters() - 1), GPRInfo::regT3);
352 jit.and32(MacroAssembler::TrustedImm32(-stackAlignmentRegisters()), GPRInfo::regT3);
353 jit.add32(GPRInfo::regT3, GPRInfo::regT2);
354 arityIntact.link(&jit);
355
356 // First set up SP so that our data doesn't get clobbered by signals.
fpizlo@apple.com68952692014-03-23 18:56:56 +0000357 unsigned conservativeStackDelta =
358 registerPreservationOffset() +
359 exit.m_values.numberOfLocals() * sizeof(Register) +
360 maxFrameExtentForSlowPathCall;
361 conservativeStackDelta = WTF::roundUpToMultipleOf(
362 stackAlignmentBytes(), conservativeStackDelta);
msaboff@apple.com95894332014-01-29 19:18:54 +0000363 jit.addPtr(
fpizlo@apple.com68952692014-03-23 18:56:56 +0000364 MacroAssembler::TrustedImm32(-conservativeStackDelta),
msaboff@apple.com95894332014-01-29 19:18:54 +0000365 MacroAssembler::framePointerRegister, MacroAssembler::stackPointerRegister);
fpizlo@apple.com68952692014-03-23 18:56:56 +0000366 jit.checkStackPointerAlignment();
msaboff@apple.com95894332014-01-29 19:18:54 +0000367
368 jit.subPtr(
369 MacroAssembler::TrustedImm32(registerPreservationOffset()),
370 MacroAssembler::framePointerRegister);
371
372 // Copy the old frame data into its new location.
373 jit.add32(MacroAssembler::TrustedImm32(JSStack::CallFrameHeaderSize), GPRInfo::regT2);
374 jit.move(MacroAssembler::framePointerRegister, GPRInfo::regT1);
375 MacroAssembler::Label loop = jit.label();
376 jit.sub32(MacroAssembler::TrustedImm32(1), GPRInfo::regT2);
377 jit.load64(MacroAssembler::Address(GPRInfo::regT1, offset), GPRInfo::regT0);
378 jit.store64(GPRInfo::regT0, GPRInfo::regT1);
379 jit.addPtr(MacroAssembler::TrustedImm32(sizeof(Register)), GPRInfo::regT1);
380 jit.branchTest32(MacroAssembler::NonZero, GPRInfo::regT2).linkTo(loop, &jit);
381
382 // At this point regT1 points to where we would save our registers. Save them here.
383 ptrdiff_t currentOffset = 0;
fpizlo@apple.com4118d352014-02-18 23:04:53 +0000384 for (Reg reg = Reg::first(); reg <= Reg::last(); reg = reg.next()) {
385 if (!toSave.get(reg))
msaboff@apple.com95894332014-01-29 19:18:54 +0000386 continue;
387 currentOffset += sizeof(Register);
fpizlo@apple.com4118d352014-02-18 23:04:53 +0000388 unsigned unwindIndex = jitCode->unwindInfo.indexOf(reg);
msaboff@apple.com95894332014-01-29 19:18:54 +0000389 if (unwindIndex == UINT_MAX) {
390 // The FTL compilation didn't preserve this register. This means that it also
391 // didn't use the register. So its value at the beginning of OSR exit should be
392 // preserved by the thunk. Luckily, we saved all registers into the register
393 // scratch buffer, so we can restore them from there.
fpizlo@apple.com4118d352014-02-18 23:04:53 +0000394 jit.load64(registerScratch + offsetOfReg(reg), GPRInfo::regT0);
msaboff@apple.com95894332014-01-29 19:18:54 +0000395 } else {
396 // The FTL compilation preserved the register. Its new value is therefore
397 // irrelevant, but we can get the value that was preserved by using the unwind
398 // data. We've already copied all unwind-able preserved registers into the unwind
399 // scratch buffer, so we can get it from there.
400 jit.load64(unwindScratch + unwindIndex, GPRInfo::regT0);
401 }
402 jit.store64(GPRInfo::regT0, AssemblyHelpers::Address(GPRInfo::regT1, currentOffset));
403 }
404
405 // We need to make sure that we return into the register restoration thunk. This works
406 // differently depending on whether or not we had arity issues.
fpizlo@apple.com0cec6dea2014-03-04 21:27:37 +0000407 MacroAssembler::Jump arityIntactForReturnPC = jit.branch32(
408 MacroAssembler::GreaterThanOrEqual,
409 CCallHelpers::payloadFor(JSStack::ArgumentCount),
410 MacroAssembler::TrustedImm32(codeBlock->numParameters()));
msaboff@apple.com95894332014-01-29 19:18:54 +0000411
412 // The return PC in the call frame header points at exactly the right arity restoration
413 // thunk. We don't want to change that. But the arity restoration thunk's frame has a
414 // return PC and we want to reroute that to our register restoration thunk. The arity
415 // restoration's return PC just just below regT1, and the register restoration's return PC
416 // is right at regT1.
417 jit.loadPtr(MacroAssembler::Address(GPRInfo::regT1, -static_cast<ptrdiff_t>(sizeof(Register))), GPRInfo::regT0);
418 jit.storePtr(GPRInfo::regT0, GPRInfo::regT1);
419 jit.storePtr(
420 MacroAssembler::TrustedImmPtr(vm->getCTIStub(registerRestorationThunkGenerator).code().executableAddress()),
421 MacroAssembler::Address(GPRInfo::regT1, -static_cast<ptrdiff_t>(sizeof(Register))));
422
423 MacroAssembler::Jump arityReturnPCReady = jit.jump();
424
425 arityIntactForReturnPC.link(&jit);
426
427 jit.loadPtr(MacroAssembler::Address(MacroAssembler::framePointerRegister, CallFrame::returnPCOffset()), GPRInfo::regT0);
428 jit.storePtr(GPRInfo::regT0, GPRInfo::regT1);
429 jit.storePtr(
430 MacroAssembler::TrustedImmPtr(vm->getCTIStub(registerRestorationThunkGenerator).code().executableAddress()),
431 MacroAssembler::Address(MacroAssembler::framePointerRegister, CallFrame::returnPCOffset()));
432
433 arityReturnPCReady.link(&jit);
434
fpizlo@apple.com2a69df42014-09-20 18:45:54 +0000435 // Now get state out of the scratch buffer and place it back into the stack. The values are
436 // already reboxed so we just move them.
fpizlo@apple.comea92c202013-10-10 04:24:57 +0000437 for (unsigned index = exit.m_values.size(); index--;) {
438 int operand = exit.m_values.operandForIndex(index);
fpizlo@apple.comea92c202013-10-10 04:24:57 +0000439
440 jit.load64(scratch + index, GPRInfo::regT0);
msaboff@apple.com95894332014-01-29 19:18:54 +0000441 jit.store64(GPRInfo::regT0, AssemblyHelpers::addressFor(static_cast<VirtualRegister>(operand)));
fpizlo@apple.comea92c202013-10-10 04:24:57 +0000442 }
443
444 handleExitCounts(jit, exit);
445 reifyInlinedCallFrames(jit, exit);
fpizlo@apple.comf8ec8b42014-03-01 19:57:40 +0000446
447 ArgumentsRecoveryGenerator argumentsRecovery;
448 for (unsigned index = exit.m_values.size(); index--;) {
449 if (!exit.m_values[index].isArgumentsObjectThatWasNotCreated())
450 continue;
451 int operand = exit.m_values.operandForIndex(index);
452 argumentsRecovery.generateFor(operand, exit.m_codeOrigin, jit);
453 }
454
fpizlo@apple.comea92c202013-10-10 04:24:57 +0000455 adjustAndJumpToTarget(jit, exit);
456
benjamin@webkit.orgf766fd92014-07-08 04:23:30 +0000457 LinkBuffer patchBuffer(*vm, jit, codeBlock);
fpizlo@apple.comea92c202013-10-10 04:24:57 +0000458 exit.m_code = FINALIZE_CODE_IF(
fpizlo@apple.com95192c72014-02-10 17:04:28 +0000459 shouldShowDisassembly() || Options::verboseOSR() || Options::verboseFTLOSRExit(),
fpizlo@apple.comea92c202013-10-10 04:24:57 +0000460 patchBuffer,
fpizlo@apple.comd2ceb392013-11-11 07:30:50 +0000461 ("FTL OSR exit #%u (%s, %s) from %s, with operands = %s, and record = %s",
462 exitID, toCString(exit.m_codeOrigin).data(),
fpizlo@apple.comea92c202013-10-10 04:24:57 +0000463 exitKindToString(exit.m_kind), toCString(*codeBlock).data(),
fpizlo@apple.com47ce2112013-10-13 20:45:12 +0000464 toCString(ignoringContext<DumpContext>(exit.m_values)).data(),
465 toCString(*record).data()));
fpizlo@apple.comea92c202013-10-10 04:24:57 +0000466}
467
oliver@apple.comea771492013-07-25 03:58:38 +0000468extern "C" void* compileFTLOSRExit(ExecState* exec, unsigned exitID)
469{
470 SamplingRegion samplingRegion("FTL OSR Exit Compilation");
mhahnenberg@apple.com2385e392014-02-27 01:09:39 +0000471
472 if (shouldShowDisassembly() || Options::verboseOSR() || Options::verboseFTLOSRExit())
473 dataLog("Compiling OSR exit with exitID = ", exitID, "\n");
fpizlo@apple.com41bed902014-02-25 22:18:21 +0000474
oliver@apple.comea771492013-07-25 03:58:38 +0000475 CodeBlock* codeBlock = exec->codeBlock();
476
477 ASSERT(codeBlock);
oliver@apple.com5a24fdd2013-07-25 04:00:54 +0000478 ASSERT(codeBlock->jitType() == JITCode::FTLJIT);
oliver@apple.comea771492013-07-25 03:58:38 +0000479
480 VM* vm = &exec->vm();
481
fpizlo@apple.comfe6bd742013-09-17 20:56:57 +0000482 // It's sort of preferable that we don't GC while in here. Anyways, doing so wouldn't
483 // really be profitable.
484 DeferGCForAWhile deferGC(vm->heap);
485
fpizlo@apple.comea92c202013-10-10 04:24:57 +0000486 JITCode* jitCode = codeBlock->jitCode()->ftl();
487 OSRExit& exit = jitCode->osrExit[exitID];
oliver@apple.comea771492013-07-25 03:58:38 +0000488
489 prepareCodeOriginForOSRExit(exec, exit.m_codeOrigin);
490
fpizlo@apple.com6bf11982013-11-03 18:24:20 +0000491 compileStub(exitID, jitCode, exit, vm, codeBlock);
oliver@apple.comea771492013-07-25 03:58:38 +0000492
493 RepatchBuffer repatchBuffer(codeBlock);
494 repatchBuffer.relink(
495 exit.codeLocationForRepatch(codeBlock), CodeLocationLabel(exit.m_code.code()));
496
497 return exit.m_code.code().executableAddress();
498}
499
500} } // namespace JSC::FTL
501
502#endif // ENABLE(FTL_JIT)
503