]> git.saurik.com Git - apple/javascriptcore.git/blame - jit/JITCall32_64.cpp
JavaScriptCore-1218.0.1.tar.gz
[apple/javascriptcore.git] / jit / JITCall32_64.cpp
CommitLineData
14957cd0 1/*
93a37866 2 * Copyright (C) 2008, 2013 Apple Inc. All rights reserved.
14957cd0
A
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26#include "config.h"
27
28#if ENABLE(JIT)
29#if USE(JSVALUE32_64)
30#include "JIT.h"
31
6fe7ccc8 32#include "Arguments.h"
14957cd0
A
33#include "CodeBlock.h"
34#include "Interpreter.h"
93a37866 35#include "JITInlines.h"
14957cd0
A
36#include "JITStubCall.h"
37#include "JSArray.h"
38#include "JSFunction.h"
93a37866
A
39#include "Operations.h"
40#include "RepatchBuffer.h"
14957cd0
A
41#include "ResultType.h"
42#include "SamplingTool.h"
93a37866 43#include <wtf/StringPrintStream.h>
14957cd0
A
44
45#ifndef NDEBUG
46#include <stdio.h>
47#endif
48
49using namespace std;
50
51namespace JSC {
52
14957cd0
A
53void JIT::emit_op_call_put_result(Instruction* instruction)
54{
55 int dst = instruction[1].u.operand;
6fe7ccc8 56 emitValueProfilingSite();
14957cd0
A
57 emitStore(dst, regT1, regT0);
58}
59
14957cd0
A
60void JIT::emit_op_ret(Instruction* currentInstruction)
61{
62 unsigned dst = currentInstruction[1].u.operand;
63
64 emitLoad(dst, regT1, regT0);
93a37866
A
65 emitGetFromCallFrameHeaderPtr(JSStack::ReturnPC, regT2);
66 emitGetFromCallFrameHeaderPtr(JSStack::CallerFrame, callFrameRegister);
14957cd0
A
67
68 restoreReturnAddressBeforeReturn(regT2);
69 ret();
70}
71
72void JIT::emit_op_ret_object_or_this(Instruction* currentInstruction)
73{
74 unsigned result = currentInstruction[1].u.operand;
75 unsigned thisReg = currentInstruction[2].u.operand;
76
77 emitLoad(result, regT1, regT0);
78 Jump notJSCell = branch32(NotEqual, regT1, TrustedImm32(JSValue::CellTag));
79 loadPtr(Address(regT0, JSCell::structureOffset()), regT2);
6fe7ccc8 80 Jump notObject = emitJumpIfNotObject(regT2);
14957cd0 81
93a37866
A
82 emitGetFromCallFrameHeaderPtr(JSStack::ReturnPC, regT2);
83 emitGetFromCallFrameHeaderPtr(JSStack::CallerFrame, callFrameRegister);
14957cd0
A
84
85 restoreReturnAddressBeforeReturn(regT2);
86 ret();
87
88 notJSCell.link(this);
89 notObject.link(this);
90 emitLoad(thisReg, regT1, regT0);
91
93a37866
A
92 emitGetFromCallFrameHeaderPtr(JSStack::ReturnPC, regT2);
93 emitGetFromCallFrameHeaderPtr(JSStack::CallerFrame, callFrameRegister);
14957cd0
A
94
95 restoreReturnAddressBeforeReturn(regT2);
96 ret();
97}
98
99void JIT::emitSlow_op_call(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
100{
6fe7ccc8 101 compileOpCallSlowCase(op_call, currentInstruction, iter, m_callLinkInfoIndex++);
14957cd0
A
102}
103
104void JIT::emitSlow_op_call_eval(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
105{
6fe7ccc8 106 compileOpCallSlowCase(op_call_eval, currentInstruction, iter, m_callLinkInfoIndex);
14957cd0 107}
6fe7ccc8 108
14957cd0
A
109void JIT::emitSlow_op_call_varargs(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
110{
6fe7ccc8 111 compileOpCallSlowCase(op_call_varargs, currentInstruction, iter, m_callLinkInfoIndex++);
14957cd0
A
112}
113
114void JIT::emitSlow_op_construct(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
115{
6fe7ccc8 116 compileOpCallSlowCase(op_construct, currentInstruction, iter, m_callLinkInfoIndex++);
14957cd0
A
117}
118
119void JIT::emit_op_call(Instruction* currentInstruction)
120{
121 compileOpCall(op_call, currentInstruction, m_callLinkInfoIndex++);
122}
123
124void JIT::emit_op_call_eval(Instruction* currentInstruction)
125{
6fe7ccc8 126 compileOpCall(op_call_eval, currentInstruction, m_callLinkInfoIndex);
14957cd0
A
127}
128
129void JIT::emit_op_call_varargs(Instruction* currentInstruction)
130{
6fe7ccc8 131 compileOpCall(op_call_varargs, currentInstruction, m_callLinkInfoIndex++);
14957cd0
A
132}
133
134void JIT::emit_op_construct(Instruction* currentInstruction)
135{
136 compileOpCall(op_construct, currentInstruction, m_callLinkInfoIndex++);
137}
138
6fe7ccc8 139void JIT::compileLoadVarargs(Instruction* instruction)
14957cd0 140{
6fe7ccc8
A
141 int thisValue = instruction[2].u.operand;
142 int arguments = instruction[3].u.operand;
143 int firstFreeRegister = instruction[4].u.operand;
144
145 JumpList slowCase;
146 JumpList end;
93a37866
A
147 bool canOptimize = m_codeBlock->usesArguments()
148 && arguments == m_codeBlock->argumentsRegister()
149 && !m_codeBlock->symbolTable()->slowArguments();
150
151 if (canOptimize) {
6fe7ccc8
A
152 emitLoadTag(arguments, regT1);
153 slowCase.append(branch32(NotEqual, regT1, TrustedImm32(JSValue::EmptyValueTag)));
154
93a37866 155 load32(payloadFor(JSStack::ArgumentCount), regT2);
6fe7ccc8
A
156 slowCase.append(branch32(Above, regT2, TrustedImm32(Arguments::MaxArguments + 1)));
157 // regT2: argumentCountIncludingThis
158
159 move(regT2, regT3);
93a37866 160 add32(TrustedImm32(firstFreeRegister + JSStack::CallFrameHeaderSize), regT3);
6fe7ccc8
A
161 lshift32(TrustedImm32(3), regT3);
162 addPtr(callFrameRegister, regT3);
163 // regT3: newCallFrame
164
93a37866 165 slowCase.append(branchPtr(Below, AbsoluteAddress(m_vm->interpreter->stack().addressOfEnd()), regT3));
6fe7ccc8
A
166
167 // Initialize ArgumentCount.
93a37866 168 store32(regT2, payloadFor(JSStack::ArgumentCount, regT3));
6fe7ccc8
A
169
170 // Initialize 'this'.
171 emitLoad(thisValue, regT1, regT0);
172 store32(regT0, Address(regT3, OBJECT_OFFSETOF(JSValue, u.asBits.payload) + (CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))));
173 store32(regT1, Address(regT3, OBJECT_OFFSETOF(JSValue, u.asBits.tag) + (CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))));
174
175 // Copy arguments.
176 neg32(regT2);
177 end.append(branchAdd32(Zero, TrustedImm32(1), regT2));
178 // regT2: -argumentCount;
179
180 Label copyLoop = label();
181 load32(BaseIndex(callFrameRegister, regT2, TimesEight, OBJECT_OFFSETOF(JSValue, u.asBits.payload) +(CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))), regT0);
182 load32(BaseIndex(callFrameRegister, regT2, TimesEight, OBJECT_OFFSETOF(JSValue, u.asBits.tag) +(CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))), regT1);
183 store32(regT0, BaseIndex(regT3, regT2, TimesEight, OBJECT_OFFSETOF(JSValue, u.asBits.payload) +(CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))));
184 store32(regT1, BaseIndex(regT3, regT2, TimesEight, OBJECT_OFFSETOF(JSValue, u.asBits.tag) +(CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))));
185 branchAdd32(NonZero, TrustedImm32(1), regT2).linkTo(copyLoop, this);
186
187 end.append(jump());
14957cd0
A
188 }
189
93a37866 190 if (canOptimize)
6fe7ccc8 191 slowCase.link(this);
14957cd0 192
6fe7ccc8
A
193 JITStubCall stubCall(this, cti_op_load_varargs);
194 stubCall.addArgument(thisValue);
195 stubCall.addArgument(arguments);
196 stubCall.addArgument(Imm32(firstFreeRegister));
197 stubCall.call(regT3);
14957cd0 198
93a37866 199 if (canOptimize)
6fe7ccc8
A
200 end.link(this);
201}
14957cd0 202
6fe7ccc8
A
203void JIT::compileCallEval()
204{
205 JITStubCall stubCall(this, cti_op_call_eval); // Initializes ScopeChain; ReturnPC; CodeBlock.
206 stubCall.call();
207 addSlowCase(branch32(Equal, regT1, TrustedImm32(JSValue::EmptyValueTag)));
93a37866 208 emitGetFromCallFrameHeaderPtr(JSStack::CallerFrame, callFrameRegister);
14957cd0
A
209
210 sampleCodeBlock(m_codeBlock);
211}
212
6fe7ccc8 213void JIT::compileCallEvalSlowCase(Vector<SlowCaseEntry>::iterator& iter)
14957cd0 214{
14957cd0
A
215 linkSlowCase(iter);
216
93a37866
A
217 emitLoad(JSStack::Callee, regT1, regT0);
218 emitNakedCall(m_vm->getCTIStub(virtualCallGenerator).code());
14957cd0
A
219
220 sampleCodeBlock(m_codeBlock);
221}
222
14957cd0
A
223void JIT::compileOpCall(OpcodeID opcodeID, Instruction* instruction, unsigned callLinkInfoIndex)
224{
225 int callee = instruction[1].u.operand;
14957cd0 226
6fe7ccc8
A
227 /* Caller always:
228 - Updates callFrameRegister to callee callFrame.
229 - Initializes ArgumentCount; CallerFrame; Callee.
14957cd0 230
6fe7ccc8
A
231 For a JS call:
232 - Caller initializes ScopeChain.
233 - Callee initializes ReturnPC; CodeBlock.
234 - Callee restores callFrameRegister before return.
14957cd0 235
6fe7ccc8
A
236 For a non-JS call:
237 - Caller initializes ScopeChain; ReturnPC; CodeBlock.
238 - Caller restores callFrameRegister after return.
239 */
240
241 if (opcodeID == op_call_varargs)
242 compileLoadVarargs(instruction);
243 else {
244 int argCount = instruction[2].u.operand;
245 int registerOffset = instruction[3].u.operand;
93a37866
A
246
247 if (opcodeID == op_call && shouldEmitProfiling()) {
248 emitLoad(registerOffset + CallFrame::argumentOffsetIncludingThis(0), regT0, regT1);
249 Jump done = branch32(NotEqual, regT0, TrustedImm32(JSValue::CellTag));
250 loadPtr(Address(regT1, JSCell::structureOffset()), regT1);
251 storePtr(regT1, instruction[5].u.arrayProfile->addressOfLastSeenStructure());
252 done.link(this);
253 }
254
6fe7ccc8 255 addPtr(TrustedImm32(registerOffset * sizeof(Register)), callFrameRegister, regT3);
14957cd0 256
93a37866 257 store32(TrustedImm32(argCount), payloadFor(JSStack::ArgumentCount, regT3));
6fe7ccc8
A
258 } // regT3 holds newCallFrame with ArgumentCount initialized.
259
93a37866 260 storePtr(TrustedImmPtr(instruction), tagFor(JSStack::ArgumentCount, callFrameRegister));
6fe7ccc8 261 emitLoad(callee, regT1, regT0); // regT1, regT0 holds callee.
14957cd0 262
93a37866
A
263 storePtr(callFrameRegister, Address(regT3, JSStack::CallerFrame * static_cast<int>(sizeof(Register))));
264 emitStore(JSStack::Callee, regT1, regT0, regT3);
6fe7ccc8 265 move(regT3, callFrameRegister);
14957cd0 266
6fe7ccc8
A
267 if (opcodeID == op_call_eval) {
268 compileCallEval();
269 return;
270 }
14957cd0 271
6fe7ccc8
A
272 DataLabelPtr addressOfLinkedFunctionCheck;
273 BEGIN_UNINTERRUPTED_SEQUENCE(sequenceOpCall);
274 Jump slowCase = branchPtrWithPatch(NotEqual, regT0, addressOfLinkedFunctionCheck, TrustedImmPtr(0));
275 END_UNINTERRUPTED_SEQUENCE(sequenceOpCall);
14957cd0 276
6fe7ccc8
A
277 addSlowCase(slowCase);
278 addSlowCase(branch32(NotEqual, regT1, TrustedImm32(JSValue::CellTag)));
14957cd0 279
6fe7ccc8
A
280 ASSERT(m_callStructureStubCompilationInfo.size() == callLinkInfoIndex);
281 m_callStructureStubCompilationInfo.append(StructureStubCompilationInfo());
282 m_callStructureStubCompilationInfo[callLinkInfoIndex].hotPathBegin = addressOfLinkedFunctionCheck;
283 m_callStructureStubCompilationInfo[callLinkInfoIndex].callType = CallLinkInfo::callTypeFor(opcodeID);
284 m_callStructureStubCompilationInfo[callLinkInfoIndex].bytecodeIndex = m_bytecodeOffset;
14957cd0 285
93a37866
A
286 loadPtr(Address(regT0, OBJECT_OFFSETOF(JSFunction, m_scope)), regT1);
287 emitPutCellToCallFrameHeader(regT1, JSStack::ScopeChain);
14957cd0 288 m_callStructureStubCompilationInfo[callLinkInfoIndex].hotPathOther = emitNakedCall();
14957cd0
A
289
290 sampleCodeBlock(m_codeBlock);
291}
292
6fe7ccc8 293void JIT::compileOpCallSlowCase(OpcodeID opcodeID, Instruction*, Vector<SlowCaseEntry>::iterator& iter, unsigned callLinkInfoIndex)
14957cd0 294{
6fe7ccc8
A
295 if (opcodeID == op_call_eval) {
296 compileCallEvalSlowCase(iter);
297 return;
298 }
14957cd0
A
299
300 linkSlowCase(iter);
301 linkSlowCase(iter);
6fe7ccc8 302
93a37866 303 m_callStructureStubCompilationInfo[callLinkInfoIndex].callReturnLocation = emitNakedCall(opcodeID == op_construct ? m_vm->getCTIStub(linkConstructGenerator).code() : m_vm->getCTIStub(linkCallGenerator).code());
14957cd0 304
14957cd0
A
305 sampleCodeBlock(m_codeBlock);
306}
307
93a37866
A
308void JIT::privateCompileClosureCall(CallLinkInfo* callLinkInfo, CodeBlock* calleeCodeBlock, Structure* expectedStructure, ExecutableBase* expectedExecutable, MacroAssemblerCodePtr codePtr)
309{
310 JumpList slowCases;
311
312 slowCases.append(branch32(NotEqual, regT1, TrustedImm32(JSValue::CellTag)));
313 slowCases.append(branchPtr(NotEqual, Address(regT0, JSCell::structureOffset()), TrustedImmPtr(expectedStructure)));
314 slowCases.append(branchPtr(NotEqual, Address(regT0, JSFunction::offsetOfExecutable()), TrustedImmPtr(expectedExecutable)));
315
316 loadPtr(Address(regT0, JSFunction::offsetOfScopeChain()), regT1);
317 emitPutCellToCallFrameHeader(regT1, JSStack::ScopeChain);
318
319 Call call = nearCall();
320 Jump done = jump();
321
322 slowCases.link(this);
323 move(TrustedImmPtr(callLinkInfo->callReturnLocation.executableAddress()), regT2);
324 restoreReturnAddressBeforeReturn(regT2);
325 Jump slow = jump();
326
327 LinkBuffer patchBuffer(*m_vm, this, m_codeBlock);
328
329 patchBuffer.link(call, FunctionPtr(codePtr.executableAddress()));
330 patchBuffer.link(done, callLinkInfo->hotPathOther.labelAtOffset(0));
331 patchBuffer.link(slow, CodeLocationLabel(m_vm->getCTIStub(virtualCallGenerator).code()));
332
333 RefPtr<ClosureCallStubRoutine> stubRoutine = adoptRef(new ClosureCallStubRoutine(
334 FINALIZE_CODE(
335 patchBuffer,
336 ("Baseline closure call stub for %s, return point %p, target %p (%s)",
337 toCString(*m_codeBlock).data(),
338 callLinkInfo->hotPathOther.labelAtOffset(0).executableAddress(),
339 codePtr.executableAddress(),
340 toCString(pointerDump(calleeCodeBlock)).data())),
341 *m_vm, m_codeBlock->ownerExecutable(), expectedStructure, expectedExecutable,
342 callLinkInfo->codeOrigin));
343
344 RepatchBuffer repatchBuffer(m_codeBlock);
345
346 repatchBuffer.replaceWithJump(
347 RepatchBuffer::startOfBranchPtrWithPatchOnRegister(callLinkInfo->hotPathBegin),
348 CodeLocationLabel(stubRoutine->code().code()));
349 repatchBuffer.relink(callLinkInfo->callReturnLocation, m_vm->getCTIStub(virtualCallGenerator).code());
350
351 callLinkInfo->stub = stubRoutine.release();
352}
353
14957cd0
A
354} // namespace JSC
355
356#endif // USE(JSVALUE32_64)
357#endif // ENABLE(JIT)