]> git.saurik.com Git - apple/javascriptcore.git/blame - interpreter/Interpreter.h
JavaScriptCore-1097.13.tar.gz
[apple/javascriptcore.git] / interpreter / Interpreter.h
CommitLineData
9dae56ea
A
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 "JSCell.h"
6fe7ccc8 34#include "JSFunction.h"
9dae56ea 35#include "JSValue.h"
ba379fdc 36#include "JSObject.h"
6fe7ccc8 37#include "LLIntData.h"
9dae56ea
A
38#include "Opcode.h"
39#include "RegisterFile.h"
9dae56ea 40
f9bf01c6
A
41#include <wtf/HashMap.h>
42
9dae56ea
A
43namespace JSC {
44
45 class CodeBlock;
f9bf01c6 46 class EvalExecutable;
6fe7ccc8 47 class ExecutableBase;
f9bf01c6 48 class FunctionExecutable;
9dae56ea 49 class JSGlobalObject;
6fe7ccc8 50 class LLIntOffsetsExtractor;
f9bf01c6 51 class ProgramExecutable;
9dae56ea
A
52 class Register;
53 class ScopeChainNode;
54 class SamplingTool;
ba379fdc 55 struct CallFrameClosure;
9dae56ea 56 struct HandlerInfo;
f9bf01c6
A
57 struct Instruction;
58
9dae56ea
A
59 enum DebugHookID {
60 WillExecuteProgram,
61 DidExecuteProgram,
62 DidEnterCallFrame,
63 DidReachBreakpoint,
64 WillLeaveCallFrame,
65 WillExecuteStatement
66 };
67
6fe7ccc8
A
68 enum StackFrameCodeType {
69 StackFrameGlobalCode,
70 StackFrameEvalCode,
71 StackFrameFunctionCode,
72 StackFrameNativeCode
73 };
74
75 struct StackFrame {
76 Strong<JSObject> callee;
77 StackFrameCodeType codeType;
78 Strong<ExecutableBase> executable;
79 int line;
80 UString sourceURL;
81 UString toString(CallFrame* callFrame) const
82 {
83 bool hasSourceURLInfo = !sourceURL.isNull() && !sourceURL.isEmpty();
84 bool hasLineInfo = line > -1;
85 String traceLine;
86 JSObject* stackFrameCallee = callee.get();
87
88 switch (codeType) {
89 case StackFrameEvalCode:
90 if (hasSourceURLInfo) {
91 traceLine = hasLineInfo ? String::format("eval code@%s:%d", sourceURL.ascii().data(), line)
92 : String::format("eval code@%s", sourceURL.ascii().data());
93 } else
94 traceLine = String::format("eval code");
95 break;
96 case StackFrameNativeCode: {
97 if (callee) {
98 UString functionName = getCalculatedDisplayName(callFrame, stackFrameCallee);
99 traceLine = String::format("%s@[native code]", functionName.ascii().data());
100 } else
101 traceLine = "[native code]";
102 break;
103 }
104 case StackFrameFunctionCode: {
105 UString functionName = getCalculatedDisplayName(callFrame, stackFrameCallee);
106 if (hasSourceURLInfo) {
107 traceLine = hasLineInfo ? String::format("%s@%s:%d", functionName.ascii().data(), sourceURL.ascii().data(), line)
108 : String::format("%s@%s", functionName.ascii().data(), sourceURL.ascii().data());
109 } else
110 traceLine = String::format("%s\n", functionName.ascii().data());
111 break;
112 }
113 case StackFrameGlobalCode:
114 if (hasSourceURLInfo) {
115 traceLine = hasLineInfo ? String::format("global code@%s:%d", sourceURL.ascii().data(), line)
116 : String::format("global code@%s", sourceURL.ascii().data());
117 } else
118 traceLine = String::format("global code");
119
120 }
121 return traceLine.impl();
122 }
123 };
124
125 class TopCallFrameSetter {
126 public:
127 TopCallFrameSetter(JSGlobalData& global, CallFrame* callFrame)
128 : globalData(global)
129 , oldCallFrame(global.topCallFrame)
130 {
131 global.topCallFrame = callFrame;
132 }
133
134 ~TopCallFrameSetter()
135 {
136 globalData.topCallFrame = oldCallFrame;
137 }
138 private:
139 JSGlobalData& globalData;
140 CallFrame* oldCallFrame;
141 };
142
143 class NativeCallFrameTracer {
144 public:
145 ALWAYS_INLINE NativeCallFrameTracer(JSGlobalData* global, CallFrame* callFrame)
146 {
147 ASSERT(global);
148 ASSERT(callFrame);
149 global->topCallFrame = callFrame;
150 }
151 };
152
9bcd318d
A
153 // We use a smaller reentrancy limit on iPhone because of the high amount of
154 // stack space required on the web thread.
6fe7ccc8 155 enum { MaxLargeThreadReentryDepth = 64, MaxSmallThreadReentryDepth = 16 };
9dae56ea 156
14957cd0
A
157 class Interpreter {
158 WTF_MAKE_FAST_ALLOCATED;
ba379fdc 159 friend class CachedCall;
6fe7ccc8
A
160 friend class LLIntOffsetsExtractor;
161 friend class JIT;
9dae56ea 162 public:
6fe7ccc8
A
163 Interpreter();
164 ~Interpreter();
165
166 void initialize(LLInt::Data*, bool canUseJIT);
9dae56ea 167
9dae56ea
A
168 RegisterFile& registerFile() { return m_registerFile; }
169
170 Opcode getOpcode(OpcodeID id)
171 {
6fe7ccc8
A
172 ASSERT(m_initialized);
173#if ENABLE(COMPUTED_GOTO_CLASSIC_INTERPRETER) || ENABLE(LLINT)
174 return m_opcodeTable[id];
175#else
176 return id;
177#endif
9dae56ea
A
178 }
179
180 OpcodeID getOpcodeID(Opcode opcode)
181 {
6fe7ccc8
A
182 ASSERT(m_initialized);
183#if ENABLE(LLINT)
184 ASSERT(isOpcode(opcode));
185 return m_opcodeIDTable.get(opcode);
186#elif ENABLE(COMPUTED_GOTO_CLASSIC_INTERPRETER)
187 ASSERT(isOpcode(opcode));
188 if (!m_classicEnabled)
189 return static_cast<OpcodeID>(bitwise_cast<uintptr_t>(opcode));
190
191 return m_opcodeIDTable.get(opcode);
192#else
193 return opcode;
194#endif
195 }
196
197 bool classicEnabled()
198 {
199 return m_classicEnabled;
9dae56ea
A
200 }
201
202 bool isOpcode(Opcode);
14957cd0
A
203
204 JSValue execute(ProgramExecutable*, CallFrame*, ScopeChainNode*, JSObject* thisObj);
205 JSValue executeCall(CallFrame*, JSObject* function, CallType, const CallData&, JSValue thisValue, const ArgList&);
206 JSObject* executeConstruct(CallFrame*, JSObject* function, ConstructType, const ConstructData&, const ArgList&);
6fe7ccc8
A
207 JSValue execute(EvalExecutable*, CallFrame*, JSValue thisValue, ScopeChainNode*);
208 JSValue execute(EvalExecutable*, CallFrame*, JSValue thisValue, ScopeChainNode*, int globalRegisterOffset);
9dae56ea 209
6fe7ccc8
A
210 JSValue retrieveArgumentsFromVMCode(CallFrame*, JSFunction*) const;
211 JSValue retrieveCallerFromVMCode(CallFrame*, JSFunction*) const;
212 JS_EXPORT_PRIVATE void retrieveLastCaller(CallFrame*, int& lineNumber, intptr_t& sourceID, UString& sourceURL, JSValue& function) const;
9dae56ea
A
213
214 void getArgumentsData(CallFrame*, JSFunction*&, ptrdiff_t& firstParameterIndex, Register*& argv, int& argc);
9dae56ea 215
f9bf01c6 216 SamplingTool* sampler() { return m_sampler.get(); }
9dae56ea 217
14957cd0 218 NEVER_INLINE HandlerInfo* throwException(CallFrame*&, JSValue&, unsigned bytecodeOffset);
ba379fdc 219 NEVER_INLINE void debug(CallFrame*, DebugHookID, int firstLine, int lastLine);
6fe7ccc8
A
220 static const UString getTraceLine(CallFrame*, StackFrameCodeType, const UString&, int);
221 JS_EXPORT_PRIVATE static void getStackTrace(JSGlobalData*, Vector<StackFrame>& results);
222 static void addStackTraceIfNecessary(CallFrame*, JSObject* error);
9dae56ea 223
f9bf01c6
A
224 void dumpSampleData(ExecState* exec);
225 void startSampling();
226 void stopSampling();
9dae56ea
A
227 private:
228 enum ExecutionFlag { Normal, InitializeAndReturn };
229
6fe7ccc8 230 CallFrameClosure prepareForRepeatCall(FunctionExecutable*, CallFrame*, JSFunction*, int argumentCountIncludingThis, ScopeChainNode*);
ba379fdc 231 void endRepeatCall(CallFrameClosure&);
14957cd0 232 JSValue execute(CallFrameClosure&);
9dae56ea 233
6fe7ccc8 234#if ENABLE(CLASSIC_INTERPRETER)
ba379fdc
A
235 NEVER_INLINE bool resolve(CallFrame*, Instruction*, JSValue& exceptionValue);
236 NEVER_INLINE bool resolveSkip(CallFrame*, Instruction*, JSValue& exceptionValue);
237 NEVER_INLINE bool resolveGlobal(CallFrame*, Instruction*, JSValue& exceptionValue);
4e4e5a6f 238 NEVER_INLINE bool resolveGlobalDynamic(CallFrame*, Instruction*, JSValue& exceptionValue);
9dae56ea 239 NEVER_INLINE void resolveBase(CallFrame*, Instruction* vPC);
ba379fdc 240 NEVER_INLINE bool resolveBaseAndProperty(CallFrame*, Instruction*, JSValue& exceptionValue);
6fe7ccc8 241 NEVER_INLINE bool resolveThisAndProperty(CallFrame*, Instruction*, JSValue& exceptionValue);
9dae56ea
A
242 NEVER_INLINE ScopeChainNode* createExceptionScope(CallFrame*, const Instruction* vPC);
243
ba379fdc
A
244 void tryCacheGetByID(CallFrame*, CodeBlock*, Instruction*, JSValue baseValue, const Identifier& propertyName, const PropertySlot&);
245 void uncacheGetByID(CodeBlock*, Instruction* vPC);
246 void tryCachePutByID(CallFrame*, CodeBlock*, Instruction*, JSValue baseValue, const PutPropertySlot&);
247 void uncachePutByID(CodeBlock*, Instruction* vPC);
6fe7ccc8 248#endif // ENABLE(CLASSIC_INTERPRETER)
ba379fdc
A
249
250 NEVER_INLINE bool unwindCallFrame(CallFrame*&, JSValue, unsigned& bytecodeOffset, CodeBlock*&);
9dae56ea
A
251
252 static ALWAYS_INLINE CallFrame* slideRegisterWindowForCall(CodeBlock*, RegisterFile*, CallFrame*, size_t registerOffset, int argc);
253
6fe7ccc8 254 static CallFrame* findFunctionCallFrameFromVMCode(CallFrame*, JSFunction*);
9dae56ea 255
14957cd0 256 JSValue privateExecute(ExecutionFlag, RegisterFile*, CallFrame*);
9dae56ea
A
257
258 void dumpCallFrame(CallFrame*);
259 void dumpRegisters(CallFrame*);
9dae56ea
A
260
261 bool isCallBytecode(Opcode opcode) { return opcode == getOpcode(op_call) || opcode == getOpcode(op_construct) || opcode == getOpcode(op_call_eval); }
262
f9bf01c6
A
263 void enableSampler();
264 int m_sampleEntryDepth;
265 OwnPtr<SamplingTool> m_sampler;
9dae56ea 266
9dae56ea 267 int m_reentryDepth;
9dae56ea
A
268
269 RegisterFile m_registerFile;
270
6fe7ccc8
A
271#if ENABLE(LLINT)
272 Opcode* m_opcodeTable; // Maps OpcodeID => Opcode for compiling
273 HashMap<Opcode, OpcodeID> m_opcodeIDTable; // Maps Opcode => OpcodeID for decompiling
274#elif ENABLE(COMPUTED_GOTO_CLASSIC_INTERPRETER)
9dae56ea
A
275 Opcode m_opcodeTable[numOpcodeIDs]; // Maps OpcodeID => Opcode for compiling
276 HashMap<Opcode, OpcodeID> m_opcodeIDTable; // Maps Opcode => OpcodeID for decompiling
277#endif
6fe7ccc8
A
278
279#if !ASSERT_DISABLED
280 bool m_initialized;
281#endif
282 bool m_classicEnabled;
9dae56ea 283 };
6fe7ccc8
A
284
285 // This value must not be an object that would require this conversion (WebCore's global object).
286 inline bool isValidThisObject(JSValue thisValue, ExecState* exec)
287 {
288 return !thisValue.isObject() || thisValue.toThisObject(exec) == thisValue;
289 }
290
291 inline JSValue Interpreter::execute(EvalExecutable* eval, CallFrame* callFrame, JSValue thisValue, ScopeChainNode* scopeChain)
292 {
293 return execute(eval, callFrame, thisValue, scopeChain, m_registerFile.size() + 1 + RegisterFile::CallFrameHeaderSize);
294 }
295
296 JSValue eval(CallFrame*);
297 CallFrame* loadVarargs(CallFrame*, RegisterFile*, JSValue thisValue, JSValue arguments, int firstFreeRegister);
298
9dae56ea
A
299} // namespace JSC
300
301#endif // Interpreter_h