blob: d37ef06687c3ae9a4cf247e65bf94a03b0d39bef [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
63class NullCheckSlowPathX86_64 : public SlowPathCode {
64 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010065 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010066
67 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
68 __ Bind(GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010069 __ gs()->call(
70 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowNullPointer), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +010071 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010072 }
73
74 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010075 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
77};
78
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010079class StackOverflowCheckSlowPathX86_64 : public SlowPathCode {
80 public:
81 StackOverflowCheckSlowPathX86_64() {}
82
83 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
84 __ Bind(GetEntryLabel());
85 __ addq(CpuRegister(RSP),
86 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
87 __ gs()->jmp(
88 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowStackOverflow), true));
89 }
90
91 private:
92 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86_64);
93};
94
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000095class SuspendCheckSlowPathX86_64 : public SlowPathCode {
96 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +010097 explicit SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
98 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +000099
100 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
101 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100102 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000103 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pTestSuspend), true));
104 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100105 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100106 if (successor_ == nullptr) {
107 __ jmp(GetReturnLabel());
108 } else {
109 __ jmp(codegen->GetLabelOf(successor_));
110 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000111 }
112
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100113 Label* GetReturnLabel() {
114 DCHECK(successor_ == nullptr);
115 return &return_label_;
116 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000117
118 private:
119 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100120 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000121 Label return_label_;
122
123 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
124};
125
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100126class BoundsCheckSlowPathX86_64 : public SlowPathCode {
127 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100128 BoundsCheckSlowPathX86_64(HBoundsCheck* instruction,
129 Location index_location,
130 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100131 : instruction_(instruction),
132 index_location_(index_location),
133 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100134
135 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
136 CodeGeneratorX86_64* x64_codegen = reinterpret_cast<CodeGeneratorX86_64*>(codegen);
137 __ Bind(GetEntryLabel());
138 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100139 x64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), index_location_);
140 x64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(1)), length_location_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100141 __ gs()->call(Address::Absolute(
142 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowArrayBounds), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100143 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100144 }
145
146 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100147 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100148 const Location index_location_;
149 const Location length_location_;
150
151 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
152};
153
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100154#undef __
155#define __ reinterpret_cast<X86_64Assembler*>(GetAssembler())->
156
Dave Allison20dfc792014-06-16 20:44:29 -0700157inline Condition X86_64Condition(IfCondition cond) {
158 switch (cond) {
159 case kCondEQ: return kEqual;
160 case kCondNE: return kNotEqual;
161 case kCondLT: return kLess;
162 case kCondLE: return kLessEqual;
163 case kCondGT: return kGreater;
164 case kCondGE: return kGreaterEqual;
165 default:
166 LOG(FATAL) << "Unknown if condition";
167 }
168 return kEqual;
169}
170
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100171void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
172 stream << X86_64ManagedRegister::FromCpuRegister(Register(reg));
173}
174
175void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
176 stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg));
177}
178
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100179void CodeGeneratorX86_64::SaveCoreRegister(Location stack_location, uint32_t reg_id) {
180 __ movq(Address(CpuRegister(RSP), stack_location.GetStackIndex()), CpuRegister(reg_id));
181}
182
183void CodeGeneratorX86_64::RestoreCoreRegister(Location stack_location, uint32_t reg_id) {
184 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_location.GetStackIndex()));
185}
186
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100187CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph)
188 : CodeGenerator(graph, kNumberOfRegIds),
189 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000190 instruction_visitor_(graph, this),
191 move_resolver_(graph->GetArena(), this) {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100192
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100193size_t CodeGeneratorX86_64::FrameEntrySpillSize() const {
194 return kNumberOfPushedRegistersAtEntry * kX86_64WordSize;
195}
196
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100197InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
198 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100199 : HGraphVisitor(graph),
200 assembler_(codegen->GetAssembler()),
201 codegen_(codegen) {}
202
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100203Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type,
204 bool* blocked_registers) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100205 switch (type) {
206 case Primitive::kPrimLong:
207 case Primitive::kPrimByte:
208 case Primitive::kPrimBoolean:
209 case Primitive::kPrimChar:
210 case Primitive::kPrimShort:
211 case Primitive::kPrimInt:
212 case Primitive::kPrimNot: {
213 size_t reg = AllocateFreeRegisterInternal(blocked_registers, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100214 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100215 }
216
217 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100218 case Primitive::kPrimDouble: {
219 size_t reg = AllocateFreeRegisterInternal(
220 blocked_registers + kNumberOfCpuRegisters, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100221 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100222 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100223
224 case Primitive::kPrimVoid:
225 LOG(FATAL) << "Unreachable type " << type;
226 }
227
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100228 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100229}
230
231void CodeGeneratorX86_64::SetupBlockedRegisters(bool* blocked_registers) const {
232 // Stack register is always reserved.
233 blocked_registers[RSP] = true;
234
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000235 // Block the register used as TMP.
236 blocked_registers[TMP] = true;
237
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100238 // TODO: We currently don't use Quick's callee saved registers.
239 blocked_registers[RBX] = true;
240 blocked_registers[RBP] = true;
241 blocked_registers[R12] = true;
242 blocked_registers[R13] = true;
243 blocked_registers[R14] = true;
244 blocked_registers[R15] = true;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100245
246 bool* blocked_xmm_registers = blocked_registers + kNumberOfCpuRegisters;
247 blocked_xmm_registers[XMM12] = true;
248 blocked_xmm_registers[XMM13] = true;
249 blocked_xmm_registers[XMM14] = true;
250 blocked_xmm_registers[XMM15] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100251}
252
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100253void CodeGeneratorX86_64::GenerateFrameEntry() {
254 // Create a fake register to mimic Quick.
255 static const int kFakeReturnRegister = 16;
256 core_spill_mask_ |= (1 << kFakeReturnRegister);
257
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100258 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700259 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100260
261 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
262 __ testq(CpuRegister(RAX), Address(
263 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100264 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100265 }
266
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100267 // The return PC has already been pushed on the stack.
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100268 __ subq(CpuRegister(RSP),
269 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
270
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100271 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
272 SlowPathCode* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86_64();
273 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100274
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100275 __ gs()->cmpq(CpuRegister(RSP),
276 Address::Absolute(Thread::StackEndOffset<kX86_64WordSize>(), true));
277 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100278 }
279
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100280 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
281}
282
283void CodeGeneratorX86_64::GenerateFrameExit() {
284 __ addq(CpuRegister(RSP),
285 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
286}
287
288void CodeGeneratorX86_64::Bind(Label* label) {
289 __ Bind(label);
290}
291
292void InstructionCodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
293 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
294}
295
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100296Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
297 switch (load->GetType()) {
298 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100299 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100300 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
301 break;
302
303 case Primitive::kPrimInt:
304 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100305 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100306 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100307
308 case Primitive::kPrimBoolean:
309 case Primitive::kPrimByte:
310 case Primitive::kPrimChar:
311 case Primitive::kPrimShort:
312 case Primitive::kPrimVoid:
313 LOG(FATAL) << "Unexpected type " << load->GetType();
314 }
315
316 LOG(FATAL) << "Unreachable";
317 return Location();
318}
319
320void CodeGeneratorX86_64::Move(Location destination, Location source) {
321 if (source.Equals(destination)) {
322 return;
323 }
324 if (destination.IsRegister()) {
325 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100326 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100327 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100328 __ movd(destination.As<CpuRegister>(), source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100329 } else if (source.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100330 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100331 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100332 } else {
333 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100334 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100335 Address(CpuRegister(RSP), source.GetStackIndex()));
336 }
337 } else if (destination.IsFpuRegister()) {
338 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100339 __ movd(destination.As<XmmRegister>(), source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100340 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100341 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100342 } else if (source.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100343 __ movss(destination.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100344 Address(CpuRegister(RSP), source.GetStackIndex()));
345 } else {
346 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100347 __ movsd(destination.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100348 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100349 }
350 } else if (destination.IsStackSlot()) {
351 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100352 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100353 source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100354 } else if (source.IsFpuRegister()) {
355 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100356 source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100357 } else {
358 DCHECK(source.IsStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000359 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
360 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100361 }
362 } else {
363 DCHECK(destination.IsDoubleStackSlot());
364 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100365 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100366 source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100367 } else if (source.IsFpuRegister()) {
368 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100369 source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100370 } else {
371 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000372 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
373 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100374 }
375 }
376}
377
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100378void CodeGeneratorX86_64::Move(HInstruction* instruction,
379 Location location,
380 HInstruction* move_for) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100381 if (instruction->AsIntConstant() != nullptr) {
382 Immediate imm(instruction->AsIntConstant()->GetValue());
383 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100384 __ movl(location.As<CpuRegister>(), imm);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100385 } else {
386 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
387 }
388 } else if (instruction->AsLongConstant() != nullptr) {
389 int64_t value = instruction->AsLongConstant()->GetValue();
390 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100391 __ movq(location.As<CpuRegister>(), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100392 } else {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000393 __ movq(CpuRegister(TMP), Immediate(value));
394 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100395 }
396 } else if (instruction->AsLoadLocal() != nullptr) {
397 switch (instruction->GetType()) {
398 case Primitive::kPrimBoolean:
399 case Primitive::kPrimByte:
400 case Primitive::kPrimChar:
401 case Primitive::kPrimShort:
402 case Primitive::kPrimInt:
403 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100404 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100405 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
406 break;
407
408 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100409 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100410 Move(location, Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
411 break;
412
413 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100414 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100415 }
416 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100417 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100418 switch (instruction->GetType()) {
419 case Primitive::kPrimBoolean:
420 case Primitive::kPrimByte:
421 case Primitive::kPrimChar:
422 case Primitive::kPrimShort:
423 case Primitive::kPrimInt:
424 case Primitive::kPrimNot:
425 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100426 case Primitive::kPrimFloat:
427 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100428 Move(location, instruction->GetLocations()->Out());
429 break;
430
431 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100432 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100433 }
434 }
435}
436
437void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
438 got->SetLocations(nullptr);
439}
440
441void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
442 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100443 DCHECK(!successor->IsExitBlock());
444
445 HBasicBlock* block = got->GetBlock();
446 HInstruction* previous = got->GetPrevious();
447
448 HLoopInformation* info = block->GetLoopInformation();
449 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
450 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
451 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
452 return;
453 }
454
455 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
456 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
457 }
458 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100459 __ jmp(codegen_->GetLabelOf(successor));
460 }
461}
462
463void LocationsBuilderX86_64::VisitExit(HExit* exit) {
464 exit->SetLocations(nullptr);
465}
466
467void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
468 if (kIsDebugBuild) {
469 __ Comment("Unreachable");
470 __ int3();
471 }
472}
473
474void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100475 LocationSummary* locations =
476 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100477 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100478 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +0100479 locations->SetInAt(0, Location::Any(), Location::kDiesAtEntry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100480 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100481}
482
483void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700484 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100485 bool materialized = !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
486 // Moves do not affect the eflags register, so if the condition is evaluated
487 // just before the if, we don't need to evaluate it again.
488 bool eflags_set = cond->IsCondition()
489 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
490 if (materialized) {
491 if (!eflags_set) {
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100492 // Materialized condition, compare against 0.
493 Location lhs = if_instr->GetLocations()->InAt(0);
494 if (lhs.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100495 __ cmpl(lhs.As<CpuRegister>(), Immediate(0));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100496 } else {
497 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
498 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100499 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
500 } else {
501 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
502 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700503 }
Dave Allison20dfc792014-06-16 20:44:29 -0700504 } else {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100505 Location lhs = cond->GetLocations()->InAt(0);
506 Location rhs = cond->GetLocations()->InAt(1);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100507 if (rhs.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100508 __ cmpl(lhs.As<CpuRegister>(), rhs.As<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100509 } else if (rhs.IsConstant()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100510 __ cmpl(lhs.As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100511 Immediate(rhs.GetConstant()->AsIntConstant()->GetValue()));
512 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100513 __ cmpl(lhs.As<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100514 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100515 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
Dave Allison20dfc792014-06-16 20:44:29 -0700516 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
517 }
518 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfFalseSuccessor())) {
519 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100520 }
521}
522
523void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
524 local->SetLocations(nullptr);
525}
526
527void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
528 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
529}
530
531void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
532 local->SetLocations(nullptr);
533}
534
535void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
536 // Nothing to do, this is driven by the code generator.
537}
538
539void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100540 LocationSummary* locations =
541 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100542 switch (store->InputAt(1)->GetType()) {
543 case Primitive::kPrimBoolean:
544 case Primitive::kPrimByte:
545 case Primitive::kPrimChar:
546 case Primitive::kPrimShort:
547 case Primitive::kPrimInt:
548 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100549 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100550 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
551 break;
552
553 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100554 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100555 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
556 break;
557
558 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100559 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100560 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100561}
562
563void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
564}
565
Dave Allison20dfc792014-06-16 20:44:29 -0700566void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100567 LocationSummary* locations =
568 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +0100569 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
570 locations->SetInAt(1, Location::Any(), Location::kDiesAtEntry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100571 if (comp->NeedsMaterialization()) {
572 locations->SetOut(Location::RequiresRegister());
573 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100574}
575
Dave Allison20dfc792014-06-16 20:44:29 -0700576void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
577 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100578 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100579 CpuRegister reg = locations->Out().As<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100580 // Clear register: setcc only sets the low byte.
581 __ xorq(reg, reg);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100582 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100583 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100584 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100585 } else if (locations->InAt(1).IsConstant()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100586 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100587 Immediate(locations->InAt(1).GetConstant()->AsIntConstant()->GetValue()));
588 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100589 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100590 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
591 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100592 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700593 }
594}
595
596void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
597 VisitCondition(comp);
598}
599
600void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
601 VisitCondition(comp);
602}
603
604void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
605 VisitCondition(comp);
606}
607
608void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
609 VisitCondition(comp);
610}
611
612void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
613 VisitCondition(comp);
614}
615
616void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
617 VisitCondition(comp);
618}
619
620void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
621 VisitCondition(comp);
622}
623
624void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
625 VisitCondition(comp);
626}
627
628void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
629 VisitCondition(comp);
630}
631
632void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
633 VisitCondition(comp);
634}
635
636void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
637 VisitCondition(comp);
638}
639
640void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
641 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100642}
643
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100644void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100645 LocationSummary* locations =
646 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +0100647 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
648 locations->SetInAt(1, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100649 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100650}
651
652void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
653 Label greater, done;
654 LocationSummary* locations = compare->GetLocations();
655 switch (compare->InputAt(0)->GetType()) {
656 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100657 __ cmpq(locations->InAt(0).As<CpuRegister>(),
658 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100659 break;
660 default:
661 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
662 }
663
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100664 CpuRegister output = locations->Out().As<CpuRegister>();
665 __ movl(output, Immediate(0));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100666 __ j(kEqual, &done);
667 __ j(kGreater, &greater);
668
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100669 __ movl(output, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100670 __ jmp(&done);
671
672 __ Bind(&greater);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100673 __ movl(output, Immediate(1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100674
675 __ Bind(&done);
676}
677
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100678void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100679 LocationSummary* locations =
680 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100681 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100682}
683
684void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100685}
686
687void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100688 LocationSummary* locations =
689 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100690 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100691}
692
693void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100694}
695
696void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
697 ret->SetLocations(nullptr);
698}
699
700void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
701 codegen_->GenerateFrameExit();
702 __ ret();
703}
704
705void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100706 LocationSummary* locations =
707 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100708 switch (ret->InputAt(0)->GetType()) {
709 case Primitive::kPrimBoolean:
710 case Primitive::kPrimByte:
711 case Primitive::kPrimChar:
712 case Primitive::kPrimShort:
713 case Primitive::kPrimInt:
714 case Primitive::kPrimNot:
715 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100716 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100717 break;
718
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100719 case Primitive::kPrimFloat:
720 case Primitive::kPrimDouble:
721 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100722 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100723 break;
724
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100725 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100726 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100727 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100728}
729
730void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
731 if (kIsDebugBuild) {
732 switch (ret->InputAt(0)->GetType()) {
733 case Primitive::kPrimBoolean:
734 case Primitive::kPrimByte:
735 case Primitive::kPrimChar:
736 case Primitive::kPrimShort:
737 case Primitive::kPrimInt:
738 case Primitive::kPrimNot:
739 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100740 DCHECK_EQ(ret->GetLocations()->InAt(0).As<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100741 break;
742
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100743 case Primitive::kPrimFloat:
744 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100745 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100746 XMM0);
747 break;
748
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100749 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100750 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100751 }
752 }
753 codegen_->GenerateFrameExit();
754 __ ret();
755}
756
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100757Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
758 switch (type) {
759 case Primitive::kPrimBoolean:
760 case Primitive::kPrimByte:
761 case Primitive::kPrimChar:
762 case Primitive::kPrimShort:
763 case Primitive::kPrimInt:
764 case Primitive::kPrimNot: {
765 uint32_t index = gp_index_++;
766 stack_index_++;
767 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100768 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100769 } else {
770 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
771 }
772 }
773
774 case Primitive::kPrimLong: {
775 uint32_t index = gp_index_;
776 stack_index_ += 2;
777 if (index < calling_convention.GetNumberOfRegisters()) {
778 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100779 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100780 } else {
781 gp_index_ += 2;
782 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
783 }
784 }
785
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100786 case Primitive::kPrimFloat: {
787 uint32_t index = fp_index_++;
788 stack_index_++;
789 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100790 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100791 } else {
792 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
793 }
794 }
795
796 case Primitive::kPrimDouble: {
797 uint32_t index = fp_index_++;
798 stack_index_ += 2;
799 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100800 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100801 } else {
802 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
803 }
804 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100805
806 case Primitive::kPrimVoid:
807 LOG(FATAL) << "Unexpected parameter type " << type;
808 break;
809 }
810 return Location();
811}
812
813void LocationsBuilderX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100814 HandleInvoke(invoke);
815}
816
817void InstructionCodeGeneratorX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100818 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100819 uint32_t heap_reference_size = sizeof(mirror::HeapReference<mirror::Object>);
820 size_t index_in_cache = mirror::Array::DataOffset(heap_reference_size).SizeValue() +
821 invoke->GetIndexInDexCache() * heap_reference_size;
822
823 // TODO: Implement all kinds of calls:
824 // 1) boot -> boot
825 // 2) app -> boot
826 // 3) app -> app
827 //
828 // Currently we implement the app -> app logic, which looks up in the resolve cache.
829
830 // temp = method;
831 LoadCurrentMethod(temp);
832 // temp = temp->dex_cache_resolved_methods_;
833 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
834 // temp = temp[index_in_cache]
835 __ movl(temp, Address(temp, index_in_cache));
836 // (temp + offset_of_quick_compiled_code)()
837 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
838
839 DCHECK(!codegen_->IsLeafMethod());
840 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
841}
842
843void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
844 HandleInvoke(invoke);
845}
846
847void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100848 LocationSummary* locations =
849 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100850 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100851
852 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100853 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100854 HInstruction* input = invoke->InputAt(i);
855 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
856 }
857
858 switch (invoke->GetType()) {
859 case Primitive::kPrimBoolean:
860 case Primitive::kPrimByte:
861 case Primitive::kPrimChar:
862 case Primitive::kPrimShort:
863 case Primitive::kPrimInt:
864 case Primitive::kPrimNot:
865 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100866 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100867 break;
868
869 case Primitive::kPrimVoid:
870 break;
871
872 case Primitive::kPrimDouble:
873 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100874 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100875 break;
876 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100877}
878
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100879void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100880 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100881 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
882 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
883 LocationSummary* locations = invoke->GetLocations();
884 Location receiver = locations->InAt(0);
885 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
886 // temp = object->GetClass();
887 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100888 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
889 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100890 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100891 __ movl(temp, Address(receiver.As<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100892 }
893 // temp = temp->GetMethodAt(method_offset);
894 __ movl(temp, Address(temp, method_offset));
895 // call temp->GetEntryPoint();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100896 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
897
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100898 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100899 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100900}
901
902void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100903 LocationSummary* locations =
904 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100905 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100906 case Primitive::kPrimInt: {
907 locations->SetInAt(0, Location::RequiresRegister());
908 locations->SetInAt(1, Location::Any());
909 locations->SetOut(Location::SameAsFirstInput());
910 break;
911 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100912
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100913 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000914 locations->SetInAt(0, Location::RequiresRegister());
915 locations->SetInAt(1, Location::RequiresRegister());
916 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100917 break;
918 }
919
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100920 case Primitive::kPrimDouble:
921 case Primitive::kPrimFloat: {
922 locations->SetInAt(0, Location::RequiresFpuRegister());
923 locations->SetInAt(1, Location::Any());
924 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100925 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100926 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100927
928 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100929 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100930 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100931}
932
933void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
934 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100935 Location first = locations->InAt(0);
936 Location second = locations->InAt(1);
937
938 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100939 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000940 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100941 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100942 __ addl(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100943 } else if (second.IsConstant()) {
944 HConstant* instruction = second.GetConstant();
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100945 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100946 __ addl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100947 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100948 __ addl(first.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100949 Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100950 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000951 break;
952 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100953
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100954 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100955 __ addq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100956 break;
957 }
958
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100959 case Primitive::kPrimFloat: {
960 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100961 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100962 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100963 __ addss(first.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100964 Address(CpuRegister(RSP), second.GetStackIndex()));
965 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100966 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100967 }
968
969 case Primitive::kPrimDouble: {
970 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100971 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100972 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100973 __ addsd(first.As<XmmRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100974 }
975 break;
976 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100977
978 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100979 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100980 }
981}
982
983void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100984 LocationSummary* locations =
985 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100986 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100987 case Primitive::kPrimInt: {
988 locations->SetInAt(0, Location::RequiresRegister());
989 locations->SetInAt(1, Location::Any());
990 locations->SetOut(Location::SameAsFirstInput());
991 break;
992 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100993 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000994 locations->SetInAt(0, Location::RequiresRegister());
995 locations->SetInAt(1, Location::RequiresRegister());
996 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100997 break;
998 }
999
1000 case Primitive::kPrimBoolean:
1001 case Primitive::kPrimByte:
1002 case Primitive::kPrimChar:
1003 case Primitive::kPrimShort:
1004 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
1005 break;
1006
1007 default:
1008 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
1009 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001010}
1011
1012void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1013 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001014 DCHECK_EQ(locations->InAt(0).As<CpuRegister>().AsRegister(),
1015 locations->Out().As<CpuRegister>().AsRegister());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001016 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001017 case Primitive::kPrimInt: {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001018 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001019 __ subl(locations->InAt(0).As<CpuRegister>(),
1020 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001021 } else if (locations->InAt(1).IsConstant()) {
1022 HConstant* instruction = locations->InAt(1).GetConstant();
1023 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001024 __ subl(locations->InAt(0).As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001025 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001026 __ subl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001027 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
1028 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001029 break;
1030 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001031 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001032 __ subq(locations->InAt(0).As<CpuRegister>(),
1033 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001034 break;
1035 }
1036
1037 case Primitive::kPrimBoolean:
1038 case Primitive::kPrimByte:
1039 case Primitive::kPrimChar:
1040 case Primitive::kPrimShort:
1041 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
1042 break;
1043
1044 default:
1045 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
1046 }
1047}
1048
1049void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001050 LocationSummary* locations =
1051 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001052 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001053 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1054 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1055 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001056}
1057
1058void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
1059 InvokeRuntimeCallingConvention calling_convention;
1060 LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
1061 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1062
1063 __ gs()->call(Address::Absolute(
1064 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
1065
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001066 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001067 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001068}
1069
1070void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001071 LocationSummary* locations =
1072 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001073 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1074 if (location.IsStackSlot()) {
1075 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1076 } else if (location.IsDoubleStackSlot()) {
1077 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1078 }
1079 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001080}
1081
1082void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
1083 // Nothing to do, the parameter is already at its location.
1084}
1085
1086void LocationsBuilderX86_64::VisitNot(HNot* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001087 LocationSummary* locations =
1088 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001089 locations->SetInAt(0, Location::RequiresRegister());
1090 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001091}
1092
1093void InstructionCodeGeneratorX86_64::VisitNot(HNot* instruction) {
1094 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001095 DCHECK_EQ(locations->InAt(0).As<CpuRegister>().AsRegister(),
1096 locations->Out().As<CpuRegister>().AsRegister());
1097 __ xorq(locations->Out().As<CpuRegister>(), Immediate(1));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001098}
1099
1100void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001101 LocationSummary* locations =
1102 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001103 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1104 locations->SetInAt(i, Location::Any());
1105 }
1106 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001107}
1108
1109void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
1110 LOG(FATAL) << "Unimplemented";
1111}
1112
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001113void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001114 LocationSummary* locations =
1115 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001116 Primitive::Type field_type = instruction->GetFieldType();
1117 bool is_object_type = field_type == Primitive::kPrimNot;
1118 bool dies_at_entry = !is_object_type;
1119 locations->SetInAt(0, Location::RequiresRegister(), dies_at_entry);
1120 locations->SetInAt(1, Location::RequiresRegister(), dies_at_entry);
1121 if (is_object_type) {
1122 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001123 locations->AddTemp(Location::RequiresRegister());
1124 locations->AddTemp(Location::RequiresRegister());
1125 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001126}
1127
1128void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1129 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001130 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
1131 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001132 size_t offset = instruction->GetFieldOffset().SizeValue();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001133 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001134
1135 switch (field_type) {
1136 case Primitive::kPrimBoolean:
1137 case Primitive::kPrimByte: {
1138 __ movb(Address(obj, offset), value);
1139 break;
1140 }
1141
1142 case Primitive::kPrimShort:
1143 case Primitive::kPrimChar: {
1144 __ movw(Address(obj, offset), value);
1145 break;
1146 }
1147
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001148 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001149 case Primitive::kPrimNot: {
1150 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001151 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001152 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
1153 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001154 codegen_->MarkGCCard(temp, card, obj, value);
1155 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001156 break;
1157 }
1158
1159 case Primitive::kPrimLong: {
1160 __ movq(Address(obj, offset), value);
1161 break;
1162 }
1163
1164 case Primitive::kPrimFloat:
1165 case Primitive::kPrimDouble:
1166 LOG(FATAL) << "Unimplemented register type " << field_type;
1167
1168 case Primitive::kPrimVoid:
1169 LOG(FATAL) << "Unreachable type " << field_type;
1170 }
1171}
1172
1173void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001174 LocationSummary* locations =
1175 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001176 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001177 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001178}
1179
1180void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1181 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001182 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
1183 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001184 size_t offset = instruction->GetFieldOffset().SizeValue();
1185
1186 switch (instruction->GetType()) {
1187 case Primitive::kPrimBoolean: {
1188 __ movzxb(out, Address(obj, offset));
1189 break;
1190 }
1191
1192 case Primitive::kPrimByte: {
1193 __ movsxb(out, Address(obj, offset));
1194 break;
1195 }
1196
1197 case Primitive::kPrimShort: {
1198 __ movsxw(out, Address(obj, offset));
1199 break;
1200 }
1201
1202 case Primitive::kPrimChar: {
1203 __ movzxw(out, Address(obj, offset));
1204 break;
1205 }
1206
1207 case Primitive::kPrimInt:
1208 case Primitive::kPrimNot: {
1209 __ movl(out, Address(obj, offset));
1210 break;
1211 }
1212
1213 case Primitive::kPrimLong: {
1214 __ movq(out, Address(obj, offset));
1215 break;
1216 }
1217
1218 case Primitive::kPrimFloat:
1219 case Primitive::kPrimDouble:
1220 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1221
1222 case Primitive::kPrimVoid:
1223 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1224 }
1225}
1226
1227void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001228 LocationSummary* locations =
1229 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001230 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001231 if (instruction->HasUses()) {
1232 locations->SetOut(Location::SameAsFirstInput());
1233 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001234}
1235
1236void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001237 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001238 codegen_->AddSlowPath(slow_path);
1239
1240 LocationSummary* locations = instruction->GetLocations();
1241 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001242
1243 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001244 __ cmpl(obj.As<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001245 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001246 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001247 } else {
1248 DCHECK(obj.IsConstant()) << obj;
1249 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1250 __ jmp(slow_path->GetEntryLabel());
1251 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001252 }
1253 __ j(kEqual, slow_path->GetEntryLabel());
1254}
1255
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001256void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001257 LocationSummary* locations =
1258 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001259 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
1260 locations->SetInAt(
1261 1, Location::RegisterOrConstant(instruction->InputAt(1)), Location::kDiesAtEntry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001262 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001263}
1264
1265void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
1266 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001267 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001268 Location index = locations->InAt(1);
1269
1270 switch (instruction->GetType()) {
1271 case Primitive::kPrimBoolean: {
1272 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001273 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001274 if (index.IsConstant()) {
1275 __ movzxb(out, Address(obj,
1276 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1277 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001278 __ movzxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001279 }
1280 break;
1281 }
1282
1283 case Primitive::kPrimByte: {
1284 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001285 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001286 if (index.IsConstant()) {
1287 __ movsxb(out, Address(obj,
1288 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1289 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001290 __ movsxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001291 }
1292 break;
1293 }
1294
1295 case Primitive::kPrimShort: {
1296 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001297 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001298 if (index.IsConstant()) {
1299 __ movsxw(out, Address(obj,
1300 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1301 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001302 __ movsxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001303 }
1304 break;
1305 }
1306
1307 case Primitive::kPrimChar: {
1308 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001309 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001310 if (index.IsConstant()) {
1311 __ movzxw(out, Address(obj,
1312 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1313 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001314 __ movzxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001315 }
1316 break;
1317 }
1318
1319 case Primitive::kPrimInt:
1320 case Primitive::kPrimNot: {
1321 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1322 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001323 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001324 if (index.IsConstant()) {
1325 __ movl(out, Address(obj,
1326 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1327 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001328 __ movl(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001329 }
1330 break;
1331 }
1332
1333 case Primitive::kPrimLong: {
1334 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001335 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001336 if (index.IsConstant()) {
1337 __ movq(out, Address(obj,
1338 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
1339 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001340 __ movq(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001341 }
1342 break;
1343 }
1344
1345 case Primitive::kPrimFloat:
1346 case Primitive::kPrimDouble:
1347 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1348
1349 case Primitive::kPrimVoid:
1350 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1351 }
1352}
1353
1354void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001355 Primitive::Type value_type = instruction->GetComponentType();
1356 bool is_object = value_type == Primitive::kPrimNot;
1357 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1358 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
1359 if (is_object) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001360 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001361 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1362 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1363 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001364 } else {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001365 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
1366 locations->SetInAt(
1367 1, Location::RegisterOrConstant(instruction->InputAt(1)), Location::kDiesAtEntry);
1368 locations->SetInAt(2, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001369 if (value_type == Primitive::kPrimLong) {
1370 locations->SetInAt(2, Location::RequiresRegister(), Location::kDiesAtEntry);
1371 } else {
1372 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)), Location::kDiesAtEntry);
1373 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001374 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001375}
1376
1377void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
1378 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001379 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001380 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001381 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001382 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001383
1384 switch (value_type) {
1385 case Primitive::kPrimBoolean:
1386 case Primitive::kPrimByte: {
1387 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001388 if (index.IsConstant()) {
1389 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001390 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001391 __ movb(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001392 } else {
1393 __ movb(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1394 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001395 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001396 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001397 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
1398 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001399 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001400 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001401 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1402 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001403 }
1404 break;
1405 }
1406
1407 case Primitive::kPrimShort:
1408 case Primitive::kPrimChar: {
1409 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001410 if (index.IsConstant()) {
1411 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001412 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001413 __ movw(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001414 } else {
1415 __ movw(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1416 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001417 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001418 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001419 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
1420 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001421 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001422 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001423 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1424 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001425 }
1426 break;
1427 }
1428
1429 case Primitive::kPrimInt: {
1430 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001431 if (index.IsConstant()) {
1432 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001433 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001434 __ movl(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001435 } else {
1436 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1437 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001438 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001439 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001440 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
1441 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001442 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001443 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001444 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1445 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001446 }
1447 break;
1448 }
1449
1450 case Primitive::kPrimNot: {
1451 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject), true));
1452 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001453 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001454 break;
1455 }
1456
1457 case Primitive::kPrimLong: {
1458 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001459 if (index.IsConstant()) {
1460 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001461 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001462 __ movq(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001463 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001464 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001465 __ movq(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
1466 value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001467 }
1468 break;
1469 }
1470
1471 case Primitive::kPrimFloat:
1472 case Primitive::kPrimDouble:
1473 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1474
1475 case Primitive::kPrimVoid:
1476 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1477 }
1478}
1479
1480void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001481 LocationSummary* locations =
1482 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001483 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001484 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001485}
1486
1487void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
1488 LocationSummary* locations = instruction->GetLocations();
1489 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001490 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
1491 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001492 __ movl(out, Address(obj, offset));
1493}
1494
1495void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001496 LocationSummary* locations =
1497 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001498 locations->SetInAt(0, Location::RequiresRegister());
1499 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001500 if (instruction->HasUses()) {
1501 locations->SetOut(Location::SameAsFirstInput());
1502 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001503}
1504
1505void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
1506 LocationSummary* locations = instruction->GetLocations();
1507 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01001508 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001509 codegen_->AddSlowPath(slow_path);
1510
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001511 CpuRegister index = locations->InAt(0).As<CpuRegister>();
1512 CpuRegister length = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001513
1514 __ cmpl(index, length);
1515 __ j(kAboveEqual, slow_path->GetEntryLabel());
1516}
1517
1518void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
1519 CpuRegister card,
1520 CpuRegister object,
1521 CpuRegister value) {
1522 Label is_null;
1523 __ testl(value, value);
1524 __ j(kEqual, &is_null);
1525 __ gs()->movq(card, Address::Absolute(
1526 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
1527 __ movq(temp, object);
1528 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
1529 __ movb(Address(temp, card, TIMES_1, 0), card);
1530 __ Bind(&is_null);
1531}
1532
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001533void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
1534 temp->SetLocations(nullptr);
1535}
1536
1537void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
1538 // Nothing to do, this is driven by the code generator.
1539}
1540
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001541void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
1542 LOG(FATAL) << "Unimplemented";
1543}
1544
1545void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001546 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
1547}
1548
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001549void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
1550 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
1551}
1552
1553void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001554 HBasicBlock* block = instruction->GetBlock();
1555 if (block->GetLoopInformation() != nullptr) {
1556 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
1557 // The back edge will generate the suspend check.
1558 return;
1559 }
1560 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
1561 // The goto will generate the suspend check.
1562 return;
1563 }
1564 GenerateSuspendCheck(instruction, nullptr);
1565}
1566
1567void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
1568 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001569 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001570 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001571 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001572 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001573 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001574 if (successor == nullptr) {
1575 __ j(kNotEqual, slow_path->GetEntryLabel());
1576 __ Bind(slow_path->GetReturnLabel());
1577 } else {
1578 __ j(kEqual, codegen_->GetLabelOf(successor));
1579 __ jmp(slow_path->GetEntryLabel());
1580 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001581}
1582
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001583X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
1584 return codegen_->GetAssembler();
1585}
1586
1587void ParallelMoveResolverX86_64::EmitMove(size_t index) {
1588 MoveOperands* move = moves_.Get(index);
1589 Location source = move->GetSource();
1590 Location destination = move->GetDestination();
1591
1592 if (source.IsRegister()) {
1593 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001594 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001595 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001596 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001597 source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001598 } else {
1599 DCHECK(destination.IsDoubleStackSlot());
1600 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001601 source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001602 }
1603 } else if (source.IsStackSlot()) {
1604 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001605 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001606 Address(CpuRegister(RSP), source.GetStackIndex()));
1607 } else {
1608 DCHECK(destination.IsStackSlot());
1609 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1610 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
1611 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001612 } else if (source.IsDoubleStackSlot()) {
1613 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001614 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001615 Address(CpuRegister(RSP), source.GetStackIndex()));
1616 } else {
1617 DCHECK(destination.IsDoubleStackSlot());
1618 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1619 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
1620 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001621 } else if (source.IsConstant()) {
1622 HConstant* constant = source.GetConstant();
1623 if (constant->IsIntConstant()) {
1624 Immediate imm(constant->AsIntConstant()->GetValue());
1625 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001626 __ movl(destination.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001627 } else {
1628 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
1629 }
1630 } else if (constant->IsLongConstant()) {
1631 int64_t value = constant->AsLongConstant()->GetValue();
1632 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001633 __ movq(destination.As<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001634 } else {
1635 __ movq(CpuRegister(TMP), Immediate(value));
1636 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
1637 }
1638 } else {
1639 LOG(FATAL) << "Unimplemented constant type";
1640 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001641 } else {
1642 LOG(FATAL) << "Unimplemented";
1643 }
1644}
1645
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001646void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001647 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001648 __ movl(Address(CpuRegister(RSP), mem), reg);
1649 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001650}
1651
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001652void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001653 ScratchRegisterScope ensure_scratch(
1654 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
1655
1656 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
1657 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
1658 __ movl(CpuRegister(ensure_scratch.GetRegister()),
1659 Address(CpuRegister(RSP), mem2 + stack_offset));
1660 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
1661 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
1662 CpuRegister(ensure_scratch.GetRegister()));
1663}
1664
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001665void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
1666 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
1667 __ movq(Address(CpuRegister(RSP), mem), reg);
1668 __ movq(reg, CpuRegister(TMP));
1669}
1670
1671void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
1672 ScratchRegisterScope ensure_scratch(
1673 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
1674
1675 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
1676 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
1677 __ movq(CpuRegister(ensure_scratch.GetRegister()),
1678 Address(CpuRegister(RSP), mem2 + stack_offset));
1679 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
1680 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
1681 CpuRegister(ensure_scratch.GetRegister()));
1682}
1683
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001684void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
1685 MoveOperands* move = moves_.Get(index);
1686 Location source = move->GetSource();
1687 Location destination = move->GetDestination();
1688
1689 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001690 __ xchgq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001691 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001692 Exchange32(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001693 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001694 Exchange32(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001695 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001696 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
1697 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001698 Exchange64(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001699 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001700 Exchange64(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001701 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
1702 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001703 } else {
1704 LOG(FATAL) << "Unimplemented";
1705 }
1706}
1707
1708
1709void ParallelMoveResolverX86_64::SpillScratch(int reg) {
1710 __ pushq(CpuRegister(reg));
1711}
1712
1713
1714void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
1715 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001716}
1717
1718} // namespace x86_64
1719} // namespace art