3ac41d90ee7475d6bf0ae1aeccf5b0feed3a80a0
[WebKit-https.git] / Source / JavaScriptCore / ftl / FTLJITCode.cpp
1 /*
2  * Copyright (C) 2013, 2015-2016 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 "FTLJITCode.h"
28
29 #if ENABLE(FTL_JIT)
30
31 #include "FTLState.h"
32
33 namespace JSC { namespace FTL {
34
35 using namespace B3;
36
37 JITCode::JITCode()
38     : JSC::JITCode(FTLJIT)
39 {
40 }
41
42 JITCode::~JITCode()
43 {
44     if (FTL::shouldDumpDisassembly()) {
45         dataLog("Destroying FTL JIT code at ");
46         CommaPrinter comma;
47         dataLog(comma, m_b3Code);
48         dataLog(comma, m_arityCheckEntrypoint);
49         dataLog("\n");
50     }
51 }
52
53 void JITCode::initializeB3Code(CodeRef b3Code)
54 {
55     m_b3Code = b3Code;
56 }
57
58 void JITCode::initializeB3Byproducts(std::unique_ptr<OpaqueByproducts> byproducts)
59 {
60     m_b3Byproducts = WTFMove(byproducts);
61 }
62
63 void JITCode::initializeAddressForCall(CodePtr address)
64 {
65     m_addressForCall = address;
66 }
67
68 void JITCode::initializeArityCheckEntrypoint(CodeRef entrypoint)
69 {
70     m_arityCheckEntrypoint = entrypoint;
71 }
72
73 JITCode::CodePtr JITCode::addressForCall(ArityCheckMode arityCheck)
74 {
75     switch (arityCheck) {
76     case ArityCheckNotRequired:
77         return m_addressForCall;
78     case MustCheckArity:
79         return m_arityCheckEntrypoint.code();
80     }
81     RELEASE_ASSERT_NOT_REACHED();
82     return CodePtr();
83 }
84
85 void* JITCode::executableAddressAtOffset(size_t offset)
86 {
87     return reinterpret_cast<char*>(m_addressForCall.executableAddress()) + offset;
88 }
89
90 void* JITCode::dataAddressAtOffset(size_t)
91 {
92     // We can't patch FTL code, yet. Even if we did, it's not clear that we would do so
93     // through this API.
94     RELEASE_ASSERT_NOT_REACHED();
95     return 0;
96 }
97
98 unsigned JITCode::offsetOf(void*)
99 {
100     // We currently don't have visibility into the FTL code.
101     RELEASE_ASSERT_NOT_REACHED();
102     return 0;
103 }
104
105 size_t JITCode::size()
106 {
107     // We don't know the size of FTL code, yet. Make a wild guess. This is mostly used for
108     // GC load estimates.
109     return 1000;
110 }
111
112 bool JITCode::contains(void*)
113 {
114     // We have no idea what addresses the FTL code contains, yet.
115     RELEASE_ASSERT_NOT_REACHED();
116     return false;
117 }
118
119 JITCode* JITCode::ftl()
120 {
121     return this;
122 }
123
124 DFG::CommonData* JITCode::dfgCommon()
125 {
126     return &common;
127 }
128
129 void JITCode::validateReferences(const TrackedReferences& trackedReferences)
130 {
131     common.validateReferences(trackedReferences);
132     
133     for (OSRExit& exit : osrExit)
134         exit.m_descriptor->validateReferences(trackedReferences);
135 }
136
137 RegisterSet JITCode::liveRegistersToPreserveAtExceptionHandlingCallSite(CodeBlock*, CallSiteIndex callSiteIndex)
138 {
139     for (OSRExit& exit : osrExit) {
140         if (exit.m_exceptionHandlerCallSiteIndex.bits() == callSiteIndex.bits()) {
141             RELEASE_ASSERT(exit.isExceptionHandler());
142             RELEASE_ASSERT(exit.isGenericUnwindHandler());
143             return ValueRep::usedRegisters(exit.m_valueReps);
144         }
145     }
146     return RegisterSet();
147 }
148
149 Optional<CodeOrigin> JITCode::findPC(CodeBlock* codeBlock, void* pc)
150 {
151     for (OSRExit& exit : osrExit) {
152         if (ExecutableMemoryHandle* handle = exit.m_code.executableMemory()) {
153             if (handle->start() <= pc && pc < handle->end())
154                 return Optional<CodeOrigin>(exit.m_codeOriginForExitProfile);
155         }
156     }
157
158     for (std::unique_ptr<LazySlowPath>& lazySlowPath : lazySlowPaths) {
159         if (ExecutableMemoryHandle* handle = lazySlowPath->stub().executableMemory()) {
160             if (handle->start() <= pc && pc < handle->end())
161                 return Optional<CodeOrigin>(codeBlock->codeOrigin(lazySlowPath->callSiteIndex()));
162         }
163     }
164
165     return Nullopt;
166 }
167
168 } } // namespace JSC::FTL
169
170 #endif // ENABLE(FTL_JIT)
171