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