]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (C) 2011-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 | #ifndef CodeOrigin_h | |
27 | #define CodeOrigin_h | |
28 | ||
29 | #include "CodeBlockHash.h" | |
30 | #include "CodeSpecializationKind.h" | |
31 | #include "ValueRecovery.h" | |
32 | #include "WriteBarrier.h" | |
33 | #include <wtf/BitVector.h> | |
34 | #include <wtf/HashMap.h> | |
35 | #include <wtf/PrintStream.h> | |
36 | #include <wtf/StdLibExtras.h> | |
37 | #include <wtf/Vector.h> | |
38 | ||
39 | namespace JSC { | |
40 | ||
41 | struct InlineCallFrame; | |
42 | class ExecState; | |
43 | class ScriptExecutable; | |
44 | class JSFunction; | |
45 | ||
46 | struct CodeOrigin { | |
47 | static const unsigned invalidBytecodeIndex = UINT_MAX; | |
48 | ||
49 | // Bytecode offset that you'd use to re-execute this instruction, and the | |
50 | // bytecode index of the bytecode instruction that produces some result that | |
51 | // you're interested in (used for mapping Nodes whose values you're using | |
52 | // to bytecode instructions that have the appropriate value profile). | |
53 | unsigned bytecodeIndex; | |
54 | ||
55 | InlineCallFrame* inlineCallFrame; | |
56 | ||
57 | CodeOrigin() | |
58 | : bytecodeIndex(invalidBytecodeIndex) | |
59 | , inlineCallFrame(0) | |
60 | { | |
61 | } | |
62 | ||
63 | CodeOrigin(WTF::HashTableDeletedValueType) | |
64 | : bytecodeIndex(invalidBytecodeIndex) | |
65 | , inlineCallFrame(deletedMarker()) | |
66 | { | |
67 | } | |
68 | ||
69 | explicit CodeOrigin(unsigned bytecodeIndex, InlineCallFrame* inlineCallFrame = 0) | |
70 | : bytecodeIndex(bytecodeIndex) | |
71 | , inlineCallFrame(inlineCallFrame) | |
72 | { | |
73 | ASSERT(bytecodeIndex < invalidBytecodeIndex); | |
74 | } | |
75 | ||
76 | bool isSet() const { return bytecodeIndex != invalidBytecodeIndex; } | |
77 | bool operator!() const { return !isSet(); } | |
78 | ||
79 | bool isHashTableDeletedValue() const | |
80 | { | |
81 | return bytecodeIndex == invalidBytecodeIndex && !!inlineCallFrame; | |
82 | } | |
83 | ||
84 | // The inline depth is the depth of the inline stack, so 1 = not inlined, | |
85 | // 2 = inlined one deep, etc. | |
86 | unsigned inlineDepth() const; | |
87 | ||
88 | // If the code origin corresponds to inlined code, gives you the heap object that | |
89 | // would have owned the code if it had not been inlined. Otherwise returns 0. | |
90 | ScriptExecutable* codeOriginOwner() const; | |
91 | ||
92 | int stackOffset() const; | |
93 | ||
94 | static unsigned inlineDepthForCallFrame(InlineCallFrame*); | |
95 | ||
96 | unsigned hash() const; | |
97 | bool operator==(const CodeOrigin& other) const; | |
98 | bool operator!=(const CodeOrigin& other) const { return !(*this == other); } | |
99 | ||
100 | // This checks if the two code origins correspond to the same stack trace snippets, | |
101 | // but ignore whether the InlineCallFrame's are identical. | |
102 | bool isApproximatelyEqualTo(const CodeOrigin& other) const; | |
103 | ||
104 | unsigned approximateHash() const; | |
105 | ||
106 | // Get the inline stack. This is slow, and is intended for debugging only. | |
107 | Vector<CodeOrigin> inlineStack() const; | |
108 | ||
109 | void dump(PrintStream&) const; | |
110 | void dumpInContext(PrintStream&, DumpContext*) const; | |
111 | ||
112 | private: | |
113 | static InlineCallFrame* deletedMarker() | |
114 | { | |
115 | return bitwise_cast<InlineCallFrame*>(static_cast<uintptr_t>(1)); | |
116 | } | |
117 | }; | |
118 | ||
119 | struct InlineCallFrame { | |
120 | enum Kind { | |
121 | Call, | |
122 | Construct, | |
123 | CallVarargs, | |
124 | ConstructVarargs, | |
125 | ||
126 | // For these, the stackOffset incorporates the argument count plus the true return PC | |
127 | // slot. | |
128 | GetterCall, | |
129 | SetterCall | |
130 | }; | |
131 | ||
132 | static Kind kindFor(CodeSpecializationKind kind) | |
133 | { | |
134 | switch (kind) { | |
135 | case CodeForCall: | |
136 | return Call; | |
137 | case CodeForConstruct: | |
138 | return Construct; | |
139 | } | |
140 | RELEASE_ASSERT_NOT_REACHED(); | |
141 | return Call; | |
142 | } | |
143 | ||
144 | static Kind varargsKindFor(CodeSpecializationKind kind) | |
145 | { | |
146 | switch (kind) { | |
147 | case CodeForCall: | |
148 | return CallVarargs; | |
149 | case CodeForConstruct: | |
150 | return ConstructVarargs; | |
151 | } | |
152 | RELEASE_ASSERT_NOT_REACHED(); | |
153 | return Call; | |
154 | } | |
155 | ||
156 | static CodeSpecializationKind specializationKindFor(Kind kind) | |
157 | { | |
158 | switch (kind) { | |
159 | case Call: | |
160 | case CallVarargs: | |
161 | case GetterCall: | |
162 | case SetterCall: | |
163 | return CodeForCall; | |
164 | case Construct: | |
165 | case ConstructVarargs: | |
166 | return CodeForConstruct; | |
167 | } | |
168 | RELEASE_ASSERT_NOT_REACHED(); | |
169 | return CodeForCall; | |
170 | } | |
171 | ||
172 | static bool isVarargs(Kind kind) | |
173 | { | |
174 | switch (kind) { | |
175 | case CallVarargs: | |
176 | case ConstructVarargs: | |
177 | return true; | |
178 | default: | |
179 | return false; | |
180 | } | |
181 | } | |
182 | bool isVarargs() const | |
183 | { | |
184 | return isVarargs(static_cast<Kind>(kind)); | |
185 | } | |
186 | ||
187 | Vector<ValueRecovery> arguments; // Includes 'this'. | |
188 | WriteBarrier<ScriptExecutable> executable; | |
189 | ValueRecovery calleeRecovery; | |
190 | CodeOrigin caller; | |
191 | ||
192 | signed stackOffset : 28; | |
193 | unsigned kind : 3; // real type is Kind | |
194 | bool isClosureCall : 1; // If false then we know that callee/scope are constants and the DFG won't treat them as variables, i.e. they have to be recovered manually. | |
195 | VirtualRegister argumentCountRegister; // Only set when we inline a varargs call. | |
196 | ||
197 | // There is really no good notion of a "default" set of values for | |
198 | // InlineCallFrame's fields. This constructor is here just to reduce confusion if | |
199 | // we forgot to initialize explicitly. | |
200 | InlineCallFrame() | |
201 | : stackOffset(0) | |
202 | , kind(Call) | |
203 | , isClosureCall(false) | |
204 | { | |
205 | } | |
206 | ||
207 | CodeSpecializationKind specializationKind() const { return specializationKindFor(static_cast<Kind>(kind)); } | |
208 | ||
209 | JSFunction* calleeConstant() const; | |
210 | void visitAggregate(SlotVisitor&); | |
211 | ||
212 | // Get the callee given a machine call frame to which this InlineCallFrame belongs. | |
213 | JSFunction* calleeForCallFrame(ExecState*) const; | |
214 | ||
215 | CString inferredName() const; | |
216 | CodeBlockHash hash() const; | |
217 | CString hashAsStringIfPossible() const; | |
218 | ||
219 | CodeBlock* baselineCodeBlock() const; | |
220 | ||
221 | void setStackOffset(signed offset) | |
222 | { | |
223 | stackOffset = offset; | |
224 | RELEASE_ASSERT(static_cast<signed>(stackOffset) == offset); | |
225 | } | |
226 | ||
227 | ptrdiff_t callerFrameOffset() const { return stackOffset * sizeof(Register) + CallFrame::callerFrameOffset(); } | |
228 | ptrdiff_t returnPCOffset() const { return stackOffset * sizeof(Register) + CallFrame::returnPCOffset(); } | |
229 | ||
230 | void dumpBriefFunctionInformation(PrintStream&) const; | |
231 | void dump(PrintStream&) const; | |
232 | void dumpInContext(PrintStream&, DumpContext*) const; | |
233 | ||
234 | MAKE_PRINT_METHOD(InlineCallFrame, dumpBriefFunctionInformation, briefFunctionInformation); | |
235 | }; | |
236 | ||
237 | inline int CodeOrigin::stackOffset() const | |
238 | { | |
239 | if (!inlineCallFrame) | |
240 | return 0; | |
241 | ||
242 | return inlineCallFrame->stackOffset; | |
243 | } | |
244 | ||
245 | inline unsigned CodeOrigin::hash() const | |
246 | { | |
247 | return WTF::IntHash<unsigned>::hash(bytecodeIndex) + | |
248 | WTF::PtrHash<InlineCallFrame*>::hash(inlineCallFrame); | |
249 | } | |
250 | ||
251 | inline bool CodeOrigin::operator==(const CodeOrigin& other) const | |
252 | { | |
253 | return bytecodeIndex == other.bytecodeIndex | |
254 | && inlineCallFrame == other.inlineCallFrame; | |
255 | } | |
256 | ||
257 | inline ScriptExecutable* CodeOrigin::codeOriginOwner() const | |
258 | { | |
259 | if (!inlineCallFrame) | |
260 | return 0; | |
261 | return inlineCallFrame->executable.get(); | |
262 | } | |
263 | ||
264 | struct CodeOriginHash { | |
265 | static unsigned hash(const CodeOrigin& key) { return key.hash(); } | |
266 | static bool equal(const CodeOrigin& a, const CodeOrigin& b) { return a == b; } | |
267 | static const bool safeToCompareToEmptyOrDeleted = true; | |
268 | }; | |
269 | ||
270 | struct CodeOriginApproximateHash { | |
271 | static unsigned hash(const CodeOrigin& key) { return key.approximateHash(); } | |
272 | static bool equal(const CodeOrigin& a, const CodeOrigin& b) { return a.isApproximatelyEqualTo(b); } | |
273 | static const bool safeToCompareToEmptyOrDeleted = true; | |
274 | }; | |
275 | ||
276 | } // namespace JSC | |
277 | ||
278 | namespace WTF { | |
279 | ||
280 | void printInternal(PrintStream&, JSC::InlineCallFrame::Kind); | |
281 | ||
282 | template<typename T> struct DefaultHash; | |
283 | template<> struct DefaultHash<JSC::CodeOrigin> { | |
284 | typedef JSC::CodeOriginHash Hash; | |
285 | }; | |
286 | ||
287 | template<typename T> struct HashTraits; | |
288 | template<> struct HashTraits<JSC::CodeOrigin> : SimpleClassHashTraits<JSC::CodeOrigin> { | |
289 | static const bool emptyValueIsZero = false; | |
290 | }; | |
291 | ||
292 | } // namespace WTF | |
293 | ||
294 | #endif // CodeOrigin_h | |
295 |