Source/JavaScriptCore: https://bugs.webkit.org/show_bug.cgi?id=119548
[WebKit-https.git] / Source / JavaScriptCore / jit / JITCode.cpp
1 /*
2  * Copyright (C) 2008, 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 #include "config.h"
27 #include "JITCode.h"
28
29 #include "Operations.h"
30 #include <wtf/PrintStream.h>
31
32 namespace JSC {
33
34 JITCode::JITCode(JITType jitType)
35     : m_jitType(jitType)
36 {
37 }
38
39 JITCode::~JITCode()
40 {
41 }
42
43 #if ENABLE(JIT)
44 JSValue JITCode::execute(JSStack* stack, CallFrame* callFrame, VM* vm)
45 {
46     JSValue result = JSValue::decode(ctiTrampoline(executableAddress(), stack, callFrame, 0, 0, vm));
47     return vm->exception() ? jsNull() : result;
48 }
49 #endif
50
51 DFG::CommonData* JITCode::dfgCommon()
52 {
53     RELEASE_ASSERT_NOT_REACHED();
54     return 0;
55 }
56
57 DFG::JITCode* JITCode::dfg()
58 {
59     RELEASE_ASSERT_NOT_REACHED();
60     return 0;
61 }
62
63 FTL::JITCode* JITCode::ftl()
64 {
65     RELEASE_ASSERT_NOT_REACHED();
66     return 0;
67 }
68
69 PassRefPtr<JITCode> JITCode::hostFunction(JITCode::CodeRef code)
70 {
71     return adoptRef(new DirectJITCode(code, HostCallThunk));
72 }
73
74 DirectJITCode::DirectJITCode(JITType jitType)
75     : JITCode(jitType)
76 {
77 }
78
79 DirectJITCode::DirectJITCode(const JITCode::CodeRef ref, JITType jitType)
80     : JITCode(jitType)
81     , m_ref(ref)
82 {
83 }
84
85 DirectJITCode::~DirectJITCode()
86 {
87 }
88
89 void DirectJITCode::initializeCodeRef(const JITCode::CodeRef ref)
90 {
91     RELEASE_ASSERT(!m_ref);
92     m_ref = ref;
93 }
94
95 JITCode::CodePtr DirectJITCode::addressForCall()
96 {
97     RELEASE_ASSERT(m_ref);
98     return m_ref.code();
99 }
100
101 void* DirectJITCode::executableAddressAtOffset(size_t offset)
102 {
103     RELEASE_ASSERT(m_ref);
104     return reinterpret_cast<char*>(m_ref.code().executableAddress()) + offset;
105 }
106
107 void* DirectJITCode::dataAddressAtOffset(size_t offset)
108 {
109     RELEASE_ASSERT(m_ref);
110     ASSERT(offset <= size()); // use <= instead of < because it is valid to ask for an address at the exclusive end of the code.
111     return reinterpret_cast<char*>(m_ref.code().dataLocation()) + offset;
112 }
113
114 unsigned DirectJITCode::offsetOf(void* pointerIntoCode)
115 {
116     RELEASE_ASSERT(m_ref);
117     intptr_t result = reinterpret_cast<intptr_t>(pointerIntoCode) - reinterpret_cast<intptr_t>(m_ref.code().executableAddress());
118     ASSERT(static_cast<intptr_t>(static_cast<unsigned>(result)) == result);
119     return static_cast<unsigned>(result);
120 }
121
122 size_t DirectJITCode::size()
123 {
124     RELEASE_ASSERT(m_ref);
125     return m_ref.size();
126 }
127
128 bool DirectJITCode::contains(void* address)
129 {
130     RELEASE_ASSERT(m_ref);
131     return m_ref.executableMemory()->contains(address);
132 }
133
134 } // namespace JSC
135
136 namespace WTF {
137
138 void printInternal(PrintStream& out, JSC::JITCode::JITType type)
139 {
140     switch (type) {
141     case JSC::JITCode::None:
142         out.print("None");
143         return;
144     case JSC::JITCode::HostCallThunk:
145         out.print("Host");
146         return;
147     case JSC::JITCode::InterpreterThunk:
148         out.print("LLInt");
149         return;
150     case JSC::JITCode::BaselineJIT:
151         out.print("Baseline");
152         return;
153     case JSC::JITCode::DFGJIT:
154         out.print("DFG");
155         return;
156     case JSC::JITCode::FTLJIT:
157         out.print("FTL");
158         return;
159     default:
160         CRASH();
161         return;
162     }
163 }
164
165 } // namespace WTF
166