Alexandre Rames | 5319def | 2014-10-23 10:03: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_arm64.h" |
| 18 | |
| 19 | #include "entrypoints/quick/quick_entrypoints.h" |
| 20 | #include "gc/accounting/card_table.h" |
| 21 | #include "mirror/array-inl.h" |
| 22 | #include "mirror/art_method.h" |
| 23 | #include "mirror/class.h" |
| 24 | #include "thread.h" |
| 25 | #include "utils/arm64/assembler_arm64.h" |
| 26 | #include "utils/assembler.h" |
| 27 | #include "utils/stack_checks.h" |
| 28 | |
| 29 | |
| 30 | using namespace vixl; // NOLINT(build/namespaces) |
| 31 | |
| 32 | #ifdef __ |
| 33 | #error "ARM64 Codegen VIXL macro-assembler macro already defined." |
| 34 | #endif |
| 35 | |
| 36 | |
| 37 | namespace art { |
| 38 | |
| 39 | namespace arm64 { |
| 40 | |
| 41 | static bool IsFPType(Primitive::Type type) { |
| 42 | return type == Primitive::kPrimFloat || type == Primitive::kPrimDouble; |
| 43 | } |
| 44 | |
| 45 | // TODO: clean-up some of the constant definitions. |
| 46 | static constexpr size_t kHeapRefSize = sizeof(mirror::HeapReference<mirror::Object>); |
| 47 | static constexpr int kCurrentMethodStackOffset = 0; |
| 48 | |
| 49 | namespace { |
| 50 | // Convenience helpers to ease conversion to and from VIXL operands. |
| 51 | |
| 52 | int VIXLRegCodeFromART(int code) { |
| 53 | // TODO: static check? |
| 54 | DCHECK_EQ(SP, 31); |
| 55 | DCHECK_EQ(WSP, 31); |
| 56 | DCHECK_EQ(XZR, 32); |
| 57 | DCHECK_EQ(WZR, 32); |
| 58 | if (code == SP) { |
| 59 | return vixl::kSPRegInternalCode; |
| 60 | } |
| 61 | if (code == XZR) { |
| 62 | return vixl::kZeroRegCode; |
| 63 | } |
| 64 | return code; |
| 65 | } |
| 66 | |
| 67 | int ARTRegCodeFromVIXL(int code) { |
| 68 | // TODO: static check? |
| 69 | DCHECK_EQ(SP, 31); |
| 70 | DCHECK_EQ(WSP, 31); |
| 71 | DCHECK_EQ(XZR, 32); |
| 72 | DCHECK_EQ(WZR, 32); |
| 73 | if (code == vixl::kSPRegInternalCode) { |
| 74 | return SP; |
| 75 | } |
| 76 | if (code == vixl::kZeroRegCode) { |
| 77 | return XZR; |
| 78 | } |
| 79 | return code; |
| 80 | } |
| 81 | |
| 82 | Register XRegisterFrom(Location location) { |
| 83 | return Register::XRegFromCode(VIXLRegCodeFromART(location.reg())); |
| 84 | } |
| 85 | |
| 86 | Register WRegisterFrom(Location location) { |
| 87 | return Register::WRegFromCode(VIXLRegCodeFromART(location.reg())); |
| 88 | } |
| 89 | |
| 90 | Register RegisterFrom(Location location, Primitive::Type type) { |
| 91 | DCHECK(type != Primitive::kPrimVoid && !IsFPType(type)); |
| 92 | return type == Primitive::kPrimLong ? XRegisterFrom(location) : WRegisterFrom(location); |
| 93 | } |
| 94 | |
| 95 | Register OutputRegister(HInstruction* instr) { |
| 96 | return RegisterFrom(instr->GetLocations()->Out(), instr->GetType()); |
| 97 | } |
| 98 | |
| 99 | Register InputRegisterAt(HInstruction* instr, int input_index) { |
| 100 | return RegisterFrom(instr->GetLocations()->InAt(input_index), |
| 101 | instr->InputAt(input_index)->GetType()); |
| 102 | } |
| 103 | |
| 104 | int64_t Int64ConstantFrom(Location location) { |
| 105 | HConstant* instr = location.GetConstant(); |
| 106 | return instr->IsIntConstant() ? instr->AsIntConstant()->GetValue() |
| 107 | : instr->AsLongConstant()->GetValue(); |
| 108 | } |
| 109 | |
| 110 | Operand OperandFrom(Location location, Primitive::Type type) { |
| 111 | if (location.IsRegister()) { |
| 112 | return Operand(RegisterFrom(location, type)); |
| 113 | } else { |
| 114 | return Operand(Int64ConstantFrom(location)); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | Operand InputOperandAt(HInstruction* instr, int input_index) { |
| 119 | return OperandFrom(instr->GetLocations()->InAt(input_index), |
| 120 | instr->InputAt(input_index)->GetType()); |
| 121 | } |
| 122 | |
| 123 | MemOperand StackOperandFrom(Location location) { |
| 124 | return MemOperand(sp, location.GetStackIndex()); |
| 125 | } |
| 126 | |
| 127 | MemOperand HeapOperand(const Register& base, Offset offset) { |
| 128 | // A heap reference must be 32bit, so fit in a W register. |
| 129 | DCHECK(base.IsW()); |
| 130 | return MemOperand(base.X(), offset.SizeValue()); |
| 131 | } |
| 132 | |
| 133 | MemOperand HeapOperandFrom(Location location, Primitive::Type type, Offset offset) { |
| 134 | return HeapOperand(RegisterFrom(location, type), offset); |
| 135 | } |
| 136 | |
| 137 | Location LocationFrom(const Register& reg) { |
| 138 | return Location::RegisterLocation(ARTRegCodeFromVIXL(reg.code())); |
| 139 | } |
| 140 | |
| 141 | } // namespace |
| 142 | |
| 143 | inline Condition ARM64Condition(IfCondition cond) { |
| 144 | switch (cond) { |
| 145 | case kCondEQ: return eq; |
| 146 | case kCondNE: return ne; |
| 147 | case kCondLT: return lt; |
| 148 | case kCondLE: return le; |
| 149 | case kCondGT: return gt; |
| 150 | case kCondGE: return ge; |
| 151 | default: |
| 152 | LOG(FATAL) << "Unknown if condition"; |
| 153 | } |
| 154 | return nv; // Unreachable. |
| 155 | } |
| 156 | |
| 157 | static const Register kRuntimeParameterCoreRegisters[] = { x0, x1, x2, x3, x4, x5, x6, x7 }; |
| 158 | static constexpr size_t kRuntimeParameterCoreRegistersLength = |
| 159 | arraysize(kRuntimeParameterCoreRegisters); |
| 160 | static const FPRegister kRuntimeParameterFpuRegisters[] = { }; |
| 161 | static constexpr size_t kRuntimeParameterFpuRegistersLength = 0; |
| 162 | |
| 163 | class InvokeRuntimeCallingConvention : public CallingConvention<Register, FPRegister> { |
| 164 | public: |
| 165 | static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters); |
| 166 | |
| 167 | InvokeRuntimeCallingConvention() |
| 168 | : CallingConvention(kRuntimeParameterCoreRegisters, |
| 169 | kRuntimeParameterCoreRegistersLength, |
| 170 | kRuntimeParameterFpuRegisters, |
| 171 | kRuntimeParameterFpuRegistersLength) {} |
| 172 | |
| 173 | Location GetReturnLocation(Primitive::Type return_type); |
| 174 | |
| 175 | private: |
| 176 | DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention); |
| 177 | }; |
| 178 | |
| 179 | Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) { |
| 180 | DCHECK_NE(return_type, Primitive::kPrimVoid); |
| 181 | if (return_type == Primitive::kPrimFloat || return_type == Primitive::kPrimDouble) { |
| 182 | LOG(FATAL) << "Unimplemented return type " << return_type; |
| 183 | } |
| 184 | return LocationFrom(x0); |
| 185 | } |
| 186 | |
| 187 | #define __ reinterpret_cast<Arm64Assembler*>(codegen->GetAssembler())->vixl_masm_-> |
| 188 | |
| 189 | class SlowPathCodeARM64 : public SlowPathCode { |
| 190 | public: |
| 191 | SlowPathCodeARM64() : entry_label_(), exit_label_() {} |
| 192 | |
| 193 | vixl::Label* GetEntryLabel() { return &entry_label_; } |
| 194 | vixl::Label* GetExitLabel() { return &exit_label_; } |
| 195 | |
| 196 | private: |
| 197 | vixl::Label entry_label_; |
| 198 | vixl::Label exit_label_; |
| 199 | |
| 200 | DISALLOW_COPY_AND_ASSIGN(SlowPathCodeARM64); |
| 201 | }; |
| 202 | |
| 203 | class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 { |
| 204 | public: |
| 205 | explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction, |
| 206 | Location index_location, |
| 207 | Location length_location) |
| 208 | : instruction_(instruction), |
| 209 | index_location_(index_location), |
| 210 | length_location_(length_location) {} |
| 211 | |
| 212 | virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE { |
| 213 | CodeGeneratorARM64* arm64_codegen = reinterpret_cast<CodeGeneratorARM64*>(codegen); |
| 214 | __ Bind(GetEntryLabel()); |
| 215 | InvokeRuntimeCallingConvention calling_convention; |
| 216 | arm64_codegen->MoveHelper(LocationFrom(calling_convention.GetRegisterAt(0)), |
| 217 | index_location_, Primitive::kPrimInt); |
| 218 | arm64_codegen->MoveHelper(LocationFrom(calling_convention.GetRegisterAt(1)), |
| 219 | length_location_, Primitive::kPrimInt); |
| 220 | size_t offset = QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, pThrowArrayBounds).SizeValue(); |
| 221 | __ Ldr(lr, MemOperand(tr, offset)); |
| 222 | __ Blr(lr); |
| 223 | codegen->RecordPcInfo(instruction_, instruction_->GetDexPc()); |
| 224 | } |
| 225 | |
| 226 | private: |
| 227 | HBoundsCheck* const instruction_; |
| 228 | const Location index_location_; |
| 229 | const Location length_location_; |
| 230 | |
| 231 | DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64); |
| 232 | }; |
| 233 | |
| 234 | class NullCheckSlowPathARM64 : public SlowPathCodeARM64 { |
| 235 | public: |
| 236 | explicit NullCheckSlowPathARM64(HNullCheck* instr) : instruction_(instr) {} |
| 237 | |
| 238 | virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE { |
| 239 | __ Bind(GetEntryLabel()); |
| 240 | int32_t offset = QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, pThrowNullPointer).Int32Value(); |
| 241 | __ Ldr(lr, MemOperand(tr, offset)); |
| 242 | __ Blr(lr); |
| 243 | codegen->RecordPcInfo(instruction_, instruction_->GetDexPc()); |
| 244 | } |
| 245 | |
| 246 | private: |
| 247 | HNullCheck* const instruction_; |
| 248 | |
| 249 | DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64); |
| 250 | }; |
| 251 | |
| 252 | class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 { |
| 253 | public: |
| 254 | explicit SuspendCheckSlowPathARM64(HSuspendCheck* instruction, |
| 255 | HBasicBlock* successor) |
| 256 | : instruction_(instruction), successor_(successor) {} |
| 257 | |
| 258 | virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE { |
| 259 | size_t offset = QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, pTestSuspend).SizeValue(); |
| 260 | __ Bind(GetEntryLabel()); |
| 261 | __ Ldr(lr, MemOperand(tr, offset)); |
| 262 | __ Blr(lr); |
| 263 | codegen->RecordPcInfo(instruction_, instruction_->GetDexPc()); |
| 264 | __ B(GetReturnLabel()); |
| 265 | } |
| 266 | |
| 267 | vixl::Label* GetReturnLabel() { |
| 268 | DCHECK(successor_ == nullptr); |
| 269 | return &return_label_; |
| 270 | } |
| 271 | |
| 272 | |
| 273 | private: |
| 274 | HSuspendCheck* const instruction_; |
| 275 | // If not null, the block to branch to after the suspend check. |
| 276 | HBasicBlock* const successor_; |
| 277 | |
| 278 | // If `successor_` is null, the label to branch to after the suspend check. |
| 279 | vixl::Label return_label_; |
| 280 | |
| 281 | DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64); |
| 282 | }; |
| 283 | |
| 284 | #undef __ |
| 285 | |
| 286 | Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) { |
| 287 | Location next_location; |
| 288 | if (type == Primitive::kPrimVoid) { |
| 289 | LOG(FATAL) << "Unreachable type " << type; |
| 290 | } |
| 291 | |
| 292 | if (type == Primitive::kPrimFloat || type == Primitive::kPrimDouble) { |
| 293 | LOG(FATAL) << "Unimplemented type " << type; |
| 294 | } |
| 295 | |
| 296 | if (gp_index_ < calling_convention.GetNumberOfRegisters()) { |
| 297 | next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_)); |
| 298 | if (type == Primitive::kPrimLong) { |
| 299 | // Double stack slot reserved on the stack. |
| 300 | stack_index_++; |
| 301 | } |
| 302 | } else { // Stack. |
| 303 | if (type == Primitive::kPrimLong) { |
| 304 | next_location = Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_)); |
| 305 | // Double stack slot reserved on the stack. |
| 306 | stack_index_++; |
| 307 | } else { |
| 308 | next_location = Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_)); |
| 309 | } |
| 310 | } |
| 311 | // Move to the next register/stack slot. |
| 312 | gp_index_++; |
| 313 | stack_index_++; |
| 314 | return next_location; |
| 315 | } |
| 316 | |
| 317 | CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph) |
| 318 | : CodeGenerator(graph, |
| 319 | kNumberOfAllocatableRegisters, |
| 320 | kNumberOfAllocatableFloatingPointRegisters, |
| 321 | kNumberOfAllocatableRegisterPairs), |
| 322 | block_labels_(nullptr), |
| 323 | location_builder_(graph, this), |
| 324 | instruction_visitor_(graph, this) {} |
| 325 | |
| 326 | #define __ reinterpret_cast<Arm64Assembler*>(GetAssembler())->vixl_masm_-> |
| 327 | |
| 328 | void CodeGeneratorARM64::GenerateFrameEntry() { |
| 329 | // TODO: Add proper support for the stack overflow check. |
| 330 | UseScratchRegisterScope temps(assembler_.vixl_masm_); |
| 331 | Register temp = temps.AcquireX(); |
| 332 | __ Add(temp, sp, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64))); |
| 333 | __ Ldr(temp, MemOperand(temp, 0)); |
| 334 | RecordPcInfo(nullptr, 0); |
| 335 | |
| 336 | CPURegList preserved_regs = GetFramePreservedRegisters(); |
| 337 | int frame_size = GetFrameSize(); |
| 338 | core_spill_mask_ |= preserved_regs.list(); |
| 339 | |
| 340 | __ Str(w0, MemOperand(sp, -frame_size, PreIndex)); |
| 341 | __ PokeCPURegList(preserved_regs, frame_size - preserved_regs.TotalSizeInBytes()); |
| 342 | |
| 343 | // Stack layout: |
| 344 | // sp[frame_size - 8] : lr. |
| 345 | // ... : other preserved registers. |
| 346 | // sp[frame_size - regs_size]: first preserved register. |
| 347 | // ... : reserved frame space. |
| 348 | // sp[0] : context pointer. |
| 349 | } |
| 350 | |
| 351 | void CodeGeneratorARM64::GenerateFrameExit() { |
| 352 | int frame_size = GetFrameSize(); |
| 353 | CPURegList preserved_regs = GetFramePreservedRegisters(); |
| 354 | __ PeekCPURegList(preserved_regs, frame_size - preserved_regs.TotalSizeInBytes()); |
| 355 | __ Drop(frame_size); |
| 356 | } |
| 357 | |
| 358 | void CodeGeneratorARM64::Bind(HBasicBlock* block) { |
| 359 | __ Bind(GetLabelOf(block)); |
| 360 | } |
| 361 | |
| 362 | void CodeGeneratorARM64::MoveHelper(Location destination, |
| 363 | Location source, |
| 364 | Primitive::Type type) { |
| 365 | if (source.Equals(destination)) { |
| 366 | return; |
| 367 | } |
| 368 | if (destination.IsRegister()) { |
| 369 | Register dst = RegisterFrom(destination, type); |
| 370 | if (source.IsRegister()) { |
| 371 | Register src = RegisterFrom(source, type); |
| 372 | DCHECK(dst.IsSameSizeAndType(src)); |
| 373 | __ Mov(dst, src); |
| 374 | } else { |
| 375 | DCHECK(dst.Is64Bits() || !source.IsDoubleStackSlot()); |
| 376 | __ Ldr(dst, StackOperandFrom(source)); |
| 377 | } |
| 378 | } else { |
| 379 | DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot()); |
| 380 | if (source.IsRegister()) { |
| 381 | __ Str(RegisterFrom(source, type), StackOperandFrom(destination)); |
| 382 | } else { |
| 383 | UseScratchRegisterScope temps(assembler_.vixl_masm_); |
| 384 | Register temp = destination.IsDoubleStackSlot() ? temps.AcquireX() : temps.AcquireW(); |
| 385 | __ Ldr(temp, StackOperandFrom(source)); |
| 386 | __ Str(temp, StackOperandFrom(destination)); |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | void CodeGeneratorARM64::Move(HInstruction* instruction, |
| 392 | Location location, |
| 393 | HInstruction* move_for) { |
| 394 | LocationSummary* locations = instruction->GetLocations(); |
| 395 | if (locations != nullptr && locations->Out().Equals(location)) { |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | Primitive::Type type = instruction->GetType(); |
| 400 | |
| 401 | if (instruction->IsIntConstant() || instruction->IsLongConstant()) { |
| 402 | int64_t value = instruction->IsIntConstant() ? instruction->AsIntConstant()->GetValue() |
| 403 | : instruction->AsLongConstant()->GetValue(); |
| 404 | if (location.IsRegister()) { |
| 405 | Register dst = RegisterFrom(location, type); |
| 406 | DCHECK((instruction->IsIntConstant() && dst.Is32Bits()) || |
| 407 | (instruction->IsLongConstant() && dst.Is64Bits())); |
| 408 | __ Mov(dst, value); |
| 409 | } else { |
| 410 | DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot()); |
| 411 | UseScratchRegisterScope temps(assembler_.vixl_masm_); |
| 412 | Register temp = instruction->IsIntConstant() ? temps.AcquireW() : temps.AcquireX(); |
| 413 | __ Mov(temp, value); |
| 414 | __ Str(temp, StackOperandFrom(location)); |
| 415 | } |
| 416 | |
| 417 | } else if (instruction->IsLoadLocal()) { |
| 418 | uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal()); |
| 419 | switch (type) { |
| 420 | case Primitive::kPrimNot: |
| 421 | case Primitive::kPrimBoolean: |
| 422 | case Primitive::kPrimByte: |
| 423 | case Primitive::kPrimChar: |
| 424 | case Primitive::kPrimShort: |
| 425 | case Primitive::kPrimInt: |
| 426 | MoveHelper(location, Location::StackSlot(stack_slot), type); |
| 427 | break; |
| 428 | case Primitive::kPrimLong: |
| 429 | MoveHelper(location, Location::DoubleStackSlot(stack_slot), type); |
| 430 | break; |
| 431 | default: |
| 432 | LOG(FATAL) << "Unimplemented type" << type; |
| 433 | } |
| 434 | |
| 435 | } else { |
| 436 | DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary()); |
| 437 | MoveHelper(location, locations->Out(), type); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | size_t CodeGeneratorARM64::FrameEntrySpillSize() const { |
| 442 | return GetFramePreservedRegistersSize(); |
| 443 | } |
| 444 | |
| 445 | Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const { |
| 446 | Primitive::Type type = load->GetType(); |
| 447 | switch (type) { |
| 448 | case Primitive::kPrimNot: |
| 449 | case Primitive::kPrimBoolean: |
| 450 | case Primitive::kPrimByte: |
| 451 | case Primitive::kPrimChar: |
| 452 | case Primitive::kPrimShort: |
| 453 | case Primitive::kPrimInt: |
| 454 | return Location::StackSlot(GetStackSlot(load->GetLocal())); |
| 455 | case Primitive::kPrimLong: |
| 456 | return Location::DoubleStackSlot(GetStackSlot(load->GetLocal())); |
| 457 | case Primitive::kPrimFloat: |
| 458 | case Primitive::kPrimDouble: |
| 459 | LOG(FATAL) << "Unimplemented type " << type; |
| 460 | break; |
| 461 | case Primitive::kPrimVoid: |
| 462 | default: |
| 463 | LOG(FATAL) << "Unexpected type " << type; |
| 464 | } |
| 465 | LOG(FATAL) << "Unreachable"; |
| 466 | return Location::NoLocation(); |
| 467 | } |
| 468 | |
| 469 | void CodeGeneratorARM64::MarkGCCard(Register object, Register value) { |
| 470 | UseScratchRegisterScope temps(assembler_.vixl_masm_); |
| 471 | Register card = temps.AcquireX(); |
| 472 | Register temp = temps.AcquireX(); |
| 473 | vixl::Label done; |
| 474 | __ Cbz(value, &done); |
| 475 | __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value())); |
| 476 | __ Lsr(temp, object, gc::accounting::CardTable::kCardShift); |
| 477 | __ Strb(card, MemOperand(card, temp)); |
| 478 | __ Bind(&done); |
| 479 | } |
| 480 | |
| 481 | void CodeGeneratorARM64::SetupBlockedRegisters() const { |
| 482 | // Block reserved registers: |
| 483 | // ip0 (VIXL temporary) |
| 484 | // ip1 (VIXL temporary) |
| 485 | // xSuspend (Suspend counter) |
| 486 | // lr |
| 487 | // sp is not part of the allocatable registers, so we don't need to block it. |
| 488 | CPURegList reserved_core_registers = vixl_reserved_core_registers; |
| 489 | reserved_core_registers.Combine(runtime_reserved_core_registers); |
| 490 | // TODO: See if we should instead allow allocating but preserve those if used. |
| 491 | reserved_core_registers.Combine(quick_callee_saved_registers); |
| 492 | while (!reserved_core_registers.IsEmpty()) { |
| 493 | blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const { |
| 498 | if (type == Primitive::kPrimVoid) { |
| 499 | LOG(FATAL) << "Unreachable type " << type; |
| 500 | } |
| 501 | |
| 502 | if (type == Primitive::kPrimFloat || type == Primitive::kPrimDouble) { |
| 503 | LOG(FATAL) << "Unimplemented support for floating-point"; |
| 504 | } |
| 505 | |
| 506 | ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfXRegisters); |
| 507 | DCHECK_NE(reg, -1); |
| 508 | blocked_core_registers_[reg] = true; |
| 509 | |
| 510 | if (IsFPType(type)) { |
| 511 | return Location::FpuRegisterLocation(reg); |
| 512 | } else { |
| 513 | return Location::RegisterLocation(reg); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const { |
| 518 | stream << Arm64ManagedRegister::FromXRegister(XRegister(reg)); |
| 519 | } |
| 520 | |
| 521 | void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const { |
| 522 | stream << Arm64ManagedRegister::FromDRegister(DRegister(reg)); |
| 523 | } |
| 524 | |
| 525 | #undef __ |
| 526 | #define __ assembler_->vixl_masm_-> |
| 527 | |
| 528 | InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph, |
| 529 | CodeGeneratorARM64* codegen) |
| 530 | : HGraphVisitor(graph), |
| 531 | assembler_(codegen->GetAssembler()), |
| 532 | codegen_(codegen) {} |
| 533 | |
| 534 | #define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \ |
| 535 | M(ArrayGet) \ |
| 536 | M(ArraySet) \ |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 537 | M(ClinitCheck) \ |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 538 | M(DoubleConstant) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 539 | M(Div) \ |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 540 | M(FloatConstant) \ |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 541 | M(LoadClass) \ |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 542 | M(LoadString) \ |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 543 | M(Neg) \ |
| 544 | M(NewArray) \ |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 545 | M(ParallelMove) \ |
| 546 | M(StaticFieldGet) \ |
| 547 | M(StaticFieldSet) \ |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 548 | |
| 549 | #define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode |
| 550 | |
| 551 | enum UnimplementedInstructionBreakCode { |
| 552 | #define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name), |
| 553 | FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION) |
| 554 | #undef ENUM_UNIMPLEMENTED_INSTRUCTION |
| 555 | }; |
| 556 | |
| 557 | #define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \ |
| 558 | void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \ |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 559 | UNUSED(instr); \ |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 560 | __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \ |
| 561 | } \ |
| 562 | void LocationsBuilderARM64::Visit##name(H##name* instr) { \ |
| 563 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \ |
| 564 | locations->SetOut(Location::Any()); \ |
| 565 | } |
| 566 | FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS) |
| 567 | #undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS |
| 568 | |
| 569 | #undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE |
| 570 | |
| 571 | void LocationsBuilderARM64::HandleAddSub(HBinaryOperation* instr) { |
| 572 | DCHECK(instr->IsAdd() || instr->IsSub()); |
| 573 | DCHECK_EQ(instr->InputCount(), 2U); |
| 574 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); |
| 575 | Primitive::Type type = instr->GetResultType(); |
| 576 | switch (type) { |
| 577 | case Primitive::kPrimInt: |
| 578 | case Primitive::kPrimLong: { |
| 579 | locations->SetInAt(0, Location::RequiresRegister()); |
| 580 | locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1))); |
| 581 | locations->SetOut(Location::RequiresRegister()); |
| 582 | break; |
| 583 | } |
| 584 | case Primitive::kPrimBoolean: |
| 585 | case Primitive::kPrimByte: |
| 586 | case Primitive::kPrimChar: |
| 587 | case Primitive::kPrimShort: |
| 588 | LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type; |
| 589 | break; |
| 590 | default: |
| 591 | LOG(FATAL) << "Unimplemented " << instr->DebugName() << " type " << type; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | void InstructionCodeGeneratorARM64::HandleAddSub(HBinaryOperation* instr) { |
| 596 | DCHECK(instr->IsAdd() || instr->IsSub()); |
| 597 | |
| 598 | Primitive::Type type = instr->GetType(); |
| 599 | Register dst = OutputRegister(instr); |
| 600 | Register lhs = InputRegisterAt(instr, 0); |
| 601 | Operand rhs = InputOperandAt(instr, 1); |
| 602 | |
| 603 | switch (type) { |
| 604 | case Primitive::kPrimInt: |
| 605 | case Primitive::kPrimLong: |
| 606 | if (instr->IsAdd()) { |
| 607 | __ Add(dst, lhs, rhs); |
| 608 | } else { |
| 609 | __ Sub(dst, lhs, rhs); |
| 610 | } |
| 611 | break; |
| 612 | |
| 613 | case Primitive::kPrimBoolean: |
| 614 | case Primitive::kPrimByte: |
| 615 | case Primitive::kPrimChar: |
| 616 | case Primitive::kPrimShort: |
| 617 | LOG(FATAL) << "Unexpected add/sub type " << type; |
| 618 | break; |
| 619 | default: |
| 620 | LOG(FATAL) << "Unimplemented add/sub type " << type; |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | void LocationsBuilderARM64::VisitAdd(HAdd* instruction) { |
| 625 | HandleAddSub(instruction); |
| 626 | } |
| 627 | |
| 628 | void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) { |
| 629 | HandleAddSub(instruction); |
| 630 | } |
| 631 | |
| 632 | void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) { |
| 633 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
| 634 | locations->SetInAt(0, Location::RequiresRegister()); |
| 635 | locations->SetOut(Location::RequiresRegister()); |
| 636 | } |
| 637 | |
| 638 | void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) { |
| 639 | __ Ldr(OutputRegister(instruction), |
| 640 | HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset())); |
| 641 | } |
| 642 | |
| 643 | void LocationsBuilderARM64::VisitCompare(HCompare* instruction) { |
| 644 | LocationSummary* locations = |
| 645 | new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall); |
| 646 | locations->SetInAt(0, Location::RequiresRegister()); |
| 647 | locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1))); |
| 648 | locations->SetOut(Location::RequiresRegister()); |
| 649 | } |
| 650 | |
| 651 | void InstructionCodeGeneratorARM64::VisitCompare(HCompare* instruction) { |
| 652 | Primitive::Type in_type = instruction->InputAt(0)->GetType(); |
| 653 | |
| 654 | DCHECK_EQ(in_type, Primitive::kPrimLong); |
| 655 | switch (in_type) { |
| 656 | case Primitive::kPrimLong: { |
| 657 | vixl::Label done; |
| 658 | Register result = OutputRegister(instruction); |
| 659 | Register left = InputRegisterAt(instruction, 0); |
| 660 | Operand right = InputOperandAt(instruction, 1); |
| 661 | __ Subs(result, left, right); |
| 662 | __ B(eq, &done); |
| 663 | __ Mov(result, 1); |
| 664 | __ Cneg(result, result, le); |
| 665 | __ Bind(&done); |
| 666 | break; |
| 667 | } |
| 668 | default: |
| 669 | LOG(FATAL) << "Unimplemented compare type " << in_type; |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | void LocationsBuilderARM64::VisitCondition(HCondition* instruction) { |
| 674 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
| 675 | locations->SetInAt(0, Location::RequiresRegister()); |
| 676 | locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1))); |
| 677 | if (instruction->NeedsMaterialization()) { |
| 678 | locations->SetOut(Location::RequiresRegister()); |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) { |
| 683 | if (!instruction->NeedsMaterialization()) { |
| 684 | return; |
| 685 | } |
| 686 | |
| 687 | LocationSummary* locations = instruction->GetLocations(); |
| 688 | Register lhs = InputRegisterAt(instruction, 0); |
| 689 | Operand rhs = InputOperandAt(instruction, 1); |
| 690 | Register res = RegisterFrom(locations->Out(), instruction->GetType()); |
| 691 | Condition cond = ARM64Condition(instruction->GetCondition()); |
| 692 | |
| 693 | __ Cmp(lhs, rhs); |
| 694 | __ Csel(res, vixl::Assembler::AppropriateZeroRegFor(res), Operand(1), InvertCondition(cond)); |
| 695 | } |
| 696 | |
| 697 | #define FOR_EACH_CONDITION_INSTRUCTION(M) \ |
| 698 | M(Equal) \ |
| 699 | M(NotEqual) \ |
| 700 | M(LessThan) \ |
| 701 | M(LessThanOrEqual) \ |
| 702 | M(GreaterThan) \ |
| 703 | M(GreaterThanOrEqual) |
| 704 | #define DEFINE_CONDITION_VISITORS(Name) \ |
| 705 | void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \ |
| 706 | void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } |
| 707 | FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS) |
| 708 | #undef FOR_EACH_CONDITION_INSTRUCTION |
| 709 | |
| 710 | void LocationsBuilderARM64::VisitExit(HExit* exit) { |
| 711 | exit->SetLocations(nullptr); |
| 712 | } |
| 713 | |
| 714 | void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 715 | UNUSED(exit); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 716 | if (kIsDebugBuild) { |
| 717 | down_cast<Arm64Assembler*>(GetAssembler())->Comment("Unreachable"); |
| 718 | __ Brk(0); // TODO: Introduce special markers for such code locations. |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | void LocationsBuilderARM64::VisitGoto(HGoto* got) { |
| 723 | got->SetLocations(nullptr); |
| 724 | } |
| 725 | |
| 726 | void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) { |
| 727 | HBasicBlock* successor = got->GetSuccessor(); |
| 728 | // TODO: Support for suspend checks emission. |
| 729 | if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) { |
| 730 | __ B(codegen_->GetLabelOf(successor)); |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | void LocationsBuilderARM64::VisitIf(HIf* if_instr) { |
| 735 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr); |
| 736 | HInstruction* cond = if_instr->InputAt(0); |
| 737 | DCHECK(cond->IsCondition()); |
| 738 | if (cond->AsCondition()->NeedsMaterialization()) { |
| 739 | locations->SetInAt(0, Location::RequiresRegister()); |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) { |
| 744 | HInstruction* cond = if_instr->InputAt(0); |
| 745 | DCHECK(cond->IsCondition()); |
| 746 | HCondition* condition = cond->AsCondition(); |
| 747 | vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor()); |
| 748 | vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor()); |
| 749 | |
| 750 | // TODO: Support constant condition input in VisitIf. |
| 751 | |
| 752 | if (condition->NeedsMaterialization()) { |
| 753 | // The condition instruction has been materialized, compare the output to 0. |
| 754 | Location cond_val = if_instr->GetLocations()->InAt(0); |
| 755 | DCHECK(cond_val.IsRegister()); |
| 756 | __ Cbnz(InputRegisterAt(if_instr, 0), true_target); |
| 757 | |
| 758 | } else { |
| 759 | // The condition instruction has not been materialized, use its inputs as |
| 760 | // the comparison and its condition as the branch condition. |
| 761 | Register lhs = InputRegisterAt(condition, 0); |
| 762 | Operand rhs = InputOperandAt(condition, 1); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame^] | 763 | Condition arm64_cond = ARM64Condition(condition->GetCondition()); |
| 764 | if ((arm64_cond == eq || arm64_cond == ne) && rhs.IsImmediate() && (rhs.immediate() == 0)) { |
| 765 | if (arm64_cond == eq) { |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 766 | __ Cbz(lhs, true_target); |
| 767 | } else { |
| 768 | __ Cbnz(lhs, true_target); |
| 769 | } |
| 770 | } else { |
| 771 | __ Cmp(lhs, rhs); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame^] | 772 | __ B(arm64_cond, true_target); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 773 | } |
| 774 | } |
| 775 | |
| 776 | if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfFalseSuccessor())) { |
| 777 | __ B(false_target); |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) { |
| 782 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
| 783 | locations->SetInAt(0, Location::RequiresRegister()); |
| 784 | locations->SetOut(Location::RequiresRegister()); |
| 785 | } |
| 786 | |
| 787 | void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) { |
| 788 | Primitive::Type res_type = instruction->GetType(); |
| 789 | Register res = OutputRegister(instruction); |
| 790 | Register obj = InputRegisterAt(instruction, 0); |
| 791 | uint32_t offset = instruction->GetFieldOffset().Uint32Value(); |
| 792 | |
| 793 | switch (res_type) { |
| 794 | case Primitive::kPrimBoolean: { |
| 795 | __ Ldrb(res, MemOperand(obj, offset)); |
| 796 | break; |
| 797 | } |
| 798 | case Primitive::kPrimByte: { |
| 799 | __ Ldrsb(res, MemOperand(obj, offset)); |
| 800 | break; |
| 801 | } |
| 802 | case Primitive::kPrimShort: { |
| 803 | __ Ldrsh(res, MemOperand(obj, offset)); |
| 804 | break; |
| 805 | } |
| 806 | case Primitive::kPrimChar: { |
| 807 | __ Ldrh(res, MemOperand(obj, offset)); |
| 808 | break; |
| 809 | } |
| 810 | case Primitive::kPrimInt: |
| 811 | case Primitive::kPrimNot: |
| 812 | case Primitive::kPrimLong: { // TODO: support volatile. |
| 813 | DCHECK(res.IsX() == (res_type == Primitive::kPrimLong)); |
| 814 | __ Ldr(res, MemOperand(obj, offset)); |
| 815 | break; |
| 816 | } |
| 817 | |
| 818 | case Primitive::kPrimFloat: |
| 819 | case Primitive::kPrimDouble: |
| 820 | LOG(FATAL) << "Unimplemented register res_type " << res_type; |
| 821 | break; |
| 822 | |
| 823 | case Primitive::kPrimVoid: |
| 824 | LOG(FATAL) << "Unreachable res_type " << res_type; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) { |
| 829 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
| 830 | locations->SetInAt(0, Location::RequiresRegister()); |
| 831 | locations->SetInAt(1, Location::RequiresRegister()); |
| 832 | } |
| 833 | |
| 834 | void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) { |
| 835 | Register obj = InputRegisterAt(instruction, 0); |
| 836 | Register value = InputRegisterAt(instruction, 1); |
| 837 | Primitive::Type field_type = instruction->InputAt(1)->GetType(); |
| 838 | uint32_t offset = instruction->GetFieldOffset().Uint32Value(); |
| 839 | |
| 840 | switch (field_type) { |
| 841 | case Primitive::kPrimBoolean: |
| 842 | case Primitive::kPrimByte: { |
| 843 | __ Strb(value, MemOperand(obj, offset)); |
| 844 | break; |
| 845 | } |
| 846 | |
| 847 | case Primitive::kPrimShort: |
| 848 | case Primitive::kPrimChar: { |
| 849 | __ Strh(value, MemOperand(obj, offset)); |
| 850 | break; |
| 851 | } |
| 852 | |
| 853 | case Primitive::kPrimInt: |
| 854 | case Primitive::kPrimNot: |
| 855 | case Primitive::kPrimLong: { |
| 856 | DCHECK(value.IsX() == (field_type == Primitive::kPrimLong)); |
| 857 | __ Str(value, MemOperand(obj, offset)); |
| 858 | |
| 859 | if (field_type == Primitive::kPrimNot) { |
| 860 | codegen_->MarkGCCard(obj, value); |
| 861 | } |
| 862 | break; |
| 863 | } |
| 864 | |
| 865 | case Primitive::kPrimFloat: |
| 866 | case Primitive::kPrimDouble: |
| 867 | LOG(FATAL) << "Unimplemented register type " << field_type; |
| 868 | break; |
| 869 | |
| 870 | case Primitive::kPrimVoid: |
| 871 | LOG(FATAL) << "Unreachable type " << field_type; |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) { |
| 876 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant); |
| 877 | locations->SetOut(Location::ConstantLocation(constant)); |
| 878 | } |
| 879 | |
| 880 | void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) { |
| 881 | // Will be generated at use site. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 882 | UNUSED(constant); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | void LocationsBuilderARM64::VisitInvokeStatic(HInvokeStatic* invoke) { |
| 886 | HandleInvoke(invoke); |
| 887 | } |
| 888 | |
| 889 | void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) { |
| 890 | HandleInvoke(invoke); |
| 891 | } |
| 892 | |
| 893 | void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) { |
| 894 | LocationSummary* locations = |
| 895 | new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall); |
| 896 | locations->AddTemp(LocationFrom(x0)); |
| 897 | |
| 898 | InvokeDexCallingConventionVisitor calling_convention_visitor; |
| 899 | for (size_t i = 0; i < invoke->InputCount(); i++) { |
| 900 | HInstruction* input = invoke->InputAt(i); |
| 901 | locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType())); |
| 902 | } |
| 903 | |
| 904 | Primitive::Type return_type = invoke->GetType(); |
| 905 | if (return_type != Primitive::kPrimVoid) { |
| 906 | locations->SetOut(calling_convention_visitor.GetReturnLocation(return_type)); |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | void InstructionCodeGeneratorARM64::VisitInvokeStatic(HInvokeStatic* invoke) { |
| 911 | Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0)); |
| 912 | // Make sure that ArtMethod* is passed in W0 as per the calling convention |
| 913 | DCHECK(temp.Is(w0)); |
| 914 | size_t index_in_cache = mirror::Array::DataOffset(kHeapRefSize).SizeValue() + |
| 915 | invoke->GetIndexInDexCache() * kHeapRefSize; |
| 916 | |
| 917 | // TODO: Implement all kinds of calls: |
| 918 | // 1) boot -> boot |
| 919 | // 2) app -> boot |
| 920 | // 3) app -> app |
| 921 | // |
| 922 | // Currently we implement the app -> app logic, which looks up in the resolve cache. |
| 923 | |
| 924 | // temp = method; |
| 925 | __ Ldr(temp, MemOperand(sp, kCurrentMethodStackOffset)); |
| 926 | // temp = temp->dex_cache_resolved_methods_; |
| 927 | __ Ldr(temp, MemOperand(temp.X(), mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue())); |
| 928 | // temp = temp[index_in_cache]; |
| 929 | __ Ldr(temp, MemOperand(temp.X(), index_in_cache)); |
| 930 | // lr = temp->entry_point_from_quick_compiled_code_; |
| 931 | __ Ldr(lr, MemOperand(temp.X(), mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue())); |
| 932 | // lr(); |
| 933 | __ Blr(lr); |
| 934 | |
| 935 | codegen_->RecordPcInfo(invoke, invoke->GetDexPc()); |
| 936 | DCHECK(!codegen_->IsLeafMethod()); |
| 937 | } |
| 938 | |
| 939 | void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) { |
| 940 | LocationSummary* locations = invoke->GetLocations(); |
| 941 | Location receiver = locations->InAt(0); |
| 942 | Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0)); |
| 943 | size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() + |
| 944 | invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry); |
| 945 | Offset class_offset = mirror::Object::ClassOffset(); |
| 946 | Offset entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(); |
| 947 | |
| 948 | // temp = object->GetClass(); |
| 949 | if (receiver.IsStackSlot()) { |
| 950 | __ Ldr(temp.W(), MemOperand(sp, receiver.GetStackIndex())); |
| 951 | __ Ldr(temp.W(), MemOperand(temp, class_offset.SizeValue())); |
| 952 | } else { |
| 953 | DCHECK(receiver.IsRegister()); |
| 954 | __ Ldr(temp.W(), HeapOperandFrom(receiver, Primitive::kPrimNot, |
| 955 | class_offset)); |
| 956 | } |
| 957 | // temp = temp->GetMethodAt(method_offset); |
| 958 | __ Ldr(temp.W(), MemOperand(temp, method_offset)); |
| 959 | // lr = temp->GetEntryPoint(); |
| 960 | __ Ldr(lr, MemOperand(temp, entry_point.SizeValue())); |
| 961 | // lr(); |
| 962 | __ Blr(lr); |
| 963 | DCHECK(!codegen_->IsLeafMethod()); |
| 964 | codegen_->RecordPcInfo(invoke, invoke->GetDexPc()); |
| 965 | } |
| 966 | |
| 967 | void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) { |
| 968 | load->SetLocations(nullptr); |
| 969 | } |
| 970 | |
| 971 | void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) { |
| 972 | // Nothing to do, this is driven by the code generator. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 973 | UNUSED(load); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | void LocationsBuilderARM64::VisitLocal(HLocal* local) { |
| 977 | local->SetLocations(nullptr); |
| 978 | } |
| 979 | |
| 980 | void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) { |
| 981 | DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock()); |
| 982 | } |
| 983 | |
| 984 | void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) { |
| 985 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant); |
| 986 | locations->SetOut(Location::ConstantLocation(constant)); |
| 987 | } |
| 988 | |
| 989 | void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) { |
| 990 | // Will be generated at use site. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 991 | UNUSED(constant); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 992 | } |
| 993 | |
Alexandre Rames | 42d641b | 2014-10-27 14:00:51 +0000 | [diff] [blame] | 994 | void LocationsBuilderARM64::VisitMul(HMul* mul) { |
| 995 | LocationSummary* locations = |
| 996 | new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall); |
| 997 | switch (mul->GetResultType()) { |
| 998 | case Primitive::kPrimInt: |
| 999 | case Primitive::kPrimLong: |
| 1000 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1001 | locations->SetInAt(1, Location::RequiresRegister()); |
| 1002 | locations->SetOut(Location::RequiresRegister()); |
| 1003 | break; |
| 1004 | |
| 1005 | case Primitive::kPrimFloat: |
| 1006 | case Primitive::kPrimDouble: |
| 1007 | LOG(FATAL) << "Unimplemented mul type " << mul->GetResultType(); |
| 1008 | break; |
| 1009 | |
| 1010 | default: |
| 1011 | LOG(FATAL) << "Unexpected mul type " << mul->GetResultType(); |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) { |
| 1016 | switch (mul->GetResultType()) { |
| 1017 | case Primitive::kPrimInt: |
| 1018 | case Primitive::kPrimLong: |
| 1019 | __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1)); |
| 1020 | break; |
| 1021 | |
| 1022 | case Primitive::kPrimFloat: |
| 1023 | case Primitive::kPrimDouble: |
| 1024 | LOG(FATAL) << "Unimplemented mul type " << mul->GetResultType(); |
| 1025 | break; |
| 1026 | |
| 1027 | default: |
| 1028 | LOG(FATAL) << "Unexpected mul type " << mul->GetResultType(); |
| 1029 | } |
| 1030 | } |
| 1031 | |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 1032 | void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) { |
| 1033 | LocationSummary* locations = |
| 1034 | new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall); |
| 1035 | InvokeRuntimeCallingConvention calling_convention; |
| 1036 | locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0))); |
| 1037 | locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(1))); |
| 1038 | locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot)); |
| 1039 | } |
| 1040 | |
| 1041 | void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) { |
| 1042 | LocationSummary* locations = instruction->GetLocations(); |
| 1043 | Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt); |
| 1044 | DCHECK(type_index.Is(w0)); |
| 1045 | Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimNot); |
| 1046 | DCHECK(current_method.Is(w1)); |
| 1047 | __ Ldr(current_method, MemOperand(sp, kCurrentMethodStackOffset)); |
| 1048 | __ Mov(type_index, instruction->GetTypeIndex()); |
| 1049 | __ Ldr(lr, MemOperand(tr, QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, pAllocObjectWithAccessCheck).Int32Value())); |
| 1050 | __ Blr(lr); |
| 1051 | codegen_->RecordPcInfo(instruction, instruction->GetDexPc()); |
| 1052 | DCHECK(!codegen_->IsLeafMethod()); |
| 1053 | } |
| 1054 | |
| 1055 | void LocationsBuilderARM64::VisitNot(HNot* instruction) { |
| 1056 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
| 1057 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1058 | locations->SetOut(Location::RequiresRegister()); |
| 1059 | } |
| 1060 | |
| 1061 | void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) { |
| 1062 | switch (instruction->InputAt(0)->GetType()) { |
| 1063 | case Primitive::kPrimBoolean: |
| 1064 | __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), Operand(1)); |
| 1065 | break; |
| 1066 | |
| 1067 | case Primitive::kPrimInt: |
| 1068 | __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0)); |
| 1069 | break; |
| 1070 | |
| 1071 | case Primitive::kPrimLong: |
| 1072 | LOG(FATAL) << "Not yet implemented type for not operation " << instruction->GetResultType(); |
| 1073 | break; |
| 1074 | |
| 1075 | default: |
| 1076 | LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType(); |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) { |
| 1081 | LocationSummary* locations = |
| 1082 | new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall); |
| 1083 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1084 | if (instruction->HasUses()) { |
| 1085 | locations->SetOut(Location::SameAsFirstInput()); |
| 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) { |
| 1090 | SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction); |
| 1091 | codegen_->AddSlowPath(slow_path); |
| 1092 | |
| 1093 | LocationSummary* locations = instruction->GetLocations(); |
| 1094 | Location obj = locations->InAt(0); |
| 1095 | if (obj.IsRegister()) { |
| 1096 | __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel()); |
| 1097 | } else { |
| 1098 | DCHECK(obj.IsConstant()) << obj; |
| 1099 | DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0); |
| 1100 | __ B(slow_path->GetEntryLabel()); |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) { |
| 1105 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
| 1106 | Location location = parameter_visitor_.GetNextLocation(instruction->GetType()); |
| 1107 | if (location.IsStackSlot()) { |
| 1108 | location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize()); |
| 1109 | } else if (location.IsDoubleStackSlot()) { |
| 1110 | location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize()); |
| 1111 | } |
| 1112 | locations->SetOut(location); |
| 1113 | } |
| 1114 | |
| 1115 | void InstructionCodeGeneratorARM64::VisitParameterValue(HParameterValue* instruction) { |
| 1116 | // Nothing to do, the parameter is already at its location. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1117 | UNUSED(instruction); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | void LocationsBuilderARM64::VisitPhi(HPhi* instruction) { |
| 1121 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
| 1122 | for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) { |
| 1123 | locations->SetInAt(i, Location::Any()); |
| 1124 | } |
| 1125 | locations->SetOut(Location::Any()); |
| 1126 | } |
| 1127 | |
| 1128 | void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1129 | UNUSED(instruction); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 1130 | LOG(FATAL) << "Unreachable"; |
| 1131 | } |
| 1132 | |
| 1133 | void LocationsBuilderARM64::VisitReturn(HReturn* instruction) { |
| 1134 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
| 1135 | Primitive::Type return_type = instruction->InputAt(0)->GetType(); |
| 1136 | |
| 1137 | if (return_type == Primitive::kPrimFloat || return_type == Primitive::kPrimDouble) { |
| 1138 | LOG(FATAL) << "Unimplemented return type " << return_type; |
| 1139 | } |
| 1140 | |
| 1141 | locations->SetInAt(0, LocationFrom(x0)); |
| 1142 | } |
| 1143 | |
| 1144 | void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) { |
| 1145 | if (kIsDebugBuild) { |
| 1146 | Primitive::Type type = instruction->InputAt(0)->GetType(); |
| 1147 | switch (type) { |
| 1148 | case Primitive::kPrimBoolean: |
| 1149 | case Primitive::kPrimByte: |
| 1150 | case Primitive::kPrimChar: |
| 1151 | case Primitive::kPrimShort: |
| 1152 | case Primitive::kPrimInt: |
| 1153 | case Primitive::kPrimNot: |
| 1154 | DCHECK(InputRegisterAt(instruction, 0).Is(w0)); |
| 1155 | break; |
| 1156 | |
| 1157 | case Primitive::kPrimLong: |
| 1158 | DCHECK(InputRegisterAt(instruction, 0).Is(x0)); |
| 1159 | break; |
| 1160 | |
| 1161 | default: |
| 1162 | LOG(FATAL) << "Unimplemented return type " << type; |
| 1163 | } |
| 1164 | } |
| 1165 | codegen_->GenerateFrameExit(); |
| 1166 | __ Br(lr); |
| 1167 | } |
| 1168 | |
| 1169 | void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) { |
| 1170 | instruction->SetLocations(nullptr); |
| 1171 | } |
| 1172 | |
| 1173 | void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1174 | UNUSED(instruction); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 1175 | codegen_->GenerateFrameExit(); |
| 1176 | __ Br(lr); |
| 1177 | } |
| 1178 | |
| 1179 | void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) { |
| 1180 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store); |
| 1181 | Primitive::Type field_type = store->InputAt(1)->GetType(); |
| 1182 | switch (field_type) { |
| 1183 | case Primitive::kPrimBoolean: |
| 1184 | case Primitive::kPrimByte: |
| 1185 | case Primitive::kPrimChar: |
| 1186 | case Primitive::kPrimShort: |
| 1187 | case Primitive::kPrimInt: |
| 1188 | case Primitive::kPrimNot: |
| 1189 | locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal()))); |
| 1190 | break; |
| 1191 | |
| 1192 | case Primitive::kPrimLong: |
| 1193 | locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal()))); |
| 1194 | break; |
| 1195 | |
| 1196 | default: |
| 1197 | LOG(FATAL) << "Unimplemented local type " << field_type; |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1202 | UNUSED(store); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | void LocationsBuilderARM64::VisitSub(HSub* instruction) { |
| 1206 | HandleAddSub(instruction); |
| 1207 | } |
| 1208 | |
| 1209 | void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) { |
| 1210 | HandleAddSub(instruction); |
| 1211 | } |
| 1212 | |
| 1213 | void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) { |
| 1214 | LocationSummary* locations = |
| 1215 | new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall); |
| 1216 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1217 | locations->SetInAt(1, Location::RequiresRegister()); |
| 1218 | if (instruction->HasUses()) { |
| 1219 | locations->SetOut(Location::SameAsFirstInput()); |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) { |
| 1224 | LocationSummary* locations = instruction->GetLocations(); |
| 1225 | BoundsCheckSlowPathARM64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64( |
| 1226 | instruction, locations->InAt(0), locations->InAt(1)); |
| 1227 | codegen_->AddSlowPath(slow_path); |
| 1228 | |
| 1229 | __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1)); |
| 1230 | __ B(slow_path->GetEntryLabel(), hs); |
| 1231 | } |
| 1232 | |
| 1233 | void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) { |
| 1234 | new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath); |
| 1235 | } |
| 1236 | |
| 1237 | void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) { |
| 1238 | // TODO: Improve support for suspend checks. |
| 1239 | SuspendCheckSlowPathARM64* slow_path = |
| 1240 | new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, nullptr); |
| 1241 | codegen_->AddSlowPath(slow_path); |
| 1242 | |
| 1243 | __ Subs(wSuspend, wSuspend, 1); |
| 1244 | __ B(slow_path->GetEntryLabel(), le); |
| 1245 | __ Bind(slow_path->GetReturnLabel()); |
| 1246 | } |
| 1247 | |
| 1248 | void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) { |
| 1249 | temp->SetLocations(nullptr); |
| 1250 | } |
| 1251 | |
| 1252 | void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) { |
| 1253 | // Nothing to do, this is driven by the code generator. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1254 | UNUSED(temp); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | } // namespace arm64 |
| 1258 | } // namespace art |