]> git.saurik.com Git - apple/javascriptcore.git/blob - interpreter/Interpreter.h
dc467e37dd572560379b5ea79cd0dc654d6f5e16
[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 "JSCell.h"
34 #include "JSValue.h"
35 #include "Opcode.h"
36 #include "RegisterFile.h"
37 #include <wtf/HashMap.h>
38
39 namespace JSC {
40
41 class CodeBlock;
42 class EvalNode;
43 class FunctionBodyNode;
44 class Instruction;
45 class InternalFunction;
46 class AssemblerBuffer;
47 class JSFunction;
48 class JSGlobalObject;
49 class ProgramNode;
50 class Register;
51 class ScopeChainNode;
52 class SamplingTool;
53 struct HandlerInfo;
54
55 #if ENABLE(JIT)
56
57 #if USE(JIT_STUB_ARGUMENT_VA_LIST)
58 #define STUB_ARGS void* args, ...
59 #define ARGS (reinterpret_cast<void**>(vl_args) - 1)
60 #else // JIT_STUB_ARGUMENT_REGISTER or JIT_STUB_ARGUMENT_STACK
61 #define STUB_ARGS void** args
62 #define ARGS (args)
63 #endif
64
65 #if USE(JIT_STUB_ARGUMENT_REGISTER)
66 #if PLATFORM(X86_64)
67 #define JIT_STUB
68 #elif COMPILER(MSVC)
69 #define JIT_STUB __fastcall
70 #elif COMPILER(GCC)
71 #define JIT_STUB __attribute__ ((fastcall))
72 #else
73 #error Need to support register calling convention in this compiler
74 #endif
75 #else // JIT_STUB_ARGUMENT_VA_LIST or JIT_STUB_ARGUMENT_STACK
76 #if COMPILER(MSVC)
77 #define JIT_STUB __cdecl
78 #else
79 #define JIT_STUB
80 #endif
81 #endif
82
83 // The Mac compilers are fine with this,
84 #if PLATFORM(MAC)
85 struct VoidPtrPair {
86 void* first;
87 void* second;
88 };
89 #define RETURN_PAIR(a,b) VoidPtrPair pair = { a, b }; return pair
90 #else
91 typedef uint64_t VoidPtrPair;
92 union VoidPtrPairValue {
93 struct { void* first; void* second; } s;
94 VoidPtrPair i;
95 };
96 #define RETURN_PAIR(a,b) VoidPtrPairValue pair = {{ a, b }}; return pair.i
97 #endif
98
99 #endif // ENABLE(JIT)
100
101 enum DebugHookID {
102 WillExecuteProgram,
103 DidExecuteProgram,
104 DidEnterCallFrame,
105 DidReachBreakpoint,
106 WillLeaveCallFrame,
107 WillExecuteStatement
108 };
109
110 enum { MaxReentryDepth = 128 };
111
112 class Interpreter {
113 friend class JIT;
114 public:
115 Interpreter();
116 ~Interpreter();
117
118 void initialize(JSGlobalData*);
119
120 RegisterFile& registerFile() { return m_registerFile; }
121
122 Opcode getOpcode(OpcodeID id)
123 {
124 #if HAVE(COMPUTED_GOTO)
125 return m_opcodeTable[id];
126 #else
127 return id;
128 #endif
129 }
130
131 OpcodeID getOpcodeID(Opcode opcode)
132 {
133 #if HAVE(COMPUTED_GOTO)
134 ASSERT(isOpcode(opcode));
135 return m_opcodeIDTable.get(opcode);
136 #else
137 return opcode;
138 #endif
139 }
140
141 bool isOpcode(Opcode);
142
143 JSValuePtr execute(ProgramNode*, CallFrame*, ScopeChainNode*, JSObject* thisObj, JSValuePtr* exception);
144 JSValuePtr execute(FunctionBodyNode*, CallFrame*, JSFunction*, JSObject* thisObj, const ArgList& args, ScopeChainNode*, JSValuePtr* exception);
145 JSValuePtr execute(EvalNode* evalNode, CallFrame* exec, JSObject* thisObj, ScopeChainNode* scopeChain, JSValuePtr* exception);
146
147 JSValuePtr retrieveArguments(CallFrame*, JSFunction*) const;
148 JSValuePtr retrieveCaller(CallFrame*, InternalFunction*) const;
149 void retrieveLastCaller(CallFrame*, int& lineNumber, intptr_t& sourceID, UString& sourceURL, JSValuePtr& function) const;
150
151 void getArgumentsData(CallFrame*, JSFunction*&, ptrdiff_t& firstParameterIndex, Register*& argv, int& argc);
152 void setTimeoutTime(unsigned timeoutTime) { m_timeoutTime = timeoutTime; }
153
154 void startTimeoutCheck()
155 {
156 if (!m_timeoutCheckCount)
157 resetTimeoutCheck();
158
159 ++m_timeoutCheckCount;
160 }
161
162 void stopTimeoutCheck()
163 {
164 ASSERT(m_timeoutCheckCount);
165 --m_timeoutCheckCount;
166 }
167
168 inline void initTimeout()
169 {
170 ASSERT(!m_timeoutCheckCount);
171 resetTimeoutCheck();
172 m_timeoutTime = 0;
173 m_timeoutCheckCount = 0;
174 }
175
176 void setSampler(SamplingTool* sampler) { m_sampler = sampler; }
177 SamplingTool* sampler() { return m_sampler; }
178
179 #if ENABLE(JIT)
180
181 static int JIT_STUB cti_timeout_check(STUB_ARGS);
182 static void JIT_STUB cti_register_file_check(STUB_ARGS);
183
184 static JSObject* JIT_STUB cti_op_convert_this(STUB_ARGS);
185 static void JIT_STUB cti_op_end(STUB_ARGS);
186 static JSValueEncodedAsPointer* JIT_STUB cti_op_add(STUB_ARGS);
187 static JSValueEncodedAsPointer* JIT_STUB cti_op_pre_inc(STUB_ARGS);
188 static int JIT_STUB cti_op_loop_if_less(STUB_ARGS);
189 static int JIT_STUB cti_op_loop_if_lesseq(STUB_ARGS);
190 static JSObject* JIT_STUB cti_op_new_object(STUB_ARGS);
191 static void JIT_STUB cti_op_put_by_id(STUB_ARGS);
192 static void JIT_STUB cti_op_put_by_id_second(STUB_ARGS);
193 static void JIT_STUB cti_op_put_by_id_generic(STUB_ARGS);
194 static void JIT_STUB cti_op_put_by_id_fail(STUB_ARGS);
195 static JSValueEncodedAsPointer* JIT_STUB cti_op_get_by_id(STUB_ARGS);
196 static JSValueEncodedAsPointer* JIT_STUB cti_op_get_by_id_second(STUB_ARGS);
197 static JSValueEncodedAsPointer* JIT_STUB cti_op_get_by_id_generic(STUB_ARGS);
198 static JSValueEncodedAsPointer* JIT_STUB cti_op_get_by_id_self_fail(STUB_ARGS);
199 static JSValueEncodedAsPointer* JIT_STUB cti_op_get_by_id_proto_list(STUB_ARGS);
200 static JSValueEncodedAsPointer* JIT_STUB cti_op_get_by_id_proto_list_full(STUB_ARGS);
201 static JSValueEncodedAsPointer* JIT_STUB cti_op_get_by_id_proto_fail(STUB_ARGS);
202 static JSValueEncodedAsPointer* JIT_STUB cti_op_get_by_id_array_fail(STUB_ARGS);
203 static JSValueEncodedAsPointer* JIT_STUB cti_op_get_by_id_string_fail(STUB_ARGS);
204 static JSValueEncodedAsPointer* JIT_STUB cti_op_del_by_id(STUB_ARGS);
205 static JSValueEncodedAsPointer* JIT_STUB cti_op_instanceof(STUB_ARGS);
206 static JSValueEncodedAsPointer* JIT_STUB cti_op_mul(STUB_ARGS);
207 static JSObject* JIT_STUB cti_op_new_func(STUB_ARGS);
208 static void* JIT_STUB cti_op_call_JSFunction(STUB_ARGS);
209 static VoidPtrPair JIT_STUB cti_op_call_arityCheck(STUB_ARGS);
210 static JSValueEncodedAsPointer* JIT_STUB cti_op_call_NotJSFunction(STUB_ARGS);
211 static void JIT_STUB cti_op_create_arguments(STUB_ARGS);
212 static void JIT_STUB cti_op_create_arguments_no_params(STUB_ARGS);
213 static void JIT_STUB cti_op_tear_off_activation(STUB_ARGS);
214 static void JIT_STUB cti_op_tear_off_arguments(STUB_ARGS);
215 static void JIT_STUB cti_op_profile_will_call(STUB_ARGS);
216 static void JIT_STUB cti_op_profile_did_call(STUB_ARGS);
217 static void JIT_STUB cti_op_ret_scopeChain(STUB_ARGS);
218 static JSObject* JIT_STUB cti_op_new_array(STUB_ARGS);
219 static JSValueEncodedAsPointer* JIT_STUB cti_op_resolve(STUB_ARGS);
220 static JSValueEncodedAsPointer* JIT_STUB cti_op_resolve_global(STUB_ARGS);
221 static JSObject* JIT_STUB cti_op_construct_JSConstruct(STUB_ARGS);
222 static JSValueEncodedAsPointer* JIT_STUB cti_op_construct_NotJSConstruct(STUB_ARGS);
223 static JSValueEncodedAsPointer* JIT_STUB cti_op_get_by_val(STUB_ARGS);
224 static JSValueEncodedAsPointer* JIT_STUB cti_op_get_by_val_byte_array(STUB_ARGS);
225 static VoidPtrPair JIT_STUB cti_op_resolve_func(STUB_ARGS);
226 static JSValueEncodedAsPointer* JIT_STUB cti_op_sub(STUB_ARGS);
227 static void JIT_STUB cti_op_put_by_val(STUB_ARGS);
228 static void JIT_STUB cti_op_put_by_val_array(STUB_ARGS);
229 static void JIT_STUB cti_op_put_by_val_byte_array(STUB_ARGS);
230 static JSValueEncodedAsPointer* JIT_STUB cti_op_lesseq(STUB_ARGS);
231 static int JIT_STUB cti_op_loop_if_true(STUB_ARGS);
232 static JSValueEncodedAsPointer* JIT_STUB cti_op_resolve_base(STUB_ARGS);
233 static JSValueEncodedAsPointer* JIT_STUB cti_op_negate(STUB_ARGS);
234 static JSValueEncodedAsPointer* JIT_STUB cti_op_resolve_skip(STUB_ARGS);
235 static JSValueEncodedAsPointer* JIT_STUB cti_op_div(STUB_ARGS);
236 static JSValueEncodedAsPointer* JIT_STUB cti_op_pre_dec(STUB_ARGS);
237 static int JIT_STUB cti_op_jless(STUB_ARGS);
238 static JSValueEncodedAsPointer* JIT_STUB cti_op_not(STUB_ARGS);
239 static int JIT_STUB cti_op_jtrue(STUB_ARGS);
240 static VoidPtrPair JIT_STUB cti_op_post_inc(STUB_ARGS);
241 static JSValueEncodedAsPointer* JIT_STUB cti_op_eq(STUB_ARGS);
242 static JSValueEncodedAsPointer* JIT_STUB cti_op_lshift(STUB_ARGS);
243 static JSValueEncodedAsPointer* JIT_STUB cti_op_bitand(STUB_ARGS);
244 static JSValueEncodedAsPointer* JIT_STUB cti_op_rshift(STUB_ARGS);
245 static JSValueEncodedAsPointer* JIT_STUB cti_op_bitnot(STUB_ARGS);
246 static VoidPtrPair JIT_STUB cti_op_resolve_with_base(STUB_ARGS);
247 static JSObject* JIT_STUB cti_op_new_func_exp(STUB_ARGS);
248 static JSValueEncodedAsPointer* JIT_STUB cti_op_mod(STUB_ARGS);
249 static JSValueEncodedAsPointer* JIT_STUB cti_op_less(STUB_ARGS);
250 static JSValueEncodedAsPointer* JIT_STUB cti_op_neq(STUB_ARGS);
251 static VoidPtrPair JIT_STUB cti_op_post_dec(STUB_ARGS);
252 static JSValueEncodedAsPointer* JIT_STUB cti_op_urshift(STUB_ARGS);
253 static JSValueEncodedAsPointer* JIT_STUB cti_op_bitxor(STUB_ARGS);
254 static JSObject* JIT_STUB cti_op_new_regexp(STUB_ARGS);
255 static JSValueEncodedAsPointer* JIT_STUB cti_op_bitor(STUB_ARGS);
256 static JSValueEncodedAsPointer* JIT_STUB cti_op_call_eval(STUB_ARGS);
257 static JSValueEncodedAsPointer* JIT_STUB cti_op_throw(STUB_ARGS);
258 static JSPropertyNameIterator* JIT_STUB cti_op_get_pnames(STUB_ARGS);
259 static JSValueEncodedAsPointer* JIT_STUB cti_op_next_pname(STUB_ARGS);
260 static JSObject* JIT_STUB cti_op_push_scope(STUB_ARGS);
261 static void JIT_STUB cti_op_pop_scope(STUB_ARGS);
262 static JSValueEncodedAsPointer* JIT_STUB cti_op_typeof(STUB_ARGS);
263 static JSValueEncodedAsPointer* JIT_STUB cti_op_is_undefined(STUB_ARGS);
264 static JSValueEncodedAsPointer* JIT_STUB cti_op_is_boolean(STUB_ARGS);
265 static JSValueEncodedAsPointer* JIT_STUB cti_op_is_number(STUB_ARGS);
266 static JSValueEncodedAsPointer* JIT_STUB cti_op_is_string(STUB_ARGS);
267 static JSValueEncodedAsPointer* JIT_STUB cti_op_is_object(STUB_ARGS);
268 static JSValueEncodedAsPointer* JIT_STUB cti_op_is_function(STUB_ARGS);
269 static JSValueEncodedAsPointer* JIT_STUB cti_op_stricteq(STUB_ARGS);
270 static JSValueEncodedAsPointer* JIT_STUB cti_op_nstricteq(STUB_ARGS);
271 static JSValueEncodedAsPointer* JIT_STUB cti_op_to_jsnumber(STUB_ARGS);
272 static JSValueEncodedAsPointer* JIT_STUB cti_op_in(STUB_ARGS);
273 static JSObject* JIT_STUB cti_op_push_new_scope(STUB_ARGS);
274 static void JIT_STUB cti_op_jmp_scopes(STUB_ARGS);
275 static void JIT_STUB cti_op_put_by_index(STUB_ARGS);
276 static void* JIT_STUB cti_op_switch_imm(STUB_ARGS);
277 static void* JIT_STUB cti_op_switch_char(STUB_ARGS);
278 static void* JIT_STUB cti_op_switch_string(STUB_ARGS);
279 static JSValueEncodedAsPointer* JIT_STUB cti_op_del_by_val(STUB_ARGS);
280 static void JIT_STUB cti_op_put_getter(STUB_ARGS);
281 static void JIT_STUB cti_op_put_setter(STUB_ARGS);
282 static JSObject* JIT_STUB cti_op_new_error(STUB_ARGS);
283 static void JIT_STUB cti_op_debug(STUB_ARGS);
284
285 static JSValueEncodedAsPointer* JIT_STUB cti_vm_throw(STUB_ARGS);
286 static void* JIT_STUB cti_vm_dontLazyLinkCall(STUB_ARGS);
287 static void* JIT_STUB cti_vm_lazyLinkCall(STUB_ARGS);
288 static JSObject* JIT_STUB cti_op_push_activation(STUB_ARGS);
289
290 #endif // ENABLE(JIT)
291
292 // Default number of ticks before a timeout check should be done.
293 static const int initialTickCountThreshold = 1024;
294
295 bool isJSArray(JSValuePtr v) { return v.isCell() && v.asCell()->vptr() == m_jsArrayVptr; }
296 bool isJSString(JSValuePtr v) { return v.isCell() && v.asCell()->vptr() == m_jsStringVptr; }
297 bool isJSByteArray(JSValuePtr v) { return v.isCell() && v.asCell()->vptr() == m_jsByteArrayVptr; }
298
299 private:
300 enum ExecutionFlag { Normal, InitializeAndReturn };
301
302 NEVER_INLINE JSValuePtr callEval(CallFrame*, RegisterFile*, Register* argv, int argc, int registerOffset, JSValuePtr& exceptionValue);
303 JSValuePtr execute(EvalNode*, CallFrame*, JSObject* thisObject, int globalRegisterOffset, ScopeChainNode*, JSValuePtr* exception);
304
305 NEVER_INLINE void debug(CallFrame*, DebugHookID, int firstLine, int lastLine);
306
307 NEVER_INLINE bool resolve(CallFrame*, Instruction*, JSValuePtr& exceptionValue);
308 NEVER_INLINE bool resolveSkip(CallFrame*, Instruction*, JSValuePtr& exceptionValue);
309 NEVER_INLINE bool resolveGlobal(CallFrame*, Instruction*, JSValuePtr& exceptionValue);
310 NEVER_INLINE void resolveBase(CallFrame*, Instruction* vPC);
311 NEVER_INLINE bool resolveBaseAndProperty(CallFrame*, Instruction*, JSValuePtr& exceptionValue);
312 NEVER_INLINE ScopeChainNode* createExceptionScope(CallFrame*, const Instruction* vPC);
313
314 NEVER_INLINE bool unwindCallFrame(CallFrame*&, JSValuePtr, unsigned& bytecodeOffset, CodeBlock*&);
315 NEVER_INLINE HandlerInfo* throwException(CallFrame*&, JSValuePtr&, unsigned bytecodeOffset, bool);
316 NEVER_INLINE bool resolveBaseAndFunc(CallFrame*, Instruction*, JSValuePtr& exceptionValue);
317
318 static ALWAYS_INLINE CallFrame* slideRegisterWindowForCall(CodeBlock*, RegisterFile*, CallFrame*, size_t registerOffset, int argc);
319
320 static CallFrame* findFunctionCallFrame(CallFrame*, InternalFunction*);
321
322 JSValuePtr privateExecute(ExecutionFlag, RegisterFile*, CallFrame*, JSValuePtr* exception);
323
324 void dumpCallFrame(CallFrame*);
325 void dumpRegisters(CallFrame*);
326
327 bool checkTimeout(JSGlobalObject*);
328 void resetTimeoutCheck();
329
330 void tryCacheGetByID(CallFrame*, CodeBlock*, Instruction*, JSValuePtr baseValue, const Identifier& propertyName, const PropertySlot&);
331 void uncacheGetByID(CodeBlock*, Instruction* vPC);
332 void tryCachePutByID(CallFrame*, CodeBlock*, Instruction*, JSValuePtr baseValue, const PutPropertySlot&);
333 void uncachePutByID(CodeBlock*, Instruction* vPC);
334
335 bool isCallBytecode(Opcode opcode) { return opcode == getOpcode(op_call) || opcode == getOpcode(op_construct) || opcode == getOpcode(op_call_eval); }
336
337 #if ENABLE(JIT)
338 static void throwStackOverflowPreviousFrame(CallFrame**, JSGlobalData*, void*& returnAddress);
339
340 void tryCTICacheGetByID(CallFrame*, CodeBlock*, void* returnAddress, JSValuePtr baseValue, const Identifier& propertyName, const PropertySlot&);
341 void tryCTICachePutByID(CallFrame*, CodeBlock*, void* returnAddress, JSValuePtr baseValue, const PutPropertySlot&);
342 #endif
343
344 SamplingTool* m_sampler;
345
346 #if ENABLE(JIT)
347 RefPtr<ExecutablePool> m_executablePool;
348 void* m_ctiArrayLengthTrampoline;
349 void* m_ctiStringLengthTrampoline;
350 void* m_ctiVirtualCallPreLink;
351 void* m_ctiVirtualCallLink;
352 void* m_ctiVirtualCall;
353 #endif
354
355 int m_reentryDepth;
356 unsigned m_timeoutTime;
357 unsigned m_timeAtLastCheckTimeout;
358 unsigned m_timeExecuting;
359 unsigned m_timeoutCheckCount;
360 unsigned m_ticksUntilNextTimeoutCheck;
361
362 RegisterFile m_registerFile;
363
364 void* m_jsArrayVptr;
365 void* m_jsByteArrayVptr;
366 void* m_jsStringVptr;
367 void* m_jsFunctionVptr;
368
369 #if HAVE(COMPUTED_GOTO)
370 Opcode m_opcodeTable[numOpcodeIDs]; // Maps OpcodeID => Opcode for compiling
371 HashMap<Opcode, OpcodeID> m_opcodeIDTable; // Maps Opcode => OpcodeID for decompiling
372 #endif
373 };
374
375 } // namespace JSC
376
377 #endif // Interpreter_h