tizen beta release
[profile/ivi/webkit-efl.git] / Source / 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 "JSObject.h"
36 #include "Opcode.h"
37 #include "RegisterFile.h"
38
39 #include <wtf/HashMap.h>
40
41 namespace JSC {
42
43     class CodeBlock;
44     class EvalExecutable;
45     class FunctionExecutable;
46     class JSFunction;
47     class JSGlobalObject;
48     class ProgramExecutable;
49     class Register;
50     class ScopeChainNode;
51     class SamplingTool;
52     struct CallFrameClosure;
53     struct HandlerInfo;
54     struct Instruction;
55     
56     enum DebugHookID {
57         WillExecuteProgram,
58         DidExecuteProgram,
59         DidEnterCallFrame,
60         DidReachBreakpoint,
61         WillLeaveCallFrame,
62         WillExecuteStatement
63     };
64
65     class TopCallFrameSetter {
66     public:
67         TopCallFrameSetter(JSGlobalData& global, CallFrame* callFrame)
68             : globalData(global)
69             , oldCallFrame(global.topCallFrame) 
70         {
71             global.topCallFrame = callFrame;
72         }
73         
74         ~TopCallFrameSetter() 
75         {
76             globalData.topCallFrame = oldCallFrame;
77         }
78     private:
79         JSGlobalData& globalData;
80         CallFrame* oldCallFrame;
81     };
82
83 #if PLATFORM(IOS)
84     // We use a smaller reentrancy limit on iPhone because of the high amount of
85     // stack space required on the web thread.
86     enum { MaxLargeThreadReentryDepth = 93, MaxSmallThreadReentryDepth = 32 };
87 #else
88     enum { MaxLargeThreadReentryDepth = 256, MaxSmallThreadReentryDepth = 32 };
89 #endif // PLATFORM(IOS)
90
91     class Interpreter {
92         WTF_MAKE_FAST_ALLOCATED;
93         friend class JIT;
94         friend class CachedCall;
95     public:
96         Interpreter();
97
98         RegisterFile& registerFile() { return m_registerFile; }
99         
100         Opcode getOpcode(OpcodeID id)
101         {
102             #if ENABLE(COMPUTED_GOTO_INTERPRETER)
103                 return m_opcodeTable[id];
104             #else
105                 return id;
106             #endif
107         }
108
109         OpcodeID getOpcodeID(Opcode opcode)
110         {
111             #if ENABLE(COMPUTED_GOTO_INTERPRETER)
112                 ASSERT(isOpcode(opcode));
113                 return m_opcodeIDTable.get(opcode);
114             #else
115                 return opcode;
116             #endif
117         }
118
119         bool isOpcode(Opcode);
120
121         JSValue execute(ProgramExecutable*, CallFrame*, ScopeChainNode*, JSObject* thisObj);
122         JSValue executeCall(CallFrame*, JSObject* function, CallType, const CallData&, JSValue thisValue, const ArgList&);
123         JSObject* executeConstruct(CallFrame*, JSObject* function, ConstructType, const ConstructData&, const ArgList&);
124         JSValue execute(EvalExecutable*, CallFrame*, JSValue thisValue, ScopeChainNode*);
125         JSValue execute(EvalExecutable*, CallFrame*, JSValue thisValue, ScopeChainNode*, int globalRegisterOffset);
126
127         JSValue retrieveArguments(CallFrame*, JSFunction*) const;
128         JS_EXPORT_PRIVATE JSValue retrieveCaller(CallFrame*, JSFunction*) const;
129         JS_EXPORT_PRIVATE void retrieveLastCaller(CallFrame*, int& lineNumber, intptr_t& sourceID, UString& sourceURL, JSValue& function) const;
130         
131         void getArgumentsData(CallFrame*, JSFunction*&, ptrdiff_t& firstParameterIndex, Register*& argv, int& argc);
132         
133         SamplingTool* sampler() { return m_sampler.get(); }
134
135         NEVER_INLINE HandlerInfo* throwException(CallFrame*&, JSValue&, unsigned bytecodeOffset);
136         NEVER_INLINE void debug(CallFrame*, DebugHookID, int firstLine, int lastLine);
137
138         void dumpSampleData(ExecState* exec);
139         void startSampling();
140         void stopSampling();
141     private:
142         enum ExecutionFlag { Normal, InitializeAndReturn };
143
144         CallFrameClosure prepareForRepeatCall(FunctionExecutable*, CallFrame*, JSFunction*, int argumentCountIncludingThis, ScopeChainNode*);
145         void endRepeatCall(CallFrameClosure&);
146         JSValue execute(CallFrameClosure&);
147
148 #if ENABLE(INTERPRETER)
149         NEVER_INLINE bool resolve(CallFrame*, Instruction*, JSValue& exceptionValue);
150         NEVER_INLINE bool resolveSkip(CallFrame*, Instruction*, JSValue& exceptionValue);
151         NEVER_INLINE bool resolveGlobal(CallFrame*, Instruction*, JSValue& exceptionValue);
152         NEVER_INLINE bool resolveGlobalDynamic(CallFrame*, Instruction*, JSValue& exceptionValue);
153         NEVER_INLINE void resolveBase(CallFrame*, Instruction* vPC);
154         NEVER_INLINE bool resolveBaseAndProperty(CallFrame*, Instruction*, JSValue& exceptionValue);
155         NEVER_INLINE bool resolveThisAndProperty(CallFrame*, Instruction*, JSValue& exceptionValue);
156         NEVER_INLINE ScopeChainNode* createExceptionScope(CallFrame*, const Instruction* vPC);
157
158         void tryCacheGetByID(CallFrame*, CodeBlock*, Instruction*, JSValue baseValue, const Identifier& propertyName, const PropertySlot&);
159         void uncacheGetByID(CodeBlock*, Instruction* vPC);
160         void tryCachePutByID(CallFrame*, CodeBlock*, Instruction*, JSValue baseValue, const PutPropertySlot&);
161         void uncachePutByID(CodeBlock*, Instruction* vPC);        
162 #endif // ENABLE(INTERPRETER)
163
164         NEVER_INLINE bool unwindCallFrame(CallFrame*&, JSValue, unsigned& bytecodeOffset, CodeBlock*&);
165
166         static ALWAYS_INLINE CallFrame* slideRegisterWindowForCall(CodeBlock*, RegisterFile*, CallFrame*, size_t registerOffset, int argc);
167
168         static CallFrame* findFunctionCallFrame(CallFrame*, JSFunction*);
169
170         JSValue privateExecute(ExecutionFlag, RegisterFile*, CallFrame*);
171
172         void dumpCallFrame(CallFrame*);
173         void dumpRegisters(CallFrame*);
174         
175         bool isCallBytecode(Opcode opcode) { return opcode == getOpcode(op_call) || opcode == getOpcode(op_construct) || opcode == getOpcode(op_call_eval); }
176
177         void enableSampler();
178         int m_sampleEntryDepth;
179         OwnPtr<SamplingTool> m_sampler;
180
181         int m_reentryDepth;
182
183         RegisterFile m_registerFile;
184         
185 #if ENABLE(COMPUTED_GOTO_INTERPRETER)
186         Opcode m_opcodeTable[numOpcodeIDs]; // Maps OpcodeID => Opcode for compiling
187         HashMap<Opcode, OpcodeID> m_opcodeIDTable; // Maps Opcode => OpcodeID for decompiling
188 #endif
189     };
190
191     // This value must not be an object that would require this conversion (WebCore's global object).
192     inline bool isValidThisObject(JSValue thisValue, ExecState* exec)
193     {
194         return !thisValue.isObject() || thisValue.toThisObject(exec) == thisValue;
195     }
196
197     inline JSValue Interpreter::execute(EvalExecutable* eval, CallFrame* callFrame, JSValue thisValue, ScopeChainNode* scopeChain)
198     {
199         return execute(eval, callFrame, thisValue, scopeChain, m_registerFile.size() + 1 + RegisterFile::CallFrameHeaderSize);
200     }
201
202     JSValue eval(CallFrame*);
203     CallFrame* loadVarargs(CallFrame*, RegisterFile*, JSValue thisValue, JSValue arguments, int firstFreeRegister);
204
205 } // namespace JSC
206
207 #endif // Interpreter_h