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.
29 #include <wtf/Platform.h>
33 #define WTF_USE_CTI_REPATCH_PIC 1
35 #include "Interpreter.h"
37 #include "RegisterFile.h"
38 #include "X86Assembler.h"
40 #include <wtf/AlwaysInline.h>
41 #include <wtf/Vector.h>
43 #define CTI_ARGS_code 0x0C
44 #define CTI_ARGS_registerFile 0x0D
45 #define CTI_ARGS_callFrame 0x0E
46 #define CTI_ARGS_exception 0x0F
47 #define CTI_ARGS_profilerReference 0x10
48 #define CTI_ARGS_globalData 0x11
50 #define ARG_callFrame static_cast<CallFrame*>(ARGS[CTI_ARGS_callFrame])
51 #define ARG_registerFile static_cast<RegisterFile*>(ARGS[CTI_ARGS_registerFile])
52 #define ARG_exception static_cast<JSValue**>(ARGS[CTI_ARGS_exception])
53 #define ARG_profilerReference static_cast<Profiler**>(ARGS[CTI_ARGS_profilerReference])
54 #define ARG_globalData static_cast<JSGlobalData*>(ARGS[CTI_ARGS_globalData])
56 #define ARG_setCallFrame(newCallFrame) (ARGS[CTI_ARGS_callFrame] = (newCallFrame))
58 #define ARG_src1 static_cast<JSValue*>(ARGS[1])
59 #define ARG_src2 static_cast<JSValue*>(ARGS[2])
60 #define ARG_src3 static_cast<JSValue*>(ARGS[3])
61 #define ARG_src4 static_cast<JSValue*>(ARGS[4])
62 #define ARG_src5 static_cast<JSValue*>(ARGS[5])
63 #define ARG_id1 static_cast<Identifier*>(ARGS[1])
64 #define ARG_id2 static_cast<Identifier*>(ARGS[2])
65 #define ARG_id3 static_cast<Identifier*>(ARGS[3])
66 #define ARG_id4 static_cast<Identifier*>(ARGS[4])
67 #define ARG_int1 reinterpret_cast<intptr_t>(ARGS[1])
68 #define ARG_int2 reinterpret_cast<intptr_t>(ARGS[2])
69 #define ARG_int3 reinterpret_cast<intptr_t>(ARGS[3])
70 #define ARG_int4 reinterpret_cast<intptr_t>(ARGS[4])
71 #define ARG_int5 reinterpret_cast<intptr_t>(ARGS[5])
72 #define ARG_int6 reinterpret_cast<intptr_t>(ARGS[6])
73 #define ARG_func1 static_cast<FuncDeclNode*>(ARGS[1])
74 #define ARG_funcexp1 static_cast<FuncExprNode*>(ARGS[1])
75 #define ARG_registers1 static_cast<Register*>(ARGS[1])
76 #define ARG_regexp1 static_cast<RegExp*>(ARGS[1])
77 #define ARG_pni1 static_cast<JSPropertyNameIterator*>(ARGS[1])
78 #define ARG_instr1 static_cast<Instruction*>(ARGS[1])
79 #define ARG_instr2 static_cast<Instruction*>(ARGS[2])
80 #define ARG_instr3 static_cast<Instruction*>(ARGS[3])
81 #define ARG_instr4 static_cast<Instruction*>(ARGS[4])
82 #define ARG_instr5 static_cast<Instruction*>(ARGS[5])
83 #define ARG_instr6 static_cast<Instruction*>(ARGS[6])
84 #define ARG_returnAddress2 static_cast<void*>(ARGS[2])
85 #define ARG_codeBlock4 static_cast<CodeBlock*>(ARGS[4])
87 #define CTI_RETURN_ADDRESS_SLOT (ARGS[-1])
92 class JSPropertyNameIterator;
97 class SimpleJumpTable;
98 class StringJumpTable;
104 struct PolymorphicAccessStructureList;
105 struct StructureStubInfo;
107 typedef JSValue* (SFX_CALL *CTIHelper_j)(CTI_ARGS);
108 typedef JSObject* (SFX_CALL *CTIHelper_o)(CTI_ARGS);
109 typedef JSPropertyNameIterator* (SFX_CALL *CTIHelper_p)(CTI_ARGS);
110 typedef void (SFX_CALL *CTIHelper_v)(CTI_ARGS);
111 typedef void* (SFX_CALL *CTIHelper_s)(CTI_ARGS);
112 typedef int (SFX_CALL *CTIHelper_b)(CTI_ARGS);
113 typedef VoidPtrPair (SFX_CALL *CTIHelper_2)(CTI_ARGS);
116 typedef X86Assembler::JmpSrc JmpSrc;
120 unsigned bytecodeIndex;
126 CallRecord(JmpSrc f, CTIHelper_j t, unsigned i)
128 , to(reinterpret_cast<void*>(t))
133 CallRecord(JmpSrc f, CTIHelper_o t, unsigned i)
135 , to(reinterpret_cast<void*>(t))
140 CallRecord(JmpSrc f, CTIHelper_p t, unsigned i)
142 , to(reinterpret_cast<void*>(t))
147 CallRecord(JmpSrc f, CTIHelper_v t, unsigned i)
149 , to(reinterpret_cast<void*>(t))
154 CallRecord(JmpSrc f, CTIHelper_s t, unsigned i)
156 , to(reinterpret_cast<void*>(t))
161 CallRecord(JmpSrc f, CTIHelper_b t, unsigned i)
163 , to(reinterpret_cast<void*>(t))
168 CallRecord(JmpSrc f, CTIHelper_2 t, unsigned i)
170 , to(reinterpret_cast<void*>(t))
175 CallRecord(JmpSrc f, unsigned i)
184 typedef X86Assembler::JmpSrc JmpSrc;
189 JmpTable(JmpSrc f, unsigned t)
196 struct SlowCaseEntry {
197 typedef X86Assembler::JmpSrc JmpSrc;
203 SlowCaseEntry(JmpSrc f, unsigned t, unsigned h = 0)
211 struct SwitchRecord {
221 SimpleJumpTable* simpleJumpTable;
222 StringJumpTable* stringJumpTable;
225 unsigned bytecodeIndex;
226 unsigned defaultOffset;
228 SwitchRecord(SimpleJumpTable* jumpTable, unsigned bytecodeIndex, unsigned defaultOffset, Type type)
230 , bytecodeIndex(bytecodeIndex)
231 , defaultOffset(defaultOffset)
233 this->jumpTable.simpleJumpTable = jumpTable;
236 SwitchRecord(StringJumpTable* jumpTable, unsigned bytecodeIndex, unsigned defaultOffset)
238 , bytecodeIndex(bytecodeIndex)
239 , defaultOffset(defaultOffset)
241 this->jumpTable.stringJumpTable = jumpTable;
245 struct StructureStubCompilationInfo {
246 typedef X86Assembler::JmpSrc JmpSrc;
247 typedef X86Assembler::JmpDst JmpDst;
249 JmpSrc callReturnLocation;
252 JmpDst coldPathOther;
256 JSValue* ctiTrampoline(void* code, RegisterFile*, CallFrame*, JSValue** exception, Profiler**, JSGlobalData*);
257 void ctiVMThrowTrampoline();
260 void ctiSetReturnAddress(void** where, void* what);
261 void ctiRepatchCallByReturnAddress(void* where, void* what);
264 typedef X86Assembler::RegisterID RegisterID;
265 typedef X86Assembler::XMMRegisterID XMMRegisterID;
266 typedef X86Assembler::JmpSrc JmpSrc;
267 typedef X86Assembler::JmpDst JmpDst;
269 static const int repatchGetByIdDefaultStructure = -1;
270 // Magic number - initial offset cannot be representable as a signed 8bit value, or the X86Assembler
271 // will compress the displacement, and we may not be able to fit a repatched offset.
272 static const int repatchGetByIdDefaultOffset = 256;
274 #if USE(FAST_CALL_CTI_ARGUMENT)
275 static const int ctiArgumentInitSize = 2;
276 #elif USE(CTI_ARGUMENT)
277 static const int ctiArgumentInitSize = 4;
279 static const int ctiArgumentInitSize = 0;
281 // These architecture specific value are used to enable repatching - see comment on op_put_by_id.
282 static const int repatchOffsetPutByIdStructure = 7;
283 static const int repatchOffsetPutByIdPropertyMapOffset = 22;
284 // These architecture specific value are used to enable repatching - see comment on op_get_by_id.
285 static const int repatchOffsetGetByIdStructure = 7;
286 static const int repatchOffsetGetByIdBranchToSlowCase = 13;
287 static const int repatchOffsetGetByIdPropertyMapOffset = 22;
288 #if ENABLE(OPCODE_SAMPLING)
289 static const int repatchOffsetGetByIdSlowCaseCall = 27 + 4 + ctiArgumentInitSize;
291 static const int repatchOffsetGetByIdSlowCaseCall = 17 + 4 + ctiArgumentInitSize;
293 static const int repatchOffsetOpCallCall = 6;
296 static void compile(JSGlobalData* globalData, CodeBlock* codeBlock)
298 JIT jit(globalData, codeBlock);
299 jit.privateCompile();
302 static void compileGetByIdSelf(JSGlobalData* globalData, CodeBlock* codeBlock, Structure* structure, size_t cachedOffset, void* returnAddress)
304 JIT jit(globalData, codeBlock);
305 jit.privateCompileGetByIdSelf(structure, cachedOffset, returnAddress);
308 static void compileGetByIdProto(JSGlobalData* globalData, CallFrame* callFrame, CodeBlock* codeBlock, Structure* structure, Structure* prototypeStructure, size_t cachedOffset, void* returnAddress)
310 JIT jit(globalData, codeBlock);
311 jit.privateCompileGetByIdProto(structure, prototypeStructure, cachedOffset, returnAddress, callFrame);
314 #if USE(CTI_REPATCH_PIC)
315 static void compileGetByIdSelfList(JSGlobalData* globalData, CodeBlock* codeBlock, StructureStubInfo* stubInfo, PolymorphicAccessStructureList* polymorphicStructures, int currentIndex, Structure* structure, size_t cachedOffset)
317 JIT jit(globalData, codeBlock);
318 jit.privateCompileGetByIdSelfList(stubInfo, polymorphicStructures, currentIndex, structure, cachedOffset);
320 static void compileGetByIdProtoList(JSGlobalData* globalData, CallFrame* callFrame, CodeBlock* codeBlock, StructureStubInfo* stubInfo, PolymorphicAccessStructureList* prototypeStructureList, int currentIndex, Structure* structure, Structure* prototypeStructure, size_t cachedOffset)
322 JIT jit(globalData, codeBlock);
323 jit.privateCompileGetByIdProtoList(stubInfo, prototypeStructureList, currentIndex, structure, prototypeStructure, cachedOffset, callFrame);
325 static void compileGetByIdChainList(JSGlobalData* globalData, CallFrame* callFrame, CodeBlock* codeBlock, StructureStubInfo* stubInfo, PolymorphicAccessStructureList* prototypeStructureList, int currentIndex, Structure* structure, StructureChain* chain, size_t count, size_t cachedOffset)
327 JIT jit(globalData, codeBlock);
328 jit.privateCompileGetByIdChainList(stubInfo, prototypeStructureList, currentIndex, structure, chain, count, cachedOffset, callFrame);
332 static void compileGetByIdChain(JSGlobalData* globalData, CallFrame* callFrame, CodeBlock* codeBlock, Structure* structure, StructureChain* chain, size_t count, size_t cachedOffset, void* returnAddress)
334 JIT jit(globalData, codeBlock);
335 jit.privateCompileGetByIdChain(structure, chain, count, cachedOffset, returnAddress, callFrame);
338 static void compilePutByIdReplace(JSGlobalData* globalData, CodeBlock* codeBlock, Structure* structure, size_t cachedOffset, void* returnAddress)
340 JIT jit(globalData, codeBlock);
341 jit.privateCompilePutByIdReplace(structure, cachedOffset, returnAddress);
344 static void compilePutByIdTransition(JSGlobalData* globalData, CodeBlock* codeBlock, Structure* oldStructure, Structure* newStructure, size_t cachedOffset, StructureChain* chain, void* returnAddress)
346 JIT jit(globalData, codeBlock);
347 jit.privateCompilePutByIdTransition(oldStructure, newStructure, cachedOffset, chain, returnAddress);
350 static void compileCTIMachineTrampolines(JSGlobalData* globalData)
353 jit.privateCompileCTIMachineTrampolines();
355 static void freeCTIMachineTrampolines(Interpreter*);
357 static void patchGetByIdSelf(CodeBlock* codeBlock, Structure* structure, size_t cachedOffset, void* returnAddress);
358 static void patchPutByIdReplace(CodeBlock* codeBlock, Structure* structure, size_t cachedOffset, void* returnAddress);
360 static void compilePatchGetArrayLength(JSGlobalData* globalData, CodeBlock* codeBlock, void* returnAddress)
362 JIT jit(globalData, codeBlock);
363 return jit.privateCompilePatchGetArrayLength(returnAddress);
366 static void linkCall(JSFunction* callee, CodeBlock* calleeCodeBlock, void* ctiCode, CallLinkInfo* callLinkInfo, int callerArgCount);
367 static void unlinkCall(CallLinkInfo*);
369 inline static JSValue* execute(void* code, RegisterFile* registerFile, CallFrame* callFrame, JSGlobalData* globalData, JSValue** exception)
371 return ctiTrampoline(code, registerFile, callFrame, exception, Profiler::enabledProfilerReference(), globalData);
375 JIT(JSGlobalData*, CodeBlock* = 0);
377 void privateCompileMainPass();
378 void privateCompileLinkPass();
379 void privateCompileSlowCases();
380 void privateCompile();
381 void privateCompileGetByIdSelf(Structure*, size_t cachedOffset, void* returnAddress);
382 void privateCompileGetByIdProto(Structure*, Structure* prototypeStructure, size_t cachedOffset, void* returnAddress, CallFrame* callFrame);
383 #if USE(CTI_REPATCH_PIC)
384 void privateCompileGetByIdSelfList(StructureStubInfo*, PolymorphicAccessStructureList*, int, Structure*, size_t cachedOffset);
385 void privateCompileGetByIdProtoList(StructureStubInfo*, PolymorphicAccessStructureList*, int, Structure*, Structure* prototypeStructure, size_t cachedOffset, CallFrame* callFrame);
386 void privateCompileGetByIdChainList(StructureStubInfo*, PolymorphicAccessStructureList*, int, Structure*, StructureChain* chain, size_t count, size_t cachedOffset, CallFrame* callFrame);
388 void privateCompileGetByIdChain(Structure*, StructureChain*, size_t count, size_t cachedOffset, void* returnAddress, CallFrame* callFrame);
389 void privateCompilePutByIdReplace(Structure*, size_t cachedOffset, void* returnAddress);
390 void privateCompilePutByIdTransition(Structure*, Structure*, size_t cachedOffset, StructureChain*, void* returnAddress);
392 void privateCompileCTIMachineTrampolines();
393 void privateCompilePatchGetArrayLength(void* returnAddress);
395 void compileOpCall(OpcodeID, Instruction* instruction, unsigned i, unsigned callLinkInfoIndex);
396 void compileOpCallInitializeCallFrame();
397 void compileOpCallSetupArgs(Instruction*);
398 void compileOpCallEvalSetupArgs(Instruction*);
399 void compileOpConstructSetupArgs(Instruction*);
400 enum CompileOpStrictEqType { OpStrictEq, OpNStrictEq };
401 void compileOpStrictEq(Instruction* instruction, unsigned i, CompileOpStrictEqType type);
402 void putDoubleResultToJSNumberCellOrJSImmediate(XMMRegisterID xmmSource, RegisterID jsNumberCell, unsigned dst, JmpSrc* wroteJSNumberCell, XMMRegisterID tempXmm, RegisterID tempReg1, RegisterID tempReg2);
403 void compileBinaryArithOp(OpcodeID, unsigned dst, unsigned src1, unsigned src2, OperandTypes opi, unsigned i);
404 void compileBinaryArithOpSlowCase(OpcodeID, Vector<SlowCaseEntry>::iterator& iter, unsigned dst, unsigned src1, unsigned src2, OperandTypes opi, unsigned i);
406 void emitGetVirtualRegister(int src, RegisterID dst, unsigned i);
407 void emitGetVirtualRegisters(int src1, RegisterID dst1, int src2, RegisterID dst2, unsigned i);
408 void emitPutVirtualRegister(unsigned dst, RegisterID from = X86::eax);
410 void emitPutCTIArg(RegisterID src, unsigned offset);
411 void emitPutCTIArgFromVirtualRegister(unsigned src, unsigned offset, RegisterID scratch);
412 void emitPutCTIArgConstant(unsigned value, unsigned offset);
413 void emitGetCTIArg(unsigned offset, RegisterID dst);
415 void emitInitRegister(unsigned dst);
417 void emitPutCTIParam(void* value, unsigned name);
418 void emitPutCTIParam(RegisterID from, unsigned name);
419 void emitGetCTIParam(unsigned name, RegisterID to);
421 void emitPutToCallFrameHeader(RegisterID from, RegisterFile::CallFrameHeaderEntry entry);
422 void emitGetFromCallFrameHeader(RegisterFile::CallFrameHeaderEntry entry, RegisterID to);
424 JSValue* getConstantImmediateNumericArg(unsigned src);
425 unsigned getDeTaggedConstantImmediate(JSValue* imm);
427 bool linkSlowCaseIfNotJSCell(const Vector<SlowCaseEntry>::iterator&, int vReg);
428 void emitJumpSlowCaseIfNotJSCell(RegisterID, unsigned bytecodeIndex);
429 void emitJumpSlowCaseIfNotJSCell(RegisterID, unsigned bytecodeIndex, int VReg);
431 void emitJumpSlowCaseIfNotImmNum(RegisterID, unsigned bytecodeIndex);
432 void emitJumpSlowCaseIfNotImmNums(RegisterID, RegisterID, unsigned bytecodeIndex);
434 JmpSrc checkStructure(RegisterID reg, Structure* structure);
436 void emitFastArithDeTagImmediate(RegisterID);
437 JmpSrc emitFastArithDeTagImmediateJumpIfZero(RegisterID);
438 void emitFastArithReTagImmediate(RegisterID);
439 void emitFastArithPotentiallyReTagImmediate(RegisterID);
440 void emitFastArithImmToInt(RegisterID);
441 void emitFastArithIntToImmOrSlowCase(RegisterID, unsigned bytecodeIndex);
442 void emitFastArithIntToImmNoCheck(RegisterID);
444 void emitTagAsBoolImmediate(RegisterID reg);
446 JmpSrc emitNakedCall(unsigned bytecodeIndex, RegisterID);
447 JmpSrc emitNakedCall(unsigned bytecodeIndex, void* function);
448 JmpSrc emitCTICall(unsigned bytecodeIndex, CTIHelper_j);
449 JmpSrc emitCTICall(unsigned bytecodeIndex, CTIHelper_o);
450 JmpSrc emitCTICall(unsigned bytecodeIndex, CTIHelper_p);
451 JmpSrc emitCTICall(unsigned bytecodeIndex, CTIHelper_v);
452 JmpSrc emitCTICall(unsigned bytecodeIndex, CTIHelper_s);
453 JmpSrc emitCTICall(unsigned bytecodeIndex, CTIHelper_b);
454 JmpSrc emitCTICall(unsigned bytecodeIndex, CTIHelper_2);
456 void emitGetVariableObjectRegister(RegisterID variableObject, int index, RegisterID dst);
457 void emitPutVariableObjectRegister(RegisterID src, RegisterID variableObject, int index);
459 void emitSlowScriptCheck(unsigned bytecodeIndex);
461 void printBytecodeOperandTypes(unsigned src1, unsigned src2);
464 void killLastResultRegister();
466 X86Assembler m_assembler;
467 Interpreter* m_interpreter;
468 JSGlobalData* m_globalData;
469 CodeBlock* m_codeBlock;
471 Vector<CallRecord> m_calls;
472 Vector<JmpDst> m_labels;
473 Vector<StructureStubCompilationInfo> m_propertyAccessCompilationInfo;
474 Vector<StructureStubCompilationInfo> m_callStructureStubCompilationInfo;
475 Vector<JmpTable> m_jmpTable;
481 JSRInfo(const JmpDst& storeLocation, const JmpDst& targetLocation)
482 : addrPosition(storeLocation)
483 , target(targetLocation)
488 Vector<JSRInfo> m_jsrSites;
489 Vector<SlowCaseEntry> m_slowCases;
490 Vector<SwitchRecord> m_switches;
492 int m_lastResultBytecodeRegister;
493 unsigned m_jumpTargetsPosition;
497 #endif // ENABLE(JIT)