#ifndef CallFrame_h
#define CallFrame_h
#include "AbstractPC.h"
#include "VM.h"
#include "JSStack.h"
#include "MacroAssemblerCodeRef.h"
#include "Register.h"
namespace JSC {
class Arguments;
class JSActivation;
class Interpreter;
class JSScope;
class ExecState : private Register {
public:
JSValue calleeAsValue() const { return this[JSStack::Callee].jsValue(); }
JSObject* callee() const { return this[JSStack::Callee].function(); }
CodeBlock* codeBlock() const { return this[JSStack::CodeBlock].Register::codeBlock(); }
JSScope* scope() const
{
ASSERT(this[JSStack::ScopeChain].Register::scope());
return this[JSStack::ScopeChain].Register::scope();
}
JSGlobalObject* dynamicGlobalObject();
JSGlobalObject* lexicalGlobalObject() const;
JSObject* globalThisValue() const;
VM& vm() const;
void clearException() { vm().exception = JSValue(); }
void clearSupplementaryExceptionInfo()
{
vm().clearExceptionStack();
}
JSValue exception() const { return vm().exception; }
bool hadException() const { return vm().exception; }
const CommonIdentifiers& propertyNames() const { return *vm().propertyNames; }
const MarkedArgumentBuffer& emptyList() const { return *vm().emptyList; }
Interpreter* interpreter() { return vm().interpreter; }
Heap* heap() { return &vm().heap; }
#ifndef NDEBUG
void dumpCaller();
#endif
static const HashTable* arrayConstructorTable(CallFrame* callFrame) { return callFrame->vm().arrayConstructorTable; }
static const HashTable* arrayPrototypeTable(CallFrame* callFrame) { return callFrame->vm().arrayPrototypeTable; }
static const HashTable* booleanPrototypeTable(CallFrame* callFrame) { return callFrame->vm().booleanPrototypeTable; }
static const HashTable* dateTable(CallFrame* callFrame) { return callFrame->vm().dateTable; }
static const HashTable* dateConstructorTable(CallFrame* callFrame) { return callFrame->vm().dateConstructorTable; }
static const HashTable* errorPrototypeTable(CallFrame* callFrame) { return callFrame->vm().errorPrototypeTable; }
static const HashTable* globalObjectTable(CallFrame* callFrame) { return callFrame->vm().globalObjectTable; }
static const HashTable* jsonTable(CallFrame* callFrame) { return callFrame->vm().jsonTable; }
static const HashTable* mathTable(CallFrame* callFrame) { return callFrame->vm().mathTable; }
static const HashTable* numberConstructorTable(CallFrame* callFrame) { return callFrame->vm().numberConstructorTable; }
static const HashTable* numberPrototypeTable(CallFrame* callFrame) { return callFrame->vm().numberPrototypeTable; }
static const HashTable* objectConstructorTable(CallFrame* callFrame) { return callFrame->vm().objectConstructorTable; }
static const HashTable* privateNamePrototypeTable(CallFrame* callFrame) { return callFrame->vm().privateNamePrototypeTable; }
static const HashTable* regExpTable(CallFrame* callFrame) { return callFrame->vm().regExpTable; }
static const HashTable* regExpConstructorTable(CallFrame* callFrame) { return callFrame->vm().regExpConstructorTable; }
static const HashTable* regExpPrototypeTable(CallFrame* callFrame) { return callFrame->vm().regExpPrototypeTable; }
static const HashTable* stringConstructorTable(CallFrame* callFrame) { return callFrame->vm().stringConstructorTable; }
static CallFrame* create(Register* callFrameBase) { return static_cast<CallFrame*>(callFrameBase); }
Register* registers() { return this; }
CallFrame& operator=(const Register& r) { *static_cast<Register*>(this) = r; return *this; }
CallFrame* callerFrame() const { return this[JSStack::CallerFrame].callFrame(); }
#if ENABLE(JIT) || ENABLE(LLINT)
ReturnAddressPtr returnPC() const { return ReturnAddressPtr(this[JSStack::ReturnPC].vPC()); }
bool hasReturnPC() const { return !!this[JSStack::ReturnPC].vPC(); }
void clearReturnPC() { registers()[JSStack::ReturnPC] = static_cast<Instruction*>(0); }
#endif
AbstractPC abstractReturnPC(VM& vm) { return AbstractPC(vm, this); }
#if USE(JSVALUE32_64)
unsigned bytecodeOffsetForNonDFGCode() const;
void setBytecodeOffsetForNonDFGCode(unsigned offset);
#else
unsigned bytecodeOffsetForNonDFGCode() const
{
ASSERT(codeBlock());
return this[JSStack::ArgumentCount].tag();
}
void setBytecodeOffsetForNonDFGCode(unsigned offset)
{
ASSERT(codeBlock());
this[JSStack::ArgumentCount].tag() = static_cast<int32_t>(offset);
}
#endif
Register* frameExtent()
{
if (!codeBlock())
return registers();
return frameExtentInternal();
}
Register* frameExtentInternal();
#if ENABLE(DFG_JIT)
InlineCallFrame* inlineCallFrame() const { return this[JSStack::ReturnPC].asInlineCallFrame(); }
unsigned codeOriginIndexForDFG() const { return this[JSStack::ArgumentCount].tag(); }
#else
InlineCallFrame* inlineCallFrame() const
{
RELEASE_ASSERT_NOT_REACHED();
return 0;
}
#endif
#if USE(JSVALUE32_64)
Instruction* currentVPC() const
{
return bitwise_cast<Instruction*>(this[JSStack::ArgumentCount].tag());
}
void setCurrentVPC(Instruction* vpc)
{
this[JSStack::ArgumentCount].tag() = bitwise_cast<int32_t>(vpc);
}
#else
Instruction* currentVPC() const;
void setCurrentVPC(Instruction* vpc);
#endif
void setCallerFrame(CallFrame* callerFrame) { static_cast<Register*>(this)[JSStack::CallerFrame] = callerFrame; }
void setScope(JSScope* scope) { static_cast<Register*>(this)[JSStack::ScopeChain] = scope; }
ALWAYS_INLINE void init(CodeBlock* codeBlock, Instruction* vPC, JSScope* scope,
CallFrame* callerFrame, int argc, JSObject* callee)
{
ASSERT(callerFrame); ASSERT(callerFrame == noCaller() || callerFrame->removeHostCallFrameFlag()->stack()->end() >= this);
setCodeBlock(codeBlock);
setScope(scope);
setCallerFrame(callerFrame);
setReturnPC(vPC); setArgumentCountIncludingThis(argc); setCallee(callee);
}
Register& r(int);
Register& uncheckedR(int);
size_t argumentCount() const { return argumentCountIncludingThis() - 1; }
size_t argumentCountIncludingThis() const { return this[JSStack::ArgumentCount].payload(); }
static int argumentOffset(int argument) { return s_firstArgumentOffset - argument; }
static int argumentOffsetIncludingThis(int argument) { return s_thisArgumentOffset - argument; }
JSValue argument(size_t argument)
{
if (argument >= argumentCount())
return jsUndefined();
return this[argumentOffset(argument)].jsValue();
}
void setArgument(size_t argument, JSValue value)
{
this[argumentOffset(argument)] = value;
}
static int thisArgumentOffset() { return argumentOffsetIncludingThis(0); }
JSValue thisValue() { return this[thisArgumentOffset()].jsValue(); }
void setThisValue(JSValue value) { this[thisArgumentOffset()] = value; }
JSValue argumentAfterCapture(size_t argument);
static int offsetFor(size_t argumentCountIncludingThis) { return argumentCountIncludingThis + JSStack::CallFrameHeaderSize; }
int hostThisRegister() { return thisArgumentOffset(); }
JSValue hostThisValue() { return thisValue(); }
static CallFrame* noCaller() { return reinterpret_cast<CallFrame*>(HostCallFrameFlag); }
bool hasHostCallFrameFlag() const { return reinterpret_cast<intptr_t>(this) & HostCallFrameFlag; }
CallFrame* addHostCallFrameFlag() const { return reinterpret_cast<CallFrame*>(reinterpret_cast<intptr_t>(this) | HostCallFrameFlag); }
CallFrame* removeHostCallFrameFlag() { return reinterpret_cast<CallFrame*>(reinterpret_cast<intptr_t>(this) & ~HostCallFrameFlag); }
void setArgumentCountIncludingThis(int count) { static_cast<Register*>(this)[JSStack::ArgumentCount].payload() = count; }
void setCallee(JSObject* callee) { static_cast<Register*>(this)[JSStack::Callee] = Register::withCallee(callee); }
void setCodeBlock(CodeBlock* codeBlock) { static_cast<Register*>(this)[JSStack::CodeBlock] = codeBlock; }
void setReturnPC(void* value) { static_cast<Register*>(this)[JSStack::ReturnPC] = (Instruction*)value; }
#if ENABLE(DFG_JIT)
bool isInlineCallFrame();
void setInlineCallFrame(InlineCallFrame* inlineCallFrame) { static_cast<Register*>(this)[JSStack::ReturnPC] = inlineCallFrame; }
CallFrame* trueCallFrame(AbstractPC);
CallFrame* trueCallerFrame();
CodeBlock* someCodeBlockForPossiblyInlinedCode();
#else
bool isInlineCallFrame() { return false; }
CallFrame* trueCallFrame(AbstractPC) { return this; }
CallFrame* trueCallerFrame() { return callerFrame()->removeHostCallFrameFlag(); }
CodeBlock* someCodeBlockForPossiblyInlinedCode() { return codeBlock(); }
#endif
CallFrame* callerFrameNoFlags() { return callerFrame()->removeHostCallFrameFlag(); }
CallFrame* trueCallFrameFromVMCode() { return trueCallFrame(AbstractPC()); }
private:
static const intptr_t HostCallFrameFlag = 1;
static const int s_thisArgumentOffset = -1 - JSStack::CallFrameHeaderSize;
static const int s_firstArgumentOffset = s_thisArgumentOffset - 1;
#ifndef NDEBUG
JSStack* stack();
#endif
#if ENABLE(DFG_JIT)
bool isInlineCallFrameSlow();
#endif
ExecState();
~ExecState();
size_t argIndexForRegister(Register* reg)
{
int offset = reg - this->registers();
size_t argIndex = s_firstArgumentOffset - offset;
return argIndex;
}
JSValue getArgumentUnsafe(size_t argIndex)
{
return this[argumentOffset(argIndex)].jsValue();
}
friend class JSStack;
friend class VMInspector;
};
}
#endif // CallFrame_h