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
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #define WTF_USE_CTI_REPATCH_PIC 1
34 #include "RegisterFile.h"
35 #include <masm/X86Assembler.h>
36 #include <profiler/Profiler.h>
37 #include <wtf/AlwaysInline.h>
38 #include <wtf/Vector.h>
40 #if ENABLE(SAMPLING_TOOL)
41 #include "SamplingTool.h"
45 #define CTI_ARGS void** args
48 #define CTI_ARGS void* args
52 #define CTI_ARGS_2ndResult 0x08
54 #define CTI_ARGS_code 0x0C
55 #define CTI_ARGS_exec 0x0D
56 #define CTI_ARGS_registerFile 0x0E
57 #define CTI_ARGS_r 0x0F
58 #define CTI_ARGS_scopeChain 0x10
59 #define CTI_ARGS_codeBlock 0x11
60 #define CTI_ARGS_exception 0x12
61 #define CTI_ARGS_profilerReference 0x13
62 #define ARG_exec ((ExecState*)(ARGS)[CTI_ARGS_exec])
63 #define ARG_registerFile ((RegisterFile*)(ARGS)[CTI_ARGS_registerFile])
64 #define ARG_r ((Register*)(ARGS)[CTI_ARGS_r])
65 #define ARG_scopeChain ((ScopeChainNode*)(ARGS)[CTI_ARGS_scopeChain])
66 #define ARG_codeBlock ((CodeBlock*)(ARGS)[CTI_ARGS_codeBlock])
67 #define ARG_exception ((JSValue**)(ARGS)[CTI_ARGS_exception])
68 #define ARG_profilerReference ((Profiler**)(ARGS)[CTI_ARGS_profilerReference])
70 #define ARG_setScopeChain(newScopeChain) (*(volatile ScopeChainNode**)&(ARGS)[CTI_ARGS_scopeChain] = newScopeChain)
71 #define ARG_setCodeBlock(newCodeBlock) (*(volatile CodeBlock**)&(ARGS)[CTI_ARGS_codeBlock] = newCodeBlock)
72 #define ARG_setR(newR) (*(volatile Register**)&(ARGS)[CTI_ARGS_r] = newR)
73 #define ARG_set2ndResult(new2ndResult) (*(volatile JSValue**)&(ARGS)[CTI_ARGS_2ndResult] = new2ndResult)
75 #define ARG_src1 ((JSValue*)((ARGS)[1]))
76 #define ARG_src2 ((JSValue*)((ARGS)[2]))
77 #define ARG_src3 ((JSValue*)((ARGS)[3]))
78 #define ARG_src4 ((JSValue*)((ARGS)[4]))
79 #define ARG_id1 ((Identifier*)((ARGS)[1]))
80 #define ARG_id2 ((Identifier*)((ARGS)[2]))
81 #define ARG_id3 ((Identifier*)((ARGS)[3]))
82 #define ARG_id4 ((Identifier*)((ARGS)[4]))
83 #define ARG_int1 ((int)((ARGS)[1]))
84 #define ARG_int2 ((int)((ARGS)[2]))
85 #define ARG_int3 ((int)((ARGS)[3]))
86 #define ARG_int4 ((int)((ARGS)[4]))
87 #define ARG_func1 ((FuncDeclNode*)((ARGS)[1]))
88 #define ARG_funcexp1 ((FuncExprNode*)((ARGS)[1]))
89 #define ARG_registers1 ((Register*)((ARGS)[1]))
90 #define ARG_regexp1 ((RegExp*)((ARGS)[1]))
91 #define ARG_pni1 ((JSPropertyNameIterator*)((ARGS)[1]))
92 #define ARG_instr4 ((Instruction*)((ARGS)[4]))
93 #define ARG_instr5 ((Instruction*)((ARGS)[5]))
95 #define CTI_RETURN_ADDRESS ((ARGS)[-1])
101 class JSPropertyNameIterator;
106 class ScopeChainNode;
107 class SimpleJumpTable;
108 class StringJumpTable;
109 class StructureIDChain;
112 typedef JSValue* (*CTIHelper_j)(CTI_ARGS);
113 typedef JSPropertyNameIterator* (*CTIHelper_p)(CTI_ARGS);
114 typedef void (*CTIHelper_v)(CTI_ARGS);
115 typedef void* (*CTIHelper_s)(CTI_ARGS);
116 typedef int (*CTIHelper_b)(CTI_ARGS);
119 X86Assembler::JmpSrc from;
121 unsigned opcodeIndex;
127 CallRecord(X86Assembler::JmpSrc f, CTIHelper_j t, unsigned i)
134 CallRecord(X86Assembler::JmpSrc f, CTIHelper_p t, unsigned i)
141 CallRecord(X86Assembler::JmpSrc f, CTIHelper_v t, unsigned i)
148 CallRecord(X86Assembler::JmpSrc f, CTIHelper_s t, unsigned i)
155 CallRecord(X86Assembler::JmpSrc f, CTIHelper_b t, unsigned i)
164 X86Assembler::JmpSrc from;
167 JmpTable(X86Assembler::JmpSrc f, unsigned t)
174 struct SlowCaseEntry {
175 X86Assembler::JmpSrc from;
179 SlowCaseEntry(X86Assembler::JmpSrc f, unsigned t, unsigned h = 0)
187 struct SwitchRecord {
197 SimpleJumpTable* m_simpleJumpTable;
198 StringJumpTable* m_stringJumpTable;
201 unsigned m_opcodeIndex;
202 unsigned m_defaultOffset;
204 SwitchRecord(SimpleJumpTable* jumpTable, unsigned opcodeIndex, unsigned defaultOffset, Type type)
206 , m_opcodeIndex(opcodeIndex)
207 , m_defaultOffset(defaultOffset)
209 m_jumpTable.m_simpleJumpTable = jumpTable;
212 SwitchRecord(StringJumpTable* jumpTable, unsigned opcodeIndex, unsigned defaultOffset)
214 , m_opcodeIndex(opcodeIndex)
215 , m_defaultOffset(defaultOffset)
217 m_jumpTable.m_stringJumpTable = jumpTable;
221 struct StructureStubCompilationInfo {
222 X86Assembler::JmpSrc callReturnLocation;
223 X86Assembler::JmpDst hotPathBegin;
227 JSValue* ctiTrampoline(void* code, ExecState* exec, RegisterFile* registerFile, Register* r, ScopeChainNode* scopeChain, CodeBlock* codeBlock, JSValue** exception, Profiler**);
228 void ctiVMThrowTrampoline();
231 void ctiSetReturnAddress(void** where, void* what);
232 void ctiRepatchCallByReturnAddress(void* where, void* what);
235 static const int repatchGetByIdDefaultStructureID = -1;
236 // Magic number - initial offset cannot be representable as a signed 8bit value, or the X86Assembler
237 // will compress the displacement, and we may not be able to fit a repatched offset.
238 static const int repatchGetByIdDefaultOffset = 256;
240 // These architecture specific value are used to enable repatching - see comment on op_put_by_id.
241 static const int repatchOffsetPutByIdStructureID = 19;
242 static const int repatchOffsetPutByIdPropertyMapOffset = 34;
243 // These architecture specific value are used to enable repatching - see comment on op_get_by_id.
244 static const int repatchOffsetGetByIdStructureID = 19;
245 static const int repatchOffsetGetByIdBranchToSlowCase = 25;
246 static const int repatchOffsetGetByIdPropertyMapOffset = 34;
247 static const int repatchOffsetGetByIdSlowCaseCall = 17;
250 static void compile(Machine* machine, ExecState* exec, CodeBlock* codeBlock)
252 CTI cti(machine, exec, codeBlock);
253 cti.privateCompile();
257 static void* compileRegExp(ExecState* exec, const UString& pattern, unsigned* numSubpatterns_ptr, const char** error_ptr, bool ignoreCase = false, bool multiline = false);
260 static void compileGetByIdSelf(Machine* machine, ExecState* exec, CodeBlock* codeBlock, StructureID* structureID, size_t cachedOffset, void* returnAddress)
262 CTI cti(machine, exec, codeBlock);
263 cti.privateCompileGetByIdSelf(structureID, cachedOffset, returnAddress);
266 static void compileGetByIdProto(Machine* machine, ExecState* exec, CodeBlock* codeBlock, StructureID* structureID, StructureID* prototypeStructureID, size_t cachedOffset, void* returnAddress)
268 CTI cti(machine, exec, codeBlock);
269 cti.privateCompileGetByIdProto(structureID, prototypeStructureID, cachedOffset, returnAddress);
272 static void compileGetByIdChain(Machine* machine, ExecState* exec, CodeBlock* codeBlock, StructureID* structureID, StructureIDChain* chain, size_t count, size_t cachedOffset, void* returnAddress)
274 CTI cti(machine, exec, codeBlock);
275 cti.privateCompileGetByIdChain(structureID, chain, count, cachedOffset, returnAddress);
278 static void compilePutByIdReplace(Machine* machine, ExecState* exec, CodeBlock* codeBlock, StructureID* structureID, size_t cachedOffset, void* returnAddress)
280 CTI cti(machine, exec, codeBlock);
281 cti.privateCompilePutByIdReplace(structureID, cachedOffset, returnAddress);
284 static void compilePutByIdTransition(Machine* machine, ExecState* exec, CodeBlock* codeBlock, StructureID* oldStructureID, StructureID* newStructureID, size_t cachedOffset, StructureIDChain* sIDC, void* returnAddress)
286 CTI cti(machine, exec, codeBlock);
287 cti.privateCompilePutByIdTransition(oldStructureID, newStructureID, cachedOffset, sIDC, returnAddress);
290 static void* compileArrayLengthTrampoline(Machine* machine, ExecState* exec, CodeBlock* codeBlock)
292 CTI cti(machine, exec, codeBlock);
293 return cti.privateCompileArrayLengthTrampoline();
296 static void* compileStringLengthTrampoline(Machine* machine, ExecState* exec, CodeBlock* codeBlock)
298 CTI cti(machine, exec, codeBlock);
299 return cti.privateCompileStringLengthTrampoline();
302 static void patchGetByIdSelf(CodeBlock* codeBlock, StructureID* structureID, size_t cachedOffset, void* returnAddress);
303 static void patchPutByIdReplace(CodeBlock* codeBlock, StructureID* structureID, size_t cachedOffset, void* returnAddress);
305 static void compilePatchGetArrayLength(Machine* machine, ExecState* exec, CodeBlock* codeBlock, void* returnAddress)
307 CTI cti(machine, exec, codeBlock);
308 return cti.privateCompilePatchGetArrayLength(returnAddress);
311 inline static JSValue* execute(void* code, ExecState* exec, RegisterFile* registerFile, Register* r, ScopeChainNode* scopeChain, CodeBlock* codeBlock, JSValue** exception)
313 JSValue* value = ctiTrampoline(code, exec, registerFile, r, scopeChain, codeBlock, exception, Profiler::enabledProfilerReference());
314 #if ENABLE(SAMPLING_TOOL)
315 currentOpcodeID = static_cast<OpcodeID>(-1);
321 CTI(Machine*, ExecState*, CodeBlock*);
322 void privateCompileMainPass();
323 void privateCompileLinkPass();
324 void privateCompileSlowCases();
325 void privateCompile();
326 void privateCompileGetByIdSelf(StructureID*, size_t cachedOffset, void* returnAddress);
327 void privateCompileGetByIdProto(StructureID*, StructureID* prototypeStructureID, size_t cachedOffset, void* returnAddress);
328 void privateCompileGetByIdChain(StructureID*, StructureIDChain*, size_t count, size_t cachedOffset, void* returnAddress);
329 void privateCompilePutByIdReplace(StructureID*, size_t cachedOffset, void* returnAddress);
330 void privateCompilePutByIdTransition(StructureID*, StructureID*, size_t cachedOffset, StructureIDChain*, void* returnAddress);
332 void* privateCompileArrayLengthTrampoline();
333 void* privateCompileStringLengthTrampoline();
334 void privateCompilePatchGetArrayLength(void* returnAddress);
336 enum CompileOpCallType { OpCallNormal, OpCallEval, OpConstruct };
337 void compileOpCall(Instruction* instruction, unsigned i, CompileOpCallType type = OpCallNormal);
339 void emitGetArg(unsigned src, X86Assembler::RegisterID dst);
340 void emitGetPutArg(unsigned src, unsigned offset, X86Assembler::RegisterID scratch);
341 void emitPutArg(X86Assembler::RegisterID src, unsigned offset);
342 void emitPutArgConstant(unsigned value, unsigned offset);
343 void emitPutResult(unsigned dst, X86Assembler::RegisterID from = X86::eax);
345 void emitPutCTIParam(X86Assembler::RegisterID from, unsigned name);
346 void emitGetCTIParam(unsigned name, X86Assembler::RegisterID to);
348 void emitPutToCallFrameHeader(X86Assembler::RegisterID from, RegisterFile::CallFrameHeaderEntry entry);
349 void emitGetFromCallFrameHeader(RegisterFile::CallFrameHeaderEntry entry, X86Assembler::RegisterID to);
351 JSValue* getConstantImmediateNumericArg(unsigned src);
352 unsigned getDeTaggedConstantImmediate(JSValue* imm);
354 void emitJumpSlowCaseIfIsJSCell(X86Assembler::RegisterID reg, unsigned opcodeIndex);
355 void emitJumpSlowCaseIfNotJSCell(X86Assembler::RegisterID reg, unsigned opcodeIndex);
357 void emitJumpSlowCaseIfNotImm(X86Assembler::RegisterID, unsigned opcodeIndex);
358 void emitJumpSlowCaseIfNotImms(X86Assembler::RegisterID, X86Assembler::RegisterID, unsigned opcodeIndex);
360 void emitFastArithDeTagImmediate(X86Assembler::RegisterID);
361 void emitFastArithReTagImmediate(X86Assembler::RegisterID);
362 void emitFastArithPotentiallyReTagImmediate(X86Assembler::RegisterID);
363 void emitFastArithImmToInt(X86Assembler::RegisterID);
364 void emitFastArithIntToImmOrSlowCase(X86Assembler::RegisterID, unsigned opcodeIndex);
365 void emitFastArithIntToImmNoCheck(X86Assembler::RegisterID);
367 void emitDebugExceptionCheck();
369 X86Assembler::JmpSrc emitCall(unsigned opcodeIndex, CTIHelper_j);
370 X86Assembler::JmpSrc emitCall(unsigned opcodeIndex, CTIHelper_p);
371 X86Assembler::JmpSrc emitCall(unsigned opcodeIndex, CTIHelper_b);
372 X86Assembler::JmpSrc emitCall(unsigned opcodeIndex, CTIHelper_v);
373 X86Assembler::JmpSrc emitCall(unsigned opcodeIndex, CTIHelper_s);
375 void emitGetVariableObjectRegister(X86Assembler::RegisterID variableObject, int index, X86Assembler::RegisterID dst);
376 void emitPutVariableObjectRegister(X86Assembler::RegisterID src, X86Assembler::RegisterID variableObject, int index);
378 void emitSlowScriptCheck(unsigned opcodeIndex);
380 void printOpcodeOperandTypes(unsigned src1, unsigned src2);
386 CodeBlock* m_codeBlock;
388 Vector<CallRecord> m_calls;
389 Vector<X86Assembler::JmpDst> m_labels;
390 Vector<StructureStubCompilationInfo> m_structureStubCompilationInfo;
391 Vector<JmpTable> m_jmpTable;
394 X86Assembler::JmpDst addrPosition;
395 X86Assembler::JmpDst target;
397 JSRInfo(const X86Assembler::JmpDst& storeLocation, const X86Assembler::JmpDst& targetLocation)
398 : addrPosition(storeLocation)
399 , target(targetLocation)
404 Vector<JSRInfo> m_jsrSites;
405 Vector<SlowCaseEntry> m_slowCases;
406 Vector<SwitchRecord> m_switches;
408 // This limit comes from the limit set in PCRE
409 static const int MaxPatternSize = (1 << 16);
414 #endif // ENABLE(CTI)