blob: f9e868501dc4ec01700c400d9def424cae5c0dc4 [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()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000542 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100543 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000544 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100545 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000546 __ movl(destination.AsRegister<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());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000550 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100551 Address(CpuRegister(RSP), source.GetStackIndex()));
552 }
553 } else if (destination.IsFpuRegister()) {
554 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000555 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100556 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000557 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100558 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000559 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100560 Address(CpuRegister(RSP), source.GetStackIndex()));
561 } else {
562 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000563 __ movsd(destination.AsFpuRegister<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()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000569 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100570 } else if (source.IsFpuRegister()) {
571 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000572 source.AsFpuRegister<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()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000582 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100583 } else if (source.IsFpuRegister()) {
584 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +0000585 source.AsFpuRegister<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()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000607 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000608 } 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()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000617 __ movq(location.AsRegister<CpuRegister>(), Immediate(value));
Calin Juravlea21f5982014-11-13 15:53:04 +0000618 } 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:
Roland Levillain199f3362014-11-27 17:15:16 +0000640 Move(location,
641 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100642 break;
643
644 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100645 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100646 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000647 } else if (instruction->IsTemporary()) {
648 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
649 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100650 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100651 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100652 switch (instruction->GetType()) {
653 case Primitive::kPrimBoolean:
654 case Primitive::kPrimByte:
655 case Primitive::kPrimChar:
656 case Primitive::kPrimShort:
657 case Primitive::kPrimInt:
658 case Primitive::kPrimNot:
659 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100660 case Primitive::kPrimFloat:
661 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000662 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100663 break;
664
665 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100666 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100667 }
668 }
669}
670
671void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
672 got->SetLocations(nullptr);
673}
674
675void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
676 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100677 DCHECK(!successor->IsExitBlock());
678
679 HBasicBlock* block = got->GetBlock();
680 HInstruction* previous = got->GetPrevious();
681
682 HLoopInformation* info = block->GetLoopInformation();
683 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
684 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
685 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
686 return;
687 }
688
689 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
690 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
691 }
692 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100693 __ jmp(codegen_->GetLabelOf(successor));
694 }
695}
696
697void LocationsBuilderX86_64::VisitExit(HExit* exit) {
698 exit->SetLocations(nullptr);
699}
700
701void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700702 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100703 if (kIsDebugBuild) {
704 __ Comment("Unreachable");
705 __ int3();
706 }
707}
708
709void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100710 LocationSummary* locations =
711 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100712 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100713 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100714 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100715 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100716}
717
718void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700719 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100720 if (cond->IsIntConstant()) {
721 // Constant condition, statically compared against 1.
722 int32_t cond_value = cond->AsIntConstant()->GetValue();
723 if (cond_value == 1) {
724 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
725 if_instr->IfTrueSuccessor())) {
726 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100727 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100728 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100729 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100730 DCHECK_EQ(cond_value, 0);
731 }
732 } else {
733 bool materialized =
734 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
735 // Moves do not affect the eflags register, so if the condition is
736 // evaluated just before the if, we don't need to evaluate it
737 // again.
738 bool eflags_set = cond->IsCondition()
739 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
740 if (materialized) {
741 if (!eflags_set) {
742 // Materialized condition, compare against 0.
743 Location lhs = if_instr->GetLocations()->InAt(0);
744 if (lhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000745 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(0));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100746 } else {
747 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
748 Immediate(0));
749 }
750 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
751 } else {
752 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
753 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
754 }
755 } else {
756 Location lhs = cond->GetLocations()->InAt(0);
757 Location rhs = cond->GetLocations()->InAt(1);
758 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000759 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100760 } else if (rhs.IsConstant()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000761 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100762 Immediate(rhs.GetConstant()->AsIntConstant()->GetValue()));
763 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000764 __ cmpl(lhs.AsRegister<CpuRegister>(),
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100765 Address(CpuRegister(RSP), rhs.GetStackIndex()));
766 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100767 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
768 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700769 }
Dave Allison20dfc792014-06-16 20:44:29 -0700770 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100771 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
772 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700773 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100774 }
775}
776
777void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
778 local->SetLocations(nullptr);
779}
780
781void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
782 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
783}
784
785void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
786 local->SetLocations(nullptr);
787}
788
789void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
790 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700791 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100792}
793
794void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100795 LocationSummary* locations =
796 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100797 switch (store->InputAt(1)->GetType()) {
798 case Primitive::kPrimBoolean:
799 case Primitive::kPrimByte:
800 case Primitive::kPrimChar:
801 case Primitive::kPrimShort:
802 case Primitive::kPrimInt:
803 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100804 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100805 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
806 break;
807
808 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100809 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100810 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
811 break;
812
813 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100814 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100815 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100816}
817
818void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700819 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100820}
821
Dave Allison20dfc792014-06-16 20:44:29 -0700822void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100823 LocationSummary* locations =
824 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100825 locations->SetInAt(0, Location::RequiresRegister());
826 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100827 if (comp->NeedsMaterialization()) {
828 locations->SetOut(Location::RequiresRegister());
829 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100830}
831
Dave Allison20dfc792014-06-16 20:44:29 -0700832void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
833 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100834 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000835 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100836 // Clear register: setcc only sets the low byte.
837 __ xorq(reg, reg);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100838 if (locations->InAt(1).IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000839 __ cmpl(locations->InAt(0).AsRegister<CpuRegister>(),
840 locations->InAt(1).AsRegister<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100841 } else if (locations->InAt(1).IsConstant()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000842 __ cmpl(locations->InAt(0).AsRegister<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100843 Immediate(locations->InAt(1).GetConstant()->AsIntConstant()->GetValue()));
844 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000845 __ cmpl(locations->InAt(0).AsRegister<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100846 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
847 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100848 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700849 }
850}
851
852void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
853 VisitCondition(comp);
854}
855
856void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
857 VisitCondition(comp);
858}
859
860void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
861 VisitCondition(comp);
862}
863
864void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
865 VisitCondition(comp);
866}
867
868void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
869 VisitCondition(comp);
870}
871
872void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
873 VisitCondition(comp);
874}
875
876void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
877 VisitCondition(comp);
878}
879
880void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
881 VisitCondition(comp);
882}
883
884void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
885 VisitCondition(comp);
886}
887
888void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
889 VisitCondition(comp);
890}
891
892void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
893 VisitCondition(comp);
894}
895
896void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
897 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100898}
899
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100900void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100901 LocationSummary* locations =
902 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +0000903 switch (compare->InputAt(0)->GetType()) {
904 case Primitive::kPrimLong: {
905 locations->SetInAt(0, Location::RequiresRegister());
906 locations->SetInAt(1, Location::RequiresRegister());
907 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
908 break;
909 }
910 case Primitive::kPrimFloat:
911 case Primitive::kPrimDouble: {
912 locations->SetInAt(0, Location::RequiresFpuRegister());
913 locations->SetInAt(1, Location::RequiresFpuRegister());
914 locations->SetOut(Location::RequiresRegister());
915 break;
916 }
917 default:
918 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
919 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100920}
921
922void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100923 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000924 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +0000925 Location left = locations->InAt(0);
926 Location right = locations->InAt(1);
927
928 Label less, greater, done;
929 Primitive::Type type = compare->InputAt(0)->GetType();
930 switch (type) {
931 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000932 __ cmpq(left.AsRegister<CpuRegister>(), right.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100933 break;
Calin Juravleddb7df22014-11-25 20:56:51 +0000934 }
935 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000936 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +0000937 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
938 break;
939 }
940 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000941 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +0000942 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
943 break;
944 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100945 default:
Calin Juravleddb7df22014-11-25 20:56:51 +0000946 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100947 }
Calin Juravleddb7df22014-11-25 20:56:51 +0000948 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +0000949 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +0000950 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +0000951
Calin Juravle91debbc2014-11-26 19:01:09 +0000952 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +0000953 __ movl(out, Immediate(1));
954 __ jmp(&done);
955
956 __ Bind(&less);
957 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100958
959 __ Bind(&done);
960}
961
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100962void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100963 LocationSummary* locations =
964 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100965 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100966}
967
968void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100969 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700970 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100971}
972
973void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100974 LocationSummary* locations =
975 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100976 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100977}
978
979void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100980 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700981 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100982}
983
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100984void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
985 LocationSummary* locations =
986 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
987 locations->SetOut(Location::ConstantLocation(constant));
988}
989
990void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
991 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700992 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100993}
994
995void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
996 LocationSummary* locations =
997 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
998 locations->SetOut(Location::ConstantLocation(constant));
999}
1000
1001void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1002 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001003 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001004}
1005
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001006void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1007 ret->SetLocations(nullptr);
1008}
1009
1010void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001011 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001012 codegen_->GenerateFrameExit();
1013 __ ret();
1014}
1015
1016void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001017 LocationSummary* locations =
1018 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001019 switch (ret->InputAt(0)->GetType()) {
1020 case Primitive::kPrimBoolean:
1021 case Primitive::kPrimByte:
1022 case Primitive::kPrimChar:
1023 case Primitive::kPrimShort:
1024 case Primitive::kPrimInt:
1025 case Primitive::kPrimNot:
1026 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001027 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001028 break;
1029
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001030 case Primitive::kPrimFloat:
1031 case Primitive::kPrimDouble:
1032 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001033 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001034 break;
1035
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001036 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001037 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001038 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001039}
1040
1041void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1042 if (kIsDebugBuild) {
1043 switch (ret->InputAt(0)->GetType()) {
1044 case Primitive::kPrimBoolean:
1045 case Primitive::kPrimByte:
1046 case Primitive::kPrimChar:
1047 case Primitive::kPrimShort:
1048 case Primitive::kPrimInt:
1049 case Primitive::kPrimNot:
1050 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001051 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001052 break;
1053
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001054 case Primitive::kPrimFloat:
1055 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001056 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001057 XMM0);
1058 break;
1059
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001060 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001061 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001062 }
1063 }
1064 codegen_->GenerateFrameExit();
1065 __ ret();
1066}
1067
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001068Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
1069 switch (type) {
1070 case Primitive::kPrimBoolean:
1071 case Primitive::kPrimByte:
1072 case Primitive::kPrimChar:
1073 case Primitive::kPrimShort:
1074 case Primitive::kPrimInt:
1075 case Primitive::kPrimNot: {
1076 uint32_t index = gp_index_++;
1077 stack_index_++;
1078 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001079 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001080 } else {
1081 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1082 }
1083 }
1084
1085 case Primitive::kPrimLong: {
1086 uint32_t index = gp_index_;
1087 stack_index_ += 2;
1088 if (index < calling_convention.GetNumberOfRegisters()) {
1089 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001090 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001091 } else {
1092 gp_index_ += 2;
1093 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1094 }
1095 }
1096
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001097 case Primitive::kPrimFloat: {
1098 uint32_t index = fp_index_++;
1099 stack_index_++;
1100 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001101 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001102 } else {
1103 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1104 }
1105 }
1106
1107 case Primitive::kPrimDouble: {
1108 uint32_t index = fp_index_++;
1109 stack_index_ += 2;
1110 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001111 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001112 } else {
1113 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1114 }
1115 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001116
1117 case Primitive::kPrimVoid:
1118 LOG(FATAL) << "Unexpected parameter type " << type;
1119 break;
1120 }
1121 return Location();
1122}
1123
1124void LocationsBuilderX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001125 HandleInvoke(invoke);
1126}
1127
1128void InstructionCodeGeneratorX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001129 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001130 // TODO: Implement all kinds of calls:
1131 // 1) boot -> boot
1132 // 2) app -> boot
1133 // 3) app -> app
1134 //
1135 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1136
1137 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001138 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001139 // temp = temp->dex_cache_resolved_methods_;
1140 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
1141 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001142 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001143 // (temp + offset_of_quick_compiled_code)()
Mathieu Chartier2d721012014-11-10 11:08:06 -08001144 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001145 kX86_64WordSize).SizeValue()));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001146
1147 DCHECK(!codegen_->IsLeafMethod());
1148 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1149}
1150
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001151void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001152 LocationSummary* locations =
1153 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001154 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001155
1156 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001157 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001158 HInstruction* input = invoke->InputAt(i);
1159 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1160 }
1161
1162 switch (invoke->GetType()) {
1163 case Primitive::kPrimBoolean:
1164 case Primitive::kPrimByte:
1165 case Primitive::kPrimChar:
1166 case Primitive::kPrimShort:
1167 case Primitive::kPrimInt:
1168 case Primitive::kPrimNot:
1169 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001170 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001171 break;
1172
1173 case Primitive::kPrimVoid:
1174 break;
1175
1176 case Primitive::kPrimDouble:
1177 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001178 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001179 break;
1180 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001181}
1182
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001183void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1184 HandleInvoke(invoke);
1185}
1186
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001187void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001188 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001189 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1190 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1191 LocationSummary* locations = invoke->GetLocations();
1192 Location receiver = locations->InAt(0);
1193 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1194 // temp = object->GetClass();
1195 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001196 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1197 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001198 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001199 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001200 }
1201 // temp = temp->GetMethodAt(method_offset);
1202 __ movl(temp, Address(temp, method_offset));
1203 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001204 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001205 kX86_64WordSize).SizeValue()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001206
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001207 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001208 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001209}
1210
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001211void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1212 HandleInvoke(invoke);
1213 // Add the hidden argument.
1214 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1215}
1216
1217void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1218 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001219 CpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001220 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1221 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1222 LocationSummary* locations = invoke->GetLocations();
1223 Location receiver = locations->InAt(0);
1224 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1225
1226 // Set the hidden argument.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001227 __ movq(invoke->GetLocations()->GetTemp(1).AsRegister<CpuRegister>(),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001228 Immediate(invoke->GetDexMethodIndex()));
1229
1230 // temp = object->GetClass();
1231 if (receiver.IsStackSlot()) {
1232 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1233 __ movl(temp, Address(temp, class_offset));
1234 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001235 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001236 }
1237 // temp = temp->GetImtEntryAt(method_offset);
1238 __ movl(temp, Address(temp, method_offset));
1239 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001240 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001241 kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001242
1243 DCHECK(!codegen_->IsLeafMethod());
1244 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1245}
1246
Roland Levillain88cb1752014-10-20 16:36:47 +01001247void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1248 LocationSummary* locations =
1249 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1250 switch (neg->GetResultType()) {
1251 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001252 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001253 locations->SetInAt(0, Location::RequiresRegister());
1254 locations->SetOut(Location::SameAsFirstInput());
1255 break;
1256
Roland Levillain88cb1752014-10-20 16:36:47 +01001257 case Primitive::kPrimFloat:
1258 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001259 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001260 locations->SetOut(Location::SameAsFirstInput());
1261 locations->AddTemp(Location::RequiresRegister());
1262 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001263 break;
1264
1265 default:
1266 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1267 }
1268}
1269
1270void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1271 LocationSummary* locations = neg->GetLocations();
1272 Location out = locations->Out();
1273 Location in = locations->InAt(0);
1274 switch (neg->GetResultType()) {
1275 case Primitive::kPrimInt:
1276 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001277 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001278 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001279 break;
1280
1281 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001282 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001283 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001284 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001285 break;
1286
Roland Levillain5368c212014-11-27 15:03:41 +00001287 case Primitive::kPrimFloat: {
1288 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001289 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1290 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001291 // Implement float negation with an exclusive or with value
1292 // 0x80000000 (mask for bit 31, representing the sign of a
1293 // single-precision floating-point number).
1294 __ movq(constant, Immediate(INT64_C(0x80000000)));
1295 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001296 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001297 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001298 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001299
Roland Levillain5368c212014-11-27 15:03:41 +00001300 case Primitive::kPrimDouble: {
1301 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001302 CpuRegister constant = locations->GetTemp(0).AsRegister<CpuRegister>();
1303 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001304 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00001305 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00001306 // a double-precision floating-point number).
1307 __ movq(constant, Immediate(INT64_C(0x8000000000000000)));
1308 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001309 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001310 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001311 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001312
1313 default:
1314 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1315 }
1316}
1317
Roland Levillaindff1f282014-11-05 14:15:05 +00001318void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1319 LocationSummary* locations =
1320 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1321 Primitive::Type result_type = conversion->GetResultType();
1322 Primitive::Type input_type = conversion->GetInputType();
1323 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001324 case Primitive::kPrimByte:
1325 switch (input_type) {
1326 case Primitive::kPrimShort:
1327 case Primitive::kPrimInt:
1328 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001329 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001330 locations->SetInAt(0, Location::Any());
1331 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1332 break;
1333
1334 default:
1335 LOG(FATAL) << "Unexpected type conversion from " << input_type
1336 << " to " << result_type;
1337 }
1338 break;
1339
Roland Levillain01a8d712014-11-14 16:27:39 +00001340 case Primitive::kPrimShort:
1341 switch (input_type) {
1342 case Primitive::kPrimByte:
1343 case Primitive::kPrimInt:
1344 case Primitive::kPrimChar:
1345 // Processing a Dex `int-to-short' instruction.
1346 locations->SetInAt(0, Location::Any());
1347 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1348 break;
1349
1350 default:
1351 LOG(FATAL) << "Unexpected type conversion from " << input_type
1352 << " to " << result_type;
1353 }
1354 break;
1355
Roland Levillain946e1432014-11-11 17:35:19 +00001356 case Primitive::kPrimInt:
1357 switch (input_type) {
1358 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001359 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001360 locations->SetInAt(0, Location::Any());
1361 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1362 break;
1363
1364 case Primitive::kPrimFloat:
1365 case Primitive::kPrimDouble:
1366 LOG(FATAL) << "Type conversion from " << input_type
1367 << " to " << result_type << " not yet implemented";
1368 break;
1369
1370 default:
1371 LOG(FATAL) << "Unexpected type conversion from " << input_type
1372 << " to " << result_type;
1373 }
1374 break;
1375
Roland Levillaindff1f282014-11-05 14:15:05 +00001376 case Primitive::kPrimLong:
1377 switch (input_type) {
1378 case Primitive::kPrimByte:
1379 case Primitive::kPrimShort:
1380 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001381 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001382 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001383 // TODO: We would benefit from a (to-be-implemented)
1384 // Location::RegisterOrStackSlot requirement for this input.
1385 locations->SetInAt(0, Location::RequiresRegister());
1386 locations->SetOut(Location::RequiresRegister());
1387 break;
1388
1389 case Primitive::kPrimFloat:
1390 case Primitive::kPrimDouble:
1391 LOG(FATAL) << "Type conversion from " << input_type << " to "
1392 << result_type << " not yet implemented";
1393 break;
1394
1395 default:
1396 LOG(FATAL) << "Unexpected type conversion from " << input_type
1397 << " to " << result_type;
1398 }
1399 break;
1400
Roland Levillain981e4542014-11-14 11:47:14 +00001401 case Primitive::kPrimChar:
1402 switch (input_type) {
1403 case Primitive::kPrimByte:
1404 case Primitive::kPrimShort:
1405 case Primitive::kPrimInt:
1406 case Primitive::kPrimChar:
1407 // Processing a Dex `int-to-char' instruction.
1408 locations->SetInAt(0, Location::Any());
1409 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1410 break;
1411
1412 default:
1413 LOG(FATAL) << "Unexpected type conversion from " << input_type
1414 << " to " << result_type;
1415 }
1416 break;
1417
Roland Levillaindff1f282014-11-05 14:15:05 +00001418 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001419 switch (input_type) {
1420 case Primitive::kPrimByte:
1421 case Primitive::kPrimShort:
1422 case Primitive::kPrimInt:
1423 case Primitive::kPrimChar:
1424 // Processing a Dex `int-to-float' instruction.
1425 locations->SetInAt(0, Location::RequiresRegister());
1426 locations->SetOut(Location::RequiresFpuRegister());
1427 break;
1428
1429 case Primitive::kPrimLong:
1430 case Primitive::kPrimDouble:
1431 LOG(FATAL) << "Type conversion from " << input_type
1432 << " to " << result_type << " not yet implemented";
1433 break;
1434
1435 default:
1436 LOG(FATAL) << "Unexpected type conversion from " << input_type
1437 << " to " << result_type;
1438 };
1439 break;
1440
Roland Levillaindff1f282014-11-05 14:15:05 +00001441 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001442 switch (input_type) {
1443 case Primitive::kPrimByte:
1444 case Primitive::kPrimShort:
1445 case Primitive::kPrimInt:
1446 case Primitive::kPrimChar:
1447 // Processing a Dex `int-to-double' instruction.
1448 locations->SetInAt(0, Location::RequiresRegister());
1449 locations->SetOut(Location::RequiresFpuRegister());
1450 break;
1451
1452 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001453 // Processing a Dex `long-to-double' instruction.
1454 locations->SetInAt(0, Location::RequiresRegister());
1455 locations->SetOut(Location::RequiresFpuRegister());
1456 break;
1457
Roland Levillaincff13742014-11-17 14:32:17 +00001458 case Primitive::kPrimFloat:
1459 LOG(FATAL) << "Type conversion from " << input_type
1460 << " to " << result_type << " not yet implemented";
1461 break;
1462
1463 default:
1464 LOG(FATAL) << "Unexpected type conversion from " << input_type
1465 << " to " << result_type;
1466 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001467 break;
1468
1469 default:
1470 LOG(FATAL) << "Unexpected type conversion from " << input_type
1471 << " to " << result_type;
1472 }
1473}
1474
1475void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1476 LocationSummary* locations = conversion->GetLocations();
1477 Location out = locations->Out();
1478 Location in = locations->InAt(0);
1479 Primitive::Type result_type = conversion->GetResultType();
1480 Primitive::Type input_type = conversion->GetInputType();
1481 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001482 case Primitive::kPrimByte:
1483 switch (input_type) {
1484 case Primitive::kPrimShort:
1485 case Primitive::kPrimInt:
1486 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001487 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001488 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001489 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001490 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001491 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001492 Address(CpuRegister(RSP), in.GetStackIndex()));
1493 } else {
1494 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001495 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00001496 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1497 }
1498 break;
1499
1500 default:
1501 LOG(FATAL) << "Unexpected type conversion from " << input_type
1502 << " to " << result_type;
1503 }
1504 break;
1505
Roland Levillain01a8d712014-11-14 16:27:39 +00001506 case Primitive::kPrimShort:
1507 switch (input_type) {
1508 case Primitive::kPrimByte:
1509 case Primitive::kPrimInt:
1510 case Primitive::kPrimChar:
1511 // Processing a Dex `int-to-short' instruction.
1512 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001513 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001514 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001515 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001516 Address(CpuRegister(RSP), in.GetStackIndex()));
1517 } else {
1518 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001519 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00001520 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1521 }
1522 break;
1523
1524 default:
1525 LOG(FATAL) << "Unexpected type conversion from " << input_type
1526 << " to " << result_type;
1527 }
1528 break;
1529
Roland Levillain946e1432014-11-11 17:35:19 +00001530 case Primitive::kPrimInt:
1531 switch (input_type) {
1532 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001533 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001534 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001535 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00001536 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001537 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00001538 Address(CpuRegister(RSP), in.GetStackIndex()));
1539 } else {
1540 DCHECK(in.IsConstant());
1541 DCHECK(in.GetConstant()->IsLongConstant());
1542 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001543 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001544 }
1545 break;
1546
1547 case Primitive::kPrimFloat:
1548 case Primitive::kPrimDouble:
1549 LOG(FATAL) << "Type conversion from " << input_type
1550 << " to " << result_type << " not yet implemented";
1551 break;
1552
1553 default:
1554 LOG(FATAL) << "Unexpected type conversion from " << input_type
1555 << " to " << result_type;
1556 }
1557 break;
1558
Roland Levillaindff1f282014-11-05 14:15:05 +00001559 case Primitive::kPrimLong:
1560 switch (input_type) {
1561 DCHECK(out.IsRegister());
1562 case Primitive::kPrimByte:
1563 case Primitive::kPrimShort:
1564 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001565 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001566 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001567 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001568 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00001569 break;
1570
1571 case Primitive::kPrimFloat:
1572 case Primitive::kPrimDouble:
1573 LOG(FATAL) << "Type conversion from " << input_type << " to "
1574 << result_type << " not yet implemented";
1575 break;
1576
1577 default:
1578 LOG(FATAL) << "Unexpected type conversion from " << input_type
1579 << " to " << result_type;
1580 }
1581 break;
1582
Roland Levillain981e4542014-11-14 11:47:14 +00001583 case Primitive::kPrimChar:
1584 switch (input_type) {
1585 case Primitive::kPrimByte:
1586 case Primitive::kPrimShort:
1587 case Primitive::kPrimInt:
1588 case Primitive::kPrimChar:
1589 // Processing a Dex `int-to-char' instruction.
1590 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001591 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00001592 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001593 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001594 Address(CpuRegister(RSP), in.GetStackIndex()));
1595 } else {
1596 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001597 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00001598 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1599 }
1600 break;
1601
1602 default:
1603 LOG(FATAL) << "Unexpected type conversion from " << input_type
1604 << " to " << result_type;
1605 }
1606 break;
1607
Roland Levillaindff1f282014-11-05 14:15:05 +00001608 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001609 switch (input_type) {
1610 // Processing a Dex `int-to-float' instruction.
1611 case Primitive::kPrimByte:
1612 case Primitive::kPrimShort:
1613 case Primitive::kPrimInt:
1614 case Primitive::kPrimChar:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001615 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001616 break;
1617
1618 case Primitive::kPrimLong:
1619 case Primitive::kPrimDouble:
1620 LOG(FATAL) << "Type conversion from " << input_type
1621 << " to " << result_type << " not yet implemented";
1622 break;
1623
1624 default:
1625 LOG(FATAL) << "Unexpected type conversion from " << input_type
1626 << " to " << result_type;
1627 };
1628 break;
1629
Roland Levillaindff1f282014-11-05 14:15:05 +00001630 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001631 switch (input_type) {
1632 // Processing a Dex `int-to-double' instruction.
1633 case Primitive::kPrimByte:
1634 case Primitive::kPrimShort:
1635 case Primitive::kPrimInt:
1636 case Primitive::kPrimChar:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001637 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
Roland Levillaincff13742014-11-17 14:32:17 +00001638 break;
1639
1640 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001641 // Processing a Dex `long-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001642 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001643 break;
1644
Roland Levillaincff13742014-11-17 14:32:17 +00001645 case Primitive::kPrimFloat:
1646 LOG(FATAL) << "Type conversion from " << input_type
1647 << " to " << result_type << " not yet implemented";
1648 break;
1649
1650 default:
1651 LOG(FATAL) << "Unexpected type conversion from " << input_type
1652 << " to " << result_type;
1653 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001654 break;
1655
1656 default:
1657 LOG(FATAL) << "Unexpected type conversion from " << input_type
1658 << " to " << result_type;
1659 }
1660}
1661
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001662void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001663 LocationSummary* locations =
1664 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001665 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001666 case Primitive::kPrimInt: {
1667 locations->SetInAt(0, Location::RequiresRegister());
1668 locations->SetInAt(1, Location::Any());
1669 locations->SetOut(Location::SameAsFirstInput());
1670 break;
1671 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001672
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001673 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001674 locations->SetInAt(0, Location::RequiresRegister());
1675 locations->SetInAt(1, Location::RequiresRegister());
1676 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001677 break;
1678 }
1679
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001680 case Primitive::kPrimDouble:
1681 case Primitive::kPrimFloat: {
1682 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001683 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001684 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001685 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001686 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001687
1688 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001689 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001690 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001691}
1692
1693void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1694 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001695 Location first = locations->InAt(0);
1696 Location second = locations->InAt(1);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001697 DCHECK(first.Equals(locations->Out()));
Calin Juravle11351682014-10-23 15:38:15 +01001698
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001699 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001700 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001701 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001702 __ addl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001703 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001704 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001705 __ addl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001706 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001707 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001708 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001709 break;
1710 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001711
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001712 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001713 __ addq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001714 break;
1715 }
1716
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001717 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001718 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001719 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001720 }
1721
1722 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001723 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001724 break;
1725 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001726
1727 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001728 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001729 }
1730}
1731
1732void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001733 LocationSummary* locations =
1734 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001735 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001736 case Primitive::kPrimInt: {
1737 locations->SetInAt(0, Location::RequiresRegister());
1738 locations->SetInAt(1, Location::Any());
1739 locations->SetOut(Location::SameAsFirstInput());
1740 break;
1741 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001742 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001743 locations->SetInAt(0, Location::RequiresRegister());
1744 locations->SetInAt(1, Location::RequiresRegister());
1745 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001746 break;
1747 }
Calin Juravle11351682014-10-23 15:38:15 +01001748 case Primitive::kPrimFloat:
1749 case Primitive::kPrimDouble: {
1750 locations->SetInAt(0, Location::RequiresFpuRegister());
1751 locations->SetInAt(1, Location::RequiresFpuRegister());
1752 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001753 break;
Calin Juravle11351682014-10-23 15:38:15 +01001754 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001755 default:
Calin Juravle11351682014-10-23 15:38:15 +01001756 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001757 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001758}
1759
1760void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1761 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001762 Location first = locations->InAt(0);
1763 Location second = locations->InAt(1);
1764 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001765 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001766 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001767 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001768 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001769 } else if (second.IsConstant()) {
1770 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001771 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001772 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001773 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001774 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001775 break;
1776 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001777 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001778 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001779 break;
1780 }
1781
Calin Juravle11351682014-10-23 15:38:15 +01001782 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001783 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001784 break;
Calin Juravle11351682014-10-23 15:38:15 +01001785 }
1786
1787 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001788 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001789 break;
1790 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001791
1792 default:
Calin Juravle11351682014-10-23 15:38:15 +01001793 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001794 }
1795}
1796
Calin Juravle34bacdf2014-10-07 20:23:36 +01001797void LocationsBuilderX86_64::VisitMul(HMul* mul) {
1798 LocationSummary* locations =
1799 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1800 switch (mul->GetResultType()) {
1801 case Primitive::kPrimInt: {
1802 locations->SetInAt(0, Location::RequiresRegister());
1803 locations->SetInAt(1, Location::Any());
1804 locations->SetOut(Location::SameAsFirstInput());
1805 break;
1806 }
1807 case Primitive::kPrimLong: {
1808 locations->SetInAt(0, Location::RequiresRegister());
1809 locations->SetInAt(1, Location::RequiresRegister());
1810 locations->SetOut(Location::SameAsFirstInput());
1811 break;
1812 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001813 case Primitive::kPrimFloat:
1814 case Primitive::kPrimDouble: {
1815 locations->SetInAt(0, Location::RequiresFpuRegister());
1816 locations->SetInAt(1, Location::RequiresFpuRegister());
1817 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001818 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001819 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001820
1821 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001822 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001823 }
1824}
1825
1826void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
1827 LocationSummary* locations = mul->GetLocations();
1828 Location first = locations->InAt(0);
1829 Location second = locations->InAt(1);
1830 DCHECK(first.Equals(locations->Out()));
1831 switch (mul->GetResultType()) {
1832 case Primitive::kPrimInt: {
1833 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001834 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001835 } else if (second.IsConstant()) {
1836 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001837 __ imull(first.AsRegister<CpuRegister>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001838 } else {
1839 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00001840 __ imull(first.AsRegister<CpuRegister>(),
1841 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01001842 }
1843 break;
1844 }
1845 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001846 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001847 break;
1848 }
1849
Calin Juravleb5bfa962014-10-21 18:02:24 +01001850 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001851 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001852 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001853 }
1854
1855 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001856 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01001857 break;
1858 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001859
1860 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001861 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001862 }
1863}
1864
Calin Juravlebacfec32014-11-14 15:54:36 +00001865void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
1866 DCHECK(instruction->IsDiv() || instruction->IsRem());
1867 Primitive::Type type = instruction->GetResultType();
1868 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
1869
1870 bool is_div = instruction->IsDiv();
1871 LocationSummary* locations = instruction->GetLocations();
1872
Roland Levillain271ab9c2014-11-27 15:23:57 +00001873 CpuRegister out_reg = locations->Out().AsRegister<CpuRegister>();
1874 CpuRegister second_reg = locations->InAt(1).AsRegister<CpuRegister>();
Calin Juravlebacfec32014-11-14 15:54:36 +00001875
Roland Levillain271ab9c2014-11-27 15:23:57 +00001876 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00001877 DCHECK_EQ(is_div ? RAX : RDX, out_reg.AsRegister());
1878
1879 SlowPathCodeX86_64* slow_path =
1880 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
1881 out_reg.AsRegister(), type, is_div);
1882 codegen_->AddSlowPath(slow_path);
1883
1884 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
1885 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
1886 // so it's safe to just use negl instead of more complex comparisons.
1887
1888 __ cmpl(second_reg, Immediate(-1));
1889 __ j(kEqual, slow_path->GetEntryLabel());
1890
1891 if (type == Primitive::kPrimInt) {
1892 // edx:eax <- sign-extended of eax
1893 __ cdq();
1894 // eax = quotient, edx = remainder
1895 __ idivl(second_reg);
1896 } else {
1897 // rdx:rax <- sign-extended of rax
1898 __ cqo();
1899 // rax = quotient, rdx = remainder
1900 __ idivq(second_reg);
1901 }
1902
1903 __ Bind(slow_path->GetExitLabel());
1904}
1905
Calin Juravle7c4954d2014-10-28 16:57:40 +00001906void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
1907 LocationSummary* locations =
1908 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1909 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001910 case Primitive::kPrimInt:
1911 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00001912 locations->SetInAt(0, Location::RegisterLocation(RAX));
1913 locations->SetInAt(1, Location::RequiresRegister());
1914 locations->SetOut(Location::SameAsFirstInput());
1915 // Intel uses edx:eax as the dividend.
1916 locations->AddTemp(Location::RegisterLocation(RDX));
1917 break;
1918 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001919
Calin Juravle7c4954d2014-10-28 16:57:40 +00001920 case Primitive::kPrimFloat:
1921 case Primitive::kPrimDouble: {
1922 locations->SetInAt(0, Location::RequiresFpuRegister());
1923 locations->SetInAt(1, Location::RequiresFpuRegister());
1924 locations->SetOut(Location::SameAsFirstInput());
1925 break;
1926 }
1927
1928 default:
1929 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1930 }
1931}
1932
1933void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
1934 LocationSummary* locations = div->GetLocations();
1935 Location first = locations->InAt(0);
1936 Location second = locations->InAt(1);
1937 DCHECK(first.Equals(locations->Out()));
1938
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001939 Primitive::Type type = div->GetResultType();
1940 switch (type) {
1941 case Primitive::kPrimInt:
1942 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001943 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00001944 break;
1945 }
1946
Calin Juravle7c4954d2014-10-28 16:57:40 +00001947 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001948 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00001949 break;
1950 }
1951
1952 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001953 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00001954 break;
1955 }
1956
1957 default:
1958 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1959 }
1960}
1961
Calin Juravlebacfec32014-11-14 15:54:36 +00001962void LocationsBuilderX86_64::VisitRem(HRem* rem) {
1963 LocationSummary* locations =
1964 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
1965 switch (rem->GetResultType()) {
1966 case Primitive::kPrimInt:
1967 case Primitive::kPrimLong: {
1968 locations->SetInAt(0, Location::RegisterLocation(RAX));
1969 locations->SetInAt(1, Location::RequiresRegister());
1970 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
1971 locations->SetOut(Location::RegisterLocation(RDX));
1972 break;
1973 }
1974
1975 case Primitive::kPrimFloat:
1976 case Primitive::kPrimDouble: {
1977 LOG(FATAL) << "Unimplemented rem type " << rem->GetResultType();
1978 break;
1979 }
1980
1981 default:
1982 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
1983 }
1984}
1985
1986void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
1987 Primitive::Type type = rem->GetResultType();
1988 switch (type) {
1989 case Primitive::kPrimInt:
1990 case Primitive::kPrimLong: {
1991 GenerateDivRemIntegral(rem);
1992 break;
1993 }
1994
1995 case Primitive::kPrimFloat:
1996 case Primitive::kPrimDouble: {
1997 LOG(FATAL) << "Unimplemented rem type " << rem->GetResultType();
1998 break;
1999 }
2000
2001 default:
2002 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2003 }
2004}
2005
Calin Juravled0d48522014-11-04 16:40:20 +00002006void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2007 LocationSummary* locations =
2008 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2009 locations->SetInAt(0, Location::Any());
2010 if (instruction->HasUses()) {
2011 locations->SetOut(Location::SameAsFirstInput());
2012 }
2013}
2014
2015void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2016 SlowPathCodeX86_64* slow_path =
2017 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
2018 codegen_->AddSlowPath(slow_path);
2019
2020 LocationSummary* locations = instruction->GetLocations();
2021 Location value = locations->InAt(0);
2022
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002023 switch (instruction->GetType()) {
2024 case Primitive::kPrimInt: {
2025 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002026 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002027 __ j(kEqual, slow_path->GetEntryLabel());
2028 } else if (value.IsStackSlot()) {
2029 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2030 __ j(kEqual, slow_path->GetEntryLabel());
2031 } else {
2032 DCHECK(value.IsConstant()) << value;
2033 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2034 __ jmp(slow_path->GetEntryLabel());
2035 }
2036 }
2037 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002038 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002039 case Primitive::kPrimLong: {
2040 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002041 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002042 __ j(kEqual, slow_path->GetEntryLabel());
2043 } else if (value.IsDoubleStackSlot()) {
2044 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
2045 __ j(kEqual, slow_path->GetEntryLabel());
2046 } else {
2047 DCHECK(value.IsConstant()) << value;
2048 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2049 __ jmp(slow_path->GetEntryLabel());
2050 }
2051 }
2052 break;
2053 }
2054 default:
2055 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002056 }
Calin Juravled0d48522014-11-04 16:40:20 +00002057}
2058
Calin Juravle9aec02f2014-11-18 23:06:35 +00002059void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
2060 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2061
2062 LocationSummary* locations =
2063 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2064
2065 switch (op->GetResultType()) {
2066 case Primitive::kPrimInt:
2067 case Primitive::kPrimLong: {
2068 locations->SetInAt(0, Location::RequiresRegister());
2069 // The shift count needs to be in CL.
2070 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
2071 locations->SetOut(Location::SameAsFirstInput());
2072 break;
2073 }
2074 default:
2075 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2076 }
2077}
2078
2079void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
2080 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2081
2082 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002083 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002084 Location second = locations->InAt(1);
2085
2086 switch (op->GetResultType()) {
2087 case Primitive::kPrimInt: {
2088 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002089 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002090 if (op->IsShl()) {
2091 __ shll(first_reg, second_reg);
2092 } else if (op->IsShr()) {
2093 __ sarl(first_reg, second_reg);
2094 } else {
2095 __ shrl(first_reg, second_reg);
2096 }
2097 } else {
2098 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
2099 if (op->IsShl()) {
2100 __ shll(first_reg, imm);
2101 } else if (op->IsShr()) {
2102 __ sarl(first_reg, imm);
2103 } else {
2104 __ shrl(first_reg, imm);
2105 }
2106 }
2107 break;
2108 }
2109 case Primitive::kPrimLong: {
2110 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002111 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002112 if (op->IsShl()) {
2113 __ shlq(first_reg, second_reg);
2114 } else if (op->IsShr()) {
2115 __ sarq(first_reg, second_reg);
2116 } else {
2117 __ shrq(first_reg, second_reg);
2118 }
2119 } else {
2120 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
2121 if (op->IsShl()) {
2122 __ shlq(first_reg, imm);
2123 } else if (op->IsShr()) {
2124 __ sarq(first_reg, imm);
2125 } else {
2126 __ shrq(first_reg, imm);
2127 }
2128 }
2129 break;
2130 }
2131 default:
2132 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
2133 }
2134}
2135
2136void LocationsBuilderX86_64::VisitShl(HShl* shl) {
2137 HandleShift(shl);
2138}
2139
2140void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
2141 HandleShift(shl);
2142}
2143
2144void LocationsBuilderX86_64::VisitShr(HShr* shr) {
2145 HandleShift(shr);
2146}
2147
2148void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
2149 HandleShift(shr);
2150}
2151
2152void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
2153 HandleShift(ushr);
2154}
2155
2156void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
2157 HandleShift(ushr);
2158}
2159
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002160void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002161 LocationSummary* locations =
2162 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002163 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002164 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2165 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2166 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002167}
2168
2169void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
2170 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002171 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002172 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2173
2174 __ gs()->call(Address::Absolute(
2175 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
2176
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002177 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002178 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002179}
2180
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002181void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
2182 LocationSummary* locations =
2183 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2184 InvokeRuntimeCallingConvention calling_convention;
2185 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2186 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2187 locations->SetOut(Location::RegisterLocation(RAX));
2188 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2189}
2190
2191void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
2192 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002193 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002194 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
2195
2196 __ gs()->call(Address::Absolute(
2197 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocArrayWithAccessCheck), true));
2198
2199 DCHECK(!codegen_->IsLeafMethod());
2200 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2201}
2202
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002203void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002204 LocationSummary* locations =
2205 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002206 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2207 if (location.IsStackSlot()) {
2208 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2209 } else if (location.IsDoubleStackSlot()) {
2210 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2211 }
2212 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002213}
2214
2215void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
2216 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002217 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002218}
2219
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002220void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002221 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002222 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002223 locations->SetInAt(0, Location::RequiresRegister());
2224 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002225}
2226
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002227void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
2228 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002229 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
2230 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002231 Location out = locations->Out();
2232 switch (not_->InputAt(0)->GetType()) {
2233 case Primitive::kPrimBoolean:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002234 __ xorq(out.AsRegister<CpuRegister>(), Immediate(1));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002235 break;
2236
2237 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002238 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002239 break;
2240
2241 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002242 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002243 break;
2244
2245 default:
2246 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2247 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002248}
2249
2250void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002251 LocationSummary* locations =
2252 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002253 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2254 locations->SetInAt(i, Location::Any());
2255 }
2256 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002257}
2258
2259void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002260 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002261 LOG(FATAL) << "Unimplemented";
2262}
2263
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002264void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002265 LocationSummary* locations =
2266 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002267 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002268 bool needs_write_barrier =
2269 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->GetValue());
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002270 locations->SetInAt(0, Location::RequiresRegister());
2271 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002272 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002273 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002274 locations->AddTemp(Location::RequiresRegister());
2275 locations->AddTemp(Location::RequiresRegister());
2276 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002277}
2278
2279void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2280 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002281 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002282 size_t offset = instruction->GetFieldOffset().SizeValue();
Nicolas Geoffray39468442014-09-02 15:17:15 +01002283 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002284
2285 switch (field_type) {
2286 case Primitive::kPrimBoolean:
2287 case Primitive::kPrimByte: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002288 CpuRegister value = locations->InAt(1).AsRegister<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002289 __ movb(Address(obj, offset), value);
2290 break;
2291 }
2292
2293 case Primitive::kPrimShort:
2294 case Primitive::kPrimChar: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002295 CpuRegister value = locations->InAt(1).AsRegister<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002296 __ movw(Address(obj, offset), value);
2297 break;
2298 }
2299
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002300 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002301 case Primitive::kPrimNot: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002302 CpuRegister value = locations->InAt(1).AsRegister<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002303 __ movl(Address(obj, offset), value);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002304 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->GetValue())) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002305 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2306 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002307 codegen_->MarkGCCard(temp, card, obj, value);
2308 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002309 break;
2310 }
2311
2312 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002313 CpuRegister value = locations->InAt(1).AsRegister<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002314 __ movq(Address(obj, offset), value);
2315 break;
2316 }
2317
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002318 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002319 XmmRegister value = locations->InAt(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002320 __ movss(Address(obj, offset), value);
2321 break;
2322 }
2323
2324 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002325 XmmRegister value = locations->InAt(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002326 __ movsd(Address(obj, offset), value);
2327 break;
2328 }
2329
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002330 case Primitive::kPrimVoid:
2331 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002332 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002333 }
2334}
2335
2336void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002337 LocationSummary* locations =
2338 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002339 locations->SetInAt(0, Location::RequiresRegister());
2340 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002341}
2342
2343void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2344 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002345 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002346 size_t offset = instruction->GetFieldOffset().SizeValue();
2347
2348 switch (instruction->GetType()) {
2349 case Primitive::kPrimBoolean: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002350 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002351 __ movzxb(out, Address(obj, offset));
2352 break;
2353 }
2354
2355 case Primitive::kPrimByte: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002356 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002357 __ movsxb(out, Address(obj, offset));
2358 break;
2359 }
2360
2361 case Primitive::kPrimShort: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002362 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002363 __ movsxw(out, Address(obj, offset));
2364 break;
2365 }
2366
2367 case Primitive::kPrimChar: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002368 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002369 __ movzxw(out, Address(obj, offset));
2370 break;
2371 }
2372
2373 case Primitive::kPrimInt:
2374 case Primitive::kPrimNot: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002375 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002376 __ movl(out, Address(obj, offset));
2377 break;
2378 }
2379
2380 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002381 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002382 __ movq(out, Address(obj, offset));
2383 break;
2384 }
2385
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002386 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002387 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002388 __ movss(out, Address(obj, offset));
2389 break;
2390 }
2391
2392 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002393 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002394 __ movsd(out, Address(obj, offset));
2395 break;
2396 }
2397
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002398 case Primitive::kPrimVoid:
2399 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002400 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002401 }
2402}
2403
2404void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002405 LocationSummary* locations =
2406 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002407 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002408 if (instruction->HasUses()) {
2409 locations->SetOut(Location::SameAsFirstInput());
2410 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002411}
2412
2413void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002414 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002415 codegen_->AddSlowPath(slow_path);
2416
2417 LocationSummary* locations = instruction->GetLocations();
2418 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002419
2420 if (obj.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002421 __ cmpl(obj.AsRegister<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002422 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002423 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002424 } else {
2425 DCHECK(obj.IsConstant()) << obj;
2426 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2427 __ jmp(slow_path->GetEntryLabel());
2428 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002429 }
2430 __ j(kEqual, slow_path->GetEntryLabel());
2431}
2432
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002433void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002434 LocationSummary* locations =
2435 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002436 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002437 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002438 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2439 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002440}
2441
2442void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
2443 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002444 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002445 Location index = locations->InAt(1);
2446
2447 switch (instruction->GetType()) {
2448 case Primitive::kPrimBoolean: {
2449 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002450 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002451 if (index.IsConstant()) {
2452 __ movzxb(out, Address(obj,
2453 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2454 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002455 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002456 }
2457 break;
2458 }
2459
2460 case Primitive::kPrimByte: {
2461 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002462 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002463 if (index.IsConstant()) {
2464 __ movsxb(out, Address(obj,
2465 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2466 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002467 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002468 }
2469 break;
2470 }
2471
2472 case Primitive::kPrimShort: {
2473 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002474 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002475 if (index.IsConstant()) {
2476 __ movsxw(out, Address(obj,
2477 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2478 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002479 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002480 }
2481 break;
2482 }
2483
2484 case Primitive::kPrimChar: {
2485 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002486 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002487 if (index.IsConstant()) {
2488 __ movzxw(out, Address(obj,
2489 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2490 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002491 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002492 }
2493 break;
2494 }
2495
2496 case Primitive::kPrimInt:
2497 case Primitive::kPrimNot: {
2498 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
2499 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002500 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002501 if (index.IsConstant()) {
2502 __ movl(out, Address(obj,
2503 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2504 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002505 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002506 }
2507 break;
2508 }
2509
2510 case Primitive::kPrimLong: {
2511 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002512 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002513 if (index.IsConstant()) {
2514 __ movq(out, Address(obj,
2515 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2516 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002517 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002518 }
2519 break;
2520 }
2521
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002522 case Primitive::kPrimFloat: {
2523 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002524 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002525 if (index.IsConstant()) {
2526 __ movss(out, Address(obj,
2527 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2528 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002529 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002530 }
2531 break;
2532 }
2533
2534 case Primitive::kPrimDouble: {
2535 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002536 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002537 if (index.IsConstant()) {
2538 __ movsd(out, Address(obj,
2539 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2540 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002541 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002542 }
2543 break;
2544 }
2545
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002546 case Primitive::kPrimVoid:
2547 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002548 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002549 }
2550}
2551
2552void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002553 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002554
2555 bool needs_write_barrier =
2556 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2557 bool needs_runtime_call = instruction->NeedsTypeCheck();
2558
Nicolas Geoffray39468442014-09-02 15:17:15 +01002559 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002560 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
2561 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002562 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002563 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2564 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2565 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002566 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002567 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002568 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002569 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2570 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002571 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002572 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002573 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
2574 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002575 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002576 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002577 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002578
2579 if (needs_write_barrier) {
2580 // Temporary registers for the write barrier.
2581 locations->AddTemp(Location::RequiresRegister());
2582 locations->AddTemp(Location::RequiresRegister());
2583 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002584 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002585}
2586
2587void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
2588 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002589 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002590 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002591 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002592 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002593 bool needs_runtime_call = locations->WillCall();
2594 bool needs_write_barrier =
2595 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002596
2597 switch (value_type) {
2598 case Primitive::kPrimBoolean:
2599 case Primitive::kPrimByte: {
2600 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002601 if (index.IsConstant()) {
2602 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002603 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002604 __ movb(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002605 } else {
Roland Levillain199f3362014-11-27 17:15:16 +00002606 __ movb(Address(obj, offset),
2607 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002608 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002609 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002610 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002611 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
2612 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002613 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002614 __ movb(Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002615 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2616 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002617 }
2618 break;
2619 }
2620
2621 case Primitive::kPrimShort:
2622 case Primitive::kPrimChar: {
2623 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002624 if (index.IsConstant()) {
2625 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002626 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002627 __ movw(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002628 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002629 DCHECK(value.IsConstant()) << value;
Roland Levillain199f3362014-11-27 17:15:16 +00002630 __ movw(Address(obj, offset),
2631 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002632 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002633 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002634 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002635 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002636 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
2637 value.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002638 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002639 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00002640 __ movw(Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002641 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2642 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002643 }
2644 break;
2645 }
2646
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002647 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002648 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002649 if (!needs_runtime_call) {
2650 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2651 if (index.IsConstant()) {
2652 size_t offset =
2653 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2654 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002655 __ movl(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002656 } else {
2657 DCHECK(value.IsConstant()) << value;
2658 __ movl(Address(obj, offset),
2659 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2660 }
2661 } else {
2662 DCHECK(index.IsRegister()) << index;
2663 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002664 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
2665 value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002666 } else {
2667 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00002668 __ movl(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002669 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2670 }
2671 }
2672
2673 if (needs_write_barrier) {
2674 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002675 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2676 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
2677 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<CpuRegister>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002678 }
2679 } else {
2680 DCHECK_EQ(value_type, Primitive::kPrimNot);
Roland Levillain199f3362014-11-27 17:15:16 +00002681 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject),
2682 true));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002683 DCHECK(!codegen_->IsLeafMethod());
2684 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2685 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002686 break;
2687 }
2688
2689 case Primitive::kPrimLong: {
2690 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002691 if (index.IsConstant()) {
2692 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002693 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002694 __ movq(Address(obj, offset), value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002695 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002696 DCHECK(value.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002697 __ movq(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
2698 value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002699 }
2700 break;
2701 }
2702
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002703 case Primitive::kPrimFloat: {
2704 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2705 if (index.IsConstant()) {
2706 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2707 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002708 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002709 } else {
2710 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002711 __ movss(Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset),
2712 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002713 }
2714 break;
2715 }
2716
2717 case Primitive::kPrimDouble: {
2718 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2719 if (index.IsConstant()) {
2720 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
2721 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002722 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002723 } else {
2724 DCHECK(value.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002725 __ movsd(Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset),
2726 value.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002727 }
2728 break;
2729 }
2730
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002731 case Primitive::kPrimVoid:
2732 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002733 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002734 }
2735}
2736
2737void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002738 LocationSummary* locations =
2739 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002740 locations->SetInAt(0, Location::RequiresRegister());
2741 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002742}
2743
2744void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
2745 LocationSummary* locations = instruction->GetLocations();
2746 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002747 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
2748 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002749 __ movl(out, Address(obj, offset));
2750}
2751
2752void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002753 LocationSummary* locations =
2754 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002755 locations->SetInAt(0, Location::RequiresRegister());
2756 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002757 if (instruction->HasUses()) {
2758 locations->SetOut(Location::SameAsFirstInput());
2759 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002760}
2761
2762void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
2763 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002764 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002765 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002766 codegen_->AddSlowPath(slow_path);
2767
Roland Levillain271ab9c2014-11-27 15:23:57 +00002768 CpuRegister index = locations->InAt(0).AsRegister<CpuRegister>();
2769 CpuRegister length = locations->InAt(1).AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002770
2771 __ cmpl(index, length);
2772 __ j(kAboveEqual, slow_path->GetEntryLabel());
2773}
2774
2775void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
2776 CpuRegister card,
2777 CpuRegister object,
2778 CpuRegister value) {
2779 Label is_null;
2780 __ testl(value, value);
2781 __ j(kEqual, &is_null);
2782 __ gs()->movq(card, Address::Absolute(
2783 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
2784 __ movq(temp, object);
2785 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
2786 __ movb(Address(temp, card, TIMES_1, 0), card);
2787 __ Bind(&is_null);
2788}
2789
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002790void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
2791 temp->SetLocations(nullptr);
2792}
2793
2794void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
2795 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002796 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002797}
2798
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002799void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002800 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002801 LOG(FATAL) << "Unimplemented";
2802}
2803
2804void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002805 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2806}
2807
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002808void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
2809 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2810}
2811
2812void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002813 HBasicBlock* block = instruction->GetBlock();
2814 if (block->GetLoopInformation() != nullptr) {
2815 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2816 // The back edge will generate the suspend check.
2817 return;
2818 }
2819 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2820 // The goto will generate the suspend check.
2821 return;
2822 }
2823 GenerateSuspendCheck(instruction, nullptr);
2824}
2825
2826void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
2827 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002828 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002829 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002830 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002831 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002832 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002833 if (successor == nullptr) {
2834 __ j(kNotEqual, slow_path->GetEntryLabel());
2835 __ Bind(slow_path->GetReturnLabel());
2836 } else {
2837 __ j(kEqual, codegen_->GetLabelOf(successor));
2838 __ jmp(slow_path->GetEntryLabel());
2839 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002840}
2841
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002842X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
2843 return codegen_->GetAssembler();
2844}
2845
2846void ParallelMoveResolverX86_64::EmitMove(size_t index) {
2847 MoveOperands* move = moves_.Get(index);
2848 Location source = move->GetSource();
2849 Location destination = move->GetDestination();
2850
2851 if (source.IsRegister()) {
2852 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002853 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002854 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002855 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00002856 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002857 } else {
2858 DCHECK(destination.IsDoubleStackSlot());
2859 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00002860 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002861 }
2862 } else if (source.IsStackSlot()) {
2863 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002864 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002865 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002866 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002867 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002868 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002869 } else {
2870 DCHECK(destination.IsStackSlot());
2871 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2872 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2873 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002874 } else if (source.IsDoubleStackSlot()) {
2875 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002876 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002877 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002878 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002879 __ movsd(destination.AsFpuRegister<XmmRegister>(),
2880 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002881 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01002882 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002883 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2884 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2885 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002886 } else if (source.IsConstant()) {
2887 HConstant* constant = source.GetConstant();
2888 if (constant->IsIntConstant()) {
2889 Immediate imm(constant->AsIntConstant()->GetValue());
2890 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002891 __ movl(destination.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002892 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002893 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002894 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2895 }
2896 } else if (constant->IsLongConstant()) {
2897 int64_t value = constant->AsLongConstant()->GetValue();
2898 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002899 __ movq(destination.AsRegister<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002900 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002901 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002902 __ movq(CpuRegister(TMP), Immediate(value));
2903 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2904 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002905 } else if (constant->IsFloatConstant()) {
2906 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
2907 if (destination.IsFpuRegister()) {
2908 __ movl(CpuRegister(TMP), imm);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002909 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002910 } else {
2911 DCHECK(destination.IsStackSlot()) << destination;
2912 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2913 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002914 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002915 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
2916 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
2917 if (destination.IsFpuRegister()) {
2918 __ movq(CpuRegister(TMP), imm);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002919 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002920 } else {
2921 DCHECK(destination.IsDoubleStackSlot()) << destination;
2922 __ movq(CpuRegister(TMP), imm);
2923 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2924 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002925 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002926 } else if (source.IsFpuRegister()) {
2927 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002928 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002929 } else if (destination.IsStackSlot()) {
2930 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00002931 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002932 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00002933 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002934 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00002935 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002936 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002937 }
2938}
2939
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002940void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002941 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002942 __ movl(Address(CpuRegister(RSP), mem), reg);
2943 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002944}
2945
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002946void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002947 ScratchRegisterScope ensure_scratch(
2948 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2949
2950 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2951 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2952 __ movl(CpuRegister(ensure_scratch.GetRegister()),
2953 Address(CpuRegister(RSP), mem2 + stack_offset));
2954 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2955 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
2956 CpuRegister(ensure_scratch.GetRegister()));
2957}
2958
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002959void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
2960 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2961 __ movq(Address(CpuRegister(RSP), mem), reg);
2962 __ movq(reg, CpuRegister(TMP));
2963}
2964
2965void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
2966 ScratchRegisterScope ensure_scratch(
2967 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2968
2969 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2970 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2971 __ movq(CpuRegister(ensure_scratch.GetRegister()),
2972 Address(CpuRegister(RSP), mem2 + stack_offset));
2973 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2974 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
2975 CpuRegister(ensure_scratch.GetRegister()));
2976}
2977
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002978void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
2979 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2980 __ movss(Address(CpuRegister(RSP), mem), reg);
2981 __ movd(reg, CpuRegister(TMP));
2982}
2983
2984void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
2985 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2986 __ movsd(Address(CpuRegister(RSP), mem), reg);
2987 __ movd(reg, CpuRegister(TMP));
2988}
2989
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002990void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
2991 MoveOperands* move = moves_.Get(index);
2992 Location source = move->GetSource();
2993 Location destination = move->GetDestination();
2994
2995 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002996 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002997 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002998 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002999 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003000 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003001 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003002 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
3003 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003004 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003005 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003006 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003007 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
3008 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003009 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003010 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
3011 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3012 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003013 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003014 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003015 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003016 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003017 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003018 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003019 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003020 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003021 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003022 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003023 }
3024}
3025
3026
3027void ParallelMoveResolverX86_64::SpillScratch(int reg) {
3028 __ pushq(CpuRegister(reg));
3029}
3030
3031
3032void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
3033 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003034}
3035
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003036void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
3037 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
3038 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3039 Immediate(mirror::Class::kStatusInitialized));
3040 __ j(kLess, slow_path->GetEntryLabel());
3041 __ Bind(slow_path->GetExitLabel());
3042 // No need for memory fence, thanks to the X86_64 memory model.
3043}
3044
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003045void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003046 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3047 ? LocationSummary::kCallOnSlowPath
3048 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003049 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003050 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003051 locations->SetOut(Location::RequiresRegister());
3052}
3053
3054void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003055 CpuRegister out = cls->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003056 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003057 DCHECK(!cls->CanCallRuntime());
3058 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003059 codegen_->LoadCurrentMethod(out);
3060 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3061 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003062 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003063 codegen_->LoadCurrentMethod(out);
3064 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3065 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003066 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3067 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3068 codegen_->AddSlowPath(slow_path);
3069 __ testl(out, out);
3070 __ j(kEqual, slow_path->GetEntryLabel());
3071 if (cls->MustGenerateClinitCheck()) {
3072 GenerateClassInitializationCheck(slow_path, out);
3073 } else {
3074 __ Bind(slow_path->GetExitLabel());
3075 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003076 }
3077}
3078
3079void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
3080 LocationSummary* locations =
3081 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3082 locations->SetInAt(0, Location::RequiresRegister());
3083 if (check->HasUses()) {
3084 locations->SetOut(Location::SameAsFirstInput());
3085 }
3086}
3087
3088void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003089 // We assume the class to not be null.
3090 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
3091 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003092 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003093 GenerateClassInitializationCheck(slow_path,
3094 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003095}
3096
3097void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3098 LocationSummary* locations =
3099 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3100 locations->SetInAt(0, Location::RequiresRegister());
3101 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3102}
3103
3104void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3105 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003106 CpuRegister cls = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003107 size_t offset = instruction->GetFieldOffset().SizeValue();
3108
3109 switch (instruction->GetType()) {
3110 case Primitive::kPrimBoolean: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003111 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003112 __ movzxb(out, Address(cls, offset));
3113 break;
3114 }
3115
3116 case Primitive::kPrimByte: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003117 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003118 __ movsxb(out, Address(cls, offset));
3119 break;
3120 }
3121
3122 case Primitive::kPrimShort: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003123 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003124 __ movsxw(out, Address(cls, offset));
3125 break;
3126 }
3127
3128 case Primitive::kPrimChar: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003129 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003130 __ movzxw(out, Address(cls, offset));
3131 break;
3132 }
3133
3134 case Primitive::kPrimInt:
3135 case Primitive::kPrimNot: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003136 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003137 __ movl(out, Address(cls, offset));
3138 break;
3139 }
3140
3141 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003142 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003143 __ movq(out, Address(cls, offset));
3144 break;
3145 }
3146
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003147 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003148 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003149 __ movss(out, Address(cls, offset));
3150 break;
3151 }
3152
3153 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003154 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003155 __ movsd(out, Address(cls, offset));
3156 break;
3157 }
3158
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003159 case Primitive::kPrimVoid:
3160 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3161 UNREACHABLE();
3162 }
3163}
3164
3165void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3166 LocationSummary* locations =
3167 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3168 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003169 bool needs_write_barrier =
3170 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->GetValue());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003171 locations->SetInAt(0, Location::RequiresRegister());
3172 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003173 if (needs_write_barrier) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003174 // Temporary registers for the write barrier.
3175 locations->AddTemp(Location::RequiresRegister());
3176 locations->AddTemp(Location::RequiresRegister());
3177 }
3178}
3179
3180void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3181 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003182 CpuRegister cls = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003183 size_t offset = instruction->GetFieldOffset().SizeValue();
3184 Primitive::Type field_type = instruction->GetFieldType();
3185
3186 switch (field_type) {
3187 case Primitive::kPrimBoolean:
3188 case Primitive::kPrimByte: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003189 CpuRegister value = locations->InAt(1).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003190 __ movb(Address(cls, offset), value);
3191 break;
3192 }
3193
3194 case Primitive::kPrimShort:
3195 case Primitive::kPrimChar: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003196 CpuRegister value = locations->InAt(1).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003197 __ movw(Address(cls, offset), value);
3198 break;
3199 }
3200
3201 case Primitive::kPrimInt:
3202 case Primitive::kPrimNot: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003203 CpuRegister value = locations->InAt(1).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003204 __ movl(Address(cls, offset), value);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003205 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->GetValue())) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003206 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
3207 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003208 codegen_->MarkGCCard(temp, card, cls, value);
3209 }
3210 break;
3211 }
3212
3213 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003214 CpuRegister value = locations->InAt(1).AsRegister<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003215 __ movq(Address(cls, offset), value);
3216 break;
3217 }
3218
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003219 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003220 XmmRegister value = locations->InAt(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003221 __ movss(Address(cls, offset), value);
3222 break;
3223 }
3224
3225 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003226 XmmRegister value = locations->InAt(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003227 __ movsd(Address(cls, offset), value);
3228 break;
3229 }
3230
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003231 case Primitive::kPrimVoid:
3232 LOG(FATAL) << "Unreachable type " << field_type;
3233 UNREACHABLE();
3234 }
3235}
3236
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003237void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
3238 LocationSummary* locations =
3239 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3240 locations->SetOut(Location::RequiresRegister());
3241}
3242
3243void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
3244 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
3245 codegen_->AddSlowPath(slow_path);
3246
Roland Levillain271ab9c2014-11-27 15:23:57 +00003247 CpuRegister out = load->GetLocations()->Out().AsRegister<CpuRegister>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003248 codegen_->LoadCurrentMethod(CpuRegister(out));
Mathieu Chartiereace4582014-11-24 18:29:54 -08003249 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3250 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003251 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3252 __ testl(out, out);
3253 __ j(kEqual, slow_path->GetEntryLabel());
3254 __ Bind(slow_path->GetExitLabel());
3255}
3256
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003257void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
3258 LocationSummary* locations =
3259 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3260 locations->SetOut(Location::RequiresRegister());
3261}
3262
3263void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
3264 Address address = Address::Absolute(
3265 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003266 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003267 __ gs()->movl(address, Immediate(0));
3268}
3269
3270void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
3271 LocationSummary* locations =
3272 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3273 InvokeRuntimeCallingConvention calling_convention;
3274 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3275}
3276
3277void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
3278 __ gs()->call(
3279 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
3280 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3281}
3282
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003283void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003284 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3285 ? LocationSummary::kNoCall
3286 : LocationSummary::kCallOnSlowPath;
3287 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3288 locations->SetInAt(0, Location::RequiresRegister());
3289 locations->SetInAt(1, Location::Any());
3290 locations->SetOut(Location::RequiresRegister());
3291}
3292
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003293void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003294 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003295 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003296 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003297 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003298 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3299 Label done, zero;
3300 SlowPathCodeX86_64* slow_path = nullptr;
3301
3302 // Return 0 if `obj` is null.
3303 // TODO: avoid this check if we know obj is not null.
3304 __ testl(obj, obj);
3305 __ j(kEqual, &zero);
3306 // Compare the class of `obj` with `cls`.
3307 __ movl(out, Address(obj, class_offset));
3308 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003309 __ cmpl(out, cls.AsRegister<CpuRegister>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003310 } else {
3311 DCHECK(cls.IsStackSlot()) << cls;
3312 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
3313 }
3314 if (instruction->IsClassFinal()) {
3315 // Classes must be equal for the instanceof to succeed.
3316 __ j(kNotEqual, &zero);
3317 __ movl(out, Immediate(1));
3318 __ jmp(&done);
3319 } else {
3320 // If the classes are not equal, we go into a slow path.
3321 DCHECK(locations->OnlyCallsOnSlowPath());
3322 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003323 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003324 codegen_->AddSlowPath(slow_path);
3325 __ j(kNotEqual, slow_path->GetEntryLabel());
3326 __ movl(out, Immediate(1));
3327 __ jmp(&done);
3328 }
3329 __ Bind(&zero);
3330 __ movl(out, Immediate(0));
3331 if (slow_path != nullptr) {
3332 __ Bind(slow_path->GetExitLabel());
3333 }
3334 __ Bind(&done);
3335}
3336
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003337void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
3338 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3339 instruction, LocationSummary::kCallOnSlowPath);
3340 locations->SetInAt(0, Location::RequiresRegister());
3341 locations->SetInAt(1, Location::Any());
3342 locations->AddTemp(Location::RequiresRegister());
3343}
3344
3345void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
3346 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003347 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003348 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003349 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003350 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3351 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
3352 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3353 codegen_->AddSlowPath(slow_path);
3354
3355 // TODO: avoid this check if we know obj is not null.
3356 __ testl(obj, obj);
3357 __ j(kEqual, slow_path->GetExitLabel());
3358 // Compare the class of `obj` with `cls`.
3359 __ movl(temp, Address(obj, class_offset));
3360 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003361 __ cmpl(temp, cls.AsRegister<CpuRegister>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003362 } else {
3363 DCHECK(cls.IsStackSlot()) << cls;
3364 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
3365 }
3366 // Classes must be equal for the checkcast to succeed.
3367 __ j(kNotEqual, slow_path->GetEntryLabel());
3368 __ Bind(slow_path->GetExitLabel());
3369}
3370
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003371void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3372 LocationSummary* locations =
3373 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3374 InvokeRuntimeCallingConvention calling_convention;
3375 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3376}
3377
3378void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3379 __ gs()->call(Address::Absolute(instruction->IsEnter()
3380 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
3381 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
3382 true));
3383 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3384}
3385
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003386void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3387void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3388void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3389
3390void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3391 LocationSummary* locations =
3392 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3393 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3394 || instruction->GetResultType() == Primitive::kPrimLong);
3395 locations->SetInAt(0, Location::RequiresRegister());
3396 if (instruction->GetType() == Primitive::kPrimInt) {
3397 locations->SetInAt(1, Location::Any());
3398 } else {
3399 // Request a register to avoid loading a 64bits constant.
3400 locations->SetInAt(1, Location::RequiresRegister());
3401 }
3402 locations->SetOut(Location::SameAsFirstInput());
3403}
3404
3405void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
3406 HandleBitwiseOperation(instruction);
3407}
3408
3409void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
3410 HandleBitwiseOperation(instruction);
3411}
3412
3413void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
3414 HandleBitwiseOperation(instruction);
3415}
3416
3417void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3418 LocationSummary* locations = instruction->GetLocations();
3419 Location first = locations->InAt(0);
3420 Location second = locations->InAt(1);
3421 DCHECK(first.Equals(locations->Out()));
3422
3423 if (instruction->GetResultType() == Primitive::kPrimInt) {
3424 if (second.IsRegister()) {
3425 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003426 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003427 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003428 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003429 } else {
3430 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003431 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003432 }
3433 } else if (second.IsConstant()) {
3434 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
3435 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003436 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003437 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003438 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003439 } else {
3440 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003441 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003442 }
3443 } else {
3444 Address address(CpuRegister(RSP), second.GetStackIndex());
3445 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003446 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003447 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003448 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003449 } else {
3450 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003451 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003452 }
3453 }
3454 } else {
3455 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3456 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003457 __ andq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003458 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003459 __ orq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003460 } else {
3461 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003462 __ xorq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003463 }
3464 }
3465}
3466
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003467} // namespace x86_64
3468} // namespace art