blob: 7545e4dfd7181b7d262a5bd7a13940127e04932f [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:
111 explicit DivMinusOneSlowPathX86_64(Register reg) : reg_(reg) {}
112
113 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
114 __ Bind(GetEntryLabel());
115 __ negl(CpuRegister(reg_));
116 __ jmp(GetExitLabel());
117 }
118
119 private:
120 Register reg_;
121 DISALLOW_COPY_AND_ASSIGN(DivMinusOneSlowPathX86_64);
122};
123
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100124class StackOverflowCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100125 public:
126 StackOverflowCheckSlowPathX86_64() {}
127
128 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
129 __ Bind(GetEntryLabel());
130 __ addq(CpuRegister(RSP),
131 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
132 __ gs()->jmp(
133 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowStackOverflow), true));
134 }
135
136 private:
137 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86_64);
138};
139
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100140class SuspendCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000141 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100142 explicit SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
143 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000144
145 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100146 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000147 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100148 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000149 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pTestSuspend), true));
150 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100151 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100152 if (successor_ == nullptr) {
153 __ jmp(GetReturnLabel());
154 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100155 __ jmp(x64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100156 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000157 }
158
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100159 Label* GetReturnLabel() {
160 DCHECK(successor_ == nullptr);
161 return &return_label_;
162 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000163
164 private:
165 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100166 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000167 Label return_label_;
168
169 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
170};
171
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100172class BoundsCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100173 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100174 BoundsCheckSlowPathX86_64(HBoundsCheck* instruction,
175 Location index_location,
176 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100177 : instruction_(instruction),
178 index_location_(index_location),
179 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100180
181 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100182 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000183 // We're moving two locations to locations that could overlap, so we need a parallel
184 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100185 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000186 codegen->EmitParallelMoves(
187 index_location_,
188 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
189 length_location_,
190 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100191 __ gs()->call(Address::Absolute(
192 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowArrayBounds), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100193 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100194 }
195
196 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100197 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100198 const Location index_location_;
199 const Location length_location_;
200
201 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
202};
203
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000204class LoadClassSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100205 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000206 LoadClassSlowPathX86_64(HLoadClass* cls,
207 HInstruction* at,
208 uint32_t dex_pc,
209 bool do_clinit)
210 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
211 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
212 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100213
214 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000215 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100216 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
217 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100218
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000219 codegen->SaveLiveRegisters(locations);
220
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100221 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000222 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100223 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000224 __ gs()->call(Address::Absolute((do_clinit_
225 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeStaticStorage)
226 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeType)) , true));
227 codegen->RecordPcInfo(at_, dex_pc_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100228
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000229 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000230 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000231 if (out.IsValid()) {
232 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
233 x64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000234 }
235
236 codegen->RestoreLiveRegisters(locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100237 __ jmp(GetExitLabel());
238 }
239
240 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000241 // The class this slow path will load.
242 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100243
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000244 // The instruction where this slow path is happening.
245 // (Might be the load class or an initialization check).
246 HInstruction* const at_;
247
248 // The dex PC of `at_`.
249 const uint32_t dex_pc_;
250
251 // Whether to initialize the class.
252 const bool do_clinit_;
253
254 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100255};
256
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000257class LoadStringSlowPathX86_64 : public SlowPathCodeX86_64 {
258 public:
259 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
260
261 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
262 LocationSummary* locations = instruction_->GetLocations();
263 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
264
265 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
266 __ Bind(GetEntryLabel());
267 codegen->SaveLiveRegisters(locations);
268
269 InvokeRuntimeCallingConvention calling_convention;
270 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(0)));
271 __ movl(CpuRegister(calling_convention.GetRegisterAt(1)),
272 Immediate(instruction_->GetStringIndex()));
273 __ gs()->call(Address::Absolute(
274 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pResolveString), true));
275 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
276 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
277 codegen->RestoreLiveRegisters(locations);
278 __ jmp(GetExitLabel());
279 }
280
281 private:
282 HLoadString* const instruction_;
283
284 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
285};
286
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000287class TypeCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
288 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000289 TypeCheckSlowPathX86_64(HInstruction* instruction,
290 Location class_to_check,
291 Location object_class,
292 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000293 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000294 class_to_check_(class_to_check),
295 object_class_(object_class),
296 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000297
298 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
299 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000300 DCHECK(instruction_->IsCheckCast()
301 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000302
303 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
304 __ Bind(GetEntryLabel());
305 codegen->SaveLiveRegisters(locations);
306
307 // We're moving two locations to locations that could overlap, so we need a parallel
308 // move resolver.
309 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000310 codegen->EmitParallelMoves(
311 class_to_check_,
312 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
313 object_class_,
314 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000315
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000316 if (instruction_->IsInstanceOf()) {
317 __ gs()->call(
318 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInstanceofNonTrivial), true));
319 } else {
320 DCHECK(instruction_->IsCheckCast());
321 __ gs()->call(
322 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pCheckCast), true));
323 }
324 codegen->RecordPcInfo(instruction_, dex_pc_);
325
326 if (instruction_->IsInstanceOf()) {
327 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
328 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000329
330 codegen->RestoreLiveRegisters(locations);
331 __ jmp(GetExitLabel());
332 }
333
334 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000335 HInstruction* const instruction_;
336 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000337 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000338 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000339
340 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
341};
342
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100343#undef __
344#define __ reinterpret_cast<X86_64Assembler*>(GetAssembler())->
345
Dave Allison20dfc792014-06-16 20:44:29 -0700346inline Condition X86_64Condition(IfCondition cond) {
347 switch (cond) {
348 case kCondEQ: return kEqual;
349 case kCondNE: return kNotEqual;
350 case kCondLT: return kLess;
351 case kCondLE: return kLessEqual;
352 case kCondGT: return kGreater;
353 case kCondGE: return kGreaterEqual;
354 default:
355 LOG(FATAL) << "Unknown if condition";
356 }
357 return kEqual;
358}
359
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100360void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
361 stream << X86_64ManagedRegister::FromCpuRegister(Register(reg));
362}
363
364void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
365 stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg));
366}
367
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100368size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
369 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
370 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100371}
372
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100373size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
374 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
375 return kX86_64WordSize;
376}
377
378size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
379 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
380 return kX86_64WordSize;
381}
382
383size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
384 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
385 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100386}
387
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100388CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100389 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfFloatRegisters, 0),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100390 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100391 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000392 instruction_visitor_(graph, this),
393 move_resolver_(graph->GetArena(), this) {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100394
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100395size_t CodeGeneratorX86_64::FrameEntrySpillSize() const {
396 return kNumberOfPushedRegistersAtEntry * kX86_64WordSize;
397}
398
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100399InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
400 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100401 : HGraphVisitor(graph),
402 assembler_(codegen->GetAssembler()),
403 codegen_(codegen) {}
404
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100405Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100406 switch (type) {
407 case Primitive::kPrimLong:
408 case Primitive::kPrimByte:
409 case Primitive::kPrimBoolean:
410 case Primitive::kPrimChar:
411 case Primitive::kPrimShort:
412 case Primitive::kPrimInt:
413 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100414 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100415 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100416 }
417
418 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100419 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100420 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100421 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100422 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100423
424 case Primitive::kPrimVoid:
425 LOG(FATAL) << "Unreachable type " << type;
426 }
427
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100428 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100429}
430
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100431void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100432 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100433 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100434
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000435 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100436 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000437
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100438 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100439 blocked_core_registers_[RBX] = true;
440 blocked_core_registers_[RBP] = true;
441 blocked_core_registers_[R12] = true;
442 blocked_core_registers_[R13] = true;
443 blocked_core_registers_[R14] = true;
444 blocked_core_registers_[R15] = true;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100445
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100446 blocked_fpu_registers_[XMM12] = true;
447 blocked_fpu_registers_[XMM13] = true;
448 blocked_fpu_registers_[XMM14] = true;
449 blocked_fpu_registers_[XMM15] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100450}
451
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100452void CodeGeneratorX86_64::GenerateFrameEntry() {
453 // Create a fake register to mimic Quick.
454 static const int kFakeReturnRegister = 16;
455 core_spill_mask_ |= (1 << kFakeReturnRegister);
456
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100457 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700458 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100459
460 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
461 __ testq(CpuRegister(RAX), Address(
462 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100463 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100464 }
465
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100466 // The return PC has already been pushed on the stack.
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100467 __ subq(CpuRegister(RSP),
468 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
469
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100470 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100471 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86_64();
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100472 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100473
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100474 __ gs()->cmpq(CpuRegister(RSP),
475 Address::Absolute(Thread::StackEndOffset<kX86_64WordSize>(), true));
476 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100477 }
478
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100479 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
480}
481
482void CodeGeneratorX86_64::GenerateFrameExit() {
483 __ addq(CpuRegister(RSP),
484 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
485}
486
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100487void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
488 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100489}
490
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100491void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100492 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
493}
494
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100495Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
496 switch (load->GetType()) {
497 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100498 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100499 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
500 break;
501
502 case Primitive::kPrimInt:
503 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100504 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100505 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100506
507 case Primitive::kPrimBoolean:
508 case Primitive::kPrimByte:
509 case Primitive::kPrimChar:
510 case Primitive::kPrimShort:
511 case Primitive::kPrimVoid:
512 LOG(FATAL) << "Unexpected type " << load->GetType();
513 }
514
515 LOG(FATAL) << "Unreachable";
516 return Location();
517}
518
519void CodeGeneratorX86_64::Move(Location destination, Location source) {
520 if (source.Equals(destination)) {
521 return;
522 }
523 if (destination.IsRegister()) {
524 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100525 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100526 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100527 __ movd(destination.As<CpuRegister>(), source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100528 } else if (source.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100529 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100530 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100531 } else {
532 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100533 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100534 Address(CpuRegister(RSP), source.GetStackIndex()));
535 }
536 } else if (destination.IsFpuRegister()) {
537 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100538 __ movd(destination.As<XmmRegister>(), source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100539 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100540 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100541 } else if (source.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100542 __ movss(destination.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100543 Address(CpuRegister(RSP), source.GetStackIndex()));
544 } else {
545 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100546 __ movsd(destination.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100547 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100548 }
549 } else if (destination.IsStackSlot()) {
550 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100551 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100552 source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100553 } else if (source.IsFpuRegister()) {
554 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100555 source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100556 } else {
557 DCHECK(source.IsStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000558 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
559 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100560 }
561 } else {
562 DCHECK(destination.IsDoubleStackSlot());
563 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100564 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100565 source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100566 } else if (source.IsFpuRegister()) {
567 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100568 source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100569 } else {
570 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000571 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
572 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100573 }
574 }
575}
576
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100577void CodeGeneratorX86_64::Move(HInstruction* instruction,
578 Location location,
579 HInstruction* move_for) {
Roland Levillain476df552014-10-09 17:51:36 +0100580 if (instruction->IsIntConstant()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100581 Immediate imm(instruction->AsIntConstant()->GetValue());
582 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100583 __ movl(location.As<CpuRegister>(), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100584 } else if (location.IsStackSlot()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100585 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100586 } else {
587 DCHECK(location.IsConstant());
588 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100589 }
Roland Levillain476df552014-10-09 17:51:36 +0100590 } else if (instruction->IsLongConstant()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100591 int64_t value = instruction->AsLongConstant()->GetValue();
592 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100593 __ movq(location.As<CpuRegister>(), Immediate(value));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100594 } else if (location.IsDoubleStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000595 __ movq(CpuRegister(TMP), Immediate(value));
596 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100597 } else {
598 DCHECK(location.IsConstant());
599 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100600 }
Roland Levillain476df552014-10-09 17:51:36 +0100601 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100602 switch (instruction->GetType()) {
603 case Primitive::kPrimBoolean:
604 case Primitive::kPrimByte:
605 case Primitive::kPrimChar:
606 case Primitive::kPrimShort:
607 case Primitive::kPrimInt:
608 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100609 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100610 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
611 break;
612
613 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100614 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100615 Move(location, Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
616 break;
617
618 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100619 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100620 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000621 } else if (instruction->IsTemporary()) {
622 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
623 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100624 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100625 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100626 switch (instruction->GetType()) {
627 case Primitive::kPrimBoolean:
628 case Primitive::kPrimByte:
629 case Primitive::kPrimChar:
630 case Primitive::kPrimShort:
631 case Primitive::kPrimInt:
632 case Primitive::kPrimNot:
633 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100634 case Primitive::kPrimFloat:
635 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100636 Move(location, instruction->GetLocations()->Out());
637 break;
638
639 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100640 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100641 }
642 }
643}
644
645void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
646 got->SetLocations(nullptr);
647}
648
649void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
650 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100651 DCHECK(!successor->IsExitBlock());
652
653 HBasicBlock* block = got->GetBlock();
654 HInstruction* previous = got->GetPrevious();
655
656 HLoopInformation* info = block->GetLoopInformation();
657 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
658 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
659 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
660 return;
661 }
662
663 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
664 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
665 }
666 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100667 __ jmp(codegen_->GetLabelOf(successor));
668 }
669}
670
671void LocationsBuilderX86_64::VisitExit(HExit* exit) {
672 exit->SetLocations(nullptr);
673}
674
675void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700676 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100677 if (kIsDebugBuild) {
678 __ Comment("Unreachable");
679 __ int3();
680 }
681}
682
683void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100684 LocationSummary* locations =
685 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100686 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100687 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100688 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100689 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100690}
691
692void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700693 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100694 if (cond->IsIntConstant()) {
695 // Constant condition, statically compared against 1.
696 int32_t cond_value = cond->AsIntConstant()->GetValue();
697 if (cond_value == 1) {
698 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
699 if_instr->IfTrueSuccessor())) {
700 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100701 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100702 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100703 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100704 DCHECK_EQ(cond_value, 0);
705 }
706 } else {
707 bool materialized =
708 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
709 // Moves do not affect the eflags register, so if the condition is
710 // evaluated just before the if, we don't need to evaluate it
711 // again.
712 bool eflags_set = cond->IsCondition()
713 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
714 if (materialized) {
715 if (!eflags_set) {
716 // Materialized condition, compare against 0.
717 Location lhs = if_instr->GetLocations()->InAt(0);
718 if (lhs.IsRegister()) {
719 __ cmpl(lhs.As<CpuRegister>(), Immediate(0));
720 } else {
721 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
722 Immediate(0));
723 }
724 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
725 } else {
726 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
727 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
728 }
729 } else {
730 Location lhs = cond->GetLocations()->InAt(0);
731 Location rhs = cond->GetLocations()->InAt(1);
732 if (rhs.IsRegister()) {
733 __ cmpl(lhs.As<CpuRegister>(), rhs.As<CpuRegister>());
734 } else if (rhs.IsConstant()) {
735 __ cmpl(lhs.As<CpuRegister>(),
736 Immediate(rhs.GetConstant()->AsIntConstant()->GetValue()));
737 } else {
738 __ cmpl(lhs.As<CpuRegister>(),
739 Address(CpuRegister(RSP), rhs.GetStackIndex()));
740 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100741 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
742 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700743 }
Dave Allison20dfc792014-06-16 20:44:29 -0700744 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100745 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
746 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700747 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100748 }
749}
750
751void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
752 local->SetLocations(nullptr);
753}
754
755void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
756 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
757}
758
759void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
760 local->SetLocations(nullptr);
761}
762
763void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
764 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700765 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100766}
767
768void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100769 LocationSummary* locations =
770 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100771 switch (store->InputAt(1)->GetType()) {
772 case Primitive::kPrimBoolean:
773 case Primitive::kPrimByte:
774 case Primitive::kPrimChar:
775 case Primitive::kPrimShort:
776 case Primitive::kPrimInt:
777 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100778 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100779 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
780 break;
781
782 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100783 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100784 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
785 break;
786
787 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100788 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100789 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100790}
791
792void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700793 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100794}
795
Dave Allison20dfc792014-06-16 20:44:29 -0700796void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100797 LocationSummary* locations =
798 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100799 locations->SetInAt(0, Location::RequiresRegister());
800 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100801 if (comp->NeedsMaterialization()) {
802 locations->SetOut(Location::RequiresRegister());
803 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100804}
805
Dave Allison20dfc792014-06-16 20:44:29 -0700806void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
807 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100808 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100809 CpuRegister reg = locations->Out().As<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100810 // Clear register: setcc only sets the low byte.
811 __ xorq(reg, reg);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100812 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100813 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100814 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100815 } else if (locations->InAt(1).IsConstant()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100816 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100817 Immediate(locations->InAt(1).GetConstant()->AsIntConstant()->GetValue()));
818 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100819 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100820 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
821 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100822 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700823 }
824}
825
826void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
827 VisitCondition(comp);
828}
829
830void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
831 VisitCondition(comp);
832}
833
834void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
835 VisitCondition(comp);
836}
837
838void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
839 VisitCondition(comp);
840}
841
842void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
843 VisitCondition(comp);
844}
845
846void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
847 VisitCondition(comp);
848}
849
850void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
851 VisitCondition(comp);
852}
853
854void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
855 VisitCondition(comp);
856}
857
858void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
859 VisitCondition(comp);
860}
861
862void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
863 VisitCondition(comp);
864}
865
866void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
867 VisitCondition(comp);
868}
869
870void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
871 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100872}
873
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100874void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100875 LocationSummary* locations =
876 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100877 locations->SetInAt(0, Location::RequiresRegister());
878 locations->SetInAt(1, Location::RequiresRegister());
879 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100880}
881
882void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
883 Label greater, done;
884 LocationSummary* locations = compare->GetLocations();
885 switch (compare->InputAt(0)->GetType()) {
886 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100887 __ cmpq(locations->InAt(0).As<CpuRegister>(),
888 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100889 break;
890 default:
891 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
892 }
893
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100894 CpuRegister output = locations->Out().As<CpuRegister>();
895 __ movl(output, Immediate(0));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100896 __ j(kEqual, &done);
897 __ j(kGreater, &greater);
898
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100899 __ movl(output, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100900 __ jmp(&done);
901
902 __ Bind(&greater);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100903 __ movl(output, Immediate(1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100904
905 __ Bind(&done);
906}
907
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100908void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100909 LocationSummary* locations =
910 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100911 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100912}
913
914void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100915 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700916 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100917}
918
919void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100920 LocationSummary* locations =
921 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100922 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100923}
924
925void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100926 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700927 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100928}
929
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100930void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
931 LocationSummary* locations =
932 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
933 locations->SetOut(Location::ConstantLocation(constant));
934}
935
936void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
937 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700938 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100939}
940
941void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
942 LocationSummary* locations =
943 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
944 locations->SetOut(Location::ConstantLocation(constant));
945}
946
947void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
948 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700949 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100950}
951
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100952void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
953 ret->SetLocations(nullptr);
954}
955
956void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700957 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100958 codegen_->GenerateFrameExit();
959 __ ret();
960}
961
962void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100963 LocationSummary* locations =
964 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100965 switch (ret->InputAt(0)->GetType()) {
966 case Primitive::kPrimBoolean:
967 case Primitive::kPrimByte:
968 case Primitive::kPrimChar:
969 case Primitive::kPrimShort:
970 case Primitive::kPrimInt:
971 case Primitive::kPrimNot:
972 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100973 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100974 break;
975
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100976 case Primitive::kPrimFloat:
977 case Primitive::kPrimDouble:
978 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100979 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100980 break;
981
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100982 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100983 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100984 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100985}
986
987void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
988 if (kIsDebugBuild) {
989 switch (ret->InputAt(0)->GetType()) {
990 case Primitive::kPrimBoolean:
991 case Primitive::kPrimByte:
992 case Primitive::kPrimChar:
993 case Primitive::kPrimShort:
994 case Primitive::kPrimInt:
995 case Primitive::kPrimNot:
996 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100997 DCHECK_EQ(ret->GetLocations()->InAt(0).As<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100998 break;
999
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001000 case Primitive::kPrimFloat:
1001 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001002 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001003 XMM0);
1004 break;
1005
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001006 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001007 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001008 }
1009 }
1010 codegen_->GenerateFrameExit();
1011 __ ret();
1012}
1013
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001014Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
1015 switch (type) {
1016 case Primitive::kPrimBoolean:
1017 case Primitive::kPrimByte:
1018 case Primitive::kPrimChar:
1019 case Primitive::kPrimShort:
1020 case Primitive::kPrimInt:
1021 case Primitive::kPrimNot: {
1022 uint32_t index = gp_index_++;
1023 stack_index_++;
1024 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001025 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001026 } else {
1027 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1028 }
1029 }
1030
1031 case Primitive::kPrimLong: {
1032 uint32_t index = gp_index_;
1033 stack_index_ += 2;
1034 if (index < calling_convention.GetNumberOfRegisters()) {
1035 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001036 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001037 } else {
1038 gp_index_ += 2;
1039 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1040 }
1041 }
1042
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001043 case Primitive::kPrimFloat: {
1044 uint32_t index = fp_index_++;
1045 stack_index_++;
1046 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001047 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001048 } else {
1049 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1050 }
1051 }
1052
1053 case Primitive::kPrimDouble: {
1054 uint32_t index = fp_index_++;
1055 stack_index_ += 2;
1056 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001057 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001058 } else {
1059 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1060 }
1061 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001062
1063 case Primitive::kPrimVoid:
1064 LOG(FATAL) << "Unexpected parameter type " << type;
1065 break;
1066 }
1067 return Location();
1068}
1069
1070void LocationsBuilderX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001071 HandleInvoke(invoke);
1072}
1073
1074void InstructionCodeGeneratorX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001075 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001076 // TODO: Implement all kinds of calls:
1077 // 1) boot -> boot
1078 // 2) app -> boot
1079 // 3) app -> app
1080 //
1081 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1082
1083 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001084 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001085 // temp = temp->dex_cache_resolved_methods_;
1086 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
1087 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001088 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001089 // (temp + offset_of_quick_compiled_code)()
1090 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
1091
1092 DCHECK(!codegen_->IsLeafMethod());
1093 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1094}
1095
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001096void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001097 LocationSummary* locations =
1098 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001099 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001100
1101 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001102 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001103 HInstruction* input = invoke->InputAt(i);
1104 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1105 }
1106
1107 switch (invoke->GetType()) {
1108 case Primitive::kPrimBoolean:
1109 case Primitive::kPrimByte:
1110 case Primitive::kPrimChar:
1111 case Primitive::kPrimShort:
1112 case Primitive::kPrimInt:
1113 case Primitive::kPrimNot:
1114 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001115 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001116 break;
1117
1118 case Primitive::kPrimVoid:
1119 break;
1120
1121 case Primitive::kPrimDouble:
1122 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001123 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001124 break;
1125 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001126}
1127
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001128void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1129 HandleInvoke(invoke);
1130}
1131
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001132void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001133 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001134 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1135 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1136 LocationSummary* locations = invoke->GetLocations();
1137 Location receiver = locations->InAt(0);
1138 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1139 // temp = object->GetClass();
1140 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001141 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1142 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001143 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001144 __ movl(temp, Address(receiver.As<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001145 }
1146 // temp = temp->GetMethodAt(method_offset);
1147 __ movl(temp, Address(temp, method_offset));
1148 // call temp->GetEntryPoint();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001149 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
1150
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001151 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001152 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001153}
1154
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001155void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1156 HandleInvoke(invoke);
1157 // Add the hidden argument.
1158 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1159}
1160
1161void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1162 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1163 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
1164 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1165 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1166 LocationSummary* locations = invoke->GetLocations();
1167 Location receiver = locations->InAt(0);
1168 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1169
1170 // Set the hidden argument.
1171 __ movq(invoke->GetLocations()->GetTemp(1).As<CpuRegister>(),
1172 Immediate(invoke->GetDexMethodIndex()));
1173
1174 // temp = object->GetClass();
1175 if (receiver.IsStackSlot()) {
1176 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1177 __ movl(temp, Address(temp, class_offset));
1178 } else {
1179 __ movl(temp, Address(receiver.As<CpuRegister>(), class_offset));
1180 }
1181 // temp = temp->GetImtEntryAt(method_offset);
1182 __ movl(temp, Address(temp, method_offset));
1183 // call temp->GetEntryPoint();
1184 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
1185
1186 DCHECK(!codegen_->IsLeafMethod());
1187 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1188}
1189
Roland Levillain88cb1752014-10-20 16:36:47 +01001190void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1191 LocationSummary* locations =
1192 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1193 switch (neg->GetResultType()) {
1194 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001195 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001196 locations->SetInAt(0, Location::RequiresRegister());
1197 locations->SetOut(Location::SameAsFirstInput());
1198 break;
1199
Roland Levillain88cb1752014-10-20 16:36:47 +01001200 case Primitive::kPrimFloat:
1201 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001202 locations->SetInAt(0, Location::RequiresFpuRegister());
1203 // Output overlaps as we need a fresh (zero-initialized)
1204 // register to perform subtraction from zero.
1205 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001206 break;
1207
1208 default:
1209 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1210 }
1211}
1212
1213void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1214 LocationSummary* locations = neg->GetLocations();
1215 Location out = locations->Out();
1216 Location in = locations->InAt(0);
1217 switch (neg->GetResultType()) {
1218 case Primitive::kPrimInt:
1219 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001220 DCHECK(in.Equals(out));
Roland Levillain88cb1752014-10-20 16:36:47 +01001221 __ negl(out.As<CpuRegister>());
1222 break;
1223
1224 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001225 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001226 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001227 __ negq(out.As<CpuRegister>());
1228 break;
1229
Roland Levillain88cb1752014-10-20 16:36:47 +01001230 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001231 DCHECK(in.IsFpuRegister());
1232 DCHECK(out.IsFpuRegister());
1233 DCHECK(!in.Equals(out));
1234 // TODO: Instead of computing negation as a subtraction from
1235 // zero, implement it with an exclusive or with value 0x80000000
1236 // (mask for bit 31, representing the sign of a single-precision
1237 // floating-point number), fetched from a constant pool:
1238 //
1239 // xorps out, [RIP:...] // value at RIP is 0x80 00 00 00
1240
1241 // out = 0
1242 __ xorps(out.As<XmmRegister>(), out.As<XmmRegister>());
1243 // out = out - in
1244 __ subss(out.As<XmmRegister>(), in.As<XmmRegister>());
1245 break;
1246
Roland Levillain88cb1752014-10-20 16:36:47 +01001247 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001248 DCHECK(in.IsFpuRegister());
1249 DCHECK(out.IsFpuRegister());
1250 DCHECK(!in.Equals(out));
1251 // TODO: Instead of computing negation as a subtraction from
1252 // zero, implement it with an exclusive or with value
1253 // 0x8000000000000000 (mask for bit 63, representing the sign of
1254 // a double-precision floating-point number), fetched from a
1255 // constant pool:
1256 //
1257 // xorpd out, [RIP:...] // value at RIP is 0x80 00 00 00 00 00 00 00
1258
1259 // out = 0
1260 __ xorpd(out.As<XmmRegister>(), out.As<XmmRegister>());
1261 // out = out - in
1262 __ subsd(out.As<XmmRegister>(), in.As<XmmRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001263 break;
1264
1265 default:
1266 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1267 }
1268}
1269
Roland Levillaindff1f282014-11-05 14:15:05 +00001270void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1271 LocationSummary* locations =
1272 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1273 Primitive::Type result_type = conversion->GetResultType();
1274 Primitive::Type input_type = conversion->GetInputType();
1275 switch (result_type) {
Roland Levillain946e1432014-11-11 17:35:19 +00001276 case Primitive::kPrimInt:
1277 switch (input_type) {
1278 case Primitive::kPrimLong:
1279 // long-to-int conversion.
1280 locations->SetInAt(0, Location::Any());
1281 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1282 break;
1283
1284 case Primitive::kPrimFloat:
1285 case Primitive::kPrimDouble:
1286 LOG(FATAL) << "Type conversion from " << input_type
1287 << " to " << result_type << " not yet implemented";
1288 break;
1289
1290 default:
1291 LOG(FATAL) << "Unexpected type conversion from " << input_type
1292 << " to " << result_type;
1293 }
1294 break;
1295
Roland Levillaindff1f282014-11-05 14:15:05 +00001296 case Primitive::kPrimLong:
1297 switch (input_type) {
1298 case Primitive::kPrimByte:
1299 case Primitive::kPrimShort:
1300 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001301 case Primitive::kPrimChar:
Roland Levillaindff1f282014-11-05 14:15:05 +00001302 // int-to-long conversion.
1303 // TODO: We would benefit from a (to-be-implemented)
1304 // Location::RegisterOrStackSlot requirement for this input.
1305 locations->SetInAt(0, Location::RequiresRegister());
1306 locations->SetOut(Location::RequiresRegister());
1307 break;
1308
1309 case Primitive::kPrimFloat:
1310 case Primitive::kPrimDouble:
1311 LOG(FATAL) << "Type conversion from " << input_type << " to "
1312 << result_type << " not yet implemented";
1313 break;
1314
1315 default:
1316 LOG(FATAL) << "Unexpected type conversion from " << input_type
1317 << " to " << result_type;
1318 }
1319 break;
1320
Roland Levillaindff1f282014-11-05 14:15:05 +00001321 case Primitive::kPrimFloat:
1322 case Primitive::kPrimDouble:
1323 LOG(FATAL) << "Type conversion from " << input_type
1324 << " to " << result_type << " not yet implemented";
1325 break;
1326
1327 default:
1328 LOG(FATAL) << "Unexpected type conversion from " << input_type
1329 << " to " << result_type;
1330 }
1331}
1332
1333void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1334 LocationSummary* locations = conversion->GetLocations();
1335 Location out = locations->Out();
1336 Location in = locations->InAt(0);
1337 Primitive::Type result_type = conversion->GetResultType();
1338 Primitive::Type input_type = conversion->GetInputType();
1339 switch (result_type) {
Roland Levillain946e1432014-11-11 17:35:19 +00001340 case Primitive::kPrimInt:
1341 switch (input_type) {
1342 case Primitive::kPrimLong:
1343 // long-to-int conversion.
1344 if (in.IsRegister()) {
1345 __ movl(out.As<CpuRegister>(), in.As<CpuRegister>());
1346 } else if (in.IsDoubleStackSlot()) {
1347 __ movl(out.As<CpuRegister>(),
1348 Address(CpuRegister(RSP), in.GetStackIndex()));
1349 } else {
1350 DCHECK(in.IsConstant());
1351 DCHECK(in.GetConstant()->IsLongConstant());
1352 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
1353 __ movl(out.As<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1354 }
1355 break;
1356
1357 case Primitive::kPrimFloat:
1358 case Primitive::kPrimDouble:
1359 LOG(FATAL) << "Type conversion from " << input_type
1360 << " to " << result_type << " not yet implemented";
1361 break;
1362
1363 default:
1364 LOG(FATAL) << "Unexpected type conversion from " << input_type
1365 << " to " << result_type;
1366 }
1367 break;
1368
Roland Levillaindff1f282014-11-05 14:15:05 +00001369 case Primitive::kPrimLong:
1370 switch (input_type) {
1371 DCHECK(out.IsRegister());
1372 case Primitive::kPrimByte:
1373 case Primitive::kPrimShort:
1374 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001375 case Primitive::kPrimChar:
Roland Levillaindff1f282014-11-05 14:15:05 +00001376 // int-to-long conversion.
1377 DCHECK(in.IsRegister());
1378 __ movsxd(out.As<CpuRegister>(), in.As<CpuRegister>());
1379 break;
1380
1381 case Primitive::kPrimFloat:
1382 case Primitive::kPrimDouble:
1383 LOG(FATAL) << "Type conversion from " << input_type << " to "
1384 << result_type << " not yet implemented";
1385 break;
1386
1387 default:
1388 LOG(FATAL) << "Unexpected type conversion from " << input_type
1389 << " to " << result_type;
1390 }
1391 break;
1392
Roland Levillaindff1f282014-11-05 14:15:05 +00001393 case Primitive::kPrimFloat:
1394 case Primitive::kPrimDouble:
1395 LOG(FATAL) << "Type conversion from " << input_type
1396 << " to " << result_type << " not yet implemented";
1397 break;
1398
1399 default:
1400 LOG(FATAL) << "Unexpected type conversion from " << input_type
1401 << " to " << result_type;
1402 }
1403}
1404
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001405void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001406 LocationSummary* locations =
1407 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001408 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001409 case Primitive::kPrimInt: {
1410 locations->SetInAt(0, Location::RequiresRegister());
1411 locations->SetInAt(1, Location::Any());
1412 locations->SetOut(Location::SameAsFirstInput());
1413 break;
1414 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001415
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001416 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001417 locations->SetInAt(0, Location::RequiresRegister());
1418 locations->SetInAt(1, Location::RequiresRegister());
1419 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001420 break;
1421 }
1422
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001423 case Primitive::kPrimDouble:
1424 case Primitive::kPrimFloat: {
1425 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001426 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001427 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001428 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001429 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001430
1431 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001432 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001433 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001434}
1435
1436void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1437 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001438 Location first = locations->InAt(0);
1439 Location second = locations->InAt(1);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001440 DCHECK(first.Equals(locations->Out()));
Calin Juravle11351682014-10-23 15:38:15 +01001441
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001442 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001443 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001444 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001445 __ addl(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001446 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001447 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001448 __ addl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001449 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001450 __ addl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001451 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001452 break;
1453 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001454
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001455 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001456 __ addq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001457 break;
1458 }
1459
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001460 case Primitive::kPrimFloat: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001461 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001462 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001463 }
1464
1465 case Primitive::kPrimDouble: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001466 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001467 break;
1468 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001469
1470 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001471 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001472 }
1473}
1474
1475void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001476 LocationSummary* locations =
1477 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001478 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001479 case Primitive::kPrimInt: {
1480 locations->SetInAt(0, Location::RequiresRegister());
1481 locations->SetInAt(1, Location::Any());
1482 locations->SetOut(Location::SameAsFirstInput());
1483 break;
1484 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001485 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001486 locations->SetInAt(0, Location::RequiresRegister());
1487 locations->SetInAt(1, Location::RequiresRegister());
1488 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001489 break;
1490 }
Calin Juravle11351682014-10-23 15:38:15 +01001491 case Primitive::kPrimFloat:
1492 case Primitive::kPrimDouble: {
1493 locations->SetInAt(0, Location::RequiresFpuRegister());
1494 locations->SetInAt(1, Location::RequiresFpuRegister());
1495 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001496 break;
Calin Juravle11351682014-10-23 15:38:15 +01001497 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001498 default:
Calin Juravle11351682014-10-23 15:38:15 +01001499 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001500 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001501}
1502
1503void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1504 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001505 Location first = locations->InAt(0);
1506 Location second = locations->InAt(1);
1507 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001508 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001509 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001510 if (second.IsRegister()) {
1511 __ subl(first.As<CpuRegister>(), second.As<CpuRegister>());
1512 } else if (second.IsConstant()) {
1513 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1514 __ subl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001515 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001516 __ subl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001517 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001518 break;
1519 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001520 case Primitive::kPrimLong: {
Calin Juravle11351682014-10-23 15:38:15 +01001521 __ subq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001522 break;
1523 }
1524
Calin Juravle11351682014-10-23 15:38:15 +01001525 case Primitive::kPrimFloat: {
1526 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001527 break;
Calin Juravle11351682014-10-23 15:38:15 +01001528 }
1529
1530 case Primitive::kPrimDouble: {
1531 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1532 break;
1533 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001534
1535 default:
Calin Juravle11351682014-10-23 15:38:15 +01001536 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001537 }
1538}
1539
Calin Juravle34bacdf2014-10-07 20:23:36 +01001540void LocationsBuilderX86_64::VisitMul(HMul* mul) {
1541 LocationSummary* locations =
1542 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1543 switch (mul->GetResultType()) {
1544 case Primitive::kPrimInt: {
1545 locations->SetInAt(0, Location::RequiresRegister());
1546 locations->SetInAt(1, Location::Any());
1547 locations->SetOut(Location::SameAsFirstInput());
1548 break;
1549 }
1550 case Primitive::kPrimLong: {
1551 locations->SetInAt(0, Location::RequiresRegister());
1552 locations->SetInAt(1, Location::RequiresRegister());
1553 locations->SetOut(Location::SameAsFirstInput());
1554 break;
1555 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001556 case Primitive::kPrimFloat:
1557 case Primitive::kPrimDouble: {
1558 locations->SetInAt(0, Location::RequiresFpuRegister());
1559 locations->SetInAt(1, Location::RequiresFpuRegister());
1560 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001561 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001562 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001563
1564 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001565 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001566 }
1567}
1568
1569void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
1570 LocationSummary* locations = mul->GetLocations();
1571 Location first = locations->InAt(0);
1572 Location second = locations->InAt(1);
1573 DCHECK(first.Equals(locations->Out()));
1574 switch (mul->GetResultType()) {
1575 case Primitive::kPrimInt: {
1576 if (second.IsRegister()) {
1577 __ imull(first.As<CpuRegister>(), second.As<CpuRegister>());
1578 } else if (second.IsConstant()) {
1579 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1580 __ imull(first.As<CpuRegister>(), imm);
1581 } else {
1582 DCHECK(second.IsStackSlot());
1583 __ imull(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
1584 }
1585 break;
1586 }
1587 case Primitive::kPrimLong: {
1588 __ imulq(first.As<CpuRegister>(), second.As<CpuRegister>());
1589 break;
1590 }
1591
Calin Juravleb5bfa962014-10-21 18:02:24 +01001592 case Primitive::kPrimFloat: {
1593 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001594 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001595 }
1596
1597 case Primitive::kPrimDouble: {
1598 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1599 break;
1600 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001601
1602 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001603 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001604 }
1605}
1606
Calin Juravle7c4954d2014-10-28 16:57:40 +00001607void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
1608 LocationSummary* locations =
1609 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1610 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001611 case Primitive::kPrimInt: {
1612 locations->SetInAt(0, Location::RegisterLocation(RAX));
1613 locations->SetInAt(1, Location::RequiresRegister());
1614 locations->SetOut(Location::SameAsFirstInput());
1615 // Intel uses edx:eax as the dividend.
1616 locations->AddTemp(Location::RegisterLocation(RDX));
1617 break;
1618 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001619 case Primitive::kPrimLong: {
1620 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1621 break;
1622 }
1623 case Primitive::kPrimFloat:
1624 case Primitive::kPrimDouble: {
1625 locations->SetInAt(0, Location::RequiresFpuRegister());
1626 locations->SetInAt(1, Location::RequiresFpuRegister());
1627 locations->SetOut(Location::SameAsFirstInput());
1628 break;
1629 }
1630
1631 default:
1632 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1633 }
1634}
1635
1636void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
1637 LocationSummary* locations = div->GetLocations();
1638 Location first = locations->InAt(0);
1639 Location second = locations->InAt(1);
1640 DCHECK(first.Equals(locations->Out()));
1641
1642 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001643 case Primitive::kPrimInt: {
1644 CpuRegister first_reg = first.As<CpuRegister>();
1645 CpuRegister second_reg = second.As<CpuRegister>();
1646 DCHECK_EQ(RAX, first_reg.AsRegister());
1647 DCHECK_EQ(RDX, locations->GetTemp(0).As<CpuRegister>().AsRegister());
1648
1649 SlowPathCodeX86_64* slow_path =
1650 new (GetGraph()->GetArena()) DivMinusOneSlowPathX86_64(first_reg.AsRegister());
1651 codegen_->AddSlowPath(slow_path);
1652
1653 // 0x80000000/-1 triggers an arithmetic exception!
1654 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
1655 // it's safe to just use negl instead of more complex comparisons.
1656
1657 __ cmpl(second_reg, Immediate(-1));
1658 __ j(kEqual, slow_path->GetEntryLabel());
1659
1660 // edx:eax <- sign-extended of eax
1661 __ cdq();
1662 // eax = quotient, edx = remainder
1663 __ idivl(second_reg);
1664
1665 __ Bind(slow_path->GetExitLabel());
1666 break;
1667 }
1668
Calin Juravle7c4954d2014-10-28 16:57:40 +00001669 case Primitive::kPrimLong: {
1670 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1671 break;
1672 }
1673
1674 case Primitive::kPrimFloat: {
1675 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1676 break;
1677 }
1678
1679 case Primitive::kPrimDouble: {
1680 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1681 break;
1682 }
1683
1684 default:
1685 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1686 }
1687}
1688
Calin Juravled0d48522014-11-04 16:40:20 +00001689void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1690 LocationSummary* locations =
1691 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1692 locations->SetInAt(0, Location::Any());
1693 if (instruction->HasUses()) {
1694 locations->SetOut(Location::SameAsFirstInput());
1695 }
1696}
1697
1698void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1699 SlowPathCodeX86_64* slow_path =
1700 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
1701 codegen_->AddSlowPath(slow_path);
1702
1703 LocationSummary* locations = instruction->GetLocations();
1704 Location value = locations->InAt(0);
1705
1706 if (value.IsRegister()) {
1707 __ testl(value.As<CpuRegister>(), value.As<CpuRegister>());
1708 } else if (value.IsStackSlot()) {
1709 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
1710 } else {
1711 DCHECK(value.IsConstant()) << value;
1712 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
1713 __ jmp(slow_path->GetEntryLabel());
1714 }
1715 return;
1716 }
1717 __ j(kEqual, slow_path->GetEntryLabel());
1718}
1719
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001720void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001721 LocationSummary* locations =
1722 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001723 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001724 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1725 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1726 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001727}
1728
1729void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
1730 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001731 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001732 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1733
1734 __ gs()->call(Address::Absolute(
1735 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
1736
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001737 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001738 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001739}
1740
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001741void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
1742 LocationSummary* locations =
1743 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1744 InvokeRuntimeCallingConvention calling_convention;
1745 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1746 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1747 locations->SetOut(Location::RegisterLocation(RAX));
1748 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1749}
1750
1751void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
1752 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001753 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001754 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1755
1756 __ gs()->call(Address::Absolute(
1757 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocArrayWithAccessCheck), true));
1758
1759 DCHECK(!codegen_->IsLeafMethod());
1760 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1761}
1762
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001763void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001764 LocationSummary* locations =
1765 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001766 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1767 if (location.IsStackSlot()) {
1768 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1769 } else if (location.IsDoubleStackSlot()) {
1770 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1771 }
1772 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001773}
1774
1775void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
1776 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001777 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001778}
1779
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001780void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001781 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001782 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001783 locations->SetInAt(0, Location::RequiresRegister());
1784 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001785}
1786
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001787void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
1788 LocationSummary* locations = not_->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001789 DCHECK_EQ(locations->InAt(0).As<CpuRegister>().AsRegister(),
1790 locations->Out().As<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001791 Location out = locations->Out();
1792 switch (not_->InputAt(0)->GetType()) {
1793 case Primitive::kPrimBoolean:
1794 __ xorq(out.As<CpuRegister>(), Immediate(1));
1795 break;
1796
1797 case Primitive::kPrimInt:
1798 __ notl(out.As<CpuRegister>());
1799 break;
1800
1801 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001802 __ notq(out.As<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001803 break;
1804
1805 default:
1806 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1807 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001808}
1809
1810void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001811 LocationSummary* locations =
1812 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001813 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1814 locations->SetInAt(i, Location::Any());
1815 }
1816 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001817}
1818
1819void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001820 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001821 LOG(FATAL) << "Unimplemented";
1822}
1823
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001824void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001825 LocationSummary* locations =
1826 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001827 Primitive::Type field_type = instruction->GetFieldType();
1828 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001829 locations->SetInAt(0, Location::RequiresRegister());
1830 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001831 if (is_object_type) {
1832 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001833 locations->AddTemp(Location::RequiresRegister());
1834 locations->AddTemp(Location::RequiresRegister());
1835 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001836}
1837
1838void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1839 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001840 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001841 size_t offset = instruction->GetFieldOffset().SizeValue();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001842 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001843
1844 switch (field_type) {
1845 case Primitive::kPrimBoolean:
1846 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001847 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001848 __ movb(Address(obj, offset), value);
1849 break;
1850 }
1851
1852 case Primitive::kPrimShort:
1853 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001854 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001855 __ movw(Address(obj, offset), value);
1856 break;
1857 }
1858
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001859 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001860 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001861 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001862 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001863 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001864 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
1865 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001866 codegen_->MarkGCCard(temp, card, obj, value);
1867 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001868 break;
1869 }
1870
1871 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001872 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001873 __ movq(Address(obj, offset), value);
1874 break;
1875 }
1876
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001877 case Primitive::kPrimFloat: {
1878 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1879 __ movss(Address(obj, offset), value);
1880 break;
1881 }
1882
1883 case Primitive::kPrimDouble: {
1884 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1885 __ movsd(Address(obj, offset), value);
1886 break;
1887 }
1888
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001889 case Primitive::kPrimVoid:
1890 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001891 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001892 }
1893}
1894
1895void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001896 LocationSummary* locations =
1897 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001898 locations->SetInAt(0, Location::RequiresRegister());
1899 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001900}
1901
1902void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1903 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001904 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001905 size_t offset = instruction->GetFieldOffset().SizeValue();
1906
1907 switch (instruction->GetType()) {
1908 case Primitive::kPrimBoolean: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001909 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001910 __ movzxb(out, Address(obj, offset));
1911 break;
1912 }
1913
1914 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001915 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001916 __ movsxb(out, Address(obj, offset));
1917 break;
1918 }
1919
1920 case Primitive::kPrimShort: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001921 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001922 __ movsxw(out, Address(obj, offset));
1923 break;
1924 }
1925
1926 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001927 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001928 __ movzxw(out, Address(obj, offset));
1929 break;
1930 }
1931
1932 case Primitive::kPrimInt:
1933 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001934 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001935 __ movl(out, Address(obj, offset));
1936 break;
1937 }
1938
1939 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001940 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001941 __ movq(out, Address(obj, offset));
1942 break;
1943 }
1944
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001945 case Primitive::kPrimFloat: {
1946 XmmRegister out = locations->Out().As<XmmRegister>();
1947 __ movss(out, Address(obj, offset));
1948 break;
1949 }
1950
1951 case Primitive::kPrimDouble: {
1952 XmmRegister out = locations->Out().As<XmmRegister>();
1953 __ movsd(out, Address(obj, offset));
1954 break;
1955 }
1956
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001957 case Primitive::kPrimVoid:
1958 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001959 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001960 }
1961}
1962
1963void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001964 LocationSummary* locations =
1965 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001966 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001967 if (instruction->HasUses()) {
1968 locations->SetOut(Location::SameAsFirstInput());
1969 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001970}
1971
1972void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001973 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001974 codegen_->AddSlowPath(slow_path);
1975
1976 LocationSummary* locations = instruction->GetLocations();
1977 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001978
1979 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001980 __ cmpl(obj.As<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001981 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001982 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001983 } else {
1984 DCHECK(obj.IsConstant()) << obj;
1985 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1986 __ jmp(slow_path->GetEntryLabel());
1987 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001988 }
1989 __ j(kEqual, slow_path->GetEntryLabel());
1990}
1991
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001992void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001993 LocationSummary* locations =
1994 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001995 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001996 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001997 1, Location::RegisterOrConstant(instruction->InputAt(1)));
1998 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001999}
2000
2001void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
2002 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002003 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002004 Location index = locations->InAt(1);
2005
2006 switch (instruction->GetType()) {
2007 case Primitive::kPrimBoolean: {
2008 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002009 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002010 if (index.IsConstant()) {
2011 __ movzxb(out, Address(obj,
2012 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2013 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002014 __ movzxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002015 }
2016 break;
2017 }
2018
2019 case Primitive::kPrimByte: {
2020 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002021 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002022 if (index.IsConstant()) {
2023 __ movsxb(out, Address(obj,
2024 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2025 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002026 __ movsxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002027 }
2028 break;
2029 }
2030
2031 case Primitive::kPrimShort: {
2032 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002033 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002034 if (index.IsConstant()) {
2035 __ movsxw(out, Address(obj,
2036 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2037 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002038 __ movsxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002039 }
2040 break;
2041 }
2042
2043 case Primitive::kPrimChar: {
2044 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002045 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002046 if (index.IsConstant()) {
2047 __ movzxw(out, Address(obj,
2048 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2049 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002050 __ movzxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002051 }
2052 break;
2053 }
2054
2055 case Primitive::kPrimInt:
2056 case Primitive::kPrimNot: {
2057 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
2058 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002059 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002060 if (index.IsConstant()) {
2061 __ movl(out, Address(obj,
2062 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2063 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002064 __ movl(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002065 }
2066 break;
2067 }
2068
2069 case Primitive::kPrimLong: {
2070 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002071 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002072 if (index.IsConstant()) {
2073 __ movq(out, Address(obj,
2074 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2075 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002076 __ movq(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002077 }
2078 break;
2079 }
2080
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002081 case Primitive::kPrimFloat: {
2082 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2083 XmmRegister out = locations->Out().As<XmmRegister>();
2084 if (index.IsConstant()) {
2085 __ movss(out, Address(obj,
2086 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2087 } else {
2088 __ movss(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
2089 }
2090 break;
2091 }
2092
2093 case Primitive::kPrimDouble: {
2094 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2095 XmmRegister out = locations->Out().As<XmmRegister>();
2096 if (index.IsConstant()) {
2097 __ movsd(out, Address(obj,
2098 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2099 } else {
2100 __ movsd(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
2101 }
2102 break;
2103 }
2104
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002105 case Primitive::kPrimVoid:
2106 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002107 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002108 }
2109}
2110
2111void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002112 Primitive::Type value_type = instruction->GetComponentType();
2113 bool is_object = value_type == Primitive::kPrimNot;
2114 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2115 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
2116 if (is_object) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002117 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002118 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2119 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2120 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002121 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002122 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002123 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002124 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2125 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002126 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002127 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002128 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
2129 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002130 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002131 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002132 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002133 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002134}
2135
2136void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
2137 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002138 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002139 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002140 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002141 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002142
2143 switch (value_type) {
2144 case Primitive::kPrimBoolean:
2145 case Primitive::kPrimByte: {
2146 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002147 if (index.IsConstant()) {
2148 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002149 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002150 __ movb(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002151 } else {
2152 __ movb(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2153 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002154 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002155 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002156 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
2157 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002158 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002159 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002160 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2161 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002162 }
2163 break;
2164 }
2165
2166 case Primitive::kPrimShort:
2167 case Primitive::kPrimChar: {
2168 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002169 if (index.IsConstant()) {
2170 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002171 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002172 __ movw(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002173 } else {
2174 __ movw(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2175 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002176 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002177 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002178 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
2179 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002180 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002181 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002182 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2183 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002184 }
2185 break;
2186 }
2187
2188 case Primitive::kPrimInt: {
2189 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002190 if (index.IsConstant()) {
2191 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002192 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002193 __ movl(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002194 } else {
2195 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2196 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002197 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002198 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002199 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2200 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002201 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002202 DCHECK(value.IsConstant()) << value;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002203 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002204 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2205 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002206 }
2207 break;
2208 }
2209
2210 case Primitive::kPrimNot: {
2211 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject), true));
2212 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002213 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002214 break;
2215 }
2216
2217 case Primitive::kPrimLong: {
2218 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002219 if (index.IsConstant()) {
2220 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002221 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002222 __ movq(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002223 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002224 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002225 __ movq(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
2226 value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002227 }
2228 break;
2229 }
2230
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002231 case Primitive::kPrimFloat: {
2232 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2233 if (index.IsConstant()) {
2234 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2235 DCHECK(value.IsFpuRegister());
2236 __ movss(Address(obj, offset), value.As<XmmRegister>());
2237 } else {
2238 DCHECK(value.IsFpuRegister());
2239 __ movss(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2240 value.As<XmmRegister>());
2241 }
2242 break;
2243 }
2244
2245 case Primitive::kPrimDouble: {
2246 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2247 if (index.IsConstant()) {
2248 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
2249 DCHECK(value.IsFpuRegister());
2250 __ movsd(Address(obj, offset), value.As<XmmRegister>());
2251 } else {
2252 DCHECK(value.IsFpuRegister());
2253 __ movsd(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
2254 value.As<XmmRegister>());
2255 }
2256 break;
2257 }
2258
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002259 case Primitive::kPrimVoid:
2260 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002261 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002262 }
2263}
2264
2265void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002266 LocationSummary* locations =
2267 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002268 locations->SetInAt(0, Location::RequiresRegister());
2269 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002270}
2271
2272void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
2273 LocationSummary* locations = instruction->GetLocations();
2274 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002275 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2276 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002277 __ movl(out, Address(obj, offset));
2278}
2279
2280void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002281 LocationSummary* locations =
2282 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002283 locations->SetInAt(0, Location::RequiresRegister());
2284 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002285 if (instruction->HasUses()) {
2286 locations->SetOut(Location::SameAsFirstInput());
2287 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002288}
2289
2290void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
2291 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002292 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002293 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002294 codegen_->AddSlowPath(slow_path);
2295
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002296 CpuRegister index = locations->InAt(0).As<CpuRegister>();
2297 CpuRegister length = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002298
2299 __ cmpl(index, length);
2300 __ j(kAboveEqual, slow_path->GetEntryLabel());
2301}
2302
2303void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
2304 CpuRegister card,
2305 CpuRegister object,
2306 CpuRegister value) {
2307 Label is_null;
2308 __ testl(value, value);
2309 __ j(kEqual, &is_null);
2310 __ gs()->movq(card, Address::Absolute(
2311 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
2312 __ movq(temp, object);
2313 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
2314 __ movb(Address(temp, card, TIMES_1, 0), card);
2315 __ Bind(&is_null);
2316}
2317
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002318void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
2319 temp->SetLocations(nullptr);
2320}
2321
2322void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
2323 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002324 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002325}
2326
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002327void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002328 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002329 LOG(FATAL) << "Unimplemented";
2330}
2331
2332void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002333 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2334}
2335
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002336void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
2337 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2338}
2339
2340void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002341 HBasicBlock* block = instruction->GetBlock();
2342 if (block->GetLoopInformation() != nullptr) {
2343 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2344 // The back edge will generate the suspend check.
2345 return;
2346 }
2347 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2348 // The goto will generate the suspend check.
2349 return;
2350 }
2351 GenerateSuspendCheck(instruction, nullptr);
2352}
2353
2354void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
2355 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002356 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002357 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002358 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002359 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002360 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002361 if (successor == nullptr) {
2362 __ j(kNotEqual, slow_path->GetEntryLabel());
2363 __ Bind(slow_path->GetReturnLabel());
2364 } else {
2365 __ j(kEqual, codegen_->GetLabelOf(successor));
2366 __ jmp(slow_path->GetEntryLabel());
2367 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002368}
2369
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002370X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
2371 return codegen_->GetAssembler();
2372}
2373
2374void ParallelMoveResolverX86_64::EmitMove(size_t index) {
2375 MoveOperands* move = moves_.Get(index);
2376 Location source = move->GetSource();
2377 Location destination = move->GetDestination();
2378
2379 if (source.IsRegister()) {
2380 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002381 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002382 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002383 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002384 source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002385 } else {
2386 DCHECK(destination.IsDoubleStackSlot());
2387 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002388 source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002389 }
2390 } else if (source.IsStackSlot()) {
2391 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002392 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002393 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002394 } else if (destination.IsFpuRegister()) {
2395 __ movss(destination.As<XmmRegister>(),
2396 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002397 } else {
2398 DCHECK(destination.IsStackSlot());
2399 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2400 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2401 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002402 } else if (source.IsDoubleStackSlot()) {
2403 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002404 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002405 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002406 } else if (destination.IsFpuRegister()) {
2407 __ movsd(destination.As<XmmRegister>(), Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002408 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01002409 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002410 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2411 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2412 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002413 } else if (source.IsConstant()) {
2414 HConstant* constant = source.GetConstant();
2415 if (constant->IsIntConstant()) {
2416 Immediate imm(constant->AsIntConstant()->GetValue());
2417 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002418 __ movl(destination.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002419 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002420 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002421 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2422 }
2423 } else if (constant->IsLongConstant()) {
2424 int64_t value = constant->AsLongConstant()->GetValue();
2425 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002426 __ movq(destination.As<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002427 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002428 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002429 __ movq(CpuRegister(TMP), Immediate(value));
2430 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2431 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002432 } else if (constant->IsFloatConstant()) {
2433 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
2434 if (destination.IsFpuRegister()) {
2435 __ movl(CpuRegister(TMP), imm);
2436 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2437 } else {
2438 DCHECK(destination.IsStackSlot()) << destination;
2439 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2440 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002441 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002442 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
2443 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
2444 if (destination.IsFpuRegister()) {
2445 __ movq(CpuRegister(TMP), imm);
2446 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2447 } else {
2448 DCHECK(destination.IsDoubleStackSlot()) << destination;
2449 __ movq(CpuRegister(TMP), imm);
2450 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2451 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002452 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002453 } else if (source.IsFpuRegister()) {
2454 if (destination.IsFpuRegister()) {
2455 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
2456 } else if (destination.IsStackSlot()) {
2457 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
2458 source.As<XmmRegister>());
2459 } else {
2460 DCHECK(destination.IsDoubleStackSlot());
2461 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
2462 source.As<XmmRegister>());
2463 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002464 }
2465}
2466
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002467void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002468 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002469 __ movl(Address(CpuRegister(RSP), mem), reg);
2470 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002471}
2472
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002473void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002474 ScratchRegisterScope ensure_scratch(
2475 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2476
2477 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2478 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2479 __ movl(CpuRegister(ensure_scratch.GetRegister()),
2480 Address(CpuRegister(RSP), mem2 + stack_offset));
2481 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2482 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
2483 CpuRegister(ensure_scratch.GetRegister()));
2484}
2485
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002486void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
2487 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2488 __ movq(Address(CpuRegister(RSP), mem), reg);
2489 __ movq(reg, CpuRegister(TMP));
2490}
2491
2492void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
2493 ScratchRegisterScope ensure_scratch(
2494 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2495
2496 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2497 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2498 __ movq(CpuRegister(ensure_scratch.GetRegister()),
2499 Address(CpuRegister(RSP), mem2 + stack_offset));
2500 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2501 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
2502 CpuRegister(ensure_scratch.GetRegister()));
2503}
2504
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002505void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
2506 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2507 __ movss(Address(CpuRegister(RSP), mem), reg);
2508 __ movd(reg, CpuRegister(TMP));
2509}
2510
2511void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
2512 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2513 __ movsd(Address(CpuRegister(RSP), mem), reg);
2514 __ movd(reg, CpuRegister(TMP));
2515}
2516
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002517void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
2518 MoveOperands* move = moves_.Get(index);
2519 Location source = move->GetSource();
2520 Location destination = move->GetDestination();
2521
2522 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002523 __ xchgq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002524 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002525 Exchange32(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002526 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002527 Exchange32(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002528 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002529 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
2530 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002531 Exchange64(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002532 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002533 Exchange64(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002534 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
2535 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002536 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
2537 __ movd(CpuRegister(TMP), source.As<XmmRegister>());
2538 __ movaps(source.As<XmmRegister>(), destination.As<XmmRegister>());
2539 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2540 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
2541 Exchange32(source.As<XmmRegister>(), destination.GetStackIndex());
2542 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
2543 Exchange32(destination.As<XmmRegister>(), source.GetStackIndex());
2544 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
2545 Exchange64(source.As<XmmRegister>(), destination.GetStackIndex());
2546 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
2547 Exchange64(destination.As<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002548 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002549 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002550 }
2551}
2552
2553
2554void ParallelMoveResolverX86_64::SpillScratch(int reg) {
2555 __ pushq(CpuRegister(reg));
2556}
2557
2558
2559void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
2560 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002561}
2562
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002563void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
2564 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
2565 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2566 Immediate(mirror::Class::kStatusInitialized));
2567 __ j(kLess, slow_path->GetEntryLabel());
2568 __ Bind(slow_path->GetExitLabel());
2569 // No need for memory fence, thanks to the X86_64 memory model.
2570}
2571
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002572void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002573 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2574 ? LocationSummary::kCallOnSlowPath
2575 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002576 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002577 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002578 locations->SetOut(Location::RequiresRegister());
2579}
2580
2581void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
2582 CpuRegister out = cls->GetLocations()->Out().As<CpuRegister>();
2583 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002584 DCHECK(!cls->CanCallRuntime());
2585 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002586 codegen_->LoadCurrentMethod(out);
2587 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2588 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002589 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002590 codegen_->LoadCurrentMethod(out);
2591 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2592 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002593 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
2594 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2595 codegen_->AddSlowPath(slow_path);
2596 __ testl(out, out);
2597 __ j(kEqual, slow_path->GetEntryLabel());
2598 if (cls->MustGenerateClinitCheck()) {
2599 GenerateClassInitializationCheck(slow_path, out);
2600 } else {
2601 __ Bind(slow_path->GetExitLabel());
2602 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002603 }
2604}
2605
2606void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
2607 LocationSummary* locations =
2608 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2609 locations->SetInAt(0, Location::RequiresRegister());
2610 if (check->HasUses()) {
2611 locations->SetOut(Location::SameAsFirstInput());
2612 }
2613}
2614
2615void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002616 // We assume the class to not be null.
2617 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
2618 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002619 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002620 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002621}
2622
2623void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2624 LocationSummary* locations =
2625 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2626 locations->SetInAt(0, Location::RequiresRegister());
2627 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2628}
2629
2630void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2631 LocationSummary* locations = instruction->GetLocations();
2632 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002633 size_t offset = instruction->GetFieldOffset().SizeValue();
2634
2635 switch (instruction->GetType()) {
2636 case Primitive::kPrimBoolean: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002637 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002638 __ movzxb(out, Address(cls, offset));
2639 break;
2640 }
2641
2642 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002643 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002644 __ movsxb(out, Address(cls, offset));
2645 break;
2646 }
2647
2648 case Primitive::kPrimShort: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002649 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002650 __ movsxw(out, Address(cls, offset));
2651 break;
2652 }
2653
2654 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002655 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002656 __ movzxw(out, Address(cls, offset));
2657 break;
2658 }
2659
2660 case Primitive::kPrimInt:
2661 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002662 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002663 __ movl(out, Address(cls, offset));
2664 break;
2665 }
2666
2667 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002668 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002669 __ movq(out, Address(cls, offset));
2670 break;
2671 }
2672
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002673 case Primitive::kPrimFloat: {
2674 XmmRegister out = locations->Out().As<XmmRegister>();
2675 __ movss(out, Address(cls, offset));
2676 break;
2677 }
2678
2679 case Primitive::kPrimDouble: {
2680 XmmRegister out = locations->Out().As<XmmRegister>();
2681 __ movsd(out, Address(cls, offset));
2682 break;
2683 }
2684
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002685 case Primitive::kPrimVoid:
2686 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2687 UNREACHABLE();
2688 }
2689}
2690
2691void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2692 LocationSummary* locations =
2693 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2694 Primitive::Type field_type = instruction->GetFieldType();
2695 bool is_object_type = field_type == Primitive::kPrimNot;
2696 locations->SetInAt(0, Location::RequiresRegister());
2697 locations->SetInAt(1, Location::RequiresRegister());
2698 if (is_object_type) {
2699 // Temporary registers for the write barrier.
2700 locations->AddTemp(Location::RequiresRegister());
2701 locations->AddTemp(Location::RequiresRegister());
2702 }
2703}
2704
2705void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2706 LocationSummary* locations = instruction->GetLocations();
2707 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002708 size_t offset = instruction->GetFieldOffset().SizeValue();
2709 Primitive::Type field_type = instruction->GetFieldType();
2710
2711 switch (field_type) {
2712 case Primitive::kPrimBoolean:
2713 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002714 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002715 __ movb(Address(cls, offset), value);
2716 break;
2717 }
2718
2719 case Primitive::kPrimShort:
2720 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002721 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002722 __ movw(Address(cls, offset), value);
2723 break;
2724 }
2725
2726 case Primitive::kPrimInt:
2727 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002728 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002729 __ movl(Address(cls, offset), value);
2730 if (field_type == Primitive::kPrimNot) {
2731 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
2732 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
2733 codegen_->MarkGCCard(temp, card, cls, value);
2734 }
2735 break;
2736 }
2737
2738 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002739 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002740 __ movq(Address(cls, offset), value);
2741 break;
2742 }
2743
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002744 case Primitive::kPrimFloat: {
2745 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2746 __ movss(Address(cls, offset), value);
2747 break;
2748 }
2749
2750 case Primitive::kPrimDouble: {
2751 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2752 __ movsd(Address(cls, offset), value);
2753 break;
2754 }
2755
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002756 case Primitive::kPrimVoid:
2757 LOG(FATAL) << "Unreachable type " << field_type;
2758 UNREACHABLE();
2759 }
2760}
2761
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002762void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
2763 LocationSummary* locations =
2764 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2765 locations->SetOut(Location::RequiresRegister());
2766}
2767
2768void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
2769 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
2770 codegen_->AddSlowPath(slow_path);
2771
2772 CpuRegister out = load->GetLocations()->Out().As<CpuRegister>();
2773 codegen_->LoadCurrentMethod(CpuRegister(out));
2774 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
2775 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2776 __ testl(out, out);
2777 __ j(kEqual, slow_path->GetEntryLabel());
2778 __ Bind(slow_path->GetExitLabel());
2779}
2780
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002781void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
2782 LocationSummary* locations =
2783 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2784 locations->SetOut(Location::RequiresRegister());
2785}
2786
2787void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
2788 Address address = Address::Absolute(
2789 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
2790 __ gs()->movl(load->GetLocations()->Out().As<CpuRegister>(), address);
2791 __ gs()->movl(address, Immediate(0));
2792}
2793
2794void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
2795 LocationSummary* locations =
2796 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2797 InvokeRuntimeCallingConvention calling_convention;
2798 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2799}
2800
2801void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
2802 __ gs()->call(
2803 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
2804 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2805}
2806
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002807void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002808 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
2809 ? LocationSummary::kNoCall
2810 : LocationSummary::kCallOnSlowPath;
2811 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2812 locations->SetInAt(0, Location::RequiresRegister());
2813 locations->SetInAt(1, Location::Any());
2814 locations->SetOut(Location::RequiresRegister());
2815}
2816
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002817void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002818 LocationSummary* locations = instruction->GetLocations();
2819 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2820 Location cls = locations->InAt(1);
2821 CpuRegister out = locations->Out().As<CpuRegister>();
2822 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2823 Label done, zero;
2824 SlowPathCodeX86_64* slow_path = nullptr;
2825
2826 // Return 0 if `obj` is null.
2827 // TODO: avoid this check if we know obj is not null.
2828 __ testl(obj, obj);
2829 __ j(kEqual, &zero);
2830 // Compare the class of `obj` with `cls`.
2831 __ movl(out, Address(obj, class_offset));
2832 if (cls.IsRegister()) {
2833 __ cmpl(out, cls.As<CpuRegister>());
2834 } else {
2835 DCHECK(cls.IsStackSlot()) << cls;
2836 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
2837 }
2838 if (instruction->IsClassFinal()) {
2839 // Classes must be equal for the instanceof to succeed.
2840 __ j(kNotEqual, &zero);
2841 __ movl(out, Immediate(1));
2842 __ jmp(&done);
2843 } else {
2844 // If the classes are not equal, we go into a slow path.
2845 DCHECK(locations->OnlyCallsOnSlowPath());
2846 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002847 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002848 codegen_->AddSlowPath(slow_path);
2849 __ j(kNotEqual, slow_path->GetEntryLabel());
2850 __ movl(out, Immediate(1));
2851 __ jmp(&done);
2852 }
2853 __ Bind(&zero);
2854 __ movl(out, Immediate(0));
2855 if (slow_path != nullptr) {
2856 __ Bind(slow_path->GetExitLabel());
2857 }
2858 __ Bind(&done);
2859}
2860
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002861void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
2862 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2863 instruction, LocationSummary::kCallOnSlowPath);
2864 locations->SetInAt(0, Location::RequiresRegister());
2865 locations->SetInAt(1, Location::Any());
2866 locations->AddTemp(Location::RequiresRegister());
2867}
2868
2869void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
2870 LocationSummary* locations = instruction->GetLocations();
2871 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2872 Location cls = locations->InAt(1);
2873 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
2874 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2875 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
2876 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
2877 codegen_->AddSlowPath(slow_path);
2878
2879 // TODO: avoid this check if we know obj is not null.
2880 __ testl(obj, obj);
2881 __ j(kEqual, slow_path->GetExitLabel());
2882 // Compare the class of `obj` with `cls`.
2883 __ movl(temp, Address(obj, class_offset));
2884 if (cls.IsRegister()) {
2885 __ cmpl(temp, cls.As<CpuRegister>());
2886 } else {
2887 DCHECK(cls.IsStackSlot()) << cls;
2888 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
2889 }
2890 // Classes must be equal for the checkcast to succeed.
2891 __ j(kNotEqual, slow_path->GetEntryLabel());
2892 __ Bind(slow_path->GetExitLabel());
2893}
2894
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002895void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
2896 LocationSummary* locations =
2897 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2898 InvokeRuntimeCallingConvention calling_convention;
2899 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2900}
2901
2902void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
2903 __ gs()->call(Address::Absolute(instruction->IsEnter()
2904 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
2905 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
2906 true));
2907 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2908}
2909
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002910} // namespace x86_64
2911} // namespace art