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 "MacroAssemblerCodeRef.h"
35 #include "RegisterPreservationMode.h"
44 class ForOSREntryJITCode
;
48 struct ProtoCallFrame
;
49 class TrackedReferences
;
52 class JITCode
: public ThreadSafeRefCounted
<JITCode
> {
54 typedef MacroAssemblerCodeRef CodeRef
;
55 typedef MacroAssemblerCodePtr CodePtr
;
57 enum JITType
: uint8_t {
66 static const char* typeName(JITType
);
68 static JITType
bottomTierJIT()
73 static JITType
topTierJIT()
78 static JITType
nextTierJIT(JITType jitType
)
86 RELEASE_ASSERT_NOT_REACHED();
91 static bool isExecutableScript(JITType jitType
)
102 static bool couldBeInterpreted(JITType jitType
)
105 case InterpreterThunk
:
113 static bool isJIT(JITType jitType
)
125 static bool isLowerTier(JITType expectedLower
, JITType expectedHigher
)
127 RELEASE_ASSERT(isExecutableScript(expectedLower
));
128 RELEASE_ASSERT(isExecutableScript(expectedHigher
));
129 return expectedLower
< expectedHigher
;
132 static bool isHigherTier(JITType expectedHigher
, JITType expectedLower
)
134 return isLowerTier(expectedLower
, expectedHigher
);
137 static bool isLowerOrSameTier(JITType expectedLower
, JITType expectedHigher
)
139 return !isHigherTier(expectedLower
, expectedHigher
);
142 static bool isHigherOrSameTier(JITType expectedHigher
, JITType expectedLower
)
144 return isLowerOrSameTier(expectedLower
, expectedHigher
);
147 static bool isOptimizingJIT(JITType jitType
)
149 return jitType
== DFGJIT
|| jitType
== FTLJIT
;
152 static bool isBaselineCode(JITType jitType
)
154 return jitType
== InterpreterThunk
|| jitType
== BaselineJIT
;
163 JITType
jitType() const
168 template<typename PointerType
>
169 static JITType
jitTypeFor(PointerType jitCode
)
173 return jitCode
->jitType();
176 virtual CodePtr
addressForCall(VM
&, ExecutableBase
*, ArityCheckMode
, RegisterPreservationMode
) = 0;
177 virtual void* executableAddressAtOffset(size_t offset
) = 0;
178 void* executableAddress() { return executableAddressAtOffset(0); }
179 virtual void* dataAddressAtOffset(size_t offset
) = 0;
180 virtual unsigned offsetOf(void* pointerIntoCode
) = 0;
182 virtual DFG::CommonData
* dfgCommon();
183 virtual DFG::JITCode
* dfg();
184 virtual FTL::JITCode
* ftl();
185 virtual FTL::ForOSREntryJITCode
* ftlForOSREntry();
187 virtual void validateReferences(const TrackedReferences
&);
189 JSValue
execute(VM
*, ProtoCallFrame
*);
191 void* start() { return dataAddressAtOffset(0); }
192 virtual size_t size() = 0;
193 void* end() { return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(start()) + size()); }
195 virtual bool contains(void*) = 0;
201 class JITCodeWithCodeRef
: public JITCode
{
203 JITCodeWithCodeRef(JITType
);
204 JITCodeWithCodeRef(CodeRef
, JITType
);
207 virtual ~JITCodeWithCodeRef();
209 virtual void* executableAddressAtOffset(size_t offset
) override
;
210 virtual void* dataAddressAtOffset(size_t offset
) override
;
211 virtual unsigned offsetOf(void* pointerIntoCode
) override
;
212 virtual size_t size() override
;
213 virtual bool contains(void*) override
;
219 class DirectJITCode
: public JITCodeWithCodeRef
{
221 DirectJITCode(JITType
);
222 DirectJITCode(CodeRef
, CodePtr withArityCheck
, JITType
);
223 virtual ~DirectJITCode();
225 void initializeCodeRef(CodeRef
, CodePtr withArityCheck
);
227 virtual CodePtr
addressForCall(VM
&, ExecutableBase
*, ArityCheckMode
, RegisterPreservationMode
) override
;
230 struct RegisterPreservationWrappers
{
231 CodeRef withoutArityCheck
;
232 CodeRef withArityCheck
;
235 RegisterPreservationWrappers
* ensureWrappers();
237 CodePtr m_withArityCheck
;
239 std::unique_ptr
<RegisterPreservationWrappers
> m_wrappers
;
242 class NativeJITCode
: public JITCodeWithCodeRef
{
244 NativeJITCode(JITType
);
245 NativeJITCode(CodeRef
, JITType
);
246 virtual ~NativeJITCode();
248 void initializeCodeRef(CodeRef
);
250 virtual CodePtr
addressForCall(VM
&, ExecutableBase
*, ArityCheckMode
, RegisterPreservationMode
) override
;
258 void printInternal(PrintStream
&, JSC::JITCode::JITType
);