Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "code_generator_x86.h" |
| 18 | #include "utils/assembler.h" |
| 19 | #include "utils/x86/assembler_x86.h" |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 20 | #include "utils/x86/managed_register_x86.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 21 | |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 22 | #include "entrypoints/quick/quick_entrypoints.h" |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 23 | #include "mirror/array.h" |
| 24 | #include "mirror/art_method.h" |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 25 | #include "thread.h" |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 26 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 27 | #define __ reinterpret_cast<X86Assembler*>(GetAssembler())-> |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 28 | |
| 29 | namespace art { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 30 | |
| 31 | x86::X86ManagedRegister Location::AsX86() const { |
| 32 | return reg().AsX86(); |
| 33 | } |
| 34 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 35 | namespace x86 { |
| 36 | |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 37 | static constexpr int kNumberOfPushedRegistersAtEntry = 1; |
| 38 | static constexpr int kCurrentMethodStackOffset = 0; |
| 39 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 40 | void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const { |
| 41 | stream << X86ManagedRegister::FromCpuRegister(Register(reg)); |
| 42 | } |
| 43 | |
| 44 | void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const { |
| 45 | stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg)); |
| 46 | } |
| 47 | |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 48 | CodeGeneratorX86::CodeGeneratorX86(HGraph* graph) |
| 49 | : CodeGenerator(graph, kNumberOfRegIds), |
| 50 | location_builder_(graph, this), |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 51 | instruction_visitor_(graph, this), |
| 52 | move_resolver_(graph->GetArena(), this) {} |
| 53 | |
| 54 | void CodeGeneratorX86::ComputeFrameSize(size_t number_of_spill_slots) { |
| 55 | SetFrameSize(RoundUp( |
| 56 | number_of_spill_slots * kVRegSize |
| 57 | + kVRegSize // Art method |
| 58 | + kNumberOfPushedRegistersAtEntry * kX86WordSize, |
| 59 | kStackAlignment)); |
| 60 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 61 | |
| 62 | static bool* GetBlockedRegisterPairs(bool* blocked_registers) { |
| 63 | return blocked_registers + kNumberOfAllocIds; |
| 64 | } |
| 65 | |
| 66 | ManagedRegister CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type, |
| 67 | bool* blocked_registers) const { |
| 68 | switch (type) { |
| 69 | case Primitive::kPrimLong: { |
| 70 | size_t reg = AllocateFreeRegisterInternal( |
| 71 | GetBlockedRegisterPairs(blocked_registers), kNumberOfRegisterPairs); |
| 72 | X86ManagedRegister pair = |
| 73 | X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg)); |
| 74 | blocked_registers[pair.AsRegisterPairLow()] = true; |
| 75 | blocked_registers[pair.AsRegisterPairHigh()] = true; |
| 76 | return pair; |
| 77 | } |
| 78 | |
| 79 | case Primitive::kPrimByte: |
| 80 | case Primitive::kPrimBoolean: |
| 81 | case Primitive::kPrimChar: |
| 82 | case Primitive::kPrimShort: |
| 83 | case Primitive::kPrimInt: |
| 84 | case Primitive::kPrimNot: { |
| 85 | size_t reg = AllocateFreeRegisterInternal(blocked_registers, kNumberOfCpuRegisters); |
| 86 | return X86ManagedRegister::FromCpuRegister(static_cast<Register>(reg)); |
| 87 | } |
| 88 | |
| 89 | case Primitive::kPrimFloat: |
| 90 | case Primitive::kPrimDouble: |
| 91 | LOG(FATAL) << "Unimplemented register type " << type; |
| 92 | |
| 93 | case Primitive::kPrimVoid: |
| 94 | LOG(FATAL) << "Unreachable type " << type; |
| 95 | } |
| 96 | |
| 97 | return ManagedRegister::NoRegister(); |
| 98 | } |
| 99 | |
| 100 | void CodeGeneratorX86::SetupBlockedRegisters(bool* blocked_registers) const { |
| 101 | bool* blocked_register_pairs = GetBlockedRegisterPairs(blocked_registers); |
| 102 | |
| 103 | // Don't allocate the dalvik style register pair passing. |
| 104 | blocked_register_pairs[ECX_EDX] = true; |
| 105 | |
| 106 | // Stack register is always reserved. |
| 107 | blocked_registers[ESP] = true; |
| 108 | |
| 109 | // TODO: We currently don't use Quick's callee saved registers. |
| 110 | blocked_registers[EBP] = true; |
| 111 | blocked_registers[ESI] = true; |
| 112 | blocked_registers[EDI] = true; |
| 113 | blocked_register_pairs[EAX_EDI] = true; |
| 114 | blocked_register_pairs[EDX_EDI] = true; |
| 115 | blocked_register_pairs[ECX_EDI] = true; |
| 116 | blocked_register_pairs[EBX_EDI] = true; |
| 117 | } |
| 118 | |
| 119 | size_t CodeGeneratorX86::GetNumberOfRegisters() const { |
| 120 | return kNumberOfRegIds; |
| 121 | } |
| 122 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 123 | static Location X86CpuLocation(Register reg) { |
| 124 | return Location::RegisterLocation(X86ManagedRegister::FromCpuRegister(reg)); |
| 125 | } |
| 126 | |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 127 | InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen) |
| 128 | : HGraphVisitor(graph), |
| 129 | assembler_(codegen->GetAssembler()), |
| 130 | codegen_(codegen) {} |
| 131 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 132 | void CodeGeneratorX86::GenerateFrameEntry() { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 133 | // Create a fake register to mimic Quick. |
| 134 | static const int kFakeReturnRegister = 8; |
| 135 | core_spill_mask_ |= (1 << kFakeReturnRegister); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 136 | |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 137 | // The return PC has already been pushed on the stack. |
Nicolas Geoffray | 707c809 | 2014-04-04 10:50:14 +0100 | [diff] [blame] | 138 | __ subl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize)); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 139 | __ movl(Address(ESP, kCurrentMethodStackOffset), EAX); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void CodeGeneratorX86::GenerateFrameExit() { |
Nicolas Geoffray | 707c809 | 2014-04-04 10:50:14 +0100 | [diff] [blame] | 143 | __ addl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize)); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void CodeGeneratorX86::Bind(Label* label) { |
| 147 | __ Bind(label); |
| 148 | } |
| 149 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 150 | void InstructionCodeGeneratorX86::LoadCurrentMethod(Register reg) { |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 151 | __ movl(reg, Address(ESP, kCurrentMethodStackOffset)); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 154 | int32_t CodeGeneratorX86::GetStackSlot(HLocal* local) const { |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 155 | uint16_t reg_number = local->GetRegNumber(); |
| 156 | uint16_t number_of_vregs = GetGraph()->GetNumberOfVRegs(); |
| 157 | uint16_t number_of_in_vregs = GetGraph()->GetNumberOfInVRegs(); |
| 158 | if (reg_number >= number_of_vregs - number_of_in_vregs) { |
| 159 | // Local is a parameter of the method. It is stored in the caller's frame. |
| 160 | return GetFrameSize() + kX86WordSize // ART method |
Nicolas Geoffray | a747a39 | 2014-04-17 14:56:23 +0100 | [diff] [blame] | 161 | + (reg_number - number_of_vregs + number_of_in_vregs) * kVRegSize; |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 162 | } else { |
| 163 | // Local is a temporary in this method. It is stored in this method's frame. |
| 164 | return GetFrameSize() - (kNumberOfPushedRegistersAtEntry * kX86WordSize) |
Nicolas Geoffray | a747a39 | 2014-04-17 14:56:23 +0100 | [diff] [blame] | 165 | - kVRegSize // filler. |
| 166 | - (number_of_vregs * kVRegSize) |
| 167 | + (reg_number * kVRegSize); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 168 | } |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 171 | |
| 172 | Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const { |
| 173 | switch (load->GetType()) { |
| 174 | case Primitive::kPrimLong: |
| 175 | return Location::DoubleStackSlot(GetStackSlot(load->GetLocal())); |
| 176 | break; |
| 177 | |
| 178 | case Primitive::kPrimInt: |
| 179 | case Primitive::kPrimNot: |
| 180 | return Location::StackSlot(GetStackSlot(load->GetLocal())); |
| 181 | |
| 182 | case Primitive::kPrimFloat: |
| 183 | case Primitive::kPrimDouble: |
| 184 | LOG(FATAL) << "Unimplemented type " << load->GetType(); |
| 185 | |
| 186 | case Primitive::kPrimBoolean: |
| 187 | case Primitive::kPrimByte: |
| 188 | case Primitive::kPrimChar: |
| 189 | case Primitive::kPrimShort: |
| 190 | case Primitive::kPrimVoid: |
| 191 | LOG(FATAL) << "Unexpected type " << load->GetType(); |
| 192 | } |
| 193 | |
| 194 | LOG(FATAL) << "Unreachable"; |
| 195 | return Location(); |
| 196 | } |
| 197 | |
Nicolas Geoffray | db928fc | 2014-04-16 17:38:32 +0100 | [diff] [blame] | 198 | static constexpr Register kRuntimeParameterCoreRegisters[] = { EAX, ECX, EDX }; |
| 199 | static constexpr size_t kRuntimeParameterCoreRegistersLength = |
| 200 | arraysize(kRuntimeParameterCoreRegisters); |
| 201 | |
| 202 | class InvokeRuntimeCallingConvention : public CallingConvention<Register> { |
| 203 | public: |
| 204 | InvokeRuntimeCallingConvention() |
| 205 | : CallingConvention(kRuntimeParameterCoreRegisters, |
| 206 | kRuntimeParameterCoreRegistersLength) {} |
| 207 | |
| 208 | private: |
| 209 | DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention); |
| 210 | }; |
| 211 | |
Nicolas Geoffray | a747a39 | 2014-04-17 14:56:23 +0100 | [diff] [blame] | 212 | Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) { |
| 213 | switch (type) { |
| 214 | case Primitive::kPrimBoolean: |
| 215 | case Primitive::kPrimByte: |
| 216 | case Primitive::kPrimChar: |
| 217 | case Primitive::kPrimShort: |
| 218 | case Primitive::kPrimInt: |
| 219 | case Primitive::kPrimNot: { |
| 220 | uint32_t index = gp_index_++; |
| 221 | if (index < calling_convention.GetNumberOfRegisters()) { |
| 222 | return X86CpuLocation(calling_convention.GetRegisterAt(index)); |
| 223 | } else { |
| 224 | return Location::StackSlot(calling_convention.GetStackOffsetOf(index, kX86WordSize)); |
Nicolas Geoffray | db928fc | 2014-04-16 17:38:32 +0100 | [diff] [blame] | 225 | } |
Nicolas Geoffray | db928fc | 2014-04-16 17:38:32 +0100 | [diff] [blame] | 226 | } |
Nicolas Geoffray | a747a39 | 2014-04-17 14:56:23 +0100 | [diff] [blame] | 227 | |
| 228 | case Primitive::kPrimLong: { |
| 229 | uint32_t index = gp_index_; |
| 230 | gp_index_ += 2; |
| 231 | if (index + 1 < calling_convention.GetNumberOfRegisters()) { |
| 232 | return Location::RegisterLocation(X86ManagedRegister::FromRegisterPair( |
| 233 | calling_convention.GetRegisterPairAt(index))); |
| 234 | } else if (index + 1 == calling_convention.GetNumberOfRegisters()) { |
| 235 | return Location::QuickParameter(index); |
| 236 | } else { |
| 237 | return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index, kX86WordSize)); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | case Primitive::kPrimDouble: |
| 242 | case Primitive::kPrimFloat: |
| 243 | LOG(FATAL) << "Unimplemented parameter type " << type; |
| 244 | break; |
| 245 | |
| 246 | case Primitive::kPrimVoid: |
| 247 | LOG(FATAL) << "Unexpected parameter type " << type; |
| 248 | break; |
Nicolas Geoffray | db928fc | 2014-04-16 17:38:32 +0100 | [diff] [blame] | 249 | } |
Nicolas Geoffray | a747a39 | 2014-04-17 14:56:23 +0100 | [diff] [blame] | 250 | return Location(); |
| 251 | } |
Nicolas Geoffray | db928fc | 2014-04-16 17:38:32 +0100 | [diff] [blame] | 252 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 253 | void CodeGeneratorX86::Move32(Location destination, Location source) { |
| 254 | if (source.Equals(destination)) { |
| 255 | return; |
| 256 | } |
| 257 | if (destination.IsRegister()) { |
| 258 | if (source.IsRegister()) { |
| 259 | __ movl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister()); |
| 260 | } else { |
| 261 | DCHECK(source.IsStackSlot()); |
| 262 | __ movl(destination.AsX86().AsCpuRegister(), Address(ESP, source.GetStackIndex())); |
| 263 | } |
| 264 | } else { |
| 265 | if (source.IsRegister()) { |
| 266 | __ movl(Address(ESP, destination.GetStackIndex()), source.AsX86().AsCpuRegister()); |
| 267 | } else { |
| 268 | DCHECK(source.IsStackSlot()); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 269 | __ pushl(Address(ESP, source.GetStackIndex())); |
| 270 | __ popl(Address(ESP, destination.GetStackIndex())); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | void CodeGeneratorX86::Move64(Location destination, Location source) { |
| 276 | if (source.Equals(destination)) { |
| 277 | return; |
| 278 | } |
| 279 | if (destination.IsRegister()) { |
| 280 | if (source.IsRegister()) { |
| 281 | __ movl(destination.AsX86().AsRegisterPairLow(), source.AsX86().AsRegisterPairLow()); |
| 282 | __ movl(destination.AsX86().AsRegisterPairHigh(), source.AsX86().AsRegisterPairHigh()); |
| 283 | } else if (source.IsQuickParameter()) { |
| 284 | uint32_t argument_index = source.GetQuickParameterIndex(); |
| 285 | InvokeDexCallingConvention calling_convention; |
| 286 | __ movl(destination.AsX86().AsRegisterPairLow(), |
| 287 | calling_convention.GetRegisterAt(argument_index)); |
Nicolas Geoffray | a747a39 | 2014-04-17 14:56:23 +0100 | [diff] [blame] | 288 | __ movl(destination.AsX86().AsRegisterPairHigh(), Address(ESP, |
| 289 | calling_convention.GetStackOffsetOf(argument_index + 1, kX86WordSize) + GetFrameSize())); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 290 | } else { |
| 291 | DCHECK(source.IsDoubleStackSlot()); |
| 292 | __ movl(destination.AsX86().AsRegisterPairLow(), Address(ESP, source.GetStackIndex())); |
| 293 | __ movl(destination.AsX86().AsRegisterPairHigh(), |
| 294 | Address(ESP, source.GetHighStackIndex(kX86WordSize))); |
| 295 | } |
| 296 | } else if (destination.IsQuickParameter()) { |
| 297 | InvokeDexCallingConvention calling_convention; |
| 298 | uint32_t argument_index = destination.GetQuickParameterIndex(); |
| 299 | if (source.IsRegister()) { |
| 300 | __ movl(calling_convention.GetRegisterAt(argument_index), source.AsX86().AsRegisterPairLow()); |
Nicolas Geoffray | a747a39 | 2014-04-17 14:56:23 +0100 | [diff] [blame] | 301 | __ movl(Address(ESP, calling_convention.GetStackOffsetOf(argument_index + 1, kX86WordSize)), |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 302 | source.AsX86().AsRegisterPairHigh()); |
| 303 | } else { |
| 304 | DCHECK(source.IsDoubleStackSlot()); |
| 305 | __ movl(calling_convention.GetRegisterAt(argument_index), |
| 306 | Address(ESP, source.GetStackIndex())); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 307 | __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize))); |
| 308 | __ popl(Address(ESP, calling_convention.GetStackOffsetOf(argument_index + 1, kX86WordSize))); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 309 | } |
| 310 | } else { |
| 311 | if (source.IsRegister()) { |
| 312 | __ movl(Address(ESP, destination.GetStackIndex()), source.AsX86().AsRegisterPairLow()); |
| 313 | __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), |
| 314 | source.AsX86().AsRegisterPairHigh()); |
| 315 | } else if (source.IsQuickParameter()) { |
| 316 | InvokeDexCallingConvention calling_convention; |
| 317 | uint32_t argument_index = source.GetQuickParameterIndex(); |
| 318 | __ movl(Address(ESP, destination.GetStackIndex()), |
| 319 | calling_convention.GetRegisterAt(argument_index)); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 320 | __ pushl(Address(ESP, |
Nicolas Geoffray | a747a39 | 2014-04-17 14:56:23 +0100 | [diff] [blame] | 321 | calling_convention.GetStackOffsetOf(argument_index + 1, kX86WordSize) + GetFrameSize())); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 322 | __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize))); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 323 | } else { |
| 324 | DCHECK(source.IsDoubleStackSlot()); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 325 | __ pushl(Address(ESP, source.GetStackIndex())); |
| 326 | __ popl(Address(ESP, destination.GetStackIndex())); |
| 327 | __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize))); |
| 328 | __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize))); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 333 | void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) { |
| 334 | if (instruction->AsIntConstant() != nullptr) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 335 | Immediate imm(instruction->AsIntConstant()->GetValue()); |
| 336 | if (location.IsRegister()) { |
| 337 | __ movl(location.AsX86().AsCpuRegister(), imm); |
| 338 | } else { |
| 339 | __ movl(Address(ESP, location.GetStackIndex()), imm); |
| 340 | } |
| 341 | } else if (instruction->AsLongConstant() != nullptr) { |
| 342 | int64_t value = instruction->AsLongConstant()->GetValue(); |
| 343 | if (location.IsRegister()) { |
| 344 | __ movl(location.AsX86().AsRegisterPairLow(), Immediate(Low32Bits(value))); |
| 345 | __ movl(location.AsX86().AsRegisterPairHigh(), Immediate(High32Bits(value))); |
| 346 | } else { |
| 347 | __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value))); |
| 348 | __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value))); |
| 349 | } |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 350 | } else if (instruction->AsLoadLocal() != nullptr) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 351 | switch (instruction->GetType()) { |
| 352 | case Primitive::kPrimBoolean: |
| 353 | case Primitive::kPrimByte: |
| 354 | case Primitive::kPrimChar: |
| 355 | case Primitive::kPrimShort: |
| 356 | case Primitive::kPrimInt: |
| 357 | case Primitive::kPrimNot: |
| 358 | Move32(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal()))); |
| 359 | break; |
| 360 | |
| 361 | case Primitive::kPrimLong: |
| 362 | Move64(location, Location::DoubleStackSlot( |
| 363 | GetStackSlot(instruction->AsLoadLocal()->GetLocal()))); |
| 364 | break; |
| 365 | |
| 366 | default: |
| 367 | LOG(FATAL) << "Unimplemented local type " << instruction->GetType(); |
| 368 | } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 369 | } else { |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 370 | // This can currently only happen when the instruction that requests the move |
| 371 | // is the next to be compiled. |
| 372 | DCHECK_EQ(instruction->GetNext(), move_for); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 373 | switch (instruction->GetType()) { |
| 374 | case Primitive::kPrimBoolean: |
| 375 | case Primitive::kPrimByte: |
| 376 | case Primitive::kPrimChar: |
| 377 | case Primitive::kPrimShort: |
| 378 | case Primitive::kPrimInt: |
| 379 | case Primitive::kPrimNot: |
| 380 | Move32(location, instruction->GetLocations()->Out()); |
| 381 | break; |
| 382 | |
| 383 | case Primitive::kPrimLong: |
| 384 | Move64(location, instruction->GetLocations()->Out()); |
| 385 | break; |
| 386 | |
| 387 | default: |
| 388 | LOG(FATAL) << "Unimplemented type " << instruction->GetType(); |
| 389 | } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
| 393 | void LocationsBuilderX86::VisitGoto(HGoto* got) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 394 | got->SetLocations(nullptr); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 397 | void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 398 | HBasicBlock* successor = got->GetSuccessor(); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 399 | if (GetGraph()->GetExitBlock() == successor) { |
| 400 | codegen_->GenerateFrameExit(); |
| 401 | } else if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) { |
| 402 | __ jmp(codegen_->GetLabelOf(successor)); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 406 | void LocationsBuilderX86::VisitExit(HExit* exit) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 407 | exit->SetLocations(nullptr); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 410 | void InstructionCodeGeneratorX86::VisitExit(HExit* exit) { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 411 | if (kIsDebugBuild) { |
| 412 | __ Comment("Unreachable"); |
| 413 | __ int3(); |
| 414 | } |
| 415 | } |
| 416 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 417 | void LocationsBuilderX86::VisitIf(HIf* if_instr) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 418 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 419 | locations->SetInAt(0, Location::Any()); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 420 | if_instr->SetLocations(locations); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 423 | void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 424 | // TODO: Generate the input as a condition, instead of materializing in a register. |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 425 | Location location = if_instr->GetLocations()->InAt(0); |
| 426 | if (location.IsRegister()) { |
| 427 | __ cmpl(location.AsX86().AsCpuRegister(), Immediate(0)); |
| 428 | } else { |
| 429 | __ cmpl(Address(ESP, location.GetStackIndex()), Immediate(0)); |
| 430 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 431 | __ j(kEqual, codegen_->GetLabelOf(if_instr->IfFalseSuccessor())); |
| 432 | if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfTrueSuccessor())) { |
| 433 | __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor())); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
| 437 | void LocationsBuilderX86::VisitLocal(HLocal* local) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 438 | local->SetLocations(nullptr); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 441 | void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) { |
| 442 | DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock()); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 445 | void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) { |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 446 | local->SetLocations(nullptr); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 449 | void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) { |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 450 | // Nothing to do, this is driven by the code generator. |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 453 | void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) { |
| 454 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store); |
| 455 | switch (store->InputAt(1)->GetType()) { |
| 456 | case Primitive::kPrimBoolean: |
| 457 | case Primitive::kPrimByte: |
| 458 | case Primitive::kPrimChar: |
| 459 | case Primitive::kPrimShort: |
| 460 | case Primitive::kPrimInt: |
| 461 | case Primitive::kPrimNot: |
| 462 | locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal()))); |
| 463 | break; |
| 464 | |
| 465 | case Primitive::kPrimLong: |
| 466 | locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal()))); |
| 467 | break; |
| 468 | |
| 469 | default: |
| 470 | LOG(FATAL) << "Unimplemented local type " << store->InputAt(1)->GetType(); |
| 471 | } |
| 472 | store->SetLocations(locations); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 475 | void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | void LocationsBuilderX86::VisitEqual(HEqual* equal) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 479 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(equal); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 480 | locations->SetInAt(0, Location::RequiresRegister()); |
| 481 | locations->SetInAt(1, Location::Any()); |
| 482 | locations->SetOut(Location::SameAsFirstInput()); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 483 | equal->SetLocations(locations); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 486 | void InstructionCodeGeneratorX86::VisitEqual(HEqual* equal) { |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 487 | LocationSummary* locations = equal->GetLocations(); |
| 488 | if (locations->InAt(1).IsRegister()) { |
| 489 | __ cmpl(locations->InAt(0).AsX86().AsCpuRegister(), |
| 490 | locations->InAt(1).AsX86().AsCpuRegister()); |
| 491 | } else { |
| 492 | __ cmpl(locations->InAt(0).AsX86().AsCpuRegister(), |
| 493 | Address(ESP, locations->InAt(1).GetStackIndex())); |
| 494 | } |
| 495 | __ setb(kEqual, locations->Out().AsX86().AsCpuRegister()); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) { |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 499 | // TODO: Support constant locations. |
| 500 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant); |
| 501 | locations->SetOut(Location::RequiresRegister()); |
| 502 | constant->SetLocations(locations); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 505 | void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) { |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 506 | codegen_->Move(constant, constant->GetLocations()->Out(), nullptr); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 509 | void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) { |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 510 | // TODO: Support constant locations. |
| 511 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant); |
| 512 | locations->SetOut(Location::RequiresRegister()); |
| 513 | constant->SetLocations(locations); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) { |
| 517 | // Will be generated at use site. |
| 518 | } |
| 519 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 520 | void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 521 | ret->SetLocations(nullptr); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 524 | void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) { |
| 525 | codegen_->GenerateFrameExit(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 526 | __ ret(); |
| 527 | } |
| 528 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 529 | void LocationsBuilderX86::VisitReturn(HReturn* ret) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 530 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 531 | switch (ret->InputAt(0)->GetType()) { |
| 532 | case Primitive::kPrimBoolean: |
| 533 | case Primitive::kPrimByte: |
| 534 | case Primitive::kPrimChar: |
| 535 | case Primitive::kPrimShort: |
| 536 | case Primitive::kPrimInt: |
| 537 | case Primitive::kPrimNot: |
| 538 | locations->SetInAt(0, X86CpuLocation(EAX)); |
| 539 | break; |
| 540 | |
| 541 | case Primitive::kPrimLong: |
| 542 | locations->SetInAt( |
| 543 | 0, Location::RegisterLocation(X86ManagedRegister::FromRegisterPair(EAX_EDX))); |
| 544 | break; |
| 545 | |
| 546 | default: |
| 547 | LOG(FATAL) << "Unimplemented return type " << ret->InputAt(0)->GetType(); |
| 548 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 549 | ret->SetLocations(locations); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 552 | void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 553 | if (kIsDebugBuild) { |
| 554 | switch (ret->InputAt(0)->GetType()) { |
| 555 | case Primitive::kPrimBoolean: |
| 556 | case Primitive::kPrimByte: |
| 557 | case Primitive::kPrimChar: |
| 558 | case Primitive::kPrimShort: |
| 559 | case Primitive::kPrimInt: |
| 560 | case Primitive::kPrimNot: |
| 561 | DCHECK_EQ(ret->GetLocations()->InAt(0).AsX86().AsCpuRegister(), EAX); |
| 562 | break; |
| 563 | |
| 564 | case Primitive::kPrimLong: |
| 565 | DCHECK_EQ(ret->GetLocations()->InAt(0).AsX86().AsRegisterPair(), EAX_EDX); |
| 566 | break; |
| 567 | |
| 568 | default: |
| 569 | LOG(FATAL) << "Unimplemented return type " << ret->InputAt(0)->GetType(); |
| 570 | } |
| 571 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 572 | codegen_->GenerateFrameExit(); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 573 | __ ret(); |
| 574 | } |
| 575 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 576 | void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) { |
| 577 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(invoke); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 578 | locations->AddTemp(X86CpuLocation(EAX)); |
Nicolas Geoffray | db928fc | 2014-04-16 17:38:32 +0100 | [diff] [blame] | 579 | |
| 580 | InvokeDexCallingConventionVisitor calling_convention_visitor; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 581 | for (size_t i = 0; i < invoke->InputCount(); i++) { |
Nicolas Geoffray | db928fc | 2014-04-16 17:38:32 +0100 | [diff] [blame] | 582 | HInstruction* input = invoke->InputAt(i); |
| 583 | locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType())); |
| 584 | } |
| 585 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 586 | switch (invoke->GetType()) { |
| 587 | case Primitive::kPrimBoolean: |
| 588 | case Primitive::kPrimByte: |
| 589 | case Primitive::kPrimChar: |
| 590 | case Primitive::kPrimShort: |
| 591 | case Primitive::kPrimInt: |
| 592 | case Primitive::kPrimNot: |
| 593 | locations->SetOut(X86CpuLocation(EAX)); |
| 594 | break; |
| 595 | |
| 596 | case Primitive::kPrimLong: |
| 597 | locations->SetOut(Location::RegisterLocation(X86ManagedRegister::FromRegisterPair(EAX_EDX))); |
| 598 | break; |
| 599 | |
| 600 | case Primitive::kPrimVoid: |
| 601 | break; |
| 602 | |
| 603 | case Primitive::kPrimDouble: |
| 604 | case Primitive::kPrimFloat: |
| 605 | LOG(FATAL) << "Unimplemented return type " << invoke->GetType(); |
| 606 | break; |
| 607 | } |
| 608 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 609 | invoke->SetLocations(locations); |
| 610 | } |
| 611 | |
| 612 | void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 613 | Register temp = invoke->GetLocations()->GetTemp(0).AsX86().AsCpuRegister(); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 614 | size_t index_in_cache = mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() + |
Nicolas Geoffray | 707c809 | 2014-04-04 10:50:14 +0100 | [diff] [blame] | 615 | invoke->GetIndexInDexCache() * kX86WordSize; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 616 | |
| 617 | // TODO: Implement all kinds of calls: |
| 618 | // 1) boot -> boot |
| 619 | // 2) app -> boot |
| 620 | // 3) app -> app |
| 621 | // |
| 622 | // Currently we implement the app -> app logic, which looks up in the resolve cache. |
| 623 | |
| 624 | // temp = method; |
| 625 | LoadCurrentMethod(temp); |
| 626 | // temp = temp->dex_cache_resolved_methods_; |
| 627 | __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value())); |
| 628 | // temp = temp[index_in_cache] |
| 629 | __ movl(temp, Address(temp, index_in_cache)); |
| 630 | // (temp + offset_of_quick_compiled_code)() |
| 631 | __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value())); |
| 632 | |
| 633 | codegen_->RecordPcInfo(invoke->GetDexPc()); |
| 634 | } |
| 635 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 636 | void LocationsBuilderX86::VisitAdd(HAdd* add) { |
| 637 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(add); |
| 638 | switch (add->GetResultType()) { |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 639 | case Primitive::kPrimInt: |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 640 | case Primitive::kPrimLong: { |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 641 | locations->SetInAt(0, Location::RequiresRegister()); |
| 642 | locations->SetInAt(1, Location::Any()); |
| 643 | locations->SetOut(Location::SameAsFirstInput()); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 644 | break; |
| 645 | } |
| 646 | |
| 647 | case Primitive::kPrimBoolean: |
| 648 | case Primitive::kPrimByte: |
| 649 | case Primitive::kPrimChar: |
| 650 | case Primitive::kPrimShort: |
| 651 | LOG(FATAL) << "Unexpected add type " << add->GetResultType(); |
| 652 | break; |
| 653 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 654 | default: |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 655 | LOG(FATAL) << "Unimplemented add type " << add->GetResultType(); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 656 | } |
| 657 | add->SetLocations(locations); |
| 658 | } |
| 659 | |
| 660 | void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) { |
| 661 | LocationSummary* locations = add->GetLocations(); |
| 662 | switch (add->GetResultType()) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 663 | case Primitive::kPrimInt: { |
| 664 | DCHECK_EQ(locations->InAt(0).AsX86().AsCpuRegister(), |
| 665 | locations->Out().AsX86().AsCpuRegister()); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 666 | if (locations->InAt(1).IsRegister()) { |
| 667 | __ addl(locations->InAt(0).AsX86().AsCpuRegister(), |
| 668 | locations->InAt(1).AsX86().AsCpuRegister()); |
| 669 | } else { |
| 670 | __ addl(locations->InAt(0).AsX86().AsCpuRegister(), |
| 671 | Address(ESP, locations->InAt(1).GetStackIndex())); |
| 672 | } |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 673 | break; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | case Primitive::kPrimLong: { |
| 677 | DCHECK_EQ(locations->InAt(0).AsX86().AsRegisterPair(), |
| 678 | locations->Out().AsX86().AsRegisterPair()); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 679 | if (locations->InAt(1).IsRegister()) { |
| 680 | __ addl(locations->InAt(0).AsX86().AsRegisterPairLow(), |
| 681 | locations->InAt(1).AsX86().AsRegisterPairLow()); |
| 682 | __ adcl(locations->InAt(0).AsX86().AsRegisterPairHigh(), |
| 683 | locations->InAt(1).AsX86().AsRegisterPairHigh()); |
| 684 | } else { |
| 685 | __ addl(locations->InAt(0).AsX86().AsRegisterPairLow(), |
| 686 | Address(ESP, locations->InAt(1).GetStackIndex())); |
| 687 | __ adcl(locations->InAt(0).AsX86().AsRegisterPairHigh(), |
| 688 | Address(ESP, locations->InAt(1).GetHighStackIndex(kX86WordSize))); |
| 689 | } |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 690 | break; |
| 691 | } |
| 692 | |
| 693 | case Primitive::kPrimBoolean: |
| 694 | case Primitive::kPrimByte: |
| 695 | case Primitive::kPrimChar: |
| 696 | case Primitive::kPrimShort: |
| 697 | LOG(FATAL) << "Unexpected add type " << add->GetResultType(); |
| 698 | break; |
| 699 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 700 | default: |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 701 | LOG(FATAL) << "Unimplemented add type " << add->GetResultType(); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 702 | } |
| 703 | } |
| 704 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 705 | void LocationsBuilderX86::VisitSub(HSub* sub) { |
| 706 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(sub); |
| 707 | switch (sub->GetResultType()) { |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 708 | case Primitive::kPrimInt: |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 709 | case Primitive::kPrimLong: { |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 710 | locations->SetInAt(0, Location::RequiresRegister()); |
| 711 | locations->SetInAt(1, Location::Any()); |
| 712 | locations->SetOut(Location::SameAsFirstInput()); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 713 | break; |
| 714 | } |
| 715 | |
| 716 | case Primitive::kPrimBoolean: |
| 717 | case Primitive::kPrimByte: |
| 718 | case Primitive::kPrimChar: |
| 719 | case Primitive::kPrimShort: |
| 720 | LOG(FATAL) << "Unexpected sub type " << sub->GetResultType(); |
| 721 | break; |
| 722 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 723 | default: |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 724 | LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType(); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 725 | } |
| 726 | sub->SetLocations(locations); |
| 727 | } |
| 728 | |
| 729 | void InstructionCodeGeneratorX86::VisitSub(HSub* sub) { |
| 730 | LocationSummary* locations = sub->GetLocations(); |
| 731 | switch (sub->GetResultType()) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 732 | case Primitive::kPrimInt: { |
| 733 | DCHECK_EQ(locations->InAt(0).AsX86().AsCpuRegister(), |
| 734 | locations->Out().AsX86().AsCpuRegister()); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 735 | if (locations->InAt(1).IsRegister()) { |
| 736 | __ subl(locations->InAt(0).AsX86().AsCpuRegister(), |
| 737 | locations->InAt(1).AsX86().AsCpuRegister()); |
| 738 | } else { |
| 739 | __ subl(locations->InAt(0).AsX86().AsCpuRegister(), |
| 740 | Address(ESP, locations->InAt(1).GetStackIndex())); |
| 741 | } |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 742 | break; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | case Primitive::kPrimLong: { |
| 746 | DCHECK_EQ(locations->InAt(0).AsX86().AsRegisterPair(), |
| 747 | locations->Out().AsX86().AsRegisterPair()); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 748 | if (locations->InAt(1).IsRegister()) { |
| 749 | __ subl(locations->InAt(0).AsX86().AsRegisterPairLow(), |
| 750 | locations->InAt(1).AsX86().AsRegisterPairLow()); |
| 751 | __ sbbl(locations->InAt(0).AsX86().AsRegisterPairHigh(), |
| 752 | locations->InAt(1).AsX86().AsRegisterPairHigh()); |
| 753 | } else { |
| 754 | __ subl(locations->InAt(0).AsX86().AsRegisterPairLow(), |
| 755 | Address(ESP, locations->InAt(1).GetStackIndex())); |
| 756 | __ sbbl(locations->InAt(0).AsX86().AsRegisterPairHigh(), |
| 757 | Address(ESP, locations->InAt(1).GetHighStackIndex(kX86WordSize))); |
| 758 | } |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 759 | break; |
| 760 | } |
| 761 | |
| 762 | case Primitive::kPrimBoolean: |
| 763 | case Primitive::kPrimByte: |
| 764 | case Primitive::kPrimChar: |
| 765 | case Primitive::kPrimShort: |
| 766 | LOG(FATAL) << "Unexpected sub type " << sub->GetResultType(); |
| 767 | break; |
| 768 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 769 | default: |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 770 | LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType(); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 771 | } |
| 772 | } |
| 773 | |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 774 | void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) { |
| 775 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 776 | locations->SetOut(X86CpuLocation(EAX)); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 777 | InvokeRuntimeCallingConvention calling_convention; |
| 778 | locations->AddTemp(X86CpuLocation(calling_convention.GetRegisterAt(0))); |
| 779 | locations->AddTemp(X86CpuLocation(calling_convention.GetRegisterAt(1))); |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 780 | instruction->SetLocations(locations); |
| 781 | } |
| 782 | |
| 783 | void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) { |
| 784 | InvokeRuntimeCallingConvention calling_convention; |
| 785 | LoadCurrentMethod(calling_convention.GetRegisterAt(1)); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 786 | __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex())); |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 787 | |
Nicolas Geoffray | 707c809 | 2014-04-04 10:50:14 +0100 | [diff] [blame] | 788 | __ fs()->call( |
| 789 | Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck))); |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 790 | |
| 791 | codegen_->RecordPcInfo(instruction->GetDexPc()); |
| 792 | } |
| 793 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 794 | void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) { |
| 795 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
Nicolas Geoffray | a747a39 | 2014-04-17 14:56:23 +0100 | [diff] [blame] | 796 | Location location = parameter_visitor_.GetNextLocation(instruction->GetType()); |
| 797 | if (location.IsStackSlot()) { |
| 798 | location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize()); |
| 799 | } else if (location.IsDoubleStackSlot()) { |
| 800 | location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize()); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 801 | } |
Nicolas Geoffray | a747a39 | 2014-04-17 14:56:23 +0100 | [diff] [blame] | 802 | locations->SetOut(location); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 803 | instruction->SetLocations(locations); |
| 804 | } |
| 805 | |
| 806 | void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) { |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 807 | } |
| 808 | |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 809 | void LocationsBuilderX86::VisitNot(HNot* instruction) { |
| 810 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 811 | locations->SetInAt(0, Location::RequiresRegister()); |
| 812 | locations->SetOut(Location::SameAsFirstInput()); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 813 | instruction->SetLocations(locations); |
| 814 | } |
| 815 | |
| 816 | void InstructionCodeGeneratorX86::VisitNot(HNot* instruction) { |
| 817 | LocationSummary* locations = instruction->GetLocations(); |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 818 | Location out = locations->Out(); |
| 819 | DCHECK_EQ(locations->InAt(0).AsX86().AsCpuRegister(), out.AsX86().AsCpuRegister()); |
| 820 | __ xorl(out.AsX86().AsCpuRegister(), Immediate(1)); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 821 | } |
| 822 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 823 | void LocationsBuilderX86::VisitPhi(HPhi* instruction) { |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 824 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
| 825 | for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) { |
| 826 | locations->SetInAt(i, Location::Any()); |
| 827 | } |
| 828 | locations->SetOut(Location::Any()); |
| 829 | instruction->SetLocations(locations); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) { |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 833 | LOG(FATAL) << "Unreachable"; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 834 | } |
| 835 | |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 836 | void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) { |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 837 | LOG(FATAL) << "Unreachable"; |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) { |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 841 | codegen_->GetMoveResolver()->EmitNativeCode(instruction); |
| 842 | } |
| 843 | |
| 844 | X86Assembler* ParallelMoveResolverX86::GetAssembler() const { |
| 845 | return codegen_->GetAssembler(); |
| 846 | } |
| 847 | |
| 848 | void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) { |
| 849 | ScratchRegisterScope ensure_scratch( |
| 850 | this, kNoRegister, codegen_->GetNumberOfCoreRegisters()); |
| 851 | int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0; |
| 852 | __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset)); |
| 853 | __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister())); |
| 854 | } |
| 855 | |
| 856 | void ParallelMoveResolverX86::EmitMove(size_t index) { |
| 857 | MoveOperands* move = moves_.Get(index); |
| 858 | Location source = move->GetSource(); |
| 859 | Location destination = move->GetDestination(); |
| 860 | |
| 861 | if (source.IsRegister()) { |
| 862 | if (destination.IsRegister()) { |
| 863 | __ movl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister()); |
| 864 | } else { |
| 865 | DCHECK(destination.IsStackSlot()); |
| 866 | __ movl(Address(ESP, destination.GetStackIndex()), source.AsX86().AsCpuRegister()); |
| 867 | } |
| 868 | } else if (source.IsStackSlot()) { |
| 869 | if (destination.IsRegister()) { |
| 870 | __ movl(destination.AsX86().AsCpuRegister(), Address(ESP, source.GetStackIndex())); |
| 871 | } else { |
| 872 | DCHECK(destination.IsStackSlot()); |
| 873 | MoveMemoryToMemory(destination.GetStackIndex(), |
| 874 | source.GetStackIndex()); |
| 875 | } |
| 876 | } else { |
| 877 | LOG(FATAL) << "Unimplemented"; |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | void ParallelMoveResolverX86::Exchange(Register reg, int mem) { |
| 882 | ScratchRegisterScope ensure_scratch(this, reg, codegen_->GetNumberOfCoreRegisters()); |
| 883 | int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0; |
| 884 | __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset)); |
| 885 | __ movl(Address(ESP, mem + stack_offset), reg); |
| 886 | __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister())); |
| 887 | } |
| 888 | |
| 889 | |
| 890 | void ParallelMoveResolverX86::Exchange(int mem1, int mem2) { |
| 891 | ScratchRegisterScope ensure_scratch1( |
| 892 | this, kNoRegister, codegen_->GetNumberOfCoreRegisters()); |
| 893 | ScratchRegisterScope ensure_scratch2( |
| 894 | this, ensure_scratch1.GetRegister(), codegen_->GetNumberOfCoreRegisters()); |
| 895 | int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0; |
| 896 | stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0; |
| 897 | __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset)); |
| 898 | __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset)); |
| 899 | __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister())); |
| 900 | __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister())); |
| 901 | } |
| 902 | |
| 903 | void ParallelMoveResolverX86::EmitSwap(size_t index) { |
| 904 | MoveOperands* move = moves_.Get(index); |
| 905 | Location source = move->GetSource(); |
| 906 | Location destination = move->GetDestination(); |
| 907 | |
| 908 | if (source.IsRegister() && destination.IsRegister()) { |
| 909 | __ xchgl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister()); |
| 910 | } else if (source.IsRegister() && destination.IsStackSlot()) { |
| 911 | Exchange(source.AsX86().AsCpuRegister(), destination.GetStackIndex()); |
| 912 | } else if (source.IsStackSlot() && destination.IsRegister()) { |
| 913 | Exchange(destination.AsX86().AsCpuRegister(), source.GetStackIndex()); |
| 914 | } else if (source.IsStackSlot() && destination.IsStackSlot()) { |
| 915 | Exchange(destination.GetStackIndex(), source.GetStackIndex()); |
| 916 | } else { |
| 917 | LOG(FATAL) << "Unimplemented"; |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | void ParallelMoveResolverX86::SpillScratch(int reg) { |
| 922 | __ pushl(static_cast<Register>(reg)); |
| 923 | } |
| 924 | |
| 925 | void ParallelMoveResolverX86::RestoreScratch(int reg) { |
| 926 | __ popl(static_cast<Register>(reg)); |
Nicolas Geoffray | 4e3d23a | 2014-05-22 18:32:45 +0100 | [diff] [blame] | 927 | } |
| 928 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 929 | } // namespace x86 |
| 930 | } // namespace art |