#ifndef CallFrame_h
#define CallFrame_h
#include "AbstractPC.h"
#include "JSStack.h"
#include "MacroAssemblerCodeRef.h"
#include "Register.h"
#include "StackVisitor.h"
#include "VM.h"
#include "VMEntryRecord.h"
namespace JSC {
class Arguments;
class JSLexicalEnvironment;
class Interpreter;
class JSScope;
class ExecState : private Register {
public:
JSValue calleeAsValue() const { return this[JSStack::Callee].jsValue(); }
JSObject* callee() const { return this[JSStack::Callee].object(); }
CodeBlock* codeBlock() const { return this[JSStack::CodeBlock].Register::codeBlock(); }
JSScope* scope(int scopeRegisterOffset) const
{
ASSERT(this[scopeRegisterOffset].Register::scope());
return this[scopeRegisterOffset].Register::scope();
}
bool hasActivation() const;
JSLexicalEnvironment* lexicalEnvironment() const;
JSLexicalEnvironment* lexicalEnvironmentOrNullptr() const;
JSValue uncheckedActivation() const;
JS_EXPORT_PRIVATE JSGlobalObject* vmEntryGlobalObject();
JSGlobalObject* lexicalGlobalObject() const;
JSObject* globalThisValue() const;
VM& vm() const;
void clearException() { vm().clearException(); }
Exception* exception() const { return vm().exception(); }
bool hadException() const { return !!vm().exception(); }
Exception* lastException() const { return vm().lastException(); }
void clearLastException() { vm().clearLastException(); }
AtomicStringTable* atomicStringTable() const { return vm().atomicStringTable(); }
const CommonIdentifiers& propertyNames() const { return *vm().propertyNames; }
const MarkedArgumentBuffer& emptyList() const { return *vm().emptyList; }
Interpreter* interpreter() { return vm().interpreter; }
Heap* heap() { return &vm().heap; }
static CallFrame* create(Register* callFrameBase) { return static_cast<CallFrame*>(callFrameBase); }
Register* registers() { return this; }
const Register* registers() const { return this; }
CallFrame& operator=(const Register& r) { *static_cast<Register*>(this) = r; return *this; }
CallFrame* callerFrame() const { return static_cast<CallFrame*>(callerFrameOrVMEntryFrame()); }
void* callerFrameOrVMEntryFrame() const { return callerFrameAndPC().callerFrame; }
JS_EXPORT_PRIVATE CallFrame* callerFrame(VMEntryFrame*&);
static ptrdiff_t callerFrameOffset() { return OBJECT_OFFSETOF(CallerFrameAndPC, callerFrame); }
ReturnAddressPtr returnPC() const { return ReturnAddressPtr(callerFrameAndPC().pc); }
bool hasReturnPC() const { return !!callerFrameAndPC().pc; }
void clearReturnPC() { callerFrameAndPC().pc = 0; }
static ptrdiff_t returnPCOffset() { return OBJECT_OFFSETOF(CallerFrameAndPC, pc); }
AbstractPC abstractReturnPC(VM& vm) { return AbstractPC(vm, this); }
class Location {
public:
static inline uint32_t decode(uint32_t bits);
static inline bool isBytecodeLocation(uint32_t bits);
#if USE(JSVALUE64)
static inline uint32_t encodeAsBytecodeOffset(uint32_t bits);
#else
static inline uint32_t encodeAsBytecodeInstruction(Instruction*);
#endif
static inline bool isCodeOriginIndex(uint32_t bits);
static inline uint32_t encodeAsCodeOriginIndex(uint32_t bits);
private:
enum TypeTag {
BytecodeLocationTag = 0,
CodeOriginIndexTag = 1,
};
static inline uint32_t encode(TypeTag, uint32_t bits);
static const uint32_t s_mask = 0x1;
#if USE(JSVALUE64)
static const uint32_t s_shift = 31;
static const uint32_t s_shiftedMask = s_mask << s_shift;
#else
static const uint32_t s_shift = 1;
#endif
};
bool hasLocationAsBytecodeOffset() const;
bool hasLocationAsCodeOriginIndex() const;
unsigned locationAsRawBits() const;
unsigned locationAsBytecodeOffset() const;
unsigned locationAsCodeOriginIndex() const;
void setLocationAsRawBits(unsigned);
void setLocationAsBytecodeOffset(unsigned);
#if ENABLE(DFG_JIT)
unsigned bytecodeOffsetFromCodeOriginIndex();
#endif
unsigned bytecodeOffset();
CodeOrigin codeOrigin();
Register* topOfFrame()
{
if (!codeBlock())
return registers();
return topOfFrameInternal();
}
#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* frame) { callerFrameAndPC().callerFrame = frame; }
void setScope(int scopeRegisterOffset, JSScope* scope) { static_cast<Register*>(this)[scopeRegisterOffset] = scope; }
void setActivation(JSLexicalEnvironment*);
ALWAYS_INLINE void init(CodeBlock* codeBlock, Instruction* vPC,
CallFrame* callerFrame, int argc, JSObject* callee)
{
ASSERT(callerFrame == noCaller() || callerFrame->stack()->containsAddress(this));
setCodeBlock(codeBlock);
setCallerFrame(callerFrame);
setReturnPC(vPC); setArgumentCountIncludingThis(argc); setCallee(callee);
}
Register& r(int);
Register& r(VirtualRegister);
Register& uncheckedR(int);
Register& uncheckedR(VirtualRegister);
size_t argumentCount() const { return argumentCountIncludingThis() - 1; }
size_t argumentCountIncludingThis() const { return this[JSStack::ArgumentCount].payload(); }
static int argumentOffset(int argument) { return (JSStack::FirstArgument + argument); }
static int argumentOffsetIncludingThis(int argument) { return (JSStack::ThisArgument + argument); }
JSValue argument(size_t argument)
{
if (argument >= argumentCount())
return jsUndefined();
return getArgumentUnsafe(argument);
}
JSValue uncheckedArgument(size_t argument)
{
ASSERT(argument < argumentCount());
return getArgumentUnsafe(argument);
}
void setArgument(size_t argument, JSValue value)
{
this[argumentOffset(argument)] = value;
}
JSValue getArgumentUnsafe(size_t argIndex)
{
return this[argumentOffset(argIndex)].jsValue();
}
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::ThisArgument - 1; }
static CallFrame* noCaller() { return 0; }
void setArgumentCountIncludingThis(int count) { static_cast<Register*>(this)[JSStack::ArgumentCount].payload() = count; }
void setCallee(JSObject* callee) { static_cast<Register*>(this)[JSStack::Callee] = callee; }
void setCodeBlock(CodeBlock* codeBlock) { static_cast<Register*>(this)[JSStack::CodeBlock] = codeBlock; }
void setReturnPC(void* value) { callerFrameAndPC().pc = reinterpret_cast<Instruction*>(value); }
template <typename Functor> void iterate(Functor& functor)
{
StackVisitor::visit<Functor>(this, functor);
}
void dump(PrintStream&);
JS_EXPORT_PRIVATE const char* describeFrame();
private:
#ifndef NDEBUG
JSStack* stack();
#endif
ExecState();
~ExecState();
Register* topOfFrameInternal();
size_t argIndexForRegister(Register* reg)
{
int offset = reg - this->registers();
size_t argIndex = offset - JSStack::FirstArgument;
return argIndex;
}
CallerFrameAndPC& callerFrameAndPC() { return *reinterpret_cast<CallerFrameAndPC*>(this); }
const CallerFrameAndPC& callerFrameAndPC() const { return *reinterpret_cast<const CallerFrameAndPC*>(this); }
friend class JSStack;
};
}
#endif // CallFrame_h