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