blob: 9237cc3a4df8db08d2681801a36b08aacd13b429 [file] [log] [blame]
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86_64.h"
18
19#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010020#include "gc/accounting/card_table.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070021#include "mirror/array-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010022#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010023#include "mirror/class.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010024#include "mirror/object_reference.h"
25#include "thread.h"
26#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010027#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010028#include "utils/x86_64/assembler_x86_64.h"
29#include "utils/x86_64/managed_register_x86_64.h"
30
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010031namespace art {
32
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010033namespace x86_64 {
34
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010035static constexpr bool kExplicitStackOverflowCheck = false;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010036
37// Some x86_64 instructions require a register to be available as temp.
38static constexpr Register TMP = R11;
39
40static constexpr int kNumberOfPushedRegistersAtEntry = 1;
41static constexpr int kCurrentMethodStackOffset = 0;
42
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010043static constexpr Register kRuntimeParameterCoreRegisters[] = { RDI, RSI, RDX };
44static constexpr size_t kRuntimeParameterCoreRegistersLength =
45 arraysize(kRuntimeParameterCoreRegisters);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010046static constexpr FloatRegister kRuntimeParameterFpuRegisters[] = { };
47static constexpr size_t kRuntimeParameterFpuRegistersLength = 0;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010048
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010049class InvokeRuntimeCallingConvention : public CallingConvention<Register, FloatRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010050 public:
51 InvokeRuntimeCallingConvention()
52 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010053 kRuntimeParameterCoreRegistersLength,
54 kRuntimeParameterFpuRegisters,
55 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010056
57 private:
58 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
59};
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010060
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061#define __ reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler())->
62
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010063class SlowPathCodeX86_64 : public SlowPathCode {
64 public:
65 SlowPathCodeX86_64() : entry_label_(), exit_label_() {}
66
67 Label* GetEntryLabel() { return &entry_label_; }
68 Label* GetExitLabel() { return &exit_label_; }
69
70 private:
71 Label entry_label_;
72 Label exit_label_;
73
74 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86_64);
75};
76
77class NullCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010078 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010079 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080
81 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
82 __ Bind(GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010083 __ gs()->call(
84 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowNullPointer), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +010085 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010086 }
87
88 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010089 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010090 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
91};
92
Calin Juravled0d48522014-11-04 16:40:20 +000093class DivZeroCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
94 public:
95 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : instruction_(instruction) {}
96
97 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
98 __ Bind(GetEntryLabel());
99 __ gs()->call(
100 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowDivZero), true));
101 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
102 }
103
104 private:
105 HDivZeroCheck* const instruction_;
106 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
107};
108
109class DivMinusOneSlowPathX86_64 : public SlowPathCodeX86_64 {
110 public:
111 explicit DivMinusOneSlowPathX86_64(Register reg) : reg_(reg) {}
112
113 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
114 __ Bind(GetEntryLabel());
115 __ negl(CpuRegister(reg_));
116 __ jmp(GetExitLabel());
117 }
118
119 private:
120 Register reg_;
121 DISALLOW_COPY_AND_ASSIGN(DivMinusOneSlowPathX86_64);
122};
123
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100124class StackOverflowCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100125 public:
126 StackOverflowCheckSlowPathX86_64() {}
127
128 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
129 __ Bind(GetEntryLabel());
130 __ addq(CpuRegister(RSP),
131 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
132 __ gs()->jmp(
133 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowStackOverflow), true));
134 }
135
136 private:
137 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86_64);
138};
139
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100140class SuspendCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000141 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100142 explicit SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
143 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000144
145 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100146 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000147 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100148 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000149 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pTestSuspend), true));
150 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100151 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100152 if (successor_ == nullptr) {
153 __ jmp(GetReturnLabel());
154 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100155 __ jmp(x64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100156 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000157 }
158
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100159 Label* GetReturnLabel() {
160 DCHECK(successor_ == nullptr);
161 return &return_label_;
162 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000163
164 private:
165 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100166 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000167 Label return_label_;
168
169 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
170};
171
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100172class BoundsCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100173 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100174 BoundsCheckSlowPathX86_64(HBoundsCheck* instruction,
175 Location index_location,
176 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100177 : instruction_(instruction),
178 index_location_(index_location),
179 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100180
181 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100182 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100183 __ Bind(GetEntryLabel());
184 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100185 x64_codegen->Move(
186 Location::RegisterLocation(calling_convention.GetRegisterAt(0)), index_location_);
187 x64_codegen->Move(
188 Location::RegisterLocation(calling_convention.GetRegisterAt(1)), length_location_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100189 __ gs()->call(Address::Absolute(
190 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowArrayBounds), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100191 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100192 }
193
194 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100195 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100196 const Location index_location_;
197 const Location length_location_;
198
199 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
200};
201
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000202class LoadClassSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100203 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000204 LoadClassSlowPathX86_64(HLoadClass* cls,
205 HInstruction* at,
206 uint32_t dex_pc,
207 bool do_clinit)
208 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
209 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
210 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100211
212 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000213 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100214 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
215 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100216
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000217 codegen->SaveLiveRegisters(locations);
218
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100219 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000220 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100221 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000222 __ gs()->call(Address::Absolute((do_clinit_
223 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeStaticStorage)
224 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeType)) , true));
225 codegen->RecordPcInfo(at_, dex_pc_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100226
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000227 // Move the class to the desired location.
228 if (locations->Out().IsValid()) {
229 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
230 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
231 }
232
233 codegen->RestoreLiveRegisters(locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100234 __ jmp(GetExitLabel());
235 }
236
237 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000238 // The class this slow path will load.
239 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100240
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000241 // The instruction where this slow path is happening.
242 // (Might be the load class or an initialization check).
243 HInstruction* const at_;
244
245 // The dex PC of `at_`.
246 const uint32_t dex_pc_;
247
248 // Whether to initialize the class.
249 const bool do_clinit_;
250
251 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100252};
253
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000254class LoadStringSlowPathX86_64 : public SlowPathCodeX86_64 {
255 public:
256 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
257
258 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
259 LocationSummary* locations = instruction_->GetLocations();
260 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
261
262 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
263 __ Bind(GetEntryLabel());
264 codegen->SaveLiveRegisters(locations);
265
266 InvokeRuntimeCallingConvention calling_convention;
267 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(0)));
268 __ movl(CpuRegister(calling_convention.GetRegisterAt(1)),
269 Immediate(instruction_->GetStringIndex()));
270 __ gs()->call(Address::Absolute(
271 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pResolveString), true));
272 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
273 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
274 codegen->RestoreLiveRegisters(locations);
275 __ jmp(GetExitLabel());
276 }
277
278 private:
279 HLoadString* const instruction_;
280
281 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
282};
283
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100284#undef __
285#define __ reinterpret_cast<X86_64Assembler*>(GetAssembler())->
286
Dave Allison20dfc792014-06-16 20:44:29 -0700287inline Condition X86_64Condition(IfCondition cond) {
288 switch (cond) {
289 case kCondEQ: return kEqual;
290 case kCondNE: return kNotEqual;
291 case kCondLT: return kLess;
292 case kCondLE: return kLessEqual;
293 case kCondGT: return kGreater;
294 case kCondGE: return kGreaterEqual;
295 default:
296 LOG(FATAL) << "Unknown if condition";
297 }
298 return kEqual;
299}
300
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100301void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
302 stream << X86_64ManagedRegister::FromCpuRegister(Register(reg));
303}
304
305void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
306 stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg));
307}
308
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100309size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
310 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
311 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100312}
313
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100314size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
315 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
316 return kX86_64WordSize;
317}
318
319size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
320 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
321 return kX86_64WordSize;
322}
323
324size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
325 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
326 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100327}
328
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100329CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100330 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfFloatRegisters, 0),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100331 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100332 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000333 instruction_visitor_(graph, this),
334 move_resolver_(graph->GetArena(), this) {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100335
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100336size_t CodeGeneratorX86_64::FrameEntrySpillSize() const {
337 return kNumberOfPushedRegistersAtEntry * kX86_64WordSize;
338}
339
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100340InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
341 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100342 : HGraphVisitor(graph),
343 assembler_(codegen->GetAssembler()),
344 codegen_(codegen) {}
345
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100346Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100347 switch (type) {
348 case Primitive::kPrimLong:
349 case Primitive::kPrimByte:
350 case Primitive::kPrimBoolean:
351 case Primitive::kPrimChar:
352 case Primitive::kPrimShort:
353 case Primitive::kPrimInt:
354 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100355 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100356 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100357 }
358
359 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100360 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100361 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100362 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100363 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100364
365 case Primitive::kPrimVoid:
366 LOG(FATAL) << "Unreachable type " << type;
367 }
368
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100369 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100370}
371
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100372void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100373 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100374 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100375
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000376 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100377 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000378
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100379 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100380 blocked_core_registers_[RBX] = true;
381 blocked_core_registers_[RBP] = true;
382 blocked_core_registers_[R12] = true;
383 blocked_core_registers_[R13] = true;
384 blocked_core_registers_[R14] = true;
385 blocked_core_registers_[R15] = true;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100386
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100387 blocked_fpu_registers_[XMM12] = true;
388 blocked_fpu_registers_[XMM13] = true;
389 blocked_fpu_registers_[XMM14] = true;
390 blocked_fpu_registers_[XMM15] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100391}
392
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100393void CodeGeneratorX86_64::GenerateFrameEntry() {
394 // Create a fake register to mimic Quick.
395 static const int kFakeReturnRegister = 16;
396 core_spill_mask_ |= (1 << kFakeReturnRegister);
397
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100398 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700399 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100400
401 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
402 __ testq(CpuRegister(RAX), Address(
403 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100404 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100405 }
406
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100407 // The return PC has already been pushed on the stack.
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100408 __ subq(CpuRegister(RSP),
409 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
410
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100411 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100412 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86_64();
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100413 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100414
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100415 __ gs()->cmpq(CpuRegister(RSP),
416 Address::Absolute(Thread::StackEndOffset<kX86_64WordSize>(), true));
417 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100418 }
419
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100420 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
421}
422
423void CodeGeneratorX86_64::GenerateFrameExit() {
424 __ addq(CpuRegister(RSP),
425 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
426}
427
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100428void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
429 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100430}
431
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100432void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100433 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
434}
435
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100436Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
437 switch (load->GetType()) {
438 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100439 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100440 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
441 break;
442
443 case Primitive::kPrimInt:
444 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100445 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100446 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100447
448 case Primitive::kPrimBoolean:
449 case Primitive::kPrimByte:
450 case Primitive::kPrimChar:
451 case Primitive::kPrimShort:
452 case Primitive::kPrimVoid:
453 LOG(FATAL) << "Unexpected type " << load->GetType();
454 }
455
456 LOG(FATAL) << "Unreachable";
457 return Location();
458}
459
460void CodeGeneratorX86_64::Move(Location destination, Location source) {
461 if (source.Equals(destination)) {
462 return;
463 }
464 if (destination.IsRegister()) {
465 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100466 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100467 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100468 __ movd(destination.As<CpuRegister>(), source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100469 } else if (source.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100470 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100471 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100472 } else {
473 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100474 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100475 Address(CpuRegister(RSP), source.GetStackIndex()));
476 }
477 } else if (destination.IsFpuRegister()) {
478 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100479 __ movd(destination.As<XmmRegister>(), source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100480 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100481 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100482 } else if (source.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100483 __ movss(destination.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100484 Address(CpuRegister(RSP), source.GetStackIndex()));
485 } else {
486 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100487 __ movsd(destination.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100488 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100489 }
490 } else if (destination.IsStackSlot()) {
491 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100492 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100493 source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100494 } else if (source.IsFpuRegister()) {
495 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100496 source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100497 } else {
498 DCHECK(source.IsStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000499 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
500 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100501 }
502 } else {
503 DCHECK(destination.IsDoubleStackSlot());
504 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100505 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100506 source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100507 } else if (source.IsFpuRegister()) {
508 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100509 source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100510 } else {
511 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000512 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
513 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100514 }
515 }
516}
517
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100518void CodeGeneratorX86_64::Move(HInstruction* instruction,
519 Location location,
520 HInstruction* move_for) {
Roland Levillain476df552014-10-09 17:51:36 +0100521 if (instruction->IsIntConstant()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100522 Immediate imm(instruction->AsIntConstant()->GetValue());
523 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100524 __ movl(location.As<CpuRegister>(), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100525 } else if (location.IsStackSlot()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100526 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100527 } else {
528 DCHECK(location.IsConstant());
529 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100530 }
Roland Levillain476df552014-10-09 17:51:36 +0100531 } else if (instruction->IsLongConstant()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100532 int64_t value = instruction->AsLongConstant()->GetValue();
533 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100534 __ movq(location.As<CpuRegister>(), Immediate(value));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100535 } else if (location.IsDoubleStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000536 __ movq(CpuRegister(TMP), Immediate(value));
537 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100538 } else {
539 DCHECK(location.IsConstant());
540 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100541 }
Roland Levillain476df552014-10-09 17:51:36 +0100542 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100543 switch (instruction->GetType()) {
544 case Primitive::kPrimBoolean:
545 case Primitive::kPrimByte:
546 case Primitive::kPrimChar:
547 case Primitive::kPrimShort:
548 case Primitive::kPrimInt:
549 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100550 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100551 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
552 break;
553
554 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100555 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100556 Move(location, Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
557 break;
558
559 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100560 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100561 }
562 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100563 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100564 switch (instruction->GetType()) {
565 case Primitive::kPrimBoolean:
566 case Primitive::kPrimByte:
567 case Primitive::kPrimChar:
568 case Primitive::kPrimShort:
569 case Primitive::kPrimInt:
570 case Primitive::kPrimNot:
571 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100572 case Primitive::kPrimFloat:
573 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100574 Move(location, instruction->GetLocations()->Out());
575 break;
576
577 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100578 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100579 }
580 }
581}
582
583void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
584 got->SetLocations(nullptr);
585}
586
587void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
588 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100589 DCHECK(!successor->IsExitBlock());
590
591 HBasicBlock* block = got->GetBlock();
592 HInstruction* previous = got->GetPrevious();
593
594 HLoopInformation* info = block->GetLoopInformation();
595 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
596 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
597 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
598 return;
599 }
600
601 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
602 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
603 }
604 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100605 __ jmp(codegen_->GetLabelOf(successor));
606 }
607}
608
609void LocationsBuilderX86_64::VisitExit(HExit* exit) {
610 exit->SetLocations(nullptr);
611}
612
613void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700614 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100615 if (kIsDebugBuild) {
616 __ Comment("Unreachable");
617 __ int3();
618 }
619}
620
621void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100622 LocationSummary* locations =
623 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100624 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100625 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100626 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100627 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100628}
629
630void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700631 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100632 if (cond->IsIntConstant()) {
633 // Constant condition, statically compared against 1.
634 int32_t cond_value = cond->AsIntConstant()->GetValue();
635 if (cond_value == 1) {
636 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
637 if_instr->IfTrueSuccessor())) {
638 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100639 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100640 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100641 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100642 DCHECK_EQ(cond_value, 0);
643 }
644 } else {
645 bool materialized =
646 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
647 // Moves do not affect the eflags register, so if the condition is
648 // evaluated just before the if, we don't need to evaluate it
649 // again.
650 bool eflags_set = cond->IsCondition()
651 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
652 if (materialized) {
653 if (!eflags_set) {
654 // Materialized condition, compare against 0.
655 Location lhs = if_instr->GetLocations()->InAt(0);
656 if (lhs.IsRegister()) {
657 __ cmpl(lhs.As<CpuRegister>(), Immediate(0));
658 } else {
659 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
660 Immediate(0));
661 }
662 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
663 } else {
664 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
665 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
666 }
667 } else {
668 Location lhs = cond->GetLocations()->InAt(0);
669 Location rhs = cond->GetLocations()->InAt(1);
670 if (rhs.IsRegister()) {
671 __ cmpl(lhs.As<CpuRegister>(), rhs.As<CpuRegister>());
672 } else if (rhs.IsConstant()) {
673 __ cmpl(lhs.As<CpuRegister>(),
674 Immediate(rhs.GetConstant()->AsIntConstant()->GetValue()));
675 } else {
676 __ cmpl(lhs.As<CpuRegister>(),
677 Address(CpuRegister(RSP), rhs.GetStackIndex()));
678 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100679 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
680 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700681 }
Dave Allison20dfc792014-06-16 20:44:29 -0700682 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100683 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
684 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700685 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100686 }
687}
688
689void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
690 local->SetLocations(nullptr);
691}
692
693void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
694 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
695}
696
697void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
698 local->SetLocations(nullptr);
699}
700
701void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
702 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700703 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100704}
705
706void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100707 LocationSummary* locations =
708 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100709 switch (store->InputAt(1)->GetType()) {
710 case Primitive::kPrimBoolean:
711 case Primitive::kPrimByte:
712 case Primitive::kPrimChar:
713 case Primitive::kPrimShort:
714 case Primitive::kPrimInt:
715 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100716 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100717 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
718 break;
719
720 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100721 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100722 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
723 break;
724
725 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100726 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100727 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100728}
729
730void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700731 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100732}
733
Dave Allison20dfc792014-06-16 20:44:29 -0700734void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100735 LocationSummary* locations =
736 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100737 locations->SetInAt(0, Location::RequiresRegister());
738 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100739 if (comp->NeedsMaterialization()) {
740 locations->SetOut(Location::RequiresRegister());
741 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100742}
743
Dave Allison20dfc792014-06-16 20:44:29 -0700744void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
745 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100746 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100747 CpuRegister reg = locations->Out().As<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100748 // Clear register: setcc only sets the low byte.
749 __ xorq(reg, reg);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100750 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100751 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100752 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100753 } else if (locations->InAt(1).IsConstant()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100754 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100755 Immediate(locations->InAt(1).GetConstant()->AsIntConstant()->GetValue()));
756 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100757 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100758 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
759 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100760 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700761 }
762}
763
764void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
765 VisitCondition(comp);
766}
767
768void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
769 VisitCondition(comp);
770}
771
772void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
773 VisitCondition(comp);
774}
775
776void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
777 VisitCondition(comp);
778}
779
780void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
781 VisitCondition(comp);
782}
783
784void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
785 VisitCondition(comp);
786}
787
788void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
789 VisitCondition(comp);
790}
791
792void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
793 VisitCondition(comp);
794}
795
796void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
797 VisitCondition(comp);
798}
799
800void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
801 VisitCondition(comp);
802}
803
804void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
805 VisitCondition(comp);
806}
807
808void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
809 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100810}
811
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100812void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100813 LocationSummary* locations =
814 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100815 locations->SetInAt(0, Location::RequiresRegister());
816 locations->SetInAt(1, Location::RequiresRegister());
817 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100818}
819
820void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
821 Label greater, done;
822 LocationSummary* locations = compare->GetLocations();
823 switch (compare->InputAt(0)->GetType()) {
824 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100825 __ cmpq(locations->InAt(0).As<CpuRegister>(),
826 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100827 break;
828 default:
829 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
830 }
831
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100832 CpuRegister output = locations->Out().As<CpuRegister>();
833 __ movl(output, Immediate(0));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100834 __ j(kEqual, &done);
835 __ j(kGreater, &greater);
836
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100837 __ movl(output, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100838 __ jmp(&done);
839
840 __ Bind(&greater);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100841 __ movl(output, Immediate(1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100842
843 __ Bind(&done);
844}
845
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100846void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100847 LocationSummary* locations =
848 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100849 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100850}
851
852void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100853 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700854 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100855}
856
857void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100858 LocationSummary* locations =
859 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100860 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100861}
862
863void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100864 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700865 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100866}
867
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100868void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
869 LocationSummary* locations =
870 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
871 locations->SetOut(Location::ConstantLocation(constant));
872}
873
874void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
875 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700876 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100877}
878
879void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
880 LocationSummary* locations =
881 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
882 locations->SetOut(Location::ConstantLocation(constant));
883}
884
885void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
886 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700887 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100888}
889
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100890void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
891 ret->SetLocations(nullptr);
892}
893
894void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700895 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100896 codegen_->GenerateFrameExit();
897 __ ret();
898}
899
900void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100901 LocationSummary* locations =
902 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100903 switch (ret->InputAt(0)->GetType()) {
904 case Primitive::kPrimBoolean:
905 case Primitive::kPrimByte:
906 case Primitive::kPrimChar:
907 case Primitive::kPrimShort:
908 case Primitive::kPrimInt:
909 case Primitive::kPrimNot:
910 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100911 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100912 break;
913
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100914 case Primitive::kPrimFloat:
915 case Primitive::kPrimDouble:
916 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100917 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100918 break;
919
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100920 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100921 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100922 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100923}
924
925void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
926 if (kIsDebugBuild) {
927 switch (ret->InputAt(0)->GetType()) {
928 case Primitive::kPrimBoolean:
929 case Primitive::kPrimByte:
930 case Primitive::kPrimChar:
931 case Primitive::kPrimShort:
932 case Primitive::kPrimInt:
933 case Primitive::kPrimNot:
934 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100935 DCHECK_EQ(ret->GetLocations()->InAt(0).As<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100936 break;
937
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100938 case Primitive::kPrimFloat:
939 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100940 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100941 XMM0);
942 break;
943
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100944 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100945 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100946 }
947 }
948 codegen_->GenerateFrameExit();
949 __ ret();
950}
951
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100952Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
953 switch (type) {
954 case Primitive::kPrimBoolean:
955 case Primitive::kPrimByte:
956 case Primitive::kPrimChar:
957 case Primitive::kPrimShort:
958 case Primitive::kPrimInt:
959 case Primitive::kPrimNot: {
960 uint32_t index = gp_index_++;
961 stack_index_++;
962 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100963 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100964 } else {
965 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
966 }
967 }
968
969 case Primitive::kPrimLong: {
970 uint32_t index = gp_index_;
971 stack_index_ += 2;
972 if (index < calling_convention.GetNumberOfRegisters()) {
973 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100974 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100975 } else {
976 gp_index_ += 2;
977 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
978 }
979 }
980
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100981 case Primitive::kPrimFloat: {
982 uint32_t index = fp_index_++;
983 stack_index_++;
984 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100985 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100986 } else {
987 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
988 }
989 }
990
991 case Primitive::kPrimDouble: {
992 uint32_t index = fp_index_++;
993 stack_index_ += 2;
994 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100995 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100996 } else {
997 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
998 }
999 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001000
1001 case Primitive::kPrimVoid:
1002 LOG(FATAL) << "Unexpected parameter type " << type;
1003 break;
1004 }
1005 return Location();
1006}
1007
1008void LocationsBuilderX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001009 HandleInvoke(invoke);
1010}
1011
1012void InstructionCodeGeneratorX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001013 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001014 // TODO: Implement all kinds of calls:
1015 // 1) boot -> boot
1016 // 2) app -> boot
1017 // 3) app -> app
1018 //
1019 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1020
1021 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001022 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001023 // temp = temp->dex_cache_resolved_methods_;
1024 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
1025 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001026 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001027 // (temp + offset_of_quick_compiled_code)()
1028 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
1029
1030 DCHECK(!codegen_->IsLeafMethod());
1031 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1032}
1033
1034void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1035 HandleInvoke(invoke);
1036}
1037
1038void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001039 LocationSummary* locations =
1040 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001041 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001042
1043 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001044 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001045 HInstruction* input = invoke->InputAt(i);
1046 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1047 }
1048
1049 switch (invoke->GetType()) {
1050 case Primitive::kPrimBoolean:
1051 case Primitive::kPrimByte:
1052 case Primitive::kPrimChar:
1053 case Primitive::kPrimShort:
1054 case Primitive::kPrimInt:
1055 case Primitive::kPrimNot:
1056 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001057 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001058 break;
1059
1060 case Primitive::kPrimVoid:
1061 break;
1062
1063 case Primitive::kPrimDouble:
1064 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001065 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001066 break;
1067 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001068}
1069
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001070void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001071 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001072 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1073 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1074 LocationSummary* locations = invoke->GetLocations();
1075 Location receiver = locations->InAt(0);
1076 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1077 // temp = object->GetClass();
1078 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001079 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1080 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001081 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001082 __ movl(temp, Address(receiver.As<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001083 }
1084 // temp = temp->GetMethodAt(method_offset);
1085 __ movl(temp, Address(temp, method_offset));
1086 // call temp->GetEntryPoint();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001087 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
1088
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001089 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001090 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001091}
1092
Roland Levillain88cb1752014-10-20 16:36:47 +01001093void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1094 LocationSummary* locations =
1095 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1096 switch (neg->GetResultType()) {
1097 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001098 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001099 locations->SetInAt(0, Location::RequiresRegister());
1100 locations->SetOut(Location::SameAsFirstInput());
1101 break;
1102
Roland Levillain88cb1752014-10-20 16:36:47 +01001103 case Primitive::kPrimFloat:
1104 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001105 locations->SetInAt(0, Location::RequiresFpuRegister());
1106 // Output overlaps as we need a fresh (zero-initialized)
1107 // register to perform subtraction from zero.
1108 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001109 break;
1110
1111 default:
1112 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1113 }
1114}
1115
1116void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1117 LocationSummary* locations = neg->GetLocations();
1118 Location out = locations->Out();
1119 Location in = locations->InAt(0);
1120 switch (neg->GetResultType()) {
1121 case Primitive::kPrimInt:
1122 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001123 DCHECK(in.Equals(out));
Roland Levillain88cb1752014-10-20 16:36:47 +01001124 __ negl(out.As<CpuRegister>());
1125 break;
1126
1127 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001128 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001129 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001130 __ negq(out.As<CpuRegister>());
1131 break;
1132
Roland Levillain88cb1752014-10-20 16:36:47 +01001133 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001134 DCHECK(in.IsFpuRegister());
1135 DCHECK(out.IsFpuRegister());
1136 DCHECK(!in.Equals(out));
1137 // TODO: Instead of computing negation as a subtraction from
1138 // zero, implement it with an exclusive or with value 0x80000000
1139 // (mask for bit 31, representing the sign of a single-precision
1140 // floating-point number), fetched from a constant pool:
1141 //
1142 // xorps out, [RIP:...] // value at RIP is 0x80 00 00 00
1143
1144 // out = 0
1145 __ xorps(out.As<XmmRegister>(), out.As<XmmRegister>());
1146 // out = out - in
1147 __ subss(out.As<XmmRegister>(), in.As<XmmRegister>());
1148 break;
1149
Roland Levillain88cb1752014-10-20 16:36:47 +01001150 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001151 DCHECK(in.IsFpuRegister());
1152 DCHECK(out.IsFpuRegister());
1153 DCHECK(!in.Equals(out));
1154 // TODO: Instead of computing negation as a subtraction from
1155 // zero, implement it with an exclusive or with value
1156 // 0x8000000000000000 (mask for bit 63, representing the sign of
1157 // a double-precision floating-point number), fetched from a
1158 // constant pool:
1159 //
1160 // xorpd out, [RIP:...] // value at RIP is 0x80 00 00 00 00 00 00 00
1161
1162 // out = 0
1163 __ xorpd(out.As<XmmRegister>(), out.As<XmmRegister>());
1164 // out = out - in
1165 __ subsd(out.As<XmmRegister>(), in.As<XmmRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001166 break;
1167
1168 default:
1169 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1170 }
1171}
1172
Roland Levillaindff1f282014-11-05 14:15:05 +00001173void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1174 LocationSummary* locations =
1175 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1176 Primitive::Type result_type = conversion->GetResultType();
1177 Primitive::Type input_type = conversion->GetInputType();
1178 switch (result_type) {
1179 case Primitive::kPrimLong:
1180 switch (input_type) {
1181 case Primitive::kPrimByte:
1182 case Primitive::kPrimShort:
1183 case Primitive::kPrimInt:
1184 // int-to-long conversion.
1185 // TODO: We would benefit from a (to-be-implemented)
1186 // Location::RegisterOrStackSlot requirement for this input.
1187 locations->SetInAt(0, Location::RequiresRegister());
1188 locations->SetOut(Location::RequiresRegister());
1189 break;
1190
1191 case Primitive::kPrimFloat:
1192 case Primitive::kPrimDouble:
1193 LOG(FATAL) << "Type conversion from " << input_type << " to "
1194 << result_type << " not yet implemented";
1195 break;
1196
1197 default:
1198 LOG(FATAL) << "Unexpected type conversion from " << input_type
1199 << " to " << result_type;
1200 }
1201 break;
1202
1203 case Primitive::kPrimInt:
1204 case Primitive::kPrimFloat:
1205 case Primitive::kPrimDouble:
1206 LOG(FATAL) << "Type conversion from " << input_type
1207 << " to " << result_type << " not yet implemented";
1208 break;
1209
1210 default:
1211 LOG(FATAL) << "Unexpected type conversion from " << input_type
1212 << " to " << result_type;
1213 }
1214}
1215
1216void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1217 LocationSummary* locations = conversion->GetLocations();
1218 Location out = locations->Out();
1219 Location in = locations->InAt(0);
1220 Primitive::Type result_type = conversion->GetResultType();
1221 Primitive::Type input_type = conversion->GetInputType();
1222 switch (result_type) {
1223 case Primitive::kPrimLong:
1224 switch (input_type) {
1225 DCHECK(out.IsRegister());
1226 case Primitive::kPrimByte:
1227 case Primitive::kPrimShort:
1228 case Primitive::kPrimInt:
1229 // int-to-long conversion.
1230 DCHECK(in.IsRegister());
1231 __ movsxd(out.As<CpuRegister>(), in.As<CpuRegister>());
1232 break;
1233
1234 case Primitive::kPrimFloat:
1235 case Primitive::kPrimDouble:
1236 LOG(FATAL) << "Type conversion from " << input_type << " to "
1237 << result_type << " not yet implemented";
1238 break;
1239
1240 default:
1241 LOG(FATAL) << "Unexpected type conversion from " << input_type
1242 << " to " << result_type;
1243 }
1244 break;
1245
1246 case Primitive::kPrimInt:
1247 case Primitive::kPrimFloat:
1248 case Primitive::kPrimDouble:
1249 LOG(FATAL) << "Type conversion from " << input_type
1250 << " to " << result_type << " not yet implemented";
1251 break;
1252
1253 default:
1254 LOG(FATAL) << "Unexpected type conversion from " << input_type
1255 << " to " << result_type;
1256 }
1257}
1258
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001259void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001260 LocationSummary* locations =
1261 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001262 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001263 case Primitive::kPrimInt: {
1264 locations->SetInAt(0, Location::RequiresRegister());
1265 locations->SetInAt(1, Location::Any());
1266 locations->SetOut(Location::SameAsFirstInput());
1267 break;
1268 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001269
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001270 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001271 locations->SetInAt(0, Location::RequiresRegister());
1272 locations->SetInAt(1, Location::RequiresRegister());
1273 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001274 break;
1275 }
1276
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001277 case Primitive::kPrimDouble:
1278 case Primitive::kPrimFloat: {
1279 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001280 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001281 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001282 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001283 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001284
1285 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001286 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001287 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001288}
1289
1290void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1291 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001292 Location first = locations->InAt(0);
1293 Location second = locations->InAt(1);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001294 DCHECK(first.Equals(locations->Out()));
Calin Juravle11351682014-10-23 15:38:15 +01001295
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001296 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001297 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001298 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001299 __ addl(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001300 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001301 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001302 __ addl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001303 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001304 __ addl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001305 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001306 break;
1307 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001308
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001309 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001310 __ addq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001311 break;
1312 }
1313
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001314 case Primitive::kPrimFloat: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001315 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001316 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001317 }
1318
1319 case Primitive::kPrimDouble: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001320 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001321 break;
1322 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001323
1324 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001325 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001326 }
1327}
1328
1329void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001330 LocationSummary* locations =
1331 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001332 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001333 case Primitive::kPrimInt: {
1334 locations->SetInAt(0, Location::RequiresRegister());
1335 locations->SetInAt(1, Location::Any());
1336 locations->SetOut(Location::SameAsFirstInput());
1337 break;
1338 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001339 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001340 locations->SetInAt(0, Location::RequiresRegister());
1341 locations->SetInAt(1, Location::RequiresRegister());
1342 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001343 break;
1344 }
Calin Juravle11351682014-10-23 15:38:15 +01001345 case Primitive::kPrimFloat:
1346 case Primitive::kPrimDouble: {
1347 locations->SetInAt(0, Location::RequiresFpuRegister());
1348 locations->SetInAt(1, Location::RequiresFpuRegister());
1349 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001350 break;
Calin Juravle11351682014-10-23 15:38:15 +01001351 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001352 default:
Calin Juravle11351682014-10-23 15:38:15 +01001353 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001354 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001355}
1356
1357void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1358 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001359 Location first = locations->InAt(0);
1360 Location second = locations->InAt(1);
1361 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001362 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001363 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001364 if (second.IsRegister()) {
1365 __ subl(first.As<CpuRegister>(), second.As<CpuRegister>());
1366 } else if (second.IsConstant()) {
1367 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1368 __ subl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001369 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001370 __ subl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001371 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001372 break;
1373 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001374 case Primitive::kPrimLong: {
Calin Juravle11351682014-10-23 15:38:15 +01001375 __ subq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001376 break;
1377 }
1378
Calin Juravle11351682014-10-23 15:38:15 +01001379 case Primitive::kPrimFloat: {
1380 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001381 break;
Calin Juravle11351682014-10-23 15:38:15 +01001382 }
1383
1384 case Primitive::kPrimDouble: {
1385 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1386 break;
1387 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001388
1389 default:
Calin Juravle11351682014-10-23 15:38:15 +01001390 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001391 }
1392}
1393
Calin Juravle34bacdf2014-10-07 20:23:36 +01001394void LocationsBuilderX86_64::VisitMul(HMul* mul) {
1395 LocationSummary* locations =
1396 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1397 switch (mul->GetResultType()) {
1398 case Primitive::kPrimInt: {
1399 locations->SetInAt(0, Location::RequiresRegister());
1400 locations->SetInAt(1, Location::Any());
1401 locations->SetOut(Location::SameAsFirstInput());
1402 break;
1403 }
1404 case Primitive::kPrimLong: {
1405 locations->SetInAt(0, Location::RequiresRegister());
1406 locations->SetInAt(1, Location::RequiresRegister());
1407 locations->SetOut(Location::SameAsFirstInput());
1408 break;
1409 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001410 case Primitive::kPrimFloat:
1411 case Primitive::kPrimDouble: {
1412 locations->SetInAt(0, Location::RequiresFpuRegister());
1413 locations->SetInAt(1, Location::RequiresFpuRegister());
1414 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001415 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001416 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001417
1418 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001419 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001420 }
1421}
1422
1423void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
1424 LocationSummary* locations = mul->GetLocations();
1425 Location first = locations->InAt(0);
1426 Location second = locations->InAt(1);
1427 DCHECK(first.Equals(locations->Out()));
1428 switch (mul->GetResultType()) {
1429 case Primitive::kPrimInt: {
1430 if (second.IsRegister()) {
1431 __ imull(first.As<CpuRegister>(), second.As<CpuRegister>());
1432 } else if (second.IsConstant()) {
1433 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1434 __ imull(first.As<CpuRegister>(), imm);
1435 } else {
1436 DCHECK(second.IsStackSlot());
1437 __ imull(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
1438 }
1439 break;
1440 }
1441 case Primitive::kPrimLong: {
1442 __ imulq(first.As<CpuRegister>(), second.As<CpuRegister>());
1443 break;
1444 }
1445
Calin Juravleb5bfa962014-10-21 18:02:24 +01001446 case Primitive::kPrimFloat: {
1447 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001448 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001449 }
1450
1451 case Primitive::kPrimDouble: {
1452 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1453 break;
1454 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001455
1456 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001457 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001458 }
1459}
1460
Calin Juravle7c4954d2014-10-28 16:57:40 +00001461void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
1462 LocationSummary* locations =
1463 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1464 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001465 case Primitive::kPrimInt: {
1466 locations->SetInAt(0, Location::RegisterLocation(RAX));
1467 locations->SetInAt(1, Location::RequiresRegister());
1468 locations->SetOut(Location::SameAsFirstInput());
1469 // Intel uses edx:eax as the dividend.
1470 locations->AddTemp(Location::RegisterLocation(RDX));
1471 break;
1472 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001473 case Primitive::kPrimLong: {
1474 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1475 break;
1476 }
1477 case Primitive::kPrimFloat:
1478 case Primitive::kPrimDouble: {
1479 locations->SetInAt(0, Location::RequiresFpuRegister());
1480 locations->SetInAt(1, Location::RequiresFpuRegister());
1481 locations->SetOut(Location::SameAsFirstInput());
1482 break;
1483 }
1484
1485 default:
1486 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1487 }
1488}
1489
1490void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
1491 LocationSummary* locations = div->GetLocations();
1492 Location first = locations->InAt(0);
1493 Location second = locations->InAt(1);
1494 DCHECK(first.Equals(locations->Out()));
1495
1496 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001497 case Primitive::kPrimInt: {
1498 CpuRegister first_reg = first.As<CpuRegister>();
1499 CpuRegister second_reg = second.As<CpuRegister>();
1500 DCHECK_EQ(RAX, first_reg.AsRegister());
1501 DCHECK_EQ(RDX, locations->GetTemp(0).As<CpuRegister>().AsRegister());
1502
1503 SlowPathCodeX86_64* slow_path =
1504 new (GetGraph()->GetArena()) DivMinusOneSlowPathX86_64(first_reg.AsRegister());
1505 codegen_->AddSlowPath(slow_path);
1506
1507 // 0x80000000/-1 triggers an arithmetic exception!
1508 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
1509 // it's safe to just use negl instead of more complex comparisons.
1510
1511 __ cmpl(second_reg, Immediate(-1));
1512 __ j(kEqual, slow_path->GetEntryLabel());
1513
1514 // edx:eax <- sign-extended of eax
1515 __ cdq();
1516 // eax = quotient, edx = remainder
1517 __ idivl(second_reg);
1518
1519 __ Bind(slow_path->GetExitLabel());
1520 break;
1521 }
1522
Calin Juravle7c4954d2014-10-28 16:57:40 +00001523 case Primitive::kPrimLong: {
1524 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1525 break;
1526 }
1527
1528 case Primitive::kPrimFloat: {
1529 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1530 break;
1531 }
1532
1533 case Primitive::kPrimDouble: {
1534 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1535 break;
1536 }
1537
1538 default:
1539 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1540 }
1541}
1542
Calin Juravled0d48522014-11-04 16:40:20 +00001543void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1544 LocationSummary* locations =
1545 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1546 locations->SetInAt(0, Location::Any());
1547 if (instruction->HasUses()) {
1548 locations->SetOut(Location::SameAsFirstInput());
1549 }
1550}
1551
1552void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1553 SlowPathCodeX86_64* slow_path =
1554 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
1555 codegen_->AddSlowPath(slow_path);
1556
1557 LocationSummary* locations = instruction->GetLocations();
1558 Location value = locations->InAt(0);
1559
1560 if (value.IsRegister()) {
1561 __ testl(value.As<CpuRegister>(), value.As<CpuRegister>());
1562 } else if (value.IsStackSlot()) {
1563 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
1564 } else {
1565 DCHECK(value.IsConstant()) << value;
1566 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
1567 __ jmp(slow_path->GetEntryLabel());
1568 }
1569 return;
1570 }
1571 __ j(kEqual, slow_path->GetEntryLabel());
1572}
1573
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001574void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001575 LocationSummary* locations =
1576 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001577 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001578 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1579 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1580 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001581}
1582
1583void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
1584 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001585 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001586 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1587
1588 __ gs()->call(Address::Absolute(
1589 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
1590
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001591 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001592 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001593}
1594
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001595void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
1596 LocationSummary* locations =
1597 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1598 InvokeRuntimeCallingConvention calling_convention;
1599 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1600 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1601 locations->SetOut(Location::RegisterLocation(RAX));
1602 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1603}
1604
1605void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
1606 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001607 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001608 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1609
1610 __ gs()->call(Address::Absolute(
1611 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocArrayWithAccessCheck), true));
1612
1613 DCHECK(!codegen_->IsLeafMethod());
1614 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1615}
1616
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001617void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001618 LocationSummary* locations =
1619 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001620 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1621 if (location.IsStackSlot()) {
1622 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1623 } else if (location.IsDoubleStackSlot()) {
1624 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1625 }
1626 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001627}
1628
1629void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
1630 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001631 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001632}
1633
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001634void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001635 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001636 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001637 locations->SetInAt(0, Location::RequiresRegister());
1638 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001639}
1640
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001641void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
1642 LocationSummary* locations = not_->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001643 DCHECK_EQ(locations->InAt(0).As<CpuRegister>().AsRegister(),
1644 locations->Out().As<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001645 Location out = locations->Out();
1646 switch (not_->InputAt(0)->GetType()) {
1647 case Primitive::kPrimBoolean:
1648 __ xorq(out.As<CpuRegister>(), Immediate(1));
1649 break;
1650
1651 case Primitive::kPrimInt:
1652 __ notl(out.As<CpuRegister>());
1653 break;
1654
1655 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001656 __ notq(out.As<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001657 break;
1658
1659 default:
1660 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1661 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001662}
1663
1664void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001665 LocationSummary* locations =
1666 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001667 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1668 locations->SetInAt(i, Location::Any());
1669 }
1670 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001671}
1672
1673void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001674 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001675 LOG(FATAL) << "Unimplemented";
1676}
1677
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001678void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001679 LocationSummary* locations =
1680 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001681 Primitive::Type field_type = instruction->GetFieldType();
1682 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001683 locations->SetInAt(0, Location::RequiresRegister());
1684 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001685 if (is_object_type) {
1686 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001687 locations->AddTemp(Location::RequiresRegister());
1688 locations->AddTemp(Location::RequiresRegister());
1689 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001690}
1691
1692void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1693 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001694 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001695 size_t offset = instruction->GetFieldOffset().SizeValue();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001696 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001697
1698 switch (field_type) {
1699 case Primitive::kPrimBoolean:
1700 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001701 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001702 __ movb(Address(obj, offset), value);
1703 break;
1704 }
1705
1706 case Primitive::kPrimShort:
1707 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001708 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001709 __ movw(Address(obj, offset), value);
1710 break;
1711 }
1712
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001713 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001714 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001715 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001716 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001717 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001718 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
1719 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001720 codegen_->MarkGCCard(temp, card, obj, value);
1721 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001722 break;
1723 }
1724
1725 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001726 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001727 __ movq(Address(obj, offset), value);
1728 break;
1729 }
1730
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001731 case Primitive::kPrimFloat: {
1732 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1733 __ movss(Address(obj, offset), value);
1734 break;
1735 }
1736
1737 case Primitive::kPrimDouble: {
1738 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1739 __ movsd(Address(obj, offset), value);
1740 break;
1741 }
1742
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001743 case Primitive::kPrimVoid:
1744 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001745 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001746 }
1747}
1748
1749void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001750 LocationSummary* locations =
1751 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001752 locations->SetInAt(0, Location::RequiresRegister());
1753 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001754}
1755
1756void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1757 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001758 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001759 size_t offset = instruction->GetFieldOffset().SizeValue();
1760
1761 switch (instruction->GetType()) {
1762 case Primitive::kPrimBoolean: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001763 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001764 __ movzxb(out, Address(obj, offset));
1765 break;
1766 }
1767
1768 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001769 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001770 __ movsxb(out, Address(obj, offset));
1771 break;
1772 }
1773
1774 case Primitive::kPrimShort: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001775 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001776 __ movsxw(out, Address(obj, offset));
1777 break;
1778 }
1779
1780 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001781 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001782 __ movzxw(out, Address(obj, offset));
1783 break;
1784 }
1785
1786 case Primitive::kPrimInt:
1787 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001788 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001789 __ movl(out, Address(obj, offset));
1790 break;
1791 }
1792
1793 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001794 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001795 __ movq(out, Address(obj, offset));
1796 break;
1797 }
1798
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001799 case Primitive::kPrimFloat: {
1800 XmmRegister out = locations->Out().As<XmmRegister>();
1801 __ movss(out, Address(obj, offset));
1802 break;
1803 }
1804
1805 case Primitive::kPrimDouble: {
1806 XmmRegister out = locations->Out().As<XmmRegister>();
1807 __ movsd(out, Address(obj, offset));
1808 break;
1809 }
1810
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001811 case Primitive::kPrimVoid:
1812 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001813 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001814 }
1815}
1816
1817void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001818 LocationSummary* locations =
1819 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001820 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001821 if (instruction->HasUses()) {
1822 locations->SetOut(Location::SameAsFirstInput());
1823 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001824}
1825
1826void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001827 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001828 codegen_->AddSlowPath(slow_path);
1829
1830 LocationSummary* locations = instruction->GetLocations();
1831 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001832
1833 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001834 __ cmpl(obj.As<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001835 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001836 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001837 } else {
1838 DCHECK(obj.IsConstant()) << obj;
1839 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1840 __ jmp(slow_path->GetEntryLabel());
1841 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001842 }
1843 __ j(kEqual, slow_path->GetEntryLabel());
1844}
1845
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001846void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001847 LocationSummary* locations =
1848 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001849 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001850 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001851 1, Location::RegisterOrConstant(instruction->InputAt(1)));
1852 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001853}
1854
1855void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
1856 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001857 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001858 Location index = locations->InAt(1);
1859
1860 switch (instruction->GetType()) {
1861 case Primitive::kPrimBoolean: {
1862 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001863 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001864 if (index.IsConstant()) {
1865 __ movzxb(out, Address(obj,
1866 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1867 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001868 __ movzxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001869 }
1870 break;
1871 }
1872
1873 case Primitive::kPrimByte: {
1874 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001875 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001876 if (index.IsConstant()) {
1877 __ movsxb(out, Address(obj,
1878 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1879 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001880 __ movsxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001881 }
1882 break;
1883 }
1884
1885 case Primitive::kPrimShort: {
1886 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001887 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001888 if (index.IsConstant()) {
1889 __ movsxw(out, Address(obj,
1890 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1891 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001892 __ movsxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001893 }
1894 break;
1895 }
1896
1897 case Primitive::kPrimChar: {
1898 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001899 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001900 if (index.IsConstant()) {
1901 __ movzxw(out, Address(obj,
1902 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1903 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001904 __ movzxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001905 }
1906 break;
1907 }
1908
1909 case Primitive::kPrimInt:
1910 case Primitive::kPrimNot: {
1911 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1912 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001913 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001914 if (index.IsConstant()) {
1915 __ movl(out, Address(obj,
1916 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1917 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001918 __ movl(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001919 }
1920 break;
1921 }
1922
1923 case Primitive::kPrimLong: {
1924 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001925 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001926 if (index.IsConstant()) {
1927 __ movq(out, Address(obj,
1928 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
1929 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001930 __ movq(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001931 }
1932 break;
1933 }
1934
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001935 case Primitive::kPrimFloat: {
1936 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1937 XmmRegister out = locations->Out().As<XmmRegister>();
1938 if (index.IsConstant()) {
1939 __ movss(out, Address(obj,
1940 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1941 } else {
1942 __ movss(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
1943 }
1944 break;
1945 }
1946
1947 case Primitive::kPrimDouble: {
1948 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1949 XmmRegister out = locations->Out().As<XmmRegister>();
1950 if (index.IsConstant()) {
1951 __ movsd(out, Address(obj,
1952 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
1953 } else {
1954 __ movsd(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
1955 }
1956 break;
1957 }
1958
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001959 case Primitive::kPrimVoid:
1960 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001961 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001962 }
1963}
1964
1965void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001966 Primitive::Type value_type = instruction->GetComponentType();
1967 bool is_object = value_type == Primitive::kPrimNot;
1968 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1969 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
1970 if (is_object) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001971 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001972 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1973 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1974 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001975 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001976 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001977 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001978 1, Location::RegisterOrConstant(instruction->InputAt(1)));
1979 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001980 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001981 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001982 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
1983 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001984 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001985 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001986 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001987 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001988}
1989
1990void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
1991 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001992 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001993 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001994 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001995 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001996
1997 switch (value_type) {
1998 case Primitive::kPrimBoolean:
1999 case Primitive::kPrimByte: {
2000 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002001 if (index.IsConstant()) {
2002 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002003 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002004 __ movb(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002005 } else {
2006 __ movb(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2007 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002008 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002009 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002010 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
2011 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002012 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002013 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002014 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2015 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002016 }
2017 break;
2018 }
2019
2020 case Primitive::kPrimShort:
2021 case Primitive::kPrimChar: {
2022 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002023 if (index.IsConstant()) {
2024 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002025 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002026 __ movw(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002027 } else {
2028 __ movw(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2029 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002030 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002031 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002032 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
2033 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002034 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002035 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002036 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2037 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002038 }
2039 break;
2040 }
2041
2042 case Primitive::kPrimInt: {
2043 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002044 if (index.IsConstant()) {
2045 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002046 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002047 __ movl(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002048 } else {
2049 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2050 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002051 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002052 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002053 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2054 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002055 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002056 DCHECK(value.IsConstant()) << value;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002057 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002058 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2059 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002060 }
2061 break;
2062 }
2063
2064 case Primitive::kPrimNot: {
2065 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject), true));
2066 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002067 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002068 break;
2069 }
2070
2071 case Primitive::kPrimLong: {
2072 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002073 if (index.IsConstant()) {
2074 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002075 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002076 __ movq(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002077 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002078 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002079 __ movq(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
2080 value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002081 }
2082 break;
2083 }
2084
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002085 case Primitive::kPrimFloat: {
2086 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2087 if (index.IsConstant()) {
2088 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2089 DCHECK(value.IsFpuRegister());
2090 __ movss(Address(obj, offset), value.As<XmmRegister>());
2091 } else {
2092 DCHECK(value.IsFpuRegister());
2093 __ movss(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2094 value.As<XmmRegister>());
2095 }
2096 break;
2097 }
2098
2099 case Primitive::kPrimDouble: {
2100 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2101 if (index.IsConstant()) {
2102 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
2103 DCHECK(value.IsFpuRegister());
2104 __ movsd(Address(obj, offset), value.As<XmmRegister>());
2105 } else {
2106 DCHECK(value.IsFpuRegister());
2107 __ movsd(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
2108 value.As<XmmRegister>());
2109 }
2110 break;
2111 }
2112
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002113 case Primitive::kPrimVoid:
2114 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002115 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002116 }
2117}
2118
2119void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002120 LocationSummary* locations =
2121 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002122 locations->SetInAt(0, Location::RequiresRegister());
2123 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002124}
2125
2126void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
2127 LocationSummary* locations = instruction->GetLocations();
2128 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002129 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2130 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002131 __ movl(out, Address(obj, offset));
2132}
2133
2134void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002135 LocationSummary* locations =
2136 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002137 locations->SetInAt(0, Location::RequiresRegister());
2138 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002139 if (instruction->HasUses()) {
2140 locations->SetOut(Location::SameAsFirstInput());
2141 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002142}
2143
2144void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
2145 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002146 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002147 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002148 codegen_->AddSlowPath(slow_path);
2149
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002150 CpuRegister index = locations->InAt(0).As<CpuRegister>();
2151 CpuRegister length = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002152
2153 __ cmpl(index, length);
2154 __ j(kAboveEqual, slow_path->GetEntryLabel());
2155}
2156
2157void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
2158 CpuRegister card,
2159 CpuRegister object,
2160 CpuRegister value) {
2161 Label is_null;
2162 __ testl(value, value);
2163 __ j(kEqual, &is_null);
2164 __ gs()->movq(card, Address::Absolute(
2165 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
2166 __ movq(temp, object);
2167 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
2168 __ movb(Address(temp, card, TIMES_1, 0), card);
2169 __ Bind(&is_null);
2170}
2171
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002172void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
2173 temp->SetLocations(nullptr);
2174}
2175
2176void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
2177 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002178 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002179}
2180
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002181void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002182 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002183 LOG(FATAL) << "Unimplemented";
2184}
2185
2186void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002187 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2188}
2189
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002190void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
2191 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2192}
2193
2194void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002195 HBasicBlock* block = instruction->GetBlock();
2196 if (block->GetLoopInformation() != nullptr) {
2197 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2198 // The back edge will generate the suspend check.
2199 return;
2200 }
2201 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2202 // The goto will generate the suspend check.
2203 return;
2204 }
2205 GenerateSuspendCheck(instruction, nullptr);
2206}
2207
2208void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
2209 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002210 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002211 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002212 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002213 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002214 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002215 if (successor == nullptr) {
2216 __ j(kNotEqual, slow_path->GetEntryLabel());
2217 __ Bind(slow_path->GetReturnLabel());
2218 } else {
2219 __ j(kEqual, codegen_->GetLabelOf(successor));
2220 __ jmp(slow_path->GetEntryLabel());
2221 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002222}
2223
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002224X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
2225 return codegen_->GetAssembler();
2226}
2227
2228void ParallelMoveResolverX86_64::EmitMove(size_t index) {
2229 MoveOperands* move = moves_.Get(index);
2230 Location source = move->GetSource();
2231 Location destination = move->GetDestination();
2232
2233 if (source.IsRegister()) {
2234 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002235 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002236 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002237 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002238 source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002239 } else {
2240 DCHECK(destination.IsDoubleStackSlot());
2241 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002242 source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002243 }
2244 } else if (source.IsStackSlot()) {
2245 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002246 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002247 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002248 } else if (destination.IsFpuRegister()) {
2249 __ movss(destination.As<XmmRegister>(),
2250 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002251 } else {
2252 DCHECK(destination.IsStackSlot());
2253 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2254 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2255 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002256 } else if (source.IsDoubleStackSlot()) {
2257 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002258 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002259 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002260 } else if (destination.IsFpuRegister()) {
2261 __ movsd(destination.As<XmmRegister>(), Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002262 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01002263 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002264 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2265 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2266 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002267 } else if (source.IsConstant()) {
2268 HConstant* constant = source.GetConstant();
2269 if (constant->IsIntConstant()) {
2270 Immediate imm(constant->AsIntConstant()->GetValue());
2271 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002272 __ movl(destination.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002273 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002274 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002275 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2276 }
2277 } else if (constant->IsLongConstant()) {
2278 int64_t value = constant->AsLongConstant()->GetValue();
2279 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002280 __ movq(destination.As<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002281 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002282 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002283 __ movq(CpuRegister(TMP), Immediate(value));
2284 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2285 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002286 } else if (constant->IsFloatConstant()) {
2287 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
2288 if (destination.IsFpuRegister()) {
2289 __ movl(CpuRegister(TMP), imm);
2290 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2291 } else {
2292 DCHECK(destination.IsStackSlot()) << destination;
2293 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2294 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002295 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002296 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
2297 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
2298 if (destination.IsFpuRegister()) {
2299 __ movq(CpuRegister(TMP), imm);
2300 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2301 } else {
2302 DCHECK(destination.IsDoubleStackSlot()) << destination;
2303 __ movq(CpuRegister(TMP), imm);
2304 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2305 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002306 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002307 } else if (source.IsFpuRegister()) {
2308 if (destination.IsFpuRegister()) {
2309 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
2310 } else if (destination.IsStackSlot()) {
2311 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
2312 source.As<XmmRegister>());
2313 } else {
2314 DCHECK(destination.IsDoubleStackSlot());
2315 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
2316 source.As<XmmRegister>());
2317 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002318 }
2319}
2320
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002321void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002322 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002323 __ movl(Address(CpuRegister(RSP), mem), reg);
2324 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002325}
2326
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002327void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002328 ScratchRegisterScope ensure_scratch(
2329 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2330
2331 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2332 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2333 __ movl(CpuRegister(ensure_scratch.GetRegister()),
2334 Address(CpuRegister(RSP), mem2 + stack_offset));
2335 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2336 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
2337 CpuRegister(ensure_scratch.GetRegister()));
2338}
2339
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002340void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
2341 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2342 __ movq(Address(CpuRegister(RSP), mem), reg);
2343 __ movq(reg, CpuRegister(TMP));
2344}
2345
2346void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
2347 ScratchRegisterScope ensure_scratch(
2348 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2349
2350 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2351 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2352 __ movq(CpuRegister(ensure_scratch.GetRegister()),
2353 Address(CpuRegister(RSP), mem2 + stack_offset));
2354 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2355 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
2356 CpuRegister(ensure_scratch.GetRegister()));
2357}
2358
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002359void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
2360 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2361 __ movss(Address(CpuRegister(RSP), mem), reg);
2362 __ movd(reg, CpuRegister(TMP));
2363}
2364
2365void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
2366 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2367 __ movsd(Address(CpuRegister(RSP), mem), reg);
2368 __ movd(reg, CpuRegister(TMP));
2369}
2370
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002371void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
2372 MoveOperands* move = moves_.Get(index);
2373 Location source = move->GetSource();
2374 Location destination = move->GetDestination();
2375
2376 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002377 __ xchgq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002378 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002379 Exchange32(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002380 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002381 Exchange32(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002382 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002383 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
2384 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002385 Exchange64(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002386 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002387 Exchange64(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002388 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
2389 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002390 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
2391 __ movd(CpuRegister(TMP), source.As<XmmRegister>());
2392 __ movaps(source.As<XmmRegister>(), destination.As<XmmRegister>());
2393 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2394 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
2395 Exchange32(source.As<XmmRegister>(), destination.GetStackIndex());
2396 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
2397 Exchange32(destination.As<XmmRegister>(), source.GetStackIndex());
2398 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
2399 Exchange64(source.As<XmmRegister>(), destination.GetStackIndex());
2400 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
2401 Exchange64(destination.As<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002402 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002403 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002404 }
2405}
2406
2407
2408void ParallelMoveResolverX86_64::SpillScratch(int reg) {
2409 __ pushq(CpuRegister(reg));
2410}
2411
2412
2413void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
2414 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002415}
2416
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002417void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
2418 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
2419 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2420 Immediate(mirror::Class::kStatusInitialized));
2421 __ j(kLess, slow_path->GetEntryLabel());
2422 __ Bind(slow_path->GetExitLabel());
2423 // No need for memory fence, thanks to the X86_64 memory model.
2424}
2425
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002426void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002427 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2428 ? LocationSummary::kCallOnSlowPath
2429 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002430 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002431 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002432 locations->SetOut(Location::RequiresRegister());
2433}
2434
2435void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
2436 CpuRegister out = cls->GetLocations()->Out().As<CpuRegister>();
2437 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002438 DCHECK(!cls->CanCallRuntime());
2439 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002440 codegen_->LoadCurrentMethod(out);
2441 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2442 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002443 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002444 codegen_->LoadCurrentMethod(out);
2445 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2446 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002447 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
2448 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2449 codegen_->AddSlowPath(slow_path);
2450 __ testl(out, out);
2451 __ j(kEqual, slow_path->GetEntryLabel());
2452 if (cls->MustGenerateClinitCheck()) {
2453 GenerateClassInitializationCheck(slow_path, out);
2454 } else {
2455 __ Bind(slow_path->GetExitLabel());
2456 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002457 }
2458}
2459
2460void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
2461 LocationSummary* locations =
2462 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2463 locations->SetInAt(0, Location::RequiresRegister());
2464 if (check->HasUses()) {
2465 locations->SetOut(Location::SameAsFirstInput());
2466 }
2467}
2468
2469void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002470 // We assume the class to not be null.
2471 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
2472 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002473 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002474 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002475}
2476
2477void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2478 LocationSummary* locations =
2479 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2480 locations->SetInAt(0, Location::RequiresRegister());
2481 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2482}
2483
2484void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2485 LocationSummary* locations = instruction->GetLocations();
2486 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002487 size_t offset = instruction->GetFieldOffset().SizeValue();
2488
2489 switch (instruction->GetType()) {
2490 case Primitive::kPrimBoolean: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002491 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002492 __ movzxb(out, Address(cls, offset));
2493 break;
2494 }
2495
2496 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002497 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002498 __ movsxb(out, Address(cls, offset));
2499 break;
2500 }
2501
2502 case Primitive::kPrimShort: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002503 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002504 __ movsxw(out, Address(cls, offset));
2505 break;
2506 }
2507
2508 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002509 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002510 __ movzxw(out, Address(cls, offset));
2511 break;
2512 }
2513
2514 case Primitive::kPrimInt:
2515 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002516 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002517 __ movl(out, Address(cls, offset));
2518 break;
2519 }
2520
2521 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002522 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002523 __ movq(out, Address(cls, offset));
2524 break;
2525 }
2526
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002527 case Primitive::kPrimFloat: {
2528 XmmRegister out = locations->Out().As<XmmRegister>();
2529 __ movss(out, Address(cls, offset));
2530 break;
2531 }
2532
2533 case Primitive::kPrimDouble: {
2534 XmmRegister out = locations->Out().As<XmmRegister>();
2535 __ movsd(out, Address(cls, offset));
2536 break;
2537 }
2538
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002539 case Primitive::kPrimVoid:
2540 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2541 UNREACHABLE();
2542 }
2543}
2544
2545void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2546 LocationSummary* locations =
2547 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2548 Primitive::Type field_type = instruction->GetFieldType();
2549 bool is_object_type = field_type == Primitive::kPrimNot;
2550 locations->SetInAt(0, Location::RequiresRegister());
2551 locations->SetInAt(1, Location::RequiresRegister());
2552 if (is_object_type) {
2553 // Temporary registers for the write barrier.
2554 locations->AddTemp(Location::RequiresRegister());
2555 locations->AddTemp(Location::RequiresRegister());
2556 }
2557}
2558
2559void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2560 LocationSummary* locations = instruction->GetLocations();
2561 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002562 size_t offset = instruction->GetFieldOffset().SizeValue();
2563 Primitive::Type field_type = instruction->GetFieldType();
2564
2565 switch (field_type) {
2566 case Primitive::kPrimBoolean:
2567 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002568 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002569 __ movb(Address(cls, offset), value);
2570 break;
2571 }
2572
2573 case Primitive::kPrimShort:
2574 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002575 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002576 __ movw(Address(cls, offset), value);
2577 break;
2578 }
2579
2580 case Primitive::kPrimInt:
2581 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002582 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002583 __ movl(Address(cls, offset), value);
2584 if (field_type == Primitive::kPrimNot) {
2585 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
2586 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
2587 codegen_->MarkGCCard(temp, card, cls, value);
2588 }
2589 break;
2590 }
2591
2592 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002593 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002594 __ movq(Address(cls, offset), value);
2595 break;
2596 }
2597
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002598 case Primitive::kPrimFloat: {
2599 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2600 __ movss(Address(cls, offset), value);
2601 break;
2602 }
2603
2604 case Primitive::kPrimDouble: {
2605 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2606 __ movsd(Address(cls, offset), value);
2607 break;
2608 }
2609
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002610 case Primitive::kPrimVoid:
2611 LOG(FATAL) << "Unreachable type " << field_type;
2612 UNREACHABLE();
2613 }
2614}
2615
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002616void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
2617 LocationSummary* locations =
2618 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2619 locations->SetOut(Location::RequiresRegister());
2620}
2621
2622void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
2623 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
2624 codegen_->AddSlowPath(slow_path);
2625
2626 CpuRegister out = load->GetLocations()->Out().As<CpuRegister>();
2627 codegen_->LoadCurrentMethod(CpuRegister(out));
2628 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
2629 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2630 __ testl(out, out);
2631 __ j(kEqual, slow_path->GetEntryLabel());
2632 __ Bind(slow_path->GetExitLabel());
2633}
2634
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002635void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
2636 LocationSummary* locations =
2637 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2638 locations->SetOut(Location::RequiresRegister());
2639}
2640
2641void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
2642 Address address = Address::Absolute(
2643 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
2644 __ gs()->movl(load->GetLocations()->Out().As<CpuRegister>(), address);
2645 __ gs()->movl(address, Immediate(0));
2646}
2647
2648void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
2649 LocationSummary* locations =
2650 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2651 InvokeRuntimeCallingConvention calling_convention;
2652 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2653}
2654
2655void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
2656 __ gs()->call(
2657 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
2658 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2659}
2660
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002661} // namespace x86_64
2662} // namespace art