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