blob: 112c17975a9027e901c2db4fce1b1946d166eb1b [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) {
Roland Levillain946e1432014-11-11 17:35:19 +00001279 case Primitive::kPrimInt:
1280 switch (input_type) {
1281 case Primitive::kPrimLong:
1282 // long-to-int conversion.
1283 locations->SetInAt(0, Location::Any());
1284 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1285 break;
1286
1287 case Primitive::kPrimFloat:
1288 case Primitive::kPrimDouble:
1289 LOG(FATAL) << "Type conversion from " << input_type
1290 << " to " << result_type << " not yet implemented";
1291 break;
1292
1293 default:
1294 LOG(FATAL) << "Unexpected type conversion from " << input_type
1295 << " to " << result_type;
1296 }
1297 break;
1298
Roland Levillaindff1f282014-11-05 14:15:05 +00001299 case Primitive::kPrimLong:
1300 switch (input_type) {
1301 case Primitive::kPrimByte:
1302 case Primitive::kPrimShort:
1303 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001304 case Primitive::kPrimChar:
Roland Levillaindff1f282014-11-05 14:15:05 +00001305 // int-to-long conversion.
1306 // TODO: We would benefit from a (to-be-implemented)
1307 // Location::RegisterOrStackSlot requirement for this input.
1308 locations->SetInAt(0, Location::RequiresRegister());
1309 locations->SetOut(Location::RequiresRegister());
1310 break;
1311
1312 case Primitive::kPrimFloat:
1313 case Primitive::kPrimDouble:
1314 LOG(FATAL) << "Type conversion from " << input_type << " to "
1315 << result_type << " not yet implemented";
1316 break;
1317
1318 default:
1319 LOG(FATAL) << "Unexpected type conversion from " << input_type
1320 << " to " << result_type;
1321 }
1322 break;
1323
Roland Levillaindff1f282014-11-05 14:15:05 +00001324 case Primitive::kPrimFloat:
1325 case Primitive::kPrimDouble:
1326 LOG(FATAL) << "Type conversion from " << input_type
1327 << " to " << result_type << " not yet implemented";
1328 break;
1329
1330 default:
1331 LOG(FATAL) << "Unexpected type conversion from " << input_type
1332 << " to " << result_type;
1333 }
1334}
1335
1336void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1337 LocationSummary* locations = conversion->GetLocations();
1338 Location out = locations->Out();
1339 Location in = locations->InAt(0);
1340 Primitive::Type result_type = conversion->GetResultType();
1341 Primitive::Type input_type = conversion->GetInputType();
1342 switch (result_type) {
Roland Levillain946e1432014-11-11 17:35:19 +00001343 case Primitive::kPrimInt:
1344 switch (input_type) {
1345 case Primitive::kPrimLong:
1346 // long-to-int conversion.
1347 if (in.IsRegister()) {
1348 __ movl(out.As<CpuRegister>(), in.As<CpuRegister>());
1349 } else if (in.IsDoubleStackSlot()) {
1350 __ movl(out.As<CpuRegister>(),
1351 Address(CpuRegister(RSP), in.GetStackIndex()));
1352 } else {
1353 DCHECK(in.IsConstant());
1354 DCHECK(in.GetConstant()->IsLongConstant());
1355 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
1356 __ movl(out.As<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1357 }
1358 break;
1359
1360 case Primitive::kPrimFloat:
1361 case Primitive::kPrimDouble:
1362 LOG(FATAL) << "Type conversion from " << input_type
1363 << " to " << result_type << " not yet implemented";
1364 break;
1365
1366 default:
1367 LOG(FATAL) << "Unexpected type conversion from " << input_type
1368 << " to " << result_type;
1369 }
1370 break;
1371
Roland Levillaindff1f282014-11-05 14:15:05 +00001372 case Primitive::kPrimLong:
1373 switch (input_type) {
1374 DCHECK(out.IsRegister());
1375 case Primitive::kPrimByte:
1376 case Primitive::kPrimShort:
1377 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001378 case Primitive::kPrimChar:
Roland Levillaindff1f282014-11-05 14:15:05 +00001379 // int-to-long conversion.
1380 DCHECK(in.IsRegister());
1381 __ movsxd(out.As<CpuRegister>(), in.As<CpuRegister>());
1382 break;
1383
1384 case Primitive::kPrimFloat:
1385 case Primitive::kPrimDouble:
1386 LOG(FATAL) << "Type conversion from " << input_type << " to "
1387 << result_type << " not yet implemented";
1388 break;
1389
1390 default:
1391 LOG(FATAL) << "Unexpected type conversion from " << input_type
1392 << " to " << result_type;
1393 }
1394 break;
1395
Roland Levillaindff1f282014-11-05 14:15:05 +00001396 case Primitive::kPrimFloat:
1397 case Primitive::kPrimDouble:
1398 LOG(FATAL) << "Type conversion from " << input_type
1399 << " to " << result_type << " not yet implemented";
1400 break;
1401
1402 default:
1403 LOG(FATAL) << "Unexpected type conversion from " << input_type
1404 << " to " << result_type;
1405 }
1406}
1407
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001408void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001409 LocationSummary* locations =
1410 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001411 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001412 case Primitive::kPrimInt: {
1413 locations->SetInAt(0, Location::RequiresRegister());
1414 locations->SetInAt(1, Location::Any());
1415 locations->SetOut(Location::SameAsFirstInput());
1416 break;
1417 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001418
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001419 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001420 locations->SetInAt(0, Location::RequiresRegister());
1421 locations->SetInAt(1, Location::RequiresRegister());
1422 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001423 break;
1424 }
1425
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001426 case Primitive::kPrimDouble:
1427 case Primitive::kPrimFloat: {
1428 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001429 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001430 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001431 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001432 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001433
1434 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001435 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001436 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001437}
1438
1439void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1440 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001441 Location first = locations->InAt(0);
1442 Location second = locations->InAt(1);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001443 DCHECK(first.Equals(locations->Out()));
Calin Juravle11351682014-10-23 15:38:15 +01001444
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001445 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001446 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001447 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001448 __ addl(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001449 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001450 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001451 __ addl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001452 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001453 __ addl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001454 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001455 break;
1456 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001457
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001458 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001459 __ addq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001460 break;
1461 }
1462
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001463 case Primitive::kPrimFloat: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001464 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001465 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001466 }
1467
1468 case Primitive::kPrimDouble: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001469 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001470 break;
1471 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001472
1473 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001474 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001475 }
1476}
1477
1478void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001479 LocationSummary* locations =
1480 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001481 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001482 case Primitive::kPrimInt: {
1483 locations->SetInAt(0, Location::RequiresRegister());
1484 locations->SetInAt(1, Location::Any());
1485 locations->SetOut(Location::SameAsFirstInput());
1486 break;
1487 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001488 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001489 locations->SetInAt(0, Location::RequiresRegister());
1490 locations->SetInAt(1, Location::RequiresRegister());
1491 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001492 break;
1493 }
Calin Juravle11351682014-10-23 15:38:15 +01001494 case Primitive::kPrimFloat:
1495 case Primitive::kPrimDouble: {
1496 locations->SetInAt(0, Location::RequiresFpuRegister());
1497 locations->SetInAt(1, Location::RequiresFpuRegister());
1498 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001499 break;
Calin Juravle11351682014-10-23 15:38:15 +01001500 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001501 default:
Calin Juravle11351682014-10-23 15:38:15 +01001502 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001503 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001504}
1505
1506void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1507 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001508 Location first = locations->InAt(0);
1509 Location second = locations->InAt(1);
1510 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001511 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001512 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001513 if (second.IsRegister()) {
1514 __ subl(first.As<CpuRegister>(), second.As<CpuRegister>());
1515 } else if (second.IsConstant()) {
1516 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1517 __ subl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001518 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001519 __ subl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001520 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001521 break;
1522 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001523 case Primitive::kPrimLong: {
Calin Juravle11351682014-10-23 15:38:15 +01001524 __ subq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001525 break;
1526 }
1527
Calin Juravle11351682014-10-23 15:38:15 +01001528 case Primitive::kPrimFloat: {
1529 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001530 break;
Calin Juravle11351682014-10-23 15:38:15 +01001531 }
1532
1533 case Primitive::kPrimDouble: {
1534 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1535 break;
1536 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001537
1538 default:
Calin Juravle11351682014-10-23 15:38:15 +01001539 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001540 }
1541}
1542
Calin Juravle34bacdf2014-10-07 20:23:36 +01001543void LocationsBuilderX86_64::VisitMul(HMul* mul) {
1544 LocationSummary* locations =
1545 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1546 switch (mul->GetResultType()) {
1547 case Primitive::kPrimInt: {
1548 locations->SetInAt(0, Location::RequiresRegister());
1549 locations->SetInAt(1, Location::Any());
1550 locations->SetOut(Location::SameAsFirstInput());
1551 break;
1552 }
1553 case Primitive::kPrimLong: {
1554 locations->SetInAt(0, Location::RequiresRegister());
1555 locations->SetInAt(1, Location::RequiresRegister());
1556 locations->SetOut(Location::SameAsFirstInput());
1557 break;
1558 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001559 case Primitive::kPrimFloat:
1560 case Primitive::kPrimDouble: {
1561 locations->SetInAt(0, Location::RequiresFpuRegister());
1562 locations->SetInAt(1, Location::RequiresFpuRegister());
1563 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001564 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001565 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001566
1567 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001568 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001569 }
1570}
1571
1572void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
1573 LocationSummary* locations = mul->GetLocations();
1574 Location first = locations->InAt(0);
1575 Location second = locations->InAt(1);
1576 DCHECK(first.Equals(locations->Out()));
1577 switch (mul->GetResultType()) {
1578 case Primitive::kPrimInt: {
1579 if (second.IsRegister()) {
1580 __ imull(first.As<CpuRegister>(), second.As<CpuRegister>());
1581 } else if (second.IsConstant()) {
1582 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1583 __ imull(first.As<CpuRegister>(), imm);
1584 } else {
1585 DCHECK(second.IsStackSlot());
1586 __ imull(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
1587 }
1588 break;
1589 }
1590 case Primitive::kPrimLong: {
1591 __ imulq(first.As<CpuRegister>(), second.As<CpuRegister>());
1592 break;
1593 }
1594
Calin Juravleb5bfa962014-10-21 18:02:24 +01001595 case Primitive::kPrimFloat: {
1596 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001597 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001598 }
1599
1600 case Primitive::kPrimDouble: {
1601 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1602 break;
1603 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001604
1605 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001606 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001607 }
1608}
1609
Calin Juravle7c4954d2014-10-28 16:57:40 +00001610void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
1611 LocationSummary* locations =
1612 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1613 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001614 case Primitive::kPrimInt: {
1615 locations->SetInAt(0, Location::RegisterLocation(RAX));
1616 locations->SetInAt(1, Location::RequiresRegister());
1617 locations->SetOut(Location::SameAsFirstInput());
1618 // Intel uses edx:eax as the dividend.
1619 locations->AddTemp(Location::RegisterLocation(RDX));
1620 break;
1621 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001622 case Primitive::kPrimLong: {
1623 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1624 break;
1625 }
1626 case Primitive::kPrimFloat:
1627 case Primitive::kPrimDouble: {
1628 locations->SetInAt(0, Location::RequiresFpuRegister());
1629 locations->SetInAt(1, Location::RequiresFpuRegister());
1630 locations->SetOut(Location::SameAsFirstInput());
1631 break;
1632 }
1633
1634 default:
1635 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1636 }
1637}
1638
1639void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
1640 LocationSummary* locations = div->GetLocations();
1641 Location first = locations->InAt(0);
1642 Location second = locations->InAt(1);
1643 DCHECK(first.Equals(locations->Out()));
1644
1645 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001646 case Primitive::kPrimInt: {
1647 CpuRegister first_reg = first.As<CpuRegister>();
1648 CpuRegister second_reg = second.As<CpuRegister>();
1649 DCHECK_EQ(RAX, first_reg.AsRegister());
1650 DCHECK_EQ(RDX, locations->GetTemp(0).As<CpuRegister>().AsRegister());
1651
1652 SlowPathCodeX86_64* slow_path =
1653 new (GetGraph()->GetArena()) DivMinusOneSlowPathX86_64(first_reg.AsRegister());
1654 codegen_->AddSlowPath(slow_path);
1655
1656 // 0x80000000/-1 triggers an arithmetic exception!
1657 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
1658 // it's safe to just use negl instead of more complex comparisons.
1659
1660 __ cmpl(second_reg, Immediate(-1));
1661 __ j(kEqual, slow_path->GetEntryLabel());
1662
1663 // edx:eax <- sign-extended of eax
1664 __ cdq();
1665 // eax = quotient, edx = remainder
1666 __ idivl(second_reg);
1667
1668 __ Bind(slow_path->GetExitLabel());
1669 break;
1670 }
1671
Calin Juravle7c4954d2014-10-28 16:57:40 +00001672 case Primitive::kPrimLong: {
1673 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1674 break;
1675 }
1676
1677 case Primitive::kPrimFloat: {
1678 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1679 break;
1680 }
1681
1682 case Primitive::kPrimDouble: {
1683 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1684 break;
1685 }
1686
1687 default:
1688 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1689 }
1690}
1691
Calin Juravled0d48522014-11-04 16:40:20 +00001692void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1693 LocationSummary* locations =
1694 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1695 locations->SetInAt(0, Location::Any());
1696 if (instruction->HasUses()) {
1697 locations->SetOut(Location::SameAsFirstInput());
1698 }
1699}
1700
1701void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1702 SlowPathCodeX86_64* slow_path =
1703 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
1704 codegen_->AddSlowPath(slow_path);
1705
1706 LocationSummary* locations = instruction->GetLocations();
1707 Location value = locations->InAt(0);
1708
1709 if (value.IsRegister()) {
1710 __ testl(value.As<CpuRegister>(), value.As<CpuRegister>());
1711 } else if (value.IsStackSlot()) {
1712 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
1713 } else {
1714 DCHECK(value.IsConstant()) << value;
1715 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
1716 __ jmp(slow_path->GetEntryLabel());
1717 }
1718 return;
1719 }
1720 __ j(kEqual, slow_path->GetEntryLabel());
1721}
1722
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001723void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001724 LocationSummary* locations =
1725 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001726 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001727 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1728 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1729 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001730}
1731
1732void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
1733 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001734 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001735 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1736
1737 __ gs()->call(Address::Absolute(
1738 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
1739
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001740 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001741 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001742}
1743
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001744void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
1745 LocationSummary* locations =
1746 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1747 InvokeRuntimeCallingConvention calling_convention;
1748 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1749 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1750 locations->SetOut(Location::RegisterLocation(RAX));
1751 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1752}
1753
1754void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
1755 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001756 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001757 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1758
1759 __ gs()->call(Address::Absolute(
1760 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocArrayWithAccessCheck), true));
1761
1762 DCHECK(!codegen_->IsLeafMethod());
1763 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1764}
1765
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001766void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* 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 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1770 if (location.IsStackSlot()) {
1771 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1772 } else if (location.IsDoubleStackSlot()) {
1773 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1774 }
1775 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001776}
1777
1778void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
1779 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001780 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001781}
1782
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001783void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001784 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001785 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001786 locations->SetInAt(0, Location::RequiresRegister());
1787 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001788}
1789
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001790void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
1791 LocationSummary* locations = not_->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001792 DCHECK_EQ(locations->InAt(0).As<CpuRegister>().AsRegister(),
1793 locations->Out().As<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001794 Location out = locations->Out();
1795 switch (not_->InputAt(0)->GetType()) {
1796 case Primitive::kPrimBoolean:
1797 __ xorq(out.As<CpuRegister>(), Immediate(1));
1798 break;
1799
1800 case Primitive::kPrimInt:
1801 __ notl(out.As<CpuRegister>());
1802 break;
1803
1804 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001805 __ notq(out.As<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001806 break;
1807
1808 default:
1809 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1810 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001811}
1812
1813void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001814 LocationSummary* locations =
1815 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001816 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1817 locations->SetInAt(i, Location::Any());
1818 }
1819 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001820}
1821
1822void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001823 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001824 LOG(FATAL) << "Unimplemented";
1825}
1826
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001827void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001828 LocationSummary* locations =
1829 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001830 Primitive::Type field_type = instruction->GetFieldType();
1831 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001832 locations->SetInAt(0, Location::RequiresRegister());
1833 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001834 if (is_object_type) {
1835 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001836 locations->AddTemp(Location::RequiresRegister());
1837 locations->AddTemp(Location::RequiresRegister());
1838 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001839}
1840
1841void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1842 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001843 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001844 size_t offset = instruction->GetFieldOffset().SizeValue();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001845 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001846
1847 switch (field_type) {
1848 case Primitive::kPrimBoolean:
1849 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001850 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001851 __ movb(Address(obj, offset), value);
1852 break;
1853 }
1854
1855 case Primitive::kPrimShort:
1856 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001857 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001858 __ movw(Address(obj, offset), value);
1859 break;
1860 }
1861
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001862 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001863 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001864 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001865 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001866 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001867 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
1868 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001869 codegen_->MarkGCCard(temp, card, obj, value);
1870 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001871 break;
1872 }
1873
1874 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001875 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001876 __ movq(Address(obj, offset), value);
1877 break;
1878 }
1879
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001880 case Primitive::kPrimFloat: {
1881 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1882 __ movss(Address(obj, offset), value);
1883 break;
1884 }
1885
1886 case Primitive::kPrimDouble: {
1887 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1888 __ movsd(Address(obj, offset), value);
1889 break;
1890 }
1891
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001892 case Primitive::kPrimVoid:
1893 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001894 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001895 }
1896}
1897
1898void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001899 LocationSummary* locations =
1900 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001901 locations->SetInAt(0, Location::RequiresRegister());
1902 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001903}
1904
1905void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1906 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001907 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001908 size_t offset = instruction->GetFieldOffset().SizeValue();
1909
1910 switch (instruction->GetType()) {
1911 case Primitive::kPrimBoolean: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001912 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001913 __ movzxb(out, Address(obj, offset));
1914 break;
1915 }
1916
1917 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001918 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001919 __ movsxb(out, Address(obj, offset));
1920 break;
1921 }
1922
1923 case Primitive::kPrimShort: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001924 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001925 __ movsxw(out, Address(obj, offset));
1926 break;
1927 }
1928
1929 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001930 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001931 __ movzxw(out, Address(obj, offset));
1932 break;
1933 }
1934
1935 case Primitive::kPrimInt:
1936 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001937 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001938 __ movl(out, Address(obj, offset));
1939 break;
1940 }
1941
1942 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001943 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001944 __ movq(out, Address(obj, offset));
1945 break;
1946 }
1947
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001948 case Primitive::kPrimFloat: {
1949 XmmRegister out = locations->Out().As<XmmRegister>();
1950 __ movss(out, Address(obj, offset));
1951 break;
1952 }
1953
1954 case Primitive::kPrimDouble: {
1955 XmmRegister out = locations->Out().As<XmmRegister>();
1956 __ movsd(out, Address(obj, offset));
1957 break;
1958 }
1959
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001960 case Primitive::kPrimVoid:
1961 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001962 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001963 }
1964}
1965
1966void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001967 LocationSummary* locations =
1968 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001969 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001970 if (instruction->HasUses()) {
1971 locations->SetOut(Location::SameAsFirstInput());
1972 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001973}
1974
1975void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001976 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001977 codegen_->AddSlowPath(slow_path);
1978
1979 LocationSummary* locations = instruction->GetLocations();
1980 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001981
1982 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001983 __ cmpl(obj.As<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001984 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001985 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001986 } else {
1987 DCHECK(obj.IsConstant()) << obj;
1988 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1989 __ jmp(slow_path->GetEntryLabel());
1990 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001991 }
1992 __ j(kEqual, slow_path->GetEntryLabel());
1993}
1994
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001995void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001996 LocationSummary* locations =
1997 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001998 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001999 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002000 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2001 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002002}
2003
2004void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
2005 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002006 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002007 Location index = locations->InAt(1);
2008
2009 switch (instruction->GetType()) {
2010 case Primitive::kPrimBoolean: {
2011 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002012 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002013 if (index.IsConstant()) {
2014 __ movzxb(out, Address(obj,
2015 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2016 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002017 __ movzxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002018 }
2019 break;
2020 }
2021
2022 case Primitive::kPrimByte: {
2023 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002024 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002025 if (index.IsConstant()) {
2026 __ movsxb(out, Address(obj,
2027 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2028 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002029 __ movsxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002030 }
2031 break;
2032 }
2033
2034 case Primitive::kPrimShort: {
2035 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002036 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002037 if (index.IsConstant()) {
2038 __ movsxw(out, Address(obj,
2039 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2040 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002041 __ movsxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002042 }
2043 break;
2044 }
2045
2046 case Primitive::kPrimChar: {
2047 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002048 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002049 if (index.IsConstant()) {
2050 __ movzxw(out, Address(obj,
2051 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2052 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002053 __ movzxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002054 }
2055 break;
2056 }
2057
2058 case Primitive::kPrimInt:
2059 case Primitive::kPrimNot: {
2060 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
2061 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002062 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002063 if (index.IsConstant()) {
2064 __ movl(out, Address(obj,
2065 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2066 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002067 __ movl(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002068 }
2069 break;
2070 }
2071
2072 case Primitive::kPrimLong: {
2073 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002074 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002075 if (index.IsConstant()) {
2076 __ movq(out, Address(obj,
2077 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2078 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002079 __ movq(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002080 }
2081 break;
2082 }
2083
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002084 case Primitive::kPrimFloat: {
2085 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2086 XmmRegister out = locations->Out().As<XmmRegister>();
2087 if (index.IsConstant()) {
2088 __ movss(out, Address(obj,
2089 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2090 } else {
2091 __ movss(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
2092 }
2093 break;
2094 }
2095
2096 case Primitive::kPrimDouble: {
2097 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2098 XmmRegister out = locations->Out().As<XmmRegister>();
2099 if (index.IsConstant()) {
2100 __ movsd(out, Address(obj,
2101 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2102 } else {
2103 __ movsd(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
2104 }
2105 break;
2106 }
2107
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002108 case Primitive::kPrimVoid:
2109 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002110 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002111 }
2112}
2113
2114void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002115 Primitive::Type value_type = instruction->GetComponentType();
2116 bool is_object = value_type == Primitive::kPrimNot;
2117 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2118 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
2119 if (is_object) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002120 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002121 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2122 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2123 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002124 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002125 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002126 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002127 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2128 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002129 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002130 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002131 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
2132 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002133 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002134 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002135 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002136 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002137}
2138
2139void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
2140 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002141 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002142 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002143 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002144 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002145
2146 switch (value_type) {
2147 case Primitive::kPrimBoolean:
2148 case Primitive::kPrimByte: {
2149 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002150 if (index.IsConstant()) {
2151 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002152 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002153 __ movb(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002154 } else {
2155 __ movb(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2156 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002157 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002158 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002159 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
2160 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002161 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002162 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002163 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2164 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002165 }
2166 break;
2167 }
2168
2169 case Primitive::kPrimShort:
2170 case Primitive::kPrimChar: {
2171 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002172 if (index.IsConstant()) {
2173 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002174 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002175 __ movw(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002176 } else {
2177 __ movw(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2178 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002179 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002180 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002181 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
2182 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002183 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002184 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002185 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2186 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002187 }
2188 break;
2189 }
2190
2191 case Primitive::kPrimInt: {
2192 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002193 if (index.IsConstant()) {
2194 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002195 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002196 __ movl(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002197 } else {
2198 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2199 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002200 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002201 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002202 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2203 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002204 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002205 DCHECK(value.IsConstant()) << value;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002206 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002207 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2208 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002209 }
2210 break;
2211 }
2212
2213 case Primitive::kPrimNot: {
2214 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject), true));
2215 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002216 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002217 break;
2218 }
2219
2220 case Primitive::kPrimLong: {
2221 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002222 if (index.IsConstant()) {
2223 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002224 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002225 __ movq(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002226 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002227 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002228 __ movq(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
2229 value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002230 }
2231 break;
2232 }
2233
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002234 case Primitive::kPrimFloat: {
2235 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2236 if (index.IsConstant()) {
2237 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2238 DCHECK(value.IsFpuRegister());
2239 __ movss(Address(obj, offset), value.As<XmmRegister>());
2240 } else {
2241 DCHECK(value.IsFpuRegister());
2242 __ movss(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2243 value.As<XmmRegister>());
2244 }
2245 break;
2246 }
2247
2248 case Primitive::kPrimDouble: {
2249 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2250 if (index.IsConstant()) {
2251 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
2252 DCHECK(value.IsFpuRegister());
2253 __ movsd(Address(obj, offset), value.As<XmmRegister>());
2254 } else {
2255 DCHECK(value.IsFpuRegister());
2256 __ movsd(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
2257 value.As<XmmRegister>());
2258 }
2259 break;
2260 }
2261
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002262 case Primitive::kPrimVoid:
2263 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002264 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002265 }
2266}
2267
2268void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002269 LocationSummary* locations =
2270 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002271 locations->SetInAt(0, Location::RequiresRegister());
2272 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002273}
2274
2275void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
2276 LocationSummary* locations = instruction->GetLocations();
2277 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002278 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2279 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002280 __ movl(out, Address(obj, offset));
2281}
2282
2283void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002284 LocationSummary* locations =
2285 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002286 locations->SetInAt(0, Location::RequiresRegister());
2287 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002288 if (instruction->HasUses()) {
2289 locations->SetOut(Location::SameAsFirstInput());
2290 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002291}
2292
2293void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
2294 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002295 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002296 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002297 codegen_->AddSlowPath(slow_path);
2298
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002299 CpuRegister index = locations->InAt(0).As<CpuRegister>();
2300 CpuRegister length = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002301
2302 __ cmpl(index, length);
2303 __ j(kAboveEqual, slow_path->GetEntryLabel());
2304}
2305
2306void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
2307 CpuRegister card,
2308 CpuRegister object,
2309 CpuRegister value) {
2310 Label is_null;
2311 __ testl(value, value);
2312 __ j(kEqual, &is_null);
2313 __ gs()->movq(card, Address::Absolute(
2314 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
2315 __ movq(temp, object);
2316 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
2317 __ movb(Address(temp, card, TIMES_1, 0), card);
2318 __ Bind(&is_null);
2319}
2320
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002321void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
2322 temp->SetLocations(nullptr);
2323}
2324
2325void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
2326 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002327 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002328}
2329
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002330void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002331 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002332 LOG(FATAL) << "Unimplemented";
2333}
2334
2335void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002336 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2337}
2338
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002339void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
2340 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2341}
2342
2343void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002344 HBasicBlock* block = instruction->GetBlock();
2345 if (block->GetLoopInformation() != nullptr) {
2346 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2347 // The back edge will generate the suspend check.
2348 return;
2349 }
2350 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2351 // The goto will generate the suspend check.
2352 return;
2353 }
2354 GenerateSuspendCheck(instruction, nullptr);
2355}
2356
2357void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
2358 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002359 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002360 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002361 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002362 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002363 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002364 if (successor == nullptr) {
2365 __ j(kNotEqual, slow_path->GetEntryLabel());
2366 __ Bind(slow_path->GetReturnLabel());
2367 } else {
2368 __ j(kEqual, codegen_->GetLabelOf(successor));
2369 __ jmp(slow_path->GetEntryLabel());
2370 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002371}
2372
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002373X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
2374 return codegen_->GetAssembler();
2375}
2376
2377void ParallelMoveResolverX86_64::EmitMove(size_t index) {
2378 MoveOperands* move = moves_.Get(index);
2379 Location source = move->GetSource();
2380 Location destination = move->GetDestination();
2381
2382 if (source.IsRegister()) {
2383 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002384 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002385 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002386 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002387 source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002388 } else {
2389 DCHECK(destination.IsDoubleStackSlot());
2390 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002391 source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002392 }
2393 } else if (source.IsStackSlot()) {
2394 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002395 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002396 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002397 } else if (destination.IsFpuRegister()) {
2398 __ movss(destination.As<XmmRegister>(),
2399 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002400 } else {
2401 DCHECK(destination.IsStackSlot());
2402 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2403 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2404 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002405 } else if (source.IsDoubleStackSlot()) {
2406 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002407 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002408 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002409 } else if (destination.IsFpuRegister()) {
2410 __ movsd(destination.As<XmmRegister>(), Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002411 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01002412 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002413 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2414 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2415 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002416 } else if (source.IsConstant()) {
2417 HConstant* constant = source.GetConstant();
2418 if (constant->IsIntConstant()) {
2419 Immediate imm(constant->AsIntConstant()->GetValue());
2420 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002421 __ movl(destination.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002422 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002423 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002424 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2425 }
2426 } else if (constant->IsLongConstant()) {
2427 int64_t value = constant->AsLongConstant()->GetValue();
2428 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002429 __ movq(destination.As<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002430 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002431 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002432 __ movq(CpuRegister(TMP), Immediate(value));
2433 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2434 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002435 } else if (constant->IsFloatConstant()) {
2436 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
2437 if (destination.IsFpuRegister()) {
2438 __ movl(CpuRegister(TMP), imm);
2439 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2440 } else {
2441 DCHECK(destination.IsStackSlot()) << destination;
2442 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2443 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002444 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002445 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
2446 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
2447 if (destination.IsFpuRegister()) {
2448 __ movq(CpuRegister(TMP), imm);
2449 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2450 } else {
2451 DCHECK(destination.IsDoubleStackSlot()) << destination;
2452 __ movq(CpuRegister(TMP), imm);
2453 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2454 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002455 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002456 } else if (source.IsFpuRegister()) {
2457 if (destination.IsFpuRegister()) {
2458 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
2459 } else if (destination.IsStackSlot()) {
2460 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
2461 source.As<XmmRegister>());
2462 } else {
2463 DCHECK(destination.IsDoubleStackSlot());
2464 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
2465 source.As<XmmRegister>());
2466 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002467 }
2468}
2469
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002470void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002471 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002472 __ movl(Address(CpuRegister(RSP), mem), reg);
2473 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002474}
2475
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002476void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002477 ScratchRegisterScope ensure_scratch(
2478 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2479
2480 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2481 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2482 __ movl(CpuRegister(ensure_scratch.GetRegister()),
2483 Address(CpuRegister(RSP), mem2 + stack_offset));
2484 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2485 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
2486 CpuRegister(ensure_scratch.GetRegister()));
2487}
2488
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002489void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
2490 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2491 __ movq(Address(CpuRegister(RSP), mem), reg);
2492 __ movq(reg, CpuRegister(TMP));
2493}
2494
2495void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
2496 ScratchRegisterScope ensure_scratch(
2497 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2498
2499 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2500 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2501 __ movq(CpuRegister(ensure_scratch.GetRegister()),
2502 Address(CpuRegister(RSP), mem2 + stack_offset));
2503 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2504 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
2505 CpuRegister(ensure_scratch.GetRegister()));
2506}
2507
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002508void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
2509 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2510 __ movss(Address(CpuRegister(RSP), mem), reg);
2511 __ movd(reg, CpuRegister(TMP));
2512}
2513
2514void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
2515 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2516 __ movsd(Address(CpuRegister(RSP), mem), reg);
2517 __ movd(reg, CpuRegister(TMP));
2518}
2519
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002520void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
2521 MoveOperands* move = moves_.Get(index);
2522 Location source = move->GetSource();
2523 Location destination = move->GetDestination();
2524
2525 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002526 __ xchgq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002527 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002528 Exchange32(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002529 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002530 Exchange32(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002531 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002532 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
2533 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002534 Exchange64(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002535 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002536 Exchange64(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002537 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
2538 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002539 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
2540 __ movd(CpuRegister(TMP), source.As<XmmRegister>());
2541 __ movaps(source.As<XmmRegister>(), destination.As<XmmRegister>());
2542 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2543 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
2544 Exchange32(source.As<XmmRegister>(), destination.GetStackIndex());
2545 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
2546 Exchange32(destination.As<XmmRegister>(), source.GetStackIndex());
2547 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
2548 Exchange64(source.As<XmmRegister>(), destination.GetStackIndex());
2549 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
2550 Exchange64(destination.As<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002551 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002552 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002553 }
2554}
2555
2556
2557void ParallelMoveResolverX86_64::SpillScratch(int reg) {
2558 __ pushq(CpuRegister(reg));
2559}
2560
2561
2562void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
2563 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002564}
2565
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002566void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
2567 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
2568 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2569 Immediate(mirror::Class::kStatusInitialized));
2570 __ j(kLess, slow_path->GetEntryLabel());
2571 __ Bind(slow_path->GetExitLabel());
2572 // No need for memory fence, thanks to the X86_64 memory model.
2573}
2574
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002575void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002576 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2577 ? LocationSummary::kCallOnSlowPath
2578 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002579 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002580 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002581 locations->SetOut(Location::RequiresRegister());
2582}
2583
2584void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
2585 CpuRegister out = cls->GetLocations()->Out().As<CpuRegister>();
2586 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002587 DCHECK(!cls->CanCallRuntime());
2588 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002589 codegen_->LoadCurrentMethod(out);
2590 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2591 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002592 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002593 codegen_->LoadCurrentMethod(out);
2594 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2595 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002596 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
2597 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2598 codegen_->AddSlowPath(slow_path);
2599 __ testl(out, out);
2600 __ j(kEqual, slow_path->GetEntryLabel());
2601 if (cls->MustGenerateClinitCheck()) {
2602 GenerateClassInitializationCheck(slow_path, out);
2603 } else {
2604 __ Bind(slow_path->GetExitLabel());
2605 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002606 }
2607}
2608
2609void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
2610 LocationSummary* locations =
2611 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2612 locations->SetInAt(0, Location::RequiresRegister());
2613 if (check->HasUses()) {
2614 locations->SetOut(Location::SameAsFirstInput());
2615 }
2616}
2617
2618void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002619 // We assume the class to not be null.
2620 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
2621 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002622 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002623 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002624}
2625
2626void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2627 LocationSummary* locations =
2628 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2629 locations->SetInAt(0, Location::RequiresRegister());
2630 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2631}
2632
2633void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2634 LocationSummary* locations = instruction->GetLocations();
2635 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002636 size_t offset = instruction->GetFieldOffset().SizeValue();
2637
2638 switch (instruction->GetType()) {
2639 case Primitive::kPrimBoolean: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002640 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002641 __ movzxb(out, Address(cls, offset));
2642 break;
2643 }
2644
2645 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002646 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002647 __ movsxb(out, Address(cls, offset));
2648 break;
2649 }
2650
2651 case Primitive::kPrimShort: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002652 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002653 __ movsxw(out, Address(cls, offset));
2654 break;
2655 }
2656
2657 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002658 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002659 __ movzxw(out, Address(cls, offset));
2660 break;
2661 }
2662
2663 case Primitive::kPrimInt:
2664 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002665 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002666 __ movl(out, Address(cls, offset));
2667 break;
2668 }
2669
2670 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002671 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002672 __ movq(out, Address(cls, offset));
2673 break;
2674 }
2675
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002676 case Primitive::kPrimFloat: {
2677 XmmRegister out = locations->Out().As<XmmRegister>();
2678 __ movss(out, Address(cls, offset));
2679 break;
2680 }
2681
2682 case Primitive::kPrimDouble: {
2683 XmmRegister out = locations->Out().As<XmmRegister>();
2684 __ movsd(out, Address(cls, offset));
2685 break;
2686 }
2687
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002688 case Primitive::kPrimVoid:
2689 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2690 UNREACHABLE();
2691 }
2692}
2693
2694void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2695 LocationSummary* locations =
2696 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2697 Primitive::Type field_type = instruction->GetFieldType();
2698 bool is_object_type = field_type == Primitive::kPrimNot;
2699 locations->SetInAt(0, Location::RequiresRegister());
2700 locations->SetInAt(1, Location::RequiresRegister());
2701 if (is_object_type) {
2702 // Temporary registers for the write barrier.
2703 locations->AddTemp(Location::RequiresRegister());
2704 locations->AddTemp(Location::RequiresRegister());
2705 }
2706}
2707
2708void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2709 LocationSummary* locations = instruction->GetLocations();
2710 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002711 size_t offset = instruction->GetFieldOffset().SizeValue();
2712 Primitive::Type field_type = instruction->GetFieldType();
2713
2714 switch (field_type) {
2715 case Primitive::kPrimBoolean:
2716 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002717 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002718 __ movb(Address(cls, offset), value);
2719 break;
2720 }
2721
2722 case Primitive::kPrimShort:
2723 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002724 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002725 __ movw(Address(cls, offset), value);
2726 break;
2727 }
2728
2729 case Primitive::kPrimInt:
2730 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002731 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002732 __ movl(Address(cls, offset), value);
2733 if (field_type == Primitive::kPrimNot) {
2734 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
2735 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
2736 codegen_->MarkGCCard(temp, card, cls, value);
2737 }
2738 break;
2739 }
2740
2741 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002742 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002743 __ movq(Address(cls, offset), value);
2744 break;
2745 }
2746
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002747 case Primitive::kPrimFloat: {
2748 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2749 __ movss(Address(cls, offset), value);
2750 break;
2751 }
2752
2753 case Primitive::kPrimDouble: {
2754 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2755 __ movsd(Address(cls, offset), value);
2756 break;
2757 }
2758
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002759 case Primitive::kPrimVoid:
2760 LOG(FATAL) << "Unreachable type " << field_type;
2761 UNREACHABLE();
2762 }
2763}
2764
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002765void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
2766 LocationSummary* locations =
2767 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2768 locations->SetOut(Location::RequiresRegister());
2769}
2770
2771void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
2772 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
2773 codegen_->AddSlowPath(slow_path);
2774
2775 CpuRegister out = load->GetLocations()->Out().As<CpuRegister>();
2776 codegen_->LoadCurrentMethod(CpuRegister(out));
2777 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
2778 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2779 __ testl(out, out);
2780 __ j(kEqual, slow_path->GetEntryLabel());
2781 __ Bind(slow_path->GetExitLabel());
2782}
2783
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002784void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
2785 LocationSummary* locations =
2786 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2787 locations->SetOut(Location::RequiresRegister());
2788}
2789
2790void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
2791 Address address = Address::Absolute(
2792 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
2793 __ gs()->movl(load->GetLocations()->Out().As<CpuRegister>(), address);
2794 __ gs()->movl(address, Immediate(0));
2795}
2796
2797void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
2798 LocationSummary* locations =
2799 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2800 InvokeRuntimeCallingConvention calling_convention;
2801 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2802}
2803
2804void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
2805 __ gs()->call(
2806 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
2807 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2808}
2809
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002810void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002811 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
2812 ? LocationSummary::kNoCall
2813 : LocationSummary::kCallOnSlowPath;
2814 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2815 locations->SetInAt(0, Location::RequiresRegister());
2816 locations->SetInAt(1, Location::Any());
2817 locations->SetOut(Location::RequiresRegister());
2818}
2819
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002820void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002821 LocationSummary* locations = instruction->GetLocations();
2822 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2823 Location cls = locations->InAt(1);
2824 CpuRegister out = locations->Out().As<CpuRegister>();
2825 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2826 Label done, zero;
2827 SlowPathCodeX86_64* slow_path = nullptr;
2828
2829 // Return 0 if `obj` is null.
2830 // TODO: avoid this check if we know obj is not null.
2831 __ testl(obj, obj);
2832 __ j(kEqual, &zero);
2833 // Compare the class of `obj` with `cls`.
2834 __ movl(out, Address(obj, class_offset));
2835 if (cls.IsRegister()) {
2836 __ cmpl(out, cls.As<CpuRegister>());
2837 } else {
2838 DCHECK(cls.IsStackSlot()) << cls;
2839 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
2840 }
2841 if (instruction->IsClassFinal()) {
2842 // Classes must be equal for the instanceof to succeed.
2843 __ j(kNotEqual, &zero);
2844 __ movl(out, Immediate(1));
2845 __ jmp(&done);
2846 } else {
2847 // If the classes are not equal, we go into a slow path.
2848 DCHECK(locations->OnlyCallsOnSlowPath());
2849 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002850 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002851 codegen_->AddSlowPath(slow_path);
2852 __ j(kNotEqual, slow_path->GetEntryLabel());
2853 __ movl(out, Immediate(1));
2854 __ jmp(&done);
2855 }
2856 __ Bind(&zero);
2857 __ movl(out, Immediate(0));
2858 if (slow_path != nullptr) {
2859 __ Bind(slow_path->GetExitLabel());
2860 }
2861 __ Bind(&done);
2862}
2863
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002864void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
2865 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2866 instruction, LocationSummary::kCallOnSlowPath);
2867 locations->SetInAt(0, Location::RequiresRegister());
2868 locations->SetInAt(1, Location::Any());
2869 locations->AddTemp(Location::RequiresRegister());
2870}
2871
2872void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
2873 LocationSummary* locations = instruction->GetLocations();
2874 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2875 Location cls = locations->InAt(1);
2876 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
2877 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2878 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
2879 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
2880 codegen_->AddSlowPath(slow_path);
2881
2882 // TODO: avoid this check if we know obj is not null.
2883 __ testl(obj, obj);
2884 __ j(kEqual, slow_path->GetExitLabel());
2885 // Compare the class of `obj` with `cls`.
2886 __ movl(temp, Address(obj, class_offset));
2887 if (cls.IsRegister()) {
2888 __ cmpl(temp, cls.As<CpuRegister>());
2889 } else {
2890 DCHECK(cls.IsStackSlot()) << cls;
2891 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
2892 }
2893 // Classes must be equal for the checkcast to succeed.
2894 __ j(kNotEqual, slow_path->GetEntryLabel());
2895 __ Bind(slow_path->GetExitLabel());
2896}
2897
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002898void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
2899 LocationSummary* locations =
2900 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2901 InvokeRuntimeCallingConvention calling_convention;
2902 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2903}
2904
2905void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
2906 __ gs()->call(Address::Absolute(instruction->IsEnter()
2907 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
2908 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
2909 true));
2910 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2911}
2912
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002913} // namespace x86_64
2914} // namespace art