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