fpizlo@apple.com | 5098c8b | 2011-10-14 22:01:10 +0000 | [diff] [blame] | 1 | /* |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 2 | * Copyright (C) 2011, 2012, 2013, 2014 Apple Inc. All rights reserved. |
fpizlo@apple.com | 5098c8b | 2011-10-14 22:01:10 +0000 | [diff] [blame] | 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 | |
fpizlo@apple.com | 0bfcc38 | 2012-11-30 03:42:29 +0000 | [diff] [blame] | 29 | #include "CodeBlockHash.h" |
| 30 | #include "CodeSpecializationKind.h" |
fpizlo@apple.com | a62d482 | 2013-10-06 04:22:43 +0000 | [diff] [blame] | 31 | #include "JSFunction.h" |
fpizlo@apple.com | 116a089 | 2011-11-03 08:06:42 +0000 | [diff] [blame] | 32 | #include "ValueRecovery.h" |
fpizlo@apple.com | d9ded3b | 2011-10-22 01:22:46 +0000 | [diff] [blame] | 33 | #include "WriteBarrier.h" |
fpizlo@apple.com | 1688cc1 | 2012-05-23 07:29:02 +0000 | [diff] [blame] | 34 | #include <wtf/BitVector.h> |
fpizlo@apple.com | d49bfe8 | 2013-10-19 02:20:14 +0000 | [diff] [blame] | 35 | #include <wtf/HashMap.h> |
fpizlo@apple.com | 20d4624 | 2012-11-30 21:56:24 +0000 | [diff] [blame] | 36 | #include <wtf/PrintStream.h> |
fpizlo@apple.com | 5098c8b | 2011-10-14 22:01:10 +0000 | [diff] [blame] | 37 | #include <wtf/StdLibExtras.h> |
fpizlo@apple.com | d9ded3b | 2011-10-22 01:22:46 +0000 | [diff] [blame] | 38 | #include <wtf/Vector.h> |
fpizlo@apple.com | 5098c8b | 2011-10-14 22:01:10 +0000 | [diff] [blame] | 39 | |
| 40 | namespace JSC { |
| 41 | |
| 42 | struct InlineCallFrame; |
fpizlo@apple.com | 608ecbe | 2013-01-08 22:02:04 +0000 | [diff] [blame] | 43 | class ExecState; |
oliver@apple.com | e2fe4ce | 2013-07-25 03:59:41 +0000 | [diff] [blame] | 44 | class ScriptExecutable; |
fpizlo@apple.com | 39edeaf | 2011-10-30 22:47:31 +0000 | [diff] [blame] | 45 | class JSFunction; |
fpizlo@apple.com | 5098c8b | 2011-10-14 22:01:10 +0000 | [diff] [blame] | 46 | |
| 47 | struct CodeOrigin { |
oliver@apple.com | bc2930b | 2013-07-25 04:01:48 +0000 | [diff] [blame] | 48 | static const unsigned invalidBytecodeIndex = UINT_MAX; |
fpizlo@apple.com | 5e13577 | 2012-07-12 00:12:03 +0000 | [diff] [blame] | 49 | |
oliver@apple.com | bc2930b | 2013-07-25 04:01:48 +0000 | [diff] [blame] | 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; |
fpizlo@apple.com | afcf904 | 2012-01-20 20:22:18 +0000 | [diff] [blame] | 55 | |
fpizlo@apple.com | 5098c8b | 2011-10-14 22:01:10 +0000 | [diff] [blame] | 56 | InlineCallFrame* inlineCallFrame; |
| 57 | |
| 58 | CodeOrigin() |
oliver@apple.com | bc2930b | 2013-07-25 04:01:48 +0000 | [diff] [blame] | 59 | : bytecodeIndex(invalidBytecodeIndex) |
fpizlo@apple.com | 5098c8b | 2011-10-14 22:01:10 +0000 | [diff] [blame] | 60 | , inlineCallFrame(0) |
| 61 | { |
| 62 | } |
| 63 | |
fpizlo@apple.com | d49bfe8 | 2013-10-19 02:20:14 +0000 | [diff] [blame] | 64 | CodeOrigin(WTF::HashTableDeletedValueType) |
| 65 | : bytecodeIndex(invalidBytecodeIndex) |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 66 | , inlineCallFrame(deletedMarker()) |
fpizlo@apple.com | d49bfe8 | 2013-10-19 02:20:14 +0000 | [diff] [blame] | 67 | { |
| 68 | } |
| 69 | |
oliver@apple.com | bc2930b | 2013-07-25 04:01:48 +0000 | [diff] [blame] | 70 | explicit CodeOrigin(unsigned bytecodeIndex, InlineCallFrame* inlineCallFrame = 0) |
fpizlo@apple.com | 5098c8b | 2011-10-14 22:01:10 +0000 | [diff] [blame] | 71 | : bytecodeIndex(bytecodeIndex) |
fpizlo@apple.com | 41f5c95 | 2011-10-17 23:54:41 +0000 | [diff] [blame] | 72 | , inlineCallFrame(inlineCallFrame) |
| 73 | { |
oliver@apple.com | bc2930b | 2013-07-25 04:01:48 +0000 | [diff] [blame] | 74 | ASSERT(bytecodeIndex < invalidBytecodeIndex); |
fpizlo@apple.com | 41f5c95 | 2011-10-17 23:54:41 +0000 | [diff] [blame] | 75 | } |
| 76 | |
oliver@apple.com | bc2930b | 2013-07-25 04:01:48 +0000 | [diff] [blame] | 77 | bool isSet() const { return bytecodeIndex != invalidBytecodeIndex; } |
fpizlo@apple.com | d4a77bb | 2014-04-12 18:22:27 +0000 | [diff] [blame] | 78 | bool operator!() const { return !isSet(); } |
fpizlo@apple.com | afcf904 | 2012-01-20 20:22:18 +0000 | [diff] [blame] | 79 | |
fpizlo@apple.com | d49bfe8 | 2013-10-19 02:20:14 +0000 | [diff] [blame] | 80 | bool isHashTableDeletedValue() const |
| 81 | { |
| 82 | return bytecodeIndex == invalidBytecodeIndex && !!inlineCallFrame; |
| 83 | } |
| 84 | |
fpizlo@apple.com | d9ded3b | 2011-10-22 01:22:46 +0000 | [diff] [blame] | 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; |
| 88 | |
fpizlo@apple.com | 5e37f9a | 2011-11-17 00:49:50 +0000 | [diff] [blame] | 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. |
oliver@apple.com | e2fe4ce | 2013-07-25 03:59:41 +0000 | [diff] [blame] | 91 | ScriptExecutable* codeOriginOwner() const; |
fpizlo@apple.com | 5e37f9a | 2011-11-17 00:49:50 +0000 | [diff] [blame] | 92 | |
msaboff@apple.com | b70e41b | 2013-09-13 18:03:55 +0000 | [diff] [blame] | 93 | int stackOffset() const; |
fpizlo@apple.com | 439e9e5 | 2013-01-02 23:54:42 +0000 | [diff] [blame] | 94 | |
fpizlo@apple.com | d9ded3b | 2011-10-22 01:22:46 +0000 | [diff] [blame] | 95 | static unsigned inlineDepthForCallFrame(InlineCallFrame*); |
| 96 | |
fpizlo@apple.com | d49bfe8 | 2013-10-19 02:20:14 +0000 | [diff] [blame] | 97 | unsigned hash() const; |
fpizlo@apple.com | d9ded3b | 2011-10-22 01:22:46 +0000 | [diff] [blame] | 98 | bool operator==(const CodeOrigin& other) const; |
fpizlo@apple.com | 769d8d3 | 2011-12-14 01:46:36 +0000 | [diff] [blame] | 99 | bool operator!=(const CodeOrigin& other) const { return !(*this == other); } |
| 100 | |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 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; |
| 104 | |
| 105 | unsigned approximateHash() const; |
| 106 | |
fpizlo@apple.com | d9ded3b | 2011-10-22 01:22:46 +0000 | [diff] [blame] | 107 | // Get the inline stack. This is slow, and is intended for debugging only. |
| 108 | Vector<CodeOrigin> inlineStack() const; |
fpizlo@apple.com | 20d4624 | 2012-11-30 21:56:24 +0000 | [diff] [blame] | 109 | |
| 110 | void dump(PrintStream&) const; |
oliver@apple.com | 237b146 | 2013-07-25 04:05:36 +0000 | [diff] [blame] | 111 | void dumpInContext(PrintStream&, DumpContext*) const; |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 112 | |
| 113 | private: |
| 114 | static InlineCallFrame* deletedMarker() |
| 115 | { |
| 116 | return bitwise_cast<InlineCallFrame*>(static_cast<uintptr_t>(1)); |
| 117 | } |
fpizlo@apple.com | 5098c8b | 2011-10-14 22:01:10 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | struct InlineCallFrame { |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 121 | enum Kind { |
| 122 | Call, |
| 123 | Construct, |
| 124 | |
| 125 | // For these, the stackOffset incorporates the argument count plus the true return PC |
| 126 | // slot. |
| 127 | GetterCall, |
| 128 | SetterCall |
| 129 | }; |
| 130 | |
| 131 | static Kind kindFor(CodeSpecializationKind kind) |
| 132 | { |
| 133 | switch (kind) { |
| 134 | case CodeForCall: |
| 135 | return Call; |
| 136 | case CodeForConstruct: |
| 137 | return Construct; |
| 138 | } |
| 139 | RELEASE_ASSERT_NOT_REACHED(); |
| 140 | return Call; |
| 141 | } |
| 142 | |
| 143 | static CodeSpecializationKind specializationKindFor(Kind kind) |
| 144 | { |
| 145 | switch (kind) { |
| 146 | case Call: |
| 147 | case GetterCall: |
| 148 | case SetterCall: |
| 149 | return CodeForCall; |
| 150 | case Construct: |
| 151 | return CodeForConstruct; |
| 152 | } |
| 153 | RELEASE_ASSERT_NOT_REACHED(); |
| 154 | return CodeForCall; |
| 155 | } |
| 156 | |
fpizlo@apple.com | 29abafe | 2014-08-28 19:09:48 +0000 | [diff] [blame^] | 157 | static bool isNormalCall(Kind kind) |
| 158 | { |
| 159 | switch (kind) { |
| 160 | case Call: |
| 161 | case Construct: |
| 162 | return true; |
| 163 | default: |
| 164 | return false; |
| 165 | } |
| 166 | } |
| 167 | |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 168 | Vector<ValueRecovery> arguments; // Includes 'this'. |
oliver@apple.com | e2fe4ce | 2013-07-25 03:59:41 +0000 | [diff] [blame] | 169 | WriteBarrier<ScriptExecutable> executable; |
fpizlo@apple.com | a62d482 | 2013-10-06 04:22:43 +0000 | [diff] [blame] | 170 | ValueRecovery calleeRecovery; |
fpizlo@apple.com | 5098c8b | 2011-10-14 22:01:10 +0000 | [diff] [blame] | 171 | CodeOrigin caller; |
fpizlo@apple.com | 1688cc1 | 2012-05-23 07:29:02 +0000 | [diff] [blame] | 172 | BitVector capturedVars; // Indexed by the machine call frame's variable numbering. |
mark.lam@apple.com | b2a4eb7 | 2014-08-22 05:30:02 +0000 | [diff] [blame] | 173 | |
| 174 | signed stackOffset : 29; |
commit-queue@webkit.org | cb8934b | 2014-08-20 18:11:15 +0000 | [diff] [blame] | 175 | unsigned kind : 2; // real type is Kind |
fpizlo@apple.com | a62d482 | 2013-10-06 04:22:43 +0000 | [diff] [blame] | 176 | 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. |
| 177 | VirtualRegister argumentsRegister; // This is only set if the code uses arguments. The unmodified arguments register follows the unmodifiedArgumentsRegister() convention (see CodeBlock.h). |
fpizlo@apple.com | 0bfcc38 | 2012-11-30 03:42:29 +0000 | [diff] [blame] | 178 | |
fpizlo@apple.com | 48a7bc8 | 2013-09-23 22:09:24 +0000 | [diff] [blame] | 179 | // There is really no good notion of a "default" set of values for |
| 180 | // InlineCallFrame's fields. This constructor is here just to reduce confusion if |
| 181 | // we forgot to initialize explicitly. |
| 182 | InlineCallFrame() |
| 183 | : stackOffset(0) |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 184 | , kind(Call) |
fpizlo@apple.com | a62d482 | 2013-10-06 04:22:43 +0000 | [diff] [blame] | 185 | , isClosureCall(false) |
fpizlo@apple.com | 48a7bc8 | 2013-09-23 22:09:24 +0000 | [diff] [blame] | 186 | { |
| 187 | } |
| 188 | |
commit-queue@webkit.org | cb8934b | 2014-08-20 18:11:15 +0000 | [diff] [blame] | 189 | CodeSpecializationKind specializationKind() const { return specializationKindFor(static_cast<Kind>(kind)); } |
fpizlo@apple.com | a62d482 | 2013-10-06 04:22:43 +0000 | [diff] [blame] | 190 | |
| 191 | JSFunction* calleeConstant() const |
| 192 | { |
| 193 | if (calleeRecovery.isConstant()) |
| 194 | return jsCast<JSFunction*>(calleeRecovery.constant()); |
| 195 | return 0; |
| 196 | } |
ggaren@apple.com | de69654 | 2014-08-01 20:17:25 +0000 | [diff] [blame] | 197 | |
| 198 | void visitAggregate(SlotVisitor& visitor) |
| 199 | { |
| 200 | visitor.append(&executable); |
| 201 | } |
fpizlo@apple.com | 439e9e5 | 2013-01-02 23:54:42 +0000 | [diff] [blame] | 202 | |
fpizlo@apple.com | 608ecbe | 2013-01-08 22:02:04 +0000 | [diff] [blame] | 203 | // Get the callee given a machine call frame to which this InlineCallFrame belongs. |
| 204 | JSFunction* calleeForCallFrame(ExecState*) const; |
| 205 | |
oliver@apple.com | 0203946 | 2013-07-25 03:59:29 +0000 | [diff] [blame] | 206 | CString inferredName() const; |
fpizlo@apple.com | 0bfcc38 | 2012-11-30 03:42:29 +0000 | [diff] [blame] | 207 | CodeBlockHash hash() const; |
fpizlo@apple.com | f285f71 | 2014-03-13 01:50:41 +0000 | [diff] [blame] | 208 | CString hashAsStringIfPossible() const; |
fpizlo@apple.com | 20d4624 | 2012-11-30 21:56:24 +0000 | [diff] [blame] | 209 | |
fpizlo@apple.com | 4a81fa4 | 2012-12-05 01:26:13 +0000 | [diff] [blame] | 210 | CodeBlock* baselineCodeBlock() const; |
| 211 | |
mark.lam@apple.com | b2a4eb7 | 2014-08-22 05:30:02 +0000 | [diff] [blame] | 212 | void setStackOffset(signed offset) |
| 213 | { |
| 214 | stackOffset = offset; |
| 215 | RELEASE_ASSERT(static_cast<signed>(stackOffset) == offset); |
| 216 | } |
| 217 | |
mark.lam@apple.com | c8151c4 | 2013-10-30 21:26:46 +0000 | [diff] [blame] | 218 | ptrdiff_t callerFrameOffset() const { return stackOffset * sizeof(Register) + CallFrame::callerFrameOffset(); } |
| 219 | ptrdiff_t returnPCOffset() const { return stackOffset * sizeof(Register) + CallFrame::returnPCOffset(); } |
| 220 | |
fpizlo@apple.com | 806b582 | 2013-01-08 01:23:38 +0000 | [diff] [blame] | 221 | void dumpBriefFunctionInformation(PrintStream&) const; |
fpizlo@apple.com | 20d4624 | 2012-11-30 21:56:24 +0000 | [diff] [blame] | 222 | void dump(PrintStream&) const; |
oliver@apple.com | 237b146 | 2013-07-25 04:05:36 +0000 | [diff] [blame] | 223 | void dumpInContext(PrintStream&, DumpContext*) const; |
fpizlo@apple.com | 806b582 | 2013-01-08 01:23:38 +0000 | [diff] [blame] | 224 | |
| 225 | MAKE_PRINT_METHOD(InlineCallFrame, dumpBriefFunctionInformation, briefFunctionInformation); |
fpizlo@apple.com | 5098c8b | 2011-10-14 22:01:10 +0000 | [diff] [blame] | 226 | }; |
| 227 | |
msaboff@apple.com | b70e41b | 2013-09-13 18:03:55 +0000 | [diff] [blame] | 228 | inline int CodeOrigin::stackOffset() const |
fpizlo@apple.com | 439e9e5 | 2013-01-02 23:54:42 +0000 | [diff] [blame] | 229 | { |
| 230 | if (!inlineCallFrame) |
| 231 | return 0; |
| 232 | |
| 233 | return inlineCallFrame->stackOffset; |
| 234 | } |
| 235 | |
fpizlo@apple.com | d49bfe8 | 2013-10-19 02:20:14 +0000 | [diff] [blame] | 236 | inline unsigned CodeOrigin::hash() const |
| 237 | { |
| 238 | return WTF::IntHash<unsigned>::hash(bytecodeIndex) + |
| 239 | WTF::PtrHash<InlineCallFrame*>::hash(inlineCallFrame); |
| 240 | } |
| 241 | |
fpizlo@apple.com | d9ded3b | 2011-10-22 01:22:46 +0000 | [diff] [blame] | 242 | inline bool CodeOrigin::operator==(const CodeOrigin& other) const |
| 243 | { |
| 244 | return bytecodeIndex == other.bytecodeIndex |
| 245 | && inlineCallFrame == other.inlineCallFrame; |
| 246 | } |
| 247 | |
oliver@apple.com | e2fe4ce | 2013-07-25 03:59:41 +0000 | [diff] [blame] | 248 | inline ScriptExecutable* CodeOrigin::codeOriginOwner() const |
fpizlo@apple.com | 5e37f9a | 2011-11-17 00:49:50 +0000 | [diff] [blame] | 249 | { |
| 250 | if (!inlineCallFrame) |
| 251 | return 0; |
| 252 | return inlineCallFrame->executable.get(); |
| 253 | } |
| 254 | |
fpizlo@apple.com | d49bfe8 | 2013-10-19 02:20:14 +0000 | [diff] [blame] | 255 | struct CodeOriginHash { |
| 256 | static unsigned hash(const CodeOrigin& key) { return key.hash(); } |
| 257 | static bool equal(const CodeOrigin& a, const CodeOrigin& b) { return a == b; } |
| 258 | static const bool safeToCompareToEmptyOrDeleted = true; |
| 259 | }; |
| 260 | |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 261 | struct CodeOriginApproximateHash { |
| 262 | static unsigned hash(const CodeOrigin& key) { return key.approximateHash(); } |
| 263 | static bool equal(const CodeOrigin& a, const CodeOrigin& b) { return a.isApproximatelyEqualTo(b); } |
| 264 | static const bool safeToCompareToEmptyOrDeleted = true; |
| 265 | }; |
| 266 | |
fpizlo@apple.com | 5098c8b | 2011-10-14 22:01:10 +0000 | [diff] [blame] | 267 | } // namespace JSC |
| 268 | |
fpizlo@apple.com | d49bfe8 | 2013-10-19 02:20:14 +0000 | [diff] [blame] | 269 | namespace WTF { |
| 270 | |
fpizlo@apple.com | 4c6b8ad | 2014-07-22 21:08:50 +0000 | [diff] [blame] | 271 | void printInternal(PrintStream&, JSC::InlineCallFrame::Kind); |
| 272 | |
fpizlo@apple.com | d49bfe8 | 2013-10-19 02:20:14 +0000 | [diff] [blame] | 273 | template<typename T> struct DefaultHash; |
| 274 | template<> struct DefaultHash<JSC::CodeOrigin> { |
| 275 | typedef JSC::CodeOriginHash Hash; |
| 276 | }; |
| 277 | |
| 278 | template<typename T> struct HashTraits; |
| 279 | template<> struct HashTraits<JSC::CodeOrigin> : SimpleClassHashTraits<JSC::CodeOrigin> { |
| 280 | static const bool emptyValueIsZero = false; |
| 281 | }; |
| 282 | |
| 283 | } // namespace WTF |
| 284 | |
fpizlo@apple.com | 5098c8b | 2011-10-14 22:01:10 +0000 | [diff] [blame] | 285 | #endif // CodeOrigin_h |
| 286 | |