blob: a827f99995513720186d17dd52c22ff35a19c2fb [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 Geoffray3c7bb982014-07-23 16:04:16 +0100189 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000190 // We're moving two locations to locations that could overlap, so we need a parallel
191 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100192 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000193 codegen->EmitParallelMoves(
194 index_location_,
195 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
196 length_location_,
197 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100198 __ gs()->call(Address::Absolute(
199 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowArrayBounds), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100200 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100201 }
202
203 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100204 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100205 const Location index_location_;
206 const Location length_location_;
207
208 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
209};
210
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000211class LoadClassSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100212 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000213 LoadClassSlowPathX86_64(HLoadClass* cls,
214 HInstruction* at,
215 uint32_t dex_pc,
216 bool do_clinit)
217 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
218 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
219 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100220
221 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000222 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100223 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
224 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100225
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000226 codegen->SaveLiveRegisters(locations);
227
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100228 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000229 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100230 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000231 __ gs()->call(Address::Absolute((do_clinit_
232 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeStaticStorage)
233 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeType)) , true));
234 codegen->RecordPcInfo(at_, dex_pc_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100235
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000236 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000237 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000238 if (out.IsValid()) {
239 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
240 x64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000241 }
242
243 codegen->RestoreLiveRegisters(locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100244 __ jmp(GetExitLabel());
245 }
246
247 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000248 // The class this slow path will load.
249 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100250
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000251 // The instruction where this slow path is happening.
252 // (Might be the load class or an initialization check).
253 HInstruction* const at_;
254
255 // The dex PC of `at_`.
256 const uint32_t dex_pc_;
257
258 // Whether to initialize the class.
259 const bool do_clinit_;
260
261 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100262};
263
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000264class LoadStringSlowPathX86_64 : public SlowPathCodeX86_64 {
265 public:
266 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
267
268 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
269 LocationSummary* locations = instruction_->GetLocations();
270 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
271
272 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
273 __ Bind(GetEntryLabel());
274 codegen->SaveLiveRegisters(locations);
275
276 InvokeRuntimeCallingConvention calling_convention;
277 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(0)));
278 __ movl(CpuRegister(calling_convention.GetRegisterAt(1)),
279 Immediate(instruction_->GetStringIndex()));
280 __ gs()->call(Address::Absolute(
281 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pResolveString), true));
282 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
283 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
284 codegen->RestoreLiveRegisters(locations);
285 __ jmp(GetExitLabel());
286 }
287
288 private:
289 HLoadString* const instruction_;
290
291 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
292};
293
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000294class TypeCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
295 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000296 TypeCheckSlowPathX86_64(HInstruction* instruction,
297 Location class_to_check,
298 Location object_class,
299 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000300 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000301 class_to_check_(class_to_check),
302 object_class_(object_class),
303 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000304
305 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
306 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000307 DCHECK(instruction_->IsCheckCast()
308 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000309
310 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
311 __ Bind(GetEntryLabel());
312 codegen->SaveLiveRegisters(locations);
313
314 // We're moving two locations to locations that could overlap, so we need a parallel
315 // move resolver.
316 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000317 codegen->EmitParallelMoves(
318 class_to_check_,
319 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
320 object_class_,
321 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000322
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000323 if (instruction_->IsInstanceOf()) {
324 __ gs()->call(
325 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInstanceofNonTrivial), true));
326 } else {
327 DCHECK(instruction_->IsCheckCast());
328 __ gs()->call(
329 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pCheckCast), true));
330 }
331 codegen->RecordPcInfo(instruction_, dex_pc_);
332
333 if (instruction_->IsInstanceOf()) {
334 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
335 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000336
337 codegen->RestoreLiveRegisters(locations);
338 __ jmp(GetExitLabel());
339 }
340
341 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000342 HInstruction* const instruction_;
343 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000344 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000345 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000346
347 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
348};
349
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100350#undef __
351#define __ reinterpret_cast<X86_64Assembler*>(GetAssembler())->
352
Dave Allison20dfc792014-06-16 20:44:29 -0700353inline Condition X86_64Condition(IfCondition cond) {
354 switch (cond) {
355 case kCondEQ: return kEqual;
356 case kCondNE: return kNotEqual;
357 case kCondLT: return kLess;
358 case kCondLE: return kLessEqual;
359 case kCondGT: return kGreater;
360 case kCondGE: return kGreaterEqual;
361 default:
362 LOG(FATAL) << "Unknown if condition";
363 }
364 return kEqual;
365}
366
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100367void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
368 stream << X86_64ManagedRegister::FromCpuRegister(Register(reg));
369}
370
371void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
372 stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg));
373}
374
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100375size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
376 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
377 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100378}
379
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100380size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
381 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
382 return kX86_64WordSize;
383}
384
385size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
386 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
387 return kX86_64WordSize;
388}
389
390size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
391 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
392 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100393}
394
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100395CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100396 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfFloatRegisters, 0),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100397 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100398 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000399 instruction_visitor_(graph, this),
400 move_resolver_(graph->GetArena(), this) {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100401
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100402size_t CodeGeneratorX86_64::FrameEntrySpillSize() const {
403 return kNumberOfPushedRegistersAtEntry * kX86_64WordSize;
404}
405
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100406InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
407 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100408 : HGraphVisitor(graph),
409 assembler_(codegen->GetAssembler()),
410 codegen_(codegen) {}
411
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100412Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100413 switch (type) {
414 case Primitive::kPrimLong:
415 case Primitive::kPrimByte:
416 case Primitive::kPrimBoolean:
417 case Primitive::kPrimChar:
418 case Primitive::kPrimShort:
419 case Primitive::kPrimInt:
420 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100421 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100422 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100423 }
424
425 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100426 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100427 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100428 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100429 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100430
431 case Primitive::kPrimVoid:
432 LOG(FATAL) << "Unreachable type " << type;
433 }
434
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100435 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100436}
437
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100438void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100439 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100440 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100441
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000442 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100443 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000444
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100445 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100446 blocked_core_registers_[RBX] = true;
447 blocked_core_registers_[RBP] = true;
448 blocked_core_registers_[R12] = true;
449 blocked_core_registers_[R13] = true;
450 blocked_core_registers_[R14] = true;
451 blocked_core_registers_[R15] = true;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100452
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100453 blocked_fpu_registers_[XMM12] = true;
454 blocked_fpu_registers_[XMM13] = true;
455 blocked_fpu_registers_[XMM14] = true;
456 blocked_fpu_registers_[XMM15] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100457}
458
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100459void CodeGeneratorX86_64::GenerateFrameEntry() {
460 // Create a fake register to mimic Quick.
461 static const int kFakeReturnRegister = 16;
462 core_spill_mask_ |= (1 << kFakeReturnRegister);
463
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100464 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700465 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100466
467 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
468 __ testq(CpuRegister(RAX), Address(
469 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100470 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100471 }
472
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100473 // The return PC has already been pushed on the stack.
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100474 __ subq(CpuRegister(RSP),
475 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
476
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100477 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100478 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86_64();
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100479 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100480
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100481 __ gs()->cmpq(CpuRegister(RSP),
482 Address::Absolute(Thread::StackEndOffset<kX86_64WordSize>(), true));
483 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100484 }
485
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100486 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
487}
488
489void CodeGeneratorX86_64::GenerateFrameExit() {
490 __ addq(CpuRegister(RSP),
491 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
492}
493
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100494void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
495 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100496}
497
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100498void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100499 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
500}
501
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100502Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
503 switch (load->GetType()) {
504 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100505 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100506 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
507 break;
508
509 case Primitive::kPrimInt:
510 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100511 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100512 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100513
514 case Primitive::kPrimBoolean:
515 case Primitive::kPrimByte:
516 case Primitive::kPrimChar:
517 case Primitive::kPrimShort:
518 case Primitive::kPrimVoid:
519 LOG(FATAL) << "Unexpected type " << load->GetType();
520 }
521
522 LOG(FATAL) << "Unreachable";
523 return Location();
524}
525
526void CodeGeneratorX86_64::Move(Location destination, Location source) {
527 if (source.Equals(destination)) {
528 return;
529 }
530 if (destination.IsRegister()) {
531 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100532 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100533 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100534 __ movd(destination.As<CpuRegister>(), source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100535 } else if (source.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100536 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100537 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100538 } else {
539 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100540 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100541 Address(CpuRegister(RSP), source.GetStackIndex()));
542 }
543 } else if (destination.IsFpuRegister()) {
544 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100545 __ movd(destination.As<XmmRegister>(), source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100546 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100547 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100548 } else if (source.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100549 __ movss(destination.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100550 Address(CpuRegister(RSP), source.GetStackIndex()));
551 } else {
552 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100553 __ movsd(destination.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100554 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100555 }
556 } else if (destination.IsStackSlot()) {
557 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100558 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100559 source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100560 } else if (source.IsFpuRegister()) {
561 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100562 source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100563 } else {
564 DCHECK(source.IsStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000565 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
566 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100567 }
568 } else {
569 DCHECK(destination.IsDoubleStackSlot());
570 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100571 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100572 source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100573 } else if (source.IsFpuRegister()) {
574 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100575 source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100576 } else {
577 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000578 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
579 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100580 }
581 }
582}
583
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100584void CodeGeneratorX86_64::Move(HInstruction* instruction,
585 Location location,
586 HInstruction* move_for) {
Roland Levillain476df552014-10-09 17:51:36 +0100587 if (instruction->IsIntConstant()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100588 Immediate imm(instruction->AsIntConstant()->GetValue());
589 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100590 __ movl(location.As<CpuRegister>(), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100591 } else if (location.IsStackSlot()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100592 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100593 } else {
594 DCHECK(location.IsConstant());
595 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100596 }
Roland Levillain476df552014-10-09 17:51:36 +0100597 } else if (instruction->IsLongConstant()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100598 int64_t value = instruction->AsLongConstant()->GetValue();
599 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100600 __ movq(location.As<CpuRegister>(), Immediate(value));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100601 } else if (location.IsDoubleStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000602 __ movq(CpuRegister(TMP), Immediate(value));
603 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100604 } else {
605 DCHECK(location.IsConstant());
606 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100607 }
Roland Levillain476df552014-10-09 17:51:36 +0100608 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100609 switch (instruction->GetType()) {
610 case Primitive::kPrimBoolean:
611 case Primitive::kPrimByte:
612 case Primitive::kPrimChar:
613 case Primitive::kPrimShort:
614 case Primitive::kPrimInt:
615 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100616 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100617 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
618 break;
619
620 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100621 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100622 Move(location, Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
623 break;
624
625 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100626 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100627 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000628 } else if (instruction->IsTemporary()) {
629 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
630 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100631 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100632 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100633 switch (instruction->GetType()) {
634 case Primitive::kPrimBoolean:
635 case Primitive::kPrimByte:
636 case Primitive::kPrimChar:
637 case Primitive::kPrimShort:
638 case Primitive::kPrimInt:
639 case Primitive::kPrimNot:
640 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100641 case Primitive::kPrimFloat:
642 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100643 Move(location, instruction->GetLocations()->Out());
644 break;
645
646 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100647 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100648 }
649 }
650}
651
652void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
653 got->SetLocations(nullptr);
654}
655
656void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
657 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100658 DCHECK(!successor->IsExitBlock());
659
660 HBasicBlock* block = got->GetBlock();
661 HInstruction* previous = got->GetPrevious();
662
663 HLoopInformation* info = block->GetLoopInformation();
664 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
665 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
666 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
667 return;
668 }
669
670 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
671 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
672 }
673 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100674 __ jmp(codegen_->GetLabelOf(successor));
675 }
676}
677
678void LocationsBuilderX86_64::VisitExit(HExit* exit) {
679 exit->SetLocations(nullptr);
680}
681
682void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700683 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100684 if (kIsDebugBuild) {
685 __ Comment("Unreachable");
686 __ int3();
687 }
688}
689
690void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100691 LocationSummary* locations =
692 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100693 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100694 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100695 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100696 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100697}
698
699void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700700 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100701 if (cond->IsIntConstant()) {
702 // Constant condition, statically compared against 1.
703 int32_t cond_value = cond->AsIntConstant()->GetValue();
704 if (cond_value == 1) {
705 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
706 if_instr->IfTrueSuccessor())) {
707 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100708 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100709 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100710 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100711 DCHECK_EQ(cond_value, 0);
712 }
713 } else {
714 bool materialized =
715 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
716 // Moves do not affect the eflags register, so if the condition is
717 // evaluated just before the if, we don't need to evaluate it
718 // again.
719 bool eflags_set = cond->IsCondition()
720 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
721 if (materialized) {
722 if (!eflags_set) {
723 // Materialized condition, compare against 0.
724 Location lhs = if_instr->GetLocations()->InAt(0);
725 if (lhs.IsRegister()) {
726 __ cmpl(lhs.As<CpuRegister>(), Immediate(0));
727 } else {
728 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
729 Immediate(0));
730 }
731 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
732 } else {
733 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
734 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
735 }
736 } else {
737 Location lhs = cond->GetLocations()->InAt(0);
738 Location rhs = cond->GetLocations()->InAt(1);
739 if (rhs.IsRegister()) {
740 __ cmpl(lhs.As<CpuRegister>(), rhs.As<CpuRegister>());
741 } else if (rhs.IsConstant()) {
742 __ cmpl(lhs.As<CpuRegister>(),
743 Immediate(rhs.GetConstant()->AsIntConstant()->GetValue()));
744 } else {
745 __ cmpl(lhs.As<CpuRegister>(),
746 Address(CpuRegister(RSP), rhs.GetStackIndex()));
747 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100748 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
749 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700750 }
Dave Allison20dfc792014-06-16 20:44:29 -0700751 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100752 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
753 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700754 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100755 }
756}
757
758void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
759 local->SetLocations(nullptr);
760}
761
762void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
763 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
764}
765
766void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
767 local->SetLocations(nullptr);
768}
769
770void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
771 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700772 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100773}
774
775void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100776 LocationSummary* locations =
777 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100778 switch (store->InputAt(1)->GetType()) {
779 case Primitive::kPrimBoolean:
780 case Primitive::kPrimByte:
781 case Primitive::kPrimChar:
782 case Primitive::kPrimShort:
783 case Primitive::kPrimInt:
784 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100785 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100786 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
787 break;
788
789 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100790 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100791 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
792 break;
793
794 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100795 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100796 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100797}
798
799void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700800 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100801}
802
Dave Allison20dfc792014-06-16 20:44:29 -0700803void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100804 LocationSummary* locations =
805 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100806 locations->SetInAt(0, Location::RequiresRegister());
807 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100808 if (comp->NeedsMaterialization()) {
809 locations->SetOut(Location::RequiresRegister());
810 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100811}
812
Dave Allison20dfc792014-06-16 20:44:29 -0700813void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
814 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100815 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100816 CpuRegister reg = locations->Out().As<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100817 // Clear register: setcc only sets the low byte.
818 __ xorq(reg, reg);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100819 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100820 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100821 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100822 } else if (locations->InAt(1).IsConstant()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100823 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100824 Immediate(locations->InAt(1).GetConstant()->AsIntConstant()->GetValue()));
825 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100826 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100827 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
828 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100829 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700830 }
831}
832
833void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
834 VisitCondition(comp);
835}
836
837void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
838 VisitCondition(comp);
839}
840
841void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
842 VisitCondition(comp);
843}
844
845void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
846 VisitCondition(comp);
847}
848
849void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
850 VisitCondition(comp);
851}
852
853void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
854 VisitCondition(comp);
855}
856
857void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
858 VisitCondition(comp);
859}
860
861void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
862 VisitCondition(comp);
863}
864
865void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
866 VisitCondition(comp);
867}
868
869void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
870 VisitCondition(comp);
871}
872
873void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
874 VisitCondition(comp);
875}
876
877void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
878 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100879}
880
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100881void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100882 LocationSummary* locations =
883 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100884 locations->SetInAt(0, Location::RequiresRegister());
885 locations->SetInAt(1, Location::RequiresRegister());
886 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100887}
888
889void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
890 Label greater, done;
891 LocationSummary* locations = compare->GetLocations();
892 switch (compare->InputAt(0)->GetType()) {
893 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100894 __ cmpq(locations->InAt(0).As<CpuRegister>(),
895 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100896 break;
897 default:
898 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
899 }
900
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100901 CpuRegister output = locations->Out().As<CpuRegister>();
902 __ movl(output, Immediate(0));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100903 __ j(kEqual, &done);
904 __ j(kGreater, &greater);
905
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100906 __ movl(output, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100907 __ jmp(&done);
908
909 __ Bind(&greater);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100910 __ movl(output, Immediate(1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100911
912 __ Bind(&done);
913}
914
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100915void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100916 LocationSummary* locations =
917 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100918 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100919}
920
921void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100922 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700923 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100924}
925
926void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100927 LocationSummary* locations =
928 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100929 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100930}
931
932void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100933 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700934 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100935}
936
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100937void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
938 LocationSummary* locations =
939 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
940 locations->SetOut(Location::ConstantLocation(constant));
941}
942
943void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
944 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700945 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100946}
947
948void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
949 LocationSummary* locations =
950 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
951 locations->SetOut(Location::ConstantLocation(constant));
952}
953
954void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
955 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700956 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100957}
958
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100959void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
960 ret->SetLocations(nullptr);
961}
962
963void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700964 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100965 codegen_->GenerateFrameExit();
966 __ ret();
967}
968
969void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100970 LocationSummary* locations =
971 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100972 switch (ret->InputAt(0)->GetType()) {
973 case Primitive::kPrimBoolean:
974 case Primitive::kPrimByte:
975 case Primitive::kPrimChar:
976 case Primitive::kPrimShort:
977 case Primitive::kPrimInt:
978 case Primitive::kPrimNot:
979 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100980 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100981 break;
982
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100983 case Primitive::kPrimFloat:
984 case Primitive::kPrimDouble:
985 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100986 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100987 break;
988
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100989 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100990 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100991 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100992}
993
994void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
995 if (kIsDebugBuild) {
996 switch (ret->InputAt(0)->GetType()) {
997 case Primitive::kPrimBoolean:
998 case Primitive::kPrimByte:
999 case Primitive::kPrimChar:
1000 case Primitive::kPrimShort:
1001 case Primitive::kPrimInt:
1002 case Primitive::kPrimNot:
1003 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001004 DCHECK_EQ(ret->GetLocations()->InAt(0).As<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001005 break;
1006
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001007 case Primitive::kPrimFloat:
1008 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001009 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001010 XMM0);
1011 break;
1012
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001013 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001014 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001015 }
1016 }
1017 codegen_->GenerateFrameExit();
1018 __ ret();
1019}
1020
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001021Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
1022 switch (type) {
1023 case Primitive::kPrimBoolean:
1024 case Primitive::kPrimByte:
1025 case Primitive::kPrimChar:
1026 case Primitive::kPrimShort:
1027 case Primitive::kPrimInt:
1028 case Primitive::kPrimNot: {
1029 uint32_t index = gp_index_++;
1030 stack_index_++;
1031 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001032 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001033 } else {
1034 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1035 }
1036 }
1037
1038 case Primitive::kPrimLong: {
1039 uint32_t index = gp_index_;
1040 stack_index_ += 2;
1041 if (index < calling_convention.GetNumberOfRegisters()) {
1042 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001043 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001044 } else {
1045 gp_index_ += 2;
1046 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1047 }
1048 }
1049
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001050 case Primitive::kPrimFloat: {
1051 uint32_t index = fp_index_++;
1052 stack_index_++;
1053 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001054 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001055 } else {
1056 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1057 }
1058 }
1059
1060 case Primitive::kPrimDouble: {
1061 uint32_t index = fp_index_++;
1062 stack_index_ += 2;
1063 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001064 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001065 } else {
1066 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1067 }
1068 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001069
1070 case Primitive::kPrimVoid:
1071 LOG(FATAL) << "Unexpected parameter type " << type;
1072 break;
1073 }
1074 return Location();
1075}
1076
1077void LocationsBuilderX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001078 HandleInvoke(invoke);
1079}
1080
1081void InstructionCodeGeneratorX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001082 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001083 // TODO: Implement all kinds of calls:
1084 // 1) boot -> boot
1085 // 2) app -> boot
1086 // 3) app -> app
1087 //
1088 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1089
1090 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001091 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001092 // temp = temp->dex_cache_resolved_methods_;
1093 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
1094 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001095 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001096 // (temp + offset_of_quick_compiled_code)()
1097 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
1098
1099 DCHECK(!codegen_->IsLeafMethod());
1100 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1101}
1102
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001103void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001104 LocationSummary* locations =
1105 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001106 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001107
1108 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001109 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001110 HInstruction* input = invoke->InputAt(i);
1111 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1112 }
1113
1114 switch (invoke->GetType()) {
1115 case Primitive::kPrimBoolean:
1116 case Primitive::kPrimByte:
1117 case Primitive::kPrimChar:
1118 case Primitive::kPrimShort:
1119 case Primitive::kPrimInt:
1120 case Primitive::kPrimNot:
1121 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001122 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001123 break;
1124
1125 case Primitive::kPrimVoid:
1126 break;
1127
1128 case Primitive::kPrimDouble:
1129 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001130 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001131 break;
1132 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001133}
1134
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001135void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1136 HandleInvoke(invoke);
1137}
1138
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001139void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001140 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001141 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1142 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1143 LocationSummary* locations = invoke->GetLocations();
1144 Location receiver = locations->InAt(0);
1145 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1146 // temp = object->GetClass();
1147 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001148 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1149 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001150 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001151 __ movl(temp, Address(receiver.As<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001152 }
1153 // temp = temp->GetMethodAt(method_offset);
1154 __ movl(temp, Address(temp, method_offset));
1155 // call temp->GetEntryPoint();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001156 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
1157
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001158 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001159 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001160}
1161
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001162void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1163 HandleInvoke(invoke);
1164 // Add the hidden argument.
1165 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1166}
1167
1168void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1169 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1170 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
1171 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1172 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1173 LocationSummary* locations = invoke->GetLocations();
1174 Location receiver = locations->InAt(0);
1175 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1176
1177 // Set the hidden argument.
1178 __ movq(invoke->GetLocations()->GetTemp(1).As<CpuRegister>(),
1179 Immediate(invoke->GetDexMethodIndex()));
1180
1181 // temp = object->GetClass();
1182 if (receiver.IsStackSlot()) {
1183 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1184 __ movl(temp, Address(temp, class_offset));
1185 } else {
1186 __ movl(temp, Address(receiver.As<CpuRegister>(), class_offset));
1187 }
1188 // temp = temp->GetImtEntryAt(method_offset);
1189 __ movl(temp, Address(temp, method_offset));
1190 // call temp->GetEntryPoint();
1191 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
1192
1193 DCHECK(!codegen_->IsLeafMethod());
1194 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1195}
1196
Roland Levillain88cb1752014-10-20 16:36:47 +01001197void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1198 LocationSummary* locations =
1199 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1200 switch (neg->GetResultType()) {
1201 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001202 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001203 locations->SetInAt(0, Location::RequiresRegister());
1204 locations->SetOut(Location::SameAsFirstInput());
1205 break;
1206
Roland Levillain88cb1752014-10-20 16:36:47 +01001207 case Primitive::kPrimFloat:
1208 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001209 locations->SetInAt(0, Location::RequiresFpuRegister());
1210 // Output overlaps as we need a fresh (zero-initialized)
1211 // register to perform subtraction from zero.
1212 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001213 break;
1214
1215 default:
1216 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1217 }
1218}
1219
1220void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1221 LocationSummary* locations = neg->GetLocations();
1222 Location out = locations->Out();
1223 Location in = locations->InAt(0);
1224 switch (neg->GetResultType()) {
1225 case Primitive::kPrimInt:
1226 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001227 DCHECK(in.Equals(out));
Roland Levillain88cb1752014-10-20 16:36:47 +01001228 __ negl(out.As<CpuRegister>());
1229 break;
1230
1231 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001232 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001233 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001234 __ negq(out.As<CpuRegister>());
1235 break;
1236
Roland Levillain88cb1752014-10-20 16:36:47 +01001237 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001238 DCHECK(in.IsFpuRegister());
1239 DCHECK(out.IsFpuRegister());
1240 DCHECK(!in.Equals(out));
1241 // TODO: Instead of computing negation as a subtraction from
1242 // zero, implement it with an exclusive or with value 0x80000000
1243 // (mask for bit 31, representing the sign of a single-precision
1244 // floating-point number), fetched from a constant pool:
1245 //
1246 // xorps out, [RIP:...] // value at RIP is 0x80 00 00 00
1247
1248 // out = 0
1249 __ xorps(out.As<XmmRegister>(), out.As<XmmRegister>());
1250 // out = out - in
1251 __ subss(out.As<XmmRegister>(), in.As<XmmRegister>());
1252 break;
1253
Roland Levillain88cb1752014-10-20 16:36:47 +01001254 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001255 DCHECK(in.IsFpuRegister());
1256 DCHECK(out.IsFpuRegister());
1257 DCHECK(!in.Equals(out));
1258 // TODO: Instead of computing negation as a subtraction from
1259 // zero, implement it with an exclusive or with value
1260 // 0x8000000000000000 (mask for bit 63, representing the sign of
1261 // a double-precision floating-point number), fetched from a
1262 // constant pool:
1263 //
1264 // xorpd out, [RIP:...] // value at RIP is 0x80 00 00 00 00 00 00 00
1265
1266 // out = 0
1267 __ xorpd(out.As<XmmRegister>(), out.As<XmmRegister>());
1268 // out = out - in
1269 __ subsd(out.As<XmmRegister>(), in.As<XmmRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001270 break;
1271
1272 default:
1273 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1274 }
1275}
1276
Roland Levillaindff1f282014-11-05 14:15:05 +00001277void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1278 LocationSummary* locations =
1279 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1280 Primitive::Type result_type = conversion->GetResultType();
1281 Primitive::Type input_type = conversion->GetInputType();
1282 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001283 case Primitive::kPrimByte:
1284 switch (input_type) {
1285 case Primitive::kPrimShort:
1286 case Primitive::kPrimInt:
1287 case Primitive::kPrimChar:
1288 // int-to-byte conversion.
1289 locations->SetInAt(0, Location::Any());
1290 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1291 break;
1292
1293 default:
1294 LOG(FATAL) << "Unexpected type conversion from " << input_type
1295 << " to " << result_type;
1296 }
1297 break;
1298
Roland Levillain946e1432014-11-11 17:35:19 +00001299 case Primitive::kPrimInt:
1300 switch (input_type) {
1301 case Primitive::kPrimLong:
1302 // long-to-int conversion.
1303 locations->SetInAt(0, Location::Any());
1304 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1305 break;
1306
1307 case Primitive::kPrimFloat:
1308 case Primitive::kPrimDouble:
1309 LOG(FATAL) << "Type conversion from " << input_type
1310 << " to " << result_type << " not yet implemented";
1311 break;
1312
1313 default:
1314 LOG(FATAL) << "Unexpected type conversion from " << input_type
1315 << " to " << result_type;
1316 }
1317 break;
1318
Roland Levillaindff1f282014-11-05 14:15:05 +00001319 case Primitive::kPrimLong:
1320 switch (input_type) {
1321 case Primitive::kPrimByte:
1322 case Primitive::kPrimShort:
1323 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001324 case Primitive::kPrimChar:
Roland Levillaindff1f282014-11-05 14:15:05 +00001325 // int-to-long conversion.
1326 // TODO: We would benefit from a (to-be-implemented)
1327 // Location::RegisterOrStackSlot requirement for this input.
1328 locations->SetInAt(0, Location::RequiresRegister());
1329 locations->SetOut(Location::RequiresRegister());
1330 break;
1331
1332 case Primitive::kPrimFloat:
1333 case Primitive::kPrimDouble:
1334 LOG(FATAL) << "Type conversion from " << input_type << " to "
1335 << result_type << " not yet implemented";
1336 break;
1337
1338 default:
1339 LOG(FATAL) << "Unexpected type conversion from " << input_type
1340 << " to " << result_type;
1341 }
1342 break;
1343
Roland Levillaindff1f282014-11-05 14:15:05 +00001344 case Primitive::kPrimFloat:
1345 case Primitive::kPrimDouble:
1346 LOG(FATAL) << "Type conversion from " << input_type
1347 << " to " << result_type << " not yet implemented";
1348 break;
1349
1350 default:
1351 LOG(FATAL) << "Unexpected type conversion from " << input_type
1352 << " to " << result_type;
1353 }
1354}
1355
1356void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1357 LocationSummary* locations = conversion->GetLocations();
1358 Location out = locations->Out();
1359 Location in = locations->InAt(0);
1360 Primitive::Type result_type = conversion->GetResultType();
1361 Primitive::Type input_type = conversion->GetInputType();
1362 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001363 case Primitive::kPrimByte:
1364 switch (input_type) {
1365 case Primitive::kPrimShort:
1366 case Primitive::kPrimInt:
1367 case Primitive::kPrimChar:
1368 // int-to-byte conversion.
1369 if (in.IsRegister()) {
1370 __ movsxb(out.As<CpuRegister>(), in.As<CpuRegister>());
1371 } else if (in.IsStackSlot()) {
1372 __ movsxb(out.As<CpuRegister>(),
1373 Address(CpuRegister(RSP), in.GetStackIndex()));
1374 } else {
1375 DCHECK(in.GetConstant()->IsIntConstant());
1376 __ movl(out.As<CpuRegister>(),
1377 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1378 }
1379 break;
1380
1381 default:
1382 LOG(FATAL) << "Unexpected type conversion from " << input_type
1383 << " to " << result_type;
1384 }
1385 break;
1386
Roland Levillain946e1432014-11-11 17:35:19 +00001387 case Primitive::kPrimInt:
1388 switch (input_type) {
1389 case Primitive::kPrimLong:
1390 // long-to-int conversion.
1391 if (in.IsRegister()) {
1392 __ movl(out.As<CpuRegister>(), in.As<CpuRegister>());
1393 } else if (in.IsDoubleStackSlot()) {
1394 __ movl(out.As<CpuRegister>(),
1395 Address(CpuRegister(RSP), in.GetStackIndex()));
1396 } else {
1397 DCHECK(in.IsConstant());
1398 DCHECK(in.GetConstant()->IsLongConstant());
1399 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
1400 __ movl(out.As<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1401 }
1402 break;
1403
1404 case Primitive::kPrimFloat:
1405 case Primitive::kPrimDouble:
1406 LOG(FATAL) << "Type conversion from " << input_type
1407 << " to " << result_type << " not yet implemented";
1408 break;
1409
1410 default:
1411 LOG(FATAL) << "Unexpected type conversion from " << input_type
1412 << " to " << result_type;
1413 }
1414 break;
1415
Roland Levillaindff1f282014-11-05 14:15:05 +00001416 case Primitive::kPrimLong:
1417 switch (input_type) {
1418 DCHECK(out.IsRegister());
1419 case Primitive::kPrimByte:
1420 case Primitive::kPrimShort:
1421 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001422 case Primitive::kPrimChar:
Roland Levillaindff1f282014-11-05 14:15:05 +00001423 // int-to-long conversion.
1424 DCHECK(in.IsRegister());
1425 __ movsxd(out.As<CpuRegister>(), in.As<CpuRegister>());
1426 break;
1427
1428 case Primitive::kPrimFloat:
1429 case Primitive::kPrimDouble:
1430 LOG(FATAL) << "Type conversion from " << input_type << " to "
1431 << result_type << " not yet implemented";
1432 break;
1433
1434 default:
1435 LOG(FATAL) << "Unexpected type conversion from " << input_type
1436 << " to " << result_type;
1437 }
1438 break;
1439
Roland Levillaindff1f282014-11-05 14:15:05 +00001440 case Primitive::kPrimFloat:
1441 case Primitive::kPrimDouble:
1442 LOG(FATAL) << "Type conversion from " << input_type
1443 << " to " << result_type << " not yet implemented";
1444 break;
1445
1446 default:
1447 LOG(FATAL) << "Unexpected type conversion from " << input_type
1448 << " to " << result_type;
1449 }
1450}
1451
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001452void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001453 LocationSummary* locations =
1454 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001455 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001456 case Primitive::kPrimInt: {
1457 locations->SetInAt(0, Location::RequiresRegister());
1458 locations->SetInAt(1, Location::Any());
1459 locations->SetOut(Location::SameAsFirstInput());
1460 break;
1461 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001462
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001463 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001464 locations->SetInAt(0, Location::RequiresRegister());
1465 locations->SetInAt(1, Location::RequiresRegister());
1466 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001467 break;
1468 }
1469
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001470 case Primitive::kPrimDouble:
1471 case Primitive::kPrimFloat: {
1472 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001473 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001474 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001475 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001476 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001477
1478 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001479 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001480 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001481}
1482
1483void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1484 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001485 Location first = locations->InAt(0);
1486 Location second = locations->InAt(1);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001487 DCHECK(first.Equals(locations->Out()));
Calin Juravle11351682014-10-23 15:38:15 +01001488
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001489 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001490 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001491 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001492 __ addl(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001493 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001494 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001495 __ addl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001496 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001497 __ addl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001498 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001499 break;
1500 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001501
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001502 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001503 __ addq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001504 break;
1505 }
1506
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001507 case Primitive::kPrimFloat: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001508 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001509 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001510 }
1511
1512 case Primitive::kPrimDouble: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001513 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001514 break;
1515 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001516
1517 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001518 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001519 }
1520}
1521
1522void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001523 LocationSummary* locations =
1524 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001525 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001526 case Primitive::kPrimInt: {
1527 locations->SetInAt(0, Location::RequiresRegister());
1528 locations->SetInAt(1, Location::Any());
1529 locations->SetOut(Location::SameAsFirstInput());
1530 break;
1531 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001532 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001533 locations->SetInAt(0, Location::RequiresRegister());
1534 locations->SetInAt(1, Location::RequiresRegister());
1535 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001536 break;
1537 }
Calin Juravle11351682014-10-23 15:38:15 +01001538 case Primitive::kPrimFloat:
1539 case Primitive::kPrimDouble: {
1540 locations->SetInAt(0, Location::RequiresFpuRegister());
1541 locations->SetInAt(1, Location::RequiresFpuRegister());
1542 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001543 break;
Calin Juravle11351682014-10-23 15:38:15 +01001544 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001545 default:
Calin Juravle11351682014-10-23 15:38:15 +01001546 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001547 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001548}
1549
1550void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1551 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001552 Location first = locations->InAt(0);
1553 Location second = locations->InAt(1);
1554 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001555 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001556 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001557 if (second.IsRegister()) {
1558 __ subl(first.As<CpuRegister>(), second.As<CpuRegister>());
1559 } else if (second.IsConstant()) {
1560 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1561 __ subl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001562 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001563 __ subl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001564 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001565 break;
1566 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001567 case Primitive::kPrimLong: {
Calin Juravle11351682014-10-23 15:38:15 +01001568 __ subq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001569 break;
1570 }
1571
Calin Juravle11351682014-10-23 15:38:15 +01001572 case Primitive::kPrimFloat: {
1573 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001574 break;
Calin Juravle11351682014-10-23 15:38:15 +01001575 }
1576
1577 case Primitive::kPrimDouble: {
1578 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1579 break;
1580 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001581
1582 default:
Calin Juravle11351682014-10-23 15:38:15 +01001583 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001584 }
1585}
1586
Calin Juravle34bacdf2014-10-07 20:23:36 +01001587void LocationsBuilderX86_64::VisitMul(HMul* mul) {
1588 LocationSummary* locations =
1589 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1590 switch (mul->GetResultType()) {
1591 case Primitive::kPrimInt: {
1592 locations->SetInAt(0, Location::RequiresRegister());
1593 locations->SetInAt(1, Location::Any());
1594 locations->SetOut(Location::SameAsFirstInput());
1595 break;
1596 }
1597 case Primitive::kPrimLong: {
1598 locations->SetInAt(0, Location::RequiresRegister());
1599 locations->SetInAt(1, Location::RequiresRegister());
1600 locations->SetOut(Location::SameAsFirstInput());
1601 break;
1602 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001603 case Primitive::kPrimFloat:
1604 case Primitive::kPrimDouble: {
1605 locations->SetInAt(0, Location::RequiresFpuRegister());
1606 locations->SetInAt(1, Location::RequiresFpuRegister());
1607 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001608 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001609 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001610
1611 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001612 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001613 }
1614}
1615
1616void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
1617 LocationSummary* locations = mul->GetLocations();
1618 Location first = locations->InAt(0);
1619 Location second = locations->InAt(1);
1620 DCHECK(first.Equals(locations->Out()));
1621 switch (mul->GetResultType()) {
1622 case Primitive::kPrimInt: {
1623 if (second.IsRegister()) {
1624 __ imull(first.As<CpuRegister>(), second.As<CpuRegister>());
1625 } else if (second.IsConstant()) {
1626 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1627 __ imull(first.As<CpuRegister>(), imm);
1628 } else {
1629 DCHECK(second.IsStackSlot());
1630 __ imull(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
1631 }
1632 break;
1633 }
1634 case Primitive::kPrimLong: {
1635 __ imulq(first.As<CpuRegister>(), second.As<CpuRegister>());
1636 break;
1637 }
1638
Calin Juravleb5bfa962014-10-21 18:02:24 +01001639 case Primitive::kPrimFloat: {
1640 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001641 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001642 }
1643
1644 case Primitive::kPrimDouble: {
1645 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1646 break;
1647 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001648
1649 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001650 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001651 }
1652}
1653
Calin Juravle7c4954d2014-10-28 16:57:40 +00001654void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
1655 LocationSummary* locations =
1656 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1657 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001658 case Primitive::kPrimInt:
1659 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00001660 locations->SetInAt(0, Location::RegisterLocation(RAX));
1661 locations->SetInAt(1, Location::RequiresRegister());
1662 locations->SetOut(Location::SameAsFirstInput());
1663 // Intel uses edx:eax as the dividend.
1664 locations->AddTemp(Location::RegisterLocation(RDX));
1665 break;
1666 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001667
Calin Juravle7c4954d2014-10-28 16:57:40 +00001668 case Primitive::kPrimFloat:
1669 case Primitive::kPrimDouble: {
1670 locations->SetInAt(0, Location::RequiresFpuRegister());
1671 locations->SetInAt(1, Location::RequiresFpuRegister());
1672 locations->SetOut(Location::SameAsFirstInput());
1673 break;
1674 }
1675
1676 default:
1677 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1678 }
1679}
1680
1681void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
1682 LocationSummary* locations = div->GetLocations();
1683 Location first = locations->InAt(0);
1684 Location second = locations->InAt(1);
1685 DCHECK(first.Equals(locations->Out()));
1686
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001687 Primitive::Type type = div->GetResultType();
1688 switch (type) {
1689 case Primitive::kPrimInt:
1690 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00001691 CpuRegister first_reg = first.As<CpuRegister>();
1692 CpuRegister second_reg = second.As<CpuRegister>();
1693 DCHECK_EQ(RAX, first_reg.AsRegister());
1694 DCHECK_EQ(RDX, locations->GetTemp(0).As<CpuRegister>().AsRegister());
1695
1696 SlowPathCodeX86_64* slow_path =
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001697 new (GetGraph()->GetArena()) DivMinusOneSlowPathX86_64(first_reg.AsRegister(), type);
Calin Juravled0d48522014-11-04 16:40:20 +00001698 codegen_->AddSlowPath(slow_path);
1699
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001700 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
1701 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
1702 // so it's safe to just use negl instead of more complex comparisons.
Calin Juravled0d48522014-11-04 16:40:20 +00001703
1704 __ cmpl(second_reg, Immediate(-1));
1705 __ j(kEqual, slow_path->GetEntryLabel());
1706
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001707 if (type == Primitive::kPrimInt) {
1708 // edx:eax <- sign-extended of eax
1709 __ cdq();
1710 // eax = quotient, edx = remainder
1711 __ idivl(second_reg);
1712 } else {
1713 // rdx:rax <- sign-extended of rax
1714 __ cqo();
1715 // rax = quotient, rdx = remainder
1716 __ idivq(second_reg);
1717 }
Calin Juravled0d48522014-11-04 16:40:20 +00001718
1719 __ Bind(slow_path->GetExitLabel());
1720 break;
1721 }
1722
Calin Juravle7c4954d2014-10-28 16:57:40 +00001723 case Primitive::kPrimFloat: {
1724 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1725 break;
1726 }
1727
1728 case Primitive::kPrimDouble: {
1729 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1730 break;
1731 }
1732
1733 default:
1734 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1735 }
1736}
1737
Calin Juravled0d48522014-11-04 16:40:20 +00001738void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1739 LocationSummary* locations =
1740 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1741 locations->SetInAt(0, Location::Any());
1742 if (instruction->HasUses()) {
1743 locations->SetOut(Location::SameAsFirstInput());
1744 }
1745}
1746
1747void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1748 SlowPathCodeX86_64* slow_path =
1749 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
1750 codegen_->AddSlowPath(slow_path);
1751
1752 LocationSummary* locations = instruction->GetLocations();
1753 Location value = locations->InAt(0);
1754
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001755 switch (instruction->GetType()) {
1756 case Primitive::kPrimInt: {
1757 if (value.IsRegister()) {
1758 __ testl(value.As<CpuRegister>(), value.As<CpuRegister>());
1759 __ j(kEqual, slow_path->GetEntryLabel());
1760 } else if (value.IsStackSlot()) {
1761 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
1762 __ j(kEqual, slow_path->GetEntryLabel());
1763 } else {
1764 DCHECK(value.IsConstant()) << value;
1765 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
1766 __ jmp(slow_path->GetEntryLabel());
1767 }
1768 }
1769 break;
Calin Juravled0d48522014-11-04 16:40:20 +00001770 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001771 case Primitive::kPrimLong: {
1772 if (value.IsRegister()) {
1773 __ testq(value.As<CpuRegister>(), value.As<CpuRegister>());
1774 __ j(kEqual, slow_path->GetEntryLabel());
1775 } else if (value.IsDoubleStackSlot()) {
1776 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
1777 __ j(kEqual, slow_path->GetEntryLabel());
1778 } else {
1779 DCHECK(value.IsConstant()) << value;
1780 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
1781 __ jmp(slow_path->GetEntryLabel());
1782 }
1783 }
1784 break;
1785 }
1786 default:
1787 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00001788 }
Calin Juravled0d48522014-11-04 16:40:20 +00001789}
1790
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001791void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001792 LocationSummary* locations =
1793 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001794 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001795 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1796 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1797 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001798}
1799
1800void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
1801 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001802 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001803 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1804
1805 __ gs()->call(Address::Absolute(
1806 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
1807
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001808 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001809 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001810}
1811
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001812void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
1813 LocationSummary* locations =
1814 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1815 InvokeRuntimeCallingConvention calling_convention;
1816 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1817 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1818 locations->SetOut(Location::RegisterLocation(RAX));
1819 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1820}
1821
1822void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
1823 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001824 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001825 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1826
1827 __ gs()->call(Address::Absolute(
1828 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocArrayWithAccessCheck), true));
1829
1830 DCHECK(!codegen_->IsLeafMethod());
1831 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1832}
1833
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001834void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001835 LocationSummary* locations =
1836 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001837 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1838 if (location.IsStackSlot()) {
1839 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1840 } else if (location.IsDoubleStackSlot()) {
1841 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1842 }
1843 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001844}
1845
1846void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
1847 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001848 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001849}
1850
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001851void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001852 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001853 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001854 locations->SetInAt(0, Location::RequiresRegister());
1855 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001856}
1857
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001858void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
1859 LocationSummary* locations = not_->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001860 DCHECK_EQ(locations->InAt(0).As<CpuRegister>().AsRegister(),
1861 locations->Out().As<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001862 Location out = locations->Out();
1863 switch (not_->InputAt(0)->GetType()) {
1864 case Primitive::kPrimBoolean:
1865 __ xorq(out.As<CpuRegister>(), Immediate(1));
1866 break;
1867
1868 case Primitive::kPrimInt:
1869 __ notl(out.As<CpuRegister>());
1870 break;
1871
1872 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001873 __ notq(out.As<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001874 break;
1875
1876 default:
1877 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1878 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001879}
1880
1881void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001882 LocationSummary* locations =
1883 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001884 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1885 locations->SetInAt(i, Location::Any());
1886 }
1887 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001888}
1889
1890void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001891 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001892 LOG(FATAL) << "Unimplemented";
1893}
1894
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001895void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001896 LocationSummary* locations =
1897 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001898 Primitive::Type field_type = instruction->GetFieldType();
1899 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001900 locations->SetInAt(0, Location::RequiresRegister());
1901 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001902 if (is_object_type) {
1903 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001904 locations->AddTemp(Location::RequiresRegister());
1905 locations->AddTemp(Location::RequiresRegister());
1906 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001907}
1908
1909void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1910 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001911 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001912 size_t offset = instruction->GetFieldOffset().SizeValue();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001913 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001914
1915 switch (field_type) {
1916 case Primitive::kPrimBoolean:
1917 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001918 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001919 __ movb(Address(obj, offset), value);
1920 break;
1921 }
1922
1923 case Primitive::kPrimShort:
1924 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001925 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001926 __ movw(Address(obj, offset), value);
1927 break;
1928 }
1929
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001930 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001931 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001932 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001933 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001934 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001935 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
1936 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001937 codegen_->MarkGCCard(temp, card, obj, value);
1938 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001939 break;
1940 }
1941
1942 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001943 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001944 __ movq(Address(obj, offset), value);
1945 break;
1946 }
1947
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001948 case Primitive::kPrimFloat: {
1949 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1950 __ movss(Address(obj, offset), value);
1951 break;
1952 }
1953
1954 case Primitive::kPrimDouble: {
1955 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1956 __ movsd(Address(obj, offset), value);
1957 break;
1958 }
1959
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001960 case Primitive::kPrimVoid:
1961 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001962 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001963 }
1964}
1965
1966void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001967 LocationSummary* locations =
1968 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001969 locations->SetInAt(0, Location::RequiresRegister());
1970 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001971}
1972
1973void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1974 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001975 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001976 size_t offset = instruction->GetFieldOffset().SizeValue();
1977
1978 switch (instruction->GetType()) {
1979 case Primitive::kPrimBoolean: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001980 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001981 __ movzxb(out, Address(obj, offset));
1982 break;
1983 }
1984
1985 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001986 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001987 __ movsxb(out, Address(obj, offset));
1988 break;
1989 }
1990
1991 case Primitive::kPrimShort: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001992 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001993 __ movsxw(out, Address(obj, offset));
1994 break;
1995 }
1996
1997 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001998 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001999 __ movzxw(out, Address(obj, offset));
2000 break;
2001 }
2002
2003 case Primitive::kPrimInt:
2004 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002005 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002006 __ movl(out, Address(obj, offset));
2007 break;
2008 }
2009
2010 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002011 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002012 __ movq(out, Address(obj, offset));
2013 break;
2014 }
2015
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002016 case Primitive::kPrimFloat: {
2017 XmmRegister out = locations->Out().As<XmmRegister>();
2018 __ movss(out, Address(obj, offset));
2019 break;
2020 }
2021
2022 case Primitive::kPrimDouble: {
2023 XmmRegister out = locations->Out().As<XmmRegister>();
2024 __ movsd(out, Address(obj, offset));
2025 break;
2026 }
2027
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002028 case Primitive::kPrimVoid:
2029 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002030 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002031 }
2032}
2033
2034void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002035 LocationSummary* locations =
2036 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002037 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002038 if (instruction->HasUses()) {
2039 locations->SetOut(Location::SameAsFirstInput());
2040 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002041}
2042
2043void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002044 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002045 codegen_->AddSlowPath(slow_path);
2046
2047 LocationSummary* locations = instruction->GetLocations();
2048 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002049
2050 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002051 __ cmpl(obj.As<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002052 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002053 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002054 } else {
2055 DCHECK(obj.IsConstant()) << obj;
2056 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2057 __ jmp(slow_path->GetEntryLabel());
2058 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002059 }
2060 __ j(kEqual, slow_path->GetEntryLabel());
2061}
2062
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002063void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002064 LocationSummary* locations =
2065 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002066 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002067 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002068 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2069 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002070}
2071
2072void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
2073 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002074 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002075 Location index = locations->InAt(1);
2076
2077 switch (instruction->GetType()) {
2078 case Primitive::kPrimBoolean: {
2079 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002080 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002081 if (index.IsConstant()) {
2082 __ movzxb(out, Address(obj,
2083 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2084 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002085 __ movzxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002086 }
2087 break;
2088 }
2089
2090 case Primitive::kPrimByte: {
2091 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002092 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002093 if (index.IsConstant()) {
2094 __ movsxb(out, Address(obj,
2095 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2096 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002097 __ movsxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002098 }
2099 break;
2100 }
2101
2102 case Primitive::kPrimShort: {
2103 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002104 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002105 if (index.IsConstant()) {
2106 __ movsxw(out, Address(obj,
2107 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2108 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002109 __ movsxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002110 }
2111 break;
2112 }
2113
2114 case Primitive::kPrimChar: {
2115 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002116 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002117 if (index.IsConstant()) {
2118 __ movzxw(out, Address(obj,
2119 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2120 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002121 __ movzxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002122 }
2123 break;
2124 }
2125
2126 case Primitive::kPrimInt:
2127 case Primitive::kPrimNot: {
2128 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
2129 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002130 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002131 if (index.IsConstant()) {
2132 __ movl(out, Address(obj,
2133 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2134 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002135 __ movl(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002136 }
2137 break;
2138 }
2139
2140 case Primitive::kPrimLong: {
2141 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002142 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002143 if (index.IsConstant()) {
2144 __ movq(out, Address(obj,
2145 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2146 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002147 __ movq(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002148 }
2149 break;
2150 }
2151
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002152 case Primitive::kPrimFloat: {
2153 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2154 XmmRegister out = locations->Out().As<XmmRegister>();
2155 if (index.IsConstant()) {
2156 __ movss(out, Address(obj,
2157 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2158 } else {
2159 __ movss(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
2160 }
2161 break;
2162 }
2163
2164 case Primitive::kPrimDouble: {
2165 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2166 XmmRegister out = locations->Out().As<XmmRegister>();
2167 if (index.IsConstant()) {
2168 __ movsd(out, Address(obj,
2169 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2170 } else {
2171 __ movsd(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
2172 }
2173 break;
2174 }
2175
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002176 case Primitive::kPrimVoid:
2177 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002178 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002179 }
2180}
2181
2182void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002183 Primitive::Type value_type = instruction->GetComponentType();
2184 bool is_object = value_type == Primitive::kPrimNot;
2185 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2186 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
2187 if (is_object) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002188 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002189 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2190 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2191 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002192 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002193 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002194 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002195 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2196 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002197 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002198 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002199 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
2200 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002201 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002202 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002203 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002204 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002205}
2206
2207void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
2208 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002209 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002210 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002211 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002212 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002213
2214 switch (value_type) {
2215 case Primitive::kPrimBoolean:
2216 case Primitive::kPrimByte: {
2217 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002218 if (index.IsConstant()) {
2219 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002220 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002221 __ movb(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002222 } else {
2223 __ movb(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2224 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002225 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002226 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002227 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
2228 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002229 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002230 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002231 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2232 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002233 }
2234 break;
2235 }
2236
2237 case Primitive::kPrimShort:
2238 case Primitive::kPrimChar: {
2239 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002240 if (index.IsConstant()) {
2241 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002242 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002243 __ movw(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002244 } else {
2245 __ movw(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2246 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002247 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002248 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002249 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
2250 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002251 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002252 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002253 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2254 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002255 }
2256 break;
2257 }
2258
2259 case Primitive::kPrimInt: {
2260 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002261 if (index.IsConstant()) {
2262 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002263 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002264 __ movl(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002265 } else {
2266 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2267 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002268 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002269 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002270 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2271 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002272 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002273 DCHECK(value.IsConstant()) << value;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002274 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002275 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2276 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002277 }
2278 break;
2279 }
2280
2281 case Primitive::kPrimNot: {
2282 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject), true));
2283 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002284 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002285 break;
2286 }
2287
2288 case Primitive::kPrimLong: {
2289 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002290 if (index.IsConstant()) {
2291 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002292 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002293 __ movq(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002294 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002295 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002296 __ movq(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
2297 value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002298 }
2299 break;
2300 }
2301
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002302 case Primitive::kPrimFloat: {
2303 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2304 if (index.IsConstant()) {
2305 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2306 DCHECK(value.IsFpuRegister());
2307 __ movss(Address(obj, offset), value.As<XmmRegister>());
2308 } else {
2309 DCHECK(value.IsFpuRegister());
2310 __ movss(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2311 value.As<XmmRegister>());
2312 }
2313 break;
2314 }
2315
2316 case Primitive::kPrimDouble: {
2317 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2318 if (index.IsConstant()) {
2319 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
2320 DCHECK(value.IsFpuRegister());
2321 __ movsd(Address(obj, offset), value.As<XmmRegister>());
2322 } else {
2323 DCHECK(value.IsFpuRegister());
2324 __ movsd(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
2325 value.As<XmmRegister>());
2326 }
2327 break;
2328 }
2329
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002330 case Primitive::kPrimVoid:
2331 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002332 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002333 }
2334}
2335
2336void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002337 LocationSummary* locations =
2338 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002339 locations->SetInAt(0, Location::RequiresRegister());
2340 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002341}
2342
2343void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
2344 LocationSummary* locations = instruction->GetLocations();
2345 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002346 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2347 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002348 __ movl(out, Address(obj, offset));
2349}
2350
2351void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002352 LocationSummary* locations =
2353 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002354 locations->SetInAt(0, Location::RequiresRegister());
2355 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002356 if (instruction->HasUses()) {
2357 locations->SetOut(Location::SameAsFirstInput());
2358 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002359}
2360
2361void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
2362 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002363 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002364 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002365 codegen_->AddSlowPath(slow_path);
2366
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002367 CpuRegister index = locations->InAt(0).As<CpuRegister>();
2368 CpuRegister length = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002369
2370 __ cmpl(index, length);
2371 __ j(kAboveEqual, slow_path->GetEntryLabel());
2372}
2373
2374void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
2375 CpuRegister card,
2376 CpuRegister object,
2377 CpuRegister value) {
2378 Label is_null;
2379 __ testl(value, value);
2380 __ j(kEqual, &is_null);
2381 __ gs()->movq(card, Address::Absolute(
2382 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
2383 __ movq(temp, object);
2384 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
2385 __ movb(Address(temp, card, TIMES_1, 0), card);
2386 __ Bind(&is_null);
2387}
2388
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002389void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
2390 temp->SetLocations(nullptr);
2391}
2392
2393void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
2394 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002395 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002396}
2397
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002398void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002399 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002400 LOG(FATAL) << "Unimplemented";
2401}
2402
2403void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002404 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2405}
2406
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002407void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
2408 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2409}
2410
2411void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002412 HBasicBlock* block = instruction->GetBlock();
2413 if (block->GetLoopInformation() != nullptr) {
2414 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2415 // The back edge will generate the suspend check.
2416 return;
2417 }
2418 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2419 // The goto will generate the suspend check.
2420 return;
2421 }
2422 GenerateSuspendCheck(instruction, nullptr);
2423}
2424
2425void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
2426 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002427 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002428 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002429 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002430 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002431 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002432 if (successor == nullptr) {
2433 __ j(kNotEqual, slow_path->GetEntryLabel());
2434 __ Bind(slow_path->GetReturnLabel());
2435 } else {
2436 __ j(kEqual, codegen_->GetLabelOf(successor));
2437 __ jmp(slow_path->GetEntryLabel());
2438 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002439}
2440
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002441X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
2442 return codegen_->GetAssembler();
2443}
2444
2445void ParallelMoveResolverX86_64::EmitMove(size_t index) {
2446 MoveOperands* move = moves_.Get(index);
2447 Location source = move->GetSource();
2448 Location destination = move->GetDestination();
2449
2450 if (source.IsRegister()) {
2451 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002452 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002453 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002454 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002455 source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002456 } else {
2457 DCHECK(destination.IsDoubleStackSlot());
2458 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002459 source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002460 }
2461 } else if (source.IsStackSlot()) {
2462 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002463 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002464 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002465 } else if (destination.IsFpuRegister()) {
2466 __ movss(destination.As<XmmRegister>(),
2467 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002468 } else {
2469 DCHECK(destination.IsStackSlot());
2470 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2471 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2472 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002473 } else if (source.IsDoubleStackSlot()) {
2474 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002475 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002476 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002477 } else if (destination.IsFpuRegister()) {
2478 __ movsd(destination.As<XmmRegister>(), Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002479 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01002480 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002481 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2482 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2483 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002484 } else if (source.IsConstant()) {
2485 HConstant* constant = source.GetConstant();
2486 if (constant->IsIntConstant()) {
2487 Immediate imm(constant->AsIntConstant()->GetValue());
2488 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002489 __ movl(destination.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002490 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002491 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002492 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2493 }
2494 } else if (constant->IsLongConstant()) {
2495 int64_t value = constant->AsLongConstant()->GetValue();
2496 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002497 __ movq(destination.As<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002498 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002499 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002500 __ movq(CpuRegister(TMP), Immediate(value));
2501 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2502 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002503 } else if (constant->IsFloatConstant()) {
2504 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
2505 if (destination.IsFpuRegister()) {
2506 __ movl(CpuRegister(TMP), imm);
2507 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2508 } else {
2509 DCHECK(destination.IsStackSlot()) << destination;
2510 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2511 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002512 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002513 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
2514 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
2515 if (destination.IsFpuRegister()) {
2516 __ movq(CpuRegister(TMP), imm);
2517 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2518 } else {
2519 DCHECK(destination.IsDoubleStackSlot()) << destination;
2520 __ movq(CpuRegister(TMP), imm);
2521 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2522 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002523 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002524 } else if (source.IsFpuRegister()) {
2525 if (destination.IsFpuRegister()) {
2526 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
2527 } else if (destination.IsStackSlot()) {
2528 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
2529 source.As<XmmRegister>());
2530 } else {
2531 DCHECK(destination.IsDoubleStackSlot());
2532 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
2533 source.As<XmmRegister>());
2534 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002535 }
2536}
2537
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002538void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002539 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002540 __ movl(Address(CpuRegister(RSP), mem), reg);
2541 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002542}
2543
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002544void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002545 ScratchRegisterScope ensure_scratch(
2546 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2547
2548 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2549 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2550 __ movl(CpuRegister(ensure_scratch.GetRegister()),
2551 Address(CpuRegister(RSP), mem2 + stack_offset));
2552 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2553 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
2554 CpuRegister(ensure_scratch.GetRegister()));
2555}
2556
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002557void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
2558 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2559 __ movq(Address(CpuRegister(RSP), mem), reg);
2560 __ movq(reg, CpuRegister(TMP));
2561}
2562
2563void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
2564 ScratchRegisterScope ensure_scratch(
2565 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2566
2567 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2568 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2569 __ movq(CpuRegister(ensure_scratch.GetRegister()),
2570 Address(CpuRegister(RSP), mem2 + stack_offset));
2571 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2572 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
2573 CpuRegister(ensure_scratch.GetRegister()));
2574}
2575
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002576void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
2577 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2578 __ movss(Address(CpuRegister(RSP), mem), reg);
2579 __ movd(reg, CpuRegister(TMP));
2580}
2581
2582void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
2583 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2584 __ movsd(Address(CpuRegister(RSP), mem), reg);
2585 __ movd(reg, CpuRegister(TMP));
2586}
2587
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002588void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
2589 MoveOperands* move = moves_.Get(index);
2590 Location source = move->GetSource();
2591 Location destination = move->GetDestination();
2592
2593 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002594 __ xchgq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002595 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002596 Exchange32(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002597 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002598 Exchange32(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002599 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002600 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
2601 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002602 Exchange64(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002603 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002604 Exchange64(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002605 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
2606 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002607 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
2608 __ movd(CpuRegister(TMP), source.As<XmmRegister>());
2609 __ movaps(source.As<XmmRegister>(), destination.As<XmmRegister>());
2610 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2611 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
2612 Exchange32(source.As<XmmRegister>(), destination.GetStackIndex());
2613 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
2614 Exchange32(destination.As<XmmRegister>(), source.GetStackIndex());
2615 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
2616 Exchange64(source.As<XmmRegister>(), destination.GetStackIndex());
2617 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
2618 Exchange64(destination.As<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002619 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002620 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002621 }
2622}
2623
2624
2625void ParallelMoveResolverX86_64::SpillScratch(int reg) {
2626 __ pushq(CpuRegister(reg));
2627}
2628
2629
2630void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
2631 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002632}
2633
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002634void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
2635 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
2636 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2637 Immediate(mirror::Class::kStatusInitialized));
2638 __ j(kLess, slow_path->GetEntryLabel());
2639 __ Bind(slow_path->GetExitLabel());
2640 // No need for memory fence, thanks to the X86_64 memory model.
2641}
2642
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002643void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002644 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2645 ? LocationSummary::kCallOnSlowPath
2646 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002647 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002648 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002649 locations->SetOut(Location::RequiresRegister());
2650}
2651
2652void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
2653 CpuRegister out = cls->GetLocations()->Out().As<CpuRegister>();
2654 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002655 DCHECK(!cls->CanCallRuntime());
2656 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002657 codegen_->LoadCurrentMethod(out);
2658 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2659 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002660 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002661 codegen_->LoadCurrentMethod(out);
2662 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2663 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002664 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
2665 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2666 codegen_->AddSlowPath(slow_path);
2667 __ testl(out, out);
2668 __ j(kEqual, slow_path->GetEntryLabel());
2669 if (cls->MustGenerateClinitCheck()) {
2670 GenerateClassInitializationCheck(slow_path, out);
2671 } else {
2672 __ Bind(slow_path->GetExitLabel());
2673 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002674 }
2675}
2676
2677void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
2678 LocationSummary* locations =
2679 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2680 locations->SetInAt(0, Location::RequiresRegister());
2681 if (check->HasUses()) {
2682 locations->SetOut(Location::SameAsFirstInput());
2683 }
2684}
2685
2686void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002687 // We assume the class to not be null.
2688 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
2689 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002690 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002691 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002692}
2693
2694void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2695 LocationSummary* locations =
2696 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2697 locations->SetInAt(0, Location::RequiresRegister());
2698 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2699}
2700
2701void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2702 LocationSummary* locations = instruction->GetLocations();
2703 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002704 size_t offset = instruction->GetFieldOffset().SizeValue();
2705
2706 switch (instruction->GetType()) {
2707 case Primitive::kPrimBoolean: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002708 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002709 __ movzxb(out, Address(cls, offset));
2710 break;
2711 }
2712
2713 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002714 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002715 __ movsxb(out, Address(cls, offset));
2716 break;
2717 }
2718
2719 case Primitive::kPrimShort: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002720 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002721 __ movsxw(out, Address(cls, offset));
2722 break;
2723 }
2724
2725 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002726 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002727 __ movzxw(out, Address(cls, offset));
2728 break;
2729 }
2730
2731 case Primitive::kPrimInt:
2732 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002733 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002734 __ movl(out, Address(cls, offset));
2735 break;
2736 }
2737
2738 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002739 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002740 __ movq(out, Address(cls, offset));
2741 break;
2742 }
2743
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002744 case Primitive::kPrimFloat: {
2745 XmmRegister out = locations->Out().As<XmmRegister>();
2746 __ movss(out, Address(cls, offset));
2747 break;
2748 }
2749
2750 case Primitive::kPrimDouble: {
2751 XmmRegister out = locations->Out().As<XmmRegister>();
2752 __ movsd(out, Address(cls, offset));
2753 break;
2754 }
2755
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002756 case Primitive::kPrimVoid:
2757 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2758 UNREACHABLE();
2759 }
2760}
2761
2762void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2763 LocationSummary* locations =
2764 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2765 Primitive::Type field_type = instruction->GetFieldType();
2766 bool is_object_type = field_type == Primitive::kPrimNot;
2767 locations->SetInAt(0, Location::RequiresRegister());
2768 locations->SetInAt(1, Location::RequiresRegister());
2769 if (is_object_type) {
2770 // Temporary registers for the write barrier.
2771 locations->AddTemp(Location::RequiresRegister());
2772 locations->AddTemp(Location::RequiresRegister());
2773 }
2774}
2775
2776void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2777 LocationSummary* locations = instruction->GetLocations();
2778 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002779 size_t offset = instruction->GetFieldOffset().SizeValue();
2780 Primitive::Type field_type = instruction->GetFieldType();
2781
2782 switch (field_type) {
2783 case Primitive::kPrimBoolean:
2784 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002785 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002786 __ movb(Address(cls, offset), value);
2787 break;
2788 }
2789
2790 case Primitive::kPrimShort:
2791 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002792 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002793 __ movw(Address(cls, offset), value);
2794 break;
2795 }
2796
2797 case Primitive::kPrimInt:
2798 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002799 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002800 __ movl(Address(cls, offset), value);
2801 if (field_type == Primitive::kPrimNot) {
2802 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
2803 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
2804 codegen_->MarkGCCard(temp, card, cls, value);
2805 }
2806 break;
2807 }
2808
2809 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002810 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002811 __ movq(Address(cls, offset), value);
2812 break;
2813 }
2814
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002815 case Primitive::kPrimFloat: {
2816 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2817 __ movss(Address(cls, offset), value);
2818 break;
2819 }
2820
2821 case Primitive::kPrimDouble: {
2822 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2823 __ movsd(Address(cls, offset), value);
2824 break;
2825 }
2826
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002827 case Primitive::kPrimVoid:
2828 LOG(FATAL) << "Unreachable type " << field_type;
2829 UNREACHABLE();
2830 }
2831}
2832
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002833void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
2834 LocationSummary* locations =
2835 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2836 locations->SetOut(Location::RequiresRegister());
2837}
2838
2839void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
2840 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
2841 codegen_->AddSlowPath(slow_path);
2842
2843 CpuRegister out = load->GetLocations()->Out().As<CpuRegister>();
2844 codegen_->LoadCurrentMethod(CpuRegister(out));
2845 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
2846 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2847 __ testl(out, out);
2848 __ j(kEqual, slow_path->GetEntryLabel());
2849 __ Bind(slow_path->GetExitLabel());
2850}
2851
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002852void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
2853 LocationSummary* locations =
2854 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2855 locations->SetOut(Location::RequiresRegister());
2856}
2857
2858void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
2859 Address address = Address::Absolute(
2860 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
2861 __ gs()->movl(load->GetLocations()->Out().As<CpuRegister>(), address);
2862 __ gs()->movl(address, Immediate(0));
2863}
2864
2865void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
2866 LocationSummary* locations =
2867 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2868 InvokeRuntimeCallingConvention calling_convention;
2869 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2870}
2871
2872void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
2873 __ gs()->call(
2874 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
2875 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2876}
2877
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002878void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002879 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
2880 ? LocationSummary::kNoCall
2881 : LocationSummary::kCallOnSlowPath;
2882 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2883 locations->SetInAt(0, Location::RequiresRegister());
2884 locations->SetInAt(1, Location::Any());
2885 locations->SetOut(Location::RequiresRegister());
2886}
2887
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002888void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002889 LocationSummary* locations = instruction->GetLocations();
2890 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2891 Location cls = locations->InAt(1);
2892 CpuRegister out = locations->Out().As<CpuRegister>();
2893 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2894 Label done, zero;
2895 SlowPathCodeX86_64* slow_path = nullptr;
2896
2897 // Return 0 if `obj` is null.
2898 // TODO: avoid this check if we know obj is not null.
2899 __ testl(obj, obj);
2900 __ j(kEqual, &zero);
2901 // Compare the class of `obj` with `cls`.
2902 __ movl(out, Address(obj, class_offset));
2903 if (cls.IsRegister()) {
2904 __ cmpl(out, cls.As<CpuRegister>());
2905 } else {
2906 DCHECK(cls.IsStackSlot()) << cls;
2907 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
2908 }
2909 if (instruction->IsClassFinal()) {
2910 // Classes must be equal for the instanceof to succeed.
2911 __ j(kNotEqual, &zero);
2912 __ movl(out, Immediate(1));
2913 __ jmp(&done);
2914 } else {
2915 // If the classes are not equal, we go into a slow path.
2916 DCHECK(locations->OnlyCallsOnSlowPath());
2917 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002918 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002919 codegen_->AddSlowPath(slow_path);
2920 __ j(kNotEqual, slow_path->GetEntryLabel());
2921 __ movl(out, Immediate(1));
2922 __ jmp(&done);
2923 }
2924 __ Bind(&zero);
2925 __ movl(out, Immediate(0));
2926 if (slow_path != nullptr) {
2927 __ Bind(slow_path->GetExitLabel());
2928 }
2929 __ Bind(&done);
2930}
2931
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002932void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
2933 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2934 instruction, LocationSummary::kCallOnSlowPath);
2935 locations->SetInAt(0, Location::RequiresRegister());
2936 locations->SetInAt(1, Location::Any());
2937 locations->AddTemp(Location::RequiresRegister());
2938}
2939
2940void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
2941 LocationSummary* locations = instruction->GetLocations();
2942 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2943 Location cls = locations->InAt(1);
2944 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
2945 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2946 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
2947 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
2948 codegen_->AddSlowPath(slow_path);
2949
2950 // TODO: avoid this check if we know obj is not null.
2951 __ testl(obj, obj);
2952 __ j(kEqual, slow_path->GetExitLabel());
2953 // Compare the class of `obj` with `cls`.
2954 __ movl(temp, Address(obj, class_offset));
2955 if (cls.IsRegister()) {
2956 __ cmpl(temp, cls.As<CpuRegister>());
2957 } else {
2958 DCHECK(cls.IsStackSlot()) << cls;
2959 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
2960 }
2961 // Classes must be equal for the checkcast to succeed.
2962 __ j(kNotEqual, slow_path->GetEntryLabel());
2963 __ Bind(slow_path->GetExitLabel());
2964}
2965
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002966void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
2967 LocationSummary* locations =
2968 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2969 InvokeRuntimeCallingConvention calling_convention;
2970 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2971}
2972
2973void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
2974 __ gs()->call(Address::Absolute(instruction->IsEnter()
2975 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
2976 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
2977 true));
2978 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2979}
2980
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002981void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
2982void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
2983void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
2984
2985void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
2986 LocationSummary* locations =
2987 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2988 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
2989 || instruction->GetResultType() == Primitive::kPrimLong);
2990 locations->SetInAt(0, Location::RequiresRegister());
2991 if (instruction->GetType() == Primitive::kPrimInt) {
2992 locations->SetInAt(1, Location::Any());
2993 } else {
2994 // Request a register to avoid loading a 64bits constant.
2995 locations->SetInAt(1, Location::RequiresRegister());
2996 }
2997 locations->SetOut(Location::SameAsFirstInput());
2998}
2999
3000void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
3001 HandleBitwiseOperation(instruction);
3002}
3003
3004void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
3005 HandleBitwiseOperation(instruction);
3006}
3007
3008void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
3009 HandleBitwiseOperation(instruction);
3010}
3011
3012void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3013 LocationSummary* locations = instruction->GetLocations();
3014 Location first = locations->InAt(0);
3015 Location second = locations->InAt(1);
3016 DCHECK(first.Equals(locations->Out()));
3017
3018 if (instruction->GetResultType() == Primitive::kPrimInt) {
3019 if (second.IsRegister()) {
3020 if (instruction->IsAnd()) {
3021 __ andl(first.As<CpuRegister>(), second.As<CpuRegister>());
3022 } else if (instruction->IsOr()) {
3023 __ orl(first.As<CpuRegister>(), second.As<CpuRegister>());
3024 } else {
3025 DCHECK(instruction->IsXor());
3026 __ xorl(first.As<CpuRegister>(), second.As<CpuRegister>());
3027 }
3028 } else if (second.IsConstant()) {
3029 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
3030 if (instruction->IsAnd()) {
3031 __ andl(first.As<CpuRegister>(), imm);
3032 } else if (instruction->IsOr()) {
3033 __ orl(first.As<CpuRegister>(), imm);
3034 } else {
3035 DCHECK(instruction->IsXor());
3036 __ xorl(first.As<CpuRegister>(), imm);
3037 }
3038 } else {
3039 Address address(CpuRegister(RSP), second.GetStackIndex());
3040 if (instruction->IsAnd()) {
3041 __ andl(first.As<CpuRegister>(), address);
3042 } else if (instruction->IsOr()) {
3043 __ orl(first.As<CpuRegister>(), address);
3044 } else {
3045 DCHECK(instruction->IsXor());
3046 __ xorl(first.As<CpuRegister>(), address);
3047 }
3048 }
3049 } else {
3050 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3051 if (instruction->IsAnd()) {
3052 __ andq(first.As<CpuRegister>(), second.As<CpuRegister>());
3053 } else if (instruction->IsOr()) {
3054 __ orq(first.As<CpuRegister>(), second.As<CpuRegister>());
3055 } else {
3056 DCHECK(instruction->IsXor());
3057 __ xorq(first.As<CpuRegister>(), second.As<CpuRegister>());
3058 }
3059 }
3060}
3061
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003062} // namespace x86_64
3063} // namespace art