blob: 63938f3fa8d5cffae0b5d50c4d805df5970ed297 [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);
Calin Juravle91debbc2014-11-26 19:01:09 +0000902 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) {
Calin Juravle91debbc2014-11-26 19:01:09 +0000908 Label greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100909 LocationSummary* locations = compare->GetLocations();
Calin Juravle91debbc2014-11-26 19:01:09 +0000910 switch (compare->InputAt(0)->GetType()) {
911 case Primitive::kPrimLong:
912 __ cmpq(locations->InAt(0).As<CpuRegister>(),
913 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100914 break;
915 default:
Calin Juravle91debbc2014-11-26 19:01:09 +0000916 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100917 }
Calin Juravlecea28ec2014-11-25 20:56:51 +0000918
Calin Juravle91debbc2014-11-26 19:01:09 +0000919 CpuRegister output = locations->Out().As<CpuRegister>();
920 __ movl(output, Immediate(0));
921 __ j(kEqual, &done);
922 __ j(kGreater, &greater);
923
924 __ movl(output, Immediate(-1));
Calin Juravlefd861242014-11-25 20:56:51 +0000925 __ jmp(&done);
926
Calin Juravle91debbc2014-11-26 19:01:09 +0000927 __ Bind(&greater);
928 __ 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)()
Mathieu Chartier2d721012014-11-10 11:08:06 -08001115 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001116 kX86_64WordSize).SizeValue()));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001117
1118 DCHECK(!codegen_->IsLeafMethod());
1119 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1120}
1121
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001122void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001123 LocationSummary* locations =
1124 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001125 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001126
1127 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001128 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001129 HInstruction* input = invoke->InputAt(i);
1130 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1131 }
1132
1133 switch (invoke->GetType()) {
1134 case Primitive::kPrimBoolean:
1135 case Primitive::kPrimByte:
1136 case Primitive::kPrimChar:
1137 case Primitive::kPrimShort:
1138 case Primitive::kPrimInt:
1139 case Primitive::kPrimNot:
1140 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001141 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001142 break;
1143
1144 case Primitive::kPrimVoid:
1145 break;
1146
1147 case Primitive::kPrimDouble:
1148 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001149 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001150 break;
1151 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001152}
1153
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001154void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1155 HandleInvoke(invoke);
1156}
1157
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001158void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001159 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001160 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1161 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1162 LocationSummary* locations = invoke->GetLocations();
1163 Location receiver = locations->InAt(0);
1164 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1165 // temp = object->GetClass();
1166 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001167 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1168 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001169 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001170 __ movl(temp, Address(receiver.As<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001171 }
1172 // temp = temp->GetMethodAt(method_offset);
1173 __ movl(temp, Address(temp, method_offset));
1174 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001175 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001176 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001177
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001178 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001179 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001180}
1181
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001182void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1183 HandleInvoke(invoke);
1184 // Add the hidden argument.
1185 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1186}
1187
1188void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1189 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1190 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
1191 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1192 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1193 LocationSummary* locations = invoke->GetLocations();
1194 Location receiver = locations->InAt(0);
1195 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1196
1197 // Set the hidden argument.
1198 __ movq(invoke->GetLocations()->GetTemp(1).As<CpuRegister>(),
1199 Immediate(invoke->GetDexMethodIndex()));
1200
1201 // temp = object->GetClass();
1202 if (receiver.IsStackSlot()) {
1203 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1204 __ movl(temp, Address(temp, class_offset));
1205 } else {
1206 __ movl(temp, Address(receiver.As<CpuRegister>(), class_offset));
1207 }
1208 // temp = temp->GetImtEntryAt(method_offset);
1209 __ movl(temp, Address(temp, method_offset));
1210 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001211 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001212 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001213
1214 DCHECK(!codegen_->IsLeafMethod());
1215 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1216}
1217
Roland Levillain88cb1752014-10-20 16:36:47 +01001218void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1219 LocationSummary* locations =
1220 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1221 switch (neg->GetResultType()) {
1222 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001223 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001224 locations->SetInAt(0, Location::RequiresRegister());
1225 locations->SetOut(Location::SameAsFirstInput());
1226 break;
1227
Roland Levillain88cb1752014-10-20 16:36:47 +01001228 case Primitive::kPrimFloat:
1229 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001230 locations->SetInAt(0, Location::RequiresFpuRegister());
1231 // Output overlaps as we need a fresh (zero-initialized)
1232 // register to perform subtraction from zero.
1233 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001234 break;
1235
1236 default:
1237 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1238 }
1239}
1240
1241void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1242 LocationSummary* locations = neg->GetLocations();
1243 Location out = locations->Out();
1244 Location in = locations->InAt(0);
1245 switch (neg->GetResultType()) {
1246 case Primitive::kPrimInt:
1247 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001248 DCHECK(in.Equals(out));
Roland Levillain88cb1752014-10-20 16:36:47 +01001249 __ negl(out.As<CpuRegister>());
1250 break;
1251
1252 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001253 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001254 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001255 __ negq(out.As<CpuRegister>());
1256 break;
1257
Roland Levillain88cb1752014-10-20 16:36:47 +01001258 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001259 DCHECK(in.IsFpuRegister());
1260 DCHECK(out.IsFpuRegister());
1261 DCHECK(!in.Equals(out));
1262 // TODO: Instead of computing negation as a subtraction from
1263 // zero, implement it with an exclusive or with value 0x80000000
1264 // (mask for bit 31, representing the sign of a single-precision
1265 // floating-point number), fetched from a constant pool:
1266 //
1267 // xorps out, [RIP:...] // value at RIP is 0x80 00 00 00
1268
1269 // out = 0
1270 __ xorps(out.As<XmmRegister>(), out.As<XmmRegister>());
1271 // out = out - in
1272 __ subss(out.As<XmmRegister>(), in.As<XmmRegister>());
1273 break;
1274
Roland Levillain88cb1752014-10-20 16:36:47 +01001275 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001276 DCHECK(in.IsFpuRegister());
1277 DCHECK(out.IsFpuRegister());
1278 DCHECK(!in.Equals(out));
1279 // TODO: Instead of computing negation as a subtraction from
1280 // zero, implement it with an exclusive or with value
1281 // 0x8000000000000000 (mask for bit 63, representing the sign of
1282 // a double-precision floating-point number), fetched from a
1283 // constant pool:
1284 //
1285 // xorpd out, [RIP:...] // value at RIP is 0x80 00 00 00 00 00 00 00
1286
1287 // out = 0
1288 __ xorpd(out.As<XmmRegister>(), out.As<XmmRegister>());
1289 // out = out - in
1290 __ subsd(out.As<XmmRegister>(), in.As<XmmRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001291 break;
1292
1293 default:
1294 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1295 }
1296}
1297
Roland Levillaindff1f282014-11-05 14:15:05 +00001298void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1299 LocationSummary* locations =
1300 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1301 Primitive::Type result_type = conversion->GetResultType();
1302 Primitive::Type input_type = conversion->GetInputType();
1303 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001304 case Primitive::kPrimByte:
1305 switch (input_type) {
1306 case Primitive::kPrimShort:
1307 case Primitive::kPrimInt:
1308 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001309 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001310 locations->SetInAt(0, Location::Any());
1311 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1312 break;
1313
1314 default:
1315 LOG(FATAL) << "Unexpected type conversion from " << input_type
1316 << " to " << result_type;
1317 }
1318 break;
1319
Roland Levillain01a8d712014-11-14 16:27:39 +00001320 case Primitive::kPrimShort:
1321 switch (input_type) {
1322 case Primitive::kPrimByte:
1323 case Primitive::kPrimInt:
1324 case Primitive::kPrimChar:
1325 // Processing a Dex `int-to-short' instruction.
1326 locations->SetInAt(0, Location::Any());
1327 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1328 break;
1329
1330 default:
1331 LOG(FATAL) << "Unexpected type conversion from " << input_type
1332 << " to " << result_type;
1333 }
1334 break;
1335
Roland Levillain946e1432014-11-11 17:35:19 +00001336 case Primitive::kPrimInt:
1337 switch (input_type) {
1338 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001339 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001340 locations->SetInAt(0, Location::Any());
1341 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1342 break;
1343
1344 case Primitive::kPrimFloat:
1345 case Primitive::kPrimDouble:
1346 LOG(FATAL) << "Type conversion from " << input_type
1347 << " to " << result_type << " not yet implemented";
1348 break;
1349
1350 default:
1351 LOG(FATAL) << "Unexpected type conversion from " << input_type
1352 << " to " << result_type;
1353 }
1354 break;
1355
Roland Levillaindff1f282014-11-05 14:15:05 +00001356 case Primitive::kPrimLong:
1357 switch (input_type) {
1358 case Primitive::kPrimByte:
1359 case Primitive::kPrimShort:
1360 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001361 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001362 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001363 // TODO: We would benefit from a (to-be-implemented)
1364 // Location::RegisterOrStackSlot requirement for this input.
1365 locations->SetInAt(0, Location::RequiresRegister());
1366 locations->SetOut(Location::RequiresRegister());
1367 break;
1368
1369 case Primitive::kPrimFloat:
1370 case Primitive::kPrimDouble:
1371 LOG(FATAL) << "Type conversion from " << input_type << " to "
1372 << result_type << " not yet implemented";
1373 break;
1374
1375 default:
1376 LOG(FATAL) << "Unexpected type conversion from " << input_type
1377 << " to " << result_type;
1378 }
1379 break;
1380
Roland Levillain981e4542014-11-14 11:47:14 +00001381 case Primitive::kPrimChar:
1382 switch (input_type) {
1383 case Primitive::kPrimByte:
1384 case Primitive::kPrimShort:
1385 case Primitive::kPrimInt:
1386 case Primitive::kPrimChar:
1387 // Processing a Dex `int-to-char' instruction.
1388 locations->SetInAt(0, Location::Any());
1389 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1390 break;
1391
1392 default:
1393 LOG(FATAL) << "Unexpected type conversion from " << input_type
1394 << " to " << result_type;
1395 }
1396 break;
1397
Roland Levillaindff1f282014-11-05 14:15:05 +00001398 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001399 switch (input_type) {
1400 case Primitive::kPrimByte:
1401 case Primitive::kPrimShort:
1402 case Primitive::kPrimInt:
1403 case Primitive::kPrimChar:
1404 // Processing a Dex `int-to-float' instruction.
1405 locations->SetInAt(0, Location::RequiresRegister());
1406 locations->SetOut(Location::RequiresFpuRegister());
1407 break;
1408
1409 case Primitive::kPrimLong:
1410 case Primitive::kPrimDouble:
1411 LOG(FATAL) << "Type conversion from " << input_type
1412 << " to " << result_type << " not yet implemented";
1413 break;
1414
1415 default:
1416 LOG(FATAL) << "Unexpected type conversion from " << input_type
1417 << " to " << result_type;
1418 };
1419 break;
1420
Roland Levillaindff1f282014-11-05 14:15:05 +00001421 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001422 switch (input_type) {
1423 case Primitive::kPrimByte:
1424 case Primitive::kPrimShort:
1425 case Primitive::kPrimInt:
1426 case Primitive::kPrimChar:
1427 // Processing a Dex `int-to-double' instruction.
1428 locations->SetInAt(0, Location::RequiresRegister());
1429 locations->SetOut(Location::RequiresFpuRegister());
1430 break;
1431
1432 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001433 // Processing a Dex `long-to-double' instruction.
1434 locations->SetInAt(0, Location::RequiresRegister());
1435 locations->SetOut(Location::RequiresFpuRegister());
1436 break;
1437
Roland Levillaincff13742014-11-17 14:32:17 +00001438 case Primitive::kPrimFloat:
1439 LOG(FATAL) << "Type conversion from " << input_type
1440 << " to " << result_type << " not yet implemented";
1441 break;
1442
1443 default:
1444 LOG(FATAL) << "Unexpected type conversion from " << input_type
1445 << " to " << result_type;
1446 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001447 break;
1448
1449 default:
1450 LOG(FATAL) << "Unexpected type conversion from " << input_type
1451 << " to " << result_type;
1452 }
1453}
1454
1455void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1456 LocationSummary* locations = conversion->GetLocations();
1457 Location out = locations->Out();
1458 Location in = locations->InAt(0);
1459 Primitive::Type result_type = conversion->GetResultType();
1460 Primitive::Type input_type = conversion->GetInputType();
1461 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001462 case Primitive::kPrimByte:
1463 switch (input_type) {
1464 case Primitive::kPrimShort:
1465 case Primitive::kPrimInt:
1466 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001467 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001468 if (in.IsRegister()) {
1469 __ movsxb(out.As<CpuRegister>(), in.As<CpuRegister>());
1470 } else if (in.IsStackSlot()) {
1471 __ movsxb(out.As<CpuRegister>(),
1472 Address(CpuRegister(RSP), in.GetStackIndex()));
1473 } else {
1474 DCHECK(in.GetConstant()->IsIntConstant());
1475 __ movl(out.As<CpuRegister>(),
1476 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1477 }
1478 break;
1479
1480 default:
1481 LOG(FATAL) << "Unexpected type conversion from " << input_type
1482 << " to " << result_type;
1483 }
1484 break;
1485
Roland Levillain01a8d712014-11-14 16:27:39 +00001486 case Primitive::kPrimShort:
1487 switch (input_type) {
1488 case Primitive::kPrimByte:
1489 case Primitive::kPrimInt:
1490 case Primitive::kPrimChar:
1491 // Processing a Dex `int-to-short' instruction.
1492 if (in.IsRegister()) {
1493 __ movsxw(out.As<CpuRegister>(), in.As<CpuRegister>());
1494 } else if (in.IsStackSlot()) {
1495 __ movsxw(out.As<CpuRegister>(),
1496 Address(CpuRegister(RSP), in.GetStackIndex()));
1497 } else {
1498 DCHECK(in.GetConstant()->IsIntConstant());
1499 __ movl(out.As<CpuRegister>(),
1500 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1501 }
1502 break;
1503
1504 default:
1505 LOG(FATAL) << "Unexpected type conversion from " << input_type
1506 << " to " << result_type;
1507 }
1508 break;
1509
Roland Levillain946e1432014-11-11 17:35:19 +00001510 case Primitive::kPrimInt:
1511 switch (input_type) {
1512 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001513 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001514 if (in.IsRegister()) {
1515 __ movl(out.As<CpuRegister>(), in.As<CpuRegister>());
1516 } else if (in.IsDoubleStackSlot()) {
1517 __ movl(out.As<CpuRegister>(),
1518 Address(CpuRegister(RSP), in.GetStackIndex()));
1519 } else {
1520 DCHECK(in.IsConstant());
1521 DCHECK(in.GetConstant()->IsLongConstant());
1522 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
1523 __ movl(out.As<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1524 }
1525 break;
1526
1527 case Primitive::kPrimFloat:
1528 case Primitive::kPrimDouble:
1529 LOG(FATAL) << "Type conversion from " << input_type
1530 << " to " << result_type << " not yet implemented";
1531 break;
1532
1533 default:
1534 LOG(FATAL) << "Unexpected type conversion from " << input_type
1535 << " to " << result_type;
1536 }
1537 break;
1538
Roland Levillaindff1f282014-11-05 14:15:05 +00001539 case Primitive::kPrimLong:
1540 switch (input_type) {
1541 DCHECK(out.IsRegister());
1542 case Primitive::kPrimByte:
1543 case Primitive::kPrimShort:
1544 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001545 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001546 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001547 DCHECK(in.IsRegister());
1548 __ movsxd(out.As<CpuRegister>(), in.As<CpuRegister>());
1549 break;
1550
1551 case Primitive::kPrimFloat:
1552 case Primitive::kPrimDouble:
1553 LOG(FATAL) << "Type conversion from " << input_type << " to "
1554 << result_type << " not yet implemented";
1555 break;
1556
1557 default:
1558 LOG(FATAL) << "Unexpected type conversion from " << input_type
1559 << " to " << result_type;
1560 }
1561 break;
1562
Roland Levillain981e4542014-11-14 11:47:14 +00001563 case Primitive::kPrimChar:
1564 switch (input_type) {
1565 case Primitive::kPrimByte:
1566 case Primitive::kPrimShort:
1567 case Primitive::kPrimInt:
1568 case Primitive::kPrimChar:
1569 // Processing a Dex `int-to-char' instruction.
1570 if (in.IsRegister()) {
1571 __ movzxw(out.As<CpuRegister>(), in.As<CpuRegister>());
1572 } else if (in.IsStackSlot()) {
1573 __ movzxw(out.As<CpuRegister>(),
1574 Address(CpuRegister(RSP), in.GetStackIndex()));
1575 } else {
1576 DCHECK(in.GetConstant()->IsIntConstant());
1577 __ movl(out.As<CpuRegister>(),
1578 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1579 }
1580 break;
1581
1582 default:
1583 LOG(FATAL) << "Unexpected type conversion from " << input_type
1584 << " to " << result_type;
1585 }
1586 break;
1587
Roland Levillaindff1f282014-11-05 14:15:05 +00001588 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001589 switch (input_type) {
1590 // Processing a Dex `int-to-float' instruction.
1591 case Primitive::kPrimByte:
1592 case Primitive::kPrimShort:
1593 case Primitive::kPrimInt:
1594 case Primitive::kPrimChar:
1595 __ cvtsi2ss(out.As<XmmRegister>(), in.As<CpuRegister>());
1596 break;
1597
1598 case Primitive::kPrimLong:
1599 case Primitive::kPrimDouble:
1600 LOG(FATAL) << "Type conversion from " << input_type
1601 << " to " << result_type << " not yet implemented";
1602 break;
1603
1604 default:
1605 LOG(FATAL) << "Unexpected type conversion from " << input_type
1606 << " to " << result_type;
1607 };
1608 break;
1609
Roland Levillaindff1f282014-11-05 14:15:05 +00001610 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001611 switch (input_type) {
1612 // Processing a Dex `int-to-double' instruction.
1613 case Primitive::kPrimByte:
1614 case Primitive::kPrimShort:
1615 case Primitive::kPrimInt:
1616 case Primitive::kPrimChar:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001617 __ cvtsi2sd(out.As<XmmRegister>(), in.As<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001618 break;
1619
1620 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001621 // Processing a Dex `long-to-double' instruction.
1622 __ cvtsi2sd(out.As<XmmRegister>(), in.As<CpuRegister>(), true);
1623 break;
1624
Roland Levillaincff13742014-11-17 14:32:17 +00001625 case Primitive::kPrimFloat:
1626 LOG(FATAL) << "Type conversion from " << input_type
1627 << " to " << result_type << " not yet implemented";
1628 break;
1629
1630 default:
1631 LOG(FATAL) << "Unexpected type conversion from " << input_type
1632 << " to " << result_type;
1633 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001634 break;
1635
1636 default:
1637 LOG(FATAL) << "Unexpected type conversion from " << input_type
1638 << " to " << result_type;
1639 }
1640}
1641
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001642void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001643 LocationSummary* locations =
1644 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001645 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001646 case Primitive::kPrimInt: {
1647 locations->SetInAt(0, Location::RequiresRegister());
1648 locations->SetInAt(1, Location::Any());
1649 locations->SetOut(Location::SameAsFirstInput());
1650 break;
1651 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001652
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001653 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001654 locations->SetInAt(0, Location::RequiresRegister());
1655 locations->SetInAt(1, Location::RequiresRegister());
1656 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001657 break;
1658 }
1659
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001660 case Primitive::kPrimDouble:
1661 case Primitive::kPrimFloat: {
1662 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001663 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001664 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001665 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001666 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001667
1668 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001669 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001670 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001671}
1672
1673void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1674 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001675 Location first = locations->InAt(0);
1676 Location second = locations->InAt(1);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001677 DCHECK(first.Equals(locations->Out()));
Calin Juravle11351682014-10-23 15:38:15 +01001678
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001679 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001680 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001681 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001682 __ addl(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001683 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001684 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001685 __ addl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001686 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001687 __ addl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001688 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001689 break;
1690 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001691
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001692 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001693 __ addq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001694 break;
1695 }
1696
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001697 case Primitive::kPrimFloat: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001698 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001699 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001700 }
1701
1702 case Primitive::kPrimDouble: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001703 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001704 break;
1705 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001706
1707 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001708 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001709 }
1710}
1711
1712void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001713 LocationSummary* locations =
1714 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001715 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001716 case Primitive::kPrimInt: {
1717 locations->SetInAt(0, Location::RequiresRegister());
1718 locations->SetInAt(1, Location::Any());
1719 locations->SetOut(Location::SameAsFirstInput());
1720 break;
1721 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001722 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001723 locations->SetInAt(0, Location::RequiresRegister());
1724 locations->SetInAt(1, Location::RequiresRegister());
1725 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001726 break;
1727 }
Calin Juravle11351682014-10-23 15:38:15 +01001728 case Primitive::kPrimFloat:
1729 case Primitive::kPrimDouble: {
1730 locations->SetInAt(0, Location::RequiresFpuRegister());
1731 locations->SetInAt(1, Location::RequiresFpuRegister());
1732 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001733 break;
Calin Juravle11351682014-10-23 15:38:15 +01001734 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001735 default:
Calin Juravle11351682014-10-23 15:38:15 +01001736 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001737 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001738}
1739
1740void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1741 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001742 Location first = locations->InAt(0);
1743 Location second = locations->InAt(1);
1744 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001745 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001746 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001747 if (second.IsRegister()) {
1748 __ subl(first.As<CpuRegister>(), second.As<CpuRegister>());
1749 } else if (second.IsConstant()) {
1750 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1751 __ subl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001752 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001753 __ subl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001754 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001755 break;
1756 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001757 case Primitive::kPrimLong: {
Calin Juravle11351682014-10-23 15:38:15 +01001758 __ subq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001759 break;
1760 }
1761
Calin Juravle11351682014-10-23 15:38:15 +01001762 case Primitive::kPrimFloat: {
1763 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001764 break;
Calin Juravle11351682014-10-23 15:38:15 +01001765 }
1766
1767 case Primitive::kPrimDouble: {
1768 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1769 break;
1770 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001771
1772 default:
Calin Juravle11351682014-10-23 15:38:15 +01001773 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001774 }
1775}
1776
Calin Juravle34bacdf2014-10-07 20:23:36 +01001777void LocationsBuilderX86_64::VisitMul(HMul* mul) {
1778 LocationSummary* locations =
1779 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1780 switch (mul->GetResultType()) {
1781 case Primitive::kPrimInt: {
1782 locations->SetInAt(0, Location::RequiresRegister());
1783 locations->SetInAt(1, Location::Any());
1784 locations->SetOut(Location::SameAsFirstInput());
1785 break;
1786 }
1787 case Primitive::kPrimLong: {
1788 locations->SetInAt(0, Location::RequiresRegister());
1789 locations->SetInAt(1, Location::RequiresRegister());
1790 locations->SetOut(Location::SameAsFirstInput());
1791 break;
1792 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001793 case Primitive::kPrimFloat:
1794 case Primitive::kPrimDouble: {
1795 locations->SetInAt(0, Location::RequiresFpuRegister());
1796 locations->SetInAt(1, Location::RequiresFpuRegister());
1797 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001798 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001799 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001800
1801 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001802 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001803 }
1804}
1805
1806void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
1807 LocationSummary* locations = mul->GetLocations();
1808 Location first = locations->InAt(0);
1809 Location second = locations->InAt(1);
1810 DCHECK(first.Equals(locations->Out()));
1811 switch (mul->GetResultType()) {
1812 case Primitive::kPrimInt: {
1813 if (second.IsRegister()) {
1814 __ imull(first.As<CpuRegister>(), second.As<CpuRegister>());
1815 } else if (second.IsConstant()) {
1816 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1817 __ imull(first.As<CpuRegister>(), imm);
1818 } else {
1819 DCHECK(second.IsStackSlot());
1820 __ imull(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
1821 }
1822 break;
1823 }
1824 case Primitive::kPrimLong: {
1825 __ imulq(first.As<CpuRegister>(), second.As<CpuRegister>());
1826 break;
1827 }
1828
Calin Juravleb5bfa962014-10-21 18:02:24 +01001829 case Primitive::kPrimFloat: {
1830 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001831 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001832 }
1833
1834 case Primitive::kPrimDouble: {
1835 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1836 break;
1837 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001838
1839 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001840 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001841 }
1842}
1843
Calin Juravlebacfec32014-11-14 15:54:36 +00001844void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
1845 DCHECK(instruction->IsDiv() || instruction->IsRem());
1846 Primitive::Type type = instruction->GetResultType();
1847 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
1848
1849 bool is_div = instruction->IsDiv();
1850 LocationSummary* locations = instruction->GetLocations();
1851
1852 CpuRegister out_reg = locations->Out().As<CpuRegister>();
1853 CpuRegister second_reg = locations->InAt(1).As<CpuRegister>();
1854
1855 DCHECK_EQ(RAX, locations->InAt(0).As<CpuRegister>().AsRegister());
1856 DCHECK_EQ(is_div ? RAX : RDX, out_reg.AsRegister());
1857
1858 SlowPathCodeX86_64* slow_path =
1859 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
1860 out_reg.AsRegister(), type, is_div);
1861 codegen_->AddSlowPath(slow_path);
1862
1863 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
1864 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
1865 // so it's safe to just use negl instead of more complex comparisons.
1866
1867 __ cmpl(second_reg, Immediate(-1));
1868 __ j(kEqual, slow_path->GetEntryLabel());
1869
1870 if (type == Primitive::kPrimInt) {
1871 // edx:eax <- sign-extended of eax
1872 __ cdq();
1873 // eax = quotient, edx = remainder
1874 __ idivl(second_reg);
1875 } else {
1876 // rdx:rax <- sign-extended of rax
1877 __ cqo();
1878 // rax = quotient, rdx = remainder
1879 __ idivq(second_reg);
1880 }
1881
1882 __ Bind(slow_path->GetExitLabel());
1883}
1884
Calin Juravle7c4954d2014-10-28 16:57:40 +00001885void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
1886 LocationSummary* locations =
1887 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1888 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001889 case Primitive::kPrimInt:
1890 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00001891 locations->SetInAt(0, Location::RegisterLocation(RAX));
1892 locations->SetInAt(1, Location::RequiresRegister());
1893 locations->SetOut(Location::SameAsFirstInput());
1894 // Intel uses edx:eax as the dividend.
1895 locations->AddTemp(Location::RegisterLocation(RDX));
1896 break;
1897 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001898
Calin Juravle7c4954d2014-10-28 16:57:40 +00001899 case Primitive::kPrimFloat:
1900 case Primitive::kPrimDouble: {
1901 locations->SetInAt(0, Location::RequiresFpuRegister());
1902 locations->SetInAt(1, Location::RequiresFpuRegister());
1903 locations->SetOut(Location::SameAsFirstInput());
1904 break;
1905 }
1906
1907 default:
1908 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1909 }
1910}
1911
1912void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
1913 LocationSummary* locations = div->GetLocations();
1914 Location first = locations->InAt(0);
1915 Location second = locations->InAt(1);
1916 DCHECK(first.Equals(locations->Out()));
1917
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001918 Primitive::Type type = div->GetResultType();
1919 switch (type) {
1920 case Primitive::kPrimInt:
1921 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001922 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00001923 break;
1924 }
1925
Calin Juravle7c4954d2014-10-28 16:57:40 +00001926 case Primitive::kPrimFloat: {
1927 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1928 break;
1929 }
1930
1931 case Primitive::kPrimDouble: {
1932 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1933 break;
1934 }
1935
1936 default:
1937 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1938 }
1939}
1940
Calin Juravlebacfec32014-11-14 15:54:36 +00001941void LocationsBuilderX86_64::VisitRem(HRem* rem) {
1942 LocationSummary* locations =
1943 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
1944 switch (rem->GetResultType()) {
1945 case Primitive::kPrimInt:
1946 case Primitive::kPrimLong: {
1947 locations->SetInAt(0, Location::RegisterLocation(RAX));
1948 locations->SetInAt(1, Location::RequiresRegister());
1949 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
1950 locations->SetOut(Location::RegisterLocation(RDX));
1951 break;
1952 }
1953
1954 case Primitive::kPrimFloat:
1955 case Primitive::kPrimDouble: {
1956 LOG(FATAL) << "Unimplemented rem type " << rem->GetResultType();
1957 break;
1958 }
1959
1960 default:
1961 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
1962 }
1963}
1964
1965void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
1966 Primitive::Type type = rem->GetResultType();
1967 switch (type) {
1968 case Primitive::kPrimInt:
1969 case Primitive::kPrimLong: {
1970 GenerateDivRemIntegral(rem);
1971 break;
1972 }
1973
1974 case Primitive::kPrimFloat:
1975 case Primitive::kPrimDouble: {
1976 LOG(FATAL) << "Unimplemented rem type " << rem->GetResultType();
1977 break;
1978 }
1979
1980 default:
1981 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
1982 }
1983}
1984
Calin Juravled0d48522014-11-04 16:40:20 +00001985void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1986 LocationSummary* locations =
1987 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1988 locations->SetInAt(0, Location::Any());
1989 if (instruction->HasUses()) {
1990 locations->SetOut(Location::SameAsFirstInput());
1991 }
1992}
1993
1994void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1995 SlowPathCodeX86_64* slow_path =
1996 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
1997 codegen_->AddSlowPath(slow_path);
1998
1999 LocationSummary* locations = instruction->GetLocations();
2000 Location value = locations->InAt(0);
2001
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002002 switch (instruction->GetType()) {
2003 case Primitive::kPrimInt: {
2004 if (value.IsRegister()) {
2005 __ testl(value.As<CpuRegister>(), value.As<CpuRegister>());
2006 __ j(kEqual, slow_path->GetEntryLabel());
2007 } else if (value.IsStackSlot()) {
2008 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2009 __ j(kEqual, slow_path->GetEntryLabel());
2010 } else {
2011 DCHECK(value.IsConstant()) << value;
2012 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2013 __ jmp(slow_path->GetEntryLabel());
2014 }
2015 }
2016 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002017 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002018 case Primitive::kPrimLong: {
2019 if (value.IsRegister()) {
2020 __ testq(value.As<CpuRegister>(), value.As<CpuRegister>());
2021 __ j(kEqual, slow_path->GetEntryLabel());
2022 } else if (value.IsDoubleStackSlot()) {
2023 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2024 __ j(kEqual, slow_path->GetEntryLabel());
2025 } else {
2026 DCHECK(value.IsConstant()) << value;
2027 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2028 __ jmp(slow_path->GetEntryLabel());
2029 }
2030 }
2031 break;
2032 }
2033 default:
2034 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002035 }
Calin Juravled0d48522014-11-04 16:40:20 +00002036}
2037
Calin Juravle9aec02f2014-11-18 23:06:35 +00002038void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
2039 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2040
2041 LocationSummary* locations =
2042 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2043
2044 switch (op->GetResultType()) {
2045 case Primitive::kPrimInt:
2046 case Primitive::kPrimLong: {
2047 locations->SetInAt(0, Location::RequiresRegister());
2048 // The shift count needs to be in CL.
2049 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
2050 locations->SetOut(Location::SameAsFirstInput());
2051 break;
2052 }
2053 default:
2054 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2055 }
2056}
2057
2058void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
2059 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2060
2061 LocationSummary* locations = op->GetLocations();
2062 CpuRegister first_reg = locations->InAt(0).As<CpuRegister>();
2063 Location second = locations->InAt(1);
2064
2065 switch (op->GetResultType()) {
2066 case Primitive::kPrimInt: {
2067 if (second.IsRegister()) {
2068 CpuRegister second_reg = second.As<CpuRegister>();
2069 if (op->IsShl()) {
2070 __ shll(first_reg, second_reg);
2071 } else if (op->IsShr()) {
2072 __ sarl(first_reg, second_reg);
2073 } else {
2074 __ shrl(first_reg, second_reg);
2075 }
2076 } else {
2077 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
2078 if (op->IsShl()) {
2079 __ shll(first_reg, imm);
2080 } else if (op->IsShr()) {
2081 __ sarl(first_reg, imm);
2082 } else {
2083 __ shrl(first_reg, imm);
2084 }
2085 }
2086 break;
2087 }
2088 case Primitive::kPrimLong: {
2089 if (second.IsRegister()) {
2090 CpuRegister second_reg = second.As<CpuRegister>();
2091 if (op->IsShl()) {
2092 __ shlq(first_reg, second_reg);
2093 } else if (op->IsShr()) {
2094 __ sarq(first_reg, second_reg);
2095 } else {
2096 __ shrq(first_reg, second_reg);
2097 }
2098 } else {
2099 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
2100 if (op->IsShl()) {
2101 __ shlq(first_reg, imm);
2102 } else if (op->IsShr()) {
2103 __ sarq(first_reg, imm);
2104 } else {
2105 __ shrq(first_reg, imm);
2106 }
2107 }
2108 break;
2109 }
2110 default:
2111 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2112 }
2113}
2114
2115void LocationsBuilderX86_64::VisitShl(HShl* shl) {
2116 HandleShift(shl);
2117}
2118
2119void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
2120 HandleShift(shl);
2121}
2122
2123void LocationsBuilderX86_64::VisitShr(HShr* shr) {
2124 HandleShift(shr);
2125}
2126
2127void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
2128 HandleShift(shr);
2129}
2130
2131void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
2132 HandleShift(ushr);
2133}
2134
2135void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
2136 HandleShift(ushr);
2137}
2138
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002139void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002140 LocationSummary* locations =
2141 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002142 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002143 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2144 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2145 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002146}
2147
2148void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
2149 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002150 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002151 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2152
2153 __ gs()->call(Address::Absolute(
2154 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
2155
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002156 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002157 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002158}
2159
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002160void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
2161 LocationSummary* locations =
2162 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2163 InvokeRuntimeCallingConvention calling_convention;
2164 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2165 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2166 locations->SetOut(Location::RegisterLocation(RAX));
2167 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2168}
2169
2170void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
2171 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002172 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002173 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2174
2175 __ gs()->call(Address::Absolute(
2176 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocArrayWithAccessCheck), true));
2177
2178 DCHECK(!codegen_->IsLeafMethod());
2179 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2180}
2181
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002182void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002183 LocationSummary* locations =
2184 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002185 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2186 if (location.IsStackSlot()) {
2187 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2188 } else if (location.IsDoubleStackSlot()) {
2189 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2190 }
2191 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002192}
2193
2194void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
2195 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002196 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002197}
2198
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002199void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002200 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002201 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002202 locations->SetInAt(0, Location::RequiresRegister());
2203 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002204}
2205
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002206void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
2207 LocationSummary* locations = not_->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002208 DCHECK_EQ(locations->InAt(0).As<CpuRegister>().AsRegister(),
2209 locations->Out().As<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002210 Location out = locations->Out();
2211 switch (not_->InputAt(0)->GetType()) {
2212 case Primitive::kPrimBoolean:
2213 __ xorq(out.As<CpuRegister>(), Immediate(1));
2214 break;
2215
2216 case Primitive::kPrimInt:
2217 __ notl(out.As<CpuRegister>());
2218 break;
2219
2220 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002221 __ notq(out.As<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002222 break;
2223
2224 default:
2225 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2226 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002227}
2228
2229void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002230 LocationSummary* locations =
2231 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002232 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2233 locations->SetInAt(i, Location::Any());
2234 }
2235 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002236}
2237
2238void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002239 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002240 LOG(FATAL) << "Unimplemented";
2241}
2242
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002243void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002244 LocationSummary* locations =
2245 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002246 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002247 bool needs_write_barrier =
2248 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->GetValue());
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002249 locations->SetInAt(0, Location::RequiresRegister());
2250 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002251 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002252 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002253 locations->AddTemp(Location::RequiresRegister());
2254 locations->AddTemp(Location::RequiresRegister());
2255 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002256}
2257
2258void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2259 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002260 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002261 size_t offset = instruction->GetFieldOffset().SizeValue();
Nicolas Geoffray39468442014-09-02 15:17:15 +01002262 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002263
2264 switch (field_type) {
2265 case Primitive::kPrimBoolean:
2266 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002267 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002268 __ movb(Address(obj, offset), value);
2269 break;
2270 }
2271
2272 case Primitive::kPrimShort:
2273 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002274 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002275 __ movw(Address(obj, offset), value);
2276 break;
2277 }
2278
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002279 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002280 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002281 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002282 __ movl(Address(obj, offset), value);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002283 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->GetValue())) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002284 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
2285 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002286 codegen_->MarkGCCard(temp, card, obj, value);
2287 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002288 break;
2289 }
2290
2291 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002292 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002293 __ movq(Address(obj, offset), value);
2294 break;
2295 }
2296
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002297 case Primitive::kPrimFloat: {
2298 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2299 __ movss(Address(obj, offset), value);
2300 break;
2301 }
2302
2303 case Primitive::kPrimDouble: {
2304 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2305 __ movsd(Address(obj, offset), value);
2306 break;
2307 }
2308
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002309 case Primitive::kPrimVoid:
2310 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002311 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002312 }
2313}
2314
2315void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002316 LocationSummary* locations =
2317 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002318 locations->SetInAt(0, Location::RequiresRegister());
2319 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002320}
2321
2322void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2323 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002324 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002325 size_t offset = instruction->GetFieldOffset().SizeValue();
2326
2327 switch (instruction->GetType()) {
2328 case Primitive::kPrimBoolean: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002329 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002330 __ movzxb(out, Address(obj, offset));
2331 break;
2332 }
2333
2334 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002335 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002336 __ movsxb(out, Address(obj, offset));
2337 break;
2338 }
2339
2340 case Primitive::kPrimShort: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002341 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002342 __ movsxw(out, Address(obj, offset));
2343 break;
2344 }
2345
2346 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002347 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002348 __ movzxw(out, Address(obj, offset));
2349 break;
2350 }
2351
2352 case Primitive::kPrimInt:
2353 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002354 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002355 __ movl(out, Address(obj, offset));
2356 break;
2357 }
2358
2359 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002360 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002361 __ movq(out, Address(obj, offset));
2362 break;
2363 }
2364
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002365 case Primitive::kPrimFloat: {
2366 XmmRegister out = locations->Out().As<XmmRegister>();
2367 __ movss(out, Address(obj, offset));
2368 break;
2369 }
2370
2371 case Primitive::kPrimDouble: {
2372 XmmRegister out = locations->Out().As<XmmRegister>();
2373 __ movsd(out, Address(obj, offset));
2374 break;
2375 }
2376
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002377 case Primitive::kPrimVoid:
2378 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002379 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002380 }
2381}
2382
2383void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002384 LocationSummary* locations =
2385 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002386 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002387 if (instruction->HasUses()) {
2388 locations->SetOut(Location::SameAsFirstInput());
2389 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002390}
2391
2392void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002393 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002394 codegen_->AddSlowPath(slow_path);
2395
2396 LocationSummary* locations = instruction->GetLocations();
2397 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002398
2399 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002400 __ cmpl(obj.As<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002401 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002402 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002403 } else {
2404 DCHECK(obj.IsConstant()) << obj;
2405 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2406 __ jmp(slow_path->GetEntryLabel());
2407 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002408 }
2409 __ j(kEqual, slow_path->GetEntryLabel());
2410}
2411
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002412void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002413 LocationSummary* locations =
2414 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002415 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002416 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002417 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2418 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002419}
2420
2421void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
2422 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002423 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002424 Location index = locations->InAt(1);
2425
2426 switch (instruction->GetType()) {
2427 case Primitive::kPrimBoolean: {
2428 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002429 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002430 if (index.IsConstant()) {
2431 __ movzxb(out, Address(obj,
2432 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2433 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002434 __ movzxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002435 }
2436 break;
2437 }
2438
2439 case Primitive::kPrimByte: {
2440 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002441 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002442 if (index.IsConstant()) {
2443 __ movsxb(out, Address(obj,
2444 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2445 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002446 __ movsxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002447 }
2448 break;
2449 }
2450
2451 case Primitive::kPrimShort: {
2452 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002453 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002454 if (index.IsConstant()) {
2455 __ movsxw(out, Address(obj,
2456 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2457 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002458 __ movsxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002459 }
2460 break;
2461 }
2462
2463 case Primitive::kPrimChar: {
2464 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002465 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002466 if (index.IsConstant()) {
2467 __ movzxw(out, Address(obj,
2468 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2469 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002470 __ movzxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002471 }
2472 break;
2473 }
2474
2475 case Primitive::kPrimInt:
2476 case Primitive::kPrimNot: {
2477 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
2478 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002479 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002480 if (index.IsConstant()) {
2481 __ movl(out, Address(obj,
2482 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2483 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002484 __ movl(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002485 }
2486 break;
2487 }
2488
2489 case Primitive::kPrimLong: {
2490 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002491 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002492 if (index.IsConstant()) {
2493 __ movq(out, Address(obj,
2494 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2495 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002496 __ movq(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002497 }
2498 break;
2499 }
2500
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002501 case Primitive::kPrimFloat: {
2502 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2503 XmmRegister out = locations->Out().As<XmmRegister>();
2504 if (index.IsConstant()) {
2505 __ movss(out, Address(obj,
2506 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2507 } else {
2508 __ movss(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
2509 }
2510 break;
2511 }
2512
2513 case Primitive::kPrimDouble: {
2514 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2515 XmmRegister out = locations->Out().As<XmmRegister>();
2516 if (index.IsConstant()) {
2517 __ movsd(out, Address(obj,
2518 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2519 } else {
2520 __ movsd(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
2521 }
2522 break;
2523 }
2524
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002525 case Primitive::kPrimVoid:
2526 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002527 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002528 }
2529}
2530
2531void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002532 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002533
2534 bool needs_write_barrier =
2535 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2536 bool needs_runtime_call = instruction->NeedsTypeCheck();
2537
Nicolas Geoffray39468442014-09-02 15:17:15 +01002538 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002539 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
2540 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002541 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002542 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2543 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2544 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002545 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002546 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002547 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002548 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2549 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002550 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002551 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002552 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
2553 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002554 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002555 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002556 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002557
2558 if (needs_write_barrier) {
2559 // Temporary registers for the write barrier.
2560 locations->AddTemp(Location::RequiresRegister());
2561 locations->AddTemp(Location::RequiresRegister());
2562 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002563 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002564}
2565
2566void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
2567 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002568 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002569 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002570 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002571 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002572 bool needs_runtime_call = locations->WillCall();
2573 bool needs_write_barrier =
2574 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002575
2576 switch (value_type) {
2577 case Primitive::kPrimBoolean:
2578 case Primitive::kPrimByte: {
2579 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002580 if (index.IsConstant()) {
2581 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002582 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002583 __ movb(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002584 } else {
2585 __ movb(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2586 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002587 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002588 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002589 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
2590 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002591 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002592 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002593 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2594 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002595 }
2596 break;
2597 }
2598
2599 case Primitive::kPrimShort:
2600 case Primitive::kPrimChar: {
2601 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002602 if (index.IsConstant()) {
2603 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002604 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002605 __ movw(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002606 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002607 DCHECK(value.IsConstant()) << value;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002608 __ movw(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2609 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002610 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002611 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002612 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002613 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
2614 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002615 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002616 DCHECK(value.IsConstant()) << value;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002617 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002618 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2619 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002620 }
2621 break;
2622 }
2623
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002624 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002625 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002626 if (!needs_runtime_call) {
2627 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2628 if (index.IsConstant()) {
2629 size_t offset =
2630 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2631 if (value.IsRegister()) {
2632 __ movl(Address(obj, offset), value.As<CpuRegister>());
2633 } else {
2634 DCHECK(value.IsConstant()) << value;
2635 __ movl(Address(obj, offset),
2636 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2637 }
2638 } else {
2639 DCHECK(index.IsRegister()) << index;
2640 if (value.IsRegister()) {
2641 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2642 value.As<CpuRegister>());
2643 } else {
2644 DCHECK(value.IsConstant()) << value;
2645 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2646 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2647 }
2648 }
2649
2650 if (needs_write_barrier) {
2651 DCHECK_EQ(value_type, Primitive::kPrimNot);
2652 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
2653 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
2654 codegen_->MarkGCCard(temp, card, obj, value.As<CpuRegister>());
2655 }
2656 } else {
2657 DCHECK_EQ(value_type, Primitive::kPrimNot);
2658 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject), true));
2659 DCHECK(!codegen_->IsLeafMethod());
2660 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2661 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002662 break;
2663 }
2664
2665 case Primitive::kPrimLong: {
2666 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002667 if (index.IsConstant()) {
2668 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002669 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002670 __ movq(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002671 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002672 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002673 __ movq(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
2674 value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002675 }
2676 break;
2677 }
2678
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002679 case Primitive::kPrimFloat: {
2680 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2681 if (index.IsConstant()) {
2682 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2683 DCHECK(value.IsFpuRegister());
2684 __ movss(Address(obj, offset), value.As<XmmRegister>());
2685 } else {
2686 DCHECK(value.IsFpuRegister());
2687 __ movss(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2688 value.As<XmmRegister>());
2689 }
2690 break;
2691 }
2692
2693 case Primitive::kPrimDouble: {
2694 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2695 if (index.IsConstant()) {
2696 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
2697 DCHECK(value.IsFpuRegister());
2698 __ movsd(Address(obj, offset), value.As<XmmRegister>());
2699 } else {
2700 DCHECK(value.IsFpuRegister());
2701 __ movsd(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
2702 value.As<XmmRegister>());
2703 }
2704 break;
2705 }
2706
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002707 case Primitive::kPrimVoid:
2708 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002709 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002710 }
2711}
2712
2713void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002714 LocationSummary* locations =
2715 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002716 locations->SetInAt(0, Location::RequiresRegister());
2717 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002718}
2719
2720void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
2721 LocationSummary* locations = instruction->GetLocations();
2722 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002723 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2724 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002725 __ movl(out, Address(obj, offset));
2726}
2727
2728void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002729 LocationSummary* locations =
2730 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002731 locations->SetInAt(0, Location::RequiresRegister());
2732 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002733 if (instruction->HasUses()) {
2734 locations->SetOut(Location::SameAsFirstInput());
2735 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002736}
2737
2738void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
2739 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002740 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002741 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002742 codegen_->AddSlowPath(slow_path);
2743
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002744 CpuRegister index = locations->InAt(0).As<CpuRegister>();
2745 CpuRegister length = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002746
2747 __ cmpl(index, length);
2748 __ j(kAboveEqual, slow_path->GetEntryLabel());
2749}
2750
2751void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
2752 CpuRegister card,
2753 CpuRegister object,
2754 CpuRegister value) {
2755 Label is_null;
2756 __ testl(value, value);
2757 __ j(kEqual, &is_null);
2758 __ gs()->movq(card, Address::Absolute(
2759 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
2760 __ movq(temp, object);
2761 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
2762 __ movb(Address(temp, card, TIMES_1, 0), card);
2763 __ Bind(&is_null);
2764}
2765
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002766void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
2767 temp->SetLocations(nullptr);
2768}
2769
2770void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
2771 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002772 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002773}
2774
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002775void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002776 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002777 LOG(FATAL) << "Unimplemented";
2778}
2779
2780void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002781 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2782}
2783
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002784void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
2785 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2786}
2787
2788void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002789 HBasicBlock* block = instruction->GetBlock();
2790 if (block->GetLoopInformation() != nullptr) {
2791 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2792 // The back edge will generate the suspend check.
2793 return;
2794 }
2795 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2796 // The goto will generate the suspend check.
2797 return;
2798 }
2799 GenerateSuspendCheck(instruction, nullptr);
2800}
2801
2802void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
2803 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002804 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002805 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002806 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002807 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002808 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002809 if (successor == nullptr) {
2810 __ j(kNotEqual, slow_path->GetEntryLabel());
2811 __ Bind(slow_path->GetReturnLabel());
2812 } else {
2813 __ j(kEqual, codegen_->GetLabelOf(successor));
2814 __ jmp(slow_path->GetEntryLabel());
2815 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002816}
2817
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002818X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
2819 return codegen_->GetAssembler();
2820}
2821
2822void ParallelMoveResolverX86_64::EmitMove(size_t index) {
2823 MoveOperands* move = moves_.Get(index);
2824 Location source = move->GetSource();
2825 Location destination = move->GetDestination();
2826
2827 if (source.IsRegister()) {
2828 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002829 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002830 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002831 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002832 source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002833 } else {
2834 DCHECK(destination.IsDoubleStackSlot());
2835 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002836 source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002837 }
2838 } else if (source.IsStackSlot()) {
2839 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002840 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002841 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002842 } else if (destination.IsFpuRegister()) {
2843 __ movss(destination.As<XmmRegister>(),
2844 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002845 } else {
2846 DCHECK(destination.IsStackSlot());
2847 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2848 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2849 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002850 } else if (source.IsDoubleStackSlot()) {
2851 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002852 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002853 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002854 } else if (destination.IsFpuRegister()) {
2855 __ movsd(destination.As<XmmRegister>(), Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002856 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01002857 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002858 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2859 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2860 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002861 } else if (source.IsConstant()) {
2862 HConstant* constant = source.GetConstant();
2863 if (constant->IsIntConstant()) {
2864 Immediate imm(constant->AsIntConstant()->GetValue());
2865 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002866 __ movl(destination.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002867 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002868 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002869 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2870 }
2871 } else if (constant->IsLongConstant()) {
2872 int64_t value = constant->AsLongConstant()->GetValue();
2873 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002874 __ movq(destination.As<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002875 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002876 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002877 __ movq(CpuRegister(TMP), Immediate(value));
2878 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2879 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002880 } else if (constant->IsFloatConstant()) {
2881 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
2882 if (destination.IsFpuRegister()) {
2883 __ movl(CpuRegister(TMP), imm);
2884 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2885 } else {
2886 DCHECK(destination.IsStackSlot()) << destination;
2887 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2888 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002889 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002890 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
2891 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
2892 if (destination.IsFpuRegister()) {
2893 __ movq(CpuRegister(TMP), imm);
2894 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2895 } else {
2896 DCHECK(destination.IsDoubleStackSlot()) << destination;
2897 __ movq(CpuRegister(TMP), imm);
2898 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2899 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002900 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002901 } else if (source.IsFpuRegister()) {
2902 if (destination.IsFpuRegister()) {
2903 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
2904 } else if (destination.IsStackSlot()) {
2905 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
2906 source.As<XmmRegister>());
2907 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00002908 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002909 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
2910 source.As<XmmRegister>());
2911 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002912 }
2913}
2914
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002915void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002916 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002917 __ movl(Address(CpuRegister(RSP), mem), reg);
2918 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002919}
2920
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002921void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002922 ScratchRegisterScope ensure_scratch(
2923 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2924
2925 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2926 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2927 __ movl(CpuRegister(ensure_scratch.GetRegister()),
2928 Address(CpuRegister(RSP), mem2 + stack_offset));
2929 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2930 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
2931 CpuRegister(ensure_scratch.GetRegister()));
2932}
2933
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002934void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
2935 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2936 __ movq(Address(CpuRegister(RSP), mem), reg);
2937 __ movq(reg, CpuRegister(TMP));
2938}
2939
2940void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
2941 ScratchRegisterScope ensure_scratch(
2942 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2943
2944 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2945 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2946 __ movq(CpuRegister(ensure_scratch.GetRegister()),
2947 Address(CpuRegister(RSP), mem2 + stack_offset));
2948 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2949 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
2950 CpuRegister(ensure_scratch.GetRegister()));
2951}
2952
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002953void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
2954 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2955 __ movss(Address(CpuRegister(RSP), mem), reg);
2956 __ movd(reg, CpuRegister(TMP));
2957}
2958
2959void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
2960 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2961 __ movsd(Address(CpuRegister(RSP), mem), reg);
2962 __ movd(reg, CpuRegister(TMP));
2963}
2964
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002965void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
2966 MoveOperands* move = moves_.Get(index);
2967 Location source = move->GetSource();
2968 Location destination = move->GetDestination();
2969
2970 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002971 __ xchgq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002972 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002973 Exchange32(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002974 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002975 Exchange32(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002976 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002977 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
2978 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002979 Exchange64(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002980 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002981 Exchange64(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002982 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
2983 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002984 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
2985 __ movd(CpuRegister(TMP), source.As<XmmRegister>());
2986 __ movaps(source.As<XmmRegister>(), destination.As<XmmRegister>());
2987 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2988 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
2989 Exchange32(source.As<XmmRegister>(), destination.GetStackIndex());
2990 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
2991 Exchange32(destination.As<XmmRegister>(), source.GetStackIndex());
2992 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
2993 Exchange64(source.As<XmmRegister>(), destination.GetStackIndex());
2994 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
2995 Exchange64(destination.As<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002996 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002997 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002998 }
2999}
3000
3001
3002void ParallelMoveResolverX86_64::SpillScratch(int reg) {
3003 __ pushq(CpuRegister(reg));
3004}
3005
3006
3007void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
3008 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003009}
3010
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003011void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
3012 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
3013 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3014 Immediate(mirror::Class::kStatusInitialized));
3015 __ j(kLess, slow_path->GetEntryLabel());
3016 __ Bind(slow_path->GetExitLabel());
3017 // No need for memory fence, thanks to the X86_64 memory model.
3018}
3019
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003020void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003021 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3022 ? LocationSummary::kCallOnSlowPath
3023 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003024 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003025 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003026 locations->SetOut(Location::RequiresRegister());
3027}
3028
3029void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
3030 CpuRegister out = cls->GetLocations()->Out().As<CpuRegister>();
3031 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003032 DCHECK(!cls->CanCallRuntime());
3033 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003034 codegen_->LoadCurrentMethod(out);
3035 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3036 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003037 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003038 codegen_->LoadCurrentMethod(out);
3039 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3040 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003041 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3042 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3043 codegen_->AddSlowPath(slow_path);
3044 __ testl(out, out);
3045 __ j(kEqual, slow_path->GetEntryLabel());
3046 if (cls->MustGenerateClinitCheck()) {
3047 GenerateClassInitializationCheck(slow_path, out);
3048 } else {
3049 __ Bind(slow_path->GetExitLabel());
3050 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003051 }
3052}
3053
3054void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
3055 LocationSummary* locations =
3056 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3057 locations->SetInAt(0, Location::RequiresRegister());
3058 if (check->HasUses()) {
3059 locations->SetOut(Location::SameAsFirstInput());
3060 }
3061}
3062
3063void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003064 // We assume the class to not be null.
3065 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3066 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003067 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003068 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003069}
3070
3071void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3072 LocationSummary* locations =
3073 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3074 locations->SetInAt(0, Location::RequiresRegister());
3075 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3076}
3077
3078void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3079 LocationSummary* locations = instruction->GetLocations();
3080 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003081 size_t offset = instruction->GetFieldOffset().SizeValue();
3082
3083 switch (instruction->GetType()) {
3084 case Primitive::kPrimBoolean: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003085 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003086 __ movzxb(out, Address(cls, offset));
3087 break;
3088 }
3089
3090 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003091 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003092 __ movsxb(out, Address(cls, offset));
3093 break;
3094 }
3095
3096 case Primitive::kPrimShort: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003097 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003098 __ movsxw(out, Address(cls, offset));
3099 break;
3100 }
3101
3102 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003103 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003104 __ movzxw(out, Address(cls, offset));
3105 break;
3106 }
3107
3108 case Primitive::kPrimInt:
3109 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003110 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003111 __ movl(out, Address(cls, offset));
3112 break;
3113 }
3114
3115 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003116 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003117 __ movq(out, Address(cls, offset));
3118 break;
3119 }
3120
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003121 case Primitive::kPrimFloat: {
3122 XmmRegister out = locations->Out().As<XmmRegister>();
3123 __ movss(out, Address(cls, offset));
3124 break;
3125 }
3126
3127 case Primitive::kPrimDouble: {
3128 XmmRegister out = locations->Out().As<XmmRegister>();
3129 __ movsd(out, Address(cls, offset));
3130 break;
3131 }
3132
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003133 case Primitive::kPrimVoid:
3134 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3135 UNREACHABLE();
3136 }
3137}
3138
3139void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3140 LocationSummary* locations =
3141 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3142 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003143 bool needs_write_barrier =
3144 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->GetValue());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003145 locations->SetInAt(0, Location::RequiresRegister());
3146 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003147 if (needs_write_barrier) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003148 // Temporary registers for the write barrier.
3149 locations->AddTemp(Location::RequiresRegister());
3150 locations->AddTemp(Location::RequiresRegister());
3151 }
3152}
3153
3154void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3155 LocationSummary* locations = instruction->GetLocations();
3156 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003157 size_t offset = instruction->GetFieldOffset().SizeValue();
3158 Primitive::Type field_type = instruction->GetFieldType();
3159
3160 switch (field_type) {
3161 case Primitive::kPrimBoolean:
3162 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003163 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003164 __ movb(Address(cls, offset), value);
3165 break;
3166 }
3167
3168 case Primitive::kPrimShort:
3169 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003170 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003171 __ movw(Address(cls, offset), value);
3172 break;
3173 }
3174
3175 case Primitive::kPrimInt:
3176 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003177 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003178 __ movl(Address(cls, offset), value);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003179 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->GetValue())) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003180 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
3181 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
3182 codegen_->MarkGCCard(temp, card, cls, value);
3183 }
3184 break;
3185 }
3186
3187 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003188 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003189 __ movq(Address(cls, offset), value);
3190 break;
3191 }
3192
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003193 case Primitive::kPrimFloat: {
3194 XmmRegister value = locations->InAt(1).As<XmmRegister>();
3195 __ movss(Address(cls, offset), value);
3196 break;
3197 }
3198
3199 case Primitive::kPrimDouble: {
3200 XmmRegister value = locations->InAt(1).As<XmmRegister>();
3201 __ movsd(Address(cls, offset), value);
3202 break;
3203 }
3204
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003205 case Primitive::kPrimVoid:
3206 LOG(FATAL) << "Unreachable type " << field_type;
3207 UNREACHABLE();
3208 }
3209}
3210
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003211void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
3212 LocationSummary* locations =
3213 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3214 locations->SetOut(Location::RequiresRegister());
3215}
3216
3217void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
3218 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
3219 codegen_->AddSlowPath(slow_path);
3220
3221 CpuRegister out = load->GetLocations()->Out().As<CpuRegister>();
3222 codegen_->LoadCurrentMethod(CpuRegister(out));
Mathieu Chartiereace4582014-11-24 18:29:54 -08003223 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3224 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003225 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3226 __ testl(out, out);
3227 __ j(kEqual, slow_path->GetEntryLabel());
3228 __ Bind(slow_path->GetExitLabel());
3229}
3230
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003231void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
3232 LocationSummary* locations =
3233 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3234 locations->SetOut(Location::RequiresRegister());
3235}
3236
3237void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
3238 Address address = Address::Absolute(
3239 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
3240 __ gs()->movl(load->GetLocations()->Out().As<CpuRegister>(), address);
3241 __ gs()->movl(address, Immediate(0));
3242}
3243
3244void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
3245 LocationSummary* locations =
3246 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3247 InvokeRuntimeCallingConvention calling_convention;
3248 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3249}
3250
3251void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
3252 __ gs()->call(
3253 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
3254 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3255}
3256
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003257void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003258 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3259 ? LocationSummary::kNoCall
3260 : LocationSummary::kCallOnSlowPath;
3261 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3262 locations->SetInAt(0, Location::RequiresRegister());
3263 locations->SetInAt(1, Location::Any());
3264 locations->SetOut(Location::RequiresRegister());
3265}
3266
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003267void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003268 LocationSummary* locations = instruction->GetLocations();
3269 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
3270 Location cls = locations->InAt(1);
3271 CpuRegister out = locations->Out().As<CpuRegister>();
3272 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3273 Label done, zero;
3274 SlowPathCodeX86_64* slow_path = nullptr;
3275
3276 // Return 0 if `obj` is null.
3277 // TODO: avoid this check if we know obj is not null.
3278 __ testl(obj, obj);
3279 __ j(kEqual, &zero);
3280 // Compare the class of `obj` with `cls`.
3281 __ movl(out, Address(obj, class_offset));
3282 if (cls.IsRegister()) {
3283 __ cmpl(out, cls.As<CpuRegister>());
3284 } else {
3285 DCHECK(cls.IsStackSlot()) << cls;
3286 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
3287 }
3288 if (instruction->IsClassFinal()) {
3289 // Classes must be equal for the instanceof to succeed.
3290 __ j(kNotEqual, &zero);
3291 __ movl(out, Immediate(1));
3292 __ jmp(&done);
3293 } else {
3294 // If the classes are not equal, we go into a slow path.
3295 DCHECK(locations->OnlyCallsOnSlowPath());
3296 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003297 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003298 codegen_->AddSlowPath(slow_path);
3299 __ j(kNotEqual, slow_path->GetEntryLabel());
3300 __ movl(out, Immediate(1));
3301 __ jmp(&done);
3302 }
3303 __ Bind(&zero);
3304 __ movl(out, Immediate(0));
3305 if (slow_path != nullptr) {
3306 __ Bind(slow_path->GetExitLabel());
3307 }
3308 __ Bind(&done);
3309}
3310
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003311void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
3312 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3313 instruction, LocationSummary::kCallOnSlowPath);
3314 locations->SetInAt(0, Location::RequiresRegister());
3315 locations->SetInAt(1, Location::Any());
3316 locations->AddTemp(Location::RequiresRegister());
3317}
3318
3319void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
3320 LocationSummary* locations = instruction->GetLocations();
3321 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
3322 Location cls = locations->InAt(1);
3323 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
3324 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3325 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
3326 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3327 codegen_->AddSlowPath(slow_path);
3328
3329 // TODO: avoid this check if we know obj is not null.
3330 __ testl(obj, obj);
3331 __ j(kEqual, slow_path->GetExitLabel());
3332 // Compare the class of `obj` with `cls`.
3333 __ movl(temp, Address(obj, class_offset));
3334 if (cls.IsRegister()) {
3335 __ cmpl(temp, cls.As<CpuRegister>());
3336 } else {
3337 DCHECK(cls.IsStackSlot()) << cls;
3338 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
3339 }
3340 // Classes must be equal for the checkcast to succeed.
3341 __ j(kNotEqual, slow_path->GetEntryLabel());
3342 __ Bind(slow_path->GetExitLabel());
3343}
3344
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003345void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3346 LocationSummary* locations =
3347 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3348 InvokeRuntimeCallingConvention calling_convention;
3349 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3350}
3351
3352void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3353 __ gs()->call(Address::Absolute(instruction->IsEnter()
3354 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
3355 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
3356 true));
3357 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3358}
3359
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003360void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3361void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3362void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3363
3364void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3365 LocationSummary* locations =
3366 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3367 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3368 || instruction->GetResultType() == Primitive::kPrimLong);
3369 locations->SetInAt(0, Location::RequiresRegister());
3370 if (instruction->GetType() == Primitive::kPrimInt) {
3371 locations->SetInAt(1, Location::Any());
3372 } else {
3373 // Request a register to avoid loading a 64bits constant.
3374 locations->SetInAt(1, Location::RequiresRegister());
3375 }
3376 locations->SetOut(Location::SameAsFirstInput());
3377}
3378
3379void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
3380 HandleBitwiseOperation(instruction);
3381}
3382
3383void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
3384 HandleBitwiseOperation(instruction);
3385}
3386
3387void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
3388 HandleBitwiseOperation(instruction);
3389}
3390
3391void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3392 LocationSummary* locations = instruction->GetLocations();
3393 Location first = locations->InAt(0);
3394 Location second = locations->InAt(1);
3395 DCHECK(first.Equals(locations->Out()));
3396
3397 if (instruction->GetResultType() == Primitive::kPrimInt) {
3398 if (second.IsRegister()) {
3399 if (instruction->IsAnd()) {
3400 __ andl(first.As<CpuRegister>(), second.As<CpuRegister>());
3401 } else if (instruction->IsOr()) {
3402 __ orl(first.As<CpuRegister>(), second.As<CpuRegister>());
3403 } else {
3404 DCHECK(instruction->IsXor());
3405 __ xorl(first.As<CpuRegister>(), second.As<CpuRegister>());
3406 }
3407 } else if (second.IsConstant()) {
3408 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
3409 if (instruction->IsAnd()) {
3410 __ andl(first.As<CpuRegister>(), imm);
3411 } else if (instruction->IsOr()) {
3412 __ orl(first.As<CpuRegister>(), imm);
3413 } else {
3414 DCHECK(instruction->IsXor());
3415 __ xorl(first.As<CpuRegister>(), imm);
3416 }
3417 } else {
3418 Address address(CpuRegister(RSP), second.GetStackIndex());
3419 if (instruction->IsAnd()) {
3420 __ andl(first.As<CpuRegister>(), address);
3421 } else if (instruction->IsOr()) {
3422 __ orl(first.As<CpuRegister>(), address);
3423 } else {
3424 DCHECK(instruction->IsXor());
3425 __ xorl(first.As<CpuRegister>(), address);
3426 }
3427 }
3428 } else {
3429 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3430 if (instruction->IsAnd()) {
3431 __ andq(first.As<CpuRegister>(), second.As<CpuRegister>());
3432 } else if (instruction->IsOr()) {
3433 __ orq(first.As<CpuRegister>(), second.As<CpuRegister>());
3434 } else {
3435 DCHECK(instruction->IsXor());
3436 __ xorq(first.As<CpuRegister>(), second.As<CpuRegister>());
3437 }
3438 }
3439}
3440
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003441} // namespace x86_64
3442} // namespace art