REGRESSION(r173031): crashes during run-layout-jsc on x86/Linux
[WebKit-https.git] / Source / JavaScriptCore / jit / JITCall.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(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 "StackAlignment.h"
44 #include "ThunkGenerators.h"
45 #include <wtf/StringPrintStream.h>
46
47
48 namespace JSC {
49
50 void JIT::emitPutCallResult(Instruction* instruction)
51 {
52     int dst = instruction[1].u.operand;
53     emitValueProfilingSite();
54     emitPutVirtualRegister(dst);
55 }
56
57 void JIT::compileLoadVarargs(Instruction* instruction)
58 {
59     int thisValue = instruction[3].u.operand;
60     int arguments = instruction[4].u.operand;
61     int firstFreeRegister = instruction[5].u.operand;
62     int firstVarArgOffset = instruction[6].u.operand;
63
64     JumpList slowCase;
65     JumpList end;
66     bool canOptimize = m_codeBlock->usesArguments()
67         && arguments == m_codeBlock->argumentsRegister().offset()
68         && !m_codeBlock->symbolTable()->slowArguments();
69
70     if (canOptimize) {
71         emitGetVirtualRegister(arguments, regT0);
72         slowCase.append(branch64(NotEqual, regT0, TrustedImm64(JSValue::encode(JSValue()))));
73
74         emitGetFromCallFrameHeader32(JSStack::ArgumentCount, regT0);
75         if (firstVarArgOffset) {
76             Jump sufficientArguments = branch32(GreaterThan, regT0, TrustedImm32(firstVarArgOffset + 1));
77             move(TrustedImm32(1), regT0);
78             Jump endVarArgs = jump();
79             sufficientArguments.link(this);
80             sub32(TrustedImm32(firstVarArgOffset), regT0);
81             endVarArgs.link(this);
82         }
83         slowCase.append(branch32(Above, regT0, TrustedImm32(Arguments::MaxArguments + 1)));
84         // regT0: argumentCountIncludingThis
85         move(regT0, regT1);
86         add64(TrustedImm32(-firstFreeRegister + JSStack::CallFrameHeaderSize), regT1);
87         // regT1 now has the required frame size in Register units
88         // Round regT1 to next multiple of stackAlignmentRegisters()
89         add64(TrustedImm32(stackAlignmentRegisters() - 1), regT1);
90         and64(TrustedImm32(~(stackAlignmentRegisters() - 1)), regT1);
91
92         neg64(regT1);
93         lshift64(TrustedImm32(3), regT1);
94         addPtr(callFrameRegister, regT1);
95         // regT1: newCallFrame
96
97         slowCase.append(branchPtr(Above, AbsoluteAddress(m_vm->addressOfStackLimit()), regT1));
98
99         // Initialize ArgumentCount.
100         store32(regT0, Address(regT1, JSStack::ArgumentCount * static_cast<int>(sizeof(Register)) + OBJECT_OFFSETOF(EncodedValueDescriptor, asBits.payload)));
101
102         // Initialize 'this'.
103         emitGetVirtualRegister(thisValue, regT2);
104         store64(regT2, Address(regT1, CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register))));
105
106         // Copy arguments.
107         signExtend32ToPtr(regT0, regT0);
108         end.append(branchSub64(Zero, TrustedImm32(1), regT0));
109         // regT0: argumentCount
110
111         Label copyLoop = label();
112         load64(BaseIndex(callFrameRegister, regT0, TimesEight, (CallFrame::thisArgumentOffset() + firstVarArgOffset) * static_cast<int>(sizeof(Register))), regT2);
113         store64(regT2, BaseIndex(regT1, regT0, TimesEight, CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register))));
114         branchSub64(NonZero, TrustedImm32(1), regT0).linkTo(copyLoop, this);
115
116         end.append(jump());
117     }
118
119     if (canOptimize)
120         slowCase.link(this);
121
122     emitGetVirtualRegister(arguments, regT1);
123     callOperation(operationSizeFrameForVarargs, regT1, firstFreeRegister, firstVarArgOffset);
124     move(returnValueGPR, stackPointerRegister);
125     emitGetVirtualRegister(thisValue, regT1);
126     emitGetVirtualRegister(arguments, regT2);
127     callOperation(operationLoadVarargs, returnValueGPR, regT1, regT2, firstVarArgOffset);
128     move(returnValueGPR, regT1);
129
130     if (canOptimize)
131         end.link(this);
132     
133     addPtr(TrustedImm32(sizeof(CallerFrameAndPC)), regT1, stackPointerRegister);
134 }
135
136 void JIT::compileCallEval(Instruction* instruction)
137 {
138     addPtr(TrustedImm32(-static_cast<ptrdiff_t>(sizeof(CallerFrameAndPC))), stackPointerRegister, regT1);
139     storePtr(callFrameRegister, Address(regT1, CallFrame::callerFrameOffset()));
140
141     addPtr(TrustedImm32(stackPointerOffsetFor(m_codeBlock) * sizeof(Register)), callFrameRegister, stackPointerRegister);
142     checkStackPointerAlignment();
143
144     callOperation(operationCallEval, regT1);
145
146     addSlowCase(branch64(Equal, regT0, TrustedImm64(JSValue::encode(JSValue()))));
147
148     sampleCodeBlock(m_codeBlock);
149     
150     emitPutCallResult(instruction);
151 }
152
153 void JIT::compileCallEvalSlowCase(Instruction* instruction, Vector<SlowCaseEntry>::iterator& iter)
154 {
155     linkSlowCase(iter);
156     int registerOffset = -instruction[4].u.operand;
157
158     addPtr(TrustedImm32(registerOffset * sizeof(Register) + sizeof(CallerFrameAndPC)), callFrameRegister, stackPointerRegister);
159
160     load64(Address(stackPointerRegister, sizeof(Register) * JSStack::Callee - sizeof(CallerFrameAndPC)), regT0);
161     move(TrustedImmPtr(&CallLinkInfo::dummy()), regT2);
162     emitNakedCall(m_vm->getCTIStub(virtualCallThunkGenerator).code());
163     addPtr(TrustedImm32(stackPointerOffsetFor(m_codeBlock) * sizeof(Register)), callFrameRegister, stackPointerRegister);
164     checkStackPointerAlignment();
165
166     sampleCodeBlock(m_codeBlock);
167     
168     emitPutCallResult(instruction);
169 }
170
171 void JIT::compileOpCall(OpcodeID opcodeID, Instruction* instruction, unsigned callLinkInfoIndex)
172 {
173     int callee = instruction[2].u.operand;
174
175     /* Caller always:
176         - Updates callFrameRegister to callee callFrame.
177         - Initializes ArgumentCount; CallerFrame; Callee.
178
179        For a JS call:
180         - Caller initializes ScopeChain.
181         - Callee initializes ReturnPC; CodeBlock.
182         - Callee restores callFrameRegister before return.
183
184        For a non-JS call:
185         - Caller initializes ScopeChain; ReturnPC; CodeBlock.
186         - Caller restores callFrameRegister after return.
187     */
188     COMPILE_ASSERT(OPCODE_LENGTH(op_call) == OPCODE_LENGTH(op_construct), call_and_construct_opcodes_must_be_same_length);
189     COMPILE_ASSERT(OPCODE_LENGTH(op_call) == OPCODE_LENGTH(op_call_varargs), call_and_call_varargs_opcodes_must_be_same_length);
190     COMPILE_ASSERT(OPCODE_LENGTH(op_call) == OPCODE_LENGTH(op_construct_varargs), call_and_construct_varargs_opcodes_must_be_same_length);
191     if (opcodeID == op_call_varargs || opcodeID == op_construct_varargs)
192         compileLoadVarargs(instruction);
193     else {
194         int argCount = instruction[3].u.operand;
195         int registerOffset = -instruction[4].u.operand;
196
197         if (opcodeID == op_call && shouldEmitProfiling()) {
198             emitGetVirtualRegister(registerOffset + CallFrame::argumentOffsetIncludingThis(0), regT0);
199             Jump done = emitJumpIfNotJSCell(regT0);
200             load32(Address(regT0, JSCell::structureIDOffset()), regT0);
201             store32(regT0, instruction[OPCODE_LENGTH(op_call) - 2].u.arrayProfile->addressOfLastSeenStructureID());
202             done.link(this);
203         }
204     
205         addPtr(TrustedImm32(registerOffset * sizeof(Register) + sizeof(CallerFrameAndPC)), callFrameRegister, stackPointerRegister);
206         store32(TrustedImm32(argCount), Address(stackPointerRegister, JSStack::ArgumentCount * static_cast<int>(sizeof(Register)) + PayloadOffset - sizeof(CallerFrameAndPC)));
207     } // SP holds newCallFrame + sizeof(CallerFrameAndPC), with ArgumentCount initialized.
208     
209     uint32_t bytecodeOffset = instruction - m_codeBlock->instructions().begin();
210     uint32_t locationBits = CallFrame::Location::encodeAsBytecodeOffset(bytecodeOffset);
211     store32(TrustedImm32(locationBits), Address(callFrameRegister, JSStack::ArgumentCount * static_cast<int>(sizeof(Register)) + TagOffset));
212     emitGetVirtualRegister(callee, regT0); // regT0 holds callee.
213
214     store64(regT0, Address(stackPointerRegister, JSStack::Callee * static_cast<int>(sizeof(Register)) - sizeof(CallerFrameAndPC)));
215     
216     CallLinkInfo* info = m_codeBlock->addCallLinkInfo();
217
218     if (CallEdgeLog::isEnabled() && shouldEmitProfiling()
219         && Options::baselineDoesCallEdgeProfiling())
220         m_vm->ensureCallEdgeLog().emitLogCode(*this, info->callEdgeProfile, JSValueRegs(regT0));
221
222     if (opcodeID == op_call_eval) {
223         compileCallEval(instruction);
224         return;
225     }
226
227     DataLabelPtr addressOfLinkedFunctionCheck;
228     Jump slowCase = branchPtrWithPatch(NotEqual, regT0, addressOfLinkedFunctionCheck, TrustedImmPtr(0));
229     addSlowCase(slowCase);
230
231     ASSERT(m_callCompilationInfo.size() == callLinkInfoIndex);
232     info->callType = CallLinkInfo::callTypeFor(opcodeID);
233     info->codeOrigin = CodeOrigin(m_bytecodeOffset);
234     info->calleeGPR = regT0;
235     m_callCompilationInfo.append(CallCompilationInfo());
236     m_callCompilationInfo[callLinkInfoIndex].hotPathBegin = addressOfLinkedFunctionCheck;
237     m_callCompilationInfo[callLinkInfoIndex].callLinkInfo = info;
238
239     loadPtr(Address(regT0, OBJECT_OFFSETOF(JSFunction, m_scope)), regT2);
240     store64(regT2, Address(MacroAssembler::stackPointerRegister, JSStack::ScopeChain * sizeof(Register) - sizeof(CallerFrameAndPC)));
241
242     m_callCompilationInfo[callLinkInfoIndex].hotPathOther = emitNakedCall();
243
244     addPtr(TrustedImm32(stackPointerOffsetFor(m_codeBlock) * sizeof(Register)), callFrameRegister, stackPointerRegister);
245     checkStackPointerAlignment();
246
247     sampleCodeBlock(m_codeBlock);
248     
249     emitPutCallResult(instruction);
250 }
251
252 void JIT::compileOpCallSlowCase(OpcodeID opcodeID, Instruction* instruction, Vector<SlowCaseEntry>::iterator& iter, unsigned callLinkInfoIndex)
253 {
254     if (opcodeID == op_call_eval) {
255         compileCallEvalSlowCase(instruction, iter);
256         return;
257     }
258
259     linkSlowCase(iter);
260
261     ThunkGenerator generator = linkThunkGeneratorFor(
262         (opcodeID == op_construct || opcodeID == op_construct_varargs) ? CodeForConstruct : CodeForCall,
263         RegisterPreservationNotRequired);
264     
265     move(TrustedImmPtr(m_callCompilationInfo[callLinkInfoIndex].callLinkInfo), regT2);
266     m_callCompilationInfo[callLinkInfoIndex].callReturnLocation = emitNakedCall(m_vm->getCTIStub(generator).code());
267
268     addPtr(TrustedImm32(stackPointerOffsetFor(m_codeBlock) * sizeof(Register)), callFrameRegister, stackPointerRegister);
269     checkStackPointerAlignment();
270
271     sampleCodeBlock(m_codeBlock);
272     
273     emitPutCallResult(instruction);
274 }
275
276 void JIT::emit_op_call(Instruction* currentInstruction)
277 {
278     compileOpCall(op_call, currentInstruction, m_callLinkInfoIndex++);
279 }
280
281 void JIT::emit_op_call_eval(Instruction* currentInstruction)
282 {
283     compileOpCall(op_call_eval, currentInstruction, m_callLinkInfoIndex);
284 }
285
286 void JIT::emit_op_call_varargs(Instruction* currentInstruction)
287 {
288     compileOpCall(op_call_varargs, currentInstruction, m_callLinkInfoIndex++);
289 }
290     
291 void JIT::emit_op_construct_varargs(Instruction* currentInstruction)
292 {
293     compileOpCall(op_construct_varargs, currentInstruction, m_callLinkInfoIndex++);
294 }
295
296 void JIT::emit_op_construct(Instruction* currentInstruction)
297 {
298     compileOpCall(op_construct, currentInstruction, m_callLinkInfoIndex++);
299 }
300
301 void JIT::emitSlow_op_call(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
302 {
303     compileOpCallSlowCase(op_call, currentInstruction, iter, m_callLinkInfoIndex++);
304 }
305
306 void JIT::emitSlow_op_call_eval(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
307 {
308     compileOpCallSlowCase(op_call_eval, currentInstruction, iter, m_callLinkInfoIndex);
309 }
310  
311 void JIT::emitSlow_op_call_varargs(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
312 {
313     compileOpCallSlowCase(op_call_varargs, currentInstruction, iter, m_callLinkInfoIndex++);
314 }
315     
316 void JIT::emitSlow_op_construct_varargs(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
317 {
318     compileOpCallSlowCase(op_construct_varargs, currentInstruction, iter, m_callLinkInfoIndex++);
319 }
320     
321 void JIT::emitSlow_op_construct(Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
322 {
323     compileOpCallSlowCase(op_construct, currentInstruction, iter, m_callLinkInfoIndex++);
324 }
325
326 } // namespace JSC
327
328 #endif // USE(JSVALUE64)
329 #endif // ENABLE(JIT)