#pragma once
#include "AbstractPC.h"
#include "CalleeBits.h"
#include "MacroAssemblerCodeRef.h"
#include "Register.h"
#include "StackVisitor.h"
#include "VM.h"
#include <wtf/EnumClassOperatorOverloads.h>
namespace JSC {
class Arguments;
class CallFrame;
class Interpreter;
class JSCallee;
class JSScope;
class SourceOrigin;
struct Instruction;
class CallSiteIndex {
public:
CallSiteIndex() = default;
explicit CallSiteIndex(BytecodeIndex bytecodeIndex)
: m_bits(bytecodeIndex.offset())
{
ASSERT(!bytecodeIndex.checkpoint());
}
explicit CallSiteIndex(uint32_t bits)
: m_bits(bits)
{ }
explicit operator bool() const { return !!m_bits; }
bool operator==(const CallSiteIndex& other) const { return m_bits == other.m_bits; }
uint32_t bits() const { return m_bits; }
static CallSiteIndex fromBits(uint32_t bits) { return CallSiteIndex(bits); }
BytecodeIndex bytecodeIndex() const { return BytecodeIndex(bits()); }
private:
uint32_t m_bits { BytecodeIndex().offset() };
};
class DisposableCallSiteIndex : public CallSiteIndex {
public:
DisposableCallSiteIndex() = default;
explicit DisposableCallSiteIndex(uint32_t bits)
: CallSiteIndex(bits)
{
}
static DisposableCallSiteIndex fromCallSiteIndex(CallSiteIndex callSiteIndex)
{
return DisposableCallSiteIndex(callSiteIndex.bits());
}
};
struct CallerFrameAndPC {
alignas(CPURegister) CallFrame* callerFrame;
alignas(CPURegister) void* returnPC;
static constexpr int sizeInRegisters = 2 * sizeof(CPURegister) / sizeof(Register);
};
static_assert(CallerFrameAndPC::sizeInRegisters == sizeof(CallerFrameAndPC) / sizeof(Register), "CallerFrameAndPC::sizeInRegisters is incorrect.");
enum class CallFrameSlot {
codeBlock = CallerFrameAndPC::sizeInRegisters,
callee = codeBlock + 1,
argumentCountIncludingThis = callee + 1,
thisArgument = argumentCountIncludingThis + 1,
firstArgument = thisArgument + 1,
};
OVERLOAD_MATH_OPERATORS_FOR_ENUM_CLASS_WITH_INTEGRALS(CallFrameSlot)
OVERLOAD_RELATIONAL_OPERATORS_FOR_ENUM_CLASS_WITH_INTEGRALS(CallFrameSlot)
class CallFrame : private Register {
public:
static constexpr int headerSizeInRegisters = static_cast<int>(CallFrameSlot::argumentCountIncludingThis) + 1;
inline JSValue guaranteedJSValueCallee() const;
inline JSObject* jsCallee() const;
CalleeBits callee() const { return CalleeBits(this[static_cast<int>(CallFrameSlot::callee)].pointer()); }
SUPPRESS_ASAN CalleeBits unsafeCallee() const { return CalleeBits(this[static_cast<int>(CallFrameSlot::callee)].asanUnsafePointer()); }
CodeBlock* codeBlock() const;
CodeBlock** addressOfCodeBlock() const { return bitwise_cast<CodeBlock**>(this + static_cast<int>(CallFrameSlot::codeBlock)); }
inline SUPPRESS_ASAN CodeBlock* unsafeCodeBlock() const;
inline JSScope* scope(int scopeRegisterOffset) const;
JS_EXPORT_PRIVATE bool isAnyWasmCallee();
JSGlobalObject* lexicalGlobalObject(VM&) const;
VM& deprecatedVM() const;
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*>(callerFrameOrEntryFrame()); }
void* callerFrameOrEntryFrame() const { return callerFrameAndPC().callerFrame; }
SUPPRESS_ASAN void* unsafeCallerFrameOrEntryFrame() const { return unsafeCallerFrameAndPC().callerFrame; }
CallFrame* unsafeCallerFrame(EntryFrame*&) const;
JS_EXPORT_PRIVATE CallFrame* callerFrame(EntryFrame*&) const;
JS_EXPORT_PRIVATE SourceOrigin callerSourceOrigin(VM&);
static ptrdiff_t callerFrameOffset() { return OBJECT_OFFSETOF(CallerFrameAndPC, callerFrame); }
ReturnAddressPtr returnPC() const { return ReturnAddressPtr(callerFrameAndPC().returnPC); }
bool hasReturnPC() const { return !!callerFrameAndPC().returnPC; }
void clearReturnPC() { callerFrameAndPC().returnPC = nullptr; }
static ptrdiff_t returnPCOffset() { return OBJECT_OFFSETOF(CallerFrameAndPC, returnPC); }
AbstractPC abstractReturnPC(VM& vm) { return AbstractPC(vm, this); }
bool callSiteBitsAreBytecodeOffset() const;
bool callSiteBitsAreCodeOriginIndex() const;
unsigned callSiteAsRawBits() const;
unsigned unsafeCallSiteAsRawBits() const;
CallSiteIndex callSiteIndex() const;
CallSiteIndex unsafeCallSiteIndex() const;
private:
unsigned callSiteBitsAsBytecodeOffset() const;
#if ENABLE(WEBASSEMBLY)
JS_EXPORT_PRIVATE JSGlobalObject* lexicalGlobalObjectFromWasmCallee(VM&) const;
#endif
public:
BytecodeIndex bytecodeIndex() const;
JS_EXPORT_PRIVATE CodeOrigin codeOrigin() const;
inline Register* topOfFrame();
const Instruction* currentVPC() const; void setCurrentVPC(const Instruction*);
void setCallerFrame(CallFrame* frame) { callerFrameAndPC().callerFrame = frame; }
inline void setScope(int scopeRegisterOffset, JSScope*);
static void initDeprecatedCallFrameForDebugger(CallFrame* globalExec, JSCallee* globalCallee);
Register& r(VirtualRegister);
Register& uncheckedR(VirtualRegister);
size_t argumentCount() const { return argumentCountIncludingThis() - 1; }
size_t argumentCountIncludingThis() const { return this[static_cast<int>(CallFrameSlot::argumentCountIncludingThis)].payload(); }
static int argumentOffset(int argument) { return (CallFrameSlot::firstArgument + argument); }
static int argumentOffsetIncludingThis(int argument) { return (CallFrameSlot::thisArgument + argument); }
JSValue* addressOfArgumentsStart() const { return bitwise_cast<JSValue*>(this + argumentOffset(0)); }
JSValue argument(size_t argument) const
{
if (argument >= argumentCount())
return jsUndefined();
return getArgumentUnsafe(argument);
}
JSValue uncheckedArgument(size_t argument) const
{
ASSERT(argument < argumentCount());
return getArgumentUnsafe(argument);
}
void setArgument(size_t argument, JSValue value)
{
this[argumentOffset(argument)] = value;
}
JSValue getArgumentUnsafe(size_t argIndex) const
{
return this[argumentOffset(argIndex)].jsValue();
}
static int thisArgumentOffset() { return argumentOffsetIncludingThis(0); }
JSValue thisValue() const { return this[thisArgumentOffset()].jsValue(); }
void setThisValue(JSValue value) { this[thisArgumentOffset()] = value; }
JSValue newTarget() const { return thisValue(); }
JSValue argumentAfterCapture(size_t argument);
static int offsetFor(size_t argumentCountIncludingThis) { return CallFrameSlot::thisArgument + argumentCountIncludingThis - 1; }
static CallFrame* noCaller() { return nullptr; }
bool isDeprecatedCallFrameForDebugger() const
{
return callerFrameAndPC().callerFrame == noCaller() && callerFrameAndPC().returnPC == nullptr;
}
void convertToStackOverflowFrame(VM&, CodeBlock* codeBlockToKeepAliveUntilFrameIsUnwound);
bool isStackOverflowFrame() const;
bool isWasmFrame() const;
void setArgumentCountIncludingThis(int count) { static_cast<Register*>(this)[static_cast<int>(CallFrameSlot::argumentCountIncludingThis)].payload() = count; }
inline void setCallee(JSObject*);
inline void setCodeBlock(CodeBlock*);
void setReturnPC(void* value) { callerFrameAndPC().returnPC = value; }
String friendlyFunctionName();
template <StackVisitor::EmptyEntryFrameAction action = StackVisitor::ContinueIfTopEntryFrameIsEmpty, typename Functor> void iterate(VM& vm, const Functor& functor)
{
void* rawThis = this;
if (!!rawThis)
RELEASE_ASSERT(callee().isCell());
StackVisitor::visit<action, Functor>(this, vm, functor);
}
void dump(PrintStream&) const;
JS_EXPORT_PRIVATE const char* describeFrame();
private:
CallFrame();
~CallFrame();
Register* topOfFrameInternal();
size_t argIndexForRegister(Register* reg)
{
int offset = reg - this->registers();
size_t argIndex = offset - CallFrameSlot::firstArgument;
return argIndex;
}
CallerFrameAndPC& callerFrameAndPC() { return *reinterpret_cast<CallerFrameAndPC*>(this); }
const CallerFrameAndPC& callerFrameAndPC() const { return *reinterpret_cast<const CallerFrameAndPC*>(this); }
SUPPRESS_ASAN const CallerFrameAndPC& unsafeCallerFrameAndPC() const { return *reinterpret_cast<const CallerFrameAndPC*>(this); }
};
JS_EXPORT_PRIVATE bool isFromJSCode(void* returnAddress);
#if USE(BUILTIN_FRAME_ADDRESS)
#define DECLARE_CALL_FRAME(vm) \
({ \
ASSERT(JSC::isFromJSCode(removeCodePtrTag<void*>(__builtin_return_address(0)))); \
bitwise_cast<JSC::CallFrame*>(__builtin_frame_address(1)); \
})
#else
#define DECLARE_CALL_FRAME(vm) ((vm).topCallFrame)
#endif
}