2 * Copyright (C) 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
34 #include "JSFunction.h"
37 #include "LLIntData.h"
39 #include "RegisterFile.h"
41 #include <wtf/HashMap.h>
48 class FunctionExecutable;
50 class LLIntOffsetsExtractor;
51 class ProgramExecutable;
55 struct CallFrameClosure;
68 enum StackFrameCodeType {
71 StackFrameFunctionCode,
76 Strong<JSObject> callee;
77 StackFrameCodeType codeType;
78 Strong<ExecutableBase> executable;
81 UString toString(CallFrame* callFrame) const
83 bool hasSourceURLInfo = !sourceURL.isNull() && !sourceURL.isEmpty();
84 bool hasLineInfo = line > -1;
86 JSObject* stackFrameCallee = callee.get();
89 case StackFrameEvalCode:
90 if (hasSourceURLInfo) {
91 traceLine = hasLineInfo ? String::format("eval code@%s:%d", sourceURL.ascii().data(), line)
92 : String::format("eval code@%s", sourceURL.ascii().data());
94 traceLine = String::format("eval code");
96 case StackFrameNativeCode: {
98 UString functionName = getCalculatedDisplayName(callFrame, stackFrameCallee);
99 traceLine = String::format("%s@[native code]", functionName.ascii().data());
101 traceLine = "[native code]";
104 case StackFrameFunctionCode: {
105 UString functionName = getCalculatedDisplayName(callFrame, stackFrameCallee);
106 if (hasSourceURLInfo) {
107 traceLine = hasLineInfo ? String::format("%s@%s:%d", functionName.ascii().data(), sourceURL.ascii().data(), line)
108 : String::format("%s@%s", functionName.ascii().data(), sourceURL.ascii().data());
110 traceLine = String::format("%s\n", functionName.ascii().data());
113 case StackFrameGlobalCode:
114 if (hasSourceURLInfo) {
115 traceLine = hasLineInfo ? String::format("global code@%s:%d", sourceURL.ascii().data(), line)
116 : String::format("global code@%s", sourceURL.ascii().data());
118 traceLine = String::format("global code");
121 return traceLine.impl();
125 class TopCallFrameSetter {
127 TopCallFrameSetter(JSGlobalData& global, CallFrame* callFrame)
129 , oldCallFrame(global.topCallFrame)
131 global.topCallFrame = callFrame;
134 ~TopCallFrameSetter()
136 globalData.topCallFrame = oldCallFrame;
139 JSGlobalData& globalData;
140 CallFrame* oldCallFrame;
143 class NativeCallFrameTracer {
145 ALWAYS_INLINE NativeCallFrameTracer(JSGlobalData* global, CallFrame* callFrame)
149 global->topCallFrame = callFrame;
153 // We use a smaller reentrancy limit on iPhone because of the high amount of
154 // stack space required on the web thread.
156 enum { MaxLargeThreadReentryDepth = 64, MaxSmallThreadReentryDepth = 16 };
158 enum { MaxLargeThreadReentryDepth = 256, MaxSmallThreadReentryDepth = 16 };
159 #endif // PLATFORM(IOS)
162 WTF_MAKE_FAST_ALLOCATED;
163 friend class CachedCall;
164 friend class LLIntOffsetsExtractor;
170 void initialize(LLInt::Data*, bool canUseJIT);
172 RegisterFile& registerFile() { return m_registerFile; }
174 Opcode getOpcode(OpcodeID id)
176 ASSERT(m_initialized);
177 #if ENABLE(COMPUTED_GOTO_CLASSIC_INTERPRETER) || ENABLE(LLINT)
178 return m_opcodeTable[id];
184 OpcodeID getOpcodeID(Opcode opcode)
186 ASSERT(m_initialized);
188 ASSERT(isOpcode(opcode));
189 return m_opcodeIDTable.get(opcode);
190 #elif ENABLE(COMPUTED_GOTO_CLASSIC_INTERPRETER)
191 ASSERT(isOpcode(opcode));
192 if (!m_classicEnabled)
193 return static_cast<OpcodeID>(bitwise_cast<uintptr_t>(opcode));
195 return m_opcodeIDTable.get(opcode);
201 bool classicEnabled()
203 return m_classicEnabled;
206 bool isOpcode(Opcode);
208 JSValue execute(ProgramExecutable*, CallFrame*, ScopeChainNode*, JSObject* thisObj);
209 JSValue executeCall(CallFrame*, JSObject* function, CallType, const CallData&, JSValue thisValue, const ArgList&);
210 JSObject* executeConstruct(CallFrame*, JSObject* function, ConstructType, const ConstructData&, const ArgList&);
211 JSValue execute(EvalExecutable*, CallFrame*, JSValue thisValue, ScopeChainNode*);
212 JSValue execute(EvalExecutable*, CallFrame*, JSValue thisValue, ScopeChainNode*, int globalRegisterOffset);
214 JSValue retrieveArgumentsFromVMCode(CallFrame*, JSFunction*) const;
215 JSValue retrieveCallerFromVMCode(CallFrame*, JSFunction*) const;
216 JS_EXPORT_PRIVATE void retrieveLastCaller(CallFrame*, int& lineNumber, intptr_t& sourceID, UString& sourceURL, JSValue& function) const;
218 void getArgumentsData(CallFrame*, JSFunction*&, ptrdiff_t& firstParameterIndex, Register*& argv, int& argc);
220 SamplingTool* sampler() { return m_sampler.get(); }
222 NEVER_INLINE HandlerInfo* throwException(CallFrame*&, JSValue&, unsigned bytecodeOffset);
223 NEVER_INLINE void debug(CallFrame*, DebugHookID, int firstLine, int lastLine);
224 static const UString getTraceLine(CallFrame*, StackFrameCodeType, const UString&, int);
225 JS_EXPORT_PRIVATE static void getStackTrace(JSGlobalData*, Vector<StackFrame>& results);
226 static void addStackTraceIfNecessary(CallFrame*, JSObject* error);
228 void dumpSampleData(ExecState* exec);
229 void startSampling();
232 enum ExecutionFlag { Normal, InitializeAndReturn };
234 CallFrameClosure prepareForRepeatCall(FunctionExecutable*, CallFrame*, JSFunction*, int argumentCountIncludingThis, ScopeChainNode*);
235 void endRepeatCall(CallFrameClosure&);
236 JSValue execute(CallFrameClosure&);
238 #if ENABLE(CLASSIC_INTERPRETER)
239 NEVER_INLINE bool resolve(CallFrame*, Instruction*, JSValue& exceptionValue);
240 NEVER_INLINE bool resolveSkip(CallFrame*, Instruction*, JSValue& exceptionValue);
241 NEVER_INLINE bool resolveGlobal(CallFrame*, Instruction*, JSValue& exceptionValue);
242 NEVER_INLINE bool resolveGlobalDynamic(CallFrame*, Instruction*, JSValue& exceptionValue);
243 NEVER_INLINE void resolveBase(CallFrame*, Instruction* vPC);
244 NEVER_INLINE bool resolveBaseAndProperty(CallFrame*, Instruction*, JSValue& exceptionValue);
245 NEVER_INLINE bool resolveThisAndProperty(CallFrame*, Instruction*, JSValue& exceptionValue);
246 NEVER_INLINE ScopeChainNode* createExceptionScope(CallFrame*, const Instruction* vPC);
248 void tryCacheGetByID(CallFrame*, CodeBlock*, Instruction*, JSValue baseValue, const Identifier& propertyName, const PropertySlot&);
249 void uncacheGetByID(CodeBlock*, Instruction* vPC);
250 void tryCachePutByID(CallFrame*, CodeBlock*, Instruction*, JSValue baseValue, const PutPropertySlot&);
251 void uncachePutByID(CodeBlock*, Instruction* vPC);
252 #endif // ENABLE(CLASSIC_INTERPRETER)
254 NEVER_INLINE bool unwindCallFrame(CallFrame*&, JSValue, unsigned& bytecodeOffset, CodeBlock*&);
256 static ALWAYS_INLINE CallFrame* slideRegisterWindowForCall(CodeBlock*, RegisterFile*, CallFrame*, size_t registerOffset, int argc);
258 static CallFrame* findFunctionCallFrameFromVMCode(CallFrame*, JSFunction*);
260 JSValue privateExecute(ExecutionFlag, RegisterFile*, CallFrame*);
262 void dumpCallFrame(CallFrame*);
263 void dumpRegisters(CallFrame*);
265 bool isCallBytecode(Opcode opcode) { return opcode == getOpcode(op_call) || opcode == getOpcode(op_construct) || opcode == getOpcode(op_call_eval); }
267 void enableSampler();
268 int m_sampleEntryDepth;
269 OwnPtr<SamplingTool> m_sampler;
273 RegisterFile m_registerFile;
276 Opcode* m_opcodeTable; // Maps OpcodeID => Opcode for compiling
277 HashMap<Opcode, OpcodeID> m_opcodeIDTable; // Maps Opcode => OpcodeID for decompiling
278 #elif ENABLE(COMPUTED_GOTO_CLASSIC_INTERPRETER)
279 Opcode m_opcodeTable[numOpcodeIDs]; // Maps OpcodeID => Opcode for compiling
280 HashMap<Opcode, OpcodeID> m_opcodeIDTable; // Maps Opcode => OpcodeID for decompiling
286 bool m_classicEnabled;
289 // This value must not be an object that would require this conversion (WebCore's global object).
290 inline bool isValidThisObject(JSValue thisValue, ExecState* exec)
292 return !thisValue.isObject() || thisValue.toThisObject(exec) == thisValue;
295 inline JSValue Interpreter::execute(EvalExecutable* eval, CallFrame* callFrame, JSValue thisValue, ScopeChainNode* scopeChain)
297 return execute(eval, callFrame, thisValue, scopeChain, m_registerFile.size() + 1 + RegisterFile::CallFrameHeaderSize);
300 JSValue eval(CallFrame*);
301 CallFrame* loadVarargs(CallFrame*, RegisterFile*, JSValue thisValue, JSValue arguments, int firstFreeRegister);
305 #endif // Interpreter_h