blob: b9891d6cd972e0372b73656b80b282c94b5d69e8 [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
1082void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1083 HandleInvoke(invoke);
1084}
1085
1086void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001087 LocationSummary* locations =
1088 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001089 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001090
1091 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001092 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001093 HInstruction* input = invoke->InputAt(i);
1094 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1095 }
1096
1097 switch (invoke->GetType()) {
1098 case Primitive::kPrimBoolean:
1099 case Primitive::kPrimByte:
1100 case Primitive::kPrimChar:
1101 case Primitive::kPrimShort:
1102 case Primitive::kPrimInt:
1103 case Primitive::kPrimNot:
1104 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001105 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001106 break;
1107
1108 case Primitive::kPrimVoid:
1109 break;
1110
1111 case Primitive::kPrimDouble:
1112 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001113 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001114 break;
1115 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001116}
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
Roland Levillain88cb1752014-10-20 16:36:47 +01001141void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1142 LocationSummary* locations =
1143 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1144 switch (neg->GetResultType()) {
1145 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001146 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001147 locations->SetInAt(0, Location::RequiresRegister());
1148 locations->SetOut(Location::SameAsFirstInput());
1149 break;
1150
Roland Levillain88cb1752014-10-20 16:36:47 +01001151 case Primitive::kPrimFloat:
1152 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001153 locations->SetInAt(0, Location::RequiresFpuRegister());
1154 // Output overlaps as we need a fresh (zero-initialized)
1155 // register to perform subtraction from zero.
1156 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001157 break;
1158
1159 default:
1160 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1161 }
1162}
1163
1164void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1165 LocationSummary* locations = neg->GetLocations();
1166 Location out = locations->Out();
1167 Location in = locations->InAt(0);
1168 switch (neg->GetResultType()) {
1169 case Primitive::kPrimInt:
1170 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001171 DCHECK(in.Equals(out));
Roland Levillain88cb1752014-10-20 16:36:47 +01001172 __ negl(out.As<CpuRegister>());
1173 break;
1174
1175 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001176 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001177 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001178 __ negq(out.As<CpuRegister>());
1179 break;
1180
Roland Levillain88cb1752014-10-20 16:36:47 +01001181 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001182 DCHECK(in.IsFpuRegister());
1183 DCHECK(out.IsFpuRegister());
1184 DCHECK(!in.Equals(out));
1185 // TODO: Instead of computing negation as a subtraction from
1186 // zero, implement it with an exclusive or with value 0x80000000
1187 // (mask for bit 31, representing the sign of a single-precision
1188 // floating-point number), fetched from a constant pool:
1189 //
1190 // xorps out, [RIP:...] // value at RIP is 0x80 00 00 00
1191
1192 // out = 0
1193 __ xorps(out.As<XmmRegister>(), out.As<XmmRegister>());
1194 // out = out - in
1195 __ subss(out.As<XmmRegister>(), in.As<XmmRegister>());
1196 break;
1197
Roland Levillain88cb1752014-10-20 16:36:47 +01001198 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001199 DCHECK(in.IsFpuRegister());
1200 DCHECK(out.IsFpuRegister());
1201 DCHECK(!in.Equals(out));
1202 // TODO: Instead of computing negation as a subtraction from
1203 // zero, implement it with an exclusive or with value
1204 // 0x8000000000000000 (mask for bit 63, representing the sign of
1205 // a double-precision floating-point number), fetched from a
1206 // constant pool:
1207 //
1208 // xorpd out, [RIP:...] // value at RIP is 0x80 00 00 00 00 00 00 00
1209
1210 // out = 0
1211 __ xorpd(out.As<XmmRegister>(), out.As<XmmRegister>());
1212 // out = out - in
1213 __ subsd(out.As<XmmRegister>(), in.As<XmmRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001214 break;
1215
1216 default:
1217 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1218 }
1219}
1220
Roland Levillaindff1f282014-11-05 14:15:05 +00001221void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1222 LocationSummary* locations =
1223 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1224 Primitive::Type result_type = conversion->GetResultType();
1225 Primitive::Type input_type = conversion->GetInputType();
1226 switch (result_type) {
1227 case Primitive::kPrimLong:
1228 switch (input_type) {
1229 case Primitive::kPrimByte:
1230 case Primitive::kPrimShort:
1231 case Primitive::kPrimInt:
1232 // int-to-long conversion.
1233 // TODO: We would benefit from a (to-be-implemented)
1234 // Location::RegisterOrStackSlot requirement for this input.
1235 locations->SetInAt(0, Location::RequiresRegister());
1236 locations->SetOut(Location::RequiresRegister());
1237 break;
1238
1239 case Primitive::kPrimFloat:
1240 case Primitive::kPrimDouble:
1241 LOG(FATAL) << "Type conversion from " << input_type << " to "
1242 << result_type << " not yet implemented";
1243 break;
1244
1245 default:
1246 LOG(FATAL) << "Unexpected type conversion from " << input_type
1247 << " to " << result_type;
1248 }
1249 break;
1250
1251 case Primitive::kPrimInt:
1252 case Primitive::kPrimFloat:
1253 case Primitive::kPrimDouble:
1254 LOG(FATAL) << "Type conversion from " << input_type
1255 << " to " << result_type << " not yet implemented";
1256 break;
1257
1258 default:
1259 LOG(FATAL) << "Unexpected type conversion from " << input_type
1260 << " to " << result_type;
1261 }
1262}
1263
1264void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1265 LocationSummary* locations = conversion->GetLocations();
1266 Location out = locations->Out();
1267 Location in = locations->InAt(0);
1268 Primitive::Type result_type = conversion->GetResultType();
1269 Primitive::Type input_type = conversion->GetInputType();
1270 switch (result_type) {
1271 case Primitive::kPrimLong:
1272 switch (input_type) {
1273 DCHECK(out.IsRegister());
1274 case Primitive::kPrimByte:
1275 case Primitive::kPrimShort:
1276 case Primitive::kPrimInt:
1277 // int-to-long conversion.
1278 DCHECK(in.IsRegister());
1279 __ movsxd(out.As<CpuRegister>(), in.As<CpuRegister>());
1280 break;
1281
1282 case Primitive::kPrimFloat:
1283 case Primitive::kPrimDouble:
1284 LOG(FATAL) << "Type conversion from " << input_type << " to "
1285 << result_type << " not yet implemented";
1286 break;
1287
1288 default:
1289 LOG(FATAL) << "Unexpected type conversion from " << input_type
1290 << " to " << result_type;
1291 }
1292 break;
1293
1294 case Primitive::kPrimInt:
1295 case Primitive::kPrimFloat:
1296 case Primitive::kPrimDouble:
1297 LOG(FATAL) << "Type conversion from " << input_type
1298 << " to " << result_type << " not yet implemented";
1299 break;
1300
1301 default:
1302 LOG(FATAL) << "Unexpected type conversion from " << input_type
1303 << " to " << result_type;
1304 }
1305}
1306
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001307void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001308 LocationSummary* locations =
1309 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001310 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001311 case Primitive::kPrimInt: {
1312 locations->SetInAt(0, Location::RequiresRegister());
1313 locations->SetInAt(1, Location::Any());
1314 locations->SetOut(Location::SameAsFirstInput());
1315 break;
1316 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001317
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001318 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001319 locations->SetInAt(0, Location::RequiresRegister());
1320 locations->SetInAt(1, Location::RequiresRegister());
1321 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001322 break;
1323 }
1324
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001325 case Primitive::kPrimDouble:
1326 case Primitive::kPrimFloat: {
1327 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001328 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001329 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001330 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001331 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001332
1333 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001334 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001335 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001336}
1337
1338void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1339 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001340 Location first = locations->InAt(0);
1341 Location second = locations->InAt(1);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001342 DCHECK(first.Equals(locations->Out()));
Calin Juravle11351682014-10-23 15:38:15 +01001343
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001344 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001345 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001346 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001347 __ addl(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001348 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001349 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001350 __ addl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001351 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001352 __ addl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001353 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001354 break;
1355 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001356
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001357 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001358 __ addq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001359 break;
1360 }
1361
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001362 case Primitive::kPrimFloat: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001363 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001364 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001365 }
1366
1367 case Primitive::kPrimDouble: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001368 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001369 break;
1370 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001371
1372 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001373 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001374 }
1375}
1376
1377void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001378 LocationSummary* locations =
1379 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001380 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001381 case Primitive::kPrimInt: {
1382 locations->SetInAt(0, Location::RequiresRegister());
1383 locations->SetInAt(1, Location::Any());
1384 locations->SetOut(Location::SameAsFirstInput());
1385 break;
1386 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001387 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001388 locations->SetInAt(0, Location::RequiresRegister());
1389 locations->SetInAt(1, Location::RequiresRegister());
1390 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001391 break;
1392 }
Calin Juravle11351682014-10-23 15:38:15 +01001393 case Primitive::kPrimFloat:
1394 case Primitive::kPrimDouble: {
1395 locations->SetInAt(0, Location::RequiresFpuRegister());
1396 locations->SetInAt(1, Location::RequiresFpuRegister());
1397 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001398 break;
Calin Juravle11351682014-10-23 15:38:15 +01001399 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001400 default:
Calin Juravle11351682014-10-23 15:38:15 +01001401 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001402 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001403}
1404
1405void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1406 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001407 Location first = locations->InAt(0);
1408 Location second = locations->InAt(1);
1409 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001410 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001411 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001412 if (second.IsRegister()) {
1413 __ subl(first.As<CpuRegister>(), second.As<CpuRegister>());
1414 } else if (second.IsConstant()) {
1415 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1416 __ subl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001417 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001418 __ subl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001419 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001420 break;
1421 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001422 case Primitive::kPrimLong: {
Calin Juravle11351682014-10-23 15:38:15 +01001423 __ subq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001424 break;
1425 }
1426
Calin Juravle11351682014-10-23 15:38:15 +01001427 case Primitive::kPrimFloat: {
1428 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001429 break;
Calin Juravle11351682014-10-23 15:38:15 +01001430 }
1431
1432 case Primitive::kPrimDouble: {
1433 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1434 break;
1435 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001436
1437 default:
Calin Juravle11351682014-10-23 15:38:15 +01001438 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001439 }
1440}
1441
Calin Juravle34bacdf2014-10-07 20:23:36 +01001442void LocationsBuilderX86_64::VisitMul(HMul* mul) {
1443 LocationSummary* locations =
1444 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1445 switch (mul->GetResultType()) {
1446 case Primitive::kPrimInt: {
1447 locations->SetInAt(0, Location::RequiresRegister());
1448 locations->SetInAt(1, Location::Any());
1449 locations->SetOut(Location::SameAsFirstInput());
1450 break;
1451 }
1452 case Primitive::kPrimLong: {
1453 locations->SetInAt(0, Location::RequiresRegister());
1454 locations->SetInAt(1, Location::RequiresRegister());
1455 locations->SetOut(Location::SameAsFirstInput());
1456 break;
1457 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001458 case Primitive::kPrimFloat:
1459 case Primitive::kPrimDouble: {
1460 locations->SetInAt(0, Location::RequiresFpuRegister());
1461 locations->SetInAt(1, Location::RequiresFpuRegister());
1462 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001463 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001464 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001465
1466 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001467 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001468 }
1469}
1470
1471void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
1472 LocationSummary* locations = mul->GetLocations();
1473 Location first = locations->InAt(0);
1474 Location second = locations->InAt(1);
1475 DCHECK(first.Equals(locations->Out()));
1476 switch (mul->GetResultType()) {
1477 case Primitive::kPrimInt: {
1478 if (second.IsRegister()) {
1479 __ imull(first.As<CpuRegister>(), second.As<CpuRegister>());
1480 } else if (second.IsConstant()) {
1481 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1482 __ imull(first.As<CpuRegister>(), imm);
1483 } else {
1484 DCHECK(second.IsStackSlot());
1485 __ imull(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
1486 }
1487 break;
1488 }
1489 case Primitive::kPrimLong: {
1490 __ imulq(first.As<CpuRegister>(), second.As<CpuRegister>());
1491 break;
1492 }
1493
Calin Juravleb5bfa962014-10-21 18:02:24 +01001494 case Primitive::kPrimFloat: {
1495 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001496 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001497 }
1498
1499 case Primitive::kPrimDouble: {
1500 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1501 break;
1502 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001503
1504 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001505 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001506 }
1507}
1508
Calin Juravle7c4954d2014-10-28 16:57:40 +00001509void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
1510 LocationSummary* locations =
1511 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1512 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001513 case Primitive::kPrimInt: {
1514 locations->SetInAt(0, Location::RegisterLocation(RAX));
1515 locations->SetInAt(1, Location::RequiresRegister());
1516 locations->SetOut(Location::SameAsFirstInput());
1517 // Intel uses edx:eax as the dividend.
1518 locations->AddTemp(Location::RegisterLocation(RDX));
1519 break;
1520 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001521 case Primitive::kPrimLong: {
1522 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1523 break;
1524 }
1525 case Primitive::kPrimFloat:
1526 case Primitive::kPrimDouble: {
1527 locations->SetInAt(0, Location::RequiresFpuRegister());
1528 locations->SetInAt(1, Location::RequiresFpuRegister());
1529 locations->SetOut(Location::SameAsFirstInput());
1530 break;
1531 }
1532
1533 default:
1534 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1535 }
1536}
1537
1538void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
1539 LocationSummary* locations = div->GetLocations();
1540 Location first = locations->InAt(0);
1541 Location second = locations->InAt(1);
1542 DCHECK(first.Equals(locations->Out()));
1543
1544 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001545 case Primitive::kPrimInt: {
1546 CpuRegister first_reg = first.As<CpuRegister>();
1547 CpuRegister second_reg = second.As<CpuRegister>();
1548 DCHECK_EQ(RAX, first_reg.AsRegister());
1549 DCHECK_EQ(RDX, locations->GetTemp(0).As<CpuRegister>().AsRegister());
1550
1551 SlowPathCodeX86_64* slow_path =
1552 new (GetGraph()->GetArena()) DivMinusOneSlowPathX86_64(first_reg.AsRegister());
1553 codegen_->AddSlowPath(slow_path);
1554
1555 // 0x80000000/-1 triggers an arithmetic exception!
1556 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
1557 // it's safe to just use negl instead of more complex comparisons.
1558
1559 __ cmpl(second_reg, Immediate(-1));
1560 __ j(kEqual, slow_path->GetEntryLabel());
1561
1562 // edx:eax <- sign-extended of eax
1563 __ cdq();
1564 // eax = quotient, edx = remainder
1565 __ idivl(second_reg);
1566
1567 __ Bind(slow_path->GetExitLabel());
1568 break;
1569 }
1570
Calin Juravle7c4954d2014-10-28 16:57:40 +00001571 case Primitive::kPrimLong: {
1572 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1573 break;
1574 }
1575
1576 case Primitive::kPrimFloat: {
1577 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1578 break;
1579 }
1580
1581 case Primitive::kPrimDouble: {
1582 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1583 break;
1584 }
1585
1586 default:
1587 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1588 }
1589}
1590
Calin Juravled0d48522014-11-04 16:40:20 +00001591void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1592 LocationSummary* locations =
1593 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1594 locations->SetInAt(0, Location::Any());
1595 if (instruction->HasUses()) {
1596 locations->SetOut(Location::SameAsFirstInput());
1597 }
1598}
1599
1600void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1601 SlowPathCodeX86_64* slow_path =
1602 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
1603 codegen_->AddSlowPath(slow_path);
1604
1605 LocationSummary* locations = instruction->GetLocations();
1606 Location value = locations->InAt(0);
1607
1608 if (value.IsRegister()) {
1609 __ testl(value.As<CpuRegister>(), value.As<CpuRegister>());
1610 } else if (value.IsStackSlot()) {
1611 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
1612 } else {
1613 DCHECK(value.IsConstant()) << value;
1614 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
1615 __ jmp(slow_path->GetEntryLabel());
1616 }
1617 return;
1618 }
1619 __ j(kEqual, slow_path->GetEntryLabel());
1620}
1621
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001622void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001623 LocationSummary* locations =
1624 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001625 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001626 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1627 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1628 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001629}
1630
1631void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
1632 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001633 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001634 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1635
1636 __ gs()->call(Address::Absolute(
1637 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
1638
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001639 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001640 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001641}
1642
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001643void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
1644 LocationSummary* locations =
1645 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1646 InvokeRuntimeCallingConvention calling_convention;
1647 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1648 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1649 locations->SetOut(Location::RegisterLocation(RAX));
1650 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1651}
1652
1653void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
1654 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001655 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001656 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1657
1658 __ gs()->call(Address::Absolute(
1659 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocArrayWithAccessCheck), true));
1660
1661 DCHECK(!codegen_->IsLeafMethod());
1662 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1663}
1664
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001665void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001666 LocationSummary* locations =
1667 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001668 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1669 if (location.IsStackSlot()) {
1670 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1671 } else if (location.IsDoubleStackSlot()) {
1672 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1673 }
1674 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001675}
1676
1677void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
1678 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001679 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001680}
1681
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001682void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001683 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001684 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001685 locations->SetInAt(0, Location::RequiresRegister());
1686 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001687}
1688
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001689void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
1690 LocationSummary* locations = not_->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001691 DCHECK_EQ(locations->InAt(0).As<CpuRegister>().AsRegister(),
1692 locations->Out().As<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001693 Location out = locations->Out();
1694 switch (not_->InputAt(0)->GetType()) {
1695 case Primitive::kPrimBoolean:
1696 __ xorq(out.As<CpuRegister>(), Immediate(1));
1697 break;
1698
1699 case Primitive::kPrimInt:
1700 __ notl(out.As<CpuRegister>());
1701 break;
1702
1703 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001704 __ notq(out.As<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001705 break;
1706
1707 default:
1708 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1709 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001710}
1711
1712void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001713 LocationSummary* locations =
1714 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001715 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1716 locations->SetInAt(i, Location::Any());
1717 }
1718 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001719}
1720
1721void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001722 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001723 LOG(FATAL) << "Unimplemented";
1724}
1725
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001726void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001727 LocationSummary* locations =
1728 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001729 Primitive::Type field_type = instruction->GetFieldType();
1730 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001731 locations->SetInAt(0, Location::RequiresRegister());
1732 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001733 if (is_object_type) {
1734 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001735 locations->AddTemp(Location::RequiresRegister());
1736 locations->AddTemp(Location::RequiresRegister());
1737 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001738}
1739
1740void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1741 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001742 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001743 size_t offset = instruction->GetFieldOffset().SizeValue();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001744 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001745
1746 switch (field_type) {
1747 case Primitive::kPrimBoolean:
1748 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001749 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001750 __ movb(Address(obj, offset), value);
1751 break;
1752 }
1753
1754 case Primitive::kPrimShort:
1755 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001756 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001757 __ movw(Address(obj, offset), value);
1758 break;
1759 }
1760
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001761 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001762 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001763 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001764 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001765 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001766 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
1767 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001768 codegen_->MarkGCCard(temp, card, obj, value);
1769 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001770 break;
1771 }
1772
1773 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001774 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001775 __ movq(Address(obj, offset), value);
1776 break;
1777 }
1778
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001779 case Primitive::kPrimFloat: {
1780 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1781 __ movss(Address(obj, offset), value);
1782 break;
1783 }
1784
1785 case Primitive::kPrimDouble: {
1786 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1787 __ movsd(Address(obj, offset), value);
1788 break;
1789 }
1790
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001791 case Primitive::kPrimVoid:
1792 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001793 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001794 }
1795}
1796
1797void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001798 LocationSummary* locations =
1799 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001800 locations->SetInAt(0, Location::RequiresRegister());
1801 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001802}
1803
1804void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1805 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001806 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001807 size_t offset = instruction->GetFieldOffset().SizeValue();
1808
1809 switch (instruction->GetType()) {
1810 case Primitive::kPrimBoolean: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001811 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001812 __ movzxb(out, Address(obj, offset));
1813 break;
1814 }
1815
1816 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001817 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001818 __ movsxb(out, Address(obj, offset));
1819 break;
1820 }
1821
1822 case Primitive::kPrimShort: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001823 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001824 __ movsxw(out, Address(obj, offset));
1825 break;
1826 }
1827
1828 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001829 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001830 __ movzxw(out, Address(obj, offset));
1831 break;
1832 }
1833
1834 case Primitive::kPrimInt:
1835 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001836 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001837 __ movl(out, Address(obj, offset));
1838 break;
1839 }
1840
1841 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001842 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001843 __ movq(out, Address(obj, offset));
1844 break;
1845 }
1846
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001847 case Primitive::kPrimFloat: {
1848 XmmRegister out = locations->Out().As<XmmRegister>();
1849 __ movss(out, Address(obj, offset));
1850 break;
1851 }
1852
1853 case Primitive::kPrimDouble: {
1854 XmmRegister out = locations->Out().As<XmmRegister>();
1855 __ movsd(out, Address(obj, offset));
1856 break;
1857 }
1858
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001859 case Primitive::kPrimVoid:
1860 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001861 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001862 }
1863}
1864
1865void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001866 LocationSummary* locations =
1867 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001868 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001869 if (instruction->HasUses()) {
1870 locations->SetOut(Location::SameAsFirstInput());
1871 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001872}
1873
1874void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001875 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001876 codegen_->AddSlowPath(slow_path);
1877
1878 LocationSummary* locations = instruction->GetLocations();
1879 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001880
1881 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001882 __ cmpl(obj.As<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001883 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001884 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001885 } else {
1886 DCHECK(obj.IsConstant()) << obj;
1887 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1888 __ jmp(slow_path->GetEntryLabel());
1889 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001890 }
1891 __ j(kEqual, slow_path->GetEntryLabel());
1892}
1893
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001894void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001895 LocationSummary* locations =
1896 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001897 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001898 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001899 1, Location::RegisterOrConstant(instruction->InputAt(1)));
1900 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001901}
1902
1903void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
1904 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001905 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001906 Location index = locations->InAt(1);
1907
1908 switch (instruction->GetType()) {
1909 case Primitive::kPrimBoolean: {
1910 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001911 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001912 if (index.IsConstant()) {
1913 __ movzxb(out, Address(obj,
1914 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1915 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001916 __ movzxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001917 }
1918 break;
1919 }
1920
1921 case Primitive::kPrimByte: {
1922 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001923 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001924 if (index.IsConstant()) {
1925 __ movsxb(out, Address(obj,
1926 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1927 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001928 __ movsxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001929 }
1930 break;
1931 }
1932
1933 case Primitive::kPrimShort: {
1934 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001935 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001936 if (index.IsConstant()) {
1937 __ movsxw(out, Address(obj,
1938 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1939 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001940 __ movsxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001941 }
1942 break;
1943 }
1944
1945 case Primitive::kPrimChar: {
1946 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001947 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001948 if (index.IsConstant()) {
1949 __ movzxw(out, Address(obj,
1950 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1951 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001952 __ movzxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001953 }
1954 break;
1955 }
1956
1957 case Primitive::kPrimInt:
1958 case Primitive::kPrimNot: {
1959 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1960 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001961 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001962 if (index.IsConstant()) {
1963 __ movl(out, Address(obj,
1964 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1965 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001966 __ movl(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001967 }
1968 break;
1969 }
1970
1971 case Primitive::kPrimLong: {
1972 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001973 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001974 if (index.IsConstant()) {
1975 __ movq(out, Address(obj,
1976 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
1977 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001978 __ movq(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001979 }
1980 break;
1981 }
1982
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001983 case Primitive::kPrimFloat: {
1984 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1985 XmmRegister out = locations->Out().As<XmmRegister>();
1986 if (index.IsConstant()) {
1987 __ movss(out, Address(obj,
1988 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1989 } else {
1990 __ movss(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
1991 }
1992 break;
1993 }
1994
1995 case Primitive::kPrimDouble: {
1996 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1997 XmmRegister out = locations->Out().As<XmmRegister>();
1998 if (index.IsConstant()) {
1999 __ movsd(out, Address(obj,
2000 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2001 } else {
2002 __ movsd(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
2003 }
2004 break;
2005 }
2006
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002007 case Primitive::kPrimVoid:
2008 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002009 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002010 }
2011}
2012
2013void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002014 Primitive::Type value_type = instruction->GetComponentType();
2015 bool is_object = value_type == Primitive::kPrimNot;
2016 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2017 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
2018 if (is_object) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002019 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002020 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2021 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2022 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002023 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002024 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002025 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002026 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2027 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002028 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002029 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002030 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
2031 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002032 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002033 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002034 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002035 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002036}
2037
2038void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
2039 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002040 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002041 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002042 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002043 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002044
2045 switch (value_type) {
2046 case Primitive::kPrimBoolean:
2047 case Primitive::kPrimByte: {
2048 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002049 if (index.IsConstant()) {
2050 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002051 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002052 __ movb(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002053 } else {
2054 __ movb(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2055 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002056 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002057 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002058 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
2059 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002060 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002061 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002062 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2063 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002064 }
2065 break;
2066 }
2067
2068 case Primitive::kPrimShort:
2069 case Primitive::kPrimChar: {
2070 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002071 if (index.IsConstant()) {
2072 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002073 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002074 __ movw(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002075 } else {
2076 __ movw(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2077 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002078 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002079 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002080 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
2081 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002082 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002083 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002084 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2085 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002086 }
2087 break;
2088 }
2089
2090 case Primitive::kPrimInt: {
2091 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002092 if (index.IsConstant()) {
2093 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002094 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002095 __ movl(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002096 } else {
2097 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2098 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002099 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002100 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002101 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2102 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002103 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002104 DCHECK(value.IsConstant()) << value;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002105 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002106 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2107 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002108 }
2109 break;
2110 }
2111
2112 case Primitive::kPrimNot: {
2113 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject), true));
2114 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002115 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002116 break;
2117 }
2118
2119 case Primitive::kPrimLong: {
2120 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002121 if (index.IsConstant()) {
2122 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002123 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002124 __ movq(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002125 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002126 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002127 __ movq(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
2128 value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002129 }
2130 break;
2131 }
2132
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002133 case Primitive::kPrimFloat: {
2134 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2135 if (index.IsConstant()) {
2136 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2137 DCHECK(value.IsFpuRegister());
2138 __ movss(Address(obj, offset), value.As<XmmRegister>());
2139 } else {
2140 DCHECK(value.IsFpuRegister());
2141 __ movss(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2142 value.As<XmmRegister>());
2143 }
2144 break;
2145 }
2146
2147 case Primitive::kPrimDouble: {
2148 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2149 if (index.IsConstant()) {
2150 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
2151 DCHECK(value.IsFpuRegister());
2152 __ movsd(Address(obj, offset), value.As<XmmRegister>());
2153 } else {
2154 DCHECK(value.IsFpuRegister());
2155 __ movsd(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
2156 value.As<XmmRegister>());
2157 }
2158 break;
2159 }
2160
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002161 case Primitive::kPrimVoid:
2162 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002163 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002164 }
2165}
2166
2167void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002168 LocationSummary* locations =
2169 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002170 locations->SetInAt(0, Location::RequiresRegister());
2171 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002172}
2173
2174void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
2175 LocationSummary* locations = instruction->GetLocations();
2176 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002177 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2178 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002179 __ movl(out, Address(obj, offset));
2180}
2181
2182void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002183 LocationSummary* locations =
2184 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002185 locations->SetInAt(0, Location::RequiresRegister());
2186 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002187 if (instruction->HasUses()) {
2188 locations->SetOut(Location::SameAsFirstInput());
2189 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002190}
2191
2192void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
2193 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002194 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002195 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002196 codegen_->AddSlowPath(slow_path);
2197
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002198 CpuRegister index = locations->InAt(0).As<CpuRegister>();
2199 CpuRegister length = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002200
2201 __ cmpl(index, length);
2202 __ j(kAboveEqual, slow_path->GetEntryLabel());
2203}
2204
2205void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
2206 CpuRegister card,
2207 CpuRegister object,
2208 CpuRegister value) {
2209 Label is_null;
2210 __ testl(value, value);
2211 __ j(kEqual, &is_null);
2212 __ gs()->movq(card, Address::Absolute(
2213 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
2214 __ movq(temp, object);
2215 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
2216 __ movb(Address(temp, card, TIMES_1, 0), card);
2217 __ Bind(&is_null);
2218}
2219
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002220void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
2221 temp->SetLocations(nullptr);
2222}
2223
2224void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
2225 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002226 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002227}
2228
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002229void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002230 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002231 LOG(FATAL) << "Unimplemented";
2232}
2233
2234void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002235 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2236}
2237
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002238void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
2239 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2240}
2241
2242void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002243 HBasicBlock* block = instruction->GetBlock();
2244 if (block->GetLoopInformation() != nullptr) {
2245 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2246 // The back edge will generate the suspend check.
2247 return;
2248 }
2249 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2250 // The goto will generate the suspend check.
2251 return;
2252 }
2253 GenerateSuspendCheck(instruction, nullptr);
2254}
2255
2256void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
2257 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002258 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002259 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002260 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002261 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002262 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002263 if (successor == nullptr) {
2264 __ j(kNotEqual, slow_path->GetEntryLabel());
2265 __ Bind(slow_path->GetReturnLabel());
2266 } else {
2267 __ j(kEqual, codegen_->GetLabelOf(successor));
2268 __ jmp(slow_path->GetEntryLabel());
2269 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002270}
2271
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002272X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
2273 return codegen_->GetAssembler();
2274}
2275
2276void ParallelMoveResolverX86_64::EmitMove(size_t index) {
2277 MoveOperands* move = moves_.Get(index);
2278 Location source = move->GetSource();
2279 Location destination = move->GetDestination();
2280
2281 if (source.IsRegister()) {
2282 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002283 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002284 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002285 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002286 source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002287 } else {
2288 DCHECK(destination.IsDoubleStackSlot());
2289 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002290 source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002291 }
2292 } else if (source.IsStackSlot()) {
2293 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002294 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002295 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002296 } else if (destination.IsFpuRegister()) {
2297 __ movss(destination.As<XmmRegister>(),
2298 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002299 } else {
2300 DCHECK(destination.IsStackSlot());
2301 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2302 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2303 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002304 } else if (source.IsDoubleStackSlot()) {
2305 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002306 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002307 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002308 } else if (destination.IsFpuRegister()) {
2309 __ movsd(destination.As<XmmRegister>(), Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002310 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01002311 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002312 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2313 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2314 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002315 } else if (source.IsConstant()) {
2316 HConstant* constant = source.GetConstant();
2317 if (constant->IsIntConstant()) {
2318 Immediate imm(constant->AsIntConstant()->GetValue());
2319 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002320 __ movl(destination.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002321 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002322 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002323 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2324 }
2325 } else if (constant->IsLongConstant()) {
2326 int64_t value = constant->AsLongConstant()->GetValue();
2327 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002328 __ movq(destination.As<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002329 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002330 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002331 __ movq(CpuRegister(TMP), Immediate(value));
2332 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2333 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002334 } else if (constant->IsFloatConstant()) {
2335 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
2336 if (destination.IsFpuRegister()) {
2337 __ movl(CpuRegister(TMP), imm);
2338 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2339 } else {
2340 DCHECK(destination.IsStackSlot()) << destination;
2341 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2342 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002343 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002344 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
2345 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
2346 if (destination.IsFpuRegister()) {
2347 __ movq(CpuRegister(TMP), imm);
2348 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2349 } else {
2350 DCHECK(destination.IsDoubleStackSlot()) << destination;
2351 __ movq(CpuRegister(TMP), imm);
2352 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2353 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002354 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002355 } else if (source.IsFpuRegister()) {
2356 if (destination.IsFpuRegister()) {
2357 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
2358 } else if (destination.IsStackSlot()) {
2359 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
2360 source.As<XmmRegister>());
2361 } else {
2362 DCHECK(destination.IsDoubleStackSlot());
2363 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
2364 source.As<XmmRegister>());
2365 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002366 }
2367}
2368
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002369void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002370 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002371 __ movl(Address(CpuRegister(RSP), mem), reg);
2372 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002373}
2374
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002375void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002376 ScratchRegisterScope ensure_scratch(
2377 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2378
2379 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2380 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2381 __ movl(CpuRegister(ensure_scratch.GetRegister()),
2382 Address(CpuRegister(RSP), mem2 + stack_offset));
2383 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2384 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
2385 CpuRegister(ensure_scratch.GetRegister()));
2386}
2387
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002388void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
2389 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2390 __ movq(Address(CpuRegister(RSP), mem), reg);
2391 __ movq(reg, CpuRegister(TMP));
2392}
2393
2394void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
2395 ScratchRegisterScope ensure_scratch(
2396 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2397
2398 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2399 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2400 __ movq(CpuRegister(ensure_scratch.GetRegister()),
2401 Address(CpuRegister(RSP), mem2 + stack_offset));
2402 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2403 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
2404 CpuRegister(ensure_scratch.GetRegister()));
2405}
2406
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002407void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
2408 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2409 __ movss(Address(CpuRegister(RSP), mem), reg);
2410 __ movd(reg, CpuRegister(TMP));
2411}
2412
2413void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
2414 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2415 __ movsd(Address(CpuRegister(RSP), mem), reg);
2416 __ movd(reg, CpuRegister(TMP));
2417}
2418
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002419void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
2420 MoveOperands* move = moves_.Get(index);
2421 Location source = move->GetSource();
2422 Location destination = move->GetDestination();
2423
2424 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002425 __ xchgq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002426 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002427 Exchange32(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002428 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002429 Exchange32(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002430 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002431 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
2432 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002433 Exchange64(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002434 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002435 Exchange64(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002436 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
2437 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002438 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
2439 __ movd(CpuRegister(TMP), source.As<XmmRegister>());
2440 __ movaps(source.As<XmmRegister>(), destination.As<XmmRegister>());
2441 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2442 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
2443 Exchange32(source.As<XmmRegister>(), destination.GetStackIndex());
2444 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
2445 Exchange32(destination.As<XmmRegister>(), source.GetStackIndex());
2446 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
2447 Exchange64(source.As<XmmRegister>(), destination.GetStackIndex());
2448 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
2449 Exchange64(destination.As<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002450 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002451 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002452 }
2453}
2454
2455
2456void ParallelMoveResolverX86_64::SpillScratch(int reg) {
2457 __ pushq(CpuRegister(reg));
2458}
2459
2460
2461void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
2462 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002463}
2464
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002465void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
2466 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
2467 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2468 Immediate(mirror::Class::kStatusInitialized));
2469 __ j(kLess, slow_path->GetEntryLabel());
2470 __ Bind(slow_path->GetExitLabel());
2471 // No need for memory fence, thanks to the X86_64 memory model.
2472}
2473
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002474void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002475 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2476 ? LocationSummary::kCallOnSlowPath
2477 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002478 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002479 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002480 locations->SetOut(Location::RequiresRegister());
2481}
2482
2483void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
2484 CpuRegister out = cls->GetLocations()->Out().As<CpuRegister>();
2485 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002486 DCHECK(!cls->CanCallRuntime());
2487 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002488 codegen_->LoadCurrentMethod(out);
2489 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2490 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002491 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002492 codegen_->LoadCurrentMethod(out);
2493 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2494 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002495 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
2496 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2497 codegen_->AddSlowPath(slow_path);
2498 __ testl(out, out);
2499 __ j(kEqual, slow_path->GetEntryLabel());
2500 if (cls->MustGenerateClinitCheck()) {
2501 GenerateClassInitializationCheck(slow_path, out);
2502 } else {
2503 __ Bind(slow_path->GetExitLabel());
2504 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002505 }
2506}
2507
2508void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
2509 LocationSummary* locations =
2510 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2511 locations->SetInAt(0, Location::RequiresRegister());
2512 if (check->HasUses()) {
2513 locations->SetOut(Location::SameAsFirstInput());
2514 }
2515}
2516
2517void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002518 // We assume the class to not be null.
2519 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
2520 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002521 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002522 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002523}
2524
2525void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2526 LocationSummary* locations =
2527 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2528 locations->SetInAt(0, Location::RequiresRegister());
2529 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2530}
2531
2532void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2533 LocationSummary* locations = instruction->GetLocations();
2534 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002535 size_t offset = instruction->GetFieldOffset().SizeValue();
2536
2537 switch (instruction->GetType()) {
2538 case Primitive::kPrimBoolean: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002539 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002540 __ movzxb(out, Address(cls, offset));
2541 break;
2542 }
2543
2544 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002545 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002546 __ movsxb(out, Address(cls, offset));
2547 break;
2548 }
2549
2550 case Primitive::kPrimShort: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002551 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002552 __ movsxw(out, Address(cls, offset));
2553 break;
2554 }
2555
2556 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002557 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002558 __ movzxw(out, Address(cls, offset));
2559 break;
2560 }
2561
2562 case Primitive::kPrimInt:
2563 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002564 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002565 __ movl(out, Address(cls, offset));
2566 break;
2567 }
2568
2569 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002570 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002571 __ movq(out, Address(cls, offset));
2572 break;
2573 }
2574
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002575 case Primitive::kPrimFloat: {
2576 XmmRegister out = locations->Out().As<XmmRegister>();
2577 __ movss(out, Address(cls, offset));
2578 break;
2579 }
2580
2581 case Primitive::kPrimDouble: {
2582 XmmRegister out = locations->Out().As<XmmRegister>();
2583 __ movsd(out, Address(cls, offset));
2584 break;
2585 }
2586
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002587 case Primitive::kPrimVoid:
2588 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2589 UNREACHABLE();
2590 }
2591}
2592
2593void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2594 LocationSummary* locations =
2595 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2596 Primitive::Type field_type = instruction->GetFieldType();
2597 bool is_object_type = field_type == Primitive::kPrimNot;
2598 locations->SetInAt(0, Location::RequiresRegister());
2599 locations->SetInAt(1, Location::RequiresRegister());
2600 if (is_object_type) {
2601 // Temporary registers for the write barrier.
2602 locations->AddTemp(Location::RequiresRegister());
2603 locations->AddTemp(Location::RequiresRegister());
2604 }
2605}
2606
2607void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2608 LocationSummary* locations = instruction->GetLocations();
2609 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002610 size_t offset = instruction->GetFieldOffset().SizeValue();
2611 Primitive::Type field_type = instruction->GetFieldType();
2612
2613 switch (field_type) {
2614 case Primitive::kPrimBoolean:
2615 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002616 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002617 __ movb(Address(cls, offset), value);
2618 break;
2619 }
2620
2621 case Primitive::kPrimShort:
2622 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002623 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002624 __ movw(Address(cls, offset), value);
2625 break;
2626 }
2627
2628 case Primitive::kPrimInt:
2629 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002630 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002631 __ movl(Address(cls, offset), value);
2632 if (field_type == Primitive::kPrimNot) {
2633 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
2634 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
2635 codegen_->MarkGCCard(temp, card, cls, value);
2636 }
2637 break;
2638 }
2639
2640 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002641 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002642 __ movq(Address(cls, offset), value);
2643 break;
2644 }
2645
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002646 case Primitive::kPrimFloat: {
2647 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2648 __ movss(Address(cls, offset), value);
2649 break;
2650 }
2651
2652 case Primitive::kPrimDouble: {
2653 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2654 __ movsd(Address(cls, offset), value);
2655 break;
2656 }
2657
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002658 case Primitive::kPrimVoid:
2659 LOG(FATAL) << "Unreachable type " << field_type;
2660 UNREACHABLE();
2661 }
2662}
2663
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002664void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
2665 LocationSummary* locations =
2666 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2667 locations->SetOut(Location::RequiresRegister());
2668}
2669
2670void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
2671 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
2672 codegen_->AddSlowPath(slow_path);
2673
2674 CpuRegister out = load->GetLocations()->Out().As<CpuRegister>();
2675 codegen_->LoadCurrentMethod(CpuRegister(out));
2676 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
2677 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2678 __ testl(out, out);
2679 __ j(kEqual, slow_path->GetEntryLabel());
2680 __ Bind(slow_path->GetExitLabel());
2681}
2682
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002683void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
2684 LocationSummary* locations =
2685 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2686 locations->SetOut(Location::RequiresRegister());
2687}
2688
2689void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
2690 Address address = Address::Absolute(
2691 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
2692 __ gs()->movl(load->GetLocations()->Out().As<CpuRegister>(), address);
2693 __ gs()->movl(address, Immediate(0));
2694}
2695
2696void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
2697 LocationSummary* locations =
2698 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2699 InvokeRuntimeCallingConvention calling_convention;
2700 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2701}
2702
2703void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
2704 __ gs()->call(
2705 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
2706 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2707}
2708
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002709void LocationsBuilderX86_64::VisitTypeCheck(HTypeCheck* instruction) {
2710 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
2711 ? LocationSummary::kNoCall
2712 : LocationSummary::kCallOnSlowPath;
2713 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2714 locations->SetInAt(0, Location::RequiresRegister());
2715 locations->SetInAt(1, Location::Any());
2716 locations->SetOut(Location::RequiresRegister());
2717}
2718
2719void InstructionCodeGeneratorX86_64::VisitTypeCheck(HTypeCheck* instruction) {
2720 LocationSummary* locations = instruction->GetLocations();
2721 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2722 Location cls = locations->InAt(1);
2723 CpuRegister out = locations->Out().As<CpuRegister>();
2724 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2725 Label done, zero;
2726 SlowPathCodeX86_64* slow_path = nullptr;
2727
2728 // Return 0 if `obj` is null.
2729 // TODO: avoid this check if we know obj is not null.
2730 __ testl(obj, obj);
2731 __ j(kEqual, &zero);
2732 // Compare the class of `obj` with `cls`.
2733 __ movl(out, Address(obj, class_offset));
2734 if (cls.IsRegister()) {
2735 __ cmpl(out, cls.As<CpuRegister>());
2736 } else {
2737 DCHECK(cls.IsStackSlot()) << cls;
2738 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
2739 }
2740 if (instruction->IsClassFinal()) {
2741 // Classes must be equal for the instanceof to succeed.
2742 __ j(kNotEqual, &zero);
2743 __ movl(out, Immediate(1));
2744 __ jmp(&done);
2745 } else {
2746 // If the classes are not equal, we go into a slow path.
2747 DCHECK(locations->OnlyCallsOnSlowPath());
2748 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
2749 instruction, Location::RegisterLocation(out.AsRegister()));
2750 codegen_->AddSlowPath(slow_path);
2751 __ j(kNotEqual, slow_path->GetEntryLabel());
2752 __ movl(out, Immediate(1));
2753 __ jmp(&done);
2754 }
2755 __ Bind(&zero);
2756 __ movl(out, Immediate(0));
2757 if (slow_path != nullptr) {
2758 __ Bind(slow_path->GetExitLabel());
2759 }
2760 __ Bind(&done);
2761}
2762
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002763} // namespace x86_64
2764} // namespace art