713803fb320f466559b98c00add96d9ab1bcbc42
[WebKit-https.git] / Source / JavaScriptCore / jit / JITCall32_64.cpp
1 /*
2  * Copyright (C) 2008, 2013, 2014 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 "JSCInlines.h"
39 #include "LinkBuffer.h"
40 #include "RepatchBuffer.h"
41 #include "ResultType.h"
42 #include "SamplingTool.h"
43 #include "StackAlignment.h"
44 #include <wtf/StringPrintStream.h>
45
46
47 namespace JSC {
48
49 void JIT::emitPutCallResult(Instruction* instruction)
50 {
51     int dst = instruction[1].u.operand;
52     emitValueProfilingSite();
53     emitStore(dst, regT1, regT0);
54 }
55
56 void JIT::emit_op_ret(Instruction* currentInstruction)
57 {
58     unsigned dst = currentInstruction[1].u.operand;
59
60     emitLoad(dst, regT1, regT0);
61
62     checkStackPointerAlignment();
63     emitFunctionEpilogue();
64     ret();
65 }
66
67 void JIT::emitSlow_op_call(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
68 {
69     compileOpCallSlowCase(op_call, currentInstruction, iter, m_callLinkInfoIndex++);
70 }
71
72 void JIT::emitSlow_op_call_eval(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
73 {
74     compileOpCallSlowCase(op_call_eval, currentInstruction, iter, m_callLinkInfoIndex);
75 }
76  
77 void JIT::emitSlow_op_call_varargs(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
78 {
79     compileOpCallSlowCase(op_call_varargs, currentInstruction, iter, m_callLinkInfoIndex++);
80 }
81     
82 void JIT::emitSlow_op_construct_varargs(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
83 {
84     compileOpCallSlowCase(op_construct_varargs, currentInstruction, iter, m_callLinkInfoIndex++);
85 }
86     
87 void JIT::emitSlow_op_construct(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
88 {
89     compileOpCallSlowCase(op_construct, currentInstruction, iter, m_callLinkInfoIndex++);
90 }
91
92 void JIT::emit_op_call(Instruction* currentInstruction)
93 {
94     compileOpCall(op_call, currentInstruction, m_callLinkInfoIndex++);
95 }
96
97 void JIT::emit_op_call_eval(Instruction* currentInstruction)
98 {
99     compileOpCall(op_call_eval, currentInstruction, m_callLinkInfoIndex);
100 }
101
102 void JIT::emit_op_call_varargs(Instruction* currentInstruction)
103 {
104     compileOpCall(op_call_varargs, currentInstruction, m_callLinkInfoIndex++);
105 }
106     
107 void JIT::emit_op_construct_varargs(Instruction* currentInstruction)
108 {
109     compileOpCall(op_construct_varargs, currentInstruction, m_callLinkInfoIndex++);
110 }
111     
112 void JIT::emit_op_construct(Instruction* currentInstruction)
113 {
114     compileOpCall(op_construct, currentInstruction, m_callLinkInfoIndex++);
115 }
116
117 void JIT::compileLoadVarargs(Instruction* instruction)
118 {
119     int thisValue = instruction[3].u.operand;
120     int arguments = instruction[4].u.operand;
121     int firstFreeRegister = instruction[5].u.operand;
122     int firstVarArgOffset = instruction[6].u.operand;
123
124     JumpList slowCase;
125     JumpList end;
126     bool canOptimize = m_codeBlock->usesArguments()
127         && VirtualRegister(arguments) == m_codeBlock->argumentsRegister()
128         && !m_codeBlock->symbolTable()->slowArguments();
129
130     if (canOptimize) {
131         emitLoadTag(arguments, regT1);
132         slowCase.append(branch32(NotEqual, regT1, TrustedImm32(JSValue::EmptyValueTag)));
133
134         load32(payloadFor(JSStack::ArgumentCount), regT2);
135         if (firstVarArgOffset) {
136             Jump sufficientArguments = branch32(GreaterThan, regT2, TrustedImm32(firstVarArgOffset + 1));
137             move(TrustedImm32(1), regT2);
138             Jump endVarArgs = jump();
139             sufficientArguments.link(this);
140             sub32(TrustedImm32(firstVarArgOffset), regT2);
141             endVarArgs.link(this);
142         }
143         slowCase.append(branch32(Above, regT2, TrustedImm32(Arguments::MaxArguments + 1)));
144         // regT2: argumentCountIncludingThis
145
146         move(regT2, regT3);
147         addPtr(TrustedImm32(-firstFreeRegister + JSStack::CallFrameHeaderSize), regT3);
148         // regT1 now has the required frame size in Register units
149         // Round regT1 to next multiple of stackAlignmentRegisters()
150         addPtr(TrustedImm32(stackAlignmentRegisters() - 1), regT3);
151         andPtr(TrustedImm32(~(stackAlignmentRegisters() - 1)), regT3);
152         neg32(regT3);
153         lshift32(TrustedImm32(3), regT3);
154         addPtr(callFrameRegister, regT3);
155         // regT3: newCallFrame
156
157         slowCase.append(branchPtr(Above, AbsoluteAddress(m_vm->addressOfStackLimit()), regT3));
158
159         // Initialize ArgumentCount.
160         store32(regT2, payloadFor(JSStack::ArgumentCount, regT3));
161
162         // Initialize 'this'.
163         emitLoad(thisValue, regT1, regT0);
164         store32(regT0, Address(regT3, OBJECT_OFFSETOF(JSValue, u.asBits.payload) + (CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))));
165         store32(regT1, Address(regT3, OBJECT_OFFSETOF(JSValue, u.asBits.tag) + (CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))));
166
167         // Copy arguments.
168         end.append(branchSub32(Zero, TrustedImm32(1), regT2));
169         // regT2: argumentCount;
170
171         Label copyLoop = label();
172         load32(BaseIndex(callFrameRegister, regT2, TimesEight, OBJECT_OFFSETOF(JSValue, u.asBits.payload) +((CallFrame::thisArgumentOffset() + firstVarArgOffset) * static_cast<int>(sizeof(Register)))), regT0);
173         load32(BaseIndex(callFrameRegister, regT2, TimesEight, OBJECT_OFFSETOF(JSValue, u.asBits.tag) +((CallFrame::thisArgumentOffset() + firstVarArgOffset) * static_cast<int>(sizeof(Register)))), regT1);
174         store32(regT0, BaseIndex(regT3, regT2, TimesEight, OBJECT_OFFSETOF(JSValue, u.asBits.payload) +(CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))));
175         store32(regT1, BaseIndex(regT3, regT2, TimesEight, OBJECT_OFFSETOF(JSValue, u.asBits.tag) +(CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))));
176         branchSub32(NonZero, TrustedImm32(1), regT2).linkTo(copyLoop, this);
177
178         end.append(jump());
179     }
180
181     if (canOptimize)
182         slowCase.link(this);
183
184     emitLoad(arguments, regT1, regT0);
185     callOperation(operationSizeFrameForVarargs, regT1, regT0, firstFreeRegister, firstVarArgOffset);
186     addPtr(TrustedImm32(-sizeof(CallerFrameAndPC)), returnValueGPR, stackPointerRegister);
187     emitLoad(thisValue, regT1, regT4);
188     emitLoad(arguments, regT3, regT2);
189     callOperation(operationLoadVarargs, returnValueGPR, regT1, regT4, regT3, regT2, firstVarArgOffset);
190     move(returnValueGPR, regT3);
191
192     if (canOptimize)
193         end.link(this);
194
195     addPtr(TrustedImm32(sizeof(CallerFrameAndPC)), regT3, stackPointerRegister);
196 }
197
198 void JIT::compileCallEval(Instruction* instruction)
199 {
200     addPtr(TrustedImm32(-static_cast<ptrdiff_t>(sizeof(CallerFrameAndPC))), stackPointerRegister, regT1);
201     storePtr(callFrameRegister, Address(regT1, CallFrame::callerFrameOffset()));
202
203     addPtr(TrustedImm32(stackPointerOffsetFor(m_codeBlock) * sizeof(Register)), callFrameRegister, stackPointerRegister);
204
205     callOperation(operationCallEval, regT1);
206
207     addSlowCase(branch32(Equal, regT1, TrustedImm32(JSValue::EmptyValueTag)));
208
209     sampleCodeBlock(m_codeBlock);
210     
211     emitPutCallResult(instruction);
212 }
213
214 void JIT::compileCallEvalSlowCase(Instruction* instruction, Vector<SlowCaseEntry>::iterator& iter)
215 {
216     linkSlowCase(iter);
217
218     int registerOffset = -instruction[4].u.operand;
219
220     addPtr(TrustedImm32(registerOffset * sizeof(Register) + sizeof(CallerFrameAndPC)), callFrameRegister, stackPointerRegister);
221
222     loadPtr(Address(stackPointerRegister, sizeof(Register) * JSStack::Callee - sizeof(CallerFrameAndPC)), regT0);
223     loadPtr(Address(stackPointerRegister, sizeof(Register) * JSStack::Callee - sizeof(CallerFrameAndPC)), regT1);
224     move(TrustedImmPtr(&CallLinkInfo::dummy()), regT2);
225
226     emitLoad(JSStack::Callee, regT1, regT0);
227     emitNakedCall(m_vm->getCTIStub(virtualCallThunkGenerator).code());
228     addPtr(TrustedImm32(stackPointerOffsetFor(m_codeBlock) * sizeof(Register)), callFrameRegister, stackPointerRegister);
229     checkStackPointerAlignment();
230
231     sampleCodeBlock(m_codeBlock);
232     
233     emitPutCallResult(instruction);
234 }
235
236 void JIT::compileOpCall(OpcodeID opcodeID, Instruction* instruction, unsigned callLinkInfoIndex)
237 {
238     int callee = instruction[2].u.operand;
239
240     /* Caller always:
241         - Updates callFrameRegister to callee callFrame.
242         - Initializes ArgumentCount; CallerFrame; Callee.
243
244        For a JS call:
245         - Callee initializes ReturnPC; CodeBlock.
246         - Callee restores callFrameRegister before return.
247
248        For a non-JS call:
249         - Caller initializes ReturnPC; CodeBlock.
250         - Caller restores callFrameRegister after return.
251     */
252     
253     if (opcodeID == op_call_varargs || opcodeID == op_construct_varargs)
254         compileLoadVarargs(instruction);
255     else {
256         int argCount = instruction[3].u.operand;
257         int registerOffset = -instruction[4].u.operand;
258         
259         if (opcodeID == op_call && shouldEmitProfiling()) {
260             emitLoad(registerOffset + CallFrame::argumentOffsetIncludingThis(0), regT0, regT1);
261             Jump done = branch32(NotEqual, regT0, TrustedImm32(JSValue::CellTag));
262             loadPtr(Address(regT1, JSCell::structureIDOffset()), regT1);
263             storePtr(regT1, instruction[OPCODE_LENGTH(op_call) - 2].u.arrayProfile->addressOfLastSeenStructureID());
264             done.link(this);
265         }
266     
267         addPtr(TrustedImm32(registerOffset * sizeof(Register) + sizeof(CallerFrameAndPC)), callFrameRegister, stackPointerRegister);
268
269         store32(TrustedImm32(argCount), Address(stackPointerRegister, JSStack::ArgumentCount * static_cast<int>(sizeof(Register)) + PayloadOffset - sizeof(CallerFrameAndPC)));
270     } // SP holds newCallFrame + sizeof(CallerFrameAndPC), with ArgumentCount initialized.
271     
272     uint32_t locationBits = CallFrame::Location::encodeAsBytecodeInstruction(instruction);
273     store32(TrustedImm32(locationBits), tagFor(JSStack::ArgumentCount, callFrameRegister));
274     emitLoad(callee, regT1, regT0); // regT1, regT0 holds callee.
275
276     store32(regT0, Address(stackPointerRegister, JSStack::Callee * static_cast<int>(sizeof(Register)) + PayloadOffset - sizeof(CallerFrameAndPC)));
277     store32(regT1, Address(stackPointerRegister, JSStack::Callee * static_cast<int>(sizeof(Register)) + TagOffset - sizeof(CallerFrameAndPC)));
278
279     CallLinkInfo* info = m_codeBlock->addCallLinkInfo();
280
281     if (opcodeID == op_call_eval) {
282         compileCallEval(instruction);
283         return;
284     }
285
286     addSlowCase(branch32(NotEqual, regT1, TrustedImm32(JSValue::CellTag)));
287
288     DataLabelPtr addressOfLinkedFunctionCheck;
289     Jump slowCase = branchPtrWithPatch(NotEqual, regT0, addressOfLinkedFunctionCheck, TrustedImmPtr(0));
290
291     addSlowCase(slowCase);
292
293     ASSERT(m_callCompilationInfo.size() == callLinkInfoIndex);
294     info->callType = CallLinkInfo::callTypeFor(opcodeID);
295     info->codeOrigin = CodeOrigin(m_bytecodeOffset);
296     info->calleeGPR = regT0;
297     m_callCompilationInfo.append(CallCompilationInfo());
298     m_callCompilationInfo[callLinkInfoIndex].hotPathBegin = addressOfLinkedFunctionCheck;
299     m_callCompilationInfo[callLinkInfoIndex].callLinkInfo = info;
300
301     checkStackPointerAlignment();
302     m_callCompilationInfo[callLinkInfoIndex].hotPathOther = emitNakedCall();
303
304     addPtr(TrustedImm32(stackPointerOffsetFor(m_codeBlock) * sizeof(Register)), callFrameRegister, stackPointerRegister);
305     checkStackPointerAlignment();
306
307     sampleCodeBlock(m_codeBlock);
308     emitPutCallResult(instruction);
309 }
310
311 void JIT::compileOpCallSlowCase(OpcodeID opcodeID, Instruction* instruction, Vector<SlowCaseEntry>::iterator& iter, unsigned callLinkInfoIndex)
312 {
313     if (opcodeID == op_call_eval) {
314         compileCallEvalSlowCase(instruction, iter);
315         return;
316     }
317
318     linkSlowCase(iter);
319     linkSlowCase(iter);
320
321     ThunkGenerator generator = linkThunkGeneratorFor(
322         (opcodeID == op_construct || opcodeID == op_construct_varargs) ? CodeForConstruct : CodeForCall,
323         RegisterPreservationNotRequired);
324     
325     move(TrustedImmPtr(m_callCompilationInfo[callLinkInfoIndex].callLinkInfo), regT2);
326     m_callCompilationInfo[callLinkInfoIndex].callReturnLocation = emitNakedCall(m_vm->getCTIStub(generator).code());
327
328     addPtr(TrustedImm32(stackPointerOffsetFor(m_codeBlock) * sizeof(Register)), callFrameRegister, stackPointerRegister);
329     checkStackPointerAlignment();
330
331     sampleCodeBlock(m_codeBlock);
332     emitPutCallResult(instruction);
333 }
334
335 } // namespace JSC
336
337 #endif // USE(JSVALUE32_64)
338 #endif // ENABLE(JIT)