blob: 5781e7e07ae3b4dccccc4b45edb26c99d665616f [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)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100188 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfFloatRegisters, 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100189 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 Geoffray71175b72014-10-09 22:13:55 +0100203Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100204 switch (type) {
205 case Primitive::kPrimLong:
206 case Primitive::kPrimByte:
207 case Primitive::kPrimBoolean:
208 case Primitive::kPrimChar:
209 case Primitive::kPrimShort:
210 case Primitive::kPrimInt:
211 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100212 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100213 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100214 }
215
216 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100217 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100218 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100219 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100220 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100221
222 case Primitive::kPrimVoid:
223 LOG(FATAL) << "Unreachable type " << type;
224 }
225
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100226 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100227}
228
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100229void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100230 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100231 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100232
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000233 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100234 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000235
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100236 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100237 blocked_core_registers_[RBX] = true;
238 blocked_core_registers_[RBP] = true;
239 blocked_core_registers_[R12] = true;
240 blocked_core_registers_[R13] = true;
241 blocked_core_registers_[R14] = true;
242 blocked_core_registers_[R15] = true;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100243
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100244 blocked_fpu_registers_[XMM12] = true;
245 blocked_fpu_registers_[XMM13] = true;
246 blocked_fpu_registers_[XMM14] = true;
247 blocked_fpu_registers_[XMM15] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100248}
249
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100250void CodeGeneratorX86_64::GenerateFrameEntry() {
251 // Create a fake register to mimic Quick.
252 static const int kFakeReturnRegister = 16;
253 core_spill_mask_ |= (1 << kFakeReturnRegister);
254
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100255 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700256 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100257
258 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
259 __ testq(CpuRegister(RAX), Address(
260 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100261 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100262 }
263
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100264 // The return PC has already been pushed on the stack.
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100265 __ subq(CpuRegister(RSP),
266 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
267
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100268 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
269 SlowPathCode* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86_64();
270 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100271
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100272 __ gs()->cmpq(CpuRegister(RSP),
273 Address::Absolute(Thread::StackEndOffset<kX86_64WordSize>(), true));
274 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100275 }
276
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100277 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
278}
279
280void CodeGeneratorX86_64::GenerateFrameExit() {
281 __ addq(CpuRegister(RSP),
282 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
283}
284
285void CodeGeneratorX86_64::Bind(Label* label) {
286 __ Bind(label);
287}
288
289void InstructionCodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
290 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
291}
292
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100293Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
294 switch (load->GetType()) {
295 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100296 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100297 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
298 break;
299
300 case Primitive::kPrimInt:
301 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100302 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100303 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100304
305 case Primitive::kPrimBoolean:
306 case Primitive::kPrimByte:
307 case Primitive::kPrimChar:
308 case Primitive::kPrimShort:
309 case Primitive::kPrimVoid:
310 LOG(FATAL) << "Unexpected type " << load->GetType();
311 }
312
313 LOG(FATAL) << "Unreachable";
314 return Location();
315}
316
317void CodeGeneratorX86_64::Move(Location destination, Location source) {
318 if (source.Equals(destination)) {
319 return;
320 }
321 if (destination.IsRegister()) {
322 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100323 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100324 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100325 __ movd(destination.As<CpuRegister>(), source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100326 } else if (source.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100327 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100328 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100329 } else {
330 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100331 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100332 Address(CpuRegister(RSP), source.GetStackIndex()));
333 }
334 } else if (destination.IsFpuRegister()) {
335 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100336 __ movd(destination.As<XmmRegister>(), source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100337 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100338 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100339 } else if (source.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100340 __ movss(destination.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100341 Address(CpuRegister(RSP), source.GetStackIndex()));
342 } else {
343 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100344 __ movsd(destination.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100345 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100346 }
347 } else if (destination.IsStackSlot()) {
348 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100349 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100350 source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100351 } else if (source.IsFpuRegister()) {
352 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100353 source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100354 } else {
355 DCHECK(source.IsStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000356 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
357 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100358 }
359 } else {
360 DCHECK(destination.IsDoubleStackSlot());
361 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100362 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100363 source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100364 } else if (source.IsFpuRegister()) {
365 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100366 source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100367 } else {
368 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000369 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
370 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100371 }
372 }
373}
374
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100375void CodeGeneratorX86_64::Move(HInstruction* instruction,
376 Location location,
377 HInstruction* move_for) {
Roland Levillain476df552014-10-09 17:51:36 +0100378 if (instruction->IsIntConstant()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100379 Immediate imm(instruction->AsIntConstant()->GetValue());
380 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100381 __ movl(location.As<CpuRegister>(), imm);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100382 } else {
383 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
384 }
Roland Levillain476df552014-10-09 17:51:36 +0100385 } else if (instruction->IsLongConstant()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100386 int64_t value = instruction->AsLongConstant()->GetValue();
387 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100388 __ movq(location.As<CpuRegister>(), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100389 } else {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000390 __ movq(CpuRegister(TMP), Immediate(value));
391 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100392 }
Roland Levillain476df552014-10-09 17:51:36 +0100393 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100394 switch (instruction->GetType()) {
395 case Primitive::kPrimBoolean:
396 case Primitive::kPrimByte:
397 case Primitive::kPrimChar:
398 case Primitive::kPrimShort:
399 case Primitive::kPrimInt:
400 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100401 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100402 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
403 break;
404
405 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100406 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100407 Move(location, Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
408 break;
409
410 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100411 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100412 }
413 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100414 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100415 switch (instruction->GetType()) {
416 case Primitive::kPrimBoolean:
417 case Primitive::kPrimByte:
418 case Primitive::kPrimChar:
419 case Primitive::kPrimShort:
420 case Primitive::kPrimInt:
421 case Primitive::kPrimNot:
422 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100423 case Primitive::kPrimFloat:
424 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100425 Move(location, instruction->GetLocations()->Out());
426 break;
427
428 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100429 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100430 }
431 }
432}
433
434void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
435 got->SetLocations(nullptr);
436}
437
438void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
439 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100440 DCHECK(!successor->IsExitBlock());
441
442 HBasicBlock* block = got->GetBlock();
443 HInstruction* previous = got->GetPrevious();
444
445 HLoopInformation* info = block->GetLoopInformation();
446 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
447 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
448 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
449 return;
450 }
451
452 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
453 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
454 }
455 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100456 __ jmp(codegen_->GetLabelOf(successor));
457 }
458}
459
460void LocationsBuilderX86_64::VisitExit(HExit* exit) {
461 exit->SetLocations(nullptr);
462}
463
464void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
465 if (kIsDebugBuild) {
466 __ Comment("Unreachable");
467 __ int3();
468 }
469}
470
471void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100472 LocationSummary* locations =
473 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100474 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100475 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +0100476 locations->SetInAt(0, Location::Any(), Location::kDiesAtEntry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100477 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100478}
479
480void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700481 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100482 if (cond->IsIntConstant()) {
483 // Constant condition, statically compared against 1.
484 int32_t cond_value = cond->AsIntConstant()->GetValue();
485 if (cond_value == 1) {
486 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
487 if_instr->IfTrueSuccessor())) {
488 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100489 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100490 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100491 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100492 DCHECK_EQ(cond_value, 0);
493 }
494 } else {
495 bool materialized =
496 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
497 // Moves do not affect the eflags register, so if the condition is
498 // evaluated just before the if, we don't need to evaluate it
499 // again.
500 bool eflags_set = cond->IsCondition()
501 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
502 if (materialized) {
503 if (!eflags_set) {
504 // Materialized condition, compare against 0.
505 Location lhs = if_instr->GetLocations()->InAt(0);
506 if (lhs.IsRegister()) {
507 __ cmpl(lhs.As<CpuRegister>(), Immediate(0));
508 } else {
509 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
510 Immediate(0));
511 }
512 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
513 } else {
514 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
515 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
516 }
517 } else {
518 Location lhs = cond->GetLocations()->InAt(0);
519 Location rhs = cond->GetLocations()->InAt(1);
520 if (rhs.IsRegister()) {
521 __ cmpl(lhs.As<CpuRegister>(), rhs.As<CpuRegister>());
522 } else if (rhs.IsConstant()) {
523 __ cmpl(lhs.As<CpuRegister>(),
524 Immediate(rhs.GetConstant()->AsIntConstant()->GetValue()));
525 } else {
526 __ cmpl(lhs.As<CpuRegister>(),
527 Address(CpuRegister(RSP), rhs.GetStackIndex()));
528 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100529 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
530 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700531 }
Dave Allison20dfc792014-06-16 20:44:29 -0700532 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100533 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
534 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700535 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100536 }
537}
538
539void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
540 local->SetLocations(nullptr);
541}
542
543void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
544 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
545}
546
547void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
548 local->SetLocations(nullptr);
549}
550
551void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
552 // Nothing to do, this is driven by the code generator.
553}
554
555void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100556 LocationSummary* locations =
557 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100558 switch (store->InputAt(1)->GetType()) {
559 case Primitive::kPrimBoolean:
560 case Primitive::kPrimByte:
561 case Primitive::kPrimChar:
562 case Primitive::kPrimShort:
563 case Primitive::kPrimInt:
564 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100565 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100566 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
567 break;
568
569 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100570 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100571 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
572 break;
573
574 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100575 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100576 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100577}
578
579void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
580}
581
Dave Allison20dfc792014-06-16 20:44:29 -0700582void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100583 LocationSummary* locations =
584 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +0100585 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
586 locations->SetInAt(1, Location::Any(), Location::kDiesAtEntry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100587 if (comp->NeedsMaterialization()) {
588 locations->SetOut(Location::RequiresRegister());
589 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100590}
591
Dave Allison20dfc792014-06-16 20:44:29 -0700592void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
593 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100594 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100595 CpuRegister reg = locations->Out().As<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100596 // Clear register: setcc only sets the low byte.
597 __ xorq(reg, reg);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100598 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100599 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100600 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100601 } else if (locations->InAt(1).IsConstant()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100602 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100603 Immediate(locations->InAt(1).GetConstant()->AsIntConstant()->GetValue()));
604 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100605 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100606 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
607 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100608 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700609 }
610}
611
612void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
613 VisitCondition(comp);
614}
615
616void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
617 VisitCondition(comp);
618}
619
620void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
621 VisitCondition(comp);
622}
623
624void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
625 VisitCondition(comp);
626}
627
628void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
629 VisitCondition(comp);
630}
631
632void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
633 VisitCondition(comp);
634}
635
636void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
637 VisitCondition(comp);
638}
639
640void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
641 VisitCondition(comp);
642}
643
644void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
645 VisitCondition(comp);
646}
647
648void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
649 VisitCondition(comp);
650}
651
652void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
653 VisitCondition(comp);
654}
655
656void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
657 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100658}
659
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100660void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100661 LocationSummary* locations =
662 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +0100663 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
664 locations->SetInAt(1, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100665 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100666}
667
668void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
669 Label greater, done;
670 LocationSummary* locations = compare->GetLocations();
671 switch (compare->InputAt(0)->GetType()) {
672 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100673 __ cmpq(locations->InAt(0).As<CpuRegister>(),
674 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100675 break;
676 default:
677 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
678 }
679
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100680 CpuRegister output = locations->Out().As<CpuRegister>();
681 __ movl(output, Immediate(0));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100682 __ j(kEqual, &done);
683 __ j(kGreater, &greater);
684
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100685 __ movl(output, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100686 __ jmp(&done);
687
688 __ Bind(&greater);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100689 __ movl(output, Immediate(1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100690
691 __ Bind(&done);
692}
693
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100694void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100695 LocationSummary* locations =
696 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100697 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100698}
699
700void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100701 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100702}
703
704void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100705 LocationSummary* locations =
706 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100707 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100708}
709
710void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100711 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100712}
713
714void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
715 ret->SetLocations(nullptr);
716}
717
718void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
719 codegen_->GenerateFrameExit();
720 __ ret();
721}
722
723void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100724 LocationSummary* locations =
725 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100726 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 locations->SetInAt(0, Location::RegisterLocation(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:
739 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100740 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100741 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 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100746}
747
748void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
749 if (kIsDebugBuild) {
750 switch (ret->InputAt(0)->GetType()) {
751 case Primitive::kPrimBoolean:
752 case Primitive::kPrimByte:
753 case Primitive::kPrimChar:
754 case Primitive::kPrimShort:
755 case Primitive::kPrimInt:
756 case Primitive::kPrimNot:
757 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100758 DCHECK_EQ(ret->GetLocations()->InAt(0).As<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100759 break;
760
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100761 case Primitive::kPrimFloat:
762 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100763 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100764 XMM0);
765 break;
766
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100767 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100768 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100769 }
770 }
771 codegen_->GenerateFrameExit();
772 __ ret();
773}
774
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100775Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
776 switch (type) {
777 case Primitive::kPrimBoolean:
778 case Primitive::kPrimByte:
779 case Primitive::kPrimChar:
780 case Primitive::kPrimShort:
781 case Primitive::kPrimInt:
782 case Primitive::kPrimNot: {
783 uint32_t index = gp_index_++;
784 stack_index_++;
785 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100786 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100787 } else {
788 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
789 }
790 }
791
792 case Primitive::kPrimLong: {
793 uint32_t index = gp_index_;
794 stack_index_ += 2;
795 if (index < calling_convention.GetNumberOfRegisters()) {
796 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100797 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100798 } else {
799 gp_index_ += 2;
800 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
801 }
802 }
803
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100804 case Primitive::kPrimFloat: {
805 uint32_t index = fp_index_++;
806 stack_index_++;
807 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100808 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100809 } else {
810 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
811 }
812 }
813
814 case Primitive::kPrimDouble: {
815 uint32_t index = fp_index_++;
816 stack_index_ += 2;
817 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100818 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100819 } else {
820 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
821 }
822 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100823
824 case Primitive::kPrimVoid:
825 LOG(FATAL) << "Unexpected parameter type " << type;
826 break;
827 }
828 return Location();
829}
830
831void LocationsBuilderX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100832 HandleInvoke(invoke);
833}
834
835void InstructionCodeGeneratorX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100836 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100837 uint32_t heap_reference_size = sizeof(mirror::HeapReference<mirror::Object>);
838 size_t index_in_cache = mirror::Array::DataOffset(heap_reference_size).SizeValue() +
839 invoke->GetIndexInDexCache() * heap_reference_size;
840
841 // TODO: Implement all kinds of calls:
842 // 1) boot -> boot
843 // 2) app -> boot
844 // 3) app -> app
845 //
846 // Currently we implement the app -> app logic, which looks up in the resolve cache.
847
848 // temp = method;
849 LoadCurrentMethod(temp);
850 // temp = temp->dex_cache_resolved_methods_;
851 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
852 // temp = temp[index_in_cache]
853 __ movl(temp, Address(temp, index_in_cache));
854 // (temp + offset_of_quick_compiled_code)()
855 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
856
857 DCHECK(!codegen_->IsLeafMethod());
858 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
859}
860
861void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
862 HandleInvoke(invoke);
863}
864
865void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100866 LocationSummary* locations =
867 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100868 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100869
870 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100871 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100872 HInstruction* input = invoke->InputAt(i);
873 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
874 }
875
876 switch (invoke->GetType()) {
877 case Primitive::kPrimBoolean:
878 case Primitive::kPrimByte:
879 case Primitive::kPrimChar:
880 case Primitive::kPrimShort:
881 case Primitive::kPrimInt:
882 case Primitive::kPrimNot:
883 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100884 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100885 break;
886
887 case Primitive::kPrimVoid:
888 break;
889
890 case Primitive::kPrimDouble:
891 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100892 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100893 break;
894 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100895}
896
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100897void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100898 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100899 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
900 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
901 LocationSummary* locations = invoke->GetLocations();
902 Location receiver = locations->InAt(0);
903 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
904 // temp = object->GetClass();
905 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100906 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
907 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100908 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100909 __ movl(temp, Address(receiver.As<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100910 }
911 // temp = temp->GetMethodAt(method_offset);
912 __ movl(temp, Address(temp, method_offset));
913 // call temp->GetEntryPoint();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100914 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
915
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100916 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100917 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100918}
919
920void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100921 LocationSummary* locations =
922 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100923 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100924 case Primitive::kPrimInt: {
925 locations->SetInAt(0, Location::RequiresRegister());
926 locations->SetInAt(1, Location::Any());
927 locations->SetOut(Location::SameAsFirstInput());
928 break;
929 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100930
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100931 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000932 locations->SetInAt(0, Location::RequiresRegister());
933 locations->SetInAt(1, Location::RequiresRegister());
934 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100935 break;
936 }
937
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100938 case Primitive::kPrimDouble:
939 case Primitive::kPrimFloat: {
940 locations->SetInAt(0, Location::RequiresFpuRegister());
941 locations->SetInAt(1, Location::Any());
942 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100943 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100944 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100945
946 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100947 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100948 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100949}
950
951void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
952 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100953 Location first = locations->InAt(0);
954 Location second = locations->InAt(1);
955
956 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100957 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000958 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100959 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100960 __ addl(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100961 } else if (second.IsConstant()) {
962 HConstant* instruction = second.GetConstant();
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100963 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100964 __ addl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100965 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100966 __ addl(first.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100967 Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100968 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000969 break;
970 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100971
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100972 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100973 __ addq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100974 break;
975 }
976
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100977 case Primitive::kPrimFloat: {
978 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100979 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100980 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100981 __ addss(first.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100982 Address(CpuRegister(RSP), second.GetStackIndex()));
983 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100984 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100985 }
986
987 case Primitive::kPrimDouble: {
988 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100989 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100990 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100991 __ addsd(first.As<XmmRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100992 }
993 break;
994 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100995
996 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100997 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100998 }
999}
1000
1001void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001002 LocationSummary* locations =
1003 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001004 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001005 case Primitive::kPrimInt: {
1006 locations->SetInAt(0, Location::RequiresRegister());
1007 locations->SetInAt(1, Location::Any());
1008 locations->SetOut(Location::SameAsFirstInput());
1009 break;
1010 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001011 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001012 locations->SetInAt(0, Location::RequiresRegister());
1013 locations->SetInAt(1, Location::RequiresRegister());
1014 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001015 break;
1016 }
1017
1018 case Primitive::kPrimBoolean:
1019 case Primitive::kPrimByte:
1020 case Primitive::kPrimChar:
1021 case Primitive::kPrimShort:
1022 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
1023 break;
1024
1025 default:
1026 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
1027 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001028}
1029
1030void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1031 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001032 DCHECK_EQ(locations->InAt(0).As<CpuRegister>().AsRegister(),
1033 locations->Out().As<CpuRegister>().AsRegister());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001034 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001035 case Primitive::kPrimInt: {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001036 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001037 __ subl(locations->InAt(0).As<CpuRegister>(),
1038 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001039 } else if (locations->InAt(1).IsConstant()) {
1040 HConstant* instruction = locations->InAt(1).GetConstant();
1041 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001042 __ subl(locations->InAt(0).As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001043 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001044 __ subl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001045 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
1046 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001047 break;
1048 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001049 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001050 __ subq(locations->InAt(0).As<CpuRegister>(),
1051 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001052 break;
1053 }
1054
1055 case Primitive::kPrimBoolean:
1056 case Primitive::kPrimByte:
1057 case Primitive::kPrimChar:
1058 case Primitive::kPrimShort:
1059 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
1060 break;
1061
1062 default:
1063 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
1064 }
1065}
1066
1067void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001068 LocationSummary* locations =
1069 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001070 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001071 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1072 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1073 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001074}
1075
1076void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
1077 InvokeRuntimeCallingConvention calling_convention;
1078 LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
1079 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1080
1081 __ gs()->call(Address::Absolute(
1082 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
1083
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001084 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001085 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001086}
1087
1088void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001089 LocationSummary* locations =
1090 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001091 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1092 if (location.IsStackSlot()) {
1093 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1094 } else if (location.IsDoubleStackSlot()) {
1095 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1096 }
1097 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001098}
1099
1100void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
1101 // Nothing to do, the parameter is already at its location.
1102}
1103
1104void LocationsBuilderX86_64::VisitNot(HNot* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001105 LocationSummary* locations =
1106 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001107 locations->SetInAt(0, Location::RequiresRegister());
1108 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001109}
1110
1111void InstructionCodeGeneratorX86_64::VisitNot(HNot* instruction) {
1112 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001113 DCHECK_EQ(locations->InAt(0).As<CpuRegister>().AsRegister(),
1114 locations->Out().As<CpuRegister>().AsRegister());
1115 __ xorq(locations->Out().As<CpuRegister>(), Immediate(1));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001116}
1117
1118void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001119 LocationSummary* locations =
1120 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001121 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1122 locations->SetInAt(i, Location::Any());
1123 }
1124 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001125}
1126
1127void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
1128 LOG(FATAL) << "Unimplemented";
1129}
1130
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001131void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001132 LocationSummary* locations =
1133 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001134 Primitive::Type field_type = instruction->GetFieldType();
1135 bool is_object_type = field_type == Primitive::kPrimNot;
1136 bool dies_at_entry = !is_object_type;
1137 locations->SetInAt(0, Location::RequiresRegister(), dies_at_entry);
1138 locations->SetInAt(1, Location::RequiresRegister(), dies_at_entry);
1139 if (is_object_type) {
1140 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001141 locations->AddTemp(Location::RequiresRegister());
1142 locations->AddTemp(Location::RequiresRegister());
1143 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001144}
1145
1146void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1147 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001148 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
1149 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001150 size_t offset = instruction->GetFieldOffset().SizeValue();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001151 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001152
1153 switch (field_type) {
1154 case Primitive::kPrimBoolean:
1155 case Primitive::kPrimByte: {
1156 __ movb(Address(obj, offset), value);
1157 break;
1158 }
1159
1160 case Primitive::kPrimShort:
1161 case Primitive::kPrimChar: {
1162 __ movw(Address(obj, offset), value);
1163 break;
1164 }
1165
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001166 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001167 case Primitive::kPrimNot: {
1168 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001169 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001170 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
1171 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001172 codegen_->MarkGCCard(temp, card, obj, value);
1173 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001174 break;
1175 }
1176
1177 case Primitive::kPrimLong: {
1178 __ movq(Address(obj, offset), value);
1179 break;
1180 }
1181
1182 case Primitive::kPrimFloat:
1183 case Primitive::kPrimDouble:
1184 LOG(FATAL) << "Unimplemented register type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001185 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001186 case Primitive::kPrimVoid:
1187 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001188 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001189 }
1190}
1191
1192void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001193 LocationSummary* locations =
1194 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001195 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001196 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001197}
1198
1199void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1200 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001201 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
1202 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001203 size_t offset = instruction->GetFieldOffset().SizeValue();
1204
1205 switch (instruction->GetType()) {
1206 case Primitive::kPrimBoolean: {
1207 __ movzxb(out, Address(obj, offset));
1208 break;
1209 }
1210
1211 case Primitive::kPrimByte: {
1212 __ movsxb(out, Address(obj, offset));
1213 break;
1214 }
1215
1216 case Primitive::kPrimShort: {
1217 __ movsxw(out, Address(obj, offset));
1218 break;
1219 }
1220
1221 case Primitive::kPrimChar: {
1222 __ movzxw(out, Address(obj, offset));
1223 break;
1224 }
1225
1226 case Primitive::kPrimInt:
1227 case Primitive::kPrimNot: {
1228 __ movl(out, Address(obj, offset));
1229 break;
1230 }
1231
1232 case Primitive::kPrimLong: {
1233 __ movq(out, Address(obj, offset));
1234 break;
1235 }
1236
1237 case Primitive::kPrimFloat:
1238 case Primitive::kPrimDouble:
1239 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001240 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001241 case Primitive::kPrimVoid:
1242 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001243 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001244 }
1245}
1246
1247void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001248 LocationSummary* locations =
1249 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001250 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001251 if (instruction->HasUses()) {
1252 locations->SetOut(Location::SameAsFirstInput());
1253 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001254}
1255
1256void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001257 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001258 codegen_->AddSlowPath(slow_path);
1259
1260 LocationSummary* locations = instruction->GetLocations();
1261 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001262
1263 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001264 __ cmpl(obj.As<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001265 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001266 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001267 } else {
1268 DCHECK(obj.IsConstant()) << obj;
1269 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1270 __ jmp(slow_path->GetEntryLabel());
1271 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001272 }
1273 __ j(kEqual, slow_path->GetEntryLabel());
1274}
1275
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001276void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001277 LocationSummary* locations =
1278 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001279 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
1280 locations->SetInAt(
1281 1, Location::RegisterOrConstant(instruction->InputAt(1)), Location::kDiesAtEntry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001282 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001283}
1284
1285void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
1286 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001287 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001288 Location index = locations->InAt(1);
1289
1290 switch (instruction->GetType()) {
1291 case Primitive::kPrimBoolean: {
1292 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_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 __ movzxb(out, Address(obj,
1296 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1297 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001298 __ movzxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001299 }
1300 break;
1301 }
1302
1303 case Primitive::kPrimByte: {
1304 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_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 __ movsxb(out, Address(obj,
1308 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1309 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001310 __ movsxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001311 }
1312 break;
1313 }
1314
1315 case Primitive::kPrimShort: {
1316 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_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 __ movsxw(out, Address(obj,
1320 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1321 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001322 __ movsxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001323 }
1324 break;
1325 }
1326
1327 case Primitive::kPrimChar: {
1328 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_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 __ movzxw(out, Address(obj,
1332 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1333 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001334 __ movzxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001335 }
1336 break;
1337 }
1338
1339 case Primitive::kPrimInt:
1340 case Primitive::kPrimNot: {
1341 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1342 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001343 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001344 if (index.IsConstant()) {
1345 __ movl(out, Address(obj,
1346 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1347 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001348 __ movl(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001349 }
1350 break;
1351 }
1352
1353 case Primitive::kPrimLong: {
1354 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001355 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001356 if (index.IsConstant()) {
1357 __ movq(out, Address(obj,
1358 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
1359 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001360 __ movq(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001361 }
1362 break;
1363 }
1364
1365 case Primitive::kPrimFloat:
1366 case Primitive::kPrimDouble:
1367 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001368 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001369 case Primitive::kPrimVoid:
1370 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001371 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001372 }
1373}
1374
1375void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001376 Primitive::Type value_type = instruction->GetComponentType();
1377 bool is_object = value_type == Primitive::kPrimNot;
1378 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1379 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
1380 if (is_object) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001381 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001382 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1383 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1384 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001385 } else {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001386 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
1387 locations->SetInAt(
1388 1, Location::RegisterOrConstant(instruction->InputAt(1)), Location::kDiesAtEntry);
1389 locations->SetInAt(2, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001390 if (value_type == Primitive::kPrimLong) {
1391 locations->SetInAt(2, Location::RequiresRegister(), Location::kDiesAtEntry);
1392 } else {
1393 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)), Location::kDiesAtEntry);
1394 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001395 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001396}
1397
1398void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
1399 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001400 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001401 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001402 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001403 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001404
1405 switch (value_type) {
1406 case Primitive::kPrimBoolean:
1407 case Primitive::kPrimByte: {
1408 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001409 if (index.IsConstant()) {
1410 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001411 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001412 __ movb(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001413 } else {
1414 __ movb(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1415 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001416 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001417 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001418 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
1419 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001420 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001421 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001422 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1423 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001424 }
1425 break;
1426 }
1427
1428 case Primitive::kPrimShort:
1429 case Primitive::kPrimChar: {
1430 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001431 if (index.IsConstant()) {
1432 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001433 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001434 __ movw(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001435 } else {
1436 __ movw(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1437 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001438 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001439 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001440 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
1441 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001442 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001443 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001444 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1445 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001446 }
1447 break;
1448 }
1449
1450 case Primitive::kPrimInt: {
1451 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001452 if (index.IsConstant()) {
1453 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001454 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001455 __ movl(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001456 } else {
1457 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1458 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001459 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001460 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001461 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
1462 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001463 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001464 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001465 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1466 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001467 }
1468 break;
1469 }
1470
1471 case Primitive::kPrimNot: {
1472 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject), true));
1473 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001474 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001475 break;
1476 }
1477
1478 case Primitive::kPrimLong: {
1479 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001480 if (index.IsConstant()) {
1481 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001482 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001483 __ movq(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001484 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001485 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001486 __ movq(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
1487 value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001488 }
1489 break;
1490 }
1491
1492 case Primitive::kPrimFloat:
1493 case Primitive::kPrimDouble:
1494 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001495 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001496 case Primitive::kPrimVoid:
1497 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001498 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001499 }
1500}
1501
1502void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001503 LocationSummary* locations =
1504 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001505 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001506 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001507}
1508
1509void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
1510 LocationSummary* locations = instruction->GetLocations();
1511 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001512 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
1513 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001514 __ movl(out, Address(obj, offset));
1515}
1516
1517void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001518 LocationSummary* locations =
1519 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001520 locations->SetInAt(0, Location::RequiresRegister());
1521 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001522 if (instruction->HasUses()) {
1523 locations->SetOut(Location::SameAsFirstInput());
1524 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001525}
1526
1527void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
1528 LocationSummary* locations = instruction->GetLocations();
1529 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01001530 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001531 codegen_->AddSlowPath(slow_path);
1532
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001533 CpuRegister index = locations->InAt(0).As<CpuRegister>();
1534 CpuRegister length = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001535
1536 __ cmpl(index, length);
1537 __ j(kAboveEqual, slow_path->GetEntryLabel());
1538}
1539
1540void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
1541 CpuRegister card,
1542 CpuRegister object,
1543 CpuRegister value) {
1544 Label is_null;
1545 __ testl(value, value);
1546 __ j(kEqual, &is_null);
1547 __ gs()->movq(card, Address::Absolute(
1548 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
1549 __ movq(temp, object);
1550 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
1551 __ movb(Address(temp, card, TIMES_1, 0), card);
1552 __ Bind(&is_null);
1553}
1554
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001555void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
1556 temp->SetLocations(nullptr);
1557}
1558
1559void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
1560 // Nothing to do, this is driven by the code generator.
1561}
1562
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001563void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
1564 LOG(FATAL) << "Unimplemented";
1565}
1566
1567void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001568 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
1569}
1570
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001571void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
1572 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
1573}
1574
1575void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001576 HBasicBlock* block = instruction->GetBlock();
1577 if (block->GetLoopInformation() != nullptr) {
1578 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
1579 // The back edge will generate the suspend check.
1580 return;
1581 }
1582 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
1583 // The goto will generate the suspend check.
1584 return;
1585 }
1586 GenerateSuspendCheck(instruction, nullptr);
1587}
1588
1589void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
1590 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001591 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001592 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001593 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001594 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001595 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001596 if (successor == nullptr) {
1597 __ j(kNotEqual, slow_path->GetEntryLabel());
1598 __ Bind(slow_path->GetReturnLabel());
1599 } else {
1600 __ j(kEqual, codegen_->GetLabelOf(successor));
1601 __ jmp(slow_path->GetEntryLabel());
1602 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001603}
1604
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001605X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
1606 return codegen_->GetAssembler();
1607}
1608
1609void ParallelMoveResolverX86_64::EmitMove(size_t index) {
1610 MoveOperands* move = moves_.Get(index);
1611 Location source = move->GetSource();
1612 Location destination = move->GetDestination();
1613
1614 if (source.IsRegister()) {
1615 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001616 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001617 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001618 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001619 source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001620 } else {
1621 DCHECK(destination.IsDoubleStackSlot());
1622 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001623 source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001624 }
1625 } else if (source.IsStackSlot()) {
1626 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001627 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001628 Address(CpuRegister(RSP), source.GetStackIndex()));
1629 } else {
1630 DCHECK(destination.IsStackSlot());
1631 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1632 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
1633 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001634 } else if (source.IsDoubleStackSlot()) {
1635 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001636 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001637 Address(CpuRegister(RSP), source.GetStackIndex()));
1638 } else {
1639 DCHECK(destination.IsDoubleStackSlot());
1640 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1641 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
1642 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001643 } else if (source.IsConstant()) {
1644 HConstant* constant = source.GetConstant();
1645 if (constant->IsIntConstant()) {
1646 Immediate imm(constant->AsIntConstant()->GetValue());
1647 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001648 __ movl(destination.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001649 } else {
1650 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
1651 }
1652 } else if (constant->IsLongConstant()) {
1653 int64_t value = constant->AsLongConstant()->GetValue();
1654 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001655 __ movq(destination.As<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001656 } else {
1657 __ movq(CpuRegister(TMP), Immediate(value));
1658 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
1659 }
1660 } else {
1661 LOG(FATAL) << "Unimplemented constant type";
1662 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001663 } else {
1664 LOG(FATAL) << "Unimplemented";
1665 }
1666}
1667
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001668void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001669 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001670 __ movl(Address(CpuRegister(RSP), mem), reg);
1671 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001672}
1673
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001674void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001675 ScratchRegisterScope ensure_scratch(
1676 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
1677
1678 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
1679 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
1680 __ movl(CpuRegister(ensure_scratch.GetRegister()),
1681 Address(CpuRegister(RSP), mem2 + stack_offset));
1682 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
1683 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
1684 CpuRegister(ensure_scratch.GetRegister()));
1685}
1686
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001687void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
1688 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
1689 __ movq(Address(CpuRegister(RSP), mem), reg);
1690 __ movq(reg, CpuRegister(TMP));
1691}
1692
1693void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
1694 ScratchRegisterScope ensure_scratch(
1695 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
1696
1697 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
1698 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
1699 __ movq(CpuRegister(ensure_scratch.GetRegister()),
1700 Address(CpuRegister(RSP), mem2 + stack_offset));
1701 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
1702 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
1703 CpuRegister(ensure_scratch.GetRegister()));
1704}
1705
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001706void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
1707 MoveOperands* move = moves_.Get(index);
1708 Location source = move->GetSource();
1709 Location destination = move->GetDestination();
1710
1711 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001712 __ xchgq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001713 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001714 Exchange32(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001715 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001716 Exchange32(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001717 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001718 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
1719 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001720 Exchange64(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001721 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001722 Exchange64(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001723 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
1724 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001725 } else {
1726 LOG(FATAL) << "Unimplemented";
1727 }
1728}
1729
1730
1731void ParallelMoveResolverX86_64::SpillScratch(int reg) {
1732 __ pushq(CpuRegister(reg));
1733}
1734
1735
1736void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
1737 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001738}
1739
1740} // namespace x86_64
1741} // namespace art