1/*
2 * Copyright (C) 2013-2017 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 "FTLOSREntry.h"
28
29#include "CallFrame.h"
30#include "CodeBlock.h"
31#include "DFGJITCode.h"
32#include "FTLForOSREntryJITCode.h"
33#include "OperandsInlines.h"
34#include "JSCInlines.h"
35#include "VMInlines.h"
36
37#if ENABLE(FTL_JIT)
38
39namespace JSC { namespace FTL {
40
41SUPPRESS_ASAN
42void* prepareOSREntry(
43 ExecState* exec, CodeBlock* dfgCodeBlock, CodeBlock* entryCodeBlock,
44 unsigned bytecodeIndex, unsigned streamIndex)
45{
46 VM& vm = exec->vm();
47 CodeBlock* baseline = dfgCodeBlock->baselineVersion();
48 ExecutableBase* executable = dfgCodeBlock->ownerExecutable();
49 DFG::JITCode* dfgCode = dfgCodeBlock->jitCode()->dfg();
50 ForOSREntryJITCode* entryCode = entryCodeBlock->jitCode()->ftlForOSREntry();
51
52 if (Options::verboseOSR()) {
53 dataLog(
54 "FTL OSR from ", *dfgCodeBlock, " to ", *entryCodeBlock, " at bc#",
55 bytecodeIndex, ".\n");
56 }
57
58 if (bytecodeIndex)
59 jsCast<ScriptExecutable*>(executable)->setDidTryToEnterInLoop(true);
60
61 if (bytecodeIndex != entryCode->bytecodeIndex()) {
62 if (Options::verboseOSR())
63 dataLog(" OSR failed because we don't have an entrypoint for bc#", bytecodeIndex, "; ours is for bc#", entryCode->bytecodeIndex(), "\n");
64 return 0;
65 }
66
67 Operands<Optional<JSValue>> values;
68 dfgCode->reconstruct(
69 exec, dfgCodeBlock, CodeOrigin(bytecodeIndex), streamIndex, values);
70
71 if (Options::verboseOSR())
72 dataLog(" Values at entry: ", values, "\n");
73
74 for (int argument = values.numberOfArguments(); argument--;) {
75 JSValue valueOnStack = exec->r(virtualRegisterForArgument(argument).offset()).asanUnsafeJSValue();
76 Optional<JSValue> reconstructedValue = values.argument(argument);
77 if ((reconstructedValue && valueOnStack == reconstructedValue.value()) || !argument)
78 continue;
79 dataLog("Mismatch between reconstructed values and the value on the stack for argument arg", argument, " for ", *entryCodeBlock, " at bc#", bytecodeIndex, ":\n");
80 dataLog(" Value on stack: ", valueOnStack, "\n");
81 dataLog(" Reconstructed value: ", reconstructedValue, "\n");
82 RELEASE_ASSERT_NOT_REACHED();
83 }
84
85 RELEASE_ASSERT(
86 static_cast<int>(values.numberOfLocals()) == baseline->numCalleeLocals());
87
88 EncodedJSValue* scratch = static_cast<EncodedJSValue*>(
89 entryCode->entryBuffer()->dataBuffer());
90
91 for (int local = values.numberOfLocals(); local--;) {
92 Optional<JSValue> value = values.local(local);
93 if (value)
94 scratch[local] = JSValue::encode(value.value());
95 else
96 scratch[local] = JSValue::encode(JSValue());
97 }
98
99 int stackFrameSize = entryCode->common.requiredRegisterCountForExecutionAndExit();
100 if (UNLIKELY(!vm.ensureStackCapacityFor(&exec->registers()[virtualRegisterForLocal(stackFrameSize - 1).offset()]))) {
101 if (Options::verboseOSR())
102 dataLog(" OSR failed because stack growth failed.\n");
103 return 0;
104 }
105
106 exec->setCodeBlock(entryCodeBlock);
107
108 void* result = entryCode->addressForCall(ArityCheckNotRequired).executableAddress();
109 if (Options::verboseOSR())
110 dataLog(" Entry will succeed, going to address ", RawPointer(result), "\n");
111
112 return result;
113}
114
115} } // namespace JSC::FTL
116
117#endif // ENABLE(FTL_JIT)
118
119
120