Value profiling in baseline JIT for JSVALUE32_64
[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 "CodeBlock.h"
33 #include "Interpreter.h"
34 #include "JITInlineMethods.h"
35 #include "JITStubCall.h"
36 #include "JSArray.h"
37 #include "JSFunction.h"
38 #include "ResultType.h"
39 #include "SamplingTool.h"
40
41 #ifndef NDEBUG
42 #include <stdio.h>
43 #endif
44
45 using namespace std;
46
47 namespace JSC {
48
49 void JIT::compileOpCallInitializeCallFrame()
50 {
51     // regT0 holds callee, regT1 holds argCount
52     loadPtr(Address(regT0, OBJECT_OFFSETOF(JSFunction, m_scopeChain)), regT3); // scopeChain
53     emitPutIntToCallFrameHeader(regT1, RegisterFile::ArgumentCount);
54     emitPutCellToCallFrameHeader(regT0, RegisterFile::Callee);
55     emitPutCellToCallFrameHeader(regT3, RegisterFile::ScopeChain);
56 }
57
58 void JIT::emit_op_call_put_result(Instruction* instruction)
59 {
60     int dst = instruction[1].u.operand;
61     emitValueProfilingSite(FirstProfilingSite);
62     emitStore(dst, regT1, regT0);
63 }
64
65 void JIT::compileOpCallVarargs(Instruction* instruction)
66 {
67     int callee = instruction[1].u.operand;
68     int argCountRegister = instruction[2].u.operand;
69     int registerOffset = instruction[3].u.operand;
70
71     emitLoad(callee, regT1, regT0);
72     emitLoadPayload(argCountRegister, regT2); // argCount
73     addPtr(Imm32(registerOffset), regT2, regT3); // registerOffset
74
75     emitJumpSlowCaseIfNotJSCell(callee, regT1);
76     addSlowCase(emitJumpIfNotType(regT0, regT1, JSFunctionType));
77
78     // Speculatively roll the callframe, assuming argCount will match the arity.
79     mul32(TrustedImm32(sizeof(Register)), regT3, regT3);
80     addPtr(callFrameRegister, regT3);
81     store32(TrustedImm32(JSValue::CellTag), tagFor(RegisterFile::CallerFrame, regT3));
82     storePtr(callFrameRegister, payloadFor(RegisterFile::CallerFrame, regT3));
83     move(regT3, callFrameRegister);
84
85     move(regT2, regT1); // argCount
86
87     emitNakedCall(m_globalData->jitStubs->ctiVirtualCall());
88
89     sampleCodeBlock(m_codeBlock);
90 }
91
92 void JIT::compileOpCallVarargsSlowCase(Instruction* instruction, Vector<SlowCaseEntry>::iterator& iter)
93 {
94     int callee = instruction[1].u.operand;
95
96     linkSlowCaseIfNotJSCell(iter, callee);
97     Jump notCell = jump();
98     linkSlowCase(iter);
99     move(TrustedImm32(JSValue::CellTag), regT1); // Need to restore cell tag in regT1 because it was clobbered.
100     notCell.link(this);
101
102     JITStubCall stubCall(this, cti_op_call_NotJSFunction);
103     stubCall.addArgument(regT1, regT0);
104     stubCall.addArgument(regT3);
105     stubCall.addArgument(regT2);
106     stubCall.call();
107
108     sampleCodeBlock(m_codeBlock);
109 }
110
111 void JIT::emit_op_ret(Instruction* currentInstruction)
112 {
113     emitOptimizationCheck(RetOptimizationCheck);
114     
115     unsigned dst = currentInstruction[1].u.operand;
116
117     emitLoad(dst, regT1, regT0);
118     emitGetFromCallFrameHeaderPtr(RegisterFile::ReturnPC, regT2);
119     emitGetFromCallFrameHeaderPtr(RegisterFile::CallerFrame, callFrameRegister);
120
121     restoreReturnAddressBeforeReturn(regT2);
122     ret();
123 }
124
125 void JIT::emit_op_ret_object_or_this(Instruction* currentInstruction)
126 {
127     emitOptimizationCheck(RetOptimizationCheck);
128     
129     unsigned result = currentInstruction[1].u.operand;
130     unsigned thisReg = currentInstruction[2].u.operand;
131
132     emitLoad(result, regT1, regT0);
133     Jump notJSCell = branch32(NotEqual, regT1, TrustedImm32(JSValue::CellTag));
134     loadPtr(Address(regT0, JSCell::structureOffset()), regT2);
135     Jump notObject = emitJumpIfNotObject(regT2);
136
137     emitGetFromCallFrameHeaderPtr(RegisterFile::ReturnPC, regT2);
138     emitGetFromCallFrameHeaderPtr(RegisterFile::CallerFrame, callFrameRegister);
139
140     restoreReturnAddressBeforeReturn(regT2);
141     ret();
142
143     notJSCell.link(this);
144     notObject.link(this);
145     emitLoad(thisReg, regT1, regT0);
146
147     emitGetFromCallFrameHeaderPtr(RegisterFile::ReturnPC, regT2);
148     emitGetFromCallFrameHeaderPtr(RegisterFile::CallerFrame, callFrameRegister);
149
150     restoreReturnAddressBeforeReturn(regT2);
151     ret();
152 }
153
154 void JIT::emitSlow_op_call(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
155 {
156     compileOpCallSlowCase(currentInstruction, iter, m_callLinkInfoIndex++, op_call);
157 }
158
159 void JIT::emitSlow_op_call_eval(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
160 {
161     compileOpCallSlowCase(currentInstruction, iter, m_callLinkInfoIndex++, op_call_eval);
162 }
163
164 void JIT::emitSlow_op_call_varargs(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
165 {
166     compileOpCallVarargsSlowCase(currentInstruction, iter);
167 }
168
169 void JIT::emitSlow_op_construct(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
170 {
171     compileOpCallSlowCase(currentInstruction, iter, m_callLinkInfoIndex++, op_construct);
172 }
173
174 void JIT::emit_op_call(Instruction* currentInstruction)
175 {
176     compileOpCall(op_call, currentInstruction, m_callLinkInfoIndex++);
177 }
178
179 void JIT::emit_op_call_eval(Instruction* currentInstruction)
180 {
181     compileOpCall(op_call_eval, currentInstruction, m_callLinkInfoIndex++);
182 }
183
184 void JIT::emit_op_call_varargs(Instruction* currentInstruction)
185 {
186     compileOpCallVarargs(currentInstruction);
187 }
188
189 void JIT::emit_op_construct(Instruction* currentInstruction)
190 {
191     compileOpCall(op_construct, currentInstruction, m_callLinkInfoIndex++);
192 }
193
194 void JIT::compileOpCall(OpcodeID opcodeID, Instruction* instruction, unsigned callLinkInfoIndex)
195 {
196     int callee = instruction[1].u.operand;
197     int argCount = instruction[2].u.operand;
198     int registerOffset = instruction[3].u.operand;
199
200     Jump wasEval;
201     if (opcodeID == op_call_eval) {
202         JITStubCall stubCall(this, cti_op_call_eval);
203         stubCall.addArgument(callee);
204         stubCall.addArgument(JIT::Imm32(registerOffset));
205         stubCall.addArgument(JIT::Imm32(argCount));
206         stubCall.call();
207         wasEval = branch32(NotEqual, regT1, TrustedImm32(JSValue::EmptyValueTag));
208     }
209
210     emitLoad(callee, regT1, regT0);
211
212     DataLabelPtr addressOfLinkedFunctionCheck;
213
214     BEGIN_UNINTERRUPTED_SEQUENCE(sequenceOpCall);
215
216     Jump jumpToSlow = branchPtrWithPatch(NotEqual, regT0, addressOfLinkedFunctionCheck, TrustedImmPtr(0));
217
218     END_UNINTERRUPTED_SEQUENCE(sequenceOpCall);
219
220     addSlowCase(jumpToSlow);
221     ASSERT_JIT_OFFSET(differenceBetween(addressOfLinkedFunctionCheck, jumpToSlow), patchOffsetOpCallCompareToJump);
222     ASSERT(m_callStructureStubCompilationInfo.size() == callLinkInfoIndex);
223     m_callStructureStubCompilationInfo.append(StructureStubCompilationInfo());
224     m_callStructureStubCompilationInfo[callLinkInfoIndex].hotPathBegin = addressOfLinkedFunctionCheck;
225     m_callStructureStubCompilationInfo[callLinkInfoIndex].isCall = opcodeID != op_construct;
226
227     addSlowCase(branch32(NotEqual, regT1, TrustedImm32(JSValue::CellTag)));
228
229     // The following is the fast case, only used whan a callee can be linked.
230
231     // Fast version of stack frame initialization, directly relative to edi.
232     // Note that this omits to set up RegisterFile::CodeBlock, which is set in the callee
233     loadPtr(Address(regT0, OBJECT_OFFSETOF(JSFunction, m_scopeChain)), regT2);
234
235     store32(TrustedImm32(JSValue::Int32Tag), tagFor(registerOffset + RegisterFile::ArgumentCount));
236     store32(Imm32(argCount), payloadFor(registerOffset + RegisterFile::ArgumentCount));
237     storePtr(callFrameRegister, payloadFor(RegisterFile::CallerFrame + registerOffset, callFrameRegister));
238     emitStore(registerOffset + RegisterFile::Callee, regT1, regT0);
239     store32(TrustedImm32(JSValue::CellTag), tagFor(registerOffset + RegisterFile::ScopeChain));
240     store32(regT2, payloadFor(registerOffset + RegisterFile::ScopeChain));
241     addPtr(Imm32(registerOffset * sizeof(Register)), callFrameRegister);
242
243     // Call to the callee
244     m_callStructureStubCompilationInfo[callLinkInfoIndex].hotPathOther = emitNakedCall();
245     
246     if (opcodeID == op_call_eval)
247         wasEval.link(this);
248
249     sampleCodeBlock(m_codeBlock);
250 }
251
252 void JIT::compileOpCallSlowCase(Instruction* instruction, Vector<SlowCaseEntry>::iterator& iter, unsigned callLinkInfoIndex, OpcodeID opcodeID)
253 {
254     int callee = instruction[1].u.operand;
255     int argCount = instruction[2].u.operand;
256     int registerOffset = instruction[3].u.operand;
257
258     linkSlowCase(iter);
259     linkSlowCase(iter);
260
261     // Fast check for JS function.
262     Jump callLinkFailNotObject = branch32(NotEqual, regT1, TrustedImm32(JSValue::CellTag));
263     Jump callLinkFailNotJSFunction = emitJumpIfNotType(regT0, regT1, JSFunctionType);
264
265     // Speculatively roll the callframe, assuming argCount will match the arity.
266     store32(TrustedImm32(JSValue::CellTag), tagFor(RegisterFile::CallerFrame + registerOffset, callFrameRegister));
267     storePtr(callFrameRegister, payloadFor(RegisterFile::CallerFrame + registerOffset, callFrameRegister));
268     addPtr(Imm32(registerOffset * static_cast<int>(sizeof(Register))), callFrameRegister);
269     move(Imm32(argCount), regT1);
270
271     m_callStructureStubCompilationInfo[callLinkInfoIndex].callReturnLocation = emitNakedCall(opcodeID == op_construct ? m_globalData->jitStubs->ctiVirtualConstructLink() : m_globalData->jitStubs->ctiVirtualCallLink());
272
273     // Done! - return back to the hot path.
274     ASSERT(OPCODE_LENGTH(op_call) == OPCODE_LENGTH(op_call_eval));
275     ASSERT(OPCODE_LENGTH(op_call) == OPCODE_LENGTH(op_construct));
276     emitJumpSlowToHot(jump(), OPCODE_LENGTH(op_call));
277
278     // This handles host functions
279     callLinkFailNotJSFunction.link(this);
280     move(TrustedImm32(JSValue::CellTag), regT1); // Restore cell tag since it was clobbered.
281     callLinkFailNotObject.link(this);
282
283     JITStubCall stubCall(this, opcodeID == op_construct ? cti_op_construct_NotJSConstruct : cti_op_call_NotJSFunction);
284     stubCall.addArgument(callee);
285     stubCall.addArgument(JIT::Imm32(registerOffset));
286     stubCall.addArgument(JIT::Imm32(argCount));
287     stubCall.call();
288
289     sampleCodeBlock(m_codeBlock);
290 }
291
292 } // namespace JSC
293
294 #endif // USE(JSVALUE32_64)
295 #endif // ENABLE(JIT)