blob: 23b8bd75ea29199ad42cc1c74ad0db7a9c924543 [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();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00001828 bool needs_write_barrier =
1829 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->GetValue());
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001830 locations->SetInAt(0, Location::RequiresRegister());
1831 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00001832 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001833 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001834 locations->AddTemp(Location::RequiresRegister());
1835 locations->AddTemp(Location::RequiresRegister());
1836 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001837}
1838
1839void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1840 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001841 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001842 size_t offset = instruction->GetFieldOffset().SizeValue();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001843 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001844
1845 switch (field_type) {
1846 case Primitive::kPrimBoolean:
1847 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001848 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001849 __ movb(Address(obj, offset), value);
1850 break;
1851 }
1852
1853 case Primitive::kPrimShort:
1854 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001855 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001856 __ movw(Address(obj, offset), value);
1857 break;
1858 }
1859
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001860 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001861 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001862 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001863 __ movl(Address(obj, offset), value);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00001864 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->GetValue())) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001865 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
1866 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001867 codegen_->MarkGCCard(temp, card, obj, value);
1868 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001869 break;
1870 }
1871
1872 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001873 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001874 __ movq(Address(obj, offset), value);
1875 break;
1876 }
1877
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001878 case Primitive::kPrimFloat: {
1879 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1880 __ movss(Address(obj, offset), value);
1881 break;
1882 }
1883
1884 case Primitive::kPrimDouble: {
1885 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1886 __ movsd(Address(obj, offset), value);
1887 break;
1888 }
1889
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001890 case Primitive::kPrimVoid:
1891 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001892 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001893 }
1894}
1895
1896void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001897 LocationSummary* locations =
1898 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001899 locations->SetInAt(0, Location::RequiresRegister());
1900 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001901}
1902
1903void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1904 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001905 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001906 size_t offset = instruction->GetFieldOffset().SizeValue();
1907
1908 switch (instruction->GetType()) {
1909 case Primitive::kPrimBoolean: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001910 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001911 __ movzxb(out, Address(obj, offset));
1912 break;
1913 }
1914
1915 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001916 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001917 __ movsxb(out, Address(obj, offset));
1918 break;
1919 }
1920
1921 case Primitive::kPrimShort: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001922 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001923 __ movsxw(out, Address(obj, offset));
1924 break;
1925 }
1926
1927 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001928 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001929 __ movzxw(out, Address(obj, offset));
1930 break;
1931 }
1932
1933 case Primitive::kPrimInt:
1934 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001935 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001936 __ movl(out, Address(obj, offset));
1937 break;
1938 }
1939
1940 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001941 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001942 __ movq(out, Address(obj, offset));
1943 break;
1944 }
1945
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001946 case Primitive::kPrimFloat: {
1947 XmmRegister out = locations->Out().As<XmmRegister>();
1948 __ movss(out, Address(obj, offset));
1949 break;
1950 }
1951
1952 case Primitive::kPrimDouble: {
1953 XmmRegister out = locations->Out().As<XmmRegister>();
1954 __ movsd(out, Address(obj, offset));
1955 break;
1956 }
1957
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001958 case Primitive::kPrimVoid:
1959 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001960 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001961 }
1962}
1963
1964void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001965 LocationSummary* locations =
1966 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001967 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001968 if (instruction->HasUses()) {
1969 locations->SetOut(Location::SameAsFirstInput());
1970 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001971}
1972
1973void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001974 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001975 codegen_->AddSlowPath(slow_path);
1976
1977 LocationSummary* locations = instruction->GetLocations();
1978 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001979
1980 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001981 __ cmpl(obj.As<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001982 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001983 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001984 } else {
1985 DCHECK(obj.IsConstant()) << obj;
1986 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1987 __ jmp(slow_path->GetEntryLabel());
1988 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001989 }
1990 __ j(kEqual, slow_path->GetEntryLabel());
1991}
1992
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001993void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001994 LocationSummary* locations =
1995 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001996 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001997 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001998 1, Location::RegisterOrConstant(instruction->InputAt(1)));
1999 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002000}
2001
2002void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
2003 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002004 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002005 Location index = locations->InAt(1);
2006
2007 switch (instruction->GetType()) {
2008 case Primitive::kPrimBoolean: {
2009 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002010 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002011 if (index.IsConstant()) {
2012 __ movzxb(out, Address(obj,
2013 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2014 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002015 __ movzxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002016 }
2017 break;
2018 }
2019
2020 case Primitive::kPrimByte: {
2021 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002022 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002023 if (index.IsConstant()) {
2024 __ movsxb(out, Address(obj,
2025 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2026 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002027 __ movsxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002028 }
2029 break;
2030 }
2031
2032 case Primitive::kPrimShort: {
2033 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002034 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002035 if (index.IsConstant()) {
2036 __ movsxw(out, Address(obj,
2037 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2038 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002039 __ movsxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002040 }
2041 break;
2042 }
2043
2044 case Primitive::kPrimChar: {
2045 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002046 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002047 if (index.IsConstant()) {
2048 __ movzxw(out, Address(obj,
2049 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2050 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002051 __ movzxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002052 }
2053 break;
2054 }
2055
2056 case Primitive::kPrimInt:
2057 case Primitive::kPrimNot: {
2058 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
2059 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002060 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002061 if (index.IsConstant()) {
2062 __ movl(out, Address(obj,
2063 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2064 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002065 __ movl(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002066 }
2067 break;
2068 }
2069
2070 case Primitive::kPrimLong: {
2071 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002072 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002073 if (index.IsConstant()) {
2074 __ movq(out, Address(obj,
2075 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2076 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002077 __ movq(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002078 }
2079 break;
2080 }
2081
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002082 case Primitive::kPrimFloat: {
2083 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2084 XmmRegister out = locations->Out().As<XmmRegister>();
2085 if (index.IsConstant()) {
2086 __ movss(out, Address(obj,
2087 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2088 } else {
2089 __ movss(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
2090 }
2091 break;
2092 }
2093
2094 case Primitive::kPrimDouble: {
2095 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2096 XmmRegister out = locations->Out().As<XmmRegister>();
2097 if (index.IsConstant()) {
2098 __ movsd(out, Address(obj,
2099 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2100 } else {
2101 __ movsd(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
2102 }
2103 break;
2104 }
2105
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002106 case Primitive::kPrimVoid:
2107 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002108 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002109 }
2110}
2111
2112void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002113 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002114
2115 bool needs_write_barrier =
2116 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2117 bool needs_runtime_call = instruction->NeedsTypeCheck();
2118
Nicolas Geoffray39468442014-09-02 15:17:15 +01002119 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002120 instruction, needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
2121 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002122 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002123 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2124 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2125 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002126 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002127 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002128 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002129 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2130 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002131 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002132 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002133 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
2134 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002135 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002136 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002137 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002138
2139 if (needs_write_barrier) {
2140 // Temporary registers for the write barrier.
2141 locations->AddTemp(Location::RequiresRegister());
2142 locations->AddTemp(Location::RequiresRegister());
2143 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002144 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002145}
2146
2147void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
2148 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002149 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002150 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002151 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002152 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002153 bool needs_runtime_call = locations->WillCall();
2154 bool needs_write_barrier =
2155 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002156
2157 switch (value_type) {
2158 case Primitive::kPrimBoolean:
2159 case Primitive::kPrimByte: {
2160 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002161 if (index.IsConstant()) {
2162 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002163 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002164 __ movb(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002165 } else {
2166 __ movb(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2167 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002168 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002169 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002170 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
2171 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002172 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002173 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002174 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2175 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002176 }
2177 break;
2178 }
2179
2180 case Primitive::kPrimShort:
2181 case Primitive::kPrimChar: {
2182 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002183 if (index.IsConstant()) {
2184 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002185 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002186 __ movw(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002187 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002188 DCHECK(value.IsConstant()) << value;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002189 __ movw(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2190 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002191 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002192 DCHECK(index.IsRegister()) << index;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002193 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002194 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
2195 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002196 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002197 DCHECK(value.IsConstant()) << value;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002198 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002199 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2200 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002201 }
2202 break;
2203 }
2204
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002205 case Primitive::kPrimInt:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002206 case Primitive::kPrimNot: {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002207 if (!needs_runtime_call) {
2208 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2209 if (index.IsConstant()) {
2210 size_t offset =
2211 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2212 if (value.IsRegister()) {
2213 __ movl(Address(obj, offset), value.As<CpuRegister>());
2214 } else {
2215 DCHECK(value.IsConstant()) << value;
2216 __ movl(Address(obj, offset),
2217 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2218 }
2219 } else {
2220 DCHECK(index.IsRegister()) << index;
2221 if (value.IsRegister()) {
2222 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2223 value.As<CpuRegister>());
2224 } else {
2225 DCHECK(value.IsConstant()) << value;
2226 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2227 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2228 }
2229 }
2230
2231 if (needs_write_barrier) {
2232 DCHECK_EQ(value_type, Primitive::kPrimNot);
2233 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
2234 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
2235 codegen_->MarkGCCard(temp, card, obj, value.As<CpuRegister>());
2236 }
2237 } else {
2238 DCHECK_EQ(value_type, Primitive::kPrimNot);
2239 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject), true));
2240 DCHECK(!codegen_->IsLeafMethod());
2241 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2242 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002243 break;
2244 }
2245
2246 case Primitive::kPrimLong: {
2247 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002248 if (index.IsConstant()) {
2249 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002250 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002251 __ movq(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002252 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002253 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002254 __ movq(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
2255 value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002256 }
2257 break;
2258 }
2259
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002260 case Primitive::kPrimFloat: {
2261 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2262 if (index.IsConstant()) {
2263 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2264 DCHECK(value.IsFpuRegister());
2265 __ movss(Address(obj, offset), value.As<XmmRegister>());
2266 } else {
2267 DCHECK(value.IsFpuRegister());
2268 __ movss(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2269 value.As<XmmRegister>());
2270 }
2271 break;
2272 }
2273
2274 case Primitive::kPrimDouble: {
2275 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2276 if (index.IsConstant()) {
2277 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
2278 DCHECK(value.IsFpuRegister());
2279 __ movsd(Address(obj, offset), value.As<XmmRegister>());
2280 } else {
2281 DCHECK(value.IsFpuRegister());
2282 __ movsd(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
2283 value.As<XmmRegister>());
2284 }
2285 break;
2286 }
2287
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002288 case Primitive::kPrimVoid:
2289 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002290 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002291 }
2292}
2293
2294void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002295 LocationSummary* locations =
2296 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002297 locations->SetInAt(0, Location::RequiresRegister());
2298 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002299}
2300
2301void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
2302 LocationSummary* locations = instruction->GetLocations();
2303 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002304 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2305 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002306 __ movl(out, Address(obj, offset));
2307}
2308
2309void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002310 LocationSummary* locations =
2311 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002312 locations->SetInAt(0, Location::RequiresRegister());
2313 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002314 if (instruction->HasUses()) {
2315 locations->SetOut(Location::SameAsFirstInput());
2316 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002317}
2318
2319void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
2320 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002321 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002322 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002323 codegen_->AddSlowPath(slow_path);
2324
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002325 CpuRegister index = locations->InAt(0).As<CpuRegister>();
2326 CpuRegister length = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002327
2328 __ cmpl(index, length);
2329 __ j(kAboveEqual, slow_path->GetEntryLabel());
2330}
2331
2332void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
2333 CpuRegister card,
2334 CpuRegister object,
2335 CpuRegister value) {
2336 Label is_null;
2337 __ testl(value, value);
2338 __ j(kEqual, &is_null);
2339 __ gs()->movq(card, Address::Absolute(
2340 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
2341 __ movq(temp, object);
2342 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
2343 __ movb(Address(temp, card, TIMES_1, 0), card);
2344 __ Bind(&is_null);
2345}
2346
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002347void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
2348 temp->SetLocations(nullptr);
2349}
2350
2351void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
2352 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002353 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002354}
2355
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002356void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002357 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002358 LOG(FATAL) << "Unimplemented";
2359}
2360
2361void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002362 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2363}
2364
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002365void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
2366 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2367}
2368
2369void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002370 HBasicBlock* block = instruction->GetBlock();
2371 if (block->GetLoopInformation() != nullptr) {
2372 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2373 // The back edge will generate the suspend check.
2374 return;
2375 }
2376 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2377 // The goto will generate the suspend check.
2378 return;
2379 }
2380 GenerateSuspendCheck(instruction, nullptr);
2381}
2382
2383void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
2384 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002385 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002386 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002387 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002388 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002389 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002390 if (successor == nullptr) {
2391 __ j(kNotEqual, slow_path->GetEntryLabel());
2392 __ Bind(slow_path->GetReturnLabel());
2393 } else {
2394 __ j(kEqual, codegen_->GetLabelOf(successor));
2395 __ jmp(slow_path->GetEntryLabel());
2396 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002397}
2398
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002399X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
2400 return codegen_->GetAssembler();
2401}
2402
2403void ParallelMoveResolverX86_64::EmitMove(size_t index) {
2404 MoveOperands* move = moves_.Get(index);
2405 Location source = move->GetSource();
2406 Location destination = move->GetDestination();
2407
2408 if (source.IsRegister()) {
2409 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002410 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002411 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002412 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002413 source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002414 } else {
2415 DCHECK(destination.IsDoubleStackSlot());
2416 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002417 source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002418 }
2419 } else if (source.IsStackSlot()) {
2420 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002421 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002422 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002423 } else if (destination.IsFpuRegister()) {
2424 __ movss(destination.As<XmmRegister>(),
2425 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002426 } else {
2427 DCHECK(destination.IsStackSlot());
2428 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2429 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2430 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002431 } else if (source.IsDoubleStackSlot()) {
2432 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002433 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002434 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002435 } else if (destination.IsFpuRegister()) {
2436 __ movsd(destination.As<XmmRegister>(), Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002437 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01002438 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002439 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2440 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2441 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002442 } else if (source.IsConstant()) {
2443 HConstant* constant = source.GetConstant();
2444 if (constant->IsIntConstant()) {
2445 Immediate imm(constant->AsIntConstant()->GetValue());
2446 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002447 __ movl(destination.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002448 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002449 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002450 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2451 }
2452 } else if (constant->IsLongConstant()) {
2453 int64_t value = constant->AsLongConstant()->GetValue();
2454 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002455 __ movq(destination.As<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002456 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002457 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002458 __ movq(CpuRegister(TMP), Immediate(value));
2459 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2460 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002461 } else if (constant->IsFloatConstant()) {
2462 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
2463 if (destination.IsFpuRegister()) {
2464 __ movl(CpuRegister(TMP), imm);
2465 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2466 } else {
2467 DCHECK(destination.IsStackSlot()) << destination;
2468 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2469 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002470 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002471 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
2472 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
2473 if (destination.IsFpuRegister()) {
2474 __ movq(CpuRegister(TMP), imm);
2475 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2476 } else {
2477 DCHECK(destination.IsDoubleStackSlot()) << destination;
2478 __ movq(CpuRegister(TMP), imm);
2479 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2480 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002481 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002482 } else if (source.IsFpuRegister()) {
2483 if (destination.IsFpuRegister()) {
2484 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
2485 } else if (destination.IsStackSlot()) {
2486 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
2487 source.As<XmmRegister>());
2488 } else {
2489 DCHECK(destination.IsDoubleStackSlot());
2490 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
2491 source.As<XmmRegister>());
2492 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002493 }
2494}
2495
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002496void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002497 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002498 __ movl(Address(CpuRegister(RSP), mem), reg);
2499 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002500}
2501
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002502void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002503 ScratchRegisterScope ensure_scratch(
2504 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2505
2506 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2507 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2508 __ movl(CpuRegister(ensure_scratch.GetRegister()),
2509 Address(CpuRegister(RSP), mem2 + stack_offset));
2510 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2511 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
2512 CpuRegister(ensure_scratch.GetRegister()));
2513}
2514
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002515void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
2516 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2517 __ movq(Address(CpuRegister(RSP), mem), reg);
2518 __ movq(reg, CpuRegister(TMP));
2519}
2520
2521void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
2522 ScratchRegisterScope ensure_scratch(
2523 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2524
2525 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2526 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2527 __ movq(CpuRegister(ensure_scratch.GetRegister()),
2528 Address(CpuRegister(RSP), mem2 + stack_offset));
2529 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2530 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
2531 CpuRegister(ensure_scratch.GetRegister()));
2532}
2533
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002534void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
2535 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2536 __ movss(Address(CpuRegister(RSP), mem), reg);
2537 __ movd(reg, CpuRegister(TMP));
2538}
2539
2540void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
2541 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2542 __ movsd(Address(CpuRegister(RSP), mem), reg);
2543 __ movd(reg, CpuRegister(TMP));
2544}
2545
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002546void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
2547 MoveOperands* move = moves_.Get(index);
2548 Location source = move->GetSource();
2549 Location destination = move->GetDestination();
2550
2551 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002552 __ xchgq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002553 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002554 Exchange32(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002555 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002556 Exchange32(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002557 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002558 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
2559 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002560 Exchange64(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002561 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002562 Exchange64(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002563 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
2564 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002565 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
2566 __ movd(CpuRegister(TMP), source.As<XmmRegister>());
2567 __ movaps(source.As<XmmRegister>(), destination.As<XmmRegister>());
2568 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2569 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
2570 Exchange32(source.As<XmmRegister>(), destination.GetStackIndex());
2571 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
2572 Exchange32(destination.As<XmmRegister>(), source.GetStackIndex());
2573 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
2574 Exchange64(source.As<XmmRegister>(), destination.GetStackIndex());
2575 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
2576 Exchange64(destination.As<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002577 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002578 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002579 }
2580}
2581
2582
2583void ParallelMoveResolverX86_64::SpillScratch(int reg) {
2584 __ pushq(CpuRegister(reg));
2585}
2586
2587
2588void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
2589 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002590}
2591
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002592void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
2593 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
2594 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2595 Immediate(mirror::Class::kStatusInitialized));
2596 __ j(kLess, slow_path->GetEntryLabel());
2597 __ Bind(slow_path->GetExitLabel());
2598 // No need for memory fence, thanks to the X86_64 memory model.
2599}
2600
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002601void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002602 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2603 ? LocationSummary::kCallOnSlowPath
2604 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002605 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002606 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002607 locations->SetOut(Location::RequiresRegister());
2608}
2609
2610void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
2611 CpuRegister out = cls->GetLocations()->Out().As<CpuRegister>();
2612 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002613 DCHECK(!cls->CanCallRuntime());
2614 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002615 codegen_->LoadCurrentMethod(out);
2616 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2617 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002618 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002619 codegen_->LoadCurrentMethod(out);
2620 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2621 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002622 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
2623 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2624 codegen_->AddSlowPath(slow_path);
2625 __ testl(out, out);
2626 __ j(kEqual, slow_path->GetEntryLabel());
2627 if (cls->MustGenerateClinitCheck()) {
2628 GenerateClassInitializationCheck(slow_path, out);
2629 } else {
2630 __ Bind(slow_path->GetExitLabel());
2631 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002632 }
2633}
2634
2635void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
2636 LocationSummary* locations =
2637 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2638 locations->SetInAt(0, Location::RequiresRegister());
2639 if (check->HasUses()) {
2640 locations->SetOut(Location::SameAsFirstInput());
2641 }
2642}
2643
2644void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002645 // We assume the class to not be null.
2646 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
2647 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002648 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002649 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002650}
2651
2652void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2653 LocationSummary* locations =
2654 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2655 locations->SetInAt(0, Location::RequiresRegister());
2656 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2657}
2658
2659void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2660 LocationSummary* locations = instruction->GetLocations();
2661 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002662 size_t offset = instruction->GetFieldOffset().SizeValue();
2663
2664 switch (instruction->GetType()) {
2665 case Primitive::kPrimBoolean: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002666 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002667 __ movzxb(out, Address(cls, offset));
2668 break;
2669 }
2670
2671 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002672 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002673 __ movsxb(out, Address(cls, offset));
2674 break;
2675 }
2676
2677 case Primitive::kPrimShort: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002678 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002679 __ movsxw(out, Address(cls, offset));
2680 break;
2681 }
2682
2683 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002684 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002685 __ movzxw(out, Address(cls, offset));
2686 break;
2687 }
2688
2689 case Primitive::kPrimInt:
2690 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002691 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002692 __ movl(out, Address(cls, offset));
2693 break;
2694 }
2695
2696 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002697 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002698 __ movq(out, Address(cls, offset));
2699 break;
2700 }
2701
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002702 case Primitive::kPrimFloat: {
2703 XmmRegister out = locations->Out().As<XmmRegister>();
2704 __ movss(out, Address(cls, offset));
2705 break;
2706 }
2707
2708 case Primitive::kPrimDouble: {
2709 XmmRegister out = locations->Out().As<XmmRegister>();
2710 __ movsd(out, Address(cls, offset));
2711 break;
2712 }
2713
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002714 case Primitive::kPrimVoid:
2715 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2716 UNREACHABLE();
2717 }
2718}
2719
2720void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2721 LocationSummary* locations =
2722 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2723 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002724 bool needs_write_barrier =
2725 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->GetValue());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002726 locations->SetInAt(0, Location::RequiresRegister());
2727 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002728 if (needs_write_barrier) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002729 // Temporary registers for the write barrier.
2730 locations->AddTemp(Location::RequiresRegister());
2731 locations->AddTemp(Location::RequiresRegister());
2732 }
2733}
2734
2735void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2736 LocationSummary* locations = instruction->GetLocations();
2737 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002738 size_t offset = instruction->GetFieldOffset().SizeValue();
2739 Primitive::Type field_type = instruction->GetFieldType();
2740
2741 switch (field_type) {
2742 case Primitive::kPrimBoolean:
2743 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002744 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002745 __ movb(Address(cls, offset), value);
2746 break;
2747 }
2748
2749 case Primitive::kPrimShort:
2750 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002751 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002752 __ movw(Address(cls, offset), value);
2753 break;
2754 }
2755
2756 case Primitive::kPrimInt:
2757 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002758 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002759 __ movl(Address(cls, offset), value);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002760 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->GetValue())) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002761 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
2762 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
2763 codegen_->MarkGCCard(temp, card, cls, value);
2764 }
2765 break;
2766 }
2767
2768 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002769 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002770 __ movq(Address(cls, offset), value);
2771 break;
2772 }
2773
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002774 case Primitive::kPrimFloat: {
2775 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2776 __ movss(Address(cls, offset), value);
2777 break;
2778 }
2779
2780 case Primitive::kPrimDouble: {
2781 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2782 __ movsd(Address(cls, offset), value);
2783 break;
2784 }
2785
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002786 case Primitive::kPrimVoid:
2787 LOG(FATAL) << "Unreachable type " << field_type;
2788 UNREACHABLE();
2789 }
2790}
2791
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002792void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
2793 LocationSummary* locations =
2794 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2795 locations->SetOut(Location::RequiresRegister());
2796}
2797
2798void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
2799 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
2800 codegen_->AddSlowPath(slow_path);
2801
2802 CpuRegister out = load->GetLocations()->Out().As<CpuRegister>();
2803 codegen_->LoadCurrentMethod(CpuRegister(out));
2804 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
2805 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2806 __ testl(out, out);
2807 __ j(kEqual, slow_path->GetEntryLabel());
2808 __ Bind(slow_path->GetExitLabel());
2809}
2810
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002811void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
2812 LocationSummary* locations =
2813 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2814 locations->SetOut(Location::RequiresRegister());
2815}
2816
2817void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
2818 Address address = Address::Absolute(
2819 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
2820 __ gs()->movl(load->GetLocations()->Out().As<CpuRegister>(), address);
2821 __ gs()->movl(address, Immediate(0));
2822}
2823
2824void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
2825 LocationSummary* locations =
2826 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2827 InvokeRuntimeCallingConvention calling_convention;
2828 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2829}
2830
2831void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
2832 __ gs()->call(
2833 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
2834 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2835}
2836
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002837void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002838 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
2839 ? LocationSummary::kNoCall
2840 : LocationSummary::kCallOnSlowPath;
2841 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2842 locations->SetInAt(0, Location::RequiresRegister());
2843 locations->SetInAt(1, Location::Any());
2844 locations->SetOut(Location::RequiresRegister());
2845}
2846
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002847void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002848 LocationSummary* locations = instruction->GetLocations();
2849 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2850 Location cls = locations->InAt(1);
2851 CpuRegister out = locations->Out().As<CpuRegister>();
2852 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2853 Label done, zero;
2854 SlowPathCodeX86_64* slow_path = nullptr;
2855
2856 // Return 0 if `obj` is null.
2857 // TODO: avoid this check if we know obj is not null.
2858 __ testl(obj, obj);
2859 __ j(kEqual, &zero);
2860 // Compare the class of `obj` with `cls`.
2861 __ movl(out, Address(obj, class_offset));
2862 if (cls.IsRegister()) {
2863 __ cmpl(out, cls.As<CpuRegister>());
2864 } else {
2865 DCHECK(cls.IsStackSlot()) << cls;
2866 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
2867 }
2868 if (instruction->IsClassFinal()) {
2869 // Classes must be equal for the instanceof to succeed.
2870 __ j(kNotEqual, &zero);
2871 __ movl(out, Immediate(1));
2872 __ jmp(&done);
2873 } else {
2874 // If the classes are not equal, we go into a slow path.
2875 DCHECK(locations->OnlyCallsOnSlowPath());
2876 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002877 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002878 codegen_->AddSlowPath(slow_path);
2879 __ j(kNotEqual, slow_path->GetEntryLabel());
2880 __ movl(out, Immediate(1));
2881 __ jmp(&done);
2882 }
2883 __ Bind(&zero);
2884 __ movl(out, Immediate(0));
2885 if (slow_path != nullptr) {
2886 __ Bind(slow_path->GetExitLabel());
2887 }
2888 __ Bind(&done);
2889}
2890
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002891void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
2892 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2893 instruction, LocationSummary::kCallOnSlowPath);
2894 locations->SetInAt(0, Location::RequiresRegister());
2895 locations->SetInAt(1, Location::Any());
2896 locations->AddTemp(Location::RequiresRegister());
2897}
2898
2899void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
2900 LocationSummary* locations = instruction->GetLocations();
2901 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2902 Location cls = locations->InAt(1);
2903 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
2904 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2905 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
2906 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
2907 codegen_->AddSlowPath(slow_path);
2908
2909 // TODO: avoid this check if we know obj is not null.
2910 __ testl(obj, obj);
2911 __ j(kEqual, slow_path->GetExitLabel());
2912 // Compare the class of `obj` with `cls`.
2913 __ movl(temp, Address(obj, class_offset));
2914 if (cls.IsRegister()) {
2915 __ cmpl(temp, cls.As<CpuRegister>());
2916 } else {
2917 DCHECK(cls.IsStackSlot()) << cls;
2918 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
2919 }
2920 // Classes must be equal for the checkcast to succeed.
2921 __ j(kNotEqual, slow_path->GetEntryLabel());
2922 __ Bind(slow_path->GetExitLabel());
2923}
2924
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002925void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
2926 LocationSummary* locations =
2927 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2928 InvokeRuntimeCallingConvention calling_convention;
2929 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2930}
2931
2932void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
2933 __ gs()->call(Address::Absolute(instruction->IsEnter()
2934 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
2935 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
2936 true));
2937 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2938}
2939
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002940void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
2941void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
2942void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
2943
2944void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
2945 LocationSummary* locations =
2946 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2947 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
2948 || instruction->GetResultType() == Primitive::kPrimLong);
2949 locations->SetInAt(0, Location::RequiresRegister());
2950 if (instruction->GetType() == Primitive::kPrimInt) {
2951 locations->SetInAt(1, Location::Any());
2952 } else {
2953 // Request a register to avoid loading a 64bits constant.
2954 locations->SetInAt(1, Location::RequiresRegister());
2955 }
2956 locations->SetOut(Location::SameAsFirstInput());
2957}
2958
2959void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
2960 HandleBitwiseOperation(instruction);
2961}
2962
2963void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
2964 HandleBitwiseOperation(instruction);
2965}
2966
2967void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
2968 HandleBitwiseOperation(instruction);
2969}
2970
2971void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
2972 LocationSummary* locations = instruction->GetLocations();
2973 Location first = locations->InAt(0);
2974 Location second = locations->InAt(1);
2975 DCHECK(first.Equals(locations->Out()));
2976
2977 if (instruction->GetResultType() == Primitive::kPrimInt) {
2978 if (second.IsRegister()) {
2979 if (instruction->IsAnd()) {
2980 __ andl(first.As<CpuRegister>(), second.As<CpuRegister>());
2981 } else if (instruction->IsOr()) {
2982 __ orl(first.As<CpuRegister>(), second.As<CpuRegister>());
2983 } else {
2984 DCHECK(instruction->IsXor());
2985 __ xorl(first.As<CpuRegister>(), second.As<CpuRegister>());
2986 }
2987 } else if (second.IsConstant()) {
2988 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
2989 if (instruction->IsAnd()) {
2990 __ andl(first.As<CpuRegister>(), imm);
2991 } else if (instruction->IsOr()) {
2992 __ orl(first.As<CpuRegister>(), imm);
2993 } else {
2994 DCHECK(instruction->IsXor());
2995 __ xorl(first.As<CpuRegister>(), imm);
2996 }
2997 } else {
2998 Address address(CpuRegister(RSP), second.GetStackIndex());
2999 if (instruction->IsAnd()) {
3000 __ andl(first.As<CpuRegister>(), address);
3001 } else if (instruction->IsOr()) {
3002 __ orl(first.As<CpuRegister>(), address);
3003 } else {
3004 DCHECK(instruction->IsXor());
3005 __ xorl(first.As<CpuRegister>(), address);
3006 }
3007 }
3008 } else {
3009 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3010 if (instruction->IsAnd()) {
3011 __ andq(first.As<CpuRegister>(), second.As<CpuRegister>());
3012 } else if (instruction->IsOr()) {
3013 __ orq(first.As<CpuRegister>(), second.As<CpuRegister>());
3014 } else {
3015 DCHECK(instruction->IsXor());
3016 __ xorq(first.As<CpuRegister>(), second.As<CpuRegister>());
3017 }
3018 }
3019}
3020
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003021} // namespace x86_64
3022} // namespace art