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 | } |
Nicolas Geoffray | f43083d | 2014-11-07 10:48:10 +0000 | [diff] [blame] | 416 | } else if (instruction->IsTemporary()) { |
| 417 | Location temp_location = GetTemporaryLocation(instruction->AsTemporary()); |
| 418 | MoveHelper(location, temp_location, type); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 419 | } else if (instruction->IsLoadLocal()) { |
| 420 | uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal()); |
| 421 | switch (type) { |
| 422 | case Primitive::kPrimNot: |
| 423 | case Primitive::kPrimBoolean: |
| 424 | case Primitive::kPrimByte: |
| 425 | case Primitive::kPrimChar: |
| 426 | case Primitive::kPrimShort: |
| 427 | case Primitive::kPrimInt: |
| 428 | MoveHelper(location, Location::StackSlot(stack_slot), type); |
| 429 | break; |
| 430 | case Primitive::kPrimLong: |
| 431 | MoveHelper(location, Location::DoubleStackSlot(stack_slot), type); |
| 432 | break; |
| 433 | default: |
| 434 | LOG(FATAL) << "Unimplemented type" << type; |
| 435 | } |
| 436 | |
| 437 | } else { |
| 438 | DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary()); |
| 439 | MoveHelper(location, locations->Out(), type); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | size_t CodeGeneratorARM64::FrameEntrySpillSize() const { |
| 444 | return GetFramePreservedRegistersSize(); |
| 445 | } |
| 446 | |
| 447 | Location CodeGeneratorARM64::GetStackLocation(HLoadLocal* load) const { |
| 448 | Primitive::Type type = load->GetType(); |
| 449 | switch (type) { |
| 450 | case Primitive::kPrimNot: |
| 451 | case Primitive::kPrimBoolean: |
| 452 | case Primitive::kPrimByte: |
| 453 | case Primitive::kPrimChar: |
| 454 | case Primitive::kPrimShort: |
| 455 | case Primitive::kPrimInt: |
| 456 | return Location::StackSlot(GetStackSlot(load->GetLocal())); |
| 457 | case Primitive::kPrimLong: |
| 458 | return Location::DoubleStackSlot(GetStackSlot(load->GetLocal())); |
| 459 | case Primitive::kPrimFloat: |
| 460 | case Primitive::kPrimDouble: |
| 461 | LOG(FATAL) << "Unimplemented type " << type; |
| 462 | break; |
| 463 | case Primitive::kPrimVoid: |
| 464 | default: |
| 465 | LOG(FATAL) << "Unexpected type " << type; |
| 466 | } |
| 467 | LOG(FATAL) << "Unreachable"; |
| 468 | return Location::NoLocation(); |
| 469 | } |
| 470 | |
| 471 | void CodeGeneratorARM64::MarkGCCard(Register object, Register value) { |
| 472 | UseScratchRegisterScope temps(assembler_.vixl_masm_); |
| 473 | Register card = temps.AcquireX(); |
| 474 | Register temp = temps.AcquireX(); |
| 475 | vixl::Label done; |
| 476 | __ Cbz(value, &done); |
| 477 | __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value())); |
| 478 | __ Lsr(temp, object, gc::accounting::CardTable::kCardShift); |
| 479 | __ Strb(card, MemOperand(card, temp)); |
| 480 | __ Bind(&done); |
| 481 | } |
| 482 | |
| 483 | void CodeGeneratorARM64::SetupBlockedRegisters() const { |
| 484 | // Block reserved registers: |
| 485 | // ip0 (VIXL temporary) |
| 486 | // ip1 (VIXL temporary) |
| 487 | // xSuspend (Suspend counter) |
| 488 | // lr |
| 489 | // sp is not part of the allocatable registers, so we don't need to block it. |
| 490 | CPURegList reserved_core_registers = vixl_reserved_core_registers; |
| 491 | reserved_core_registers.Combine(runtime_reserved_core_registers); |
| 492 | // TODO: See if we should instead allow allocating but preserve those if used. |
| 493 | reserved_core_registers.Combine(quick_callee_saved_registers); |
| 494 | while (!reserved_core_registers.IsEmpty()) { |
| 495 | blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | Location CodeGeneratorARM64::AllocateFreeRegister(Primitive::Type type) const { |
| 500 | if (type == Primitive::kPrimVoid) { |
| 501 | LOG(FATAL) << "Unreachable type " << type; |
| 502 | } |
| 503 | |
| 504 | if (type == Primitive::kPrimFloat || type == Primitive::kPrimDouble) { |
| 505 | LOG(FATAL) << "Unimplemented support for floating-point"; |
| 506 | } |
| 507 | |
| 508 | ssize_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfXRegisters); |
| 509 | DCHECK_NE(reg, -1); |
| 510 | blocked_core_registers_[reg] = true; |
| 511 | |
| 512 | if (IsFPType(type)) { |
| 513 | return Location::FpuRegisterLocation(reg); |
| 514 | } else { |
| 515 | return Location::RegisterLocation(reg); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const { |
| 520 | stream << Arm64ManagedRegister::FromXRegister(XRegister(reg)); |
| 521 | } |
| 522 | |
| 523 | void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const { |
| 524 | stream << Arm64ManagedRegister::FromDRegister(DRegister(reg)); |
| 525 | } |
| 526 | |
| 527 | #undef __ |
| 528 | #define __ assembler_->vixl_masm_-> |
| 529 | |
| 530 | InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph, |
| 531 | CodeGeneratorARM64* codegen) |
| 532 | : HGraphVisitor(graph), |
| 533 | assembler_(codegen->GetAssembler()), |
| 534 | codegen_(codegen) {} |
| 535 | |
| 536 | #define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \ |
| 537 | M(ArrayGet) \ |
| 538 | M(ArraySet) \ |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 539 | M(ClinitCheck) \ |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 540 | M(DoubleConstant) \ |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 541 | M(Div) \ |
Calin Juravle | d0d4852 | 2014-11-04 16:40:20 +0000 | [diff] [blame] | 542 | M(DivZeroCheck) \ |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 543 | M(FloatConstant) \ |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 544 | M(LoadClass) \ |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 545 | M(LoadException) \ |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 546 | M(LoadString) \ |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 547 | M(Neg) \ |
| 548 | M(NewArray) \ |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 549 | M(ParallelMove) \ |
| 550 | M(StaticFieldGet) \ |
| 551 | M(StaticFieldSet) \ |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 552 | M(Throw) \ |
Roland Levillain | dff1f28 | 2014-11-05 14:15:05 +0000 | [diff] [blame] | 553 | M(TypeConversion) \ |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 554 | |
| 555 | #define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode |
| 556 | |
| 557 | enum UnimplementedInstructionBreakCode { |
| 558 | #define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name), |
| 559 | FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION) |
| 560 | #undef ENUM_UNIMPLEMENTED_INSTRUCTION |
| 561 | }; |
| 562 | |
| 563 | #define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \ |
| 564 | void InstructionCodeGeneratorARM64::Visit##name(H##name* instr) { \ |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 565 | UNUSED(instr); \ |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 566 | __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \ |
| 567 | } \ |
| 568 | void LocationsBuilderARM64::Visit##name(H##name* instr) { \ |
| 569 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \ |
| 570 | locations->SetOut(Location::Any()); \ |
| 571 | } |
| 572 | FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS) |
| 573 | #undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS |
| 574 | |
| 575 | #undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE |
| 576 | |
| 577 | void LocationsBuilderARM64::HandleAddSub(HBinaryOperation* instr) { |
| 578 | DCHECK(instr->IsAdd() || instr->IsSub()); |
| 579 | DCHECK_EQ(instr->InputCount(), 2U); |
| 580 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); |
| 581 | Primitive::Type type = instr->GetResultType(); |
| 582 | switch (type) { |
| 583 | case Primitive::kPrimInt: |
| 584 | case Primitive::kPrimLong: { |
| 585 | locations->SetInAt(0, Location::RequiresRegister()); |
| 586 | locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1))); |
| 587 | locations->SetOut(Location::RequiresRegister()); |
| 588 | break; |
| 589 | } |
| 590 | case Primitive::kPrimBoolean: |
| 591 | case Primitive::kPrimByte: |
| 592 | case Primitive::kPrimChar: |
| 593 | case Primitive::kPrimShort: |
| 594 | LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type; |
| 595 | break; |
| 596 | default: |
| 597 | LOG(FATAL) << "Unimplemented " << instr->DebugName() << " type " << type; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | void InstructionCodeGeneratorARM64::HandleAddSub(HBinaryOperation* instr) { |
| 602 | DCHECK(instr->IsAdd() || instr->IsSub()); |
| 603 | |
| 604 | Primitive::Type type = instr->GetType(); |
| 605 | Register dst = OutputRegister(instr); |
| 606 | Register lhs = InputRegisterAt(instr, 0); |
| 607 | Operand rhs = InputOperandAt(instr, 1); |
| 608 | |
| 609 | switch (type) { |
| 610 | case Primitive::kPrimInt: |
| 611 | case Primitive::kPrimLong: |
| 612 | if (instr->IsAdd()) { |
| 613 | __ Add(dst, lhs, rhs); |
| 614 | } else { |
| 615 | __ Sub(dst, lhs, rhs); |
| 616 | } |
| 617 | break; |
| 618 | |
| 619 | case Primitive::kPrimBoolean: |
| 620 | case Primitive::kPrimByte: |
| 621 | case Primitive::kPrimChar: |
| 622 | case Primitive::kPrimShort: |
| 623 | LOG(FATAL) << "Unexpected add/sub type " << type; |
| 624 | break; |
| 625 | default: |
| 626 | LOG(FATAL) << "Unimplemented add/sub type " << type; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | void LocationsBuilderARM64::VisitAdd(HAdd* instruction) { |
| 631 | HandleAddSub(instruction); |
| 632 | } |
| 633 | |
| 634 | void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) { |
| 635 | HandleAddSub(instruction); |
| 636 | } |
| 637 | |
| 638 | void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) { |
| 639 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
| 640 | locations->SetInAt(0, Location::RequiresRegister()); |
| 641 | locations->SetOut(Location::RequiresRegister()); |
| 642 | } |
| 643 | |
| 644 | void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) { |
| 645 | __ Ldr(OutputRegister(instruction), |
| 646 | HeapOperand(InputRegisterAt(instruction, 0), mirror::Array::LengthOffset())); |
| 647 | } |
| 648 | |
| 649 | void LocationsBuilderARM64::VisitCompare(HCompare* instruction) { |
| 650 | LocationSummary* locations = |
| 651 | new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall); |
| 652 | locations->SetInAt(0, Location::RequiresRegister()); |
| 653 | locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1))); |
| 654 | locations->SetOut(Location::RequiresRegister()); |
| 655 | } |
| 656 | |
| 657 | void InstructionCodeGeneratorARM64::VisitCompare(HCompare* instruction) { |
| 658 | Primitive::Type in_type = instruction->InputAt(0)->GetType(); |
| 659 | |
| 660 | DCHECK_EQ(in_type, Primitive::kPrimLong); |
| 661 | switch (in_type) { |
| 662 | case Primitive::kPrimLong: { |
| 663 | vixl::Label done; |
| 664 | Register result = OutputRegister(instruction); |
| 665 | Register left = InputRegisterAt(instruction, 0); |
| 666 | Operand right = InputOperandAt(instruction, 1); |
| 667 | __ Subs(result, left, right); |
| 668 | __ B(eq, &done); |
| 669 | __ Mov(result, 1); |
| 670 | __ Cneg(result, result, le); |
| 671 | __ Bind(&done); |
| 672 | break; |
| 673 | } |
| 674 | default: |
| 675 | LOG(FATAL) << "Unimplemented compare type " << in_type; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | void LocationsBuilderARM64::VisitCondition(HCondition* instruction) { |
| 680 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
| 681 | locations->SetInAt(0, Location::RequiresRegister()); |
| 682 | locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1))); |
| 683 | if (instruction->NeedsMaterialization()) { |
| 684 | locations->SetOut(Location::RequiresRegister()); |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | void InstructionCodeGeneratorARM64::VisitCondition(HCondition* instruction) { |
| 689 | if (!instruction->NeedsMaterialization()) { |
| 690 | return; |
| 691 | } |
| 692 | |
| 693 | LocationSummary* locations = instruction->GetLocations(); |
| 694 | Register lhs = InputRegisterAt(instruction, 0); |
| 695 | Operand rhs = InputOperandAt(instruction, 1); |
| 696 | Register res = RegisterFrom(locations->Out(), instruction->GetType()); |
| 697 | Condition cond = ARM64Condition(instruction->GetCondition()); |
| 698 | |
| 699 | __ Cmp(lhs, rhs); |
| 700 | __ Csel(res, vixl::Assembler::AppropriateZeroRegFor(res), Operand(1), InvertCondition(cond)); |
| 701 | } |
| 702 | |
| 703 | #define FOR_EACH_CONDITION_INSTRUCTION(M) \ |
| 704 | M(Equal) \ |
| 705 | M(NotEqual) \ |
| 706 | M(LessThan) \ |
| 707 | M(LessThanOrEqual) \ |
| 708 | M(GreaterThan) \ |
| 709 | M(GreaterThanOrEqual) |
| 710 | #define DEFINE_CONDITION_VISITORS(Name) \ |
| 711 | void LocationsBuilderARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } \ |
| 712 | void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { VisitCondition(comp); } |
| 713 | FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS) |
| 714 | #undef FOR_EACH_CONDITION_INSTRUCTION |
| 715 | |
| 716 | void LocationsBuilderARM64::VisitExit(HExit* exit) { |
| 717 | exit->SetLocations(nullptr); |
| 718 | } |
| 719 | |
| 720 | void InstructionCodeGeneratorARM64::VisitExit(HExit* exit) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 721 | UNUSED(exit); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 722 | if (kIsDebugBuild) { |
| 723 | down_cast<Arm64Assembler*>(GetAssembler())->Comment("Unreachable"); |
| 724 | __ Brk(0); // TODO: Introduce special markers for such code locations. |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | void LocationsBuilderARM64::VisitGoto(HGoto* got) { |
| 729 | got->SetLocations(nullptr); |
| 730 | } |
| 731 | |
| 732 | void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) { |
| 733 | HBasicBlock* successor = got->GetSuccessor(); |
| 734 | // TODO: Support for suspend checks emission. |
| 735 | if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) { |
| 736 | __ B(codegen_->GetLabelOf(successor)); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | void LocationsBuilderARM64::VisitIf(HIf* if_instr) { |
| 741 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr); |
| 742 | HInstruction* cond = if_instr->InputAt(0); |
| 743 | DCHECK(cond->IsCondition()); |
| 744 | if (cond->AsCondition()->NeedsMaterialization()) { |
| 745 | locations->SetInAt(0, Location::RequiresRegister()); |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) { |
| 750 | HInstruction* cond = if_instr->InputAt(0); |
| 751 | DCHECK(cond->IsCondition()); |
| 752 | HCondition* condition = cond->AsCondition(); |
| 753 | vixl::Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor()); |
| 754 | vixl::Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor()); |
| 755 | |
| 756 | // TODO: Support constant condition input in VisitIf. |
| 757 | |
| 758 | if (condition->NeedsMaterialization()) { |
| 759 | // The condition instruction has been materialized, compare the output to 0. |
| 760 | Location cond_val = if_instr->GetLocations()->InAt(0); |
| 761 | DCHECK(cond_val.IsRegister()); |
| 762 | __ Cbnz(InputRegisterAt(if_instr, 0), true_target); |
| 763 | |
| 764 | } else { |
| 765 | // The condition instruction has not been materialized, use its inputs as |
| 766 | // the comparison and its condition as the branch condition. |
| 767 | Register lhs = InputRegisterAt(condition, 0); |
| 768 | Operand rhs = InputOperandAt(condition, 1); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 769 | Condition arm64_cond = ARM64Condition(condition->GetCondition()); |
| 770 | if ((arm64_cond == eq || arm64_cond == ne) && rhs.IsImmediate() && (rhs.immediate() == 0)) { |
| 771 | if (arm64_cond == eq) { |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 772 | __ Cbz(lhs, true_target); |
| 773 | } else { |
| 774 | __ Cbnz(lhs, true_target); |
| 775 | } |
| 776 | } else { |
| 777 | __ Cmp(lhs, rhs); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 778 | __ B(arm64_cond, true_target); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 779 | } |
| 780 | } |
| 781 | |
| 782 | if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfFalseSuccessor())) { |
| 783 | __ B(false_target); |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) { |
| 788 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
| 789 | locations->SetInAt(0, Location::RequiresRegister()); |
| 790 | locations->SetOut(Location::RequiresRegister()); |
| 791 | } |
| 792 | |
| 793 | void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) { |
| 794 | Primitive::Type res_type = instruction->GetType(); |
| 795 | Register res = OutputRegister(instruction); |
| 796 | Register obj = InputRegisterAt(instruction, 0); |
| 797 | uint32_t offset = instruction->GetFieldOffset().Uint32Value(); |
| 798 | |
| 799 | switch (res_type) { |
| 800 | case Primitive::kPrimBoolean: { |
| 801 | __ Ldrb(res, MemOperand(obj, offset)); |
| 802 | break; |
| 803 | } |
| 804 | case Primitive::kPrimByte: { |
| 805 | __ Ldrsb(res, MemOperand(obj, offset)); |
| 806 | break; |
| 807 | } |
| 808 | case Primitive::kPrimShort: { |
| 809 | __ Ldrsh(res, MemOperand(obj, offset)); |
| 810 | break; |
| 811 | } |
| 812 | case Primitive::kPrimChar: { |
| 813 | __ Ldrh(res, MemOperand(obj, offset)); |
| 814 | break; |
| 815 | } |
| 816 | case Primitive::kPrimInt: |
| 817 | case Primitive::kPrimNot: |
| 818 | case Primitive::kPrimLong: { // TODO: support volatile. |
| 819 | DCHECK(res.IsX() == (res_type == Primitive::kPrimLong)); |
| 820 | __ Ldr(res, MemOperand(obj, offset)); |
| 821 | break; |
| 822 | } |
| 823 | |
| 824 | case Primitive::kPrimFloat: |
| 825 | case Primitive::kPrimDouble: |
| 826 | LOG(FATAL) << "Unimplemented register res_type " << res_type; |
| 827 | break; |
| 828 | |
| 829 | case Primitive::kPrimVoid: |
| 830 | LOG(FATAL) << "Unreachable res_type " << res_type; |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) { |
| 835 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
| 836 | locations->SetInAt(0, Location::RequiresRegister()); |
| 837 | locations->SetInAt(1, Location::RequiresRegister()); |
| 838 | } |
| 839 | |
| 840 | void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) { |
| 841 | Register obj = InputRegisterAt(instruction, 0); |
| 842 | Register value = InputRegisterAt(instruction, 1); |
| 843 | Primitive::Type field_type = instruction->InputAt(1)->GetType(); |
| 844 | uint32_t offset = instruction->GetFieldOffset().Uint32Value(); |
| 845 | |
| 846 | switch (field_type) { |
| 847 | case Primitive::kPrimBoolean: |
| 848 | case Primitive::kPrimByte: { |
| 849 | __ Strb(value, MemOperand(obj, offset)); |
| 850 | break; |
| 851 | } |
| 852 | |
| 853 | case Primitive::kPrimShort: |
| 854 | case Primitive::kPrimChar: { |
| 855 | __ Strh(value, MemOperand(obj, offset)); |
| 856 | break; |
| 857 | } |
| 858 | |
| 859 | case Primitive::kPrimInt: |
| 860 | case Primitive::kPrimNot: |
| 861 | case Primitive::kPrimLong: { |
| 862 | DCHECK(value.IsX() == (field_type == Primitive::kPrimLong)); |
| 863 | __ Str(value, MemOperand(obj, offset)); |
| 864 | |
| 865 | if (field_type == Primitive::kPrimNot) { |
| 866 | codegen_->MarkGCCard(obj, value); |
| 867 | } |
| 868 | break; |
| 869 | } |
| 870 | |
| 871 | case Primitive::kPrimFloat: |
| 872 | case Primitive::kPrimDouble: |
| 873 | LOG(FATAL) << "Unimplemented register type " << field_type; |
| 874 | break; |
| 875 | |
| 876 | case Primitive::kPrimVoid: |
| 877 | LOG(FATAL) << "Unreachable type " << field_type; |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) { |
| 882 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant); |
| 883 | locations->SetOut(Location::ConstantLocation(constant)); |
| 884 | } |
| 885 | |
| 886 | void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant) { |
| 887 | // Will be generated at use site. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 888 | UNUSED(constant); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | void LocationsBuilderARM64::VisitInvokeStatic(HInvokeStatic* invoke) { |
| 892 | HandleInvoke(invoke); |
| 893 | } |
| 894 | |
| 895 | void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) { |
| 896 | HandleInvoke(invoke); |
| 897 | } |
| 898 | |
| 899 | void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) { |
| 900 | LocationSummary* locations = |
| 901 | new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall); |
| 902 | locations->AddTemp(LocationFrom(x0)); |
| 903 | |
| 904 | InvokeDexCallingConventionVisitor calling_convention_visitor; |
| 905 | for (size_t i = 0; i < invoke->InputCount(); i++) { |
| 906 | HInstruction* input = invoke->InputAt(i); |
| 907 | locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType())); |
| 908 | } |
| 909 | |
| 910 | Primitive::Type return_type = invoke->GetType(); |
| 911 | if (return_type != Primitive::kPrimVoid) { |
| 912 | locations->SetOut(calling_convention_visitor.GetReturnLocation(return_type)); |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | void InstructionCodeGeneratorARM64::VisitInvokeStatic(HInvokeStatic* invoke) { |
| 917 | Register temp = WRegisterFrom(invoke->GetLocations()->GetTemp(0)); |
| 918 | // Make sure that ArtMethod* is passed in W0 as per the calling convention |
| 919 | DCHECK(temp.Is(w0)); |
| 920 | size_t index_in_cache = mirror::Array::DataOffset(kHeapRefSize).SizeValue() + |
| 921 | invoke->GetIndexInDexCache() * kHeapRefSize; |
| 922 | |
| 923 | // TODO: Implement all kinds of calls: |
| 924 | // 1) boot -> boot |
| 925 | // 2) app -> boot |
| 926 | // 3) app -> app |
| 927 | // |
| 928 | // Currently we implement the app -> app logic, which looks up in the resolve cache. |
| 929 | |
| 930 | // temp = method; |
| 931 | __ Ldr(temp, MemOperand(sp, kCurrentMethodStackOffset)); |
| 932 | // temp = temp->dex_cache_resolved_methods_; |
| 933 | __ Ldr(temp, MemOperand(temp.X(), mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue())); |
| 934 | // temp = temp[index_in_cache]; |
| 935 | __ Ldr(temp, MemOperand(temp.X(), index_in_cache)); |
| 936 | // lr = temp->entry_point_from_quick_compiled_code_; |
| 937 | __ Ldr(lr, MemOperand(temp.X(), mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue())); |
| 938 | // lr(); |
| 939 | __ Blr(lr); |
| 940 | |
| 941 | codegen_->RecordPcInfo(invoke, invoke->GetDexPc()); |
| 942 | DCHECK(!codegen_->IsLeafMethod()); |
| 943 | } |
| 944 | |
| 945 | void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) { |
| 946 | LocationSummary* locations = invoke->GetLocations(); |
| 947 | Location receiver = locations->InAt(0); |
| 948 | Register temp = XRegisterFrom(invoke->GetLocations()->GetTemp(0)); |
| 949 | size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() + |
| 950 | invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry); |
| 951 | Offset class_offset = mirror::Object::ClassOffset(); |
| 952 | Offset entry_point = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(); |
| 953 | |
| 954 | // temp = object->GetClass(); |
| 955 | if (receiver.IsStackSlot()) { |
| 956 | __ Ldr(temp.W(), MemOperand(sp, receiver.GetStackIndex())); |
| 957 | __ Ldr(temp.W(), MemOperand(temp, class_offset.SizeValue())); |
| 958 | } else { |
| 959 | DCHECK(receiver.IsRegister()); |
| 960 | __ Ldr(temp.W(), HeapOperandFrom(receiver, Primitive::kPrimNot, |
| 961 | class_offset)); |
| 962 | } |
| 963 | // temp = temp->GetMethodAt(method_offset); |
| 964 | __ Ldr(temp.W(), MemOperand(temp, method_offset)); |
| 965 | // lr = temp->GetEntryPoint(); |
| 966 | __ Ldr(lr, MemOperand(temp, entry_point.SizeValue())); |
| 967 | // lr(); |
| 968 | __ Blr(lr); |
| 969 | DCHECK(!codegen_->IsLeafMethod()); |
| 970 | codegen_->RecordPcInfo(invoke, invoke->GetDexPc()); |
| 971 | } |
| 972 | |
| 973 | void LocationsBuilderARM64::VisitLoadLocal(HLoadLocal* load) { |
| 974 | load->SetLocations(nullptr); |
| 975 | } |
| 976 | |
| 977 | void InstructionCodeGeneratorARM64::VisitLoadLocal(HLoadLocal* load) { |
| 978 | // Nothing to do, this is driven by the code generator. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 979 | UNUSED(load); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | void LocationsBuilderARM64::VisitLocal(HLocal* local) { |
| 983 | local->SetLocations(nullptr); |
| 984 | } |
| 985 | |
| 986 | void InstructionCodeGeneratorARM64::VisitLocal(HLocal* local) { |
| 987 | DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock()); |
| 988 | } |
| 989 | |
| 990 | void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) { |
| 991 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant); |
| 992 | locations->SetOut(Location::ConstantLocation(constant)); |
| 993 | } |
| 994 | |
| 995 | void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant) { |
| 996 | // Will be generated at use site. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 997 | UNUSED(constant); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 998 | } |
| 999 | |
Alexandre Rames | 42d641b | 2014-10-27 14:00:51 +0000 | [diff] [blame] | 1000 | void LocationsBuilderARM64::VisitMul(HMul* mul) { |
| 1001 | LocationSummary* locations = |
| 1002 | new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall); |
| 1003 | switch (mul->GetResultType()) { |
| 1004 | case Primitive::kPrimInt: |
| 1005 | case Primitive::kPrimLong: |
| 1006 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1007 | locations->SetInAt(1, Location::RequiresRegister()); |
| 1008 | locations->SetOut(Location::RequiresRegister()); |
| 1009 | break; |
| 1010 | |
| 1011 | case Primitive::kPrimFloat: |
| 1012 | case Primitive::kPrimDouble: |
| 1013 | LOG(FATAL) << "Unimplemented mul type " << mul->GetResultType(); |
| 1014 | break; |
| 1015 | |
| 1016 | default: |
| 1017 | LOG(FATAL) << "Unexpected mul type " << mul->GetResultType(); |
| 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) { |
| 1022 | switch (mul->GetResultType()) { |
| 1023 | case Primitive::kPrimInt: |
| 1024 | case Primitive::kPrimLong: |
| 1025 | __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1)); |
| 1026 | break; |
| 1027 | |
| 1028 | case Primitive::kPrimFloat: |
| 1029 | case Primitive::kPrimDouble: |
| 1030 | LOG(FATAL) << "Unimplemented mul type " << mul->GetResultType(); |
| 1031 | break; |
| 1032 | |
| 1033 | default: |
| 1034 | LOG(FATAL) << "Unexpected mul type " << mul->GetResultType(); |
| 1035 | } |
| 1036 | } |
| 1037 | |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 1038 | void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) { |
| 1039 | LocationSummary* locations = |
| 1040 | new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall); |
| 1041 | InvokeRuntimeCallingConvention calling_convention; |
| 1042 | locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0))); |
| 1043 | locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(1))); |
| 1044 | locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot)); |
| 1045 | } |
| 1046 | |
| 1047 | void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) { |
| 1048 | LocationSummary* locations = instruction->GetLocations(); |
| 1049 | Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt); |
| 1050 | DCHECK(type_index.Is(w0)); |
| 1051 | Register current_method = RegisterFrom(locations->GetTemp(1), Primitive::kPrimNot); |
| 1052 | DCHECK(current_method.Is(w1)); |
| 1053 | __ Ldr(current_method, MemOperand(sp, kCurrentMethodStackOffset)); |
| 1054 | __ Mov(type_index, instruction->GetTypeIndex()); |
| 1055 | __ Ldr(lr, MemOperand(tr, QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, pAllocObjectWithAccessCheck).Int32Value())); |
| 1056 | __ Blr(lr); |
| 1057 | codegen_->RecordPcInfo(instruction, instruction->GetDexPc()); |
| 1058 | DCHECK(!codegen_->IsLeafMethod()); |
| 1059 | } |
| 1060 | |
| 1061 | void LocationsBuilderARM64::VisitNot(HNot* instruction) { |
| 1062 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
| 1063 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1064 | locations->SetOut(Location::RequiresRegister()); |
| 1065 | } |
| 1066 | |
| 1067 | void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) { |
| 1068 | switch (instruction->InputAt(0)->GetType()) { |
| 1069 | case Primitive::kPrimBoolean: |
| 1070 | __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), Operand(1)); |
| 1071 | break; |
| 1072 | |
| 1073 | case Primitive::kPrimInt: |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 1074 | case Primitive::kPrimLong: |
Roland Levillain | 55dcfb5 | 2014-10-24 18:09:09 +0100 | [diff] [blame] | 1075 | __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0)); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 1076 | break; |
| 1077 | |
| 1078 | default: |
| 1079 | LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType(); |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) { |
| 1084 | LocationSummary* locations = |
| 1085 | new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall); |
| 1086 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1087 | if (instruction->HasUses()) { |
| 1088 | locations->SetOut(Location::SameAsFirstInput()); |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) { |
| 1093 | SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction); |
| 1094 | codegen_->AddSlowPath(slow_path); |
| 1095 | |
| 1096 | LocationSummary* locations = instruction->GetLocations(); |
| 1097 | Location obj = locations->InAt(0); |
| 1098 | if (obj.IsRegister()) { |
| 1099 | __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel()); |
| 1100 | } else { |
| 1101 | DCHECK(obj.IsConstant()) << obj; |
| 1102 | DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0); |
| 1103 | __ B(slow_path->GetEntryLabel()); |
| 1104 | } |
| 1105 | } |
| 1106 | |
| 1107 | void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) { |
| 1108 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
| 1109 | Location location = parameter_visitor_.GetNextLocation(instruction->GetType()); |
| 1110 | if (location.IsStackSlot()) { |
| 1111 | location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize()); |
| 1112 | } else if (location.IsDoubleStackSlot()) { |
| 1113 | location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize()); |
| 1114 | } |
| 1115 | locations->SetOut(location); |
| 1116 | } |
| 1117 | |
| 1118 | void InstructionCodeGeneratorARM64::VisitParameterValue(HParameterValue* instruction) { |
| 1119 | // Nothing to do, the parameter is already at its location. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1120 | UNUSED(instruction); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | void LocationsBuilderARM64::VisitPhi(HPhi* instruction) { |
| 1124 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
| 1125 | for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) { |
| 1126 | locations->SetInAt(i, Location::Any()); |
| 1127 | } |
| 1128 | locations->SetOut(Location::Any()); |
| 1129 | } |
| 1130 | |
| 1131 | void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1132 | UNUSED(instruction); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 1133 | LOG(FATAL) << "Unreachable"; |
| 1134 | } |
| 1135 | |
| 1136 | void LocationsBuilderARM64::VisitReturn(HReturn* instruction) { |
| 1137 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction); |
| 1138 | Primitive::Type return_type = instruction->InputAt(0)->GetType(); |
| 1139 | |
| 1140 | if (return_type == Primitive::kPrimFloat || return_type == Primitive::kPrimDouble) { |
| 1141 | LOG(FATAL) << "Unimplemented return type " << return_type; |
| 1142 | } |
| 1143 | |
| 1144 | locations->SetInAt(0, LocationFrom(x0)); |
| 1145 | } |
| 1146 | |
| 1147 | void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction) { |
| 1148 | if (kIsDebugBuild) { |
| 1149 | Primitive::Type type = instruction->InputAt(0)->GetType(); |
| 1150 | switch (type) { |
| 1151 | case Primitive::kPrimBoolean: |
| 1152 | case Primitive::kPrimByte: |
| 1153 | case Primitive::kPrimChar: |
| 1154 | case Primitive::kPrimShort: |
| 1155 | case Primitive::kPrimInt: |
| 1156 | case Primitive::kPrimNot: |
| 1157 | DCHECK(InputRegisterAt(instruction, 0).Is(w0)); |
| 1158 | break; |
| 1159 | |
| 1160 | case Primitive::kPrimLong: |
| 1161 | DCHECK(InputRegisterAt(instruction, 0).Is(x0)); |
| 1162 | break; |
| 1163 | |
| 1164 | default: |
| 1165 | LOG(FATAL) << "Unimplemented return type " << type; |
| 1166 | } |
| 1167 | } |
| 1168 | codegen_->GenerateFrameExit(); |
| 1169 | __ Br(lr); |
| 1170 | } |
| 1171 | |
| 1172 | void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) { |
| 1173 | instruction->SetLocations(nullptr); |
| 1174 | } |
| 1175 | |
| 1176 | void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1177 | UNUSED(instruction); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 1178 | codegen_->GenerateFrameExit(); |
| 1179 | __ Br(lr); |
| 1180 | } |
| 1181 | |
| 1182 | void LocationsBuilderARM64::VisitStoreLocal(HStoreLocal* store) { |
| 1183 | LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store); |
| 1184 | Primitive::Type field_type = store->InputAt(1)->GetType(); |
| 1185 | switch (field_type) { |
| 1186 | case Primitive::kPrimBoolean: |
| 1187 | case Primitive::kPrimByte: |
| 1188 | case Primitive::kPrimChar: |
| 1189 | case Primitive::kPrimShort: |
| 1190 | case Primitive::kPrimInt: |
| 1191 | case Primitive::kPrimNot: |
| 1192 | locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal()))); |
| 1193 | break; |
| 1194 | |
| 1195 | case Primitive::kPrimLong: |
| 1196 | locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal()))); |
| 1197 | break; |
| 1198 | |
| 1199 | default: |
| 1200 | LOG(FATAL) << "Unimplemented local type " << field_type; |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | void InstructionCodeGeneratorARM64::VisitStoreLocal(HStoreLocal* store) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1205 | UNUSED(store); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | void LocationsBuilderARM64::VisitSub(HSub* instruction) { |
| 1209 | HandleAddSub(instruction); |
| 1210 | } |
| 1211 | |
| 1212 | void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) { |
| 1213 | HandleAddSub(instruction); |
| 1214 | } |
| 1215 | |
| 1216 | void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) { |
| 1217 | LocationSummary* locations = |
| 1218 | new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall); |
| 1219 | locations->SetInAt(0, Location::RequiresRegister()); |
| 1220 | locations->SetInAt(1, Location::RequiresRegister()); |
| 1221 | if (instruction->HasUses()) { |
| 1222 | locations->SetOut(Location::SameAsFirstInput()); |
| 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) { |
| 1227 | LocationSummary* locations = instruction->GetLocations(); |
| 1228 | BoundsCheckSlowPathARM64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64( |
| 1229 | instruction, locations->InAt(0), locations->InAt(1)); |
| 1230 | codegen_->AddSlowPath(slow_path); |
| 1231 | |
| 1232 | __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1)); |
| 1233 | __ B(slow_path->GetEntryLabel(), hs); |
| 1234 | } |
| 1235 | |
| 1236 | void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) { |
| 1237 | new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath); |
| 1238 | } |
| 1239 | |
| 1240 | void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) { |
| 1241 | // TODO: Improve support for suspend checks. |
| 1242 | SuspendCheckSlowPathARM64* slow_path = |
| 1243 | new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, nullptr); |
| 1244 | codegen_->AddSlowPath(slow_path); |
| 1245 | |
| 1246 | __ Subs(wSuspend, wSuspend, 1); |
| 1247 | __ B(slow_path->GetEntryLabel(), le); |
| 1248 | __ Bind(slow_path->GetReturnLabel()); |
| 1249 | } |
| 1250 | |
| 1251 | void LocationsBuilderARM64::VisitTemporary(HTemporary* temp) { |
| 1252 | temp->SetLocations(nullptr); |
| 1253 | } |
| 1254 | |
| 1255 | void InstructionCodeGeneratorARM64::VisitTemporary(HTemporary* temp) { |
| 1256 | // Nothing to do, this is driven by the code generator. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1257 | UNUSED(temp); |
Alexandre Rames | 5319def | 2014-10-23 10:03:10 +0100 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | } // namespace arm64 |
| 1261 | } // namespace art |