Varargs frame set-up should be factored out for use by other JITs
[WebKit-https.git] / Source / JavaScriptCore / jit / JITCall.cpp
1 /*
2  * Copyright (C) 2008, 2013-2015 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 "SetupVarargsFrame.h"
44 #include "StackAlignment.h"
45 #include "ThunkGenerators.h"
46 #include <wtf/StringPrintStream.h>
47
48
49 namespace JSC {
50
51 void JIT::emitPutCallResult(Instruction* instruction)
52 {
53     int dst = instruction[1].u.operand;
54     emitValueProfilingSite();
55     emitPutVirtualRegister(dst);
56 }
57
58 void JIT::compileSetupVarargsFrame(Instruction* instruction)
59 {
60     int thisValue = instruction[3].u.operand;
61     int arguments = instruction[4].u.operand;
62     int firstFreeRegister = instruction[5].u.operand;
63     int firstVarArgOffset = instruction[6].u.operand;
64
65     JumpList slowCase;
66     JumpList end;
67     bool canOptimize = m_codeBlock->usesArguments()
68         && arguments == m_codeBlock->argumentsRegister().offset()
69         && !m_codeBlock->symbolTable()->slowArguments();
70
71     if (canOptimize) {
72         emitGetVirtualRegister(arguments, regT0);
73         slowCase.append(branch64(NotEqual, regT0, TrustedImm64(JSValue::encode(JSValue()))));
74         
75         move(TrustedImm32(-firstFreeRegister), regT1);
76         emitSetupVarargsFrameFastCase(*this, regT1, regT0, regT1, regT2, 0, firstVarArgOffset, slowCase);
77         end.append(jump());
78         slowCase.link(this);
79     }
80
81     emitGetVirtualRegister(arguments, regT1);
82     callOperation(operationSizeFrameForVarargs, regT1, -firstFreeRegister, firstVarArgOffset);
83     move(returnValueGPR, stackPointerRegister);
84     emitGetVirtualRegister(arguments, regT1);
85     callOperation(operationSetupVarargsFrame, returnValueGPR, regT1, firstVarArgOffset);
86     move(returnValueGPR, regT1);
87
88     if (canOptimize)
89         end.link(this);
90     
91     // Initialize 'this'.
92     emitGetVirtualRegister(thisValue, regT0);
93     store64(regT0, Address(regT1, CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register))));
94
95     addPtr(TrustedImm32(sizeof(CallerFrameAndPC)), regT1, stackPointerRegister);
96 }
97
98 void JIT::compileCallEval(Instruction* instruction)
99 {
100     addPtr(TrustedImm32(-static_cast<ptrdiff_t>(sizeof(CallerFrameAndPC))), stackPointerRegister, regT1);
101     storePtr(callFrameRegister, Address(regT1, CallFrame::callerFrameOffset()));
102
103     addPtr(TrustedImm32(stackPointerOffsetFor(m_codeBlock) * sizeof(Register)), callFrameRegister, stackPointerRegister);
104     checkStackPointerAlignment();
105
106     callOperation(operationCallEval, regT1);
107
108     addSlowCase(branch64(Equal, regT0, TrustedImm64(JSValue::encode(JSValue()))));
109
110     sampleCodeBlock(m_codeBlock);
111     
112     emitPutCallResult(instruction);
113 }
114
115 void JIT::compileCallEvalSlowCase(Instruction* instruction, Vector<SlowCaseEntry>::iterator& iter)
116 {
117     linkSlowCase(iter);
118     int registerOffset = -instruction[4].u.operand;
119
120     addPtr(TrustedImm32(registerOffset * sizeof(Register) + sizeof(CallerFrameAndPC)), callFrameRegister, stackPointerRegister);
121
122     load64(Address(stackPointerRegister, sizeof(Register) * JSStack::Callee - sizeof(CallerFrameAndPC)), regT0);
123     move(TrustedImmPtr(&CallLinkInfo::dummy()), regT2);
124     emitNakedCall(m_vm->getCTIStub(virtualCallThunkGenerator).code());
125     addPtr(TrustedImm32(stackPointerOffsetFor(m_codeBlock) * sizeof(Register)), callFrameRegister, stackPointerRegister);
126     checkStackPointerAlignment();
127
128     sampleCodeBlock(m_codeBlock);
129     
130     emitPutCallResult(instruction);
131 }
132
133 void JIT::compileOpCall(OpcodeID opcodeID, Instruction* instruction, unsigned callLinkInfoIndex)
134 {
135     int callee = instruction[2].u.operand;
136
137     /* Caller always:
138         - Updates callFrameRegister to callee callFrame.
139         - Initializes ArgumentCount; CallerFrame; Callee.
140
141        For a JS call:
142         - Callee initializes ReturnPC; CodeBlock.
143         - Callee restores callFrameRegister before return.
144
145        For a non-JS call:
146         - Caller initializes ReturnPC; CodeBlock.
147         - Caller restores callFrameRegister after return.
148     */
149     COMPILE_ASSERT(OPCODE_LENGTH(op_call) == OPCODE_LENGTH(op_construct), call_and_construct_opcodes_must_be_same_length);
150     COMPILE_ASSERT(OPCODE_LENGTH(op_call) == OPCODE_LENGTH(op_call_varargs), call_and_call_varargs_opcodes_must_be_same_length);
151     COMPILE_ASSERT(OPCODE_LENGTH(op_call) == OPCODE_LENGTH(op_construct_varargs), call_and_construct_varargs_opcodes_must_be_same_length);
152     if (opcodeID == op_call_varargs || opcodeID == op_construct_varargs)
153         compileSetupVarargsFrame(instruction);
154     else {
155         int argCount = instruction[3].u.operand;
156         int registerOffset = -instruction[4].u.operand;
157
158         if (opcodeID == op_call && shouldEmitProfiling()) {
159             emitGetVirtualRegister(registerOffset + CallFrame::argumentOffsetIncludingThis(0), regT0);
160             Jump done = emitJumpIfNotJSCell(regT0);
161             load32(Address(regT0, JSCell::structureIDOffset()), regT0);
162             store32(regT0, instruction[OPCODE_LENGTH(op_call) - 2].u.arrayProfile->addressOfLastSeenStructureID());
163             done.link(this);
164         }
165     
166         addPtr(TrustedImm32(registerOffset * sizeof(Register) + sizeof(CallerFrameAndPC)), callFrameRegister, stackPointerRegister);
167         store32(TrustedImm32(argCount), Address(stackPointerRegister, JSStack::ArgumentCount * static_cast<int>(sizeof(Register)) + PayloadOffset - sizeof(CallerFrameAndPC)));
168     } // SP holds newCallFrame + sizeof(CallerFrameAndPC), with ArgumentCount initialized.
169     
170     uint32_t bytecodeOffset = instruction - m_codeBlock->instructions().begin();
171     uint32_t locationBits = CallFrame::Location::encodeAsBytecodeOffset(bytecodeOffset);
172     store32(TrustedImm32(locationBits), Address(callFrameRegister, JSStack::ArgumentCount * static_cast<int>(sizeof(Register)) + TagOffset));
173     emitGetVirtualRegister(callee, regT0); // regT0 holds callee.
174
175     store64(regT0, Address(stackPointerRegister, JSStack::Callee * static_cast<int>(sizeof(Register)) - sizeof(CallerFrameAndPC)));
176     
177     CallLinkInfo* info = m_codeBlock->addCallLinkInfo();
178
179     if (opcodeID == op_call_eval) {
180         compileCallEval(instruction);
181         return;
182     }
183
184     DataLabelPtr addressOfLinkedFunctionCheck;
185     Jump slowCase = branchPtrWithPatch(NotEqual, regT0, addressOfLinkedFunctionCheck, TrustedImmPtr(0));
186     addSlowCase(slowCase);
187
188     ASSERT(m_callCompilationInfo.size() == callLinkInfoIndex);
189     info->callType = CallLinkInfo::callTypeFor(opcodeID);
190     info->codeOrigin = CodeOrigin(m_bytecodeOffset);
191     info->calleeGPR = regT0;
192     m_callCompilationInfo.append(CallCompilationInfo());
193     m_callCompilationInfo[callLinkInfoIndex].hotPathBegin = addressOfLinkedFunctionCheck;
194     m_callCompilationInfo[callLinkInfoIndex].callLinkInfo = info;
195
196     m_callCompilationInfo[callLinkInfoIndex].hotPathOther = emitNakedCall();
197
198     addPtr(TrustedImm32(stackPointerOffsetFor(m_codeBlock) * sizeof(Register)), callFrameRegister, stackPointerRegister);
199     checkStackPointerAlignment();
200
201     sampleCodeBlock(m_codeBlock);
202     
203     emitPutCallResult(instruction);
204 }
205
206 void JIT::compileOpCallSlowCase(OpcodeID opcodeID, Instruction* instruction, Vector<SlowCaseEntry>::iterator& iter, unsigned callLinkInfoIndex)
207 {
208     if (opcodeID == op_call_eval) {
209         compileCallEvalSlowCase(instruction, iter);
210         return;
211     }
212
213     linkSlowCase(iter);
214
215     ThunkGenerator generator = linkThunkGeneratorFor(
216         (opcodeID == op_construct || opcodeID == op_construct_varargs) ? CodeForConstruct : CodeForCall,
217         RegisterPreservationNotRequired);
218     
219     move(TrustedImmPtr(m_callCompilationInfo[callLinkInfoIndex].callLinkInfo), regT2);
220     m_callCompilationInfo[callLinkInfoIndex].callReturnLocation = emitNakedCall(m_vm->getCTIStub(generator).code());
221
222     addPtr(TrustedImm32(stackPointerOffsetFor(m_codeBlock) * sizeof(Register)), callFrameRegister, stackPointerRegister);
223     checkStackPointerAlignment();
224
225     sampleCodeBlock(m_codeBlock);
226     
227     emitPutCallResult(instruction);
228 }
229
230 void JIT::emit_op_call(Instruction* currentInstruction)
231 {
232     compileOpCall(op_call, currentInstruction, m_callLinkInfoIndex++);
233 }
234
235 void JIT::emit_op_call_eval(Instruction* currentInstruction)
236 {
237     compileOpCall(op_call_eval, currentInstruction, m_callLinkInfoIndex);
238 }
239
240 void JIT::emit_op_call_varargs(Instruction* currentInstruction)
241 {
242     compileOpCall(op_call_varargs, currentInstruction, m_callLinkInfoIndex++);
243 }
244     
245 void JIT::emit_op_construct_varargs(Instruction* currentInstruction)
246 {
247     compileOpCall(op_construct_varargs, currentInstruction, m_callLinkInfoIndex++);
248 }
249
250 void JIT::emit_op_construct(Instruction* currentInstruction)
251 {
252     compileOpCall(op_construct, currentInstruction, m_callLinkInfoIndex++);
253 }
254
255 void JIT::emitSlow_op_call(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
256 {
257     compileOpCallSlowCase(op_call, currentInstruction, iter, m_callLinkInfoIndex++);
258 }
259
260 void JIT::emitSlow_op_call_eval(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
261 {
262     compileOpCallSlowCase(op_call_eval, currentInstruction, iter, m_callLinkInfoIndex);
263 }
264  
265 void JIT::emitSlow_op_call_varargs(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
266 {
267     compileOpCallSlowCase(op_call_varargs, currentInstruction, iter, m_callLinkInfoIndex++);
268 }
269     
270 void JIT::emitSlow_op_construct_varargs(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
271 {
272     compileOpCallSlowCase(op_construct_varargs, currentInstruction, iter, m_callLinkInfoIndex++);
273 }
274     
275 void JIT::emitSlow_op_construct(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
276 {
277     compileOpCallSlowCase(op_construct, currentInstruction, iter, m_callLinkInfoIndex++);
278 }
279
280 } // namespace JSC
281
282 #endif // USE(JSVALUE64)
283 #endif // ENABLE(JIT)