]> git.saurik.com Git - apple/javascriptcore.git/blob - interpreter/Interpreter.h
JavaScriptCore-554.1.tar.gz
[apple/javascriptcore.git] / interpreter / Interpreter.h
1 /*
2 * Copyright (C) 2008 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #ifndef Interpreter_h
30 #define Interpreter_h
31
32 #include "ArgList.h"
33 #include "FastAllocBase.h"
34 #include "HashMap.h"
35 #include "JSCell.h"
36 #include "JSValue.h"
37 #include "JSObject.h"
38 #include "Opcode.h"
39 #include "RegisterFile.h"
40
41 namespace JSC {
42
43 class CodeBlock;
44 class EvalNode;
45 class FunctionBodyNode;
46 class Instruction;
47 class InternalFunction;
48 class JSFunction;
49 class JSGlobalObject;
50 class ProgramNode;
51 class Register;
52 class ScopeChainNode;
53 class SamplingTool;
54 struct CallFrameClosure;
55 struct HandlerInfo;
56
57 enum DebugHookID {
58 WillExecuteProgram,
59 DidExecuteProgram,
60 DidEnterCallFrame,
61 DidReachBreakpoint,
62 WillLeaveCallFrame,
63 WillExecuteStatement
64 };
65
66 // We use a smaller reentrancy limit on iPhone because of the high amount of
67 // stack space required on the web thread.
68 enum { MaxMainThreadReentryDepth = 100, MaxSecondaryThreadReentryDepth = 32 };
69
70 class Interpreter : public FastAllocBase {
71 friend class JIT;
72 friend class CachedCall;
73 public:
74 Interpreter();
75
76 RegisterFile& registerFile() { return m_registerFile; }
77
78 Opcode getOpcode(OpcodeID id)
79 {
80 #if HAVE(COMPUTED_GOTO)
81 return m_opcodeTable[id];
82 #else
83 return id;
84 #endif
85 }
86
87 OpcodeID getOpcodeID(Opcode opcode)
88 {
89 #if HAVE(COMPUTED_GOTO)
90 ASSERT(isOpcode(opcode));
91 return m_opcodeIDTable.get(opcode);
92 #else
93 return opcode;
94 #endif
95 }
96
97 bool isOpcode(Opcode);
98
99 JSValue execute(ProgramNode*, CallFrame*, ScopeChainNode*, JSObject* thisObj, JSValue* exception);
100 JSValue execute(FunctionBodyNode*, CallFrame*, JSFunction*, JSObject* thisObj, const ArgList& args, ScopeChainNode*, JSValue* exception);
101 JSValue execute(EvalNode* evalNode, CallFrame* exec, JSObject* thisObj, ScopeChainNode* scopeChain, JSValue* exception);
102
103 JSValue retrieveArguments(CallFrame*, JSFunction*) const;
104 JSValue retrieveCaller(CallFrame*, InternalFunction*) const;
105 void retrieveLastCaller(CallFrame*, int& lineNumber, intptr_t& sourceID, UString& sourceURL, JSValue& function) const;
106
107 void getArgumentsData(CallFrame*, JSFunction*&, ptrdiff_t& firstParameterIndex, Register*& argv, int& argc);
108
109 void setSampler(SamplingTool* sampler) { m_sampler = sampler; }
110 SamplingTool* sampler() { return m_sampler; }
111
112 NEVER_INLINE JSValue callEval(CallFrame*, RegisterFile*, Register* argv, int argc, int registerOffset, JSValue& exceptionValue);
113 NEVER_INLINE HandlerInfo* throwException(CallFrame*&, JSValue&, unsigned bytecodeOffset, bool);
114 NEVER_INLINE void debug(CallFrame*, DebugHookID, int firstLine, int lastLine);
115
116 private:
117 enum ExecutionFlag { Normal, InitializeAndReturn };
118
119 CallFrameClosure prepareForRepeatCall(FunctionBodyNode*, CallFrame*, JSFunction*, int argCount, ScopeChainNode*, JSValue* exception);
120 void endRepeatCall(CallFrameClosure&);
121 JSValue execute(CallFrameClosure&, JSValue* exception);
122
123 JSValue execute(EvalNode*, CallFrame*, JSObject* thisObject, int globalRegisterOffset, ScopeChainNode*, JSValue* exception);
124
125 #if USE(INTERPRETER)
126 NEVER_INLINE bool resolve(CallFrame*, Instruction*, JSValue& exceptionValue);
127 NEVER_INLINE bool resolveSkip(CallFrame*, Instruction*, JSValue& exceptionValue);
128 NEVER_INLINE bool resolveGlobal(CallFrame*, Instruction*, JSValue& exceptionValue);
129 NEVER_INLINE void resolveBase(CallFrame*, Instruction* vPC);
130 NEVER_INLINE bool resolveBaseAndProperty(CallFrame*, Instruction*, JSValue& exceptionValue);
131 NEVER_INLINE bool resolveBaseAndFunc(CallFrame*, Instruction*, JSValue& exceptionValue);
132 NEVER_INLINE ScopeChainNode* createExceptionScope(CallFrame*, const Instruction* vPC);
133
134 void tryCacheGetByID(CallFrame*, CodeBlock*, Instruction*, JSValue baseValue, const Identifier& propertyName, const PropertySlot&);
135 void uncacheGetByID(CodeBlock*, Instruction* vPC);
136 void tryCachePutByID(CallFrame*, CodeBlock*, Instruction*, JSValue baseValue, const PutPropertySlot&);
137 void uncachePutByID(CodeBlock*, Instruction* vPC);
138 #endif
139
140 NEVER_INLINE bool unwindCallFrame(CallFrame*&, JSValue, unsigned& bytecodeOffset, CodeBlock*&);
141
142 static ALWAYS_INLINE CallFrame* slideRegisterWindowForCall(CodeBlock*, RegisterFile*, CallFrame*, size_t registerOffset, int argc);
143
144 static CallFrame* findFunctionCallFrame(CallFrame*, InternalFunction*);
145
146 JSValue privateExecute(ExecutionFlag, RegisterFile*, CallFrame*, JSValue* exception);
147
148 void dumpCallFrame(CallFrame*);
149 void dumpRegisters(CallFrame*);
150
151 bool isCallBytecode(Opcode opcode) { return opcode == getOpcode(op_call) || opcode == getOpcode(op_construct) || opcode == getOpcode(op_call_eval); }
152
153 SamplingTool* m_sampler;
154
155 int m_reentryDepth;
156
157 RegisterFile m_registerFile;
158
159 #if HAVE(COMPUTED_GOTO)
160 Opcode m_opcodeTable[numOpcodeIDs]; // Maps OpcodeID => Opcode for compiling
161 HashMap<Opcode, OpcodeID> m_opcodeIDTable; // Maps Opcode => OpcodeID for decompiling
162 #endif
163 };
164
165 } // namespace JSC
166
167 #endif // Interpreter_h