<https://bugs.webkit.org/show_bug.cgi?id=23049> [jsfunfuzz] With blocks do not correc...
[WebKit-https.git] / JavaScriptCore / 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         JSValue* execute(ProgramNode*, CallFrame*, ScopeChainNode*, JSObject* thisObj, JSValue** exception);
144         JSValue* execute(FunctionBodyNode*, CallFrame*, JSFunction*, JSObject* thisObj, const ArgList& args, ScopeChainNode*, JSValue** exception);
145         JSValue* execute(EvalNode* evalNode, CallFrame* exec, JSObject* thisObj, ScopeChainNode* scopeChain, JSValue** exception);
146
147         JSValue* retrieveArguments(CallFrame*, JSFunction*) const;
148         JSValue* retrieveCaller(CallFrame*, InternalFunction*) const;
149         void retrieveLastCaller(CallFrame*, int& lineNumber, intptr_t& sourceID, UString& sourceURL, JSValue*& 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 JSValue* JIT_STUB cti_op_add(STUB_ARGS);
187         static JSValue* 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 JSValue* JIT_STUB cti_op_get_by_id(STUB_ARGS);
196         static JSValue* JIT_STUB cti_op_get_by_id_second(STUB_ARGS);
197         static JSValue* JIT_STUB cti_op_get_by_id_generic(STUB_ARGS);
198         static JSValue* JIT_STUB cti_op_get_by_id_self_fail(STUB_ARGS);
199         static JSValue* JIT_STUB cti_op_get_by_id_proto_list(STUB_ARGS);
200         static JSValue* JIT_STUB cti_op_get_by_id_proto_list_full(STUB_ARGS);
201         static JSValue* JIT_STUB cti_op_get_by_id_proto_fail(STUB_ARGS);
202         static JSValue* JIT_STUB cti_op_get_by_id_array_fail(STUB_ARGS);
203         static JSValue* JIT_STUB cti_op_get_by_id_string_fail(STUB_ARGS);
204         static JSValue* JIT_STUB cti_op_del_by_id(STUB_ARGS);
205         static JSValue* JIT_STUB cti_op_instanceof(STUB_ARGS);
206         static JSValue* 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 JSValue* 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 JSValue* JIT_STUB cti_op_resolve(STUB_ARGS);
220         static JSValue* JIT_STUB cti_op_resolve_global(STUB_ARGS);
221         static JSObject* JIT_STUB cti_op_construct_JSConstruct(STUB_ARGS);
222         static JSValue* JIT_STUB cti_op_construct_NotJSConstruct(STUB_ARGS);
223         static JSValue* JIT_STUB cti_op_get_by_val(STUB_ARGS);
224         static VoidPtrPair JIT_STUB cti_op_resolve_func(STUB_ARGS);
225         static JSValue* JIT_STUB cti_op_sub(STUB_ARGS);
226         static void JIT_STUB cti_op_put_by_val(STUB_ARGS);
227         static void JIT_STUB cti_op_put_by_val_array(STUB_ARGS);
228         static JSValue* JIT_STUB cti_op_lesseq(STUB_ARGS);
229         static int JIT_STUB cti_op_loop_if_true(STUB_ARGS);
230         static JSValue* JIT_STUB cti_op_resolve_base(STUB_ARGS);
231         static JSValue* JIT_STUB cti_op_negate(STUB_ARGS);
232         static JSValue* JIT_STUB cti_op_resolve_skip(STUB_ARGS);
233         static JSValue* JIT_STUB cti_op_div(STUB_ARGS);
234         static JSValue* JIT_STUB cti_op_pre_dec(STUB_ARGS);
235         static int JIT_STUB cti_op_jless(STUB_ARGS);
236         static JSValue* JIT_STUB cti_op_not(STUB_ARGS);
237         static int JIT_STUB cti_op_jtrue(STUB_ARGS);
238         static VoidPtrPair JIT_STUB cti_op_post_inc(STUB_ARGS);
239         static JSValue* JIT_STUB cti_op_eq(STUB_ARGS);
240         static JSValue* JIT_STUB cti_op_lshift(STUB_ARGS);
241         static JSValue* JIT_STUB cti_op_bitand(STUB_ARGS);
242         static JSValue* JIT_STUB cti_op_rshift(STUB_ARGS);
243         static JSValue* JIT_STUB cti_op_bitnot(STUB_ARGS);
244         static VoidPtrPair JIT_STUB cti_op_resolve_with_base(STUB_ARGS);
245         static JSObject* JIT_STUB cti_op_new_func_exp(STUB_ARGS);
246         static JSValue* JIT_STUB cti_op_mod(STUB_ARGS);
247         static JSValue* JIT_STUB cti_op_less(STUB_ARGS);
248         static JSValue* JIT_STUB cti_op_neq(STUB_ARGS);
249         static VoidPtrPair JIT_STUB cti_op_post_dec(STUB_ARGS);
250         static JSValue* JIT_STUB cti_op_urshift(STUB_ARGS);
251         static JSValue* JIT_STUB cti_op_bitxor(STUB_ARGS);
252         static JSObject* JIT_STUB cti_op_new_regexp(STUB_ARGS);
253         static JSValue* JIT_STUB cti_op_bitor(STUB_ARGS);
254         static JSValue* JIT_STUB cti_op_call_eval(STUB_ARGS);
255         static JSValue* JIT_STUB cti_op_throw(STUB_ARGS);
256         static JSPropertyNameIterator* JIT_STUB cti_op_get_pnames(STUB_ARGS);
257         static JSValue* JIT_STUB cti_op_next_pname(STUB_ARGS);
258         static JSObject* JIT_STUB cti_op_push_scope(STUB_ARGS);
259         static void JIT_STUB cti_op_pop_scope(STUB_ARGS);
260         static JSValue* JIT_STUB cti_op_typeof(STUB_ARGS);
261         static JSValue* JIT_STUB cti_op_is_undefined(STUB_ARGS);
262         static JSValue* JIT_STUB cti_op_is_boolean(STUB_ARGS);
263         static JSValue* JIT_STUB cti_op_is_number(STUB_ARGS);
264         static JSValue* JIT_STUB cti_op_is_string(STUB_ARGS);
265         static JSValue* JIT_STUB cti_op_is_object(STUB_ARGS);
266         static JSValue* JIT_STUB cti_op_is_function(STUB_ARGS);
267         static JSValue* JIT_STUB cti_op_stricteq(STUB_ARGS);
268         static JSValue* JIT_STUB cti_op_nstricteq(STUB_ARGS);
269         static JSValue* JIT_STUB cti_op_to_jsnumber(STUB_ARGS);
270         static JSValue* JIT_STUB cti_op_in(STUB_ARGS);
271         static JSObject* JIT_STUB cti_op_push_new_scope(STUB_ARGS);
272         static void JIT_STUB cti_op_jmp_scopes(STUB_ARGS);
273         static void JIT_STUB cti_op_put_by_index(STUB_ARGS);
274         static void* JIT_STUB cti_op_switch_imm(STUB_ARGS);
275         static void* JIT_STUB cti_op_switch_char(STUB_ARGS);
276         static void* JIT_STUB cti_op_switch_string(STUB_ARGS);
277         static JSValue* JIT_STUB cti_op_del_by_val(STUB_ARGS);
278         static void JIT_STUB cti_op_put_getter(STUB_ARGS);
279         static void JIT_STUB cti_op_put_setter(STUB_ARGS);
280         static JSObject* JIT_STUB cti_op_new_error(STUB_ARGS);
281         static void JIT_STUB cti_op_debug(STUB_ARGS);
282
283         static JSValue* JIT_STUB cti_vm_throw(STUB_ARGS);
284         static void* JIT_STUB cti_vm_dontLazyLinkCall(STUB_ARGS);
285         static void* JIT_STUB cti_vm_lazyLinkCall(STUB_ARGS);
286         static JSObject* JIT_STUB cti_op_push_activation(STUB_ARGS);
287         
288 #endif // ENABLE(JIT)
289
290         // Default number of ticks before a timeout check should be done.
291         static const int initialTickCountThreshold = 1024;
292
293         bool isJSArray(JSValue* v) { return !JSImmediate::isImmediate(v) && v->asCell()->vptr() == m_jsArrayVptr; }
294         bool isJSString(JSValue* v) { return !JSImmediate::isImmediate(v) && v->asCell()->vptr() == m_jsStringVptr; }
295
296     private:
297         enum ExecutionFlag { Normal, InitializeAndReturn };
298
299         NEVER_INLINE JSValue* callEval(CallFrame*, RegisterFile*, Register* argv, int argc, int registerOffset, JSValue*& exceptionValue);
300         JSValue* execute(EvalNode*, CallFrame*, JSObject* thisObject, int globalRegisterOffset, ScopeChainNode*, JSValue** exception);
301
302         NEVER_INLINE void debug(CallFrame*, DebugHookID, int firstLine, int lastLine);
303
304         NEVER_INLINE bool resolve(CallFrame*, Instruction*, JSValue*& exceptionValue);
305         NEVER_INLINE bool resolveSkip(CallFrame*, Instruction*, JSValue*& exceptionValue);
306         NEVER_INLINE bool resolveGlobal(CallFrame*, Instruction*, JSValue*& exceptionValue);
307         NEVER_INLINE void resolveBase(CallFrame*, Instruction* vPC);
308         NEVER_INLINE bool resolveBaseAndProperty(CallFrame*, Instruction*, JSValue*& exceptionValue);
309         NEVER_INLINE ScopeChainNode* createExceptionScope(CallFrame*, const Instruction* vPC);
310
311         NEVER_INLINE bool unwindCallFrame(CallFrame*&, JSValue*, unsigned& bytecodeOffset, CodeBlock*&);
312         NEVER_INLINE HandlerInfo* throwException(CallFrame*&, JSValue*&, unsigned bytecodeOffset, bool);
313         NEVER_INLINE bool resolveBaseAndFunc(CallFrame*, Instruction*, JSValue*& exceptionValue);
314
315         static ALWAYS_INLINE CallFrame* slideRegisterWindowForCall(CodeBlock*, RegisterFile*, CallFrame*, size_t registerOffset, int argc);
316
317         static CallFrame* findFunctionCallFrame(CallFrame*, InternalFunction*);
318
319         JSValue* privateExecute(ExecutionFlag, RegisterFile*, CallFrame*, JSValue** exception);
320
321         void dumpCallFrame(CallFrame*);
322         void dumpRegisters(CallFrame*);
323
324         JSValue* checkTimeout(JSGlobalObject*);
325         void resetTimeoutCheck();
326
327         void tryCacheGetByID(CallFrame*, CodeBlock*, Instruction*, JSValue* baseValue, const Identifier& propertyName, const PropertySlot&);
328         void uncacheGetByID(CodeBlock*, Instruction* vPC);
329         void tryCachePutByID(CallFrame*, CodeBlock*, Instruction*, JSValue* baseValue, const PutPropertySlot&);
330         void uncachePutByID(CodeBlock*, Instruction* vPC);
331         
332         bool isCallBytecode(Opcode opcode) { return opcode == getOpcode(op_call) || opcode == getOpcode(op_construct) || opcode == getOpcode(op_call_eval); }
333
334 #if ENABLE(JIT)
335         static void throwStackOverflowPreviousFrame(CallFrame**, JSGlobalData*, void*& returnAddress);
336
337         void tryCTICacheGetByID(CallFrame*, CodeBlock*, void* returnAddress, JSValue* baseValue, const Identifier& propertyName, const PropertySlot&);
338         void tryCTICachePutByID(CallFrame*, CodeBlock*, void* returnAddress, JSValue* baseValue, const PutPropertySlot&);
339 #endif
340
341         SamplingTool* m_sampler;
342
343 #if ENABLE(JIT)
344         RefPtr<ExecutablePool> m_executablePool;
345         void* m_ctiArrayLengthTrampoline;
346         void* m_ctiStringLengthTrampoline;
347         void* m_ctiVirtualCallPreLink;
348         void* m_ctiVirtualCallLink;
349         void* m_ctiVirtualCall;
350 #endif
351
352         int m_reentryDepth;
353         unsigned m_timeoutTime;
354         unsigned m_timeAtLastCheckTimeout;
355         unsigned m_timeExecuting;
356         unsigned m_timeoutCheckCount;
357         unsigned m_ticksUntilNextTimeoutCheck;
358
359         RegisterFile m_registerFile;
360         
361         void* m_jsArrayVptr;
362         void* m_jsStringVptr;
363         void* m_jsFunctionVptr;
364
365 #if HAVE(COMPUTED_GOTO)
366         Opcode m_opcodeTable[numOpcodeIDs]; // Maps OpcodeID => Opcode for compiling
367         HashMap<Opcode, OpcodeID> m_opcodeIDTable; // Maps Opcode => OpcodeID for decompiling
368 #endif
369     };
370
371 } // namespace JSC
372
373 #endif // Interpreter_h