]> git.saurik.com Git - apple/javascriptcore.git/blame - interpreter/CallFrame.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / interpreter / CallFrame.cpp
CommitLineData
b37bf2e1 1/*
81345200 2 * Copyright (C) 2008, 2013, 2014 Apple Inc. All Rights Reserved.
b37bf2e1
A
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 *
9dae56ea 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
b37bf2e1
A
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
9dae56ea 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
b37bf2e1
A
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
9dae56ea
A
26#include "config.h"
27#include "CallFrame.h"
b37bf2e1 28
81345200 29#include "CallFrameInlines.h"
9dae56ea 30#include "CodeBlock.h"
ba379fdc 31#include "Interpreter.h"
ed1e77d3 32#include "JSLexicalEnvironment.h"
81345200
A
33#include "JSCInlines.h"
34#include "VMEntryScope.h"
35#include <wtf/StringPrintStream.h>
b37bf2e1 36
9dae56ea 37namespace JSC {
b37bf2e1 38
ba379fdc 39#ifndef NDEBUG
93a37866 40JSStack* CallFrame::stack()
14957cd0 41{
93a37866 42 return &interpreter()->stack();
14957cd0
A
43}
44
ba379fdc
A
45#endif
46
6fe7ccc8 47#if USE(JSVALUE32_64)
81345200 48unsigned CallFrame::locationAsBytecodeOffset() const
6fe7ccc8
A
49{
50 ASSERT(codeBlock());
81345200 51 ASSERT(hasLocationAsBytecodeOffset());
6fe7ccc8
A
52 return currentVPC() - codeBlock()->instructions().begin();
53}
54
81345200 55void CallFrame::setLocationAsBytecodeOffset(unsigned offset)
6fe7ccc8
A
56{
57 ASSERT(codeBlock());
58 setCurrentVPC(codeBlock()->instructions().begin() + offset);
81345200 59 ASSERT(hasLocationAsBytecodeOffset());
6fe7ccc8
A
60}
61#else
62Instruction* CallFrame::currentVPC() const
63{
81345200 64 return codeBlock()->instructions().begin() + locationAsBytecodeOffset();
6fe7ccc8
A
65}
66void CallFrame::setCurrentVPC(Instruction* vpc)
67{
81345200 68 setLocationAsBytecodeOffset(vpc - codeBlock()->instructions().begin());
6fe7ccc8
A
69}
70#endif
71
72#if ENABLE(DFG_JIT)
81345200 73unsigned CallFrame::bytecodeOffsetFromCodeOriginIndex()
6fe7ccc8 74{
81345200
A
75 ASSERT(hasLocationAsCodeOriginIndex());
76 CodeBlock* codeBlock = this->codeBlock();
77 ASSERT(codeBlock);
6fe7ccc8 78
6fe7ccc8 79 CodeOrigin codeOrigin;
81345200
A
80 unsigned index = locationAsCodeOriginIndex();
81 ASSERT(codeBlock->canGetCodeOrigin(index));
82 codeOrigin = codeBlock->codeOrigin(index);
6fe7ccc8 83
6fe7ccc8 84 for (InlineCallFrame* inlineCallFrame = codeOrigin.inlineCallFrame; inlineCallFrame;) {
81345200
A
85 if (inlineCallFrame->baselineCodeBlock() == codeBlock)
86 return codeOrigin.bytecodeIndex;
87
88 codeOrigin = inlineCallFrame->caller;
89 inlineCallFrame = codeOrigin.inlineCallFrame;
6fe7ccc8 90 }
81345200 91 return codeOrigin.bytecodeIndex;
6fe7ccc8 92}
81345200
A
93
94#endif // ENABLE(DFG_JIT)
95
96unsigned CallFrame::bytecodeOffset()
6fe7ccc8 97{
93a37866 98 if (!codeBlock())
6fe7ccc8 99 return 0;
81345200
A
100#if ENABLE(DFG_JIT)
101 if (hasLocationAsCodeOriginIndex())
102 return bytecodeOffsetFromCodeOriginIndex();
103#endif
104 return locationAsBytecodeOffset();
6fe7ccc8 105}
93a37866 106
81345200 107CodeOrigin CallFrame::codeOrigin()
93a37866 108{
81345200
A
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 }
6fe7ccc8 117#endif
81345200
A
118 return CodeOrigin(locationAsBytecodeOffset());
119}
6fe7ccc8 120
81345200 121Register* CallFrame::topOfFrameInternal()
6fe7ccc8
A
122{
123 CodeBlock* codeBlock = this->codeBlock();
124 ASSERT(codeBlock);
81345200
A
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
ed1e77d3
A
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
81345200
A
150{
151 CodeBlock* codeBlock = this->codeBlock();
152 RELEASE_ASSERT(codeBlock->needsActivation());
153 VirtualRegister activationRegister = codeBlock->activationRegister();
ed1e77d3 154 return registers()[activationRegister.offset()].Register::lexicalEnvironment();
81345200
A
155}
156
ed1e77d3
A
157JSLexicalEnvironment* CallFrame::lexicalEnvironmentOrNullptr() const
158{
159 CodeBlock* codeBlock = this->codeBlock();
160 return codeBlock->needsActivation() ? lexicalEnvironment() : nullptr;
161}
162
163void CallFrame::setActivation(JSLexicalEnvironment* lexicalEnvironment)
81345200
A
164{
165 CodeBlock* codeBlock = this->codeBlock();
166 RELEASE_ASSERT(codeBlock->needsActivation());
167 VirtualRegister activationRegister = codeBlock->activationRegister();
ed1e77d3 168 registers()[activationRegister.offset()] = lexicalEnvironment;
81345200
A
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());
6fe7ccc8
A
191}
192
81345200
A
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;
9dae56ea 206}
81345200
A
207
208} // namespace JSC