blob: 82bb7db7de6021315c18c274bdf3d3337b2e8da4 [file] [log] [blame]
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86_64.h"
18
19#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010020#include "gc/accounting/card_table.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070021#include "mirror/array-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010022#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010023#include "mirror/class.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010024#include "mirror/object_reference.h"
25#include "thread.h"
26#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010027#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010028#include "utils/x86_64/assembler_x86_64.h"
29#include "utils/x86_64/managed_register_x86_64.h"
30
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010031namespace art {
32
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010033namespace x86_64 {
34
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010035static constexpr bool kExplicitStackOverflowCheck = false;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010036
37// Some x86_64 instructions require a register to be available as temp.
38static constexpr Register TMP = R11;
39
40static constexpr int kNumberOfPushedRegistersAtEntry = 1;
41static constexpr int kCurrentMethodStackOffset = 0;
42
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010043static constexpr Register kRuntimeParameterCoreRegisters[] = { RDI, RSI, RDX };
44static constexpr size_t kRuntimeParameterCoreRegistersLength =
45 arraysize(kRuntimeParameterCoreRegisters);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010046static constexpr FloatRegister kRuntimeParameterFpuRegisters[] = { };
47static constexpr size_t kRuntimeParameterFpuRegistersLength = 0;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010048
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010049class InvokeRuntimeCallingConvention : public CallingConvention<Register, FloatRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010050 public:
51 InvokeRuntimeCallingConvention()
52 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010053 kRuntimeParameterCoreRegistersLength,
54 kRuntimeParameterFpuRegisters,
55 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010056
57 private:
58 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
59};
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010060
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061#define __ reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler())->
62
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010063class SlowPathCodeX86_64 : public SlowPathCode {
64 public:
65 SlowPathCodeX86_64() : entry_label_(), exit_label_() {}
66
67 Label* GetEntryLabel() { return &entry_label_; }
68 Label* GetExitLabel() { return &exit_label_; }
69
70 private:
71 Label entry_label_;
72 Label exit_label_;
73
74 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86_64);
75};
76
77class NullCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010078 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010079 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080
81 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
82 __ Bind(GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010083 __ gs()->call(
84 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowNullPointer), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +010085 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010086 }
87
88 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010089 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010090 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
91};
92
Calin Juravled0d48522014-11-04 16:40:20 +000093class DivZeroCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
94 public:
95 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : instruction_(instruction) {}
96
97 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
98 __ Bind(GetEntryLabel());
99 __ gs()->call(
100 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowDivZero), true));
101 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
102 }
103
104 private:
105 HDivZeroCheck* const instruction_;
106 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
107};
108
109class DivMinusOneSlowPathX86_64 : public SlowPathCodeX86_64 {
110 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000111 explicit DivMinusOneSlowPathX86_64(Register reg, Primitive::Type type)
112 : reg_(reg), type_(type) {}
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) {
117 __ negl(CpuRegister(reg_));
118 } else {
119 DCHECK_EQ(Primitive::kPrimLong, type_);
120 __ negq(CpuRegister(reg_));
121 }
Calin Juravled0d48522014-11-04 16:40:20 +0000122 __ jmp(GetExitLabel());
123 }
124
125 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000126 const Register reg_;
127 const Primitive::Type type_;
Calin Juravled0d48522014-11-04 16:40:20 +0000128 DISALLOW_COPY_AND_ASSIGN(DivMinusOneSlowPathX86_64);
129};
130
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100131class StackOverflowCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100132 public:
133 StackOverflowCheckSlowPathX86_64() {}
134
135 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
136 __ Bind(GetEntryLabel());
137 __ addq(CpuRegister(RSP),
138 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
139 __ gs()->jmp(
140 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowStackOverflow), true));
141 }
142
143 private:
144 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86_64);
145};
146
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100147class SuspendCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000148 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100149 explicit SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
150 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000151
152 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100153 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000154 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100155 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000156 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pTestSuspend), true));
157 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100158 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100159 if (successor_ == nullptr) {
160 __ jmp(GetReturnLabel());
161 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100162 __ jmp(x64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100163 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000164 }
165
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100166 Label* GetReturnLabel() {
167 DCHECK(successor_ == nullptr);
168 return &return_label_;
169 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000170
171 private:
172 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100173 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000174 Label return_label_;
175
176 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
177};
178
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100179class BoundsCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100180 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100181 BoundsCheckSlowPathX86_64(HBoundsCheck* instruction,
182 Location index_location,
183 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100184 : instruction_(instruction),
185 index_location_(index_location),
186 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100187
188 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100189 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100190 __ Bind(GetEntryLabel());
191 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100192 x64_codegen->Move(
193 Location::RegisterLocation(calling_convention.GetRegisterAt(0)), index_location_);
194 x64_codegen->Move(
195 Location::RegisterLocation(calling_convention.GetRegisterAt(1)), length_location_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100196 __ gs()->call(Address::Absolute(
197 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowArrayBounds), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100198 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100199 }
200
201 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100202 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100203 const Location index_location_;
204 const Location length_location_;
205
206 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
207};
208
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000209class LoadClassSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100210 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000211 LoadClassSlowPathX86_64(HLoadClass* cls,
212 HInstruction* at,
213 uint32_t dex_pc,
214 bool do_clinit)
215 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
216 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
217 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100218
219 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000220 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100221 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
222 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100223
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000224 codegen->SaveLiveRegisters(locations);
225
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100226 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000227 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100228 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000229 __ gs()->call(Address::Absolute((do_clinit_
230 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeStaticStorage)
231 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeType)) , true));
232 codegen->RecordPcInfo(at_, dex_pc_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100233
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000234 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000235 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000236 if (out.IsValid()) {
237 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
238 x64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000239 }
240
241 codegen->RestoreLiveRegisters(locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100242 __ jmp(GetExitLabel());
243 }
244
245 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000246 // The class this slow path will load.
247 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100248
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000249 // The instruction where this slow path is happening.
250 // (Might be the load class or an initialization check).
251 HInstruction* const at_;
252
253 // The dex PC of `at_`.
254 const uint32_t dex_pc_;
255
256 // Whether to initialize the class.
257 const bool do_clinit_;
258
259 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100260};
261
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000262class LoadStringSlowPathX86_64 : public SlowPathCodeX86_64 {
263 public:
264 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
265
266 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
267 LocationSummary* locations = instruction_->GetLocations();
268 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
269
270 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
271 __ Bind(GetEntryLabel());
272 codegen->SaveLiveRegisters(locations);
273
274 InvokeRuntimeCallingConvention calling_convention;
275 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(0)));
276 __ movl(CpuRegister(calling_convention.GetRegisterAt(1)),
277 Immediate(instruction_->GetStringIndex()));
278 __ gs()->call(Address::Absolute(
279 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pResolveString), true));
280 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
281 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
282 codegen->RestoreLiveRegisters(locations);
283 __ jmp(GetExitLabel());
284 }
285
286 private:
287 HLoadString* const instruction_;
288
289 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
290};
291
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000292class TypeCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
293 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000294 TypeCheckSlowPathX86_64(HInstruction* instruction,
295 Location class_to_check,
296 Location object_class,
297 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000298 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000299 class_to_check_(class_to_check),
300 object_class_(object_class),
301 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000302
303 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
304 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000305 DCHECK(instruction_->IsCheckCast()
306 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000307
308 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
309 __ Bind(GetEntryLabel());
310 codegen->SaveLiveRegisters(locations);
311
312 // We're moving two locations to locations that could overlap, so we need a parallel
313 // move resolver.
314 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000315 MoveOperands move1(class_to_check_,
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000316 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
317 nullptr);
318 MoveOperands move2(object_class_,
319 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
320 nullptr);
321 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
322 parallel_move.AddMove(&move1);
323 parallel_move.AddMove(&move2);
324 x64_codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
325
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000326 if (instruction_->IsInstanceOf()) {
327 __ gs()->call(
328 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInstanceofNonTrivial), true));
329 } else {
330 DCHECK(instruction_->IsCheckCast());
331 __ gs()->call(
332 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pCheckCast), true));
333 }
334 codegen->RecordPcInfo(instruction_, dex_pc_);
335
336 if (instruction_->IsInstanceOf()) {
337 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
338 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000339
340 codegen->RestoreLiveRegisters(locations);
341 __ jmp(GetExitLabel());
342 }
343
344 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000345 HInstruction* const instruction_;
346 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000347 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000348 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000349
350 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
351};
352
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100353#undef __
354#define __ reinterpret_cast<X86_64Assembler*>(GetAssembler())->
355
Dave Allison20dfc792014-06-16 20:44:29 -0700356inline Condition X86_64Condition(IfCondition cond) {
357 switch (cond) {
358 case kCondEQ: return kEqual;
359 case kCondNE: return kNotEqual;
360 case kCondLT: return kLess;
361 case kCondLE: return kLessEqual;
362 case kCondGT: return kGreater;
363 case kCondGE: return kGreaterEqual;
364 default:
365 LOG(FATAL) << "Unknown if condition";
366 }
367 return kEqual;
368}
369
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100370void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
371 stream << X86_64ManagedRegister::FromCpuRegister(Register(reg));
372}
373
374void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
375 stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg));
376}
377
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100378size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
379 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
380 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100381}
382
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100383size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
384 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
385 return kX86_64WordSize;
386}
387
388size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
389 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
390 return kX86_64WordSize;
391}
392
393size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
394 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
395 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100396}
397
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100398CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100399 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfFloatRegisters, 0),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100400 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100401 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000402 instruction_visitor_(graph, this),
403 move_resolver_(graph->GetArena(), this) {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100404
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100405size_t CodeGeneratorX86_64::FrameEntrySpillSize() const {
406 return kNumberOfPushedRegistersAtEntry * kX86_64WordSize;
407}
408
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100409InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
410 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100411 : HGraphVisitor(graph),
412 assembler_(codegen->GetAssembler()),
413 codegen_(codegen) {}
414
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100415Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100416 switch (type) {
417 case Primitive::kPrimLong:
418 case Primitive::kPrimByte:
419 case Primitive::kPrimBoolean:
420 case Primitive::kPrimChar:
421 case Primitive::kPrimShort:
422 case Primitive::kPrimInt:
423 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100424 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100425 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100426 }
427
428 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100429 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100430 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100431 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100432 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100433
434 case Primitive::kPrimVoid:
435 LOG(FATAL) << "Unreachable type " << type;
436 }
437
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100438 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100439}
440
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100441void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100442 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100443 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100444
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000445 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100446 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000447
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100448 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100449 blocked_core_registers_[RBX] = true;
450 blocked_core_registers_[RBP] = true;
451 blocked_core_registers_[R12] = true;
452 blocked_core_registers_[R13] = true;
453 blocked_core_registers_[R14] = true;
454 blocked_core_registers_[R15] = true;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100455
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100456 blocked_fpu_registers_[XMM12] = true;
457 blocked_fpu_registers_[XMM13] = true;
458 blocked_fpu_registers_[XMM14] = true;
459 blocked_fpu_registers_[XMM15] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100460}
461
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100462void CodeGeneratorX86_64::GenerateFrameEntry() {
463 // Create a fake register to mimic Quick.
464 static const int kFakeReturnRegister = 16;
465 core_spill_mask_ |= (1 << kFakeReturnRegister);
466
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100467 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700468 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100469
470 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
471 __ testq(CpuRegister(RAX), Address(
472 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100473 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100474 }
475
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100476 // The return PC has already been pushed on the stack.
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100477 __ subq(CpuRegister(RSP),
478 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
479
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100480 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100481 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86_64();
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100482 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100483
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100484 __ gs()->cmpq(CpuRegister(RSP),
485 Address::Absolute(Thread::StackEndOffset<kX86_64WordSize>(), true));
486 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100487 }
488
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100489 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
490}
491
492void CodeGeneratorX86_64::GenerateFrameExit() {
493 __ addq(CpuRegister(RSP),
494 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
495}
496
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100497void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
498 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100499}
500
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100501void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100502 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
503}
504
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100505Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
506 switch (load->GetType()) {
507 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100508 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100509 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
510 break;
511
512 case Primitive::kPrimInt:
513 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100514 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100515 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100516
517 case Primitive::kPrimBoolean:
518 case Primitive::kPrimByte:
519 case Primitive::kPrimChar:
520 case Primitive::kPrimShort:
521 case Primitive::kPrimVoid:
522 LOG(FATAL) << "Unexpected type " << load->GetType();
523 }
524
525 LOG(FATAL) << "Unreachable";
526 return Location();
527}
528
529void CodeGeneratorX86_64::Move(Location destination, Location source) {
530 if (source.Equals(destination)) {
531 return;
532 }
533 if (destination.IsRegister()) {
534 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100535 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100536 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100537 __ movd(destination.As<CpuRegister>(), source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100538 } else if (source.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100539 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100540 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100541 } else {
542 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100543 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100544 Address(CpuRegister(RSP), source.GetStackIndex()));
545 }
546 } else if (destination.IsFpuRegister()) {
547 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100548 __ movd(destination.As<XmmRegister>(), source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100549 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100550 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100551 } else if (source.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100552 __ movss(destination.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100553 Address(CpuRegister(RSP), source.GetStackIndex()));
554 } else {
555 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100556 __ movsd(destination.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100557 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100558 }
559 } else if (destination.IsStackSlot()) {
560 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100561 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100562 source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100563 } else if (source.IsFpuRegister()) {
564 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100565 source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100566 } else {
567 DCHECK(source.IsStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000568 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
569 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100570 }
571 } else {
572 DCHECK(destination.IsDoubleStackSlot());
573 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100574 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100575 source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100576 } else if (source.IsFpuRegister()) {
577 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100578 source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100579 } else {
580 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000581 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
582 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100583 }
584 }
585}
586
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100587void CodeGeneratorX86_64::Move(HInstruction* instruction,
588 Location location,
589 HInstruction* move_for) {
Roland Levillain476df552014-10-09 17:51:36 +0100590 if (instruction->IsIntConstant()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100591 Immediate imm(instruction->AsIntConstant()->GetValue());
592 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100593 __ movl(location.As<CpuRegister>(), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100594 } else if (location.IsStackSlot()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100595 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100596 } else {
597 DCHECK(location.IsConstant());
598 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100599 }
Roland Levillain476df552014-10-09 17:51:36 +0100600 } else if (instruction->IsLongConstant()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100601 int64_t value = instruction->AsLongConstant()->GetValue();
602 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100603 __ movq(location.As<CpuRegister>(), Immediate(value));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100604 } else if (location.IsDoubleStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000605 __ movq(CpuRegister(TMP), Immediate(value));
606 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100607 } else {
608 DCHECK(location.IsConstant());
609 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100610 }
Roland Levillain476df552014-10-09 17:51:36 +0100611 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100612 switch (instruction->GetType()) {
613 case Primitive::kPrimBoolean:
614 case Primitive::kPrimByte:
615 case Primitive::kPrimChar:
616 case Primitive::kPrimShort:
617 case Primitive::kPrimInt:
618 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100619 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100620 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
621 break;
622
623 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100624 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100625 Move(location, Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
626 break;
627
628 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100629 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100630 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000631 } else if (instruction->IsTemporary()) {
632 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
633 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100634 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100635 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100636 switch (instruction->GetType()) {
637 case Primitive::kPrimBoolean:
638 case Primitive::kPrimByte:
639 case Primitive::kPrimChar:
640 case Primitive::kPrimShort:
641 case Primitive::kPrimInt:
642 case Primitive::kPrimNot:
643 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100644 case Primitive::kPrimFloat:
645 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100646 Move(location, instruction->GetLocations()->Out());
647 break;
648
649 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100650 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100651 }
652 }
653}
654
655void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
656 got->SetLocations(nullptr);
657}
658
659void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
660 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100661 DCHECK(!successor->IsExitBlock());
662
663 HBasicBlock* block = got->GetBlock();
664 HInstruction* previous = got->GetPrevious();
665
666 HLoopInformation* info = block->GetLoopInformation();
667 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
668 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
669 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
670 return;
671 }
672
673 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
674 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
675 }
676 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100677 __ jmp(codegen_->GetLabelOf(successor));
678 }
679}
680
681void LocationsBuilderX86_64::VisitExit(HExit* exit) {
682 exit->SetLocations(nullptr);
683}
684
685void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700686 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100687 if (kIsDebugBuild) {
688 __ Comment("Unreachable");
689 __ int3();
690 }
691}
692
693void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100694 LocationSummary* locations =
695 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100696 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100697 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100698 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100699 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100700}
701
702void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700703 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100704 if (cond->IsIntConstant()) {
705 // Constant condition, statically compared against 1.
706 int32_t cond_value = cond->AsIntConstant()->GetValue();
707 if (cond_value == 1) {
708 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
709 if_instr->IfTrueSuccessor())) {
710 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100711 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100712 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100713 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100714 DCHECK_EQ(cond_value, 0);
715 }
716 } else {
717 bool materialized =
718 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
719 // Moves do not affect the eflags register, so if the condition is
720 // evaluated just before the if, we don't need to evaluate it
721 // again.
722 bool eflags_set = cond->IsCondition()
723 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
724 if (materialized) {
725 if (!eflags_set) {
726 // Materialized condition, compare against 0.
727 Location lhs = if_instr->GetLocations()->InAt(0);
728 if (lhs.IsRegister()) {
729 __ cmpl(lhs.As<CpuRegister>(), Immediate(0));
730 } else {
731 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
732 Immediate(0));
733 }
734 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
735 } else {
736 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
737 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
738 }
739 } else {
740 Location lhs = cond->GetLocations()->InAt(0);
741 Location rhs = cond->GetLocations()->InAt(1);
742 if (rhs.IsRegister()) {
743 __ cmpl(lhs.As<CpuRegister>(), rhs.As<CpuRegister>());
744 } else if (rhs.IsConstant()) {
745 __ cmpl(lhs.As<CpuRegister>(),
746 Immediate(rhs.GetConstant()->AsIntConstant()->GetValue()));
747 } else {
748 __ cmpl(lhs.As<CpuRegister>(),
749 Address(CpuRegister(RSP), rhs.GetStackIndex()));
750 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100751 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
752 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700753 }
Dave Allison20dfc792014-06-16 20:44:29 -0700754 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100755 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
756 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700757 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100758 }
759}
760
761void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
762 local->SetLocations(nullptr);
763}
764
765void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
766 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
767}
768
769void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
770 local->SetLocations(nullptr);
771}
772
773void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
774 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700775 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100776}
777
778void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100779 LocationSummary* locations =
780 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100781 switch (store->InputAt(1)->GetType()) {
782 case Primitive::kPrimBoolean:
783 case Primitive::kPrimByte:
784 case Primitive::kPrimChar:
785 case Primitive::kPrimShort:
786 case Primitive::kPrimInt:
787 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100788 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100789 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
790 break;
791
792 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100793 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100794 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
795 break;
796
797 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100798 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100799 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100800}
801
802void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700803 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100804}
805
Dave Allison20dfc792014-06-16 20:44:29 -0700806void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100807 LocationSummary* locations =
808 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100809 locations->SetInAt(0, Location::RequiresRegister());
810 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100811 if (comp->NeedsMaterialization()) {
812 locations->SetOut(Location::RequiresRegister());
813 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100814}
815
Dave Allison20dfc792014-06-16 20:44:29 -0700816void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
817 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100818 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100819 CpuRegister reg = locations->Out().As<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100820 // Clear register: setcc only sets the low byte.
821 __ xorq(reg, reg);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100822 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100823 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100824 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100825 } else if (locations->InAt(1).IsConstant()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100826 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100827 Immediate(locations->InAt(1).GetConstant()->AsIntConstant()->GetValue()));
828 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100829 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100830 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
831 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100832 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700833 }
834}
835
836void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
837 VisitCondition(comp);
838}
839
840void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
841 VisitCondition(comp);
842}
843
844void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
845 VisitCondition(comp);
846}
847
848void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
849 VisitCondition(comp);
850}
851
852void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
853 VisitCondition(comp);
854}
855
856void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
857 VisitCondition(comp);
858}
859
860void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
861 VisitCondition(comp);
862}
863
864void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
865 VisitCondition(comp);
866}
867
868void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
869 VisitCondition(comp);
870}
871
872void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
873 VisitCondition(comp);
874}
875
876void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
877 VisitCondition(comp);
878}
879
880void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
881 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100882}
883
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100884void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100885 LocationSummary* locations =
886 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100887 locations->SetInAt(0, Location::RequiresRegister());
888 locations->SetInAt(1, Location::RequiresRegister());
889 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100890}
891
892void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
893 Label greater, done;
894 LocationSummary* locations = compare->GetLocations();
895 switch (compare->InputAt(0)->GetType()) {
896 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100897 __ cmpq(locations->InAt(0).As<CpuRegister>(),
898 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100899 break;
900 default:
901 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
902 }
903
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100904 CpuRegister output = locations->Out().As<CpuRegister>();
905 __ movl(output, Immediate(0));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100906 __ j(kEqual, &done);
907 __ j(kGreater, &greater);
908
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100909 __ movl(output, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100910 __ jmp(&done);
911
912 __ Bind(&greater);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100913 __ movl(output, Immediate(1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100914
915 __ Bind(&done);
916}
917
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100918void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100919 LocationSummary* locations =
920 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100921 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100922}
923
924void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100925 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700926 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100927}
928
929void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100930 LocationSummary* locations =
931 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100932 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100933}
934
935void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100936 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700937 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100938}
939
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100940void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
941 LocationSummary* locations =
942 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
943 locations->SetOut(Location::ConstantLocation(constant));
944}
945
946void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
947 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700948 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100949}
950
951void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
952 LocationSummary* locations =
953 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
954 locations->SetOut(Location::ConstantLocation(constant));
955}
956
957void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
958 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700959 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100960}
961
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100962void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
963 ret->SetLocations(nullptr);
964}
965
966void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700967 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100968 codegen_->GenerateFrameExit();
969 __ ret();
970}
971
972void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100973 LocationSummary* locations =
974 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100975 switch (ret->InputAt(0)->GetType()) {
976 case Primitive::kPrimBoolean:
977 case Primitive::kPrimByte:
978 case Primitive::kPrimChar:
979 case Primitive::kPrimShort:
980 case Primitive::kPrimInt:
981 case Primitive::kPrimNot:
982 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100983 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100984 break;
985
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100986 case Primitive::kPrimFloat:
987 case Primitive::kPrimDouble:
988 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100989 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100990 break;
991
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100992 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100993 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100994 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100995}
996
997void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
998 if (kIsDebugBuild) {
999 switch (ret->InputAt(0)->GetType()) {
1000 case Primitive::kPrimBoolean:
1001 case Primitive::kPrimByte:
1002 case Primitive::kPrimChar:
1003 case Primitive::kPrimShort:
1004 case Primitive::kPrimInt:
1005 case Primitive::kPrimNot:
1006 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001007 DCHECK_EQ(ret->GetLocations()->InAt(0).As<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001008 break;
1009
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001010 case Primitive::kPrimFloat:
1011 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001012 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001013 XMM0);
1014 break;
1015
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001016 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001017 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001018 }
1019 }
1020 codegen_->GenerateFrameExit();
1021 __ ret();
1022}
1023
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001024Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
1025 switch (type) {
1026 case Primitive::kPrimBoolean:
1027 case Primitive::kPrimByte:
1028 case Primitive::kPrimChar:
1029 case Primitive::kPrimShort:
1030 case Primitive::kPrimInt:
1031 case Primitive::kPrimNot: {
1032 uint32_t index = gp_index_++;
1033 stack_index_++;
1034 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001035 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001036 } else {
1037 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1038 }
1039 }
1040
1041 case Primitive::kPrimLong: {
1042 uint32_t index = gp_index_;
1043 stack_index_ += 2;
1044 if (index < calling_convention.GetNumberOfRegisters()) {
1045 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001046 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001047 } else {
1048 gp_index_ += 2;
1049 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1050 }
1051 }
1052
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001053 case Primitive::kPrimFloat: {
1054 uint32_t index = fp_index_++;
1055 stack_index_++;
1056 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001057 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001058 } else {
1059 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1060 }
1061 }
1062
1063 case Primitive::kPrimDouble: {
1064 uint32_t index = fp_index_++;
1065 stack_index_ += 2;
1066 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001067 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001068 } else {
1069 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1070 }
1071 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001072
1073 case Primitive::kPrimVoid:
1074 LOG(FATAL) << "Unexpected parameter type " << type;
1075 break;
1076 }
1077 return Location();
1078}
1079
1080void LocationsBuilderX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001081 HandleInvoke(invoke);
1082}
1083
1084void InstructionCodeGeneratorX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001085 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001086 // TODO: Implement all kinds of calls:
1087 // 1) boot -> boot
1088 // 2) app -> boot
1089 // 3) app -> app
1090 //
1091 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1092
1093 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001094 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001095 // temp = temp->dex_cache_resolved_methods_;
1096 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
1097 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001098 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001099 // (temp + offset_of_quick_compiled_code)()
1100 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
1101
1102 DCHECK(!codegen_->IsLeafMethod());
1103 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1104}
1105
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001106void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001107 LocationSummary* locations =
1108 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001109 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001110
1111 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001112 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001113 HInstruction* input = invoke->InputAt(i);
1114 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1115 }
1116
1117 switch (invoke->GetType()) {
1118 case Primitive::kPrimBoolean:
1119 case Primitive::kPrimByte:
1120 case Primitive::kPrimChar:
1121 case Primitive::kPrimShort:
1122 case Primitive::kPrimInt:
1123 case Primitive::kPrimNot:
1124 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001125 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001126 break;
1127
1128 case Primitive::kPrimVoid:
1129 break;
1130
1131 case Primitive::kPrimDouble:
1132 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001133 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001134 break;
1135 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001136}
1137
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001138void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1139 HandleInvoke(invoke);
1140}
1141
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001142void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001143 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001144 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1145 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1146 LocationSummary* locations = invoke->GetLocations();
1147 Location receiver = locations->InAt(0);
1148 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1149 // temp = object->GetClass();
1150 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001151 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1152 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001153 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001154 __ movl(temp, Address(receiver.As<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001155 }
1156 // temp = temp->GetMethodAt(method_offset);
1157 __ movl(temp, Address(temp, method_offset));
1158 // call temp->GetEntryPoint();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001159 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
1160
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001161 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001162 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001163}
1164
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001165void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1166 HandleInvoke(invoke);
1167 // Add the hidden argument.
1168 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1169}
1170
1171void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1172 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1173 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
1174 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1175 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1176 LocationSummary* locations = invoke->GetLocations();
1177 Location receiver = locations->InAt(0);
1178 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1179
1180 // Set the hidden argument.
1181 __ movq(invoke->GetLocations()->GetTemp(1).As<CpuRegister>(),
1182 Immediate(invoke->GetDexMethodIndex()));
1183
1184 // temp = object->GetClass();
1185 if (receiver.IsStackSlot()) {
1186 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1187 __ movl(temp, Address(temp, class_offset));
1188 } else {
1189 __ movl(temp, Address(receiver.As<CpuRegister>(), class_offset));
1190 }
1191 // temp = temp->GetImtEntryAt(method_offset);
1192 __ movl(temp, Address(temp, method_offset));
1193 // call temp->GetEntryPoint();
1194 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
1195
1196 DCHECK(!codegen_->IsLeafMethod());
1197 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1198}
1199
Roland Levillain88cb1752014-10-20 16:36:47 +01001200void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1201 LocationSummary* locations =
1202 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1203 switch (neg->GetResultType()) {
1204 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001205 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001206 locations->SetInAt(0, Location::RequiresRegister());
1207 locations->SetOut(Location::SameAsFirstInput());
1208 break;
1209
Roland Levillain88cb1752014-10-20 16:36:47 +01001210 case Primitive::kPrimFloat:
1211 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001212 locations->SetInAt(0, Location::RequiresFpuRegister());
1213 // Output overlaps as we need a fresh (zero-initialized)
1214 // register to perform subtraction from zero.
1215 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001216 break;
1217
1218 default:
1219 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1220 }
1221}
1222
1223void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1224 LocationSummary* locations = neg->GetLocations();
1225 Location out = locations->Out();
1226 Location in = locations->InAt(0);
1227 switch (neg->GetResultType()) {
1228 case Primitive::kPrimInt:
1229 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001230 DCHECK(in.Equals(out));
Roland Levillain88cb1752014-10-20 16:36:47 +01001231 __ negl(out.As<CpuRegister>());
1232 break;
1233
1234 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001235 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001236 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001237 __ negq(out.As<CpuRegister>());
1238 break;
1239
Roland Levillain88cb1752014-10-20 16:36:47 +01001240 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001241 DCHECK(in.IsFpuRegister());
1242 DCHECK(out.IsFpuRegister());
1243 DCHECK(!in.Equals(out));
1244 // TODO: Instead of computing negation as a subtraction from
1245 // zero, implement it with an exclusive or with value 0x80000000
1246 // (mask for bit 31, representing the sign of a single-precision
1247 // floating-point number), fetched from a constant pool:
1248 //
1249 // xorps out, [RIP:...] // value at RIP is 0x80 00 00 00
1250
1251 // out = 0
1252 __ xorps(out.As<XmmRegister>(), out.As<XmmRegister>());
1253 // out = out - in
1254 __ subss(out.As<XmmRegister>(), in.As<XmmRegister>());
1255 break;
1256
Roland Levillain88cb1752014-10-20 16:36:47 +01001257 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001258 DCHECK(in.IsFpuRegister());
1259 DCHECK(out.IsFpuRegister());
1260 DCHECK(!in.Equals(out));
1261 // TODO: Instead of computing negation as a subtraction from
1262 // zero, implement it with an exclusive or with value
1263 // 0x8000000000000000 (mask for bit 63, representing the sign of
1264 // a double-precision floating-point number), fetched from a
1265 // constant pool:
1266 //
1267 // xorpd out, [RIP:...] // value at RIP is 0x80 00 00 00 00 00 00 00
1268
1269 // out = 0
1270 __ xorpd(out.As<XmmRegister>(), out.As<XmmRegister>());
1271 // out = out - in
1272 __ subsd(out.As<XmmRegister>(), in.As<XmmRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001273 break;
1274
1275 default:
1276 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1277 }
1278}
1279
Roland Levillaindff1f282014-11-05 14:15:05 +00001280void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1281 LocationSummary* locations =
1282 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1283 Primitive::Type result_type = conversion->GetResultType();
1284 Primitive::Type input_type = conversion->GetInputType();
1285 switch (result_type) {
Roland Levillain946e1432014-11-11 17:35:19 +00001286 case Primitive::kPrimInt:
1287 switch (input_type) {
1288 case Primitive::kPrimLong:
1289 // long-to-int conversion.
1290 locations->SetInAt(0, Location::Any());
1291 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1292 break;
1293
1294 case Primitive::kPrimFloat:
1295 case Primitive::kPrimDouble:
1296 LOG(FATAL) << "Type conversion from " << input_type
1297 << " to " << result_type << " not yet implemented";
1298 break;
1299
1300 default:
1301 LOG(FATAL) << "Unexpected type conversion from " << input_type
1302 << " to " << result_type;
1303 }
1304 break;
1305
Roland Levillaindff1f282014-11-05 14:15:05 +00001306 case Primitive::kPrimLong:
1307 switch (input_type) {
1308 case Primitive::kPrimByte:
1309 case Primitive::kPrimShort:
1310 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001311 case Primitive::kPrimChar:
Roland Levillaindff1f282014-11-05 14:15:05 +00001312 // int-to-long conversion.
1313 // TODO: We would benefit from a (to-be-implemented)
1314 // Location::RegisterOrStackSlot requirement for this input.
1315 locations->SetInAt(0, Location::RequiresRegister());
1316 locations->SetOut(Location::RequiresRegister());
1317 break;
1318
1319 case Primitive::kPrimFloat:
1320 case Primitive::kPrimDouble:
1321 LOG(FATAL) << "Type conversion from " << input_type << " to "
1322 << result_type << " not yet implemented";
1323 break;
1324
1325 default:
1326 LOG(FATAL) << "Unexpected type conversion from " << input_type
1327 << " to " << result_type;
1328 }
1329 break;
1330
Roland Levillaindff1f282014-11-05 14:15:05 +00001331 case Primitive::kPrimFloat:
1332 case Primitive::kPrimDouble:
1333 LOG(FATAL) << "Type conversion from " << input_type
1334 << " to " << result_type << " not yet implemented";
1335 break;
1336
1337 default:
1338 LOG(FATAL) << "Unexpected type conversion from " << input_type
1339 << " to " << result_type;
1340 }
1341}
1342
1343void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1344 LocationSummary* locations = conversion->GetLocations();
1345 Location out = locations->Out();
1346 Location in = locations->InAt(0);
1347 Primitive::Type result_type = conversion->GetResultType();
1348 Primitive::Type input_type = conversion->GetInputType();
1349 switch (result_type) {
Roland Levillain946e1432014-11-11 17:35:19 +00001350 case Primitive::kPrimInt:
1351 switch (input_type) {
1352 case Primitive::kPrimLong:
1353 // long-to-int conversion.
1354 if (in.IsRegister()) {
1355 __ movl(out.As<CpuRegister>(), in.As<CpuRegister>());
1356 } else if (in.IsDoubleStackSlot()) {
1357 __ movl(out.As<CpuRegister>(),
1358 Address(CpuRegister(RSP), in.GetStackIndex()));
1359 } else {
1360 DCHECK(in.IsConstant());
1361 DCHECK(in.GetConstant()->IsLongConstant());
1362 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
1363 __ movl(out.As<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1364 }
1365 break;
1366
1367 case Primitive::kPrimFloat:
1368 case Primitive::kPrimDouble:
1369 LOG(FATAL) << "Type conversion from " << input_type
1370 << " to " << result_type << " not yet implemented";
1371 break;
1372
1373 default:
1374 LOG(FATAL) << "Unexpected type conversion from " << input_type
1375 << " to " << result_type;
1376 }
1377 break;
1378
Roland Levillaindff1f282014-11-05 14:15:05 +00001379 case Primitive::kPrimLong:
1380 switch (input_type) {
1381 DCHECK(out.IsRegister());
1382 case Primitive::kPrimByte:
1383 case Primitive::kPrimShort:
1384 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001385 case Primitive::kPrimChar:
Roland Levillaindff1f282014-11-05 14:15:05 +00001386 // int-to-long conversion.
1387 DCHECK(in.IsRegister());
1388 __ movsxd(out.As<CpuRegister>(), in.As<CpuRegister>());
1389 break;
1390
1391 case Primitive::kPrimFloat:
1392 case Primitive::kPrimDouble:
1393 LOG(FATAL) << "Type conversion from " << input_type << " to "
1394 << result_type << " not yet implemented";
1395 break;
1396
1397 default:
1398 LOG(FATAL) << "Unexpected type conversion from " << input_type
1399 << " to " << result_type;
1400 }
1401 break;
1402
Roland Levillaindff1f282014-11-05 14:15:05 +00001403 case Primitive::kPrimFloat:
1404 case Primitive::kPrimDouble:
1405 LOG(FATAL) << "Type conversion from " << input_type
1406 << " to " << result_type << " not yet implemented";
1407 break;
1408
1409 default:
1410 LOG(FATAL) << "Unexpected type conversion from " << input_type
1411 << " to " << result_type;
1412 }
1413}
1414
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001415void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001416 LocationSummary* locations =
1417 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001418 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001419 case Primitive::kPrimInt: {
1420 locations->SetInAt(0, Location::RequiresRegister());
1421 locations->SetInAt(1, Location::Any());
1422 locations->SetOut(Location::SameAsFirstInput());
1423 break;
1424 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001425
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001426 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001427 locations->SetInAt(0, Location::RequiresRegister());
1428 locations->SetInAt(1, Location::RequiresRegister());
1429 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001430 break;
1431 }
1432
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001433 case Primitive::kPrimDouble:
1434 case Primitive::kPrimFloat: {
1435 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001436 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001437 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001438 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001439 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001440
1441 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001442 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001443 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001444}
1445
1446void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1447 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001448 Location first = locations->InAt(0);
1449 Location second = locations->InAt(1);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001450 DCHECK(first.Equals(locations->Out()));
Calin Juravle11351682014-10-23 15:38:15 +01001451
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001452 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001453 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001454 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001455 __ addl(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001456 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001457 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001458 __ addl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001459 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001460 __ addl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001461 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001462 break;
1463 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001464
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001465 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001466 __ addq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001467 break;
1468 }
1469
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001470 case Primitive::kPrimFloat: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001471 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001472 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001473 }
1474
1475 case Primitive::kPrimDouble: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001476 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001477 break;
1478 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001479
1480 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001481 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001482 }
1483}
1484
1485void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001486 LocationSummary* locations =
1487 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001488 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001489 case Primitive::kPrimInt: {
1490 locations->SetInAt(0, Location::RequiresRegister());
1491 locations->SetInAt(1, Location::Any());
1492 locations->SetOut(Location::SameAsFirstInput());
1493 break;
1494 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001495 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001496 locations->SetInAt(0, Location::RequiresRegister());
1497 locations->SetInAt(1, Location::RequiresRegister());
1498 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001499 break;
1500 }
Calin Juravle11351682014-10-23 15:38:15 +01001501 case Primitive::kPrimFloat:
1502 case Primitive::kPrimDouble: {
1503 locations->SetInAt(0, Location::RequiresFpuRegister());
1504 locations->SetInAt(1, Location::RequiresFpuRegister());
1505 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001506 break;
Calin Juravle11351682014-10-23 15:38:15 +01001507 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001508 default:
Calin Juravle11351682014-10-23 15:38:15 +01001509 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001510 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001511}
1512
1513void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1514 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001515 Location first = locations->InAt(0);
1516 Location second = locations->InAt(1);
1517 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001518 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001519 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001520 if (second.IsRegister()) {
1521 __ subl(first.As<CpuRegister>(), second.As<CpuRegister>());
1522 } else if (second.IsConstant()) {
1523 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1524 __ subl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001525 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001526 __ subl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001527 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001528 break;
1529 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001530 case Primitive::kPrimLong: {
Calin Juravle11351682014-10-23 15:38:15 +01001531 __ subq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001532 break;
1533 }
1534
Calin Juravle11351682014-10-23 15:38:15 +01001535 case Primitive::kPrimFloat: {
1536 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001537 break;
Calin Juravle11351682014-10-23 15:38:15 +01001538 }
1539
1540 case Primitive::kPrimDouble: {
1541 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1542 break;
1543 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001544
1545 default:
Calin Juravle11351682014-10-23 15:38:15 +01001546 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001547 }
1548}
1549
Calin Juravle34bacdf2014-10-07 20:23:36 +01001550void LocationsBuilderX86_64::VisitMul(HMul* mul) {
1551 LocationSummary* locations =
1552 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1553 switch (mul->GetResultType()) {
1554 case Primitive::kPrimInt: {
1555 locations->SetInAt(0, Location::RequiresRegister());
1556 locations->SetInAt(1, Location::Any());
1557 locations->SetOut(Location::SameAsFirstInput());
1558 break;
1559 }
1560 case Primitive::kPrimLong: {
1561 locations->SetInAt(0, Location::RequiresRegister());
1562 locations->SetInAt(1, Location::RequiresRegister());
1563 locations->SetOut(Location::SameAsFirstInput());
1564 break;
1565 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001566 case Primitive::kPrimFloat:
1567 case Primitive::kPrimDouble: {
1568 locations->SetInAt(0, Location::RequiresFpuRegister());
1569 locations->SetInAt(1, Location::RequiresFpuRegister());
1570 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001571 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001572 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001573
1574 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001575 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001576 }
1577}
1578
1579void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
1580 LocationSummary* locations = mul->GetLocations();
1581 Location first = locations->InAt(0);
1582 Location second = locations->InAt(1);
1583 DCHECK(first.Equals(locations->Out()));
1584 switch (mul->GetResultType()) {
1585 case Primitive::kPrimInt: {
1586 if (second.IsRegister()) {
1587 __ imull(first.As<CpuRegister>(), second.As<CpuRegister>());
1588 } else if (second.IsConstant()) {
1589 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1590 __ imull(first.As<CpuRegister>(), imm);
1591 } else {
1592 DCHECK(second.IsStackSlot());
1593 __ imull(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
1594 }
1595 break;
1596 }
1597 case Primitive::kPrimLong: {
1598 __ imulq(first.As<CpuRegister>(), second.As<CpuRegister>());
1599 break;
1600 }
1601
Calin Juravleb5bfa962014-10-21 18:02:24 +01001602 case Primitive::kPrimFloat: {
1603 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001604 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001605 }
1606
1607 case Primitive::kPrimDouble: {
1608 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1609 break;
1610 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001611
1612 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001613 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001614 }
1615}
1616
Calin Juravle7c4954d2014-10-28 16:57:40 +00001617void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
1618 LocationSummary* locations =
1619 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1620 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001621 case Primitive::kPrimInt:
1622 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00001623 locations->SetInAt(0, Location::RegisterLocation(RAX));
1624 locations->SetInAt(1, Location::RequiresRegister());
1625 locations->SetOut(Location::SameAsFirstInput());
1626 // Intel uses edx:eax as the dividend.
1627 locations->AddTemp(Location::RegisterLocation(RDX));
1628 break;
1629 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001630
Calin Juravle7c4954d2014-10-28 16:57:40 +00001631 case Primitive::kPrimFloat:
1632 case Primitive::kPrimDouble: {
1633 locations->SetInAt(0, Location::RequiresFpuRegister());
1634 locations->SetInAt(1, Location::RequiresFpuRegister());
1635 locations->SetOut(Location::SameAsFirstInput());
1636 break;
1637 }
1638
1639 default:
1640 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1641 }
1642}
1643
1644void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
1645 LocationSummary* locations = div->GetLocations();
1646 Location first = locations->InAt(0);
1647 Location second = locations->InAt(1);
1648 DCHECK(first.Equals(locations->Out()));
1649
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001650 Primitive::Type type = div->GetResultType();
1651 switch (type) {
1652 case Primitive::kPrimInt:
1653 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00001654 CpuRegister first_reg = first.As<CpuRegister>();
1655 CpuRegister second_reg = second.As<CpuRegister>();
1656 DCHECK_EQ(RAX, first_reg.AsRegister());
1657 DCHECK_EQ(RDX, locations->GetTemp(0).As<CpuRegister>().AsRegister());
1658
1659 SlowPathCodeX86_64* slow_path =
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001660 new (GetGraph()->GetArena()) DivMinusOneSlowPathX86_64(first_reg.AsRegister(), type);
Calin Juravled0d48522014-11-04 16:40:20 +00001661 codegen_->AddSlowPath(slow_path);
1662
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001663 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
1664 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
1665 // so it's safe to just use negl instead of more complex comparisons.
Calin Juravled0d48522014-11-04 16:40:20 +00001666
1667 __ cmpl(second_reg, Immediate(-1));
1668 __ j(kEqual, slow_path->GetEntryLabel());
1669
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001670 if (type == Primitive::kPrimInt) {
1671 // edx:eax <- sign-extended of eax
1672 __ cdq();
1673 // eax = quotient, edx = remainder
1674 __ idivl(second_reg);
1675 } else {
1676 // rdx:rax <- sign-extended of rax
1677 __ cqo();
1678 // rax = quotient, rdx = remainder
1679 __ idivq(second_reg);
1680 }
Calin Juravled0d48522014-11-04 16:40:20 +00001681
1682 __ Bind(slow_path->GetExitLabel());
1683 break;
1684 }
1685
Calin Juravle7c4954d2014-10-28 16:57:40 +00001686 case Primitive::kPrimFloat: {
1687 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1688 break;
1689 }
1690
1691 case Primitive::kPrimDouble: {
1692 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1693 break;
1694 }
1695
1696 default:
1697 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1698 }
1699}
1700
Calin Juravled0d48522014-11-04 16:40:20 +00001701void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1702 LocationSummary* locations =
1703 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1704 locations->SetInAt(0, Location::Any());
1705 if (instruction->HasUses()) {
1706 locations->SetOut(Location::SameAsFirstInput());
1707 }
1708}
1709
1710void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1711 SlowPathCodeX86_64* slow_path =
1712 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
1713 codegen_->AddSlowPath(slow_path);
1714
1715 LocationSummary* locations = instruction->GetLocations();
1716 Location value = locations->InAt(0);
1717
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001718 switch (instruction->GetType()) {
1719 case Primitive::kPrimInt: {
1720 if (value.IsRegister()) {
1721 __ testl(value.As<CpuRegister>(), value.As<CpuRegister>());
1722 __ j(kEqual, slow_path->GetEntryLabel());
1723 } else if (value.IsStackSlot()) {
1724 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
1725 __ j(kEqual, slow_path->GetEntryLabel());
1726 } else {
1727 DCHECK(value.IsConstant()) << value;
1728 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
1729 __ jmp(slow_path->GetEntryLabel());
1730 }
1731 }
1732 break;
Calin Juravled0d48522014-11-04 16:40:20 +00001733 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001734 case Primitive::kPrimLong: {
1735 if (value.IsRegister()) {
1736 __ testq(value.As<CpuRegister>(), value.As<CpuRegister>());
1737 __ j(kEqual, slow_path->GetEntryLabel());
1738 } else if (value.IsDoubleStackSlot()) {
1739 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
1740 __ j(kEqual, slow_path->GetEntryLabel());
1741 } else {
1742 DCHECK(value.IsConstant()) << value;
1743 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
1744 __ jmp(slow_path->GetEntryLabel());
1745 }
1746 }
1747 break;
1748 }
1749 default:
1750 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00001751 }
Calin Juravled0d48522014-11-04 16:40:20 +00001752}
1753
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001754void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001755 LocationSummary* locations =
1756 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001757 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001758 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1759 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1760 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001761}
1762
1763void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
1764 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001765 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001766 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1767
1768 __ gs()->call(Address::Absolute(
1769 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
1770
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001771 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001772 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001773}
1774
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001775void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
1776 LocationSummary* locations =
1777 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1778 InvokeRuntimeCallingConvention calling_convention;
1779 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1780 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1781 locations->SetOut(Location::RegisterLocation(RAX));
1782 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1783}
1784
1785void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
1786 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001787 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001788 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1789
1790 __ gs()->call(Address::Absolute(
1791 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocArrayWithAccessCheck), true));
1792
1793 DCHECK(!codegen_->IsLeafMethod());
1794 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1795}
1796
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001797void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001798 LocationSummary* locations =
1799 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001800 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1801 if (location.IsStackSlot()) {
1802 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1803 } else if (location.IsDoubleStackSlot()) {
1804 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1805 }
1806 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001807}
1808
1809void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
1810 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001811 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001812}
1813
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001814void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001815 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001816 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001817 locations->SetInAt(0, Location::RequiresRegister());
1818 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001819}
1820
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001821void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
1822 LocationSummary* locations = not_->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001823 DCHECK_EQ(locations->InAt(0).As<CpuRegister>().AsRegister(),
1824 locations->Out().As<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001825 Location out = locations->Out();
1826 switch (not_->InputAt(0)->GetType()) {
1827 case Primitive::kPrimBoolean:
1828 __ xorq(out.As<CpuRegister>(), Immediate(1));
1829 break;
1830
1831 case Primitive::kPrimInt:
1832 __ notl(out.As<CpuRegister>());
1833 break;
1834
1835 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001836 __ notq(out.As<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001837 break;
1838
1839 default:
1840 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1841 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001842}
1843
1844void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001845 LocationSummary* locations =
1846 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001847 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1848 locations->SetInAt(i, Location::Any());
1849 }
1850 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001851}
1852
1853void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001854 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001855 LOG(FATAL) << "Unimplemented";
1856}
1857
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001858void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001859 LocationSummary* locations =
1860 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001861 Primitive::Type field_type = instruction->GetFieldType();
1862 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001863 locations->SetInAt(0, Location::RequiresRegister());
1864 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001865 if (is_object_type) {
1866 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001867 locations->AddTemp(Location::RequiresRegister());
1868 locations->AddTemp(Location::RequiresRegister());
1869 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001870}
1871
1872void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1873 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001874 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001875 size_t offset = instruction->GetFieldOffset().SizeValue();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001876 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001877
1878 switch (field_type) {
1879 case Primitive::kPrimBoolean:
1880 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001881 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001882 __ movb(Address(obj, offset), value);
1883 break;
1884 }
1885
1886 case Primitive::kPrimShort:
1887 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001888 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001889 __ movw(Address(obj, offset), value);
1890 break;
1891 }
1892
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001893 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001894 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001895 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001896 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001897 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001898 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
1899 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001900 codegen_->MarkGCCard(temp, card, obj, value);
1901 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001902 break;
1903 }
1904
1905 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001906 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001907 __ movq(Address(obj, offset), value);
1908 break;
1909 }
1910
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001911 case Primitive::kPrimFloat: {
1912 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1913 __ movss(Address(obj, offset), value);
1914 break;
1915 }
1916
1917 case Primitive::kPrimDouble: {
1918 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1919 __ movsd(Address(obj, offset), value);
1920 break;
1921 }
1922
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001923 case Primitive::kPrimVoid:
1924 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001925 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001926 }
1927}
1928
1929void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001930 LocationSummary* locations =
1931 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001932 locations->SetInAt(0, Location::RequiresRegister());
1933 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001934}
1935
1936void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1937 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001938 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001939 size_t offset = instruction->GetFieldOffset().SizeValue();
1940
1941 switch (instruction->GetType()) {
1942 case Primitive::kPrimBoolean: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001943 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001944 __ movzxb(out, Address(obj, offset));
1945 break;
1946 }
1947
1948 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001949 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001950 __ movsxb(out, Address(obj, offset));
1951 break;
1952 }
1953
1954 case Primitive::kPrimShort: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001955 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001956 __ movsxw(out, Address(obj, offset));
1957 break;
1958 }
1959
1960 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001961 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001962 __ movzxw(out, Address(obj, offset));
1963 break;
1964 }
1965
1966 case Primitive::kPrimInt:
1967 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001968 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001969 __ movl(out, Address(obj, offset));
1970 break;
1971 }
1972
1973 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001974 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001975 __ movq(out, Address(obj, offset));
1976 break;
1977 }
1978
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001979 case Primitive::kPrimFloat: {
1980 XmmRegister out = locations->Out().As<XmmRegister>();
1981 __ movss(out, Address(obj, offset));
1982 break;
1983 }
1984
1985 case Primitive::kPrimDouble: {
1986 XmmRegister out = locations->Out().As<XmmRegister>();
1987 __ movsd(out, Address(obj, offset));
1988 break;
1989 }
1990
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001991 case Primitive::kPrimVoid:
1992 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001993 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001994 }
1995}
1996
1997void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001998 LocationSummary* locations =
1999 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002000 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002001 if (instruction->HasUses()) {
2002 locations->SetOut(Location::SameAsFirstInput());
2003 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002004}
2005
2006void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002007 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002008 codegen_->AddSlowPath(slow_path);
2009
2010 LocationSummary* locations = instruction->GetLocations();
2011 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002012
2013 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002014 __ cmpl(obj.As<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002015 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002016 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002017 } else {
2018 DCHECK(obj.IsConstant()) << obj;
2019 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2020 __ jmp(slow_path->GetEntryLabel());
2021 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002022 }
2023 __ j(kEqual, slow_path->GetEntryLabel());
2024}
2025
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002026void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002027 LocationSummary* locations =
2028 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002029 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002030 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002031 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2032 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002033}
2034
2035void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
2036 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002037 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002038 Location index = locations->InAt(1);
2039
2040 switch (instruction->GetType()) {
2041 case Primitive::kPrimBoolean: {
2042 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002043 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002044 if (index.IsConstant()) {
2045 __ movzxb(out, Address(obj,
2046 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2047 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002048 __ movzxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002049 }
2050 break;
2051 }
2052
2053 case Primitive::kPrimByte: {
2054 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002055 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002056 if (index.IsConstant()) {
2057 __ movsxb(out, Address(obj,
2058 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2059 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002060 __ movsxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002061 }
2062 break;
2063 }
2064
2065 case Primitive::kPrimShort: {
2066 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002067 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002068 if (index.IsConstant()) {
2069 __ movsxw(out, Address(obj,
2070 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2071 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002072 __ movsxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002073 }
2074 break;
2075 }
2076
2077 case Primitive::kPrimChar: {
2078 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002079 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002080 if (index.IsConstant()) {
2081 __ movzxw(out, Address(obj,
2082 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2083 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002084 __ movzxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002085 }
2086 break;
2087 }
2088
2089 case Primitive::kPrimInt:
2090 case Primitive::kPrimNot: {
2091 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
2092 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002093 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002094 if (index.IsConstant()) {
2095 __ movl(out, Address(obj,
2096 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2097 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002098 __ movl(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002099 }
2100 break;
2101 }
2102
2103 case Primitive::kPrimLong: {
2104 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002105 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002106 if (index.IsConstant()) {
2107 __ movq(out, Address(obj,
2108 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2109 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002110 __ movq(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002111 }
2112 break;
2113 }
2114
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002115 case Primitive::kPrimFloat: {
2116 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2117 XmmRegister out = locations->Out().As<XmmRegister>();
2118 if (index.IsConstant()) {
2119 __ movss(out, Address(obj,
2120 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2121 } else {
2122 __ movss(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
2123 }
2124 break;
2125 }
2126
2127 case Primitive::kPrimDouble: {
2128 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2129 XmmRegister out = locations->Out().As<XmmRegister>();
2130 if (index.IsConstant()) {
2131 __ movsd(out, Address(obj,
2132 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2133 } else {
2134 __ movsd(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
2135 }
2136 break;
2137 }
2138
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002139 case Primitive::kPrimVoid:
2140 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002141 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002142 }
2143}
2144
2145void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002146 Primitive::Type value_type = instruction->GetComponentType();
2147 bool is_object = value_type == Primitive::kPrimNot;
2148 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2149 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
2150 if (is_object) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002151 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002152 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2153 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2154 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002155 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002156 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002157 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002158 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2159 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002160 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002161 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002162 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
2163 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002164 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002165 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002166 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002167 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002168}
2169
2170void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
2171 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002172 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002173 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002174 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002175 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002176
2177 switch (value_type) {
2178 case Primitive::kPrimBoolean:
2179 case Primitive::kPrimByte: {
2180 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002181 if (index.IsConstant()) {
2182 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002183 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002184 __ movb(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002185 } else {
2186 __ movb(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2187 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002188 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002189 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002190 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
2191 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002192 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002193 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002194 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2195 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002196 }
2197 break;
2198 }
2199
2200 case Primitive::kPrimShort:
2201 case Primitive::kPrimChar: {
2202 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002203 if (index.IsConstant()) {
2204 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002205 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002206 __ movw(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002207 } else {
2208 __ movw(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2209 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002210 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002211 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002212 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
2213 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002214 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002215 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002216 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2217 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002218 }
2219 break;
2220 }
2221
2222 case Primitive::kPrimInt: {
2223 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002224 if (index.IsConstant()) {
2225 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002226 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002227 __ movl(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002228 } else {
2229 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2230 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002231 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002232 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002233 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2234 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002235 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002236 DCHECK(value.IsConstant()) << value;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002237 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002238 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2239 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002240 }
2241 break;
2242 }
2243
2244 case Primitive::kPrimNot: {
2245 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject), true));
2246 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002247 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002248 break;
2249 }
2250
2251 case Primitive::kPrimLong: {
2252 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002253 if (index.IsConstant()) {
2254 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002255 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002256 __ movq(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002257 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002258 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002259 __ movq(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
2260 value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002261 }
2262 break;
2263 }
2264
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002265 case Primitive::kPrimFloat: {
2266 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2267 if (index.IsConstant()) {
2268 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2269 DCHECK(value.IsFpuRegister());
2270 __ movss(Address(obj, offset), value.As<XmmRegister>());
2271 } else {
2272 DCHECK(value.IsFpuRegister());
2273 __ movss(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2274 value.As<XmmRegister>());
2275 }
2276 break;
2277 }
2278
2279 case Primitive::kPrimDouble: {
2280 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2281 if (index.IsConstant()) {
2282 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
2283 DCHECK(value.IsFpuRegister());
2284 __ movsd(Address(obj, offset), value.As<XmmRegister>());
2285 } else {
2286 DCHECK(value.IsFpuRegister());
2287 __ movsd(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
2288 value.As<XmmRegister>());
2289 }
2290 break;
2291 }
2292
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002293 case Primitive::kPrimVoid:
2294 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002295 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002296 }
2297}
2298
2299void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002300 LocationSummary* locations =
2301 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002302 locations->SetInAt(0, Location::RequiresRegister());
2303 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002304}
2305
2306void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
2307 LocationSummary* locations = instruction->GetLocations();
2308 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002309 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2310 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002311 __ movl(out, Address(obj, offset));
2312}
2313
2314void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002315 LocationSummary* locations =
2316 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002317 locations->SetInAt(0, Location::RequiresRegister());
2318 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002319 if (instruction->HasUses()) {
2320 locations->SetOut(Location::SameAsFirstInput());
2321 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002322}
2323
2324void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
2325 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002326 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002327 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002328 codegen_->AddSlowPath(slow_path);
2329
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002330 CpuRegister index = locations->InAt(0).As<CpuRegister>();
2331 CpuRegister length = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002332
2333 __ cmpl(index, length);
2334 __ j(kAboveEqual, slow_path->GetEntryLabel());
2335}
2336
2337void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
2338 CpuRegister card,
2339 CpuRegister object,
2340 CpuRegister value) {
2341 Label is_null;
2342 __ testl(value, value);
2343 __ j(kEqual, &is_null);
2344 __ gs()->movq(card, Address::Absolute(
2345 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
2346 __ movq(temp, object);
2347 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
2348 __ movb(Address(temp, card, TIMES_1, 0), card);
2349 __ Bind(&is_null);
2350}
2351
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002352void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
2353 temp->SetLocations(nullptr);
2354}
2355
2356void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
2357 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002358 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002359}
2360
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002361void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002362 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002363 LOG(FATAL) << "Unimplemented";
2364}
2365
2366void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002367 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2368}
2369
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002370void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
2371 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2372}
2373
2374void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002375 HBasicBlock* block = instruction->GetBlock();
2376 if (block->GetLoopInformation() != nullptr) {
2377 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2378 // The back edge will generate the suspend check.
2379 return;
2380 }
2381 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2382 // The goto will generate the suspend check.
2383 return;
2384 }
2385 GenerateSuspendCheck(instruction, nullptr);
2386}
2387
2388void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
2389 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002390 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002391 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002392 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002393 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002394 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002395 if (successor == nullptr) {
2396 __ j(kNotEqual, slow_path->GetEntryLabel());
2397 __ Bind(slow_path->GetReturnLabel());
2398 } else {
2399 __ j(kEqual, codegen_->GetLabelOf(successor));
2400 __ jmp(slow_path->GetEntryLabel());
2401 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002402}
2403
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002404X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
2405 return codegen_->GetAssembler();
2406}
2407
2408void ParallelMoveResolverX86_64::EmitMove(size_t index) {
2409 MoveOperands* move = moves_.Get(index);
2410 Location source = move->GetSource();
2411 Location destination = move->GetDestination();
2412
2413 if (source.IsRegister()) {
2414 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002415 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002416 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002417 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002418 source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002419 } else {
2420 DCHECK(destination.IsDoubleStackSlot());
2421 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002422 source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002423 }
2424 } else if (source.IsStackSlot()) {
2425 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002426 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002427 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002428 } else if (destination.IsFpuRegister()) {
2429 __ movss(destination.As<XmmRegister>(),
2430 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002431 } else {
2432 DCHECK(destination.IsStackSlot());
2433 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2434 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2435 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002436 } else if (source.IsDoubleStackSlot()) {
2437 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002438 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002439 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002440 } else if (destination.IsFpuRegister()) {
2441 __ movsd(destination.As<XmmRegister>(), Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002442 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01002443 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002444 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2445 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2446 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002447 } else if (source.IsConstant()) {
2448 HConstant* constant = source.GetConstant();
2449 if (constant->IsIntConstant()) {
2450 Immediate imm(constant->AsIntConstant()->GetValue());
2451 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002452 __ movl(destination.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002453 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002454 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002455 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2456 }
2457 } else if (constant->IsLongConstant()) {
2458 int64_t value = constant->AsLongConstant()->GetValue();
2459 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002460 __ movq(destination.As<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002461 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002462 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002463 __ movq(CpuRegister(TMP), Immediate(value));
2464 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2465 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002466 } else if (constant->IsFloatConstant()) {
2467 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
2468 if (destination.IsFpuRegister()) {
2469 __ movl(CpuRegister(TMP), imm);
2470 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2471 } else {
2472 DCHECK(destination.IsStackSlot()) << destination;
2473 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2474 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002475 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002476 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
2477 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
2478 if (destination.IsFpuRegister()) {
2479 __ movq(CpuRegister(TMP), imm);
2480 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2481 } else {
2482 DCHECK(destination.IsDoubleStackSlot()) << destination;
2483 __ movq(CpuRegister(TMP), imm);
2484 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2485 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002486 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002487 } else if (source.IsFpuRegister()) {
2488 if (destination.IsFpuRegister()) {
2489 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
2490 } else if (destination.IsStackSlot()) {
2491 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
2492 source.As<XmmRegister>());
2493 } else {
2494 DCHECK(destination.IsDoubleStackSlot());
2495 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
2496 source.As<XmmRegister>());
2497 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002498 }
2499}
2500
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002501void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002502 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002503 __ movl(Address(CpuRegister(RSP), mem), reg);
2504 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002505}
2506
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002507void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002508 ScratchRegisterScope ensure_scratch(
2509 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2510
2511 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2512 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2513 __ movl(CpuRegister(ensure_scratch.GetRegister()),
2514 Address(CpuRegister(RSP), mem2 + stack_offset));
2515 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2516 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
2517 CpuRegister(ensure_scratch.GetRegister()));
2518}
2519
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002520void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
2521 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2522 __ movq(Address(CpuRegister(RSP), mem), reg);
2523 __ movq(reg, CpuRegister(TMP));
2524}
2525
2526void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
2527 ScratchRegisterScope ensure_scratch(
2528 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2529
2530 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2531 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2532 __ movq(CpuRegister(ensure_scratch.GetRegister()),
2533 Address(CpuRegister(RSP), mem2 + stack_offset));
2534 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2535 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
2536 CpuRegister(ensure_scratch.GetRegister()));
2537}
2538
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002539void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
2540 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2541 __ movss(Address(CpuRegister(RSP), mem), reg);
2542 __ movd(reg, CpuRegister(TMP));
2543}
2544
2545void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
2546 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2547 __ movsd(Address(CpuRegister(RSP), mem), reg);
2548 __ movd(reg, CpuRegister(TMP));
2549}
2550
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002551void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
2552 MoveOperands* move = moves_.Get(index);
2553 Location source = move->GetSource();
2554 Location destination = move->GetDestination();
2555
2556 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002557 __ xchgq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002558 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002559 Exchange32(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002560 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002561 Exchange32(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002562 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002563 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
2564 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002565 Exchange64(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002566 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002567 Exchange64(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002568 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
2569 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002570 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
2571 __ movd(CpuRegister(TMP), source.As<XmmRegister>());
2572 __ movaps(source.As<XmmRegister>(), destination.As<XmmRegister>());
2573 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2574 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
2575 Exchange32(source.As<XmmRegister>(), destination.GetStackIndex());
2576 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
2577 Exchange32(destination.As<XmmRegister>(), source.GetStackIndex());
2578 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
2579 Exchange64(source.As<XmmRegister>(), destination.GetStackIndex());
2580 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
2581 Exchange64(destination.As<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002582 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002583 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002584 }
2585}
2586
2587
2588void ParallelMoveResolverX86_64::SpillScratch(int reg) {
2589 __ pushq(CpuRegister(reg));
2590}
2591
2592
2593void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
2594 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002595}
2596
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002597void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
2598 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
2599 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2600 Immediate(mirror::Class::kStatusInitialized));
2601 __ j(kLess, slow_path->GetEntryLabel());
2602 __ Bind(slow_path->GetExitLabel());
2603 // No need for memory fence, thanks to the X86_64 memory model.
2604}
2605
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002606void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002607 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2608 ? LocationSummary::kCallOnSlowPath
2609 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002610 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002611 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002612 locations->SetOut(Location::RequiresRegister());
2613}
2614
2615void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
2616 CpuRegister out = cls->GetLocations()->Out().As<CpuRegister>();
2617 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002618 DCHECK(!cls->CanCallRuntime());
2619 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002620 codegen_->LoadCurrentMethod(out);
2621 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2622 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002623 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002624 codegen_->LoadCurrentMethod(out);
2625 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2626 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002627 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
2628 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2629 codegen_->AddSlowPath(slow_path);
2630 __ testl(out, out);
2631 __ j(kEqual, slow_path->GetEntryLabel());
2632 if (cls->MustGenerateClinitCheck()) {
2633 GenerateClassInitializationCheck(slow_path, out);
2634 } else {
2635 __ Bind(slow_path->GetExitLabel());
2636 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002637 }
2638}
2639
2640void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
2641 LocationSummary* locations =
2642 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2643 locations->SetInAt(0, Location::RequiresRegister());
2644 if (check->HasUses()) {
2645 locations->SetOut(Location::SameAsFirstInput());
2646 }
2647}
2648
2649void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002650 // We assume the class to not be null.
2651 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
2652 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002653 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002654 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002655}
2656
2657void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2658 LocationSummary* locations =
2659 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2660 locations->SetInAt(0, Location::RequiresRegister());
2661 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2662}
2663
2664void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2665 LocationSummary* locations = instruction->GetLocations();
2666 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002667 size_t offset = instruction->GetFieldOffset().SizeValue();
2668
2669 switch (instruction->GetType()) {
2670 case Primitive::kPrimBoolean: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002671 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002672 __ movzxb(out, Address(cls, offset));
2673 break;
2674 }
2675
2676 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002677 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002678 __ movsxb(out, Address(cls, offset));
2679 break;
2680 }
2681
2682 case Primitive::kPrimShort: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002683 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002684 __ movsxw(out, Address(cls, offset));
2685 break;
2686 }
2687
2688 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002689 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002690 __ movzxw(out, Address(cls, offset));
2691 break;
2692 }
2693
2694 case Primitive::kPrimInt:
2695 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002696 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002697 __ movl(out, Address(cls, offset));
2698 break;
2699 }
2700
2701 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002702 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002703 __ movq(out, Address(cls, offset));
2704 break;
2705 }
2706
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002707 case Primitive::kPrimFloat: {
2708 XmmRegister out = locations->Out().As<XmmRegister>();
2709 __ movss(out, Address(cls, offset));
2710 break;
2711 }
2712
2713 case Primitive::kPrimDouble: {
2714 XmmRegister out = locations->Out().As<XmmRegister>();
2715 __ movsd(out, Address(cls, offset));
2716 break;
2717 }
2718
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002719 case Primitive::kPrimVoid:
2720 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2721 UNREACHABLE();
2722 }
2723}
2724
2725void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2726 LocationSummary* locations =
2727 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2728 Primitive::Type field_type = instruction->GetFieldType();
2729 bool is_object_type = field_type == Primitive::kPrimNot;
2730 locations->SetInAt(0, Location::RequiresRegister());
2731 locations->SetInAt(1, Location::RequiresRegister());
2732 if (is_object_type) {
2733 // Temporary registers for the write barrier.
2734 locations->AddTemp(Location::RequiresRegister());
2735 locations->AddTemp(Location::RequiresRegister());
2736 }
2737}
2738
2739void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2740 LocationSummary* locations = instruction->GetLocations();
2741 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002742 size_t offset = instruction->GetFieldOffset().SizeValue();
2743 Primitive::Type field_type = instruction->GetFieldType();
2744
2745 switch (field_type) {
2746 case Primitive::kPrimBoolean:
2747 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002748 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002749 __ movb(Address(cls, offset), value);
2750 break;
2751 }
2752
2753 case Primitive::kPrimShort:
2754 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002755 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002756 __ movw(Address(cls, offset), value);
2757 break;
2758 }
2759
2760 case Primitive::kPrimInt:
2761 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002762 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002763 __ movl(Address(cls, offset), value);
2764 if (field_type == Primitive::kPrimNot) {
2765 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
2766 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
2767 codegen_->MarkGCCard(temp, card, cls, value);
2768 }
2769 break;
2770 }
2771
2772 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002773 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002774 __ movq(Address(cls, offset), value);
2775 break;
2776 }
2777
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002778 case Primitive::kPrimFloat: {
2779 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2780 __ movss(Address(cls, offset), value);
2781 break;
2782 }
2783
2784 case Primitive::kPrimDouble: {
2785 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2786 __ movsd(Address(cls, offset), value);
2787 break;
2788 }
2789
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002790 case Primitive::kPrimVoid:
2791 LOG(FATAL) << "Unreachable type " << field_type;
2792 UNREACHABLE();
2793 }
2794}
2795
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002796void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
2797 LocationSummary* locations =
2798 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2799 locations->SetOut(Location::RequiresRegister());
2800}
2801
2802void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
2803 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
2804 codegen_->AddSlowPath(slow_path);
2805
2806 CpuRegister out = load->GetLocations()->Out().As<CpuRegister>();
2807 codegen_->LoadCurrentMethod(CpuRegister(out));
2808 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
2809 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2810 __ testl(out, out);
2811 __ j(kEqual, slow_path->GetEntryLabel());
2812 __ Bind(slow_path->GetExitLabel());
2813}
2814
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002815void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
2816 LocationSummary* locations =
2817 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2818 locations->SetOut(Location::RequiresRegister());
2819}
2820
2821void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
2822 Address address = Address::Absolute(
2823 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
2824 __ gs()->movl(load->GetLocations()->Out().As<CpuRegister>(), address);
2825 __ gs()->movl(address, Immediate(0));
2826}
2827
2828void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
2829 LocationSummary* locations =
2830 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2831 InvokeRuntimeCallingConvention calling_convention;
2832 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2833}
2834
2835void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
2836 __ gs()->call(
2837 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
2838 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2839}
2840
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002841void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002842 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
2843 ? LocationSummary::kNoCall
2844 : LocationSummary::kCallOnSlowPath;
2845 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2846 locations->SetInAt(0, Location::RequiresRegister());
2847 locations->SetInAt(1, Location::Any());
2848 locations->SetOut(Location::RequiresRegister());
2849}
2850
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002851void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002852 LocationSummary* locations = instruction->GetLocations();
2853 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2854 Location cls = locations->InAt(1);
2855 CpuRegister out = locations->Out().As<CpuRegister>();
2856 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2857 Label done, zero;
2858 SlowPathCodeX86_64* slow_path = nullptr;
2859
2860 // Return 0 if `obj` is null.
2861 // TODO: avoid this check if we know obj is not null.
2862 __ testl(obj, obj);
2863 __ j(kEqual, &zero);
2864 // Compare the class of `obj` with `cls`.
2865 __ movl(out, Address(obj, class_offset));
2866 if (cls.IsRegister()) {
2867 __ cmpl(out, cls.As<CpuRegister>());
2868 } else {
2869 DCHECK(cls.IsStackSlot()) << cls;
2870 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
2871 }
2872 if (instruction->IsClassFinal()) {
2873 // Classes must be equal for the instanceof to succeed.
2874 __ j(kNotEqual, &zero);
2875 __ movl(out, Immediate(1));
2876 __ jmp(&done);
2877 } else {
2878 // If the classes are not equal, we go into a slow path.
2879 DCHECK(locations->OnlyCallsOnSlowPath());
2880 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002881 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002882 codegen_->AddSlowPath(slow_path);
2883 __ j(kNotEqual, slow_path->GetEntryLabel());
2884 __ movl(out, Immediate(1));
2885 __ jmp(&done);
2886 }
2887 __ Bind(&zero);
2888 __ movl(out, Immediate(0));
2889 if (slow_path != nullptr) {
2890 __ Bind(slow_path->GetExitLabel());
2891 }
2892 __ Bind(&done);
2893}
2894
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002895void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
2896 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2897 instruction, LocationSummary::kCallOnSlowPath);
2898 locations->SetInAt(0, Location::RequiresRegister());
2899 locations->SetInAt(1, Location::Any());
2900 locations->AddTemp(Location::RequiresRegister());
2901}
2902
2903void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
2904 LocationSummary* locations = instruction->GetLocations();
2905 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2906 Location cls = locations->InAt(1);
2907 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
2908 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2909 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
2910 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
2911 codegen_->AddSlowPath(slow_path);
2912
2913 // TODO: avoid this check if we know obj is not null.
2914 __ testl(obj, obj);
2915 __ j(kEqual, slow_path->GetExitLabel());
2916 // Compare the class of `obj` with `cls`.
2917 __ movl(temp, Address(obj, class_offset));
2918 if (cls.IsRegister()) {
2919 __ cmpl(temp, cls.As<CpuRegister>());
2920 } else {
2921 DCHECK(cls.IsStackSlot()) << cls;
2922 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
2923 }
2924 // Classes must be equal for the checkcast to succeed.
2925 __ j(kNotEqual, slow_path->GetEntryLabel());
2926 __ Bind(slow_path->GetExitLabel());
2927}
2928
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002929void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
2930 LocationSummary* locations =
2931 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2932 InvokeRuntimeCallingConvention calling_convention;
2933 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2934}
2935
2936void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
2937 __ gs()->call(Address::Absolute(instruction->IsEnter()
2938 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
2939 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
2940 true));
2941 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2942}
2943
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002944void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
2945void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
2946void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
2947
2948void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
2949 LocationSummary* locations =
2950 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2951 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
2952 || instruction->GetResultType() == Primitive::kPrimLong);
2953 locations->SetInAt(0, Location::RequiresRegister());
2954 if (instruction->GetType() == Primitive::kPrimInt) {
2955 locations->SetInAt(1, Location::Any());
2956 } else {
2957 // Request a register to avoid loading a 64bits constant.
2958 locations->SetInAt(1, Location::RequiresRegister());
2959 }
2960 locations->SetOut(Location::SameAsFirstInput());
2961}
2962
2963void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
2964 HandleBitwiseOperation(instruction);
2965}
2966
2967void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
2968 HandleBitwiseOperation(instruction);
2969}
2970
2971void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
2972 HandleBitwiseOperation(instruction);
2973}
2974
2975void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
2976 LocationSummary* locations = instruction->GetLocations();
2977 Location first = locations->InAt(0);
2978 Location second = locations->InAt(1);
2979 DCHECK(first.Equals(locations->Out()));
2980
2981 if (instruction->GetResultType() == Primitive::kPrimInt) {
2982 if (second.IsRegister()) {
2983 if (instruction->IsAnd()) {
2984 __ andl(first.As<CpuRegister>(), second.As<CpuRegister>());
2985 } else if (instruction->IsOr()) {
2986 __ orl(first.As<CpuRegister>(), second.As<CpuRegister>());
2987 } else {
2988 DCHECK(instruction->IsXor());
2989 __ xorl(first.As<CpuRegister>(), second.As<CpuRegister>());
2990 }
2991 } else if (second.IsConstant()) {
2992 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
2993 if (instruction->IsAnd()) {
2994 __ andl(first.As<CpuRegister>(), imm);
2995 } else if (instruction->IsOr()) {
2996 __ orl(first.As<CpuRegister>(), imm);
2997 } else {
2998 DCHECK(instruction->IsXor());
2999 __ xorl(first.As<CpuRegister>(), imm);
3000 }
3001 } else {
3002 Address address(CpuRegister(RSP), second.GetStackIndex());
3003 if (instruction->IsAnd()) {
3004 __ andl(first.As<CpuRegister>(), address);
3005 } else if (instruction->IsOr()) {
3006 __ orl(first.As<CpuRegister>(), address);
3007 } else {
3008 DCHECK(instruction->IsXor());
3009 __ xorl(first.As<CpuRegister>(), address);
3010 }
3011 }
3012 } else {
3013 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3014 if (instruction->IsAnd()) {
3015 __ andq(first.As<CpuRegister>(), second.As<CpuRegister>());
3016 } else if (instruction->IsOr()) {
3017 __ orq(first.As<CpuRegister>(), second.As<CpuRegister>());
3018 } else {
3019 DCHECK(instruction->IsXor());
3020 __ xorq(first.As<CpuRegister>(), second.As<CpuRegister>());
3021 }
3022 }
3023}
3024
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003025} // namespace x86_64
3026} // namespace art