blob: 393eb1a2d490cef24a95620c1048bb8d3769e807 [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 Geoffray01ef3452014-10-01 11:32:17 +0100485 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100486 // 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.
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100488 if (!cond->IsCondition() || !cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr)) {
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100489 // Materialized condition, compare against 0.
490 Location lhs = if_instr->GetLocations()->InAt(0);
491 if (lhs.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100492 __ cmpl(lhs.As<CpuRegister>(), Immediate(0));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100493 } else {
494 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
495 }
Dave Allison20dfc792014-06-16 20:44:29 -0700496 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100497 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700498 } else {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100499 Location lhs = cond->GetLocations()->InAt(0);
500 Location rhs = cond->GetLocations()->InAt(1);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100501 if (rhs.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100502 __ cmpl(lhs.As<CpuRegister>(), rhs.As<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100503 } else if (rhs.IsConstant()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100504 __ cmpl(lhs.As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100505 Immediate(rhs.GetConstant()->AsIntConstant()->GetValue()));
506 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100507 __ cmpl(lhs.As<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100508 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100509 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
Dave Allison20dfc792014-06-16 20:44:29 -0700510 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
511 }
512 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfFalseSuccessor())) {
513 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100514 }
515}
516
517void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
518 local->SetLocations(nullptr);
519}
520
521void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
522 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
523}
524
525void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
526 local->SetLocations(nullptr);
527}
528
529void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
530 // Nothing to do, this is driven by the code generator.
531}
532
533void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100534 LocationSummary* locations =
535 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100536 switch (store->InputAt(1)->GetType()) {
537 case Primitive::kPrimBoolean:
538 case Primitive::kPrimByte:
539 case Primitive::kPrimChar:
540 case Primitive::kPrimShort:
541 case Primitive::kPrimInt:
542 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100543 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100544 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
545 break;
546
547 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100548 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100549 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
550 break;
551
552 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100553 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100554 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100555}
556
557void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
558}
559
Dave Allison20dfc792014-06-16 20:44:29 -0700560void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100561 LocationSummary* locations =
562 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +0100563 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
564 locations->SetInAt(1, Location::Any(), Location::kDiesAtEntry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100565 if (comp->NeedsMaterialization()) {
566 locations->SetOut(Location::RequiresRegister());
567 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100568}
569
Dave Allison20dfc792014-06-16 20:44:29 -0700570void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
571 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100572 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100573 CpuRegister reg = locations->Out().As<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100574 // Clear register: setcc only sets the low byte.
575 __ xorq(reg, reg);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100576 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100577 __ cmpq(locations->InAt(0).As<CpuRegister>(),
578 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100579 } else if (locations->InAt(1).IsConstant()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100580 __ cmpq(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100581 Immediate(locations->InAt(1).GetConstant()->AsIntConstant()->GetValue()));
582 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100583 __ cmpq(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100584 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
585 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100586 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700587 }
588}
589
590void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
591 VisitCondition(comp);
592}
593
594void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
595 VisitCondition(comp);
596}
597
598void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
599 VisitCondition(comp);
600}
601
602void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
603 VisitCondition(comp);
604}
605
606void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
607 VisitCondition(comp);
608}
609
610void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
611 VisitCondition(comp);
612}
613
614void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
615 VisitCondition(comp);
616}
617
618void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
619 VisitCondition(comp);
620}
621
622void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
623 VisitCondition(comp);
624}
625
626void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
627 VisitCondition(comp);
628}
629
630void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
631 VisitCondition(comp);
632}
633
634void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
635 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100636}
637
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100638void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100639 LocationSummary* locations =
640 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +0100641 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
642 locations->SetInAt(1, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100643 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100644}
645
646void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
647 Label greater, done;
648 LocationSummary* locations = compare->GetLocations();
649 switch (compare->InputAt(0)->GetType()) {
650 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100651 __ cmpq(locations->InAt(0).As<CpuRegister>(),
652 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100653 break;
654 default:
655 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
656 }
657
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100658 CpuRegister output = locations->Out().As<CpuRegister>();
659 __ movl(output, Immediate(0));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100660 __ j(kEqual, &done);
661 __ j(kGreater, &greater);
662
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100663 __ movl(output, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100664 __ jmp(&done);
665
666 __ Bind(&greater);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100667 __ movl(output, Immediate(1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100668
669 __ Bind(&done);
670}
671
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100672void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100673 LocationSummary* locations =
674 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100675 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100676}
677
678void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100679}
680
681void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100682 LocationSummary* locations =
683 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100684 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100685}
686
687void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100688}
689
690void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
691 ret->SetLocations(nullptr);
692}
693
694void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
695 codegen_->GenerateFrameExit();
696 __ ret();
697}
698
699void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100700 LocationSummary* locations =
701 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100702 switch (ret->InputAt(0)->GetType()) {
703 case Primitive::kPrimBoolean:
704 case Primitive::kPrimByte:
705 case Primitive::kPrimChar:
706 case Primitive::kPrimShort:
707 case Primitive::kPrimInt:
708 case Primitive::kPrimNot:
709 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100710 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100711 break;
712
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100713 case Primitive::kPrimFloat:
714 case Primitive::kPrimDouble:
715 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100716 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100717 break;
718
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100719 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100720 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100721 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100722}
723
724void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
725 if (kIsDebugBuild) {
726 switch (ret->InputAt(0)->GetType()) {
727 case Primitive::kPrimBoolean:
728 case Primitive::kPrimByte:
729 case Primitive::kPrimChar:
730 case Primitive::kPrimShort:
731 case Primitive::kPrimInt:
732 case Primitive::kPrimNot:
733 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100734 DCHECK_EQ(ret->GetLocations()->InAt(0).As<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100735 break;
736
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100737 case Primitive::kPrimFloat:
738 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100739 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100740 XMM0);
741 break;
742
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100743 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100744 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100745 }
746 }
747 codegen_->GenerateFrameExit();
748 __ ret();
749}
750
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100751Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
752 switch (type) {
753 case Primitive::kPrimBoolean:
754 case Primitive::kPrimByte:
755 case Primitive::kPrimChar:
756 case Primitive::kPrimShort:
757 case Primitive::kPrimInt:
758 case Primitive::kPrimNot: {
759 uint32_t index = gp_index_++;
760 stack_index_++;
761 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100762 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100763 } else {
764 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
765 }
766 }
767
768 case Primitive::kPrimLong: {
769 uint32_t index = gp_index_;
770 stack_index_ += 2;
771 if (index < calling_convention.GetNumberOfRegisters()) {
772 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100773 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100774 } else {
775 gp_index_ += 2;
776 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
777 }
778 }
779
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100780 case Primitive::kPrimFloat: {
781 uint32_t index = fp_index_++;
782 stack_index_++;
783 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100784 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100785 } else {
786 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
787 }
788 }
789
790 case Primitive::kPrimDouble: {
791 uint32_t index = fp_index_++;
792 stack_index_ += 2;
793 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100794 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100795 } else {
796 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
797 }
798 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100799
800 case Primitive::kPrimVoid:
801 LOG(FATAL) << "Unexpected parameter type " << type;
802 break;
803 }
804 return Location();
805}
806
807void LocationsBuilderX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100808 HandleInvoke(invoke);
809}
810
811void InstructionCodeGeneratorX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100812 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100813 uint32_t heap_reference_size = sizeof(mirror::HeapReference<mirror::Object>);
814 size_t index_in_cache = mirror::Array::DataOffset(heap_reference_size).SizeValue() +
815 invoke->GetIndexInDexCache() * heap_reference_size;
816
817 // TODO: Implement all kinds of calls:
818 // 1) boot -> boot
819 // 2) app -> boot
820 // 3) app -> app
821 //
822 // Currently we implement the app -> app logic, which looks up in the resolve cache.
823
824 // temp = method;
825 LoadCurrentMethod(temp);
826 // temp = temp->dex_cache_resolved_methods_;
827 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
828 // temp = temp[index_in_cache]
829 __ movl(temp, Address(temp, index_in_cache));
830 // (temp + offset_of_quick_compiled_code)()
831 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
832
833 DCHECK(!codegen_->IsLeafMethod());
834 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
835}
836
837void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
838 HandleInvoke(invoke);
839}
840
841void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100842 LocationSummary* locations =
843 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100844 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100845
846 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100847 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100848 HInstruction* input = invoke->InputAt(i);
849 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
850 }
851
852 switch (invoke->GetType()) {
853 case Primitive::kPrimBoolean:
854 case Primitive::kPrimByte:
855 case Primitive::kPrimChar:
856 case Primitive::kPrimShort:
857 case Primitive::kPrimInt:
858 case Primitive::kPrimNot:
859 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100860 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100861 break;
862
863 case Primitive::kPrimVoid:
864 break;
865
866 case Primitive::kPrimDouble:
867 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100868 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100869 break;
870 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100871}
872
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100873void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100874 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100875 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
876 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
877 LocationSummary* locations = invoke->GetLocations();
878 Location receiver = locations->InAt(0);
879 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
880 // temp = object->GetClass();
881 if (receiver.IsStackSlot()) {
882 __ movq(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
883 __ movq(temp, Address(temp, class_offset));
884 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100885 __ movq(temp, Address(receiver.As<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100886 }
887 // temp = temp->GetMethodAt(method_offset);
888 __ movl(temp, Address(temp, method_offset));
889 // call temp->GetEntryPoint();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100890 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
891
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100892 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100893 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100894}
895
896void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100897 LocationSummary* locations =
898 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100899 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100900 case Primitive::kPrimInt: {
901 locations->SetInAt(0, Location::RequiresRegister());
902 locations->SetInAt(1, Location::Any());
903 locations->SetOut(Location::SameAsFirstInput());
904 break;
905 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100906
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100907 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000908 locations->SetInAt(0, Location::RequiresRegister());
909 locations->SetInAt(1, Location::RequiresRegister());
910 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100911 break;
912 }
913
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100914 case Primitive::kPrimDouble:
915 case Primitive::kPrimFloat: {
916 locations->SetInAt(0, Location::RequiresFpuRegister());
917 locations->SetInAt(1, Location::Any());
918 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100919 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100920 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100921
922 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100923 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100924 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100925}
926
927void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
928 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100929 Location first = locations->InAt(0);
930 Location second = locations->InAt(1);
931
932 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100933 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000934 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100935 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100936 __ addl(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100937 } else if (second.IsConstant()) {
938 HConstant* instruction = second.GetConstant();
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100939 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100940 __ addl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100941 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100942 __ addl(first.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100943 Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100944 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000945 break;
946 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100947
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100948 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100949 __ addq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100950 break;
951 }
952
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100953 case Primitive::kPrimFloat: {
954 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100955 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100956 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100957 __ addss(first.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100958 Address(CpuRegister(RSP), second.GetStackIndex()));
959 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100960 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100961 }
962
963 case Primitive::kPrimDouble: {
964 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100965 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100966 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100967 __ addsd(first.As<XmmRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100968 }
969 break;
970 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100971
972 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100973 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100974 }
975}
976
977void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100978 LocationSummary* locations =
979 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100980 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100981 case Primitive::kPrimInt: {
982 locations->SetInAt(0, Location::RequiresRegister());
983 locations->SetInAt(1, Location::Any());
984 locations->SetOut(Location::SameAsFirstInput());
985 break;
986 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100987 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000988 locations->SetInAt(0, Location::RequiresRegister());
989 locations->SetInAt(1, Location::RequiresRegister());
990 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100991 break;
992 }
993
994 case Primitive::kPrimBoolean:
995 case Primitive::kPrimByte:
996 case Primitive::kPrimChar:
997 case Primitive::kPrimShort:
998 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
999 break;
1000
1001 default:
1002 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
1003 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001004}
1005
1006void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1007 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001008 DCHECK_EQ(locations->InAt(0).As<CpuRegister>().AsRegister(),
1009 locations->Out().As<CpuRegister>().AsRegister());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001010 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001011 case Primitive::kPrimInt: {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001012 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001013 __ subl(locations->InAt(0).As<CpuRegister>(),
1014 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001015 } else if (locations->InAt(1).IsConstant()) {
1016 HConstant* instruction = locations->InAt(1).GetConstant();
1017 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001018 __ subl(locations->InAt(0).As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001019 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001020 __ subl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001021 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
1022 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001023 break;
1024 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001025 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001026 __ subq(locations->InAt(0).As<CpuRegister>(),
1027 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001028 break;
1029 }
1030
1031 case Primitive::kPrimBoolean:
1032 case Primitive::kPrimByte:
1033 case Primitive::kPrimChar:
1034 case Primitive::kPrimShort:
1035 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
1036 break;
1037
1038 default:
1039 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
1040 }
1041}
1042
1043void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001044 LocationSummary* locations =
1045 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001046 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001047 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1048 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1049 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001050}
1051
1052void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
1053 InvokeRuntimeCallingConvention calling_convention;
1054 LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
1055 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1056
1057 __ gs()->call(Address::Absolute(
1058 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
1059
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001060 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001061 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001062}
1063
1064void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001065 LocationSummary* locations =
1066 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001067 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1068 if (location.IsStackSlot()) {
1069 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1070 } else if (location.IsDoubleStackSlot()) {
1071 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1072 }
1073 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001074}
1075
1076void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
1077 // Nothing to do, the parameter is already at its location.
1078}
1079
1080void LocationsBuilderX86_64::VisitNot(HNot* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001081 LocationSummary* locations =
1082 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001083 locations->SetInAt(0, Location::RequiresRegister());
1084 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001085}
1086
1087void InstructionCodeGeneratorX86_64::VisitNot(HNot* instruction) {
1088 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001089 DCHECK_EQ(locations->InAt(0).As<CpuRegister>().AsRegister(),
1090 locations->Out().As<CpuRegister>().AsRegister());
1091 __ xorq(locations->Out().As<CpuRegister>(), Immediate(1));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001092}
1093
1094void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001095 LocationSummary* locations =
1096 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001097 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1098 locations->SetInAt(i, Location::Any());
1099 }
1100 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001101}
1102
1103void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
1104 LOG(FATAL) << "Unimplemented";
1105}
1106
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001107void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001108 LocationSummary* locations =
1109 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001110 Primitive::Type field_type = instruction->GetFieldType();
1111 bool is_object_type = field_type == Primitive::kPrimNot;
1112 bool dies_at_entry = !is_object_type;
1113 locations->SetInAt(0, Location::RequiresRegister(), dies_at_entry);
1114 locations->SetInAt(1, Location::RequiresRegister(), dies_at_entry);
1115 if (is_object_type) {
1116 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001117 locations->AddTemp(Location::RequiresRegister());
1118 locations->AddTemp(Location::RequiresRegister());
1119 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001120}
1121
1122void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1123 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001124 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
1125 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001126 size_t offset = instruction->GetFieldOffset().SizeValue();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001127 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001128
1129 switch (field_type) {
1130 case Primitive::kPrimBoolean:
1131 case Primitive::kPrimByte: {
1132 __ movb(Address(obj, offset), value);
1133 break;
1134 }
1135
1136 case Primitive::kPrimShort:
1137 case Primitive::kPrimChar: {
1138 __ movw(Address(obj, offset), value);
1139 break;
1140 }
1141
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001142 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001143 case Primitive::kPrimNot: {
1144 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001145 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001146 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
1147 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001148 codegen_->MarkGCCard(temp, card, obj, value);
1149 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001150 break;
1151 }
1152
1153 case Primitive::kPrimLong: {
1154 __ movq(Address(obj, offset), value);
1155 break;
1156 }
1157
1158 case Primitive::kPrimFloat:
1159 case Primitive::kPrimDouble:
1160 LOG(FATAL) << "Unimplemented register type " << field_type;
1161
1162 case Primitive::kPrimVoid:
1163 LOG(FATAL) << "Unreachable type " << field_type;
1164 }
1165}
1166
1167void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001168 LocationSummary* locations =
1169 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001170 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001171 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001172}
1173
1174void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1175 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001176 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
1177 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001178 size_t offset = instruction->GetFieldOffset().SizeValue();
1179
1180 switch (instruction->GetType()) {
1181 case Primitive::kPrimBoolean: {
1182 __ movzxb(out, Address(obj, offset));
1183 break;
1184 }
1185
1186 case Primitive::kPrimByte: {
1187 __ movsxb(out, Address(obj, offset));
1188 break;
1189 }
1190
1191 case Primitive::kPrimShort: {
1192 __ movsxw(out, Address(obj, offset));
1193 break;
1194 }
1195
1196 case Primitive::kPrimChar: {
1197 __ movzxw(out, Address(obj, offset));
1198 break;
1199 }
1200
1201 case Primitive::kPrimInt:
1202 case Primitive::kPrimNot: {
1203 __ movl(out, Address(obj, offset));
1204 break;
1205 }
1206
1207 case Primitive::kPrimLong: {
1208 __ movq(out, Address(obj, offset));
1209 break;
1210 }
1211
1212 case Primitive::kPrimFloat:
1213 case Primitive::kPrimDouble:
1214 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1215
1216 case Primitive::kPrimVoid:
1217 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1218 }
1219}
1220
1221void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001222 LocationSummary* locations =
1223 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001224 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001225 if (instruction->HasUses()) {
1226 locations->SetOut(Location::SameAsFirstInput());
1227 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001228}
1229
1230void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001231 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001232 codegen_->AddSlowPath(slow_path);
1233
1234 LocationSummary* locations = instruction->GetLocations();
1235 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001236
1237 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001238 __ cmpl(obj.As<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001239 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001240 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001241 } else {
1242 DCHECK(obj.IsConstant()) << obj;
1243 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1244 __ jmp(slow_path->GetEntryLabel());
1245 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001246 }
1247 __ j(kEqual, slow_path->GetEntryLabel());
1248}
1249
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001250void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001251 LocationSummary* locations =
1252 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001253 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
1254 locations->SetInAt(
1255 1, Location::RegisterOrConstant(instruction->InputAt(1)), Location::kDiesAtEntry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001256 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001257}
1258
1259void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
1260 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001261 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001262 Location index = locations->InAt(1);
1263
1264 switch (instruction->GetType()) {
1265 case Primitive::kPrimBoolean: {
1266 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001267 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001268 if (index.IsConstant()) {
1269 __ movzxb(out, Address(obj,
1270 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1271 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001272 __ movzxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001273 }
1274 break;
1275 }
1276
1277 case Primitive::kPrimByte: {
1278 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001279 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001280 if (index.IsConstant()) {
1281 __ movsxb(out, Address(obj,
1282 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1283 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001284 __ movsxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001285 }
1286 break;
1287 }
1288
1289 case Primitive::kPrimShort: {
1290 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001291 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001292 if (index.IsConstant()) {
1293 __ movsxw(out, Address(obj,
1294 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1295 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001296 __ movsxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001297 }
1298 break;
1299 }
1300
1301 case Primitive::kPrimChar: {
1302 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001303 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001304 if (index.IsConstant()) {
1305 __ movzxw(out, Address(obj,
1306 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1307 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001308 __ movzxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001309 }
1310 break;
1311 }
1312
1313 case Primitive::kPrimInt:
1314 case Primitive::kPrimNot: {
1315 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1316 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001317 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001318 if (index.IsConstant()) {
1319 __ movl(out, Address(obj,
1320 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1321 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001322 __ movl(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001323 }
1324 break;
1325 }
1326
1327 case Primitive::kPrimLong: {
1328 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001329 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001330 if (index.IsConstant()) {
1331 __ movq(out, Address(obj,
1332 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
1333 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001334 __ movq(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001335 }
1336 break;
1337 }
1338
1339 case Primitive::kPrimFloat:
1340 case Primitive::kPrimDouble:
1341 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1342
1343 case Primitive::kPrimVoid:
1344 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1345 }
1346}
1347
1348void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001349 Primitive::Type value_type = instruction->GetComponentType();
1350 bool is_object = value_type == Primitive::kPrimNot;
1351 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1352 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
1353 if (is_object) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001354 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001355 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1356 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1357 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001358 } else {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001359 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
1360 locations->SetInAt(
1361 1, Location::RegisterOrConstant(instruction->InputAt(1)), Location::kDiesAtEntry);
1362 locations->SetInAt(2, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001363 if (value_type == Primitive::kPrimLong) {
1364 locations->SetInAt(2, Location::RequiresRegister(), Location::kDiesAtEntry);
1365 } else {
1366 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)), Location::kDiesAtEntry);
1367 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001368 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001369}
1370
1371void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
1372 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001373 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001374 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001375 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001376 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001377
1378 switch (value_type) {
1379 case Primitive::kPrimBoolean:
1380 case Primitive::kPrimByte: {
1381 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001382 if (index.IsConstant()) {
1383 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001384 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001385 __ movb(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001386 } else {
1387 __ movb(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1388 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001389 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001390 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001391 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
1392 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001393 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001394 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001395 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1396 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001397 }
1398 break;
1399 }
1400
1401 case Primitive::kPrimShort:
1402 case Primitive::kPrimChar: {
1403 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001404 if (index.IsConstant()) {
1405 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001406 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001407 __ movw(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001408 } else {
1409 __ movw(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1410 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001411 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001412 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001413 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
1414 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001415 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001416 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001417 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1418 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001419 }
1420 break;
1421 }
1422
1423 case Primitive::kPrimInt: {
1424 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001425 if (index.IsConstant()) {
1426 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001427 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001428 __ movl(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001429 } else {
1430 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1431 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001432 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001433 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001434 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
1435 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001436 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001437 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001438 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1439 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001440 }
1441 break;
1442 }
1443
1444 case Primitive::kPrimNot: {
1445 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject), true));
1446 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001447 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001448 break;
1449 }
1450
1451 case Primitive::kPrimLong: {
1452 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001453 if (index.IsConstant()) {
1454 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001455 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001456 __ movq(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001457 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001458 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001459 __ movq(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
1460 value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001461 }
1462 break;
1463 }
1464
1465 case Primitive::kPrimFloat:
1466 case Primitive::kPrimDouble:
1467 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1468
1469 case Primitive::kPrimVoid:
1470 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1471 }
1472}
1473
1474void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001475 LocationSummary* locations =
1476 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001477 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001478 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001479}
1480
1481void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
1482 LocationSummary* locations = instruction->GetLocations();
1483 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001484 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
1485 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001486 __ movl(out, Address(obj, offset));
1487}
1488
1489void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001490 LocationSummary* locations =
1491 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001492 locations->SetInAt(0, Location::RequiresRegister());
1493 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001494 if (instruction->HasUses()) {
1495 locations->SetOut(Location::SameAsFirstInput());
1496 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001497}
1498
1499void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
1500 LocationSummary* locations = instruction->GetLocations();
1501 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01001502 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001503 codegen_->AddSlowPath(slow_path);
1504
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001505 CpuRegister index = locations->InAt(0).As<CpuRegister>();
1506 CpuRegister length = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001507
1508 __ cmpl(index, length);
1509 __ j(kAboveEqual, slow_path->GetEntryLabel());
1510}
1511
1512void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
1513 CpuRegister card,
1514 CpuRegister object,
1515 CpuRegister value) {
1516 Label is_null;
1517 __ testl(value, value);
1518 __ j(kEqual, &is_null);
1519 __ gs()->movq(card, Address::Absolute(
1520 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
1521 __ movq(temp, object);
1522 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
1523 __ movb(Address(temp, card, TIMES_1, 0), card);
1524 __ Bind(&is_null);
1525}
1526
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001527void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
1528 temp->SetLocations(nullptr);
1529}
1530
1531void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
1532 // Nothing to do, this is driven by the code generator.
1533}
1534
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001535void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
1536 LOG(FATAL) << "Unimplemented";
1537}
1538
1539void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001540 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
1541}
1542
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001543void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
1544 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
1545}
1546
1547void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001548 HBasicBlock* block = instruction->GetBlock();
1549 if (block->GetLoopInformation() != nullptr) {
1550 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
1551 // The back edge will generate the suspend check.
1552 return;
1553 }
1554 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
1555 // The goto will generate the suspend check.
1556 return;
1557 }
1558 GenerateSuspendCheck(instruction, nullptr);
1559}
1560
1561void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
1562 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001563 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001564 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001565 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001566 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001567 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001568 if (successor == nullptr) {
1569 __ j(kNotEqual, slow_path->GetEntryLabel());
1570 __ Bind(slow_path->GetReturnLabel());
1571 } else {
1572 __ j(kEqual, codegen_->GetLabelOf(successor));
1573 __ jmp(slow_path->GetEntryLabel());
1574 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001575}
1576
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001577X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
1578 return codegen_->GetAssembler();
1579}
1580
1581void ParallelMoveResolverX86_64::EmitMove(size_t index) {
1582 MoveOperands* move = moves_.Get(index);
1583 Location source = move->GetSource();
1584 Location destination = move->GetDestination();
1585
1586 if (source.IsRegister()) {
1587 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001588 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001589 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001590 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001591 source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001592 } else {
1593 DCHECK(destination.IsDoubleStackSlot());
1594 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001595 source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001596 }
1597 } else if (source.IsStackSlot()) {
1598 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001599 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001600 Address(CpuRegister(RSP), source.GetStackIndex()));
1601 } else {
1602 DCHECK(destination.IsStackSlot());
1603 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1604 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
1605 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001606 } else if (source.IsDoubleStackSlot()) {
1607 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001608 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001609 Address(CpuRegister(RSP), source.GetStackIndex()));
1610 } else {
1611 DCHECK(destination.IsDoubleStackSlot());
1612 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1613 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
1614 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001615 } else if (source.IsConstant()) {
1616 HConstant* constant = source.GetConstant();
1617 if (constant->IsIntConstant()) {
1618 Immediate imm(constant->AsIntConstant()->GetValue());
1619 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001620 __ movl(destination.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001621 } else {
1622 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
1623 }
1624 } else if (constant->IsLongConstant()) {
1625 int64_t value = constant->AsLongConstant()->GetValue();
1626 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001627 __ movq(destination.As<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001628 } else {
1629 __ movq(CpuRegister(TMP), Immediate(value));
1630 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
1631 }
1632 } else {
1633 LOG(FATAL) << "Unimplemented constant type";
1634 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001635 } else {
1636 LOG(FATAL) << "Unimplemented";
1637 }
1638}
1639
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001640void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001641 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001642 __ movl(Address(CpuRegister(RSP), mem), reg);
1643 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001644}
1645
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001646void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001647 ScratchRegisterScope ensure_scratch(
1648 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
1649
1650 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
1651 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
1652 __ movl(CpuRegister(ensure_scratch.GetRegister()),
1653 Address(CpuRegister(RSP), mem2 + stack_offset));
1654 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
1655 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
1656 CpuRegister(ensure_scratch.GetRegister()));
1657}
1658
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001659void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
1660 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
1661 __ movq(Address(CpuRegister(RSP), mem), reg);
1662 __ movq(reg, CpuRegister(TMP));
1663}
1664
1665void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
1666 ScratchRegisterScope ensure_scratch(
1667 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
1668
1669 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
1670 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
1671 __ movq(CpuRegister(ensure_scratch.GetRegister()),
1672 Address(CpuRegister(RSP), mem2 + stack_offset));
1673 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
1674 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
1675 CpuRegister(ensure_scratch.GetRegister()));
1676}
1677
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001678void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
1679 MoveOperands* move = moves_.Get(index);
1680 Location source = move->GetSource();
1681 Location destination = move->GetDestination();
1682
1683 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001684 __ xchgq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001685 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001686 Exchange32(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001687 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001688 Exchange32(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001689 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001690 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
1691 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001692 Exchange64(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001693 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001694 Exchange64(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001695 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
1696 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001697 } else {
1698 LOG(FATAL) << "Unimplemented";
1699 }
1700}
1701
1702
1703void ParallelMoveResolverX86_64::SpillScratch(int reg) {
1704 __ pushq(CpuRegister(reg));
1705}
1706
1707
1708void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
1709 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001710}
1711
1712} // namespace x86_64
1713} // namespace art