2 * Copyright (C) 2008, 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.
29 #include "ArityCheckMode.h"
30 #include "CallFrame.h"
31 #include "Disassembler.h"
33 #include "JSCJSValue.h"
34 #include "LegacyProfiler.h"
35 #include "MacroAssemblerCodeRef.h"
36 #include "RegisterPreservationMode.h"
45 class ForOSREntryJITCode
;
49 struct ProtoCallFrame
;
52 class JITCode
: public ThreadSafeRefCounted
<JITCode
> {
54 typedef MacroAssemblerCodeRef CodeRef
;
55 typedef MacroAssemblerCodePtr CodePtr
;
57 enum JITType
: uint8_t {
66 static JITType
bottomTierJIT()
71 static JITType
topTierJIT()
76 static JITType
nextTierJIT(JITType jitType
)
84 RELEASE_ASSERT_NOT_REACHED();
89 static bool isExecutableScript(JITType jitType
)
100 static bool couldBeInterpreted(JITType jitType
)
103 case InterpreterThunk
:
111 static bool isJIT(JITType jitType
)
123 static bool isLowerTier(JITType expectedLower
, JITType expectedHigher
)
125 RELEASE_ASSERT(isExecutableScript(expectedLower
));
126 RELEASE_ASSERT(isExecutableScript(expectedHigher
));
127 return expectedLower
< expectedHigher
;
130 static bool isHigherTier(JITType expectedHigher
, JITType expectedLower
)
132 return isLowerTier(expectedLower
, expectedHigher
);
135 static bool isLowerOrSameTier(JITType expectedLower
, JITType expectedHigher
)
137 return !isHigherTier(expectedLower
, expectedHigher
);
140 static bool isHigherOrSameTier(JITType expectedHigher
, JITType expectedLower
)
142 return isLowerOrSameTier(expectedLower
, expectedHigher
);
145 static bool isOptimizingJIT(JITType jitType
)
147 return jitType
== DFGJIT
|| jitType
== FTLJIT
;
150 static bool isBaselineCode(JITType jitType
)
152 return jitType
== InterpreterThunk
|| jitType
== BaselineJIT
;
161 JITType
jitType() const
166 template<typename PointerType
>
167 static JITType
jitTypeFor(PointerType jitCode
)
171 return jitCode
->jitType();
174 virtual CodePtr
addressForCall(VM
&, ExecutableBase
*, ArityCheckMode
, RegisterPreservationMode
) = 0;
175 virtual void* executableAddressAtOffset(size_t offset
) = 0;
176 void* executableAddress() { return executableAddressAtOffset(0); }
177 virtual void* dataAddressAtOffset(size_t offset
) = 0;
178 virtual unsigned offsetOf(void* pointerIntoCode
) = 0;
180 virtual DFG::CommonData
* dfgCommon();
181 virtual DFG::JITCode
* dfg();
182 virtual FTL::JITCode
* ftl();
183 virtual FTL::ForOSREntryJITCode
* ftlForOSREntry();
185 JSValue
execute(VM
*, ProtoCallFrame
*);
187 void* start() { return dataAddressAtOffset(0); }
188 virtual size_t size() = 0;
189 void* end() { return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(start()) + size()); }
191 virtual bool contains(void*) = 0;
197 class JITCodeWithCodeRef
: public JITCode
{
199 JITCodeWithCodeRef(JITType
);
200 JITCodeWithCodeRef(CodeRef
, JITType
);
203 virtual ~JITCodeWithCodeRef();
205 virtual void* executableAddressAtOffset(size_t offset
) override
;
206 virtual void* dataAddressAtOffset(size_t offset
) override
;
207 virtual unsigned offsetOf(void* pointerIntoCode
) override
;
208 virtual size_t size() override
;
209 virtual bool contains(void*) override
;
215 class DirectJITCode
: public JITCodeWithCodeRef
{
217 DirectJITCode(JITType
);
218 DirectJITCode(CodeRef
, CodePtr withArityCheck
, JITType
);
219 virtual ~DirectJITCode();
221 void initializeCodeRef(CodeRef
, CodePtr withArityCheck
);
223 virtual CodePtr
addressForCall(VM
&, ExecutableBase
*, ArityCheckMode
, RegisterPreservationMode
) override
;
226 struct RegisterPreservationWrappers
{
227 CodeRef withoutArityCheck
;
228 CodeRef withArityCheck
;
231 RegisterPreservationWrappers
* ensureWrappers();
233 CodePtr m_withArityCheck
;
235 std::unique_ptr
<RegisterPreservationWrappers
> m_wrappers
;
238 class NativeJITCode
: public JITCodeWithCodeRef
{
240 NativeJITCode(JITType
);
241 NativeJITCode(CodeRef
, JITType
);
242 virtual ~NativeJITCode();
244 void initializeCodeRef(CodeRef
);
246 virtual CodePtr
addressForCall(VM
&, ExecutableBase
*, ArityCheckMode
, RegisterPreservationMode
) override
;
254 void printInternal(PrintStream
&, JSC::JITCode::JITType
);