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 "Arguments.h"
33 #include "CodeBlock.h"
34 #include "JITInlineMethods.h"
35 #include "JITStubCall.h"
37 #include "JSFunction.h"
38 #include "Interpreter.h"
39 #include "ResultType.h"
40 #include "SamplingTool.h"
50 void JIT::emit_op_call_put_result(Instruction* instruction)
52 int dst = instruction[1].u.operand;
53 emitValueProfilingSite(FirstProfilingSite);
54 emitPutVirtualRegister(dst);
57 void JIT::compileLoadVarargs(Instruction* instruction)
59 int thisValue = instruction[2].u.operand;
60 int arguments = instruction[3].u.operand;
61 int firstFreeRegister = instruction[4].u.operand;
63 killLastResultRegister();
67 if (m_codeBlock->usesArguments() && arguments == m_codeBlock->argumentsRegister()) {
68 emitGetVirtualRegister(arguments, regT0);
69 slowCase.append(branchPtr(NotEqual, regT0, TrustedImmPtr(JSValue::encode(JSValue()))));
71 emitGetFromCallFrameHeader32(RegisterFile::ArgumentCount, regT0);
72 slowCase.append(branch32(Above, regT0, TrustedImm32(Arguments::MaxArguments + 1)));
73 // regT0: argumentCountIncludingThis
76 add32(TrustedImm32(firstFreeRegister + RegisterFile::CallFrameHeaderSize), regT1);
77 lshift32(TrustedImm32(3), regT1);
78 addPtr(callFrameRegister, regT1);
79 // regT1: newCallFrame
81 slowCase.append(branchPtr(Below, AbsoluteAddress(m_globalData->interpreter->registerFile().addressOfEnd()), regT1));
83 // Initialize ArgumentCount.
84 emitFastArithReTagImmediate(regT0, regT2);
85 storePtr(regT2, Address(regT1, RegisterFile::ArgumentCount * static_cast<int>(sizeof(Register))));
88 emitGetVirtualRegister(thisValue, regT2);
89 storePtr(regT2, Address(regT1, CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register))));
93 signExtend32ToPtr(regT0, regT0);
94 end.append(branchAddPtr(Zero, Imm32(1), regT0));
95 // regT0: -argumentCount
97 Label copyLoop = label();
98 loadPtr(BaseIndex(callFrameRegister, regT0, TimesEight, CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register))), regT2);
99 storePtr(regT2, BaseIndex(regT1, regT0, TimesEight, CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register))));
100 branchAddPtr(NonZero, Imm32(1), regT0).linkTo(copyLoop, this);
105 if (m_codeBlock->usesArguments() && arguments == m_codeBlock->argumentsRegister())
108 JITStubCall stubCall(this, cti_op_load_varargs);
109 stubCall.addArgument(thisValue, regT0);
110 stubCall.addArgument(arguments, regT0);
111 stubCall.addArgument(Imm32(firstFreeRegister));
112 stubCall.call(regT1);
114 if (m_codeBlock->usesArguments() && arguments == m_codeBlock->argumentsRegister())
118 void JIT::compileCallEval()
120 JITStubCall stubCall(this, cti_op_call_eval); // Initializes ScopeChain; ReturnPC; CodeBlock.
122 addSlowCase(branchPtr(Equal, regT0, TrustedImmPtr(JSValue::encode(JSValue()))));
123 emitGetFromCallFrameHeaderPtr(RegisterFile::CallerFrame, callFrameRegister);
125 sampleCodeBlock(m_codeBlock);
128 void JIT::compileCallEvalSlowCase(Vector<SlowCaseEntry>::iterator& iter)
132 emitGetFromCallFrameHeaderPtr(RegisterFile::Callee, regT0);
133 emitNakedCall(m_globalData->jitStubs->ctiVirtualCall());
135 sampleCodeBlock(m_codeBlock);
138 void JIT::compileOpCall(OpcodeID opcodeID, Instruction* instruction, unsigned callLinkInfoIndex)
140 int callee = instruction[1].u.operand;
143 - Updates callFrameRegister to callee callFrame.
144 - Initializes ArgumentCount; CallerFrame; Callee.
147 - Caller initializes ScopeChain.
148 - Callee initializes ReturnPC; CodeBlock.
149 - Callee restores callFrameRegister before return.
152 - Caller initializes ScopeChain; ReturnPC; CodeBlock.
153 - Caller restores callFrameRegister after return.
156 if (opcodeID == op_call_varargs)
157 compileLoadVarargs(instruction);
159 int argCount = instruction[2].u.operand;
160 int registerOffset = instruction[3].u.operand;
162 addPtr(TrustedImm32(registerOffset * sizeof(Register)), callFrameRegister, regT1);
163 store32(TrustedImm32(argCount), Address(regT1, RegisterFile::ArgumentCount * static_cast<int>(sizeof(Register))));
164 } // regT1 holds newCallFrame with ArgumentCount initialized.
165 emitGetVirtualRegister(callee, regT0); // regT0 holds callee.
167 storePtr(callFrameRegister, Address(regT1, RegisterFile::CallerFrame * static_cast<int>(sizeof(Register))));
168 storePtr(regT0, Address(regT1, RegisterFile::Callee * static_cast<int>(sizeof(Register))));
169 move(regT1, callFrameRegister);
171 if (opcodeID == op_call_eval) {
176 DataLabelPtr addressOfLinkedFunctionCheck;
177 BEGIN_UNINTERRUPTED_SEQUENCE(sequenceOpCall);
178 Jump slowCase = branchPtrWithPatch(NotEqual, regT0, addressOfLinkedFunctionCheck, TrustedImmPtr(JSValue::encode(JSValue())));
179 END_UNINTERRUPTED_SEQUENCE(sequenceOpCall);
180 addSlowCase(slowCase);
182 ASSERT_JIT_OFFSET(differenceBetween(addressOfLinkedFunctionCheck, slowCase), patchOffsetOpCallCompareToJump);
183 ASSERT(m_callStructureStubCompilationInfo.size() == callLinkInfoIndex);
184 m_callStructureStubCompilationInfo.append(StructureStubCompilationInfo());
185 m_callStructureStubCompilationInfo[callLinkInfoIndex].hotPathBegin = addressOfLinkedFunctionCheck;
186 m_callStructureStubCompilationInfo[callLinkInfoIndex].callType = CallLinkInfo::callTypeFor(opcodeID);
187 m_callStructureStubCompilationInfo[callLinkInfoIndex].bytecodeIndex = m_bytecodeOffset;
189 loadPtr(Address(regT0, OBJECT_OFFSETOF(JSFunction, m_scopeChain)), regT1);
190 emitPutToCallFrameHeader(regT1, RegisterFile::ScopeChain);
191 m_callStructureStubCompilationInfo[callLinkInfoIndex].hotPathOther = emitNakedCall();
193 sampleCodeBlock(m_codeBlock);
196 void JIT::compileOpCallSlowCase(OpcodeID opcodeID, Instruction*, Vector<SlowCaseEntry>::iterator& iter, unsigned callLinkInfoIndex)
198 if (opcodeID == op_call_eval) {
199 compileCallEvalSlowCase(iter);
205 m_callStructureStubCompilationInfo[callLinkInfoIndex].callReturnLocation = emitNakedCall(opcodeID == op_construct ? m_globalData->jitStubs->ctiVirtualConstructLink() : m_globalData->jitStubs->ctiVirtualCallLink());
207 sampleCodeBlock(m_codeBlock);
212 #endif // USE(JSVALUE64)
213 #endif // ENABLE(JIT)