]> git.saurik.com Git - apple/javascriptcore.git/blobdiff - interpreter/CallFrame.h
JavaScriptCore-1218.tar.gz
[apple/javascriptcore.git] / interpreter / CallFrame.h
index 4ec3de7f3d6f1e065a31fe26ed93f76cc09e62fb..c09132c579320d16ce11f9f253a4d955dc6656cf 100644 (file)
 #define CallFrame_h
 
 #include "AbstractPC.h"
-#include "JSGlobalData.h"
+#include "VM.h"
+#include "JSStack.h"
 #include "MacroAssemblerCodeRef.h"
-#include "RegisterFile.h"
+#include "Register.h"
 
 namespace JSC  {
 
     class Arguments;
     class JSActivation;
     class Interpreter;
-    class ScopeChainNode;
+    class JSScope;
 
     // Represents the current state of script execution.
     // Passed as the first argument to most functions.
     class ExecState : private Register {
     public:
-        JSValue calleeAsValue() const { return this[RegisterFile::Callee].jsValue(); }
-        JSObject* callee() const { return this[RegisterFile::Callee].function(); }
-        CodeBlock* codeBlock() const { return this[RegisterFile::CodeBlock].Register::codeBlock(); }
-        ScopeChainNode* scopeChain() const
+        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[RegisterFile::ScopeChain].Register::scopeChain());
-            return this[RegisterFile::ScopeChain].Register::scopeChain();
+            ASSERT(this[JSStack::ScopeChain].Register::scope());
+            return this[JSStack::ScopeChain].Register::scope();
         }
 
         // Global object in which execution began.
@@ -53,61 +54,65 @@ namespace JSC  {
 
         // Global object in which the currently executing code was defined.
         // Differs from dynamicGlobalObject() during function calls across web browser frames.
-        inline JSGlobalObject* lexicalGlobalObject() const;
+        JSGlobalObject* lexicalGlobalObject() const;
 
         // Differs from lexicalGlobalObject because this will have DOM window shell rather than
         // the actual DOM window, which can't be "this" for security reasons.
-        inline JSObject* globalThisValue() const;
+        JSObject* globalThisValue() const;
 
-        inline JSGlobalData& globalData() const;
+        VM& vm() const;
 
         // Convenience functions for access to global data.
         // It takes a few memory references to get from a call frame to the global data
         // pointer, so these are inefficient, and should be used sparingly in new code.
         // But they're used in many places in legacy code, so they're not going away any time soon.
 
-        void clearException() { globalData().exception = JSValue(); }
-        JSValue exception() const { return globalData().exception; }
-        bool hadException() const { return globalData().exception; }
+        void clearException() { vm().exception = JSValue(); }
+        void clearSupplementaryExceptionInfo()
+        {
+            vm().clearExceptionStack();
+        }
 
-        const CommonIdentifiers& propertyNames() const { return *globalData().propertyNames; }
-        const MarkedArgumentBuffer& emptyList() const { return *globalData().emptyList; }
-        Interpreter* interpreter() { return globalData().interpreter; }
-        Heap* heap() { return &globalData().heap; }
+        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->globalData().arrayConstructorTable; }
-        static const HashTable* arrayPrototypeTable(CallFrame* callFrame) { return callFrame->globalData().arrayPrototypeTable; }
-        static const HashTable* booleanPrototypeTable(CallFrame* callFrame) { return callFrame->globalData().booleanPrototypeTable; }
-        static const HashTable* dateTable(CallFrame* callFrame) { return callFrame->globalData().dateTable; }
-        static const HashTable* dateConstructorTable(CallFrame* callFrame) { return callFrame->globalData().dateConstructorTable; }
-        static const HashTable* errorPrototypeTable(CallFrame* callFrame) { return callFrame->globalData().errorPrototypeTable; }
-        static const HashTable* globalObjectTable(CallFrame* callFrame) { return callFrame->globalData().globalObjectTable; }
-        static const HashTable* jsonTable(CallFrame* callFrame) { return callFrame->globalData().jsonTable; }
-        static const HashTable* mathTable(CallFrame* callFrame) { return callFrame->globalData().mathTable; }
-        static const HashTable* numberConstructorTable(CallFrame* callFrame) { return callFrame->globalData().numberConstructorTable; }
-        static const HashTable* numberPrototypeTable(CallFrame* callFrame) { return callFrame->globalData().numberPrototypeTable; }
-        static const HashTable* objectConstructorTable(CallFrame* callFrame) { return callFrame->globalData().objectConstructorTable; }
-        static const HashTable* objectPrototypeTable(CallFrame* callFrame) { return callFrame->globalData().objectPrototypeTable; }
-        static const HashTable* regExpTable(CallFrame* callFrame) { return callFrame->globalData().regExpTable; }
-        static const HashTable* regExpConstructorTable(CallFrame* callFrame) { return callFrame->globalData().regExpConstructorTable; }
-        static const HashTable* regExpPrototypeTable(CallFrame* callFrame) { return callFrame->globalData().regExpPrototypeTable; }
-        static const HashTable* stringTable(CallFrame* callFrame) { return callFrame->globalData().stringTable; }
-        static const HashTable* stringConstructorTable(CallFrame* callFrame) { return callFrame->globalData().stringConstructorTable; }
+        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[RegisterFile::CallerFrame].callFrame(); }
-#if ENABLE(JIT)
-        ReturnAddressPtr returnPC() const { return ReturnAddressPtr(this[RegisterFile::ReturnPC].vPC()); }
-        bool hasReturnPC() const { return !!this[RegisterFile::ReturnPC].vPC(); }
-        void clearReturnPC() { registers()[RegisterFile::ReturnPC] = static_cast<Instruction*>(0); }
+        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(JSGlobalData& globalData) { return AbstractPC(globalData, this); }
+        AbstractPC abstractReturnPC(VM& vm) { return AbstractPC(vm, this); }
 #if USE(JSVALUE32_64)
         unsigned bytecodeOffsetForNonDFGCode() const;
         void setBytecodeOffsetForNonDFGCode(unsigned offset);
@@ -115,13 +120,13 @@ namespace JSC  {
         unsigned bytecodeOffsetForNonDFGCode() const
         {
             ASSERT(codeBlock());
-            return this[RegisterFile::ArgumentCount].tag();
+            return this[JSStack::ArgumentCount].tag();
         }
         
         void setBytecodeOffsetForNonDFGCode(unsigned offset)
         {
             ASSERT(codeBlock());
-            this[RegisterFile::ArgumentCount].tag() = static_cast<int32_t>(offset);
+            this[JSStack::ArgumentCount].tag() = static_cast<int32_t>(offset);
         }
 #endif
 
@@ -135,46 +140,43 @@ namespace JSC  {
         Register* frameExtentInternal();
     
 #if ENABLE(DFG_JIT)
-        InlineCallFrame* inlineCallFrame() const { return this[RegisterFile::ReturnPC].asInlineCallFrame(); }
-        unsigned codeOriginIndexForDFG() const { return this[RegisterFile::ArgumentCount].tag(); }
+        InlineCallFrame* inlineCallFrame() const { return this[JSStack::ReturnPC].asInlineCallFrame(); }
+        unsigned codeOriginIndexForDFG() const { return this[JSStack::ArgumentCount].tag(); }
 #else
         // This will never be called if !ENABLE(DFG_JIT) since all calls should be guarded by
         // isInlineCallFrame(). But to make it easier to write code without having a bunch of
         // #if's, we make a dummy implementation available anyway.
         InlineCallFrame* inlineCallFrame() const
         {
-            ASSERT_NOT_REACHED();
+            RELEASE_ASSERT_NOT_REACHED();
             return 0;
         }
 #endif
-#if ENABLE(CLASSIC_INTERPRETER)
-        Instruction* returnVPC() const { return this[RegisterFile::ReturnPC].vPC(); }
-#endif
 #if USE(JSVALUE32_64)
         Instruction* currentVPC() const
         {
-            return bitwise_cast<Instruction*>(this[RegisterFile::ArgumentCount].tag());
+            return bitwise_cast<Instruction*>(this[JSStack::ArgumentCount].tag());
         }
         void setCurrentVPC(Instruction* vpc)
         {
-            this[RegisterFile::ArgumentCount].tag() = bitwise_cast<int32_t>(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)[RegisterFile::CallerFrame] = callerFrame; }
-        void setScopeChain(ScopeChainNode* scopeChain) { static_cast<Register*>(this)[RegisterFile::ScopeChain] = scopeChain; }
+        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, ScopeChainNode* scopeChain,
+        ALWAYS_INLINE void init(CodeBlock* codeBlock, Instruction* vPC, JSScope* scope,
             CallFrame* callerFrame, int argc, JSObject* callee)
         {
             ASSERT(callerFrame); // Use noCaller() rather than 0 for the outer host call frame caller.
-            ASSERT(callerFrame == noCaller() || callerFrame->removeHostCallFrameFlag()->registerFile()->end() >= this);
+            ASSERT(callerFrame == noCaller() || callerFrame->removeHostCallFrameFlag()->stack()->end() >= this);
 
             setCodeBlock(codeBlock);
-            setScopeChain(scopeChain);
+            setScope(scope);
             setCallerFrame(callerFrame);
             setReturnPC(vPC); // This is either an Instruction* or a pointer into JIT generated code stored as an Instruction*.
             setArgumentCountIncludingThis(argc); // original argument count (for the sake of the "arguments" object)
@@ -182,15 +184,24 @@ namespace JSC  {
         }
 
         // Read a register from the codeframe (or constant from the CodeBlock).
-        inline Register& r(int);
+        Register& r(int);
         // Read a register for a non-constant 
-        inline Register& uncheckedR(int);
+        Register& uncheckedR(int);
 
-        // Access to arguments.
+        // Access to arguments as passed. (After capture, arguments may move to a different location.)
         size_t argumentCount() const { return argumentCountIncludingThis() - 1; }
-        size_t argumentCountIncludingThis() const { return this[RegisterFile::ArgumentCount].payload(); }
-        static int argumentOffset(size_t argument) { return s_firstArgumentOffset - argument; }
-        static int argumentOffsetIncludingThis(size_t argument) { return s_thisArgumentOffset - argument; }
+        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; }
+
+        // In the following (argument() and setArgument()), the 'argument'
+        // parameter is the index of the arguments of the target function of
+        // this frame. The index starts at 0 for the first arg, 1 for the
+        // second, etc.
+        //
+        // The arguments (in this case) do not include the 'this' value.
+        // arguments(0) will not fetch the 'this' value. To get/set 'this',
+        // use thisValue() and setThisValue() below.
 
         JSValue argument(size_t argument)
         {
@@ -207,7 +218,9 @@ namespace JSC  {
         JSValue thisValue() { return this[thisArgumentOffset()].jsValue(); }
         void setThisValue(JSValue value) { this[thisArgumentOffset()] = value; }
 
-        static int offsetFor(size_t argumentCountIncludingThis) { return argumentCountIncludingThis + RegisterFile::CallFrameHeaderSize; }
+        JSValue argumentAfterCapture(size_t argument);
+
+        static int offsetFor(size_t argumentCountIncludingThis) { return argumentCountIncludingThis + JSStack::CallFrameHeaderSize; }
 
         // FIXME: Remove these.
         int hostThisRegister() { return thisArgumentOffset(); }
@@ -219,15 +232,15 @@ namespace JSC  {
         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)[RegisterFile::ArgumentCount].payload() = count; }
-        void setCallee(JSObject* callee) { static_cast<Register*>(this)[RegisterFile::Callee] = Register::withCallee(callee); }
-        void setCodeBlock(CodeBlock* codeBlock) { static_cast<Register*>(this)[RegisterFile::CodeBlock] = codeBlock; }
-        void setReturnPC(void* value) { static_cast<Register*>(this)[RegisterFile::ReturnPC] = (Instruction*)value; }
+        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)[RegisterFile::ReturnPC] = inlineCallFrame; }
+        void setInlineCallFrame(InlineCallFrame* inlineCallFrame) { static_cast<Register*>(this)[JSStack::ReturnPC] = inlineCallFrame; }
         
         // Call this to get the semantically correct JS CallFrame* for the
         // currently executing function.
@@ -237,12 +250,17 @@ namespace JSC  {
         // to the caller. This resolves issues surrounding inlining and the
         // HostCallFrameFlag stuff.
         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(); }
         
         // Call this to get the true call frame (accounted for inlining and any
         // other optimizations), when you have entered into VM code through one
@@ -256,17 +274,49 @@ namespace JSC  {
 
     private:
         static const intptr_t HostCallFrameFlag = 1;
-        static const int s_thisArgumentOffset = -1 - RegisterFile::CallFrameHeaderSize;
+        static const int s_thisArgumentOffset = -1 - JSStack::CallFrameHeaderSize;
         static const int s_firstArgumentOffset = s_thisArgumentOffset - 1;
 
 #ifndef NDEBUG
-        RegisterFile* registerFile();
+        JSStack* stack();
 #endif
 #if ENABLE(DFG_JIT)
         bool isInlineCallFrameSlow();
 #endif
         ExecState();
         ~ExecState();
+
+        // The following are for internal use in debugging and verification
+        // code only and not meant as an API for general usage:
+
+        size_t argIndexForRegister(Register* reg)
+        {
+            // The register at 'offset' number of slots from the frame pointer
+            // i.e.
+            //       reg = frame[offset];
+            //   ==> reg = frame + offset;
+            //   ==> offset = reg - frame;
+            int offset = reg - this->registers();
+
+            // The offset is defined (based on argumentOffset()) to be:
+            //       offset = s_firstArgumentOffset - argIndex;
+            // Hence:
+            //       argIndex = s_firstArgumentOffset - offset;
+            size_t argIndex = s_firstArgumentOffset - offset;
+            return argIndex;
+        }
+
+        JSValue getArgumentUnsafe(size_t argIndex)
+        {
+            // User beware! This method does not verify that there is a valid
+            // argument at the specified argIndex. This is used for debugging
+            // and verification code only. The caller is expected to know what
+            // he/she is doing when calling this method.
+            return this[argumentOffset(argIndex)].jsValue();
+        }
+
+        friend class JSStack;
+        friend class VMInspector;
     };
 
 } // namespace JSC