2 * Copyright (C) 2011-2015 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 "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>
41 struct InlineCallFrame
;
43 class ScriptExecutable
;
47 static const unsigned invalidBytecodeIndex
= UINT_MAX
;
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
;
55 InlineCallFrame
* inlineCallFrame
;
58 : bytecodeIndex(invalidBytecodeIndex
)
63 CodeOrigin(WTF::HashTableDeletedValueType
)
64 : bytecodeIndex(invalidBytecodeIndex
)
65 , inlineCallFrame(deletedMarker())
69 explicit CodeOrigin(unsigned bytecodeIndex
, InlineCallFrame
* inlineCallFrame
= 0)
70 : bytecodeIndex(bytecodeIndex
)
71 , inlineCallFrame(inlineCallFrame
)
73 ASSERT(bytecodeIndex
< invalidBytecodeIndex
);
76 bool isSet() const { return bytecodeIndex
!= invalidBytecodeIndex
; }
77 bool operator!() const { return !isSet(); }
79 bool isHashTableDeletedValue() const
81 return bytecodeIndex
== invalidBytecodeIndex
&& !!inlineCallFrame
;
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;
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;
92 int stackOffset() const;
94 static unsigned inlineDepthForCallFrame(InlineCallFrame
*);
96 unsigned hash() const;
97 bool operator==(const CodeOrigin
& other
) const;
98 bool operator!=(const CodeOrigin
& other
) const { return !(*this == other
); }
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;
104 unsigned approximateHash() const;
106 // Get the inline stack. This is slow, and is intended for debugging only.
107 Vector
<CodeOrigin
> inlineStack() const;
109 void dump(PrintStream
&) const;
110 void dumpInContext(PrintStream
&, DumpContext
*) const;
113 static InlineCallFrame
* deletedMarker()
115 return bitwise_cast
<InlineCallFrame
*>(static_cast<uintptr_t>(1));
119 struct InlineCallFrame
{
126 // For these, the stackOffset incorporates the argument count plus the true return PC
132 static Kind
kindFor(CodeSpecializationKind kind
)
137 case CodeForConstruct
:
140 RELEASE_ASSERT_NOT_REACHED();
144 static Kind
varargsKindFor(CodeSpecializationKind kind
)
149 case CodeForConstruct
:
150 return ConstructVarargs
;
152 RELEASE_ASSERT_NOT_REACHED();
156 static CodeSpecializationKind
specializationKindFor(Kind kind
)
165 case ConstructVarargs
:
166 return CodeForConstruct
;
168 RELEASE_ASSERT_NOT_REACHED();
172 static bool isVarargs(Kind kind
)
176 case ConstructVarargs
:
182 bool isVarargs() const
184 return isVarargs(static_cast<Kind
>(kind
));
187 Vector
<ValueRecovery
> arguments
; // Includes 'this'.
188 WriteBarrier
<ScriptExecutable
> executable
;
189 ValueRecovery calleeRecovery
;
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.
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.
203 , isClosureCall(false)
207 CodeSpecializationKind
specializationKind() const { return specializationKindFor(static_cast<Kind
>(kind
)); }
209 JSFunction
* calleeConstant() const;
210 void visitAggregate(SlotVisitor
&);
212 // Get the callee given a machine call frame to which this InlineCallFrame belongs.
213 JSFunction
* calleeForCallFrame(ExecState
*) const;
215 CString
inferredName() const;
216 CodeBlockHash
hash() const;
217 CString
hashAsStringIfPossible() const;
219 CodeBlock
* baselineCodeBlock() const;
221 void setStackOffset(signed offset
)
223 stackOffset
= offset
;
224 RELEASE_ASSERT(static_cast<signed>(stackOffset
) == offset
);
227 ptrdiff_t callerFrameOffset() const { return stackOffset
* sizeof(Register
) + CallFrame::callerFrameOffset(); }
228 ptrdiff_t returnPCOffset() const { return stackOffset
* sizeof(Register
) + CallFrame::returnPCOffset(); }
230 void dumpBriefFunctionInformation(PrintStream
&) const;
231 void dump(PrintStream
&) const;
232 void dumpInContext(PrintStream
&, DumpContext
*) const;
234 MAKE_PRINT_METHOD(InlineCallFrame
, dumpBriefFunctionInformation
, briefFunctionInformation
);
237 inline int CodeOrigin::stackOffset() const
239 if (!inlineCallFrame
)
242 return inlineCallFrame
->stackOffset
;
245 inline unsigned CodeOrigin::hash() const
247 return WTF::IntHash
<unsigned>::hash(bytecodeIndex
) +
248 WTF::PtrHash
<InlineCallFrame
*>::hash(inlineCallFrame
);
251 inline bool CodeOrigin::operator==(const CodeOrigin
& other
) const
253 return bytecodeIndex
== other
.bytecodeIndex
254 && inlineCallFrame
== other
.inlineCallFrame
;
257 inline ScriptExecutable
* CodeOrigin::codeOriginOwner() const
259 if (!inlineCallFrame
)
261 return inlineCallFrame
->executable
.get();
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;
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;
280 void printInternal(PrintStream
&, JSC::InlineCallFrame::Kind
);
282 template<typename T
> struct DefaultHash
;
283 template<> struct DefaultHash
<JSC::CodeOrigin
> {
284 typedef JSC::CodeOriginHash Hash
;
287 template<typename T
> struct HashTraits
;
288 template<> struct HashTraits
<JSC::CodeOrigin
> : SimpleClassHashTraits
<JSC::CodeOrigin
> {
289 static const bool emptyValueIsZero
= false;
294 #endif // CodeOrigin_h