blob: 910e73db25a4aaa8a6b7cd131805bc4e3d05efce [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
Calin Juravlebacfec32014-11-14 15:54:36 +0000109class DivRemMinusOneSlowPathX86_64 : public SlowPathCodeX86_64 {
Calin Juravled0d48522014-11-04 16:40:20 +0000110 public:
Calin Juravlebacfec32014-11-14 15:54:36 +0000111 explicit DivRemMinusOneSlowPathX86_64(Register reg, Primitive::Type type, bool is_div)
112 : cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000113
114 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
115 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000116 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000117 if (is_div_) {
118 __ negl(cpu_reg_);
119 } else {
120 __ movl(cpu_reg_, Immediate(0));
121 }
122
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000123 } else {
124 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000125 if (is_div_) {
126 __ negq(cpu_reg_);
127 } else {
128 __ movq(cpu_reg_, Immediate(0));
129 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000130 }
Calin Juravled0d48522014-11-04 16:40:20 +0000131 __ jmp(GetExitLabel());
132 }
133
134 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000135 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000136 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000137 const bool is_div_;
138 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000139};
140
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100141class StackOverflowCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100142 public:
143 StackOverflowCheckSlowPathX86_64() {}
144
145 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
146 __ Bind(GetEntryLabel());
147 __ addq(CpuRegister(RSP),
148 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
149 __ gs()->jmp(
150 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowStackOverflow), true));
151 }
152
153 private:
154 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86_64);
155};
156
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100157class SuspendCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000158 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100159 explicit SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
160 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000161
162 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100163 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000164 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100165 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000166 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pTestSuspend), true));
167 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100168 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100169 if (successor_ == nullptr) {
170 __ jmp(GetReturnLabel());
171 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100172 __ jmp(x64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100173 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000174 }
175
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100176 Label* GetReturnLabel() {
177 DCHECK(successor_ == nullptr);
178 return &return_label_;
179 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000180
181 private:
182 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100183 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000184 Label return_label_;
185
186 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
187};
188
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100189class BoundsCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100190 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100191 BoundsCheckSlowPathX86_64(HBoundsCheck* instruction,
192 Location index_location,
193 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100194 : instruction_(instruction),
195 index_location_(index_location),
196 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100197
198 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100199 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000200 // We're moving two locations to locations that could overlap, so we need a parallel
201 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100202 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000203 codegen->EmitParallelMoves(
204 index_location_,
205 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
206 length_location_,
207 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100208 __ gs()->call(Address::Absolute(
209 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowArrayBounds), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100210 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100211 }
212
213 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100214 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100215 const Location index_location_;
216 const Location length_location_;
217
218 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
219};
220
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000221class LoadClassSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100222 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000223 LoadClassSlowPathX86_64(HLoadClass* cls,
224 HInstruction* at,
225 uint32_t dex_pc,
226 bool do_clinit)
227 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
228 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
229 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100230
231 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000232 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100233 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
234 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100235
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000236 codegen->SaveLiveRegisters(locations);
237
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100238 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000239 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100240 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000241 __ gs()->call(Address::Absolute((do_clinit_
242 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeStaticStorage)
243 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeType)) , true));
244 codegen->RecordPcInfo(at_, dex_pc_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100245
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000246 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000247 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000248 if (out.IsValid()) {
249 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
250 x64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000251 }
252
253 codegen->RestoreLiveRegisters(locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100254 __ jmp(GetExitLabel());
255 }
256
257 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000258 // The class this slow path will load.
259 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100260
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000261 // The instruction where this slow path is happening.
262 // (Might be the load class or an initialization check).
263 HInstruction* const at_;
264
265 // The dex PC of `at_`.
266 const uint32_t dex_pc_;
267
268 // Whether to initialize the class.
269 const bool do_clinit_;
270
271 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100272};
273
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000274class LoadStringSlowPathX86_64 : public SlowPathCodeX86_64 {
275 public:
276 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
277
278 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
279 LocationSummary* locations = instruction_->GetLocations();
280 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
281
282 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
283 __ Bind(GetEntryLabel());
284 codegen->SaveLiveRegisters(locations);
285
286 InvokeRuntimeCallingConvention calling_convention;
287 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(0)));
288 __ movl(CpuRegister(calling_convention.GetRegisterAt(1)),
289 Immediate(instruction_->GetStringIndex()));
290 __ gs()->call(Address::Absolute(
291 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pResolveString), true));
292 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
293 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
294 codegen->RestoreLiveRegisters(locations);
295 __ jmp(GetExitLabel());
296 }
297
298 private:
299 HLoadString* const instruction_;
300
301 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
302};
303
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000304class TypeCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
305 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000306 TypeCheckSlowPathX86_64(HInstruction* instruction,
307 Location class_to_check,
308 Location object_class,
309 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000310 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000311 class_to_check_(class_to_check),
312 object_class_(object_class),
313 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000314
315 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
316 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000317 DCHECK(instruction_->IsCheckCast()
318 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000319
320 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
321 __ Bind(GetEntryLabel());
322 codegen->SaveLiveRegisters(locations);
323
324 // We're moving two locations to locations that could overlap, so we need a parallel
325 // move resolver.
326 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000327 codegen->EmitParallelMoves(
328 class_to_check_,
329 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
330 object_class_,
331 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000332
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000333 if (instruction_->IsInstanceOf()) {
334 __ gs()->call(
335 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInstanceofNonTrivial), true));
336 } else {
337 DCHECK(instruction_->IsCheckCast());
338 __ gs()->call(
339 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pCheckCast), true));
340 }
341 codegen->RecordPcInfo(instruction_, dex_pc_);
342
343 if (instruction_->IsInstanceOf()) {
344 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
345 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000346
347 codegen->RestoreLiveRegisters(locations);
348 __ jmp(GetExitLabel());
349 }
350
351 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000352 HInstruction* const instruction_;
353 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000354 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000355 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000356
357 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
358};
359
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100360#undef __
361#define __ reinterpret_cast<X86_64Assembler*>(GetAssembler())->
362
Dave Allison20dfc792014-06-16 20:44:29 -0700363inline Condition X86_64Condition(IfCondition cond) {
364 switch (cond) {
365 case kCondEQ: return kEqual;
366 case kCondNE: return kNotEqual;
367 case kCondLT: return kLess;
368 case kCondLE: return kLessEqual;
369 case kCondGT: return kGreater;
370 case kCondGE: return kGreaterEqual;
371 default:
372 LOG(FATAL) << "Unknown if condition";
373 }
374 return kEqual;
375}
376
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100377void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
378 stream << X86_64ManagedRegister::FromCpuRegister(Register(reg));
379}
380
381void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
382 stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg));
383}
384
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100385size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
386 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
387 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100388}
389
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100390size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
391 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
392 return kX86_64WordSize;
393}
394
395size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
396 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
397 return kX86_64WordSize;
398}
399
400size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
401 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
402 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100403}
404
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100405CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100406 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfFloatRegisters, 0),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100407 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100408 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000409 instruction_visitor_(graph, this),
410 move_resolver_(graph->GetArena(), this) {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100411
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100412size_t CodeGeneratorX86_64::FrameEntrySpillSize() const {
413 return kNumberOfPushedRegistersAtEntry * kX86_64WordSize;
414}
415
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100416InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
417 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100418 : HGraphVisitor(graph),
419 assembler_(codegen->GetAssembler()),
420 codegen_(codegen) {}
421
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100422Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100423 switch (type) {
424 case Primitive::kPrimLong:
425 case Primitive::kPrimByte:
426 case Primitive::kPrimBoolean:
427 case Primitive::kPrimChar:
428 case Primitive::kPrimShort:
429 case Primitive::kPrimInt:
430 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100431 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100432 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100433 }
434
435 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100436 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100437 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100438 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100439 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100440
441 case Primitive::kPrimVoid:
442 LOG(FATAL) << "Unreachable type " << type;
443 }
444
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100445 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100446}
447
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100448void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100449 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100450 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100451
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000452 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100453 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000454
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100455 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100456 blocked_core_registers_[RBX] = true;
457 blocked_core_registers_[RBP] = true;
458 blocked_core_registers_[R12] = true;
459 blocked_core_registers_[R13] = true;
460 blocked_core_registers_[R14] = true;
461 blocked_core_registers_[R15] = true;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100462
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100463 blocked_fpu_registers_[XMM12] = true;
464 blocked_fpu_registers_[XMM13] = true;
465 blocked_fpu_registers_[XMM14] = true;
466 blocked_fpu_registers_[XMM15] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100467}
468
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100469void CodeGeneratorX86_64::GenerateFrameEntry() {
470 // Create a fake register to mimic Quick.
471 static const int kFakeReturnRegister = 16;
472 core_spill_mask_ |= (1 << kFakeReturnRegister);
473
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100474 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700475 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100476
477 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
478 __ testq(CpuRegister(RAX), Address(
479 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100480 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100481 }
482
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100483 // The return PC has already been pushed on the stack.
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100484 __ subq(CpuRegister(RSP),
485 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
486
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100487 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100488 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86_64();
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100489 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100490
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100491 __ gs()->cmpq(CpuRegister(RSP),
492 Address::Absolute(Thread::StackEndOffset<kX86_64WordSize>(), true));
493 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100494 }
495
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100496 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
497}
498
499void CodeGeneratorX86_64::GenerateFrameExit() {
500 __ addq(CpuRegister(RSP),
501 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
502}
503
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100504void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
505 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100506}
507
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100508void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100509 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
510}
511
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100512Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
513 switch (load->GetType()) {
514 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100515 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100516 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
517 break;
518
519 case Primitive::kPrimInt:
520 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100521 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100522 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100523
524 case Primitive::kPrimBoolean:
525 case Primitive::kPrimByte:
526 case Primitive::kPrimChar:
527 case Primitive::kPrimShort:
528 case Primitive::kPrimVoid:
529 LOG(FATAL) << "Unexpected type " << load->GetType();
530 }
531
532 LOG(FATAL) << "Unreachable";
533 return Location();
534}
535
536void CodeGeneratorX86_64::Move(Location destination, Location source) {
537 if (source.Equals(destination)) {
538 return;
539 }
540 if (destination.IsRegister()) {
541 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100542 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100543 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100544 __ movd(destination.As<CpuRegister>(), source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100545 } else if (source.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100546 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100547 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100548 } else {
549 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100550 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100551 Address(CpuRegister(RSP), source.GetStackIndex()));
552 }
553 } else if (destination.IsFpuRegister()) {
554 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100555 __ movd(destination.As<XmmRegister>(), source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100556 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100557 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100558 } else if (source.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100559 __ movss(destination.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100560 Address(CpuRegister(RSP), source.GetStackIndex()));
561 } else {
562 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100563 __ movsd(destination.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100564 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100565 }
566 } else if (destination.IsStackSlot()) {
567 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100568 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100569 source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100570 } else if (source.IsFpuRegister()) {
571 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100572 source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100573 } else {
574 DCHECK(source.IsStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000575 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
576 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100577 }
578 } else {
579 DCHECK(destination.IsDoubleStackSlot());
580 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100581 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100582 source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100583 } else if (source.IsFpuRegister()) {
584 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100585 source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100586 } else {
587 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000588 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
589 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100590 }
591 }
592}
593
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100594void CodeGeneratorX86_64::Move(HInstruction* instruction,
595 Location location,
596 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000597 LocationSummary* locations = instruction->GetLocations();
598 if (locations != nullptr && locations->Out().Equals(location)) {
599 return;
600 }
601
602 if (locations != nullptr && locations->Out().IsConstant()) {
603 HConstant* const_to_move = locations->Out().GetConstant();
604 if (const_to_move->IsIntConstant()) {
605 Immediate imm(const_to_move->AsIntConstant()->GetValue());
606 if (location.IsRegister()) {
607 __ movl(location.As<CpuRegister>(), imm);
608 } else if (location.IsStackSlot()) {
609 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
610 } else {
611 DCHECK(location.IsConstant());
612 DCHECK_EQ(location.GetConstant(), const_to_move);
613 }
614 } else if (const_to_move->IsLongConstant()) {
615 int64_t value = const_to_move->AsLongConstant()->GetValue();
616 if (location.IsRegister()) {
617 __ movq(location.As<CpuRegister>(), Immediate(value));
618 } else if (location.IsDoubleStackSlot()) {
619 __ movq(CpuRegister(TMP), Immediate(value));
620 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
621 } else {
622 DCHECK(location.IsConstant());
623 DCHECK_EQ(location.GetConstant(), const_to_move);
624 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100625 }
Roland Levillain476df552014-10-09 17:51:36 +0100626 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100627 switch (instruction->GetType()) {
628 case Primitive::kPrimBoolean:
629 case Primitive::kPrimByte:
630 case Primitive::kPrimChar:
631 case Primitive::kPrimShort:
632 case Primitive::kPrimInt:
633 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100634 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100635 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
636 break;
637
638 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100639 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100640 Move(location, Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
641 break;
642
643 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100644 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100645 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000646 } else if (instruction->IsTemporary()) {
647 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
648 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100649 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100650 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100651 switch (instruction->GetType()) {
652 case Primitive::kPrimBoolean:
653 case Primitive::kPrimByte:
654 case Primitive::kPrimChar:
655 case Primitive::kPrimShort:
656 case Primitive::kPrimInt:
657 case Primitive::kPrimNot:
658 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100659 case Primitive::kPrimFloat:
660 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000661 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100662 break;
663
664 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100665 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100666 }
667 }
668}
669
670void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
671 got->SetLocations(nullptr);
672}
673
674void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
675 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100676 DCHECK(!successor->IsExitBlock());
677
678 HBasicBlock* block = got->GetBlock();
679 HInstruction* previous = got->GetPrevious();
680
681 HLoopInformation* info = block->GetLoopInformation();
682 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
683 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
684 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
685 return;
686 }
687
688 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
689 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
690 }
691 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100692 __ jmp(codegen_->GetLabelOf(successor));
693 }
694}
695
696void LocationsBuilderX86_64::VisitExit(HExit* exit) {
697 exit->SetLocations(nullptr);
698}
699
700void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700701 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100702 if (kIsDebugBuild) {
703 __ Comment("Unreachable");
704 __ int3();
705 }
706}
707
708void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100709 LocationSummary* locations =
710 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100711 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100712 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100713 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100714 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100715}
716
717void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700718 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100719 if (cond->IsIntConstant()) {
720 // Constant condition, statically compared against 1.
721 int32_t cond_value = cond->AsIntConstant()->GetValue();
722 if (cond_value == 1) {
723 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
724 if_instr->IfTrueSuccessor())) {
725 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100726 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100727 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100728 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100729 DCHECK_EQ(cond_value, 0);
730 }
731 } else {
732 bool materialized =
733 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
734 // Moves do not affect the eflags register, so if the condition is
735 // evaluated just before the if, we don't need to evaluate it
736 // again.
737 bool eflags_set = cond->IsCondition()
738 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
739 if (materialized) {
740 if (!eflags_set) {
741 // Materialized condition, compare against 0.
742 Location lhs = if_instr->GetLocations()->InAt(0);
743 if (lhs.IsRegister()) {
744 __ cmpl(lhs.As<CpuRegister>(), Immediate(0));
745 } else {
746 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
747 Immediate(0));
748 }
749 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
750 } else {
751 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
752 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
753 }
754 } else {
755 Location lhs = cond->GetLocations()->InAt(0);
756 Location rhs = cond->GetLocations()->InAt(1);
757 if (rhs.IsRegister()) {
758 __ cmpl(lhs.As<CpuRegister>(), rhs.As<CpuRegister>());
759 } else if (rhs.IsConstant()) {
760 __ cmpl(lhs.As<CpuRegister>(),
761 Immediate(rhs.GetConstant()->AsIntConstant()->GetValue()));
762 } else {
763 __ cmpl(lhs.As<CpuRegister>(),
764 Address(CpuRegister(RSP), rhs.GetStackIndex()));
765 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100766 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
767 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700768 }
Dave Allison20dfc792014-06-16 20:44:29 -0700769 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100770 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
771 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700772 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100773 }
774}
775
776void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
777 local->SetLocations(nullptr);
778}
779
780void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
781 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
782}
783
784void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
785 local->SetLocations(nullptr);
786}
787
788void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
789 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700790 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100791}
792
793void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100794 LocationSummary* locations =
795 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100796 switch (store->InputAt(1)->GetType()) {
797 case Primitive::kPrimBoolean:
798 case Primitive::kPrimByte:
799 case Primitive::kPrimChar:
800 case Primitive::kPrimShort:
801 case Primitive::kPrimInt:
802 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100803 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100804 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
805 break;
806
807 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100808 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100809 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
810 break;
811
812 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100813 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100814 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100815}
816
817void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700818 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100819}
820
Dave Allison20dfc792014-06-16 20:44:29 -0700821void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100822 LocationSummary* locations =
823 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100824 locations->SetInAt(0, Location::RequiresRegister());
825 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100826 if (comp->NeedsMaterialization()) {
827 locations->SetOut(Location::RequiresRegister());
828 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100829}
830
Dave Allison20dfc792014-06-16 20:44:29 -0700831void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
832 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100833 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100834 CpuRegister reg = locations->Out().As<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100835 // Clear register: setcc only sets the low byte.
836 __ xorq(reg, reg);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100837 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100838 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100839 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100840 } else if (locations->InAt(1).IsConstant()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100841 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100842 Immediate(locations->InAt(1).GetConstant()->AsIntConstant()->GetValue()));
843 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100844 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100845 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
846 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100847 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700848 }
849}
850
851void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
852 VisitCondition(comp);
853}
854
855void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
856 VisitCondition(comp);
857}
858
859void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
860 VisitCondition(comp);
861}
862
863void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
864 VisitCondition(comp);
865}
866
867void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
868 VisitCondition(comp);
869}
870
871void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
872 VisitCondition(comp);
873}
874
875void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
876 VisitCondition(comp);
877}
878
879void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
880 VisitCondition(comp);
881}
882
883void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
884 VisitCondition(comp);
885}
886
887void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
888 VisitCondition(comp);
889}
890
891void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
892 VisitCondition(comp);
893}
894
895void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
896 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100897}
898
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100899void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100900 LocationSummary* locations =
901 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100902 locations->SetInAt(0, Location::RequiresRegister());
903 locations->SetInAt(1, Location::RequiresRegister());
904 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100905}
906
907void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
908 Label greater, done;
909 LocationSummary* locations = compare->GetLocations();
910 switch (compare->InputAt(0)->GetType()) {
911 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100912 __ cmpq(locations->InAt(0).As<CpuRegister>(),
913 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100914 break;
915 default:
916 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
917 }
918
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100919 CpuRegister output = locations->Out().As<CpuRegister>();
920 __ movl(output, Immediate(0));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100921 __ j(kEqual, &done);
922 __ j(kGreater, &greater);
923
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100924 __ movl(output, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100925 __ jmp(&done);
926
927 __ Bind(&greater);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100928 __ movl(output, Immediate(1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100929
930 __ Bind(&done);
931}
932
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100933void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100934 LocationSummary* locations =
935 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100936 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100937}
938
939void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100940 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700941 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100942}
943
944void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100945 LocationSummary* locations =
946 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100947 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100948}
949
950void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100951 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700952 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100953}
954
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100955void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
956 LocationSummary* locations =
957 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
958 locations->SetOut(Location::ConstantLocation(constant));
959}
960
961void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
962 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700963 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100964}
965
966void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
967 LocationSummary* locations =
968 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
969 locations->SetOut(Location::ConstantLocation(constant));
970}
971
972void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
973 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700974 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100975}
976
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100977void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
978 ret->SetLocations(nullptr);
979}
980
981void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700982 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100983 codegen_->GenerateFrameExit();
984 __ ret();
985}
986
987void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100988 LocationSummary* locations =
989 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100990 switch (ret->InputAt(0)->GetType()) {
991 case Primitive::kPrimBoolean:
992 case Primitive::kPrimByte:
993 case Primitive::kPrimChar:
994 case Primitive::kPrimShort:
995 case Primitive::kPrimInt:
996 case Primitive::kPrimNot:
997 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100998 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100999 break;
1000
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001001 case Primitive::kPrimFloat:
1002 case Primitive::kPrimDouble:
1003 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001004 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001005 break;
1006
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001007 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001008 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001009 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001010}
1011
1012void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1013 if (kIsDebugBuild) {
1014 switch (ret->InputAt(0)->GetType()) {
1015 case Primitive::kPrimBoolean:
1016 case Primitive::kPrimByte:
1017 case Primitive::kPrimChar:
1018 case Primitive::kPrimShort:
1019 case Primitive::kPrimInt:
1020 case Primitive::kPrimNot:
1021 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001022 DCHECK_EQ(ret->GetLocations()->InAt(0).As<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001023 break;
1024
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001025 case Primitive::kPrimFloat:
1026 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001027 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001028 XMM0);
1029 break;
1030
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001031 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001032 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001033 }
1034 }
1035 codegen_->GenerateFrameExit();
1036 __ ret();
1037}
1038
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001039Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
1040 switch (type) {
1041 case Primitive::kPrimBoolean:
1042 case Primitive::kPrimByte:
1043 case Primitive::kPrimChar:
1044 case Primitive::kPrimShort:
1045 case Primitive::kPrimInt:
1046 case Primitive::kPrimNot: {
1047 uint32_t index = gp_index_++;
1048 stack_index_++;
1049 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001050 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001051 } else {
1052 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1053 }
1054 }
1055
1056 case Primitive::kPrimLong: {
1057 uint32_t index = gp_index_;
1058 stack_index_ += 2;
1059 if (index < calling_convention.GetNumberOfRegisters()) {
1060 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001061 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001062 } else {
1063 gp_index_ += 2;
1064 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1065 }
1066 }
1067
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001068 case Primitive::kPrimFloat: {
1069 uint32_t index = fp_index_++;
1070 stack_index_++;
1071 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001072 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001073 } else {
1074 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1075 }
1076 }
1077
1078 case Primitive::kPrimDouble: {
1079 uint32_t index = fp_index_++;
1080 stack_index_ += 2;
1081 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001082 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001083 } else {
1084 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1085 }
1086 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001087
1088 case Primitive::kPrimVoid:
1089 LOG(FATAL) << "Unexpected parameter type " << type;
1090 break;
1091 }
1092 return Location();
1093}
1094
1095void LocationsBuilderX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001096 HandleInvoke(invoke);
1097}
1098
1099void InstructionCodeGeneratorX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001100 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001101 // TODO: Implement all kinds of calls:
1102 // 1) boot -> boot
1103 // 2) app -> boot
1104 // 3) app -> app
1105 //
1106 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1107
1108 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001109 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001110 // temp = temp->dex_cache_resolved_methods_;
1111 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
1112 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001113 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001114 // (temp + offset_of_quick_compiled_code)()
1115 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
1116
1117 DCHECK(!codegen_->IsLeafMethod());
1118 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1119}
1120
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001121void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001122 LocationSummary* locations =
1123 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001124 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001125
1126 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001127 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001128 HInstruction* input = invoke->InputAt(i);
1129 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1130 }
1131
1132 switch (invoke->GetType()) {
1133 case Primitive::kPrimBoolean:
1134 case Primitive::kPrimByte:
1135 case Primitive::kPrimChar:
1136 case Primitive::kPrimShort:
1137 case Primitive::kPrimInt:
1138 case Primitive::kPrimNot:
1139 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001140 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001141 break;
1142
1143 case Primitive::kPrimVoid:
1144 break;
1145
1146 case Primitive::kPrimDouble:
1147 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001148 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001149 break;
1150 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001151}
1152
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001153void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1154 HandleInvoke(invoke);
1155}
1156
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001157void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001158 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001159 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1160 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1161 LocationSummary* locations = invoke->GetLocations();
1162 Location receiver = locations->InAt(0);
1163 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1164 // temp = object->GetClass();
1165 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001166 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1167 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001168 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001169 __ movl(temp, Address(receiver.As<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001170 }
1171 // temp = temp->GetMethodAt(method_offset);
1172 __ movl(temp, Address(temp, method_offset));
1173 // call temp->GetEntryPoint();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001174 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
1175
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001176 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001177 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001178}
1179
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001180void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1181 HandleInvoke(invoke);
1182 // Add the hidden argument.
1183 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1184}
1185
1186void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1187 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1188 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
1189 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1190 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1191 LocationSummary* locations = invoke->GetLocations();
1192 Location receiver = locations->InAt(0);
1193 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1194
1195 // Set the hidden argument.
1196 __ movq(invoke->GetLocations()->GetTemp(1).As<CpuRegister>(),
1197 Immediate(invoke->GetDexMethodIndex()));
1198
1199 // temp = object->GetClass();
1200 if (receiver.IsStackSlot()) {
1201 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1202 __ movl(temp, Address(temp, class_offset));
1203 } else {
1204 __ movl(temp, Address(receiver.As<CpuRegister>(), class_offset));
1205 }
1206 // temp = temp->GetImtEntryAt(method_offset);
1207 __ movl(temp, Address(temp, method_offset));
1208 // call temp->GetEntryPoint();
1209 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
1210
1211 DCHECK(!codegen_->IsLeafMethod());
1212 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1213}
1214
Roland Levillain88cb1752014-10-20 16:36:47 +01001215void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1216 LocationSummary* locations =
1217 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1218 switch (neg->GetResultType()) {
1219 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001220 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001221 locations->SetInAt(0, Location::RequiresRegister());
1222 locations->SetOut(Location::SameAsFirstInput());
1223 break;
1224
Roland Levillain88cb1752014-10-20 16:36:47 +01001225 case Primitive::kPrimFloat:
1226 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001227 locations->SetInAt(0, Location::RequiresFpuRegister());
1228 // Output overlaps as we need a fresh (zero-initialized)
1229 // register to perform subtraction from zero.
1230 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001231 break;
1232
1233 default:
1234 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1235 }
1236}
1237
1238void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1239 LocationSummary* locations = neg->GetLocations();
1240 Location out = locations->Out();
1241 Location in = locations->InAt(0);
1242 switch (neg->GetResultType()) {
1243 case Primitive::kPrimInt:
1244 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001245 DCHECK(in.Equals(out));
Roland Levillain88cb1752014-10-20 16:36:47 +01001246 __ negl(out.As<CpuRegister>());
1247 break;
1248
1249 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001250 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001251 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001252 __ negq(out.As<CpuRegister>());
1253 break;
1254
Roland Levillain88cb1752014-10-20 16:36:47 +01001255 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001256 DCHECK(in.IsFpuRegister());
1257 DCHECK(out.IsFpuRegister());
1258 DCHECK(!in.Equals(out));
1259 // TODO: Instead of computing negation as a subtraction from
1260 // zero, implement it with an exclusive or with value 0x80000000
1261 // (mask for bit 31, representing the sign of a single-precision
1262 // floating-point number), fetched from a constant pool:
1263 //
1264 // xorps out, [RIP:...] // value at RIP is 0x80 00 00 00
1265
1266 // out = 0
1267 __ xorps(out.As<XmmRegister>(), out.As<XmmRegister>());
1268 // out = out - in
1269 __ subss(out.As<XmmRegister>(), in.As<XmmRegister>());
1270 break;
1271
Roland Levillain88cb1752014-10-20 16:36:47 +01001272 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001273 DCHECK(in.IsFpuRegister());
1274 DCHECK(out.IsFpuRegister());
1275 DCHECK(!in.Equals(out));
1276 // TODO: Instead of computing negation as a subtraction from
1277 // zero, implement it with an exclusive or with value
1278 // 0x8000000000000000 (mask for bit 63, representing the sign of
1279 // a double-precision floating-point number), fetched from a
1280 // constant pool:
1281 //
1282 // xorpd out, [RIP:...] // value at RIP is 0x80 00 00 00 00 00 00 00
1283
1284 // out = 0
1285 __ xorpd(out.As<XmmRegister>(), out.As<XmmRegister>());
1286 // out = out - in
1287 __ subsd(out.As<XmmRegister>(), in.As<XmmRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001288 break;
1289
1290 default:
1291 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1292 }
1293}
1294
Roland Levillaindff1f282014-11-05 14:15:05 +00001295void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1296 LocationSummary* locations =
1297 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1298 Primitive::Type result_type = conversion->GetResultType();
1299 Primitive::Type input_type = conversion->GetInputType();
1300 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001301 case Primitive::kPrimByte:
1302 switch (input_type) {
1303 case Primitive::kPrimShort:
1304 case Primitive::kPrimInt:
1305 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001306 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001307 locations->SetInAt(0, Location::Any());
1308 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1309 break;
1310
1311 default:
1312 LOG(FATAL) << "Unexpected type conversion from " << input_type
1313 << " to " << result_type;
1314 }
1315 break;
1316
Roland Levillain946e1432014-11-11 17:35:19 +00001317 case Primitive::kPrimInt:
1318 switch (input_type) {
1319 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001320 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001321 locations->SetInAt(0, Location::Any());
1322 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1323 break;
1324
1325 case Primitive::kPrimFloat:
1326 case Primitive::kPrimDouble:
1327 LOG(FATAL) << "Type conversion from " << input_type
1328 << " to " << result_type << " not yet implemented";
1329 break;
1330
1331 default:
1332 LOG(FATAL) << "Unexpected type conversion from " << input_type
1333 << " to " << result_type;
1334 }
1335 break;
1336
Roland Levillaindff1f282014-11-05 14:15:05 +00001337 case Primitive::kPrimLong:
1338 switch (input_type) {
1339 case Primitive::kPrimByte:
1340 case Primitive::kPrimShort:
1341 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001342 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001343 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001344 // TODO: We would benefit from a (to-be-implemented)
1345 // Location::RegisterOrStackSlot requirement for this input.
1346 locations->SetInAt(0, Location::RequiresRegister());
1347 locations->SetOut(Location::RequiresRegister());
1348 break;
1349
1350 case Primitive::kPrimFloat:
1351 case Primitive::kPrimDouble:
1352 LOG(FATAL) << "Type conversion from " << input_type << " to "
1353 << result_type << " not yet implemented";
1354 break;
1355
1356 default:
1357 LOG(FATAL) << "Unexpected type conversion from " << input_type
1358 << " to " << result_type;
1359 }
1360 break;
1361
Roland Levillain981e4542014-11-14 11:47:14 +00001362 case Primitive::kPrimChar:
1363 switch (input_type) {
1364 case Primitive::kPrimByte:
1365 case Primitive::kPrimShort:
1366 case Primitive::kPrimInt:
1367 case Primitive::kPrimChar:
1368 // Processing a Dex `int-to-char' instruction.
1369 locations->SetInAt(0, Location::Any());
1370 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1371 break;
1372
1373 default:
1374 LOG(FATAL) << "Unexpected type conversion from " << input_type
1375 << " to " << result_type;
1376 }
1377 break;
1378
Roland Levillaindff1f282014-11-05 14:15:05 +00001379 case Primitive::kPrimFloat:
1380 case Primitive::kPrimDouble:
1381 LOG(FATAL) << "Type conversion from " << input_type
1382 << " to " << result_type << " not yet implemented";
1383 break;
1384
1385 default:
1386 LOG(FATAL) << "Unexpected type conversion from " << input_type
1387 << " to " << result_type;
1388 }
1389}
1390
1391void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1392 LocationSummary* locations = conversion->GetLocations();
1393 Location out = locations->Out();
1394 Location in = locations->InAt(0);
1395 Primitive::Type result_type = conversion->GetResultType();
1396 Primitive::Type input_type = conversion->GetInputType();
1397 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001398 case Primitive::kPrimByte:
1399 switch (input_type) {
1400 case Primitive::kPrimShort:
1401 case Primitive::kPrimInt:
1402 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001403 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001404 if (in.IsRegister()) {
1405 __ movsxb(out.As<CpuRegister>(), in.As<CpuRegister>());
1406 } else if (in.IsStackSlot()) {
1407 __ movsxb(out.As<CpuRegister>(),
1408 Address(CpuRegister(RSP), in.GetStackIndex()));
1409 } else {
1410 DCHECK(in.GetConstant()->IsIntConstant());
1411 __ movl(out.As<CpuRegister>(),
1412 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1413 }
1414 break;
1415
1416 default:
1417 LOG(FATAL) << "Unexpected type conversion from " << input_type
1418 << " to " << result_type;
1419 }
1420 break;
1421
Roland Levillain946e1432014-11-11 17:35:19 +00001422 case Primitive::kPrimInt:
1423 switch (input_type) {
1424 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001425 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001426 if (in.IsRegister()) {
1427 __ movl(out.As<CpuRegister>(), in.As<CpuRegister>());
1428 } else if (in.IsDoubleStackSlot()) {
1429 __ movl(out.As<CpuRegister>(),
1430 Address(CpuRegister(RSP), in.GetStackIndex()));
1431 } else {
1432 DCHECK(in.IsConstant());
1433 DCHECK(in.GetConstant()->IsLongConstant());
1434 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
1435 __ movl(out.As<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1436 }
1437 break;
1438
1439 case Primitive::kPrimFloat:
1440 case Primitive::kPrimDouble:
1441 LOG(FATAL) << "Type conversion from " << input_type
1442 << " to " << result_type << " not yet implemented";
1443 break;
1444
1445 default:
1446 LOG(FATAL) << "Unexpected type conversion from " << input_type
1447 << " to " << result_type;
1448 }
1449 break;
1450
Roland Levillaindff1f282014-11-05 14:15:05 +00001451 case Primitive::kPrimLong:
1452 switch (input_type) {
1453 DCHECK(out.IsRegister());
1454 case Primitive::kPrimByte:
1455 case Primitive::kPrimShort:
1456 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001457 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001458 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001459 DCHECK(in.IsRegister());
1460 __ movsxd(out.As<CpuRegister>(), in.As<CpuRegister>());
1461 break;
1462
1463 case Primitive::kPrimFloat:
1464 case Primitive::kPrimDouble:
1465 LOG(FATAL) << "Type conversion from " << input_type << " to "
1466 << result_type << " not yet implemented";
1467 break;
1468
1469 default:
1470 LOG(FATAL) << "Unexpected type conversion from " << input_type
1471 << " to " << result_type;
1472 }
1473 break;
1474
Roland Levillain981e4542014-11-14 11:47:14 +00001475 case Primitive::kPrimChar:
1476 switch (input_type) {
1477 case Primitive::kPrimByte:
1478 case Primitive::kPrimShort:
1479 case Primitive::kPrimInt:
1480 case Primitive::kPrimChar:
1481 // Processing a Dex `int-to-char' instruction.
1482 if (in.IsRegister()) {
1483 __ movzxw(out.As<CpuRegister>(), in.As<CpuRegister>());
1484 } else if (in.IsStackSlot()) {
1485 __ movzxw(out.As<CpuRegister>(),
1486 Address(CpuRegister(RSP), in.GetStackIndex()));
1487 } else {
1488 DCHECK(in.GetConstant()->IsIntConstant());
1489 __ movl(out.As<CpuRegister>(),
1490 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1491 }
1492 break;
1493
1494 default:
1495 LOG(FATAL) << "Unexpected type conversion from " << input_type
1496 << " to " << result_type;
1497 }
1498 break;
1499
Roland Levillaindff1f282014-11-05 14:15:05 +00001500 case Primitive::kPrimFloat:
1501 case Primitive::kPrimDouble:
1502 LOG(FATAL) << "Type conversion from " << input_type
1503 << " to " << result_type << " not yet implemented";
1504 break;
1505
1506 default:
1507 LOG(FATAL) << "Unexpected type conversion from " << input_type
1508 << " to " << result_type;
1509 }
1510}
1511
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001512void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001513 LocationSummary* locations =
1514 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001515 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001516 case Primitive::kPrimInt: {
1517 locations->SetInAt(0, Location::RequiresRegister());
1518 locations->SetInAt(1, Location::Any());
1519 locations->SetOut(Location::SameAsFirstInput());
1520 break;
1521 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001522
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001523 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001524 locations->SetInAt(0, Location::RequiresRegister());
1525 locations->SetInAt(1, Location::RequiresRegister());
1526 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001527 break;
1528 }
1529
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001530 case Primitive::kPrimDouble:
1531 case Primitive::kPrimFloat: {
1532 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001533 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001534 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001535 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001536 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001537
1538 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001539 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001540 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001541}
1542
1543void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1544 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001545 Location first = locations->InAt(0);
1546 Location second = locations->InAt(1);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001547 DCHECK(first.Equals(locations->Out()));
Calin Juravle11351682014-10-23 15:38:15 +01001548
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001549 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001550 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001551 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001552 __ addl(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001553 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001554 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001555 __ addl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001556 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001557 __ addl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001558 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001559 break;
1560 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001561
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001562 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001563 __ addq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001564 break;
1565 }
1566
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001567 case Primitive::kPrimFloat: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001568 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001569 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001570 }
1571
1572 case Primitive::kPrimDouble: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001573 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001574 break;
1575 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001576
1577 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001578 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001579 }
1580}
1581
1582void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001583 LocationSummary* locations =
1584 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001585 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001586 case Primitive::kPrimInt: {
1587 locations->SetInAt(0, Location::RequiresRegister());
1588 locations->SetInAt(1, Location::Any());
1589 locations->SetOut(Location::SameAsFirstInput());
1590 break;
1591 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001592 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001593 locations->SetInAt(0, Location::RequiresRegister());
1594 locations->SetInAt(1, Location::RequiresRegister());
1595 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001596 break;
1597 }
Calin Juravle11351682014-10-23 15:38:15 +01001598 case Primitive::kPrimFloat:
1599 case Primitive::kPrimDouble: {
1600 locations->SetInAt(0, Location::RequiresFpuRegister());
1601 locations->SetInAt(1, Location::RequiresFpuRegister());
1602 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001603 break;
Calin Juravle11351682014-10-23 15:38:15 +01001604 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001605 default:
Calin Juravle11351682014-10-23 15:38:15 +01001606 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001607 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001608}
1609
1610void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1611 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001612 Location first = locations->InAt(0);
1613 Location second = locations->InAt(1);
1614 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001615 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001616 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001617 if (second.IsRegister()) {
1618 __ subl(first.As<CpuRegister>(), second.As<CpuRegister>());
1619 } else if (second.IsConstant()) {
1620 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1621 __ subl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001622 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001623 __ subl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001624 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001625 break;
1626 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001627 case Primitive::kPrimLong: {
Calin Juravle11351682014-10-23 15:38:15 +01001628 __ subq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001629 break;
1630 }
1631
Calin Juravle11351682014-10-23 15:38:15 +01001632 case Primitive::kPrimFloat: {
1633 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001634 break;
Calin Juravle11351682014-10-23 15:38:15 +01001635 }
1636
1637 case Primitive::kPrimDouble: {
1638 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1639 break;
1640 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001641
1642 default:
Calin Juravle11351682014-10-23 15:38:15 +01001643 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001644 }
1645}
1646
Calin Juravle34bacdf2014-10-07 20:23:36 +01001647void LocationsBuilderX86_64::VisitMul(HMul* mul) {
1648 LocationSummary* locations =
1649 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1650 switch (mul->GetResultType()) {
1651 case Primitive::kPrimInt: {
1652 locations->SetInAt(0, Location::RequiresRegister());
1653 locations->SetInAt(1, Location::Any());
1654 locations->SetOut(Location::SameAsFirstInput());
1655 break;
1656 }
1657 case Primitive::kPrimLong: {
1658 locations->SetInAt(0, Location::RequiresRegister());
1659 locations->SetInAt(1, Location::RequiresRegister());
1660 locations->SetOut(Location::SameAsFirstInput());
1661 break;
1662 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001663 case Primitive::kPrimFloat:
1664 case Primitive::kPrimDouble: {
1665 locations->SetInAt(0, Location::RequiresFpuRegister());
1666 locations->SetInAt(1, Location::RequiresFpuRegister());
1667 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001668 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001669 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001670
1671 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001672 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001673 }
1674}
1675
1676void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
1677 LocationSummary* locations = mul->GetLocations();
1678 Location first = locations->InAt(0);
1679 Location second = locations->InAt(1);
1680 DCHECK(first.Equals(locations->Out()));
1681 switch (mul->GetResultType()) {
1682 case Primitive::kPrimInt: {
1683 if (second.IsRegister()) {
1684 __ imull(first.As<CpuRegister>(), second.As<CpuRegister>());
1685 } else if (second.IsConstant()) {
1686 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1687 __ imull(first.As<CpuRegister>(), imm);
1688 } else {
1689 DCHECK(second.IsStackSlot());
1690 __ imull(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
1691 }
1692 break;
1693 }
1694 case Primitive::kPrimLong: {
1695 __ imulq(first.As<CpuRegister>(), second.As<CpuRegister>());
1696 break;
1697 }
1698
Calin Juravleb5bfa962014-10-21 18:02:24 +01001699 case Primitive::kPrimFloat: {
1700 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001701 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001702 }
1703
1704 case Primitive::kPrimDouble: {
1705 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1706 break;
1707 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001708
1709 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001710 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001711 }
1712}
1713
Calin Juravlebacfec32014-11-14 15:54:36 +00001714void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
1715 DCHECK(instruction->IsDiv() || instruction->IsRem());
1716 Primitive::Type type = instruction->GetResultType();
1717 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
1718
1719 bool is_div = instruction->IsDiv();
1720 LocationSummary* locations = instruction->GetLocations();
1721
1722 CpuRegister out_reg = locations->Out().As<CpuRegister>();
1723 CpuRegister second_reg = locations->InAt(1).As<CpuRegister>();
1724
1725 DCHECK_EQ(RAX, locations->InAt(0).As<CpuRegister>().AsRegister());
1726 DCHECK_EQ(is_div ? RAX : RDX, out_reg.AsRegister());
1727
1728 SlowPathCodeX86_64* slow_path =
1729 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
1730 out_reg.AsRegister(), type, is_div);
1731 codegen_->AddSlowPath(slow_path);
1732
1733 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
1734 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
1735 // so it's safe to just use negl instead of more complex comparisons.
1736
1737 __ cmpl(second_reg, Immediate(-1));
1738 __ j(kEqual, slow_path->GetEntryLabel());
1739
1740 if (type == Primitive::kPrimInt) {
1741 // edx:eax <- sign-extended of eax
1742 __ cdq();
1743 // eax = quotient, edx = remainder
1744 __ idivl(second_reg);
1745 } else {
1746 // rdx:rax <- sign-extended of rax
1747 __ cqo();
1748 // rax = quotient, rdx = remainder
1749 __ idivq(second_reg);
1750 }
1751
1752 __ Bind(slow_path->GetExitLabel());
1753}
1754
Calin Juravle7c4954d2014-10-28 16:57:40 +00001755void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
1756 LocationSummary* locations =
1757 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1758 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001759 case Primitive::kPrimInt:
1760 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00001761 locations->SetInAt(0, Location::RegisterLocation(RAX));
1762 locations->SetInAt(1, Location::RequiresRegister());
1763 locations->SetOut(Location::SameAsFirstInput());
1764 // Intel uses edx:eax as the dividend.
1765 locations->AddTemp(Location::RegisterLocation(RDX));
1766 break;
1767 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001768
Calin Juravle7c4954d2014-10-28 16:57:40 +00001769 case Primitive::kPrimFloat:
1770 case Primitive::kPrimDouble: {
1771 locations->SetInAt(0, Location::RequiresFpuRegister());
1772 locations->SetInAt(1, Location::RequiresFpuRegister());
1773 locations->SetOut(Location::SameAsFirstInput());
1774 break;
1775 }
1776
1777 default:
1778 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1779 }
1780}
1781
1782void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
1783 LocationSummary* locations = div->GetLocations();
1784 Location first = locations->InAt(0);
1785 Location second = locations->InAt(1);
1786 DCHECK(first.Equals(locations->Out()));
1787
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001788 Primitive::Type type = div->GetResultType();
1789 switch (type) {
1790 case Primitive::kPrimInt:
1791 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001792 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00001793 break;
1794 }
1795
Calin Juravle7c4954d2014-10-28 16:57:40 +00001796 case Primitive::kPrimFloat: {
1797 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1798 break;
1799 }
1800
1801 case Primitive::kPrimDouble: {
1802 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1803 break;
1804 }
1805
1806 default:
1807 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1808 }
1809}
1810
Calin Juravlebacfec32014-11-14 15:54:36 +00001811void LocationsBuilderX86_64::VisitRem(HRem* rem) {
1812 LocationSummary* locations =
1813 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
1814 switch (rem->GetResultType()) {
1815 case Primitive::kPrimInt:
1816 case Primitive::kPrimLong: {
1817 locations->SetInAt(0, Location::RegisterLocation(RAX));
1818 locations->SetInAt(1, Location::RequiresRegister());
1819 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
1820 locations->SetOut(Location::RegisterLocation(RDX));
1821 break;
1822 }
1823
1824 case Primitive::kPrimFloat:
1825 case Primitive::kPrimDouble: {
1826 LOG(FATAL) << "Unimplemented rem type " << rem->GetResultType();
1827 break;
1828 }
1829
1830 default:
1831 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
1832 }
1833}
1834
1835void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
1836 Primitive::Type type = rem->GetResultType();
1837 switch (type) {
1838 case Primitive::kPrimInt:
1839 case Primitive::kPrimLong: {
1840 GenerateDivRemIntegral(rem);
1841 break;
1842 }
1843
1844 case Primitive::kPrimFloat:
1845 case Primitive::kPrimDouble: {
1846 LOG(FATAL) << "Unimplemented rem type " << rem->GetResultType();
1847 break;
1848 }
1849
1850 default:
1851 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
1852 }
1853}
1854
Calin Juravled0d48522014-11-04 16:40:20 +00001855void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1856 LocationSummary* locations =
1857 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1858 locations->SetInAt(0, Location::Any());
1859 if (instruction->HasUses()) {
1860 locations->SetOut(Location::SameAsFirstInput());
1861 }
1862}
1863
1864void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1865 SlowPathCodeX86_64* slow_path =
1866 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
1867 codegen_->AddSlowPath(slow_path);
1868
1869 LocationSummary* locations = instruction->GetLocations();
1870 Location value = locations->InAt(0);
1871
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001872 switch (instruction->GetType()) {
1873 case Primitive::kPrimInt: {
1874 if (value.IsRegister()) {
1875 __ testl(value.As<CpuRegister>(), value.As<CpuRegister>());
1876 __ j(kEqual, slow_path->GetEntryLabel());
1877 } else if (value.IsStackSlot()) {
1878 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
1879 __ j(kEqual, slow_path->GetEntryLabel());
1880 } else {
1881 DCHECK(value.IsConstant()) << value;
1882 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
1883 __ jmp(slow_path->GetEntryLabel());
1884 }
1885 }
1886 break;
Calin Juravled0d48522014-11-04 16:40:20 +00001887 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001888 case Primitive::kPrimLong: {
1889 if (value.IsRegister()) {
1890 __ testq(value.As<CpuRegister>(), value.As<CpuRegister>());
1891 __ j(kEqual, slow_path->GetEntryLabel());
1892 } else if (value.IsDoubleStackSlot()) {
1893 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
1894 __ j(kEqual, slow_path->GetEntryLabel());
1895 } else {
1896 DCHECK(value.IsConstant()) << value;
1897 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
1898 __ jmp(slow_path->GetEntryLabel());
1899 }
1900 }
1901 break;
1902 }
1903 default:
1904 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00001905 }
Calin Juravled0d48522014-11-04 16:40:20 +00001906}
1907
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001908void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001909 LocationSummary* locations =
1910 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001911 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001912 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1913 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1914 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001915}
1916
1917void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
1918 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001919 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001920 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1921
1922 __ gs()->call(Address::Absolute(
1923 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
1924
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001925 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001926 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001927}
1928
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001929void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
1930 LocationSummary* locations =
1931 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1932 InvokeRuntimeCallingConvention calling_convention;
1933 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1934 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1935 locations->SetOut(Location::RegisterLocation(RAX));
1936 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1937}
1938
1939void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
1940 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001941 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001942 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1943
1944 __ gs()->call(Address::Absolute(
1945 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocArrayWithAccessCheck), true));
1946
1947 DCHECK(!codegen_->IsLeafMethod());
1948 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1949}
1950
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001951void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001952 LocationSummary* locations =
1953 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001954 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1955 if (location.IsStackSlot()) {
1956 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1957 } else if (location.IsDoubleStackSlot()) {
1958 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1959 }
1960 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001961}
1962
1963void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
1964 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001965 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001966}
1967
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001968void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001969 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001970 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001971 locations->SetInAt(0, Location::RequiresRegister());
1972 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001973}
1974
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001975void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
1976 LocationSummary* locations = not_->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001977 DCHECK_EQ(locations->InAt(0).As<CpuRegister>().AsRegister(),
1978 locations->Out().As<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001979 Location out = locations->Out();
1980 switch (not_->InputAt(0)->GetType()) {
1981 case Primitive::kPrimBoolean:
1982 __ xorq(out.As<CpuRegister>(), Immediate(1));
1983 break;
1984
1985 case Primitive::kPrimInt:
1986 __ notl(out.As<CpuRegister>());
1987 break;
1988
1989 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001990 __ notq(out.As<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001991 break;
1992
1993 default:
1994 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1995 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001996}
1997
1998void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001999 LocationSummary* locations =
2000 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002001 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2002 locations->SetInAt(i, Location::Any());
2003 }
2004 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002005}
2006
2007void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002008 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002009 LOG(FATAL) << "Unimplemented";
2010}
2011
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002012void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002013 LocationSummary* locations =
2014 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002015 Primitive::Type field_type = instruction->GetFieldType();
2016 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002017 locations->SetInAt(0, Location::RequiresRegister());
2018 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002019 if (is_object_type) {
2020 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002021 locations->AddTemp(Location::RequiresRegister());
2022 locations->AddTemp(Location::RequiresRegister());
2023 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002024}
2025
2026void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2027 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002028 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002029 size_t offset = instruction->GetFieldOffset().SizeValue();
Nicolas Geoffray39468442014-09-02 15:17:15 +01002030 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002031
2032 switch (field_type) {
2033 case Primitive::kPrimBoolean:
2034 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002035 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002036 __ movb(Address(obj, offset), value);
2037 break;
2038 }
2039
2040 case Primitive::kPrimShort:
2041 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002042 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002043 __ movw(Address(obj, offset), value);
2044 break;
2045 }
2046
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002047 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002048 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002049 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002050 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002051 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002052 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
2053 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002054 codegen_->MarkGCCard(temp, card, obj, value);
2055 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002056 break;
2057 }
2058
2059 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002060 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002061 __ movq(Address(obj, offset), value);
2062 break;
2063 }
2064
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002065 case Primitive::kPrimFloat: {
2066 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2067 __ movss(Address(obj, offset), value);
2068 break;
2069 }
2070
2071 case Primitive::kPrimDouble: {
2072 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2073 __ movsd(Address(obj, offset), value);
2074 break;
2075 }
2076
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002077 case Primitive::kPrimVoid:
2078 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002079 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002080 }
2081}
2082
2083void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002084 LocationSummary* locations =
2085 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002086 locations->SetInAt(0, Location::RequiresRegister());
2087 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002088}
2089
2090void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2091 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002092 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002093 size_t offset = instruction->GetFieldOffset().SizeValue();
2094
2095 switch (instruction->GetType()) {
2096 case Primitive::kPrimBoolean: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002097 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002098 __ movzxb(out, Address(obj, offset));
2099 break;
2100 }
2101
2102 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002103 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002104 __ movsxb(out, Address(obj, offset));
2105 break;
2106 }
2107
2108 case Primitive::kPrimShort: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002109 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002110 __ movsxw(out, Address(obj, offset));
2111 break;
2112 }
2113
2114 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002115 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002116 __ movzxw(out, Address(obj, offset));
2117 break;
2118 }
2119
2120 case Primitive::kPrimInt:
2121 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002122 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002123 __ movl(out, Address(obj, offset));
2124 break;
2125 }
2126
2127 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002128 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002129 __ movq(out, Address(obj, offset));
2130 break;
2131 }
2132
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002133 case Primitive::kPrimFloat: {
2134 XmmRegister out = locations->Out().As<XmmRegister>();
2135 __ movss(out, Address(obj, offset));
2136 break;
2137 }
2138
2139 case Primitive::kPrimDouble: {
2140 XmmRegister out = locations->Out().As<XmmRegister>();
2141 __ movsd(out, Address(obj, offset));
2142 break;
2143 }
2144
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002145 case Primitive::kPrimVoid:
2146 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002147 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002148 }
2149}
2150
2151void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002152 LocationSummary* locations =
2153 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002154 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002155 if (instruction->HasUses()) {
2156 locations->SetOut(Location::SameAsFirstInput());
2157 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002158}
2159
2160void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002161 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002162 codegen_->AddSlowPath(slow_path);
2163
2164 LocationSummary* locations = instruction->GetLocations();
2165 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002166
2167 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002168 __ cmpl(obj.As<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002169 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002170 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002171 } else {
2172 DCHECK(obj.IsConstant()) << obj;
2173 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2174 __ jmp(slow_path->GetEntryLabel());
2175 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002176 }
2177 __ j(kEqual, slow_path->GetEntryLabel());
2178}
2179
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002180void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002181 LocationSummary* locations =
2182 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002183 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002184 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002185 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2186 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002187}
2188
2189void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
2190 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002191 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002192 Location index = locations->InAt(1);
2193
2194 switch (instruction->GetType()) {
2195 case Primitive::kPrimBoolean: {
2196 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002197 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002198 if (index.IsConstant()) {
2199 __ movzxb(out, Address(obj,
2200 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2201 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002202 __ movzxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002203 }
2204 break;
2205 }
2206
2207 case Primitive::kPrimByte: {
2208 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002209 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002210 if (index.IsConstant()) {
2211 __ movsxb(out, Address(obj,
2212 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2213 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002214 __ movsxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002215 }
2216 break;
2217 }
2218
2219 case Primitive::kPrimShort: {
2220 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002221 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002222 if (index.IsConstant()) {
2223 __ movsxw(out, Address(obj,
2224 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2225 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002226 __ movsxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002227 }
2228 break;
2229 }
2230
2231 case Primitive::kPrimChar: {
2232 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002233 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002234 if (index.IsConstant()) {
2235 __ movzxw(out, Address(obj,
2236 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2237 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002238 __ movzxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002239 }
2240 break;
2241 }
2242
2243 case Primitive::kPrimInt:
2244 case Primitive::kPrimNot: {
2245 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
2246 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002247 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002248 if (index.IsConstant()) {
2249 __ movl(out, Address(obj,
2250 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2251 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002252 __ movl(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002253 }
2254 break;
2255 }
2256
2257 case Primitive::kPrimLong: {
2258 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002259 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002260 if (index.IsConstant()) {
2261 __ movq(out, Address(obj,
2262 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2263 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002264 __ movq(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002265 }
2266 break;
2267 }
2268
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002269 case Primitive::kPrimFloat: {
2270 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2271 XmmRegister out = locations->Out().As<XmmRegister>();
2272 if (index.IsConstant()) {
2273 __ movss(out, Address(obj,
2274 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2275 } else {
2276 __ movss(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
2277 }
2278 break;
2279 }
2280
2281 case Primitive::kPrimDouble: {
2282 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2283 XmmRegister out = locations->Out().As<XmmRegister>();
2284 if (index.IsConstant()) {
2285 __ movsd(out, Address(obj,
2286 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2287 } else {
2288 __ movsd(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
2289 }
2290 break;
2291 }
2292
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002293 case Primitive::kPrimVoid:
2294 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002295 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002296 }
2297}
2298
2299void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002300 Primitive::Type value_type = instruction->GetComponentType();
2301 bool is_object = value_type == Primitive::kPrimNot;
2302 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2303 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
2304 if (is_object) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002305 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002306 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2307 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2308 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002309 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002310 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002311 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002312 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2313 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002314 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002315 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002316 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
2317 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002318 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002319 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002320 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002321 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002322}
2323
2324void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
2325 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002326 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002327 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002328 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002329 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002330
2331 switch (value_type) {
2332 case Primitive::kPrimBoolean:
2333 case Primitive::kPrimByte: {
2334 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002335 if (index.IsConstant()) {
2336 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002337 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002338 __ movb(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002339 } else {
2340 __ movb(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2341 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002342 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002343 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002344 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
2345 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002346 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002347 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002348 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2349 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002350 }
2351 break;
2352 }
2353
2354 case Primitive::kPrimShort:
2355 case Primitive::kPrimChar: {
2356 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002357 if (index.IsConstant()) {
2358 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002359 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002360 __ movw(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002361 } else {
2362 __ movw(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2363 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002364 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002365 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002366 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
2367 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002368 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002369 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002370 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2371 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002372 }
2373 break;
2374 }
2375
2376 case Primitive::kPrimInt: {
2377 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002378 if (index.IsConstant()) {
2379 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002380 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002381 __ movl(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002382 } else {
2383 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2384 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002385 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002386 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002387 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2388 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002389 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002390 DCHECK(value.IsConstant()) << value;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002391 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002392 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2393 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002394 }
2395 break;
2396 }
2397
2398 case Primitive::kPrimNot: {
2399 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject), true));
2400 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002401 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002402 break;
2403 }
2404
2405 case Primitive::kPrimLong: {
2406 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002407 if (index.IsConstant()) {
2408 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002409 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002410 __ movq(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002411 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002412 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002413 __ movq(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
2414 value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002415 }
2416 break;
2417 }
2418
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002419 case Primitive::kPrimFloat: {
2420 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2421 if (index.IsConstant()) {
2422 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2423 DCHECK(value.IsFpuRegister());
2424 __ movss(Address(obj, offset), value.As<XmmRegister>());
2425 } else {
2426 DCHECK(value.IsFpuRegister());
2427 __ movss(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2428 value.As<XmmRegister>());
2429 }
2430 break;
2431 }
2432
2433 case Primitive::kPrimDouble: {
2434 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2435 if (index.IsConstant()) {
2436 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
2437 DCHECK(value.IsFpuRegister());
2438 __ movsd(Address(obj, offset), value.As<XmmRegister>());
2439 } else {
2440 DCHECK(value.IsFpuRegister());
2441 __ movsd(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
2442 value.As<XmmRegister>());
2443 }
2444 break;
2445 }
2446
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002447 case Primitive::kPrimVoid:
2448 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002449 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002450 }
2451}
2452
2453void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002454 LocationSummary* locations =
2455 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002456 locations->SetInAt(0, Location::RequiresRegister());
2457 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002458}
2459
2460void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
2461 LocationSummary* locations = instruction->GetLocations();
2462 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002463 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2464 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002465 __ movl(out, Address(obj, offset));
2466}
2467
2468void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002469 LocationSummary* locations =
2470 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002471 locations->SetInAt(0, Location::RequiresRegister());
2472 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002473 if (instruction->HasUses()) {
2474 locations->SetOut(Location::SameAsFirstInput());
2475 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002476}
2477
2478void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
2479 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002480 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002481 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002482 codegen_->AddSlowPath(slow_path);
2483
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002484 CpuRegister index = locations->InAt(0).As<CpuRegister>();
2485 CpuRegister length = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002486
2487 __ cmpl(index, length);
2488 __ j(kAboveEqual, slow_path->GetEntryLabel());
2489}
2490
2491void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
2492 CpuRegister card,
2493 CpuRegister object,
2494 CpuRegister value) {
2495 Label is_null;
2496 __ testl(value, value);
2497 __ j(kEqual, &is_null);
2498 __ gs()->movq(card, Address::Absolute(
2499 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
2500 __ movq(temp, object);
2501 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
2502 __ movb(Address(temp, card, TIMES_1, 0), card);
2503 __ Bind(&is_null);
2504}
2505
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002506void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
2507 temp->SetLocations(nullptr);
2508}
2509
2510void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
2511 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002512 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002513}
2514
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002515void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002516 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002517 LOG(FATAL) << "Unimplemented";
2518}
2519
2520void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002521 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2522}
2523
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002524void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
2525 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2526}
2527
2528void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002529 HBasicBlock* block = instruction->GetBlock();
2530 if (block->GetLoopInformation() != nullptr) {
2531 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2532 // The back edge will generate the suspend check.
2533 return;
2534 }
2535 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2536 // The goto will generate the suspend check.
2537 return;
2538 }
2539 GenerateSuspendCheck(instruction, nullptr);
2540}
2541
2542void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
2543 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002544 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002545 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002546 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002547 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002548 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002549 if (successor == nullptr) {
2550 __ j(kNotEqual, slow_path->GetEntryLabel());
2551 __ Bind(slow_path->GetReturnLabel());
2552 } else {
2553 __ j(kEqual, codegen_->GetLabelOf(successor));
2554 __ jmp(slow_path->GetEntryLabel());
2555 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002556}
2557
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002558X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
2559 return codegen_->GetAssembler();
2560}
2561
2562void ParallelMoveResolverX86_64::EmitMove(size_t index) {
2563 MoveOperands* move = moves_.Get(index);
2564 Location source = move->GetSource();
2565 Location destination = move->GetDestination();
2566
2567 if (source.IsRegister()) {
2568 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002569 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002570 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002571 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002572 source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002573 } else {
2574 DCHECK(destination.IsDoubleStackSlot());
2575 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002576 source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002577 }
2578 } else if (source.IsStackSlot()) {
2579 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002580 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002581 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002582 } else if (destination.IsFpuRegister()) {
2583 __ movss(destination.As<XmmRegister>(),
2584 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002585 } else {
2586 DCHECK(destination.IsStackSlot());
2587 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2588 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2589 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002590 } else if (source.IsDoubleStackSlot()) {
2591 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002592 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002593 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002594 } else if (destination.IsFpuRegister()) {
2595 __ movsd(destination.As<XmmRegister>(), Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002596 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01002597 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002598 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2599 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2600 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002601 } else if (source.IsConstant()) {
2602 HConstant* constant = source.GetConstant();
2603 if (constant->IsIntConstant()) {
2604 Immediate imm(constant->AsIntConstant()->GetValue());
2605 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002606 __ movl(destination.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002607 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002608 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002609 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2610 }
2611 } else if (constant->IsLongConstant()) {
2612 int64_t value = constant->AsLongConstant()->GetValue();
2613 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002614 __ movq(destination.As<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002615 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002616 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002617 __ movq(CpuRegister(TMP), Immediate(value));
2618 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2619 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002620 } else if (constant->IsFloatConstant()) {
2621 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
2622 if (destination.IsFpuRegister()) {
2623 __ movl(CpuRegister(TMP), imm);
2624 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2625 } else {
2626 DCHECK(destination.IsStackSlot()) << destination;
2627 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2628 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002629 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002630 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
2631 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
2632 if (destination.IsFpuRegister()) {
2633 __ movq(CpuRegister(TMP), imm);
2634 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2635 } else {
2636 DCHECK(destination.IsDoubleStackSlot()) << destination;
2637 __ movq(CpuRegister(TMP), imm);
2638 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2639 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002640 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002641 } else if (source.IsFpuRegister()) {
2642 if (destination.IsFpuRegister()) {
2643 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
2644 } else if (destination.IsStackSlot()) {
2645 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
2646 source.As<XmmRegister>());
2647 } else {
2648 DCHECK(destination.IsDoubleStackSlot());
2649 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
2650 source.As<XmmRegister>());
2651 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002652 }
2653}
2654
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002655void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002656 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002657 __ movl(Address(CpuRegister(RSP), mem), reg);
2658 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002659}
2660
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002661void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002662 ScratchRegisterScope ensure_scratch(
2663 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2664
2665 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2666 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2667 __ movl(CpuRegister(ensure_scratch.GetRegister()),
2668 Address(CpuRegister(RSP), mem2 + stack_offset));
2669 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2670 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
2671 CpuRegister(ensure_scratch.GetRegister()));
2672}
2673
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002674void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
2675 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2676 __ movq(Address(CpuRegister(RSP), mem), reg);
2677 __ movq(reg, CpuRegister(TMP));
2678}
2679
2680void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
2681 ScratchRegisterScope ensure_scratch(
2682 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2683
2684 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2685 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2686 __ movq(CpuRegister(ensure_scratch.GetRegister()),
2687 Address(CpuRegister(RSP), mem2 + stack_offset));
2688 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2689 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
2690 CpuRegister(ensure_scratch.GetRegister()));
2691}
2692
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002693void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
2694 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2695 __ movss(Address(CpuRegister(RSP), mem), reg);
2696 __ movd(reg, CpuRegister(TMP));
2697}
2698
2699void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
2700 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2701 __ movsd(Address(CpuRegister(RSP), mem), reg);
2702 __ movd(reg, CpuRegister(TMP));
2703}
2704
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002705void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
2706 MoveOperands* move = moves_.Get(index);
2707 Location source = move->GetSource();
2708 Location destination = move->GetDestination();
2709
2710 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002711 __ xchgq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002712 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002713 Exchange32(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002714 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002715 Exchange32(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002716 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002717 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
2718 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002719 Exchange64(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002720 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002721 Exchange64(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002722 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
2723 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002724 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
2725 __ movd(CpuRegister(TMP), source.As<XmmRegister>());
2726 __ movaps(source.As<XmmRegister>(), destination.As<XmmRegister>());
2727 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2728 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
2729 Exchange32(source.As<XmmRegister>(), destination.GetStackIndex());
2730 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
2731 Exchange32(destination.As<XmmRegister>(), source.GetStackIndex());
2732 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
2733 Exchange64(source.As<XmmRegister>(), destination.GetStackIndex());
2734 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
2735 Exchange64(destination.As<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002736 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002737 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002738 }
2739}
2740
2741
2742void ParallelMoveResolverX86_64::SpillScratch(int reg) {
2743 __ pushq(CpuRegister(reg));
2744}
2745
2746
2747void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
2748 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002749}
2750
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002751void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
2752 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
2753 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2754 Immediate(mirror::Class::kStatusInitialized));
2755 __ j(kLess, slow_path->GetEntryLabel());
2756 __ Bind(slow_path->GetExitLabel());
2757 // No need for memory fence, thanks to the X86_64 memory model.
2758}
2759
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002760void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002761 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2762 ? LocationSummary::kCallOnSlowPath
2763 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002764 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002765 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002766 locations->SetOut(Location::RequiresRegister());
2767}
2768
2769void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
2770 CpuRegister out = cls->GetLocations()->Out().As<CpuRegister>();
2771 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002772 DCHECK(!cls->CanCallRuntime());
2773 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002774 codegen_->LoadCurrentMethod(out);
2775 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2776 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002777 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002778 codegen_->LoadCurrentMethod(out);
2779 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2780 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002781 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
2782 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2783 codegen_->AddSlowPath(slow_path);
2784 __ testl(out, out);
2785 __ j(kEqual, slow_path->GetEntryLabel());
2786 if (cls->MustGenerateClinitCheck()) {
2787 GenerateClassInitializationCheck(slow_path, out);
2788 } else {
2789 __ Bind(slow_path->GetExitLabel());
2790 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002791 }
2792}
2793
2794void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
2795 LocationSummary* locations =
2796 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2797 locations->SetInAt(0, Location::RequiresRegister());
2798 if (check->HasUses()) {
2799 locations->SetOut(Location::SameAsFirstInput());
2800 }
2801}
2802
2803void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002804 // We assume the class to not be null.
2805 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
2806 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002807 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002808 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002809}
2810
2811void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2812 LocationSummary* locations =
2813 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2814 locations->SetInAt(0, Location::RequiresRegister());
2815 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2816}
2817
2818void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2819 LocationSummary* locations = instruction->GetLocations();
2820 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002821 size_t offset = instruction->GetFieldOffset().SizeValue();
2822
2823 switch (instruction->GetType()) {
2824 case Primitive::kPrimBoolean: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002825 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002826 __ movzxb(out, Address(cls, offset));
2827 break;
2828 }
2829
2830 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002831 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002832 __ movsxb(out, Address(cls, offset));
2833 break;
2834 }
2835
2836 case Primitive::kPrimShort: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002837 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002838 __ movsxw(out, Address(cls, offset));
2839 break;
2840 }
2841
2842 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002843 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002844 __ movzxw(out, Address(cls, offset));
2845 break;
2846 }
2847
2848 case Primitive::kPrimInt:
2849 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002850 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002851 __ movl(out, Address(cls, offset));
2852 break;
2853 }
2854
2855 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002856 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002857 __ movq(out, Address(cls, offset));
2858 break;
2859 }
2860
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002861 case Primitive::kPrimFloat: {
2862 XmmRegister out = locations->Out().As<XmmRegister>();
2863 __ movss(out, Address(cls, offset));
2864 break;
2865 }
2866
2867 case Primitive::kPrimDouble: {
2868 XmmRegister out = locations->Out().As<XmmRegister>();
2869 __ movsd(out, Address(cls, offset));
2870 break;
2871 }
2872
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002873 case Primitive::kPrimVoid:
2874 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2875 UNREACHABLE();
2876 }
2877}
2878
2879void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2880 LocationSummary* locations =
2881 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2882 Primitive::Type field_type = instruction->GetFieldType();
2883 bool is_object_type = field_type == Primitive::kPrimNot;
2884 locations->SetInAt(0, Location::RequiresRegister());
2885 locations->SetInAt(1, Location::RequiresRegister());
2886 if (is_object_type) {
2887 // Temporary registers for the write barrier.
2888 locations->AddTemp(Location::RequiresRegister());
2889 locations->AddTemp(Location::RequiresRegister());
2890 }
2891}
2892
2893void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2894 LocationSummary* locations = instruction->GetLocations();
2895 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002896 size_t offset = instruction->GetFieldOffset().SizeValue();
2897 Primitive::Type field_type = instruction->GetFieldType();
2898
2899 switch (field_type) {
2900 case Primitive::kPrimBoolean:
2901 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002902 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002903 __ movb(Address(cls, offset), value);
2904 break;
2905 }
2906
2907 case Primitive::kPrimShort:
2908 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002909 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002910 __ movw(Address(cls, offset), value);
2911 break;
2912 }
2913
2914 case Primitive::kPrimInt:
2915 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002916 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002917 __ movl(Address(cls, offset), value);
2918 if (field_type == Primitive::kPrimNot) {
2919 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
2920 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
2921 codegen_->MarkGCCard(temp, card, cls, value);
2922 }
2923 break;
2924 }
2925
2926 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002927 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002928 __ movq(Address(cls, offset), value);
2929 break;
2930 }
2931
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002932 case Primitive::kPrimFloat: {
2933 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2934 __ movss(Address(cls, offset), value);
2935 break;
2936 }
2937
2938 case Primitive::kPrimDouble: {
2939 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2940 __ movsd(Address(cls, offset), value);
2941 break;
2942 }
2943
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002944 case Primitive::kPrimVoid:
2945 LOG(FATAL) << "Unreachable type " << field_type;
2946 UNREACHABLE();
2947 }
2948}
2949
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002950void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
2951 LocationSummary* locations =
2952 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2953 locations->SetOut(Location::RequiresRegister());
2954}
2955
2956void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
2957 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
2958 codegen_->AddSlowPath(slow_path);
2959
2960 CpuRegister out = load->GetLocations()->Out().As<CpuRegister>();
2961 codegen_->LoadCurrentMethod(CpuRegister(out));
2962 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
2963 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2964 __ testl(out, out);
2965 __ j(kEqual, slow_path->GetEntryLabel());
2966 __ Bind(slow_path->GetExitLabel());
2967}
2968
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002969void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
2970 LocationSummary* locations =
2971 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2972 locations->SetOut(Location::RequiresRegister());
2973}
2974
2975void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
2976 Address address = Address::Absolute(
2977 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
2978 __ gs()->movl(load->GetLocations()->Out().As<CpuRegister>(), address);
2979 __ gs()->movl(address, Immediate(0));
2980}
2981
2982void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
2983 LocationSummary* locations =
2984 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2985 InvokeRuntimeCallingConvention calling_convention;
2986 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2987}
2988
2989void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
2990 __ gs()->call(
2991 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
2992 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2993}
2994
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002995void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002996 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
2997 ? LocationSummary::kNoCall
2998 : LocationSummary::kCallOnSlowPath;
2999 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3000 locations->SetInAt(0, Location::RequiresRegister());
3001 locations->SetInAt(1, Location::Any());
3002 locations->SetOut(Location::RequiresRegister());
3003}
3004
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003005void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003006 LocationSummary* locations = instruction->GetLocations();
3007 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
3008 Location cls = locations->InAt(1);
3009 CpuRegister out = locations->Out().As<CpuRegister>();
3010 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3011 Label done, zero;
3012 SlowPathCodeX86_64* slow_path = nullptr;
3013
3014 // Return 0 if `obj` is null.
3015 // TODO: avoid this check if we know obj is not null.
3016 __ testl(obj, obj);
3017 __ j(kEqual, &zero);
3018 // Compare the class of `obj` with `cls`.
3019 __ movl(out, Address(obj, class_offset));
3020 if (cls.IsRegister()) {
3021 __ cmpl(out, cls.As<CpuRegister>());
3022 } else {
3023 DCHECK(cls.IsStackSlot()) << cls;
3024 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
3025 }
3026 if (instruction->IsClassFinal()) {
3027 // Classes must be equal for the instanceof to succeed.
3028 __ j(kNotEqual, &zero);
3029 __ movl(out, Immediate(1));
3030 __ jmp(&done);
3031 } else {
3032 // If the classes are not equal, we go into a slow path.
3033 DCHECK(locations->OnlyCallsOnSlowPath());
3034 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003035 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003036 codegen_->AddSlowPath(slow_path);
3037 __ j(kNotEqual, slow_path->GetEntryLabel());
3038 __ movl(out, Immediate(1));
3039 __ jmp(&done);
3040 }
3041 __ Bind(&zero);
3042 __ movl(out, Immediate(0));
3043 if (slow_path != nullptr) {
3044 __ Bind(slow_path->GetExitLabel());
3045 }
3046 __ Bind(&done);
3047}
3048
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003049void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
3050 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3051 instruction, LocationSummary::kCallOnSlowPath);
3052 locations->SetInAt(0, Location::RequiresRegister());
3053 locations->SetInAt(1, Location::Any());
3054 locations->AddTemp(Location::RequiresRegister());
3055}
3056
3057void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
3058 LocationSummary* locations = instruction->GetLocations();
3059 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
3060 Location cls = locations->InAt(1);
3061 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
3062 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3063 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
3064 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3065 codegen_->AddSlowPath(slow_path);
3066
3067 // TODO: avoid this check if we know obj is not null.
3068 __ testl(obj, obj);
3069 __ j(kEqual, slow_path->GetExitLabel());
3070 // Compare the class of `obj` with `cls`.
3071 __ movl(temp, Address(obj, class_offset));
3072 if (cls.IsRegister()) {
3073 __ cmpl(temp, cls.As<CpuRegister>());
3074 } else {
3075 DCHECK(cls.IsStackSlot()) << cls;
3076 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
3077 }
3078 // Classes must be equal for the checkcast to succeed.
3079 __ j(kNotEqual, slow_path->GetEntryLabel());
3080 __ Bind(slow_path->GetExitLabel());
3081}
3082
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003083void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3084 LocationSummary* locations =
3085 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3086 InvokeRuntimeCallingConvention calling_convention;
3087 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3088}
3089
3090void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3091 __ gs()->call(Address::Absolute(instruction->IsEnter()
3092 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
3093 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
3094 true));
3095 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3096}
3097
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003098void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3099void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3100void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3101
3102void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3103 LocationSummary* locations =
3104 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3105 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3106 || instruction->GetResultType() == Primitive::kPrimLong);
3107 locations->SetInAt(0, Location::RequiresRegister());
3108 if (instruction->GetType() == Primitive::kPrimInt) {
3109 locations->SetInAt(1, Location::Any());
3110 } else {
3111 // Request a register to avoid loading a 64bits constant.
3112 locations->SetInAt(1, Location::RequiresRegister());
3113 }
3114 locations->SetOut(Location::SameAsFirstInput());
3115}
3116
3117void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
3118 HandleBitwiseOperation(instruction);
3119}
3120
3121void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
3122 HandleBitwiseOperation(instruction);
3123}
3124
3125void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
3126 HandleBitwiseOperation(instruction);
3127}
3128
3129void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3130 LocationSummary* locations = instruction->GetLocations();
3131 Location first = locations->InAt(0);
3132 Location second = locations->InAt(1);
3133 DCHECK(first.Equals(locations->Out()));
3134
3135 if (instruction->GetResultType() == Primitive::kPrimInt) {
3136 if (second.IsRegister()) {
3137 if (instruction->IsAnd()) {
3138 __ andl(first.As<CpuRegister>(), second.As<CpuRegister>());
3139 } else if (instruction->IsOr()) {
3140 __ orl(first.As<CpuRegister>(), second.As<CpuRegister>());
3141 } else {
3142 DCHECK(instruction->IsXor());
3143 __ xorl(first.As<CpuRegister>(), second.As<CpuRegister>());
3144 }
3145 } else if (second.IsConstant()) {
3146 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
3147 if (instruction->IsAnd()) {
3148 __ andl(first.As<CpuRegister>(), imm);
3149 } else if (instruction->IsOr()) {
3150 __ orl(first.As<CpuRegister>(), imm);
3151 } else {
3152 DCHECK(instruction->IsXor());
3153 __ xorl(first.As<CpuRegister>(), imm);
3154 }
3155 } else {
3156 Address address(CpuRegister(RSP), second.GetStackIndex());
3157 if (instruction->IsAnd()) {
3158 __ andl(first.As<CpuRegister>(), address);
3159 } else if (instruction->IsOr()) {
3160 __ orl(first.As<CpuRegister>(), address);
3161 } else {
3162 DCHECK(instruction->IsXor());
3163 __ xorl(first.As<CpuRegister>(), address);
3164 }
3165 }
3166 } else {
3167 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3168 if (instruction->IsAnd()) {
3169 __ andq(first.As<CpuRegister>(), second.As<CpuRegister>());
3170 } else if (instruction->IsOr()) {
3171 __ orq(first.As<CpuRegister>(), second.As<CpuRegister>());
3172 } else {
3173 DCHECK(instruction->IsXor());
3174 __ xorq(first.As<CpuRegister>(), second.As<CpuRegister>());
3175 }
3176 }
3177}
3178
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003179} // namespace x86_64
3180} // namespace art