blob: 9a2a745376246dbd72aa3ffd4e7e340a41eb860e [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:
287 TypeCheckSlowPathX86_64(HTypeCheck* instruction, Location object_class)
288 : instruction_(instruction),
289 object_class_(object_class) {}
290
291 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
292 LocationSummary* locations = instruction_->GetLocations();
293 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
294
295 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
296 __ Bind(GetEntryLabel());
297 codegen->SaveLiveRegisters(locations);
298
299 // We're moving two locations to locations that could overlap, so we need a parallel
300 // move resolver.
301 InvokeRuntimeCallingConvention calling_convention;
302 MoveOperands move1(locations->InAt(1),
303 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
304 nullptr);
305 MoveOperands move2(object_class_,
306 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
307 nullptr);
308 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
309 parallel_move.AddMove(&move1);
310 parallel_move.AddMove(&move2);
311 x64_codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
312
313 __ gs()->call(
314 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInstanceofNonTrivial), true));
315 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
316 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
317
318 codegen->RestoreLiveRegisters(locations);
319 __ jmp(GetExitLabel());
320 }
321
322 private:
323 HTypeCheck* const instruction_;
324 const Location object_class_;
325
326 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
327};
328
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100329#undef __
330#define __ reinterpret_cast<X86_64Assembler*>(GetAssembler())->
331
Dave Allison20dfc792014-06-16 20:44:29 -0700332inline Condition X86_64Condition(IfCondition cond) {
333 switch (cond) {
334 case kCondEQ: return kEqual;
335 case kCondNE: return kNotEqual;
336 case kCondLT: return kLess;
337 case kCondLE: return kLessEqual;
338 case kCondGT: return kGreater;
339 case kCondGE: return kGreaterEqual;
340 default:
341 LOG(FATAL) << "Unknown if condition";
342 }
343 return kEqual;
344}
345
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100346void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
347 stream << X86_64ManagedRegister::FromCpuRegister(Register(reg));
348}
349
350void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
351 stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg));
352}
353
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100354size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
355 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
356 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100357}
358
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100359size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
360 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
361 return kX86_64WordSize;
362}
363
364size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
365 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
366 return kX86_64WordSize;
367}
368
369size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
370 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
371 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100372}
373
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100374CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100375 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfFloatRegisters, 0),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100376 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100377 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000378 instruction_visitor_(graph, this),
379 move_resolver_(graph->GetArena(), this) {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100380
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100381size_t CodeGeneratorX86_64::FrameEntrySpillSize() const {
382 return kNumberOfPushedRegistersAtEntry * kX86_64WordSize;
383}
384
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100385InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
386 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100387 : HGraphVisitor(graph),
388 assembler_(codegen->GetAssembler()),
389 codegen_(codegen) {}
390
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100391Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100392 switch (type) {
393 case Primitive::kPrimLong:
394 case Primitive::kPrimByte:
395 case Primitive::kPrimBoolean:
396 case Primitive::kPrimChar:
397 case Primitive::kPrimShort:
398 case Primitive::kPrimInt:
399 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100400 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100401 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100402 }
403
404 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100405 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100406 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100407 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100408 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100409
410 case Primitive::kPrimVoid:
411 LOG(FATAL) << "Unreachable type " << type;
412 }
413
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100414 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100415}
416
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100417void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100418 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100419 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100420
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000421 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100422 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000423
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100424 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100425 blocked_core_registers_[RBX] = true;
426 blocked_core_registers_[RBP] = true;
427 blocked_core_registers_[R12] = true;
428 blocked_core_registers_[R13] = true;
429 blocked_core_registers_[R14] = true;
430 blocked_core_registers_[R15] = true;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100431
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100432 blocked_fpu_registers_[XMM12] = true;
433 blocked_fpu_registers_[XMM13] = true;
434 blocked_fpu_registers_[XMM14] = true;
435 blocked_fpu_registers_[XMM15] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100436}
437
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100438void CodeGeneratorX86_64::GenerateFrameEntry() {
439 // Create a fake register to mimic Quick.
440 static const int kFakeReturnRegister = 16;
441 core_spill_mask_ |= (1 << kFakeReturnRegister);
442
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100443 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700444 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100445
446 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
447 __ testq(CpuRegister(RAX), Address(
448 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100449 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100450 }
451
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100452 // The return PC has already been pushed on the stack.
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100453 __ subq(CpuRegister(RSP),
454 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
455
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100456 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100457 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86_64();
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100458 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100459
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100460 __ gs()->cmpq(CpuRegister(RSP),
461 Address::Absolute(Thread::StackEndOffset<kX86_64WordSize>(), true));
462 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100463 }
464
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100465 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
466}
467
468void CodeGeneratorX86_64::GenerateFrameExit() {
469 __ addq(CpuRegister(RSP),
470 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
471}
472
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100473void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
474 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100475}
476
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100477void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100478 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
479}
480
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100481Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
482 switch (load->GetType()) {
483 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100484 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100485 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
486 break;
487
488 case Primitive::kPrimInt:
489 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100490 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100491 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100492
493 case Primitive::kPrimBoolean:
494 case Primitive::kPrimByte:
495 case Primitive::kPrimChar:
496 case Primitive::kPrimShort:
497 case Primitive::kPrimVoid:
498 LOG(FATAL) << "Unexpected type " << load->GetType();
499 }
500
501 LOG(FATAL) << "Unreachable";
502 return Location();
503}
504
505void CodeGeneratorX86_64::Move(Location destination, Location source) {
506 if (source.Equals(destination)) {
507 return;
508 }
509 if (destination.IsRegister()) {
510 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100511 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100512 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100513 __ movd(destination.As<CpuRegister>(), source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100514 } else if (source.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100515 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100516 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100517 } else {
518 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100519 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100520 Address(CpuRegister(RSP), source.GetStackIndex()));
521 }
522 } else if (destination.IsFpuRegister()) {
523 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100524 __ movd(destination.As<XmmRegister>(), source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100525 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100526 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100527 } else if (source.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100528 __ movss(destination.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100529 Address(CpuRegister(RSP), source.GetStackIndex()));
530 } else {
531 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100532 __ movsd(destination.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100533 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100534 }
535 } else if (destination.IsStackSlot()) {
536 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100537 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100538 source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100539 } else if (source.IsFpuRegister()) {
540 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100541 source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100542 } else {
543 DCHECK(source.IsStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000544 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
545 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100546 }
547 } else {
548 DCHECK(destination.IsDoubleStackSlot());
549 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100550 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100551 source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100552 } else if (source.IsFpuRegister()) {
553 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100554 source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100555 } else {
556 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000557 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
558 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100559 }
560 }
561}
562
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100563void CodeGeneratorX86_64::Move(HInstruction* instruction,
564 Location location,
565 HInstruction* move_for) {
Roland Levillain476df552014-10-09 17:51:36 +0100566 if (instruction->IsIntConstant()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100567 Immediate imm(instruction->AsIntConstant()->GetValue());
568 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100569 __ movl(location.As<CpuRegister>(), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100570 } else if (location.IsStackSlot()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100571 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100572 } else {
573 DCHECK(location.IsConstant());
574 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100575 }
Roland Levillain476df552014-10-09 17:51:36 +0100576 } else if (instruction->IsLongConstant()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100577 int64_t value = instruction->AsLongConstant()->GetValue();
578 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100579 __ movq(location.As<CpuRegister>(), Immediate(value));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100580 } else if (location.IsDoubleStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000581 __ movq(CpuRegister(TMP), Immediate(value));
582 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100583 } else {
584 DCHECK(location.IsConstant());
585 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100586 }
Roland Levillain476df552014-10-09 17:51:36 +0100587 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100588 switch (instruction->GetType()) {
589 case Primitive::kPrimBoolean:
590 case Primitive::kPrimByte:
591 case Primitive::kPrimChar:
592 case Primitive::kPrimShort:
593 case Primitive::kPrimInt:
594 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100595 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100596 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
597 break;
598
599 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100600 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100601 Move(location, Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
602 break;
603
604 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100605 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100606 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000607 } else if (instruction->IsTemporary()) {
608 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
609 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100610 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100611 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100612 switch (instruction->GetType()) {
613 case Primitive::kPrimBoolean:
614 case Primitive::kPrimByte:
615 case Primitive::kPrimChar:
616 case Primitive::kPrimShort:
617 case Primitive::kPrimInt:
618 case Primitive::kPrimNot:
619 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100620 case Primitive::kPrimFloat:
621 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100622 Move(location, instruction->GetLocations()->Out());
623 break;
624
625 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100626 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100627 }
628 }
629}
630
631void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
632 got->SetLocations(nullptr);
633}
634
635void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
636 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100637 DCHECK(!successor->IsExitBlock());
638
639 HBasicBlock* block = got->GetBlock();
640 HInstruction* previous = got->GetPrevious();
641
642 HLoopInformation* info = block->GetLoopInformation();
643 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
644 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
645 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
646 return;
647 }
648
649 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
650 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
651 }
652 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100653 __ jmp(codegen_->GetLabelOf(successor));
654 }
655}
656
657void LocationsBuilderX86_64::VisitExit(HExit* exit) {
658 exit->SetLocations(nullptr);
659}
660
661void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700662 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100663 if (kIsDebugBuild) {
664 __ Comment("Unreachable");
665 __ int3();
666 }
667}
668
669void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100670 LocationSummary* locations =
671 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100672 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100673 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100674 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100675 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100676}
677
678void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700679 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100680 if (cond->IsIntConstant()) {
681 // Constant condition, statically compared against 1.
682 int32_t cond_value = cond->AsIntConstant()->GetValue();
683 if (cond_value == 1) {
684 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
685 if_instr->IfTrueSuccessor())) {
686 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100687 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100688 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100689 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100690 DCHECK_EQ(cond_value, 0);
691 }
692 } else {
693 bool materialized =
694 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
695 // Moves do not affect the eflags register, so if the condition is
696 // evaluated just before the if, we don't need to evaluate it
697 // again.
698 bool eflags_set = cond->IsCondition()
699 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
700 if (materialized) {
701 if (!eflags_set) {
702 // Materialized condition, compare against 0.
703 Location lhs = if_instr->GetLocations()->InAt(0);
704 if (lhs.IsRegister()) {
705 __ cmpl(lhs.As<CpuRegister>(), Immediate(0));
706 } else {
707 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
708 Immediate(0));
709 }
710 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
711 } else {
712 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
713 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
714 }
715 } else {
716 Location lhs = cond->GetLocations()->InAt(0);
717 Location rhs = cond->GetLocations()->InAt(1);
718 if (rhs.IsRegister()) {
719 __ cmpl(lhs.As<CpuRegister>(), rhs.As<CpuRegister>());
720 } else if (rhs.IsConstant()) {
721 __ cmpl(lhs.As<CpuRegister>(),
722 Immediate(rhs.GetConstant()->AsIntConstant()->GetValue()));
723 } else {
724 __ cmpl(lhs.As<CpuRegister>(),
725 Address(CpuRegister(RSP), rhs.GetStackIndex()));
726 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100727 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
728 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700729 }
Dave Allison20dfc792014-06-16 20:44:29 -0700730 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100731 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
732 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700733 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100734 }
735}
736
737void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
738 local->SetLocations(nullptr);
739}
740
741void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
742 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
743}
744
745void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
746 local->SetLocations(nullptr);
747}
748
749void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
750 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700751 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100752}
753
754void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100755 LocationSummary* locations =
756 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100757 switch (store->InputAt(1)->GetType()) {
758 case Primitive::kPrimBoolean:
759 case Primitive::kPrimByte:
760 case Primitive::kPrimChar:
761 case Primitive::kPrimShort:
762 case Primitive::kPrimInt:
763 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100764 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100765 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
766 break;
767
768 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100769 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100770 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
771 break;
772
773 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100774 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100775 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100776}
777
778void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700779 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100780}
781
Dave Allison20dfc792014-06-16 20:44:29 -0700782void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100783 LocationSummary* locations =
784 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100785 locations->SetInAt(0, Location::RequiresRegister());
786 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100787 if (comp->NeedsMaterialization()) {
788 locations->SetOut(Location::RequiresRegister());
789 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100790}
791
Dave Allison20dfc792014-06-16 20:44:29 -0700792void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
793 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100794 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100795 CpuRegister reg = locations->Out().As<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100796 // Clear register: setcc only sets the low byte.
797 __ xorq(reg, reg);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100798 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100799 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100800 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100801 } else if (locations->InAt(1).IsConstant()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100802 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100803 Immediate(locations->InAt(1).GetConstant()->AsIntConstant()->GetValue()));
804 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100805 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100806 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
807 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100808 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700809 }
810}
811
812void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
813 VisitCondition(comp);
814}
815
816void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
817 VisitCondition(comp);
818}
819
820void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
821 VisitCondition(comp);
822}
823
824void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
825 VisitCondition(comp);
826}
827
828void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
829 VisitCondition(comp);
830}
831
832void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
833 VisitCondition(comp);
834}
835
836void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
837 VisitCondition(comp);
838}
839
840void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
841 VisitCondition(comp);
842}
843
844void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
845 VisitCondition(comp);
846}
847
848void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
849 VisitCondition(comp);
850}
851
852void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
853 VisitCondition(comp);
854}
855
856void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
857 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100858}
859
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100860void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100861 LocationSummary* locations =
862 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100863 locations->SetInAt(0, Location::RequiresRegister());
864 locations->SetInAt(1, Location::RequiresRegister());
865 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100866}
867
868void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
869 Label greater, done;
870 LocationSummary* locations = compare->GetLocations();
871 switch (compare->InputAt(0)->GetType()) {
872 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100873 __ cmpq(locations->InAt(0).As<CpuRegister>(),
874 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100875 break;
876 default:
877 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
878 }
879
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100880 CpuRegister output = locations->Out().As<CpuRegister>();
881 __ movl(output, Immediate(0));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100882 __ j(kEqual, &done);
883 __ j(kGreater, &greater);
884
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100885 __ movl(output, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100886 __ jmp(&done);
887
888 __ Bind(&greater);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100889 __ movl(output, Immediate(1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100890
891 __ Bind(&done);
892}
893
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100894void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100895 LocationSummary* locations =
896 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100897 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100898}
899
900void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100901 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700902 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100903}
904
905void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100906 LocationSummary* locations =
907 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100908 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100909}
910
911void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100912 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700913 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100914}
915
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100916void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
917 LocationSummary* locations =
918 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
919 locations->SetOut(Location::ConstantLocation(constant));
920}
921
922void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
923 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700924 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100925}
926
927void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
928 LocationSummary* locations =
929 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
930 locations->SetOut(Location::ConstantLocation(constant));
931}
932
933void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
934 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700935 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100936}
937
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100938void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
939 ret->SetLocations(nullptr);
940}
941
942void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700943 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100944 codegen_->GenerateFrameExit();
945 __ ret();
946}
947
948void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100949 LocationSummary* locations =
950 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100951 switch (ret->InputAt(0)->GetType()) {
952 case Primitive::kPrimBoolean:
953 case Primitive::kPrimByte:
954 case Primitive::kPrimChar:
955 case Primitive::kPrimShort:
956 case Primitive::kPrimInt:
957 case Primitive::kPrimNot:
958 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100959 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100960 break;
961
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100962 case Primitive::kPrimFloat:
963 case Primitive::kPrimDouble:
964 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100965 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100966 break;
967
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100968 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100969 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100970 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100971}
972
973void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
974 if (kIsDebugBuild) {
975 switch (ret->InputAt(0)->GetType()) {
976 case Primitive::kPrimBoolean:
977 case Primitive::kPrimByte:
978 case Primitive::kPrimChar:
979 case Primitive::kPrimShort:
980 case Primitive::kPrimInt:
981 case Primitive::kPrimNot:
982 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100983 DCHECK_EQ(ret->GetLocations()->InAt(0).As<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100984 break;
985
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100986 case Primitive::kPrimFloat:
987 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100988 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100989 XMM0);
990 break;
991
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100992 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100993 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100994 }
995 }
996 codegen_->GenerateFrameExit();
997 __ ret();
998}
999
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001000Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
1001 switch (type) {
1002 case Primitive::kPrimBoolean:
1003 case Primitive::kPrimByte:
1004 case Primitive::kPrimChar:
1005 case Primitive::kPrimShort:
1006 case Primitive::kPrimInt:
1007 case Primitive::kPrimNot: {
1008 uint32_t index = gp_index_++;
1009 stack_index_++;
1010 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001011 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001012 } else {
1013 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1014 }
1015 }
1016
1017 case Primitive::kPrimLong: {
1018 uint32_t index = gp_index_;
1019 stack_index_ += 2;
1020 if (index < calling_convention.GetNumberOfRegisters()) {
1021 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001022 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001023 } else {
1024 gp_index_ += 2;
1025 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1026 }
1027 }
1028
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001029 case Primitive::kPrimFloat: {
1030 uint32_t index = fp_index_++;
1031 stack_index_++;
1032 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001033 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001034 } else {
1035 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1036 }
1037 }
1038
1039 case Primitive::kPrimDouble: {
1040 uint32_t index = fp_index_++;
1041 stack_index_ += 2;
1042 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001043 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001044 } else {
1045 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1046 }
1047 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001048
1049 case Primitive::kPrimVoid:
1050 LOG(FATAL) << "Unexpected parameter type " << type;
1051 break;
1052 }
1053 return Location();
1054}
1055
1056void LocationsBuilderX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001057 HandleInvoke(invoke);
1058}
1059
1060void InstructionCodeGeneratorX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001061 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001062 // TODO: Implement all kinds of calls:
1063 // 1) boot -> boot
1064 // 2) app -> boot
1065 // 3) app -> app
1066 //
1067 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1068
1069 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001070 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001071 // temp = temp->dex_cache_resolved_methods_;
1072 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
1073 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001074 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001075 // (temp + offset_of_quick_compiled_code)()
1076 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
1077
1078 DCHECK(!codegen_->IsLeafMethod());
1079 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1080}
1081
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001082void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001083 LocationSummary* locations =
1084 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001085 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001086
1087 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001088 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001089 HInstruction* input = invoke->InputAt(i);
1090 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1091 }
1092
1093 switch (invoke->GetType()) {
1094 case Primitive::kPrimBoolean:
1095 case Primitive::kPrimByte:
1096 case Primitive::kPrimChar:
1097 case Primitive::kPrimShort:
1098 case Primitive::kPrimInt:
1099 case Primitive::kPrimNot:
1100 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001101 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001102 break;
1103
1104 case Primitive::kPrimVoid:
1105 break;
1106
1107 case Primitive::kPrimDouble:
1108 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001109 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001110 break;
1111 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001112}
1113
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001114void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1115 HandleInvoke(invoke);
1116}
1117
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001118void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001119 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001120 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1121 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1122 LocationSummary* locations = invoke->GetLocations();
1123 Location receiver = locations->InAt(0);
1124 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1125 // temp = object->GetClass();
1126 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001127 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1128 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001129 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001130 __ movl(temp, Address(receiver.As<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001131 }
1132 // temp = temp->GetMethodAt(method_offset);
1133 __ movl(temp, Address(temp, method_offset));
1134 // call temp->GetEntryPoint();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001135 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
1136
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001137 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001138 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001139}
1140
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001141void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1142 HandleInvoke(invoke);
1143 // Add the hidden argument.
1144 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1145}
1146
1147void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1148 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1149 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
1150 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1151 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1152 LocationSummary* locations = invoke->GetLocations();
1153 Location receiver = locations->InAt(0);
1154 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1155
1156 // Set the hidden argument.
1157 __ movq(invoke->GetLocations()->GetTemp(1).As<CpuRegister>(),
1158 Immediate(invoke->GetDexMethodIndex()));
1159
1160 // temp = object->GetClass();
1161 if (receiver.IsStackSlot()) {
1162 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1163 __ movl(temp, Address(temp, class_offset));
1164 } else {
1165 __ movl(temp, Address(receiver.As<CpuRegister>(), class_offset));
1166 }
1167 // temp = temp->GetImtEntryAt(method_offset);
1168 __ movl(temp, Address(temp, method_offset));
1169 // call temp->GetEntryPoint();
1170 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
1171
1172 DCHECK(!codegen_->IsLeafMethod());
1173 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1174}
1175
Roland Levillain88cb1752014-10-20 16:36:47 +01001176void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1177 LocationSummary* locations =
1178 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1179 switch (neg->GetResultType()) {
1180 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001181 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001182 locations->SetInAt(0, Location::RequiresRegister());
1183 locations->SetOut(Location::SameAsFirstInput());
1184 break;
1185
Roland Levillain88cb1752014-10-20 16:36:47 +01001186 case Primitive::kPrimFloat:
1187 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001188 locations->SetInAt(0, Location::RequiresFpuRegister());
1189 // Output overlaps as we need a fresh (zero-initialized)
1190 // register to perform subtraction from zero.
1191 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001192 break;
1193
1194 default:
1195 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1196 }
1197}
1198
1199void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1200 LocationSummary* locations = neg->GetLocations();
1201 Location out = locations->Out();
1202 Location in = locations->InAt(0);
1203 switch (neg->GetResultType()) {
1204 case Primitive::kPrimInt:
1205 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001206 DCHECK(in.Equals(out));
Roland Levillain88cb1752014-10-20 16:36:47 +01001207 __ negl(out.As<CpuRegister>());
1208 break;
1209
1210 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001211 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001212 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001213 __ negq(out.As<CpuRegister>());
1214 break;
1215
Roland Levillain88cb1752014-10-20 16:36:47 +01001216 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001217 DCHECK(in.IsFpuRegister());
1218 DCHECK(out.IsFpuRegister());
1219 DCHECK(!in.Equals(out));
1220 // TODO: Instead of computing negation as a subtraction from
1221 // zero, implement it with an exclusive or with value 0x80000000
1222 // (mask for bit 31, representing the sign of a single-precision
1223 // floating-point number), fetched from a constant pool:
1224 //
1225 // xorps out, [RIP:...] // value at RIP is 0x80 00 00 00
1226
1227 // out = 0
1228 __ xorps(out.As<XmmRegister>(), out.As<XmmRegister>());
1229 // out = out - in
1230 __ subss(out.As<XmmRegister>(), in.As<XmmRegister>());
1231 break;
1232
Roland Levillain88cb1752014-10-20 16:36:47 +01001233 case Primitive::kPrimDouble:
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
1239 // 0x8000000000000000 (mask for bit 63, representing the sign of
1240 // a double-precision floating-point number), fetched from a
1241 // constant pool:
1242 //
1243 // xorpd out, [RIP:...] // value at RIP is 0x80 00 00 00 00 00 00 00
1244
1245 // out = 0
1246 __ xorpd(out.As<XmmRegister>(), out.As<XmmRegister>());
1247 // out = out - in
1248 __ subsd(out.As<XmmRegister>(), in.As<XmmRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001249 break;
1250
1251 default:
1252 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1253 }
1254}
1255
Roland Levillaindff1f282014-11-05 14:15:05 +00001256void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1257 LocationSummary* locations =
1258 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1259 Primitive::Type result_type = conversion->GetResultType();
1260 Primitive::Type input_type = conversion->GetInputType();
1261 switch (result_type) {
Roland Levillain946e1432014-11-11 17:35:19 +00001262 case Primitive::kPrimInt:
1263 switch (input_type) {
1264 case Primitive::kPrimLong:
1265 // long-to-int conversion.
1266 locations->SetInAt(0, Location::Any());
1267 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1268 break;
1269
1270 case Primitive::kPrimFloat:
1271 case Primitive::kPrimDouble:
1272 LOG(FATAL) << "Type conversion from " << input_type
1273 << " to " << result_type << " not yet implemented";
1274 break;
1275
1276 default:
1277 LOG(FATAL) << "Unexpected type conversion from " << input_type
1278 << " to " << result_type;
1279 }
1280 break;
1281
Roland Levillaindff1f282014-11-05 14:15:05 +00001282 case Primitive::kPrimLong:
1283 switch (input_type) {
1284 case Primitive::kPrimByte:
1285 case Primitive::kPrimShort:
1286 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001287 case Primitive::kPrimChar:
Roland Levillaindff1f282014-11-05 14:15:05 +00001288 // int-to-long conversion.
1289 // TODO: We would benefit from a (to-be-implemented)
1290 // Location::RegisterOrStackSlot requirement for this input.
1291 locations->SetInAt(0, Location::RequiresRegister());
1292 locations->SetOut(Location::RequiresRegister());
1293 break;
1294
1295 case Primitive::kPrimFloat:
1296 case Primitive::kPrimDouble:
1297 LOG(FATAL) << "Type conversion from " << input_type << " to "
1298 << result_type << " not yet implemented";
1299 break;
1300
1301 default:
1302 LOG(FATAL) << "Unexpected type conversion from " << input_type
1303 << " to " << result_type;
1304 }
1305 break;
1306
Roland Levillaindff1f282014-11-05 14:15:05 +00001307 case Primitive::kPrimFloat:
1308 case Primitive::kPrimDouble:
1309 LOG(FATAL) << "Type conversion from " << input_type
1310 << " to " << result_type << " not yet implemented";
1311 break;
1312
1313 default:
1314 LOG(FATAL) << "Unexpected type conversion from " << input_type
1315 << " to " << result_type;
1316 }
1317}
1318
1319void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1320 LocationSummary* locations = conversion->GetLocations();
1321 Location out = locations->Out();
1322 Location in = locations->InAt(0);
1323 Primitive::Type result_type = conversion->GetResultType();
1324 Primitive::Type input_type = conversion->GetInputType();
1325 switch (result_type) {
Roland Levillain946e1432014-11-11 17:35:19 +00001326 case Primitive::kPrimInt:
1327 switch (input_type) {
1328 case Primitive::kPrimLong:
1329 // long-to-int conversion.
1330 if (in.IsRegister()) {
1331 __ movl(out.As<CpuRegister>(), in.As<CpuRegister>());
1332 } else if (in.IsDoubleStackSlot()) {
1333 __ movl(out.As<CpuRegister>(),
1334 Address(CpuRegister(RSP), in.GetStackIndex()));
1335 } else {
1336 DCHECK(in.IsConstant());
1337 DCHECK(in.GetConstant()->IsLongConstant());
1338 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
1339 __ movl(out.As<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1340 }
1341 break;
1342
1343 case Primitive::kPrimFloat:
1344 case Primitive::kPrimDouble:
1345 LOG(FATAL) << "Type conversion from " << input_type
1346 << " to " << result_type << " not yet implemented";
1347 break;
1348
1349 default:
1350 LOG(FATAL) << "Unexpected type conversion from " << input_type
1351 << " to " << result_type;
1352 }
1353 break;
1354
Roland Levillaindff1f282014-11-05 14:15:05 +00001355 case Primitive::kPrimLong:
1356 switch (input_type) {
1357 DCHECK(out.IsRegister());
1358 case Primitive::kPrimByte:
1359 case Primitive::kPrimShort:
1360 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001361 case Primitive::kPrimChar:
Roland Levillaindff1f282014-11-05 14:15:05 +00001362 // int-to-long conversion.
1363 DCHECK(in.IsRegister());
1364 __ movsxd(out.As<CpuRegister>(), in.As<CpuRegister>());
1365 break;
1366
1367 case Primitive::kPrimFloat:
1368 case Primitive::kPrimDouble:
1369 LOG(FATAL) << "Type conversion from " << input_type << " to "
1370 << result_type << " not yet implemented";
1371 break;
1372
1373 default:
1374 LOG(FATAL) << "Unexpected type conversion from " << input_type
1375 << " to " << result_type;
1376 }
1377 break;
1378
Roland Levillaindff1f282014-11-05 14:15:05 +00001379 case Primitive::kPrimFloat:
1380 case Primitive::kPrimDouble:
1381 LOG(FATAL) << "Type conversion from " << input_type
1382 << " to " << result_type << " not yet implemented";
1383 break;
1384
1385 default:
1386 LOG(FATAL) << "Unexpected type conversion from " << input_type
1387 << " to " << result_type;
1388 }
1389}
1390
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001391void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001392 LocationSummary* locations =
1393 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001394 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001395 case Primitive::kPrimInt: {
1396 locations->SetInAt(0, Location::RequiresRegister());
1397 locations->SetInAt(1, Location::Any());
1398 locations->SetOut(Location::SameAsFirstInput());
1399 break;
1400 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001401
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001402 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001403 locations->SetInAt(0, Location::RequiresRegister());
1404 locations->SetInAt(1, Location::RequiresRegister());
1405 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001406 break;
1407 }
1408
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001409 case Primitive::kPrimDouble:
1410 case Primitive::kPrimFloat: {
1411 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001412 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001413 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001414 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001415 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001416
1417 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001418 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001419 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001420}
1421
1422void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1423 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001424 Location first = locations->InAt(0);
1425 Location second = locations->InAt(1);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001426 DCHECK(first.Equals(locations->Out()));
Calin Juravle11351682014-10-23 15:38:15 +01001427
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001428 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001429 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001430 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001431 __ addl(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001432 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001433 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001434 __ addl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001435 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001436 __ addl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001437 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001438 break;
1439 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001440
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001441 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001442 __ addq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001443 break;
1444 }
1445
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001446 case Primitive::kPrimFloat: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001447 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001448 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001449 }
1450
1451 case Primitive::kPrimDouble: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001452 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001453 break;
1454 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001455
1456 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001457 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001458 }
1459}
1460
1461void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001462 LocationSummary* locations =
1463 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001464 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001465 case Primitive::kPrimInt: {
1466 locations->SetInAt(0, Location::RequiresRegister());
1467 locations->SetInAt(1, Location::Any());
1468 locations->SetOut(Location::SameAsFirstInput());
1469 break;
1470 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001471 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001472 locations->SetInAt(0, Location::RequiresRegister());
1473 locations->SetInAt(1, Location::RequiresRegister());
1474 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001475 break;
1476 }
Calin Juravle11351682014-10-23 15:38:15 +01001477 case Primitive::kPrimFloat:
1478 case Primitive::kPrimDouble: {
1479 locations->SetInAt(0, Location::RequiresFpuRegister());
1480 locations->SetInAt(1, Location::RequiresFpuRegister());
1481 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001482 break;
Calin Juravle11351682014-10-23 15:38:15 +01001483 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001484 default:
Calin Juravle11351682014-10-23 15:38:15 +01001485 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001486 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001487}
1488
1489void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1490 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001491 Location first = locations->InAt(0);
1492 Location second = locations->InAt(1);
1493 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001494 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001495 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001496 if (second.IsRegister()) {
1497 __ subl(first.As<CpuRegister>(), second.As<CpuRegister>());
1498 } else if (second.IsConstant()) {
1499 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1500 __ subl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001501 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001502 __ subl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001503 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001504 break;
1505 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001506 case Primitive::kPrimLong: {
Calin Juravle11351682014-10-23 15:38:15 +01001507 __ subq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001508 break;
1509 }
1510
Calin Juravle11351682014-10-23 15:38:15 +01001511 case Primitive::kPrimFloat: {
1512 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001513 break;
Calin Juravle11351682014-10-23 15:38:15 +01001514 }
1515
1516 case Primitive::kPrimDouble: {
1517 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1518 break;
1519 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001520
1521 default:
Calin Juravle11351682014-10-23 15:38:15 +01001522 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001523 }
1524}
1525
Calin Juravle34bacdf2014-10-07 20:23:36 +01001526void LocationsBuilderX86_64::VisitMul(HMul* mul) {
1527 LocationSummary* locations =
1528 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1529 switch (mul->GetResultType()) {
1530 case Primitive::kPrimInt: {
1531 locations->SetInAt(0, Location::RequiresRegister());
1532 locations->SetInAt(1, Location::Any());
1533 locations->SetOut(Location::SameAsFirstInput());
1534 break;
1535 }
1536 case Primitive::kPrimLong: {
1537 locations->SetInAt(0, Location::RequiresRegister());
1538 locations->SetInAt(1, Location::RequiresRegister());
1539 locations->SetOut(Location::SameAsFirstInput());
1540 break;
1541 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001542 case Primitive::kPrimFloat:
1543 case Primitive::kPrimDouble: {
1544 locations->SetInAt(0, Location::RequiresFpuRegister());
1545 locations->SetInAt(1, Location::RequiresFpuRegister());
1546 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001547 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001548 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001549
1550 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001551 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001552 }
1553}
1554
1555void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
1556 LocationSummary* locations = mul->GetLocations();
1557 Location first = locations->InAt(0);
1558 Location second = locations->InAt(1);
1559 DCHECK(first.Equals(locations->Out()));
1560 switch (mul->GetResultType()) {
1561 case Primitive::kPrimInt: {
1562 if (second.IsRegister()) {
1563 __ imull(first.As<CpuRegister>(), second.As<CpuRegister>());
1564 } else if (second.IsConstant()) {
1565 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1566 __ imull(first.As<CpuRegister>(), imm);
1567 } else {
1568 DCHECK(second.IsStackSlot());
1569 __ imull(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
1570 }
1571 break;
1572 }
1573 case Primitive::kPrimLong: {
1574 __ imulq(first.As<CpuRegister>(), second.As<CpuRegister>());
1575 break;
1576 }
1577
Calin Juravleb5bfa962014-10-21 18:02:24 +01001578 case Primitive::kPrimFloat: {
1579 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001580 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001581 }
1582
1583 case Primitive::kPrimDouble: {
1584 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1585 break;
1586 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001587
1588 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001589 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001590 }
1591}
1592
Calin Juravle7c4954d2014-10-28 16:57:40 +00001593void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
1594 LocationSummary* locations =
1595 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1596 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001597 case Primitive::kPrimInt: {
1598 locations->SetInAt(0, Location::RegisterLocation(RAX));
1599 locations->SetInAt(1, Location::RequiresRegister());
1600 locations->SetOut(Location::SameAsFirstInput());
1601 // Intel uses edx:eax as the dividend.
1602 locations->AddTemp(Location::RegisterLocation(RDX));
1603 break;
1604 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001605 case Primitive::kPrimLong: {
1606 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1607 break;
1608 }
1609 case Primitive::kPrimFloat:
1610 case Primitive::kPrimDouble: {
1611 locations->SetInAt(0, Location::RequiresFpuRegister());
1612 locations->SetInAt(1, Location::RequiresFpuRegister());
1613 locations->SetOut(Location::SameAsFirstInput());
1614 break;
1615 }
1616
1617 default:
1618 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1619 }
1620}
1621
1622void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
1623 LocationSummary* locations = div->GetLocations();
1624 Location first = locations->InAt(0);
1625 Location second = locations->InAt(1);
1626 DCHECK(first.Equals(locations->Out()));
1627
1628 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001629 case Primitive::kPrimInt: {
1630 CpuRegister first_reg = first.As<CpuRegister>();
1631 CpuRegister second_reg = second.As<CpuRegister>();
1632 DCHECK_EQ(RAX, first_reg.AsRegister());
1633 DCHECK_EQ(RDX, locations->GetTemp(0).As<CpuRegister>().AsRegister());
1634
1635 SlowPathCodeX86_64* slow_path =
1636 new (GetGraph()->GetArena()) DivMinusOneSlowPathX86_64(first_reg.AsRegister());
1637 codegen_->AddSlowPath(slow_path);
1638
1639 // 0x80000000/-1 triggers an arithmetic exception!
1640 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
1641 // it's safe to just use negl instead of more complex comparisons.
1642
1643 __ cmpl(second_reg, Immediate(-1));
1644 __ j(kEqual, slow_path->GetEntryLabel());
1645
1646 // edx:eax <- sign-extended of eax
1647 __ cdq();
1648 // eax = quotient, edx = remainder
1649 __ idivl(second_reg);
1650
1651 __ Bind(slow_path->GetExitLabel());
1652 break;
1653 }
1654
Calin Juravle7c4954d2014-10-28 16:57:40 +00001655 case Primitive::kPrimLong: {
1656 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1657 break;
1658 }
1659
1660 case Primitive::kPrimFloat: {
1661 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1662 break;
1663 }
1664
1665 case Primitive::kPrimDouble: {
1666 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1667 break;
1668 }
1669
1670 default:
1671 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1672 }
1673}
1674
Calin Juravled0d48522014-11-04 16:40:20 +00001675void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1676 LocationSummary* locations =
1677 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1678 locations->SetInAt(0, Location::Any());
1679 if (instruction->HasUses()) {
1680 locations->SetOut(Location::SameAsFirstInput());
1681 }
1682}
1683
1684void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1685 SlowPathCodeX86_64* slow_path =
1686 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
1687 codegen_->AddSlowPath(slow_path);
1688
1689 LocationSummary* locations = instruction->GetLocations();
1690 Location value = locations->InAt(0);
1691
1692 if (value.IsRegister()) {
1693 __ testl(value.As<CpuRegister>(), value.As<CpuRegister>());
1694 } else if (value.IsStackSlot()) {
1695 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
1696 } else {
1697 DCHECK(value.IsConstant()) << value;
1698 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
1699 __ jmp(slow_path->GetEntryLabel());
1700 }
1701 return;
1702 }
1703 __ j(kEqual, slow_path->GetEntryLabel());
1704}
1705
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001706void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001707 LocationSummary* locations =
1708 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001709 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001710 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1711 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1712 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001713}
1714
1715void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
1716 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001717 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001718 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1719
1720 __ gs()->call(Address::Absolute(
1721 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
1722
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001723 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001724 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001725}
1726
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001727void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
1728 LocationSummary* locations =
1729 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1730 InvokeRuntimeCallingConvention calling_convention;
1731 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1732 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1733 locations->SetOut(Location::RegisterLocation(RAX));
1734 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1735}
1736
1737void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
1738 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001739 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001740 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1741
1742 __ gs()->call(Address::Absolute(
1743 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocArrayWithAccessCheck), true));
1744
1745 DCHECK(!codegen_->IsLeafMethod());
1746 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1747}
1748
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001749void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001750 LocationSummary* locations =
1751 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001752 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1753 if (location.IsStackSlot()) {
1754 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1755 } else if (location.IsDoubleStackSlot()) {
1756 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1757 }
1758 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001759}
1760
1761void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
1762 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001763 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001764}
1765
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001766void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001767 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001768 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001769 locations->SetInAt(0, Location::RequiresRegister());
1770 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001771}
1772
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001773void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
1774 LocationSummary* locations = not_->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001775 DCHECK_EQ(locations->InAt(0).As<CpuRegister>().AsRegister(),
1776 locations->Out().As<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001777 Location out = locations->Out();
1778 switch (not_->InputAt(0)->GetType()) {
1779 case Primitive::kPrimBoolean:
1780 __ xorq(out.As<CpuRegister>(), Immediate(1));
1781 break;
1782
1783 case Primitive::kPrimInt:
1784 __ notl(out.As<CpuRegister>());
1785 break;
1786
1787 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001788 __ notq(out.As<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001789 break;
1790
1791 default:
1792 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1793 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001794}
1795
1796void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001797 LocationSummary* locations =
1798 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001799 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1800 locations->SetInAt(i, Location::Any());
1801 }
1802 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001803}
1804
1805void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001806 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001807 LOG(FATAL) << "Unimplemented";
1808}
1809
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001810void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001811 LocationSummary* locations =
1812 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001813 Primitive::Type field_type = instruction->GetFieldType();
1814 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001815 locations->SetInAt(0, Location::RequiresRegister());
1816 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001817 if (is_object_type) {
1818 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001819 locations->AddTemp(Location::RequiresRegister());
1820 locations->AddTemp(Location::RequiresRegister());
1821 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001822}
1823
1824void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1825 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001826 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001827 size_t offset = instruction->GetFieldOffset().SizeValue();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001828 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001829
1830 switch (field_type) {
1831 case Primitive::kPrimBoolean:
1832 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001833 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001834 __ movb(Address(obj, offset), value);
1835 break;
1836 }
1837
1838 case Primitive::kPrimShort:
1839 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001840 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001841 __ movw(Address(obj, offset), value);
1842 break;
1843 }
1844
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001845 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001846 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001847 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001848 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001849 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001850 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
1851 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001852 codegen_->MarkGCCard(temp, card, obj, value);
1853 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001854 break;
1855 }
1856
1857 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001858 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001859 __ movq(Address(obj, offset), value);
1860 break;
1861 }
1862
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001863 case Primitive::kPrimFloat: {
1864 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1865 __ movss(Address(obj, offset), value);
1866 break;
1867 }
1868
1869 case Primitive::kPrimDouble: {
1870 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1871 __ movsd(Address(obj, offset), value);
1872 break;
1873 }
1874
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001875 case Primitive::kPrimVoid:
1876 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001877 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001878 }
1879}
1880
1881void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001882 LocationSummary* locations =
1883 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001884 locations->SetInAt(0, Location::RequiresRegister());
1885 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001886}
1887
1888void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1889 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001890 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001891 size_t offset = instruction->GetFieldOffset().SizeValue();
1892
1893 switch (instruction->GetType()) {
1894 case Primitive::kPrimBoolean: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001895 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001896 __ movzxb(out, Address(obj, offset));
1897 break;
1898 }
1899
1900 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001901 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001902 __ movsxb(out, Address(obj, offset));
1903 break;
1904 }
1905
1906 case Primitive::kPrimShort: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001907 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001908 __ movsxw(out, Address(obj, offset));
1909 break;
1910 }
1911
1912 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001913 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001914 __ movzxw(out, Address(obj, offset));
1915 break;
1916 }
1917
1918 case Primitive::kPrimInt:
1919 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001920 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001921 __ movl(out, Address(obj, offset));
1922 break;
1923 }
1924
1925 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001926 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001927 __ movq(out, Address(obj, offset));
1928 break;
1929 }
1930
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001931 case Primitive::kPrimFloat: {
1932 XmmRegister out = locations->Out().As<XmmRegister>();
1933 __ movss(out, Address(obj, offset));
1934 break;
1935 }
1936
1937 case Primitive::kPrimDouble: {
1938 XmmRegister out = locations->Out().As<XmmRegister>();
1939 __ movsd(out, Address(obj, offset));
1940 break;
1941 }
1942
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001943 case Primitive::kPrimVoid:
1944 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001945 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001946 }
1947}
1948
1949void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001950 LocationSummary* locations =
1951 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001952 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001953 if (instruction->HasUses()) {
1954 locations->SetOut(Location::SameAsFirstInput());
1955 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001956}
1957
1958void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001959 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001960 codegen_->AddSlowPath(slow_path);
1961
1962 LocationSummary* locations = instruction->GetLocations();
1963 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001964
1965 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001966 __ cmpl(obj.As<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001967 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001968 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001969 } else {
1970 DCHECK(obj.IsConstant()) << obj;
1971 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1972 __ jmp(slow_path->GetEntryLabel());
1973 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001974 }
1975 __ j(kEqual, slow_path->GetEntryLabel());
1976}
1977
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001978void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001979 LocationSummary* locations =
1980 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001981 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001982 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001983 1, Location::RegisterOrConstant(instruction->InputAt(1)));
1984 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001985}
1986
1987void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
1988 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001989 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001990 Location index = locations->InAt(1);
1991
1992 switch (instruction->GetType()) {
1993 case Primitive::kPrimBoolean: {
1994 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001995 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001996 if (index.IsConstant()) {
1997 __ movzxb(out, Address(obj,
1998 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1999 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002000 __ movzxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002001 }
2002 break;
2003 }
2004
2005 case Primitive::kPrimByte: {
2006 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002007 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002008 if (index.IsConstant()) {
2009 __ movsxb(out, Address(obj,
2010 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2011 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002012 __ movsxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002013 }
2014 break;
2015 }
2016
2017 case Primitive::kPrimShort: {
2018 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002019 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002020 if (index.IsConstant()) {
2021 __ movsxw(out, Address(obj,
2022 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2023 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002024 __ movsxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002025 }
2026 break;
2027 }
2028
2029 case Primitive::kPrimChar: {
2030 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002031 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002032 if (index.IsConstant()) {
2033 __ movzxw(out, Address(obj,
2034 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2035 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002036 __ movzxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002037 }
2038 break;
2039 }
2040
2041 case Primitive::kPrimInt:
2042 case Primitive::kPrimNot: {
2043 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
2044 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002045 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002046 if (index.IsConstant()) {
2047 __ movl(out, Address(obj,
2048 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2049 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002050 __ movl(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002051 }
2052 break;
2053 }
2054
2055 case Primitive::kPrimLong: {
2056 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002057 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002058 if (index.IsConstant()) {
2059 __ movq(out, Address(obj,
2060 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2061 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002062 __ movq(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002063 }
2064 break;
2065 }
2066
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002067 case Primitive::kPrimFloat: {
2068 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2069 XmmRegister out = locations->Out().As<XmmRegister>();
2070 if (index.IsConstant()) {
2071 __ movss(out, Address(obj,
2072 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2073 } else {
2074 __ movss(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
2075 }
2076 break;
2077 }
2078
2079 case Primitive::kPrimDouble: {
2080 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2081 XmmRegister out = locations->Out().As<XmmRegister>();
2082 if (index.IsConstant()) {
2083 __ movsd(out, Address(obj,
2084 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2085 } else {
2086 __ movsd(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
2087 }
2088 break;
2089 }
2090
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002091 case Primitive::kPrimVoid:
2092 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002093 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002094 }
2095}
2096
2097void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002098 Primitive::Type value_type = instruction->GetComponentType();
2099 bool is_object = value_type == Primitive::kPrimNot;
2100 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2101 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
2102 if (is_object) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002103 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002104 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2105 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2106 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002107 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002108 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002109 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002110 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2111 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002112 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002113 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002114 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
2115 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002116 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002117 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002118 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002119 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002120}
2121
2122void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
2123 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002124 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002125 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002126 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002127 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002128
2129 switch (value_type) {
2130 case Primitive::kPrimBoolean:
2131 case Primitive::kPrimByte: {
2132 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002133 if (index.IsConstant()) {
2134 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002135 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002136 __ movb(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002137 } else {
2138 __ movb(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2139 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002140 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002141 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002142 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
2143 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002144 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002145 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002146 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2147 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002148 }
2149 break;
2150 }
2151
2152 case Primitive::kPrimShort:
2153 case Primitive::kPrimChar: {
2154 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002155 if (index.IsConstant()) {
2156 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002157 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002158 __ movw(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002159 } else {
2160 __ movw(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2161 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002162 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002163 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002164 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
2165 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002166 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002167 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002168 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2169 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002170 }
2171 break;
2172 }
2173
2174 case Primitive::kPrimInt: {
2175 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002176 if (index.IsConstant()) {
2177 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002178 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002179 __ movl(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002180 } else {
2181 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2182 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002183 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002184 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002185 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2186 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002187 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002188 DCHECK(value.IsConstant()) << value;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002189 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002190 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2191 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002192 }
2193 break;
2194 }
2195
2196 case Primitive::kPrimNot: {
2197 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject), true));
2198 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002199 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002200 break;
2201 }
2202
2203 case Primitive::kPrimLong: {
2204 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002205 if (index.IsConstant()) {
2206 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002207 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002208 __ movq(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002209 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002210 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002211 __ movq(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
2212 value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002213 }
2214 break;
2215 }
2216
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002217 case Primitive::kPrimFloat: {
2218 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2219 if (index.IsConstant()) {
2220 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2221 DCHECK(value.IsFpuRegister());
2222 __ movss(Address(obj, offset), value.As<XmmRegister>());
2223 } else {
2224 DCHECK(value.IsFpuRegister());
2225 __ movss(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2226 value.As<XmmRegister>());
2227 }
2228 break;
2229 }
2230
2231 case Primitive::kPrimDouble: {
2232 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2233 if (index.IsConstant()) {
2234 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
2235 DCHECK(value.IsFpuRegister());
2236 __ movsd(Address(obj, offset), value.As<XmmRegister>());
2237 } else {
2238 DCHECK(value.IsFpuRegister());
2239 __ movsd(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
2240 value.As<XmmRegister>());
2241 }
2242 break;
2243 }
2244
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002245 case Primitive::kPrimVoid:
2246 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002247 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002248 }
2249}
2250
2251void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002252 LocationSummary* locations =
2253 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002254 locations->SetInAt(0, Location::RequiresRegister());
2255 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002256}
2257
2258void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
2259 LocationSummary* locations = instruction->GetLocations();
2260 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002261 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2262 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002263 __ movl(out, Address(obj, offset));
2264}
2265
2266void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002267 LocationSummary* locations =
2268 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002269 locations->SetInAt(0, Location::RequiresRegister());
2270 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002271 if (instruction->HasUses()) {
2272 locations->SetOut(Location::SameAsFirstInput());
2273 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002274}
2275
2276void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
2277 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002278 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002279 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002280 codegen_->AddSlowPath(slow_path);
2281
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002282 CpuRegister index = locations->InAt(0).As<CpuRegister>();
2283 CpuRegister length = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002284
2285 __ cmpl(index, length);
2286 __ j(kAboveEqual, slow_path->GetEntryLabel());
2287}
2288
2289void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
2290 CpuRegister card,
2291 CpuRegister object,
2292 CpuRegister value) {
2293 Label is_null;
2294 __ testl(value, value);
2295 __ j(kEqual, &is_null);
2296 __ gs()->movq(card, Address::Absolute(
2297 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
2298 __ movq(temp, object);
2299 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
2300 __ movb(Address(temp, card, TIMES_1, 0), card);
2301 __ Bind(&is_null);
2302}
2303
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002304void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
2305 temp->SetLocations(nullptr);
2306}
2307
2308void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
2309 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002310 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002311}
2312
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002313void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002314 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002315 LOG(FATAL) << "Unimplemented";
2316}
2317
2318void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002319 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2320}
2321
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002322void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
2323 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2324}
2325
2326void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002327 HBasicBlock* block = instruction->GetBlock();
2328 if (block->GetLoopInformation() != nullptr) {
2329 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2330 // The back edge will generate the suspend check.
2331 return;
2332 }
2333 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2334 // The goto will generate the suspend check.
2335 return;
2336 }
2337 GenerateSuspendCheck(instruction, nullptr);
2338}
2339
2340void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
2341 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002342 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002343 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002344 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002345 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002346 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002347 if (successor == nullptr) {
2348 __ j(kNotEqual, slow_path->GetEntryLabel());
2349 __ Bind(slow_path->GetReturnLabel());
2350 } else {
2351 __ j(kEqual, codegen_->GetLabelOf(successor));
2352 __ jmp(slow_path->GetEntryLabel());
2353 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002354}
2355
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002356X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
2357 return codegen_->GetAssembler();
2358}
2359
2360void ParallelMoveResolverX86_64::EmitMove(size_t index) {
2361 MoveOperands* move = moves_.Get(index);
2362 Location source = move->GetSource();
2363 Location destination = move->GetDestination();
2364
2365 if (source.IsRegister()) {
2366 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002367 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002368 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002369 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002370 source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002371 } else {
2372 DCHECK(destination.IsDoubleStackSlot());
2373 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002374 source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002375 }
2376 } else if (source.IsStackSlot()) {
2377 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002378 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002379 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002380 } else if (destination.IsFpuRegister()) {
2381 __ movss(destination.As<XmmRegister>(),
2382 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002383 } else {
2384 DCHECK(destination.IsStackSlot());
2385 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2386 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2387 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002388 } else if (source.IsDoubleStackSlot()) {
2389 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002390 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002391 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002392 } else if (destination.IsFpuRegister()) {
2393 __ movsd(destination.As<XmmRegister>(), Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002394 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01002395 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002396 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2397 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2398 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002399 } else if (source.IsConstant()) {
2400 HConstant* constant = source.GetConstant();
2401 if (constant->IsIntConstant()) {
2402 Immediate imm(constant->AsIntConstant()->GetValue());
2403 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002404 __ movl(destination.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002405 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002406 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002407 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2408 }
2409 } else if (constant->IsLongConstant()) {
2410 int64_t value = constant->AsLongConstant()->GetValue();
2411 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002412 __ movq(destination.As<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002413 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002414 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002415 __ movq(CpuRegister(TMP), Immediate(value));
2416 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2417 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002418 } else if (constant->IsFloatConstant()) {
2419 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
2420 if (destination.IsFpuRegister()) {
2421 __ movl(CpuRegister(TMP), imm);
2422 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2423 } else {
2424 DCHECK(destination.IsStackSlot()) << destination;
2425 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2426 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002427 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002428 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
2429 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
2430 if (destination.IsFpuRegister()) {
2431 __ movq(CpuRegister(TMP), imm);
2432 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2433 } else {
2434 DCHECK(destination.IsDoubleStackSlot()) << destination;
2435 __ movq(CpuRegister(TMP), imm);
2436 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2437 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002438 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002439 } else if (source.IsFpuRegister()) {
2440 if (destination.IsFpuRegister()) {
2441 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
2442 } else if (destination.IsStackSlot()) {
2443 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
2444 source.As<XmmRegister>());
2445 } else {
2446 DCHECK(destination.IsDoubleStackSlot());
2447 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
2448 source.As<XmmRegister>());
2449 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002450 }
2451}
2452
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002453void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002454 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002455 __ movl(Address(CpuRegister(RSP), mem), reg);
2456 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002457}
2458
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002459void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002460 ScratchRegisterScope ensure_scratch(
2461 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2462
2463 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2464 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2465 __ movl(CpuRegister(ensure_scratch.GetRegister()),
2466 Address(CpuRegister(RSP), mem2 + stack_offset));
2467 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2468 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
2469 CpuRegister(ensure_scratch.GetRegister()));
2470}
2471
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002472void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
2473 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2474 __ movq(Address(CpuRegister(RSP), mem), reg);
2475 __ movq(reg, CpuRegister(TMP));
2476}
2477
2478void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
2479 ScratchRegisterScope ensure_scratch(
2480 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2481
2482 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2483 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2484 __ movq(CpuRegister(ensure_scratch.GetRegister()),
2485 Address(CpuRegister(RSP), mem2 + stack_offset));
2486 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2487 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
2488 CpuRegister(ensure_scratch.GetRegister()));
2489}
2490
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002491void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
2492 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2493 __ movss(Address(CpuRegister(RSP), mem), reg);
2494 __ movd(reg, CpuRegister(TMP));
2495}
2496
2497void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
2498 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2499 __ movsd(Address(CpuRegister(RSP), mem), reg);
2500 __ movd(reg, CpuRegister(TMP));
2501}
2502
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002503void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
2504 MoveOperands* move = moves_.Get(index);
2505 Location source = move->GetSource();
2506 Location destination = move->GetDestination();
2507
2508 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002509 __ xchgq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002510 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002511 Exchange32(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002512 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002513 Exchange32(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002514 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002515 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
2516 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002517 Exchange64(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002518 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002519 Exchange64(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002520 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
2521 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002522 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
2523 __ movd(CpuRegister(TMP), source.As<XmmRegister>());
2524 __ movaps(source.As<XmmRegister>(), destination.As<XmmRegister>());
2525 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2526 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
2527 Exchange32(source.As<XmmRegister>(), destination.GetStackIndex());
2528 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
2529 Exchange32(destination.As<XmmRegister>(), source.GetStackIndex());
2530 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
2531 Exchange64(source.As<XmmRegister>(), destination.GetStackIndex());
2532 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
2533 Exchange64(destination.As<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002534 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002535 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002536 }
2537}
2538
2539
2540void ParallelMoveResolverX86_64::SpillScratch(int reg) {
2541 __ pushq(CpuRegister(reg));
2542}
2543
2544
2545void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
2546 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002547}
2548
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002549void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
2550 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
2551 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2552 Immediate(mirror::Class::kStatusInitialized));
2553 __ j(kLess, slow_path->GetEntryLabel());
2554 __ Bind(slow_path->GetExitLabel());
2555 // No need for memory fence, thanks to the X86_64 memory model.
2556}
2557
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002558void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002559 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2560 ? LocationSummary::kCallOnSlowPath
2561 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002562 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002563 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002564 locations->SetOut(Location::RequiresRegister());
2565}
2566
2567void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
2568 CpuRegister out = cls->GetLocations()->Out().As<CpuRegister>();
2569 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002570 DCHECK(!cls->CanCallRuntime());
2571 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002572 codegen_->LoadCurrentMethod(out);
2573 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2574 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002575 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002576 codegen_->LoadCurrentMethod(out);
2577 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2578 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002579 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
2580 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2581 codegen_->AddSlowPath(slow_path);
2582 __ testl(out, out);
2583 __ j(kEqual, slow_path->GetEntryLabel());
2584 if (cls->MustGenerateClinitCheck()) {
2585 GenerateClassInitializationCheck(slow_path, out);
2586 } else {
2587 __ Bind(slow_path->GetExitLabel());
2588 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002589 }
2590}
2591
2592void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
2593 LocationSummary* locations =
2594 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2595 locations->SetInAt(0, Location::RequiresRegister());
2596 if (check->HasUses()) {
2597 locations->SetOut(Location::SameAsFirstInput());
2598 }
2599}
2600
2601void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002602 // We assume the class to not be null.
2603 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
2604 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002605 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002606 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002607}
2608
2609void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2610 LocationSummary* locations =
2611 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2612 locations->SetInAt(0, Location::RequiresRegister());
2613 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2614}
2615
2616void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2617 LocationSummary* locations = instruction->GetLocations();
2618 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002619 size_t offset = instruction->GetFieldOffset().SizeValue();
2620
2621 switch (instruction->GetType()) {
2622 case Primitive::kPrimBoolean: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002623 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002624 __ movzxb(out, Address(cls, offset));
2625 break;
2626 }
2627
2628 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002629 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002630 __ movsxb(out, Address(cls, offset));
2631 break;
2632 }
2633
2634 case Primitive::kPrimShort: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002635 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002636 __ movsxw(out, Address(cls, offset));
2637 break;
2638 }
2639
2640 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002641 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002642 __ movzxw(out, Address(cls, offset));
2643 break;
2644 }
2645
2646 case Primitive::kPrimInt:
2647 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002648 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002649 __ movl(out, Address(cls, offset));
2650 break;
2651 }
2652
2653 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002654 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002655 __ movq(out, Address(cls, offset));
2656 break;
2657 }
2658
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002659 case Primitive::kPrimFloat: {
2660 XmmRegister out = locations->Out().As<XmmRegister>();
2661 __ movss(out, Address(cls, offset));
2662 break;
2663 }
2664
2665 case Primitive::kPrimDouble: {
2666 XmmRegister out = locations->Out().As<XmmRegister>();
2667 __ movsd(out, Address(cls, offset));
2668 break;
2669 }
2670
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002671 case Primitive::kPrimVoid:
2672 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2673 UNREACHABLE();
2674 }
2675}
2676
2677void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2678 LocationSummary* locations =
2679 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2680 Primitive::Type field_type = instruction->GetFieldType();
2681 bool is_object_type = field_type == Primitive::kPrimNot;
2682 locations->SetInAt(0, Location::RequiresRegister());
2683 locations->SetInAt(1, Location::RequiresRegister());
2684 if (is_object_type) {
2685 // Temporary registers for the write barrier.
2686 locations->AddTemp(Location::RequiresRegister());
2687 locations->AddTemp(Location::RequiresRegister());
2688 }
2689}
2690
2691void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2692 LocationSummary* locations = instruction->GetLocations();
2693 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002694 size_t offset = instruction->GetFieldOffset().SizeValue();
2695 Primitive::Type field_type = instruction->GetFieldType();
2696
2697 switch (field_type) {
2698 case Primitive::kPrimBoolean:
2699 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002700 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002701 __ movb(Address(cls, offset), value);
2702 break;
2703 }
2704
2705 case Primitive::kPrimShort:
2706 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002707 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002708 __ movw(Address(cls, offset), value);
2709 break;
2710 }
2711
2712 case Primitive::kPrimInt:
2713 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002714 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002715 __ movl(Address(cls, offset), value);
2716 if (field_type == Primitive::kPrimNot) {
2717 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
2718 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
2719 codegen_->MarkGCCard(temp, card, cls, value);
2720 }
2721 break;
2722 }
2723
2724 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002725 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002726 __ movq(Address(cls, offset), value);
2727 break;
2728 }
2729
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002730 case Primitive::kPrimFloat: {
2731 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2732 __ movss(Address(cls, offset), value);
2733 break;
2734 }
2735
2736 case Primitive::kPrimDouble: {
2737 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2738 __ movsd(Address(cls, offset), value);
2739 break;
2740 }
2741
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002742 case Primitive::kPrimVoid:
2743 LOG(FATAL) << "Unreachable type " << field_type;
2744 UNREACHABLE();
2745 }
2746}
2747
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002748void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
2749 LocationSummary* locations =
2750 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2751 locations->SetOut(Location::RequiresRegister());
2752}
2753
2754void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
2755 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
2756 codegen_->AddSlowPath(slow_path);
2757
2758 CpuRegister out = load->GetLocations()->Out().As<CpuRegister>();
2759 codegen_->LoadCurrentMethod(CpuRegister(out));
2760 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
2761 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2762 __ testl(out, out);
2763 __ j(kEqual, slow_path->GetEntryLabel());
2764 __ Bind(slow_path->GetExitLabel());
2765}
2766
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002767void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
2768 LocationSummary* locations =
2769 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2770 locations->SetOut(Location::RequiresRegister());
2771}
2772
2773void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
2774 Address address = Address::Absolute(
2775 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
2776 __ gs()->movl(load->GetLocations()->Out().As<CpuRegister>(), address);
2777 __ gs()->movl(address, Immediate(0));
2778}
2779
2780void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
2781 LocationSummary* locations =
2782 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2783 InvokeRuntimeCallingConvention calling_convention;
2784 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2785}
2786
2787void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
2788 __ gs()->call(
2789 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
2790 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2791}
2792
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002793void LocationsBuilderX86_64::VisitTypeCheck(HTypeCheck* instruction) {
2794 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
2795 ? LocationSummary::kNoCall
2796 : LocationSummary::kCallOnSlowPath;
2797 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2798 locations->SetInAt(0, Location::RequiresRegister());
2799 locations->SetInAt(1, Location::Any());
2800 locations->SetOut(Location::RequiresRegister());
2801}
2802
2803void InstructionCodeGeneratorX86_64::VisitTypeCheck(HTypeCheck* instruction) {
2804 LocationSummary* locations = instruction->GetLocations();
2805 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2806 Location cls = locations->InAt(1);
2807 CpuRegister out = locations->Out().As<CpuRegister>();
2808 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2809 Label done, zero;
2810 SlowPathCodeX86_64* slow_path = nullptr;
2811
2812 // Return 0 if `obj` is null.
2813 // TODO: avoid this check if we know obj is not null.
2814 __ testl(obj, obj);
2815 __ j(kEqual, &zero);
2816 // Compare the class of `obj` with `cls`.
2817 __ movl(out, Address(obj, class_offset));
2818 if (cls.IsRegister()) {
2819 __ cmpl(out, cls.As<CpuRegister>());
2820 } else {
2821 DCHECK(cls.IsStackSlot()) << cls;
2822 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
2823 }
2824 if (instruction->IsClassFinal()) {
2825 // Classes must be equal for the instanceof to succeed.
2826 __ j(kNotEqual, &zero);
2827 __ movl(out, Immediate(1));
2828 __ jmp(&done);
2829 } else {
2830 // If the classes are not equal, we go into a slow path.
2831 DCHECK(locations->OnlyCallsOnSlowPath());
2832 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
2833 instruction, Location::RegisterLocation(out.AsRegister()));
2834 codegen_->AddSlowPath(slow_path);
2835 __ j(kNotEqual, slow_path->GetEntryLabel());
2836 __ movl(out, Immediate(1));
2837 __ jmp(&done);
2838 }
2839 __ Bind(&zero);
2840 __ movl(out, Immediate(0));
2841 if (slow_path != nullptr) {
2842 __ Bind(slow_path->GetExitLabel());
2843 }
2844 __ Bind(&done);
2845}
2846
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002847} // namespace x86_64
2848} // namespace art