upload tizen1.0 source
[profile/ivi/webkit-efl.git] / Source / JavaScriptCore / jit / JITCall.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(JSVALUE64)
30 #include "JIT.h"
31
32 #include "Arguments.h"
33 #include "CodeBlock.h"
34 #include "JITInlineMethods.h"
35 #include "JITStubCall.h"
36 #include "JSArray.h"
37 #include "JSFunction.h"
38 #include "Interpreter.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(FirstProfilingSite);
54     emitPutVirtualRegister(dst);
55 }
56
57 void JIT::compileLoadVarargs(Instruction* instruction)
58 {
59     int thisValue = instruction[2].u.operand;
60     int arguments = instruction[3].u.operand;
61     int firstFreeRegister = instruction[4].u.operand;
62
63     killLastResultRegister();
64
65     JumpList slowCase;
66     JumpList end;
67     if (m_codeBlock->usesArguments() && arguments == m_codeBlock->argumentsRegister()) {
68         emitGetVirtualRegister(arguments, regT0);
69         slowCase.append(branchPtr(NotEqual, regT0, TrustedImmPtr(JSValue::encode(JSValue()))));
70
71         emitGetFromCallFrameHeader32(RegisterFile::ArgumentCount, regT0);
72         slowCase.append(branch32(Above, regT0, TrustedImm32(Arguments::MaxArguments + 1)));
73         // regT0: argumentCountIncludingThis
74
75         move(regT0, regT1);
76         add32(TrustedImm32(firstFreeRegister + RegisterFile::CallFrameHeaderSize), regT1);
77         lshift32(TrustedImm32(3), regT1);
78         addPtr(callFrameRegister, regT1);
79         // regT1: newCallFrame
80
81         slowCase.append(branchPtr(Below, AbsoluteAddress(m_globalData->interpreter->registerFile().addressOfEnd()), regT1));
82
83         // Initialize ArgumentCount.
84         emitFastArithReTagImmediate(regT0, regT2);
85         storePtr(regT2, Address(regT1, RegisterFile::ArgumentCount * static_cast<int>(sizeof(Register))));
86
87         // Initialize 'this'.
88         emitGetVirtualRegister(thisValue, regT2);
89         storePtr(regT2, Address(regT1, CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register))));
90
91         // Copy arguments.
92         neg32(regT0);
93         signExtend32ToPtr(regT0, regT0);
94         end.append(branchAddPtr(Zero, Imm32(1), regT0));
95         // regT0: -argumentCount
96
97         Label copyLoop = label();
98         loadPtr(BaseIndex(callFrameRegister, regT0, TimesEight, CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register))), regT2);
99         storePtr(regT2, BaseIndex(regT1, regT0, TimesEight, CallFrame::thisArgumentOffset() * static_cast<int>(sizeof(Register))));
100         branchAddPtr(NonZero, Imm32(1), regT0).linkTo(copyLoop, this);
101
102         end.append(jump());
103     }
104
105     if (m_codeBlock->usesArguments() && arguments == m_codeBlock->argumentsRegister())
106         slowCase.link(this);
107
108     JITStubCall stubCall(this, cti_op_load_varargs);
109     stubCall.addArgument(thisValue, regT0);
110     stubCall.addArgument(arguments, regT0);
111     stubCall.addArgument(Imm32(firstFreeRegister));
112     stubCall.call(regT1);
113
114     if (m_codeBlock->usesArguments() && arguments == m_codeBlock->argumentsRegister())
115         end.link(this);
116 }
117
118 void JIT::compileCallEval()
119 {
120     JITStubCall stubCall(this, cti_op_call_eval); // Initializes ScopeChain; ReturnPC; CodeBlock.
121     stubCall.call();
122     addSlowCase(branchPtr(Equal, regT0, TrustedImmPtr(JSValue::encode(JSValue()))));
123     emitGetFromCallFrameHeaderPtr(RegisterFile::CallerFrame, callFrameRegister);
124
125     sampleCodeBlock(m_codeBlock);
126 }
127
128 void JIT::compileCallEvalSlowCase(Vector<SlowCaseEntry>::iterator& iter)
129 {
130     linkSlowCase(iter);
131
132     emitGetFromCallFrameHeaderPtr(RegisterFile::Callee, regT0);
133     emitNakedCall(m_globalData->jitStubs->ctiVirtualCall());
134
135     sampleCodeBlock(m_codeBlock);
136 }
137
138 void JIT::compileOpCall(OpcodeID opcodeID, Instruction* instruction, unsigned callLinkInfoIndex)
139 {
140     int callee = instruction[1].u.operand;
141
142     /* Caller always:
143         - Updates callFrameRegister to callee callFrame.
144         - Initializes ArgumentCount; CallerFrame; Callee.
145
146        For a JS call:
147         - Caller initializes ScopeChain.
148         - Callee initializes ReturnPC; CodeBlock.
149         - Callee restores callFrameRegister before return.
150
151        For a non-JS call:
152         - Caller initializes ScopeChain; ReturnPC; CodeBlock.
153         - Caller restores callFrameRegister after return.
154     */
155
156     if (opcodeID == op_call_varargs)
157         compileLoadVarargs(instruction);
158     else {
159         int argCount = instruction[2].u.operand;
160         int registerOffset = instruction[3].u.operand;
161
162         addPtr(TrustedImm32(registerOffset * sizeof(Register)), callFrameRegister, regT1);
163         store32(TrustedImm32(argCount), Address(regT1, RegisterFile::ArgumentCount * static_cast<int>(sizeof(Register))));
164     } // regT1 holds newCallFrame with ArgumentCount initialized.
165     emitGetVirtualRegister(callee, regT0); // regT0 holds callee.
166
167     storePtr(callFrameRegister, Address(regT1, RegisterFile::CallerFrame * static_cast<int>(sizeof(Register))));
168     storePtr(regT0, Address(regT1, RegisterFile::Callee * static_cast<int>(sizeof(Register))));
169     move(regT1, callFrameRegister);
170
171     if (opcodeID == op_call_eval) {
172         compileCallEval();
173         return;
174     }
175
176     DataLabelPtr addressOfLinkedFunctionCheck;
177     BEGIN_UNINTERRUPTED_SEQUENCE(sequenceOpCall);
178     Jump slowCase = branchPtrWithPatch(NotEqual, regT0, addressOfLinkedFunctionCheck, TrustedImmPtr(JSValue::encode(JSValue())));
179     END_UNINTERRUPTED_SEQUENCE(sequenceOpCall);
180     addSlowCase(slowCase);
181
182     ASSERT_JIT_OFFSET(differenceBetween(addressOfLinkedFunctionCheck, slowCase), patchOffsetOpCallCompareToJump);
183     ASSERT(m_callStructureStubCompilationInfo.size() == callLinkInfoIndex);
184     m_callStructureStubCompilationInfo.append(StructureStubCompilationInfo());
185     m_callStructureStubCompilationInfo[callLinkInfoIndex].hotPathBegin = addressOfLinkedFunctionCheck;
186     m_callStructureStubCompilationInfo[callLinkInfoIndex].callType = CallLinkInfo::callTypeFor(opcodeID);
187     m_callStructureStubCompilationInfo[callLinkInfoIndex].bytecodeIndex = m_bytecodeOffset;
188
189     loadPtr(Address(regT0, OBJECT_OFFSETOF(JSFunction, m_scopeChain)), regT1);
190     emitPutToCallFrameHeader(regT1, RegisterFile::ScopeChain);
191     m_callStructureStubCompilationInfo[callLinkInfoIndex].hotPathOther = emitNakedCall();
192
193     sampleCodeBlock(m_codeBlock);
194 }
195
196 void JIT::compileOpCallSlowCase(OpcodeID opcodeID, Instruction*, Vector<SlowCaseEntry>::iterator& iter, unsigned callLinkInfoIndex)
197 {
198     if (opcodeID == op_call_eval) {
199         compileCallEvalSlowCase(iter);
200         return;
201     }
202
203     linkSlowCase(iter);
204     
205     m_callStructureStubCompilationInfo[callLinkInfoIndex].callReturnLocation = emitNakedCall(opcodeID == op_construct ? m_globalData->jitStubs->ctiVirtualConstructLink() : m_globalData->jitStubs->ctiVirtualCallLink());
206
207     sampleCodeBlock(m_codeBlock);
208 }
209
210 } // namespace JSC
211
212 #endif // USE(JSVALUE64)
213 #endif // ENABLE(JIT)