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.
32 #include "CodeBlock.h"
33 #include "Interpreter.h"
34 #include "JITInlineMethods.h"
35 #include "JITStubCall.h"
37 #include "JSFunction.h"
38 #include "ResultType.h"
39 #include "SamplingTool.h"
49 void JIT::compileOpCallInitializeCallFrame()
51 // regT0 holds callee, regT1 holds argCount
52 loadPtr(Address(regT0, OBJECT_OFFSETOF(JSFunction, m_scopeChain)), regT3); // scopeChain
53 emitPutIntToCallFrameHeader(regT1, RegisterFile::ArgumentCount);
54 emitPutCellToCallFrameHeader(regT0, RegisterFile::Callee);
55 emitPutCellToCallFrameHeader(regT3, RegisterFile::ScopeChain);
58 void JIT::emit_op_call_put_result(Instruction* instruction)
60 int dst = instruction[1].u.operand;
61 emitStore(dst, regT1, regT0);
64 void JIT::compileOpCallVarargs(Instruction* instruction)
66 int callee = instruction[1].u.operand;
67 int argCountRegister = instruction[2].u.operand;
68 int registerOffset = instruction[3].u.operand;
70 emitLoad(callee, regT1, regT0);
71 emitLoadPayload(argCountRegister, regT2); // argCount
72 addPtr(Imm32(registerOffset), regT2, regT3); // registerOffset
74 emitJumpSlowCaseIfNotJSCell(callee, regT1);
75 addSlowCase(branchPtr(NotEqual, Address(regT0), TrustedImmPtr(m_globalData->jsFunctionVPtr)));
77 // Speculatively roll the callframe, assuming argCount will match the arity.
78 mul32(TrustedImm32(sizeof(Register)), regT3, regT3);
79 addPtr(callFrameRegister, regT3);
80 store32(TrustedImm32(JSValue::CellTag), tagFor(RegisterFile::CallerFrame, regT3));
81 storePtr(callFrameRegister, payloadFor(RegisterFile::CallerFrame, regT3));
82 move(regT3, callFrameRegister);
84 move(regT2, regT1); // argCount
86 emitNakedCall(m_globalData->jitStubs->ctiVirtualCall());
88 sampleCodeBlock(m_codeBlock);
91 void JIT::compileOpCallVarargsSlowCase(Instruction* instruction, Vector<SlowCaseEntry>::iterator& iter)
93 int callee = instruction[1].u.operand;
95 linkSlowCaseIfNotJSCell(iter, callee);
98 JITStubCall stubCall(this, cti_op_call_NotJSFunction);
99 stubCall.addArgument(regT1, regT0);
100 stubCall.addArgument(regT3);
101 stubCall.addArgument(regT2);
104 sampleCodeBlock(m_codeBlock);
107 void JIT::emit_op_ret(Instruction* currentInstruction)
109 emitOptimizationCheck(RetOptimizationCheck);
111 unsigned dst = currentInstruction[1].u.operand;
113 emitLoad(dst, regT1, regT0);
114 emitGetFromCallFrameHeaderPtr(RegisterFile::ReturnPC, regT2);
115 emitGetFromCallFrameHeaderPtr(RegisterFile::CallerFrame, callFrameRegister);
117 restoreReturnAddressBeforeReturn(regT2);
121 void JIT::emit_op_ret_object_or_this(Instruction* currentInstruction)
123 emitOptimizationCheck(RetOptimizationCheck);
125 unsigned result = currentInstruction[1].u.operand;
126 unsigned thisReg = currentInstruction[2].u.operand;
128 emitLoad(result, regT1, regT0);
129 Jump notJSCell = branch32(NotEqual, regT1, TrustedImm32(JSValue::CellTag));
130 loadPtr(Address(regT0, JSCell::structureOffset()), regT2);
131 Jump notObject = emitJumpIfNotObject(regT2);
133 emitGetFromCallFrameHeaderPtr(RegisterFile::ReturnPC, regT2);
134 emitGetFromCallFrameHeaderPtr(RegisterFile::CallerFrame, callFrameRegister);
136 restoreReturnAddressBeforeReturn(regT2);
139 notJSCell.link(this);
140 notObject.link(this);
141 emitLoad(thisReg, regT1, regT0);
143 emitGetFromCallFrameHeaderPtr(RegisterFile::ReturnPC, regT2);
144 emitGetFromCallFrameHeaderPtr(RegisterFile::CallerFrame, callFrameRegister);
146 restoreReturnAddressBeforeReturn(regT2);
150 void JIT::emitSlow_op_call(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
152 compileOpCallSlowCase(currentInstruction, iter, m_callLinkInfoIndex++, op_call);
155 void JIT::emitSlow_op_call_eval(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
157 compileOpCallSlowCase(currentInstruction, iter, m_callLinkInfoIndex++, op_call_eval);
160 void JIT::emitSlow_op_call_varargs(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
162 compileOpCallVarargsSlowCase(currentInstruction, iter);
165 void JIT::emitSlow_op_construct(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
167 compileOpCallSlowCase(currentInstruction, iter, m_callLinkInfoIndex++, op_construct);
170 void JIT::emit_op_call(Instruction* currentInstruction)
172 compileOpCall(op_call, currentInstruction, m_callLinkInfoIndex++);
175 void JIT::emit_op_call_eval(Instruction* currentInstruction)
177 compileOpCall(op_call_eval, currentInstruction, m_callLinkInfoIndex++);
180 void JIT::emit_op_call_varargs(Instruction* currentInstruction)
182 compileOpCallVarargs(currentInstruction);
185 void JIT::emit_op_construct(Instruction* currentInstruction)
187 compileOpCall(op_construct, currentInstruction, m_callLinkInfoIndex++);
190 void JIT::compileOpCall(OpcodeID opcodeID, Instruction* instruction, unsigned callLinkInfoIndex)
192 int callee = instruction[1].u.operand;
193 int argCount = instruction[2].u.operand;
194 int registerOffset = instruction[3].u.operand;
197 if (opcodeID == op_call_eval) {
198 JITStubCall stubCall(this, cti_op_call_eval);
199 stubCall.addArgument(callee);
200 stubCall.addArgument(JIT::Imm32(registerOffset));
201 stubCall.addArgument(JIT::Imm32(argCount));
203 wasEval = branch32(NotEqual, regT1, TrustedImm32(JSValue::EmptyValueTag));
206 emitLoad(callee, regT1, regT0);
208 DataLabelPtr addressOfLinkedFunctionCheck;
210 BEGIN_UNINTERRUPTED_SEQUENCE(sequenceOpCall);
212 Jump jumpToSlow = branchPtrWithPatch(NotEqual, regT0, addressOfLinkedFunctionCheck, TrustedImmPtr(0));
214 END_UNINTERRUPTED_SEQUENCE(sequenceOpCall);
216 addSlowCase(jumpToSlow);
217 ASSERT_JIT_OFFSET(differenceBetween(addressOfLinkedFunctionCheck, jumpToSlow), patchOffsetOpCallCompareToJump);
218 ASSERT(m_callStructureStubCompilationInfo.size() == callLinkInfoIndex);
219 m_callStructureStubCompilationInfo.append(StructureStubCompilationInfo());
220 m_callStructureStubCompilationInfo[callLinkInfoIndex].hotPathBegin = addressOfLinkedFunctionCheck;
221 m_callStructureStubCompilationInfo[callLinkInfoIndex].isCall = opcodeID != op_construct;
223 addSlowCase(branch32(NotEqual, regT1, TrustedImm32(JSValue::CellTag)));
225 // The following is the fast case, only used whan a callee can be linked.
227 // Fast version of stack frame initialization, directly relative to edi.
228 // Note that this omits to set up RegisterFile::CodeBlock, which is set in the callee
229 loadPtr(Address(regT0, OBJECT_OFFSETOF(JSFunction, m_scopeChain)), regT2);
231 store32(TrustedImm32(JSValue::Int32Tag), tagFor(registerOffset + RegisterFile::ArgumentCount));
232 store32(Imm32(argCount), payloadFor(registerOffset + RegisterFile::ArgumentCount));
233 storePtr(callFrameRegister, payloadFor(RegisterFile::CallerFrame + registerOffset, callFrameRegister));
234 emitStore(registerOffset + RegisterFile::Callee, regT1, regT0);
235 store32(TrustedImm32(JSValue::CellTag), tagFor(registerOffset + RegisterFile::ScopeChain));
236 store32(regT2, payloadFor(registerOffset + RegisterFile::ScopeChain));
237 addPtr(Imm32(registerOffset * sizeof(Register)), callFrameRegister);
239 // Call to the callee
240 m_callStructureStubCompilationInfo[callLinkInfoIndex].hotPathOther = emitNakedCall();
242 if (opcodeID == op_call_eval)
245 sampleCodeBlock(m_codeBlock);
248 void JIT::compileOpCallSlowCase(Instruction* instruction, Vector<SlowCaseEntry>::iterator& iter, unsigned callLinkInfoIndex, OpcodeID opcodeID)
250 int callee = instruction[1].u.operand;
251 int argCount = instruction[2].u.operand;
252 int registerOffset = instruction[3].u.operand;
257 // Fast check for JS function.
258 Jump callLinkFailNotObject = branch32(NotEqual, regT1, TrustedImm32(JSValue::CellTag));
259 Jump callLinkFailNotJSFunction = branchPtr(NotEqual, Address(regT0), TrustedImmPtr(m_globalData->jsFunctionVPtr));
261 // Speculatively roll the callframe, assuming argCount will match the arity.
262 store32(TrustedImm32(JSValue::CellTag), tagFor(RegisterFile::CallerFrame + registerOffset, callFrameRegister));
263 storePtr(callFrameRegister, payloadFor(RegisterFile::CallerFrame + registerOffset, callFrameRegister));
264 addPtr(Imm32(registerOffset * static_cast<int>(sizeof(Register))), callFrameRegister);
265 move(Imm32(argCount), regT1);
267 m_callStructureStubCompilationInfo[callLinkInfoIndex].callReturnLocation = emitNakedCall(opcodeID == op_construct ? m_globalData->jitStubs->ctiVirtualConstructLink() : m_globalData->jitStubs->ctiVirtualCallLink());
269 // Done! - return back to the hot path.
270 ASSERT(OPCODE_LENGTH(op_call) == OPCODE_LENGTH(op_call_eval));
271 ASSERT(OPCODE_LENGTH(op_call) == OPCODE_LENGTH(op_construct));
272 emitJumpSlowToHot(jump(), OPCODE_LENGTH(op_call));
274 // This handles host functions
275 callLinkFailNotObject.link(this);
276 callLinkFailNotJSFunction.link(this);
278 JITStubCall stubCall(this, opcodeID == op_construct ? cti_op_construct_NotJSConstruct : cti_op_call_NotJSFunction);
279 stubCall.addArgument(callee);
280 stubCall.addArgument(JIT::Imm32(registerOffset));
281 stubCall.addArgument(JIT::Imm32(argCount));
284 sampleCodeBlock(m_codeBlock);
289 #endif // USE(JSVALUE32_64)
290 #endif // ENABLE(JIT)