]> git.saurik.com Git - apple/javascriptcore.git/blame_incremental - ftl/FTLLink.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / ftl / FTLLink.cpp
... / ...
CommitLineData
1/*
2 * Copyright (C) 2013, 2014 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 "FTLLink.h"
28
29#if ENABLE(FTL_JIT)
30
31#include "ArityCheckFailReturnThunks.h"
32#include "CCallHelpers.h"
33#include "CodeBlockWithJITType.h"
34#include "DFGCommon.h"
35#include "FTLJITCode.h"
36#include "JITOperations.h"
37#include "JITStubs.h"
38#include "LLVMAPI.h"
39#include "LinkBuffer.h"
40#include "JSCInlines.h"
41#include "ProfilerCompilation.h"
42#include "VirtualRegister.h"
43
44namespace JSC { namespace FTL {
45
46using namespace DFG;
47
48void link(State& state)
49{
50 Graph& graph = state.graph;
51 CodeBlock* codeBlock = graph.m_codeBlock;
52 VM& vm = graph.m_vm;
53
54 // LLVM will create its own jump tables as needed.
55 codeBlock->clearSwitchJumpTables();
56
57 // FIXME: Need to know the real frame register count.
58 // https://bugs.webkit.org/show_bug.cgi?id=125727
59 state.jitCode->common.frameRegisterCount = 1000;
60
61 state.jitCode->common.requiredRegisterCountForExit = graph.requiredRegisterCountForExit();
62
63 if (!graph.m_plan.inlineCallFrames->isEmpty())
64 state.jitCode->common.inlineCallFrames = graph.m_plan.inlineCallFrames;
65
66 graph.registerFrozenValues();
67
68 // Create the entrypoint. Note that we use this entrypoint totally differently
69 // depending on whether we're doing OSR entry or not.
70 CCallHelpers jit(&vm, codeBlock);
71
72 std::unique_ptr<LinkBuffer> linkBuffer;
73
74 CCallHelpers::Address frame = CCallHelpers::Address(
75 CCallHelpers::stackPointerRegister, -static_cast<int32_t>(AssemblyHelpers::prologueStackPointerDelta()));
76
77 if (Profiler::Compilation* compilation = graph.compilation()) {
78 compilation->addDescription(
79 Profiler::OriginStack(),
80 toCString("Generated FTL JIT code for ", CodeBlockWithJITType(codeBlock, JITCode::FTLJIT), ", instruction count = ", graph.m_codeBlock->instructionCount(), ":\n"));
81
82 graph.m_dominators.computeIfNecessary(graph);
83 graph.m_naturalLoops.computeIfNecessary(graph);
84
85 const char* prefix = " ";
86
87 DumpContext dumpContext;
88 StringPrintStream out;
89 Node* lastNode = 0;
90 for (size_t blockIndex = 0; blockIndex < graph.numBlocks(); ++blockIndex) {
91 BasicBlock* block = graph.block(blockIndex);
92 if (!block)
93 continue;
94
95 graph.dumpBlockHeader(out, prefix, block, Graph::DumpLivePhisOnly, &dumpContext);
96 compilation->addDescription(Profiler::OriginStack(), out.toCString());
97 out.reset();
98
99 for (size_t nodeIndex = 0; nodeIndex < block->size(); ++nodeIndex) {
100 Node* node = block->at(nodeIndex);
101
102 Profiler::OriginStack stack;
103
104 if (node->origin.semantic.isSet()) {
105 stack = Profiler::OriginStack(
106 *vm.m_perBytecodeProfiler, codeBlock, node->origin.semantic);
107 }
108
109 if (graph.dumpCodeOrigin(out, prefix, lastNode, node, &dumpContext)) {
110 compilation->addDescription(stack, out.toCString());
111 out.reset();
112 }
113
114 graph.dump(out, prefix, node, &dumpContext);
115 compilation->addDescription(stack, out.toCString());
116 out.reset();
117
118 if (node->origin.semantic.isSet())
119 lastNode = node;
120 }
121 }
122
123 dumpContext.dump(out, prefix);
124 compilation->addDescription(Profiler::OriginStack(), out.toCString());
125 out.reset();
126
127 out.print(" Disassembly:\n");
128 for (unsigned i = 0; i < state.jitCode->handles().size(); ++i) {
129 if (state.codeSectionNames[i] != SECTION_NAME("text"))
130 continue;
131
132 ExecutableMemoryHandle* handle = state.jitCode->handles()[i].get();
133 disassemble(
134 MacroAssemblerCodePtr(handle->start()), handle->sizeInBytes(),
135 " ", out, LLVMSubset);
136 }
137 compilation->addDescription(Profiler::OriginStack(), out.toCString());
138 out.reset();
139
140 state.jitCode->common.compilation = compilation;
141 }
142
143 switch (graph.m_plan.mode) {
144 case FTLMode: {
145 CCallHelpers::JumpList mainPathJumps;
146
147 jit.load32(
148 frame.withOffset(sizeof(Register) * JSStack::ArgumentCount),
149 GPRInfo::regT1);
150 mainPathJumps.append(jit.branch32(
151 CCallHelpers::AboveOrEqual, GPRInfo::regT1,
152 CCallHelpers::TrustedImm32(codeBlock->numParameters())));
153 jit.emitFunctionPrologue();
154 jit.move(GPRInfo::callFrameRegister, GPRInfo::argumentGPR0);
155 jit.store32(
156 CCallHelpers::TrustedImm32(CallFrame::Location::encodeAsBytecodeOffset(0)),
157 CCallHelpers::tagFor(JSStack::ArgumentCount));
158 jit.storePtr(GPRInfo::callFrameRegister, &vm.topCallFrame);
159 CCallHelpers::Call callArityCheck = jit.call();
160#if !ASSERT_DISABLED
161 // FIXME: need to make this call register with exception handling somehow. This is
162 // part of a bigger problem: FTL should be able to handle exceptions.
163 // https://bugs.webkit.org/show_bug.cgi?id=113622
164 // Until then, use a JIT ASSERT.
165 jit.load64(vm.addressOfException(), GPRInfo::regT1);
166 jit.jitAssertIsNull(GPRInfo::regT1);
167#endif
168 jit.move(GPRInfo::returnValueGPR, GPRInfo::regT0);
169 jit.emitFunctionEpilogue();
170 mainPathJumps.append(jit.branchTest32(CCallHelpers::Zero, GPRInfo::regT0));
171 jit.emitFunctionPrologue();
172 CodeLocationLabel* arityThunkLabels =
173 vm.arityCheckFailReturnThunks->returnPCsFor(vm, codeBlock->numParameters());
174 jit.move(CCallHelpers::TrustedImmPtr(arityThunkLabels), GPRInfo::regT7);
175 jit.loadPtr(CCallHelpers::BaseIndex(GPRInfo::regT7, GPRInfo::regT0, CCallHelpers::timesPtr()), GPRInfo::regT7);
176 CCallHelpers::Call callArityFixup = jit.call();
177 jit.emitFunctionEpilogue();
178 mainPathJumps.append(jit.jump());
179
180 linkBuffer = std::make_unique<LinkBuffer>(vm, jit, codeBlock, JITCompilationCanFail);
181 if (linkBuffer->didFailToAllocate()) {
182 state.allocationFailed = true;
183 return;
184 }
185 linkBuffer->link(callArityCheck, codeBlock->m_isConstructor ? operationConstructArityCheck : operationCallArityCheck);
186 linkBuffer->link(callArityFixup, FunctionPtr((vm.getCTIStub(arityFixupGenerator)).code().executableAddress()));
187 linkBuffer->link(mainPathJumps, CodeLocationLabel(bitwise_cast<void*>(state.generatedFunction)));
188
189 state.jitCode->initializeAddressForCall(MacroAssemblerCodePtr(bitwise_cast<void*>(state.generatedFunction)));
190 break;
191 }
192
193 case FTLForOSREntryMode: {
194 // We jump to here straight from DFG code, after having boxed up all of the
195 // values into the scratch buffer. Everything should be good to go - at this
196 // point we've even done the stack check. Basically we just have to make the
197 // call to the LLVM-generated code.
198 CCallHelpers::Label start = jit.label();
199 jit.emitFunctionEpilogue();
200 CCallHelpers::Jump mainPathJump = jit.jump();
201
202 linkBuffer = std::make_unique<LinkBuffer>(vm, jit, codeBlock, JITCompilationCanFail);
203 if (linkBuffer->didFailToAllocate()) {
204 state.allocationFailed = true;
205 return;
206 }
207 linkBuffer->link(mainPathJump, CodeLocationLabel(bitwise_cast<void*>(state.generatedFunction)));
208
209 state.jitCode->initializeAddressForCall(linkBuffer->locationOf(start));
210 break;
211 }
212
213 default:
214 RELEASE_ASSERT_NOT_REACHED();
215 break;
216 }
217
218 state.finalizer->entrypointLinkBuffer = WTF::move(linkBuffer);
219 state.finalizer->function = state.generatedFunction;
220 state.finalizer->jitCode = state.jitCode;
221}
222
223} } // namespace JSC::FTL
224
225#endif // ENABLE(FTL_JIT)
226