Renamed ...InlineMethods.h files to ...Inlines.h.
[WebKit-https.git] / Source / JavaScriptCore / jit / JITCall32_64.cpp
1 /*
2  * Copyright (C) 2008 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 "JITStubCall.h"
37 #include "JSArray.h"
38 #include "JSFunction.h"
39 #include "ResultType.h"
40 #include "SamplingTool.h"
41
42 #ifndef NDEBUG
43 #include <stdio.h>
44 #endif
45
46 using namespace std;
47
48 namespace JSC {
49
50 void JIT::emit_op_call_put_result(Instruction* instruction)
51 {
52     int dst = instruction[1].u.operand;
53     emitValueProfilingSite();
54     emitStore(dst, regT1, regT0);
55 }
56
57 void JIT::emit_op_ret(Instruction* currentInstruction)
58 {
59     unsigned dst = currentInstruction[1].u.operand;
60
61     emitLoad(dst, regT1, regT0);
62     emitGetFromCallFrameHeaderPtr(JSStack::ReturnPC, regT2);
63     emitGetFromCallFrameHeaderPtr(JSStack::CallerFrame, callFrameRegister);
64
65     restoreReturnAddressBeforeReturn(regT2);
66     ret();
67 }
68
69 void JIT::emit_op_ret_object_or_this(Instruction* currentInstruction)
70 {
71     unsigned result = currentInstruction[1].u.operand;
72     unsigned thisReg = currentInstruction[2].u.operand;
73
74     emitLoad(result, regT1, regT0);
75     Jump notJSCell = branch32(NotEqual, regT1, TrustedImm32(JSValue::CellTag));
76     loadPtr(Address(regT0, JSCell::structureOffset()), regT2);
77     Jump notObject = emitJumpIfNotObject(regT2);
78
79     emitGetFromCallFrameHeaderPtr(JSStack::ReturnPC, regT2);
80     emitGetFromCallFrameHeaderPtr(JSStack::CallerFrame, callFrameRegister);
81
82     restoreReturnAddressBeforeReturn(regT2);
83     ret();
84
85     notJSCell.link(this);
86     notObject.link(this);
87     emitLoad(thisReg, regT1, regT0);
88
89     emitGetFromCallFrameHeaderPtr(JSStack::ReturnPC, regT2);
90     emitGetFromCallFrameHeaderPtr(JSStack::CallerFrame, callFrameRegister);
91
92     restoreReturnAddressBeforeReturn(regT2);
93     ret();
94 }
95
96 void JIT::emitSlow_op_call(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
97 {
98     compileOpCallSlowCase(op_call, currentInstruction, iter, m_callLinkInfoIndex++);
99 }
100
101 void JIT::emitSlow_op_call_eval(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
102 {
103     compileOpCallSlowCase(op_call_eval, currentInstruction, iter, m_callLinkInfoIndex);
104 }
105  
106 void JIT::emitSlow_op_call_varargs(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
107 {
108     compileOpCallSlowCase(op_call_varargs, currentInstruction, iter, m_callLinkInfoIndex++);
109 }
110
111 void JIT::emitSlow_op_construct(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
112 {
113     compileOpCallSlowCase(op_construct, currentInstruction, iter, m_callLinkInfoIndex++);
114 }
115
116 void JIT::emit_op_call(Instruction* currentInstruction)
117 {
118     compileOpCall(op_call, currentInstruction, m_callLinkInfoIndex++);
119 }
120
121 void JIT::emit_op_call_eval(Instruction* currentInstruction)
122 {
123     compileOpCall(op_call_eval, currentInstruction, m_callLinkInfoIndex);
124 }
125
126 void JIT::emit_op_call_varargs(Instruction* currentInstruction)
127 {
128     compileOpCall(op_call_varargs, currentInstruction, m_callLinkInfoIndex++);
129 }
130
131 void JIT::emit_op_construct(Instruction* currentInstruction)
132 {
133     compileOpCall(op_construct, currentInstruction, m_callLinkInfoIndex++);
134 }
135
136 void JIT::compileLoadVarargs(Instruction* instruction)
137 {
138     int thisValue = instruction[2].u.operand;
139     int arguments = instruction[3].u.operand;
140     int firstFreeRegister = instruction[4].u.operand;
141
142     JumpList slowCase;
143     JumpList end;
144     bool canOptimize = m_codeBlock->usesArguments()
145         && arguments == m_codeBlock->argumentsRegister()
146         && !m_codeBlock->symbolTable()->slowArguments();
147
148     if (canOptimize) {
149         emitLoadTag(arguments, regT1);
150         slowCase.append(branch32(NotEqual, regT1, TrustedImm32(JSValue::EmptyValueTag)));
151
152         load32(payloadFor(JSStack::ArgumentCount), regT2);
153         slowCase.append(branch32(Above, regT2, TrustedImm32(Arguments::MaxArguments + 1)));
154         // regT2: argumentCountIncludingThis
155
156         move(regT2, regT3);
157         add32(TrustedImm32(firstFreeRegister + JSStack::CallFrameHeaderSize), regT3);
158         lshift32(TrustedImm32(3), regT3);
159         addPtr(callFrameRegister, regT3);
160         // regT3: newCallFrame
161
162         slowCase.append(branchPtr(Below, AbsoluteAddress(m_globalData->interpreter->stack().addressOfEnd()), regT3));
163
164         // Initialize ArgumentCount.
165         store32(regT2, payloadFor(JSStack::ArgumentCount, regT3));
166
167         // Initialize 'this'.
168         emitLoad(thisValue, regT1, regT0);
169         store32(regT0, Address(regT3, OBJECT_OFFSETOF(JSValue, u.asBits.payload) + (CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))));
170         store32(regT1, Address(regT3, OBJECT_OFFSETOF(JSValue, u.asBits.tag) + (CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))));
171
172         // Copy arguments.
173         neg32(regT2);
174         end.append(branchAdd32(Zero, TrustedImm32(1), regT2));
175         // regT2: -argumentCount;
176
177         Label copyLoop = label();
178         load32(BaseIndex(callFrameRegister, regT2, TimesEight, OBJECT_OFFSETOF(JSValue, u.asBits.payload) +(CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))), regT0);
179         load32(BaseIndex(callFrameRegister, regT2, TimesEight, OBJECT_OFFSETOF(JSValue, u.asBits.tag) +(CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))), regT1);
180         store32(regT0, BaseIndex(regT3, regT2, TimesEight, OBJECT_OFFSETOF(JSValue, u.asBits.payload) +(CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))));
181         store32(regT1, BaseIndex(regT3, regT2, TimesEight, OBJECT_OFFSETOF(JSValue, u.asBits.tag) +(CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))));
182         branchAdd32(NonZero, TrustedImm32(1), regT2).linkTo(copyLoop, this);
183
184         end.append(jump());
185     }
186
187     if (canOptimize)
188         slowCase.link(this);
189
190     JITStubCall stubCall(this, cti_op_load_varargs);
191     stubCall.addArgument(thisValue);
192     stubCall.addArgument(arguments);
193     stubCall.addArgument(Imm32(firstFreeRegister));
194     stubCall.call(regT3);
195
196     if (canOptimize)
197         end.link(this);
198 }
199
200 void JIT::compileCallEval()
201 {
202     JITStubCall stubCall(this, cti_op_call_eval); // Initializes ScopeChain; ReturnPC; CodeBlock.
203     stubCall.call();
204     addSlowCase(branch32(Equal, regT1, TrustedImm32(JSValue::EmptyValueTag)));
205     emitGetFromCallFrameHeaderPtr(JSStack::CallerFrame, callFrameRegister);
206
207     sampleCodeBlock(m_codeBlock);
208 }
209
210 void JIT::compileCallEvalSlowCase(Vector<SlowCaseEntry>::iterator& iter)
211 {
212     linkSlowCase(iter);
213
214     emitLoad(JSStack::Callee, regT1, regT0);
215     emitNakedCall(m_globalData->jitStubs->ctiVirtualCall());
216
217     sampleCodeBlock(m_codeBlock);
218 }
219
220 void JIT::compileOpCall(OpcodeID opcodeID, Instruction* instruction, unsigned callLinkInfoIndex)
221 {
222     int callee = instruction[1].u.operand;
223
224     /* Caller always:
225         - Updates callFrameRegister to callee callFrame.
226         - Initializes ArgumentCount; CallerFrame; Callee.
227
228        For a JS call:
229         - Caller initializes ScopeChain.
230         - Callee initializes ReturnPC; CodeBlock.
231         - Callee restores callFrameRegister before return.
232
233        For a non-JS call:
234         - Caller initializes ScopeChain; ReturnPC; CodeBlock.
235         - Caller restores callFrameRegister after return.
236     */
237     
238     if (opcodeID == op_call_varargs)
239         compileLoadVarargs(instruction);
240     else {
241         int argCount = instruction[2].u.operand;
242         int registerOffset = instruction[3].u.operand;
243         
244         if (opcodeID == op_call && canBeOptimized()) {
245             emitLoad(registerOffset + CallFrame::argumentOffsetIncludingThis(0), regT0, regT1);
246             Jump done = branch32(NotEqual, regT0, TrustedImm32(JSValue::CellTag));
247             loadPtr(Address(regT1, JSCell::structureOffset()), regT1);
248             storePtr(regT1, instruction[5].u.arrayProfile->addressOfLastSeenStructure());
249             done.link(this);
250         }
251     
252         addPtr(TrustedImm32(registerOffset * sizeof(Register)), callFrameRegister, regT3);
253
254         store32(TrustedImm32(argCount), payloadFor(JSStack::ArgumentCount, regT3));
255     } // regT3 holds newCallFrame with ArgumentCount initialized.
256     
257     storePtr(TrustedImmPtr(instruction), tagFor(JSStack::ArgumentCount, callFrameRegister));
258     emitLoad(callee, regT1, regT0); // regT1, regT0 holds callee.
259
260     storePtr(callFrameRegister, Address(regT3, JSStack::CallerFrame * static_cast<int>(sizeof(Register))));
261     emitStore(JSStack::Callee, regT1, regT0, regT3);
262     move(regT3, callFrameRegister);
263
264     if (opcodeID == op_call_eval) {
265         compileCallEval();
266         return;
267     }
268
269     DataLabelPtr addressOfLinkedFunctionCheck;
270     BEGIN_UNINTERRUPTED_SEQUENCE(sequenceOpCall);
271     Jump slowCase = branchPtrWithPatch(NotEqual, regT0, addressOfLinkedFunctionCheck, TrustedImmPtr(0));
272     END_UNINTERRUPTED_SEQUENCE(sequenceOpCall);
273
274     addSlowCase(slowCase);
275     addSlowCase(branch32(NotEqual, regT1, TrustedImm32(JSValue::CellTag)));
276
277     ASSERT(m_callStructureStubCompilationInfo.size() == callLinkInfoIndex);
278     m_callStructureStubCompilationInfo.append(StructureStubCompilationInfo());
279     m_callStructureStubCompilationInfo[callLinkInfoIndex].hotPathBegin = addressOfLinkedFunctionCheck;
280     m_callStructureStubCompilationInfo[callLinkInfoIndex].callType = CallLinkInfo::callTypeFor(opcodeID);
281     m_callStructureStubCompilationInfo[callLinkInfoIndex].bytecodeIndex = m_bytecodeOffset;
282
283     loadPtr(Address(regT0, OBJECT_OFFSETOF(JSFunction, m_scope)), regT1);
284     emitPutCellToCallFrameHeader(regT1, JSStack::ScopeChain);
285     m_callStructureStubCompilationInfo[callLinkInfoIndex].hotPathOther = emitNakedCall();
286
287     sampleCodeBlock(m_codeBlock);
288 }
289
290 void JIT::compileOpCallSlowCase(OpcodeID opcodeID, Instruction*, Vector<SlowCaseEntry>::iterator& iter, unsigned callLinkInfoIndex)
291 {
292     if (opcodeID == op_call_eval) {
293         compileCallEvalSlowCase(iter);
294         return;
295     }
296
297     linkSlowCase(iter);
298     linkSlowCase(iter);
299     
300     m_callStructureStubCompilationInfo[callLinkInfoIndex].callReturnLocation = emitNakedCall(opcodeID == op_construct ? m_globalData->jitStubs->ctiVirtualConstructLink() : m_globalData->jitStubs->ctiVirtualCallLink());
301
302     sampleCodeBlock(m_codeBlock);
303 }
304
305 } // namespace JSC
306
307 #endif // USE(JSVALUE32_64)
308 #endif // ENABLE(JIT)