]> git.saurik.com Git - apple/javascriptcore.git/blobdiff - interpreter/Interpreter.h
JavaScriptCore-1218.34.tar.gz
[apple/javascriptcore.git] / interpreter / Interpreter.h
index 2bcc82ffdfaa06935ce25c1389320ab9b657c6d1..d647033e42df816cb245bb8b82ee2b822ecb83c7 100644 (file)
@@ -1,5 +1,6 @@
 /*
- * Copyright (C) 2008 Apple Inc. All rights reserved.
+ * Copyright (C) 2008, 2013 Apple Inc. All rights reserved.
+ * Copyright (C) 2012 Research In Motion Limited. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
 #define Interpreter_h
 
 #include "ArgList.h"
-#include "FastAllocBase.h"
+#include "JSCJSValue.h"
 #include "JSCell.h"
-#include "JSValue.h"
+#include "JSFunction.h"
 #include "JSObject.h"
+#include "JSStack.h"
+#include "LLIntData.h"
 #include "Opcode.h"
-#include "RegisterFile.h"
 
 #include <wtf/HashMap.h>
+#include <wtf/text/StringBuilder.h>
 
 namespace JSC {
 
     class CodeBlock;
     class EvalExecutable;
+    class ExecutableBase;
     class FunctionExecutable;
-    class InternalFunction;
-    class JSFunction;
+    class VM;
     class JSGlobalObject;
+    class LLIntOffsetsExtractor;
     class ProgramExecutable;
     class Register;
-    class ScopeChainNode;
+    class JSScope;
     class SamplingTool;
     struct CallFrameClosure;
     struct HandlerInfo;
@@ -64,91 +68,189 @@ namespace JSC {
         WillExecuteStatement
     };
 
-    // We use a smaller reentrancy limit on iPhone because of the high amount of
-    // stack space required on the web thread.
-    enum { MaxLargeThreadReentryDepth = 100, MaxSmallThreadReentryDepth = 32 };
+    enum StackFrameCodeType {
+        StackFrameGlobalCode,
+        StackFrameEvalCode,
+        StackFrameFunctionCode,
+        StackFrameNativeCode
+    };
 
-    class Interpreter : public FastAllocBase {
-        friend class JIT;
+    struct StackFrame {
+        Strong<JSObject> callee;
+        StackFrameCodeType codeType;
+        Strong<ExecutableBase> executable;
+        Strong<UnlinkedCodeBlock> codeBlock;
+        RefPtr<SourceProvider> code;
+        int lineOffset;
+        unsigned firstLineColumnOffset;
+        unsigned characterOffset;
+        unsigned bytecodeOffset;
+        String sourceURL;
+        JS_EXPORT_PRIVATE String toString(CallFrame*);
+        String friendlySourceURL() const
+        {
+            String traceLine;
+
+            switch (codeType) {
+            case StackFrameEvalCode:
+            case StackFrameFunctionCode:
+            case StackFrameGlobalCode:
+                if (!sourceURL.isEmpty())
+                    traceLine = sourceURL.impl();
+                break;
+            case StackFrameNativeCode:
+                traceLine = "[native code]";
+                break;
+            }
+            return traceLine.isNull() ? emptyString() : traceLine;
+        }
+        String friendlyFunctionName(CallFrame* callFrame) const
+        {
+            String traceLine;
+            JSObject* stackFrameCallee = callee.get();
+
+            switch (codeType) {
+            case StackFrameEvalCode:
+                traceLine = "eval code";
+                break;
+            case StackFrameNativeCode:
+                if (callee)
+                    traceLine = getCalculatedDisplayName(callFrame, stackFrameCallee).impl();
+                break;
+            case StackFrameFunctionCode:
+                traceLine = getCalculatedDisplayName(callFrame, stackFrameCallee).impl();
+                break;
+            case StackFrameGlobalCode:
+                traceLine = "global code";
+                break;
+            }
+            return traceLine.isNull() ? emptyString() : traceLine;
+        }
+        JS_EXPORT_PRIVATE void computeLineAndColumn(unsigned& line, unsigned& column);
+
+    private:
+        void expressionInfo(int& divot, int& startOffset, int& endOffset, unsigned& line, unsigned& column);
+    };
+
+    class TopCallFrameSetter {
+    public:
+        TopCallFrameSetter(VM& global, CallFrame* callFrame)
+            : vm(global)
+            , oldCallFrame(global.topCallFrame) 
+        {
+            global.topCallFrame = callFrame;
+        }
+        
+        ~TopCallFrameSetter() 
+        {
+            vm.topCallFrame = oldCallFrame;
+        }
+    private:
+        VM& vm;
+        CallFrame* oldCallFrame;
+    };
+    
+    class NativeCallFrameTracer {
+    public:
+        ALWAYS_INLINE NativeCallFrameTracer(VM* global, CallFrame* callFrame)
+        {
+            ASSERT(global);
+            ASSERT(callFrame);
+            global->topCallFrame = callFrame;
+        }
+    };
+
+    class Interpreter {
+        WTF_MAKE_FAST_ALLOCATED;
         friend class CachedCall;
+        friend class LLIntOffsetsExtractor;
+        friend class JIT;
+
     public:
-        Interpreter();
+        class ErrorHandlingMode {
+        public:
+            JS_EXPORT_PRIVATE ErrorHandlingMode(ExecState*);
+            JS_EXPORT_PRIVATE ~ErrorHandlingMode();
+        private:
+            Interpreter& m_interpreter;
+        };
+
+        Interpreter(VM &);
+        ~Interpreter();
+        
+        void initialize(bool canUseJIT);
 
-        RegisterFile& registerFile() { return m_registerFile; }
+        JSStack& stack() { return m_stack; }
         
         Opcode getOpcode(OpcodeID id)
         {
-            #if ENABLE(COMPUTED_GOTO_INTERPRETER)
-                return m_opcodeTable[id];
-            #else
-                return id;
-            #endif
+            ASSERT(m_initialized);
+#if ENABLE(COMPUTED_GOTO_OPCODES)
+            return m_opcodeTable[id];
+#else
+            return id;
+#endif
         }
 
         OpcodeID getOpcodeID(Opcode opcode)
         {
-            #if ENABLE(COMPUTED_GOTO_INTERPRETER)
-                ASSERT(isOpcode(opcode));
-                return m_opcodeIDTable.get(opcode);
-            #else
-                return opcode;
-            #endif
+            ASSERT(m_initialized);
+#if ENABLE(COMPUTED_GOTO_OPCODES) && ENABLE(LLINT)
+            ASSERT(isOpcode(opcode));
+            return m_opcodeIDTable.get(opcode);
+#else
+            return opcode;
+#endif
         }
-
-        bool isOpcode(Opcode);
         
-        JSValue execute(ProgramExecutable*, CallFrame*, ScopeChainNode*, JSObject* thisObj, JSValue* exception);
-        JSValue execute(FunctionExecutable*, CallFrame*, JSFunction*, JSObject* thisObj, const ArgList& args, ScopeChainNode*, JSValue* exception);
-        JSValue execute(EvalExecutable* evalNode, CallFrame* exec, JSObject* thisObj, ScopeChainNode* scopeChain, JSValue* exception);
+        bool isOpcode(Opcode);
+
+        JSValue execute(ProgramExecutable*, CallFrame*, JSObject* thisObj);
+        JSValue executeCall(CallFrame*, JSObject* function, CallType, const CallData&, JSValue thisValue, const ArgList&);
+        JSObject* executeConstruct(CallFrame*, JSObject* function, ConstructType, const ConstructData&, const ArgList&);
+        JSValue execute(EvalExecutable*, CallFrame*, JSValue thisValue, JSScope*);
 
-        JSValue retrieveArguments(CallFrame*, JSFunction*) const;
-        JSValue retrieveCaller(CallFrame*, InternalFunction*) const;
-        void retrieveLastCaller(CallFrame*, int& lineNumber, intptr_t& sourceID, UString& sourceURL, JSValue& function) const;
+        JSValue retrieveArgumentsFromVMCode(CallFrame*, JSFunction*) const;
+        JSValue retrieveCallerFromVMCode(CallFrame*, JSFunction*) const;
+        JS_EXPORT_PRIVATE void retrieveLastCaller(CallFrame*, int& lineNumber, intptr_t& sourceID, String& sourceURL, JSValue& function) const;
         
         void getArgumentsData(CallFrame*, JSFunction*&, ptrdiff_t& firstParameterIndex, Register*& argv, int& argc);
         
         SamplingTool* sampler() { return m_sampler.get(); }
 
-        NEVER_INLINE JSValue callEval(CallFrame*, RegisterFile*, Register* argv, int argc, int registerOffset, JSValue& exceptionValue);
-        NEVER_INLINE HandlerInfo* throwException(CallFrame*&, JSValue&, unsigned bytecodeOffset, bool);
-        NEVER_INLINE void debug(CallFrame*, DebugHookID, int firstLine, int lastLine);
+        NEVER_INLINE HandlerInfo* throwException(CallFrame*&, JSValue&, unsigned bytecodeOffset);
+        NEVER_INLINE void debug(CallFrame*, DebugHookID, int firstLine, int lastLine, int column);
+        static const String getTraceLine(CallFrame*, StackFrameCodeType, const String&, int);
+        JS_EXPORT_PRIVATE static void getStackTrace(VM*, Vector<StackFrame>& results, size_t maxStackSize = std::numeric_limits<size_t>::max());
+        static void addStackTraceIfNecessary(CallFrame*, JSValue error);
 
         void dumpSampleData(ExecState* exec);
         void startSampling();
         void stopSampling();
-    private:
-        enum ExecutionFlag { Normal, InitializeAndReturn };
-
-        CallFrameClosure prepareForRepeatCall(FunctionExecutable*, CallFrame*, JSFunction*, int argCount, ScopeChainNode*, JSValue* exception);
-        void endRepeatCall(CallFrameClosure&);
-        JSValue execute(CallFrameClosure&, JSValue* exception);
 
-        JSValue execute(EvalExecutable*, CallFrame*, JSObject* thisObject, int globalRegisterOffset, ScopeChainNode*, JSValue* exception);
+        JS_EXPORT_PRIVATE void dumpCallFrame(CallFrame*);
 
-#if ENABLE(INTERPRETER)
-        NEVER_INLINE bool resolve(CallFrame*, Instruction*, JSValue& exceptionValue);
-        NEVER_INLINE bool resolveSkip(CallFrame*, Instruction*, JSValue& exceptionValue);
-        NEVER_INLINE bool resolveGlobal(CallFrame*, Instruction*, JSValue& exceptionValue);
-        NEVER_INLINE bool resolveGlobalDynamic(CallFrame*, Instruction*, JSValue& exceptionValue);
-        NEVER_INLINE void resolveBase(CallFrame*, Instruction* vPC);
-        NEVER_INLINE bool resolveBaseAndProperty(CallFrame*, Instruction*, JSValue& exceptionValue);
-        NEVER_INLINE ScopeChainNode* createExceptionScope(CallFrame*, const Instruction* vPC);
+    private:
+        class StackPolicy {
+        public:
+            StackPolicy(Interpreter&, const StackBounds&);
+            inline size_t requiredCapacity() { return m_requiredCapacity; }
 
-        void tryCacheGetByID(CallFrame*, CodeBlock*, Instruction*, JSValue baseValue, const Identifier& propertyName, const PropertySlot&);
-        void uncacheGetByID(CodeBlock*, Instruction* vPC);
-        void tryCachePutByID(CallFrame*, CodeBlock*, Instruction*, JSValue baseValue, const PutPropertySlot&);
-        void uncachePutByID(CodeBlock*, Instruction* vPC);        
-#endif // ENABLE(INTERPRETER)
+        private:
+            Interpreter& m_interpreter;
+            size_t m_requiredCapacity;
+        };
 
-        NEVER_INLINE bool unwindCallFrame(CallFrame*&, JSValue, unsigned& bytecodeOffset, CodeBlock*&);
+        enum ExecutionFlag { Normal, InitializeAndReturn };
 
-        static ALWAYS_INLINE CallFrame* slideRegisterWindowForCall(CodeBlock*, RegisterFile*, CallFrame*, size_t registerOffset, int argc);
+        CallFrameClosure prepareForRepeatCall(FunctionExecutable*, CallFrame*, JSFunction*, int argumentCountIncludingThis, JSScope*);
+        void endRepeatCall(CallFrameClosure&);
+        JSValue execute(CallFrameClosure&);
 
-        static CallFrame* findFunctionCallFrame(CallFrame*, InternalFunction*);
+        NEVER_INLINE bool unwindCallFrame(CallFrame*&, JSValue, unsigned& bytecodeOffset, CodeBlock*&);
 
-        JSValue privateExecute(ExecutionFlag, RegisterFile*, CallFrame*, JSValue* exception);
+        static CallFrame* findFunctionCallFrameFromVMCode(CallFrame*, JSFunction*);
 
-        void dumpCallFrame(CallFrame*);
         void dumpRegisters(CallFrame*);
         
         bool isCallBytecode(Opcode opcode) { return opcode == getOpcode(op_call) || opcode == getOpcode(op_construct) || opcode == getOpcode(op_call_eval); }
@@ -157,16 +259,28 @@ namespace JSC {
         int m_sampleEntryDepth;
         OwnPtr<SamplingTool> m_sampler;
 
-        int m_reentryDepth;
-
-        RegisterFile m_registerFile;
+        JSStack m_stack;
+        int m_errorHandlingModeReentry;
         
-#if ENABLE(COMPUTED_GOTO_INTERPRETER)
-        Opcode m_opcodeTable[numOpcodeIDs]; // Maps OpcodeID => Opcode for compiling
+#if ENABLE(COMPUTED_GOTO_OPCODES) && ENABLE(LLINT)
+        Opcode* m_opcodeTable; // Maps OpcodeID => Opcode for compiling
         HashMap<Opcode, OpcodeID> m_opcodeIDTable; // Maps Opcode => OpcodeID for decompiling
 #endif
+
+#if !ASSERT_DISABLED
+        bool m_initialized;
+#endif
     };
-    
+
+    // This value must not be an object that would require this conversion (WebCore's global object).
+    inline bool isValidThisObject(JSValue thisValue, ExecState* exec)
+    {
+        return !thisValue.isObject() || thisValue.toThisObject(exec) == thisValue;
+    }
+
+    JSValue eval(CallFrame*);
+    CallFrame* loadVarargs(CallFrame*, JSStack*, JSValue thisValue, JSValue arguments, int firstFreeRegister);
+
 } // namespace JSC
 
 #endif // Interpreter_h