blob: 21b21f39d4f32336fdac029bef80c4903a4627bf [file] [log] [blame]
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86_64.h"
18
19#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010020#include "gc/accounting/card_table.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070021#include "mirror/array-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010022#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010023#include "mirror/class.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010024#include "mirror/object_reference.h"
25#include "thread.h"
26#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010027#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010028#include "utils/x86_64/assembler_x86_64.h"
29#include "utils/x86_64/managed_register_x86_64.h"
30
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010031namespace art {
32
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010033namespace x86_64 {
34
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010035static constexpr bool kExplicitStackOverflowCheck = false;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010036
37// Some x86_64 instructions require a register to be available as temp.
38static constexpr Register TMP = R11;
39
40static constexpr int kNumberOfPushedRegistersAtEntry = 1;
41static constexpr int kCurrentMethodStackOffset = 0;
42
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010043static constexpr Register kRuntimeParameterCoreRegisters[] = { RDI, RSI, RDX };
44static constexpr size_t kRuntimeParameterCoreRegistersLength =
45 arraysize(kRuntimeParameterCoreRegisters);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010046static constexpr FloatRegister kRuntimeParameterFpuRegisters[] = { };
47static constexpr size_t kRuntimeParameterFpuRegistersLength = 0;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010048
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010049class InvokeRuntimeCallingConvention : public CallingConvention<Register, FloatRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010050 public:
51 InvokeRuntimeCallingConvention()
52 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010053 kRuntimeParameterCoreRegistersLength,
54 kRuntimeParameterFpuRegisters,
55 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010056
57 private:
58 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
59};
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010060
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061#define __ reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler())->
62
63class NullCheckSlowPathX86_64 : public SlowPathCode {
64 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010065 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010066
67 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
68 __ Bind(GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010069 __ gs()->call(
70 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowNullPointer), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +010071 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010072 }
73
74 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010075 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
77};
78
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010079class StackOverflowCheckSlowPathX86_64 : public SlowPathCode {
80 public:
81 StackOverflowCheckSlowPathX86_64() {}
82
83 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
84 __ Bind(GetEntryLabel());
85 __ addq(CpuRegister(RSP),
86 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
87 __ gs()->jmp(
88 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowStackOverflow), true));
89 }
90
91 private:
92 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86_64);
93};
94
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000095class SuspendCheckSlowPathX86_64 : public SlowPathCode {
96 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +010097 explicit SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
98 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000099
100 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
101 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100102 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000103 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pTestSuspend), true));
104 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100105 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100106 if (successor_ == nullptr) {
107 __ jmp(GetReturnLabel());
108 } else {
109 __ jmp(codegen->GetLabelOf(successor_));
110 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000111 }
112
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100113 Label* GetReturnLabel() {
114 DCHECK(successor_ == nullptr);
115 return &return_label_;
116 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000117
118 private:
119 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100120 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000121 Label return_label_;
122
123 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
124};
125
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100126class BoundsCheckSlowPathX86_64 : public SlowPathCode {
127 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100128 BoundsCheckSlowPathX86_64(HBoundsCheck* instruction,
129 Location index_location,
130 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100131 : instruction_(instruction),
132 index_location_(index_location),
133 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100134
135 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
136 CodeGeneratorX86_64* x64_codegen = reinterpret_cast<CodeGeneratorX86_64*>(codegen);
137 __ Bind(GetEntryLabel());
138 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100139 x64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), index_location_);
140 x64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(1)), length_location_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100141 __ gs()->call(Address::Absolute(
142 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowArrayBounds), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100143 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100144 }
145
146 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100147 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100148 const Location index_location_;
149 const Location length_location_;
150
151 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
152};
153
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100154#undef __
155#define __ reinterpret_cast<X86_64Assembler*>(GetAssembler())->
156
Dave Allison20dfc792014-06-16 20:44:29 -0700157inline Condition X86_64Condition(IfCondition cond) {
158 switch (cond) {
159 case kCondEQ: return kEqual;
160 case kCondNE: return kNotEqual;
161 case kCondLT: return kLess;
162 case kCondLE: return kLessEqual;
163 case kCondGT: return kGreater;
164 case kCondGE: return kGreaterEqual;
165 default:
166 LOG(FATAL) << "Unknown if condition";
167 }
168 return kEqual;
169}
170
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100171void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
172 stream << X86_64ManagedRegister::FromCpuRegister(Register(reg));
173}
174
175void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
176 stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg));
177}
178
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100179void CodeGeneratorX86_64::SaveCoreRegister(Location stack_location, uint32_t reg_id) {
180 __ movq(Address(CpuRegister(RSP), stack_location.GetStackIndex()), CpuRegister(reg_id));
181}
182
183void CodeGeneratorX86_64::RestoreCoreRegister(Location stack_location, uint32_t reg_id) {
184 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_location.GetStackIndex()));
185}
186
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100187CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100188 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfFloatRegisters, 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100189 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000190 instruction_visitor_(graph, this),
191 move_resolver_(graph->GetArena(), this) {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100192
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100193size_t CodeGeneratorX86_64::FrameEntrySpillSize() const {
194 return kNumberOfPushedRegistersAtEntry * kX86_64WordSize;
195}
196
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100197InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
198 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100199 : HGraphVisitor(graph),
200 assembler_(codegen->GetAssembler()),
201 codegen_(codegen) {}
202
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100203Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100204 switch (type) {
205 case Primitive::kPrimLong:
206 case Primitive::kPrimByte:
207 case Primitive::kPrimBoolean:
208 case Primitive::kPrimChar:
209 case Primitive::kPrimShort:
210 case Primitive::kPrimInt:
211 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100212 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100213 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100214 }
215
216 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100217 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100218 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100219 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100220 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100221
222 case Primitive::kPrimVoid:
223 LOG(FATAL) << "Unreachable type " << type;
224 }
225
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100226 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100227}
228
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100229void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100230 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100231 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100232
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000233 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100234 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000235
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100236 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100237 blocked_core_registers_[RBX] = true;
238 blocked_core_registers_[RBP] = true;
239 blocked_core_registers_[R12] = true;
240 blocked_core_registers_[R13] = true;
241 blocked_core_registers_[R14] = true;
242 blocked_core_registers_[R15] = true;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100243
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100244 blocked_fpu_registers_[XMM12] = true;
245 blocked_fpu_registers_[XMM13] = true;
246 blocked_fpu_registers_[XMM14] = true;
247 blocked_fpu_registers_[XMM15] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100248}
249
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100250void CodeGeneratorX86_64::GenerateFrameEntry() {
251 // Create a fake register to mimic Quick.
252 static const int kFakeReturnRegister = 16;
253 core_spill_mask_ |= (1 << kFakeReturnRegister);
254
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100255 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700256 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100257
258 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
259 __ testq(CpuRegister(RAX), Address(
260 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100261 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100262 }
263
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100264 // The return PC has already been pushed on the stack.
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100265 __ subq(CpuRegister(RSP),
266 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
267
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100268 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
269 SlowPathCode* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86_64();
270 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100271
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100272 __ gs()->cmpq(CpuRegister(RSP),
273 Address::Absolute(Thread::StackEndOffset<kX86_64WordSize>(), true));
274 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100275 }
276
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100277 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
278}
279
280void CodeGeneratorX86_64::GenerateFrameExit() {
281 __ addq(CpuRegister(RSP),
282 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
283}
284
285void CodeGeneratorX86_64::Bind(Label* label) {
286 __ Bind(label);
287}
288
289void InstructionCodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
290 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
291}
292
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100293Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
294 switch (load->GetType()) {
295 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100296 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100297 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
298 break;
299
300 case Primitive::kPrimInt:
301 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100302 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100303 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100304
305 case Primitive::kPrimBoolean:
306 case Primitive::kPrimByte:
307 case Primitive::kPrimChar:
308 case Primitive::kPrimShort:
309 case Primitive::kPrimVoid:
310 LOG(FATAL) << "Unexpected type " << load->GetType();
311 }
312
313 LOG(FATAL) << "Unreachable";
314 return Location();
315}
316
317void CodeGeneratorX86_64::Move(Location destination, Location source) {
318 if (source.Equals(destination)) {
319 return;
320 }
321 if (destination.IsRegister()) {
322 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100323 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100324 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100325 __ movd(destination.As<CpuRegister>(), source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100326 } else if (source.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100327 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100328 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100329 } else {
330 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100331 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100332 Address(CpuRegister(RSP), source.GetStackIndex()));
333 }
334 } else if (destination.IsFpuRegister()) {
335 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100336 __ movd(destination.As<XmmRegister>(), source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100337 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100338 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100339 } else if (source.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100340 __ movss(destination.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100341 Address(CpuRegister(RSP), source.GetStackIndex()));
342 } else {
343 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100344 __ movsd(destination.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100345 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100346 }
347 } else if (destination.IsStackSlot()) {
348 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100349 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100350 source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100351 } else if (source.IsFpuRegister()) {
352 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100353 source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100354 } else {
355 DCHECK(source.IsStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000356 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
357 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100358 }
359 } else {
360 DCHECK(destination.IsDoubleStackSlot());
361 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100362 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100363 source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100364 } else if (source.IsFpuRegister()) {
365 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100366 source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100367 } else {
368 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000369 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
370 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100371 }
372 }
373}
374
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100375void CodeGeneratorX86_64::Move(HInstruction* instruction,
376 Location location,
377 HInstruction* move_for) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100378 if (instruction->AsIntConstant() != nullptr) {
379 Immediate imm(instruction->AsIntConstant()->GetValue());
380 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100381 __ movl(location.As<CpuRegister>(), imm);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100382 } else {
383 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
384 }
385 } else if (instruction->AsLongConstant() != nullptr) {
386 int64_t value = instruction->AsLongConstant()->GetValue();
387 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100388 __ movq(location.As<CpuRegister>(), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100389 } else {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000390 __ movq(CpuRegister(TMP), Immediate(value));
391 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100392 }
393 } else if (instruction->AsLoadLocal() != nullptr) {
394 switch (instruction->GetType()) {
395 case Primitive::kPrimBoolean:
396 case Primitive::kPrimByte:
397 case Primitive::kPrimChar:
398 case Primitive::kPrimShort:
399 case Primitive::kPrimInt:
400 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100401 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100402 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
403 break;
404
405 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100406 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100407 Move(location, Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
408 break;
409
410 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100411 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100412 }
413 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100414 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100415 switch (instruction->GetType()) {
416 case Primitive::kPrimBoolean:
417 case Primitive::kPrimByte:
418 case Primitive::kPrimChar:
419 case Primitive::kPrimShort:
420 case Primitive::kPrimInt:
421 case Primitive::kPrimNot:
422 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100423 case Primitive::kPrimFloat:
424 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100425 Move(location, instruction->GetLocations()->Out());
426 break;
427
428 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100429 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100430 }
431 }
432}
433
434void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
435 got->SetLocations(nullptr);
436}
437
438void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
439 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100440 DCHECK(!successor->IsExitBlock());
441
442 HBasicBlock* block = got->GetBlock();
443 HInstruction* previous = got->GetPrevious();
444
445 HLoopInformation* info = block->GetLoopInformation();
446 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
447 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
448 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
449 return;
450 }
451
452 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
453 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
454 }
455 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100456 __ jmp(codegen_->GetLabelOf(successor));
457 }
458}
459
460void LocationsBuilderX86_64::VisitExit(HExit* exit) {
461 exit->SetLocations(nullptr);
462}
463
464void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
465 if (kIsDebugBuild) {
466 __ Comment("Unreachable");
467 __ int3();
468 }
469}
470
471void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100472 LocationSummary* locations =
473 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100474 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100475 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +0100476 locations->SetInAt(0, Location::Any(), Location::kDiesAtEntry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100477 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100478}
479
480void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700481 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100482 bool materialized = !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
483 // Moves do not affect the eflags register, so if the condition is evaluated
484 // just before the if, we don't need to evaluate it again.
485 bool eflags_set = cond->IsCondition()
486 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
487 if (materialized) {
488 if (!eflags_set) {
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100489 // Materialized condition, compare against 0.
490 Location lhs = if_instr->GetLocations()->InAt(0);
491 if (lhs.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100492 __ cmpl(lhs.As<CpuRegister>(), Immediate(0));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100493 } else {
494 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
495 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100496 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
497 } else {
498 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
499 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700500 }
Dave Allison20dfc792014-06-16 20:44:29 -0700501 } else {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100502 Location lhs = cond->GetLocations()->InAt(0);
503 Location rhs = cond->GetLocations()->InAt(1);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100504 if (rhs.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100505 __ cmpl(lhs.As<CpuRegister>(), rhs.As<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100506 } else if (rhs.IsConstant()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100507 __ cmpl(lhs.As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100508 Immediate(rhs.GetConstant()->AsIntConstant()->GetValue()));
509 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100510 __ cmpl(lhs.As<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100511 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100512 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
Dave Allison20dfc792014-06-16 20:44:29 -0700513 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
514 }
515 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfFalseSuccessor())) {
516 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100517 }
518}
519
520void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
521 local->SetLocations(nullptr);
522}
523
524void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
525 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
526}
527
528void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
529 local->SetLocations(nullptr);
530}
531
532void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
533 // Nothing to do, this is driven by the code generator.
534}
535
536void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100537 LocationSummary* locations =
538 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100539 switch (store->InputAt(1)->GetType()) {
540 case Primitive::kPrimBoolean:
541 case Primitive::kPrimByte:
542 case Primitive::kPrimChar:
543 case Primitive::kPrimShort:
544 case Primitive::kPrimInt:
545 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100546 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100547 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
548 break;
549
550 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100551 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100552 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
553 break;
554
555 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100556 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100557 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100558}
559
560void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
561}
562
Dave Allison20dfc792014-06-16 20:44:29 -0700563void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100564 LocationSummary* locations =
565 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +0100566 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
567 locations->SetInAt(1, Location::Any(), Location::kDiesAtEntry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100568 if (comp->NeedsMaterialization()) {
569 locations->SetOut(Location::RequiresRegister());
570 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100571}
572
Dave Allison20dfc792014-06-16 20:44:29 -0700573void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
574 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100575 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100576 CpuRegister reg = locations->Out().As<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100577 // Clear register: setcc only sets the low byte.
578 __ xorq(reg, reg);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100579 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100580 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100581 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100582 } else if (locations->InAt(1).IsConstant()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100583 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100584 Immediate(locations->InAt(1).GetConstant()->AsIntConstant()->GetValue()));
585 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100586 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100587 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
588 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100589 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700590 }
591}
592
593void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
594 VisitCondition(comp);
595}
596
597void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
598 VisitCondition(comp);
599}
600
601void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
602 VisitCondition(comp);
603}
604
605void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
606 VisitCondition(comp);
607}
608
609void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
610 VisitCondition(comp);
611}
612
613void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
614 VisitCondition(comp);
615}
616
617void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
618 VisitCondition(comp);
619}
620
621void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
622 VisitCondition(comp);
623}
624
625void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
626 VisitCondition(comp);
627}
628
629void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
630 VisitCondition(comp);
631}
632
633void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
634 VisitCondition(comp);
635}
636
637void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
638 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100639}
640
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100641void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100642 LocationSummary* locations =
643 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +0100644 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
645 locations->SetInAt(1, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100646 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100647}
648
649void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
650 Label greater, done;
651 LocationSummary* locations = compare->GetLocations();
652 switch (compare->InputAt(0)->GetType()) {
653 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100654 __ cmpq(locations->InAt(0).As<CpuRegister>(),
655 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100656 break;
657 default:
658 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
659 }
660
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100661 CpuRegister output = locations->Out().As<CpuRegister>();
662 __ movl(output, Immediate(0));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100663 __ j(kEqual, &done);
664 __ j(kGreater, &greater);
665
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100666 __ movl(output, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100667 __ jmp(&done);
668
669 __ Bind(&greater);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100670 __ movl(output, Immediate(1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100671
672 __ Bind(&done);
673}
674
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100675void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100676 LocationSummary* locations =
677 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100678 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100679}
680
681void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100682}
683
684void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100685 LocationSummary* locations =
686 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100687 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100688}
689
690void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100691}
692
693void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
694 ret->SetLocations(nullptr);
695}
696
697void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
698 codegen_->GenerateFrameExit();
699 __ ret();
700}
701
702void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100703 LocationSummary* locations =
704 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100705 switch (ret->InputAt(0)->GetType()) {
706 case Primitive::kPrimBoolean:
707 case Primitive::kPrimByte:
708 case Primitive::kPrimChar:
709 case Primitive::kPrimShort:
710 case Primitive::kPrimInt:
711 case Primitive::kPrimNot:
712 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100713 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100714 break;
715
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100716 case Primitive::kPrimFloat:
717 case Primitive::kPrimDouble:
718 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100719 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100720 break;
721
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100722 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100723 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100724 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100725}
726
727void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
728 if (kIsDebugBuild) {
729 switch (ret->InputAt(0)->GetType()) {
730 case Primitive::kPrimBoolean:
731 case Primitive::kPrimByte:
732 case Primitive::kPrimChar:
733 case Primitive::kPrimShort:
734 case Primitive::kPrimInt:
735 case Primitive::kPrimNot:
736 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100737 DCHECK_EQ(ret->GetLocations()->InAt(0).As<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100738 break;
739
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100740 case Primitive::kPrimFloat:
741 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100742 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100743 XMM0);
744 break;
745
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100746 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100747 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100748 }
749 }
750 codegen_->GenerateFrameExit();
751 __ ret();
752}
753
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100754Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
755 switch (type) {
756 case Primitive::kPrimBoolean:
757 case Primitive::kPrimByte:
758 case Primitive::kPrimChar:
759 case Primitive::kPrimShort:
760 case Primitive::kPrimInt:
761 case Primitive::kPrimNot: {
762 uint32_t index = gp_index_++;
763 stack_index_++;
764 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100765 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100766 } else {
767 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
768 }
769 }
770
771 case Primitive::kPrimLong: {
772 uint32_t index = gp_index_;
773 stack_index_ += 2;
774 if (index < calling_convention.GetNumberOfRegisters()) {
775 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100776 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100777 } else {
778 gp_index_ += 2;
779 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
780 }
781 }
782
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100783 case Primitive::kPrimFloat: {
784 uint32_t index = fp_index_++;
785 stack_index_++;
786 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100787 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100788 } else {
789 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
790 }
791 }
792
793 case Primitive::kPrimDouble: {
794 uint32_t index = fp_index_++;
795 stack_index_ += 2;
796 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100797 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100798 } else {
799 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
800 }
801 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100802
803 case Primitive::kPrimVoid:
804 LOG(FATAL) << "Unexpected parameter type " << type;
805 break;
806 }
807 return Location();
808}
809
810void LocationsBuilderX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100811 HandleInvoke(invoke);
812}
813
814void InstructionCodeGeneratorX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100815 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100816 uint32_t heap_reference_size = sizeof(mirror::HeapReference<mirror::Object>);
817 size_t index_in_cache = mirror::Array::DataOffset(heap_reference_size).SizeValue() +
818 invoke->GetIndexInDexCache() * heap_reference_size;
819
820 // TODO: Implement all kinds of calls:
821 // 1) boot -> boot
822 // 2) app -> boot
823 // 3) app -> app
824 //
825 // Currently we implement the app -> app logic, which looks up in the resolve cache.
826
827 // temp = method;
828 LoadCurrentMethod(temp);
829 // temp = temp->dex_cache_resolved_methods_;
830 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
831 // temp = temp[index_in_cache]
832 __ movl(temp, Address(temp, index_in_cache));
833 // (temp + offset_of_quick_compiled_code)()
834 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
835
836 DCHECK(!codegen_->IsLeafMethod());
837 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
838}
839
840void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
841 HandleInvoke(invoke);
842}
843
844void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100845 LocationSummary* locations =
846 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100847 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100848
849 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100850 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100851 HInstruction* input = invoke->InputAt(i);
852 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
853 }
854
855 switch (invoke->GetType()) {
856 case Primitive::kPrimBoolean:
857 case Primitive::kPrimByte:
858 case Primitive::kPrimChar:
859 case Primitive::kPrimShort:
860 case Primitive::kPrimInt:
861 case Primitive::kPrimNot:
862 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100863 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100864 break;
865
866 case Primitive::kPrimVoid:
867 break;
868
869 case Primitive::kPrimDouble:
870 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100871 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100872 break;
873 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100874}
875
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100876void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100877 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100878 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
879 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
880 LocationSummary* locations = invoke->GetLocations();
881 Location receiver = locations->InAt(0);
882 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
883 // temp = object->GetClass();
884 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100885 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
886 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100887 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100888 __ movl(temp, Address(receiver.As<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100889 }
890 // temp = temp->GetMethodAt(method_offset);
891 __ movl(temp, Address(temp, method_offset));
892 // call temp->GetEntryPoint();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100893 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
894
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100895 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100896 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100897}
898
899void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100900 LocationSummary* locations =
901 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100902 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100903 case Primitive::kPrimInt: {
904 locations->SetInAt(0, Location::RequiresRegister());
905 locations->SetInAt(1, Location::Any());
906 locations->SetOut(Location::SameAsFirstInput());
907 break;
908 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100909
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100910 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000911 locations->SetInAt(0, Location::RequiresRegister());
912 locations->SetInAt(1, Location::RequiresRegister());
913 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100914 break;
915 }
916
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100917 case Primitive::kPrimDouble:
918 case Primitive::kPrimFloat: {
919 locations->SetInAt(0, Location::RequiresFpuRegister());
920 locations->SetInAt(1, Location::Any());
921 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100922 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100923 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100924
925 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100926 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100927 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100928}
929
930void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
931 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100932 Location first = locations->InAt(0);
933 Location second = locations->InAt(1);
934
935 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100936 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000937 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100938 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100939 __ addl(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100940 } else if (second.IsConstant()) {
941 HConstant* instruction = second.GetConstant();
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100942 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100943 __ addl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100944 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100945 __ addl(first.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100946 Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100947 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000948 break;
949 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100950
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100951 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100952 __ addq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100953 break;
954 }
955
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100956 case Primitive::kPrimFloat: {
957 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100958 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100959 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100960 __ addss(first.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100961 Address(CpuRegister(RSP), second.GetStackIndex()));
962 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100963 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100964 }
965
966 case Primitive::kPrimDouble: {
967 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100968 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100969 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100970 __ addsd(first.As<XmmRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100971 }
972 break;
973 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100974
975 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100976 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100977 }
978}
979
980void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100981 LocationSummary* locations =
982 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100983 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100984 case Primitive::kPrimInt: {
985 locations->SetInAt(0, Location::RequiresRegister());
986 locations->SetInAt(1, Location::Any());
987 locations->SetOut(Location::SameAsFirstInput());
988 break;
989 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100990 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000991 locations->SetInAt(0, Location::RequiresRegister());
992 locations->SetInAt(1, Location::RequiresRegister());
993 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100994 break;
995 }
996
997 case Primitive::kPrimBoolean:
998 case Primitive::kPrimByte:
999 case Primitive::kPrimChar:
1000 case Primitive::kPrimShort:
1001 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
1002 break;
1003
1004 default:
1005 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
1006 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001007}
1008
1009void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1010 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001011 DCHECK_EQ(locations->InAt(0).As<CpuRegister>().AsRegister(),
1012 locations->Out().As<CpuRegister>().AsRegister());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001013 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001014 case Primitive::kPrimInt: {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001015 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001016 __ subl(locations->InAt(0).As<CpuRegister>(),
1017 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001018 } else if (locations->InAt(1).IsConstant()) {
1019 HConstant* instruction = locations->InAt(1).GetConstant();
1020 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001021 __ subl(locations->InAt(0).As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001022 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001023 __ subl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001024 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
1025 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001026 break;
1027 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001028 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001029 __ subq(locations->InAt(0).As<CpuRegister>(),
1030 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001031 break;
1032 }
1033
1034 case Primitive::kPrimBoolean:
1035 case Primitive::kPrimByte:
1036 case Primitive::kPrimChar:
1037 case Primitive::kPrimShort:
1038 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
1039 break;
1040
1041 default:
1042 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
1043 }
1044}
1045
1046void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001047 LocationSummary* locations =
1048 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001049 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001050 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1051 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1052 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001053}
1054
1055void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
1056 InvokeRuntimeCallingConvention calling_convention;
1057 LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
1058 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1059
1060 __ gs()->call(Address::Absolute(
1061 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
1062
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001063 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001064 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001065}
1066
1067void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001068 LocationSummary* locations =
1069 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001070 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1071 if (location.IsStackSlot()) {
1072 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1073 } else if (location.IsDoubleStackSlot()) {
1074 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1075 }
1076 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001077}
1078
1079void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
1080 // Nothing to do, the parameter is already at its location.
1081}
1082
1083void LocationsBuilderX86_64::VisitNot(HNot* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001084 LocationSummary* locations =
1085 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001086 locations->SetInAt(0, Location::RequiresRegister());
1087 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001088}
1089
1090void InstructionCodeGeneratorX86_64::VisitNot(HNot* instruction) {
1091 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001092 DCHECK_EQ(locations->InAt(0).As<CpuRegister>().AsRegister(),
1093 locations->Out().As<CpuRegister>().AsRegister());
1094 __ xorq(locations->Out().As<CpuRegister>(), Immediate(1));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001095}
1096
1097void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001098 LocationSummary* locations =
1099 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001100 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1101 locations->SetInAt(i, Location::Any());
1102 }
1103 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001104}
1105
1106void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
1107 LOG(FATAL) << "Unimplemented";
1108}
1109
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001110void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001111 LocationSummary* locations =
1112 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001113 Primitive::Type field_type = instruction->GetFieldType();
1114 bool is_object_type = field_type == Primitive::kPrimNot;
1115 bool dies_at_entry = !is_object_type;
1116 locations->SetInAt(0, Location::RequiresRegister(), dies_at_entry);
1117 locations->SetInAt(1, Location::RequiresRegister(), dies_at_entry);
1118 if (is_object_type) {
1119 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001120 locations->AddTemp(Location::RequiresRegister());
1121 locations->AddTemp(Location::RequiresRegister());
1122 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001123}
1124
1125void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1126 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001127 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
1128 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001129 size_t offset = instruction->GetFieldOffset().SizeValue();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001130 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001131
1132 switch (field_type) {
1133 case Primitive::kPrimBoolean:
1134 case Primitive::kPrimByte: {
1135 __ movb(Address(obj, offset), value);
1136 break;
1137 }
1138
1139 case Primitive::kPrimShort:
1140 case Primitive::kPrimChar: {
1141 __ movw(Address(obj, offset), value);
1142 break;
1143 }
1144
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001145 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001146 case Primitive::kPrimNot: {
1147 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001148 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001149 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
1150 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001151 codegen_->MarkGCCard(temp, card, obj, value);
1152 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001153 break;
1154 }
1155
1156 case Primitive::kPrimLong: {
1157 __ movq(Address(obj, offset), value);
1158 break;
1159 }
1160
1161 case Primitive::kPrimFloat:
1162 case Primitive::kPrimDouble:
1163 LOG(FATAL) << "Unimplemented register type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001164 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001165 case Primitive::kPrimVoid:
1166 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001167 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001168 }
1169}
1170
1171void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001172 LocationSummary* locations =
1173 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001174 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001175 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001176}
1177
1178void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1179 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001180 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
1181 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001182 size_t offset = instruction->GetFieldOffset().SizeValue();
1183
1184 switch (instruction->GetType()) {
1185 case Primitive::kPrimBoolean: {
1186 __ movzxb(out, Address(obj, offset));
1187 break;
1188 }
1189
1190 case Primitive::kPrimByte: {
1191 __ movsxb(out, Address(obj, offset));
1192 break;
1193 }
1194
1195 case Primitive::kPrimShort: {
1196 __ movsxw(out, Address(obj, offset));
1197 break;
1198 }
1199
1200 case Primitive::kPrimChar: {
1201 __ movzxw(out, Address(obj, offset));
1202 break;
1203 }
1204
1205 case Primitive::kPrimInt:
1206 case Primitive::kPrimNot: {
1207 __ movl(out, Address(obj, offset));
1208 break;
1209 }
1210
1211 case Primitive::kPrimLong: {
1212 __ movq(out, Address(obj, offset));
1213 break;
1214 }
1215
1216 case Primitive::kPrimFloat:
1217 case Primitive::kPrimDouble:
1218 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001219 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001220 case Primitive::kPrimVoid:
1221 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001222 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001223 }
1224}
1225
1226void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001227 LocationSummary* locations =
1228 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001229 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001230 if (instruction->HasUses()) {
1231 locations->SetOut(Location::SameAsFirstInput());
1232 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001233}
1234
1235void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001236 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001237 codegen_->AddSlowPath(slow_path);
1238
1239 LocationSummary* locations = instruction->GetLocations();
1240 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001241
1242 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001243 __ cmpl(obj.As<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001244 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001245 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001246 } else {
1247 DCHECK(obj.IsConstant()) << obj;
1248 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1249 __ jmp(slow_path->GetEntryLabel());
1250 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001251 }
1252 __ j(kEqual, slow_path->GetEntryLabel());
1253}
1254
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001255void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001256 LocationSummary* locations =
1257 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001258 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
1259 locations->SetInAt(
1260 1, Location::RegisterOrConstant(instruction->InputAt(1)), Location::kDiesAtEntry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001261 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001262}
1263
1264void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
1265 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001266 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001267 Location index = locations->InAt(1);
1268
1269 switch (instruction->GetType()) {
1270 case Primitive::kPrimBoolean: {
1271 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001272 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001273 if (index.IsConstant()) {
1274 __ movzxb(out, Address(obj,
1275 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1276 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001277 __ movzxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001278 }
1279 break;
1280 }
1281
1282 case Primitive::kPrimByte: {
1283 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001284 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001285 if (index.IsConstant()) {
1286 __ movsxb(out, Address(obj,
1287 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1288 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001289 __ movsxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001290 }
1291 break;
1292 }
1293
1294 case Primitive::kPrimShort: {
1295 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001296 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001297 if (index.IsConstant()) {
1298 __ movsxw(out, Address(obj,
1299 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1300 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001301 __ movsxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001302 }
1303 break;
1304 }
1305
1306 case Primitive::kPrimChar: {
1307 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001308 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001309 if (index.IsConstant()) {
1310 __ movzxw(out, Address(obj,
1311 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1312 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001313 __ movzxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001314 }
1315 break;
1316 }
1317
1318 case Primitive::kPrimInt:
1319 case Primitive::kPrimNot: {
1320 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1321 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001322 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001323 if (index.IsConstant()) {
1324 __ movl(out, Address(obj,
1325 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1326 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001327 __ movl(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001328 }
1329 break;
1330 }
1331
1332 case Primitive::kPrimLong: {
1333 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001334 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001335 if (index.IsConstant()) {
1336 __ movq(out, Address(obj,
1337 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
1338 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001339 __ movq(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001340 }
1341 break;
1342 }
1343
1344 case Primitive::kPrimFloat:
1345 case Primitive::kPrimDouble:
1346 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001347 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001348 case Primitive::kPrimVoid:
1349 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001350 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001351 }
1352}
1353
1354void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001355 Primitive::Type value_type = instruction->GetComponentType();
1356 bool is_object = value_type == Primitive::kPrimNot;
1357 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1358 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
1359 if (is_object) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001360 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001361 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1362 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1363 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001364 } else {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001365 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
1366 locations->SetInAt(
1367 1, Location::RegisterOrConstant(instruction->InputAt(1)), Location::kDiesAtEntry);
1368 locations->SetInAt(2, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001369 if (value_type == Primitive::kPrimLong) {
1370 locations->SetInAt(2, Location::RequiresRegister(), Location::kDiesAtEntry);
1371 } else {
1372 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)), Location::kDiesAtEntry);
1373 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001374 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001375}
1376
1377void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
1378 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001379 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001380 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001381 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001382 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001383
1384 switch (value_type) {
1385 case Primitive::kPrimBoolean:
1386 case Primitive::kPrimByte: {
1387 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001388 if (index.IsConstant()) {
1389 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001390 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001391 __ movb(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001392 } else {
1393 __ movb(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1394 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001395 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001396 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001397 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
1398 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001399 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001400 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001401 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1402 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001403 }
1404 break;
1405 }
1406
1407 case Primitive::kPrimShort:
1408 case Primitive::kPrimChar: {
1409 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001410 if (index.IsConstant()) {
1411 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001412 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001413 __ movw(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001414 } else {
1415 __ movw(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1416 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001417 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001418 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001419 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
1420 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001421 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001422 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001423 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1424 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001425 }
1426 break;
1427 }
1428
1429 case Primitive::kPrimInt: {
1430 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001431 if (index.IsConstant()) {
1432 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001433 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001434 __ movl(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001435 } else {
1436 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1437 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001438 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001439 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001440 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
1441 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001442 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001443 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001444 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1445 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001446 }
1447 break;
1448 }
1449
1450 case Primitive::kPrimNot: {
1451 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject), true));
1452 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001453 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001454 break;
1455 }
1456
1457 case Primitive::kPrimLong: {
1458 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001459 if (index.IsConstant()) {
1460 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001461 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001462 __ movq(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001463 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001464 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001465 __ movq(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
1466 value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001467 }
1468 break;
1469 }
1470
1471 case Primitive::kPrimFloat:
1472 case Primitive::kPrimDouble:
1473 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001474 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001475 case Primitive::kPrimVoid:
1476 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001477 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001478 }
1479}
1480
1481void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001482 LocationSummary* locations =
1483 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001484 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001485 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001486}
1487
1488void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
1489 LocationSummary* locations = instruction->GetLocations();
1490 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001491 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
1492 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001493 __ movl(out, Address(obj, offset));
1494}
1495
1496void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001497 LocationSummary* locations =
1498 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001499 locations->SetInAt(0, Location::RequiresRegister());
1500 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001501 if (instruction->HasUses()) {
1502 locations->SetOut(Location::SameAsFirstInput());
1503 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001504}
1505
1506void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
1507 LocationSummary* locations = instruction->GetLocations();
1508 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01001509 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001510 codegen_->AddSlowPath(slow_path);
1511
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001512 CpuRegister index = locations->InAt(0).As<CpuRegister>();
1513 CpuRegister length = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001514
1515 __ cmpl(index, length);
1516 __ j(kAboveEqual, slow_path->GetEntryLabel());
1517}
1518
1519void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
1520 CpuRegister card,
1521 CpuRegister object,
1522 CpuRegister value) {
1523 Label is_null;
1524 __ testl(value, value);
1525 __ j(kEqual, &is_null);
1526 __ gs()->movq(card, Address::Absolute(
1527 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
1528 __ movq(temp, object);
1529 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
1530 __ movb(Address(temp, card, TIMES_1, 0), card);
1531 __ Bind(&is_null);
1532}
1533
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001534void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
1535 temp->SetLocations(nullptr);
1536}
1537
1538void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
1539 // Nothing to do, this is driven by the code generator.
1540}
1541
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001542void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
1543 LOG(FATAL) << "Unimplemented";
1544}
1545
1546void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001547 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
1548}
1549
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001550void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
1551 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
1552}
1553
1554void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001555 HBasicBlock* block = instruction->GetBlock();
1556 if (block->GetLoopInformation() != nullptr) {
1557 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
1558 // The back edge will generate the suspend check.
1559 return;
1560 }
1561 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
1562 // The goto will generate the suspend check.
1563 return;
1564 }
1565 GenerateSuspendCheck(instruction, nullptr);
1566}
1567
1568void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
1569 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001570 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001571 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001572 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001573 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001574 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001575 if (successor == nullptr) {
1576 __ j(kNotEqual, slow_path->GetEntryLabel());
1577 __ Bind(slow_path->GetReturnLabel());
1578 } else {
1579 __ j(kEqual, codegen_->GetLabelOf(successor));
1580 __ jmp(slow_path->GetEntryLabel());
1581 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001582}
1583
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001584X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
1585 return codegen_->GetAssembler();
1586}
1587
1588void ParallelMoveResolverX86_64::EmitMove(size_t index) {
1589 MoveOperands* move = moves_.Get(index);
1590 Location source = move->GetSource();
1591 Location destination = move->GetDestination();
1592
1593 if (source.IsRegister()) {
1594 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001595 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001596 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001597 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001598 source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001599 } else {
1600 DCHECK(destination.IsDoubleStackSlot());
1601 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001602 source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001603 }
1604 } else if (source.IsStackSlot()) {
1605 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001606 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001607 Address(CpuRegister(RSP), source.GetStackIndex()));
1608 } else {
1609 DCHECK(destination.IsStackSlot());
1610 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1611 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
1612 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001613 } else if (source.IsDoubleStackSlot()) {
1614 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001615 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001616 Address(CpuRegister(RSP), source.GetStackIndex()));
1617 } else {
1618 DCHECK(destination.IsDoubleStackSlot());
1619 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1620 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
1621 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001622 } else if (source.IsConstant()) {
1623 HConstant* constant = source.GetConstant();
1624 if (constant->IsIntConstant()) {
1625 Immediate imm(constant->AsIntConstant()->GetValue());
1626 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001627 __ movl(destination.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001628 } else {
1629 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
1630 }
1631 } else if (constant->IsLongConstant()) {
1632 int64_t value = constant->AsLongConstant()->GetValue();
1633 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001634 __ movq(destination.As<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001635 } else {
1636 __ movq(CpuRegister(TMP), Immediate(value));
1637 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
1638 }
1639 } else {
1640 LOG(FATAL) << "Unimplemented constant type";
1641 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001642 } else {
1643 LOG(FATAL) << "Unimplemented";
1644 }
1645}
1646
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001647void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001648 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001649 __ movl(Address(CpuRegister(RSP), mem), reg);
1650 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001651}
1652
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001653void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001654 ScratchRegisterScope ensure_scratch(
1655 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
1656
1657 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
1658 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
1659 __ movl(CpuRegister(ensure_scratch.GetRegister()),
1660 Address(CpuRegister(RSP), mem2 + stack_offset));
1661 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
1662 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
1663 CpuRegister(ensure_scratch.GetRegister()));
1664}
1665
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001666void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
1667 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
1668 __ movq(Address(CpuRegister(RSP), mem), reg);
1669 __ movq(reg, CpuRegister(TMP));
1670}
1671
1672void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
1673 ScratchRegisterScope ensure_scratch(
1674 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
1675
1676 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
1677 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
1678 __ movq(CpuRegister(ensure_scratch.GetRegister()),
1679 Address(CpuRegister(RSP), mem2 + stack_offset));
1680 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
1681 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
1682 CpuRegister(ensure_scratch.GetRegister()));
1683}
1684
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001685void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
1686 MoveOperands* move = moves_.Get(index);
1687 Location source = move->GetSource();
1688 Location destination = move->GetDestination();
1689
1690 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001691 __ xchgq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001692 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001693 Exchange32(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001694 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001695 Exchange32(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001696 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001697 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
1698 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001699 Exchange64(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001700 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001701 Exchange64(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001702 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
1703 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001704 } else {
1705 LOG(FATAL) << "Unimplemented";
1706 }
1707}
1708
1709
1710void ParallelMoveResolverX86_64::SpillScratch(int reg) {
1711 __ pushq(CpuRegister(reg));
1712}
1713
1714
1715void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
1716 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001717}
1718
1719} // namespace x86_64
1720} // namespace art