summaryrefslogtreecommitdiffstats
path: root/JavaScriptCore/VM/Machine.h
blob: 6cb8aaa11b2b021cddd98e8441356fd6ae212aa6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
 * Copyright (C) 2008 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
 *     its contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef Machine_h
#define Machine_h

#include "ArgList.h"
#include "JSCell.h"
#include "JSValue.h"
#include "Opcode.h"
#include "RegisterFile.h"
#include <wtf/HashMap.h>

#ifdef ANDROID_INSTRUMENT
#include "TimeCounter.h"
#endif

namespace JSC {

    class CodeBlock;
    class EvalNode;
    class FunctionBodyNode;
    class Instruction;
    class InternalFunction;
    class JITCodeBuffer;
    class JSFunction;
    class JSGlobalObject;
    class ProgramNode;
    class Register;
    class ScopeChainNode;
    class SamplingTool;

#if ENABLE(CTI)

#if USE(CTI_ARGUMENT)
#define CTI_ARGS void** args
#define ARGS (args)
#else
#define CTI_ARGS void* args, ...
#define ARGS (&args)
#endif

#if USE(FAST_CALL_CTI_ARGUMENT)

#if COMPILER(MSVC)
#define SFX_CALL __fastcall
#elif COMPILER(GCC)
#define SFX_CALL  __attribute__ ((fastcall))
#else
#error Need to support fastcall calling convention in this compiler
#endif

#else

#if COMPILER(MSVC)
#define SFX_CALL __cdecl
#else
#define SFX_CALL
#endif

#endif

    typedef uint64_t VoidPtrPair;

    typedef union
    {
        struct { void* first; void* second; } s;
        VoidPtrPair i;
    } VoidPtrPairValue;
#endif

    enum DebugHookID {
        WillExecuteProgram,
        DidExecuteProgram,
        DidEnterCallFrame,
        DidReachBreakpoint,
        WillLeaveCallFrame,
        WillExecuteStatement
    };

    enum { MaxReentryDepth = 128 };

    class Machine {
        friend class CTI;
    public:
        Machine();
        ~Machine();
        
        RegisterFile& registerFile() { return m_registerFile; }
        
        Opcode getOpcode(OpcodeID id)
        {
            #if HAVE(COMPUTED_GOTO)
                return m_opcodeTable[id];
            #else
                return id;
            #endif
        }

        OpcodeID getOpcodeID(Opcode opcode)
        {
            #if HAVE(COMPUTED_GOTO)
                ASSERT(isOpcode(opcode));
                return m_opcodeIDTable.get(opcode);
            #else
                return opcode;
            #endif
        }

        bool isOpcode(Opcode opcode);
        
        JSValue* execute(ProgramNode*, CallFrame*, ScopeChainNode*, JSObject* thisObj, JSValue** exception);
        JSValue* execute(FunctionBodyNode*, CallFrame*, JSFunction*, JSObject* thisObj, const ArgList& args, ScopeChainNode*, JSValue** exception);
        JSValue* execute(EvalNode* evalNode, CallFrame* exec, JSObject* thisObj, ScopeChainNode* scopeChain, JSValue** exception);

        JSValue* retrieveArguments(CallFrame*, JSFunction*) const;
        JSValue* retrieveCaller(CallFrame*, InternalFunction*) const;
        void retrieveLastCaller(CallFrame*, int& lineNumber, intptr_t& sourceID, UString& sourceURL, JSValue*& function) const;
        
        void getArgumentsData(CallFrame*, JSFunction*&, ptrdiff_t& firstParameterIndex, Register*& argv, int& argc);
        void setTimeoutTime(unsigned timeoutTime) { m_timeoutTime = timeoutTime; }
        
        void startTimeoutCheck()
        {
            if (!m_timeoutCheckCount)
                resetTimeoutCheck();
#ifdef ANDROID_INSTRUMENT
            if (!m_timeoutCheckCount)
                android::TimeCounter::start(android::TimeCounter::JavaScriptTimeCounter);
#endif
            ++m_timeoutCheckCount;
        }
        
        void stopTimeoutCheck()
        {
            ASSERT(m_timeoutCheckCount);
            --m_timeoutCheckCount;
#ifdef ANDROID_INSTRUMENT
            if (!m_timeoutCheckCount)
                android::TimeCounter::record(android::TimeCounter::JavaScriptTimeCounter, __FUNCTION__);
#endif
        }

        inline void initTimeout()
        {
            ASSERT(!m_timeoutCheckCount);
            resetTimeoutCheck();
            m_timeoutTime = 0;
            m_timeoutCheckCount = 0;
        }

        void setSampler(SamplingTool* sampler) { m_sampler = sampler; }
        SamplingTool* sampler() { return m_sampler; }

#if ENABLE(CTI)

        static void SFX_CALL cti_timeout_check(CTI_ARGS);
        static void SFX_CALL cti_register_file_check(CTI_ARGS);

        static JSObject* SFX_CALL cti_op_convert_this(CTI_ARGS);
        static void SFX_CALL cti_op_end(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_add(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_pre_inc(CTI_ARGS);
        static int SFX_CALL cti_op_loop_if_less(CTI_ARGS);
        static int SFX_CALL cti_op_loop_if_lesseq(CTI_ARGS);
        static JSObject* SFX_CALL cti_op_new_object(CTI_ARGS);
        static void SFX_CALL cti_op_put_by_id(CTI_ARGS);
        static void SFX_CALL cti_op_put_by_id_second(CTI_ARGS);
        static void SFX_CALL cti_op_put_by_id_generic(CTI_ARGS);
        static void SFX_CALL cti_op_put_by_id_fail(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_get_by_id(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_get_by_id_second(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_get_by_id_generic(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_get_by_id_fail(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_del_by_id(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_instanceof(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_mul(CTI_ARGS);
        static JSObject* SFX_CALL cti_op_new_func(CTI_ARGS);
        static VoidPtrPair SFX_CALL cti_op_call_JSFunction(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_call_NotJSFunction(CTI_ARGS);
        static void SFX_CALL cti_op_create_arguments(CTI_ARGS);
        static void SFX_CALL cti_op_create_arguments_no_params(CTI_ARGS);
        static void SFX_CALL cti_op_tear_off_activation(CTI_ARGS);
        static void SFX_CALL cti_op_tear_off_arguments(CTI_ARGS);
        static void SFX_CALL cti_op_profile_will_call(CTI_ARGS);
        static void SFX_CALL cti_op_profile_did_call(CTI_ARGS);
        static void SFX_CALL cti_op_ret_scopeChain(CTI_ARGS);
        static JSObject* SFX_CALL cti_op_new_array(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_resolve(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_resolve_global(CTI_ARGS);
        static JSObject* SFX_CALL cti_op_construct_JSConstructFast(CTI_ARGS);
        static VoidPtrPair SFX_CALL cti_op_construct_JSConstruct(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_construct_NotJSConstruct(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_get_by_val(CTI_ARGS);
        static VoidPtrPair SFX_CALL cti_op_resolve_func(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_sub(CTI_ARGS);
        static void SFX_CALL cti_op_put_by_val(CTI_ARGS);
        static void SFX_CALL cti_op_put_by_val_array(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_lesseq(CTI_ARGS);
        static int SFX_CALL cti_op_loop_if_true(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_resolve_base(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_negate(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_resolve_skip(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_div(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_pre_dec(CTI_ARGS);
        static int SFX_CALL cti_op_jless(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_not(CTI_ARGS);
        static int SFX_CALL cti_op_jtrue(CTI_ARGS);
        static VoidPtrPair SFX_CALL cti_op_post_inc(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_eq(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_lshift(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_bitand(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_rshift(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_bitnot(CTI_ARGS);
        static VoidPtrPair SFX_CALL cti_op_resolve_with_base(CTI_ARGS);
        static JSObject* SFX_CALL cti_op_new_func_exp(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_mod(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_less(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_neq(CTI_ARGS);
        static VoidPtrPair SFX_CALL cti_op_post_dec(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_urshift(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_bitxor(CTI_ARGS);
        static JSObject* SFX_CALL cti_op_new_regexp(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_bitor(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_call_eval(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_throw(CTI_ARGS);
        static JSPropertyNameIterator* SFX_CALL cti_op_get_pnames(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_next_pname(CTI_ARGS);
        static void SFX_CALL cti_op_push_scope(CTI_ARGS);
        static void SFX_CALL cti_op_pop_scope(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_typeof(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_is_undefined(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_is_boolean(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_is_number(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_is_string(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_is_object(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_is_function(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_stricteq(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_nstricteq(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_to_jsnumber(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_in(CTI_ARGS);
        static JSObject* SFX_CALL cti_op_push_new_scope(CTI_ARGS);
        static void SFX_CALL cti_op_jmp_scopes(CTI_ARGS);
        static void SFX_CALL cti_op_put_by_index(CTI_ARGS);
        static void* SFX_CALL cti_op_switch_imm(CTI_ARGS);
        static void* SFX_CALL cti_op_switch_char(CTI_ARGS);
        static void* SFX_CALL cti_op_switch_string(CTI_ARGS);
        static JSValue* SFX_CALL cti_op_del_by_val(CTI_ARGS);
        static void SFX_CALL cti_op_put_getter(CTI_ARGS);
        static void SFX_CALL cti_op_put_setter(CTI_ARGS);
        static JSObject* SFX_CALL cti_op_new_error(CTI_ARGS);
        static void SFX_CALL cti_op_debug(CTI_ARGS);

        static JSValue* SFX_CALL cti_allocate_number(CTI_ARGS);

        static JSValue* SFX_CALL cti_vm_throw(CTI_ARGS);
        static void* SFX_CALL cti_vm_compile(CTI_ARGS);
        static void* SFX_CALL cti_vm_lazyLinkCall(CTI_ARGS);
        static JSObject* SFX_CALL cti_op_push_activation(CTI_ARGS);
        
#endif // ENABLE(CTI)

        // Default number of ticks before a timeout check should be done.
        static const int initialTickCountThreshold = 1024;

        bool isJSArray(JSValue* v) { return !JSImmediate::isImmediate(v) && v->asCell()->vptr() == m_jsArrayVptr; }
        bool isJSString(JSValue* v) { return !JSImmediate::isImmediate(v) && v->asCell()->vptr() == m_jsStringVptr; }

    private:
        enum ExecutionFlag { Normal, InitializeAndReturn };

        NEVER_INLINE JSValue* callEval(CallFrame*, JSObject* thisObject, ScopeChainNode*, RegisterFile*, int argv, int argc, JSValue*& exceptionValue);
        JSValue* execute(EvalNode*, CallFrame*, JSObject* thisObject, int registerOffset, ScopeChainNode*, JSValue** exception);

        NEVER_INLINE void debug(CallFrame*, DebugHookID, int firstLine, int lastLine);

        NEVER_INLINE bool resolve(CallFrame*, Instruction*, JSValue*& exceptionValue);
        NEVER_INLINE bool resolveSkip(CallFrame*, Instruction*, JSValue*& exceptionValue);
        NEVER_INLINE bool resolveGlobal(CallFrame*, Instruction*, JSValue*& exceptionValue);
        NEVER_INLINE void resolveBase(CallFrame*, Instruction* vPC);
        NEVER_INLINE bool resolveBaseAndProperty(CallFrame*, Instruction*, JSValue*& exceptionValue);
        NEVER_INLINE ScopeChainNode* createExceptionScope(CallFrame*, const Instruction* vPC);

        NEVER_INLINE bool unwindCallFrame(CallFrame*&, JSValue*, const Instruction*&, CodeBlock*&);
        NEVER_INLINE Instruction* throwException(CallFrame*&, JSValue*&, const Instruction*, bool);
        NEVER_INLINE bool resolveBaseAndFunc(CallFrame*, Instruction*, JSValue*& exceptionValue);

        static ALWAYS_INLINE CallFrame* slideRegisterWindowForCall(CodeBlock*, RegisterFile*, CallFrame*, size_t registerOffset, int argc);

        static CallFrame* findFunctionCallFrame(CallFrame*, InternalFunction*);

        JSValue* privateExecute(ExecutionFlag, RegisterFile*, CallFrame*, JSValue** exception);

        void dumpCallFrame(const RegisterFile*, CallFrame*);
        void dumpRegisters(const RegisterFile*, CallFrame*);

        JSValue* checkTimeout(JSGlobalObject*);
        void resetTimeoutCheck();

        void tryCacheGetByID(CallFrame*, CodeBlock*, Instruction*, JSValue* baseValue, const Identifier& propertyName, const PropertySlot&);
        void uncacheGetByID(CodeBlock*, Instruction* vPC);
        void tryCachePutByID(CallFrame*, CodeBlock*, Instruction*, JSValue* baseValue, const PutPropertySlot&);
        void uncachePutByID(CodeBlock*, Instruction* vPC);
        
        bool isCallOpcode(Opcode opcode) { return opcode == getOpcode(op_call) || opcode == getOpcode(op_construct) || opcode == getOpcode(op_call_eval); }

#if ENABLE(CTI)
        static void throwStackOverflowPreviousFrame(CallFrame*, JSGlobalData*, void*& returnAddress);

        void tryCTICacheGetByID(CallFrame*, CodeBlock*, void* returnAddress, JSValue* baseValue, const Identifier& propertyName, const PropertySlot&);
        void tryCTICachePutByID(CallFrame*, CodeBlock*, void* returnAddress, JSValue* baseValue, const PutPropertySlot&);

        void* getCTIArrayLengthTrampoline(CallFrame*, CodeBlock*);
        void* getCTIStringLengthTrampoline(CallFrame*, CodeBlock*);

        JITCodeBuffer* jitCodeBuffer() const { return m_jitCodeBuffer.get(); }
#endif

        SamplingTool* m_sampler;

#if ENABLE(CTI)
        void* m_ctiArrayLengthTrampoline;
        void* m_ctiStringLengthTrampoline;

        OwnPtr<JITCodeBuffer> m_jitCodeBuffer;
#endif

        int m_reentryDepth;
        unsigned m_timeoutTime;
        unsigned m_timeAtLastCheckTimeout;
        unsigned m_timeExecuting;
        unsigned m_timeoutCheckCount;
        unsigned m_ticksUntilNextTimeoutCheck;

        RegisterFile m_registerFile;
        
        void* m_jsArrayVptr;
        void* m_jsStringVptr;
        void* m_jsFunctionVptr;

#if HAVE(COMPUTED_GOTO)
        Opcode m_opcodeTable[numOpcodeIDs]; // Maps OpcodeID => Opcode for compiling
        HashMap<Opcode, OpcodeID> m_opcodeIDTable; // Maps Opcode => OpcodeID for decompiling
#endif
    };

} // namespace JSC

#endif // Machine_h