2 * Copyright (C) 2011, 2012, 2013 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "DFGDriver.h"
34 #include "CodeBlock.h"
35 #include "DFGJITCode.h"
37 #include "DFGThunks.h"
38 #include "DFGWorklist.h"
40 #include "Operations.h"
42 #include "SamplingTool.h"
43 #include <wtf/Atomics.h>
46 #include "FTLThunks.h"
49 namespace JSC { namespace DFG {
51 static unsigned numCompilations;
53 unsigned getNumCompilations()
55 return numCompilations;
58 static CompilationResult compile(CompileMode compileMode, ExecState* exec, CodeBlock* codeBlock, RefPtr<JSC::JITCode>& jitCode, MacroAssemblerCodePtr* jitCodeWithArityCheck, unsigned osrEntryBytecodeIndex)
60 SamplingRegion samplingRegion("DFG Compilation (Driver)");
65 ASSERT(codeBlock->alternative());
66 ASSERT(codeBlock->alternative()->jitType() == JITCode::BaselineJIT);
68 ASSERT(osrEntryBytecodeIndex != UINT_MAX);
70 if (!Options::useDFGJIT() || !MacroAssembler::supportsFloatingPoint())
71 return CompilationFailed;
73 if (!Options::bytecodeRangeToDFGCompile().isInRange(codeBlock->instructionCount()))
74 return CompilationFailed;
76 if (logCompilationChanges())
77 dataLog("DFG(Driver) compiling ", *codeBlock, ", number of instructions = ", codeBlock->instructionCount(), "\n");
81 // Make sure that any stubs that the DFG is going to use are initialized. We want to
82 // make sure that al JIT code generation does finalization on the main thread.
83 vm.getCTIStub(osrExitGenerationThunkGenerator);
84 vm.getCTIStub(throwExceptionFromCallSlowPathGenerator);
85 vm.getCTIStub(linkCallThunkGenerator);
86 vm.getCTIStub(linkConstructThunkGenerator);
87 vm.getCTIStub(linkClosureCallThunkGenerator);
88 vm.getCTIStub(virtualCallThunkGenerator);
89 vm.getCTIStub(virtualConstructThunkGenerator);
91 vm.getCTIStub(FTL::osrExitGenerationThunkGenerator);
94 // Derive our set of must-handle values. The compilation must be at least conservative
95 // enough to allow for OSR entry with these values.
96 unsigned numVarsWithValues;
97 if (osrEntryBytecodeIndex)
98 numVarsWithValues = codeBlock->m_numVars;
100 numVarsWithValues = 0;
101 RefPtr<Plan> plan = adoptRef(
102 new Plan(compileMode, codeBlock, osrEntryBytecodeIndex, numVarsWithValues));
103 for (size_t i = 0; i < plan->mustHandleValues.size(); ++i) {
104 int operand = plan->mustHandleValues.operandForIndex(i);
105 if (operandIsArgument(operand)
106 && !operandToArgument(operand)
107 && compileMode == CompileFunction
108 && codeBlock->specializationKind() == CodeForConstruct) {
109 // Ugh. If we're in a constructor, the 'this' argument may hold garbage. It will
110 // also never be used. It doesn't matter what we put into the value for this,
111 // but it has to be an actual value that can be grokked by subsequent DFG passes,
112 // so we sanitize it here by turning it into Undefined.
113 plan->mustHandleValues[i] = jsUndefined();
115 plan->mustHandleValues[i] = exec->uncheckedR(operand).jsValue();
118 if (enableConcurrentJIT()) {
120 vm.worklist = globalWorklist();
121 if (logCompilationChanges())
122 dataLog("Deferring DFG compilation of ", *codeBlock, " with queue length ", vm.worklist->queueLength(), ".\n");
123 vm.worklist->enqueue(plan);
124 return CompilationDeferred;
127 plan->compileInThread(*vm.dfgState);
128 return plan->finalize(jitCode, jitCodeWithArityCheck);
131 CompilationResult tryCompile(ExecState* exec, CodeBlock* codeBlock, RefPtr<JSC::JITCode>& jitCode, unsigned bytecodeIndex)
133 return compile(CompileOther, exec, codeBlock, jitCode, 0, bytecodeIndex);
136 CompilationResult tryCompileFunction(ExecState* exec, CodeBlock* codeBlock, RefPtr<JSC::JITCode>& jitCode, MacroAssemblerCodePtr& jitCodeWithArityCheck, unsigned bytecodeIndex)
138 return compile(CompileFunction, exec, codeBlock, jitCode, &jitCodeWithArityCheck, bytecodeIndex);
141 CompilationResult tryFinalizePlan(PassRefPtr<Plan> plan, RefPtr<JSC::JITCode>& jitCode, MacroAssemblerCodePtr* jitCodeWithArityCheck)
143 return plan->finalize(jitCode, jitCodeWithArityCheck);
146 } } // namespace JSC::DFG
148 #endif // ENABLE(DFG_JIT)