2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
4 * Copyright (C) 2003, 2007, 2008, 2011 Apple Inc. All rights reserved.
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.
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.
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.
26 #include "AbstractPC.h"
29 #include "MacroAssemblerCodeRef.h"
39 // Represents the current state of script execution.
40 // Passed as the first argument to most functions.
41 class ExecState
: private Register
{
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
48 ASSERT(this[JSStack::ScopeChain
].Register::scope());
49 return this[JSStack::ScopeChain
].Register::scope();
52 // Global object in which execution began.
53 JSGlobalObject
* dynamicGlobalObject();
55 // Global object in which the currently executing code was defined.
56 // Differs from dynamicGlobalObject() during function calls across web browser frames.
57 JSGlobalObject
* lexicalGlobalObject() const;
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.
61 JSObject
* globalThisValue() const;
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.
70 void clearException() { vm().exception
= JSValue(); }
71 void clearSupplementaryExceptionInfo()
73 vm().clearExceptionStack();
76 JSValue
exception() const { return vm().exception
; }
77 bool hadException() const { return vm().exception
; }
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
; }
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
; }
104 static CallFrame
* create(Register
* callFrameBase
) { return static_cast<CallFrame
*>(callFrameBase
); }
105 Register
* registers() { return this; }
107 CallFrame
& operator=(const Register
& r
) { *static_cast<Register
*>(this) = r
; return *this; }
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); }
115 AbstractPC
abstractReturnPC(VM
& vm
) { return AbstractPC(vm
, this); }
116 #if USE(JSVALUE32_64)
117 unsigned bytecodeOffsetForNonDFGCode() const;
118 void setBytecodeOffsetForNonDFGCode(unsigned offset
);
120 unsigned bytecodeOffsetForNonDFGCode() const
123 return this[JSStack::ArgumentCount
].tag();
126 void setBytecodeOffsetForNonDFGCode(unsigned offset
)
129 this[JSStack::ArgumentCount
].tag() = static_cast<int32_t>(offset
);
133 Register
* frameExtent()
137 return frameExtentInternal();
140 Register
* frameExtentInternal();
143 InlineCallFrame
* inlineCallFrame() const { return this[JSStack::ReturnPC
].asInlineCallFrame(); }
144 unsigned codeOriginIndexForDFG() const { return this[JSStack::ArgumentCount
].tag(); }
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
151 RELEASE_ASSERT_NOT_REACHED();
155 #if USE(JSVALUE32_64)
156 Instruction
* currentVPC() const
158 return bitwise_cast
<Instruction
*>(this[JSStack::ArgumentCount
].tag());
160 void setCurrentVPC(Instruction
* vpc
)
162 this[JSStack::ArgumentCount
].tag() = bitwise_cast
<int32_t>(vpc
);
165 Instruction
* currentVPC() const;
166 void setCurrentVPC(Instruction
* vpc
);
169 void setCallerFrame(CallFrame
* callerFrame
) { static_cast<Register
*>(this)[JSStack::CallerFrame
] = callerFrame
; }
170 void setScope(JSScope
* scope
) { static_cast<Register
*>(this)[JSStack::ScopeChain
] = scope
; }
172 ALWAYS_INLINE
void init(CodeBlock
* codeBlock
, Instruction
* vPC
, JSScope
* scope
,
173 CallFrame
* callerFrame
, int argc
, JSObject
* callee
)
175 ASSERT(callerFrame
); // Use noCaller() rather than 0 for the outer host call frame caller.
176 ASSERT(callerFrame
== noCaller() || callerFrame
->removeHostCallFrameFlag()->stack()->end() >= this);
178 setCodeBlock(codeBlock
);
180 setCallerFrame(callerFrame
);
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)
186 // Read a register from the codeframe (or constant from the CodeBlock).
188 // Read a register for a non-constant
189 Register
& uncheckedR(int);
191 // Access to arguments as passed. (After capture, arguments may move to a different location.)
192 size_t argumentCount() const { return argumentCountIncludingThis() - 1; }
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
; }
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
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.
206 JSValue
argument(size_t argument
)
208 if (argument
>= argumentCount())
209 return jsUndefined();
210 return this[argumentOffset(argument
)].jsValue();
212 void setArgument(size_t argument
, JSValue value
)
214 this[argumentOffset(argument
)] = value
;
217 static int thisArgumentOffset() { return argumentOffsetIncludingThis(0); }
218 JSValue
thisValue() { return this[thisArgumentOffset()].jsValue(); }
219 void setThisValue(JSValue value
) { this[thisArgumentOffset()] = value
; }
221 JSValue
argumentAfterCapture(size_t argument
);
223 static int offsetFor(size_t argumentCountIncludingThis
) { return argumentCountIncludingThis
+ JSStack::CallFrameHeaderSize
; }
225 // FIXME: Remove these.
226 int hostThisRegister() { return thisArgumentOffset(); }
227 JSValue
hostThisValue() { return thisValue(); }
229 static CallFrame
* noCaller() { return reinterpret_cast<CallFrame
*>(HostCallFrameFlag
); }
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
); }
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
; }
241 bool isInlineCallFrame();
243 void setInlineCallFrame(InlineCallFrame
* inlineCallFrame
) { static_cast<Register
*>(this)[JSStack::ReturnPC
] = inlineCallFrame
; }
245 // Call this to get the semantically correct JS CallFrame* for the
246 // currently executing function.
247 CallFrame
* trueCallFrame(AbstractPC
);
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();
254 CodeBlock
* someCodeBlockForPossiblyInlinedCode();
256 bool isInlineCallFrame() { return false; }
258 CallFrame
* trueCallFrame(AbstractPC
) { return this; }
259 CallFrame
* trueCallerFrame() { return callerFrame()->removeHostCallFrameFlag(); }
261 CodeBlock
* someCodeBlockForPossiblyInlinedCode() { return codeBlock(); }
263 CallFrame
* callerFrameNoFlags() { return callerFrame()->removeHostCallFrameFlag(); }
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
273 CallFrame
* trueCallFrameFromVMCode() { return trueCallFrame(AbstractPC()); }
276 static const intptr_t HostCallFrameFlag
= 1;
277 static const int s_thisArgumentOffset
= -1 - JSStack::CallFrameHeaderSize
;
278 static const int s_firstArgumentOffset
= s_thisArgumentOffset
- 1;
284 bool isInlineCallFrameSlow();
289 // The following are for internal use in debugging and verification
290 // code only and not meant as an API for general usage:
292 size_t argIndexForRegister(Register
* reg
)
294 // The register at 'offset' number of slots from the frame pointer
296 // reg = frame[offset];
297 // ==> reg = frame + offset;
298 // ==> offset = reg - frame;
299 int offset
= reg
- this->registers();
301 // The offset is defined (based on argumentOffset()) to be:
302 // offset = s_firstArgumentOffset - argIndex;
304 // argIndex = s_firstArgumentOffset - offset;
305 size_t argIndex
= s_firstArgumentOffset
- offset
;
309 JSValue
getArgumentUnsafe(size_t argIndex
)
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();
318 friend class JSStack
;
319 friend class VMInspector
;
324 #endif // CallFrame_h