blob: c15cca62af131fac17e0ab1a9b0b0a54bb4b50cb [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;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001161 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001162 case Primitive::kPrimVoid:
1163 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001164 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001165 }
1166}
1167
1168void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001169 LocationSummary* locations =
1170 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001171 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001172 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001173}
1174
1175void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1176 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001177 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
1178 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001179 size_t offset = instruction->GetFieldOffset().SizeValue();
1180
1181 switch (instruction->GetType()) {
1182 case Primitive::kPrimBoolean: {
1183 __ movzxb(out, Address(obj, offset));
1184 break;
1185 }
1186
1187 case Primitive::kPrimByte: {
1188 __ movsxb(out, Address(obj, offset));
1189 break;
1190 }
1191
1192 case Primitive::kPrimShort: {
1193 __ movsxw(out, Address(obj, offset));
1194 break;
1195 }
1196
1197 case Primitive::kPrimChar: {
1198 __ movzxw(out, Address(obj, offset));
1199 break;
1200 }
1201
1202 case Primitive::kPrimInt:
1203 case Primitive::kPrimNot: {
1204 __ movl(out, Address(obj, offset));
1205 break;
1206 }
1207
1208 case Primitive::kPrimLong: {
1209 __ movq(out, Address(obj, offset));
1210 break;
1211 }
1212
1213 case Primitive::kPrimFloat:
1214 case Primitive::kPrimDouble:
1215 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001216 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001217 case Primitive::kPrimVoid:
1218 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001219 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001220 }
1221}
1222
1223void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001224 LocationSummary* locations =
1225 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001226 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001227 if (instruction->HasUses()) {
1228 locations->SetOut(Location::SameAsFirstInput());
1229 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001230}
1231
1232void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001233 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001234 codegen_->AddSlowPath(slow_path);
1235
1236 LocationSummary* locations = instruction->GetLocations();
1237 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001238
1239 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001240 __ cmpl(obj.As<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001241 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001242 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001243 } else {
1244 DCHECK(obj.IsConstant()) << obj;
1245 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1246 __ jmp(slow_path->GetEntryLabel());
1247 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001248 }
1249 __ j(kEqual, slow_path->GetEntryLabel());
1250}
1251
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001252void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001253 LocationSummary* locations =
1254 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001255 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
1256 locations->SetInAt(
1257 1, Location::RegisterOrConstant(instruction->InputAt(1)), Location::kDiesAtEntry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001258 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001259}
1260
1261void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
1262 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001263 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001264 Location index = locations->InAt(1);
1265
1266 switch (instruction->GetType()) {
1267 case Primitive::kPrimBoolean: {
1268 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001269 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001270 if (index.IsConstant()) {
1271 __ movzxb(out, Address(obj,
1272 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1273 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001274 __ movzxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001275 }
1276 break;
1277 }
1278
1279 case Primitive::kPrimByte: {
1280 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001281 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001282 if (index.IsConstant()) {
1283 __ movsxb(out, Address(obj,
1284 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1285 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001286 __ movsxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001287 }
1288 break;
1289 }
1290
1291 case Primitive::kPrimShort: {
1292 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001293 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001294 if (index.IsConstant()) {
1295 __ movsxw(out, Address(obj,
1296 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1297 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001298 __ movsxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001299 }
1300 break;
1301 }
1302
1303 case Primitive::kPrimChar: {
1304 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001305 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001306 if (index.IsConstant()) {
1307 __ movzxw(out, Address(obj,
1308 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1309 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001310 __ movzxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001311 }
1312 break;
1313 }
1314
1315 case Primitive::kPrimInt:
1316 case Primitive::kPrimNot: {
1317 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1318 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001319 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001320 if (index.IsConstant()) {
1321 __ movl(out, Address(obj,
1322 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1323 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001324 __ movl(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001325 }
1326 break;
1327 }
1328
1329 case Primitive::kPrimLong: {
1330 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001331 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001332 if (index.IsConstant()) {
1333 __ movq(out, Address(obj,
1334 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
1335 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001336 __ movq(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001337 }
1338 break;
1339 }
1340
1341 case Primitive::kPrimFloat:
1342 case Primitive::kPrimDouble:
1343 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001344 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001345 case Primitive::kPrimVoid:
1346 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001347 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001348 }
1349}
1350
1351void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001352 Primitive::Type value_type = instruction->GetComponentType();
1353 bool is_object = value_type == Primitive::kPrimNot;
1354 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1355 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
1356 if (is_object) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001357 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001358 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1359 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1360 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001361 } else {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001362 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
1363 locations->SetInAt(
1364 1, Location::RegisterOrConstant(instruction->InputAt(1)), Location::kDiesAtEntry);
1365 locations->SetInAt(2, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001366 if (value_type == Primitive::kPrimLong) {
1367 locations->SetInAt(2, Location::RequiresRegister(), Location::kDiesAtEntry);
1368 } else {
1369 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)), Location::kDiesAtEntry);
1370 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001371 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001372}
1373
1374void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
1375 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001376 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001377 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001378 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001379 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001380
1381 switch (value_type) {
1382 case Primitive::kPrimBoolean:
1383 case Primitive::kPrimByte: {
1384 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001385 if (index.IsConstant()) {
1386 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001387 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001388 __ movb(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001389 } else {
1390 __ movb(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1391 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001392 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001393 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001394 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
1395 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001396 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001397 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001398 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1399 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001400 }
1401 break;
1402 }
1403
1404 case Primitive::kPrimShort:
1405 case Primitive::kPrimChar: {
1406 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001407 if (index.IsConstant()) {
1408 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001409 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001410 __ movw(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001411 } else {
1412 __ movw(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1413 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001414 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001415 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001416 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
1417 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001418 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001419 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001420 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1421 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001422 }
1423 break;
1424 }
1425
1426 case Primitive::kPrimInt: {
1427 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001428 if (index.IsConstant()) {
1429 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001430 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001431 __ movl(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001432 } else {
1433 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1434 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001435 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001436 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001437 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
1438 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001439 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001440 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001441 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1442 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001443 }
1444 break;
1445 }
1446
1447 case Primitive::kPrimNot: {
1448 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject), true));
1449 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001450 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001451 break;
1452 }
1453
1454 case Primitive::kPrimLong: {
1455 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001456 if (index.IsConstant()) {
1457 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001458 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001459 __ movq(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001460 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001461 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001462 __ movq(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
1463 value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001464 }
1465 break;
1466 }
1467
1468 case Primitive::kPrimFloat:
1469 case Primitive::kPrimDouble:
1470 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001471 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001472 case Primitive::kPrimVoid:
1473 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001474 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001475 }
1476}
1477
1478void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001479 LocationSummary* locations =
1480 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001481 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001482 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001483}
1484
1485void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
1486 LocationSummary* locations = instruction->GetLocations();
1487 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001488 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
1489 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001490 __ movl(out, Address(obj, offset));
1491}
1492
1493void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001494 LocationSummary* locations =
1495 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001496 locations->SetInAt(0, Location::RequiresRegister());
1497 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001498 if (instruction->HasUses()) {
1499 locations->SetOut(Location::SameAsFirstInput());
1500 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001501}
1502
1503void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
1504 LocationSummary* locations = instruction->GetLocations();
1505 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01001506 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001507 codegen_->AddSlowPath(slow_path);
1508
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001509 CpuRegister index = locations->InAt(0).As<CpuRegister>();
1510 CpuRegister length = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001511
1512 __ cmpl(index, length);
1513 __ j(kAboveEqual, slow_path->GetEntryLabel());
1514}
1515
1516void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
1517 CpuRegister card,
1518 CpuRegister object,
1519 CpuRegister value) {
1520 Label is_null;
1521 __ testl(value, value);
1522 __ j(kEqual, &is_null);
1523 __ gs()->movq(card, Address::Absolute(
1524 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
1525 __ movq(temp, object);
1526 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
1527 __ movb(Address(temp, card, TIMES_1, 0), card);
1528 __ Bind(&is_null);
1529}
1530
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001531void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
1532 temp->SetLocations(nullptr);
1533}
1534
1535void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
1536 // Nothing to do, this is driven by the code generator.
1537}
1538
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001539void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
1540 LOG(FATAL) << "Unimplemented";
1541}
1542
1543void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001544 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
1545}
1546
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001547void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
1548 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
1549}
1550
1551void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001552 HBasicBlock* block = instruction->GetBlock();
1553 if (block->GetLoopInformation() != nullptr) {
1554 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
1555 // The back edge will generate the suspend check.
1556 return;
1557 }
1558 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
1559 // The goto will generate the suspend check.
1560 return;
1561 }
1562 GenerateSuspendCheck(instruction, nullptr);
1563}
1564
1565void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
1566 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001567 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001568 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001569 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001570 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001571 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001572 if (successor == nullptr) {
1573 __ j(kNotEqual, slow_path->GetEntryLabel());
1574 __ Bind(slow_path->GetReturnLabel());
1575 } else {
1576 __ j(kEqual, codegen_->GetLabelOf(successor));
1577 __ jmp(slow_path->GetEntryLabel());
1578 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001579}
1580
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001581X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
1582 return codegen_->GetAssembler();
1583}
1584
1585void ParallelMoveResolverX86_64::EmitMove(size_t index) {
1586 MoveOperands* move = moves_.Get(index);
1587 Location source = move->GetSource();
1588 Location destination = move->GetDestination();
1589
1590 if (source.IsRegister()) {
1591 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001592 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001593 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001594 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001595 source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001596 } else {
1597 DCHECK(destination.IsDoubleStackSlot());
1598 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001599 source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001600 }
1601 } else if (source.IsStackSlot()) {
1602 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001603 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001604 Address(CpuRegister(RSP), source.GetStackIndex()));
1605 } else {
1606 DCHECK(destination.IsStackSlot());
1607 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1608 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
1609 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001610 } else if (source.IsDoubleStackSlot()) {
1611 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001612 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001613 Address(CpuRegister(RSP), source.GetStackIndex()));
1614 } else {
1615 DCHECK(destination.IsDoubleStackSlot());
1616 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1617 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
1618 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001619 } else if (source.IsConstant()) {
1620 HConstant* constant = source.GetConstant();
1621 if (constant->IsIntConstant()) {
1622 Immediate imm(constant->AsIntConstant()->GetValue());
1623 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001624 __ movl(destination.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001625 } else {
1626 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
1627 }
1628 } else if (constant->IsLongConstant()) {
1629 int64_t value = constant->AsLongConstant()->GetValue();
1630 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001631 __ movq(destination.As<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001632 } else {
1633 __ movq(CpuRegister(TMP), Immediate(value));
1634 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
1635 }
1636 } else {
1637 LOG(FATAL) << "Unimplemented constant type";
1638 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001639 } else {
1640 LOG(FATAL) << "Unimplemented";
1641 }
1642}
1643
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001644void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001645 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001646 __ movl(Address(CpuRegister(RSP), mem), reg);
1647 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001648}
1649
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001650void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001651 ScratchRegisterScope ensure_scratch(
1652 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
1653
1654 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
1655 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
1656 __ movl(CpuRegister(ensure_scratch.GetRegister()),
1657 Address(CpuRegister(RSP), mem2 + stack_offset));
1658 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
1659 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
1660 CpuRegister(ensure_scratch.GetRegister()));
1661}
1662
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001663void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
1664 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
1665 __ movq(Address(CpuRegister(RSP), mem), reg);
1666 __ movq(reg, CpuRegister(TMP));
1667}
1668
1669void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
1670 ScratchRegisterScope ensure_scratch(
1671 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
1672
1673 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
1674 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
1675 __ movq(CpuRegister(ensure_scratch.GetRegister()),
1676 Address(CpuRegister(RSP), mem2 + stack_offset));
1677 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
1678 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
1679 CpuRegister(ensure_scratch.GetRegister()));
1680}
1681
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001682void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
1683 MoveOperands* move = moves_.Get(index);
1684 Location source = move->GetSource();
1685 Location destination = move->GetDestination();
1686
1687 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001688 __ xchgq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001689 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001690 Exchange32(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001691 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001692 Exchange32(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001693 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001694 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
1695 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001696 Exchange64(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001697 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001698 Exchange64(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001699 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
1700 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001701 } else {
1702 LOG(FATAL) << "Unimplemented";
1703 }
1704}
1705
1706
1707void ParallelMoveResolverX86_64::SpillScratch(int reg) {
1708 __ pushq(CpuRegister(reg));
1709}
1710
1711
1712void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
1713 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001714}
1715
1716} // namespace x86_64
1717} // namespace art