Unreviewed, fix for builds without VALUE_PROFILING. I had forgotten that shouldEmitPr...
[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 "JITInlineMethods.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(RegisterFile::ReturnPC, regT2);
63     emitGetFromCallFrameHeaderPtr(RegisterFile::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(RegisterFile::ReturnPC, regT2);
80     emitGetFromCallFrameHeaderPtr(RegisterFile::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(RegisterFile::ReturnPC, regT2);
90     emitGetFromCallFrameHeaderPtr(RegisterFile::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     if (m_codeBlock->usesArguments() && arguments == m_codeBlock->argumentsRegister()) {
145         emitLoadTag(arguments, regT1);
146         slowCase.append(branch32(NotEqual, regT1, TrustedImm32(JSValue::EmptyValueTag)));
147
148         load32(payloadFor(RegisterFile::ArgumentCount), regT2);
149         slowCase.append(branch32(Above, regT2, TrustedImm32(Arguments::MaxArguments + 1)));
150         // regT2: argumentCountIncludingThis
151
152         move(regT2, regT3);
153         add32(TrustedImm32(firstFreeRegister + RegisterFile::CallFrameHeaderSize), regT3);
154         lshift32(TrustedImm32(3), regT3);
155         addPtr(callFrameRegister, regT3);
156         // regT3: newCallFrame
157
158         slowCase.append(branchPtr(Below, AbsoluteAddress(m_globalData->interpreter->registerFile().addressOfEnd()), regT3));
159
160         // Initialize ArgumentCount.
161         store32(regT2, payloadFor(RegisterFile::ArgumentCount, regT3));
162
163         // Initialize 'this'.
164         emitLoad(thisValue, regT1, regT0);
165         store32(regT0, Address(regT3, OBJECT_OFFSETOF(JSValue, u.asBits.payload) + (CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))));
166         store32(regT1, Address(regT3, OBJECT_OFFSETOF(JSValue, u.asBits.tag) + (CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))));
167
168         // Copy arguments.
169         neg32(regT2);
170         end.append(branchAdd32(Zero, TrustedImm32(1), regT2));
171         // regT2: -argumentCount;
172
173         Label copyLoop = label();
174         load32(BaseIndex(callFrameRegister, regT2, TimesEight, OBJECT_OFFSETOF(JSValue, u.asBits.payload) +(CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))), regT0);
175         load32(BaseIndex(callFrameRegister, regT2, TimesEight, OBJECT_OFFSETOF(JSValue, u.asBits.tag) +(CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))), regT1);
176         store32(regT0, BaseIndex(regT3, regT2, TimesEight, OBJECT_OFFSETOF(JSValue, u.asBits.payload) +(CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))));
177         store32(regT1, BaseIndex(regT3, regT2, TimesEight, OBJECT_OFFSETOF(JSValue, u.asBits.tag) +(CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))));
178         branchAdd32(NonZero, TrustedImm32(1), regT2).linkTo(copyLoop, this);
179
180         end.append(jump());
181     }
182
183     if (m_codeBlock->usesArguments() && arguments == m_codeBlock->argumentsRegister())
184         slowCase.link(this);
185
186     JITStubCall stubCall(this, cti_op_load_varargs);
187     stubCall.addArgument(thisValue);
188     stubCall.addArgument(arguments);
189     stubCall.addArgument(Imm32(firstFreeRegister));
190     stubCall.call(regT3);
191
192     if (m_codeBlock->usesArguments() && arguments == m_codeBlock->argumentsRegister())
193         end.link(this);
194 }
195
196 void JIT::compileCallEval()
197 {
198     JITStubCall stubCall(this, cti_op_call_eval); // Initializes ScopeChain; ReturnPC; CodeBlock.
199     stubCall.call();
200     addSlowCase(branch32(Equal, regT1, TrustedImm32(JSValue::EmptyValueTag)));
201     emitGetFromCallFrameHeaderPtr(RegisterFile::CallerFrame, callFrameRegister);
202
203     sampleCodeBlock(m_codeBlock);
204 }
205
206 void JIT::compileCallEvalSlowCase(Vector<SlowCaseEntry>::iterator& iter)
207 {
208     linkSlowCase(iter);
209
210     emitLoad(RegisterFile::Callee, regT1, regT0);
211     emitNakedCall(m_globalData->jitStubs->ctiVirtualCall());
212
213     sampleCodeBlock(m_codeBlock);
214 }
215
216 void JIT::compileOpCall(OpcodeID opcodeID, Instruction* instruction, unsigned callLinkInfoIndex)
217 {
218     int callee = instruction[1].u.operand;
219
220     /* Caller always:
221         - Updates callFrameRegister to callee callFrame.
222         - Initializes ArgumentCount; CallerFrame; Callee.
223
224        For a JS call:
225         - Caller initializes ScopeChain.
226         - Callee initializes ReturnPC; CodeBlock.
227         - Callee restores callFrameRegister before return.
228
229        For a non-JS call:
230         - Caller initializes ScopeChain; ReturnPC; CodeBlock.
231         - Caller restores callFrameRegister after return.
232     */
233     
234     if (opcodeID == op_call_varargs)
235         compileLoadVarargs(instruction);
236     else {
237         int argCount = instruction[2].u.operand;
238         int registerOffset = instruction[3].u.operand;
239         
240         if (opcodeID == op_call && canBeOptimized()) {
241             emitLoad(registerOffset + CallFrame::argumentOffsetIncludingThis(0), regT0, regT1);
242             Jump done = branch32(NotEqual, regT0, TrustedImm32(JSValue::CellTag));
243             loadPtr(Address(regT1, JSCell::structureOffset()), regT1);
244             storePtr(regT1, instruction[5].u.arrayProfile->addressOfLastSeenStructure());
245             done.link(this);
246         }
247     
248         addPtr(TrustedImm32(registerOffset * sizeof(Register)), callFrameRegister, regT3);
249
250         store32(TrustedImm32(argCount), payloadFor(RegisterFile::ArgumentCount, regT3));
251     } // regT3 holds newCallFrame with ArgumentCount initialized.
252     
253     storePtr(TrustedImmPtr(instruction), tagFor(RegisterFile::ArgumentCount, callFrameRegister));
254     emitLoad(callee, regT1, regT0); // regT1, regT0 holds callee.
255
256     storePtr(callFrameRegister, Address(regT3, RegisterFile::CallerFrame * static_cast<int>(sizeof(Register))));
257     emitStore(RegisterFile::Callee, regT1, regT0, regT3);
258     move(regT3, callFrameRegister);
259
260     if (opcodeID == op_call_eval) {
261         compileCallEval();
262         return;
263     }
264
265     DataLabelPtr addressOfLinkedFunctionCheck;
266     BEGIN_UNINTERRUPTED_SEQUENCE(sequenceOpCall);
267     Jump slowCase = branchPtrWithPatch(NotEqual, regT0, addressOfLinkedFunctionCheck, TrustedImmPtr(0));
268     END_UNINTERRUPTED_SEQUENCE(sequenceOpCall);
269
270     addSlowCase(slowCase);
271     addSlowCase(branch32(NotEqual, regT1, TrustedImm32(JSValue::CellTag)));
272
273     ASSERT(m_callStructureStubCompilationInfo.size() == callLinkInfoIndex);
274     m_callStructureStubCompilationInfo.append(StructureStubCompilationInfo());
275     m_callStructureStubCompilationInfo[callLinkInfoIndex].hotPathBegin = addressOfLinkedFunctionCheck;
276     m_callStructureStubCompilationInfo[callLinkInfoIndex].callType = CallLinkInfo::callTypeFor(opcodeID);
277     m_callStructureStubCompilationInfo[callLinkInfoIndex].bytecodeIndex = m_bytecodeOffset;
278
279     loadPtr(Address(regT0, OBJECT_OFFSETOF(JSFunction, m_scopeChain)), regT1);
280     emitPutCellToCallFrameHeader(regT1, RegisterFile::ScopeChain);
281     m_callStructureStubCompilationInfo[callLinkInfoIndex].hotPathOther = emitNakedCall();
282
283     sampleCodeBlock(m_codeBlock);
284 }
285
286 void JIT::compileOpCallSlowCase(OpcodeID opcodeID, Instruction*, Vector<SlowCaseEntry>::iterator& iter, unsigned callLinkInfoIndex)
287 {
288     if (opcodeID == op_call_eval) {
289         compileCallEvalSlowCase(iter);
290         return;
291     }
292
293     linkSlowCase(iter);
294     linkSlowCase(iter);
295     
296     m_callStructureStubCompilationInfo[callLinkInfoIndex].callReturnLocation = emitNakedCall(opcodeID == op_construct ? m_globalData->jitStubs->ctiVirtualConstructLink() : m_globalData->jitStubs->ctiVirtualCallLink());
297
298     sampleCodeBlock(m_codeBlock);
299 }
300
301 } // namespace JSC
302
303 #endif // USE(JSVALUE32_64)
304 #endif // ENABLE(JIT)