22d77e578bfb41f4a7a3310251c6388a3b07d8ce
[WebKit-https.git] / Source / JavaScriptCore / runtime / ExecutionHarness.h
1 /*
2  * Copyright (C) 2012, 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 #ifndef ExecutionHarness_h
27 #define ExecutionHarness_h
28
29 #include <wtf/Platform.h>
30
31 #include "CompilationResult.h"
32 #include "DFGDriver.h"
33 #include "DFGPlan.h"
34 #include "JITDriver.h"
35 #include "LLIntEntrypoints.h"
36
37 namespace JSC {
38
39 template<typename CodeBlockType>
40 inline CompilationResult prepareForExecutionImpl(
41     ExecState* exec, CodeBlockType* codeBlock, RefPtr<JITCode>& jitCode,
42     JITCode::JITType jitType, unsigned bytecodeIndex)
43 {
44     UNUSED_PARAM(bytecodeIndex);
45 #if ENABLE(LLINT)
46     if (JITCode::isBaselineCode(jitType)) {
47         // Start off in the low level interpreter.
48         LLInt::getEntrypoint(exec->vm(), codeBlock, jitCode);
49         if (exec->vm().m_perBytecodeProfiler)
50             exec->vm().m_perBytecodeProfiler->ensureBytecodesFor(codeBlock);
51         return CompilationSuccessful;
52     }
53 #endif // ENABLE(LLINT)
54 #if ENABLE(JIT)
55     return jitCompileIfAppropriateImpl(
56         exec, codeBlock, jitCode, jitType, bytecodeIndex,
57         JITCode::isBaselineCode(jitType) ? JITCompilationMustSucceed : JITCompilationCanFail);
58 #endif
59     RELEASE_ASSERT_NOT_REACHED();
60     return CompilationFailed;
61 }
62
63 inline CompilationResult prepareFunctionForExecutionImpl(
64     ExecState* exec, FunctionCodeBlock* codeBlock, RefPtr<JITCode>& jitCode,
65     MacroAssemblerCodePtr& jitCodeWithArityCheck, JITCode::JITType jitType,
66     unsigned bytecodeIndex, CodeSpecializationKind kind)
67 {
68     UNUSED_PARAM(bytecodeIndex);
69     UNUSED_PARAM(jitCodeWithArityCheck);
70 #if ENABLE(LLINT)
71     if (JITCode::isBaselineCode(jitType)) {
72         // Start off in the low level interpreter.
73         LLInt::getFunctionEntrypoint(exec->vm(), kind, jitCode, jitCodeWithArityCheck);
74         if (exec->vm().m_perBytecodeProfiler)
75             exec->vm().m_perBytecodeProfiler->ensureBytecodesFor(codeBlock);
76         return CompilationSuccessful;
77     }
78 #else
79     UNUSED_PARAM(kind);
80 #endif // ENABLE(LLINT)
81 #if ENABLE(JIT)
82     return jitCompileFunctionIfAppropriateImpl(
83         exec, codeBlock, jitCode, jitCodeWithArityCheck, jitType, bytecodeIndex,
84         JITCode::isBaselineCode(jitType) ? JITCompilationMustSucceed : JITCompilationCanFail);
85 #endif
86     RELEASE_ASSERT_NOT_REACHED();
87     return CompilationFailed;
88 }
89
90 template<typename CodeBlockType>
91 inline CompilationResult installOptimizedCode(
92     CompilationResult result, RefPtr<CodeBlockType>& sink, CodeBlockType* codeBlock,
93     PassRefPtr<JITCode> jitCode, MacroAssemblerCodePtr jitCodeWithArityCheck,
94     int* numParameters)
95 {
96     UNUSED_PARAM(jitCodeWithArityCheck);
97     if (result != CompilationSuccessful)
98         return result;
99     
100 #if ENABLE(JIT)
101     codeBlock->setJITCode(jitCode, jitCodeWithArityCheck);
102 #endif
103
104     sink = codeBlock;
105     
106     if (numParameters)
107         *numParameters = codeBlock->numParameters();
108     
109     if (jitCode)
110         codeBlock->vm()->heap.reportExtraMemoryCost(sizeof(*codeBlock) + jitCode->size());
111     else
112         codeBlock->vm()->heap.reportExtraMemoryCost(sizeof(*codeBlock));
113     
114     if (sink != codeBlock) {
115         // This can happen if we GC and decide that the code is invalid.
116         return CompilationInvalidated;
117     }
118     
119     return result;
120 }
121
122 template<typename CodeBlockType>
123 inline CompilationResult prepareForExecution(
124     ExecState* exec, RefPtr<CodeBlockType>& sink, CodeBlockType* codeBlock,
125     RefPtr<JITCode>& jitCode, JITCode::JITType jitType, unsigned bytecodeIndex)
126 {
127     CompilationResult result = prepareForExecutionImpl(exec, codeBlock, jitCode, jitType, bytecodeIndex);
128     return installOptimizedCode(result, sink, codeBlock, jitCode, MacroAssemblerCodePtr(), 0);
129 }
130
131 inline CompilationResult prepareFunctionForExecution(
132     ExecState* exec, RefPtr<FunctionCodeBlock>& sink, FunctionCodeBlock* codeBlock,
133     RefPtr<JITCode>& jitCode, MacroAssemblerCodePtr& jitCodeWithArityCheck,
134     int& numParameters, JITCode::JITType jitType, unsigned bytecodeIndex,
135     CodeSpecializationKind kind)
136 {
137     CompilationResult result = prepareFunctionForExecutionImpl(exec, codeBlock,
138         jitCode, jitCodeWithArityCheck, jitType, bytecodeIndex, kind);
139     return installOptimizedCode(result, sink, codeBlock, jitCode, jitCodeWithArityCheck, &numParameters);
140 }
141
142 #if ENABLE(DFG_JIT)
143 template<typename CodeBlockType>
144 inline CompilationResult replaceWithDeferredOptimizedCode(
145     PassRefPtr<DFG::Plan> passedPlan, RefPtr<CodeBlockType>& sink, RefPtr<JITCode>& jitCode,
146     MacroAssemblerCodePtr* jitCodeWithArityCheck, int* numParameters)
147 {
148     RefPtr<DFG::Plan> plan = passedPlan;
149     CompilationResult result = DFG::tryFinalizePlan(plan, jitCode, jitCodeWithArityCheck);
150     if (Options::verboseOSR()) {
151         dataLog(
152             "Deferred optimizing compilation ", *plan->key(), " -> ", *plan->codeBlock,
153             " result: ", result, "\n");
154     }
155     if (result == CompilationSuccessful)
156         plan->codeBlock->alternative()->unlinkIncomingCalls();
157     return installOptimizedCode(
158         result, sink, static_cast<CodeBlockType*>(plan->codeBlock.get()), jitCode,
159         jitCodeWithArityCheck ? *jitCodeWithArityCheck : MacroAssemblerCodePtr(),
160         numParameters);
161 }
162 #endif // ENABLE(DFG_JIT)
163
164 } // namespace JSC
165
166 #endif // ExecutionHarness_h
167