fpizlo@apple.com | 0bfcc38 | 2012-11-30 03:42:29 +0000 | [diff] [blame] | 1 | /* |
fpizlo@apple.com | ed2da80 | 2018-07-22 02:48:16 +0000 | [diff] [blame] | 2 | * Copyright (C) 2012-2018 Apple Inc. All rights reserved. |
fpizlo@apple.com | 0bfcc38 | 2012-11-30 03:42:29 +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 | #include "config.h" |
| 27 | #include "CodeOrigin.h" |
| 28 | |
fpizlo@apple.com | 4a81fa4 | 2012-12-05 01:26:13 +0000 | [diff] [blame] | 29 | #include "CodeBlock.h" |
ggaren@apple.com | 21cd702 | 2015-08-18 18:28:54 +0000 | [diff] [blame] | 30 | #include "InlineCallFrame.h" |
fpizlo@apple.com | 0bfcc38 | 2012-11-30 03:42:29 +0000 | [diff] [blame] | 31 | |
| 32 | namespace JSC { |
| 33 | |
fpizlo@apple.com | 0bfcc38 | 2012-11-30 03:42:29 +0000 | [diff] [blame] | 34 | unsigned CodeOrigin::inlineDepth() const |
| 35 | { |
rmorisset@apple.com | b4811e7 | 2019-03-20 20:24:36 +0000 | [diff] [blame] | 36 | unsigned result = 1; |
| 37 | for (InlineCallFrame* current = inlineCallFrame(); current; current = current->directCaller.inlineCallFrame()) |
| 38 | result++; |
| 39 | return result; |
fpizlo@apple.com | 0bfcc38 | 2012-11-30 03:42:29 +0000 | [diff] [blame] | 40 | } |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 41 | |
fpizlo@apple.com | ed2da80 | 2018-07-22 02:48:16 +0000 | [diff] [blame] | 42 | bool CodeOrigin::isApproximatelyEqualTo(const CodeOrigin& other, InlineCallFrame* terminal) const |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 43 | { |
| 44 | CodeOrigin a = *this; |
| 45 | CodeOrigin b = other; |
| 46 | |
| 47 | if (!a.isSet()) |
| 48 | return !b.isSet(); |
| 49 | if (!b.isSet()) |
| 50 | return false; |
fpizlo@apple.com | 0bfcc38 | 2012-11-30 03:42:29 +0000 | [diff] [blame] | 51 | |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 52 | if (a.isHashTableDeletedValue()) |
| 53 | return b.isHashTableDeletedValue(); |
| 54 | if (b.isHashTableDeletedValue()) |
| 55 | return false; |
| 56 | |
| 57 | for (;;) { |
| 58 | ASSERT(a.isSet()); |
| 59 | ASSERT(b.isSet()); |
| 60 | |
rmorisset@apple.com | b4811e7 | 2019-03-20 20:24:36 +0000 | [diff] [blame] | 61 | if (a.bytecodeIndex() != b.bytecodeIndex()) |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 62 | return false; |
rmorisset@apple.com | b4811e7 | 2019-03-20 20:24:36 +0000 | [diff] [blame] | 63 | |
| 64 | auto* aInlineCallFrame = a.inlineCallFrame(); |
| 65 | auto* bInlineCallFrame = b.inlineCallFrame(); |
| 66 | bool aHasInlineCallFrame = !!aInlineCallFrame && aInlineCallFrame != terminal; |
| 67 | bool bHasInlineCallFrame = !!bInlineCallFrame; |
fpizlo@apple.com | ed2da80 | 2018-07-22 02:48:16 +0000 | [diff] [blame] | 68 | if (aHasInlineCallFrame != bHasInlineCallFrame) |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 69 | return false; |
| 70 | |
fpizlo@apple.com | ed2da80 | 2018-07-22 02:48:16 +0000 | [diff] [blame] | 71 | if (!aHasInlineCallFrame) |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 72 | return true; |
| 73 | |
rmorisset@apple.com | b4811e7 | 2019-03-20 20:24:36 +0000 | [diff] [blame] | 74 | if (aInlineCallFrame->baselineCodeBlock.get() != bInlineCallFrame->baselineCodeBlock.get()) |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 75 | return false; |
| 76 | |
rmorisset@apple.com | b4811e7 | 2019-03-20 20:24:36 +0000 | [diff] [blame] | 77 | a = aInlineCallFrame->directCaller; |
| 78 | b = bInlineCallFrame->directCaller; |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
fpizlo@apple.com | ed2da80 | 2018-07-22 02:48:16 +0000 | [diff] [blame] | 82 | unsigned CodeOrigin::approximateHash(InlineCallFrame* terminal) const |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 83 | { |
| 84 | if (!isSet()) |
| 85 | return 0; |
| 86 | if (isHashTableDeletedValue()) |
| 87 | return 1; |
| 88 | |
| 89 | unsigned result = 2; |
| 90 | CodeOrigin codeOrigin = *this; |
| 91 | for (;;) { |
keith_miller@apple.com | 0f985ec | 2019-10-23 00:55:38 +0000 | [diff] [blame] | 92 | result += codeOrigin.bytecodeIndex().asBits(); |
rmorisset@apple.com | b4811e7 | 2019-03-20 20:24:36 +0000 | [diff] [blame] | 93 | |
| 94 | auto* inlineCallFrame = codeOrigin.inlineCallFrame(); |
| 95 | |
| 96 | if (!inlineCallFrame) |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 97 | return result; |
| 98 | |
rmorisset@apple.com | b4811e7 | 2019-03-20 20:24:36 +0000 | [diff] [blame] | 99 | if (inlineCallFrame == terminal) |
fpizlo@apple.com | ed2da80 | 2018-07-22 02:48:16 +0000 | [diff] [blame] | 100 | return result; |
| 101 | |
rmorisset@apple.com | b4811e7 | 2019-03-20 20:24:36 +0000 | [diff] [blame] | 102 | result += WTF::PtrHash<JSCell*>::hash(inlineCallFrame->baselineCodeBlock.get()); |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 103 | |
rmorisset@apple.com | b4811e7 | 2019-03-20 20:24:36 +0000 | [diff] [blame] | 104 | codeOrigin = inlineCallFrame->directCaller; |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
fpizlo@apple.com | 0bfcc38 | 2012-11-30 03:42:29 +0000 | [diff] [blame] | 108 | Vector<CodeOrigin> CodeOrigin::inlineStack() const |
| 109 | { |
| 110 | Vector<CodeOrigin> result(inlineDepth()); |
| 111 | result.last() = *this; |
| 112 | unsigned index = result.size() - 2; |
rmorisset@apple.com | b4811e7 | 2019-03-20 20:24:36 +0000 | [diff] [blame] | 113 | for (InlineCallFrame* current = inlineCallFrame(); current; current = current->directCaller.inlineCallFrame()) |
msaboff@apple.com | a3dc753 | 2015-09-24 21:42:59 +0000 | [diff] [blame] | 114 | result[index--] = current->directCaller; |
rmorisset@apple.com | b4811e7 | 2019-03-20 20:24:36 +0000 | [diff] [blame] | 115 | RELEASE_ASSERT(!result[0].inlineCallFrame()); |
fpizlo@apple.com | 0bfcc38 | 2012-11-30 03:42:29 +0000 | [diff] [blame] | 116 | return result; |
| 117 | } |
| 118 | |
ggaren@apple.com | 81def5f | 2015-10-09 23:10:16 +0000 | [diff] [blame] | 119 | CodeBlock* CodeOrigin::codeOriginOwner() const |
ggaren@apple.com | 21cd702 | 2015-08-18 18:28:54 +0000 | [diff] [blame] | 120 | { |
rmorisset@apple.com | b4811e7 | 2019-03-20 20:24:36 +0000 | [diff] [blame] | 121 | auto* inlineCallFrame = this->inlineCallFrame(); |
ggaren@apple.com | 21cd702 | 2015-08-18 18:28:54 +0000 | [diff] [blame] | 122 | if (!inlineCallFrame) |
rmorisset@apple.com | b4811e7 | 2019-03-20 20:24:36 +0000 | [diff] [blame] | 123 | return nullptr; |
ggaren@apple.com | 81def5f | 2015-10-09 23:10:16 +0000 | [diff] [blame] | 124 | return inlineCallFrame->baselineCodeBlock.get(); |
ggaren@apple.com | 21cd702 | 2015-08-18 18:28:54 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | int CodeOrigin::stackOffset() const |
| 128 | { |
rmorisset@apple.com | b4811e7 | 2019-03-20 20:24:36 +0000 | [diff] [blame] | 129 | auto* inlineCallFrame = this->inlineCallFrame(); |
ggaren@apple.com | 21cd702 | 2015-08-18 18:28:54 +0000 | [diff] [blame] | 130 | if (!inlineCallFrame) |
| 131 | return 0; |
ggaren@apple.com | 21cd702 | 2015-08-18 18:28:54 +0000 | [diff] [blame] | 132 | return inlineCallFrame->stackOffset; |
| 133 | } |
| 134 | |
fpizlo@apple.com | 20d4624 | 2012-11-30 21:56:24 +0000 | [diff] [blame] | 135 | void CodeOrigin::dump(PrintStream& out) const |
| 136 | { |
fpizlo@apple.com | 50f0628 | 2013-12-10 05:52:24 +0000 | [diff] [blame] | 137 | if (!isSet()) { |
| 138 | out.print("<none>"); |
| 139 | return; |
| 140 | } |
| 141 | |
fpizlo@apple.com | 20d4624 | 2012-11-30 21:56:24 +0000 | [diff] [blame] | 142 | Vector<CodeOrigin> stack = inlineStack(); |
| 143 | for (unsigned i = 0; i < stack.size(); ++i) { |
| 144 | if (i) |
| 145 | out.print(" --> "); |
| 146 | |
rmorisset@apple.com | b4811e7 | 2019-03-20 20:24:36 +0000 | [diff] [blame] | 147 | if (InlineCallFrame* frame = stack[i].inlineCallFrame()) { |
ggaren@apple.com | 81def5f | 2015-10-09 23:10:16 +0000 | [diff] [blame] | 148 | out.print(frame->briefFunctionInformation(), ":<", RawPointer(frame->baselineCodeBlock.get()), "> "); |
fpizlo@apple.com | a62d482 | 2013-10-06 04:22:43 +0000 | [diff] [blame] | 149 | if (frame->isClosureCall) |
fpizlo@apple.com | 806b582 | 2013-01-08 01:23:38 +0000 | [diff] [blame] | 150 | out.print("(closure) "); |
| 151 | } |
fpizlo@apple.com | 20d4624 | 2012-11-30 21:56:24 +0000 | [diff] [blame] | 152 | |
keith_miller@apple.com | 0f985ec | 2019-10-23 00:55:38 +0000 | [diff] [blame] | 153 | out.print(stack[i].bytecodeIndex()); |
fpizlo@apple.com | 20d4624 | 2012-11-30 21:56:24 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
oliver@apple.com | 237b146 | 2013-07-25 04:05:36 +0000 | [diff] [blame] | 157 | void CodeOrigin::dumpInContext(PrintStream& out, DumpContext*) const |
| 158 | { |
| 159 | dump(out); |
| 160 | } |
| 161 | |
fpizlo@apple.com | 0bfcc38 | 2012-11-30 03:42:29 +0000 | [diff] [blame] | 162 | } // namespace JSC |