blob: cbf0630a3c65093ebc59d05b4444987ff45f5525 [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"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010021#include "mirror/array.h"
22#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
33x86_64::X86_64ManagedRegister Location::AsX86_64() const {
34 return reg().AsX86_64();
35}
36
37namespace x86_64 {
38
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010039static constexpr bool kExplicitStackOverflowCheck = false;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010040
41// Some x86_64 instructions require a register to be available as temp.
42static constexpr Register TMP = R11;
43
44static constexpr int kNumberOfPushedRegistersAtEntry = 1;
45static constexpr int kCurrentMethodStackOffset = 0;
46
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010047static Location X86_64CpuLocation(Register reg) {
48 return Location::RegisterLocation(X86_64ManagedRegister::FromCpuRegister(reg));
49}
50
51static constexpr Register kRuntimeParameterCoreRegisters[] = { RDI, RSI, RDX };
52static constexpr size_t kRuntimeParameterCoreRegistersLength =
53 arraysize(kRuntimeParameterCoreRegisters);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010054static constexpr FloatRegister kRuntimeParameterFpuRegisters[] = { };
55static constexpr size_t kRuntimeParameterFpuRegistersLength = 0;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010056
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010057class InvokeRuntimeCallingConvention : public CallingConvention<Register, FloatRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010058 public:
59 InvokeRuntimeCallingConvention()
60 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010061 kRuntimeParameterCoreRegistersLength,
62 kRuntimeParameterFpuRegisters,
63 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010064
65 private:
66 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
67};
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010068
Nicolas Geoffraye5038322014-07-04 09:41:32 +010069#define __ reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler())->
70
71class NullCheckSlowPathX86_64 : public SlowPathCode {
72 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010073 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010074
75 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
76 __ Bind(GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010077 __ gs()->call(
78 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowNullPointer), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +010079 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080 }
81
82 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010083 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010084 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
85};
86
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010087class StackOverflowCheckSlowPathX86_64 : public SlowPathCode {
88 public:
89 StackOverflowCheckSlowPathX86_64() {}
90
91 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
92 __ Bind(GetEntryLabel());
93 __ addq(CpuRegister(RSP),
94 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
95 __ gs()->jmp(
96 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowStackOverflow), true));
97 }
98
99 private:
100 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86_64);
101};
102
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000103class SuspendCheckSlowPathX86_64 : public SlowPathCode {
104 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100105 explicit SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
106 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000107
108 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
109 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100110 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000111 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pTestSuspend), true));
112 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100113 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100114 if (successor_ == nullptr) {
115 __ jmp(GetReturnLabel());
116 } else {
117 __ jmp(codegen->GetLabelOf(successor_));
118 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000119 }
120
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100121 Label* GetReturnLabel() {
122 DCHECK(successor_ == nullptr);
123 return &return_label_;
124 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000125
126 private:
127 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100128 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000129 Label return_label_;
130
131 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
132};
133
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100134class BoundsCheckSlowPathX86_64 : public SlowPathCode {
135 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100136 BoundsCheckSlowPathX86_64(HBoundsCheck* instruction,
137 Location index_location,
138 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100139 : instruction_(instruction),
140 index_location_(index_location),
141 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100142
143 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
144 CodeGeneratorX86_64* x64_codegen = reinterpret_cast<CodeGeneratorX86_64*>(codegen);
145 __ Bind(GetEntryLabel());
146 InvokeRuntimeCallingConvention calling_convention;
147 x64_codegen->Move(X86_64CpuLocation(calling_convention.GetRegisterAt(0)), index_location_);
148 x64_codegen->Move(X86_64CpuLocation(calling_convention.GetRegisterAt(1)), length_location_);
149 __ gs()->call(Address::Absolute(
150 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowArrayBounds), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100151 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100152 }
153
154 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100155 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100156 const Location index_location_;
157 const Location length_location_;
158
159 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
160};
161
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100162#undef __
163#define __ reinterpret_cast<X86_64Assembler*>(GetAssembler())->
164
Dave Allison20dfc792014-06-16 20:44:29 -0700165inline Condition X86_64Condition(IfCondition cond) {
166 switch (cond) {
167 case kCondEQ: return kEqual;
168 case kCondNE: return kNotEqual;
169 case kCondLT: return kLess;
170 case kCondLE: return kLessEqual;
171 case kCondGT: return kGreater;
172 case kCondGE: return kGreaterEqual;
173 default:
174 LOG(FATAL) << "Unknown if condition";
175 }
176 return kEqual;
177}
178
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100179void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
180 stream << X86_64ManagedRegister::FromCpuRegister(Register(reg));
181}
182
183void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
184 stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg));
185}
186
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100187void CodeGeneratorX86_64::SaveCoreRegister(Location stack_location, uint32_t reg_id) {
188 __ movq(Address(CpuRegister(RSP), stack_location.GetStackIndex()), CpuRegister(reg_id));
189}
190
191void CodeGeneratorX86_64::RestoreCoreRegister(Location stack_location, uint32_t reg_id) {
192 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_location.GetStackIndex()));
193}
194
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100195CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph)
196 : CodeGenerator(graph, kNumberOfRegIds),
197 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000198 instruction_visitor_(graph, this),
199 move_resolver_(graph->GetArena(), this) {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100200
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100201size_t CodeGeneratorX86_64::FrameEntrySpillSize() const {
202 return kNumberOfPushedRegistersAtEntry * kX86_64WordSize;
203}
204
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100205InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
206 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100207 : HGraphVisitor(graph),
208 assembler_(codegen->GetAssembler()),
209 codegen_(codegen) {}
210
211ManagedRegister CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type,
212 bool* blocked_registers) const {
213 switch (type) {
214 case Primitive::kPrimLong:
215 case Primitive::kPrimByte:
216 case Primitive::kPrimBoolean:
217 case Primitive::kPrimChar:
218 case Primitive::kPrimShort:
219 case Primitive::kPrimInt:
220 case Primitive::kPrimNot: {
221 size_t reg = AllocateFreeRegisterInternal(blocked_registers, kNumberOfCpuRegisters);
222 return X86_64ManagedRegister::FromCpuRegister(static_cast<Register>(reg));
223 }
224
225 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100226 case Primitive::kPrimDouble: {
227 size_t reg = AllocateFreeRegisterInternal(
228 blocked_registers + kNumberOfCpuRegisters, kNumberOfFloatRegisters);
229 return X86_64ManagedRegister::FromXmmRegister(static_cast<FloatRegister>(reg));
230 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100231
232 case Primitive::kPrimVoid:
233 LOG(FATAL) << "Unreachable type " << type;
234 }
235
236 return ManagedRegister::NoRegister();
237}
238
239void CodeGeneratorX86_64::SetupBlockedRegisters(bool* blocked_registers) const {
240 // Stack register is always reserved.
241 blocked_registers[RSP] = true;
242
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000243 // Block the register used as TMP.
244 blocked_registers[TMP] = true;
245
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100246 // TODO: We currently don't use Quick's callee saved registers.
247 blocked_registers[RBX] = true;
248 blocked_registers[RBP] = true;
249 blocked_registers[R12] = true;
250 blocked_registers[R13] = true;
251 blocked_registers[R14] = true;
252 blocked_registers[R15] = true;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100253
254 bool* blocked_xmm_registers = blocked_registers + kNumberOfCpuRegisters;
255 blocked_xmm_registers[XMM12] = true;
256 blocked_xmm_registers[XMM13] = true;
257 blocked_xmm_registers[XMM14] = true;
258 blocked_xmm_registers[XMM15] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100259}
260
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100261void CodeGeneratorX86_64::GenerateFrameEntry() {
262 // Create a fake register to mimic Quick.
263 static const int kFakeReturnRegister = 16;
264 core_spill_mask_ |= (1 << kFakeReturnRegister);
265
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100266 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700267 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100268
269 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
270 __ testq(CpuRegister(RAX), Address(
271 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100272 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100273 }
274
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100275 // The return PC has already been pushed on the stack.
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100276 __ subq(CpuRegister(RSP),
277 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
278
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100279 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
280 SlowPathCode* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86_64();
281 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100282
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100283 __ gs()->cmpq(CpuRegister(RSP),
284 Address::Absolute(Thread::StackEndOffset<kX86_64WordSize>(), true));
285 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100286 }
287
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100288 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
289}
290
291void CodeGeneratorX86_64::GenerateFrameExit() {
292 __ addq(CpuRegister(RSP),
293 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
294}
295
296void CodeGeneratorX86_64::Bind(Label* label) {
297 __ Bind(label);
298}
299
300void InstructionCodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
301 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
302}
303
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100304Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
305 switch (load->GetType()) {
306 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100307 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100308 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
309 break;
310
311 case Primitive::kPrimInt:
312 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100313 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100314 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100315
316 case Primitive::kPrimBoolean:
317 case Primitive::kPrimByte:
318 case Primitive::kPrimChar:
319 case Primitive::kPrimShort:
320 case Primitive::kPrimVoid:
321 LOG(FATAL) << "Unexpected type " << load->GetType();
322 }
323
324 LOG(FATAL) << "Unreachable";
325 return Location();
326}
327
328void CodeGeneratorX86_64::Move(Location destination, Location source) {
329 if (source.Equals(destination)) {
330 return;
331 }
332 if (destination.IsRegister()) {
333 if (source.IsRegister()) {
334 __ movq(destination.AsX86_64().AsCpuRegister(), source.AsX86_64().AsCpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100335 } else if (source.IsFpuRegister()) {
336 __ movd(destination.AsX86_64().AsCpuRegister(), source.AsX86_64().AsXmmRegister());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100337 } else if (source.IsStackSlot()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100338 __ movl(destination.AsX86_64().AsCpuRegister(),
339 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100340 } else {
341 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100342 __ movq(destination.AsX86_64().AsCpuRegister(),
343 Address(CpuRegister(RSP), source.GetStackIndex()));
344 }
345 } else if (destination.IsFpuRegister()) {
346 if (source.IsRegister()) {
347 __ movd(destination.AsX86_64().AsXmmRegister(), source.AsX86_64().AsCpuRegister());
348 } else if (source.IsFpuRegister()) {
349 __ movaps(destination.AsX86_64().AsXmmRegister(), source.AsX86_64().AsXmmRegister());
350 } else if (source.IsStackSlot()) {
351 __ movss(destination.AsX86_64().AsXmmRegister(),
352 Address(CpuRegister(RSP), source.GetStackIndex()));
353 } else {
354 DCHECK(source.IsDoubleStackSlot());
355 __ movsd(destination.AsX86_64().AsXmmRegister(),
356 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100357 }
358 } else if (destination.IsStackSlot()) {
359 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100360 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
361 source.AsX86_64().AsCpuRegister());
362 } else if (source.IsFpuRegister()) {
363 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
364 source.AsX86_64().AsXmmRegister());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100365 } else {
366 DCHECK(source.IsStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000367 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
368 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100369 }
370 } else {
371 DCHECK(destination.IsDoubleStackSlot());
372 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100373 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
374 source.AsX86_64().AsCpuRegister());
375 } else if (source.IsFpuRegister()) {
376 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
377 source.AsX86_64().AsXmmRegister());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100378 } else {
379 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000380 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
381 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100382 }
383 }
384}
385
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100386void CodeGeneratorX86_64::Move(HInstruction* instruction,
387 Location location,
388 HInstruction* move_for) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100389 if (instruction->AsIntConstant() != nullptr) {
390 Immediate imm(instruction->AsIntConstant()->GetValue());
391 if (location.IsRegister()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000392 __ movl(location.AsX86_64().AsCpuRegister(), imm);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100393 } else {
394 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
395 }
396 } else if (instruction->AsLongConstant() != nullptr) {
397 int64_t value = instruction->AsLongConstant()->GetValue();
398 if (location.IsRegister()) {
399 __ movq(location.AsX86_64().AsCpuRegister(), Immediate(value));
400 } else {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000401 __ movq(CpuRegister(TMP), Immediate(value));
402 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100403 }
404 } else if (instruction->AsLoadLocal() != nullptr) {
405 switch (instruction->GetType()) {
406 case Primitive::kPrimBoolean:
407 case Primitive::kPrimByte:
408 case Primitive::kPrimChar:
409 case Primitive::kPrimShort:
410 case Primitive::kPrimInt:
411 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100412 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100413 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
414 break;
415
416 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100417 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100418 Move(location, Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
419 break;
420
421 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100422 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100423 }
424 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100425 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100426 switch (instruction->GetType()) {
427 case Primitive::kPrimBoolean:
428 case Primitive::kPrimByte:
429 case Primitive::kPrimChar:
430 case Primitive::kPrimShort:
431 case Primitive::kPrimInt:
432 case Primitive::kPrimNot:
433 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100434 case Primitive::kPrimFloat:
435 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100436 Move(location, instruction->GetLocations()->Out());
437 break;
438
439 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100440 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100441 }
442 }
443}
444
445void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
446 got->SetLocations(nullptr);
447}
448
449void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
450 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100451 DCHECK(!successor->IsExitBlock());
452
453 HBasicBlock* block = got->GetBlock();
454 HInstruction* previous = got->GetPrevious();
455
456 HLoopInformation* info = block->GetLoopInformation();
457 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
458 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
459 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
460 return;
461 }
462
463 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
464 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
465 }
466 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100467 __ jmp(codegen_->GetLabelOf(successor));
468 }
469}
470
471void LocationsBuilderX86_64::VisitExit(HExit* exit) {
472 exit->SetLocations(nullptr);
473}
474
475void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
476 if (kIsDebugBuild) {
477 __ Comment("Unreachable");
478 __ int3();
479 }
480}
481
482void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100483 LocationSummary* locations =
484 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100485 HInstruction* cond = if_instr->InputAt(0);
486 DCHECK(cond->IsCondition());
487 HCondition* condition = cond->AsCondition();
488 if (condition->NeedsMaterialization()) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +0100489 locations->SetInAt(0, Location::Any(), Location::kDiesAtEntry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100490 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100491}
492
493void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700494 HInstruction* cond = if_instr->InputAt(0);
495 DCHECK(cond->IsCondition());
496 HCondition* condition = cond->AsCondition();
497 if (condition->NeedsMaterialization()) {
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100498 // Moves do not affect the eflags register, so if the condition is evaluated
499 // just before the if, we don't need to evaluate it again.
500 if (!condition->IsBeforeWhenDisregardMoves(if_instr)) {
501 // Materialized condition, compare against 0.
502 Location lhs = if_instr->GetLocations()->InAt(0);
503 if (lhs.IsRegister()) {
504 __ cmpl(lhs.AsX86_64().AsCpuRegister(), Immediate(0));
505 } else {
506 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
507 }
Dave Allison20dfc792014-06-16 20:44:29 -0700508 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100509 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700510 } else {
511 Location lhs = condition->GetLocations()->InAt(0);
512 Location rhs = condition->GetLocations()->InAt(1);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100513 if (rhs.IsRegister()) {
514 __ cmpl(lhs.AsX86_64().AsCpuRegister(), rhs.AsX86_64().AsCpuRegister());
515 } else if (rhs.IsConstant()) {
516 __ cmpl(lhs.AsX86_64().AsCpuRegister(),
517 Immediate(rhs.GetConstant()->AsIntConstant()->GetValue()));
518 } else {
519 __ cmpl(lhs.AsX86_64().AsCpuRegister(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
520 }
Dave Allison20dfc792014-06-16 20:44:29 -0700521 __ j(X86_64Condition(condition->GetCondition()),
522 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
523 }
524 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfFalseSuccessor())) {
525 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100526 }
527}
528
529void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
530 local->SetLocations(nullptr);
531}
532
533void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
534 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
535}
536
537void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
538 local->SetLocations(nullptr);
539}
540
541void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
542 // Nothing to do, this is driven by the code generator.
543}
544
545void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100546 LocationSummary* locations =
547 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100548 switch (store->InputAt(1)->GetType()) {
549 case Primitive::kPrimBoolean:
550 case Primitive::kPrimByte:
551 case Primitive::kPrimChar:
552 case Primitive::kPrimShort:
553 case Primitive::kPrimInt:
554 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100555 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100556 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
557 break;
558
559 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100560 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100561 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
562 break;
563
564 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100565 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100566 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100567}
568
569void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
570}
571
Dave Allison20dfc792014-06-16 20:44:29 -0700572void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100573 LocationSummary* locations =
574 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +0100575 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
576 locations->SetInAt(1, Location::Any(), Location::kDiesAtEntry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100577 if (comp->NeedsMaterialization()) {
578 locations->SetOut(Location::RequiresRegister());
579 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100580}
581
Dave Allison20dfc792014-06-16 20:44:29 -0700582void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
583 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100584 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100585 CpuRegister reg = locations->Out().AsX86_64().AsCpuRegister();
586 // Clear register: setcc only sets the low byte.
587 __ xorq(reg, reg);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100588 if (locations->InAt(1).IsRegister()) {
589 __ cmpq(locations->InAt(0).AsX86_64().AsCpuRegister(),
590 locations->InAt(1).AsX86_64().AsCpuRegister());
591 } else if (locations->InAt(1).IsConstant()) {
592 __ cmpq(locations->InAt(0).AsX86_64().AsCpuRegister(),
593 Immediate(locations->InAt(1).GetConstant()->AsIntConstant()->GetValue()));
594 } else {
595 __ cmpq(locations->InAt(0).AsX86_64().AsCpuRegister(),
596 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
597 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100598 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700599 }
600}
601
602void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
603 VisitCondition(comp);
604}
605
606void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
607 VisitCondition(comp);
608}
609
610void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
611 VisitCondition(comp);
612}
613
614void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
615 VisitCondition(comp);
616}
617
618void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
619 VisitCondition(comp);
620}
621
622void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
623 VisitCondition(comp);
624}
625
626void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
627 VisitCondition(comp);
628}
629
630void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
631 VisitCondition(comp);
632}
633
634void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
635 VisitCondition(comp);
636}
637
638void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
639 VisitCondition(comp);
640}
641
642void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
643 VisitCondition(comp);
644}
645
646void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
647 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100648}
649
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100650void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100651 LocationSummary* locations =
652 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +0100653 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
654 locations->SetInAt(1, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100655 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100656}
657
658void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
659 Label greater, done;
660 LocationSummary* locations = compare->GetLocations();
661 switch (compare->InputAt(0)->GetType()) {
662 case Primitive::kPrimLong:
663 __ cmpq(locations->InAt(0).AsX86_64().AsCpuRegister(),
664 locations->InAt(1).AsX86_64().AsCpuRegister());
665 break;
666 default:
667 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
668 }
669
670 __ movl(locations->Out().AsX86_64().AsCpuRegister(), Immediate(0));
671 __ j(kEqual, &done);
672 __ j(kGreater, &greater);
673
674 __ movl(locations->Out().AsX86_64().AsCpuRegister(), Immediate(-1));
675 __ jmp(&done);
676
677 __ Bind(&greater);
678 __ movl(locations->Out().AsX86_64().AsCpuRegister(), Immediate(1));
679
680 __ Bind(&done);
681}
682
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100683void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100684 LocationSummary* locations =
685 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100686 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100687}
688
689void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100690}
691
692void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100693 LocationSummary* locations =
694 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100695 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100696}
697
698void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100699}
700
701void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
702 ret->SetLocations(nullptr);
703}
704
705void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
706 codegen_->GenerateFrameExit();
707 __ ret();
708}
709
710void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100711 LocationSummary* locations =
712 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100713 switch (ret->InputAt(0)->GetType()) {
714 case Primitive::kPrimBoolean:
715 case Primitive::kPrimByte:
716 case Primitive::kPrimChar:
717 case Primitive::kPrimShort:
718 case Primitive::kPrimInt:
719 case Primitive::kPrimNot:
720 case Primitive::kPrimLong:
721 locations->SetInAt(0, X86_64CpuLocation(RAX));
722 break;
723
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100724 case Primitive::kPrimFloat:
725 case Primitive::kPrimDouble:
726 locations->SetInAt(0,
727 Location::FpuRegisterLocation(X86_64ManagedRegister::FromXmmRegister(XMM0)));
728 break;
729
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100730 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100731 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100732 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100733}
734
735void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
736 if (kIsDebugBuild) {
737 switch (ret->InputAt(0)->GetType()) {
738 case Primitive::kPrimBoolean:
739 case Primitive::kPrimByte:
740 case Primitive::kPrimChar:
741 case Primitive::kPrimShort:
742 case Primitive::kPrimInt:
743 case Primitive::kPrimNot:
744 case Primitive::kPrimLong:
745 DCHECK_EQ(ret->GetLocations()->InAt(0).AsX86_64().AsCpuRegister().AsRegister(), RAX);
746 break;
747
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100748 case Primitive::kPrimFloat:
749 case Primitive::kPrimDouble:
750 DCHECK_EQ(ret->GetLocations()->InAt(0).AsX86_64().AsXmmRegister().AsFloatRegister(),
751 XMM0);
752 break;
753
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100754 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100755 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100756 }
757 }
758 codegen_->GenerateFrameExit();
759 __ ret();
760}
761
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100762Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
763 switch (type) {
764 case Primitive::kPrimBoolean:
765 case Primitive::kPrimByte:
766 case Primitive::kPrimChar:
767 case Primitive::kPrimShort:
768 case Primitive::kPrimInt:
769 case Primitive::kPrimNot: {
770 uint32_t index = gp_index_++;
771 stack_index_++;
772 if (index < calling_convention.GetNumberOfRegisters()) {
773 return X86_64CpuLocation(calling_convention.GetRegisterAt(index));
774 } else {
775 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
776 }
777 }
778
779 case Primitive::kPrimLong: {
780 uint32_t index = gp_index_;
781 stack_index_ += 2;
782 if (index < calling_convention.GetNumberOfRegisters()) {
783 gp_index_ += 1;
784 return X86_64CpuLocation(calling_convention.GetRegisterAt(index));
785 } else {
786 gp_index_ += 2;
787 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
788 }
789 }
790
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100791 case Primitive::kPrimFloat: {
792 uint32_t index = fp_index_++;
793 stack_index_++;
794 if (index < calling_convention.GetNumberOfFpuRegisters()) {
795 return Location::FpuRegisterLocation(X86_64ManagedRegister::FromXmmRegister(
796 calling_convention.GetFpuRegisterAt(index)));
797 } else {
798 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
799 }
800 }
801
802 case Primitive::kPrimDouble: {
803 uint32_t index = fp_index_++;
804 stack_index_ += 2;
805 if (index < calling_convention.GetNumberOfFpuRegisters()) {
806 return Location::FpuRegisterLocation(X86_64ManagedRegister::FromXmmRegister(
807 calling_convention.GetFpuRegisterAt(index)));
808 } else {
809 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
810 }
811 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100812
813 case Primitive::kPrimVoid:
814 LOG(FATAL) << "Unexpected parameter type " << type;
815 break;
816 }
817 return Location();
818}
819
820void LocationsBuilderX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100821 HandleInvoke(invoke);
822}
823
824void InstructionCodeGeneratorX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
825 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsX86_64().AsCpuRegister();
826 uint32_t heap_reference_size = sizeof(mirror::HeapReference<mirror::Object>);
827 size_t index_in_cache = mirror::Array::DataOffset(heap_reference_size).SizeValue() +
828 invoke->GetIndexInDexCache() * heap_reference_size;
829
830 // TODO: Implement all kinds of calls:
831 // 1) boot -> boot
832 // 2) app -> boot
833 // 3) app -> app
834 //
835 // Currently we implement the app -> app logic, which looks up in the resolve cache.
836
837 // temp = method;
838 LoadCurrentMethod(temp);
839 // temp = temp->dex_cache_resolved_methods_;
840 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
841 // temp = temp[index_in_cache]
842 __ movl(temp, Address(temp, index_in_cache));
843 // (temp + offset_of_quick_compiled_code)()
844 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
845
846 DCHECK(!codegen_->IsLeafMethod());
847 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
848}
849
850void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
851 HandleInvoke(invoke);
852}
853
854void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100855 LocationSummary* locations =
856 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100857 locations->AddTemp(X86_64CpuLocation(RDI));
858
859 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100860 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100861 HInstruction* input = invoke->InputAt(i);
862 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
863 }
864
865 switch (invoke->GetType()) {
866 case Primitive::kPrimBoolean:
867 case Primitive::kPrimByte:
868 case Primitive::kPrimChar:
869 case Primitive::kPrimShort:
870 case Primitive::kPrimInt:
871 case Primitive::kPrimNot:
872 case Primitive::kPrimLong:
873 locations->SetOut(X86_64CpuLocation(RAX));
874 break;
875
876 case Primitive::kPrimVoid:
877 break;
878
879 case Primitive::kPrimDouble:
880 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100881 locations->SetOut(
882 Location::FpuRegisterLocation(X86_64ManagedRegister::FromXmmRegister(XMM0)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100883 break;
884 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100885}
886
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100887void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100888 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsX86_64().AsCpuRegister();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100889 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
890 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
891 LocationSummary* locations = invoke->GetLocations();
892 Location receiver = locations->InAt(0);
893 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
894 // temp = object->GetClass();
895 if (receiver.IsStackSlot()) {
896 __ movq(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
897 __ movq(temp, Address(temp, class_offset));
898 } else {
899 __ movq(temp, Address(receiver.AsX86_64().AsCpuRegister(), class_offset));
900 }
901 // temp = temp->GetMethodAt(method_offset);
902 __ movl(temp, Address(temp, method_offset));
903 // call temp->GetEntryPoint();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100904 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
905
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100906 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100907 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100908}
909
910void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100911 LocationSummary* locations =
912 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100913 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100914 case Primitive::kPrimInt: {
915 locations->SetInAt(0, Location::RequiresRegister());
916 locations->SetInAt(1, Location::Any());
917 locations->SetOut(Location::SameAsFirstInput());
918 break;
919 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100920
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100921 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000922 locations->SetInAt(0, Location::RequiresRegister());
923 locations->SetInAt(1, Location::RequiresRegister());
924 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100925 break;
926 }
927
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100928 case Primitive::kPrimDouble:
929 case Primitive::kPrimFloat: {
930 locations->SetInAt(0, Location::RequiresFpuRegister());
931 locations->SetInAt(1, Location::Any());
932 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100933 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100934 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100935
936 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100937 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100938 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100939}
940
941void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
942 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100943 Location first = locations->InAt(0);
944 Location second = locations->InAt(1);
945
946 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100947 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000948 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100949 if (second.IsRegister()) {
950 __ addl(first.AsX86_64().AsCpuRegister(), second.AsX86_64().AsCpuRegister());
951 } else if (second.IsConstant()) {
952 HConstant* instruction = second.GetConstant();
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100953 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100954 __ addl(first.AsX86_64().AsCpuRegister(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100955 } else {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100956 __ addl(first.AsX86_64().AsCpuRegister(),
957 Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100958 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000959 break;
960 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100961
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100962 case Primitive::kPrimLong: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100963 __ addq(first.AsX86_64().AsCpuRegister(), second.AsX86_64().AsCpuRegister());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100964 break;
965 }
966
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100967 case Primitive::kPrimFloat: {
968 if (second.IsFpuRegister()) {
969 __ addss(first.AsX86_64().AsXmmRegister(), second.AsX86_64().AsXmmRegister());
970 } else {
971 __ addss(first.AsX86_64().AsXmmRegister(),
972 Address(CpuRegister(RSP), second.GetStackIndex()));
973 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100974 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100975 }
976
977 case Primitive::kPrimDouble: {
978 if (second.IsFpuRegister()) {
979 __ addsd(first.AsX86_64().AsXmmRegister(), second.AsX86_64().AsXmmRegister());
980 } else {
981 __ addsd(first.AsX86_64().AsXmmRegister(),
982 Address(CpuRegister(RSP), second.GetStackIndex()));
983 }
984 break;
985 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100986
987 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100988 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100989 }
990}
991
992void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100993 LocationSummary* locations =
994 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100995 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100996 case Primitive::kPrimInt: {
997 locations->SetInAt(0, Location::RequiresRegister());
998 locations->SetInAt(1, Location::Any());
999 locations->SetOut(Location::SameAsFirstInput());
1000 break;
1001 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001002 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001003 locations->SetInAt(0, Location::RequiresRegister());
1004 locations->SetInAt(1, Location::RequiresRegister());
1005 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001006 break;
1007 }
1008
1009 case Primitive::kPrimBoolean:
1010 case Primitive::kPrimByte:
1011 case Primitive::kPrimChar:
1012 case Primitive::kPrimShort:
1013 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
1014 break;
1015
1016 default:
1017 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
1018 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001019}
1020
1021void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1022 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001023 DCHECK_EQ(locations->InAt(0).AsX86_64().AsCpuRegister().AsRegister(),
1024 locations->Out().AsX86_64().AsCpuRegister().AsRegister());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001025 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001026 case Primitive::kPrimInt: {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001027 if (locations->InAt(1).IsRegister()) {
1028 __ subl(locations->InAt(0).AsX86_64().AsCpuRegister(),
1029 locations->InAt(1).AsX86_64().AsCpuRegister());
1030 } else if (locations->InAt(1).IsConstant()) {
1031 HConstant* instruction = locations->InAt(1).GetConstant();
1032 Immediate imm(instruction->AsIntConstant()->GetValue());
1033 __ subl(locations->InAt(0).AsX86_64().AsCpuRegister(), imm);
1034 } else {
1035 __ subl(locations->InAt(0).AsX86_64().AsCpuRegister(),
1036 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
1037 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001038 break;
1039 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001040 case Primitive::kPrimLong: {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001041 __ subq(locations->InAt(0).AsX86_64().AsCpuRegister(),
1042 locations->InAt(1).AsX86_64().AsCpuRegister());
1043 break;
1044 }
1045
1046 case Primitive::kPrimBoolean:
1047 case Primitive::kPrimByte:
1048 case Primitive::kPrimChar:
1049 case Primitive::kPrimShort:
1050 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
1051 break;
1052
1053 default:
1054 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
1055 }
1056}
1057
1058void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001059 LocationSummary* locations =
1060 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001061 InvokeRuntimeCallingConvention calling_convention;
1062 locations->AddTemp(X86_64CpuLocation(calling_convention.GetRegisterAt(0)));
1063 locations->AddTemp(X86_64CpuLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001064 locations->SetOut(X86_64CpuLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001065}
1066
1067void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
1068 InvokeRuntimeCallingConvention calling_convention;
1069 LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
1070 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1071
1072 __ gs()->call(Address::Absolute(
1073 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
1074
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001075 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001076 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001077}
1078
1079void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001080 LocationSummary* locations =
1081 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001082 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1083 if (location.IsStackSlot()) {
1084 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1085 } else if (location.IsDoubleStackSlot()) {
1086 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1087 }
1088 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001089}
1090
1091void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
1092 // Nothing to do, the parameter is already at its location.
1093}
1094
1095void LocationsBuilderX86_64::VisitNot(HNot* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001096 LocationSummary* locations =
1097 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001098 locations->SetInAt(0, Location::RequiresRegister());
1099 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001100}
1101
1102void InstructionCodeGeneratorX86_64::VisitNot(HNot* instruction) {
1103 LocationSummary* locations = instruction->GetLocations();
1104 DCHECK_EQ(locations->InAt(0).AsX86_64().AsCpuRegister().AsRegister(),
1105 locations->Out().AsX86_64().AsCpuRegister().AsRegister());
1106 __ xorq(locations->Out().AsX86_64().AsCpuRegister(), Immediate(1));
1107}
1108
1109void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001110 LocationSummary* locations =
1111 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001112 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1113 locations->SetInAt(i, Location::Any());
1114 }
1115 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001116}
1117
1118void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
1119 LOG(FATAL) << "Unimplemented";
1120}
1121
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001122void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001123 LocationSummary* locations =
1124 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001125 Primitive::Type field_type = instruction->GetFieldType();
1126 bool is_object_type = field_type == Primitive::kPrimNot;
1127 bool dies_at_entry = !is_object_type;
1128 locations->SetInAt(0, Location::RequiresRegister(), dies_at_entry);
1129 locations->SetInAt(1, Location::RequiresRegister(), dies_at_entry);
1130 if (is_object_type) {
1131 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001132 locations->AddTemp(Location::RequiresRegister());
1133 locations->AddTemp(Location::RequiresRegister());
1134 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001135}
1136
1137void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1138 LocationSummary* locations = instruction->GetLocations();
1139 CpuRegister obj = locations->InAt(0).AsX86_64().AsCpuRegister();
1140 CpuRegister value = locations->InAt(1).AsX86_64().AsCpuRegister();
1141 size_t offset = instruction->GetFieldOffset().SizeValue();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001142 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001143
1144 switch (field_type) {
1145 case Primitive::kPrimBoolean:
1146 case Primitive::kPrimByte: {
1147 __ movb(Address(obj, offset), value);
1148 break;
1149 }
1150
1151 case Primitive::kPrimShort:
1152 case Primitive::kPrimChar: {
1153 __ movw(Address(obj, offset), value);
1154 break;
1155 }
1156
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001157 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001158 case Primitive::kPrimNot: {
1159 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001160 if (field_type == Primitive::kPrimNot) {
1161 CpuRegister temp = locations->GetTemp(0).AsX86_64().AsCpuRegister();
1162 CpuRegister card = locations->GetTemp(1).AsX86_64().AsCpuRegister();
1163 codegen_->MarkGCCard(temp, card, obj, value);
1164 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001165 break;
1166 }
1167
1168 case Primitive::kPrimLong: {
1169 __ movq(Address(obj, offset), value);
1170 break;
1171 }
1172
1173 case Primitive::kPrimFloat:
1174 case Primitive::kPrimDouble:
1175 LOG(FATAL) << "Unimplemented register type " << field_type;
1176
1177 case Primitive::kPrimVoid:
1178 LOG(FATAL) << "Unreachable type " << field_type;
1179 }
1180}
1181
1182void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001183 LocationSummary* locations =
1184 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001185 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001186 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001187}
1188
1189void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1190 LocationSummary* locations = instruction->GetLocations();
1191 CpuRegister obj = locations->InAt(0).AsX86_64().AsCpuRegister();
1192 CpuRegister out = locations->Out().AsX86_64().AsCpuRegister();
1193 size_t offset = instruction->GetFieldOffset().SizeValue();
1194
1195 switch (instruction->GetType()) {
1196 case Primitive::kPrimBoolean: {
1197 __ movzxb(out, Address(obj, offset));
1198 break;
1199 }
1200
1201 case Primitive::kPrimByte: {
1202 __ movsxb(out, Address(obj, offset));
1203 break;
1204 }
1205
1206 case Primitive::kPrimShort: {
1207 __ movsxw(out, Address(obj, offset));
1208 break;
1209 }
1210
1211 case Primitive::kPrimChar: {
1212 __ movzxw(out, Address(obj, offset));
1213 break;
1214 }
1215
1216 case Primitive::kPrimInt:
1217 case Primitive::kPrimNot: {
1218 __ movl(out, Address(obj, offset));
1219 break;
1220 }
1221
1222 case Primitive::kPrimLong: {
1223 __ movq(out, Address(obj, offset));
1224 break;
1225 }
1226
1227 case Primitive::kPrimFloat:
1228 case Primitive::kPrimDouble:
1229 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1230
1231 case Primitive::kPrimVoid:
1232 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1233 }
1234}
1235
1236void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001237 LocationSummary* locations =
1238 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001239 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001240 if (instruction->HasUses()) {
1241 locations->SetOut(Location::SameAsFirstInput());
1242 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001243}
1244
1245void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001246 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001247 codegen_->AddSlowPath(slow_path);
1248
1249 LocationSummary* locations = instruction->GetLocations();
1250 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001251
1252 if (obj.IsRegister()) {
1253 __ cmpl(obj.AsX86_64().AsCpuRegister(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001254 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001255 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001256 } else {
1257 DCHECK(obj.IsConstant()) << obj;
1258 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1259 __ jmp(slow_path->GetEntryLabel());
1260 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001261 }
1262 __ j(kEqual, slow_path->GetEntryLabel());
1263}
1264
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001265void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001266 LocationSummary* locations =
1267 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001268 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
1269 locations->SetInAt(
1270 1, Location::RegisterOrConstant(instruction->InputAt(1)), Location::kDiesAtEntry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001271 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001272}
1273
1274void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
1275 LocationSummary* locations = instruction->GetLocations();
1276 CpuRegister obj = locations->InAt(0).AsX86_64().AsCpuRegister();
1277 Location index = locations->InAt(1);
1278
1279 switch (instruction->GetType()) {
1280 case Primitive::kPrimBoolean: {
1281 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1282 CpuRegister out = locations->Out().AsX86_64().AsCpuRegister();
1283 if (index.IsConstant()) {
1284 __ movzxb(out, Address(obj,
1285 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1286 } else {
1287 __ movzxb(out, Address(obj, index.AsX86_64().AsCpuRegister(), TIMES_1, data_offset));
1288 }
1289 break;
1290 }
1291
1292 case Primitive::kPrimByte: {
1293 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1294 CpuRegister out = locations->Out().AsX86_64().AsCpuRegister();
1295 if (index.IsConstant()) {
1296 __ movsxb(out, Address(obj,
1297 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1298 } else {
1299 __ movsxb(out, Address(obj, index.AsX86_64().AsCpuRegister(), TIMES_1, data_offset));
1300 }
1301 break;
1302 }
1303
1304 case Primitive::kPrimShort: {
1305 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1306 CpuRegister out = locations->Out().AsX86_64().AsCpuRegister();
1307 if (index.IsConstant()) {
1308 __ movsxw(out, Address(obj,
1309 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1310 } else {
1311 __ movsxw(out, Address(obj, index.AsX86_64().AsCpuRegister(), TIMES_2, data_offset));
1312 }
1313 break;
1314 }
1315
1316 case Primitive::kPrimChar: {
1317 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1318 CpuRegister out = locations->Out().AsX86_64().AsCpuRegister();
1319 if (index.IsConstant()) {
1320 __ movzxw(out, Address(obj,
1321 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1322 } else {
1323 __ movzxw(out, Address(obj, index.AsX86_64().AsCpuRegister(), TIMES_2, data_offset));
1324 }
1325 break;
1326 }
1327
1328 case Primitive::kPrimInt:
1329 case Primitive::kPrimNot: {
1330 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1331 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1332 CpuRegister out = locations->Out().AsX86_64().AsCpuRegister();
1333 if (index.IsConstant()) {
1334 __ movl(out, Address(obj,
1335 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1336 } else {
1337 __ movl(out, Address(obj, index.AsX86_64().AsCpuRegister(), TIMES_4, data_offset));
1338 }
1339 break;
1340 }
1341
1342 case Primitive::kPrimLong: {
1343 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1344 CpuRegister out = locations->Out().AsX86_64().AsCpuRegister();
1345 if (index.IsConstant()) {
1346 __ movq(out, Address(obj,
1347 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
1348 } else {
1349 __ movq(out, Address(obj, index.AsX86_64().AsCpuRegister(), TIMES_8, data_offset));
1350 }
1351 break;
1352 }
1353
1354 case Primitive::kPrimFloat:
1355 case Primitive::kPrimDouble:
1356 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1357
1358 case Primitive::kPrimVoid:
1359 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1360 }
1361}
1362
1363void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001364 Primitive::Type value_type = instruction->GetComponentType();
1365 bool is_object = value_type == Primitive::kPrimNot;
1366 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1367 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
1368 if (is_object) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001369 InvokeRuntimeCallingConvention calling_convention;
1370 locations->SetInAt(0, X86_64CpuLocation(calling_convention.GetRegisterAt(0)));
1371 locations->SetInAt(1, X86_64CpuLocation(calling_convention.GetRegisterAt(1)));
1372 locations->SetInAt(2, X86_64CpuLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001373 } else {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001374 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
1375 locations->SetInAt(
1376 1, Location::RegisterOrConstant(instruction->InputAt(1)), Location::kDiesAtEntry);
1377 locations->SetInAt(2, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001378 if (value_type == Primitive::kPrimLong) {
1379 locations->SetInAt(2, Location::RequiresRegister(), Location::kDiesAtEntry);
1380 } else {
1381 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)), Location::kDiesAtEntry);
1382 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001383 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001384}
1385
1386void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
1387 LocationSummary* locations = instruction->GetLocations();
1388 CpuRegister obj = locations->InAt(0).AsX86_64().AsCpuRegister();
1389 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001390 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001391 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001392
1393 switch (value_type) {
1394 case Primitive::kPrimBoolean:
1395 case Primitive::kPrimByte: {
1396 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001397 if (index.IsConstant()) {
1398 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001399 if (value.IsRegister()) {
1400 __ movb(Address(obj, offset), value.AsX86_64().AsCpuRegister());
1401 } else {
1402 __ movb(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1403 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001404 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001405 if (value.IsRegister()) {
1406 __ movb(Address(obj, index.AsX86_64().AsCpuRegister(), TIMES_1, data_offset),
1407 value.AsX86_64().AsCpuRegister());
1408 } else {
1409 __ movb(Address(obj, index.AsX86_64().AsCpuRegister(), TIMES_1, data_offset),
1410 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1411 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001412 }
1413 break;
1414 }
1415
1416 case Primitive::kPrimShort:
1417 case Primitive::kPrimChar: {
1418 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001419 if (index.IsConstant()) {
1420 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001421 if (value.IsRegister()) {
1422 __ movw(Address(obj, offset), value.AsX86_64().AsCpuRegister());
1423 } else {
1424 __ movw(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1425 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001426 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001427 if (value.IsRegister()) {
1428 __ movw(Address(obj, index.AsX86_64().AsCpuRegister(), TIMES_2, data_offset),
1429 value.AsX86_64().AsCpuRegister());
1430 } else {
1431 __ movw(Address(obj, index.AsX86_64().AsCpuRegister(), TIMES_2, data_offset),
1432 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1433 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001434 }
1435 break;
1436 }
1437
1438 case Primitive::kPrimInt: {
1439 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001440 if (index.IsConstant()) {
1441 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001442 if (value.IsRegister()) {
1443 __ movl(Address(obj, offset), value.AsX86_64().AsCpuRegister());
1444 } else {
1445 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1446 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001447 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001448 if (value.IsRegister()) {
1449 __ movl(Address(obj, index.AsX86_64().AsCpuRegister(), TIMES_4, data_offset),
1450 value.AsX86_64().AsCpuRegister());
1451 } else {
1452 __ movl(Address(obj, index.AsX86_64().AsCpuRegister(), TIMES_4, data_offset),
1453 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1454 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001455 }
1456 break;
1457 }
1458
1459 case Primitive::kPrimNot: {
1460 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject), true));
1461 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001462 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001463 break;
1464 }
1465
1466 case Primitive::kPrimLong: {
1467 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001468 if (index.IsConstant()) {
1469 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001470 DCHECK(value.IsRegister());
1471 __ movq(Address(obj, offset), value.AsX86_64().AsCpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001472 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001473 DCHECK(value.IsRegister());
1474 __ movq(Address(obj, index.AsX86_64().AsCpuRegister(), TIMES_8, data_offset),
1475 value.AsX86_64().AsCpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001476 }
1477 break;
1478 }
1479
1480 case Primitive::kPrimFloat:
1481 case Primitive::kPrimDouble:
1482 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1483
1484 case Primitive::kPrimVoid:
1485 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1486 }
1487}
1488
1489void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001490 LocationSummary* locations =
1491 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001492 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001493 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001494}
1495
1496void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
1497 LocationSummary* locations = instruction->GetLocations();
1498 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1499 CpuRegister obj = locations->InAt(0).AsX86_64().AsCpuRegister();
1500 CpuRegister out = locations->Out().AsX86_64().AsCpuRegister();
1501 __ movl(out, Address(obj, offset));
1502}
1503
1504void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001505 LocationSummary* locations =
1506 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001507 locations->SetInAt(0, Location::RequiresRegister());
1508 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001509 if (instruction->HasUses()) {
1510 locations->SetOut(Location::SameAsFirstInput());
1511 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001512}
1513
1514void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
1515 LocationSummary* locations = instruction->GetLocations();
1516 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01001517 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001518 codegen_->AddSlowPath(slow_path);
1519
1520 CpuRegister index = locations->InAt(0).AsX86_64().AsCpuRegister();
1521 CpuRegister length = locations->InAt(1).AsX86_64().AsCpuRegister();
1522
1523 __ cmpl(index, length);
1524 __ j(kAboveEqual, slow_path->GetEntryLabel());
1525}
1526
1527void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
1528 CpuRegister card,
1529 CpuRegister object,
1530 CpuRegister value) {
1531 Label is_null;
1532 __ testl(value, value);
1533 __ j(kEqual, &is_null);
1534 __ gs()->movq(card, Address::Absolute(
1535 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
1536 __ movq(temp, object);
1537 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
1538 __ movb(Address(temp, card, TIMES_1, 0), card);
1539 __ Bind(&is_null);
1540}
1541
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001542void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
1543 temp->SetLocations(nullptr);
1544}
1545
1546void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
1547 // Nothing to do, this is driven by the code generator.
1548}
1549
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001550void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
1551 LOG(FATAL) << "Unimplemented";
1552}
1553
1554void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001555 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
1556}
1557
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001558void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
1559 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
1560}
1561
1562void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001563 HBasicBlock* block = instruction->GetBlock();
1564 if (block->GetLoopInformation() != nullptr) {
1565 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
1566 // The back edge will generate the suspend check.
1567 return;
1568 }
1569 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
1570 // The goto will generate the suspend check.
1571 return;
1572 }
1573 GenerateSuspendCheck(instruction, nullptr);
1574}
1575
1576void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
1577 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001578 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001579 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001580 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001581 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001582 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001583 if (successor == nullptr) {
1584 __ j(kNotEqual, slow_path->GetEntryLabel());
1585 __ Bind(slow_path->GetReturnLabel());
1586 } else {
1587 __ j(kEqual, codegen_->GetLabelOf(successor));
1588 __ jmp(slow_path->GetEntryLabel());
1589 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001590}
1591
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001592X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
1593 return codegen_->GetAssembler();
1594}
1595
1596void ParallelMoveResolverX86_64::EmitMove(size_t index) {
1597 MoveOperands* move = moves_.Get(index);
1598 Location source = move->GetSource();
1599 Location destination = move->GetDestination();
1600
1601 if (source.IsRegister()) {
1602 if (destination.IsRegister()) {
1603 __ movq(destination.AsX86_64().AsCpuRegister(), source.AsX86_64().AsCpuRegister());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001604 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001605 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
1606 source.AsX86_64().AsCpuRegister());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001607 } else {
1608 DCHECK(destination.IsDoubleStackSlot());
1609 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
1610 source.AsX86_64().AsCpuRegister());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001611 }
1612 } else if (source.IsStackSlot()) {
1613 if (destination.IsRegister()) {
1614 __ movl(destination.AsX86_64().AsX86_64().AsCpuRegister(),
1615 Address(CpuRegister(RSP), source.GetStackIndex()));
1616 } else {
1617 DCHECK(destination.IsStackSlot());
1618 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1619 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
1620 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001621 } else if (source.IsDoubleStackSlot()) {
1622 if (destination.IsRegister()) {
1623 __ movq(destination.AsX86_64().AsX86_64().AsCpuRegister(),
1624 Address(CpuRegister(RSP), source.GetStackIndex()));
1625 } else {
1626 DCHECK(destination.IsDoubleStackSlot());
1627 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1628 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
1629 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001630 } else if (source.IsConstant()) {
1631 HConstant* constant = source.GetConstant();
1632 if (constant->IsIntConstant()) {
1633 Immediate imm(constant->AsIntConstant()->GetValue());
1634 if (destination.IsRegister()) {
1635 __ movl(destination.AsX86_64().AsCpuRegister(), imm);
1636 } else {
1637 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
1638 }
1639 } else if (constant->IsLongConstant()) {
1640 int64_t value = constant->AsLongConstant()->GetValue();
1641 if (destination.IsRegister()) {
1642 __ movq(destination.AsX86_64().AsCpuRegister(), Immediate(value));
1643 } else {
1644 __ movq(CpuRegister(TMP), Immediate(value));
1645 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
1646 }
1647 } else {
1648 LOG(FATAL) << "Unimplemented constant type";
1649 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001650 } else {
1651 LOG(FATAL) << "Unimplemented";
1652 }
1653}
1654
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001655void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001656 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001657 __ movl(Address(CpuRegister(RSP), mem), reg);
1658 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001659}
1660
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001661void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001662 ScratchRegisterScope ensure_scratch(
1663 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
1664
1665 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
1666 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
1667 __ movl(CpuRegister(ensure_scratch.GetRegister()),
1668 Address(CpuRegister(RSP), mem2 + stack_offset));
1669 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
1670 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
1671 CpuRegister(ensure_scratch.GetRegister()));
1672}
1673
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001674void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
1675 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
1676 __ movq(Address(CpuRegister(RSP), mem), reg);
1677 __ movq(reg, CpuRegister(TMP));
1678}
1679
1680void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
1681 ScratchRegisterScope ensure_scratch(
1682 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
1683
1684 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
1685 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
1686 __ movq(CpuRegister(ensure_scratch.GetRegister()),
1687 Address(CpuRegister(RSP), mem2 + stack_offset));
1688 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
1689 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
1690 CpuRegister(ensure_scratch.GetRegister()));
1691}
1692
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001693void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
1694 MoveOperands* move = moves_.Get(index);
1695 Location source = move->GetSource();
1696 Location destination = move->GetDestination();
1697
1698 if (source.IsRegister() && destination.IsRegister()) {
1699 __ xchgq(destination.AsX86_64().AsCpuRegister(), source.AsX86_64().AsCpuRegister());
1700 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001701 Exchange32(source.AsX86_64().AsCpuRegister(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001702 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001703 Exchange32(destination.AsX86_64().AsCpuRegister(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001704 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001705 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
1706 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
1707 Exchange64(source.AsX86_64().AsCpuRegister(), destination.GetStackIndex());
1708 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
1709 Exchange64(destination.AsX86_64().AsCpuRegister(), source.GetStackIndex());
1710 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
1711 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001712 } else {
1713 LOG(FATAL) << "Unimplemented";
1714 }
1715}
1716
1717
1718void ParallelMoveResolverX86_64::SpillScratch(int reg) {
1719 __ pushq(CpuRegister(reg));
1720}
1721
1722
1723void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
1724 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001725}
1726
1727} // namespace x86_64
1728} // namespace art