blob: e8d34e38883c7caf6909e2cf706240b4891db1c2 [file] [log] [blame]
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86_64.h"
18
19#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010020#include "gc/accounting/card_table.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070021#include "mirror/array-inl.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010022#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010023#include "mirror/class.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010024#include "mirror/object_reference.h"
25#include "thread.h"
26#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010027#include "utils/stack_checks.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010028#include "utils/x86_64/assembler_x86_64.h"
29#include "utils/x86_64/managed_register_x86_64.h"
30
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010031namespace art {
32
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010033namespace x86_64 {
34
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010035static constexpr bool kExplicitStackOverflowCheck = false;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010036
37// Some x86_64 instructions require a register to be available as temp.
38static constexpr Register TMP = R11;
39
40static constexpr int kNumberOfPushedRegistersAtEntry = 1;
41static constexpr int kCurrentMethodStackOffset = 0;
42
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010043static constexpr Register kRuntimeParameterCoreRegisters[] = { RDI, RSI, RDX };
44static constexpr size_t kRuntimeParameterCoreRegistersLength =
45 arraysize(kRuntimeParameterCoreRegisters);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010046static constexpr FloatRegister kRuntimeParameterFpuRegisters[] = { };
47static constexpr size_t kRuntimeParameterFpuRegistersLength = 0;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010048
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010049class InvokeRuntimeCallingConvention : public CallingConvention<Register, FloatRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010050 public:
51 InvokeRuntimeCallingConvention()
52 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010053 kRuntimeParameterCoreRegistersLength,
54 kRuntimeParameterFpuRegisters,
55 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010056
57 private:
58 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
59};
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010060
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061#define __ reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler())->
62
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010063class SlowPathCodeX86_64 : public SlowPathCode {
64 public:
65 SlowPathCodeX86_64() : entry_label_(), exit_label_() {}
66
67 Label* GetEntryLabel() { return &entry_label_; }
68 Label* GetExitLabel() { return &exit_label_; }
69
70 private:
71 Label entry_label_;
72 Label exit_label_;
73
74 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86_64);
75};
76
77class NullCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010078 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010079 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080
81 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
82 __ Bind(GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010083 __ gs()->call(
84 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowNullPointer), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +010085 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010086 }
87
88 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010089 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010090 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
91};
92
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010093class StackOverflowCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010094 public:
95 StackOverflowCheckSlowPathX86_64() {}
96
97 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
98 __ Bind(GetEntryLabel());
99 __ addq(CpuRegister(RSP),
100 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
101 __ gs()->jmp(
102 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowStackOverflow), true));
103 }
104
105 private:
106 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86_64);
107};
108
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100109class SuspendCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000110 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100111 explicit SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
112 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000113
114 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100115 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000116 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100117 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000118 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pTestSuspend), true));
119 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100120 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100121 if (successor_ == nullptr) {
122 __ jmp(GetReturnLabel());
123 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100124 __ jmp(x64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100125 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000126 }
127
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100128 Label* GetReturnLabel() {
129 DCHECK(successor_ == nullptr);
130 return &return_label_;
131 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000132
133 private:
134 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100135 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000136 Label return_label_;
137
138 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
139};
140
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100141class BoundsCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100142 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100143 BoundsCheckSlowPathX86_64(HBoundsCheck* instruction,
144 Location index_location,
145 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100146 : instruction_(instruction),
147 index_location_(index_location),
148 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100149
150 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100151 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100152 __ Bind(GetEntryLabel());
153 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100154 x64_codegen->Move(
155 Location::RegisterLocation(calling_convention.GetRegisterAt(0)), index_location_);
156 x64_codegen->Move(
157 Location::RegisterLocation(calling_convention.GetRegisterAt(1)), length_location_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100158 __ gs()->call(Address::Absolute(
159 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowArrayBounds), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100160 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100161 }
162
163 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100164 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100165 const Location index_location_;
166 const Location length_location_;
167
168 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
169};
170
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100171class ClinitCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
172 public:
173 explicit ClinitCheckSlowPathX86_64(HClinitCheck* instruction) : instruction_(instruction) {}
174
175 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
176 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
177 __ Bind(GetEntryLabel());
178 codegen->SaveLiveRegisters(instruction_->GetLocations());
179
180 HLoadClass* cls = instruction_->GetLoadClass();
181 InvokeRuntimeCallingConvention calling_convention;
182 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls->GetTypeIndex()));
183 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
184 __ gs()->call(Address::Absolute(
185 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeStaticStorage), true));
186
187 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
188 x64_codegen->Move(instruction_->GetLocations()->InAt(0), Location::RegisterLocation(RAX));
189 codegen->RestoreLiveRegisters(instruction_->GetLocations());
190 __ jmp(GetExitLabel());
191 }
192
193 private:
194 HClinitCheck* const instruction_;
195
196 DISALLOW_COPY_AND_ASSIGN(ClinitCheckSlowPathX86_64);
197};
198
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100199#undef __
200#define __ reinterpret_cast<X86_64Assembler*>(GetAssembler())->
201
Dave Allison20dfc792014-06-16 20:44:29 -0700202inline Condition X86_64Condition(IfCondition cond) {
203 switch (cond) {
204 case kCondEQ: return kEqual;
205 case kCondNE: return kNotEqual;
206 case kCondLT: return kLess;
207 case kCondLE: return kLessEqual;
208 case kCondGT: return kGreater;
209 case kCondGE: return kGreaterEqual;
210 default:
211 LOG(FATAL) << "Unknown if condition";
212 }
213 return kEqual;
214}
215
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100216void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
217 stream << X86_64ManagedRegister::FromCpuRegister(Register(reg));
218}
219
220void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
221 stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg));
222}
223
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100224size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
225 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
226 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100227}
228
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100229size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
230 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
231 return kX86_64WordSize;
232}
233
234size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
235 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
236 return kX86_64WordSize;
237}
238
239size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
240 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
241 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100242}
243
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100244CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100245 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfFloatRegisters, 0),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100246 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100247 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000248 instruction_visitor_(graph, this),
249 move_resolver_(graph->GetArena(), this) {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100250
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100251size_t CodeGeneratorX86_64::FrameEntrySpillSize() const {
252 return kNumberOfPushedRegistersAtEntry * kX86_64WordSize;
253}
254
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100255InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
256 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100257 : HGraphVisitor(graph),
258 assembler_(codegen->GetAssembler()),
259 codegen_(codegen) {}
260
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100261Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100262 switch (type) {
263 case Primitive::kPrimLong:
264 case Primitive::kPrimByte:
265 case Primitive::kPrimBoolean:
266 case Primitive::kPrimChar:
267 case Primitive::kPrimShort:
268 case Primitive::kPrimInt:
269 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100270 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100271 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100272 }
273
274 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100275 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100276 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100277 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100278 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100279
280 case Primitive::kPrimVoid:
281 LOG(FATAL) << "Unreachable type " << type;
282 }
283
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100284 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100285}
286
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100287void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100288 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100289 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100290
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000291 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100292 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000293
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100294 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100295 blocked_core_registers_[RBX] = true;
296 blocked_core_registers_[RBP] = true;
297 blocked_core_registers_[R12] = true;
298 blocked_core_registers_[R13] = true;
299 blocked_core_registers_[R14] = true;
300 blocked_core_registers_[R15] = true;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100301
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100302 blocked_fpu_registers_[XMM12] = true;
303 blocked_fpu_registers_[XMM13] = true;
304 blocked_fpu_registers_[XMM14] = true;
305 blocked_fpu_registers_[XMM15] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100306}
307
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100308void CodeGeneratorX86_64::GenerateFrameEntry() {
309 // Create a fake register to mimic Quick.
310 static const int kFakeReturnRegister = 16;
311 core_spill_mask_ |= (1 << kFakeReturnRegister);
312
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100313 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700314 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100315
316 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
317 __ testq(CpuRegister(RAX), Address(
318 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100319 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100320 }
321
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100322 // The return PC has already been pushed on the stack.
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100323 __ subq(CpuRegister(RSP),
324 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
325
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100326 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100327 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86_64();
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100328 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100329
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100330 __ gs()->cmpq(CpuRegister(RSP),
331 Address::Absolute(Thread::StackEndOffset<kX86_64WordSize>(), true));
332 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100333 }
334
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100335 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
336}
337
338void CodeGeneratorX86_64::GenerateFrameExit() {
339 __ addq(CpuRegister(RSP),
340 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
341}
342
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100343void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
344 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100345}
346
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100347void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100348 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
349}
350
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100351Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
352 switch (load->GetType()) {
353 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100354 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100355 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
356 break;
357
358 case Primitive::kPrimInt:
359 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100360 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100361 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100362
363 case Primitive::kPrimBoolean:
364 case Primitive::kPrimByte:
365 case Primitive::kPrimChar:
366 case Primitive::kPrimShort:
367 case Primitive::kPrimVoid:
368 LOG(FATAL) << "Unexpected type " << load->GetType();
369 }
370
371 LOG(FATAL) << "Unreachable";
372 return Location();
373}
374
375void CodeGeneratorX86_64::Move(Location destination, Location source) {
376 if (source.Equals(destination)) {
377 return;
378 }
379 if (destination.IsRegister()) {
380 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100381 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100382 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100383 __ movd(destination.As<CpuRegister>(), source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100384 } else if (source.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100385 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100386 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100387 } else {
388 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100389 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100390 Address(CpuRegister(RSP), source.GetStackIndex()));
391 }
392 } else if (destination.IsFpuRegister()) {
393 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100394 __ movd(destination.As<XmmRegister>(), source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100395 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100396 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100397 } else if (source.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100398 __ movss(destination.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100399 Address(CpuRegister(RSP), source.GetStackIndex()));
400 } else {
401 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100402 __ movsd(destination.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100403 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100404 }
405 } else if (destination.IsStackSlot()) {
406 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100407 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100408 source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100409 } else if (source.IsFpuRegister()) {
410 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100411 source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100412 } else {
413 DCHECK(source.IsStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000414 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
415 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100416 }
417 } else {
418 DCHECK(destination.IsDoubleStackSlot());
419 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100420 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100421 source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100422 } else if (source.IsFpuRegister()) {
423 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100424 source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100425 } else {
426 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000427 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
428 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100429 }
430 }
431}
432
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100433void CodeGeneratorX86_64::Move(HInstruction* instruction,
434 Location location,
435 HInstruction* move_for) {
Roland Levillain476df552014-10-09 17:51:36 +0100436 if (instruction->IsIntConstant()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100437 Immediate imm(instruction->AsIntConstant()->GetValue());
438 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100439 __ movl(location.As<CpuRegister>(), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100440 } else if (location.IsStackSlot()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100441 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100442 } else {
443 DCHECK(location.IsConstant());
444 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100445 }
Roland Levillain476df552014-10-09 17:51:36 +0100446 } else if (instruction->IsLongConstant()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100447 int64_t value = instruction->AsLongConstant()->GetValue();
448 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100449 __ movq(location.As<CpuRegister>(), Immediate(value));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100450 } else if (location.IsDoubleStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000451 __ movq(CpuRegister(TMP), Immediate(value));
452 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100453 } else {
454 DCHECK(location.IsConstant());
455 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100456 }
Roland Levillain476df552014-10-09 17:51:36 +0100457 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100458 switch (instruction->GetType()) {
459 case Primitive::kPrimBoolean:
460 case Primitive::kPrimByte:
461 case Primitive::kPrimChar:
462 case Primitive::kPrimShort:
463 case Primitive::kPrimInt:
464 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100465 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100466 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
467 break;
468
469 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100470 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100471 Move(location, Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
472 break;
473
474 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100475 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100476 }
477 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100478 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100479 switch (instruction->GetType()) {
480 case Primitive::kPrimBoolean:
481 case Primitive::kPrimByte:
482 case Primitive::kPrimChar:
483 case Primitive::kPrimShort:
484 case Primitive::kPrimInt:
485 case Primitive::kPrimNot:
486 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100487 case Primitive::kPrimFloat:
488 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100489 Move(location, instruction->GetLocations()->Out());
490 break;
491
492 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100493 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100494 }
495 }
496}
497
498void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
499 got->SetLocations(nullptr);
500}
501
502void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
503 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100504 DCHECK(!successor->IsExitBlock());
505
506 HBasicBlock* block = got->GetBlock();
507 HInstruction* previous = got->GetPrevious();
508
509 HLoopInformation* info = block->GetLoopInformation();
510 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
511 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
512 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
513 return;
514 }
515
516 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
517 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
518 }
519 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100520 __ jmp(codegen_->GetLabelOf(successor));
521 }
522}
523
524void LocationsBuilderX86_64::VisitExit(HExit* exit) {
525 exit->SetLocations(nullptr);
526}
527
528void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
529 if (kIsDebugBuild) {
530 __ Comment("Unreachable");
531 __ int3();
532 }
533}
534
535void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100536 LocationSummary* locations =
537 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100538 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100539 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100540 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100541 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100542}
543
544void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700545 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100546 if (cond->IsIntConstant()) {
547 // Constant condition, statically compared against 1.
548 int32_t cond_value = cond->AsIntConstant()->GetValue();
549 if (cond_value == 1) {
550 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
551 if_instr->IfTrueSuccessor())) {
552 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100553 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100554 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100555 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100556 DCHECK_EQ(cond_value, 0);
557 }
558 } else {
559 bool materialized =
560 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
561 // Moves do not affect the eflags register, so if the condition is
562 // evaluated just before the if, we don't need to evaluate it
563 // again.
564 bool eflags_set = cond->IsCondition()
565 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
566 if (materialized) {
567 if (!eflags_set) {
568 // Materialized condition, compare against 0.
569 Location lhs = if_instr->GetLocations()->InAt(0);
570 if (lhs.IsRegister()) {
571 __ cmpl(lhs.As<CpuRegister>(), Immediate(0));
572 } else {
573 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
574 Immediate(0));
575 }
576 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
577 } else {
578 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
579 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
580 }
581 } else {
582 Location lhs = cond->GetLocations()->InAt(0);
583 Location rhs = cond->GetLocations()->InAt(1);
584 if (rhs.IsRegister()) {
585 __ cmpl(lhs.As<CpuRegister>(), rhs.As<CpuRegister>());
586 } else if (rhs.IsConstant()) {
587 __ cmpl(lhs.As<CpuRegister>(),
588 Immediate(rhs.GetConstant()->AsIntConstant()->GetValue()));
589 } else {
590 __ cmpl(lhs.As<CpuRegister>(),
591 Address(CpuRegister(RSP), rhs.GetStackIndex()));
592 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100593 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
594 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700595 }
Dave Allison20dfc792014-06-16 20:44:29 -0700596 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100597 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
598 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700599 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100600 }
601}
602
603void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
604 local->SetLocations(nullptr);
605}
606
607void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
608 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
609}
610
611void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
612 local->SetLocations(nullptr);
613}
614
615void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
616 // Nothing to do, this is driven by the code generator.
617}
618
619void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100620 LocationSummary* locations =
621 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100622 switch (store->InputAt(1)->GetType()) {
623 case Primitive::kPrimBoolean:
624 case Primitive::kPrimByte:
625 case Primitive::kPrimChar:
626 case Primitive::kPrimShort:
627 case Primitive::kPrimInt:
628 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100629 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100630 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
631 break;
632
633 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100634 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100635 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
636 break;
637
638 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100639 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100640 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100641}
642
643void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
644}
645
Dave Allison20dfc792014-06-16 20:44:29 -0700646void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100647 LocationSummary* locations =
648 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100649 locations->SetInAt(0, Location::RequiresRegister());
650 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100651 if (comp->NeedsMaterialization()) {
652 locations->SetOut(Location::RequiresRegister());
653 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100654}
655
Dave Allison20dfc792014-06-16 20:44:29 -0700656void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
657 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100658 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100659 CpuRegister reg = locations->Out().As<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100660 // Clear register: setcc only sets the low byte.
661 __ xorq(reg, reg);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100662 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100663 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100664 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100665 } else if (locations->InAt(1).IsConstant()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100666 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100667 Immediate(locations->InAt(1).GetConstant()->AsIntConstant()->GetValue()));
668 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100669 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100670 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
671 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100672 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700673 }
674}
675
676void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
677 VisitCondition(comp);
678}
679
680void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
681 VisitCondition(comp);
682}
683
684void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
685 VisitCondition(comp);
686}
687
688void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
689 VisitCondition(comp);
690}
691
692void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
693 VisitCondition(comp);
694}
695
696void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
697 VisitCondition(comp);
698}
699
700void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
701 VisitCondition(comp);
702}
703
704void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
705 VisitCondition(comp);
706}
707
708void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
709 VisitCondition(comp);
710}
711
712void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
713 VisitCondition(comp);
714}
715
716void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
717 VisitCondition(comp);
718}
719
720void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
721 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100722}
723
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100724void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100725 LocationSummary* locations =
726 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100727 locations->SetInAt(0, Location::RequiresRegister());
728 locations->SetInAt(1, Location::RequiresRegister());
729 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100730}
731
732void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
733 Label greater, done;
734 LocationSummary* locations = compare->GetLocations();
735 switch (compare->InputAt(0)->GetType()) {
736 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100737 __ cmpq(locations->InAt(0).As<CpuRegister>(),
738 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100739 break;
740 default:
741 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
742 }
743
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100744 CpuRegister output = locations->Out().As<CpuRegister>();
745 __ movl(output, Immediate(0));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100746 __ j(kEqual, &done);
747 __ j(kGreater, &greater);
748
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100749 __ movl(output, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100750 __ jmp(&done);
751
752 __ Bind(&greater);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100753 __ movl(output, Immediate(1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100754
755 __ Bind(&done);
756}
757
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100758void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100759 LocationSummary* locations =
760 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100761 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100762}
763
764void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100765 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100766}
767
768void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100769 LocationSummary* locations =
770 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100771 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100772}
773
774void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100775 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100776}
777
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100778void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
779 LocationSummary* locations =
780 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
781 locations->SetOut(Location::ConstantLocation(constant));
782}
783
784void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
785 // Will be generated at use site.
786}
787
788void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
789 LocationSummary* locations =
790 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
791 locations->SetOut(Location::ConstantLocation(constant));
792}
793
794void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
795 // Will be generated at use site.
796}
797
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100798void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
799 ret->SetLocations(nullptr);
800}
801
802void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
803 codegen_->GenerateFrameExit();
804 __ ret();
805}
806
807void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100808 LocationSummary* locations =
809 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100810 switch (ret->InputAt(0)->GetType()) {
811 case Primitive::kPrimBoolean:
812 case Primitive::kPrimByte:
813 case Primitive::kPrimChar:
814 case Primitive::kPrimShort:
815 case Primitive::kPrimInt:
816 case Primitive::kPrimNot:
817 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100818 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100819 break;
820
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100821 case Primitive::kPrimFloat:
822 case Primitive::kPrimDouble:
823 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100824 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100825 break;
826
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100827 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100828 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100829 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100830}
831
832void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
833 if (kIsDebugBuild) {
834 switch (ret->InputAt(0)->GetType()) {
835 case Primitive::kPrimBoolean:
836 case Primitive::kPrimByte:
837 case Primitive::kPrimChar:
838 case Primitive::kPrimShort:
839 case Primitive::kPrimInt:
840 case Primitive::kPrimNot:
841 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100842 DCHECK_EQ(ret->GetLocations()->InAt(0).As<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100843 break;
844
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100845 case Primitive::kPrimFloat:
846 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100847 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100848 XMM0);
849 break;
850
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100851 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100852 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100853 }
854 }
855 codegen_->GenerateFrameExit();
856 __ ret();
857}
858
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100859Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
860 switch (type) {
861 case Primitive::kPrimBoolean:
862 case Primitive::kPrimByte:
863 case Primitive::kPrimChar:
864 case Primitive::kPrimShort:
865 case Primitive::kPrimInt:
866 case Primitive::kPrimNot: {
867 uint32_t index = gp_index_++;
868 stack_index_++;
869 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100870 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100871 } else {
872 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
873 }
874 }
875
876 case Primitive::kPrimLong: {
877 uint32_t index = gp_index_;
878 stack_index_ += 2;
879 if (index < calling_convention.GetNumberOfRegisters()) {
880 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100881 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100882 } else {
883 gp_index_ += 2;
884 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
885 }
886 }
887
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100888 case Primitive::kPrimFloat: {
889 uint32_t index = fp_index_++;
890 stack_index_++;
891 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100892 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100893 } else {
894 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
895 }
896 }
897
898 case Primitive::kPrimDouble: {
899 uint32_t index = fp_index_++;
900 stack_index_ += 2;
901 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100902 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100903 } else {
904 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
905 }
906 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100907
908 case Primitive::kPrimVoid:
909 LOG(FATAL) << "Unexpected parameter type " << type;
910 break;
911 }
912 return Location();
913}
914
915void LocationsBuilderX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100916 HandleInvoke(invoke);
917}
918
919void InstructionCodeGeneratorX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100920 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100921 // TODO: Implement all kinds of calls:
922 // 1) boot -> boot
923 // 2) app -> boot
924 // 3) app -> app
925 //
926 // Currently we implement the app -> app logic, which looks up in the resolve cache.
927
928 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100929 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100930 // temp = temp->dex_cache_resolved_methods_;
931 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
932 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100933 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100934 // (temp + offset_of_quick_compiled_code)()
935 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
936
937 DCHECK(!codegen_->IsLeafMethod());
938 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
939}
940
941void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
942 HandleInvoke(invoke);
943}
944
945void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100946 LocationSummary* locations =
947 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100948 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100949
950 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100951 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100952 HInstruction* input = invoke->InputAt(i);
953 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
954 }
955
956 switch (invoke->GetType()) {
957 case Primitive::kPrimBoolean:
958 case Primitive::kPrimByte:
959 case Primitive::kPrimChar:
960 case Primitive::kPrimShort:
961 case Primitive::kPrimInt:
962 case Primitive::kPrimNot:
963 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100964 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100965 break;
966
967 case Primitive::kPrimVoid:
968 break;
969
970 case Primitive::kPrimDouble:
971 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100972 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100973 break;
974 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100975}
976
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100977void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100978 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100979 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
980 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
981 LocationSummary* locations = invoke->GetLocations();
982 Location receiver = locations->InAt(0);
983 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
984 // temp = object->GetClass();
985 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100986 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
987 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100988 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100989 __ movl(temp, Address(receiver.As<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100990 }
991 // temp = temp->GetMethodAt(method_offset);
992 __ movl(temp, Address(temp, method_offset));
993 // call temp->GetEntryPoint();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100994 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
995
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100996 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100997 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100998}
999
Roland Levillain88cb1752014-10-20 16:36:47 +01001000void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1001 LocationSummary* locations =
1002 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1003 switch (neg->GetResultType()) {
1004 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001005 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001006 locations->SetInAt(0, Location::RequiresRegister());
1007 locations->SetOut(Location::SameAsFirstInput());
1008 break;
1009
Roland Levillain88cb1752014-10-20 16:36:47 +01001010 case Primitive::kPrimFloat:
1011 case Primitive::kPrimDouble:
1012 LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
1013 break;
1014
1015 default:
1016 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1017 }
1018}
1019
1020void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1021 LocationSummary* locations = neg->GetLocations();
1022 Location out = locations->Out();
1023 Location in = locations->InAt(0);
1024 switch (neg->GetResultType()) {
1025 case Primitive::kPrimInt:
1026 DCHECK(in.IsRegister());
1027 __ negl(out.As<CpuRegister>());
1028 break;
1029
1030 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001031 DCHECK(in.IsRegister());
1032 __ negq(out.As<CpuRegister>());
1033 break;
1034
Roland Levillain88cb1752014-10-20 16:36:47 +01001035 case Primitive::kPrimFloat:
1036 case Primitive::kPrimDouble:
1037 LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
1038 break;
1039
1040 default:
1041 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1042 }
1043}
1044
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001045void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001046 LocationSummary* locations =
1047 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001048 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001049 case Primitive::kPrimInt: {
1050 locations->SetInAt(0, Location::RequiresRegister());
1051 locations->SetInAt(1, Location::Any());
1052 locations->SetOut(Location::SameAsFirstInput());
1053 break;
1054 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001055
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001056 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001057 locations->SetInAt(0, Location::RequiresRegister());
1058 locations->SetInAt(1, Location::RequiresRegister());
1059 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001060 break;
1061 }
1062
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001063 case Primitive::kPrimDouble:
1064 case Primitive::kPrimFloat: {
1065 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001066 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001067 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001068 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001069 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001070
1071 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001072 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001073 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001074}
1075
1076void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1077 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001078 Location first = locations->InAt(0);
1079 Location second = locations->InAt(1);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001080 DCHECK(first.Equals(locations->Out()));
Calin Juravle11351682014-10-23 15:38:15 +01001081
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001082 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001083 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001084 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001085 __ addl(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001086 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001087 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001088 __ addl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001089 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001090 __ addl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001091 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001092 break;
1093 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001094
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001095 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001096 __ addq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001097 break;
1098 }
1099
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001100 case Primitive::kPrimFloat: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001101 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001102 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001103 }
1104
1105 case Primitive::kPrimDouble: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001106 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001107 break;
1108 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001109
1110 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001111 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001112 }
1113}
1114
1115void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001116 LocationSummary* locations =
1117 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001118 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001119 case Primitive::kPrimInt: {
1120 locations->SetInAt(0, Location::RequiresRegister());
1121 locations->SetInAt(1, Location::Any());
1122 locations->SetOut(Location::SameAsFirstInput());
1123 break;
1124 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001125 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001126 locations->SetInAt(0, Location::RequiresRegister());
1127 locations->SetInAt(1, Location::RequiresRegister());
1128 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001129 break;
1130 }
Calin Juravle11351682014-10-23 15:38:15 +01001131 case Primitive::kPrimFloat:
1132 case Primitive::kPrimDouble: {
1133 locations->SetInAt(0, Location::RequiresFpuRegister());
1134 locations->SetInAt(1, Location::RequiresFpuRegister());
1135 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001136 break;
Calin Juravle11351682014-10-23 15:38:15 +01001137 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001138 default:
Calin Juravle11351682014-10-23 15:38:15 +01001139 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001140 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001141}
1142
1143void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1144 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001145 Location first = locations->InAt(0);
1146 Location second = locations->InAt(1);
1147 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001148 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001149 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001150 if (second.IsRegister()) {
1151 __ subl(first.As<CpuRegister>(), second.As<CpuRegister>());
1152 } else if (second.IsConstant()) {
1153 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1154 __ subl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001155 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001156 __ subl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001157 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001158 break;
1159 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001160 case Primitive::kPrimLong: {
Calin Juravle11351682014-10-23 15:38:15 +01001161 __ subq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001162 break;
1163 }
1164
Calin Juravle11351682014-10-23 15:38:15 +01001165 case Primitive::kPrimFloat: {
1166 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001167 break;
Calin Juravle11351682014-10-23 15:38:15 +01001168 }
1169
1170 case Primitive::kPrimDouble: {
1171 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1172 break;
1173 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001174
1175 default:
Calin Juravle11351682014-10-23 15:38:15 +01001176 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001177 }
1178}
1179
Calin Juravle34bacdf2014-10-07 20:23:36 +01001180void LocationsBuilderX86_64::VisitMul(HMul* mul) {
1181 LocationSummary* locations =
1182 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1183 switch (mul->GetResultType()) {
1184 case Primitive::kPrimInt: {
1185 locations->SetInAt(0, Location::RequiresRegister());
1186 locations->SetInAt(1, Location::Any());
1187 locations->SetOut(Location::SameAsFirstInput());
1188 break;
1189 }
1190 case Primitive::kPrimLong: {
1191 locations->SetInAt(0, Location::RequiresRegister());
1192 locations->SetInAt(1, Location::RequiresRegister());
1193 locations->SetOut(Location::SameAsFirstInput());
1194 break;
1195 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001196 case Primitive::kPrimFloat:
1197 case Primitive::kPrimDouble: {
1198 locations->SetInAt(0, Location::RequiresFpuRegister());
1199 locations->SetInAt(1, Location::RequiresFpuRegister());
1200 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001201 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001202 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001203
1204 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001205 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001206 }
1207}
1208
1209void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
1210 LocationSummary* locations = mul->GetLocations();
1211 Location first = locations->InAt(0);
1212 Location second = locations->InAt(1);
1213 DCHECK(first.Equals(locations->Out()));
1214 switch (mul->GetResultType()) {
1215 case Primitive::kPrimInt: {
1216 if (second.IsRegister()) {
1217 __ imull(first.As<CpuRegister>(), second.As<CpuRegister>());
1218 } else if (second.IsConstant()) {
1219 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1220 __ imull(first.As<CpuRegister>(), imm);
1221 } else {
1222 DCHECK(second.IsStackSlot());
1223 __ imull(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
1224 }
1225 break;
1226 }
1227 case Primitive::kPrimLong: {
1228 __ imulq(first.As<CpuRegister>(), second.As<CpuRegister>());
1229 break;
1230 }
1231
Calin Juravleb5bfa962014-10-21 18:02:24 +01001232 case Primitive::kPrimFloat: {
1233 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001234 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001235 }
1236
1237 case Primitive::kPrimDouble: {
1238 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1239 break;
1240 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001241
1242 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001243 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001244 }
1245}
1246
Calin Juravle7c4954d2014-10-28 16:57:40 +00001247void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
1248 LocationSummary* locations =
1249 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1250 switch (div->GetResultType()) {
1251 case Primitive::kPrimInt:
1252 case Primitive::kPrimLong: {
1253 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1254 break;
1255 }
1256 case Primitive::kPrimFloat:
1257 case Primitive::kPrimDouble: {
1258 locations->SetInAt(0, Location::RequiresFpuRegister());
1259 locations->SetInAt(1, Location::RequiresFpuRegister());
1260 locations->SetOut(Location::SameAsFirstInput());
1261 break;
1262 }
1263
1264 default:
1265 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1266 }
1267}
1268
1269void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
1270 LocationSummary* locations = div->GetLocations();
1271 Location first = locations->InAt(0);
1272 Location second = locations->InAt(1);
1273 DCHECK(first.Equals(locations->Out()));
1274
1275 switch (div->GetResultType()) {
1276 case Primitive::kPrimInt:
1277 case Primitive::kPrimLong: {
1278 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1279 break;
1280 }
1281
1282 case Primitive::kPrimFloat: {
1283 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1284 break;
1285 }
1286
1287 case Primitive::kPrimDouble: {
1288 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1289 break;
1290 }
1291
1292 default:
1293 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1294 }
1295}
1296
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001297void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001298 LocationSummary* locations =
1299 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001300 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001301 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1302 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1303 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001304}
1305
1306void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
1307 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001308 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001309 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1310
1311 __ gs()->call(Address::Absolute(
1312 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
1313
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001314 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001315 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001316}
1317
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001318void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
1319 LocationSummary* locations =
1320 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1321 InvokeRuntimeCallingConvention calling_convention;
1322 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1323 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1324 locations->SetOut(Location::RegisterLocation(RAX));
1325 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1326}
1327
1328void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
1329 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001330 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001331 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1332
1333 __ gs()->call(Address::Absolute(
1334 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocArrayWithAccessCheck), true));
1335
1336 DCHECK(!codegen_->IsLeafMethod());
1337 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1338}
1339
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001340void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001341 LocationSummary* locations =
1342 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001343 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1344 if (location.IsStackSlot()) {
1345 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1346 } else if (location.IsDoubleStackSlot()) {
1347 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1348 }
1349 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001350}
1351
1352void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
1353 // Nothing to do, the parameter is already at its location.
1354}
1355
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001356void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001357 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001358 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001359 locations->SetInAt(0, Location::RequiresRegister());
1360 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001361}
1362
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001363void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
1364 LocationSummary* locations = not_->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001365 DCHECK_EQ(locations->InAt(0).As<CpuRegister>().AsRegister(),
1366 locations->Out().As<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001367 Location out = locations->Out();
1368 switch (not_->InputAt(0)->GetType()) {
1369 case Primitive::kPrimBoolean:
1370 __ xorq(out.As<CpuRegister>(), Immediate(1));
1371 break;
1372
1373 case Primitive::kPrimInt:
1374 __ notl(out.As<CpuRegister>());
1375 break;
1376
1377 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001378 __ notq(out.As<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001379 break;
1380
1381 default:
1382 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1383 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001384}
1385
1386void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001387 LocationSummary* locations =
1388 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001389 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1390 locations->SetInAt(i, Location::Any());
1391 }
1392 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001393}
1394
1395void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
1396 LOG(FATAL) << "Unimplemented";
1397}
1398
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001399void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001400 LocationSummary* locations =
1401 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001402 Primitive::Type field_type = instruction->GetFieldType();
1403 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001404 locations->SetInAt(0, Location::RequiresRegister());
1405 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001406 if (is_object_type) {
1407 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001408 locations->AddTemp(Location::RequiresRegister());
1409 locations->AddTemp(Location::RequiresRegister());
1410 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001411}
1412
1413void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1414 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001415 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
1416 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001417 size_t offset = instruction->GetFieldOffset().SizeValue();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001418 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001419
1420 switch (field_type) {
1421 case Primitive::kPrimBoolean:
1422 case Primitive::kPrimByte: {
1423 __ movb(Address(obj, offset), value);
1424 break;
1425 }
1426
1427 case Primitive::kPrimShort:
1428 case Primitive::kPrimChar: {
1429 __ movw(Address(obj, offset), value);
1430 break;
1431 }
1432
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001433 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001434 case Primitive::kPrimNot: {
1435 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001436 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001437 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
1438 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001439 codegen_->MarkGCCard(temp, card, obj, value);
1440 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001441 break;
1442 }
1443
1444 case Primitive::kPrimLong: {
1445 __ movq(Address(obj, offset), value);
1446 break;
1447 }
1448
1449 case Primitive::kPrimFloat:
1450 case Primitive::kPrimDouble:
1451 LOG(FATAL) << "Unimplemented register type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001452 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001453 case Primitive::kPrimVoid:
1454 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001455 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001456 }
1457}
1458
1459void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001460 LocationSummary* locations =
1461 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001462 locations->SetInAt(0, Location::RequiresRegister());
1463 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001464}
1465
1466void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1467 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001468 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
1469 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001470 size_t offset = instruction->GetFieldOffset().SizeValue();
1471
1472 switch (instruction->GetType()) {
1473 case Primitive::kPrimBoolean: {
1474 __ movzxb(out, Address(obj, offset));
1475 break;
1476 }
1477
1478 case Primitive::kPrimByte: {
1479 __ movsxb(out, Address(obj, offset));
1480 break;
1481 }
1482
1483 case Primitive::kPrimShort: {
1484 __ movsxw(out, Address(obj, offset));
1485 break;
1486 }
1487
1488 case Primitive::kPrimChar: {
1489 __ movzxw(out, Address(obj, offset));
1490 break;
1491 }
1492
1493 case Primitive::kPrimInt:
1494 case Primitive::kPrimNot: {
1495 __ movl(out, Address(obj, offset));
1496 break;
1497 }
1498
1499 case Primitive::kPrimLong: {
1500 __ movq(out, Address(obj, offset));
1501 break;
1502 }
1503
1504 case Primitive::kPrimFloat:
1505 case Primitive::kPrimDouble:
1506 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001507 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001508 case Primitive::kPrimVoid:
1509 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001510 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001511 }
1512}
1513
1514void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001515 LocationSummary* locations =
1516 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001517 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001518 if (instruction->HasUses()) {
1519 locations->SetOut(Location::SameAsFirstInput());
1520 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001521}
1522
1523void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001524 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001525 codegen_->AddSlowPath(slow_path);
1526
1527 LocationSummary* locations = instruction->GetLocations();
1528 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001529
1530 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001531 __ cmpl(obj.As<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001532 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001533 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001534 } else {
1535 DCHECK(obj.IsConstant()) << obj;
1536 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1537 __ jmp(slow_path->GetEntryLabel());
1538 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001539 }
1540 __ j(kEqual, slow_path->GetEntryLabel());
1541}
1542
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001543void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001544 LocationSummary* locations =
1545 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001546 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001547 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001548 1, Location::RegisterOrConstant(instruction->InputAt(1)));
1549 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001550}
1551
1552void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
1553 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001554 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001555 Location index = locations->InAt(1);
1556
1557 switch (instruction->GetType()) {
1558 case Primitive::kPrimBoolean: {
1559 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001560 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001561 if (index.IsConstant()) {
1562 __ movzxb(out, Address(obj,
1563 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1564 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001565 __ movzxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001566 }
1567 break;
1568 }
1569
1570 case Primitive::kPrimByte: {
1571 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001572 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001573 if (index.IsConstant()) {
1574 __ movsxb(out, Address(obj,
1575 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1576 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001577 __ movsxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001578 }
1579 break;
1580 }
1581
1582 case Primitive::kPrimShort: {
1583 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001584 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001585 if (index.IsConstant()) {
1586 __ movsxw(out, Address(obj,
1587 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1588 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001589 __ movsxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001590 }
1591 break;
1592 }
1593
1594 case Primitive::kPrimChar: {
1595 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001596 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001597 if (index.IsConstant()) {
1598 __ movzxw(out, Address(obj,
1599 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1600 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001601 __ movzxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001602 }
1603 break;
1604 }
1605
1606 case Primitive::kPrimInt:
1607 case Primitive::kPrimNot: {
1608 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1609 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001610 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001611 if (index.IsConstant()) {
1612 __ movl(out, Address(obj,
1613 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1614 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001615 __ movl(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001616 }
1617 break;
1618 }
1619
1620 case Primitive::kPrimLong: {
1621 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001622 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001623 if (index.IsConstant()) {
1624 __ movq(out, Address(obj,
1625 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
1626 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001627 __ movq(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001628 }
1629 break;
1630 }
1631
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001632 case Primitive::kPrimFloat: {
1633 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1634 XmmRegister out = locations->Out().As<XmmRegister>();
1635 if (index.IsConstant()) {
1636 __ movss(out, Address(obj,
1637 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1638 } else {
1639 __ movss(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
1640 }
1641 break;
1642 }
1643
1644 case Primitive::kPrimDouble: {
1645 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1646 XmmRegister out = locations->Out().As<XmmRegister>();
1647 if (index.IsConstant()) {
1648 __ movsd(out, Address(obj,
1649 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
1650 } else {
1651 __ movsd(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
1652 }
1653 break;
1654 }
1655
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001656 case Primitive::kPrimVoid:
1657 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001658 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001659 }
1660}
1661
1662void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001663 Primitive::Type value_type = instruction->GetComponentType();
1664 bool is_object = value_type == Primitive::kPrimNot;
1665 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1666 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
1667 if (is_object) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001668 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001669 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1670 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1671 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001672 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001673 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001674 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001675 1, Location::RegisterOrConstant(instruction->InputAt(1)));
1676 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001677 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001678 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001679 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
1680 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001681 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001682 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001683 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001684 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001685}
1686
1687void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
1688 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001689 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001690 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001691 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001692 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001693
1694 switch (value_type) {
1695 case Primitive::kPrimBoolean:
1696 case Primitive::kPrimByte: {
1697 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001698 if (index.IsConstant()) {
1699 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001700 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001701 __ movb(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001702 } else {
1703 __ movb(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1704 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001705 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001706 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001707 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
1708 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001709 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001710 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001711 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1712 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001713 }
1714 break;
1715 }
1716
1717 case Primitive::kPrimShort:
1718 case Primitive::kPrimChar: {
1719 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001720 if (index.IsConstant()) {
1721 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001722 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001723 __ movw(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001724 } else {
1725 __ movw(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1726 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001727 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001728 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001729 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
1730 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001731 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001732 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001733 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1734 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001735 }
1736 break;
1737 }
1738
1739 case Primitive::kPrimInt: {
1740 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001741 if (index.IsConstant()) {
1742 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001743 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001744 __ movl(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001745 } else {
1746 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1747 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001748 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001749 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001750 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
1751 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001752 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001753 DCHECK(value.IsConstant()) << value;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001754 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001755 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1756 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001757 }
1758 break;
1759 }
1760
1761 case Primitive::kPrimNot: {
1762 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject), true));
1763 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001764 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001765 break;
1766 }
1767
1768 case Primitive::kPrimLong: {
1769 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001770 if (index.IsConstant()) {
1771 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001772 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001773 __ movq(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001774 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001775 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001776 __ movq(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
1777 value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001778 }
1779 break;
1780 }
1781
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001782 case Primitive::kPrimFloat: {
1783 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1784 if (index.IsConstant()) {
1785 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1786 DCHECK(value.IsFpuRegister());
1787 __ movss(Address(obj, offset), value.As<XmmRegister>());
1788 } else {
1789 DCHECK(value.IsFpuRegister());
1790 __ movss(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
1791 value.As<XmmRegister>());
1792 }
1793 break;
1794 }
1795
1796 case Primitive::kPrimDouble: {
1797 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1798 if (index.IsConstant()) {
1799 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1800 DCHECK(value.IsFpuRegister());
1801 __ movsd(Address(obj, offset), value.As<XmmRegister>());
1802 } else {
1803 DCHECK(value.IsFpuRegister());
1804 __ movsd(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
1805 value.As<XmmRegister>());
1806 }
1807 break;
1808 }
1809
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001810 case Primitive::kPrimVoid:
1811 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001812 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001813 }
1814}
1815
1816void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001817 LocationSummary* locations =
1818 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001819 locations->SetInAt(0, Location::RequiresRegister());
1820 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001821}
1822
1823void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
1824 LocationSummary* locations = instruction->GetLocations();
1825 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001826 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
1827 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001828 __ movl(out, Address(obj, offset));
1829}
1830
1831void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001832 LocationSummary* locations =
1833 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001834 locations->SetInAt(0, Location::RequiresRegister());
1835 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001836 if (instruction->HasUses()) {
1837 locations->SetOut(Location::SameAsFirstInput());
1838 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001839}
1840
1841void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
1842 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001843 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01001844 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001845 codegen_->AddSlowPath(slow_path);
1846
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001847 CpuRegister index = locations->InAt(0).As<CpuRegister>();
1848 CpuRegister length = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001849
1850 __ cmpl(index, length);
1851 __ j(kAboveEqual, slow_path->GetEntryLabel());
1852}
1853
1854void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
1855 CpuRegister card,
1856 CpuRegister object,
1857 CpuRegister value) {
1858 Label is_null;
1859 __ testl(value, value);
1860 __ j(kEqual, &is_null);
1861 __ gs()->movq(card, Address::Absolute(
1862 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
1863 __ movq(temp, object);
1864 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
1865 __ movb(Address(temp, card, TIMES_1, 0), card);
1866 __ Bind(&is_null);
1867}
1868
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001869void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
1870 temp->SetLocations(nullptr);
1871}
1872
1873void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
1874 // Nothing to do, this is driven by the code generator.
1875}
1876
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001877void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
1878 LOG(FATAL) << "Unimplemented";
1879}
1880
1881void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001882 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
1883}
1884
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001885void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
1886 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
1887}
1888
1889void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001890 HBasicBlock* block = instruction->GetBlock();
1891 if (block->GetLoopInformation() != nullptr) {
1892 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
1893 // The back edge will generate the suspend check.
1894 return;
1895 }
1896 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
1897 // The goto will generate the suspend check.
1898 return;
1899 }
1900 GenerateSuspendCheck(instruction, nullptr);
1901}
1902
1903void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
1904 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001905 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001906 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001907 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001908 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001909 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001910 if (successor == nullptr) {
1911 __ j(kNotEqual, slow_path->GetEntryLabel());
1912 __ Bind(slow_path->GetReturnLabel());
1913 } else {
1914 __ j(kEqual, codegen_->GetLabelOf(successor));
1915 __ jmp(slow_path->GetEntryLabel());
1916 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001917}
1918
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001919X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
1920 return codegen_->GetAssembler();
1921}
1922
1923void ParallelMoveResolverX86_64::EmitMove(size_t index) {
1924 MoveOperands* move = moves_.Get(index);
1925 Location source = move->GetSource();
1926 Location destination = move->GetDestination();
1927
1928 if (source.IsRegister()) {
1929 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001930 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001931 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001932 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001933 source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001934 } else {
1935 DCHECK(destination.IsDoubleStackSlot());
1936 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001937 source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001938 }
1939 } else if (source.IsStackSlot()) {
1940 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001941 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001942 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001943 } else if (destination.IsFpuRegister()) {
1944 __ movss(destination.As<XmmRegister>(),
1945 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001946 } else {
1947 DCHECK(destination.IsStackSlot());
1948 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1949 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
1950 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001951 } else if (source.IsDoubleStackSlot()) {
1952 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001953 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001954 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001955 } else if (destination.IsFpuRegister()) {
1956 __ movsd(destination.As<XmmRegister>(), Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001957 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01001958 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001959 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1960 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
1961 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001962 } else if (source.IsConstant()) {
1963 HConstant* constant = source.GetConstant();
1964 if (constant->IsIntConstant()) {
1965 Immediate imm(constant->AsIntConstant()->GetValue());
1966 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001967 __ movl(destination.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001968 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001969 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001970 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
1971 }
1972 } else if (constant->IsLongConstant()) {
1973 int64_t value = constant->AsLongConstant()->GetValue();
1974 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001975 __ movq(destination.As<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001976 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001977 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001978 __ movq(CpuRegister(TMP), Immediate(value));
1979 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
1980 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001981 } else if (constant->IsFloatConstant()) {
1982 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
1983 if (destination.IsFpuRegister()) {
1984 __ movl(CpuRegister(TMP), imm);
1985 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
1986 } else {
1987 DCHECK(destination.IsStackSlot()) << destination;
1988 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
1989 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001990 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001991 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
1992 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
1993 if (destination.IsFpuRegister()) {
1994 __ movq(CpuRegister(TMP), imm);
1995 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
1996 } else {
1997 DCHECK(destination.IsDoubleStackSlot()) << destination;
1998 __ movq(CpuRegister(TMP), imm);
1999 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2000 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002001 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002002 } else if (source.IsFpuRegister()) {
2003 if (destination.IsFpuRegister()) {
2004 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
2005 } else if (destination.IsStackSlot()) {
2006 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
2007 source.As<XmmRegister>());
2008 } else {
2009 DCHECK(destination.IsDoubleStackSlot());
2010 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
2011 source.As<XmmRegister>());
2012 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002013 }
2014}
2015
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002016void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002017 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002018 __ movl(Address(CpuRegister(RSP), mem), reg);
2019 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002020}
2021
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002022void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002023 ScratchRegisterScope ensure_scratch(
2024 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2025
2026 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2027 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2028 __ movl(CpuRegister(ensure_scratch.GetRegister()),
2029 Address(CpuRegister(RSP), mem2 + stack_offset));
2030 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2031 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
2032 CpuRegister(ensure_scratch.GetRegister()));
2033}
2034
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002035void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
2036 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2037 __ movq(Address(CpuRegister(RSP), mem), reg);
2038 __ movq(reg, CpuRegister(TMP));
2039}
2040
2041void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
2042 ScratchRegisterScope ensure_scratch(
2043 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2044
2045 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2046 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2047 __ movq(CpuRegister(ensure_scratch.GetRegister()),
2048 Address(CpuRegister(RSP), mem2 + stack_offset));
2049 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2050 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
2051 CpuRegister(ensure_scratch.GetRegister()));
2052}
2053
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002054void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
2055 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2056 __ movss(Address(CpuRegister(RSP), mem), reg);
2057 __ movd(reg, CpuRegister(TMP));
2058}
2059
2060void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
2061 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2062 __ movsd(Address(CpuRegister(RSP), mem), reg);
2063 __ movd(reg, CpuRegister(TMP));
2064}
2065
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002066void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
2067 MoveOperands* move = moves_.Get(index);
2068 Location source = move->GetSource();
2069 Location destination = move->GetDestination();
2070
2071 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002072 __ xchgq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002073 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002074 Exchange32(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002075 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002076 Exchange32(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002077 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002078 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
2079 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002080 Exchange64(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002081 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002082 Exchange64(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002083 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
2084 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002085 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
2086 __ movd(CpuRegister(TMP), source.As<XmmRegister>());
2087 __ movaps(source.As<XmmRegister>(), destination.As<XmmRegister>());
2088 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2089 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
2090 Exchange32(source.As<XmmRegister>(), destination.GetStackIndex());
2091 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
2092 Exchange32(destination.As<XmmRegister>(), source.GetStackIndex());
2093 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
2094 Exchange64(source.As<XmmRegister>(), destination.GetStackIndex());
2095 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
2096 Exchange64(destination.As<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002097 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002098 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002099 }
2100}
2101
2102
2103void ParallelMoveResolverX86_64::SpillScratch(int reg) {
2104 __ pushq(CpuRegister(reg));
2105}
2106
2107
2108void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
2109 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002110}
2111
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002112void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
2113 LocationSummary* locations =
2114 new (GetGraph()->GetArena()) LocationSummary(cls, LocationSummary::kNoCall);
2115 locations->SetOut(Location::RequiresRegister());
2116}
2117
2118void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
2119 CpuRegister out = cls->GetLocations()->Out().As<CpuRegister>();
2120 if (cls->IsReferrersClass()) {
2121 codegen_->LoadCurrentMethod(out);
2122 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2123 } else {
2124 codegen_->LoadCurrentMethod(out);
2125 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2126 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
2127 }
2128}
2129
2130void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
2131 LocationSummary* locations =
2132 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2133 locations->SetInAt(0, Location::RequiresRegister());
2134 if (check->HasUses()) {
2135 locations->SetOut(Location::SameAsFirstInput());
2136 }
2137}
2138
2139void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
2140 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) ClinitCheckSlowPathX86_64(check);
2141 codegen_->AddSlowPath(slow_path);
2142
2143 LocationSummary* locations = check->GetLocations();
2144 // We remove the class as a live register, we know it's null or unused in the slow path.
2145 RegisterSet* register_set = locations->GetLiveRegisters();
2146 register_set->Remove(locations->InAt(0));
2147
2148 CpuRegister class_reg = locations->InAt(0).As<CpuRegister>();
2149 __ testl(class_reg, class_reg);
2150 __ j(kEqual, slow_path->GetEntryLabel());
2151 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2152 Immediate(mirror::Class::kStatusInitialized));
2153 __ j(kLess, slow_path->GetEntryLabel());
2154 __ Bind(slow_path->GetExitLabel());
2155 // No need for memory fence, thanks to the X86_64 memory model.
2156}
2157
2158void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2159 LocationSummary* locations =
2160 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2161 locations->SetInAt(0, Location::RequiresRegister());
2162 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2163}
2164
2165void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2166 LocationSummary* locations = instruction->GetLocations();
2167 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
2168 CpuRegister out = locations->Out().As<CpuRegister>();
2169 size_t offset = instruction->GetFieldOffset().SizeValue();
2170
2171 switch (instruction->GetType()) {
2172 case Primitive::kPrimBoolean: {
2173 __ movzxb(out, Address(cls, offset));
2174 break;
2175 }
2176
2177 case Primitive::kPrimByte: {
2178 __ movsxb(out, Address(cls, offset));
2179 break;
2180 }
2181
2182 case Primitive::kPrimShort: {
2183 __ movsxw(out, Address(cls, offset));
2184 break;
2185 }
2186
2187 case Primitive::kPrimChar: {
2188 __ movzxw(out, Address(cls, offset));
2189 break;
2190 }
2191
2192 case Primitive::kPrimInt:
2193 case Primitive::kPrimNot: {
2194 __ movl(out, Address(cls, offset));
2195 break;
2196 }
2197
2198 case Primitive::kPrimLong: {
2199 __ movq(out, Address(cls, offset));
2200 break;
2201 }
2202
2203 case Primitive::kPrimFloat:
2204 case Primitive::kPrimDouble:
2205 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
2206 UNREACHABLE();
2207 case Primitive::kPrimVoid:
2208 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2209 UNREACHABLE();
2210 }
2211}
2212
2213void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2214 LocationSummary* locations =
2215 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2216 Primitive::Type field_type = instruction->GetFieldType();
2217 bool is_object_type = field_type == Primitive::kPrimNot;
2218 locations->SetInAt(0, Location::RequiresRegister());
2219 locations->SetInAt(1, Location::RequiresRegister());
2220 if (is_object_type) {
2221 // Temporary registers for the write barrier.
2222 locations->AddTemp(Location::RequiresRegister());
2223 locations->AddTemp(Location::RequiresRegister());
2224 }
2225}
2226
2227void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2228 LocationSummary* locations = instruction->GetLocations();
2229 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
2230 CpuRegister value = locations->InAt(1).As<CpuRegister>();
2231 size_t offset = instruction->GetFieldOffset().SizeValue();
2232 Primitive::Type field_type = instruction->GetFieldType();
2233
2234 switch (field_type) {
2235 case Primitive::kPrimBoolean:
2236 case Primitive::kPrimByte: {
2237 __ movb(Address(cls, offset), value);
2238 break;
2239 }
2240
2241 case Primitive::kPrimShort:
2242 case Primitive::kPrimChar: {
2243 __ movw(Address(cls, offset), value);
2244 break;
2245 }
2246
2247 case Primitive::kPrimInt:
2248 case Primitive::kPrimNot: {
2249 __ movl(Address(cls, offset), value);
2250 if (field_type == Primitive::kPrimNot) {
2251 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
2252 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
2253 codegen_->MarkGCCard(temp, card, cls, value);
2254 }
2255 break;
2256 }
2257
2258 case Primitive::kPrimLong: {
2259 __ movq(Address(cls, offset), value);
2260 break;
2261 }
2262
2263 case Primitive::kPrimFloat:
2264 case Primitive::kPrimDouble:
2265 LOG(FATAL) << "Unimplemented register type " << field_type;
2266 UNREACHABLE();
2267 case Primitive::kPrimVoid:
2268 LOG(FATAL) << "Unreachable type " << field_type;
2269 UNREACHABLE();
2270 }
2271}
2272
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002273} // namespace x86_64
2274} // namespace art