MovHint should be a strong use
[WebKit-https.git] / Source / JavaScriptCore / bytecode / CodeOrigin.cpp
1 /*
2  * Copyright (C) 2012-2015 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 "CodeOrigin.h"
28
29 #include "CallFrame.h"
30 #include "CodeBlock.h"
31 #include "Executable.h"
32 #include "JSCInlines.h"
33
34 namespace JSC {
35
36 unsigned CodeOrigin::inlineDepthForCallFrame(InlineCallFrame* inlineCallFrame)
37 {
38     unsigned result = 1;
39     for (InlineCallFrame* current = inlineCallFrame; current; current = current->caller.inlineCallFrame)
40         result++;
41     return result;
42 }
43
44 unsigned CodeOrigin::inlineDepth() const
45 {
46     return inlineDepthForCallFrame(inlineCallFrame);
47 }
48
49 bool CodeOrigin::isApproximatelyEqualTo(const CodeOrigin& other) const
50 {
51     CodeOrigin a = *this;
52     CodeOrigin b = other;
53
54     if (!a.isSet())
55         return !b.isSet();
56     if (!b.isSet())
57         return false;
58     
59     if (a.isHashTableDeletedValue())
60         return b.isHashTableDeletedValue();
61     if (b.isHashTableDeletedValue())
62         return false;
63     
64     for (;;) {
65         ASSERT(a.isSet());
66         ASSERT(b.isSet());
67         
68         if (a.bytecodeIndex != b.bytecodeIndex)
69             return false;
70         
71         if ((!!a.inlineCallFrame) != (!!b.inlineCallFrame))
72             return false;
73         
74         if (!a.inlineCallFrame)
75             return true;
76         
77         if (a.inlineCallFrame->executable != b.inlineCallFrame->executable)
78             return false;
79         
80         a = a.inlineCallFrame->caller;
81         b = b.inlineCallFrame->caller;
82     }
83 }
84
85 unsigned CodeOrigin::approximateHash() const
86 {
87     if (!isSet())
88         return 0;
89     if (isHashTableDeletedValue())
90         return 1;
91     
92     unsigned result = 2;
93     CodeOrigin codeOrigin = *this;
94     for (;;) {
95         result += codeOrigin.bytecodeIndex;
96         
97         if (!codeOrigin.inlineCallFrame)
98             return result;
99         
100         result += WTF::PtrHash<JSCell*>::hash(codeOrigin.inlineCallFrame->executable.get());
101         
102         codeOrigin = codeOrigin.inlineCallFrame->caller;
103     }
104 }
105
106 Vector<CodeOrigin> CodeOrigin::inlineStack() const
107 {
108     Vector<CodeOrigin> result(inlineDepth());
109     result.last() = *this;
110     unsigned index = result.size() - 2;
111     for (InlineCallFrame* current = inlineCallFrame; current; current = current->caller.inlineCallFrame)
112         result[index--] = current->caller;
113     RELEASE_ASSERT(!result[0].inlineCallFrame);
114     return result;
115 }
116
117 void CodeOrigin::dump(PrintStream& out) const
118 {
119     if (!isSet()) {
120         out.print("<none>");
121         return;
122     }
123     
124     Vector<CodeOrigin> stack = inlineStack();
125     for (unsigned i = 0; i < stack.size(); ++i) {
126         if (i)
127             out.print(" --> ");
128         
129         if (InlineCallFrame* frame = stack[i].inlineCallFrame) {
130             out.print(frame->briefFunctionInformation(), ":<", RawPointer(frame->executable.get()), "> ");
131             if (frame->isClosureCall)
132                 out.print("(closure) ");
133         }
134         
135         out.print("bc#", stack[i].bytecodeIndex);
136     }
137 }
138
139 void CodeOrigin::dumpInContext(PrintStream& out, DumpContext*) const
140 {
141     dump(out);
142 }
143
144 JSFunction* InlineCallFrame::calleeConstant() const
145 {
146     if (calleeRecovery.isConstant())
147         return jsCast<JSFunction*>(calleeRecovery.constant());
148     return nullptr;
149 }
150
151 void InlineCallFrame::visitAggregate(SlotVisitor& visitor)
152 {
153     visitor.append(&executable);
154 }
155
156 JSFunction* InlineCallFrame::calleeForCallFrame(ExecState* exec) const
157 {
158     return jsCast<JSFunction*>(calleeRecovery.recover(exec));
159 }
160
161 CodeBlockHash InlineCallFrame::hash() const
162 {
163     return jsCast<FunctionExecutable*>(executable.get())->codeBlockFor(
164         specializationKind())->hash();
165 }
166
167 CString InlineCallFrame::hashAsStringIfPossible() const
168 {
169     return jsCast<FunctionExecutable*>(executable.get())->codeBlockFor(
170         specializationKind())->hashAsStringIfPossible();
171 }
172
173 CString InlineCallFrame::inferredName() const
174 {
175     return jsCast<FunctionExecutable*>(executable.get())->inferredName().utf8();
176 }
177
178 CodeBlock* InlineCallFrame::baselineCodeBlock() const
179 {
180     return jsCast<FunctionExecutable*>(executable.get())->baselineCodeBlockFor(specializationKind());
181 }
182
183 void InlineCallFrame::dumpBriefFunctionInformation(PrintStream& out) const
184 {
185     out.print(inferredName(), "#", hashAsStringIfPossible());
186 }
187
188 void InlineCallFrame::dumpInContext(PrintStream& out, DumpContext* context) const
189 {
190     out.print(briefFunctionInformation(), ":<", RawPointer(executable.get()));
191     if (executable->isStrictMode())
192         out.print(" (StrictMode)");
193     out.print(", bc#", caller.bytecodeIndex, ", ", kind);
194     if (isClosureCall)
195         out.print(", closure call");
196     else
197         out.print(", known callee: ", inContext(calleeRecovery.constant(), context));
198     out.print(", numArgs+this = ", arguments.size());
199     out.print(", stackOffset = ", stackOffset);
200     out.print(" (", virtualRegisterForLocal(0), " maps to ", virtualRegisterForLocal(0) + stackOffset, ")>");
201 }
202
203 void InlineCallFrame::dump(PrintStream& out) const
204 {
205     dumpInContext(out, 0);
206 }
207
208 } // namespace JSC
209
210 namespace WTF {
211
212 void printInternal(PrintStream& out, JSC::InlineCallFrame::Kind kind)
213 {
214     switch (kind) {
215     case JSC::InlineCallFrame::Call:
216         out.print("Call");
217         return;
218     case JSC::InlineCallFrame::Construct:
219         out.print("Construct");
220         return;
221     case JSC::InlineCallFrame::CallVarargs:
222         out.print("CallVarargs");
223         return;
224     case JSC::InlineCallFrame::ConstructVarargs:
225         out.print("ConstructVarargs");
226         return;
227     case JSC::InlineCallFrame::GetterCall:
228         out.print("GetterCall");
229         return;
230     case JSC::InlineCallFrame::SetterCall:
231         out.print("SetterCall");
232         return;
233     }
234     RELEASE_ASSERT_NOT_REACHED();
235 }
236
237 } // namespace WTF
238