blob: e09b6cab083208570498f648967e223b3bf1d39a [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) {
1262 case Primitive::kPrimLong:
1263 switch (input_type) {
1264 case Primitive::kPrimByte:
1265 case Primitive::kPrimShort:
1266 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001267 case Primitive::kPrimChar:
Roland Levillaindff1f282014-11-05 14:15:05 +00001268 // int-to-long conversion.
1269 // TODO: We would benefit from a (to-be-implemented)
1270 // Location::RegisterOrStackSlot requirement for this input.
1271 locations->SetInAt(0, Location::RequiresRegister());
1272 locations->SetOut(Location::RequiresRegister());
1273 break;
1274
1275 case Primitive::kPrimFloat:
1276 case Primitive::kPrimDouble:
1277 LOG(FATAL) << "Type conversion from " << input_type << " to "
1278 << result_type << " not yet implemented";
1279 break;
1280
1281 default:
1282 LOG(FATAL) << "Unexpected type conversion from " << input_type
1283 << " to " << result_type;
1284 }
1285 break;
1286
1287 case Primitive::kPrimInt:
1288 case Primitive::kPrimFloat:
1289 case Primitive::kPrimDouble:
1290 LOG(FATAL) << "Type conversion from " << input_type
1291 << " to " << result_type << " not yet implemented";
1292 break;
1293
1294 default:
1295 LOG(FATAL) << "Unexpected type conversion from " << input_type
1296 << " to " << result_type;
1297 }
1298}
1299
1300void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1301 LocationSummary* locations = conversion->GetLocations();
1302 Location out = locations->Out();
1303 Location in = locations->InAt(0);
1304 Primitive::Type result_type = conversion->GetResultType();
1305 Primitive::Type input_type = conversion->GetInputType();
1306 switch (result_type) {
1307 case Primitive::kPrimLong:
1308 switch (input_type) {
1309 DCHECK(out.IsRegister());
1310 case Primitive::kPrimByte:
1311 case Primitive::kPrimShort:
1312 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001313 case Primitive::kPrimChar:
Roland Levillaindff1f282014-11-05 14:15:05 +00001314 // int-to-long conversion.
1315 DCHECK(in.IsRegister());
1316 __ movsxd(out.As<CpuRegister>(), in.As<CpuRegister>());
1317 break;
1318
1319 case Primitive::kPrimFloat:
1320 case Primitive::kPrimDouble:
1321 LOG(FATAL) << "Type conversion from " << input_type << " to "
1322 << result_type << " not yet implemented";
1323 break;
1324
1325 default:
1326 LOG(FATAL) << "Unexpected type conversion from " << input_type
1327 << " to " << result_type;
1328 }
1329 break;
1330
1331 case Primitive::kPrimInt:
1332 case Primitive::kPrimFloat:
1333 case Primitive::kPrimDouble:
1334 LOG(FATAL) << "Type conversion from " << input_type
1335 << " to " << result_type << " not yet implemented";
1336 break;
1337
1338 default:
1339 LOG(FATAL) << "Unexpected type conversion from " << input_type
1340 << " to " << result_type;
1341 }
1342}
1343
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001344void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001345 LocationSummary* locations =
1346 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001347 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001348 case Primitive::kPrimInt: {
1349 locations->SetInAt(0, Location::RequiresRegister());
1350 locations->SetInAt(1, Location::Any());
1351 locations->SetOut(Location::SameAsFirstInput());
1352 break;
1353 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001354
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001355 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001356 locations->SetInAt(0, Location::RequiresRegister());
1357 locations->SetInAt(1, Location::RequiresRegister());
1358 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001359 break;
1360 }
1361
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001362 case Primitive::kPrimDouble:
1363 case Primitive::kPrimFloat: {
1364 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001365 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001366 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001367 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001368 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001369
1370 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001371 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001372 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001373}
1374
1375void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1376 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001377 Location first = locations->InAt(0);
1378 Location second = locations->InAt(1);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001379 DCHECK(first.Equals(locations->Out()));
Calin Juravle11351682014-10-23 15:38:15 +01001380
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001381 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001382 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001383 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001384 __ addl(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001385 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001386 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001387 __ addl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001388 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001389 __ addl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001390 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001391 break;
1392 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001393
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001394 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001395 __ addq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001396 break;
1397 }
1398
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001399 case Primitive::kPrimFloat: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001400 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001401 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001402 }
1403
1404 case Primitive::kPrimDouble: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001405 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001406 break;
1407 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001408
1409 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001410 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001411 }
1412}
1413
1414void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001415 LocationSummary* locations =
1416 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001417 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001418 case Primitive::kPrimInt: {
1419 locations->SetInAt(0, Location::RequiresRegister());
1420 locations->SetInAt(1, Location::Any());
1421 locations->SetOut(Location::SameAsFirstInput());
1422 break;
1423 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001424 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001425 locations->SetInAt(0, Location::RequiresRegister());
1426 locations->SetInAt(1, Location::RequiresRegister());
1427 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001428 break;
1429 }
Calin Juravle11351682014-10-23 15:38:15 +01001430 case Primitive::kPrimFloat:
1431 case Primitive::kPrimDouble: {
1432 locations->SetInAt(0, Location::RequiresFpuRegister());
1433 locations->SetInAt(1, Location::RequiresFpuRegister());
1434 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001435 break;
Calin Juravle11351682014-10-23 15:38:15 +01001436 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001437 default:
Calin Juravle11351682014-10-23 15:38:15 +01001438 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001439 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001440}
1441
1442void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1443 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001444 Location first = locations->InAt(0);
1445 Location second = locations->InAt(1);
1446 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001447 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001448 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001449 if (second.IsRegister()) {
1450 __ subl(first.As<CpuRegister>(), second.As<CpuRegister>());
1451 } else if (second.IsConstant()) {
1452 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1453 __ subl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001454 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001455 __ subl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001456 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001457 break;
1458 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001459 case Primitive::kPrimLong: {
Calin Juravle11351682014-10-23 15:38:15 +01001460 __ subq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001461 break;
1462 }
1463
Calin Juravle11351682014-10-23 15:38:15 +01001464 case Primitive::kPrimFloat: {
1465 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001466 break;
Calin Juravle11351682014-10-23 15:38:15 +01001467 }
1468
1469 case Primitive::kPrimDouble: {
1470 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1471 break;
1472 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001473
1474 default:
Calin Juravle11351682014-10-23 15:38:15 +01001475 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001476 }
1477}
1478
Calin Juravle34bacdf2014-10-07 20:23:36 +01001479void LocationsBuilderX86_64::VisitMul(HMul* mul) {
1480 LocationSummary* locations =
1481 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1482 switch (mul->GetResultType()) {
1483 case Primitive::kPrimInt: {
1484 locations->SetInAt(0, Location::RequiresRegister());
1485 locations->SetInAt(1, Location::Any());
1486 locations->SetOut(Location::SameAsFirstInput());
1487 break;
1488 }
1489 case Primitive::kPrimLong: {
1490 locations->SetInAt(0, Location::RequiresRegister());
1491 locations->SetInAt(1, Location::RequiresRegister());
1492 locations->SetOut(Location::SameAsFirstInput());
1493 break;
1494 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001495 case Primitive::kPrimFloat:
1496 case Primitive::kPrimDouble: {
1497 locations->SetInAt(0, Location::RequiresFpuRegister());
1498 locations->SetInAt(1, Location::RequiresFpuRegister());
1499 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001500 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001501 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001502
1503 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001504 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001505 }
1506}
1507
1508void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
1509 LocationSummary* locations = mul->GetLocations();
1510 Location first = locations->InAt(0);
1511 Location second = locations->InAt(1);
1512 DCHECK(first.Equals(locations->Out()));
1513 switch (mul->GetResultType()) {
1514 case Primitive::kPrimInt: {
1515 if (second.IsRegister()) {
1516 __ imull(first.As<CpuRegister>(), second.As<CpuRegister>());
1517 } else if (second.IsConstant()) {
1518 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1519 __ imull(first.As<CpuRegister>(), imm);
1520 } else {
1521 DCHECK(second.IsStackSlot());
1522 __ imull(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
1523 }
1524 break;
1525 }
1526 case Primitive::kPrimLong: {
1527 __ imulq(first.As<CpuRegister>(), second.As<CpuRegister>());
1528 break;
1529 }
1530
Calin Juravleb5bfa962014-10-21 18:02:24 +01001531 case Primitive::kPrimFloat: {
1532 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001533 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001534 }
1535
1536 case Primitive::kPrimDouble: {
1537 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1538 break;
1539 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001540
1541 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001542 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001543 }
1544}
1545
Calin Juravle7c4954d2014-10-28 16:57:40 +00001546void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
1547 LocationSummary* locations =
1548 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1549 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001550 case Primitive::kPrimInt: {
1551 locations->SetInAt(0, Location::RegisterLocation(RAX));
1552 locations->SetInAt(1, Location::RequiresRegister());
1553 locations->SetOut(Location::SameAsFirstInput());
1554 // Intel uses edx:eax as the dividend.
1555 locations->AddTemp(Location::RegisterLocation(RDX));
1556 break;
1557 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001558 case Primitive::kPrimLong: {
1559 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1560 break;
1561 }
1562 case Primitive::kPrimFloat:
1563 case Primitive::kPrimDouble: {
1564 locations->SetInAt(0, Location::RequiresFpuRegister());
1565 locations->SetInAt(1, Location::RequiresFpuRegister());
1566 locations->SetOut(Location::SameAsFirstInput());
1567 break;
1568 }
1569
1570 default:
1571 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1572 }
1573}
1574
1575void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
1576 LocationSummary* locations = div->GetLocations();
1577 Location first = locations->InAt(0);
1578 Location second = locations->InAt(1);
1579 DCHECK(first.Equals(locations->Out()));
1580
1581 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001582 case Primitive::kPrimInt: {
1583 CpuRegister first_reg = first.As<CpuRegister>();
1584 CpuRegister second_reg = second.As<CpuRegister>();
1585 DCHECK_EQ(RAX, first_reg.AsRegister());
1586 DCHECK_EQ(RDX, locations->GetTemp(0).As<CpuRegister>().AsRegister());
1587
1588 SlowPathCodeX86_64* slow_path =
1589 new (GetGraph()->GetArena()) DivMinusOneSlowPathX86_64(first_reg.AsRegister());
1590 codegen_->AddSlowPath(slow_path);
1591
1592 // 0x80000000/-1 triggers an arithmetic exception!
1593 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
1594 // it's safe to just use negl instead of more complex comparisons.
1595
1596 __ cmpl(second_reg, Immediate(-1));
1597 __ j(kEqual, slow_path->GetEntryLabel());
1598
1599 // edx:eax <- sign-extended of eax
1600 __ cdq();
1601 // eax = quotient, edx = remainder
1602 __ idivl(second_reg);
1603
1604 __ Bind(slow_path->GetExitLabel());
1605 break;
1606 }
1607
Calin Juravle7c4954d2014-10-28 16:57:40 +00001608 case Primitive::kPrimLong: {
1609 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1610 break;
1611 }
1612
1613 case Primitive::kPrimFloat: {
1614 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1615 break;
1616 }
1617
1618 case Primitive::kPrimDouble: {
1619 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1620 break;
1621 }
1622
1623 default:
1624 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1625 }
1626}
1627
Calin Juravled0d48522014-11-04 16:40:20 +00001628void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1629 LocationSummary* locations =
1630 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1631 locations->SetInAt(0, Location::Any());
1632 if (instruction->HasUses()) {
1633 locations->SetOut(Location::SameAsFirstInput());
1634 }
1635}
1636
1637void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1638 SlowPathCodeX86_64* slow_path =
1639 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
1640 codegen_->AddSlowPath(slow_path);
1641
1642 LocationSummary* locations = instruction->GetLocations();
1643 Location value = locations->InAt(0);
1644
1645 if (value.IsRegister()) {
1646 __ testl(value.As<CpuRegister>(), value.As<CpuRegister>());
1647 } else if (value.IsStackSlot()) {
1648 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
1649 } else {
1650 DCHECK(value.IsConstant()) << value;
1651 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
1652 __ jmp(slow_path->GetEntryLabel());
1653 }
1654 return;
1655 }
1656 __ j(kEqual, slow_path->GetEntryLabel());
1657}
1658
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001659void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001660 LocationSummary* locations =
1661 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001662 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001663 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1664 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1665 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001666}
1667
1668void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
1669 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001670 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001671 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1672
1673 __ gs()->call(Address::Absolute(
1674 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
1675
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001676 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001677 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001678}
1679
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001680void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
1681 LocationSummary* locations =
1682 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1683 InvokeRuntimeCallingConvention calling_convention;
1684 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1685 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1686 locations->SetOut(Location::RegisterLocation(RAX));
1687 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1688}
1689
1690void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
1691 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001692 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001693 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1694
1695 __ gs()->call(Address::Absolute(
1696 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocArrayWithAccessCheck), true));
1697
1698 DCHECK(!codegen_->IsLeafMethod());
1699 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1700}
1701
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001702void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001703 LocationSummary* locations =
1704 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001705 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1706 if (location.IsStackSlot()) {
1707 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1708 } else if (location.IsDoubleStackSlot()) {
1709 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1710 }
1711 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001712}
1713
1714void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
1715 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001716 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001717}
1718
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001719void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001720 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001721 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001722 locations->SetInAt(0, Location::RequiresRegister());
1723 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001724}
1725
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001726void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
1727 LocationSummary* locations = not_->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001728 DCHECK_EQ(locations->InAt(0).As<CpuRegister>().AsRegister(),
1729 locations->Out().As<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001730 Location out = locations->Out();
1731 switch (not_->InputAt(0)->GetType()) {
1732 case Primitive::kPrimBoolean:
1733 __ xorq(out.As<CpuRegister>(), Immediate(1));
1734 break;
1735
1736 case Primitive::kPrimInt:
1737 __ notl(out.As<CpuRegister>());
1738 break;
1739
1740 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001741 __ notq(out.As<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001742 break;
1743
1744 default:
1745 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1746 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001747}
1748
1749void LocationsBuilderX86_64::VisitPhi(HPhi* 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 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1753 locations->SetInAt(i, Location::Any());
1754 }
1755 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001756}
1757
1758void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001759 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001760 LOG(FATAL) << "Unimplemented";
1761}
1762
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001763void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001764 LocationSummary* locations =
1765 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001766 Primitive::Type field_type = instruction->GetFieldType();
1767 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001768 locations->SetInAt(0, Location::RequiresRegister());
1769 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001770 if (is_object_type) {
1771 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001772 locations->AddTemp(Location::RequiresRegister());
1773 locations->AddTemp(Location::RequiresRegister());
1774 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001775}
1776
1777void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1778 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001779 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001780 size_t offset = instruction->GetFieldOffset().SizeValue();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001781 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001782
1783 switch (field_type) {
1784 case Primitive::kPrimBoolean:
1785 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001786 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001787 __ movb(Address(obj, offset), value);
1788 break;
1789 }
1790
1791 case Primitive::kPrimShort:
1792 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001793 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001794 __ movw(Address(obj, offset), value);
1795 break;
1796 }
1797
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001798 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001799 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001800 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001801 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001802 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001803 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
1804 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001805 codegen_->MarkGCCard(temp, card, obj, value);
1806 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001807 break;
1808 }
1809
1810 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001811 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001812 __ movq(Address(obj, offset), value);
1813 break;
1814 }
1815
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001816 case Primitive::kPrimFloat: {
1817 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1818 __ movss(Address(obj, offset), value);
1819 break;
1820 }
1821
1822 case Primitive::kPrimDouble: {
1823 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1824 __ movsd(Address(obj, offset), value);
1825 break;
1826 }
1827
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001828 case Primitive::kPrimVoid:
1829 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001830 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001831 }
1832}
1833
1834void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001835 LocationSummary* locations =
1836 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001837 locations->SetInAt(0, Location::RequiresRegister());
1838 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001839}
1840
1841void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1842 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001843 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001844 size_t offset = instruction->GetFieldOffset().SizeValue();
1845
1846 switch (instruction->GetType()) {
1847 case Primitive::kPrimBoolean: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001848 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001849 __ movzxb(out, Address(obj, offset));
1850 break;
1851 }
1852
1853 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001854 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001855 __ movsxb(out, Address(obj, offset));
1856 break;
1857 }
1858
1859 case Primitive::kPrimShort: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001860 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001861 __ movsxw(out, Address(obj, offset));
1862 break;
1863 }
1864
1865 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001866 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001867 __ movzxw(out, Address(obj, offset));
1868 break;
1869 }
1870
1871 case Primitive::kPrimInt:
1872 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001873 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001874 __ movl(out, Address(obj, offset));
1875 break;
1876 }
1877
1878 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001879 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001880 __ movq(out, Address(obj, offset));
1881 break;
1882 }
1883
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001884 case Primitive::kPrimFloat: {
1885 XmmRegister out = locations->Out().As<XmmRegister>();
1886 __ movss(out, Address(obj, offset));
1887 break;
1888 }
1889
1890 case Primitive::kPrimDouble: {
1891 XmmRegister out = locations->Out().As<XmmRegister>();
1892 __ movsd(out, Address(obj, offset));
1893 break;
1894 }
1895
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001896 case Primitive::kPrimVoid:
1897 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001898 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001899 }
1900}
1901
1902void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001903 LocationSummary* locations =
1904 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001905 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001906 if (instruction->HasUses()) {
1907 locations->SetOut(Location::SameAsFirstInput());
1908 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001909}
1910
1911void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001912 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001913 codegen_->AddSlowPath(slow_path);
1914
1915 LocationSummary* locations = instruction->GetLocations();
1916 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001917
1918 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001919 __ cmpl(obj.As<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001920 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001921 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001922 } else {
1923 DCHECK(obj.IsConstant()) << obj;
1924 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1925 __ jmp(slow_path->GetEntryLabel());
1926 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001927 }
1928 __ j(kEqual, slow_path->GetEntryLabel());
1929}
1930
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001931void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001932 LocationSummary* locations =
1933 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001934 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001935 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001936 1, Location::RegisterOrConstant(instruction->InputAt(1)));
1937 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001938}
1939
1940void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
1941 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001942 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001943 Location index = locations->InAt(1);
1944
1945 switch (instruction->GetType()) {
1946 case Primitive::kPrimBoolean: {
1947 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001948 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001949 if (index.IsConstant()) {
1950 __ movzxb(out, Address(obj,
1951 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1952 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001953 __ movzxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001954 }
1955 break;
1956 }
1957
1958 case Primitive::kPrimByte: {
1959 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001960 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001961 if (index.IsConstant()) {
1962 __ movsxb(out, Address(obj,
1963 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1964 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001965 __ movsxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001966 }
1967 break;
1968 }
1969
1970 case Primitive::kPrimShort: {
1971 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001972 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001973 if (index.IsConstant()) {
1974 __ movsxw(out, Address(obj,
1975 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1976 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001977 __ movsxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001978 }
1979 break;
1980 }
1981
1982 case Primitive::kPrimChar: {
1983 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001984 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001985 if (index.IsConstant()) {
1986 __ movzxw(out, Address(obj,
1987 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1988 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001989 __ movzxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001990 }
1991 break;
1992 }
1993
1994 case Primitive::kPrimInt:
1995 case Primitive::kPrimNot: {
1996 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1997 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001998 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001999 if (index.IsConstant()) {
2000 __ movl(out, Address(obj,
2001 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2002 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002003 __ movl(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002004 }
2005 break;
2006 }
2007
2008 case Primitive::kPrimLong: {
2009 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002010 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002011 if (index.IsConstant()) {
2012 __ movq(out, Address(obj,
2013 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2014 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002015 __ movq(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002016 }
2017 break;
2018 }
2019
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002020 case Primitive::kPrimFloat: {
2021 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2022 XmmRegister out = locations->Out().As<XmmRegister>();
2023 if (index.IsConstant()) {
2024 __ movss(out, Address(obj,
2025 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2026 } else {
2027 __ movss(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
2028 }
2029 break;
2030 }
2031
2032 case Primitive::kPrimDouble: {
2033 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2034 XmmRegister out = locations->Out().As<XmmRegister>();
2035 if (index.IsConstant()) {
2036 __ movsd(out, Address(obj,
2037 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2038 } else {
2039 __ movsd(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
2040 }
2041 break;
2042 }
2043
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002044 case Primitive::kPrimVoid:
2045 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002046 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002047 }
2048}
2049
2050void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002051 Primitive::Type value_type = instruction->GetComponentType();
2052 bool is_object = value_type == Primitive::kPrimNot;
2053 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2054 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
2055 if (is_object) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002056 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002057 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2058 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2059 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002060 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002061 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002062 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002063 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2064 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002065 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002066 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002067 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
2068 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002069 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002070 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002071 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002072 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002073}
2074
2075void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
2076 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002077 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002078 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002079 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002080 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002081
2082 switch (value_type) {
2083 case Primitive::kPrimBoolean:
2084 case Primitive::kPrimByte: {
2085 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002086 if (index.IsConstant()) {
2087 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002088 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002089 __ movb(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002090 } else {
2091 __ movb(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2092 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002093 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002094 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002095 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
2096 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002097 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002098 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002099 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2100 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002101 }
2102 break;
2103 }
2104
2105 case Primitive::kPrimShort:
2106 case Primitive::kPrimChar: {
2107 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002108 if (index.IsConstant()) {
2109 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002110 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002111 __ movw(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002112 } else {
2113 __ movw(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2114 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002115 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002116 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002117 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
2118 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002119 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002120 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002121 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2122 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002123 }
2124 break;
2125 }
2126
2127 case Primitive::kPrimInt: {
2128 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002129 if (index.IsConstant()) {
2130 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002131 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002132 __ movl(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002133 } else {
2134 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2135 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002136 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002137 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002138 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2139 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002140 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002141 DCHECK(value.IsConstant()) << value;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002142 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002143 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2144 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002145 }
2146 break;
2147 }
2148
2149 case Primitive::kPrimNot: {
2150 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject), true));
2151 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002152 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002153 break;
2154 }
2155
2156 case Primitive::kPrimLong: {
2157 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002158 if (index.IsConstant()) {
2159 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002160 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002161 __ movq(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002162 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002163 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002164 __ movq(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
2165 value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002166 }
2167 break;
2168 }
2169
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002170 case Primitive::kPrimFloat: {
2171 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2172 if (index.IsConstant()) {
2173 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2174 DCHECK(value.IsFpuRegister());
2175 __ movss(Address(obj, offset), value.As<XmmRegister>());
2176 } else {
2177 DCHECK(value.IsFpuRegister());
2178 __ movss(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2179 value.As<XmmRegister>());
2180 }
2181 break;
2182 }
2183
2184 case Primitive::kPrimDouble: {
2185 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2186 if (index.IsConstant()) {
2187 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
2188 DCHECK(value.IsFpuRegister());
2189 __ movsd(Address(obj, offset), value.As<XmmRegister>());
2190 } else {
2191 DCHECK(value.IsFpuRegister());
2192 __ movsd(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
2193 value.As<XmmRegister>());
2194 }
2195 break;
2196 }
2197
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002198 case Primitive::kPrimVoid:
2199 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002200 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002201 }
2202}
2203
2204void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002205 LocationSummary* locations =
2206 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002207 locations->SetInAt(0, Location::RequiresRegister());
2208 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002209}
2210
2211void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
2212 LocationSummary* locations = instruction->GetLocations();
2213 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002214 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2215 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002216 __ movl(out, Address(obj, offset));
2217}
2218
2219void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002220 LocationSummary* locations =
2221 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002222 locations->SetInAt(0, Location::RequiresRegister());
2223 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002224 if (instruction->HasUses()) {
2225 locations->SetOut(Location::SameAsFirstInput());
2226 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002227}
2228
2229void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
2230 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002231 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002232 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002233 codegen_->AddSlowPath(slow_path);
2234
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002235 CpuRegister index = locations->InAt(0).As<CpuRegister>();
2236 CpuRegister length = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002237
2238 __ cmpl(index, length);
2239 __ j(kAboveEqual, slow_path->GetEntryLabel());
2240}
2241
2242void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
2243 CpuRegister card,
2244 CpuRegister object,
2245 CpuRegister value) {
2246 Label is_null;
2247 __ testl(value, value);
2248 __ j(kEqual, &is_null);
2249 __ gs()->movq(card, Address::Absolute(
2250 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
2251 __ movq(temp, object);
2252 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
2253 __ movb(Address(temp, card, TIMES_1, 0), card);
2254 __ Bind(&is_null);
2255}
2256
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002257void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
2258 temp->SetLocations(nullptr);
2259}
2260
2261void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
2262 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002263 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002264}
2265
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002266void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002267 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002268 LOG(FATAL) << "Unimplemented";
2269}
2270
2271void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002272 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2273}
2274
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002275void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
2276 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2277}
2278
2279void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002280 HBasicBlock* block = instruction->GetBlock();
2281 if (block->GetLoopInformation() != nullptr) {
2282 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2283 // The back edge will generate the suspend check.
2284 return;
2285 }
2286 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2287 // The goto will generate the suspend check.
2288 return;
2289 }
2290 GenerateSuspendCheck(instruction, nullptr);
2291}
2292
2293void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
2294 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002295 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002296 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002297 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002298 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002299 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002300 if (successor == nullptr) {
2301 __ j(kNotEqual, slow_path->GetEntryLabel());
2302 __ Bind(slow_path->GetReturnLabel());
2303 } else {
2304 __ j(kEqual, codegen_->GetLabelOf(successor));
2305 __ jmp(slow_path->GetEntryLabel());
2306 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002307}
2308
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002309X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
2310 return codegen_->GetAssembler();
2311}
2312
2313void ParallelMoveResolverX86_64::EmitMove(size_t index) {
2314 MoveOperands* move = moves_.Get(index);
2315 Location source = move->GetSource();
2316 Location destination = move->GetDestination();
2317
2318 if (source.IsRegister()) {
2319 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002320 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002321 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002322 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002323 source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002324 } else {
2325 DCHECK(destination.IsDoubleStackSlot());
2326 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002327 source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002328 }
2329 } else if (source.IsStackSlot()) {
2330 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002331 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002332 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002333 } else if (destination.IsFpuRegister()) {
2334 __ movss(destination.As<XmmRegister>(),
2335 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002336 } else {
2337 DCHECK(destination.IsStackSlot());
2338 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2339 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2340 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002341 } else if (source.IsDoubleStackSlot()) {
2342 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002343 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002344 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002345 } else if (destination.IsFpuRegister()) {
2346 __ movsd(destination.As<XmmRegister>(), Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002347 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01002348 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002349 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2350 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2351 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002352 } else if (source.IsConstant()) {
2353 HConstant* constant = source.GetConstant();
2354 if (constant->IsIntConstant()) {
2355 Immediate imm(constant->AsIntConstant()->GetValue());
2356 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002357 __ movl(destination.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002358 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002359 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002360 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2361 }
2362 } else if (constant->IsLongConstant()) {
2363 int64_t value = constant->AsLongConstant()->GetValue();
2364 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002365 __ movq(destination.As<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002366 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002367 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002368 __ movq(CpuRegister(TMP), Immediate(value));
2369 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2370 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002371 } else if (constant->IsFloatConstant()) {
2372 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
2373 if (destination.IsFpuRegister()) {
2374 __ movl(CpuRegister(TMP), imm);
2375 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2376 } else {
2377 DCHECK(destination.IsStackSlot()) << destination;
2378 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2379 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002380 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002381 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
2382 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
2383 if (destination.IsFpuRegister()) {
2384 __ movq(CpuRegister(TMP), imm);
2385 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2386 } else {
2387 DCHECK(destination.IsDoubleStackSlot()) << destination;
2388 __ movq(CpuRegister(TMP), imm);
2389 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2390 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002391 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002392 } else if (source.IsFpuRegister()) {
2393 if (destination.IsFpuRegister()) {
2394 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
2395 } else if (destination.IsStackSlot()) {
2396 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
2397 source.As<XmmRegister>());
2398 } else {
2399 DCHECK(destination.IsDoubleStackSlot());
2400 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
2401 source.As<XmmRegister>());
2402 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002403 }
2404}
2405
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002406void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002407 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002408 __ movl(Address(CpuRegister(RSP), mem), reg);
2409 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002410}
2411
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002412void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002413 ScratchRegisterScope ensure_scratch(
2414 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2415
2416 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2417 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2418 __ movl(CpuRegister(ensure_scratch.GetRegister()),
2419 Address(CpuRegister(RSP), mem2 + stack_offset));
2420 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2421 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
2422 CpuRegister(ensure_scratch.GetRegister()));
2423}
2424
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002425void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
2426 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2427 __ movq(Address(CpuRegister(RSP), mem), reg);
2428 __ movq(reg, CpuRegister(TMP));
2429}
2430
2431void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
2432 ScratchRegisterScope ensure_scratch(
2433 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2434
2435 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2436 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2437 __ movq(CpuRegister(ensure_scratch.GetRegister()),
2438 Address(CpuRegister(RSP), mem2 + stack_offset));
2439 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2440 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
2441 CpuRegister(ensure_scratch.GetRegister()));
2442}
2443
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002444void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
2445 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2446 __ movss(Address(CpuRegister(RSP), mem), reg);
2447 __ movd(reg, CpuRegister(TMP));
2448}
2449
2450void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
2451 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2452 __ movsd(Address(CpuRegister(RSP), mem), reg);
2453 __ movd(reg, CpuRegister(TMP));
2454}
2455
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002456void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
2457 MoveOperands* move = moves_.Get(index);
2458 Location source = move->GetSource();
2459 Location destination = move->GetDestination();
2460
2461 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002462 __ xchgq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002463 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002464 Exchange32(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002465 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002466 Exchange32(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002467 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002468 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
2469 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002470 Exchange64(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002471 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002472 Exchange64(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002473 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
2474 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002475 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
2476 __ movd(CpuRegister(TMP), source.As<XmmRegister>());
2477 __ movaps(source.As<XmmRegister>(), destination.As<XmmRegister>());
2478 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2479 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
2480 Exchange32(source.As<XmmRegister>(), destination.GetStackIndex());
2481 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
2482 Exchange32(destination.As<XmmRegister>(), source.GetStackIndex());
2483 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
2484 Exchange64(source.As<XmmRegister>(), destination.GetStackIndex());
2485 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
2486 Exchange64(destination.As<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002487 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002488 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002489 }
2490}
2491
2492
2493void ParallelMoveResolverX86_64::SpillScratch(int reg) {
2494 __ pushq(CpuRegister(reg));
2495}
2496
2497
2498void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
2499 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002500}
2501
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002502void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
2503 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
2504 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2505 Immediate(mirror::Class::kStatusInitialized));
2506 __ j(kLess, slow_path->GetEntryLabel());
2507 __ Bind(slow_path->GetExitLabel());
2508 // No need for memory fence, thanks to the X86_64 memory model.
2509}
2510
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002511void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002512 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2513 ? LocationSummary::kCallOnSlowPath
2514 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002515 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002516 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002517 locations->SetOut(Location::RequiresRegister());
2518}
2519
2520void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
2521 CpuRegister out = cls->GetLocations()->Out().As<CpuRegister>();
2522 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002523 DCHECK(!cls->CanCallRuntime());
2524 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002525 codegen_->LoadCurrentMethod(out);
2526 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2527 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002528 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002529 codegen_->LoadCurrentMethod(out);
2530 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2531 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002532 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
2533 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2534 codegen_->AddSlowPath(slow_path);
2535 __ testl(out, out);
2536 __ j(kEqual, slow_path->GetEntryLabel());
2537 if (cls->MustGenerateClinitCheck()) {
2538 GenerateClassInitializationCheck(slow_path, out);
2539 } else {
2540 __ Bind(slow_path->GetExitLabel());
2541 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002542 }
2543}
2544
2545void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
2546 LocationSummary* locations =
2547 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2548 locations->SetInAt(0, Location::RequiresRegister());
2549 if (check->HasUses()) {
2550 locations->SetOut(Location::SameAsFirstInput());
2551 }
2552}
2553
2554void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002555 // We assume the class to not be null.
2556 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
2557 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002558 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002559 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002560}
2561
2562void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2563 LocationSummary* locations =
2564 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2565 locations->SetInAt(0, Location::RequiresRegister());
2566 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2567}
2568
2569void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2570 LocationSummary* locations = instruction->GetLocations();
2571 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002572 size_t offset = instruction->GetFieldOffset().SizeValue();
2573
2574 switch (instruction->GetType()) {
2575 case Primitive::kPrimBoolean: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002576 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002577 __ movzxb(out, Address(cls, offset));
2578 break;
2579 }
2580
2581 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002582 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002583 __ movsxb(out, Address(cls, offset));
2584 break;
2585 }
2586
2587 case Primitive::kPrimShort: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002588 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002589 __ movsxw(out, Address(cls, offset));
2590 break;
2591 }
2592
2593 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002594 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002595 __ movzxw(out, Address(cls, offset));
2596 break;
2597 }
2598
2599 case Primitive::kPrimInt:
2600 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002601 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002602 __ movl(out, Address(cls, offset));
2603 break;
2604 }
2605
2606 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002607 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002608 __ movq(out, Address(cls, offset));
2609 break;
2610 }
2611
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002612 case Primitive::kPrimFloat: {
2613 XmmRegister out = locations->Out().As<XmmRegister>();
2614 __ movss(out, Address(cls, offset));
2615 break;
2616 }
2617
2618 case Primitive::kPrimDouble: {
2619 XmmRegister out = locations->Out().As<XmmRegister>();
2620 __ movsd(out, Address(cls, offset));
2621 break;
2622 }
2623
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002624 case Primitive::kPrimVoid:
2625 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2626 UNREACHABLE();
2627 }
2628}
2629
2630void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2631 LocationSummary* locations =
2632 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2633 Primitive::Type field_type = instruction->GetFieldType();
2634 bool is_object_type = field_type == Primitive::kPrimNot;
2635 locations->SetInAt(0, Location::RequiresRegister());
2636 locations->SetInAt(1, Location::RequiresRegister());
2637 if (is_object_type) {
2638 // Temporary registers for the write barrier.
2639 locations->AddTemp(Location::RequiresRegister());
2640 locations->AddTemp(Location::RequiresRegister());
2641 }
2642}
2643
2644void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2645 LocationSummary* locations = instruction->GetLocations();
2646 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002647 size_t offset = instruction->GetFieldOffset().SizeValue();
2648 Primitive::Type field_type = instruction->GetFieldType();
2649
2650 switch (field_type) {
2651 case Primitive::kPrimBoolean:
2652 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002653 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002654 __ movb(Address(cls, offset), value);
2655 break;
2656 }
2657
2658 case Primitive::kPrimShort:
2659 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002660 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002661 __ movw(Address(cls, offset), value);
2662 break;
2663 }
2664
2665 case Primitive::kPrimInt:
2666 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002667 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002668 __ movl(Address(cls, offset), value);
2669 if (field_type == Primitive::kPrimNot) {
2670 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
2671 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
2672 codegen_->MarkGCCard(temp, card, cls, value);
2673 }
2674 break;
2675 }
2676
2677 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002678 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002679 __ movq(Address(cls, offset), value);
2680 break;
2681 }
2682
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002683 case Primitive::kPrimFloat: {
2684 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2685 __ movss(Address(cls, offset), value);
2686 break;
2687 }
2688
2689 case Primitive::kPrimDouble: {
2690 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2691 __ movsd(Address(cls, offset), value);
2692 break;
2693 }
2694
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002695 case Primitive::kPrimVoid:
2696 LOG(FATAL) << "Unreachable type " << field_type;
2697 UNREACHABLE();
2698 }
2699}
2700
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002701void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
2702 LocationSummary* locations =
2703 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2704 locations->SetOut(Location::RequiresRegister());
2705}
2706
2707void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
2708 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
2709 codegen_->AddSlowPath(slow_path);
2710
2711 CpuRegister out = load->GetLocations()->Out().As<CpuRegister>();
2712 codegen_->LoadCurrentMethod(CpuRegister(out));
2713 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
2714 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2715 __ testl(out, out);
2716 __ j(kEqual, slow_path->GetEntryLabel());
2717 __ Bind(slow_path->GetExitLabel());
2718}
2719
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002720void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
2721 LocationSummary* locations =
2722 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2723 locations->SetOut(Location::RequiresRegister());
2724}
2725
2726void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
2727 Address address = Address::Absolute(
2728 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
2729 __ gs()->movl(load->GetLocations()->Out().As<CpuRegister>(), address);
2730 __ gs()->movl(address, Immediate(0));
2731}
2732
2733void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
2734 LocationSummary* locations =
2735 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2736 InvokeRuntimeCallingConvention calling_convention;
2737 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2738}
2739
2740void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
2741 __ gs()->call(
2742 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
2743 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2744}
2745
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002746void LocationsBuilderX86_64::VisitTypeCheck(HTypeCheck* instruction) {
2747 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
2748 ? LocationSummary::kNoCall
2749 : LocationSummary::kCallOnSlowPath;
2750 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2751 locations->SetInAt(0, Location::RequiresRegister());
2752 locations->SetInAt(1, Location::Any());
2753 locations->SetOut(Location::RequiresRegister());
2754}
2755
2756void InstructionCodeGeneratorX86_64::VisitTypeCheck(HTypeCheck* instruction) {
2757 LocationSummary* locations = instruction->GetLocations();
2758 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2759 Location cls = locations->InAt(1);
2760 CpuRegister out = locations->Out().As<CpuRegister>();
2761 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2762 Label done, zero;
2763 SlowPathCodeX86_64* slow_path = nullptr;
2764
2765 // Return 0 if `obj` is null.
2766 // TODO: avoid this check if we know obj is not null.
2767 __ testl(obj, obj);
2768 __ j(kEqual, &zero);
2769 // Compare the class of `obj` with `cls`.
2770 __ movl(out, Address(obj, class_offset));
2771 if (cls.IsRegister()) {
2772 __ cmpl(out, cls.As<CpuRegister>());
2773 } else {
2774 DCHECK(cls.IsStackSlot()) << cls;
2775 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
2776 }
2777 if (instruction->IsClassFinal()) {
2778 // Classes must be equal for the instanceof to succeed.
2779 __ j(kNotEqual, &zero);
2780 __ movl(out, Immediate(1));
2781 __ jmp(&done);
2782 } else {
2783 // If the classes are not equal, we go into a slow path.
2784 DCHECK(locations->OnlyCallsOnSlowPath());
2785 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
2786 instruction, Location::RegisterLocation(out.AsRegister()));
2787 codegen_->AddSlowPath(slow_path);
2788 __ j(kNotEqual, slow_path->GetEntryLabel());
2789 __ movl(out, Immediate(1));
2790 __ jmp(&done);
2791 }
2792 __ Bind(&zero);
2793 __ movl(out, Immediate(0));
2794 if (slow_path != nullptr) {
2795 __ Bind(slow_path->GetExitLabel());
2796 }
2797 __ Bind(&done);
2798}
2799
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002800} // namespace x86_64
2801} // namespace art