blob: bd1e4f4f040aef608ea3968395141dec1870907e [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 Geoffray92a73ae2014-10-16 11:12:52 +0100182 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100183 __ Bind(GetEntryLabel());
184 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100185 x64_codegen->Move(
186 Location::RegisterLocation(calling_convention.GetRegisterAt(0)), index_location_);
187 x64_codegen->Move(
188 Location::RegisterLocation(calling_convention.GetRegisterAt(1)), length_location_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100189 __ gs()->call(Address::Absolute(
190 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowArrayBounds), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100191 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100192 }
193
194 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100195 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100196 const Location index_location_;
197 const Location length_location_;
198
199 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
200};
201
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000202class LoadClassSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100203 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000204 LoadClassSlowPathX86_64(HLoadClass* cls,
205 HInstruction* at,
206 uint32_t dex_pc,
207 bool do_clinit)
208 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
209 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
210 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100211
212 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000213 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100214 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
215 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100216
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000217 codegen->SaveLiveRegisters(locations);
218
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100219 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000220 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100221 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000222 __ gs()->call(Address::Absolute((do_clinit_
223 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeStaticStorage)
224 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeType)) , true));
225 codegen->RecordPcInfo(at_, dex_pc_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100226
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000227 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000228 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000229 if (out.IsValid()) {
230 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
231 x64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000232 }
233
234 codegen->RestoreLiveRegisters(locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100235 __ jmp(GetExitLabel());
236 }
237
238 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000239 // The class this slow path will load.
240 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100241
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000242 // The instruction where this slow path is happening.
243 // (Might be the load class or an initialization check).
244 HInstruction* const at_;
245
246 // The dex PC of `at_`.
247 const uint32_t dex_pc_;
248
249 // Whether to initialize the class.
250 const bool do_clinit_;
251
252 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100253};
254
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000255class LoadStringSlowPathX86_64 : public SlowPathCodeX86_64 {
256 public:
257 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
258
259 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
260 LocationSummary* locations = instruction_->GetLocations();
261 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
262
263 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
264 __ Bind(GetEntryLabel());
265 codegen->SaveLiveRegisters(locations);
266
267 InvokeRuntimeCallingConvention calling_convention;
268 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(0)));
269 __ movl(CpuRegister(calling_convention.GetRegisterAt(1)),
270 Immediate(instruction_->GetStringIndex()));
271 __ gs()->call(Address::Absolute(
272 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pResolveString), true));
273 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
274 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
275 codegen->RestoreLiveRegisters(locations);
276 __ jmp(GetExitLabel());
277 }
278
279 private:
280 HLoadString* const instruction_;
281
282 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
283};
284
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000285class TypeCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
286 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000287 TypeCheckSlowPathX86_64(HInstruction* instruction,
288 Location class_to_check,
289 Location object_class,
290 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000291 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000292 class_to_check_(class_to_check),
293 object_class_(object_class),
294 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000295
296 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
297 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000298 DCHECK(instruction_->IsCheckCast()
299 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000300
301 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
302 __ Bind(GetEntryLabel());
303 codegen->SaveLiveRegisters(locations);
304
305 // We're moving two locations to locations that could overlap, so we need a parallel
306 // move resolver.
307 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000308 MoveOperands move1(class_to_check_,
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000309 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
310 nullptr);
311 MoveOperands move2(object_class_,
312 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
313 nullptr);
314 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
315 parallel_move.AddMove(&move1);
316 parallel_move.AddMove(&move2);
317 x64_codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
318
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000319 if (instruction_->IsInstanceOf()) {
320 __ gs()->call(
321 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInstanceofNonTrivial), true));
322 } else {
323 DCHECK(instruction_->IsCheckCast());
324 __ gs()->call(
325 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pCheckCast), true));
326 }
327 codegen->RecordPcInfo(instruction_, dex_pc_);
328
329 if (instruction_->IsInstanceOf()) {
330 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
331 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000332
333 codegen->RestoreLiveRegisters(locations);
334 __ jmp(GetExitLabel());
335 }
336
337 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000338 HInstruction* const instruction_;
339 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000340 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000341 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000342
343 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
344};
345
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100346#undef __
347#define __ reinterpret_cast<X86_64Assembler*>(GetAssembler())->
348
Dave Allison20dfc792014-06-16 20:44:29 -0700349inline Condition X86_64Condition(IfCondition cond) {
350 switch (cond) {
351 case kCondEQ: return kEqual;
352 case kCondNE: return kNotEqual;
353 case kCondLT: return kLess;
354 case kCondLE: return kLessEqual;
355 case kCondGT: return kGreater;
356 case kCondGE: return kGreaterEqual;
357 default:
358 LOG(FATAL) << "Unknown if condition";
359 }
360 return kEqual;
361}
362
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100363void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
364 stream << X86_64ManagedRegister::FromCpuRegister(Register(reg));
365}
366
367void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
368 stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg));
369}
370
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100371size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
372 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
373 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100374}
375
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100376size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
377 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
378 return kX86_64WordSize;
379}
380
381size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
382 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
383 return kX86_64WordSize;
384}
385
386size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
387 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
388 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100389}
390
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100391CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100392 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfFloatRegisters, 0),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100393 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100394 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000395 instruction_visitor_(graph, this),
396 move_resolver_(graph->GetArena(), this) {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100397
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100398size_t CodeGeneratorX86_64::FrameEntrySpillSize() const {
399 return kNumberOfPushedRegistersAtEntry * kX86_64WordSize;
400}
401
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100402InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
403 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100404 : HGraphVisitor(graph),
405 assembler_(codegen->GetAssembler()),
406 codegen_(codegen) {}
407
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100408Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100409 switch (type) {
410 case Primitive::kPrimLong:
411 case Primitive::kPrimByte:
412 case Primitive::kPrimBoolean:
413 case Primitive::kPrimChar:
414 case Primitive::kPrimShort:
415 case Primitive::kPrimInt:
416 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100417 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100418 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100419 }
420
421 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100422 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100423 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100424 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100425 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100426
427 case Primitive::kPrimVoid:
428 LOG(FATAL) << "Unreachable type " << type;
429 }
430
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100431 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100432}
433
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100434void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100435 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100436 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100437
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000438 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100439 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000440
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100441 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100442 blocked_core_registers_[RBX] = true;
443 blocked_core_registers_[RBP] = true;
444 blocked_core_registers_[R12] = true;
445 blocked_core_registers_[R13] = true;
446 blocked_core_registers_[R14] = true;
447 blocked_core_registers_[R15] = true;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100448
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100449 blocked_fpu_registers_[XMM12] = true;
450 blocked_fpu_registers_[XMM13] = true;
451 blocked_fpu_registers_[XMM14] = true;
452 blocked_fpu_registers_[XMM15] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100453}
454
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100455void CodeGeneratorX86_64::GenerateFrameEntry() {
456 // Create a fake register to mimic Quick.
457 static const int kFakeReturnRegister = 16;
458 core_spill_mask_ |= (1 << kFakeReturnRegister);
459
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100460 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700461 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100462
463 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
464 __ testq(CpuRegister(RAX), Address(
465 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100466 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100467 }
468
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100469 // The return PC has already been pushed on the stack.
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100470 __ subq(CpuRegister(RSP),
471 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
472
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100473 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100474 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86_64();
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100475 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100476
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100477 __ gs()->cmpq(CpuRegister(RSP),
478 Address::Absolute(Thread::StackEndOffset<kX86_64WordSize>(), true));
479 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100480 }
481
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100482 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
483}
484
485void CodeGeneratorX86_64::GenerateFrameExit() {
486 __ addq(CpuRegister(RSP),
487 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
488}
489
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100490void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
491 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100492}
493
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100494void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100495 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
496}
497
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100498Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
499 switch (load->GetType()) {
500 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100501 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100502 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
503 break;
504
505 case Primitive::kPrimInt:
506 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100507 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100508 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100509
510 case Primitive::kPrimBoolean:
511 case Primitive::kPrimByte:
512 case Primitive::kPrimChar:
513 case Primitive::kPrimShort:
514 case Primitive::kPrimVoid:
515 LOG(FATAL) << "Unexpected type " << load->GetType();
516 }
517
518 LOG(FATAL) << "Unreachable";
519 return Location();
520}
521
522void CodeGeneratorX86_64::Move(Location destination, Location source) {
523 if (source.Equals(destination)) {
524 return;
525 }
526 if (destination.IsRegister()) {
527 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100528 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100529 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100530 __ movd(destination.As<CpuRegister>(), source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100531 } else if (source.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100532 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100533 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100534 } else {
535 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100536 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100537 Address(CpuRegister(RSP), source.GetStackIndex()));
538 }
539 } else if (destination.IsFpuRegister()) {
540 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100541 __ movd(destination.As<XmmRegister>(), source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100542 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100543 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100544 } else if (source.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100545 __ movss(destination.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100546 Address(CpuRegister(RSP), source.GetStackIndex()));
547 } else {
548 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100549 __ movsd(destination.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100550 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100551 }
552 } else if (destination.IsStackSlot()) {
553 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100554 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100555 source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100556 } else if (source.IsFpuRegister()) {
557 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100558 source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100559 } else {
560 DCHECK(source.IsStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000561 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
562 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100563 }
564 } else {
565 DCHECK(destination.IsDoubleStackSlot());
566 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100567 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100568 source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100569 } else if (source.IsFpuRegister()) {
570 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100571 source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100572 } else {
573 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000574 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
575 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100576 }
577 }
578}
579
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100580void CodeGeneratorX86_64::Move(HInstruction* instruction,
581 Location location,
582 HInstruction* move_for) {
Roland Levillain476df552014-10-09 17:51:36 +0100583 if (instruction->IsIntConstant()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100584 Immediate imm(instruction->AsIntConstant()->GetValue());
585 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100586 __ movl(location.As<CpuRegister>(), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100587 } else if (location.IsStackSlot()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100588 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100589 } else {
590 DCHECK(location.IsConstant());
591 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100592 }
Roland Levillain476df552014-10-09 17:51:36 +0100593 } else if (instruction->IsLongConstant()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100594 int64_t value = instruction->AsLongConstant()->GetValue();
595 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100596 __ movq(location.As<CpuRegister>(), Immediate(value));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100597 } else if (location.IsDoubleStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000598 __ movq(CpuRegister(TMP), Immediate(value));
599 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100600 } else {
601 DCHECK(location.IsConstant());
602 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100603 }
Roland Levillain476df552014-10-09 17:51:36 +0100604 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100605 switch (instruction->GetType()) {
606 case Primitive::kPrimBoolean:
607 case Primitive::kPrimByte:
608 case Primitive::kPrimChar:
609 case Primitive::kPrimShort:
610 case Primitive::kPrimInt:
611 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100612 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100613 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
614 break;
615
616 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100617 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100618 Move(location, Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
619 break;
620
621 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100622 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100623 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000624 } else if (instruction->IsTemporary()) {
625 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
626 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100627 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100628 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100629 switch (instruction->GetType()) {
630 case Primitive::kPrimBoolean:
631 case Primitive::kPrimByte:
632 case Primitive::kPrimChar:
633 case Primitive::kPrimShort:
634 case Primitive::kPrimInt:
635 case Primitive::kPrimNot:
636 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100637 case Primitive::kPrimFloat:
638 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100639 Move(location, instruction->GetLocations()->Out());
640 break;
641
642 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100643 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100644 }
645 }
646}
647
648void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
649 got->SetLocations(nullptr);
650}
651
652void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
653 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100654 DCHECK(!successor->IsExitBlock());
655
656 HBasicBlock* block = got->GetBlock();
657 HInstruction* previous = got->GetPrevious();
658
659 HLoopInformation* info = block->GetLoopInformation();
660 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
661 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
662 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
663 return;
664 }
665
666 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
667 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
668 }
669 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100670 __ jmp(codegen_->GetLabelOf(successor));
671 }
672}
673
674void LocationsBuilderX86_64::VisitExit(HExit* exit) {
675 exit->SetLocations(nullptr);
676}
677
678void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700679 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100680 if (kIsDebugBuild) {
681 __ Comment("Unreachable");
682 __ int3();
683 }
684}
685
686void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100687 LocationSummary* locations =
688 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100689 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100690 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100691 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100692 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100693}
694
695void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700696 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100697 if (cond->IsIntConstant()) {
698 // Constant condition, statically compared against 1.
699 int32_t cond_value = cond->AsIntConstant()->GetValue();
700 if (cond_value == 1) {
701 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
702 if_instr->IfTrueSuccessor())) {
703 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100704 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100705 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100706 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100707 DCHECK_EQ(cond_value, 0);
708 }
709 } else {
710 bool materialized =
711 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
712 // Moves do not affect the eflags register, so if the condition is
713 // evaluated just before the if, we don't need to evaluate it
714 // again.
715 bool eflags_set = cond->IsCondition()
716 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
717 if (materialized) {
718 if (!eflags_set) {
719 // Materialized condition, compare against 0.
720 Location lhs = if_instr->GetLocations()->InAt(0);
721 if (lhs.IsRegister()) {
722 __ cmpl(lhs.As<CpuRegister>(), Immediate(0));
723 } else {
724 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
725 Immediate(0));
726 }
727 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
728 } else {
729 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
730 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
731 }
732 } else {
733 Location lhs = cond->GetLocations()->InAt(0);
734 Location rhs = cond->GetLocations()->InAt(1);
735 if (rhs.IsRegister()) {
736 __ cmpl(lhs.As<CpuRegister>(), rhs.As<CpuRegister>());
737 } else if (rhs.IsConstant()) {
738 __ cmpl(lhs.As<CpuRegister>(),
739 Immediate(rhs.GetConstant()->AsIntConstant()->GetValue()));
740 } else {
741 __ cmpl(lhs.As<CpuRegister>(),
742 Address(CpuRegister(RSP), rhs.GetStackIndex()));
743 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100744 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
745 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700746 }
Dave Allison20dfc792014-06-16 20:44:29 -0700747 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100748 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
749 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700750 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100751 }
752}
753
754void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
755 local->SetLocations(nullptr);
756}
757
758void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
759 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
760}
761
762void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
763 local->SetLocations(nullptr);
764}
765
766void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
767 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700768 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100769}
770
771void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100772 LocationSummary* locations =
773 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100774 switch (store->InputAt(1)->GetType()) {
775 case Primitive::kPrimBoolean:
776 case Primitive::kPrimByte:
777 case Primitive::kPrimChar:
778 case Primitive::kPrimShort:
779 case Primitive::kPrimInt:
780 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100781 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100782 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
783 break;
784
785 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100786 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100787 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
788 break;
789
790 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100791 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100792 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100793}
794
795void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700796 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100797}
798
Dave Allison20dfc792014-06-16 20:44:29 -0700799void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100800 LocationSummary* locations =
801 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100802 locations->SetInAt(0, Location::RequiresRegister());
803 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100804 if (comp->NeedsMaterialization()) {
805 locations->SetOut(Location::RequiresRegister());
806 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100807}
808
Dave Allison20dfc792014-06-16 20:44:29 -0700809void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
810 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100811 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100812 CpuRegister reg = locations->Out().As<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100813 // Clear register: setcc only sets the low byte.
814 __ xorq(reg, reg);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100815 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100816 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100817 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100818 } else if (locations->InAt(1).IsConstant()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100819 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100820 Immediate(locations->InAt(1).GetConstant()->AsIntConstant()->GetValue()));
821 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100822 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100823 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
824 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100825 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700826 }
827}
828
829void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
830 VisitCondition(comp);
831}
832
833void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
834 VisitCondition(comp);
835}
836
837void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
838 VisitCondition(comp);
839}
840
841void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
842 VisitCondition(comp);
843}
844
845void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
846 VisitCondition(comp);
847}
848
849void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
850 VisitCondition(comp);
851}
852
853void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
854 VisitCondition(comp);
855}
856
857void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
858 VisitCondition(comp);
859}
860
861void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
862 VisitCondition(comp);
863}
864
865void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
866 VisitCondition(comp);
867}
868
869void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
870 VisitCondition(comp);
871}
872
873void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
874 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100875}
876
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100877void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100878 LocationSummary* locations =
879 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100880 locations->SetInAt(0, Location::RequiresRegister());
881 locations->SetInAt(1, Location::RequiresRegister());
882 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100883}
884
885void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
886 Label greater, done;
887 LocationSummary* locations = compare->GetLocations();
888 switch (compare->InputAt(0)->GetType()) {
889 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100890 __ cmpq(locations->InAt(0).As<CpuRegister>(),
891 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100892 break;
893 default:
894 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
895 }
896
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100897 CpuRegister output = locations->Out().As<CpuRegister>();
898 __ movl(output, Immediate(0));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100899 __ j(kEqual, &done);
900 __ j(kGreater, &greater);
901
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100902 __ movl(output, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100903 __ jmp(&done);
904
905 __ Bind(&greater);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100906 __ movl(output, Immediate(1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100907
908 __ Bind(&done);
909}
910
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100911void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100912 LocationSummary* locations =
913 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100914 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100915}
916
917void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100918 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700919 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100920}
921
922void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100923 LocationSummary* locations =
924 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100925 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100926}
927
928void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100929 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700930 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100931}
932
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100933void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
934 LocationSummary* locations =
935 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
936 locations->SetOut(Location::ConstantLocation(constant));
937}
938
939void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
940 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700941 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100942}
943
944void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
945 LocationSummary* locations =
946 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
947 locations->SetOut(Location::ConstantLocation(constant));
948}
949
950void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
951 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700952 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100953}
954
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100955void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
956 ret->SetLocations(nullptr);
957}
958
959void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700960 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100961 codegen_->GenerateFrameExit();
962 __ ret();
963}
964
965void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100966 LocationSummary* locations =
967 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100968 switch (ret->InputAt(0)->GetType()) {
969 case Primitive::kPrimBoolean:
970 case Primitive::kPrimByte:
971 case Primitive::kPrimChar:
972 case Primitive::kPrimShort:
973 case Primitive::kPrimInt:
974 case Primitive::kPrimNot:
975 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100976 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100977 break;
978
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100979 case Primitive::kPrimFloat:
980 case Primitive::kPrimDouble:
981 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100982 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100983 break;
984
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100985 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100986 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100987 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100988}
989
990void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
991 if (kIsDebugBuild) {
992 switch (ret->InputAt(0)->GetType()) {
993 case Primitive::kPrimBoolean:
994 case Primitive::kPrimByte:
995 case Primitive::kPrimChar:
996 case Primitive::kPrimShort:
997 case Primitive::kPrimInt:
998 case Primitive::kPrimNot:
999 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001000 DCHECK_EQ(ret->GetLocations()->InAt(0).As<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001001 break;
1002
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001003 case Primitive::kPrimFloat:
1004 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001005 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001006 XMM0);
1007 break;
1008
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001009 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001010 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001011 }
1012 }
1013 codegen_->GenerateFrameExit();
1014 __ ret();
1015}
1016
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001017Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
1018 switch (type) {
1019 case Primitive::kPrimBoolean:
1020 case Primitive::kPrimByte:
1021 case Primitive::kPrimChar:
1022 case Primitive::kPrimShort:
1023 case Primitive::kPrimInt:
1024 case Primitive::kPrimNot: {
1025 uint32_t index = gp_index_++;
1026 stack_index_++;
1027 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001028 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001029 } else {
1030 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1031 }
1032 }
1033
1034 case Primitive::kPrimLong: {
1035 uint32_t index = gp_index_;
1036 stack_index_ += 2;
1037 if (index < calling_convention.GetNumberOfRegisters()) {
1038 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001039 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001040 } else {
1041 gp_index_ += 2;
1042 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1043 }
1044 }
1045
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001046 case Primitive::kPrimFloat: {
1047 uint32_t index = fp_index_++;
1048 stack_index_++;
1049 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001050 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001051 } else {
1052 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1053 }
1054 }
1055
1056 case Primitive::kPrimDouble: {
1057 uint32_t index = fp_index_++;
1058 stack_index_ += 2;
1059 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001060 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001061 } else {
1062 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1063 }
1064 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001065
1066 case Primitive::kPrimVoid:
1067 LOG(FATAL) << "Unexpected parameter type " << type;
1068 break;
1069 }
1070 return Location();
1071}
1072
1073void LocationsBuilderX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001074 HandleInvoke(invoke);
1075}
1076
1077void InstructionCodeGeneratorX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001078 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001079 // TODO: Implement all kinds of calls:
1080 // 1) boot -> boot
1081 // 2) app -> boot
1082 // 3) app -> app
1083 //
1084 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1085
1086 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001087 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001088 // temp = temp->dex_cache_resolved_methods_;
1089 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
1090 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001091 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001092 // (temp + offset_of_quick_compiled_code)()
1093 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
1094
1095 DCHECK(!codegen_->IsLeafMethod());
1096 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1097}
1098
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001099void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001100 LocationSummary* locations =
1101 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001102 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001103
1104 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001105 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001106 HInstruction* input = invoke->InputAt(i);
1107 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1108 }
1109
1110 switch (invoke->GetType()) {
1111 case Primitive::kPrimBoolean:
1112 case Primitive::kPrimByte:
1113 case Primitive::kPrimChar:
1114 case Primitive::kPrimShort:
1115 case Primitive::kPrimInt:
1116 case Primitive::kPrimNot:
1117 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001118 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001119 break;
1120
1121 case Primitive::kPrimVoid:
1122 break;
1123
1124 case Primitive::kPrimDouble:
1125 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001126 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001127 break;
1128 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001129}
1130
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001131void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1132 HandleInvoke(invoke);
1133}
1134
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001135void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001136 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001137 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1138 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1139 LocationSummary* locations = invoke->GetLocations();
1140 Location receiver = locations->InAt(0);
1141 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1142 // temp = object->GetClass();
1143 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001144 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1145 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001146 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001147 __ movl(temp, Address(receiver.As<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001148 }
1149 // temp = temp->GetMethodAt(method_offset);
1150 __ movl(temp, Address(temp, method_offset));
1151 // call temp->GetEntryPoint();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001152 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
1153
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001154 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001155 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001156}
1157
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001158void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1159 HandleInvoke(invoke);
1160 // Add the hidden argument.
1161 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1162}
1163
1164void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1165 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1166 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
1167 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1168 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1169 LocationSummary* locations = invoke->GetLocations();
1170 Location receiver = locations->InAt(0);
1171 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1172
1173 // Set the hidden argument.
1174 __ movq(invoke->GetLocations()->GetTemp(1).As<CpuRegister>(),
1175 Immediate(invoke->GetDexMethodIndex()));
1176
1177 // temp = object->GetClass();
1178 if (receiver.IsStackSlot()) {
1179 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1180 __ movl(temp, Address(temp, class_offset));
1181 } else {
1182 __ movl(temp, Address(receiver.As<CpuRegister>(), class_offset));
1183 }
1184 // temp = temp->GetImtEntryAt(method_offset);
1185 __ movl(temp, Address(temp, method_offset));
1186 // call temp->GetEntryPoint();
1187 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
1188
1189 DCHECK(!codegen_->IsLeafMethod());
1190 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1191}
1192
Roland Levillain88cb1752014-10-20 16:36:47 +01001193void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1194 LocationSummary* locations =
1195 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1196 switch (neg->GetResultType()) {
1197 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001198 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001199 locations->SetInAt(0, Location::RequiresRegister());
1200 locations->SetOut(Location::SameAsFirstInput());
1201 break;
1202
Roland Levillain88cb1752014-10-20 16:36:47 +01001203 case Primitive::kPrimFloat:
1204 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001205 locations->SetInAt(0, Location::RequiresFpuRegister());
1206 // Output overlaps as we need a fresh (zero-initialized)
1207 // register to perform subtraction from zero.
1208 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001209 break;
1210
1211 default:
1212 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1213 }
1214}
1215
1216void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1217 LocationSummary* locations = neg->GetLocations();
1218 Location out = locations->Out();
1219 Location in = locations->InAt(0);
1220 switch (neg->GetResultType()) {
1221 case Primitive::kPrimInt:
1222 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001223 DCHECK(in.Equals(out));
Roland Levillain88cb1752014-10-20 16:36:47 +01001224 __ negl(out.As<CpuRegister>());
1225 break;
1226
1227 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001228 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001229 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001230 __ negq(out.As<CpuRegister>());
1231 break;
1232
Roland Levillain88cb1752014-10-20 16:36:47 +01001233 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001234 DCHECK(in.IsFpuRegister());
1235 DCHECK(out.IsFpuRegister());
1236 DCHECK(!in.Equals(out));
1237 // TODO: Instead of computing negation as a subtraction from
1238 // zero, implement it with an exclusive or with value 0x80000000
1239 // (mask for bit 31, representing the sign of a single-precision
1240 // floating-point number), fetched from a constant pool:
1241 //
1242 // xorps out, [RIP:...] // value at RIP is 0x80 00 00 00
1243
1244 // out = 0
1245 __ xorps(out.As<XmmRegister>(), out.As<XmmRegister>());
1246 // out = out - in
1247 __ subss(out.As<XmmRegister>(), in.As<XmmRegister>());
1248 break;
1249
Roland Levillain88cb1752014-10-20 16:36:47 +01001250 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001251 DCHECK(in.IsFpuRegister());
1252 DCHECK(out.IsFpuRegister());
1253 DCHECK(!in.Equals(out));
1254 // TODO: Instead of computing negation as a subtraction from
1255 // zero, implement it with an exclusive or with value
1256 // 0x8000000000000000 (mask for bit 63, representing the sign of
1257 // a double-precision floating-point number), fetched from a
1258 // constant pool:
1259 //
1260 // xorpd out, [RIP:...] // value at RIP is 0x80 00 00 00 00 00 00 00
1261
1262 // out = 0
1263 __ xorpd(out.As<XmmRegister>(), out.As<XmmRegister>());
1264 // out = out - in
1265 __ subsd(out.As<XmmRegister>(), in.As<XmmRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001266 break;
1267
1268 default:
1269 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1270 }
1271}
1272
Roland Levillaindff1f282014-11-05 14:15:05 +00001273void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1274 LocationSummary* locations =
1275 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1276 Primitive::Type result_type = conversion->GetResultType();
1277 Primitive::Type input_type = conversion->GetInputType();
1278 switch (result_type) {
1279 case Primitive::kPrimLong:
1280 switch (input_type) {
1281 case Primitive::kPrimByte:
1282 case Primitive::kPrimShort:
1283 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001284 case Primitive::kPrimChar:
Roland Levillaindff1f282014-11-05 14:15:05 +00001285 // int-to-long conversion.
1286 // TODO: We would benefit from a (to-be-implemented)
1287 // Location::RegisterOrStackSlot requirement for this input.
1288 locations->SetInAt(0, Location::RequiresRegister());
1289 locations->SetOut(Location::RequiresRegister());
1290 break;
1291
1292 case Primitive::kPrimFloat:
1293 case Primitive::kPrimDouble:
1294 LOG(FATAL) << "Type conversion from " << input_type << " to "
1295 << result_type << " not yet implemented";
1296 break;
1297
1298 default:
1299 LOG(FATAL) << "Unexpected type conversion from " << input_type
1300 << " to " << result_type;
1301 }
1302 break;
1303
Roland Levillain3adfd1b2014-11-11 14:48:08 +00001304 case Primitive::kPrimInt:
Roland Levillaindff1f282014-11-05 14:15:05 +00001305 case Primitive::kPrimFloat:
1306 case Primitive::kPrimDouble:
1307 LOG(FATAL) << "Type conversion from " << input_type
1308 << " to " << result_type << " not yet implemented";
1309 break;
1310
1311 default:
1312 LOG(FATAL) << "Unexpected type conversion from " << input_type
1313 << " to " << result_type;
1314 }
1315}
1316
1317void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1318 LocationSummary* locations = conversion->GetLocations();
1319 Location out = locations->Out();
1320 Location in = locations->InAt(0);
1321 Primitive::Type result_type = conversion->GetResultType();
1322 Primitive::Type input_type = conversion->GetInputType();
1323 switch (result_type) {
1324 case Primitive::kPrimLong:
1325 switch (input_type) {
1326 DCHECK(out.IsRegister());
1327 case Primitive::kPrimByte:
1328 case Primitive::kPrimShort:
1329 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001330 case Primitive::kPrimChar:
Roland Levillaindff1f282014-11-05 14:15:05 +00001331 // int-to-long conversion.
1332 DCHECK(in.IsRegister());
1333 __ movsxd(out.As<CpuRegister>(), in.As<CpuRegister>());
1334 break;
1335
1336 case Primitive::kPrimFloat:
1337 case Primitive::kPrimDouble:
1338 LOG(FATAL) << "Type conversion from " << input_type << " to "
1339 << result_type << " not yet implemented";
1340 break;
1341
1342 default:
1343 LOG(FATAL) << "Unexpected type conversion from " << input_type
1344 << " to " << result_type;
1345 }
1346 break;
1347
Roland Levillain3adfd1b2014-11-11 14:48:08 +00001348 case Primitive::kPrimInt:
Roland Levillaindff1f282014-11-05 14:15:05 +00001349 case Primitive::kPrimFloat:
1350 case Primitive::kPrimDouble:
1351 LOG(FATAL) << "Type conversion from " << input_type
1352 << " to " << result_type << " not yet implemented";
1353 break;
1354
1355 default:
1356 LOG(FATAL) << "Unexpected type conversion from " << input_type
1357 << " to " << result_type;
1358 }
1359}
1360
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001361void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001362 LocationSummary* locations =
1363 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001364 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001365 case Primitive::kPrimInt: {
1366 locations->SetInAt(0, Location::RequiresRegister());
1367 locations->SetInAt(1, Location::Any());
1368 locations->SetOut(Location::SameAsFirstInput());
1369 break;
1370 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001371
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001372 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001373 locations->SetInAt(0, Location::RequiresRegister());
1374 locations->SetInAt(1, Location::RequiresRegister());
1375 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001376 break;
1377 }
1378
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001379 case Primitive::kPrimDouble:
1380 case Primitive::kPrimFloat: {
1381 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001382 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001383 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001384 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001385 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001386
1387 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001388 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001389 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001390}
1391
1392void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1393 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001394 Location first = locations->InAt(0);
1395 Location second = locations->InAt(1);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001396 DCHECK(first.Equals(locations->Out()));
Calin Juravle11351682014-10-23 15:38:15 +01001397
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001398 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001399 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001400 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001401 __ addl(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001402 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001403 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001404 __ addl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001405 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001406 __ addl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001407 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001408 break;
1409 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001410
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001411 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001412 __ addq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001413 break;
1414 }
1415
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001416 case Primitive::kPrimFloat: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001417 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001418 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001419 }
1420
1421 case Primitive::kPrimDouble: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001422 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001423 break;
1424 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001425
1426 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001427 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001428 }
1429}
1430
1431void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001432 LocationSummary* locations =
1433 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001434 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001435 case Primitive::kPrimInt: {
1436 locations->SetInAt(0, Location::RequiresRegister());
1437 locations->SetInAt(1, Location::Any());
1438 locations->SetOut(Location::SameAsFirstInput());
1439 break;
1440 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001441 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001442 locations->SetInAt(0, Location::RequiresRegister());
1443 locations->SetInAt(1, Location::RequiresRegister());
1444 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001445 break;
1446 }
Calin Juravle11351682014-10-23 15:38:15 +01001447 case Primitive::kPrimFloat:
1448 case Primitive::kPrimDouble: {
1449 locations->SetInAt(0, Location::RequiresFpuRegister());
1450 locations->SetInAt(1, Location::RequiresFpuRegister());
1451 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001452 break;
Calin Juravle11351682014-10-23 15:38:15 +01001453 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001454 default:
Calin Juravle11351682014-10-23 15:38:15 +01001455 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001456 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001457}
1458
1459void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1460 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001461 Location first = locations->InAt(0);
1462 Location second = locations->InAt(1);
1463 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001464 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001465 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001466 if (second.IsRegister()) {
1467 __ subl(first.As<CpuRegister>(), second.As<CpuRegister>());
1468 } else if (second.IsConstant()) {
1469 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1470 __ subl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001471 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001472 __ subl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001473 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001474 break;
1475 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001476 case Primitive::kPrimLong: {
Calin Juravle11351682014-10-23 15:38:15 +01001477 __ subq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001478 break;
1479 }
1480
Calin Juravle11351682014-10-23 15:38:15 +01001481 case Primitive::kPrimFloat: {
1482 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001483 break;
Calin Juravle11351682014-10-23 15:38:15 +01001484 }
1485
1486 case Primitive::kPrimDouble: {
1487 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1488 break;
1489 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001490
1491 default:
Calin Juravle11351682014-10-23 15:38:15 +01001492 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001493 }
1494}
1495
Calin Juravle34bacdf2014-10-07 20:23:36 +01001496void LocationsBuilderX86_64::VisitMul(HMul* mul) {
1497 LocationSummary* locations =
1498 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1499 switch (mul->GetResultType()) {
1500 case Primitive::kPrimInt: {
1501 locations->SetInAt(0, Location::RequiresRegister());
1502 locations->SetInAt(1, Location::Any());
1503 locations->SetOut(Location::SameAsFirstInput());
1504 break;
1505 }
1506 case Primitive::kPrimLong: {
1507 locations->SetInAt(0, Location::RequiresRegister());
1508 locations->SetInAt(1, Location::RequiresRegister());
1509 locations->SetOut(Location::SameAsFirstInput());
1510 break;
1511 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001512 case Primitive::kPrimFloat:
1513 case Primitive::kPrimDouble: {
1514 locations->SetInAt(0, Location::RequiresFpuRegister());
1515 locations->SetInAt(1, Location::RequiresFpuRegister());
1516 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001517 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001518 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001519
1520 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001521 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001522 }
1523}
1524
1525void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
1526 LocationSummary* locations = mul->GetLocations();
1527 Location first = locations->InAt(0);
1528 Location second = locations->InAt(1);
1529 DCHECK(first.Equals(locations->Out()));
1530 switch (mul->GetResultType()) {
1531 case Primitive::kPrimInt: {
1532 if (second.IsRegister()) {
1533 __ imull(first.As<CpuRegister>(), second.As<CpuRegister>());
1534 } else if (second.IsConstant()) {
1535 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1536 __ imull(first.As<CpuRegister>(), imm);
1537 } else {
1538 DCHECK(second.IsStackSlot());
1539 __ imull(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
1540 }
1541 break;
1542 }
1543 case Primitive::kPrimLong: {
1544 __ imulq(first.As<CpuRegister>(), second.As<CpuRegister>());
1545 break;
1546 }
1547
Calin Juravleb5bfa962014-10-21 18:02:24 +01001548 case Primitive::kPrimFloat: {
1549 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001550 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001551 }
1552
1553 case Primitive::kPrimDouble: {
1554 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1555 break;
1556 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001557
1558 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001559 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001560 }
1561}
1562
Calin Juravle7c4954d2014-10-28 16:57:40 +00001563void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
1564 LocationSummary* locations =
1565 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1566 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001567 case Primitive::kPrimInt: {
1568 locations->SetInAt(0, Location::RegisterLocation(RAX));
1569 locations->SetInAt(1, Location::RequiresRegister());
1570 locations->SetOut(Location::SameAsFirstInput());
1571 // Intel uses edx:eax as the dividend.
1572 locations->AddTemp(Location::RegisterLocation(RDX));
1573 break;
1574 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001575 case Primitive::kPrimLong: {
1576 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1577 break;
1578 }
1579 case Primitive::kPrimFloat:
1580 case Primitive::kPrimDouble: {
1581 locations->SetInAt(0, Location::RequiresFpuRegister());
1582 locations->SetInAt(1, Location::RequiresFpuRegister());
1583 locations->SetOut(Location::SameAsFirstInput());
1584 break;
1585 }
1586
1587 default:
1588 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1589 }
1590}
1591
1592void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
1593 LocationSummary* locations = div->GetLocations();
1594 Location first = locations->InAt(0);
1595 Location second = locations->InAt(1);
1596 DCHECK(first.Equals(locations->Out()));
1597
1598 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001599 case Primitive::kPrimInt: {
1600 CpuRegister first_reg = first.As<CpuRegister>();
1601 CpuRegister second_reg = second.As<CpuRegister>();
1602 DCHECK_EQ(RAX, first_reg.AsRegister());
1603 DCHECK_EQ(RDX, locations->GetTemp(0).As<CpuRegister>().AsRegister());
1604
1605 SlowPathCodeX86_64* slow_path =
1606 new (GetGraph()->GetArena()) DivMinusOneSlowPathX86_64(first_reg.AsRegister());
1607 codegen_->AddSlowPath(slow_path);
1608
1609 // 0x80000000/-1 triggers an arithmetic exception!
1610 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
1611 // it's safe to just use negl instead of more complex comparisons.
1612
1613 __ cmpl(second_reg, Immediate(-1));
1614 __ j(kEqual, slow_path->GetEntryLabel());
1615
1616 // edx:eax <- sign-extended of eax
1617 __ cdq();
1618 // eax = quotient, edx = remainder
1619 __ idivl(second_reg);
1620
1621 __ Bind(slow_path->GetExitLabel());
1622 break;
1623 }
1624
Calin Juravle7c4954d2014-10-28 16:57:40 +00001625 case Primitive::kPrimLong: {
1626 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1627 break;
1628 }
1629
1630 case Primitive::kPrimFloat: {
1631 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1632 break;
1633 }
1634
1635 case Primitive::kPrimDouble: {
1636 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1637 break;
1638 }
1639
1640 default:
1641 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1642 }
1643}
1644
Calin Juravled0d48522014-11-04 16:40:20 +00001645void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1646 LocationSummary* locations =
1647 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1648 locations->SetInAt(0, Location::Any());
1649 if (instruction->HasUses()) {
1650 locations->SetOut(Location::SameAsFirstInput());
1651 }
1652}
1653
1654void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1655 SlowPathCodeX86_64* slow_path =
1656 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
1657 codegen_->AddSlowPath(slow_path);
1658
1659 LocationSummary* locations = instruction->GetLocations();
1660 Location value = locations->InAt(0);
1661
1662 if (value.IsRegister()) {
1663 __ testl(value.As<CpuRegister>(), value.As<CpuRegister>());
1664 } else if (value.IsStackSlot()) {
1665 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
1666 } else {
1667 DCHECK(value.IsConstant()) << value;
1668 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
1669 __ jmp(slow_path->GetEntryLabel());
1670 }
1671 return;
1672 }
1673 __ j(kEqual, slow_path->GetEntryLabel());
1674}
1675
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001676void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001677 LocationSummary* locations =
1678 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001679 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001680 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1681 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1682 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001683}
1684
1685void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
1686 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001687 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001688 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1689
1690 __ gs()->call(Address::Absolute(
1691 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
1692
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001693 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001694 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001695}
1696
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001697void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
1698 LocationSummary* locations =
1699 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1700 InvokeRuntimeCallingConvention calling_convention;
1701 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1702 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1703 locations->SetOut(Location::RegisterLocation(RAX));
1704 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1705}
1706
1707void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
1708 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001709 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001710 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1711
1712 __ gs()->call(Address::Absolute(
1713 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocArrayWithAccessCheck), true));
1714
1715 DCHECK(!codegen_->IsLeafMethod());
1716 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1717}
1718
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001719void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001720 LocationSummary* locations =
1721 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001722 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1723 if (location.IsStackSlot()) {
1724 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1725 } else if (location.IsDoubleStackSlot()) {
1726 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1727 }
1728 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001729}
1730
1731void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
1732 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001733 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001734}
1735
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001736void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001737 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001738 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001739 locations->SetInAt(0, Location::RequiresRegister());
1740 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001741}
1742
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001743void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
1744 LocationSummary* locations = not_->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001745 DCHECK_EQ(locations->InAt(0).As<CpuRegister>().AsRegister(),
1746 locations->Out().As<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001747 Location out = locations->Out();
1748 switch (not_->InputAt(0)->GetType()) {
1749 case Primitive::kPrimBoolean:
1750 __ xorq(out.As<CpuRegister>(), Immediate(1));
1751 break;
1752
1753 case Primitive::kPrimInt:
1754 __ notl(out.As<CpuRegister>());
1755 break;
1756
1757 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001758 __ notq(out.As<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001759 break;
1760
1761 default:
1762 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1763 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001764}
1765
1766void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001767 LocationSummary* locations =
1768 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001769 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1770 locations->SetInAt(i, Location::Any());
1771 }
1772 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001773}
1774
1775void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001776 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001777 LOG(FATAL) << "Unimplemented";
1778}
1779
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001780void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001781 LocationSummary* locations =
1782 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001783 Primitive::Type field_type = instruction->GetFieldType();
1784 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001785 locations->SetInAt(0, Location::RequiresRegister());
1786 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001787 if (is_object_type) {
1788 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001789 locations->AddTemp(Location::RequiresRegister());
1790 locations->AddTemp(Location::RequiresRegister());
1791 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001792}
1793
1794void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1795 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001796 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001797 size_t offset = instruction->GetFieldOffset().SizeValue();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001798 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001799
1800 switch (field_type) {
1801 case Primitive::kPrimBoolean:
1802 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001803 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001804 __ movb(Address(obj, offset), value);
1805 break;
1806 }
1807
1808 case Primitive::kPrimShort:
1809 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001810 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001811 __ movw(Address(obj, offset), value);
1812 break;
1813 }
1814
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001815 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001816 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001817 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001818 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001819 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001820 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
1821 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001822 codegen_->MarkGCCard(temp, card, obj, value);
1823 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001824 break;
1825 }
1826
1827 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001828 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001829 __ movq(Address(obj, offset), value);
1830 break;
1831 }
1832
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001833 case Primitive::kPrimFloat: {
1834 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1835 __ movss(Address(obj, offset), value);
1836 break;
1837 }
1838
1839 case Primitive::kPrimDouble: {
1840 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1841 __ movsd(Address(obj, offset), value);
1842 break;
1843 }
1844
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001845 case Primitive::kPrimVoid:
1846 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001847 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001848 }
1849}
1850
1851void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001852 LocationSummary* locations =
1853 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001854 locations->SetInAt(0, Location::RequiresRegister());
1855 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001856}
1857
1858void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1859 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001860 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001861 size_t offset = instruction->GetFieldOffset().SizeValue();
1862
1863 switch (instruction->GetType()) {
1864 case Primitive::kPrimBoolean: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001865 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001866 __ movzxb(out, Address(obj, offset));
1867 break;
1868 }
1869
1870 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001871 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001872 __ movsxb(out, Address(obj, offset));
1873 break;
1874 }
1875
1876 case Primitive::kPrimShort: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001877 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001878 __ movsxw(out, Address(obj, offset));
1879 break;
1880 }
1881
1882 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001883 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001884 __ movzxw(out, Address(obj, offset));
1885 break;
1886 }
1887
1888 case Primitive::kPrimInt:
1889 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001890 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001891 __ movl(out, Address(obj, offset));
1892 break;
1893 }
1894
1895 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001896 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001897 __ movq(out, Address(obj, offset));
1898 break;
1899 }
1900
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001901 case Primitive::kPrimFloat: {
1902 XmmRegister out = locations->Out().As<XmmRegister>();
1903 __ movss(out, Address(obj, offset));
1904 break;
1905 }
1906
1907 case Primitive::kPrimDouble: {
1908 XmmRegister out = locations->Out().As<XmmRegister>();
1909 __ movsd(out, Address(obj, offset));
1910 break;
1911 }
1912
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001913 case Primitive::kPrimVoid:
1914 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001915 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001916 }
1917}
1918
1919void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001920 LocationSummary* locations =
1921 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001922 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001923 if (instruction->HasUses()) {
1924 locations->SetOut(Location::SameAsFirstInput());
1925 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001926}
1927
1928void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001929 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001930 codegen_->AddSlowPath(slow_path);
1931
1932 LocationSummary* locations = instruction->GetLocations();
1933 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001934
1935 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001936 __ cmpl(obj.As<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001937 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001938 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001939 } else {
1940 DCHECK(obj.IsConstant()) << obj;
1941 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1942 __ jmp(slow_path->GetEntryLabel());
1943 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001944 }
1945 __ j(kEqual, slow_path->GetEntryLabel());
1946}
1947
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001948void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001949 LocationSummary* locations =
1950 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001951 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001952 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001953 1, Location::RegisterOrConstant(instruction->InputAt(1)));
1954 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001955}
1956
1957void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
1958 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001959 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001960 Location index = locations->InAt(1);
1961
1962 switch (instruction->GetType()) {
1963 case Primitive::kPrimBoolean: {
1964 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001965 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001966 if (index.IsConstant()) {
1967 __ movzxb(out, Address(obj,
1968 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1969 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001970 __ movzxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001971 }
1972 break;
1973 }
1974
1975 case Primitive::kPrimByte: {
1976 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001977 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001978 if (index.IsConstant()) {
1979 __ movsxb(out, Address(obj,
1980 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1981 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001982 __ movsxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001983 }
1984 break;
1985 }
1986
1987 case Primitive::kPrimShort: {
1988 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001989 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001990 if (index.IsConstant()) {
1991 __ movsxw(out, Address(obj,
1992 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1993 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001994 __ movsxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001995 }
1996 break;
1997 }
1998
1999 case Primitive::kPrimChar: {
2000 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002001 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002002 if (index.IsConstant()) {
2003 __ movzxw(out, Address(obj,
2004 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2005 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002006 __ movzxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002007 }
2008 break;
2009 }
2010
2011 case Primitive::kPrimInt:
2012 case Primitive::kPrimNot: {
2013 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
2014 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002015 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002016 if (index.IsConstant()) {
2017 __ movl(out, Address(obj,
2018 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2019 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002020 __ movl(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002021 }
2022 break;
2023 }
2024
2025 case Primitive::kPrimLong: {
2026 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002027 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002028 if (index.IsConstant()) {
2029 __ movq(out, Address(obj,
2030 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2031 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002032 __ movq(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002033 }
2034 break;
2035 }
2036
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002037 case Primitive::kPrimFloat: {
2038 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2039 XmmRegister out = locations->Out().As<XmmRegister>();
2040 if (index.IsConstant()) {
2041 __ movss(out, Address(obj,
2042 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2043 } else {
2044 __ movss(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
2045 }
2046 break;
2047 }
2048
2049 case Primitive::kPrimDouble: {
2050 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2051 XmmRegister out = locations->Out().As<XmmRegister>();
2052 if (index.IsConstant()) {
2053 __ movsd(out, Address(obj,
2054 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2055 } else {
2056 __ movsd(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
2057 }
2058 break;
2059 }
2060
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002061 case Primitive::kPrimVoid:
2062 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002063 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002064 }
2065}
2066
2067void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002068 Primitive::Type value_type = instruction->GetComponentType();
2069 bool is_object = value_type == Primitive::kPrimNot;
2070 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2071 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
2072 if (is_object) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002073 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002074 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2075 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2076 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002077 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002078 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002079 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002080 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2081 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002082 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002083 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002084 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
2085 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002086 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002087 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002088 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002089 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002090}
2091
2092void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
2093 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002094 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002095 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002096 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002097 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002098
2099 switch (value_type) {
2100 case Primitive::kPrimBoolean:
2101 case Primitive::kPrimByte: {
2102 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002103 if (index.IsConstant()) {
2104 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002105 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002106 __ movb(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002107 } else {
2108 __ movb(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2109 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002110 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002111 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002112 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
2113 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002114 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002115 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002116 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2117 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002118 }
2119 break;
2120 }
2121
2122 case Primitive::kPrimShort:
2123 case Primitive::kPrimChar: {
2124 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002125 if (index.IsConstant()) {
2126 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002127 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002128 __ movw(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002129 } else {
2130 __ movw(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2131 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002132 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002133 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002134 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
2135 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002136 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002137 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002138 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2139 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002140 }
2141 break;
2142 }
2143
2144 case Primitive::kPrimInt: {
2145 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002146 if (index.IsConstant()) {
2147 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002148 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002149 __ movl(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002150 } else {
2151 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2152 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002153 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002154 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002155 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2156 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002157 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002158 DCHECK(value.IsConstant()) << value;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002159 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002160 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2161 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002162 }
2163 break;
2164 }
2165
2166 case Primitive::kPrimNot: {
2167 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject), true));
2168 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002169 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002170 break;
2171 }
2172
2173 case Primitive::kPrimLong: {
2174 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002175 if (index.IsConstant()) {
2176 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002177 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002178 __ movq(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002179 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002180 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002181 __ movq(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
2182 value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002183 }
2184 break;
2185 }
2186
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002187 case Primitive::kPrimFloat: {
2188 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2189 if (index.IsConstant()) {
2190 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2191 DCHECK(value.IsFpuRegister());
2192 __ movss(Address(obj, offset), value.As<XmmRegister>());
2193 } else {
2194 DCHECK(value.IsFpuRegister());
2195 __ movss(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2196 value.As<XmmRegister>());
2197 }
2198 break;
2199 }
2200
2201 case Primitive::kPrimDouble: {
2202 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2203 if (index.IsConstant()) {
2204 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
2205 DCHECK(value.IsFpuRegister());
2206 __ movsd(Address(obj, offset), value.As<XmmRegister>());
2207 } else {
2208 DCHECK(value.IsFpuRegister());
2209 __ movsd(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
2210 value.As<XmmRegister>());
2211 }
2212 break;
2213 }
2214
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002215 case Primitive::kPrimVoid:
2216 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002217 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002218 }
2219}
2220
2221void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002222 LocationSummary* locations =
2223 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002224 locations->SetInAt(0, Location::RequiresRegister());
2225 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002226}
2227
2228void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
2229 LocationSummary* locations = instruction->GetLocations();
2230 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002231 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2232 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002233 __ movl(out, Address(obj, offset));
2234}
2235
2236void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002237 LocationSummary* locations =
2238 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002239 locations->SetInAt(0, Location::RequiresRegister());
2240 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002241 if (instruction->HasUses()) {
2242 locations->SetOut(Location::SameAsFirstInput());
2243 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002244}
2245
2246void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
2247 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002248 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002249 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002250 codegen_->AddSlowPath(slow_path);
2251
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002252 CpuRegister index = locations->InAt(0).As<CpuRegister>();
2253 CpuRegister length = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002254
2255 __ cmpl(index, length);
2256 __ j(kAboveEqual, slow_path->GetEntryLabel());
2257}
2258
2259void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
2260 CpuRegister card,
2261 CpuRegister object,
2262 CpuRegister value) {
2263 Label is_null;
2264 __ testl(value, value);
2265 __ j(kEqual, &is_null);
2266 __ gs()->movq(card, Address::Absolute(
2267 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
2268 __ movq(temp, object);
2269 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
2270 __ movb(Address(temp, card, TIMES_1, 0), card);
2271 __ Bind(&is_null);
2272}
2273
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002274void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
2275 temp->SetLocations(nullptr);
2276}
2277
2278void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
2279 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002280 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002281}
2282
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002283void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002284 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002285 LOG(FATAL) << "Unimplemented";
2286}
2287
2288void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002289 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2290}
2291
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002292void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
2293 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2294}
2295
2296void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002297 HBasicBlock* block = instruction->GetBlock();
2298 if (block->GetLoopInformation() != nullptr) {
2299 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2300 // The back edge will generate the suspend check.
2301 return;
2302 }
2303 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2304 // The goto will generate the suspend check.
2305 return;
2306 }
2307 GenerateSuspendCheck(instruction, nullptr);
2308}
2309
2310void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
2311 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002312 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002313 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002314 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002315 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002316 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002317 if (successor == nullptr) {
2318 __ j(kNotEqual, slow_path->GetEntryLabel());
2319 __ Bind(slow_path->GetReturnLabel());
2320 } else {
2321 __ j(kEqual, codegen_->GetLabelOf(successor));
2322 __ jmp(slow_path->GetEntryLabel());
2323 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002324}
2325
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002326X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
2327 return codegen_->GetAssembler();
2328}
2329
2330void ParallelMoveResolverX86_64::EmitMove(size_t index) {
2331 MoveOperands* move = moves_.Get(index);
2332 Location source = move->GetSource();
2333 Location destination = move->GetDestination();
2334
2335 if (source.IsRegister()) {
2336 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002337 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002338 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002339 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002340 source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002341 } else {
2342 DCHECK(destination.IsDoubleStackSlot());
2343 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002344 source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002345 }
2346 } else if (source.IsStackSlot()) {
2347 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002348 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002349 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002350 } else if (destination.IsFpuRegister()) {
2351 __ movss(destination.As<XmmRegister>(),
2352 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002353 } else {
2354 DCHECK(destination.IsStackSlot());
2355 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2356 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2357 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002358 } else if (source.IsDoubleStackSlot()) {
2359 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002360 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002361 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002362 } else if (destination.IsFpuRegister()) {
2363 __ movsd(destination.As<XmmRegister>(), Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002364 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01002365 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002366 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2367 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2368 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002369 } else if (source.IsConstant()) {
2370 HConstant* constant = source.GetConstant();
2371 if (constant->IsIntConstant()) {
2372 Immediate imm(constant->AsIntConstant()->GetValue());
2373 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002374 __ movl(destination.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002375 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002376 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002377 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2378 }
2379 } else if (constant->IsLongConstant()) {
2380 int64_t value = constant->AsLongConstant()->GetValue();
2381 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002382 __ movq(destination.As<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002383 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002384 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002385 __ movq(CpuRegister(TMP), Immediate(value));
2386 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2387 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002388 } else if (constant->IsFloatConstant()) {
2389 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
2390 if (destination.IsFpuRegister()) {
2391 __ movl(CpuRegister(TMP), imm);
2392 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2393 } else {
2394 DCHECK(destination.IsStackSlot()) << destination;
2395 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2396 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002397 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002398 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
2399 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
2400 if (destination.IsFpuRegister()) {
2401 __ movq(CpuRegister(TMP), imm);
2402 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2403 } else {
2404 DCHECK(destination.IsDoubleStackSlot()) << destination;
2405 __ movq(CpuRegister(TMP), imm);
2406 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2407 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002408 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002409 } else if (source.IsFpuRegister()) {
2410 if (destination.IsFpuRegister()) {
2411 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
2412 } else if (destination.IsStackSlot()) {
2413 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
2414 source.As<XmmRegister>());
2415 } else {
2416 DCHECK(destination.IsDoubleStackSlot());
2417 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
2418 source.As<XmmRegister>());
2419 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002420 }
2421}
2422
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002423void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002424 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002425 __ movl(Address(CpuRegister(RSP), mem), reg);
2426 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002427}
2428
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002429void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002430 ScratchRegisterScope ensure_scratch(
2431 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2432
2433 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2434 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2435 __ movl(CpuRegister(ensure_scratch.GetRegister()),
2436 Address(CpuRegister(RSP), mem2 + stack_offset));
2437 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2438 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
2439 CpuRegister(ensure_scratch.GetRegister()));
2440}
2441
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002442void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
2443 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2444 __ movq(Address(CpuRegister(RSP), mem), reg);
2445 __ movq(reg, CpuRegister(TMP));
2446}
2447
2448void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
2449 ScratchRegisterScope ensure_scratch(
2450 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2451
2452 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2453 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2454 __ movq(CpuRegister(ensure_scratch.GetRegister()),
2455 Address(CpuRegister(RSP), mem2 + stack_offset));
2456 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2457 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
2458 CpuRegister(ensure_scratch.GetRegister()));
2459}
2460
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002461void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
2462 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2463 __ movss(Address(CpuRegister(RSP), mem), reg);
2464 __ movd(reg, CpuRegister(TMP));
2465}
2466
2467void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
2468 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2469 __ movsd(Address(CpuRegister(RSP), mem), reg);
2470 __ movd(reg, CpuRegister(TMP));
2471}
2472
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002473void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
2474 MoveOperands* move = moves_.Get(index);
2475 Location source = move->GetSource();
2476 Location destination = move->GetDestination();
2477
2478 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002479 __ xchgq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002480 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002481 Exchange32(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002482 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002483 Exchange32(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002484 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002485 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
2486 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002487 Exchange64(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002488 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002489 Exchange64(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002490 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
2491 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002492 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
2493 __ movd(CpuRegister(TMP), source.As<XmmRegister>());
2494 __ movaps(source.As<XmmRegister>(), destination.As<XmmRegister>());
2495 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2496 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
2497 Exchange32(source.As<XmmRegister>(), destination.GetStackIndex());
2498 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
2499 Exchange32(destination.As<XmmRegister>(), source.GetStackIndex());
2500 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
2501 Exchange64(source.As<XmmRegister>(), destination.GetStackIndex());
2502 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
2503 Exchange64(destination.As<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002504 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002505 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002506 }
2507}
2508
2509
2510void ParallelMoveResolverX86_64::SpillScratch(int reg) {
2511 __ pushq(CpuRegister(reg));
2512}
2513
2514
2515void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
2516 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002517}
2518
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002519void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
2520 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
2521 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2522 Immediate(mirror::Class::kStatusInitialized));
2523 __ j(kLess, slow_path->GetEntryLabel());
2524 __ Bind(slow_path->GetExitLabel());
2525 // No need for memory fence, thanks to the X86_64 memory model.
2526}
2527
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002528void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002529 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2530 ? LocationSummary::kCallOnSlowPath
2531 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002532 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002533 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002534 locations->SetOut(Location::RequiresRegister());
2535}
2536
2537void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
2538 CpuRegister out = cls->GetLocations()->Out().As<CpuRegister>();
2539 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002540 DCHECK(!cls->CanCallRuntime());
2541 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002542 codegen_->LoadCurrentMethod(out);
2543 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2544 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002545 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002546 codegen_->LoadCurrentMethod(out);
2547 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2548 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002549 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
2550 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2551 codegen_->AddSlowPath(slow_path);
2552 __ testl(out, out);
2553 __ j(kEqual, slow_path->GetEntryLabel());
2554 if (cls->MustGenerateClinitCheck()) {
2555 GenerateClassInitializationCheck(slow_path, out);
2556 } else {
2557 __ Bind(slow_path->GetExitLabel());
2558 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002559 }
2560}
2561
2562void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
2563 LocationSummary* locations =
2564 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2565 locations->SetInAt(0, Location::RequiresRegister());
2566 if (check->HasUses()) {
2567 locations->SetOut(Location::SameAsFirstInput());
2568 }
2569}
2570
2571void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002572 // We assume the class to not be null.
2573 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
2574 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002575 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002576 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002577}
2578
2579void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2580 LocationSummary* locations =
2581 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2582 locations->SetInAt(0, Location::RequiresRegister());
2583 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2584}
2585
2586void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2587 LocationSummary* locations = instruction->GetLocations();
2588 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002589 size_t offset = instruction->GetFieldOffset().SizeValue();
2590
2591 switch (instruction->GetType()) {
2592 case Primitive::kPrimBoolean: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002593 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002594 __ movzxb(out, Address(cls, offset));
2595 break;
2596 }
2597
2598 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002599 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002600 __ movsxb(out, Address(cls, offset));
2601 break;
2602 }
2603
2604 case Primitive::kPrimShort: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002605 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002606 __ movsxw(out, Address(cls, offset));
2607 break;
2608 }
2609
2610 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002611 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002612 __ movzxw(out, Address(cls, offset));
2613 break;
2614 }
2615
2616 case Primitive::kPrimInt:
2617 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002618 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002619 __ movl(out, Address(cls, offset));
2620 break;
2621 }
2622
2623 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002624 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002625 __ movq(out, Address(cls, offset));
2626 break;
2627 }
2628
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002629 case Primitive::kPrimFloat: {
2630 XmmRegister out = locations->Out().As<XmmRegister>();
2631 __ movss(out, Address(cls, offset));
2632 break;
2633 }
2634
2635 case Primitive::kPrimDouble: {
2636 XmmRegister out = locations->Out().As<XmmRegister>();
2637 __ movsd(out, Address(cls, offset));
2638 break;
2639 }
2640
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002641 case Primitive::kPrimVoid:
2642 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2643 UNREACHABLE();
2644 }
2645}
2646
2647void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2648 LocationSummary* locations =
2649 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2650 Primitive::Type field_type = instruction->GetFieldType();
2651 bool is_object_type = field_type == Primitive::kPrimNot;
2652 locations->SetInAt(0, Location::RequiresRegister());
2653 locations->SetInAt(1, Location::RequiresRegister());
2654 if (is_object_type) {
2655 // Temporary registers for the write barrier.
2656 locations->AddTemp(Location::RequiresRegister());
2657 locations->AddTemp(Location::RequiresRegister());
2658 }
2659}
2660
2661void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2662 LocationSummary* locations = instruction->GetLocations();
2663 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002664 size_t offset = instruction->GetFieldOffset().SizeValue();
2665 Primitive::Type field_type = instruction->GetFieldType();
2666
2667 switch (field_type) {
2668 case Primitive::kPrimBoolean:
2669 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002670 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002671 __ movb(Address(cls, offset), value);
2672 break;
2673 }
2674
2675 case Primitive::kPrimShort:
2676 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002677 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002678 __ movw(Address(cls, offset), value);
2679 break;
2680 }
2681
2682 case Primitive::kPrimInt:
2683 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002684 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002685 __ movl(Address(cls, offset), value);
2686 if (field_type == Primitive::kPrimNot) {
2687 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
2688 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
2689 codegen_->MarkGCCard(temp, card, cls, value);
2690 }
2691 break;
2692 }
2693
2694 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002695 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002696 __ movq(Address(cls, offset), value);
2697 break;
2698 }
2699
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002700 case Primitive::kPrimFloat: {
2701 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2702 __ movss(Address(cls, offset), value);
2703 break;
2704 }
2705
2706 case Primitive::kPrimDouble: {
2707 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2708 __ movsd(Address(cls, offset), value);
2709 break;
2710 }
2711
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002712 case Primitive::kPrimVoid:
2713 LOG(FATAL) << "Unreachable type " << field_type;
2714 UNREACHABLE();
2715 }
2716}
2717
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002718void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
2719 LocationSummary* locations =
2720 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2721 locations->SetOut(Location::RequiresRegister());
2722}
2723
2724void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
2725 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
2726 codegen_->AddSlowPath(slow_path);
2727
2728 CpuRegister out = load->GetLocations()->Out().As<CpuRegister>();
2729 codegen_->LoadCurrentMethod(CpuRegister(out));
2730 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
2731 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2732 __ testl(out, out);
2733 __ j(kEqual, slow_path->GetEntryLabel());
2734 __ Bind(slow_path->GetExitLabel());
2735}
2736
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002737void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
2738 LocationSummary* locations =
2739 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2740 locations->SetOut(Location::RequiresRegister());
2741}
2742
2743void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
2744 Address address = Address::Absolute(
2745 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
2746 __ gs()->movl(load->GetLocations()->Out().As<CpuRegister>(), address);
2747 __ gs()->movl(address, Immediate(0));
2748}
2749
2750void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
2751 LocationSummary* locations =
2752 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2753 InvokeRuntimeCallingConvention calling_convention;
2754 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2755}
2756
2757void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
2758 __ gs()->call(
2759 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
2760 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2761}
2762
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002763void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002764 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
2765 ? LocationSummary::kNoCall
2766 : LocationSummary::kCallOnSlowPath;
2767 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2768 locations->SetInAt(0, Location::RequiresRegister());
2769 locations->SetInAt(1, Location::Any());
2770 locations->SetOut(Location::RequiresRegister());
2771}
2772
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002773void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002774 LocationSummary* locations = instruction->GetLocations();
2775 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2776 Location cls = locations->InAt(1);
2777 CpuRegister out = locations->Out().As<CpuRegister>();
2778 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2779 Label done, zero;
2780 SlowPathCodeX86_64* slow_path = nullptr;
2781
2782 // Return 0 if `obj` is null.
2783 // TODO: avoid this check if we know obj is not null.
2784 __ testl(obj, obj);
2785 __ j(kEqual, &zero);
2786 // Compare the class of `obj` with `cls`.
2787 __ movl(out, Address(obj, class_offset));
2788 if (cls.IsRegister()) {
2789 __ cmpl(out, cls.As<CpuRegister>());
2790 } else {
2791 DCHECK(cls.IsStackSlot()) << cls;
2792 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
2793 }
2794 if (instruction->IsClassFinal()) {
2795 // Classes must be equal for the instanceof to succeed.
2796 __ j(kNotEqual, &zero);
2797 __ movl(out, Immediate(1));
2798 __ jmp(&done);
2799 } else {
2800 // If the classes are not equal, we go into a slow path.
2801 DCHECK(locations->OnlyCallsOnSlowPath());
2802 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002803 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002804 codegen_->AddSlowPath(slow_path);
2805 __ j(kNotEqual, slow_path->GetEntryLabel());
2806 __ movl(out, Immediate(1));
2807 __ jmp(&done);
2808 }
2809 __ Bind(&zero);
2810 __ movl(out, Immediate(0));
2811 if (slow_path != nullptr) {
2812 __ Bind(slow_path->GetExitLabel());
2813 }
2814 __ Bind(&done);
2815}
2816
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002817void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
2818 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2819 instruction, LocationSummary::kCallOnSlowPath);
2820 locations->SetInAt(0, Location::RequiresRegister());
2821 locations->SetInAt(1, Location::Any());
2822 locations->AddTemp(Location::RequiresRegister());
2823}
2824
2825void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
2826 LocationSummary* locations = instruction->GetLocations();
2827 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2828 Location cls = locations->InAt(1);
2829 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
2830 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2831 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
2832 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
2833 codegen_->AddSlowPath(slow_path);
2834
2835 // TODO: avoid this check if we know obj is not null.
2836 __ testl(obj, obj);
2837 __ j(kEqual, slow_path->GetExitLabel());
2838 // Compare the class of `obj` with `cls`.
2839 __ movl(temp, Address(obj, class_offset));
2840 if (cls.IsRegister()) {
2841 __ cmpl(temp, cls.As<CpuRegister>());
2842 } else {
2843 DCHECK(cls.IsStackSlot()) << cls;
2844 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
2845 }
2846 // Classes must be equal for the checkcast to succeed.
2847 __ j(kNotEqual, slow_path->GetEntryLabel());
2848 __ Bind(slow_path->GetExitLabel());
2849}
2850
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002851} // namespace x86_64
2852} // namespace art