blob: 9e63f8bc526ee626eff467855bafd93822e819c0 [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
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010063class SlowPathCodeX86_64 : public SlowPathCode {
64 public:
65 SlowPathCodeX86_64() : entry_label_(), exit_label_() {}
66
67 Label* GetEntryLabel() { return &entry_label_; }
68 Label* GetExitLabel() { return &exit_label_; }
69
70 private:
71 Label entry_label_;
72 Label exit_label_;
73
74 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86_64);
75};
76
77class NullCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010078 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010079 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080
81 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
82 __ Bind(GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010083 __ gs()->call(
84 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowNullPointer), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +010085 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010086 }
87
88 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010089 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010090 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
91};
92
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010093class StackOverflowCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010094 public:
95 StackOverflowCheckSlowPathX86_64() {}
96
97 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
98 __ Bind(GetEntryLabel());
99 __ addq(CpuRegister(RSP),
100 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
101 __ gs()->jmp(
102 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowStackOverflow), true));
103 }
104
105 private:
106 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86_64);
107};
108
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100109class SuspendCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000110 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100111 explicit SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
112 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000113
114 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100115 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000116 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100117 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000118 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pTestSuspend), true));
119 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100120 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100121 if (successor_ == nullptr) {
122 __ jmp(GetReturnLabel());
123 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100124 __ jmp(x64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100125 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000126 }
127
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100128 Label* GetReturnLabel() {
129 DCHECK(successor_ == nullptr);
130 return &return_label_;
131 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000132
133 private:
134 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100135 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000136 Label return_label_;
137
138 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
139};
140
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100141class BoundsCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100142 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100143 BoundsCheckSlowPathX86_64(HBoundsCheck* instruction,
144 Location index_location,
145 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100146 : instruction_(instruction),
147 index_location_(index_location),
148 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100149
150 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100151 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100152 __ Bind(GetEntryLabel());
153 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100154 x64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), index_location_);
155 x64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(1)), length_location_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100156 __ gs()->call(Address::Absolute(
157 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowArrayBounds), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100158 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100159 }
160
161 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100162 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100163 const Location index_location_;
164 const Location length_location_;
165
166 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
167};
168
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100169#undef __
170#define __ reinterpret_cast<X86_64Assembler*>(GetAssembler())->
171
Dave Allison20dfc792014-06-16 20:44:29 -0700172inline Condition X86_64Condition(IfCondition cond) {
173 switch (cond) {
174 case kCondEQ: return kEqual;
175 case kCondNE: return kNotEqual;
176 case kCondLT: return kLess;
177 case kCondLE: return kLessEqual;
178 case kCondGT: return kGreater;
179 case kCondGE: return kGreaterEqual;
180 default:
181 LOG(FATAL) << "Unknown if condition";
182 }
183 return kEqual;
184}
185
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100186void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
187 stream << X86_64ManagedRegister::FromCpuRegister(Register(reg));
188}
189
190void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
191 stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg));
192}
193
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100194size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
195 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
196 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100197}
198
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100199size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
200 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
201 return kX86_64WordSize;
202}
203
204size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
205 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
206 return kX86_64WordSize;
207}
208
209size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
210 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
211 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100212}
213
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100214CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100215 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfFloatRegisters, 0),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100216 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100217 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000218 instruction_visitor_(graph, this),
219 move_resolver_(graph->GetArena(), this) {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100220
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100221size_t CodeGeneratorX86_64::FrameEntrySpillSize() const {
222 return kNumberOfPushedRegistersAtEntry * kX86_64WordSize;
223}
224
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100225InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
226 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100227 : HGraphVisitor(graph),
228 assembler_(codegen->GetAssembler()),
229 codegen_(codegen) {}
230
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100231Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100232 switch (type) {
233 case Primitive::kPrimLong:
234 case Primitive::kPrimByte:
235 case Primitive::kPrimBoolean:
236 case Primitive::kPrimChar:
237 case Primitive::kPrimShort:
238 case Primitive::kPrimInt:
239 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100240 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100241 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100242 }
243
244 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100245 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100246 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100247 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100248 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100249
250 case Primitive::kPrimVoid:
251 LOG(FATAL) << "Unreachable type " << type;
252 }
253
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100254 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100255}
256
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100257void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100258 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100259 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100260
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000261 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100262 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000263
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100264 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100265 blocked_core_registers_[RBX] = true;
266 blocked_core_registers_[RBP] = true;
267 blocked_core_registers_[R12] = true;
268 blocked_core_registers_[R13] = true;
269 blocked_core_registers_[R14] = true;
270 blocked_core_registers_[R15] = true;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100271
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100272 blocked_fpu_registers_[XMM12] = true;
273 blocked_fpu_registers_[XMM13] = true;
274 blocked_fpu_registers_[XMM14] = true;
275 blocked_fpu_registers_[XMM15] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100276}
277
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100278void CodeGeneratorX86_64::GenerateFrameEntry() {
279 // Create a fake register to mimic Quick.
280 static const int kFakeReturnRegister = 16;
281 core_spill_mask_ |= (1 << kFakeReturnRegister);
282
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100283 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700284 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100285
286 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
287 __ testq(CpuRegister(RAX), Address(
288 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100289 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100290 }
291
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100292 // The return PC has already been pushed on the stack.
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100293 __ subq(CpuRegister(RSP),
294 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
295
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100296 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100297 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86_64();
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100298 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100299
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100300 __ gs()->cmpq(CpuRegister(RSP),
301 Address::Absolute(Thread::StackEndOffset<kX86_64WordSize>(), true));
302 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100303 }
304
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100305 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
306}
307
308void CodeGeneratorX86_64::GenerateFrameExit() {
309 __ addq(CpuRegister(RSP),
310 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
311}
312
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100313void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
314 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100315}
316
317void InstructionCodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
318 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
319}
320
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100321Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
322 switch (load->GetType()) {
323 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100324 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100325 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
326 break;
327
328 case Primitive::kPrimInt:
329 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100330 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100331 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100332
333 case Primitive::kPrimBoolean:
334 case Primitive::kPrimByte:
335 case Primitive::kPrimChar:
336 case Primitive::kPrimShort:
337 case Primitive::kPrimVoid:
338 LOG(FATAL) << "Unexpected type " << load->GetType();
339 }
340
341 LOG(FATAL) << "Unreachable";
342 return Location();
343}
344
345void CodeGeneratorX86_64::Move(Location destination, Location source) {
346 if (source.Equals(destination)) {
347 return;
348 }
349 if (destination.IsRegister()) {
350 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100351 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100352 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100353 __ movd(destination.As<CpuRegister>(), source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100354 } else if (source.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100355 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100356 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100357 } else {
358 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100359 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100360 Address(CpuRegister(RSP), source.GetStackIndex()));
361 }
362 } else if (destination.IsFpuRegister()) {
363 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100364 __ movd(destination.As<XmmRegister>(), source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100365 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100366 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100367 } else if (source.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100368 __ movss(destination.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100369 Address(CpuRegister(RSP), source.GetStackIndex()));
370 } else {
371 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100372 __ movsd(destination.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100373 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100374 }
375 } else if (destination.IsStackSlot()) {
376 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100377 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100378 source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100379 } else if (source.IsFpuRegister()) {
380 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100381 source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100382 } else {
383 DCHECK(source.IsStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000384 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
385 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100386 }
387 } else {
388 DCHECK(destination.IsDoubleStackSlot());
389 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100390 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100391 source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100392 } else if (source.IsFpuRegister()) {
393 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100394 source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100395 } else {
396 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000397 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
398 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100399 }
400 }
401}
402
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100403void CodeGeneratorX86_64::Move(HInstruction* instruction,
404 Location location,
405 HInstruction* move_for) {
Roland Levillain476df552014-10-09 17:51:36 +0100406 if (instruction->IsIntConstant()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100407 Immediate imm(instruction->AsIntConstant()->GetValue());
408 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100409 __ movl(location.As<CpuRegister>(), imm);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100410 } else {
411 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
412 }
Roland Levillain476df552014-10-09 17:51:36 +0100413 } else if (instruction->IsLongConstant()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100414 int64_t value = instruction->AsLongConstant()->GetValue();
415 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100416 __ movq(location.As<CpuRegister>(), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100417 } else {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000418 __ movq(CpuRegister(TMP), Immediate(value));
419 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100420 }
Roland Levillain476df552014-10-09 17:51:36 +0100421 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100422 switch (instruction->GetType()) {
423 case Primitive::kPrimBoolean:
424 case Primitive::kPrimByte:
425 case Primitive::kPrimChar:
426 case Primitive::kPrimShort:
427 case Primitive::kPrimInt:
428 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100429 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100430 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
431 break;
432
433 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100434 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100435 Move(location, Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
436 break;
437
438 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100439 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100440 }
441 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100442 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100443 switch (instruction->GetType()) {
444 case Primitive::kPrimBoolean:
445 case Primitive::kPrimByte:
446 case Primitive::kPrimChar:
447 case Primitive::kPrimShort:
448 case Primitive::kPrimInt:
449 case Primitive::kPrimNot:
450 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100451 case Primitive::kPrimFloat:
452 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100453 Move(location, instruction->GetLocations()->Out());
454 break;
455
456 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100457 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100458 }
459 }
460}
461
462void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
463 got->SetLocations(nullptr);
464}
465
466void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
467 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100468 DCHECK(!successor->IsExitBlock());
469
470 HBasicBlock* block = got->GetBlock();
471 HInstruction* previous = got->GetPrevious();
472
473 HLoopInformation* info = block->GetLoopInformation();
474 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
475 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
476 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
477 return;
478 }
479
480 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
481 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
482 }
483 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100484 __ jmp(codegen_->GetLabelOf(successor));
485 }
486}
487
488void LocationsBuilderX86_64::VisitExit(HExit* exit) {
489 exit->SetLocations(nullptr);
490}
491
492void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
493 if (kIsDebugBuild) {
494 __ Comment("Unreachable");
495 __ int3();
496 }
497}
498
499void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100500 LocationSummary* locations =
501 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100502 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100503 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100504 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100505 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100506}
507
508void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700509 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100510 if (cond->IsIntConstant()) {
511 // Constant condition, statically compared against 1.
512 int32_t cond_value = cond->AsIntConstant()->GetValue();
513 if (cond_value == 1) {
514 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
515 if_instr->IfTrueSuccessor())) {
516 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100517 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100518 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100519 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100520 DCHECK_EQ(cond_value, 0);
521 }
522 } else {
523 bool materialized =
524 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
525 // Moves do not affect the eflags register, so if the condition is
526 // evaluated just before the if, we don't need to evaluate it
527 // again.
528 bool eflags_set = cond->IsCondition()
529 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
530 if (materialized) {
531 if (!eflags_set) {
532 // Materialized condition, compare against 0.
533 Location lhs = if_instr->GetLocations()->InAt(0);
534 if (lhs.IsRegister()) {
535 __ cmpl(lhs.As<CpuRegister>(), Immediate(0));
536 } else {
537 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
538 Immediate(0));
539 }
540 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
541 } else {
542 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
543 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
544 }
545 } else {
546 Location lhs = cond->GetLocations()->InAt(0);
547 Location rhs = cond->GetLocations()->InAt(1);
548 if (rhs.IsRegister()) {
549 __ cmpl(lhs.As<CpuRegister>(), rhs.As<CpuRegister>());
550 } else if (rhs.IsConstant()) {
551 __ cmpl(lhs.As<CpuRegister>(),
552 Immediate(rhs.GetConstant()->AsIntConstant()->GetValue()));
553 } else {
554 __ cmpl(lhs.As<CpuRegister>(),
555 Address(CpuRegister(RSP), rhs.GetStackIndex()));
556 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100557 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
558 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700559 }
Dave Allison20dfc792014-06-16 20:44:29 -0700560 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100561 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
562 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700563 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100564 }
565}
566
567void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
568 local->SetLocations(nullptr);
569}
570
571void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
572 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
573}
574
575void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
576 local->SetLocations(nullptr);
577}
578
579void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
580 // Nothing to do, this is driven by the code generator.
581}
582
583void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100584 LocationSummary* locations =
585 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100586 switch (store->InputAt(1)->GetType()) {
587 case Primitive::kPrimBoolean:
588 case Primitive::kPrimByte:
589 case Primitive::kPrimChar:
590 case Primitive::kPrimShort:
591 case Primitive::kPrimInt:
592 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100593 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100594 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
595 break;
596
597 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100598 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100599 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
600 break;
601
602 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100603 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100604 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100605}
606
607void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
608}
609
Dave Allison20dfc792014-06-16 20:44:29 -0700610void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100611 LocationSummary* locations =
612 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100613 locations->SetInAt(0, Location::RequiresRegister());
614 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100615 if (comp->NeedsMaterialization()) {
616 locations->SetOut(Location::RequiresRegister());
617 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100618}
619
Dave Allison20dfc792014-06-16 20:44:29 -0700620void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
621 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100622 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100623 CpuRegister reg = locations->Out().As<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100624 // Clear register: setcc only sets the low byte.
625 __ xorq(reg, reg);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100626 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100627 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100628 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100629 } else if (locations->InAt(1).IsConstant()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100630 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100631 Immediate(locations->InAt(1).GetConstant()->AsIntConstant()->GetValue()));
632 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100633 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100634 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
635 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100636 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700637 }
638}
639
640void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
641 VisitCondition(comp);
642}
643
644void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
645 VisitCondition(comp);
646}
647
648void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
649 VisitCondition(comp);
650}
651
652void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
653 VisitCondition(comp);
654}
655
656void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
657 VisitCondition(comp);
658}
659
660void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
661 VisitCondition(comp);
662}
663
664void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
665 VisitCondition(comp);
666}
667
668void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
669 VisitCondition(comp);
670}
671
672void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
673 VisitCondition(comp);
674}
675
676void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
677 VisitCondition(comp);
678}
679
680void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
681 VisitCondition(comp);
682}
683
684void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
685 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100686}
687
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100688void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100689 LocationSummary* locations =
690 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100691 locations->SetInAt(0, Location::RequiresRegister());
692 locations->SetInAt(1, Location::RequiresRegister());
693 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100694}
695
696void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
697 Label greater, done;
698 LocationSummary* locations = compare->GetLocations();
699 switch (compare->InputAt(0)->GetType()) {
700 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100701 __ cmpq(locations->InAt(0).As<CpuRegister>(),
702 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100703 break;
704 default:
705 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
706 }
707
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100708 CpuRegister output = locations->Out().As<CpuRegister>();
709 __ movl(output, Immediate(0));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100710 __ j(kEqual, &done);
711 __ j(kGreater, &greater);
712
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100713 __ movl(output, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100714 __ jmp(&done);
715
716 __ Bind(&greater);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100717 __ movl(output, Immediate(1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100718
719 __ Bind(&done);
720}
721
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100722void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100723 LocationSummary* locations =
724 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100725 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100726}
727
728void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100729 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100730}
731
732void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100733 LocationSummary* locations =
734 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100735 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100736}
737
738void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100739 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100740}
741
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100742void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
743 LocationSummary* locations =
744 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
745 locations->SetOut(Location::ConstantLocation(constant));
746}
747
748void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
749 // Will be generated at use site.
750}
751
752void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
753 LocationSummary* locations =
754 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
755 locations->SetOut(Location::ConstantLocation(constant));
756}
757
758void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
759 // Will be generated at use site.
760}
761
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100762void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
763 ret->SetLocations(nullptr);
764}
765
766void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
767 codegen_->GenerateFrameExit();
768 __ ret();
769}
770
771void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100772 LocationSummary* locations =
773 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100774 switch (ret->InputAt(0)->GetType()) {
775 case Primitive::kPrimBoolean:
776 case Primitive::kPrimByte:
777 case Primitive::kPrimChar:
778 case Primitive::kPrimShort:
779 case Primitive::kPrimInt:
780 case Primitive::kPrimNot:
781 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100782 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100783 break;
784
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100785 case Primitive::kPrimFloat:
786 case Primitive::kPrimDouble:
787 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100788 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100789 break;
790
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100791 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100792 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100793 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100794}
795
796void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
797 if (kIsDebugBuild) {
798 switch (ret->InputAt(0)->GetType()) {
799 case Primitive::kPrimBoolean:
800 case Primitive::kPrimByte:
801 case Primitive::kPrimChar:
802 case Primitive::kPrimShort:
803 case Primitive::kPrimInt:
804 case Primitive::kPrimNot:
805 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100806 DCHECK_EQ(ret->GetLocations()->InAt(0).As<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100807 break;
808
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100809 case Primitive::kPrimFloat:
810 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100811 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100812 XMM0);
813 break;
814
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100815 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100816 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100817 }
818 }
819 codegen_->GenerateFrameExit();
820 __ ret();
821}
822
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100823Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
824 switch (type) {
825 case Primitive::kPrimBoolean:
826 case Primitive::kPrimByte:
827 case Primitive::kPrimChar:
828 case Primitive::kPrimShort:
829 case Primitive::kPrimInt:
830 case Primitive::kPrimNot: {
831 uint32_t index = gp_index_++;
832 stack_index_++;
833 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100834 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100835 } else {
836 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
837 }
838 }
839
840 case Primitive::kPrimLong: {
841 uint32_t index = gp_index_;
842 stack_index_ += 2;
843 if (index < calling_convention.GetNumberOfRegisters()) {
844 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100845 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100846 } else {
847 gp_index_ += 2;
848 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
849 }
850 }
851
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100852 case Primitive::kPrimFloat: {
853 uint32_t index = fp_index_++;
854 stack_index_++;
855 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100856 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100857 } else {
858 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
859 }
860 }
861
862 case Primitive::kPrimDouble: {
863 uint32_t index = fp_index_++;
864 stack_index_ += 2;
865 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100866 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100867 } else {
868 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
869 }
870 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100871
872 case Primitive::kPrimVoid:
873 LOG(FATAL) << "Unexpected parameter type " << type;
874 break;
875 }
876 return Location();
877}
878
879void LocationsBuilderX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100880 HandleInvoke(invoke);
881}
882
883void InstructionCodeGeneratorX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100884 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100885 uint32_t heap_reference_size = sizeof(mirror::HeapReference<mirror::Object>);
886 size_t index_in_cache = mirror::Array::DataOffset(heap_reference_size).SizeValue() +
887 invoke->GetIndexInDexCache() * heap_reference_size;
888
889 // TODO: Implement all kinds of calls:
890 // 1) boot -> boot
891 // 2) app -> boot
892 // 3) app -> app
893 //
894 // Currently we implement the app -> app logic, which looks up in the resolve cache.
895
896 // temp = method;
897 LoadCurrentMethod(temp);
898 // temp = temp->dex_cache_resolved_methods_;
899 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
900 // temp = temp[index_in_cache]
901 __ movl(temp, Address(temp, index_in_cache));
902 // (temp + offset_of_quick_compiled_code)()
903 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
904
905 DCHECK(!codegen_->IsLeafMethod());
906 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
907}
908
909void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
910 HandleInvoke(invoke);
911}
912
913void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100914 LocationSummary* locations =
915 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100916 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100917
918 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100919 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100920 HInstruction* input = invoke->InputAt(i);
921 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
922 }
923
924 switch (invoke->GetType()) {
925 case Primitive::kPrimBoolean:
926 case Primitive::kPrimByte:
927 case Primitive::kPrimChar:
928 case Primitive::kPrimShort:
929 case Primitive::kPrimInt:
930 case Primitive::kPrimNot:
931 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100932 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100933 break;
934
935 case Primitive::kPrimVoid:
936 break;
937
938 case Primitive::kPrimDouble:
939 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100940 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100941 break;
942 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100943}
944
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100945void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100946 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100947 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
948 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
949 LocationSummary* locations = invoke->GetLocations();
950 Location receiver = locations->InAt(0);
951 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
952 // temp = object->GetClass();
953 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100954 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
955 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100956 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100957 __ movl(temp, Address(receiver.As<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100958 }
959 // temp = temp->GetMethodAt(method_offset);
960 __ movl(temp, Address(temp, method_offset));
961 // call temp->GetEntryPoint();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100962 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
963
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100964 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100965 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100966}
967
Roland Levillain88cb1752014-10-20 16:36:47 +0100968void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
969 LocationSummary* locations =
970 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
971 switch (neg->GetResultType()) {
972 case Primitive::kPrimInt:
973 locations->SetInAt(0, Location::RequiresRegister());
974 locations->SetOut(Location::SameAsFirstInput());
975 break;
976
977 case Primitive::kPrimLong:
978 case Primitive::kPrimFloat:
979 case Primitive::kPrimDouble:
980 LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
981 break;
982
983 default:
984 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
985 }
986}
987
988void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
989 LocationSummary* locations = neg->GetLocations();
990 Location out = locations->Out();
991 Location in = locations->InAt(0);
992 switch (neg->GetResultType()) {
993 case Primitive::kPrimInt:
994 DCHECK(in.IsRegister());
995 __ negl(out.As<CpuRegister>());
996 break;
997
998 case Primitive::kPrimLong:
999 case Primitive::kPrimFloat:
1000 case Primitive::kPrimDouble:
1001 LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
1002 break;
1003
1004 default:
1005 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1006 }
1007}
1008
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001009void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001010 LocationSummary* locations =
1011 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001012 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001013 case Primitive::kPrimInt: {
1014 locations->SetInAt(0, Location::RequiresRegister());
1015 locations->SetInAt(1, Location::Any());
1016 locations->SetOut(Location::SameAsFirstInput());
1017 break;
1018 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001019
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001020 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001021 locations->SetInAt(0, Location::RequiresRegister());
1022 locations->SetInAt(1, Location::RequiresRegister());
1023 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001024 break;
1025 }
1026
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001027 case Primitive::kPrimDouble:
1028 case Primitive::kPrimFloat: {
1029 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001030 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001031 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001032 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001033 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001034
1035 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001036 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001037 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001038}
1039
1040void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1041 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001042 Location first = locations->InAt(0);
1043 Location second = locations->InAt(1);
1044
1045 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001046 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001047 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001048 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001049 __ addl(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001050 } else if (second.IsConstant()) {
1051 HConstant* instruction = second.GetConstant();
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001052 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001053 __ addl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001054 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001055 __ addl(first.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001056 Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001057 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001058 break;
1059 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001060
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001061 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001062 __ addq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001063 break;
1064 }
1065
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001066 case Primitive::kPrimFloat: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001067 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001068 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001069 }
1070
1071 case Primitive::kPrimDouble: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001072 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001073 break;
1074 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001075
1076 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001077 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001078 }
1079}
1080
1081void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001082 LocationSummary* locations =
1083 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001084 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001085 case Primitive::kPrimInt: {
1086 locations->SetInAt(0, Location::RequiresRegister());
1087 locations->SetInAt(1, Location::Any());
1088 locations->SetOut(Location::SameAsFirstInput());
1089 break;
1090 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001091 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001092 locations->SetInAt(0, Location::RequiresRegister());
1093 locations->SetInAt(1, Location::RequiresRegister());
1094 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001095 break;
1096 }
1097
1098 case Primitive::kPrimBoolean:
1099 case Primitive::kPrimByte:
1100 case Primitive::kPrimChar:
1101 case Primitive::kPrimShort:
1102 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
1103 break;
1104
1105 default:
1106 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
1107 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001108}
1109
1110void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1111 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001112 DCHECK_EQ(locations->InAt(0).As<CpuRegister>().AsRegister(),
1113 locations->Out().As<CpuRegister>().AsRegister());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001114 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001115 case Primitive::kPrimInt: {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001116 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001117 __ subl(locations->InAt(0).As<CpuRegister>(),
1118 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001119 } else if (locations->InAt(1).IsConstant()) {
1120 HConstant* instruction = locations->InAt(1).GetConstant();
1121 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001122 __ subl(locations->InAt(0).As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001123 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001124 __ subl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001125 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
1126 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001127 break;
1128 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001129 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001130 __ subq(locations->InAt(0).As<CpuRegister>(),
1131 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001132 break;
1133 }
1134
1135 case Primitive::kPrimBoolean:
1136 case Primitive::kPrimByte:
1137 case Primitive::kPrimChar:
1138 case Primitive::kPrimShort:
1139 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
1140 break;
1141
1142 default:
1143 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
1144 }
1145}
1146
Calin Juravle34bacdf2014-10-07 20:23:36 +01001147void LocationsBuilderX86_64::VisitMul(HMul* mul) {
1148 LocationSummary* locations =
1149 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1150 switch (mul->GetResultType()) {
1151 case Primitive::kPrimInt: {
1152 locations->SetInAt(0, Location::RequiresRegister());
1153 locations->SetInAt(1, Location::Any());
1154 locations->SetOut(Location::SameAsFirstInput());
1155 break;
1156 }
1157 case Primitive::kPrimLong: {
1158 locations->SetInAt(0, Location::RequiresRegister());
1159 locations->SetInAt(1, Location::RequiresRegister());
1160 locations->SetOut(Location::SameAsFirstInput());
1161 break;
1162 }
1163
1164 case Primitive::kPrimBoolean:
1165 case Primitive::kPrimByte:
1166 case Primitive::kPrimChar:
1167 case Primitive::kPrimShort:
1168 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
1169 break;
1170
1171 default:
1172 LOG(FATAL) << "Unimplemented mul type " << mul->GetResultType();
1173 }
1174}
1175
1176void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
1177 LocationSummary* locations = mul->GetLocations();
1178 Location first = locations->InAt(0);
1179 Location second = locations->InAt(1);
1180 DCHECK(first.Equals(locations->Out()));
1181 switch (mul->GetResultType()) {
1182 case Primitive::kPrimInt: {
1183 if (second.IsRegister()) {
1184 __ imull(first.As<CpuRegister>(), second.As<CpuRegister>());
1185 } else if (second.IsConstant()) {
1186 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1187 __ imull(first.As<CpuRegister>(), imm);
1188 } else {
1189 DCHECK(second.IsStackSlot());
1190 __ imull(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
1191 }
1192 break;
1193 }
1194 case Primitive::kPrimLong: {
1195 __ imulq(first.As<CpuRegister>(), second.As<CpuRegister>());
1196 break;
1197 }
1198
1199 case Primitive::kPrimBoolean:
1200 case Primitive::kPrimByte:
1201 case Primitive::kPrimChar:
1202 case Primitive::kPrimShort:
1203 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
1204 break;
1205
1206 default:
1207 LOG(FATAL) << "Unimplemented mul type " << mul->GetResultType();
1208 }
1209}
1210
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001211void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001212 LocationSummary* locations =
1213 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001214 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001215 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1216 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1217 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001218}
1219
1220void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
1221 InvokeRuntimeCallingConvention calling_convention;
1222 LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
1223 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1224
1225 __ gs()->call(Address::Absolute(
1226 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
1227
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001228 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001229 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001230}
1231
1232void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001233 LocationSummary* locations =
1234 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001235 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1236 if (location.IsStackSlot()) {
1237 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1238 } else if (location.IsDoubleStackSlot()) {
1239 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1240 }
1241 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001242}
1243
1244void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
1245 // Nothing to do, the parameter is already at its location.
1246}
1247
1248void LocationsBuilderX86_64::VisitNot(HNot* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001249 LocationSummary* locations =
1250 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001251 locations->SetInAt(0, Location::RequiresRegister());
1252 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001253}
1254
1255void InstructionCodeGeneratorX86_64::VisitNot(HNot* instruction) {
1256 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001257 DCHECK_EQ(locations->InAt(0).As<CpuRegister>().AsRegister(),
1258 locations->Out().As<CpuRegister>().AsRegister());
1259 __ xorq(locations->Out().As<CpuRegister>(), Immediate(1));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001260}
1261
1262void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001263 LocationSummary* locations =
1264 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001265 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1266 locations->SetInAt(i, Location::Any());
1267 }
1268 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001269}
1270
1271void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
1272 LOG(FATAL) << "Unimplemented";
1273}
1274
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001275void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001276 LocationSummary* locations =
1277 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001278 Primitive::Type field_type = instruction->GetFieldType();
1279 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001280 locations->SetInAt(0, Location::RequiresRegister());
1281 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001282 if (is_object_type) {
1283 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001284 locations->AddTemp(Location::RequiresRegister());
1285 locations->AddTemp(Location::RequiresRegister());
1286 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001287}
1288
1289void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1290 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001291 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
1292 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001293 size_t offset = instruction->GetFieldOffset().SizeValue();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001294 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001295
1296 switch (field_type) {
1297 case Primitive::kPrimBoolean:
1298 case Primitive::kPrimByte: {
1299 __ movb(Address(obj, offset), value);
1300 break;
1301 }
1302
1303 case Primitive::kPrimShort:
1304 case Primitive::kPrimChar: {
1305 __ movw(Address(obj, offset), value);
1306 break;
1307 }
1308
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001309 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001310 case Primitive::kPrimNot: {
1311 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001312 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001313 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
1314 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001315 codegen_->MarkGCCard(temp, card, obj, value);
1316 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001317 break;
1318 }
1319
1320 case Primitive::kPrimLong: {
1321 __ movq(Address(obj, offset), value);
1322 break;
1323 }
1324
1325 case Primitive::kPrimFloat:
1326 case Primitive::kPrimDouble:
1327 LOG(FATAL) << "Unimplemented register type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001328 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001329 case Primitive::kPrimVoid:
1330 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001331 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001332 }
1333}
1334
1335void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001336 LocationSummary* locations =
1337 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001338 locations->SetInAt(0, Location::RequiresRegister());
1339 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001340}
1341
1342void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1343 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001344 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
1345 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001346 size_t offset = instruction->GetFieldOffset().SizeValue();
1347
1348 switch (instruction->GetType()) {
1349 case Primitive::kPrimBoolean: {
1350 __ movzxb(out, Address(obj, offset));
1351 break;
1352 }
1353
1354 case Primitive::kPrimByte: {
1355 __ movsxb(out, Address(obj, offset));
1356 break;
1357 }
1358
1359 case Primitive::kPrimShort: {
1360 __ movsxw(out, Address(obj, offset));
1361 break;
1362 }
1363
1364 case Primitive::kPrimChar: {
1365 __ movzxw(out, Address(obj, offset));
1366 break;
1367 }
1368
1369 case Primitive::kPrimInt:
1370 case Primitive::kPrimNot: {
1371 __ movl(out, Address(obj, offset));
1372 break;
1373 }
1374
1375 case Primitive::kPrimLong: {
1376 __ movq(out, Address(obj, offset));
1377 break;
1378 }
1379
1380 case Primitive::kPrimFloat:
1381 case Primitive::kPrimDouble:
1382 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001383 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001384 case Primitive::kPrimVoid:
1385 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001386 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001387 }
1388}
1389
1390void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001391 LocationSummary* locations =
1392 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001393 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001394 if (instruction->HasUses()) {
1395 locations->SetOut(Location::SameAsFirstInput());
1396 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001397}
1398
1399void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001400 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001401 codegen_->AddSlowPath(slow_path);
1402
1403 LocationSummary* locations = instruction->GetLocations();
1404 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001405
1406 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001407 __ cmpl(obj.As<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001408 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001409 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001410 } else {
1411 DCHECK(obj.IsConstant()) << obj;
1412 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1413 __ jmp(slow_path->GetEntryLabel());
1414 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001415 }
1416 __ j(kEqual, slow_path->GetEntryLabel());
1417}
1418
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001419void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001420 LocationSummary* locations =
1421 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001422 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001423 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001424 1, Location::RegisterOrConstant(instruction->InputAt(1)));
1425 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001426}
1427
1428void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
1429 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001430 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001431 Location index = locations->InAt(1);
1432
1433 switch (instruction->GetType()) {
1434 case Primitive::kPrimBoolean: {
1435 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001436 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001437 if (index.IsConstant()) {
1438 __ movzxb(out, Address(obj,
1439 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1440 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001441 __ movzxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001442 }
1443 break;
1444 }
1445
1446 case Primitive::kPrimByte: {
1447 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001448 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001449 if (index.IsConstant()) {
1450 __ movsxb(out, Address(obj,
1451 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1452 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001453 __ movsxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001454 }
1455 break;
1456 }
1457
1458 case Primitive::kPrimShort: {
1459 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001460 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001461 if (index.IsConstant()) {
1462 __ movsxw(out, Address(obj,
1463 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1464 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001465 __ movsxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001466 }
1467 break;
1468 }
1469
1470 case Primitive::kPrimChar: {
1471 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001472 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001473 if (index.IsConstant()) {
1474 __ movzxw(out, Address(obj,
1475 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1476 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001477 __ movzxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001478 }
1479 break;
1480 }
1481
1482 case Primitive::kPrimInt:
1483 case Primitive::kPrimNot: {
1484 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1485 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001486 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001487 if (index.IsConstant()) {
1488 __ movl(out, Address(obj,
1489 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1490 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001491 __ movl(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001492 }
1493 break;
1494 }
1495
1496 case Primitive::kPrimLong: {
1497 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001498 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001499 if (index.IsConstant()) {
1500 __ movq(out, Address(obj,
1501 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
1502 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001503 __ movq(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001504 }
1505 break;
1506 }
1507
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001508 case Primitive::kPrimFloat: {
1509 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1510 XmmRegister out = locations->Out().As<XmmRegister>();
1511 if (index.IsConstant()) {
1512 __ movss(out, Address(obj,
1513 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1514 } else {
1515 __ movss(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
1516 }
1517 break;
1518 }
1519
1520 case Primitive::kPrimDouble: {
1521 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1522 XmmRegister out = locations->Out().As<XmmRegister>();
1523 if (index.IsConstant()) {
1524 __ movsd(out, Address(obj,
1525 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
1526 } else {
1527 __ movsd(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
1528 }
1529 break;
1530 }
1531
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001532 case Primitive::kPrimVoid:
1533 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001534 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001535 }
1536}
1537
1538void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001539 Primitive::Type value_type = instruction->GetComponentType();
1540 bool is_object = value_type == Primitive::kPrimNot;
1541 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1542 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
1543 if (is_object) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001544 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001545 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1546 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1547 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001548 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001549 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001550 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001551 1, Location::RegisterOrConstant(instruction->InputAt(1)));
1552 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001553 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001554 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001555 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
1556 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001557 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001558 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001559 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001560 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001561}
1562
1563void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
1564 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001565 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001566 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001567 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001568 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001569
1570 switch (value_type) {
1571 case Primitive::kPrimBoolean:
1572 case Primitive::kPrimByte: {
1573 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001574 if (index.IsConstant()) {
1575 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001576 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001577 __ movb(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001578 } else {
1579 __ movb(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1580 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001581 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001582 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001583 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
1584 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001585 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001586 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001587 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1588 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001589 }
1590 break;
1591 }
1592
1593 case Primitive::kPrimShort:
1594 case Primitive::kPrimChar: {
1595 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001596 if (index.IsConstant()) {
1597 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001598 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001599 __ movw(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001600 } else {
1601 __ movw(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1602 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001603 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001604 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001605 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
1606 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001607 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001608 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001609 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1610 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001611 }
1612 break;
1613 }
1614
1615 case Primitive::kPrimInt: {
1616 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001617 if (index.IsConstant()) {
1618 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001619 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001620 __ movl(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001621 } else {
1622 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1623 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001624 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001625 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001626 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
1627 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001628 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001629 DCHECK(value.IsConstant()) << value;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001630 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001631 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1632 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001633 }
1634 break;
1635 }
1636
1637 case Primitive::kPrimNot: {
1638 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject), true));
1639 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001640 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001641 break;
1642 }
1643
1644 case Primitive::kPrimLong: {
1645 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001646 if (index.IsConstant()) {
1647 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001648 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001649 __ movq(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001650 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001651 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001652 __ movq(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
1653 value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001654 }
1655 break;
1656 }
1657
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001658 case Primitive::kPrimFloat: {
1659 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1660 if (index.IsConstant()) {
1661 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1662 DCHECK(value.IsFpuRegister());
1663 __ movss(Address(obj, offset), value.As<XmmRegister>());
1664 } else {
1665 DCHECK(value.IsFpuRegister());
1666 __ movss(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
1667 value.As<XmmRegister>());
1668 }
1669 break;
1670 }
1671
1672 case Primitive::kPrimDouble: {
1673 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1674 if (index.IsConstant()) {
1675 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1676 DCHECK(value.IsFpuRegister());
1677 __ movsd(Address(obj, offset), value.As<XmmRegister>());
1678 } else {
1679 DCHECK(value.IsFpuRegister());
1680 __ movsd(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
1681 value.As<XmmRegister>());
1682 }
1683 break;
1684 }
1685
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001686 case Primitive::kPrimVoid:
1687 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001688 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001689 }
1690}
1691
1692void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001693 LocationSummary* locations =
1694 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001695 locations->SetInAt(0, Location::RequiresRegister());
1696 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001697}
1698
1699void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
1700 LocationSummary* locations = instruction->GetLocations();
1701 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001702 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
1703 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001704 __ movl(out, Address(obj, offset));
1705}
1706
1707void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001708 LocationSummary* locations =
1709 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001710 locations->SetInAt(0, Location::RequiresRegister());
1711 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001712 if (instruction->HasUses()) {
1713 locations->SetOut(Location::SameAsFirstInput());
1714 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001715}
1716
1717void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
1718 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001719 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01001720 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001721 codegen_->AddSlowPath(slow_path);
1722
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001723 CpuRegister index = locations->InAt(0).As<CpuRegister>();
1724 CpuRegister length = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001725
1726 __ cmpl(index, length);
1727 __ j(kAboveEqual, slow_path->GetEntryLabel());
1728}
1729
1730void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
1731 CpuRegister card,
1732 CpuRegister object,
1733 CpuRegister value) {
1734 Label is_null;
1735 __ testl(value, value);
1736 __ j(kEqual, &is_null);
1737 __ gs()->movq(card, Address::Absolute(
1738 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
1739 __ movq(temp, object);
1740 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
1741 __ movb(Address(temp, card, TIMES_1, 0), card);
1742 __ Bind(&is_null);
1743}
1744
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001745void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
1746 temp->SetLocations(nullptr);
1747}
1748
1749void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
1750 // Nothing to do, this is driven by the code generator.
1751}
1752
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001753void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
1754 LOG(FATAL) << "Unimplemented";
1755}
1756
1757void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001758 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
1759}
1760
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001761void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
1762 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
1763}
1764
1765void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001766 HBasicBlock* block = instruction->GetBlock();
1767 if (block->GetLoopInformation() != nullptr) {
1768 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
1769 // The back edge will generate the suspend check.
1770 return;
1771 }
1772 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
1773 // The goto will generate the suspend check.
1774 return;
1775 }
1776 GenerateSuspendCheck(instruction, nullptr);
1777}
1778
1779void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
1780 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001781 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001782 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001783 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001784 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001785 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001786 if (successor == nullptr) {
1787 __ j(kNotEqual, slow_path->GetEntryLabel());
1788 __ Bind(slow_path->GetReturnLabel());
1789 } else {
1790 __ j(kEqual, codegen_->GetLabelOf(successor));
1791 __ jmp(slow_path->GetEntryLabel());
1792 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001793}
1794
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001795X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
1796 return codegen_->GetAssembler();
1797}
1798
1799void ParallelMoveResolverX86_64::EmitMove(size_t index) {
1800 MoveOperands* move = moves_.Get(index);
1801 Location source = move->GetSource();
1802 Location destination = move->GetDestination();
1803
1804 if (source.IsRegister()) {
1805 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001806 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001807 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001808 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001809 source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001810 } else {
1811 DCHECK(destination.IsDoubleStackSlot());
1812 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001813 source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001814 }
1815 } else if (source.IsStackSlot()) {
1816 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001817 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001818 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001819 } else if (destination.IsFpuRegister()) {
1820 __ movss(destination.As<XmmRegister>(),
1821 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001822 } else {
1823 DCHECK(destination.IsStackSlot());
1824 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1825 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
1826 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001827 } else if (source.IsDoubleStackSlot()) {
1828 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001829 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001830 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001831 } else if (destination.IsFpuRegister()) {
1832 __ movsd(destination.As<XmmRegister>(), Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001833 } else {
1834 DCHECK(destination.IsDoubleStackSlot());
1835 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1836 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
1837 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001838 } else if (source.IsConstant()) {
1839 HConstant* constant = source.GetConstant();
1840 if (constant->IsIntConstant()) {
1841 Immediate imm(constant->AsIntConstant()->GetValue());
1842 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001843 __ movl(destination.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001844 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001845 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001846 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
1847 }
1848 } else if (constant->IsLongConstant()) {
1849 int64_t value = constant->AsLongConstant()->GetValue();
1850 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001851 __ movq(destination.As<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001852 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001853 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001854 __ movq(CpuRegister(TMP), Immediate(value));
1855 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
1856 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001857 } else if (constant->IsFloatConstant()) {
1858 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
1859 if (destination.IsFpuRegister()) {
1860 __ movl(CpuRegister(TMP), imm);
1861 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
1862 } else {
1863 DCHECK(destination.IsStackSlot()) << destination;
1864 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
1865 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001866 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001867 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
1868 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
1869 if (destination.IsFpuRegister()) {
1870 __ movq(CpuRegister(TMP), imm);
1871 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
1872 } else {
1873 DCHECK(destination.IsDoubleStackSlot()) << destination;
1874 __ movq(CpuRegister(TMP), imm);
1875 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
1876 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001877 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001878 } else if (source.IsFpuRegister()) {
1879 if (destination.IsFpuRegister()) {
1880 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
1881 } else if (destination.IsStackSlot()) {
1882 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
1883 source.As<XmmRegister>());
1884 } else {
1885 DCHECK(destination.IsDoubleStackSlot());
1886 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
1887 source.As<XmmRegister>());
1888 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001889 }
1890}
1891
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001892void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001893 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001894 __ movl(Address(CpuRegister(RSP), mem), reg);
1895 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001896}
1897
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001898void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001899 ScratchRegisterScope ensure_scratch(
1900 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
1901
1902 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
1903 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
1904 __ movl(CpuRegister(ensure_scratch.GetRegister()),
1905 Address(CpuRegister(RSP), mem2 + stack_offset));
1906 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
1907 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
1908 CpuRegister(ensure_scratch.GetRegister()));
1909}
1910
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001911void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
1912 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
1913 __ movq(Address(CpuRegister(RSP), mem), reg);
1914 __ movq(reg, CpuRegister(TMP));
1915}
1916
1917void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
1918 ScratchRegisterScope ensure_scratch(
1919 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
1920
1921 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
1922 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
1923 __ movq(CpuRegister(ensure_scratch.GetRegister()),
1924 Address(CpuRegister(RSP), mem2 + stack_offset));
1925 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
1926 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
1927 CpuRegister(ensure_scratch.GetRegister()));
1928}
1929
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001930void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
1931 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
1932 __ movss(Address(CpuRegister(RSP), mem), reg);
1933 __ movd(reg, CpuRegister(TMP));
1934}
1935
1936void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
1937 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
1938 __ movsd(Address(CpuRegister(RSP), mem), reg);
1939 __ movd(reg, CpuRegister(TMP));
1940}
1941
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001942void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
1943 MoveOperands* move = moves_.Get(index);
1944 Location source = move->GetSource();
1945 Location destination = move->GetDestination();
1946
1947 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001948 __ xchgq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001949 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001950 Exchange32(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001951 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001952 Exchange32(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001953 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001954 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
1955 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001956 Exchange64(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001957 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001958 Exchange64(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001959 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
1960 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001961 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
1962 __ movd(CpuRegister(TMP), source.As<XmmRegister>());
1963 __ movaps(source.As<XmmRegister>(), destination.As<XmmRegister>());
1964 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
1965 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
1966 Exchange32(source.As<XmmRegister>(), destination.GetStackIndex());
1967 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
1968 Exchange32(destination.As<XmmRegister>(), source.GetStackIndex());
1969 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
1970 Exchange64(source.As<XmmRegister>(), destination.GetStackIndex());
1971 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
1972 Exchange64(destination.As<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001973 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001974 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001975 }
1976}
1977
1978
1979void ParallelMoveResolverX86_64::SpillScratch(int reg) {
1980 __ pushq(CpuRegister(reg));
1981}
1982
1983
1984void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
1985 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001986}
1987
1988} // namespace x86_64
1989} // namespace art