Rename Operations.h to JSCInlines.h
[WebKit-https.git] / Source / JavaScriptCore / jit / JITCall32_64.cpp
1 /*
2  * Copyright (C) 2008, 2013, 2014 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 "JSArray.h"
37 #include "JSFunction.h"
38 #include "JSCInlines.h"
39 #include "RepatchBuffer.h"
40 #include "ResultType.h"
41 #include "SamplingTool.h"
42 #include "StackAlignment.h"
43 #include <wtf/StringPrintStream.h>
44
45
46 namespace JSC {
47
48 void JIT::emitPutCallResult(Instruction* instruction)
49 {
50     int dst = instruction[1].u.operand;
51     emitValueProfilingSite();
52     emitStore(dst, regT1, regT0);
53 }
54
55 void JIT::emit_op_ret(Instruction* currentInstruction)
56 {
57     unsigned dst = currentInstruction[1].u.operand;
58
59     emitLoad(dst, regT1, regT0);
60
61     checkStackPointerAlignment();
62     emitFunctionEpilogue();
63     ret();
64 }
65
66 void JIT::emit_op_ret_object_or_this(Instruction* currentInstruction)
67 {
68     unsigned result = currentInstruction[1].u.operand;
69     unsigned thisReg = currentInstruction[2].u.operand;
70
71     emitLoad(result, regT1, regT0);
72     Jump notJSCell = branch32(NotEqual, regT1, TrustedImm32(JSValue::CellTag));
73     loadPtr(Address(regT0, JSCell::structureOffset()), regT2);
74     Jump notObject = emitJumpIfNotObject(regT2);
75
76     checkStackPointerAlignment();
77     emitFunctionEpilogue();
78     ret();
79
80     notJSCell.link(this);
81     notObject.link(this);
82     emitLoad(thisReg, regT1, regT0);
83
84     checkStackPointerAlignment();
85     emitFunctionEpilogue();
86     ret();
87 }
88
89 void JIT::emitSlow_op_call(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
90 {
91     compileOpCallSlowCase(op_call, currentInstruction, iter, m_callLinkInfoIndex++);
92 }
93
94 void JIT::emitSlow_op_call_eval(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
95 {
96     compileOpCallSlowCase(op_call_eval, currentInstruction, iter, m_callLinkInfoIndex);
97 }
98  
99 void JIT::emitSlow_op_call_varargs(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
100 {
101     compileOpCallSlowCase(op_call_varargs, currentInstruction, iter, m_callLinkInfoIndex++);
102 }
103
104 void JIT::emitSlow_op_construct(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
105 {
106     compileOpCallSlowCase(op_construct, currentInstruction, iter, m_callLinkInfoIndex++);
107 }
108
109 void JIT::emit_op_call(Instruction* currentInstruction)
110 {
111     compileOpCall(op_call, currentInstruction, m_callLinkInfoIndex++);
112 }
113
114 void JIT::emit_op_call_eval(Instruction* currentInstruction)
115 {
116     compileOpCall(op_call_eval, currentInstruction, m_callLinkInfoIndex);
117 }
118
119 void JIT::emit_op_call_varargs(Instruction* currentInstruction)
120 {
121     compileOpCall(op_call_varargs, currentInstruction, m_callLinkInfoIndex++);
122 }
123
124 void JIT::emit_op_construct(Instruction* currentInstruction)
125 {
126     compileOpCall(op_construct, currentInstruction, m_callLinkInfoIndex++);
127 }
128
129 void JIT::compileLoadVarargs(Instruction* instruction)
130 {
131     int thisValue = instruction[3].u.operand;
132     int arguments = instruction[4].u.operand;
133     int firstFreeRegister = instruction[5].u.operand;
134
135     JumpList slowCase;
136     JumpList end;
137     bool canOptimize = m_codeBlock->usesArguments()
138         && VirtualRegister(arguments) == m_codeBlock->argumentsRegister()
139         && !m_codeBlock->symbolTable()->slowArguments();
140
141     if (canOptimize) {
142         emitLoadTag(arguments, regT1);
143         slowCase.append(branch32(NotEqual, regT1, TrustedImm32(JSValue::EmptyValueTag)));
144
145         load32(payloadFor(JSStack::ArgumentCount), regT2);
146         slowCase.append(branch32(Above, regT2, TrustedImm32(Arguments::MaxArguments + 1)));
147         // regT2: argumentCountIncludingThis
148
149         move(regT2, regT3);
150         addPtr(TrustedImm32(-firstFreeRegister + JSStack::CallFrameHeaderSize), regT3);
151         // regT1 now has the required frame size in Register units
152         // Round regT1 to next multiple of stackAlignmentRegisters()
153         addPtr(TrustedImm32(stackAlignmentRegisters() - 1), regT3);
154         andPtr(TrustedImm32(~(stackAlignmentRegisters() - 1)), regT3);
155         neg32(regT3);
156         lshift32(TrustedImm32(3), regT3);
157         addPtr(callFrameRegister, regT3);
158         // regT3: newCallFrame
159
160         slowCase.append(branchPtr(Above, AbsoluteAddress(m_vm->addressOfJSStackLimit()), regT3));
161
162         // Initialize ArgumentCount.
163         store32(regT2, payloadFor(JSStack::ArgumentCount, regT3));
164
165         // Initialize 'this'.
166         emitLoad(thisValue, regT1, regT0);
167         store32(regT0, Address(regT3, OBJECT_OFFSETOF(JSValue, u.asBits.payload) + (CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))));
168         store32(regT1, Address(regT3, OBJECT_OFFSETOF(JSValue, u.asBits.tag) + (CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))));
169
170         // Copy arguments.
171         end.append(branchSub32(Zero, TrustedImm32(1), regT2));
172         // regT2: argumentCount;
173
174         Label copyLoop = label();
175         load32(BaseIndex(callFrameRegister, regT2, TimesEight, OBJECT_OFFSETOF(JSValue, u.asBits.payload) +(CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))), regT0);
176         load32(BaseIndex(callFrameRegister, regT2, TimesEight, OBJECT_OFFSETOF(JSValue, u.asBits.tag) +(CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))), regT1);
177         store32(regT0, BaseIndex(regT3, regT2, TimesEight, OBJECT_OFFSETOF(JSValue, u.asBits.payload) +(CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))));
178         store32(regT1, BaseIndex(regT3, regT2, TimesEight, OBJECT_OFFSETOF(JSValue, u.asBits.tag) +(CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register)))));
179         branchSub32(NonZero, TrustedImm32(1), regT2).linkTo(copyLoop, this);
180
181         end.append(jump());
182     }
183
184     if (canOptimize)
185         slowCase.link(this);
186
187     emitLoad(arguments, regT1, regT0);
188     callOperation(operationSizeFrameForVarargs, regT1, regT0, firstFreeRegister);
189     move(returnValueGPR, stackPointerRegister);
190     emitLoad(thisValue, regT1, regT4);
191     emitLoad(arguments, regT3, regT2);
192     callOperation(operationLoadVarargs, returnValueGPR, regT1, regT4, regT3, regT2);
193     move(returnValueGPR, regT3);
194
195     if (canOptimize)
196         end.link(this);
197
198     addPtr(TrustedImm32(sizeof(CallerFrameAndPC)), regT3, stackPointerRegister);
199 }
200
201 void JIT::compileCallEval(Instruction* instruction)
202 {
203     addPtr(TrustedImm32(-static_cast<ptrdiff_t>(sizeof(CallerFrameAndPC))), stackPointerRegister, regT1);
204
205     callOperationNoExceptionCheck(operationCallEval, regT1);
206
207     Jump noException = emitExceptionCheck(InvertedExceptionCheck);
208     addPtr(TrustedImm32(stackPointerOffsetFor(m_codeBlock) * sizeof(Register)), callFrameRegister, stackPointerRegister);
209     exceptionCheck(jump());
210
211     noException.link(this);
212     addSlowCase(branch32(Equal, regT1, TrustedImm32(JSValue::EmptyValueTag)));
213
214     addPtr(TrustedImm32(stackPointerOffsetFor(m_codeBlock) * sizeof(Register)), callFrameRegister, stackPointerRegister);
215     checkStackPointerAlignment();
216
217     sampleCodeBlock(m_codeBlock);
218     
219     emitPutCallResult(instruction);
220 }
221
222 void JIT::compileCallEvalSlowCase(Instruction* instruction, Vector<SlowCaseEntry>::iterator& iter)
223 {
224     linkSlowCase(iter);
225
226     loadPtr(Address(stackPointerRegister, sizeof(Register) * JSStack::Callee - sizeof(CallerFrameAndPC)), regT0);
227     loadPtr(Address(stackPointerRegister, sizeof(Register) * JSStack::Callee - sizeof(CallerFrameAndPC)), regT1);
228
229     emitLoad(JSStack::Callee, regT1, regT0);
230     emitNakedCall(m_vm->getCTIStub(virtualCallThunkGenerator).code());
231     addPtr(TrustedImm32(stackPointerOffsetFor(m_codeBlock) * sizeof(Register)), callFrameRegister, stackPointerRegister);
232     checkStackPointerAlignment();
233
234     sampleCodeBlock(m_codeBlock);
235     
236     emitPutCallResult(instruction);
237 }
238
239 void JIT::compileOpCall(OpcodeID opcodeID, Instruction* instruction, unsigned callLinkInfoIndex)
240 {
241     int callee = instruction[2].u.operand;
242
243     /* Caller always:
244         - Updates callFrameRegister to callee callFrame.
245         - Initializes ArgumentCount; CallerFrame; Callee.
246
247        For a JS call:
248         - Caller initializes ScopeChain.
249         - Callee initializes ReturnPC; CodeBlock.
250         - Callee restores callFrameRegister before return.
251
252        For a non-JS call:
253         - Caller initializes ScopeChain; ReturnPC; CodeBlock.
254         - Caller restores callFrameRegister after return.
255     */
256     
257     if (opcodeID == op_call_varargs)
258         compileLoadVarargs(instruction);
259     else {
260         int argCount = instruction[3].u.operand;
261         int registerOffset = -instruction[4].u.operand;
262         
263         if (opcodeID == op_call && shouldEmitProfiling()) {
264             emitLoad(registerOffset + CallFrame::argumentOffsetIncludingThis(0), regT0, regT1);
265             Jump done = branch32(NotEqual, regT0, TrustedImm32(JSValue::CellTag));
266             loadPtr(Address(regT1, JSCell::structureOffset()), regT1);
267             storePtr(regT1, instruction[6].u.arrayProfile->addressOfLastSeenStructure());
268             done.link(this);
269         }
270     
271         addPtr(TrustedImm32(registerOffset * sizeof(Register) + sizeof(CallerFrameAndPC)), callFrameRegister, stackPointerRegister);
272
273         store32(TrustedImm32(argCount), Address(stackPointerRegister, JSStack::ArgumentCount * static_cast<int>(sizeof(Register)) + PayloadOffset - sizeof(CallerFrameAndPC)));
274     } // SP holds newCallFrame + sizeof(CallerFrameAndPC), with ArgumentCount initialized.
275     
276     uint32_t locationBits = CallFrame::Location::encodeAsBytecodeInstruction(instruction);
277     store32(TrustedImm32(locationBits), tagFor(JSStack::ArgumentCount, callFrameRegister));
278     emitLoad(callee, regT1, regT0); // regT1, regT0 holds callee.
279
280     store32(regT0, Address(stackPointerRegister, JSStack::Callee * static_cast<int>(sizeof(Register)) + PayloadOffset - sizeof(CallerFrameAndPC)));
281     store32(regT1, Address(stackPointerRegister, JSStack::Callee * static_cast<int>(sizeof(Register)) + TagOffset - sizeof(CallerFrameAndPC)));
282
283     if (opcodeID == op_call_eval) {
284         compileCallEval(instruction);
285         return;
286     }
287
288     DataLabelPtr addressOfLinkedFunctionCheck;
289     Jump slowCase = branchPtrWithPatch(NotEqual, regT0, addressOfLinkedFunctionCheck, TrustedImmPtr(0));
290
291     addSlowCase(slowCase);
292     addSlowCase(branch32(NotEqual, regT1, TrustedImm32(JSValue::CellTag)));
293
294     ASSERT(m_callStructureStubCompilationInfo.size() == callLinkInfoIndex);
295     m_callStructureStubCompilationInfo.append(StructureStubCompilationInfo());
296     m_callStructureStubCompilationInfo[callLinkInfoIndex].hotPathBegin = addressOfLinkedFunctionCheck;
297     m_callStructureStubCompilationInfo[callLinkInfoIndex].callType = CallLinkInfo::callTypeFor(opcodeID);
298     m_callStructureStubCompilationInfo[callLinkInfoIndex].bytecodeIndex = m_bytecodeOffset;
299
300     loadPtr(Address(regT0, OBJECT_OFFSETOF(JSFunction, m_scope)), regT2);
301     store32(regT2, Address(MacroAssembler::stackPointerRegister, JSStack::ScopeChain * sizeof(Register) + PayloadOffset - sizeof(CallerFrameAndPC)));
302     store32(TrustedImm32(JSValue::CellTag), Address(stackPointerRegister, JSStack::ScopeChain * sizeof(Register) + TagOffset - sizeof(CallerFrameAndPC)));
303
304     checkStackPointerAlignment();
305     m_callStructureStubCompilationInfo[callLinkInfoIndex].hotPathOther = emitNakedCall();
306
307     addPtr(TrustedImm32(stackPointerOffsetFor(m_codeBlock) * sizeof(Register)), callFrameRegister, stackPointerRegister);
308     checkStackPointerAlignment();
309
310     sampleCodeBlock(m_codeBlock);
311     emitPutCallResult(instruction);
312 }
313
314 void JIT::compileOpCallSlowCase(OpcodeID opcodeID, Instruction* instruction, Vector<SlowCaseEntry>::iterator& iter, unsigned callLinkInfoIndex)
315 {
316     if (opcodeID == op_call_eval) {
317         compileCallEvalSlowCase(instruction, iter);
318         return;
319     }
320
321     linkSlowCase(iter);
322     linkSlowCase(iter);
323
324     ThunkGenerator generator = linkThunkGeneratorFor(
325         opcodeID == op_construct ? CodeForConstruct : CodeForCall,
326         RegisterPreservationNotRequired);
327     
328     m_callStructureStubCompilationInfo[callLinkInfoIndex].callReturnLocation = emitNakedCall(m_vm->getCTIStub(generator).code());
329
330     addPtr(TrustedImm32(stackPointerOffsetFor(m_codeBlock) * sizeof(Register)), callFrameRegister, stackPointerRegister);
331     checkStackPointerAlignment();
332
333     sampleCodeBlock(m_codeBlock);
334     emitPutCallResult(instruction);
335 }
336
337 void JIT::privateCompileClosureCall(CallLinkInfo* callLinkInfo, CodeBlock* calleeCodeBlock, Structure* expectedStructure, ExecutableBase* expectedExecutable, MacroAssemblerCodePtr codePtr)
338 {
339     JumpList slowCases;
340
341     slowCases.append(branch32(NotEqual, regT1, TrustedImm32(JSValue::CellTag)));
342     slowCases.append(branchPtr(NotEqual, Address(regT0, JSCell::structureOffset()), TrustedImmPtr(expectedStructure)));
343     slowCases.append(branchPtr(NotEqual, Address(regT0, JSFunction::offsetOfExecutable()), TrustedImmPtr(expectedExecutable)));
344     
345     loadPtr(Address(regT0, JSFunction::offsetOfScopeChain()), regT1);
346     emitPutCellToCallFrameHeader(regT1, JSStack::ScopeChain);
347     
348     Call call = nearCall();
349     Jump done = jump();
350     
351     slowCases.link(this);
352     move(TrustedImmPtr(callLinkInfo->callReturnLocation.executableAddress()), regT2);
353     restoreReturnAddressBeforeReturn(regT2);
354     Jump slow = jump();
355     
356     LinkBuffer patchBuffer(*m_vm, this, m_codeBlock);
357     
358     patchBuffer.link(call, FunctionPtr(codePtr.executableAddress()));
359     patchBuffer.link(done, callLinkInfo->hotPathOther.labelAtOffset(0));
360     patchBuffer.link(slow, CodeLocationLabel(m_vm->getCTIStub(virtualCallThunkGenerator).code()));
361     
362     RefPtr<ClosureCallStubRoutine> stubRoutine = adoptRef(new ClosureCallStubRoutine(
363         FINALIZE_CODE(
364             patchBuffer,
365             ("Baseline closure call stub for %s, return point %p, target %p (%s)",
366                 toCString(*m_codeBlock).data(),
367                 callLinkInfo->hotPathOther.labelAtOffset(0).executableAddress(),
368                 codePtr.executableAddress(),
369                 toCString(pointerDump(calleeCodeBlock)).data())),
370         *m_vm, m_codeBlock->ownerExecutable(), expectedStructure, expectedExecutable,
371         callLinkInfo->codeOrigin));
372     
373     RepatchBuffer repatchBuffer(m_codeBlock);
374     
375     repatchBuffer.replaceWithJump(
376         RepatchBuffer::startOfBranchPtrWithPatchOnRegister(callLinkInfo->hotPathBegin),
377         CodeLocationLabel(stubRoutine->code().code()));
378     repatchBuffer.relink(callLinkInfo->callReturnLocation, m_vm->getCTIStub(virtualCallThunkGenerator).code());
379     
380     callLinkInfo->stub = stubRoutine.release();
381 }
382
383 } // namespace JSC
384
385 #endif // USE(JSVALUE32_64)
386 #endif // ENABLE(JIT)