]> git.saurik.com Git - apple/javascriptcore.git/blame_incremental - interpreter/CallFrame.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / interpreter / CallFrame.cpp
... / ...
CommitLineData
1/*
2 * Copyright (C) 2008, 2013, 2014 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#include "config.h"
27#include "CallFrame.h"
28
29#include "CallFrameInlines.h"
30#include "CodeBlock.h"
31#include "Interpreter.h"
32#include "JSLexicalEnvironment.h"
33#include "JSCInlines.h"
34#include "VMEntryScope.h"
35#include <wtf/StringPrintStream.h>
36
37namespace JSC {
38
39#ifndef NDEBUG
40JSStack* CallFrame::stack()
41{
42 return &interpreter()->stack();
43}
44
45#endif
46
47#if USE(JSVALUE32_64)
48unsigned CallFrame::locationAsBytecodeOffset() const
49{
50 ASSERT(codeBlock());
51 ASSERT(hasLocationAsBytecodeOffset());
52 return currentVPC() - codeBlock()->instructions().begin();
53}
54
55void CallFrame::setLocationAsBytecodeOffset(unsigned offset)
56{
57 ASSERT(codeBlock());
58 setCurrentVPC(codeBlock()->instructions().begin() + offset);
59 ASSERT(hasLocationAsBytecodeOffset());
60}
61#else
62Instruction* CallFrame::currentVPC() const
63{
64 return codeBlock()->instructions().begin() + locationAsBytecodeOffset();
65}
66void CallFrame::setCurrentVPC(Instruction* vpc)
67{
68 setLocationAsBytecodeOffset(vpc - codeBlock()->instructions().begin());
69}
70#endif
71
72#if ENABLE(DFG_JIT)
73unsigned CallFrame::bytecodeOffsetFromCodeOriginIndex()
74{
75 ASSERT(hasLocationAsCodeOriginIndex());
76 CodeBlock* codeBlock = this->codeBlock();
77 ASSERT(codeBlock);
78
79 CodeOrigin codeOrigin;
80 unsigned index = locationAsCodeOriginIndex();
81 ASSERT(codeBlock->canGetCodeOrigin(index));
82 codeOrigin = codeBlock->codeOrigin(index);
83
84 for (InlineCallFrame* inlineCallFrame = codeOrigin.inlineCallFrame; inlineCallFrame;) {
85 if (inlineCallFrame->baselineCodeBlock() == codeBlock)
86 return codeOrigin.bytecodeIndex;
87
88 codeOrigin = inlineCallFrame->caller;
89 inlineCallFrame = codeOrigin.inlineCallFrame;
90 }
91 return codeOrigin.bytecodeIndex;
92}
93
94#endif // ENABLE(DFG_JIT)
95
96unsigned CallFrame::bytecodeOffset()
97{
98 if (!codeBlock())
99 return 0;
100#if ENABLE(DFG_JIT)
101 if (hasLocationAsCodeOriginIndex())
102 return bytecodeOffsetFromCodeOriginIndex();
103#endif
104 return locationAsBytecodeOffset();
105}
106
107CodeOrigin CallFrame::codeOrigin()
108{
109 if (!codeBlock())
110 return CodeOrigin(0);
111#if ENABLE(DFG_JIT)
112 if (hasLocationAsCodeOriginIndex()) {
113 unsigned index = locationAsCodeOriginIndex();
114 ASSERT(codeBlock()->canGetCodeOrigin(index));
115 return codeBlock()->codeOrigin(index);
116 }
117#endif
118 return CodeOrigin(locationAsBytecodeOffset());
119}
120
121Register* CallFrame::topOfFrameInternal()
122{
123 CodeBlock* codeBlock = this->codeBlock();
124 ASSERT(codeBlock);
125 return registers() + codeBlock->stackPointerOffset();
126}
127
128JSGlobalObject* CallFrame::vmEntryGlobalObject()
129{
130 if (this == lexicalGlobalObject()->globalExec())
131 return lexicalGlobalObject();
132
133 // For any ExecState that's not a globalExec, the
134 // dynamic global object must be set since code is running
135 ASSERT(vm().entryScope);
136 return vm().entryScope->globalObject();
137}
138
139CallFrame* CallFrame::callerFrame(VMEntryFrame*& currVMEntryFrame)
140{
141 if (callerFrameOrVMEntryFrame() == currVMEntryFrame) {
142 VMEntryRecord* currVMEntryRecord = vmEntryRecord(currVMEntryFrame);
143 currVMEntryFrame = currVMEntryRecord->prevTopVMEntryFrame();
144 return currVMEntryRecord->prevTopCallFrame();
145 }
146 return static_cast<CallFrame*>(callerFrameOrVMEntryFrame());
147}
148
149JSLexicalEnvironment* CallFrame::lexicalEnvironment() const
150{
151 CodeBlock* codeBlock = this->codeBlock();
152 RELEASE_ASSERT(codeBlock->needsActivation());
153 VirtualRegister activationRegister = codeBlock->activationRegister();
154 return registers()[activationRegister.offset()].Register::lexicalEnvironment();
155}
156
157JSLexicalEnvironment* CallFrame::lexicalEnvironmentOrNullptr() const
158{
159 CodeBlock* codeBlock = this->codeBlock();
160 return codeBlock->needsActivation() ? lexicalEnvironment() : nullptr;
161}
162
163void CallFrame::setActivation(JSLexicalEnvironment* lexicalEnvironment)
164{
165 CodeBlock* codeBlock = this->codeBlock();
166 RELEASE_ASSERT(codeBlock->needsActivation());
167 VirtualRegister activationRegister = codeBlock->activationRegister();
168 registers()[activationRegister.offset()] = lexicalEnvironment;
169}
170
171void CallFrame::dump(PrintStream& out)
172{
173 if (CodeBlock* codeBlock = this->codeBlock()) {
174 out.print(codeBlock->inferredName(), "#", codeBlock->hashAsStringIfPossible(), " [", codeBlock->jitType(), "]");
175
176 out.print("(");
177 thisValue().dumpForBacktrace(out);
178
179 for (size_t i = 0; i < argumentCount(); ++i) {
180 out.print(", ");
181 JSValue value = argument(i);
182 value.dumpForBacktrace(out);
183 }
184
185 out.print(")");
186
187 return;
188 }
189
190 out.print(returnPC());
191}
192
193const char* CallFrame::describeFrame()
194{
195 const size_t bufferSize = 200;
196 static char buffer[bufferSize + 1];
197
198 WTF::StringPrintStream stringStream;
199
200 dump(stringStream);
201
202 strncpy(buffer, stringStream.toCString().data(), bufferSize);
203 buffer[bufferSize] = '\0';
204
205 return buffer;
206}
207
208} // namespace JSC