source: webkit/trunk/JavaScriptCore/VM/Machine.h@ 38137

Last change on this file since 38137 was 37998, checked in by [email protected], 17 years ago

Try naming VoidPtrPair struct to fix old gcc/MSVC builds

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