]> git.saurik.com Git - apple/javascriptcore.git/blame - interpreter/CallFrame.h
JavaScriptCore-1218.34.tar.gz
[apple/javascriptcore.git] / interpreter / CallFrame.h
CommitLineData
9dae56ea
A
1/*
2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
14957cd0 4 * Copyright (C) 2003, 2007, 2008, 2011 Apple Inc. All rights reserved.
9dae56ea
A
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#ifndef CallFrame_h
24#define CallFrame_h
25
6fe7ccc8 26#include "AbstractPC.h"
93a37866
A
27#include "VM.h"
28#include "JSStack.h"
14957cd0 29#include "MacroAssemblerCodeRef.h"
93a37866 30#include "Register.h"
9dae56ea
A
31
32namespace JSC {
33
34 class Arguments;
35 class JSActivation;
36 class Interpreter;
93a37866 37 class JSScope;
9dae56ea
A
38
39 // Represents the current state of script execution.
40 // Passed as the first argument to most functions.
41 class ExecState : private Register {
42 public:
93a37866
A
43 JSValue calleeAsValue() const { return this[JSStack::Callee].jsValue(); }
44 JSObject* callee() const { return this[JSStack::Callee].function(); }
45 CodeBlock* codeBlock() const { return this[JSStack::CodeBlock].Register::codeBlock(); }
46 JSScope* scope() const
f9bf01c6 47 {
93a37866
A
48 ASSERT(this[JSStack::ScopeChain].Register::scope());
49 return this[JSStack::ScopeChain].Register::scope();
f9bf01c6 50 }
9dae56ea
A
51
52 // Global object in which execution began.
53 JSGlobalObject* dynamicGlobalObject();
54
55 // Global object in which the currently executing code was defined.
56 // Differs from dynamicGlobalObject() during function calls across web browser frames.
93a37866 57 JSGlobalObject* lexicalGlobalObject() const;
9dae56ea
A
58
59 // Differs from lexicalGlobalObject because this will have DOM window shell rather than
60 // the actual DOM window, which can't be "this" for security reasons.
93a37866 61 JSObject* globalThisValue() const;
9dae56ea 62
93a37866 63 VM& vm() const;
9dae56ea
A
64
65 // Convenience functions for access to global data.
66 // It takes a few memory references to get from a call frame to the global data
67 // pointer, so these are inefficient, and should be used sparingly in new code.
68 // But they're used in many places in legacy code, so they're not going away any time soon.
69
93a37866
A
70 void clearException() { vm().exception = JSValue(); }
71 void clearSupplementaryExceptionInfo()
72 {
73 vm().clearExceptionStack();
74 }
9dae56ea 75
93a37866
A
76 JSValue exception() const { return vm().exception; }
77 bool hadException() const { return vm().exception; }
78
79 const CommonIdentifiers& propertyNames() const { return *vm().propertyNames; }
80 const MarkedArgumentBuffer& emptyList() const { return *vm().emptyList; }
81 Interpreter* interpreter() { return vm().interpreter; }
82 Heap* heap() { return &vm().heap; }
ba379fdc
A
83#ifndef NDEBUG
84 void dumpCaller();
85#endif
93a37866
A
86 static const HashTable* arrayConstructorTable(CallFrame* callFrame) { return callFrame->vm().arrayConstructorTable; }
87 static const HashTable* arrayPrototypeTable(CallFrame* callFrame) { return callFrame->vm().arrayPrototypeTable; }
88 static const HashTable* booleanPrototypeTable(CallFrame* callFrame) { return callFrame->vm().booleanPrototypeTable; }
89 static const HashTable* dateTable(CallFrame* callFrame) { return callFrame->vm().dateTable; }
90 static const HashTable* dateConstructorTable(CallFrame* callFrame) { return callFrame->vm().dateConstructorTable; }
91 static const HashTable* errorPrototypeTable(CallFrame* callFrame) { return callFrame->vm().errorPrototypeTable; }
92 static const HashTable* globalObjectTable(CallFrame* callFrame) { return callFrame->vm().globalObjectTable; }
93 static const HashTable* jsonTable(CallFrame* callFrame) { return callFrame->vm().jsonTable; }
94 static const HashTable* mathTable(CallFrame* callFrame) { return callFrame->vm().mathTable; }
95 static const HashTable* numberConstructorTable(CallFrame* callFrame) { return callFrame->vm().numberConstructorTable; }
96 static const HashTable* numberPrototypeTable(CallFrame* callFrame) { return callFrame->vm().numberPrototypeTable; }
97 static const HashTable* objectConstructorTable(CallFrame* callFrame) { return callFrame->vm().objectConstructorTable; }
98 static const HashTable* privateNamePrototypeTable(CallFrame* callFrame) { return callFrame->vm().privateNamePrototypeTable; }
99 static const HashTable* regExpTable(CallFrame* callFrame) { return callFrame->vm().regExpTable; }
100 static const HashTable* regExpConstructorTable(CallFrame* callFrame) { return callFrame->vm().regExpConstructorTable; }
101 static const HashTable* regExpPrototypeTable(CallFrame* callFrame) { return callFrame->vm().regExpPrototypeTable; }
102 static const HashTable* stringConstructorTable(CallFrame* callFrame) { return callFrame->vm().stringConstructorTable; }
9dae56ea 103
9dae56ea
A
104 static CallFrame* create(Register* callFrameBase) { return static_cast<CallFrame*>(callFrameBase); }
105 Register* registers() { return this; }
106
107 CallFrame& operator=(const Register& r) { *static_cast<Register*>(this) = r; return *this; }
108
93a37866
A
109 CallFrame* callerFrame() const { return this[JSStack::CallerFrame].callFrame(); }
110#if ENABLE(JIT) || ENABLE(LLINT)
111 ReturnAddressPtr returnPC() const { return ReturnAddressPtr(this[JSStack::ReturnPC].vPC()); }
112 bool hasReturnPC() const { return !!this[JSStack::ReturnPC].vPC(); }
113 void clearReturnPC() { registers()[JSStack::ReturnPC] = static_cast<Instruction*>(0); }
4e4e5a6f 114#endif
93a37866 115 AbstractPC abstractReturnPC(VM& vm) { return AbstractPC(vm, this); }
6fe7ccc8
A
116#if USE(JSVALUE32_64)
117 unsigned bytecodeOffsetForNonDFGCode() const;
118 void setBytecodeOffsetForNonDFGCode(unsigned offset);
119#else
120 unsigned bytecodeOffsetForNonDFGCode() const
121 {
122 ASSERT(codeBlock());
93a37866 123 return this[JSStack::ArgumentCount].tag();
6fe7ccc8
A
124 }
125
126 void setBytecodeOffsetForNonDFGCode(unsigned offset)
127 {
128 ASSERT(codeBlock());
93a37866 129 this[JSStack::ArgumentCount].tag() = static_cast<int32_t>(offset);
6fe7ccc8
A
130 }
131#endif
132
133 Register* frameExtent()
134 {
135 if (!codeBlock())
136 return registers();
137 return frameExtentInternal();
138 }
139
140 Register* frameExtentInternal();
141
142#if ENABLE(DFG_JIT)
93a37866
A
143 InlineCallFrame* inlineCallFrame() const { return this[JSStack::ReturnPC].asInlineCallFrame(); }
144 unsigned codeOriginIndexForDFG() const { return this[JSStack::ArgumentCount].tag(); }
6fe7ccc8
A
145#else
146 // This will never be called if !ENABLE(DFG_JIT) since all calls should be guarded by
147 // isInlineCallFrame(). But to make it easier to write code without having a bunch of
148 // #if's, we make a dummy implementation available anyway.
149 InlineCallFrame* inlineCallFrame() const
150 {
93a37866 151 RELEASE_ASSERT_NOT_REACHED();
6fe7ccc8
A
152 return 0;
153 }
154#endif
6fe7ccc8
A
155#if USE(JSVALUE32_64)
156 Instruction* currentVPC() const
157 {
93a37866 158 return bitwise_cast<Instruction*>(this[JSStack::ArgumentCount].tag());
6fe7ccc8
A
159 }
160 void setCurrentVPC(Instruction* vpc)
161 {
93a37866 162 this[JSStack::ArgumentCount].tag() = bitwise_cast<int32_t>(vpc);
6fe7ccc8
A
163 }
164#else
165 Instruction* currentVPC() const;
166 void setCurrentVPC(Instruction* vpc);
167#endif
9dae56ea 168
93a37866
A
169 void setCallerFrame(CallFrame* callerFrame) { static_cast<Register*>(this)[JSStack::CallerFrame] = callerFrame; }
170 void setScope(JSScope* scope) { static_cast<Register*>(this)[JSStack::ScopeChain] = scope; }
9dae56ea 171
93a37866 172 ALWAYS_INLINE void init(CodeBlock* codeBlock, Instruction* vPC, JSScope* scope,
14957cd0 173 CallFrame* callerFrame, int argc, JSObject* callee)
9dae56ea
A
174 {
175 ASSERT(callerFrame); // Use noCaller() rather than 0 for the outer host call frame caller.
93a37866 176 ASSERT(callerFrame == noCaller() || callerFrame->removeHostCallFrameFlag()->stack()->end() >= this);
9dae56ea
A
177
178 setCodeBlock(codeBlock);
93a37866 179 setScope(scope);
9dae56ea 180 setCallerFrame(callerFrame);
14957cd0
A
181 setReturnPC(vPC); // This is either an Instruction* or a pointer into JIT generated code stored as an Instruction*.
182 setArgumentCountIncludingThis(argc); // original argument count (for the sake of the "arguments" object)
183 setCallee(callee);
9dae56ea
A
184 }
185
ba379fdc 186 // Read a register from the codeframe (or constant from the CodeBlock).
93a37866 187 Register& r(int);
14957cd0 188 // Read a register for a non-constant
93a37866 189 Register& uncheckedR(int);
14957cd0 190
93a37866 191 // Access to arguments as passed. (After capture, arguments may move to a different location.)
14957cd0 192 size_t argumentCount() const { return argumentCountIncludingThis() - 1; }
93a37866
A
193 size_t argumentCountIncludingThis() const { return this[JSStack::ArgumentCount].payload(); }
194 static int argumentOffset(int argument) { return s_firstArgumentOffset - argument; }
195 static int argumentOffsetIncludingThis(int argument) { return s_thisArgumentOffset - argument; }
196
197 // In the following (argument() and setArgument()), the 'argument'
198 // parameter is the index of the arguments of the target function of
199 // this frame. The index starts at 0 for the first arg, 1 for the
200 // second, etc.
201 //
202 // The arguments (in this case) do not include the 'this' value.
203 // arguments(0) will not fetch the 'this' value. To get/set 'this',
204 // use thisValue() and setThisValue() below.
6fe7ccc8
A
205
206 JSValue argument(size_t argument)
207 {
208 if (argument >= argumentCount())
209 return jsUndefined();
210 return this[argumentOffset(argument)].jsValue();
211 }
212 void setArgument(size_t argument, JSValue value)
14957cd0 213 {
6fe7ccc8 214 this[argumentOffset(argument)] = value;
14957cd0 215 }
9dae56ea 216
6fe7ccc8
A
217 static int thisArgumentOffset() { return argumentOffsetIncludingThis(0); }
218 JSValue thisValue() { return this[thisArgumentOffset()].jsValue(); }
219 void setThisValue(JSValue value) { this[thisArgumentOffset()] = value; }
220
93a37866
A
221 JSValue argumentAfterCapture(size_t argument);
222
223 static int offsetFor(size_t argumentCountIncludingThis) { return argumentCountIncludingThis + JSStack::CallFrameHeaderSize; }
6fe7ccc8
A
224
225 // FIXME: Remove these.
226 int hostThisRegister() { return thisArgumentOffset(); }
227 JSValue hostThisValue() { return thisValue(); }
228
9dae56ea 229 static CallFrame* noCaller() { return reinterpret_cast<CallFrame*>(HostCallFrameFlag); }
ba379fdc 230
9dae56ea
A
231 bool hasHostCallFrameFlag() const { return reinterpret_cast<intptr_t>(this) & HostCallFrameFlag; }
232 CallFrame* addHostCallFrameFlag() const { return reinterpret_cast<CallFrame*>(reinterpret_cast<intptr_t>(this) | HostCallFrameFlag); }
233 CallFrame* removeHostCallFrameFlag() { return reinterpret_cast<CallFrame*>(reinterpret_cast<intptr_t>(this) & ~HostCallFrameFlag); }
234
93a37866
A
235 void setArgumentCountIncludingThis(int count) { static_cast<Register*>(this)[JSStack::ArgumentCount].payload() = count; }
236 void setCallee(JSObject* callee) { static_cast<Register*>(this)[JSStack::Callee] = Register::withCallee(callee); }
237 void setCodeBlock(CodeBlock* codeBlock) { static_cast<Register*>(this)[JSStack::CodeBlock] = codeBlock; }
238 void setReturnPC(void* value) { static_cast<Register*>(this)[JSStack::ReturnPC] = (Instruction*)value; }
6fe7ccc8
A
239
240#if ENABLE(DFG_JIT)
241 bool isInlineCallFrame();
242
93a37866 243 void setInlineCallFrame(InlineCallFrame* inlineCallFrame) { static_cast<Register*>(this)[JSStack::ReturnPC] = inlineCallFrame; }
6fe7ccc8
A
244
245 // Call this to get the semantically correct JS CallFrame* for the
246 // currently executing function.
247 CallFrame* trueCallFrame(AbstractPC);
248
249 // Call this to get the semantically correct JS CallFrame* corresponding
250 // to the caller. This resolves issues surrounding inlining and the
251 // HostCallFrameFlag stuff.
252 CallFrame* trueCallerFrame();
93a37866
A
253
254 CodeBlock* someCodeBlockForPossiblyInlinedCode();
6fe7ccc8
A
255#else
256 bool isInlineCallFrame() { return false; }
257
258 CallFrame* trueCallFrame(AbstractPC) { return this; }
259 CallFrame* trueCallerFrame() { return callerFrame()->removeHostCallFrameFlag(); }
93a37866
A
260
261 CodeBlock* someCodeBlockForPossiblyInlinedCode() { return codeBlock(); }
6fe7ccc8 262#endif
93a37866 263 CallFrame* callerFrameNoFlags() { return callerFrame()->removeHostCallFrameFlag(); }
6fe7ccc8
A
264
265 // Call this to get the true call frame (accounted for inlining and any
266 // other optimizations), when you have entered into VM code through one
267 // of the "blessed" entrypoints (JITStubs or DFGOperations). This means
268 // that if you're pretty much anywhere in the VM you can safely call this;
269 // though if you were to magically get an ExecState* by, say, interrupting
270 // a thread that is running JS code and brutishly scraped the call frame
271 // register, calling this method would probably lead to horrible things
272 // happening.
273 CallFrame* trueCallFrameFromVMCode() { return trueCallFrame(AbstractPC()); }
ba379fdc 274
14957cd0 275 private:
ba379fdc 276 static const intptr_t HostCallFrameFlag = 1;
93a37866 277 static const int s_thisArgumentOffset = -1 - JSStack::CallFrameHeaderSize;
6fe7ccc8
A
278 static const int s_firstArgumentOffset = s_thisArgumentOffset - 1;
279
14957cd0 280#ifndef NDEBUG
93a37866 281 JSStack* stack();
6fe7ccc8
A
282#endif
283#if ENABLE(DFG_JIT)
284 bool isInlineCallFrameSlow();
14957cd0 285#endif
9dae56ea
A
286 ExecState();
287 ~ExecState();
93a37866
A
288
289 // The following are for internal use in debugging and verification
290 // code only and not meant as an API for general usage:
291
292 size_t argIndexForRegister(Register* reg)
293 {
294 // The register at 'offset' number of slots from the frame pointer
295 // i.e.
296 // reg = frame[offset];
297 // ==> reg = frame + offset;
298 // ==> offset = reg - frame;
299 int offset = reg - this->registers();
300
301 // The offset is defined (based on argumentOffset()) to be:
302 // offset = s_firstArgumentOffset - argIndex;
303 // Hence:
304 // argIndex = s_firstArgumentOffset - offset;
305 size_t argIndex = s_firstArgumentOffset - offset;
306 return argIndex;
307 }
308
309 JSValue getArgumentUnsafe(size_t argIndex)
310 {
311 // User beware! This method does not verify that there is a valid
312 // argument at the specified argIndex. This is used for debugging
313 // and verification code only. The caller is expected to know what
314 // he/she is doing when calling this method.
315 return this[argumentOffset(argIndex)].jsValue();
316 }
317
318 friend class JSStack;
319 friend class VMInspector;
9dae56ea
A
320 };
321
322} // namespace JSC
323
324#endif // CallFrame_h