2 * Copyright (C) 2011, 2012, 2013 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 "JSFunction.h"
32 #include "ValueRecovery.h"
33 #include "WriteBarrier.h"
34 #include <wtf/BitVector.h>
35 #include <wtf/HashMap.h>
36 #include <wtf/PrintStream.h>
37 #include <wtf/StdLibExtras.h>
38 #include <wtf/Vector.h>
42 struct InlineCallFrame
;
44 class ScriptExecutable
;
48 static const unsigned invalidBytecodeIndex
= UINT_MAX
;
50 // Bytecode offset that you'd use to re-execute this instruction, and the
51 // bytecode index of the bytecode instruction that produces some result that
52 // you're interested in (used for mapping Nodes whose values you're using
53 // to bytecode instructions that have the appropriate value profile).
54 unsigned bytecodeIndex
;
56 InlineCallFrame
* inlineCallFrame
;
59 : bytecodeIndex(invalidBytecodeIndex
)
64 CodeOrigin(WTF::HashTableDeletedValueType
)
65 : bytecodeIndex(invalidBytecodeIndex
)
66 , inlineCallFrame(deletedMarker())
70 explicit CodeOrigin(unsigned bytecodeIndex
, InlineCallFrame
* inlineCallFrame
= 0)
71 : bytecodeIndex(bytecodeIndex
)
72 , inlineCallFrame(inlineCallFrame
)
74 ASSERT(bytecodeIndex
< invalidBytecodeIndex
);
77 bool isSet() const { return bytecodeIndex
!= invalidBytecodeIndex
; }
78 bool operator!() const { return !isSet(); }
80 bool isHashTableDeletedValue() const
82 return bytecodeIndex
== invalidBytecodeIndex
&& !!inlineCallFrame
;
85 // The inline depth is the depth of the inline stack, so 1 = not inlined,
86 // 2 = inlined one deep, etc.
87 unsigned inlineDepth() const;
89 // If the code origin corresponds to inlined code, gives you the heap object that
90 // would have owned the code if it had not been inlined. Otherwise returns 0.
91 ScriptExecutable
* codeOriginOwner() const;
93 int stackOffset() const;
95 static unsigned inlineDepthForCallFrame(InlineCallFrame
*);
97 unsigned hash() const;
98 bool operator==(const CodeOrigin
& other
) const;
99 bool operator!=(const CodeOrigin
& other
) const { return !(*this == other
); }
101 // This checks if the two code origins correspond to the same stack trace snippets,
102 // but ignore whether the InlineCallFrame's are identical.
103 bool isApproximatelyEqualTo(const CodeOrigin
& other
) const;
105 unsigned approximateHash() const;
107 // Get the inline stack. This is slow, and is intended for debugging only.
108 Vector
<CodeOrigin
> inlineStack() const;
110 void dump(PrintStream
&) const;
111 void dumpInContext(PrintStream
&, DumpContext
*) const;
114 static InlineCallFrame
* deletedMarker()
116 return bitwise_cast
<InlineCallFrame
*>(static_cast<uintptr_t>(1));
120 struct InlineCallFrame
{
121 Vector
<ValueRecovery
> arguments
; // Includes 'this'.
122 WriteBarrier
<ScriptExecutable
> executable
;
123 ValueRecovery calleeRecovery
;
125 BitVector capturedVars
; // Indexed by the machine call frame's variable numbering.
126 signed stackOffset
: 30;
128 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.
129 VirtualRegister argumentsRegister
; // This is only set if the code uses arguments. The unmodified arguments register follows the unmodifiedArgumentsRegister() convention (see CodeBlock.h).
131 // There is really no good notion of a "default" set of values for
132 // InlineCallFrame's fields. This constructor is here just to reduce confusion if
133 // we forgot to initialize explicitly.
137 , isClosureCall(false)
141 CodeSpecializationKind
specializationKind() const { return specializationFromIsCall(isCall
); }
143 JSFunction
* calleeConstant() const
145 if (calleeRecovery
.isConstant())
146 return jsCast
<JSFunction
*>(calleeRecovery
.constant());
150 void visitAggregate(SlotVisitor
& visitor
)
152 visitor
.append(&executable
);
155 // Get the callee given a machine call frame to which this InlineCallFrame belongs.
156 JSFunction
* calleeForCallFrame(ExecState
*) const;
158 CString
inferredName() const;
159 CodeBlockHash
hash() const;
160 CString
hashAsStringIfPossible() const;
162 CodeBlock
* baselineCodeBlock() const;
164 ptrdiff_t callerFrameOffset() const { return stackOffset
* sizeof(Register
) + CallFrame::callerFrameOffset(); }
165 ptrdiff_t returnPCOffset() const { return stackOffset
* sizeof(Register
) + CallFrame::returnPCOffset(); }
167 void dumpBriefFunctionInformation(PrintStream
&) const;
168 void dump(PrintStream
&) const;
169 void dumpInContext(PrintStream
&, DumpContext
*) const;
171 MAKE_PRINT_METHOD(InlineCallFrame
, dumpBriefFunctionInformation
, briefFunctionInformation
);
174 inline int CodeOrigin::stackOffset() const
176 if (!inlineCallFrame
)
179 return inlineCallFrame
->stackOffset
;
182 inline unsigned CodeOrigin::hash() const
184 return WTF::IntHash
<unsigned>::hash(bytecodeIndex
) +
185 WTF::PtrHash
<InlineCallFrame
*>::hash(inlineCallFrame
);
188 inline bool CodeOrigin::operator==(const CodeOrigin
& other
) const
190 return bytecodeIndex
== other
.bytecodeIndex
191 && inlineCallFrame
== other
.inlineCallFrame
;
194 inline ScriptExecutable
* CodeOrigin::codeOriginOwner() const
196 if (!inlineCallFrame
)
198 return inlineCallFrame
->executable
.get();
201 struct CodeOriginHash
{
202 static unsigned hash(const CodeOrigin
& key
) { return key
.hash(); }
203 static bool equal(const CodeOrigin
& a
, const CodeOrigin
& b
) { return a
== b
; }
204 static const bool safeToCompareToEmptyOrDeleted
= true;
207 struct CodeOriginApproximateHash
{
208 static unsigned hash(const CodeOrigin
& key
) { return key
.approximateHash(); }
209 static bool equal(const CodeOrigin
& a
, const CodeOrigin
& b
) { return a
.isApproximatelyEqualTo(b
); }
210 static const bool safeToCompareToEmptyOrDeleted
= true;
217 template<typename T
> struct DefaultHash
;
218 template<> struct DefaultHash
<JSC::CodeOrigin
> {
219 typedef JSC::CodeOriginHash Hash
;
222 template<typename T
> struct HashTraits
;
223 template<> struct HashTraits
<JSC::CodeOrigin
> : SimpleClassHashTraits
<JSC::CodeOrigin
> {
224 static const bool emptyValueIsZero
= false;
229 #endif // CodeOrigin_h