blob: 4401b9aa68d776392e73e16ed9b6ecf65d5cbe8d [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
Calin Juravled0d48522014-11-04 16:40:20 +000093class DivZeroCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
94 public:
95 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : instruction_(instruction) {}
96
97 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
98 __ Bind(GetEntryLabel());
99 __ gs()->call(
100 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowDivZero), true));
101 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
102 }
103
104 private:
105 HDivZeroCheck* const instruction_;
106 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
107};
108
109class DivMinusOneSlowPathX86_64 : public SlowPathCodeX86_64 {
110 public:
111 explicit DivMinusOneSlowPathX86_64(Register reg) : reg_(reg) {}
112
113 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
114 __ Bind(GetEntryLabel());
115 __ negl(CpuRegister(reg_));
116 __ jmp(GetExitLabel());
117 }
118
119 private:
120 Register reg_;
121 DISALLOW_COPY_AND_ASSIGN(DivMinusOneSlowPathX86_64);
122};
123
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100124class StackOverflowCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100125 public:
126 StackOverflowCheckSlowPathX86_64() {}
127
128 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
129 __ Bind(GetEntryLabel());
130 __ addq(CpuRegister(RSP),
131 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
132 __ gs()->jmp(
133 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowStackOverflow), true));
134 }
135
136 private:
137 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86_64);
138};
139
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100140class SuspendCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000141 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100142 explicit SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
143 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000144
145 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100146 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000147 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100148 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000149 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pTestSuspend), true));
150 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100151 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100152 if (successor_ == nullptr) {
153 __ jmp(GetReturnLabel());
154 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100155 __ jmp(x64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100156 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000157 }
158
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100159 Label* GetReturnLabel() {
160 DCHECK(successor_ == nullptr);
161 return &return_label_;
162 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000163
164 private:
165 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100166 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000167 Label return_label_;
168
169 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
170};
171
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100172class BoundsCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100173 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100174 BoundsCheckSlowPathX86_64(HBoundsCheck* instruction,
175 Location index_location,
176 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100177 : instruction_(instruction),
178 index_location_(index_location),
179 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100180
181 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100182 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100183 __ Bind(GetEntryLabel());
184 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100185 x64_codegen->Move(
186 Location::RegisterLocation(calling_convention.GetRegisterAt(0)), index_location_);
187 x64_codegen->Move(
188 Location::RegisterLocation(calling_convention.GetRegisterAt(1)), length_location_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100189 __ gs()->call(Address::Absolute(
190 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowArrayBounds), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100191 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100192 }
193
194 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100195 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100196 const Location index_location_;
197 const Location length_location_;
198
199 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
200};
201
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000202class LoadClassSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100203 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000204 LoadClassSlowPathX86_64(HLoadClass* cls,
205 HInstruction* at,
206 uint32_t dex_pc,
207 bool do_clinit)
208 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
209 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
210 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100211
212 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000213 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100214 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
215 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100216
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000217 codegen->SaveLiveRegisters(locations);
218
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100219 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000220 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100221 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000222 __ gs()->call(Address::Absolute((do_clinit_
223 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeStaticStorage)
224 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeType)) , true));
225 codegen->RecordPcInfo(at_, dex_pc_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100226
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000227 // Move the class to the desired location.
228 if (locations->Out().IsValid()) {
229 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
230 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
231 }
232
233 codegen->RestoreLiveRegisters(locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100234 __ jmp(GetExitLabel());
235 }
236
237 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000238 // The class this slow path will load.
239 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100240
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000241 // The instruction where this slow path is happening.
242 // (Might be the load class or an initialization check).
243 HInstruction* const at_;
244
245 // The dex PC of `at_`.
246 const uint32_t dex_pc_;
247
248 // Whether to initialize the class.
249 const bool do_clinit_;
250
251 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100252};
253
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000254class LoadStringSlowPathX86_64 : public SlowPathCodeX86_64 {
255 public:
256 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
257
258 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
259 LocationSummary* locations = instruction_->GetLocations();
260 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
261
262 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
263 __ Bind(GetEntryLabel());
264 codegen->SaveLiveRegisters(locations);
265
266 InvokeRuntimeCallingConvention calling_convention;
267 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(0)));
268 __ movl(CpuRegister(calling_convention.GetRegisterAt(1)),
269 Immediate(instruction_->GetStringIndex()));
270 __ gs()->call(Address::Absolute(
271 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pResolveString), true));
272 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
273 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
274 codegen->RestoreLiveRegisters(locations);
275 __ jmp(GetExitLabel());
276 }
277
278 private:
279 HLoadString* const instruction_;
280
281 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
282};
283
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100284#undef __
285#define __ reinterpret_cast<X86_64Assembler*>(GetAssembler())->
286
Dave Allison20dfc792014-06-16 20:44:29 -0700287inline Condition X86_64Condition(IfCondition cond) {
288 switch (cond) {
289 case kCondEQ: return kEqual;
290 case kCondNE: return kNotEqual;
291 case kCondLT: return kLess;
292 case kCondLE: return kLessEqual;
293 case kCondGT: return kGreater;
294 case kCondGE: return kGreaterEqual;
295 default:
296 LOG(FATAL) << "Unknown if condition";
297 }
298 return kEqual;
299}
300
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100301void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
302 stream << X86_64ManagedRegister::FromCpuRegister(Register(reg));
303}
304
305void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
306 stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg));
307}
308
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100309size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
310 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
311 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100312}
313
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100314size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
315 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
316 return kX86_64WordSize;
317}
318
319size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
320 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
321 return kX86_64WordSize;
322}
323
324size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
325 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
326 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100327}
328
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100329CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100330 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfFloatRegisters, 0),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100331 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100332 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000333 instruction_visitor_(graph, this),
334 move_resolver_(graph->GetArena(), this) {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100335
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100336size_t CodeGeneratorX86_64::FrameEntrySpillSize() const {
337 return kNumberOfPushedRegistersAtEntry * kX86_64WordSize;
338}
339
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100340InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
341 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100342 : HGraphVisitor(graph),
343 assembler_(codegen->GetAssembler()),
344 codegen_(codegen) {}
345
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100346Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100347 switch (type) {
348 case Primitive::kPrimLong:
349 case Primitive::kPrimByte:
350 case Primitive::kPrimBoolean:
351 case Primitive::kPrimChar:
352 case Primitive::kPrimShort:
353 case Primitive::kPrimInt:
354 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100355 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100356 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100357 }
358
359 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100360 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100361 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100362 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100363 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100364
365 case Primitive::kPrimVoid:
366 LOG(FATAL) << "Unreachable type " << type;
367 }
368
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100369 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100370}
371
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100372void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100373 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100374 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100375
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000376 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100377 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000378
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100379 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100380 blocked_core_registers_[RBX] = true;
381 blocked_core_registers_[RBP] = true;
382 blocked_core_registers_[R12] = true;
383 blocked_core_registers_[R13] = true;
384 blocked_core_registers_[R14] = true;
385 blocked_core_registers_[R15] = true;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100386
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100387 blocked_fpu_registers_[XMM12] = true;
388 blocked_fpu_registers_[XMM13] = true;
389 blocked_fpu_registers_[XMM14] = true;
390 blocked_fpu_registers_[XMM15] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100391}
392
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100393void CodeGeneratorX86_64::GenerateFrameEntry() {
394 // Create a fake register to mimic Quick.
395 static const int kFakeReturnRegister = 16;
396 core_spill_mask_ |= (1 << kFakeReturnRegister);
397
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100398 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700399 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100400
401 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
402 __ testq(CpuRegister(RAX), Address(
403 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100404 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100405 }
406
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100407 // The return PC has already been pushed on the stack.
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100408 __ subq(CpuRegister(RSP),
409 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
410
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100411 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100412 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86_64();
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100413 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100414
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100415 __ gs()->cmpq(CpuRegister(RSP),
416 Address::Absolute(Thread::StackEndOffset<kX86_64WordSize>(), true));
417 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100418 }
419
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100420 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
421}
422
423void CodeGeneratorX86_64::GenerateFrameExit() {
424 __ addq(CpuRegister(RSP),
425 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
426}
427
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100428void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
429 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100430}
431
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100432void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100433 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
434}
435
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100436Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
437 switch (load->GetType()) {
438 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100439 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100440 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
441 break;
442
443 case Primitive::kPrimInt:
444 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100445 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100446 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100447
448 case Primitive::kPrimBoolean:
449 case Primitive::kPrimByte:
450 case Primitive::kPrimChar:
451 case Primitive::kPrimShort:
452 case Primitive::kPrimVoid:
453 LOG(FATAL) << "Unexpected type " << load->GetType();
454 }
455
456 LOG(FATAL) << "Unreachable";
457 return Location();
458}
459
460void CodeGeneratorX86_64::Move(Location destination, Location source) {
461 if (source.Equals(destination)) {
462 return;
463 }
464 if (destination.IsRegister()) {
465 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100466 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100467 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100468 __ movd(destination.As<CpuRegister>(), source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100469 } else if (source.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100470 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100471 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100472 } else {
473 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100474 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100475 Address(CpuRegister(RSP), source.GetStackIndex()));
476 }
477 } else if (destination.IsFpuRegister()) {
478 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100479 __ movd(destination.As<XmmRegister>(), source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100480 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100481 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100482 } else if (source.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100483 __ movss(destination.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100484 Address(CpuRegister(RSP), source.GetStackIndex()));
485 } else {
486 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100487 __ movsd(destination.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100488 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100489 }
490 } else if (destination.IsStackSlot()) {
491 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100492 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100493 source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100494 } else if (source.IsFpuRegister()) {
495 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100496 source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100497 } else {
498 DCHECK(source.IsStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000499 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
500 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100501 }
502 } else {
503 DCHECK(destination.IsDoubleStackSlot());
504 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100505 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100506 source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100507 } else if (source.IsFpuRegister()) {
508 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100509 source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100510 } else {
511 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000512 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
513 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100514 }
515 }
516}
517
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100518void CodeGeneratorX86_64::Move(HInstruction* instruction,
519 Location location,
520 HInstruction* move_for) {
Roland Levillain476df552014-10-09 17:51:36 +0100521 if (instruction->IsIntConstant()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100522 Immediate imm(instruction->AsIntConstant()->GetValue());
523 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100524 __ movl(location.As<CpuRegister>(), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100525 } else if (location.IsStackSlot()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100526 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100527 } else {
528 DCHECK(location.IsConstant());
529 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100530 }
Roland Levillain476df552014-10-09 17:51:36 +0100531 } else if (instruction->IsLongConstant()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100532 int64_t value = instruction->AsLongConstant()->GetValue();
533 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100534 __ movq(location.As<CpuRegister>(), Immediate(value));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100535 } else if (location.IsDoubleStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000536 __ movq(CpuRegister(TMP), Immediate(value));
537 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100538 } else {
539 DCHECK(location.IsConstant());
540 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100541 }
Roland Levillain476df552014-10-09 17:51:36 +0100542 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100543 switch (instruction->GetType()) {
544 case Primitive::kPrimBoolean:
545 case Primitive::kPrimByte:
546 case Primitive::kPrimChar:
547 case Primitive::kPrimShort:
548 case Primitive::kPrimInt:
549 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100550 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100551 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
552 break;
553
554 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100555 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100556 Move(location, Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
557 break;
558
559 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100560 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100561 }
562 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100563 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100564 switch (instruction->GetType()) {
565 case Primitive::kPrimBoolean:
566 case Primitive::kPrimByte:
567 case Primitive::kPrimChar:
568 case Primitive::kPrimShort:
569 case Primitive::kPrimInt:
570 case Primitive::kPrimNot:
571 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100572 case Primitive::kPrimFloat:
573 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100574 Move(location, instruction->GetLocations()->Out());
575 break;
576
577 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100578 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100579 }
580 }
581}
582
583void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
584 got->SetLocations(nullptr);
585}
586
587void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
588 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100589 DCHECK(!successor->IsExitBlock());
590
591 HBasicBlock* block = got->GetBlock();
592 HInstruction* previous = got->GetPrevious();
593
594 HLoopInformation* info = block->GetLoopInformation();
595 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
596 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
597 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
598 return;
599 }
600
601 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
602 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
603 }
604 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100605 __ jmp(codegen_->GetLabelOf(successor));
606 }
607}
608
609void LocationsBuilderX86_64::VisitExit(HExit* exit) {
610 exit->SetLocations(nullptr);
611}
612
613void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700614 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100615 if (kIsDebugBuild) {
616 __ Comment("Unreachable");
617 __ int3();
618 }
619}
620
621void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100622 LocationSummary* locations =
623 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100624 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100625 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100626 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100627 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100628}
629
630void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700631 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100632 if (cond->IsIntConstant()) {
633 // Constant condition, statically compared against 1.
634 int32_t cond_value = cond->AsIntConstant()->GetValue();
635 if (cond_value == 1) {
636 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
637 if_instr->IfTrueSuccessor())) {
638 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100639 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100640 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100641 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100642 DCHECK_EQ(cond_value, 0);
643 }
644 } else {
645 bool materialized =
646 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
647 // Moves do not affect the eflags register, so if the condition is
648 // evaluated just before the if, we don't need to evaluate it
649 // again.
650 bool eflags_set = cond->IsCondition()
651 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
652 if (materialized) {
653 if (!eflags_set) {
654 // Materialized condition, compare against 0.
655 Location lhs = if_instr->GetLocations()->InAt(0);
656 if (lhs.IsRegister()) {
657 __ cmpl(lhs.As<CpuRegister>(), Immediate(0));
658 } else {
659 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
660 Immediate(0));
661 }
662 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
663 } else {
664 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
665 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
666 }
667 } else {
668 Location lhs = cond->GetLocations()->InAt(0);
669 Location rhs = cond->GetLocations()->InAt(1);
670 if (rhs.IsRegister()) {
671 __ cmpl(lhs.As<CpuRegister>(), rhs.As<CpuRegister>());
672 } else if (rhs.IsConstant()) {
673 __ cmpl(lhs.As<CpuRegister>(),
674 Immediate(rhs.GetConstant()->AsIntConstant()->GetValue()));
675 } else {
676 __ cmpl(lhs.As<CpuRegister>(),
677 Address(CpuRegister(RSP), rhs.GetStackIndex()));
678 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100679 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
680 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700681 }
Dave Allison20dfc792014-06-16 20:44:29 -0700682 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100683 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
684 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700685 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100686 }
687}
688
689void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
690 local->SetLocations(nullptr);
691}
692
693void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
694 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
695}
696
697void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
698 local->SetLocations(nullptr);
699}
700
701void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
702 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700703 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100704}
705
706void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100707 LocationSummary* locations =
708 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100709 switch (store->InputAt(1)->GetType()) {
710 case Primitive::kPrimBoolean:
711 case Primitive::kPrimByte:
712 case Primitive::kPrimChar:
713 case Primitive::kPrimShort:
714 case Primitive::kPrimInt:
715 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100716 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100717 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
718 break;
719
720 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100721 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100722 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
723 break;
724
725 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100726 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100727 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100728}
729
730void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700731 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100732}
733
Dave Allison20dfc792014-06-16 20:44:29 -0700734void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100735 LocationSummary* locations =
736 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100737 locations->SetInAt(0, Location::RequiresRegister());
738 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100739 if (comp->NeedsMaterialization()) {
740 locations->SetOut(Location::RequiresRegister());
741 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100742}
743
Dave Allison20dfc792014-06-16 20:44:29 -0700744void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
745 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100746 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100747 CpuRegister reg = locations->Out().As<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100748 // Clear register: setcc only sets the low byte.
749 __ xorq(reg, reg);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100750 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100751 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100752 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100753 } else if (locations->InAt(1).IsConstant()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100754 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100755 Immediate(locations->InAt(1).GetConstant()->AsIntConstant()->GetValue()));
756 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100757 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100758 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
759 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100760 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700761 }
762}
763
764void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
765 VisitCondition(comp);
766}
767
768void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
769 VisitCondition(comp);
770}
771
772void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
773 VisitCondition(comp);
774}
775
776void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
777 VisitCondition(comp);
778}
779
780void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
781 VisitCondition(comp);
782}
783
784void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
785 VisitCondition(comp);
786}
787
788void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
789 VisitCondition(comp);
790}
791
792void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
793 VisitCondition(comp);
794}
795
796void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
797 VisitCondition(comp);
798}
799
800void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
801 VisitCondition(comp);
802}
803
804void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
805 VisitCondition(comp);
806}
807
808void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
809 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100810}
811
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100812void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100813 LocationSummary* locations =
814 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100815 locations->SetInAt(0, Location::RequiresRegister());
816 locations->SetInAt(1, Location::RequiresRegister());
817 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100818}
819
820void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
821 Label greater, done;
822 LocationSummary* locations = compare->GetLocations();
823 switch (compare->InputAt(0)->GetType()) {
824 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100825 __ cmpq(locations->InAt(0).As<CpuRegister>(),
826 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100827 break;
828 default:
829 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
830 }
831
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100832 CpuRegister output = locations->Out().As<CpuRegister>();
833 __ movl(output, Immediate(0));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100834 __ j(kEqual, &done);
835 __ j(kGreater, &greater);
836
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100837 __ movl(output, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100838 __ jmp(&done);
839
840 __ Bind(&greater);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100841 __ movl(output, Immediate(1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100842
843 __ Bind(&done);
844}
845
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100846void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100847 LocationSummary* locations =
848 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100849 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100850}
851
852void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100853 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700854 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100855}
856
857void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100858 LocationSummary* locations =
859 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100860 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100861}
862
863void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100864 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700865 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100866}
867
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100868void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
869 LocationSummary* locations =
870 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
871 locations->SetOut(Location::ConstantLocation(constant));
872}
873
874void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
875 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700876 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100877}
878
879void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
880 LocationSummary* locations =
881 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
882 locations->SetOut(Location::ConstantLocation(constant));
883}
884
885void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
886 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700887 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100888}
889
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100890void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
891 ret->SetLocations(nullptr);
892}
893
894void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700895 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100896 codegen_->GenerateFrameExit();
897 __ ret();
898}
899
900void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100901 LocationSummary* locations =
902 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100903 switch (ret->InputAt(0)->GetType()) {
904 case Primitive::kPrimBoolean:
905 case Primitive::kPrimByte:
906 case Primitive::kPrimChar:
907 case Primitive::kPrimShort:
908 case Primitive::kPrimInt:
909 case Primitive::kPrimNot:
910 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100911 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100912 break;
913
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100914 case Primitive::kPrimFloat:
915 case Primitive::kPrimDouble:
916 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100917 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100918 break;
919
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100920 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100921 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100922 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100923}
924
925void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
926 if (kIsDebugBuild) {
927 switch (ret->InputAt(0)->GetType()) {
928 case Primitive::kPrimBoolean:
929 case Primitive::kPrimByte:
930 case Primitive::kPrimChar:
931 case Primitive::kPrimShort:
932 case Primitive::kPrimInt:
933 case Primitive::kPrimNot:
934 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100935 DCHECK_EQ(ret->GetLocations()->InAt(0).As<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100936 break;
937
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100938 case Primitive::kPrimFloat:
939 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100940 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100941 XMM0);
942 break;
943
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100944 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100945 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100946 }
947 }
948 codegen_->GenerateFrameExit();
949 __ ret();
950}
951
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100952Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
953 switch (type) {
954 case Primitive::kPrimBoolean:
955 case Primitive::kPrimByte:
956 case Primitive::kPrimChar:
957 case Primitive::kPrimShort:
958 case Primitive::kPrimInt:
959 case Primitive::kPrimNot: {
960 uint32_t index = gp_index_++;
961 stack_index_++;
962 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100963 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100964 } else {
965 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
966 }
967 }
968
969 case Primitive::kPrimLong: {
970 uint32_t index = gp_index_;
971 stack_index_ += 2;
972 if (index < calling_convention.GetNumberOfRegisters()) {
973 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100974 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100975 } else {
976 gp_index_ += 2;
977 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
978 }
979 }
980
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100981 case Primitive::kPrimFloat: {
982 uint32_t index = fp_index_++;
983 stack_index_++;
984 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100985 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100986 } else {
987 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
988 }
989 }
990
991 case Primitive::kPrimDouble: {
992 uint32_t index = fp_index_++;
993 stack_index_ += 2;
994 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100995 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100996 } else {
997 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
998 }
999 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001000
1001 case Primitive::kPrimVoid:
1002 LOG(FATAL) << "Unexpected parameter type " << type;
1003 break;
1004 }
1005 return Location();
1006}
1007
1008void LocationsBuilderX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001009 HandleInvoke(invoke);
1010}
1011
1012void InstructionCodeGeneratorX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001013 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001014 // TODO: Implement all kinds of calls:
1015 // 1) boot -> boot
1016 // 2) app -> boot
1017 // 3) app -> app
1018 //
1019 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1020
1021 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001022 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001023 // temp = temp->dex_cache_resolved_methods_;
1024 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
1025 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001026 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001027 // (temp + offset_of_quick_compiled_code)()
1028 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
1029
1030 DCHECK(!codegen_->IsLeafMethod());
1031 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1032}
1033
1034void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1035 HandleInvoke(invoke);
1036}
1037
1038void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001039 LocationSummary* locations =
1040 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001041 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001042
1043 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001044 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001045 HInstruction* input = invoke->InputAt(i);
1046 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1047 }
1048
1049 switch (invoke->GetType()) {
1050 case Primitive::kPrimBoolean:
1051 case Primitive::kPrimByte:
1052 case Primitive::kPrimChar:
1053 case Primitive::kPrimShort:
1054 case Primitive::kPrimInt:
1055 case Primitive::kPrimNot:
1056 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001057 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001058 break;
1059
1060 case Primitive::kPrimVoid:
1061 break;
1062
1063 case Primitive::kPrimDouble:
1064 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001065 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001066 break;
1067 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001068}
1069
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001070void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001071 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001072 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1073 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1074 LocationSummary* locations = invoke->GetLocations();
1075 Location receiver = locations->InAt(0);
1076 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1077 // temp = object->GetClass();
1078 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001079 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1080 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001081 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001082 __ movl(temp, Address(receiver.As<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001083 }
1084 // temp = temp->GetMethodAt(method_offset);
1085 __ movl(temp, Address(temp, method_offset));
1086 // call temp->GetEntryPoint();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001087 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
1088
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001089 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001090 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001091}
1092
Roland Levillain88cb1752014-10-20 16:36:47 +01001093void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1094 LocationSummary* locations =
1095 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1096 switch (neg->GetResultType()) {
1097 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001098 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001099 locations->SetInAt(0, Location::RequiresRegister());
1100 locations->SetOut(Location::SameAsFirstInput());
1101 break;
1102
Roland Levillain88cb1752014-10-20 16:36:47 +01001103 case Primitive::kPrimFloat:
1104 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001105 locations->SetInAt(0, Location::RequiresFpuRegister());
1106 // Output overlaps as we need a fresh (zero-initialized)
1107 // register to perform subtraction from zero.
1108 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001109 break;
1110
1111 default:
1112 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1113 }
1114}
1115
1116void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1117 LocationSummary* locations = neg->GetLocations();
1118 Location out = locations->Out();
1119 Location in = locations->InAt(0);
1120 switch (neg->GetResultType()) {
1121 case Primitive::kPrimInt:
1122 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001123 DCHECK(in.Equals(out));
Roland Levillain88cb1752014-10-20 16:36:47 +01001124 __ negl(out.As<CpuRegister>());
1125 break;
1126
1127 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001128 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001129 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001130 __ negq(out.As<CpuRegister>());
1131 break;
1132
Roland Levillain88cb1752014-10-20 16:36:47 +01001133 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001134 DCHECK(in.IsFpuRegister());
1135 DCHECK(out.IsFpuRegister());
1136 DCHECK(!in.Equals(out));
1137 // TODO: Instead of computing negation as a subtraction from
1138 // zero, implement it with an exclusive or with value 0x80000000
1139 // (mask for bit 31, representing the sign of a single-precision
1140 // floating-point number), fetched from a constant pool:
1141 //
1142 // xorps out, [RIP:...] // value at RIP is 0x80 00 00 00
1143
1144 // out = 0
1145 __ xorps(out.As<XmmRegister>(), out.As<XmmRegister>());
1146 // out = out - in
1147 __ subss(out.As<XmmRegister>(), in.As<XmmRegister>());
1148 break;
1149
Roland Levillain88cb1752014-10-20 16:36:47 +01001150 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001151 DCHECK(in.IsFpuRegister());
1152 DCHECK(out.IsFpuRegister());
1153 DCHECK(!in.Equals(out));
1154 // TODO: Instead of computing negation as a subtraction from
1155 // zero, implement it with an exclusive or with value
1156 // 0x8000000000000000 (mask for bit 63, representing the sign of
1157 // a double-precision floating-point number), fetched from a
1158 // constant pool:
1159 //
1160 // xorpd out, [RIP:...] // value at RIP is 0x80 00 00 00 00 00 00 00
1161
1162 // out = 0
1163 __ xorpd(out.As<XmmRegister>(), out.As<XmmRegister>());
1164 // out = out - in
1165 __ subsd(out.As<XmmRegister>(), in.As<XmmRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001166 break;
1167
1168 default:
1169 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1170 }
1171}
1172
Roland Levillaindff1f282014-11-05 14:15:05 +00001173void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1174 LocationSummary* locations =
1175 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1176 Primitive::Type result_type = conversion->GetResultType();
1177 Primitive::Type input_type = conversion->GetInputType();
1178 switch (result_type) {
1179 case Primitive::kPrimLong:
1180 switch (input_type) {
1181 case Primitive::kPrimByte:
1182 case Primitive::kPrimShort:
1183 case Primitive::kPrimInt:
1184 // int-to-long conversion.
1185 // TODO: We would benefit from a (to-be-implemented)
1186 // Location::RegisterOrStackSlot requirement for this input.
1187 locations->SetInAt(0, Location::RequiresRegister());
1188 locations->SetOut(Location::RequiresRegister());
1189 break;
1190
1191 case Primitive::kPrimFloat:
1192 case Primitive::kPrimDouble:
1193 LOG(FATAL) << "Type conversion from " << input_type << " to "
1194 << result_type << " not yet implemented";
1195 break;
1196
1197 default:
1198 LOG(FATAL) << "Unexpected type conversion from " << input_type
1199 << " to " << result_type;
1200 }
1201 break;
1202
1203 case Primitive::kPrimInt:
1204 case Primitive::kPrimFloat:
1205 case Primitive::kPrimDouble:
1206 LOG(FATAL) << "Type conversion from " << input_type
1207 << " to " << result_type << " not yet implemented";
1208 break;
1209
1210 default:
1211 LOG(FATAL) << "Unexpected type conversion from " << input_type
1212 << " to " << result_type;
1213 }
1214}
1215
1216void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1217 LocationSummary* locations = conversion->GetLocations();
1218 Location out = locations->Out();
1219 Location in = locations->InAt(0);
1220 Primitive::Type result_type = conversion->GetResultType();
1221 Primitive::Type input_type = conversion->GetInputType();
1222 switch (result_type) {
1223 case Primitive::kPrimLong:
1224 switch (input_type) {
1225 DCHECK(out.IsRegister());
1226 case Primitive::kPrimByte:
1227 case Primitive::kPrimShort:
1228 case Primitive::kPrimInt:
1229 // int-to-long conversion.
1230 DCHECK(in.IsRegister());
1231 __ movsxd(out.As<CpuRegister>(), in.As<CpuRegister>());
1232 break;
1233
1234 case Primitive::kPrimFloat:
1235 case Primitive::kPrimDouble:
1236 LOG(FATAL) << "Type conversion from " << input_type << " to "
1237 << result_type << " not yet implemented";
1238 break;
1239
1240 default:
1241 LOG(FATAL) << "Unexpected type conversion from " << input_type
1242 << " to " << result_type;
1243 }
1244 break;
1245
1246 case Primitive::kPrimInt:
1247 case Primitive::kPrimFloat:
1248 case Primitive::kPrimDouble:
1249 LOG(FATAL) << "Type conversion from " << input_type
1250 << " to " << result_type << " not yet implemented";
1251 break;
1252
1253 default:
1254 LOG(FATAL) << "Unexpected type conversion from " << input_type
1255 << " to " << result_type;
1256 }
1257}
1258
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001259void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001260 LocationSummary* locations =
1261 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001262 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001263 case Primitive::kPrimInt: {
1264 locations->SetInAt(0, Location::RequiresRegister());
1265 locations->SetInAt(1, Location::Any());
1266 locations->SetOut(Location::SameAsFirstInput());
1267 break;
1268 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001269
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001270 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001271 locations->SetInAt(0, Location::RequiresRegister());
1272 locations->SetInAt(1, Location::RequiresRegister());
1273 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001274 break;
1275 }
1276
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001277 case Primitive::kPrimDouble:
1278 case Primitive::kPrimFloat: {
1279 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001280 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001281 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001282 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001283 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001284
1285 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001286 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001287 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001288}
1289
1290void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1291 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001292 Location first = locations->InAt(0);
1293 Location second = locations->InAt(1);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001294 DCHECK(first.Equals(locations->Out()));
Calin Juravle11351682014-10-23 15:38:15 +01001295
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001296 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001297 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001298 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001299 __ addl(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001300 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001301 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001302 __ addl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001303 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001304 __ addl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001305 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001306 break;
1307 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001308
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001309 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001310 __ addq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001311 break;
1312 }
1313
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001314 case Primitive::kPrimFloat: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001315 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001316 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001317 }
1318
1319 case Primitive::kPrimDouble: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001320 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001321 break;
1322 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001323
1324 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001325 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001326 }
1327}
1328
1329void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001330 LocationSummary* locations =
1331 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001332 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001333 case Primitive::kPrimInt: {
1334 locations->SetInAt(0, Location::RequiresRegister());
1335 locations->SetInAt(1, Location::Any());
1336 locations->SetOut(Location::SameAsFirstInput());
1337 break;
1338 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001339 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001340 locations->SetInAt(0, Location::RequiresRegister());
1341 locations->SetInAt(1, Location::RequiresRegister());
1342 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001343 break;
1344 }
Calin Juravle11351682014-10-23 15:38:15 +01001345 case Primitive::kPrimFloat:
1346 case Primitive::kPrimDouble: {
1347 locations->SetInAt(0, Location::RequiresFpuRegister());
1348 locations->SetInAt(1, Location::RequiresFpuRegister());
1349 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001350 break;
Calin Juravle11351682014-10-23 15:38:15 +01001351 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001352 default:
Calin Juravle11351682014-10-23 15:38:15 +01001353 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001354 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001355}
1356
1357void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1358 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001359 Location first = locations->InAt(0);
1360 Location second = locations->InAt(1);
1361 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001362 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001363 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001364 if (second.IsRegister()) {
1365 __ subl(first.As<CpuRegister>(), second.As<CpuRegister>());
1366 } else if (second.IsConstant()) {
1367 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1368 __ subl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001369 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001370 __ subl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001371 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001372 break;
1373 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001374 case Primitive::kPrimLong: {
Calin Juravle11351682014-10-23 15:38:15 +01001375 __ subq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001376 break;
1377 }
1378
Calin Juravle11351682014-10-23 15:38:15 +01001379 case Primitive::kPrimFloat: {
1380 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001381 break;
Calin Juravle11351682014-10-23 15:38:15 +01001382 }
1383
1384 case Primitive::kPrimDouble: {
1385 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1386 break;
1387 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001388
1389 default:
Calin Juravle11351682014-10-23 15:38:15 +01001390 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001391 }
1392}
1393
Calin Juravle34bacdf2014-10-07 20:23:36 +01001394void LocationsBuilderX86_64::VisitMul(HMul* mul) {
1395 LocationSummary* locations =
1396 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1397 switch (mul->GetResultType()) {
1398 case Primitive::kPrimInt: {
1399 locations->SetInAt(0, Location::RequiresRegister());
1400 locations->SetInAt(1, Location::Any());
1401 locations->SetOut(Location::SameAsFirstInput());
1402 break;
1403 }
1404 case Primitive::kPrimLong: {
1405 locations->SetInAt(0, Location::RequiresRegister());
1406 locations->SetInAt(1, Location::RequiresRegister());
1407 locations->SetOut(Location::SameAsFirstInput());
1408 break;
1409 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001410 case Primitive::kPrimFloat:
1411 case Primitive::kPrimDouble: {
1412 locations->SetInAt(0, Location::RequiresFpuRegister());
1413 locations->SetInAt(1, Location::RequiresFpuRegister());
1414 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001415 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001416 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001417
1418 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001419 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001420 }
1421}
1422
1423void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
1424 LocationSummary* locations = mul->GetLocations();
1425 Location first = locations->InAt(0);
1426 Location second = locations->InAt(1);
1427 DCHECK(first.Equals(locations->Out()));
1428 switch (mul->GetResultType()) {
1429 case Primitive::kPrimInt: {
1430 if (second.IsRegister()) {
1431 __ imull(first.As<CpuRegister>(), second.As<CpuRegister>());
1432 } else if (second.IsConstant()) {
1433 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1434 __ imull(first.As<CpuRegister>(), imm);
1435 } else {
1436 DCHECK(second.IsStackSlot());
1437 __ imull(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
1438 }
1439 break;
1440 }
1441 case Primitive::kPrimLong: {
1442 __ imulq(first.As<CpuRegister>(), second.As<CpuRegister>());
1443 break;
1444 }
1445
Calin Juravleb5bfa962014-10-21 18:02:24 +01001446 case Primitive::kPrimFloat: {
1447 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001448 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001449 }
1450
1451 case Primitive::kPrimDouble: {
1452 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1453 break;
1454 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001455
1456 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001457 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001458 }
1459}
1460
Calin Juravle7c4954d2014-10-28 16:57:40 +00001461void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
1462 LocationSummary* locations =
1463 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1464 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001465 case Primitive::kPrimInt: {
1466 locations->SetInAt(0, Location::RegisterLocation(RAX));
1467 locations->SetInAt(1, Location::RequiresRegister());
1468 locations->SetOut(Location::SameAsFirstInput());
1469 // Intel uses edx:eax as the dividend.
1470 locations->AddTemp(Location::RegisterLocation(RDX));
1471 break;
1472 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001473 case Primitive::kPrimLong: {
1474 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1475 break;
1476 }
1477 case Primitive::kPrimFloat:
1478 case Primitive::kPrimDouble: {
1479 locations->SetInAt(0, Location::RequiresFpuRegister());
1480 locations->SetInAt(1, Location::RequiresFpuRegister());
1481 locations->SetOut(Location::SameAsFirstInput());
1482 break;
1483 }
1484
1485 default:
1486 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1487 }
1488}
1489
1490void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
1491 LocationSummary* locations = div->GetLocations();
1492 Location first = locations->InAt(0);
1493 Location second = locations->InAt(1);
1494 DCHECK(first.Equals(locations->Out()));
1495
1496 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001497 case Primitive::kPrimInt: {
1498 CpuRegister first_reg = first.As<CpuRegister>();
1499 CpuRegister second_reg = second.As<CpuRegister>();
1500 DCHECK_EQ(RAX, first_reg.AsRegister());
1501 DCHECK_EQ(RDX, locations->GetTemp(0).As<CpuRegister>().AsRegister());
1502
1503 SlowPathCodeX86_64* slow_path =
1504 new (GetGraph()->GetArena()) DivMinusOneSlowPathX86_64(first_reg.AsRegister());
1505 codegen_->AddSlowPath(slow_path);
1506
1507 // 0x80000000/-1 triggers an arithmetic exception!
1508 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
1509 // it's safe to just use negl instead of more complex comparisons.
1510
1511 __ cmpl(second_reg, Immediate(-1));
1512 __ j(kEqual, slow_path->GetEntryLabel());
1513
1514 // edx:eax <- sign-extended of eax
1515 __ cdq();
1516 // eax = quotient, edx = remainder
1517 __ idivl(second_reg);
1518
1519 __ Bind(slow_path->GetExitLabel());
1520 break;
1521 }
1522
Calin Juravle7c4954d2014-10-28 16:57:40 +00001523 case Primitive::kPrimLong: {
1524 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1525 break;
1526 }
1527
1528 case Primitive::kPrimFloat: {
1529 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1530 break;
1531 }
1532
1533 case Primitive::kPrimDouble: {
1534 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1535 break;
1536 }
1537
1538 default:
1539 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1540 }
1541}
1542
Calin Juravled0d48522014-11-04 16:40:20 +00001543void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1544 LocationSummary* locations =
1545 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1546 locations->SetInAt(0, Location::Any());
1547 if (instruction->HasUses()) {
1548 locations->SetOut(Location::SameAsFirstInput());
1549 }
1550}
1551
1552void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1553 SlowPathCodeX86_64* slow_path =
1554 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
1555 codegen_->AddSlowPath(slow_path);
1556
1557 LocationSummary* locations = instruction->GetLocations();
1558 Location value = locations->InAt(0);
1559
1560 if (value.IsRegister()) {
1561 __ testl(value.As<CpuRegister>(), value.As<CpuRegister>());
1562 } else if (value.IsStackSlot()) {
1563 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
1564 } else {
1565 DCHECK(value.IsConstant()) << value;
1566 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
1567 __ jmp(slow_path->GetEntryLabel());
1568 }
1569 return;
1570 }
1571 __ j(kEqual, slow_path->GetEntryLabel());
1572}
1573
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001574void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001575 LocationSummary* locations =
1576 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001577 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001578 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1579 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1580 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001581}
1582
1583void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
1584 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001585 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001586 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1587
1588 __ gs()->call(Address::Absolute(
1589 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
1590
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001591 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001592 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001593}
1594
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001595void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
1596 LocationSummary* locations =
1597 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1598 InvokeRuntimeCallingConvention calling_convention;
1599 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1600 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1601 locations->SetOut(Location::RegisterLocation(RAX));
1602 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1603}
1604
1605void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
1606 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001607 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001608 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1609
1610 __ gs()->call(Address::Absolute(
1611 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocArrayWithAccessCheck), true));
1612
1613 DCHECK(!codegen_->IsLeafMethod());
1614 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1615}
1616
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001617void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001618 LocationSummary* locations =
1619 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001620 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1621 if (location.IsStackSlot()) {
1622 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1623 } else if (location.IsDoubleStackSlot()) {
1624 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1625 }
1626 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001627}
1628
1629void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
1630 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001631 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001632}
1633
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001634void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001635 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001636 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001637 locations->SetInAt(0, Location::RequiresRegister());
1638 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001639}
1640
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001641void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
1642 LocationSummary* locations = not_->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001643 DCHECK_EQ(locations->InAt(0).As<CpuRegister>().AsRegister(),
1644 locations->Out().As<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001645 Location out = locations->Out();
1646 switch (not_->InputAt(0)->GetType()) {
1647 case Primitive::kPrimBoolean:
1648 __ xorq(out.As<CpuRegister>(), Immediate(1));
1649 break;
1650
1651 case Primitive::kPrimInt:
1652 __ notl(out.As<CpuRegister>());
1653 break;
1654
1655 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001656 __ notq(out.As<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001657 break;
1658
1659 default:
1660 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1661 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001662}
1663
1664void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001665 LocationSummary* locations =
1666 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001667 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1668 locations->SetInAt(i, Location::Any());
1669 }
1670 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001671}
1672
1673void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001674 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001675 LOG(FATAL) << "Unimplemented";
1676}
1677
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001678void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001679 LocationSummary* locations =
1680 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001681 Primitive::Type field_type = instruction->GetFieldType();
1682 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001683 locations->SetInAt(0, Location::RequiresRegister());
1684 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001685 if (is_object_type) {
1686 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001687 locations->AddTemp(Location::RequiresRegister());
1688 locations->AddTemp(Location::RequiresRegister());
1689 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001690}
1691
1692void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1693 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001694 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
1695 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001696 size_t offset = instruction->GetFieldOffset().SizeValue();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001697 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001698
1699 switch (field_type) {
1700 case Primitive::kPrimBoolean:
1701 case Primitive::kPrimByte: {
1702 __ movb(Address(obj, offset), value);
1703 break;
1704 }
1705
1706 case Primitive::kPrimShort:
1707 case Primitive::kPrimChar: {
1708 __ movw(Address(obj, offset), value);
1709 break;
1710 }
1711
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001712 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001713 case Primitive::kPrimNot: {
1714 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001715 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001716 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
1717 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001718 codegen_->MarkGCCard(temp, card, obj, value);
1719 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001720 break;
1721 }
1722
1723 case Primitive::kPrimLong: {
1724 __ movq(Address(obj, offset), value);
1725 break;
1726 }
1727
1728 case Primitive::kPrimFloat:
1729 case Primitive::kPrimDouble:
1730 LOG(FATAL) << "Unimplemented register type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001731 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001732 case Primitive::kPrimVoid:
1733 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001734 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001735 }
1736}
1737
1738void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001739 LocationSummary* locations =
1740 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001741 locations->SetInAt(0, Location::RequiresRegister());
1742 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001743}
1744
1745void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1746 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001747 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
1748 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001749 size_t offset = instruction->GetFieldOffset().SizeValue();
1750
1751 switch (instruction->GetType()) {
1752 case Primitive::kPrimBoolean: {
1753 __ movzxb(out, Address(obj, offset));
1754 break;
1755 }
1756
1757 case Primitive::kPrimByte: {
1758 __ movsxb(out, Address(obj, offset));
1759 break;
1760 }
1761
1762 case Primitive::kPrimShort: {
1763 __ movsxw(out, Address(obj, offset));
1764 break;
1765 }
1766
1767 case Primitive::kPrimChar: {
1768 __ movzxw(out, Address(obj, offset));
1769 break;
1770 }
1771
1772 case Primitive::kPrimInt:
1773 case Primitive::kPrimNot: {
1774 __ movl(out, Address(obj, offset));
1775 break;
1776 }
1777
1778 case Primitive::kPrimLong: {
1779 __ movq(out, Address(obj, offset));
1780 break;
1781 }
1782
1783 case Primitive::kPrimFloat:
1784 case Primitive::kPrimDouble:
1785 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001786 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001787 case Primitive::kPrimVoid:
1788 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001789 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001790 }
1791}
1792
1793void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001794 LocationSummary* locations =
1795 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001796 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001797 if (instruction->HasUses()) {
1798 locations->SetOut(Location::SameAsFirstInput());
1799 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001800}
1801
1802void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001803 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001804 codegen_->AddSlowPath(slow_path);
1805
1806 LocationSummary* locations = instruction->GetLocations();
1807 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001808
1809 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001810 __ cmpl(obj.As<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001811 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001812 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001813 } else {
1814 DCHECK(obj.IsConstant()) << obj;
1815 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1816 __ jmp(slow_path->GetEntryLabel());
1817 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001818 }
1819 __ j(kEqual, slow_path->GetEntryLabel());
1820}
1821
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001822void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001823 LocationSummary* locations =
1824 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001825 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001826 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001827 1, Location::RegisterOrConstant(instruction->InputAt(1)));
1828 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001829}
1830
1831void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
1832 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001833 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001834 Location index = locations->InAt(1);
1835
1836 switch (instruction->GetType()) {
1837 case Primitive::kPrimBoolean: {
1838 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001839 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001840 if (index.IsConstant()) {
1841 __ movzxb(out, Address(obj,
1842 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1843 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001844 __ movzxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001845 }
1846 break;
1847 }
1848
1849 case Primitive::kPrimByte: {
1850 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001851 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001852 if (index.IsConstant()) {
1853 __ movsxb(out, Address(obj,
1854 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1855 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001856 __ movsxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001857 }
1858 break;
1859 }
1860
1861 case Primitive::kPrimShort: {
1862 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001863 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001864 if (index.IsConstant()) {
1865 __ movsxw(out, Address(obj,
1866 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1867 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001868 __ movsxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001869 }
1870 break;
1871 }
1872
1873 case Primitive::kPrimChar: {
1874 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001875 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001876 if (index.IsConstant()) {
1877 __ movzxw(out, Address(obj,
1878 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1879 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001880 __ movzxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001881 }
1882 break;
1883 }
1884
1885 case Primitive::kPrimInt:
1886 case Primitive::kPrimNot: {
1887 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1888 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001889 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001890 if (index.IsConstant()) {
1891 __ movl(out, Address(obj,
1892 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1893 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001894 __ movl(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001895 }
1896 break;
1897 }
1898
1899 case Primitive::kPrimLong: {
1900 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001901 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001902 if (index.IsConstant()) {
1903 __ movq(out, Address(obj,
1904 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
1905 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001906 __ movq(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001907 }
1908 break;
1909 }
1910
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001911 case Primitive::kPrimFloat: {
1912 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1913 XmmRegister out = locations->Out().As<XmmRegister>();
1914 if (index.IsConstant()) {
1915 __ movss(out, Address(obj,
1916 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1917 } else {
1918 __ movss(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
1919 }
1920 break;
1921 }
1922
1923 case Primitive::kPrimDouble: {
1924 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1925 XmmRegister out = locations->Out().As<XmmRegister>();
1926 if (index.IsConstant()) {
1927 __ movsd(out, Address(obj,
1928 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
1929 } else {
1930 __ movsd(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
1931 }
1932 break;
1933 }
1934
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001935 case Primitive::kPrimVoid:
1936 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001937 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001938 }
1939}
1940
1941void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001942 Primitive::Type value_type = instruction->GetComponentType();
1943 bool is_object = value_type == Primitive::kPrimNot;
1944 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1945 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
1946 if (is_object) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001947 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001948 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1949 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1950 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001951 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001952 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001953 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001954 1, Location::RegisterOrConstant(instruction->InputAt(1)));
1955 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001956 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001957 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001958 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
1959 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001960 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001961 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001962 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001963 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001964}
1965
1966void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
1967 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001968 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001969 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001970 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001971 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001972
1973 switch (value_type) {
1974 case Primitive::kPrimBoolean:
1975 case Primitive::kPrimByte: {
1976 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001977 if (index.IsConstant()) {
1978 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001979 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001980 __ movb(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001981 } else {
1982 __ movb(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1983 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001984 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001985 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001986 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
1987 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001988 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001989 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001990 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1991 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001992 }
1993 break;
1994 }
1995
1996 case Primitive::kPrimShort:
1997 case Primitive::kPrimChar: {
1998 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001999 if (index.IsConstant()) {
2000 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002001 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002002 __ movw(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002003 } else {
2004 __ movw(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2005 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002006 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002007 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002008 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
2009 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002010 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002011 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002012 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2013 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002014 }
2015 break;
2016 }
2017
2018 case Primitive::kPrimInt: {
2019 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002020 if (index.IsConstant()) {
2021 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002022 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002023 __ movl(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002024 } else {
2025 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2026 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002027 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002028 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002029 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2030 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002031 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002032 DCHECK(value.IsConstant()) << value;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002033 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002034 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2035 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002036 }
2037 break;
2038 }
2039
2040 case Primitive::kPrimNot: {
2041 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject), true));
2042 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002043 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002044 break;
2045 }
2046
2047 case Primitive::kPrimLong: {
2048 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002049 if (index.IsConstant()) {
2050 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002051 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002052 __ movq(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002053 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002054 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002055 __ movq(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
2056 value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002057 }
2058 break;
2059 }
2060
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002061 case Primitive::kPrimFloat: {
2062 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2063 if (index.IsConstant()) {
2064 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2065 DCHECK(value.IsFpuRegister());
2066 __ movss(Address(obj, offset), value.As<XmmRegister>());
2067 } else {
2068 DCHECK(value.IsFpuRegister());
2069 __ movss(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2070 value.As<XmmRegister>());
2071 }
2072 break;
2073 }
2074
2075 case Primitive::kPrimDouble: {
2076 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2077 if (index.IsConstant()) {
2078 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
2079 DCHECK(value.IsFpuRegister());
2080 __ movsd(Address(obj, offset), value.As<XmmRegister>());
2081 } else {
2082 DCHECK(value.IsFpuRegister());
2083 __ movsd(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
2084 value.As<XmmRegister>());
2085 }
2086 break;
2087 }
2088
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002089 case Primitive::kPrimVoid:
2090 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002091 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002092 }
2093}
2094
2095void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002096 LocationSummary* locations =
2097 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002098 locations->SetInAt(0, Location::RequiresRegister());
2099 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002100}
2101
2102void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
2103 LocationSummary* locations = instruction->GetLocations();
2104 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002105 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2106 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002107 __ movl(out, Address(obj, offset));
2108}
2109
2110void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002111 LocationSummary* locations =
2112 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002113 locations->SetInAt(0, Location::RequiresRegister());
2114 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002115 if (instruction->HasUses()) {
2116 locations->SetOut(Location::SameAsFirstInput());
2117 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002118}
2119
2120void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
2121 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002122 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002123 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002124 codegen_->AddSlowPath(slow_path);
2125
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002126 CpuRegister index = locations->InAt(0).As<CpuRegister>();
2127 CpuRegister length = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002128
2129 __ cmpl(index, length);
2130 __ j(kAboveEqual, slow_path->GetEntryLabel());
2131}
2132
2133void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
2134 CpuRegister card,
2135 CpuRegister object,
2136 CpuRegister value) {
2137 Label is_null;
2138 __ testl(value, value);
2139 __ j(kEqual, &is_null);
2140 __ gs()->movq(card, Address::Absolute(
2141 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
2142 __ movq(temp, object);
2143 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
2144 __ movb(Address(temp, card, TIMES_1, 0), card);
2145 __ Bind(&is_null);
2146}
2147
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002148void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
2149 temp->SetLocations(nullptr);
2150}
2151
2152void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
2153 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002154 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002155}
2156
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002157void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002158 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002159 LOG(FATAL) << "Unimplemented";
2160}
2161
2162void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002163 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2164}
2165
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002166void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
2167 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2168}
2169
2170void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002171 HBasicBlock* block = instruction->GetBlock();
2172 if (block->GetLoopInformation() != nullptr) {
2173 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2174 // The back edge will generate the suspend check.
2175 return;
2176 }
2177 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2178 // The goto will generate the suspend check.
2179 return;
2180 }
2181 GenerateSuspendCheck(instruction, nullptr);
2182}
2183
2184void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
2185 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002186 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002187 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002188 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002189 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002190 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002191 if (successor == nullptr) {
2192 __ j(kNotEqual, slow_path->GetEntryLabel());
2193 __ Bind(slow_path->GetReturnLabel());
2194 } else {
2195 __ j(kEqual, codegen_->GetLabelOf(successor));
2196 __ jmp(slow_path->GetEntryLabel());
2197 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002198}
2199
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002200X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
2201 return codegen_->GetAssembler();
2202}
2203
2204void ParallelMoveResolverX86_64::EmitMove(size_t index) {
2205 MoveOperands* move = moves_.Get(index);
2206 Location source = move->GetSource();
2207 Location destination = move->GetDestination();
2208
2209 if (source.IsRegister()) {
2210 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002211 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002212 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002213 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002214 source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002215 } else {
2216 DCHECK(destination.IsDoubleStackSlot());
2217 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002218 source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002219 }
2220 } else if (source.IsStackSlot()) {
2221 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002222 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002223 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002224 } else if (destination.IsFpuRegister()) {
2225 __ movss(destination.As<XmmRegister>(),
2226 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002227 } else {
2228 DCHECK(destination.IsStackSlot());
2229 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2230 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2231 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002232 } else if (source.IsDoubleStackSlot()) {
2233 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002234 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002235 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002236 } else if (destination.IsFpuRegister()) {
2237 __ movsd(destination.As<XmmRegister>(), Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002238 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01002239 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002240 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2241 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2242 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002243 } else if (source.IsConstant()) {
2244 HConstant* constant = source.GetConstant();
2245 if (constant->IsIntConstant()) {
2246 Immediate imm(constant->AsIntConstant()->GetValue());
2247 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002248 __ movl(destination.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002249 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002250 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002251 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2252 }
2253 } else if (constant->IsLongConstant()) {
2254 int64_t value = constant->AsLongConstant()->GetValue();
2255 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002256 __ movq(destination.As<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002257 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002258 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002259 __ movq(CpuRegister(TMP), Immediate(value));
2260 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2261 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002262 } else if (constant->IsFloatConstant()) {
2263 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
2264 if (destination.IsFpuRegister()) {
2265 __ movl(CpuRegister(TMP), imm);
2266 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2267 } else {
2268 DCHECK(destination.IsStackSlot()) << destination;
2269 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2270 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002271 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002272 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
2273 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
2274 if (destination.IsFpuRegister()) {
2275 __ movq(CpuRegister(TMP), imm);
2276 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2277 } else {
2278 DCHECK(destination.IsDoubleStackSlot()) << destination;
2279 __ movq(CpuRegister(TMP), imm);
2280 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2281 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002282 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002283 } else if (source.IsFpuRegister()) {
2284 if (destination.IsFpuRegister()) {
2285 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
2286 } else if (destination.IsStackSlot()) {
2287 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
2288 source.As<XmmRegister>());
2289 } else {
2290 DCHECK(destination.IsDoubleStackSlot());
2291 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
2292 source.As<XmmRegister>());
2293 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002294 }
2295}
2296
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002297void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002298 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002299 __ movl(Address(CpuRegister(RSP), mem), reg);
2300 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002301}
2302
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002303void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002304 ScratchRegisterScope ensure_scratch(
2305 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2306
2307 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2308 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2309 __ movl(CpuRegister(ensure_scratch.GetRegister()),
2310 Address(CpuRegister(RSP), mem2 + stack_offset));
2311 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2312 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
2313 CpuRegister(ensure_scratch.GetRegister()));
2314}
2315
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002316void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
2317 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2318 __ movq(Address(CpuRegister(RSP), mem), reg);
2319 __ movq(reg, CpuRegister(TMP));
2320}
2321
2322void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
2323 ScratchRegisterScope ensure_scratch(
2324 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2325
2326 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2327 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2328 __ movq(CpuRegister(ensure_scratch.GetRegister()),
2329 Address(CpuRegister(RSP), mem2 + stack_offset));
2330 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2331 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
2332 CpuRegister(ensure_scratch.GetRegister()));
2333}
2334
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002335void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
2336 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2337 __ movss(Address(CpuRegister(RSP), mem), reg);
2338 __ movd(reg, CpuRegister(TMP));
2339}
2340
2341void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
2342 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2343 __ movsd(Address(CpuRegister(RSP), mem), reg);
2344 __ movd(reg, CpuRegister(TMP));
2345}
2346
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002347void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
2348 MoveOperands* move = moves_.Get(index);
2349 Location source = move->GetSource();
2350 Location destination = move->GetDestination();
2351
2352 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002353 __ xchgq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002354 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002355 Exchange32(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002356 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002357 Exchange32(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002358 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002359 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
2360 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002361 Exchange64(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002362 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002363 Exchange64(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002364 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
2365 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002366 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
2367 __ movd(CpuRegister(TMP), source.As<XmmRegister>());
2368 __ movaps(source.As<XmmRegister>(), destination.As<XmmRegister>());
2369 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2370 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
2371 Exchange32(source.As<XmmRegister>(), destination.GetStackIndex());
2372 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
2373 Exchange32(destination.As<XmmRegister>(), source.GetStackIndex());
2374 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
2375 Exchange64(source.As<XmmRegister>(), destination.GetStackIndex());
2376 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
2377 Exchange64(destination.As<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002378 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002379 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002380 }
2381}
2382
2383
2384void ParallelMoveResolverX86_64::SpillScratch(int reg) {
2385 __ pushq(CpuRegister(reg));
2386}
2387
2388
2389void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
2390 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002391}
2392
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002393void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
2394 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
2395 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2396 Immediate(mirror::Class::kStatusInitialized));
2397 __ j(kLess, slow_path->GetEntryLabel());
2398 __ Bind(slow_path->GetExitLabel());
2399 // No need for memory fence, thanks to the X86_64 memory model.
2400}
2401
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002402void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002403 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2404 ? LocationSummary::kCallOnSlowPath
2405 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002406 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002407 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002408 locations->SetOut(Location::RequiresRegister());
2409}
2410
2411void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
2412 CpuRegister out = cls->GetLocations()->Out().As<CpuRegister>();
2413 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002414 DCHECK(!cls->CanCallRuntime());
2415 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002416 codegen_->LoadCurrentMethod(out);
2417 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2418 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002419 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002420 codegen_->LoadCurrentMethod(out);
2421 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2422 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002423 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
2424 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2425 codegen_->AddSlowPath(slow_path);
2426 __ testl(out, out);
2427 __ j(kEqual, slow_path->GetEntryLabel());
2428 if (cls->MustGenerateClinitCheck()) {
2429 GenerateClassInitializationCheck(slow_path, out);
2430 } else {
2431 __ Bind(slow_path->GetExitLabel());
2432 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002433 }
2434}
2435
2436void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
2437 LocationSummary* locations =
2438 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2439 locations->SetInAt(0, Location::RequiresRegister());
2440 if (check->HasUses()) {
2441 locations->SetOut(Location::SameAsFirstInput());
2442 }
2443}
2444
2445void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002446 // We assume the class to not be null.
2447 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
2448 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002449 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002450 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002451}
2452
2453void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2454 LocationSummary* locations =
2455 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2456 locations->SetInAt(0, Location::RequiresRegister());
2457 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2458}
2459
2460void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2461 LocationSummary* locations = instruction->GetLocations();
2462 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
2463 CpuRegister out = locations->Out().As<CpuRegister>();
2464 size_t offset = instruction->GetFieldOffset().SizeValue();
2465
2466 switch (instruction->GetType()) {
2467 case Primitive::kPrimBoolean: {
2468 __ movzxb(out, Address(cls, offset));
2469 break;
2470 }
2471
2472 case Primitive::kPrimByte: {
2473 __ movsxb(out, Address(cls, offset));
2474 break;
2475 }
2476
2477 case Primitive::kPrimShort: {
2478 __ movsxw(out, Address(cls, offset));
2479 break;
2480 }
2481
2482 case Primitive::kPrimChar: {
2483 __ movzxw(out, Address(cls, offset));
2484 break;
2485 }
2486
2487 case Primitive::kPrimInt:
2488 case Primitive::kPrimNot: {
2489 __ movl(out, Address(cls, offset));
2490 break;
2491 }
2492
2493 case Primitive::kPrimLong: {
2494 __ movq(out, Address(cls, offset));
2495 break;
2496 }
2497
2498 case Primitive::kPrimFloat:
2499 case Primitive::kPrimDouble:
2500 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
2501 UNREACHABLE();
2502 case Primitive::kPrimVoid:
2503 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2504 UNREACHABLE();
2505 }
2506}
2507
2508void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2509 LocationSummary* locations =
2510 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2511 Primitive::Type field_type = instruction->GetFieldType();
2512 bool is_object_type = field_type == Primitive::kPrimNot;
2513 locations->SetInAt(0, Location::RequiresRegister());
2514 locations->SetInAt(1, Location::RequiresRegister());
2515 if (is_object_type) {
2516 // Temporary registers for the write barrier.
2517 locations->AddTemp(Location::RequiresRegister());
2518 locations->AddTemp(Location::RequiresRegister());
2519 }
2520}
2521
2522void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2523 LocationSummary* locations = instruction->GetLocations();
2524 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
2525 CpuRegister value = locations->InAt(1).As<CpuRegister>();
2526 size_t offset = instruction->GetFieldOffset().SizeValue();
2527 Primitive::Type field_type = instruction->GetFieldType();
2528
2529 switch (field_type) {
2530 case Primitive::kPrimBoolean:
2531 case Primitive::kPrimByte: {
2532 __ movb(Address(cls, offset), value);
2533 break;
2534 }
2535
2536 case Primitive::kPrimShort:
2537 case Primitive::kPrimChar: {
2538 __ movw(Address(cls, offset), value);
2539 break;
2540 }
2541
2542 case Primitive::kPrimInt:
2543 case Primitive::kPrimNot: {
2544 __ movl(Address(cls, offset), value);
2545 if (field_type == Primitive::kPrimNot) {
2546 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
2547 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
2548 codegen_->MarkGCCard(temp, card, cls, value);
2549 }
2550 break;
2551 }
2552
2553 case Primitive::kPrimLong: {
2554 __ movq(Address(cls, offset), value);
2555 break;
2556 }
2557
2558 case Primitive::kPrimFloat:
2559 case Primitive::kPrimDouble:
2560 LOG(FATAL) << "Unimplemented register type " << field_type;
2561 UNREACHABLE();
2562 case Primitive::kPrimVoid:
2563 LOG(FATAL) << "Unreachable type " << field_type;
2564 UNREACHABLE();
2565 }
2566}
2567
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002568void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
2569 LocationSummary* locations =
2570 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2571 locations->SetOut(Location::RequiresRegister());
2572}
2573
2574void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
2575 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
2576 codegen_->AddSlowPath(slow_path);
2577
2578 CpuRegister out = load->GetLocations()->Out().As<CpuRegister>();
2579 codegen_->LoadCurrentMethod(CpuRegister(out));
2580 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
2581 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2582 __ testl(out, out);
2583 __ j(kEqual, slow_path->GetEntryLabel());
2584 __ Bind(slow_path->GetExitLabel());
2585}
2586
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002587} // namespace x86_64
2588} // namespace art