Make JITType an enum class
[WebKit-https.git] / Source / JavaScriptCore / llint / LLIntEntrypoint.cpp
1 /*
2  * Copyright (C) 2012-2018 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 "LLIntEntrypoint.h"
28 #include "CodeBlock.h"
29 #include "HeapInlines.h"
30 #include "JITCode.h"
31 #include "JSCellInlines.h"
32 #include "JSObject.h"
33 #include "LLIntData.h"
34 #include "LLIntThunks.h"
35 #include "LowLevelInterpreter.h"
36 #include "MaxFrameExtentForSlowPathCall.h"
37 #include "StackAlignment.h"
38 #include "VM.h"
39
40 namespace JSC { namespace LLInt {
41
42 static void setFunctionEntrypoint(CodeBlock* codeBlock)
43 {
44     CodeSpecializationKind kind = codeBlock->specializationKind();
45     
46 #if ENABLE(JIT)
47     if (VM::canUseJIT()) {
48         if (kind == CodeForCall) {
49             static DirectJITCode* jitCode;
50             static std::once_flag onceKey;
51             std::call_once(onceKey, [&] {
52                 auto callRef = functionForCallEntryThunk().retagged<JSEntryPtrTag>();
53                 auto callArityCheckRef = functionForCallArityCheckThunk().retaggedCode<JSEntryPtrTag>();
54                 jitCode = new DirectJITCode(callRef, callArityCheckRef, JITType::InterpreterThunk, JITCode::ShareAttribute::Shared);
55             });
56
57             codeBlock->setJITCode(makeRef(*jitCode));
58             return;
59         }
60         ASSERT(kind == CodeForConstruct);
61
62         static DirectJITCode* jitCode;
63         static std::once_flag onceKey;
64         std::call_once(onceKey, [&] {
65             auto constructRef = functionForConstructEntryThunk().retagged<JSEntryPtrTag>();
66             auto constructArityCheckRef = functionForConstructArityCheckThunk().retaggedCode<JSEntryPtrTag>();
67             jitCode = new DirectJITCode(constructRef, constructArityCheckRef, JITType::InterpreterThunk, JITCode::ShareAttribute::Shared);
68         });
69
70         codeBlock->setJITCode(makeRef(*jitCode));
71         return;
72     }
73 #endif // ENABLE(JIT)
74
75     if (kind == CodeForCall) {
76         static DirectJITCode* jitCode;
77         static std::once_flag onceKey;
78         std::call_once(onceKey, [&] {
79             jitCode = new DirectJITCode(getCodeRef<JSEntryPtrTag>(llint_function_for_call_prologue), getCodePtr<JSEntryPtrTag>(llint_function_for_call_arity_check), JITType::InterpreterThunk, JITCode::ShareAttribute::Shared);
80         });
81         codeBlock->setJITCode(makeRef(*jitCode));
82     } else {
83         static DirectJITCode* jitCode;
84         static std::once_flag onceKey;
85         std::call_once(onceKey, [&] {
86             jitCode = new DirectJITCode(getCodeRef<JSEntryPtrTag>(llint_function_for_construct_prologue), getCodePtr<JSEntryPtrTag>(llint_function_for_construct_arity_check), JITType::InterpreterThunk, JITCode::ShareAttribute::Shared);
87         });
88         codeBlock->setJITCode(makeRef(*jitCode));
89     }
90 }
91
92 static void setEvalEntrypoint(CodeBlock* codeBlock)
93 {
94 #if ENABLE(JIT)
95     if (VM::canUseJIT()) {
96         static NativeJITCode* jitCode;
97         static std::once_flag onceKey;
98         std::call_once(onceKey, [&] {
99             MacroAssemblerCodeRef<JSEntryPtrTag> codeRef = evalEntryThunk().retagged<JSEntryPtrTag>();
100             jitCode = new NativeJITCode(codeRef, JITType::InterpreterThunk, Intrinsic::NoIntrinsic, JITCode::ShareAttribute::Shared);
101         });
102         codeBlock->setJITCode(makeRef(*jitCode));
103         return;
104     }
105 #endif // ENABLE(JIT)
106
107     static NativeJITCode* jitCode;
108     static std::once_flag onceKey;
109     std::call_once(onceKey, [&] {
110         jitCode = new NativeJITCode(getCodeRef<JSEntryPtrTag>(llint_eval_prologue), JITType::InterpreterThunk, Intrinsic::NoIntrinsic, JITCode::ShareAttribute::Shared);
111     });
112     codeBlock->setJITCode(makeRef(*jitCode));
113 }
114
115 static void setProgramEntrypoint(CodeBlock* codeBlock)
116 {
117 #if ENABLE(JIT)
118     if (VM::canUseJIT()) {
119         static NativeJITCode* jitCode;
120         static std::once_flag onceKey;
121         std::call_once(onceKey, [&] {
122             MacroAssemblerCodeRef<JSEntryPtrTag> codeRef = programEntryThunk().retagged<JSEntryPtrTag>();
123             jitCode = new NativeJITCode(codeRef, JITType::InterpreterThunk, Intrinsic::NoIntrinsic, JITCode::ShareAttribute::Shared);
124         });
125         codeBlock->setJITCode(makeRef(*jitCode));
126         return;
127     }
128 #endif // ENABLE(JIT)
129
130     static NativeJITCode* jitCode;
131     static std::once_flag onceKey;
132     std::call_once(onceKey, [&] {
133         jitCode = new NativeJITCode(getCodeRef<JSEntryPtrTag>(llint_program_prologue), JITType::InterpreterThunk, Intrinsic::NoIntrinsic, JITCode::ShareAttribute::Shared);
134     });
135     codeBlock->setJITCode(makeRef(*jitCode));
136 }
137
138 static void setModuleProgramEntrypoint(CodeBlock* codeBlock)
139 {
140 #if ENABLE(JIT)
141     if (VM::canUseJIT()) {
142         static NativeJITCode* jitCode;
143         static std::once_flag onceKey;
144         std::call_once(onceKey, [&] {
145             MacroAssemblerCodeRef<JSEntryPtrTag> codeRef = moduleProgramEntryThunk().retagged<JSEntryPtrTag>();
146             jitCode = new NativeJITCode(codeRef, JITType::InterpreterThunk, Intrinsic::NoIntrinsic, JITCode::ShareAttribute::Shared);
147         });
148         codeBlock->setJITCode(makeRef(*jitCode));
149         return;
150     }
151 #endif // ENABLE(JIT)
152
153     static NativeJITCode* jitCode;
154     static std::once_flag onceKey;
155     std::call_once(onceKey, [&] {
156         jitCode = new NativeJITCode(getCodeRef<JSEntryPtrTag>(llint_module_program_prologue), JITType::InterpreterThunk, Intrinsic::NoIntrinsic, JITCode::ShareAttribute::Shared);
157     });
158     codeBlock->setJITCode(makeRef(*jitCode));
159 }
160
161 void setEntrypoint(CodeBlock* codeBlock)
162 {
163     switch (codeBlock->codeType()) {
164     case GlobalCode:
165         setProgramEntrypoint(codeBlock);
166         return;
167     case ModuleCode:
168         setModuleProgramEntrypoint(codeBlock);
169         return;
170     case EvalCode:
171         setEvalEntrypoint(codeBlock);
172         return;
173     case FunctionCode:
174         setFunctionEntrypoint(codeBlock);
175         return;
176     }
177     
178     RELEASE_ASSERT_NOT_REACHED();
179 }
180
181 unsigned frameRegisterCountFor(CodeBlock* codeBlock)
182 {
183     ASSERT(static_cast<unsigned>(codeBlock->numCalleeLocals()) == WTF::roundUpToMultipleOf(stackAlignmentRegisters(), static_cast<unsigned>(codeBlock->numCalleeLocals())));
184
185     return roundLocalRegisterCountForFramePointerOffset(codeBlock->numCalleeLocals() + maxFrameExtentForSlowPathCallInRegisters);
186 }
187
188 } } // namespace JSC::LLInt