blob: 2bd76c12b4e270210484e552856a00d42d7d37c4 [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:
1105 LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
1106 break;
1107
1108 default:
1109 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1110 }
1111}
1112
1113void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1114 LocationSummary* locations = neg->GetLocations();
1115 Location out = locations->Out();
1116 Location in = locations->InAt(0);
1117 switch (neg->GetResultType()) {
1118 case Primitive::kPrimInt:
1119 DCHECK(in.IsRegister());
1120 __ negl(out.As<CpuRegister>());
1121 break;
1122
1123 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001124 DCHECK(in.IsRegister());
1125 __ negq(out.As<CpuRegister>());
1126 break;
1127
Roland Levillain88cb1752014-10-20 16:36:47 +01001128 case Primitive::kPrimFloat:
1129 case Primitive::kPrimDouble:
1130 LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
1131 break;
1132
1133 default:
1134 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1135 }
1136}
1137
Roland Levillaindff1f282014-11-05 14:15:05 +00001138void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1139 LocationSummary* locations =
1140 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1141 Primitive::Type result_type = conversion->GetResultType();
1142 Primitive::Type input_type = conversion->GetInputType();
1143 switch (result_type) {
1144 case Primitive::kPrimLong:
1145 switch (input_type) {
1146 case Primitive::kPrimByte:
1147 case Primitive::kPrimShort:
1148 case Primitive::kPrimInt:
1149 // int-to-long conversion.
1150 // TODO: We would benefit from a (to-be-implemented)
1151 // Location::RegisterOrStackSlot requirement for this input.
1152 locations->SetInAt(0, Location::RequiresRegister());
1153 locations->SetOut(Location::RequiresRegister());
1154 break;
1155
1156 case Primitive::kPrimFloat:
1157 case Primitive::kPrimDouble:
1158 LOG(FATAL) << "Type conversion from " << input_type << " to "
1159 << result_type << " not yet implemented";
1160 break;
1161
1162 default:
1163 LOG(FATAL) << "Unexpected type conversion from " << input_type
1164 << " to " << result_type;
1165 }
1166 break;
1167
1168 case Primitive::kPrimInt:
1169 case Primitive::kPrimFloat:
1170 case Primitive::kPrimDouble:
1171 LOG(FATAL) << "Type conversion from " << input_type
1172 << " to " << result_type << " not yet implemented";
1173 break;
1174
1175 default:
1176 LOG(FATAL) << "Unexpected type conversion from " << input_type
1177 << " to " << result_type;
1178 }
1179}
1180
1181void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1182 LocationSummary* locations = conversion->GetLocations();
1183 Location out = locations->Out();
1184 Location in = locations->InAt(0);
1185 Primitive::Type result_type = conversion->GetResultType();
1186 Primitive::Type input_type = conversion->GetInputType();
1187 switch (result_type) {
1188 case Primitive::kPrimLong:
1189 switch (input_type) {
1190 DCHECK(out.IsRegister());
1191 case Primitive::kPrimByte:
1192 case Primitive::kPrimShort:
1193 case Primitive::kPrimInt:
1194 // int-to-long conversion.
1195 DCHECK(in.IsRegister());
1196 __ movsxd(out.As<CpuRegister>(), in.As<CpuRegister>());
1197 break;
1198
1199 case Primitive::kPrimFloat:
1200 case Primitive::kPrimDouble:
1201 LOG(FATAL) << "Type conversion from " << input_type << " to "
1202 << result_type << " not yet implemented";
1203 break;
1204
1205 default:
1206 LOG(FATAL) << "Unexpected type conversion from " << input_type
1207 << " to " << result_type;
1208 }
1209 break;
1210
1211 case Primitive::kPrimInt:
1212 case Primitive::kPrimFloat:
1213 case Primitive::kPrimDouble:
1214 LOG(FATAL) << "Type conversion from " << input_type
1215 << " to " << result_type << " not yet implemented";
1216 break;
1217
1218 default:
1219 LOG(FATAL) << "Unexpected type conversion from " << input_type
1220 << " to " << result_type;
1221 }
1222}
1223
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001224void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001225 LocationSummary* locations =
1226 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001227 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001228 case Primitive::kPrimInt: {
1229 locations->SetInAt(0, Location::RequiresRegister());
1230 locations->SetInAt(1, Location::Any());
1231 locations->SetOut(Location::SameAsFirstInput());
1232 break;
1233 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001234
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001235 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001236 locations->SetInAt(0, Location::RequiresRegister());
1237 locations->SetInAt(1, Location::RequiresRegister());
1238 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001239 break;
1240 }
1241
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001242 case Primitive::kPrimDouble:
1243 case Primitive::kPrimFloat: {
1244 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001245 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001246 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001247 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001248 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001249
1250 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001251 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001252 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001253}
1254
1255void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1256 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001257 Location first = locations->InAt(0);
1258 Location second = locations->InAt(1);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001259 DCHECK(first.Equals(locations->Out()));
Calin Juravle11351682014-10-23 15:38:15 +01001260
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001261 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001262 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001263 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001264 __ addl(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001265 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001266 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001267 __ addl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001268 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001269 __ addl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001270 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001271 break;
1272 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001273
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001274 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001275 __ addq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001276 break;
1277 }
1278
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001279 case Primitive::kPrimFloat: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001280 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001281 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001282 }
1283
1284 case Primitive::kPrimDouble: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001285 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001286 break;
1287 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001288
1289 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001290 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001291 }
1292}
1293
1294void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001295 LocationSummary* locations =
1296 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001297 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001298 case Primitive::kPrimInt: {
1299 locations->SetInAt(0, Location::RequiresRegister());
1300 locations->SetInAt(1, Location::Any());
1301 locations->SetOut(Location::SameAsFirstInput());
1302 break;
1303 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001304 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001305 locations->SetInAt(0, Location::RequiresRegister());
1306 locations->SetInAt(1, Location::RequiresRegister());
1307 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001308 break;
1309 }
Calin Juravle11351682014-10-23 15:38:15 +01001310 case Primitive::kPrimFloat:
1311 case Primitive::kPrimDouble: {
1312 locations->SetInAt(0, Location::RequiresFpuRegister());
1313 locations->SetInAt(1, Location::RequiresFpuRegister());
1314 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001315 break;
Calin Juravle11351682014-10-23 15:38:15 +01001316 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001317 default:
Calin Juravle11351682014-10-23 15:38:15 +01001318 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001319 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001320}
1321
1322void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1323 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001324 Location first = locations->InAt(0);
1325 Location second = locations->InAt(1);
1326 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001327 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001328 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001329 if (second.IsRegister()) {
1330 __ subl(first.As<CpuRegister>(), second.As<CpuRegister>());
1331 } else if (second.IsConstant()) {
1332 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1333 __ subl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001334 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001335 __ subl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001336 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001337 break;
1338 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001339 case Primitive::kPrimLong: {
Calin Juravle11351682014-10-23 15:38:15 +01001340 __ subq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001341 break;
1342 }
1343
Calin Juravle11351682014-10-23 15:38:15 +01001344 case Primitive::kPrimFloat: {
1345 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001346 break;
Calin Juravle11351682014-10-23 15:38:15 +01001347 }
1348
1349 case Primitive::kPrimDouble: {
1350 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1351 break;
1352 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001353
1354 default:
Calin Juravle11351682014-10-23 15:38:15 +01001355 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001356 }
1357}
1358
Calin Juravle34bacdf2014-10-07 20:23:36 +01001359void LocationsBuilderX86_64::VisitMul(HMul* mul) {
1360 LocationSummary* locations =
1361 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1362 switch (mul->GetResultType()) {
1363 case Primitive::kPrimInt: {
1364 locations->SetInAt(0, Location::RequiresRegister());
1365 locations->SetInAt(1, Location::Any());
1366 locations->SetOut(Location::SameAsFirstInput());
1367 break;
1368 }
1369 case Primitive::kPrimLong: {
1370 locations->SetInAt(0, Location::RequiresRegister());
1371 locations->SetInAt(1, Location::RequiresRegister());
1372 locations->SetOut(Location::SameAsFirstInput());
1373 break;
1374 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001375 case Primitive::kPrimFloat:
1376 case Primitive::kPrimDouble: {
1377 locations->SetInAt(0, Location::RequiresFpuRegister());
1378 locations->SetInAt(1, Location::RequiresFpuRegister());
1379 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001380 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001381 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001382
1383 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001384 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001385 }
1386}
1387
1388void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
1389 LocationSummary* locations = mul->GetLocations();
1390 Location first = locations->InAt(0);
1391 Location second = locations->InAt(1);
1392 DCHECK(first.Equals(locations->Out()));
1393 switch (mul->GetResultType()) {
1394 case Primitive::kPrimInt: {
1395 if (second.IsRegister()) {
1396 __ imull(first.As<CpuRegister>(), second.As<CpuRegister>());
1397 } else if (second.IsConstant()) {
1398 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1399 __ imull(first.As<CpuRegister>(), imm);
1400 } else {
1401 DCHECK(second.IsStackSlot());
1402 __ imull(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
1403 }
1404 break;
1405 }
1406 case Primitive::kPrimLong: {
1407 __ imulq(first.As<CpuRegister>(), second.As<CpuRegister>());
1408 break;
1409 }
1410
Calin Juravleb5bfa962014-10-21 18:02:24 +01001411 case Primitive::kPrimFloat: {
1412 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001413 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001414 }
1415
1416 case Primitive::kPrimDouble: {
1417 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1418 break;
1419 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001420
1421 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001422 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001423 }
1424}
1425
Calin Juravle7c4954d2014-10-28 16:57:40 +00001426void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
1427 LocationSummary* locations =
1428 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1429 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001430 case Primitive::kPrimInt: {
1431 locations->SetInAt(0, Location::RegisterLocation(RAX));
1432 locations->SetInAt(1, Location::RequiresRegister());
1433 locations->SetOut(Location::SameAsFirstInput());
1434 // Intel uses edx:eax as the dividend.
1435 locations->AddTemp(Location::RegisterLocation(RDX));
1436 break;
1437 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001438 case Primitive::kPrimLong: {
1439 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1440 break;
1441 }
1442 case Primitive::kPrimFloat:
1443 case Primitive::kPrimDouble: {
1444 locations->SetInAt(0, Location::RequiresFpuRegister());
1445 locations->SetInAt(1, Location::RequiresFpuRegister());
1446 locations->SetOut(Location::SameAsFirstInput());
1447 break;
1448 }
1449
1450 default:
1451 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1452 }
1453}
1454
1455void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
1456 LocationSummary* locations = div->GetLocations();
1457 Location first = locations->InAt(0);
1458 Location second = locations->InAt(1);
1459 DCHECK(first.Equals(locations->Out()));
1460
1461 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001462 case Primitive::kPrimInt: {
1463 CpuRegister first_reg = first.As<CpuRegister>();
1464 CpuRegister second_reg = second.As<CpuRegister>();
1465 DCHECK_EQ(RAX, first_reg.AsRegister());
1466 DCHECK_EQ(RDX, locations->GetTemp(0).As<CpuRegister>().AsRegister());
1467
1468 SlowPathCodeX86_64* slow_path =
1469 new (GetGraph()->GetArena()) DivMinusOneSlowPathX86_64(first_reg.AsRegister());
1470 codegen_->AddSlowPath(slow_path);
1471
1472 // 0x80000000/-1 triggers an arithmetic exception!
1473 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
1474 // it's safe to just use negl instead of more complex comparisons.
1475
1476 __ cmpl(second_reg, Immediate(-1));
1477 __ j(kEqual, slow_path->GetEntryLabel());
1478
1479 // edx:eax <- sign-extended of eax
1480 __ cdq();
1481 // eax = quotient, edx = remainder
1482 __ idivl(second_reg);
1483
1484 __ Bind(slow_path->GetExitLabel());
1485 break;
1486 }
1487
Calin Juravle7c4954d2014-10-28 16:57:40 +00001488 case Primitive::kPrimLong: {
1489 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1490 break;
1491 }
1492
1493 case Primitive::kPrimFloat: {
1494 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1495 break;
1496 }
1497
1498 case Primitive::kPrimDouble: {
1499 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1500 break;
1501 }
1502
1503 default:
1504 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1505 }
1506}
1507
Calin Juravled0d48522014-11-04 16:40:20 +00001508void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1509 LocationSummary* locations =
1510 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1511 locations->SetInAt(0, Location::Any());
1512 if (instruction->HasUses()) {
1513 locations->SetOut(Location::SameAsFirstInput());
1514 }
1515}
1516
1517void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1518 SlowPathCodeX86_64* slow_path =
1519 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
1520 codegen_->AddSlowPath(slow_path);
1521
1522 LocationSummary* locations = instruction->GetLocations();
1523 Location value = locations->InAt(0);
1524
1525 if (value.IsRegister()) {
1526 __ testl(value.As<CpuRegister>(), value.As<CpuRegister>());
1527 } else if (value.IsStackSlot()) {
1528 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
1529 } else {
1530 DCHECK(value.IsConstant()) << value;
1531 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
1532 __ jmp(slow_path->GetEntryLabel());
1533 }
1534 return;
1535 }
1536 __ j(kEqual, slow_path->GetEntryLabel());
1537}
1538
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001539void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001540 LocationSummary* locations =
1541 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001542 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001543 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1544 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1545 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001546}
1547
1548void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
1549 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001550 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001551 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1552
1553 __ gs()->call(Address::Absolute(
1554 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
1555
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001556 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001557 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001558}
1559
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001560void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
1561 LocationSummary* locations =
1562 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1563 InvokeRuntimeCallingConvention calling_convention;
1564 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1565 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1566 locations->SetOut(Location::RegisterLocation(RAX));
1567 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1568}
1569
1570void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
1571 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001572 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001573 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1574
1575 __ gs()->call(Address::Absolute(
1576 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocArrayWithAccessCheck), true));
1577
1578 DCHECK(!codegen_->IsLeafMethod());
1579 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1580}
1581
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001582void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001583 LocationSummary* locations =
1584 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001585 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1586 if (location.IsStackSlot()) {
1587 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1588 } else if (location.IsDoubleStackSlot()) {
1589 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1590 }
1591 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001592}
1593
1594void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
1595 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001596 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001597}
1598
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001599void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001600 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001601 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001602 locations->SetInAt(0, Location::RequiresRegister());
1603 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001604}
1605
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001606void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
1607 LocationSummary* locations = not_->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001608 DCHECK_EQ(locations->InAt(0).As<CpuRegister>().AsRegister(),
1609 locations->Out().As<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001610 Location out = locations->Out();
1611 switch (not_->InputAt(0)->GetType()) {
1612 case Primitive::kPrimBoolean:
1613 __ xorq(out.As<CpuRegister>(), Immediate(1));
1614 break;
1615
1616 case Primitive::kPrimInt:
1617 __ notl(out.As<CpuRegister>());
1618 break;
1619
1620 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001621 __ notq(out.As<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001622 break;
1623
1624 default:
1625 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1626 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001627}
1628
1629void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001630 LocationSummary* locations =
1631 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001632 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1633 locations->SetInAt(i, Location::Any());
1634 }
1635 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001636}
1637
1638void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001639 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001640 LOG(FATAL) << "Unimplemented";
1641}
1642
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001643void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001644 LocationSummary* locations =
1645 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001646 Primitive::Type field_type = instruction->GetFieldType();
1647 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001648 locations->SetInAt(0, Location::RequiresRegister());
1649 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001650 if (is_object_type) {
1651 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001652 locations->AddTemp(Location::RequiresRegister());
1653 locations->AddTemp(Location::RequiresRegister());
1654 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001655}
1656
1657void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1658 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001659 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
1660 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001661 size_t offset = instruction->GetFieldOffset().SizeValue();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001662 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001663
1664 switch (field_type) {
1665 case Primitive::kPrimBoolean:
1666 case Primitive::kPrimByte: {
1667 __ movb(Address(obj, offset), value);
1668 break;
1669 }
1670
1671 case Primitive::kPrimShort:
1672 case Primitive::kPrimChar: {
1673 __ movw(Address(obj, offset), value);
1674 break;
1675 }
1676
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001677 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001678 case Primitive::kPrimNot: {
1679 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001680 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001681 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
1682 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001683 codegen_->MarkGCCard(temp, card, obj, value);
1684 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001685 break;
1686 }
1687
1688 case Primitive::kPrimLong: {
1689 __ movq(Address(obj, offset), value);
1690 break;
1691 }
1692
1693 case Primitive::kPrimFloat:
1694 case Primitive::kPrimDouble:
1695 LOG(FATAL) << "Unimplemented register type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001696 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001697 case Primitive::kPrimVoid:
1698 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001699 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001700 }
1701}
1702
1703void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001704 LocationSummary* locations =
1705 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001706 locations->SetInAt(0, Location::RequiresRegister());
1707 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001708}
1709
1710void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1711 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001712 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
1713 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001714 size_t offset = instruction->GetFieldOffset().SizeValue();
1715
1716 switch (instruction->GetType()) {
1717 case Primitive::kPrimBoolean: {
1718 __ movzxb(out, Address(obj, offset));
1719 break;
1720 }
1721
1722 case Primitive::kPrimByte: {
1723 __ movsxb(out, Address(obj, offset));
1724 break;
1725 }
1726
1727 case Primitive::kPrimShort: {
1728 __ movsxw(out, Address(obj, offset));
1729 break;
1730 }
1731
1732 case Primitive::kPrimChar: {
1733 __ movzxw(out, Address(obj, offset));
1734 break;
1735 }
1736
1737 case Primitive::kPrimInt:
1738 case Primitive::kPrimNot: {
1739 __ movl(out, Address(obj, offset));
1740 break;
1741 }
1742
1743 case Primitive::kPrimLong: {
1744 __ movq(out, Address(obj, offset));
1745 break;
1746 }
1747
1748 case Primitive::kPrimFloat:
1749 case Primitive::kPrimDouble:
1750 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001751 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001752 case Primitive::kPrimVoid:
1753 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001754 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001755 }
1756}
1757
1758void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001759 LocationSummary* locations =
1760 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001761 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001762 if (instruction->HasUses()) {
1763 locations->SetOut(Location::SameAsFirstInput());
1764 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001765}
1766
1767void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001768 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001769 codegen_->AddSlowPath(slow_path);
1770
1771 LocationSummary* locations = instruction->GetLocations();
1772 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001773
1774 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001775 __ cmpl(obj.As<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001776 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001777 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001778 } else {
1779 DCHECK(obj.IsConstant()) << obj;
1780 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1781 __ jmp(slow_path->GetEntryLabel());
1782 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001783 }
1784 __ j(kEqual, slow_path->GetEntryLabel());
1785}
1786
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001787void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001788 LocationSummary* locations =
1789 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001790 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001791 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001792 1, Location::RegisterOrConstant(instruction->InputAt(1)));
1793 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001794}
1795
1796void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
1797 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001798 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001799 Location index = locations->InAt(1);
1800
1801 switch (instruction->GetType()) {
1802 case Primitive::kPrimBoolean: {
1803 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001804 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001805 if (index.IsConstant()) {
1806 __ movzxb(out, Address(obj,
1807 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1808 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001809 __ movzxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001810 }
1811 break;
1812 }
1813
1814 case Primitive::kPrimByte: {
1815 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001816 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001817 if (index.IsConstant()) {
1818 __ movsxb(out, Address(obj,
1819 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1820 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001821 __ movsxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001822 }
1823 break;
1824 }
1825
1826 case Primitive::kPrimShort: {
1827 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001828 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001829 if (index.IsConstant()) {
1830 __ movsxw(out, Address(obj,
1831 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1832 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001833 __ movsxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001834 }
1835 break;
1836 }
1837
1838 case Primitive::kPrimChar: {
1839 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001840 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001841 if (index.IsConstant()) {
1842 __ movzxw(out, Address(obj,
1843 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1844 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001845 __ movzxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001846 }
1847 break;
1848 }
1849
1850 case Primitive::kPrimInt:
1851 case Primitive::kPrimNot: {
1852 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1853 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001854 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001855 if (index.IsConstant()) {
1856 __ movl(out, Address(obj,
1857 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1858 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001859 __ movl(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001860 }
1861 break;
1862 }
1863
1864 case Primitive::kPrimLong: {
1865 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001866 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001867 if (index.IsConstant()) {
1868 __ movq(out, Address(obj,
1869 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
1870 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001871 __ movq(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001872 }
1873 break;
1874 }
1875
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001876 case Primitive::kPrimFloat: {
1877 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1878 XmmRegister out = locations->Out().As<XmmRegister>();
1879 if (index.IsConstant()) {
1880 __ movss(out, Address(obj,
1881 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1882 } else {
1883 __ movss(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
1884 }
1885 break;
1886 }
1887
1888 case Primitive::kPrimDouble: {
1889 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1890 XmmRegister out = locations->Out().As<XmmRegister>();
1891 if (index.IsConstant()) {
1892 __ movsd(out, Address(obj,
1893 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
1894 } else {
1895 __ movsd(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
1896 }
1897 break;
1898 }
1899
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001900 case Primitive::kPrimVoid:
1901 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001902 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001903 }
1904}
1905
1906void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001907 Primitive::Type value_type = instruction->GetComponentType();
1908 bool is_object = value_type == Primitive::kPrimNot;
1909 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1910 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
1911 if (is_object) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001912 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001913 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1914 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1915 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001916 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001917 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001918 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001919 1, Location::RegisterOrConstant(instruction->InputAt(1)));
1920 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001921 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001922 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001923 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
1924 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001925 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001926 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001927 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001928 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001929}
1930
1931void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
1932 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001933 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001934 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001935 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001936 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001937
1938 switch (value_type) {
1939 case Primitive::kPrimBoolean:
1940 case Primitive::kPrimByte: {
1941 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001942 if (index.IsConstant()) {
1943 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001944 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001945 __ movb(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001946 } else {
1947 __ movb(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1948 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001949 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001950 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001951 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
1952 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001953 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001954 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001955 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1956 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001957 }
1958 break;
1959 }
1960
1961 case Primitive::kPrimShort:
1962 case Primitive::kPrimChar: {
1963 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001964 if (index.IsConstant()) {
1965 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001966 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001967 __ movw(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001968 } else {
1969 __ movw(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1970 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001971 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001972 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001973 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
1974 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001975 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001976 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001977 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1978 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001979 }
1980 break;
1981 }
1982
1983 case Primitive::kPrimInt: {
1984 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001985 if (index.IsConstant()) {
1986 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001987 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001988 __ movl(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001989 } else {
1990 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1991 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001992 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001993 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001994 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
1995 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001996 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001997 DCHECK(value.IsConstant()) << value;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001998 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001999 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2000 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002001 }
2002 break;
2003 }
2004
2005 case Primitive::kPrimNot: {
2006 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject), true));
2007 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002008 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002009 break;
2010 }
2011
2012 case Primitive::kPrimLong: {
2013 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002014 if (index.IsConstant()) {
2015 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002016 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002017 __ movq(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002018 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002019 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002020 __ movq(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
2021 value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002022 }
2023 break;
2024 }
2025
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002026 case Primitive::kPrimFloat: {
2027 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2028 if (index.IsConstant()) {
2029 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2030 DCHECK(value.IsFpuRegister());
2031 __ movss(Address(obj, offset), value.As<XmmRegister>());
2032 } else {
2033 DCHECK(value.IsFpuRegister());
2034 __ movss(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2035 value.As<XmmRegister>());
2036 }
2037 break;
2038 }
2039
2040 case Primitive::kPrimDouble: {
2041 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2042 if (index.IsConstant()) {
2043 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
2044 DCHECK(value.IsFpuRegister());
2045 __ movsd(Address(obj, offset), value.As<XmmRegister>());
2046 } else {
2047 DCHECK(value.IsFpuRegister());
2048 __ movsd(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
2049 value.As<XmmRegister>());
2050 }
2051 break;
2052 }
2053
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002054 case Primitive::kPrimVoid:
2055 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002056 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002057 }
2058}
2059
2060void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002061 LocationSummary* locations =
2062 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002063 locations->SetInAt(0, Location::RequiresRegister());
2064 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002065}
2066
2067void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
2068 LocationSummary* locations = instruction->GetLocations();
2069 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002070 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2071 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002072 __ movl(out, Address(obj, offset));
2073}
2074
2075void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002076 LocationSummary* locations =
2077 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002078 locations->SetInAt(0, Location::RequiresRegister());
2079 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002080 if (instruction->HasUses()) {
2081 locations->SetOut(Location::SameAsFirstInput());
2082 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002083}
2084
2085void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
2086 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002087 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002088 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002089 codegen_->AddSlowPath(slow_path);
2090
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002091 CpuRegister index = locations->InAt(0).As<CpuRegister>();
2092 CpuRegister length = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002093
2094 __ cmpl(index, length);
2095 __ j(kAboveEqual, slow_path->GetEntryLabel());
2096}
2097
2098void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
2099 CpuRegister card,
2100 CpuRegister object,
2101 CpuRegister value) {
2102 Label is_null;
2103 __ testl(value, value);
2104 __ j(kEqual, &is_null);
2105 __ gs()->movq(card, Address::Absolute(
2106 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
2107 __ movq(temp, object);
2108 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
2109 __ movb(Address(temp, card, TIMES_1, 0), card);
2110 __ Bind(&is_null);
2111}
2112
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002113void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
2114 temp->SetLocations(nullptr);
2115}
2116
2117void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
2118 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002119 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002120}
2121
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002122void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002123 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002124 LOG(FATAL) << "Unimplemented";
2125}
2126
2127void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002128 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2129}
2130
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002131void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
2132 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2133}
2134
2135void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002136 HBasicBlock* block = instruction->GetBlock();
2137 if (block->GetLoopInformation() != nullptr) {
2138 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2139 // The back edge will generate the suspend check.
2140 return;
2141 }
2142 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2143 // The goto will generate the suspend check.
2144 return;
2145 }
2146 GenerateSuspendCheck(instruction, nullptr);
2147}
2148
2149void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
2150 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002151 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002152 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002153 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002154 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002155 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002156 if (successor == nullptr) {
2157 __ j(kNotEqual, slow_path->GetEntryLabel());
2158 __ Bind(slow_path->GetReturnLabel());
2159 } else {
2160 __ j(kEqual, codegen_->GetLabelOf(successor));
2161 __ jmp(slow_path->GetEntryLabel());
2162 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002163}
2164
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002165X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
2166 return codegen_->GetAssembler();
2167}
2168
2169void ParallelMoveResolverX86_64::EmitMove(size_t index) {
2170 MoveOperands* move = moves_.Get(index);
2171 Location source = move->GetSource();
2172 Location destination = move->GetDestination();
2173
2174 if (source.IsRegister()) {
2175 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002176 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002177 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002178 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002179 source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002180 } else {
2181 DCHECK(destination.IsDoubleStackSlot());
2182 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002183 source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002184 }
2185 } else if (source.IsStackSlot()) {
2186 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002187 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002188 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002189 } else if (destination.IsFpuRegister()) {
2190 __ movss(destination.As<XmmRegister>(),
2191 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002192 } else {
2193 DCHECK(destination.IsStackSlot());
2194 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2195 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2196 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002197 } else if (source.IsDoubleStackSlot()) {
2198 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002199 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002200 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002201 } else if (destination.IsFpuRegister()) {
2202 __ movsd(destination.As<XmmRegister>(), Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002203 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01002204 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002205 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2206 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2207 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002208 } else if (source.IsConstant()) {
2209 HConstant* constant = source.GetConstant();
2210 if (constant->IsIntConstant()) {
2211 Immediate imm(constant->AsIntConstant()->GetValue());
2212 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002213 __ movl(destination.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002214 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002215 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002216 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2217 }
2218 } else if (constant->IsLongConstant()) {
2219 int64_t value = constant->AsLongConstant()->GetValue();
2220 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002221 __ movq(destination.As<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002222 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002223 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002224 __ movq(CpuRegister(TMP), Immediate(value));
2225 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2226 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002227 } else if (constant->IsFloatConstant()) {
2228 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
2229 if (destination.IsFpuRegister()) {
2230 __ movl(CpuRegister(TMP), imm);
2231 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2232 } else {
2233 DCHECK(destination.IsStackSlot()) << destination;
2234 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2235 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002236 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002237 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
2238 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
2239 if (destination.IsFpuRegister()) {
2240 __ movq(CpuRegister(TMP), imm);
2241 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2242 } else {
2243 DCHECK(destination.IsDoubleStackSlot()) << destination;
2244 __ movq(CpuRegister(TMP), imm);
2245 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2246 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002247 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002248 } else if (source.IsFpuRegister()) {
2249 if (destination.IsFpuRegister()) {
2250 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
2251 } else if (destination.IsStackSlot()) {
2252 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
2253 source.As<XmmRegister>());
2254 } else {
2255 DCHECK(destination.IsDoubleStackSlot());
2256 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
2257 source.As<XmmRegister>());
2258 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002259 }
2260}
2261
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002262void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002263 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002264 __ movl(Address(CpuRegister(RSP), mem), reg);
2265 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002266}
2267
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002268void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002269 ScratchRegisterScope ensure_scratch(
2270 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2271
2272 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2273 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2274 __ movl(CpuRegister(ensure_scratch.GetRegister()),
2275 Address(CpuRegister(RSP), mem2 + stack_offset));
2276 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2277 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
2278 CpuRegister(ensure_scratch.GetRegister()));
2279}
2280
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002281void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
2282 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2283 __ movq(Address(CpuRegister(RSP), mem), reg);
2284 __ movq(reg, CpuRegister(TMP));
2285}
2286
2287void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
2288 ScratchRegisterScope ensure_scratch(
2289 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2290
2291 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2292 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2293 __ movq(CpuRegister(ensure_scratch.GetRegister()),
2294 Address(CpuRegister(RSP), mem2 + stack_offset));
2295 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2296 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
2297 CpuRegister(ensure_scratch.GetRegister()));
2298}
2299
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002300void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
2301 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2302 __ movss(Address(CpuRegister(RSP), mem), reg);
2303 __ movd(reg, CpuRegister(TMP));
2304}
2305
2306void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
2307 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2308 __ movsd(Address(CpuRegister(RSP), mem), reg);
2309 __ movd(reg, CpuRegister(TMP));
2310}
2311
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002312void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
2313 MoveOperands* move = moves_.Get(index);
2314 Location source = move->GetSource();
2315 Location destination = move->GetDestination();
2316
2317 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002318 __ xchgq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002319 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002320 Exchange32(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002321 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002322 Exchange32(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002323 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002324 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
2325 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002326 Exchange64(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002327 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002328 Exchange64(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002329 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
2330 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002331 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
2332 __ movd(CpuRegister(TMP), source.As<XmmRegister>());
2333 __ movaps(source.As<XmmRegister>(), destination.As<XmmRegister>());
2334 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2335 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
2336 Exchange32(source.As<XmmRegister>(), destination.GetStackIndex());
2337 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
2338 Exchange32(destination.As<XmmRegister>(), source.GetStackIndex());
2339 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
2340 Exchange64(source.As<XmmRegister>(), destination.GetStackIndex());
2341 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
2342 Exchange64(destination.As<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002343 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002344 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002345 }
2346}
2347
2348
2349void ParallelMoveResolverX86_64::SpillScratch(int reg) {
2350 __ pushq(CpuRegister(reg));
2351}
2352
2353
2354void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
2355 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002356}
2357
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002358void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
2359 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
2360 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2361 Immediate(mirror::Class::kStatusInitialized));
2362 __ j(kLess, slow_path->GetEntryLabel());
2363 __ Bind(slow_path->GetExitLabel());
2364 // No need for memory fence, thanks to the X86_64 memory model.
2365}
2366
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002367void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002368 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2369 ? LocationSummary::kCallOnSlowPath
2370 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002371 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002372 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002373 locations->SetOut(Location::RequiresRegister());
2374}
2375
2376void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
2377 CpuRegister out = cls->GetLocations()->Out().As<CpuRegister>();
2378 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002379 DCHECK(!cls->CanCallRuntime());
2380 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002381 codegen_->LoadCurrentMethod(out);
2382 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2383 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002384 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002385 codegen_->LoadCurrentMethod(out);
2386 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2387 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002388 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
2389 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2390 codegen_->AddSlowPath(slow_path);
2391 __ testl(out, out);
2392 __ j(kEqual, slow_path->GetEntryLabel());
2393 if (cls->MustGenerateClinitCheck()) {
2394 GenerateClassInitializationCheck(slow_path, out);
2395 } else {
2396 __ Bind(slow_path->GetExitLabel());
2397 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002398 }
2399}
2400
2401void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
2402 LocationSummary* locations =
2403 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2404 locations->SetInAt(0, Location::RequiresRegister());
2405 if (check->HasUses()) {
2406 locations->SetOut(Location::SameAsFirstInput());
2407 }
2408}
2409
2410void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002411 // We assume the class to not be null.
2412 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
2413 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002414 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002415 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002416}
2417
2418void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2419 LocationSummary* locations =
2420 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2421 locations->SetInAt(0, Location::RequiresRegister());
2422 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2423}
2424
2425void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2426 LocationSummary* locations = instruction->GetLocations();
2427 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
2428 CpuRegister out = locations->Out().As<CpuRegister>();
2429 size_t offset = instruction->GetFieldOffset().SizeValue();
2430
2431 switch (instruction->GetType()) {
2432 case Primitive::kPrimBoolean: {
2433 __ movzxb(out, Address(cls, offset));
2434 break;
2435 }
2436
2437 case Primitive::kPrimByte: {
2438 __ movsxb(out, Address(cls, offset));
2439 break;
2440 }
2441
2442 case Primitive::kPrimShort: {
2443 __ movsxw(out, Address(cls, offset));
2444 break;
2445 }
2446
2447 case Primitive::kPrimChar: {
2448 __ movzxw(out, Address(cls, offset));
2449 break;
2450 }
2451
2452 case Primitive::kPrimInt:
2453 case Primitive::kPrimNot: {
2454 __ movl(out, Address(cls, offset));
2455 break;
2456 }
2457
2458 case Primitive::kPrimLong: {
2459 __ movq(out, Address(cls, offset));
2460 break;
2461 }
2462
2463 case Primitive::kPrimFloat:
2464 case Primitive::kPrimDouble:
2465 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
2466 UNREACHABLE();
2467 case Primitive::kPrimVoid:
2468 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2469 UNREACHABLE();
2470 }
2471}
2472
2473void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2474 LocationSummary* locations =
2475 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2476 Primitive::Type field_type = instruction->GetFieldType();
2477 bool is_object_type = field_type == Primitive::kPrimNot;
2478 locations->SetInAt(0, Location::RequiresRegister());
2479 locations->SetInAt(1, Location::RequiresRegister());
2480 if (is_object_type) {
2481 // Temporary registers for the write barrier.
2482 locations->AddTemp(Location::RequiresRegister());
2483 locations->AddTemp(Location::RequiresRegister());
2484 }
2485}
2486
2487void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2488 LocationSummary* locations = instruction->GetLocations();
2489 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
2490 CpuRegister value = locations->InAt(1).As<CpuRegister>();
2491 size_t offset = instruction->GetFieldOffset().SizeValue();
2492 Primitive::Type field_type = instruction->GetFieldType();
2493
2494 switch (field_type) {
2495 case Primitive::kPrimBoolean:
2496 case Primitive::kPrimByte: {
2497 __ movb(Address(cls, offset), value);
2498 break;
2499 }
2500
2501 case Primitive::kPrimShort:
2502 case Primitive::kPrimChar: {
2503 __ movw(Address(cls, offset), value);
2504 break;
2505 }
2506
2507 case Primitive::kPrimInt:
2508 case Primitive::kPrimNot: {
2509 __ movl(Address(cls, offset), value);
2510 if (field_type == Primitive::kPrimNot) {
2511 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
2512 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
2513 codegen_->MarkGCCard(temp, card, cls, value);
2514 }
2515 break;
2516 }
2517
2518 case Primitive::kPrimLong: {
2519 __ movq(Address(cls, offset), value);
2520 break;
2521 }
2522
2523 case Primitive::kPrimFloat:
2524 case Primitive::kPrimDouble:
2525 LOG(FATAL) << "Unimplemented register type " << field_type;
2526 UNREACHABLE();
2527 case Primitive::kPrimVoid:
2528 LOG(FATAL) << "Unreachable type " << field_type;
2529 UNREACHABLE();
2530 }
2531}
2532
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002533void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
2534 LocationSummary* locations =
2535 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2536 locations->SetOut(Location::RequiresRegister());
2537}
2538
2539void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
2540 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
2541 codegen_->AddSlowPath(slow_path);
2542
2543 CpuRegister out = load->GetLocations()->Out().As<CpuRegister>();
2544 codegen_->LoadCurrentMethod(CpuRegister(out));
2545 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
2546 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2547 __ testl(out, out);
2548 __ j(kEqual, slow_path->GetEntryLabel());
2549 __ Bind(slow_path->GetExitLabel());
2550}
2551
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002552} // namespace x86_64
2553} // namespace art