blob: 892ca9d9c7f4d786dbfc09d5330a2b4a591312c4 [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 Geoffraya3d05a42014-10-20 17:41:32 +0100410 } else if (location.IsStackSlot()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100411 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100412 } else {
413 DCHECK(location.IsConstant());
414 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100415 }
Roland Levillain476df552014-10-09 17:51:36 +0100416 } else if (instruction->IsLongConstant()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100417 int64_t value = instruction->AsLongConstant()->GetValue();
418 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100419 __ movq(location.As<CpuRegister>(), Immediate(value));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100420 } else if (location.IsDoubleStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000421 __ movq(CpuRegister(TMP), Immediate(value));
422 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100423 } else {
424 DCHECK(location.IsConstant());
425 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100426 }
Roland Levillain476df552014-10-09 17:51:36 +0100427 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100428 switch (instruction->GetType()) {
429 case Primitive::kPrimBoolean:
430 case Primitive::kPrimByte:
431 case Primitive::kPrimChar:
432 case Primitive::kPrimShort:
433 case Primitive::kPrimInt:
434 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100435 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100436 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
437 break;
438
439 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100440 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100441 Move(location, Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
442 break;
443
444 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100445 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100446 }
447 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100448 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100449 switch (instruction->GetType()) {
450 case Primitive::kPrimBoolean:
451 case Primitive::kPrimByte:
452 case Primitive::kPrimChar:
453 case Primitive::kPrimShort:
454 case Primitive::kPrimInt:
455 case Primitive::kPrimNot:
456 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100457 case Primitive::kPrimFloat:
458 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100459 Move(location, instruction->GetLocations()->Out());
460 break;
461
462 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100463 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100464 }
465 }
466}
467
468void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
469 got->SetLocations(nullptr);
470}
471
472void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
473 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100474 DCHECK(!successor->IsExitBlock());
475
476 HBasicBlock* block = got->GetBlock();
477 HInstruction* previous = got->GetPrevious();
478
479 HLoopInformation* info = block->GetLoopInformation();
480 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
481 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
482 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
483 return;
484 }
485
486 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
487 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
488 }
489 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100490 __ jmp(codegen_->GetLabelOf(successor));
491 }
492}
493
494void LocationsBuilderX86_64::VisitExit(HExit* exit) {
495 exit->SetLocations(nullptr);
496}
497
498void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
499 if (kIsDebugBuild) {
500 __ Comment("Unreachable");
501 __ int3();
502 }
503}
504
505void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100506 LocationSummary* locations =
507 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100508 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100509 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100510 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100511 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100512}
513
514void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700515 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100516 if (cond->IsIntConstant()) {
517 // Constant condition, statically compared against 1.
518 int32_t cond_value = cond->AsIntConstant()->GetValue();
519 if (cond_value == 1) {
520 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
521 if_instr->IfTrueSuccessor())) {
522 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100523 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100524 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100525 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100526 DCHECK_EQ(cond_value, 0);
527 }
528 } else {
529 bool materialized =
530 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
531 // Moves do not affect the eflags register, so if the condition is
532 // evaluated just before the if, we don't need to evaluate it
533 // again.
534 bool eflags_set = cond->IsCondition()
535 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
536 if (materialized) {
537 if (!eflags_set) {
538 // Materialized condition, compare against 0.
539 Location lhs = if_instr->GetLocations()->InAt(0);
540 if (lhs.IsRegister()) {
541 __ cmpl(lhs.As<CpuRegister>(), Immediate(0));
542 } else {
543 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
544 Immediate(0));
545 }
546 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
547 } else {
548 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
549 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
550 }
551 } else {
552 Location lhs = cond->GetLocations()->InAt(0);
553 Location rhs = cond->GetLocations()->InAt(1);
554 if (rhs.IsRegister()) {
555 __ cmpl(lhs.As<CpuRegister>(), rhs.As<CpuRegister>());
556 } else if (rhs.IsConstant()) {
557 __ cmpl(lhs.As<CpuRegister>(),
558 Immediate(rhs.GetConstant()->AsIntConstant()->GetValue()));
559 } else {
560 __ cmpl(lhs.As<CpuRegister>(),
561 Address(CpuRegister(RSP), rhs.GetStackIndex()));
562 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100563 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
564 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700565 }
Dave Allison20dfc792014-06-16 20:44:29 -0700566 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100567 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
568 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700569 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100570 }
571}
572
573void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
574 local->SetLocations(nullptr);
575}
576
577void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
578 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
579}
580
581void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
582 local->SetLocations(nullptr);
583}
584
585void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
586 // Nothing to do, this is driven by the code generator.
587}
588
589void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100590 LocationSummary* locations =
591 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100592 switch (store->InputAt(1)->GetType()) {
593 case Primitive::kPrimBoolean:
594 case Primitive::kPrimByte:
595 case Primitive::kPrimChar:
596 case Primitive::kPrimShort:
597 case Primitive::kPrimInt:
598 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100599 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100600 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
601 break;
602
603 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100604 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100605 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
606 break;
607
608 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100609 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100610 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100611}
612
613void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
614}
615
Dave Allison20dfc792014-06-16 20:44:29 -0700616void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100617 LocationSummary* locations =
618 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100619 locations->SetInAt(0, Location::RequiresRegister());
620 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100621 if (comp->NeedsMaterialization()) {
622 locations->SetOut(Location::RequiresRegister());
623 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100624}
625
Dave Allison20dfc792014-06-16 20:44:29 -0700626void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
627 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100628 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100629 CpuRegister reg = locations->Out().As<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100630 // Clear register: setcc only sets the low byte.
631 __ xorq(reg, reg);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100632 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100633 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100634 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100635 } else if (locations->InAt(1).IsConstant()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100636 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100637 Immediate(locations->InAt(1).GetConstant()->AsIntConstant()->GetValue()));
638 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100639 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100640 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
641 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100642 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700643 }
644}
645
646void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
647 VisitCondition(comp);
648}
649
650void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
651 VisitCondition(comp);
652}
653
654void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
655 VisitCondition(comp);
656}
657
658void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
659 VisitCondition(comp);
660}
661
662void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
663 VisitCondition(comp);
664}
665
666void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
667 VisitCondition(comp);
668}
669
670void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
671 VisitCondition(comp);
672}
673
674void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
675 VisitCondition(comp);
676}
677
678void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
679 VisitCondition(comp);
680}
681
682void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
683 VisitCondition(comp);
684}
685
686void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
687 VisitCondition(comp);
688}
689
690void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
691 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100692}
693
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100694void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100695 LocationSummary* locations =
696 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100697 locations->SetInAt(0, Location::RequiresRegister());
698 locations->SetInAt(1, Location::RequiresRegister());
699 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100700}
701
702void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
703 Label greater, done;
704 LocationSummary* locations = compare->GetLocations();
705 switch (compare->InputAt(0)->GetType()) {
706 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100707 __ cmpq(locations->InAt(0).As<CpuRegister>(),
708 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100709 break;
710 default:
711 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
712 }
713
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100714 CpuRegister output = locations->Out().As<CpuRegister>();
715 __ movl(output, Immediate(0));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100716 __ j(kEqual, &done);
717 __ j(kGreater, &greater);
718
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100719 __ movl(output, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100720 __ jmp(&done);
721
722 __ Bind(&greater);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100723 __ movl(output, Immediate(1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100724
725 __ Bind(&done);
726}
727
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100728void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100729 LocationSummary* locations =
730 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100731 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100732}
733
734void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100735 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100736}
737
738void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100739 LocationSummary* locations =
740 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100741 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100742}
743
744void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100745 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100746}
747
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100748void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
749 LocationSummary* locations =
750 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
751 locations->SetOut(Location::ConstantLocation(constant));
752}
753
754void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
755 // Will be generated at use site.
756}
757
758void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
759 LocationSummary* locations =
760 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
761 locations->SetOut(Location::ConstantLocation(constant));
762}
763
764void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
765 // Will be generated at use site.
766}
767
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100768void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
769 ret->SetLocations(nullptr);
770}
771
772void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
773 codegen_->GenerateFrameExit();
774 __ ret();
775}
776
777void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100778 LocationSummary* locations =
779 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100780 switch (ret->InputAt(0)->GetType()) {
781 case Primitive::kPrimBoolean:
782 case Primitive::kPrimByte:
783 case Primitive::kPrimChar:
784 case Primitive::kPrimShort:
785 case Primitive::kPrimInt:
786 case Primitive::kPrimNot:
787 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100788 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100789 break;
790
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100791 case Primitive::kPrimFloat:
792 case Primitive::kPrimDouble:
793 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100794 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100795 break;
796
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100797 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100798 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100799 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100800}
801
802void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
803 if (kIsDebugBuild) {
804 switch (ret->InputAt(0)->GetType()) {
805 case Primitive::kPrimBoolean:
806 case Primitive::kPrimByte:
807 case Primitive::kPrimChar:
808 case Primitive::kPrimShort:
809 case Primitive::kPrimInt:
810 case Primitive::kPrimNot:
811 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100812 DCHECK_EQ(ret->GetLocations()->InAt(0).As<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100813 break;
814
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100815 case Primitive::kPrimFloat:
816 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100817 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100818 XMM0);
819 break;
820
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100821 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100822 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100823 }
824 }
825 codegen_->GenerateFrameExit();
826 __ ret();
827}
828
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100829Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
830 switch (type) {
831 case Primitive::kPrimBoolean:
832 case Primitive::kPrimByte:
833 case Primitive::kPrimChar:
834 case Primitive::kPrimShort:
835 case Primitive::kPrimInt:
836 case Primitive::kPrimNot: {
837 uint32_t index = gp_index_++;
838 stack_index_++;
839 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100840 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100841 } else {
842 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
843 }
844 }
845
846 case Primitive::kPrimLong: {
847 uint32_t index = gp_index_;
848 stack_index_ += 2;
849 if (index < calling_convention.GetNumberOfRegisters()) {
850 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100851 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100852 } else {
853 gp_index_ += 2;
854 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
855 }
856 }
857
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100858 case Primitive::kPrimFloat: {
859 uint32_t index = fp_index_++;
860 stack_index_++;
861 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100862 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100863 } else {
864 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
865 }
866 }
867
868 case Primitive::kPrimDouble: {
869 uint32_t index = fp_index_++;
870 stack_index_ += 2;
871 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100872 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100873 } else {
874 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
875 }
876 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100877
878 case Primitive::kPrimVoid:
879 LOG(FATAL) << "Unexpected parameter type " << type;
880 break;
881 }
882 return Location();
883}
884
885void LocationsBuilderX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100886 HandleInvoke(invoke);
887}
888
889void InstructionCodeGeneratorX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100890 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100891 uint32_t heap_reference_size = sizeof(mirror::HeapReference<mirror::Object>);
892 size_t index_in_cache = mirror::Array::DataOffset(heap_reference_size).SizeValue() +
893 invoke->GetIndexInDexCache() * heap_reference_size;
894
895 // TODO: Implement all kinds of calls:
896 // 1) boot -> boot
897 // 2) app -> boot
898 // 3) app -> app
899 //
900 // Currently we implement the app -> app logic, which looks up in the resolve cache.
901
902 // temp = method;
903 LoadCurrentMethod(temp);
904 // temp = temp->dex_cache_resolved_methods_;
905 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
906 // temp = temp[index_in_cache]
907 __ movl(temp, Address(temp, index_in_cache));
908 // (temp + offset_of_quick_compiled_code)()
909 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
910
911 DCHECK(!codegen_->IsLeafMethod());
912 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
913}
914
915void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
916 HandleInvoke(invoke);
917}
918
919void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100920 LocationSummary* locations =
921 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100922 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100923
924 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100925 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100926 HInstruction* input = invoke->InputAt(i);
927 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
928 }
929
930 switch (invoke->GetType()) {
931 case Primitive::kPrimBoolean:
932 case Primitive::kPrimByte:
933 case Primitive::kPrimChar:
934 case Primitive::kPrimShort:
935 case Primitive::kPrimInt:
936 case Primitive::kPrimNot:
937 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100938 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100939 break;
940
941 case Primitive::kPrimVoid:
942 break;
943
944 case Primitive::kPrimDouble:
945 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100946 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100947 break;
948 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100949}
950
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100951void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100952 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100953 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
954 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
955 LocationSummary* locations = invoke->GetLocations();
956 Location receiver = locations->InAt(0);
957 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
958 // temp = object->GetClass();
959 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100960 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
961 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100962 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100963 __ movl(temp, Address(receiver.As<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100964 }
965 // temp = temp->GetMethodAt(method_offset);
966 __ movl(temp, Address(temp, method_offset));
967 // call temp->GetEntryPoint();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100968 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
969
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100970 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100971 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100972}
973
Roland Levillain88cb1752014-10-20 16:36:47 +0100974void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
975 LocationSummary* locations =
976 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
977 switch (neg->GetResultType()) {
978 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +0100979 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +0100980 locations->SetInAt(0, Location::RequiresRegister());
981 locations->SetOut(Location::SameAsFirstInput());
982 break;
983
Roland Levillain88cb1752014-10-20 16:36:47 +0100984 case Primitive::kPrimFloat:
985 case Primitive::kPrimDouble:
986 LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
987 break;
988
989 default:
990 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
991 }
992}
993
994void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
995 LocationSummary* locations = neg->GetLocations();
996 Location out = locations->Out();
997 Location in = locations->InAt(0);
998 switch (neg->GetResultType()) {
999 case Primitive::kPrimInt:
1000 DCHECK(in.IsRegister());
1001 __ negl(out.As<CpuRegister>());
1002 break;
1003
1004 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001005 DCHECK(in.IsRegister());
1006 __ negq(out.As<CpuRegister>());
1007 break;
1008
Roland Levillain88cb1752014-10-20 16:36:47 +01001009 case Primitive::kPrimFloat:
1010 case Primitive::kPrimDouble:
1011 LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
1012 break;
1013
1014 default:
1015 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1016 }
1017}
1018
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001019void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001020 LocationSummary* locations =
1021 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001022 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001023 case Primitive::kPrimInt: {
1024 locations->SetInAt(0, Location::RequiresRegister());
1025 locations->SetInAt(1, Location::Any());
1026 locations->SetOut(Location::SameAsFirstInput());
1027 break;
1028 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001029
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001030 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001031 locations->SetInAt(0, Location::RequiresRegister());
1032 locations->SetInAt(1, Location::RequiresRegister());
1033 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001034 break;
1035 }
1036
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001037 case Primitive::kPrimDouble:
1038 case Primitive::kPrimFloat: {
1039 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001040 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001041 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001042 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001043 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001044
1045 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001046 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001047 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001048}
1049
1050void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1051 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001052 Location first = locations->InAt(0);
1053 Location second = locations->InAt(1);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001054 DCHECK(first.Equals(locations->Out()));
Calin Juravle11351682014-10-23 15:38:15 +01001055
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001056 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001057 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001058 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001059 __ addl(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001060 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001061 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001062 __ addl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001063 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001064 __ addl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001065 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001066 break;
1067 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001068
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001069 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001070 __ addq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001071 break;
1072 }
1073
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001074 case Primitive::kPrimFloat: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001075 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001076 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001077 }
1078
1079 case Primitive::kPrimDouble: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001080 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001081 break;
1082 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001083
1084 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001085 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001086 }
1087}
1088
1089void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001090 LocationSummary* locations =
1091 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001092 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001093 case Primitive::kPrimInt: {
1094 locations->SetInAt(0, Location::RequiresRegister());
1095 locations->SetInAt(1, Location::Any());
1096 locations->SetOut(Location::SameAsFirstInput());
1097 break;
1098 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001099 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001100 locations->SetInAt(0, Location::RequiresRegister());
1101 locations->SetInAt(1, Location::RequiresRegister());
1102 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001103 break;
1104 }
Calin Juravle11351682014-10-23 15:38:15 +01001105 case Primitive::kPrimFloat:
1106 case Primitive::kPrimDouble: {
1107 locations->SetInAt(0, Location::RequiresFpuRegister());
1108 locations->SetInAt(1, Location::RequiresFpuRegister());
1109 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001110 break;
Calin Juravle11351682014-10-23 15:38:15 +01001111 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001112 default:
Calin Juravle11351682014-10-23 15:38:15 +01001113 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001114 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001115}
1116
1117void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1118 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001119 Location first = locations->InAt(0);
1120 Location second = locations->InAt(1);
1121 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001122 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001123 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001124 if (second.IsRegister()) {
1125 __ subl(first.As<CpuRegister>(), second.As<CpuRegister>());
1126 } else if (second.IsConstant()) {
1127 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1128 __ subl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001129 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001130 __ subl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001131 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001132 break;
1133 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001134 case Primitive::kPrimLong: {
Calin Juravle11351682014-10-23 15:38:15 +01001135 __ subq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001136 break;
1137 }
1138
Calin Juravle11351682014-10-23 15:38:15 +01001139 case Primitive::kPrimFloat: {
1140 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001141 break;
Calin Juravle11351682014-10-23 15:38:15 +01001142 }
1143
1144 case Primitive::kPrimDouble: {
1145 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1146 break;
1147 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001148
1149 default:
Calin Juravle11351682014-10-23 15:38:15 +01001150 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001151 }
1152}
1153
Calin Juravle34bacdf2014-10-07 20:23:36 +01001154void LocationsBuilderX86_64::VisitMul(HMul* mul) {
1155 LocationSummary* locations =
1156 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1157 switch (mul->GetResultType()) {
1158 case Primitive::kPrimInt: {
1159 locations->SetInAt(0, Location::RequiresRegister());
1160 locations->SetInAt(1, Location::Any());
1161 locations->SetOut(Location::SameAsFirstInput());
1162 break;
1163 }
1164 case Primitive::kPrimLong: {
1165 locations->SetInAt(0, Location::RequiresRegister());
1166 locations->SetInAt(1, Location::RequiresRegister());
1167 locations->SetOut(Location::SameAsFirstInput());
1168 break;
1169 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001170 case Primitive::kPrimFloat:
1171 case Primitive::kPrimDouble: {
1172 locations->SetInAt(0, Location::RequiresFpuRegister());
1173 locations->SetInAt(1, Location::RequiresFpuRegister());
1174 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001175 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001176 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001177
1178 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001179 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001180 }
1181}
1182
1183void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
1184 LocationSummary* locations = mul->GetLocations();
1185 Location first = locations->InAt(0);
1186 Location second = locations->InAt(1);
1187 DCHECK(first.Equals(locations->Out()));
1188 switch (mul->GetResultType()) {
1189 case Primitive::kPrimInt: {
1190 if (second.IsRegister()) {
1191 __ imull(first.As<CpuRegister>(), second.As<CpuRegister>());
1192 } else if (second.IsConstant()) {
1193 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1194 __ imull(first.As<CpuRegister>(), imm);
1195 } else {
1196 DCHECK(second.IsStackSlot());
1197 __ imull(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
1198 }
1199 break;
1200 }
1201 case Primitive::kPrimLong: {
1202 __ imulq(first.As<CpuRegister>(), second.As<CpuRegister>());
1203 break;
1204 }
1205
Calin Juravleb5bfa962014-10-21 18:02:24 +01001206 case Primitive::kPrimFloat: {
1207 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001208 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001209 }
1210
1211 case Primitive::kPrimDouble: {
1212 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1213 break;
1214 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001215
1216 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001217 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001218 }
1219}
1220
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001221void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001222 LocationSummary* locations =
1223 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001224 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001225 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1226 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1227 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001228}
1229
1230void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
1231 InvokeRuntimeCallingConvention calling_convention;
1232 LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
1233 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1234
1235 __ gs()->call(Address::Absolute(
1236 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
1237
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001238 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001239 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001240}
1241
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001242void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
1243 LocationSummary* locations =
1244 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1245 InvokeRuntimeCallingConvention calling_convention;
1246 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1247 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1248 locations->SetOut(Location::RegisterLocation(RAX));
1249 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1250}
1251
1252void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
1253 InvokeRuntimeCallingConvention calling_convention;
1254 LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
1255 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1256
1257 __ gs()->call(Address::Absolute(
1258 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocArrayWithAccessCheck), true));
1259
1260 DCHECK(!codegen_->IsLeafMethod());
1261 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1262}
1263
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001264void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001265 LocationSummary* locations =
1266 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001267 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1268 if (location.IsStackSlot()) {
1269 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1270 } else if (location.IsDoubleStackSlot()) {
1271 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1272 }
1273 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001274}
1275
1276void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
1277 // Nothing to do, the parameter is already at its location.
1278}
1279
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001280void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001281 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001282 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001283 locations->SetInAt(0, Location::RequiresRegister());
1284 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001285}
1286
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001287void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
1288 LocationSummary* locations = not_->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001289 DCHECK_EQ(locations->InAt(0).As<CpuRegister>().AsRegister(),
1290 locations->Out().As<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001291 Location out = locations->Out();
1292 switch (not_->InputAt(0)->GetType()) {
1293 case Primitive::kPrimBoolean:
1294 __ xorq(out.As<CpuRegister>(), Immediate(1));
1295 break;
1296
1297 case Primitive::kPrimInt:
1298 __ notl(out.As<CpuRegister>());
1299 break;
1300
1301 case Primitive::kPrimLong:
1302 LOG(FATAL) << "Not yet implemented type for not operation " << not_->GetResultType();
1303 break;
1304
1305 default:
1306 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1307 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001308}
1309
1310void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001311 LocationSummary* locations =
1312 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001313 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1314 locations->SetInAt(i, Location::Any());
1315 }
1316 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001317}
1318
1319void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
1320 LOG(FATAL) << "Unimplemented";
1321}
1322
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001323void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001324 LocationSummary* locations =
1325 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001326 Primitive::Type field_type = instruction->GetFieldType();
1327 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001328 locations->SetInAt(0, Location::RequiresRegister());
1329 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001330 if (is_object_type) {
1331 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001332 locations->AddTemp(Location::RequiresRegister());
1333 locations->AddTemp(Location::RequiresRegister());
1334 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001335}
1336
1337void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1338 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001339 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
1340 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001341 size_t offset = instruction->GetFieldOffset().SizeValue();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001342 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001343
1344 switch (field_type) {
1345 case Primitive::kPrimBoolean:
1346 case Primitive::kPrimByte: {
1347 __ movb(Address(obj, offset), value);
1348 break;
1349 }
1350
1351 case Primitive::kPrimShort:
1352 case Primitive::kPrimChar: {
1353 __ movw(Address(obj, offset), value);
1354 break;
1355 }
1356
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001357 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001358 case Primitive::kPrimNot: {
1359 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001360 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001361 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
1362 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001363 codegen_->MarkGCCard(temp, card, obj, value);
1364 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001365 break;
1366 }
1367
1368 case Primitive::kPrimLong: {
1369 __ movq(Address(obj, offset), value);
1370 break;
1371 }
1372
1373 case Primitive::kPrimFloat:
1374 case Primitive::kPrimDouble:
1375 LOG(FATAL) << "Unimplemented register type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001376 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001377 case Primitive::kPrimVoid:
1378 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001379 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001380 }
1381}
1382
1383void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001384 LocationSummary* locations =
1385 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001386 locations->SetInAt(0, Location::RequiresRegister());
1387 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001388}
1389
1390void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1391 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001392 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
1393 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001394 size_t offset = instruction->GetFieldOffset().SizeValue();
1395
1396 switch (instruction->GetType()) {
1397 case Primitive::kPrimBoolean: {
1398 __ movzxb(out, Address(obj, offset));
1399 break;
1400 }
1401
1402 case Primitive::kPrimByte: {
1403 __ movsxb(out, Address(obj, offset));
1404 break;
1405 }
1406
1407 case Primitive::kPrimShort: {
1408 __ movsxw(out, Address(obj, offset));
1409 break;
1410 }
1411
1412 case Primitive::kPrimChar: {
1413 __ movzxw(out, Address(obj, offset));
1414 break;
1415 }
1416
1417 case Primitive::kPrimInt:
1418 case Primitive::kPrimNot: {
1419 __ movl(out, Address(obj, offset));
1420 break;
1421 }
1422
1423 case Primitive::kPrimLong: {
1424 __ movq(out, Address(obj, offset));
1425 break;
1426 }
1427
1428 case Primitive::kPrimFloat:
1429 case Primitive::kPrimDouble:
1430 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001431 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001432 case Primitive::kPrimVoid:
1433 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001434 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001435 }
1436}
1437
1438void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001439 LocationSummary* locations =
1440 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001441 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001442 if (instruction->HasUses()) {
1443 locations->SetOut(Location::SameAsFirstInput());
1444 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001445}
1446
1447void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001448 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001449 codegen_->AddSlowPath(slow_path);
1450
1451 LocationSummary* locations = instruction->GetLocations();
1452 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001453
1454 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001455 __ cmpl(obj.As<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001456 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001457 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001458 } else {
1459 DCHECK(obj.IsConstant()) << obj;
1460 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1461 __ jmp(slow_path->GetEntryLabel());
1462 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001463 }
1464 __ j(kEqual, slow_path->GetEntryLabel());
1465}
1466
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001467void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001468 LocationSummary* locations =
1469 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001470 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001471 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001472 1, Location::RegisterOrConstant(instruction->InputAt(1)));
1473 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001474}
1475
1476void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
1477 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001478 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001479 Location index = locations->InAt(1);
1480
1481 switch (instruction->GetType()) {
1482 case Primitive::kPrimBoolean: {
1483 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001484 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001485 if (index.IsConstant()) {
1486 __ movzxb(out, Address(obj,
1487 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1488 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001489 __ movzxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001490 }
1491 break;
1492 }
1493
1494 case Primitive::kPrimByte: {
1495 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001496 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001497 if (index.IsConstant()) {
1498 __ movsxb(out, Address(obj,
1499 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1500 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001501 __ movsxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001502 }
1503 break;
1504 }
1505
1506 case Primitive::kPrimShort: {
1507 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001508 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001509 if (index.IsConstant()) {
1510 __ movsxw(out, Address(obj,
1511 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1512 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001513 __ movsxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001514 }
1515 break;
1516 }
1517
1518 case Primitive::kPrimChar: {
1519 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001520 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001521 if (index.IsConstant()) {
1522 __ movzxw(out, Address(obj,
1523 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1524 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001525 __ movzxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001526 }
1527 break;
1528 }
1529
1530 case Primitive::kPrimInt:
1531 case Primitive::kPrimNot: {
1532 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1533 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001534 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001535 if (index.IsConstant()) {
1536 __ movl(out, Address(obj,
1537 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1538 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001539 __ movl(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001540 }
1541 break;
1542 }
1543
1544 case Primitive::kPrimLong: {
1545 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001546 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001547 if (index.IsConstant()) {
1548 __ movq(out, Address(obj,
1549 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
1550 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001551 __ movq(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001552 }
1553 break;
1554 }
1555
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001556 case Primitive::kPrimFloat: {
1557 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1558 XmmRegister out = locations->Out().As<XmmRegister>();
1559 if (index.IsConstant()) {
1560 __ movss(out, Address(obj,
1561 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1562 } else {
1563 __ movss(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
1564 }
1565 break;
1566 }
1567
1568 case Primitive::kPrimDouble: {
1569 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1570 XmmRegister out = locations->Out().As<XmmRegister>();
1571 if (index.IsConstant()) {
1572 __ movsd(out, Address(obj,
1573 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
1574 } else {
1575 __ movsd(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
1576 }
1577 break;
1578 }
1579
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001580 case Primitive::kPrimVoid:
1581 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001582 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001583 }
1584}
1585
1586void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001587 Primitive::Type value_type = instruction->GetComponentType();
1588 bool is_object = value_type == Primitive::kPrimNot;
1589 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1590 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
1591 if (is_object) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001592 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001593 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1594 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1595 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001596 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001597 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001598 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001599 1, Location::RegisterOrConstant(instruction->InputAt(1)));
1600 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001601 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001602 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001603 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
1604 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001605 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001606 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001607 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001608 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001609}
1610
1611void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
1612 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001613 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001614 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001615 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001616 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001617
1618 switch (value_type) {
1619 case Primitive::kPrimBoolean:
1620 case Primitive::kPrimByte: {
1621 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001622 if (index.IsConstant()) {
1623 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001624 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001625 __ movb(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001626 } else {
1627 __ movb(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1628 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001629 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001630 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001631 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
1632 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001633 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001634 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001635 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1636 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001637 }
1638 break;
1639 }
1640
1641 case Primitive::kPrimShort:
1642 case Primitive::kPrimChar: {
1643 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001644 if (index.IsConstant()) {
1645 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001646 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001647 __ movw(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001648 } else {
1649 __ movw(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1650 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001651 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001652 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001653 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
1654 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001655 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001656 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001657 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1658 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001659 }
1660 break;
1661 }
1662
1663 case Primitive::kPrimInt: {
1664 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001665 if (index.IsConstant()) {
1666 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001667 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001668 __ movl(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001669 } else {
1670 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1671 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001672 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001673 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001674 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
1675 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001676 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001677 DCHECK(value.IsConstant()) << value;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001678 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001679 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1680 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001681 }
1682 break;
1683 }
1684
1685 case Primitive::kPrimNot: {
1686 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject), true));
1687 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001688 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001689 break;
1690 }
1691
1692 case Primitive::kPrimLong: {
1693 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001694 if (index.IsConstant()) {
1695 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001696 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001697 __ movq(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001698 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001699 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001700 __ movq(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
1701 value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001702 }
1703 break;
1704 }
1705
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001706 case Primitive::kPrimFloat: {
1707 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1708 if (index.IsConstant()) {
1709 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1710 DCHECK(value.IsFpuRegister());
1711 __ movss(Address(obj, offset), value.As<XmmRegister>());
1712 } else {
1713 DCHECK(value.IsFpuRegister());
1714 __ movss(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
1715 value.As<XmmRegister>());
1716 }
1717 break;
1718 }
1719
1720 case Primitive::kPrimDouble: {
1721 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1722 if (index.IsConstant()) {
1723 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1724 DCHECK(value.IsFpuRegister());
1725 __ movsd(Address(obj, offset), value.As<XmmRegister>());
1726 } else {
1727 DCHECK(value.IsFpuRegister());
1728 __ movsd(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
1729 value.As<XmmRegister>());
1730 }
1731 break;
1732 }
1733
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001734 case Primitive::kPrimVoid:
1735 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001736 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001737 }
1738}
1739
1740void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001741 LocationSummary* locations =
1742 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001743 locations->SetInAt(0, Location::RequiresRegister());
1744 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001745}
1746
1747void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
1748 LocationSummary* locations = instruction->GetLocations();
1749 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001750 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
1751 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001752 __ movl(out, Address(obj, offset));
1753}
1754
1755void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001756 LocationSummary* locations =
1757 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001758 locations->SetInAt(0, Location::RequiresRegister());
1759 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001760 if (instruction->HasUses()) {
1761 locations->SetOut(Location::SameAsFirstInput());
1762 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001763}
1764
1765void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
1766 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001767 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01001768 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001769 codegen_->AddSlowPath(slow_path);
1770
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001771 CpuRegister index = locations->InAt(0).As<CpuRegister>();
1772 CpuRegister length = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001773
1774 __ cmpl(index, length);
1775 __ j(kAboveEqual, slow_path->GetEntryLabel());
1776}
1777
1778void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
1779 CpuRegister card,
1780 CpuRegister object,
1781 CpuRegister value) {
1782 Label is_null;
1783 __ testl(value, value);
1784 __ j(kEqual, &is_null);
1785 __ gs()->movq(card, Address::Absolute(
1786 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
1787 __ movq(temp, object);
1788 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
1789 __ movb(Address(temp, card, TIMES_1, 0), card);
1790 __ Bind(&is_null);
1791}
1792
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001793void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
1794 temp->SetLocations(nullptr);
1795}
1796
1797void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
1798 // Nothing to do, this is driven by the code generator.
1799}
1800
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001801void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
1802 LOG(FATAL) << "Unimplemented";
1803}
1804
1805void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001806 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
1807}
1808
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001809void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
1810 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
1811}
1812
1813void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001814 HBasicBlock* block = instruction->GetBlock();
1815 if (block->GetLoopInformation() != nullptr) {
1816 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
1817 // The back edge will generate the suspend check.
1818 return;
1819 }
1820 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
1821 // The goto will generate the suspend check.
1822 return;
1823 }
1824 GenerateSuspendCheck(instruction, nullptr);
1825}
1826
1827void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
1828 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001829 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001830 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001831 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001832 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001833 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001834 if (successor == nullptr) {
1835 __ j(kNotEqual, slow_path->GetEntryLabel());
1836 __ Bind(slow_path->GetReturnLabel());
1837 } else {
1838 __ j(kEqual, codegen_->GetLabelOf(successor));
1839 __ jmp(slow_path->GetEntryLabel());
1840 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001841}
1842
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001843X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
1844 return codegen_->GetAssembler();
1845}
1846
1847void ParallelMoveResolverX86_64::EmitMove(size_t index) {
1848 MoveOperands* move = moves_.Get(index);
1849 Location source = move->GetSource();
1850 Location destination = move->GetDestination();
1851
1852 if (source.IsRegister()) {
1853 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001854 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001855 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001856 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001857 source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001858 } else {
1859 DCHECK(destination.IsDoubleStackSlot());
1860 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001861 source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001862 }
1863 } else if (source.IsStackSlot()) {
1864 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001865 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001866 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001867 } else if (destination.IsFpuRegister()) {
1868 __ movss(destination.As<XmmRegister>(),
1869 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001870 } else {
1871 DCHECK(destination.IsStackSlot());
1872 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1873 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
1874 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001875 } else if (source.IsDoubleStackSlot()) {
1876 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001877 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001878 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001879 } else if (destination.IsFpuRegister()) {
1880 __ movsd(destination.As<XmmRegister>(), Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001881 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01001882 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001883 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1884 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
1885 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001886 } else if (source.IsConstant()) {
1887 HConstant* constant = source.GetConstant();
1888 if (constant->IsIntConstant()) {
1889 Immediate imm(constant->AsIntConstant()->GetValue());
1890 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001891 __ movl(destination.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001892 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001893 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001894 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
1895 }
1896 } else if (constant->IsLongConstant()) {
1897 int64_t value = constant->AsLongConstant()->GetValue();
1898 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001899 __ movq(destination.As<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001900 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001901 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001902 __ movq(CpuRegister(TMP), Immediate(value));
1903 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
1904 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001905 } else if (constant->IsFloatConstant()) {
1906 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
1907 if (destination.IsFpuRegister()) {
1908 __ movl(CpuRegister(TMP), imm);
1909 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
1910 } else {
1911 DCHECK(destination.IsStackSlot()) << destination;
1912 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
1913 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001914 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001915 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
1916 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
1917 if (destination.IsFpuRegister()) {
1918 __ movq(CpuRegister(TMP), imm);
1919 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
1920 } else {
1921 DCHECK(destination.IsDoubleStackSlot()) << destination;
1922 __ movq(CpuRegister(TMP), imm);
1923 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
1924 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001925 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001926 } else if (source.IsFpuRegister()) {
1927 if (destination.IsFpuRegister()) {
1928 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
1929 } else if (destination.IsStackSlot()) {
1930 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
1931 source.As<XmmRegister>());
1932 } else {
1933 DCHECK(destination.IsDoubleStackSlot());
1934 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
1935 source.As<XmmRegister>());
1936 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001937 }
1938}
1939
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001940void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001941 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001942 __ movl(Address(CpuRegister(RSP), mem), reg);
1943 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001944}
1945
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001946void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001947 ScratchRegisterScope ensure_scratch(
1948 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
1949
1950 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
1951 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
1952 __ movl(CpuRegister(ensure_scratch.GetRegister()),
1953 Address(CpuRegister(RSP), mem2 + stack_offset));
1954 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
1955 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
1956 CpuRegister(ensure_scratch.GetRegister()));
1957}
1958
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001959void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
1960 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
1961 __ movq(Address(CpuRegister(RSP), mem), reg);
1962 __ movq(reg, CpuRegister(TMP));
1963}
1964
1965void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
1966 ScratchRegisterScope ensure_scratch(
1967 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
1968
1969 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
1970 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
1971 __ movq(CpuRegister(ensure_scratch.GetRegister()),
1972 Address(CpuRegister(RSP), mem2 + stack_offset));
1973 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
1974 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
1975 CpuRegister(ensure_scratch.GetRegister()));
1976}
1977
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001978void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
1979 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
1980 __ movss(Address(CpuRegister(RSP), mem), reg);
1981 __ movd(reg, CpuRegister(TMP));
1982}
1983
1984void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
1985 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
1986 __ movsd(Address(CpuRegister(RSP), mem), reg);
1987 __ movd(reg, CpuRegister(TMP));
1988}
1989
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001990void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
1991 MoveOperands* move = moves_.Get(index);
1992 Location source = move->GetSource();
1993 Location destination = move->GetDestination();
1994
1995 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001996 __ xchgq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001997 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001998 Exchange32(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001999 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002000 Exchange32(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002001 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002002 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
2003 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002004 Exchange64(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002005 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002006 Exchange64(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002007 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
2008 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002009 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
2010 __ movd(CpuRegister(TMP), source.As<XmmRegister>());
2011 __ movaps(source.As<XmmRegister>(), destination.As<XmmRegister>());
2012 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2013 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
2014 Exchange32(source.As<XmmRegister>(), destination.GetStackIndex());
2015 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
2016 Exchange32(destination.As<XmmRegister>(), source.GetStackIndex());
2017 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
2018 Exchange64(source.As<XmmRegister>(), destination.GetStackIndex());
2019 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
2020 Exchange64(destination.As<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002021 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002022 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002023 }
2024}
2025
2026
2027void ParallelMoveResolverX86_64::SpillScratch(int reg) {
2028 __ pushq(CpuRegister(reg));
2029}
2030
2031
2032void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
2033 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002034}
2035
2036} // namespace x86_64
2037} // namespace art