barraclough@apple.com | e367b00 | 2008-12-04 05:43:14 +0000 | [diff] [blame] | 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" |
barraclough@apple.com | e367b00 | 2008-12-04 05:43:14 +0000 | [diff] [blame] | 27 | |
| 28 | #if ENABLE(JIT) |
oliver@apple.com | 0cc25c3 | 2010-10-19 23:55:08 +0000 | [diff] [blame] | 29 | #if USE(JSVALUE64) |
ggaren@apple.com | 7214b5a | 2010-05-21 20:52:30 +0000 | [diff] [blame] | 30 | #include "JIT.h" |
barraclough@apple.com | e367b00 | 2008-12-04 05:43:14 +0000 | [diff] [blame] | 31 | |
ggaren@apple.com | 539d1bb | 2011-11-14 19:21:40 +0000 | [diff] [blame^] | 32 | #include "Arguments.h" |
barraclough@apple.com | e367b00 | 2008-12-04 05:43:14 +0000 | [diff] [blame] | 33 | #include "CodeBlock.h" |
| 34 | #include "JITInlineMethods.h" |
ggaren@apple.com | 0e5741a | 2009-05-12 01:26:52 +0000 | [diff] [blame] | 35 | #include "JITStubCall.h" |
barraclough@apple.com | e367b00 | 2008-12-04 05:43:14 +0000 | [diff] [blame] | 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 | |
barraclough@apple.com | c73ede7 | 2010-05-20 20:12:44 +0000 | [diff] [blame] | 50 | void JIT::emit_op_call_put_result(Instruction* instruction) |
ggaren@apple.com | 2949660 | 2009-05-12 04:20:29 +0000 | [diff] [blame] | 51 | { |
| 52 | int dst = instruction[1].u.operand; |
fpizlo@apple.com | 95a9f0d | 2011-08-20 02:17:49 +0000 | [diff] [blame] | 53 | emitValueProfilingSite(FirstProfilingSite); |
barraclough@apple.com | c73ede7 | 2010-05-20 20:12:44 +0000 | [diff] [blame] | 54 | emitPutVirtualRegister(dst); |
| 55 | } |
| 56 | |
ggaren@apple.com | 539d1bb | 2011-11-14 19:21:40 +0000 | [diff] [blame^] | 57 | void JIT::compileLoadVarargs(Instruction* instruction) |
barraclough@apple.com | c73ede7 | 2010-05-20 20:12:44 +0000 | [diff] [blame] | 58 | { |
ggaren@apple.com | 539d1bb | 2011-11-14 19:21:40 +0000 | [diff] [blame^] | 59 | int thisValue = instruction[2].u.operand; |
| 60 | int arguments = instruction[3].u.operand; |
| 61 | int firstFreeRegister = instruction[4].u.operand; |
ggaren@apple.com | 2949660 | 2009-05-12 04:20:29 +0000 | [diff] [blame] | 62 | |
ggaren@apple.com | 539d1bb | 2011-11-14 19:21:40 +0000 | [diff] [blame^] | 63 | killLastResultRegister(); |
ggaren@apple.com | 2949660 | 2009-05-12 04:20:29 +0000 | [diff] [blame] | 64 | |
ggaren@apple.com | 539d1bb | 2011-11-14 19:21:40 +0000 | [diff] [blame^] | 65 | JumpList slowCase; |
| 66 | JumpList end; |
| 67 | if (m_codeBlock->usesArguments() |
| 68 | && arguments == m_codeBlock->argumentsRegister() |
| 69 | && m_codeBlock->m_numParameters == 1) { |
| 70 | emitGetVirtualRegister(arguments, regT0); |
| 71 | slowCase.append(branchPtr(NotEqual, regT0, TrustedImmPtr(JSValue::encode(JSValue())))); |
barraclough@apple.com | 6f026ee | 2009-08-20 21:49:07 +0000 | [diff] [blame] | 72 | |
ggaren@apple.com | 539d1bb | 2011-11-14 19:21:40 +0000 | [diff] [blame^] | 73 | emitGetFromCallFrameHeader32(RegisterFile::ArgumentCount, regT0); |
| 74 | slowCase.append(branch32(Above, regT0, TrustedImm32(Arguments::MaxArguments + 1))); |
| 75 | // regT0: argumentCountIncludingThis |
| 76 | |
| 77 | move(regT0, regT1); |
| 78 | add32(TrustedImm32(firstFreeRegister + RegisterFile::CallFrameHeaderSize), regT1); |
| 79 | lshift32(TrustedImm32(3), regT1); |
| 80 | addPtr(callFrameRegister, regT1); |
| 81 | // regT1: newCallFrame |
| 82 | |
| 83 | slowCase.append(branchPtr(Below, AbsoluteAddress(m_globalData->interpreter->registerFile().addressOfEnd()), regT1)); |
| 84 | |
| 85 | // Initialize ArgumentCount. |
| 86 | emitFastArithReTagImmediate(regT0, regT2); |
| 87 | storePtr(regT2, Address(regT1, RegisterFile::ArgumentCount * static_cast<int>(sizeof(Register)))); |
| 88 | |
| 89 | // Initialize 'this' and copy arguments. |
| 90 | neg32(regT0); |
| 91 | signExtend32ToPtr(regT0, regT0); |
| 92 | emitGetVirtualRegister(thisValue, regT2); |
| 93 | storePtr(regT2, BaseIndex(regT1, regT0, TimesEight, -(RegisterFile::CallFrameHeaderSize * static_cast<int>(sizeof(Register))))); |
| 94 | end.append(branchAddPtr(Zero, Imm32(1), regT0)); |
| 95 | |
| 96 | Label copyLoop = label(); |
| 97 | loadPtr(BaseIndex(callFrameRegister, regT0, TimesEight, -((RegisterFile::CallFrameHeaderSize + 1) * static_cast<int>(sizeof(Register)))), regT2); |
| 98 | storePtr(regT2, BaseIndex(regT1, regT0, TimesEight, -(RegisterFile::CallFrameHeaderSize * static_cast<int>(sizeof(Register))))); |
| 99 | branchAddPtr(NonZero, Imm32(1), regT0).linkTo(copyLoop, this); |
| 100 | |
| 101 | end.append(jump()); |
| 102 | } |
| 103 | |
| 104 | if (m_codeBlock->m_numParameters == 1) |
| 105 | slowCase.link(this); |
| 106 | |
| 107 | JITStubCall stubCall(this, cti_op_load_varargs); |
| 108 | stubCall.addArgument(thisValue, regT0); |
| 109 | stubCall.addArgument(arguments, regT0); |
| 110 | stubCall.addArgument(Imm32(firstFreeRegister)); |
| 111 | stubCall.call(regT1); |
| 112 | |
| 113 | if (m_codeBlock->m_numParameters == 1) |
| 114 | end.link(this); |
| 115 | } |
| 116 | |
| 117 | void JIT::compileCallEval() |
| 118 | { |
| 119 | JITStubCall stubCall(this, cti_op_call_eval); // Initializes ScopeChain; ReturnPC; CodeBlock. |
| 120 | stubCall.call(); |
| 121 | addSlowCase(branchPtr(Equal, regT0, TrustedImmPtr(JSValue::encode(JSValue())))); |
| 122 | emitGetFromCallFrameHeaderPtr(RegisterFile::CallerFrame, callFrameRegister); |
| 123 | |
| 124 | sampleCodeBlock(m_codeBlock); |
| 125 | } |
| 126 | |
| 127 | void JIT::compileCallEvalSlowCase(Vector<SlowCaseEntry>::iterator& iter) |
| 128 | { |
| 129 | linkSlowCase(iter); |
| 130 | |
| 131 | emitGetFromCallFrameHeaderPtr(RegisterFile::Callee, regT0); |
oliver@apple.com | 815b8e1 | 2010-07-15 04:30:13 +0000 | [diff] [blame] | 132 | emitNakedCall(m_globalData->jitStubs->ctiVirtualCall()); |
ggaren@apple.com | 2949660 | 2009-05-12 04:20:29 +0000 | [diff] [blame] | 133 | |
ggaren@apple.com | 2949660 | 2009-05-12 04:20:29 +0000 | [diff] [blame] | 134 | sampleCodeBlock(m_codeBlock); |
| 135 | } |
| 136 | |
barraclough@apple.com | 249befb | 2008-12-13 03:18:10 +0000 | [diff] [blame] | 137 | void JIT::compileOpCall(OpcodeID opcodeID, Instruction* instruction, unsigned callLinkInfoIndex) |
barraclough@apple.com | e367b00 | 2008-12-04 05:43:14 +0000 | [diff] [blame] | 138 | { |
barraclough@apple.com | c73ede7 | 2010-05-20 20:12:44 +0000 | [diff] [blame] | 139 | int callee = instruction[1].u.operand; |
barraclough@apple.com | e367b00 | 2008-12-04 05:43:14 +0000 | [diff] [blame] | 140 | |
ggaren@apple.com | 539d1bb | 2011-11-14 19:21:40 +0000 | [diff] [blame^] | 141 | /* Caller always: |
| 142 | - Updates callFrameRegister to callee callFrame. |
| 143 | - Initializes ArgumentCount; CallerFrame; Callee. |
| 144 | |
| 145 | For a JS call: |
| 146 | - Caller initializes ScopeChain. |
| 147 | - Callee initializes ReturnPC; CodeBlock. |
| 148 | - Callee restores callFrameRegister before return. |
| 149 | |
| 150 | For a non-JS call: |
| 151 | - Caller initializes ScopeChain; ReturnPC; CodeBlock. |
| 152 | - Caller restores callFrameRegister after return. |
| 153 | */ |
| 154 | |
| 155 | if (opcodeID == op_call_varargs) |
| 156 | compileLoadVarargs(instruction); |
| 157 | else { |
| 158 | int argCount = instruction[2].u.operand; |
| 159 | int registerOffset = instruction[3].u.operand; |
| 160 | |
| 161 | addPtr(TrustedImm32(registerOffset * sizeof(Register)), callFrameRegister, regT1); |
| 162 | storePtr(TrustedImmPtr(JSValue::encode(jsNumber(argCount))), Address(regT1, RegisterFile::ArgumentCount * static_cast<int>(sizeof(Register)))); |
| 163 | } // regT1 holds newCallFrame with ArgumentCount initialized. |
| 164 | emitGetVirtualRegister(callee, regT0); // regT0 holds callee. |
| 165 | |
| 166 | storePtr(callFrameRegister, Address(regT1, RegisterFile::CallerFrame * static_cast<int>(sizeof(Register)))); |
| 167 | storePtr(regT0, Address(regT1, RegisterFile::Callee * static_cast<int>(sizeof(Register)))); |
| 168 | move(regT1, callFrameRegister); |
| 169 | |
barraclough@apple.com | e367b00 | 2008-12-04 05:43:14 +0000 | [diff] [blame] | 170 | if (opcodeID == op_call_eval) { |
ggaren@apple.com | 539d1bb | 2011-11-14 19:21:40 +0000 | [diff] [blame^] | 171 | compileCallEval(); |
| 172 | return; |
barraclough@apple.com | e367b00 | 2008-12-04 05:43:14 +0000 | [diff] [blame] | 173 | } |
| 174 | |
barraclough@apple.com | 289318a | 2008-12-22 01:00:07 +0000 | [diff] [blame] | 175 | DataLabelPtr addressOfLinkedFunctionCheck; |
barraclough@apple.com | 970af2c | 2009-08-13 05:58:36 +0000 | [diff] [blame] | 176 | BEGIN_UNINTERRUPTED_SEQUENCE(sequenceOpCall); |
ggaren@apple.com | 539d1bb | 2011-11-14 19:21:40 +0000 | [diff] [blame^] | 177 | Jump slowCase = branchPtrWithPatch(NotEqual, regT0, addressOfLinkedFunctionCheck, TrustedImmPtr(JSValue::encode(JSValue()))); |
barraclough@apple.com | 970af2c | 2009-08-13 05:58:36 +0000 | [diff] [blame] | 178 | END_UNINTERRUPTED_SEQUENCE(sequenceOpCall); |
ggaren@apple.com | 539d1bb | 2011-11-14 19:21:40 +0000 | [diff] [blame^] | 179 | addSlowCase(slowCase); |
barraclough@apple.com | 970af2c | 2009-08-13 05:58:36 +0000 | [diff] [blame] | 180 | |
ggaren@apple.com | 539d1bb | 2011-11-14 19:21:40 +0000 | [diff] [blame^] | 181 | ASSERT_JIT_OFFSET(differenceBetween(addressOfLinkedFunctionCheck, slowCase), patchOffsetOpCallCompareToJump); |
commit-queue@webkit.org | 4ea4892 | 2011-07-06 00:56:49 +0000 | [diff] [blame] | 182 | ASSERT(m_callStructureStubCompilationInfo.size() == callLinkInfoIndex); |
| 183 | m_callStructureStubCompilationInfo.append(StructureStubCompilationInfo()); |
barraclough@apple.com | e367b00 | 2008-12-04 05:43:14 +0000 | [diff] [blame] | 184 | m_callStructureStubCompilationInfo[callLinkInfoIndex].hotPathBegin = addressOfLinkedFunctionCheck; |
ggaren@apple.com | 539d1bb | 2011-11-14 19:21:40 +0000 | [diff] [blame^] | 185 | m_callStructureStubCompilationInfo[callLinkInfoIndex].callType = CallLinkInfo::callTypeFor(opcodeID); |
oliver@apple.com | 1386ec9 | 2011-10-07 18:39:45 +0000 | [diff] [blame] | 186 | m_callStructureStubCompilationInfo[callLinkInfoIndex].bytecodeIndex = m_bytecodeOffset; |
barraclough@apple.com | e367b00 | 2008-12-04 05:43:14 +0000 | [diff] [blame] | 187 | |
ggaren@apple.com | 539d1bb | 2011-11-14 19:21:40 +0000 | [diff] [blame^] | 188 | loadPtr(Address(regT0, OBJECT_OFFSETOF(JSFunction, m_scopeChain)), regT1); |
| 189 | emitPutToCallFrameHeader(regT1, RegisterFile::ScopeChain); |
barraclough@apple.com | 97bacef | 2009-06-05 07:55:38 +0000 | [diff] [blame] | 190 | m_callStructureStubCompilationInfo[callLinkInfoIndex].hotPathOther = emitNakedCall(); |
barraclough@apple.com | e367b00 | 2008-12-04 05:43:14 +0000 | [diff] [blame] | 191 | |
barraclough@apple.com | 24a7abb | 2009-01-16 23:34:46 +0000 | [diff] [blame] | 192 | sampleCodeBlock(m_codeBlock); |
barraclough@apple.com | e367b00 | 2008-12-04 05:43:14 +0000 | [diff] [blame] | 193 | } |
| 194 | |
ggaren@apple.com | 539d1bb | 2011-11-14 19:21:40 +0000 | [diff] [blame^] | 195 | void JIT::compileOpCallSlowCase(OpcodeID opcodeID, Instruction*, Vector<SlowCaseEntry>::iterator& iter, unsigned callLinkInfoIndex) |
barraclough@apple.com | e367b00 | 2008-12-04 05:43:14 +0000 | [diff] [blame] | 196 | { |
ggaren@apple.com | 539d1bb | 2011-11-14 19:21:40 +0000 | [diff] [blame^] | 197 | if (opcodeID == op_call_eval) { |
| 198 | compileCallEvalSlowCase(iter); |
| 199 | return; |
| 200 | } |
barraclough@apple.com | e367b00 | 2008-12-04 05:43:14 +0000 | [diff] [blame] | 201 | |
barraclough@apple.com | 4f46a50 | 2008-12-13 01:39:38 +0000 | [diff] [blame] | 202 | linkSlowCase(iter); |
ggaren@apple.com | 539d1bb | 2011-11-14 19:21:40 +0000 | [diff] [blame^] | 203 | |
oliver@apple.com | 815b8e1 | 2010-07-15 04:30:13 +0000 | [diff] [blame] | 204 | m_callStructureStubCompilationInfo[callLinkInfoIndex].callReturnLocation = emitNakedCall(opcodeID == op_construct ? m_globalData->jitStubs->ctiVirtualConstructLink() : m_globalData->jitStubs->ctiVirtualCallLink()); |
barraclough@apple.com | e367b00 | 2008-12-04 05:43:14 +0000 | [diff] [blame] | 205 | |
barraclough@apple.com | 24a7abb | 2009-01-16 23:34:46 +0000 | [diff] [blame] | 206 | sampleCodeBlock(m_codeBlock); |
barraclough@apple.com | e367b00 | 2008-12-04 05:43:14 +0000 | [diff] [blame] | 207 | } |
| 208 | |
barraclough@apple.com | e367b00 | 2008-12-04 05:43:14 +0000 | [diff] [blame] | 209 | } // namespace JSC |
| 210 | |
oliver@apple.com | 0cc25c3 | 2010-10-19 23:55:08 +0000 | [diff] [blame] | 211 | #endif // USE(JSVALUE64) |
barraclough@apple.com | e367b00 | 2008-12-04 05:43:14 +0000 | [diff] [blame] | 212 | #endif // ENABLE(JIT) |