Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [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_64.h" |
| 18 | |
| 19 | #include "entrypoints/quick/quick_entrypoints.h" |
| 20 | #include "mirror/array.h" |
| 21 | #include "mirror/art_method.h" |
| 22 | #include "mirror/object_reference.h" |
| 23 | #include "thread.h" |
| 24 | #include "utils/assembler.h" |
| 25 | #include "utils/x86_64/assembler_x86_64.h" |
| 26 | #include "utils/x86_64/managed_register_x86_64.h" |
| 27 | |
| 28 | #define __ reinterpret_cast<X86_64Assembler*>(GetAssembler())-> |
| 29 | |
| 30 | namespace art { |
| 31 | |
| 32 | x86_64::X86_64ManagedRegister Location::AsX86_64() const { |
| 33 | return reg().AsX86_64(); |
| 34 | } |
| 35 | |
| 36 | namespace x86_64 { |
| 37 | |
| 38 | static constexpr int kNumberOfPushedRegistersAtEntry = 1; |
| 39 | static constexpr int kCurrentMethodStackOffset = 0; |
| 40 | |
| 41 | void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const { |
| 42 | stream << X86_64ManagedRegister::FromCpuRegister(Register(reg)); |
| 43 | } |
| 44 | |
| 45 | void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const { |
| 46 | stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg)); |
| 47 | } |
| 48 | |
| 49 | static Location X86_64CpuLocation(Register reg) { |
| 50 | return Location::RegisterLocation(X86_64ManagedRegister::FromCpuRegister(reg)); |
| 51 | } |
| 52 | |
| 53 | CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph) |
| 54 | : CodeGenerator(graph, kNumberOfRegIds), |
| 55 | location_builder_(graph, this), |
| 56 | instruction_visitor_(graph, this) {} |
| 57 | |
| 58 | InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph, CodeGeneratorX86_64* codegen) |
| 59 | : HGraphVisitor(graph), |
| 60 | assembler_(codegen->GetAssembler()), |
| 61 | codegen_(codegen) {} |
| 62 | |
| 63 | ManagedRegister CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type, |
| 64 | bool* blocked_registers) const { |
| 65 | switch (type) { |
| 66 | case Primitive::kPrimLong: |
| 67 | case Primitive::kPrimByte: |
| 68 | case Primitive::kPrimBoolean: |
| 69 | case Primitive::kPrimChar: |
| 70 | case Primitive::kPrimShort: |
| 71 | case Primitive::kPrimInt: |
| 72 | case Primitive::kPrimNot: { |
| 73 | size_t reg = AllocateFreeRegisterInternal(blocked_registers, kNumberOfCpuRegisters); |
| 74 | return X86_64ManagedRegister::FromCpuRegister(static_cast<Register>(reg)); |
| 75 | } |
| 76 | |
| 77 | case Primitive::kPrimFloat: |
| 78 | case Primitive::kPrimDouble: |
| 79 | LOG(FATAL) << "Unimplemented register type " << type; |
| 80 | |
| 81 | case Primitive::kPrimVoid: |
| 82 | LOG(FATAL) << "Unreachable type " << type; |
| 83 | } |
| 84 | |
| 85 | return ManagedRegister::NoRegister(); |
| 86 | } |
| 87 | |
| 88 | void CodeGeneratorX86_64::SetupBlockedRegisters(bool* blocked_registers) const { |
| 89 | // Stack register is always reserved. |
| 90 | blocked_registers[RSP] = true; |
| 91 | |
| 92 | // TODO: We currently don't use Quick's callee saved registers. |
| 93 | blocked_registers[RBX] = true; |
| 94 | blocked_registers[RBP] = true; |
| 95 | blocked_registers[R12] = true; |
| 96 | blocked_registers[R13] = true; |
| 97 | blocked_registers[R14] = true; |
| 98 | blocked_registers[R15] = true; |
| 99 | } |
| 100 | |
| 101 | void CodeGeneratorX86_64::ComputeFrameSize(size_t number_of_spill_slots) { |
| 102 | // Add the current ART method to the frame size, the return PC, and the filler. |
| 103 | SetFrameSize(RoundUp( |
| 104 | number_of_spill_slots * kVRegSize |
| 105 | + kVRegSize // filler |
| 106 | + kVRegSize // Art method |
| 107 | + kNumberOfPushedRegistersAtEntry * kX86_64WordSize, |
| 108 | kStackAlignment)); |
| 109 | } |
| 110 | |
| 111 | void CodeGeneratorX86_64::GenerateFrameEntry() { |
| 112 | // Create a fake register to mimic Quick. |
| 113 | static const int kFakeReturnRegister = 16; |
| 114 | core_spill_mask_ |= (1 << kFakeReturnRegister); |
| 115 | |
| 116 | // The return PC has already been pushed on the stack. |
| 117 | __ subq(CpuRegister(RSP), Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize)); |
| 118 | __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI)); |
| 119 | } |
| 120 | |
| 121 | void CodeGeneratorX86_64::GenerateFrameExit() { |
| 122 | __ addq(CpuRegister(RSP), |
| 123 | Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize)); |
| 124 | } |
| 125 | |
| 126 | void CodeGeneratorX86_64::Bind(Label* label) { |
| 127 | __ Bind(label); |
| 128 | } |
| 129 | |
| 130 | void InstructionCodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) { |
| 131 | __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset)); |
| 132 | } |
| 133 | |
| 134 | int32_t CodeGeneratorX86_64::GetStackSlot(HLocal* local) const { |
| 135 | uint16_t reg_number = local->GetRegNumber(); |
| 136 | uint16_t number_of_vregs = GetGraph()->GetNumberOfVRegs(); |
| 137 | uint16_t number_of_in_vregs = GetGraph()->GetNumberOfInVRegs(); |
| 138 | if (reg_number >= number_of_vregs - number_of_in_vregs) { |
| 139 | // Local is a parameter of the method. It is stored in the caller's frame. |
| 140 | return GetFrameSize() + kVRegSize // ART method |
| 141 | + (reg_number - number_of_vregs + number_of_in_vregs) * kVRegSize; |
| 142 | } else { |
| 143 | // Local is a temporary in this method. It is stored in this method's frame. |
| 144 | return GetFrameSize() - (kNumberOfPushedRegistersAtEntry * kX86_64WordSize) |
| 145 | - kVRegSize |
| 146 | - (number_of_vregs * kVRegSize) |
| 147 | + (reg_number * kVRegSize); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const { |
| 152 | switch (load->GetType()) { |
| 153 | case Primitive::kPrimLong: |
| 154 | return Location::DoubleStackSlot(GetStackSlot(load->GetLocal())); |
| 155 | break; |
| 156 | |
| 157 | case Primitive::kPrimInt: |
| 158 | case Primitive::kPrimNot: |
| 159 | return Location::StackSlot(GetStackSlot(load->GetLocal())); |
| 160 | |
| 161 | case Primitive::kPrimFloat: |
| 162 | case Primitive::kPrimDouble: |
| 163 | LOG(FATAL) << "Unimplemented type " << load->GetType(); |
| 164 | |
| 165 | case Primitive::kPrimBoolean: |
| 166 | case Primitive::kPrimByte: |
| 167 | case Primitive::kPrimChar: |
| 168 | case Primitive::kPrimShort: |
| 169 | case Primitive::kPrimVoid: |
| 170 | LOG(FATAL) << "Unexpected type " << load->GetType(); |
| 171 | } |
| 172 | |
| 173 | LOG(FATAL) << "Unreachable"; |
| 174 | return Location(); |
| 175 | } |
| 176 | |
| 177 | void CodeGeneratorX86_64::Move(Location destination, Location source) { |
| 178 | if (source.Equals(destination)) { |
| 179 | return; |
| 180 | } |
| 181 | if (destination.IsRegister()) { |
| 182 | if (source.IsRegister()) { |
| 183 | __ movq(destination.AsX86_64().AsCpuRegister(), source.AsX86_64().AsCpuRegister()); |
| 184 | } else if (source.IsStackSlot()) { |
| 185 | __ movl(destination.AsX86_64().AsCpuRegister(), Address(CpuRegister(RSP), source.GetStackIndex())); |
| 186 | } else { |
| 187 | DCHECK(source.IsDoubleStackSlot()); |
| 188 | __ movq(destination.AsX86_64().AsCpuRegister(), Address(CpuRegister(RSP), source.GetStackIndex())); |
| 189 | } |
| 190 | } else if (destination.IsStackSlot()) { |
| 191 | if (source.IsRegister()) { |
| 192 | __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), source.AsX86_64().AsCpuRegister()); |
| 193 | } else { |
| 194 | DCHECK(source.IsStackSlot()); |
| 195 | __ movl(CpuRegister(RAX), Address(CpuRegister(RSP), source.GetStackIndex())); |
| 196 | __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(RAX)); |
| 197 | } |
| 198 | } else { |
| 199 | DCHECK(destination.IsDoubleStackSlot()); |
| 200 | if (source.IsRegister()) { |
| 201 | __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), source.AsX86_64().AsCpuRegister()); |
| 202 | } else { |
| 203 | DCHECK(source.IsDoubleStackSlot()); |
| 204 | __ movq(CpuRegister(RAX), Address(CpuRegister(RSP), source.GetStackIndex())); |
| 205 | __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(RAX)); |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | void CodeGeneratorX86_64::Move(HInstruction* instruction, Location location, HInstruction* move_for) { |
| 211 | if (instruction->AsIntConstant() != nullptr) { |
| 212 | Immediate imm(instruction->AsIntConstant()->GetValue()); |
| 213 | if (location.IsRegister()) { |
| 214 | __ movq(location.AsX86_64().AsCpuRegister(), imm); |
| 215 | } else { |
| 216 | __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm); |
| 217 | } |
| 218 | } else if (instruction->AsLongConstant() != nullptr) { |
| 219 | int64_t value = instruction->AsLongConstant()->GetValue(); |
| 220 | if (location.IsRegister()) { |
| 221 | __ movq(location.AsX86_64().AsCpuRegister(), Immediate(value)); |
| 222 | } else { |
| 223 | __ movq(CpuRegister(RAX), Immediate(value)); |
| 224 | __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(RAX)); |
| 225 | } |
| 226 | } else if (instruction->AsLoadLocal() != nullptr) { |
| 227 | switch (instruction->GetType()) { |
| 228 | case Primitive::kPrimBoolean: |
| 229 | case Primitive::kPrimByte: |
| 230 | case Primitive::kPrimChar: |
| 231 | case Primitive::kPrimShort: |
| 232 | case Primitive::kPrimInt: |
| 233 | case Primitive::kPrimNot: |
| 234 | Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal()))); |
| 235 | break; |
| 236 | |
| 237 | case Primitive::kPrimLong: |
| 238 | Move(location, Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal()))); |
| 239 | break; |
| 240 | |
| 241 | default: |
| 242 | LOG(FATAL) << "Unimplemented local type " << instruction->GetType(); |
| 243 | } |
| 244 | } else { |
| 245 | // This can currently only happen when the instruction that requests the move |
| 246 | // is the next to be compiled. |
| 247 | DCHECK_EQ(instruction->GetNext(), move_for); |
| 248 | switch (instruction->GetType()) { |
| 249 | case Primitive::kPrimBoolean: |
| 250 | case Primitive::kPrimByte: |
| 251 | case Primitive::kPrimChar: |
| 252 | case Primitive::kPrimShort: |
| 253 | case Primitive::kPrimInt: |
| 254 | case Primitive::kPrimNot: |
| 255 | case Primitive::kPrimLong: |
| 256 | Move(location, instruction->GetLocations()->Out()); |
| 257 | break; |
| 258 | |
| 259 | default: |
| 260 | LOG(FATAL) << "Unimplemented type " << instruction->GetType(); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | void LocationsBuilderX86_64::VisitGoto(HGoto* got) { |
| 266 | got->SetLocations(nullptr); |
| 267 | } |
| 268 | |
| 269 | void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) { |
| 270 | HBasicBlock* successor = got->GetSuccessor(); |
| 271 | if (GetGraph()->GetExitBlock() == successor) { |
| 272 | codegen_->GenerateFrameExit(); |
| 273 | } else if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) { |
| 274 | __ jmp(codegen_->GetLabelOf(successor)); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | void LocationsBuilderX86_64::VisitExit(HExit* exit) { |
| 279 | exit->SetLocations(nullptr); |
| 280 | } |
| 281 | |
| 282 | void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) { |
| 283 | if (kIsDebugBuild) { |
| 284 | __ Comment("Unreachable"); |
| 285 | __ int3(); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | void LocationsBuilderX86_64::VisitIf(HIf* if_instr) { |
| 290 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr); |
| 291 | locations->SetInAt(0, X86_64CpuLocation(RAX)); |
| 292 | if_instr->SetLocations(locations); |
| 293 | } |
| 294 | |
| 295 | void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) { |
| 296 | // TODO: Generate the input as a condition, instead of materializing in a register. |
| 297 | __ cmpl(if_instr->GetLocations()->InAt(0).AsX86_64().AsCpuRegister(), Immediate(0)); |
| 298 | __ j(kEqual, codegen_->GetLabelOf(if_instr->IfFalseSuccessor())); |
| 299 | if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfTrueSuccessor())) { |
| 300 | __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor())); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | void LocationsBuilderX86_64::VisitLocal(HLocal* local) { |
| 305 | local->SetLocations(nullptr); |
| 306 | } |
| 307 | |
| 308 | void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) { |
| 309 | DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock()); |
| 310 | } |
| 311 | |
| 312 | void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) { |
| 313 | local->SetLocations(nullptr); |
| 314 | } |
| 315 | |
| 316 | void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) { |
| 317 | // Nothing to do, this is driven by the code generator. |
| 318 | } |
| 319 | |
| 320 | void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) { |
| 321 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store); |
| 322 | switch (store->InputAt(1)->GetType()) { |
| 323 | case Primitive::kPrimBoolean: |
| 324 | case Primitive::kPrimByte: |
| 325 | case Primitive::kPrimChar: |
| 326 | case Primitive::kPrimShort: |
| 327 | case Primitive::kPrimInt: |
| 328 | case Primitive::kPrimNot: |
| 329 | locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal()))); |
| 330 | break; |
| 331 | |
| 332 | case Primitive::kPrimLong: |
| 333 | locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal()))); |
| 334 | break; |
| 335 | |
| 336 | default: |
| 337 | LOG(FATAL) << "Unimplemented local type " << store->InputAt(1)->GetType(); |
| 338 | } |
| 339 | store->SetLocations(locations); |
| 340 | } |
| 341 | |
| 342 | void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) { |
| 343 | } |
| 344 | |
| 345 | void LocationsBuilderX86_64::VisitEqual(HEqual* equal) { |
| 346 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(equal); |
| 347 | locations->SetInAt(0, X86_64CpuLocation(RAX)); |
| 348 | locations->SetInAt(1, X86_64CpuLocation(RCX)); |
| 349 | locations->SetOut(X86_64CpuLocation(RAX)); |
| 350 | equal->SetLocations(locations); |
| 351 | } |
| 352 | |
| 353 | void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* equal) { |
| 354 | __ cmpq(equal->GetLocations()->InAt(0).AsX86_64().AsCpuRegister(), |
| 355 | equal->GetLocations()->InAt(1).AsX86_64().AsCpuRegister()); |
| 356 | __ setcc(kEqual, equal->GetLocations()->Out().AsX86_64().AsCpuRegister()); |
| 357 | } |
| 358 | |
| 359 | void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) { |
| 360 | // TODO: Support constant locations. |
| 361 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant); |
| 362 | locations->SetOut(Location::RequiresRegister()); |
| 363 | constant->SetLocations(locations); |
| 364 | } |
| 365 | |
| 366 | void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) { |
| 367 | // Will be generated at use site. |
| 368 | } |
| 369 | |
| 370 | void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) { |
| 371 | // TODO: Support constant locations. |
| 372 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant); |
| 373 | locations->SetOut(Location::RequiresRegister()); |
| 374 | constant->SetLocations(locations); |
| 375 | } |
| 376 | |
| 377 | void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) { |
| 378 | // Will be generated at use site. |
| 379 | } |
| 380 | |
| 381 | void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) { |
| 382 | ret->SetLocations(nullptr); |
| 383 | } |
| 384 | |
| 385 | void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) { |
| 386 | codegen_->GenerateFrameExit(); |
| 387 | __ ret(); |
| 388 | } |
| 389 | |
| 390 | void LocationsBuilderX86_64::VisitReturn(HReturn* ret) { |
| 391 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret); |
| 392 | switch (ret->InputAt(0)->GetType()) { |
| 393 | case Primitive::kPrimBoolean: |
| 394 | case Primitive::kPrimByte: |
| 395 | case Primitive::kPrimChar: |
| 396 | case Primitive::kPrimShort: |
| 397 | case Primitive::kPrimInt: |
| 398 | case Primitive::kPrimNot: |
| 399 | case Primitive::kPrimLong: |
| 400 | locations->SetInAt(0, X86_64CpuLocation(RAX)); |
| 401 | break; |
| 402 | |
| 403 | default: |
| 404 | LOG(FATAL) << "Unimplemented return type " << ret->InputAt(0)->GetType(); |
| 405 | } |
| 406 | ret->SetLocations(locations); |
| 407 | } |
| 408 | |
| 409 | void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) { |
| 410 | if (kIsDebugBuild) { |
| 411 | switch (ret->InputAt(0)->GetType()) { |
| 412 | case Primitive::kPrimBoolean: |
| 413 | case Primitive::kPrimByte: |
| 414 | case Primitive::kPrimChar: |
| 415 | case Primitive::kPrimShort: |
| 416 | case Primitive::kPrimInt: |
| 417 | case Primitive::kPrimNot: |
| 418 | case Primitive::kPrimLong: |
| 419 | DCHECK_EQ(ret->GetLocations()->InAt(0).AsX86_64().AsCpuRegister().AsRegister(), RAX); |
| 420 | break; |
| 421 | |
| 422 | default: |
| 423 | LOG(FATAL) << "Unimplemented return type " << ret->InputAt(0)->GetType(); |
| 424 | } |
| 425 | } |
| 426 | codegen_->GenerateFrameExit(); |
| 427 | __ ret(); |
| 428 | } |
| 429 | |
| 430 | static constexpr Register kRuntimeParameterCoreRegisters[] = { RDI, RSI, RDX }; |
| 431 | static constexpr size_t kRuntimeParameterCoreRegistersLength = |
| 432 | arraysize(kRuntimeParameterCoreRegisters); |
| 433 | |
| 434 | class InvokeRuntimeCallingConvention : public CallingConvention<Register> { |
| 435 | public: |
| 436 | InvokeRuntimeCallingConvention() |
| 437 | : CallingConvention(kRuntimeParameterCoreRegisters, |
| 438 | kRuntimeParameterCoreRegistersLength) {} |
| 439 | |
| 440 | private: |
| 441 | DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention); |
| 442 | }; |
| 443 | |
| 444 | Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) { |
| 445 | switch (type) { |
| 446 | case Primitive::kPrimBoolean: |
| 447 | case Primitive::kPrimByte: |
| 448 | case Primitive::kPrimChar: |
| 449 | case Primitive::kPrimShort: |
| 450 | case Primitive::kPrimInt: |
| 451 | case Primitive::kPrimNot: { |
| 452 | uint32_t index = gp_index_++; |
| 453 | stack_index_++; |
| 454 | if (index < calling_convention.GetNumberOfRegisters()) { |
| 455 | return X86_64CpuLocation(calling_convention.GetRegisterAt(index)); |
| 456 | } else { |
| 457 | return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1)); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | case Primitive::kPrimLong: { |
| 462 | uint32_t index = gp_index_; |
| 463 | stack_index_ += 2; |
| 464 | if (index < calling_convention.GetNumberOfRegisters()) { |
| 465 | gp_index_ += 1; |
| 466 | return X86_64CpuLocation(calling_convention.GetRegisterAt(index)); |
| 467 | } else { |
| 468 | gp_index_ += 2; |
| 469 | return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2)); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | case Primitive::kPrimDouble: |
| 474 | case Primitive::kPrimFloat: |
| 475 | LOG(FATAL) << "Unimplemented parameter type " << type; |
| 476 | break; |
| 477 | |
| 478 | case Primitive::kPrimVoid: |
| 479 | LOG(FATAL) << "Unexpected parameter type " << type; |
| 480 | break; |
| 481 | } |
| 482 | return Location(); |
| 483 | } |
| 484 | |
| 485 | void LocationsBuilderX86_64::VisitInvokeStatic(HInvokeStatic* invoke) { |
| 486 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(invoke); |
| 487 | locations->AddTemp(X86_64CpuLocation(RDI)); |
| 488 | |
| 489 | InvokeDexCallingConventionVisitor calling_convention_visitor; |
| 490 | for (size_t i = 0; i < invoke->InputCount(); ++i) { |
| 491 | HInstruction* input = invoke->InputAt(i); |
| 492 | locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType())); |
| 493 | } |
| 494 | |
| 495 | switch (invoke->GetType()) { |
| 496 | case Primitive::kPrimBoolean: |
| 497 | case Primitive::kPrimByte: |
| 498 | case Primitive::kPrimChar: |
| 499 | case Primitive::kPrimShort: |
| 500 | case Primitive::kPrimInt: |
| 501 | case Primitive::kPrimNot: |
| 502 | case Primitive::kPrimLong: |
| 503 | locations->SetOut(X86_64CpuLocation(RAX)); |
| 504 | break; |
| 505 | |
| 506 | case Primitive::kPrimVoid: |
| 507 | break; |
| 508 | |
| 509 | case Primitive::kPrimDouble: |
| 510 | case Primitive::kPrimFloat: |
| 511 | LOG(FATAL) << "Unimplemented return type " << invoke->GetType(); |
| 512 | break; |
| 513 | } |
| 514 | |
| 515 | invoke->SetLocations(locations); |
| 516 | } |
| 517 | |
| 518 | void InstructionCodeGeneratorX86_64::VisitInvokeStatic(HInvokeStatic* invoke) { |
| 519 | CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsX86_64().AsCpuRegister(); |
| 520 | uint32_t heap_reference_size = sizeof(mirror::HeapReference<mirror::Object>); |
| 521 | size_t index_in_cache = mirror::Array::DataOffset(heap_reference_size).SizeValue() + |
| 522 | invoke->GetIndexInDexCache() * heap_reference_size; |
| 523 | |
| 524 | // TODO: Implement all kinds of calls: |
| 525 | // 1) boot -> boot |
| 526 | // 2) app -> boot |
| 527 | // 3) app -> app |
| 528 | // |
| 529 | // Currently we implement the app -> app logic, which looks up in the resolve cache. |
| 530 | |
| 531 | // temp = method; |
| 532 | LoadCurrentMethod(temp); |
| 533 | // temp = temp->dex_cache_resolved_methods_; |
| 534 | __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue())); |
| 535 | // temp = temp[index_in_cache] |
| 536 | __ movl(temp, Address(temp, index_in_cache)); |
| 537 | // (temp + offset_of_quick_compiled_code)() |
| 538 | __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue())); |
| 539 | |
| 540 | codegen_->RecordPcInfo(invoke->GetDexPc()); |
| 541 | } |
| 542 | |
| 543 | void LocationsBuilderX86_64::VisitAdd(HAdd* add) { |
| 544 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(add); |
| 545 | switch (add->GetResultType()) { |
| 546 | case Primitive::kPrimInt: |
| 547 | case Primitive::kPrimLong: { |
| 548 | locations->SetInAt(0, X86_64CpuLocation(RAX)); |
| 549 | locations->SetInAt(1, X86_64CpuLocation(RCX)); |
| 550 | locations->SetOut(X86_64CpuLocation(RAX)); |
| 551 | break; |
| 552 | } |
| 553 | |
| 554 | case Primitive::kPrimBoolean: |
| 555 | case Primitive::kPrimByte: |
| 556 | case Primitive::kPrimChar: |
| 557 | case Primitive::kPrimShort: |
| 558 | LOG(FATAL) << "Unexpected add type " << add->GetResultType(); |
| 559 | break; |
| 560 | |
| 561 | default: |
| 562 | LOG(FATAL) << "Unimplemented add type " << add->GetResultType(); |
| 563 | } |
| 564 | add->SetLocations(locations); |
| 565 | } |
| 566 | |
| 567 | void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) { |
| 568 | LocationSummary* locations = add->GetLocations(); |
| 569 | switch (add->GetResultType()) { |
| 570 | case Primitive::kPrimInt: |
| 571 | case Primitive::kPrimLong: { |
| 572 | DCHECK_EQ(locations->InAt(0).AsX86_64().AsCpuRegister().AsRegister(), |
| 573 | locations->Out().AsX86_64().AsCpuRegister().AsRegister()); |
| 574 | __ addq(locations->InAt(0).AsX86_64().AsCpuRegister(), |
| 575 | locations->InAt(1).AsX86_64().AsCpuRegister()); |
| 576 | break; |
| 577 | } |
| 578 | |
| 579 | case Primitive::kPrimBoolean: |
| 580 | case Primitive::kPrimByte: |
| 581 | case Primitive::kPrimChar: |
| 582 | case Primitive::kPrimShort: |
| 583 | LOG(FATAL) << "Unexpected add type " << add->GetResultType(); |
| 584 | break; |
| 585 | |
| 586 | default: |
| 587 | LOG(FATAL) << "Unimplemented add type " << add->GetResultType(); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | void LocationsBuilderX86_64::VisitSub(HSub* sub) { |
| 592 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(sub); |
| 593 | switch (sub->GetResultType()) { |
| 594 | case Primitive::kPrimInt: |
| 595 | case Primitive::kPrimLong: { |
| 596 | locations->SetInAt(0, X86_64CpuLocation(RAX)); |
| 597 | locations->SetInAt(1, X86_64CpuLocation(RCX)); |
| 598 | locations->SetOut(X86_64CpuLocation(RAX)); |
| 599 | break; |
| 600 | } |
| 601 | |
| 602 | case Primitive::kPrimBoolean: |
| 603 | case Primitive::kPrimByte: |
| 604 | case Primitive::kPrimChar: |
| 605 | case Primitive::kPrimShort: |
| 606 | LOG(FATAL) << "Unexpected sub type " << sub->GetResultType(); |
| 607 | break; |
| 608 | |
| 609 | default: |
| 610 | LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType(); |
| 611 | } |
| 612 | sub->SetLocations(locations); |
| 613 | } |
| 614 | |
| 615 | void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) { |
| 616 | LocationSummary* locations = sub->GetLocations(); |
| 617 | switch (sub->GetResultType()) { |
| 618 | case Primitive::kPrimInt: |
| 619 | case Primitive::kPrimLong: { |
| 620 | DCHECK_EQ(locations->InAt(0).AsX86_64().AsCpuRegister().AsRegister(), |
| 621 | locations->Out().AsX86_64().AsCpuRegister().AsRegister()); |
| 622 | __ subq(locations->InAt(0).AsX86_64().AsCpuRegister(), |
| 623 | locations->InAt(1).AsX86_64().AsCpuRegister()); |
| 624 | break; |
| 625 | } |
| 626 | |
| 627 | case Primitive::kPrimBoolean: |
| 628 | case Primitive::kPrimByte: |
| 629 | case Primitive::kPrimChar: |
| 630 | case Primitive::kPrimShort: |
| 631 | LOG(FATAL) << "Unexpected sub type " << sub->GetResultType(); |
| 632 | break; |
| 633 | |
| 634 | default: |
| 635 | LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType(); |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) { |
| 640 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
| 641 | locations->SetOut(X86_64CpuLocation(RAX)); |
| 642 | instruction->SetLocations(locations); |
| 643 | } |
| 644 | |
| 645 | void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) { |
| 646 | InvokeRuntimeCallingConvention calling_convention; |
| 647 | LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1))); |
| 648 | __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex())); |
| 649 | |
| 650 | __ gs()->call(Address::Absolute( |
| 651 | QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true)); |
| 652 | |
| 653 | codegen_->RecordPcInfo(instruction->GetDexPc()); |
| 654 | } |
| 655 | |
| 656 | void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) { |
| 657 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
| 658 | Location location = parameter_visitor_.GetNextLocation(instruction->GetType()); |
| 659 | if (location.IsStackSlot()) { |
| 660 | location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize()); |
| 661 | } else if (location.IsDoubleStackSlot()) { |
| 662 | location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize()); |
| 663 | } |
| 664 | locations->SetOut(location); |
| 665 | instruction->SetLocations(locations); |
| 666 | } |
| 667 | |
| 668 | void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) { |
| 669 | // Nothing to do, the parameter is already at its location. |
| 670 | } |
| 671 | |
| 672 | void LocationsBuilderX86_64::VisitNot(HNot* instruction) { |
| 673 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
| 674 | locations->SetInAt(0, X86_64CpuLocation(RAX)); |
| 675 | locations->SetOut(X86_64CpuLocation(RAX)); |
| 676 | instruction->SetLocations(locations); |
| 677 | } |
| 678 | |
| 679 | void InstructionCodeGeneratorX86_64::VisitNot(HNot* instruction) { |
| 680 | LocationSummary* locations = instruction->GetLocations(); |
| 681 | DCHECK_EQ(locations->InAt(0).AsX86_64().AsCpuRegister().AsRegister(), |
| 682 | locations->Out().AsX86_64().AsCpuRegister().AsRegister()); |
| 683 | __ xorq(locations->Out().AsX86_64().AsCpuRegister(), Immediate(1)); |
| 684 | } |
| 685 | |
| 686 | void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) { |
| 687 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
| 688 | for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) { |
| 689 | locations->SetInAt(i, Location::Any()); |
| 690 | } |
| 691 | locations->SetOut(Location::Any()); |
| 692 | instruction->SetLocations(locations); |
| 693 | } |
| 694 | |
| 695 | void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) { |
| 696 | LOG(FATAL) << "Unimplemented"; |
| 697 | } |
| 698 | |
| 699 | void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) { |
| 700 | LOG(FATAL) << "Unimplemented"; |
| 701 | } |
| 702 | |
| 703 | void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) { |
| 704 | LOG(FATAL) << "Unimplemented"; |
| 705 | } |
| 706 | |
| 707 | } // namespace x86_64 |
| 708 | } // namespace art |