blob: d9eb17c0e9b26c2200f38ed89e01254f411008f8 [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 Geoffray424f6762014-11-03 14:51:25 +0000171class LoadClassSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100172 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000173 LoadClassSlowPathX86_64(HLoadClass* cls,
174 HInstruction* at,
175 uint32_t dex_pc,
176 bool do_clinit)
177 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
178 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
179 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100180
181 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000182 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100183 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
184 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100185
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000186 codegen->SaveLiveRegisters(locations);
187
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100188 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000189 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100190 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000191 __ gs()->call(Address::Absolute((do_clinit_
192 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeStaticStorage)
193 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeType)) , true));
194 codegen->RecordPcInfo(at_, dex_pc_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100195
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000196 // Move the class to the desired location.
197 if (locations->Out().IsValid()) {
198 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
199 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
200 }
201
202 codegen->RestoreLiveRegisters(locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100203 __ jmp(GetExitLabel());
204 }
205
206 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000207 // The class this slow path will load.
208 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100209
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000210 // The instruction where this slow path is happening.
211 // (Might be the load class or an initialization check).
212 HInstruction* const at_;
213
214 // The dex PC of `at_`.
215 const uint32_t dex_pc_;
216
217 // Whether to initialize the class.
218 const bool do_clinit_;
219
220 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100221};
222
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000223class LoadStringSlowPathX86_64 : public SlowPathCodeX86_64 {
224 public:
225 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
226
227 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
228 LocationSummary* locations = instruction_->GetLocations();
229 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
230
231 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
232 __ Bind(GetEntryLabel());
233 codegen->SaveLiveRegisters(locations);
234
235 InvokeRuntimeCallingConvention calling_convention;
236 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(0)));
237 __ movl(CpuRegister(calling_convention.GetRegisterAt(1)),
238 Immediate(instruction_->GetStringIndex()));
239 __ gs()->call(Address::Absolute(
240 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pResolveString), true));
241 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
242 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
243 codegen->RestoreLiveRegisters(locations);
244 __ jmp(GetExitLabel());
245 }
246
247 private:
248 HLoadString* const instruction_;
249
250 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
251};
252
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100253#undef __
254#define __ reinterpret_cast<X86_64Assembler*>(GetAssembler())->
255
Dave Allison20dfc792014-06-16 20:44:29 -0700256inline Condition X86_64Condition(IfCondition cond) {
257 switch (cond) {
258 case kCondEQ: return kEqual;
259 case kCondNE: return kNotEqual;
260 case kCondLT: return kLess;
261 case kCondLE: return kLessEqual;
262 case kCondGT: return kGreater;
263 case kCondGE: return kGreaterEqual;
264 default:
265 LOG(FATAL) << "Unknown if condition";
266 }
267 return kEqual;
268}
269
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100270void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
271 stream << X86_64ManagedRegister::FromCpuRegister(Register(reg));
272}
273
274void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
275 stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg));
276}
277
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100278size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
279 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
280 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100281}
282
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100283size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
284 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
285 return kX86_64WordSize;
286}
287
288size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
289 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
290 return kX86_64WordSize;
291}
292
293size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
294 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
295 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100296}
297
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100298CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100299 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfFloatRegisters, 0),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100300 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100301 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000302 instruction_visitor_(graph, this),
303 move_resolver_(graph->GetArena(), this) {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100304
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100305size_t CodeGeneratorX86_64::FrameEntrySpillSize() const {
306 return kNumberOfPushedRegistersAtEntry * kX86_64WordSize;
307}
308
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100309InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
310 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100311 : HGraphVisitor(graph),
312 assembler_(codegen->GetAssembler()),
313 codegen_(codegen) {}
314
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100315Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100316 switch (type) {
317 case Primitive::kPrimLong:
318 case Primitive::kPrimByte:
319 case Primitive::kPrimBoolean:
320 case Primitive::kPrimChar:
321 case Primitive::kPrimShort:
322 case Primitive::kPrimInt:
323 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100324 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100325 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100326 }
327
328 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100329 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100330 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100331 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100332 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100333
334 case Primitive::kPrimVoid:
335 LOG(FATAL) << "Unreachable type " << type;
336 }
337
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100338 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100339}
340
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100341void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100342 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100343 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100344
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000345 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100346 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000347
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100348 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100349 blocked_core_registers_[RBX] = true;
350 blocked_core_registers_[RBP] = true;
351 blocked_core_registers_[R12] = true;
352 blocked_core_registers_[R13] = true;
353 blocked_core_registers_[R14] = true;
354 blocked_core_registers_[R15] = true;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100355
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100356 blocked_fpu_registers_[XMM12] = true;
357 blocked_fpu_registers_[XMM13] = true;
358 blocked_fpu_registers_[XMM14] = true;
359 blocked_fpu_registers_[XMM15] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100360}
361
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100362void CodeGeneratorX86_64::GenerateFrameEntry() {
363 // Create a fake register to mimic Quick.
364 static const int kFakeReturnRegister = 16;
365 core_spill_mask_ |= (1 << kFakeReturnRegister);
366
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100367 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700368 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100369
370 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
371 __ testq(CpuRegister(RAX), Address(
372 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100373 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100374 }
375
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100376 // The return PC has already been pushed on the stack.
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100377 __ subq(CpuRegister(RSP),
378 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
379
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100380 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100381 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86_64();
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100382 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100383
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100384 __ gs()->cmpq(CpuRegister(RSP),
385 Address::Absolute(Thread::StackEndOffset<kX86_64WordSize>(), true));
386 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100387 }
388
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100389 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
390}
391
392void CodeGeneratorX86_64::GenerateFrameExit() {
393 __ addq(CpuRegister(RSP),
394 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
395}
396
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100397void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
398 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100399}
400
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100401void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100402 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
403}
404
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100405Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
406 switch (load->GetType()) {
407 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100408 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100409 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
410 break;
411
412 case Primitive::kPrimInt:
413 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100414 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100415 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100416
417 case Primitive::kPrimBoolean:
418 case Primitive::kPrimByte:
419 case Primitive::kPrimChar:
420 case Primitive::kPrimShort:
421 case Primitive::kPrimVoid:
422 LOG(FATAL) << "Unexpected type " << load->GetType();
423 }
424
425 LOG(FATAL) << "Unreachable";
426 return Location();
427}
428
429void CodeGeneratorX86_64::Move(Location destination, Location source) {
430 if (source.Equals(destination)) {
431 return;
432 }
433 if (destination.IsRegister()) {
434 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100435 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100436 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100437 __ movd(destination.As<CpuRegister>(), source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100438 } else if (source.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100439 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100440 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100441 } else {
442 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100443 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100444 Address(CpuRegister(RSP), source.GetStackIndex()));
445 }
446 } else if (destination.IsFpuRegister()) {
447 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100448 __ movd(destination.As<XmmRegister>(), source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100449 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100450 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100451 } else if (source.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100452 __ movss(destination.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100453 Address(CpuRegister(RSP), source.GetStackIndex()));
454 } else {
455 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100456 __ movsd(destination.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100457 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100458 }
459 } else if (destination.IsStackSlot()) {
460 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100461 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100462 source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100463 } else if (source.IsFpuRegister()) {
464 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100465 source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100466 } else {
467 DCHECK(source.IsStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000468 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
469 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100470 }
471 } else {
472 DCHECK(destination.IsDoubleStackSlot());
473 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100474 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100475 source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100476 } else if (source.IsFpuRegister()) {
477 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100478 source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100479 } else {
480 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000481 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
482 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100483 }
484 }
485}
486
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100487void CodeGeneratorX86_64::Move(HInstruction* instruction,
488 Location location,
489 HInstruction* move_for) {
Roland Levillain476df552014-10-09 17:51:36 +0100490 if (instruction->IsIntConstant()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100491 Immediate imm(instruction->AsIntConstant()->GetValue());
492 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100493 __ movl(location.As<CpuRegister>(), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100494 } else if (location.IsStackSlot()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100495 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100496 } else {
497 DCHECK(location.IsConstant());
498 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100499 }
Roland Levillain476df552014-10-09 17:51:36 +0100500 } else if (instruction->IsLongConstant()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100501 int64_t value = instruction->AsLongConstant()->GetValue();
502 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100503 __ movq(location.As<CpuRegister>(), Immediate(value));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100504 } else if (location.IsDoubleStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000505 __ movq(CpuRegister(TMP), Immediate(value));
506 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100507 } else {
508 DCHECK(location.IsConstant());
509 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100510 }
Roland Levillain476df552014-10-09 17:51:36 +0100511 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100512 switch (instruction->GetType()) {
513 case Primitive::kPrimBoolean:
514 case Primitive::kPrimByte:
515 case Primitive::kPrimChar:
516 case Primitive::kPrimShort:
517 case Primitive::kPrimInt:
518 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100519 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100520 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
521 break;
522
523 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100524 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100525 Move(location, Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
526 break;
527
528 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100529 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100530 }
531 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100532 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100533 switch (instruction->GetType()) {
534 case Primitive::kPrimBoolean:
535 case Primitive::kPrimByte:
536 case Primitive::kPrimChar:
537 case Primitive::kPrimShort:
538 case Primitive::kPrimInt:
539 case Primitive::kPrimNot:
540 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100541 case Primitive::kPrimFloat:
542 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100543 Move(location, instruction->GetLocations()->Out());
544 break;
545
546 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100547 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100548 }
549 }
550}
551
552void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
553 got->SetLocations(nullptr);
554}
555
556void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
557 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100558 DCHECK(!successor->IsExitBlock());
559
560 HBasicBlock* block = got->GetBlock();
561 HInstruction* previous = got->GetPrevious();
562
563 HLoopInformation* info = block->GetLoopInformation();
564 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
565 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
566 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
567 return;
568 }
569
570 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
571 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
572 }
573 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100574 __ jmp(codegen_->GetLabelOf(successor));
575 }
576}
577
578void LocationsBuilderX86_64::VisitExit(HExit* exit) {
579 exit->SetLocations(nullptr);
580}
581
582void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700583 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100584 if (kIsDebugBuild) {
585 __ Comment("Unreachable");
586 __ int3();
587 }
588}
589
590void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100591 LocationSummary* locations =
592 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100593 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100594 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100595 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100596 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100597}
598
599void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700600 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100601 if (cond->IsIntConstant()) {
602 // Constant condition, statically compared against 1.
603 int32_t cond_value = cond->AsIntConstant()->GetValue();
604 if (cond_value == 1) {
605 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
606 if_instr->IfTrueSuccessor())) {
607 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100608 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100609 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100610 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100611 DCHECK_EQ(cond_value, 0);
612 }
613 } else {
614 bool materialized =
615 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
616 // Moves do not affect the eflags register, so if the condition is
617 // evaluated just before the if, we don't need to evaluate it
618 // again.
619 bool eflags_set = cond->IsCondition()
620 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
621 if (materialized) {
622 if (!eflags_set) {
623 // Materialized condition, compare against 0.
624 Location lhs = if_instr->GetLocations()->InAt(0);
625 if (lhs.IsRegister()) {
626 __ cmpl(lhs.As<CpuRegister>(), Immediate(0));
627 } else {
628 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
629 Immediate(0));
630 }
631 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
632 } else {
633 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
634 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
635 }
636 } else {
637 Location lhs = cond->GetLocations()->InAt(0);
638 Location rhs = cond->GetLocations()->InAt(1);
639 if (rhs.IsRegister()) {
640 __ cmpl(lhs.As<CpuRegister>(), rhs.As<CpuRegister>());
641 } else if (rhs.IsConstant()) {
642 __ cmpl(lhs.As<CpuRegister>(),
643 Immediate(rhs.GetConstant()->AsIntConstant()->GetValue()));
644 } else {
645 __ cmpl(lhs.As<CpuRegister>(),
646 Address(CpuRegister(RSP), rhs.GetStackIndex()));
647 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100648 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
649 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700650 }
Dave Allison20dfc792014-06-16 20:44:29 -0700651 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100652 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
653 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700654 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100655 }
656}
657
658void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
659 local->SetLocations(nullptr);
660}
661
662void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
663 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
664}
665
666void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
667 local->SetLocations(nullptr);
668}
669
670void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
671 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700672 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100673}
674
675void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100676 LocationSummary* locations =
677 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100678 switch (store->InputAt(1)->GetType()) {
679 case Primitive::kPrimBoolean:
680 case Primitive::kPrimByte:
681 case Primitive::kPrimChar:
682 case Primitive::kPrimShort:
683 case Primitive::kPrimInt:
684 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100685 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100686 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
687 break;
688
689 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100690 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100691 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
692 break;
693
694 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100695 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100696 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100697}
698
699void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700700 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100701}
702
Dave Allison20dfc792014-06-16 20:44:29 -0700703void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100704 LocationSummary* locations =
705 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100706 locations->SetInAt(0, Location::RequiresRegister());
707 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100708 if (comp->NeedsMaterialization()) {
709 locations->SetOut(Location::RequiresRegister());
710 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100711}
712
Dave Allison20dfc792014-06-16 20:44:29 -0700713void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
714 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100715 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100716 CpuRegister reg = locations->Out().As<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100717 // Clear register: setcc only sets the low byte.
718 __ xorq(reg, reg);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100719 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100720 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100721 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100722 } else if (locations->InAt(1).IsConstant()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100723 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100724 Immediate(locations->InAt(1).GetConstant()->AsIntConstant()->GetValue()));
725 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100726 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100727 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
728 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100729 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700730 }
731}
732
733void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
734 VisitCondition(comp);
735}
736
737void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
738 VisitCondition(comp);
739}
740
741void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
742 VisitCondition(comp);
743}
744
745void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
746 VisitCondition(comp);
747}
748
749void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
750 VisitCondition(comp);
751}
752
753void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
754 VisitCondition(comp);
755}
756
757void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
758 VisitCondition(comp);
759}
760
761void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
762 VisitCondition(comp);
763}
764
765void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
766 VisitCondition(comp);
767}
768
769void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
770 VisitCondition(comp);
771}
772
773void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
774 VisitCondition(comp);
775}
776
777void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
778 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100779}
780
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100781void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100782 LocationSummary* locations =
783 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100784 locations->SetInAt(0, Location::RequiresRegister());
785 locations->SetInAt(1, Location::RequiresRegister());
786 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100787}
788
789void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
790 Label greater, done;
791 LocationSummary* locations = compare->GetLocations();
792 switch (compare->InputAt(0)->GetType()) {
793 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100794 __ cmpq(locations->InAt(0).As<CpuRegister>(),
795 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100796 break;
797 default:
798 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
799 }
800
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100801 CpuRegister output = locations->Out().As<CpuRegister>();
802 __ movl(output, Immediate(0));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100803 __ j(kEqual, &done);
804 __ j(kGreater, &greater);
805
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100806 __ movl(output, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100807 __ jmp(&done);
808
809 __ Bind(&greater);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100810 __ movl(output, Immediate(1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100811
812 __ Bind(&done);
813}
814
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100815void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100816 LocationSummary* locations =
817 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100818 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100819}
820
821void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100822 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700823 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100824}
825
826void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100827 LocationSummary* locations =
828 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100829 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100830}
831
832void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100833 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700834 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100835}
836
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100837void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
838 LocationSummary* locations =
839 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
840 locations->SetOut(Location::ConstantLocation(constant));
841}
842
843void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
844 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700845 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100846}
847
848void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
849 LocationSummary* locations =
850 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
851 locations->SetOut(Location::ConstantLocation(constant));
852}
853
854void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
855 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700856 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100857}
858
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100859void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
860 ret->SetLocations(nullptr);
861}
862
863void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700864 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100865 codegen_->GenerateFrameExit();
866 __ ret();
867}
868
869void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100870 LocationSummary* locations =
871 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100872 switch (ret->InputAt(0)->GetType()) {
873 case Primitive::kPrimBoolean:
874 case Primitive::kPrimByte:
875 case Primitive::kPrimChar:
876 case Primitive::kPrimShort:
877 case Primitive::kPrimInt:
878 case Primitive::kPrimNot:
879 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100880 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100881 break;
882
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100883 case Primitive::kPrimFloat:
884 case Primitive::kPrimDouble:
885 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100886 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100887 break;
888
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100889 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100890 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100891 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100892}
893
894void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
895 if (kIsDebugBuild) {
896 switch (ret->InputAt(0)->GetType()) {
897 case Primitive::kPrimBoolean:
898 case Primitive::kPrimByte:
899 case Primitive::kPrimChar:
900 case Primitive::kPrimShort:
901 case Primitive::kPrimInt:
902 case Primitive::kPrimNot:
903 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100904 DCHECK_EQ(ret->GetLocations()->InAt(0).As<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100905 break;
906
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100907 case Primitive::kPrimFloat:
908 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100909 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100910 XMM0);
911 break;
912
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100913 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100914 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100915 }
916 }
917 codegen_->GenerateFrameExit();
918 __ ret();
919}
920
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100921Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
922 switch (type) {
923 case Primitive::kPrimBoolean:
924 case Primitive::kPrimByte:
925 case Primitive::kPrimChar:
926 case Primitive::kPrimShort:
927 case Primitive::kPrimInt:
928 case Primitive::kPrimNot: {
929 uint32_t index = gp_index_++;
930 stack_index_++;
931 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100932 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100933 } else {
934 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
935 }
936 }
937
938 case Primitive::kPrimLong: {
939 uint32_t index = gp_index_;
940 stack_index_ += 2;
941 if (index < calling_convention.GetNumberOfRegisters()) {
942 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100943 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100944 } else {
945 gp_index_ += 2;
946 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
947 }
948 }
949
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100950 case Primitive::kPrimFloat: {
951 uint32_t index = fp_index_++;
952 stack_index_++;
953 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100954 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100955 } else {
956 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
957 }
958 }
959
960 case Primitive::kPrimDouble: {
961 uint32_t index = fp_index_++;
962 stack_index_ += 2;
963 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100964 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100965 } else {
966 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
967 }
968 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100969
970 case Primitive::kPrimVoid:
971 LOG(FATAL) << "Unexpected parameter type " << type;
972 break;
973 }
974 return Location();
975}
976
977void LocationsBuilderX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100978 HandleInvoke(invoke);
979}
980
981void InstructionCodeGeneratorX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100982 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100983 // TODO: Implement all kinds of calls:
984 // 1) boot -> boot
985 // 2) app -> boot
986 // 3) app -> app
987 //
988 // Currently we implement the app -> app logic, which looks up in the resolve cache.
989
990 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100991 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100992 // temp = temp->dex_cache_resolved_methods_;
993 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
994 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100995 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100996 // (temp + offset_of_quick_compiled_code)()
997 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
998
999 DCHECK(!codegen_->IsLeafMethod());
1000 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1001}
1002
1003void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1004 HandleInvoke(invoke);
1005}
1006
1007void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001008 LocationSummary* locations =
1009 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001010 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001011
1012 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001013 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001014 HInstruction* input = invoke->InputAt(i);
1015 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1016 }
1017
1018 switch (invoke->GetType()) {
1019 case Primitive::kPrimBoolean:
1020 case Primitive::kPrimByte:
1021 case Primitive::kPrimChar:
1022 case Primitive::kPrimShort:
1023 case Primitive::kPrimInt:
1024 case Primitive::kPrimNot:
1025 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001026 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001027 break;
1028
1029 case Primitive::kPrimVoid:
1030 break;
1031
1032 case Primitive::kPrimDouble:
1033 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001034 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001035 break;
1036 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001037}
1038
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001039void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001040 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001041 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1042 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1043 LocationSummary* locations = invoke->GetLocations();
1044 Location receiver = locations->InAt(0);
1045 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1046 // temp = object->GetClass();
1047 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001048 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1049 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001050 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001051 __ movl(temp, Address(receiver.As<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001052 }
1053 // temp = temp->GetMethodAt(method_offset);
1054 __ movl(temp, Address(temp, method_offset));
1055 // call temp->GetEntryPoint();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001056 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
1057
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001058 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001059 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001060}
1061
Roland Levillain88cb1752014-10-20 16:36:47 +01001062void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1063 LocationSummary* locations =
1064 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1065 switch (neg->GetResultType()) {
1066 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001067 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001068 locations->SetInAt(0, Location::RequiresRegister());
1069 locations->SetOut(Location::SameAsFirstInput());
1070 break;
1071
Roland Levillain88cb1752014-10-20 16:36:47 +01001072 case Primitive::kPrimFloat:
1073 case Primitive::kPrimDouble:
1074 LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
1075 break;
1076
1077 default:
1078 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1079 }
1080}
1081
1082void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1083 LocationSummary* locations = neg->GetLocations();
1084 Location out = locations->Out();
1085 Location in = locations->InAt(0);
1086 switch (neg->GetResultType()) {
1087 case Primitive::kPrimInt:
1088 DCHECK(in.IsRegister());
1089 __ negl(out.As<CpuRegister>());
1090 break;
1091
1092 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001093 DCHECK(in.IsRegister());
1094 __ negq(out.As<CpuRegister>());
1095 break;
1096
Roland Levillain88cb1752014-10-20 16:36:47 +01001097 case Primitive::kPrimFloat:
1098 case Primitive::kPrimDouble:
1099 LOG(FATAL) << "Not yet implemented neg type " << neg->GetResultType();
1100 break;
1101
1102 default:
1103 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1104 }
1105}
1106
Roland Levillaindff1f282014-11-05 14:15:05 +00001107void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1108 LocationSummary* locations =
1109 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1110 Primitive::Type result_type = conversion->GetResultType();
1111 Primitive::Type input_type = conversion->GetInputType();
1112 switch (result_type) {
1113 case Primitive::kPrimLong:
1114 switch (input_type) {
1115 case Primitive::kPrimByte:
1116 case Primitive::kPrimShort:
1117 case Primitive::kPrimInt:
1118 // int-to-long conversion.
1119 // TODO: We would benefit from a (to-be-implemented)
1120 // Location::RegisterOrStackSlot requirement for this input.
1121 locations->SetInAt(0, Location::RequiresRegister());
1122 locations->SetOut(Location::RequiresRegister());
1123 break;
1124
1125 case Primitive::kPrimFloat:
1126 case Primitive::kPrimDouble:
1127 LOG(FATAL) << "Type conversion from " << input_type << " to "
1128 << result_type << " not yet implemented";
1129 break;
1130
1131 default:
1132 LOG(FATAL) << "Unexpected type conversion from " << input_type
1133 << " to " << result_type;
1134 }
1135 break;
1136
1137 case Primitive::kPrimInt:
1138 case Primitive::kPrimFloat:
1139 case Primitive::kPrimDouble:
1140 LOG(FATAL) << "Type conversion from " << input_type
1141 << " to " << result_type << " not yet implemented";
1142 break;
1143
1144 default:
1145 LOG(FATAL) << "Unexpected type conversion from " << input_type
1146 << " to " << result_type;
1147 }
1148}
1149
1150void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1151 LocationSummary* locations = conversion->GetLocations();
1152 Location out = locations->Out();
1153 Location in = locations->InAt(0);
1154 Primitive::Type result_type = conversion->GetResultType();
1155 Primitive::Type input_type = conversion->GetInputType();
1156 switch (result_type) {
1157 case Primitive::kPrimLong:
1158 switch (input_type) {
1159 DCHECK(out.IsRegister());
1160 case Primitive::kPrimByte:
1161 case Primitive::kPrimShort:
1162 case Primitive::kPrimInt:
1163 // int-to-long conversion.
1164 DCHECK(in.IsRegister());
1165 __ movsxd(out.As<CpuRegister>(), in.As<CpuRegister>());
1166 break;
1167
1168 case Primitive::kPrimFloat:
1169 case Primitive::kPrimDouble:
1170 LOG(FATAL) << "Type conversion from " << input_type << " to "
1171 << result_type << " not yet implemented";
1172 break;
1173
1174 default:
1175 LOG(FATAL) << "Unexpected type conversion from " << input_type
1176 << " to " << result_type;
1177 }
1178 break;
1179
1180 case Primitive::kPrimInt:
1181 case Primitive::kPrimFloat:
1182 case Primitive::kPrimDouble:
1183 LOG(FATAL) << "Type conversion from " << input_type
1184 << " to " << result_type << " not yet implemented";
1185 break;
1186
1187 default:
1188 LOG(FATAL) << "Unexpected type conversion from " << input_type
1189 << " to " << result_type;
1190 }
1191}
1192
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001193void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001194 LocationSummary* locations =
1195 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001196 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001197 case Primitive::kPrimInt: {
1198 locations->SetInAt(0, Location::RequiresRegister());
1199 locations->SetInAt(1, Location::Any());
1200 locations->SetOut(Location::SameAsFirstInput());
1201 break;
1202 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001203
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001204 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001205 locations->SetInAt(0, Location::RequiresRegister());
1206 locations->SetInAt(1, Location::RequiresRegister());
1207 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001208 break;
1209 }
1210
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001211 case Primitive::kPrimDouble:
1212 case Primitive::kPrimFloat: {
1213 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001214 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001215 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001216 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001217 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001218
1219 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001220 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001221 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001222}
1223
1224void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1225 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001226 Location first = locations->InAt(0);
1227 Location second = locations->InAt(1);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001228 DCHECK(first.Equals(locations->Out()));
Calin Juravle11351682014-10-23 15:38:15 +01001229
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001230 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001231 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001232 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001233 __ addl(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001234 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001235 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001236 __ addl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001237 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001238 __ addl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001239 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001240 break;
1241 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001242
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001243 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001244 __ addq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001245 break;
1246 }
1247
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001248 case Primitive::kPrimFloat: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001249 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001250 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001251 }
1252
1253 case Primitive::kPrimDouble: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001254 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001255 break;
1256 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001257
1258 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001259 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001260 }
1261}
1262
1263void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001264 LocationSummary* locations =
1265 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001266 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001267 case Primitive::kPrimInt: {
1268 locations->SetInAt(0, Location::RequiresRegister());
1269 locations->SetInAt(1, Location::Any());
1270 locations->SetOut(Location::SameAsFirstInput());
1271 break;
1272 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001273 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001274 locations->SetInAt(0, Location::RequiresRegister());
1275 locations->SetInAt(1, Location::RequiresRegister());
1276 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001277 break;
1278 }
Calin Juravle11351682014-10-23 15:38:15 +01001279 case Primitive::kPrimFloat:
1280 case Primitive::kPrimDouble: {
1281 locations->SetInAt(0, Location::RequiresFpuRegister());
1282 locations->SetInAt(1, Location::RequiresFpuRegister());
1283 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001284 break;
Calin Juravle11351682014-10-23 15:38:15 +01001285 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001286 default:
Calin Juravle11351682014-10-23 15:38:15 +01001287 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001288 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001289}
1290
1291void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1292 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001293 Location first = locations->InAt(0);
1294 Location second = locations->InAt(1);
1295 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001296 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001297 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001298 if (second.IsRegister()) {
1299 __ subl(first.As<CpuRegister>(), second.As<CpuRegister>());
1300 } else if (second.IsConstant()) {
1301 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1302 __ subl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001303 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001304 __ subl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001305 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001306 break;
1307 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001308 case Primitive::kPrimLong: {
Calin Juravle11351682014-10-23 15:38:15 +01001309 __ subq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001310 break;
1311 }
1312
Calin Juravle11351682014-10-23 15:38:15 +01001313 case Primitive::kPrimFloat: {
1314 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001315 break;
Calin Juravle11351682014-10-23 15:38:15 +01001316 }
1317
1318 case Primitive::kPrimDouble: {
1319 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1320 break;
1321 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001322
1323 default:
Calin Juravle11351682014-10-23 15:38:15 +01001324 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001325 }
1326}
1327
Calin Juravle34bacdf2014-10-07 20:23:36 +01001328void LocationsBuilderX86_64::VisitMul(HMul* mul) {
1329 LocationSummary* locations =
1330 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1331 switch (mul->GetResultType()) {
1332 case Primitive::kPrimInt: {
1333 locations->SetInAt(0, Location::RequiresRegister());
1334 locations->SetInAt(1, Location::Any());
1335 locations->SetOut(Location::SameAsFirstInput());
1336 break;
1337 }
1338 case Primitive::kPrimLong: {
1339 locations->SetInAt(0, Location::RequiresRegister());
1340 locations->SetInAt(1, Location::RequiresRegister());
1341 locations->SetOut(Location::SameAsFirstInput());
1342 break;
1343 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001344 case Primitive::kPrimFloat:
1345 case Primitive::kPrimDouble: {
1346 locations->SetInAt(0, Location::RequiresFpuRegister());
1347 locations->SetInAt(1, Location::RequiresFpuRegister());
1348 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001349 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001350 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001351
1352 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001353 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001354 }
1355}
1356
1357void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
1358 LocationSummary* locations = mul->GetLocations();
1359 Location first = locations->InAt(0);
1360 Location second = locations->InAt(1);
1361 DCHECK(first.Equals(locations->Out()));
1362 switch (mul->GetResultType()) {
1363 case Primitive::kPrimInt: {
1364 if (second.IsRegister()) {
1365 __ imull(first.As<CpuRegister>(), second.As<CpuRegister>());
1366 } else if (second.IsConstant()) {
1367 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1368 __ imull(first.As<CpuRegister>(), imm);
1369 } else {
1370 DCHECK(second.IsStackSlot());
1371 __ imull(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
1372 }
1373 break;
1374 }
1375 case Primitive::kPrimLong: {
1376 __ imulq(first.As<CpuRegister>(), second.As<CpuRegister>());
1377 break;
1378 }
1379
Calin Juravleb5bfa962014-10-21 18:02:24 +01001380 case Primitive::kPrimFloat: {
1381 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001382 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001383 }
1384
1385 case Primitive::kPrimDouble: {
1386 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1387 break;
1388 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001389
1390 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001391 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001392 }
1393}
1394
Calin Juravle7c4954d2014-10-28 16:57:40 +00001395void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
1396 LocationSummary* locations =
1397 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1398 switch (div->GetResultType()) {
1399 case Primitive::kPrimInt:
1400 case Primitive::kPrimLong: {
1401 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1402 break;
1403 }
1404 case Primitive::kPrimFloat:
1405 case Primitive::kPrimDouble: {
1406 locations->SetInAt(0, Location::RequiresFpuRegister());
1407 locations->SetInAt(1, Location::RequiresFpuRegister());
1408 locations->SetOut(Location::SameAsFirstInput());
1409 break;
1410 }
1411
1412 default:
1413 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1414 }
1415}
1416
1417void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
1418 LocationSummary* locations = div->GetLocations();
1419 Location first = locations->InAt(0);
1420 Location second = locations->InAt(1);
1421 DCHECK(first.Equals(locations->Out()));
1422
1423 switch (div->GetResultType()) {
1424 case Primitive::kPrimInt:
1425 case Primitive::kPrimLong: {
1426 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1427 break;
1428 }
1429
1430 case Primitive::kPrimFloat: {
1431 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1432 break;
1433 }
1434
1435 case Primitive::kPrimDouble: {
1436 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1437 break;
1438 }
1439
1440 default:
1441 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1442 }
1443}
1444
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001445void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001446 LocationSummary* locations =
1447 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001448 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001449 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1450 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1451 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001452}
1453
1454void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
1455 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001456 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001457 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1458
1459 __ gs()->call(Address::Absolute(
1460 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
1461
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001462 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001463 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001464}
1465
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001466void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
1467 LocationSummary* locations =
1468 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1469 InvokeRuntimeCallingConvention calling_convention;
1470 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1471 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1472 locations->SetOut(Location::RegisterLocation(RAX));
1473 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1474}
1475
1476void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
1477 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001478 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001479 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1480
1481 __ gs()->call(Address::Absolute(
1482 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocArrayWithAccessCheck), true));
1483
1484 DCHECK(!codegen_->IsLeafMethod());
1485 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1486}
1487
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001488void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001489 LocationSummary* locations =
1490 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001491 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1492 if (location.IsStackSlot()) {
1493 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1494 } else if (location.IsDoubleStackSlot()) {
1495 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1496 }
1497 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001498}
1499
1500void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
1501 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001502 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001503}
1504
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001505void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001506 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001507 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001508 locations->SetInAt(0, Location::RequiresRegister());
1509 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001510}
1511
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001512void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
1513 LocationSummary* locations = not_->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001514 DCHECK_EQ(locations->InAt(0).As<CpuRegister>().AsRegister(),
1515 locations->Out().As<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001516 Location out = locations->Out();
1517 switch (not_->InputAt(0)->GetType()) {
1518 case Primitive::kPrimBoolean:
1519 __ xorq(out.As<CpuRegister>(), Immediate(1));
1520 break;
1521
1522 case Primitive::kPrimInt:
1523 __ notl(out.As<CpuRegister>());
1524 break;
1525
1526 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001527 __ notq(out.As<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001528 break;
1529
1530 default:
1531 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1532 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001533}
1534
1535void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001536 LocationSummary* locations =
1537 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001538 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1539 locations->SetInAt(i, Location::Any());
1540 }
1541 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001542}
1543
1544void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001545 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001546 LOG(FATAL) << "Unimplemented";
1547}
1548
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001549void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001550 LocationSummary* locations =
1551 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001552 Primitive::Type field_type = instruction->GetFieldType();
1553 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001554 locations->SetInAt(0, Location::RequiresRegister());
1555 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001556 if (is_object_type) {
1557 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001558 locations->AddTemp(Location::RequiresRegister());
1559 locations->AddTemp(Location::RequiresRegister());
1560 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001561}
1562
1563void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1564 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001565 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
1566 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001567 size_t offset = instruction->GetFieldOffset().SizeValue();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001568 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001569
1570 switch (field_type) {
1571 case Primitive::kPrimBoolean:
1572 case Primitive::kPrimByte: {
1573 __ movb(Address(obj, offset), value);
1574 break;
1575 }
1576
1577 case Primitive::kPrimShort:
1578 case Primitive::kPrimChar: {
1579 __ movw(Address(obj, offset), value);
1580 break;
1581 }
1582
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001583 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001584 case Primitive::kPrimNot: {
1585 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001586 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001587 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
1588 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001589 codegen_->MarkGCCard(temp, card, obj, value);
1590 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001591 break;
1592 }
1593
1594 case Primitive::kPrimLong: {
1595 __ movq(Address(obj, offset), value);
1596 break;
1597 }
1598
1599 case Primitive::kPrimFloat:
1600 case Primitive::kPrimDouble:
1601 LOG(FATAL) << "Unimplemented register type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001602 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001603 case Primitive::kPrimVoid:
1604 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001605 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001606 }
1607}
1608
1609void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001610 LocationSummary* locations =
1611 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001612 locations->SetInAt(0, Location::RequiresRegister());
1613 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001614}
1615
1616void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1617 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001618 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
1619 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001620 size_t offset = instruction->GetFieldOffset().SizeValue();
1621
1622 switch (instruction->GetType()) {
1623 case Primitive::kPrimBoolean: {
1624 __ movzxb(out, Address(obj, offset));
1625 break;
1626 }
1627
1628 case Primitive::kPrimByte: {
1629 __ movsxb(out, Address(obj, offset));
1630 break;
1631 }
1632
1633 case Primitive::kPrimShort: {
1634 __ movsxw(out, Address(obj, offset));
1635 break;
1636 }
1637
1638 case Primitive::kPrimChar: {
1639 __ movzxw(out, Address(obj, offset));
1640 break;
1641 }
1642
1643 case Primitive::kPrimInt:
1644 case Primitive::kPrimNot: {
1645 __ movl(out, Address(obj, offset));
1646 break;
1647 }
1648
1649 case Primitive::kPrimLong: {
1650 __ movq(out, Address(obj, offset));
1651 break;
1652 }
1653
1654 case Primitive::kPrimFloat:
1655 case Primitive::kPrimDouble:
1656 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001657 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001658 case Primitive::kPrimVoid:
1659 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001660 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001661 }
1662}
1663
1664void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001665 LocationSummary* locations =
1666 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001667 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001668 if (instruction->HasUses()) {
1669 locations->SetOut(Location::SameAsFirstInput());
1670 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001671}
1672
1673void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001674 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001675 codegen_->AddSlowPath(slow_path);
1676
1677 LocationSummary* locations = instruction->GetLocations();
1678 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001679
1680 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001681 __ cmpl(obj.As<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001682 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001683 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001684 } else {
1685 DCHECK(obj.IsConstant()) << obj;
1686 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1687 __ jmp(slow_path->GetEntryLabel());
1688 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001689 }
1690 __ j(kEqual, slow_path->GetEntryLabel());
1691}
1692
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001693void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001694 LocationSummary* locations =
1695 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001696 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001697 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001698 1, Location::RegisterOrConstant(instruction->InputAt(1)));
1699 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001700}
1701
1702void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
1703 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001704 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001705 Location index = locations->InAt(1);
1706
1707 switch (instruction->GetType()) {
1708 case Primitive::kPrimBoolean: {
1709 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001710 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001711 if (index.IsConstant()) {
1712 __ movzxb(out, Address(obj,
1713 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1714 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001715 __ movzxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001716 }
1717 break;
1718 }
1719
1720 case Primitive::kPrimByte: {
1721 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001722 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001723 if (index.IsConstant()) {
1724 __ movsxb(out, Address(obj,
1725 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1726 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001727 __ movsxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001728 }
1729 break;
1730 }
1731
1732 case Primitive::kPrimShort: {
1733 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001734 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001735 if (index.IsConstant()) {
1736 __ movsxw(out, Address(obj,
1737 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1738 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001739 __ movsxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001740 }
1741 break;
1742 }
1743
1744 case Primitive::kPrimChar: {
1745 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001746 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001747 if (index.IsConstant()) {
1748 __ movzxw(out, Address(obj,
1749 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1750 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001751 __ movzxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001752 }
1753 break;
1754 }
1755
1756 case Primitive::kPrimInt:
1757 case Primitive::kPrimNot: {
1758 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1759 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001760 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001761 if (index.IsConstant()) {
1762 __ movl(out, Address(obj,
1763 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1764 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001765 __ movl(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001766 }
1767 break;
1768 }
1769
1770 case Primitive::kPrimLong: {
1771 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001772 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001773 if (index.IsConstant()) {
1774 __ movq(out, Address(obj,
1775 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
1776 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001777 __ movq(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
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 XmmRegister out = locations->Out().As<XmmRegister>();
1785 if (index.IsConstant()) {
1786 __ movss(out, Address(obj,
1787 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1788 } else {
1789 __ movss(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
1790 }
1791 break;
1792 }
1793
1794 case Primitive::kPrimDouble: {
1795 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1796 XmmRegister out = locations->Out().As<XmmRegister>();
1797 if (index.IsConstant()) {
1798 __ movsd(out, Address(obj,
1799 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
1800 } else {
1801 __ movsd(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
1802 }
1803 break;
1804 }
1805
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001806 case Primitive::kPrimVoid:
1807 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001808 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001809 }
1810}
1811
1812void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001813 Primitive::Type value_type = instruction->GetComponentType();
1814 bool is_object = value_type == Primitive::kPrimNot;
1815 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1816 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
1817 if (is_object) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001818 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001819 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1820 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1821 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001822 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001823 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001824 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001825 1, Location::RegisterOrConstant(instruction->InputAt(1)));
1826 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001827 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001828 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001829 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
1830 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001831 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001832 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001833 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001834 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001835}
1836
1837void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
1838 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001839 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001840 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001841 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001842 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001843
1844 switch (value_type) {
1845 case Primitive::kPrimBoolean:
1846 case Primitive::kPrimByte: {
1847 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001848 if (index.IsConstant()) {
1849 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001850 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001851 __ movb(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001852 } else {
1853 __ movb(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1854 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001855 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001856 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001857 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
1858 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001859 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001860 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001861 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1862 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001863 }
1864 break;
1865 }
1866
1867 case Primitive::kPrimShort:
1868 case Primitive::kPrimChar: {
1869 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001870 if (index.IsConstant()) {
1871 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001872 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001873 __ movw(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001874 } else {
1875 __ movw(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1876 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001877 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001878 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001879 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
1880 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001881 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001882 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001883 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1884 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001885 }
1886 break;
1887 }
1888
1889 case Primitive::kPrimInt: {
1890 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001891 if (index.IsConstant()) {
1892 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001893 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001894 __ movl(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001895 } else {
1896 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1897 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001898 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001899 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001900 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
1901 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001902 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001903 DCHECK(value.IsConstant()) << value;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001904 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001905 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1906 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001907 }
1908 break;
1909 }
1910
1911 case Primitive::kPrimNot: {
1912 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject), true));
1913 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001914 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001915 break;
1916 }
1917
1918 case Primitive::kPrimLong: {
1919 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001920 if (index.IsConstant()) {
1921 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001922 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001923 __ movq(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001924 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001925 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001926 __ movq(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
1927 value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001928 }
1929 break;
1930 }
1931
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001932 case Primitive::kPrimFloat: {
1933 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1934 if (index.IsConstant()) {
1935 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1936 DCHECK(value.IsFpuRegister());
1937 __ movss(Address(obj, offset), value.As<XmmRegister>());
1938 } else {
1939 DCHECK(value.IsFpuRegister());
1940 __ movss(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
1941 value.As<XmmRegister>());
1942 }
1943 break;
1944 }
1945
1946 case Primitive::kPrimDouble: {
1947 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1948 if (index.IsConstant()) {
1949 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1950 DCHECK(value.IsFpuRegister());
1951 __ movsd(Address(obj, offset), value.As<XmmRegister>());
1952 } else {
1953 DCHECK(value.IsFpuRegister());
1954 __ movsd(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
1955 value.As<XmmRegister>());
1956 }
1957 break;
1958 }
1959
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001960 case Primitive::kPrimVoid:
1961 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001962 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001963 }
1964}
1965
1966void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001967 LocationSummary* locations =
1968 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001969 locations->SetInAt(0, Location::RequiresRegister());
1970 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001971}
1972
1973void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
1974 LocationSummary* locations = instruction->GetLocations();
1975 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001976 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
1977 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001978 __ movl(out, Address(obj, offset));
1979}
1980
1981void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001982 LocationSummary* locations =
1983 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001984 locations->SetInAt(0, Location::RequiresRegister());
1985 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001986 if (instruction->HasUses()) {
1987 locations->SetOut(Location::SameAsFirstInput());
1988 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001989}
1990
1991void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
1992 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001993 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01001994 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001995 codegen_->AddSlowPath(slow_path);
1996
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001997 CpuRegister index = locations->InAt(0).As<CpuRegister>();
1998 CpuRegister length = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001999
2000 __ cmpl(index, length);
2001 __ j(kAboveEqual, slow_path->GetEntryLabel());
2002}
2003
2004void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
2005 CpuRegister card,
2006 CpuRegister object,
2007 CpuRegister value) {
2008 Label is_null;
2009 __ testl(value, value);
2010 __ j(kEqual, &is_null);
2011 __ gs()->movq(card, Address::Absolute(
2012 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
2013 __ movq(temp, object);
2014 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
2015 __ movb(Address(temp, card, TIMES_1, 0), card);
2016 __ Bind(&is_null);
2017}
2018
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002019void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
2020 temp->SetLocations(nullptr);
2021}
2022
2023void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
2024 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002025 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002026}
2027
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002028void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002029 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002030 LOG(FATAL) << "Unimplemented";
2031}
2032
2033void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002034 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2035}
2036
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002037void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
2038 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2039}
2040
2041void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002042 HBasicBlock* block = instruction->GetBlock();
2043 if (block->GetLoopInformation() != nullptr) {
2044 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2045 // The back edge will generate the suspend check.
2046 return;
2047 }
2048 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2049 // The goto will generate the suspend check.
2050 return;
2051 }
2052 GenerateSuspendCheck(instruction, nullptr);
2053}
2054
2055void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
2056 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002057 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002058 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002059 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002060 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002061 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002062 if (successor == nullptr) {
2063 __ j(kNotEqual, slow_path->GetEntryLabel());
2064 __ Bind(slow_path->GetReturnLabel());
2065 } else {
2066 __ j(kEqual, codegen_->GetLabelOf(successor));
2067 __ jmp(slow_path->GetEntryLabel());
2068 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002069}
2070
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002071X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
2072 return codegen_->GetAssembler();
2073}
2074
2075void ParallelMoveResolverX86_64::EmitMove(size_t index) {
2076 MoveOperands* move = moves_.Get(index);
2077 Location source = move->GetSource();
2078 Location destination = move->GetDestination();
2079
2080 if (source.IsRegister()) {
2081 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002082 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002083 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002084 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002085 source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002086 } else {
2087 DCHECK(destination.IsDoubleStackSlot());
2088 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002089 source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002090 }
2091 } else if (source.IsStackSlot()) {
2092 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002093 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002094 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002095 } else if (destination.IsFpuRegister()) {
2096 __ movss(destination.As<XmmRegister>(),
2097 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002098 } else {
2099 DCHECK(destination.IsStackSlot());
2100 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2101 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2102 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002103 } else if (source.IsDoubleStackSlot()) {
2104 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002105 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002106 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002107 } else if (destination.IsFpuRegister()) {
2108 __ movsd(destination.As<XmmRegister>(), Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002109 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01002110 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002111 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2112 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2113 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002114 } else if (source.IsConstant()) {
2115 HConstant* constant = source.GetConstant();
2116 if (constant->IsIntConstant()) {
2117 Immediate imm(constant->AsIntConstant()->GetValue());
2118 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002119 __ movl(destination.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002120 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002121 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002122 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2123 }
2124 } else if (constant->IsLongConstant()) {
2125 int64_t value = constant->AsLongConstant()->GetValue();
2126 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002127 __ movq(destination.As<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002128 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002129 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002130 __ movq(CpuRegister(TMP), Immediate(value));
2131 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2132 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002133 } else if (constant->IsFloatConstant()) {
2134 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
2135 if (destination.IsFpuRegister()) {
2136 __ movl(CpuRegister(TMP), imm);
2137 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2138 } else {
2139 DCHECK(destination.IsStackSlot()) << destination;
2140 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2141 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002142 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002143 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
2144 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
2145 if (destination.IsFpuRegister()) {
2146 __ movq(CpuRegister(TMP), imm);
2147 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2148 } else {
2149 DCHECK(destination.IsDoubleStackSlot()) << destination;
2150 __ movq(CpuRegister(TMP), imm);
2151 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2152 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002153 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002154 } else if (source.IsFpuRegister()) {
2155 if (destination.IsFpuRegister()) {
2156 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
2157 } else if (destination.IsStackSlot()) {
2158 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
2159 source.As<XmmRegister>());
2160 } else {
2161 DCHECK(destination.IsDoubleStackSlot());
2162 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
2163 source.As<XmmRegister>());
2164 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002165 }
2166}
2167
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002168void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002169 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002170 __ movl(Address(CpuRegister(RSP), mem), reg);
2171 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002172}
2173
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002174void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002175 ScratchRegisterScope ensure_scratch(
2176 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2177
2178 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2179 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2180 __ movl(CpuRegister(ensure_scratch.GetRegister()),
2181 Address(CpuRegister(RSP), mem2 + stack_offset));
2182 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2183 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
2184 CpuRegister(ensure_scratch.GetRegister()));
2185}
2186
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002187void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
2188 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2189 __ movq(Address(CpuRegister(RSP), mem), reg);
2190 __ movq(reg, CpuRegister(TMP));
2191}
2192
2193void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
2194 ScratchRegisterScope ensure_scratch(
2195 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2196
2197 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2198 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2199 __ movq(CpuRegister(ensure_scratch.GetRegister()),
2200 Address(CpuRegister(RSP), mem2 + stack_offset));
2201 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2202 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
2203 CpuRegister(ensure_scratch.GetRegister()));
2204}
2205
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002206void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
2207 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2208 __ movss(Address(CpuRegister(RSP), mem), reg);
2209 __ movd(reg, CpuRegister(TMP));
2210}
2211
2212void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
2213 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2214 __ movsd(Address(CpuRegister(RSP), mem), reg);
2215 __ movd(reg, CpuRegister(TMP));
2216}
2217
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002218void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
2219 MoveOperands* move = moves_.Get(index);
2220 Location source = move->GetSource();
2221 Location destination = move->GetDestination();
2222
2223 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002224 __ xchgq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002225 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002226 Exchange32(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002227 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002228 Exchange32(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002229 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002230 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
2231 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002232 Exchange64(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002233 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002234 Exchange64(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002235 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
2236 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002237 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
2238 __ movd(CpuRegister(TMP), source.As<XmmRegister>());
2239 __ movaps(source.As<XmmRegister>(), destination.As<XmmRegister>());
2240 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2241 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
2242 Exchange32(source.As<XmmRegister>(), destination.GetStackIndex());
2243 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
2244 Exchange32(destination.As<XmmRegister>(), source.GetStackIndex());
2245 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
2246 Exchange64(source.As<XmmRegister>(), destination.GetStackIndex());
2247 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
2248 Exchange64(destination.As<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002249 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002250 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002251 }
2252}
2253
2254
2255void ParallelMoveResolverX86_64::SpillScratch(int reg) {
2256 __ pushq(CpuRegister(reg));
2257}
2258
2259
2260void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
2261 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002262}
2263
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002264void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
2265 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
2266 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2267 Immediate(mirror::Class::kStatusInitialized));
2268 __ j(kLess, slow_path->GetEntryLabel());
2269 __ Bind(slow_path->GetExitLabel());
2270 // No need for memory fence, thanks to the X86_64 memory model.
2271}
2272
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002273void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002274 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2275 ? LocationSummary::kCallOnSlowPath
2276 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002277 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002278 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002279 locations->SetOut(Location::RequiresRegister());
2280}
2281
2282void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
2283 CpuRegister out = cls->GetLocations()->Out().As<CpuRegister>();
2284 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002285 DCHECK(!cls->CanCallRuntime());
2286 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002287 codegen_->LoadCurrentMethod(out);
2288 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2289 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002290 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002291 codegen_->LoadCurrentMethod(out);
2292 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2293 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002294 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
2295 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2296 codegen_->AddSlowPath(slow_path);
2297 __ testl(out, out);
2298 __ j(kEqual, slow_path->GetEntryLabel());
2299 if (cls->MustGenerateClinitCheck()) {
2300 GenerateClassInitializationCheck(slow_path, out);
2301 } else {
2302 __ Bind(slow_path->GetExitLabel());
2303 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002304 }
2305}
2306
2307void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
2308 LocationSummary* locations =
2309 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2310 locations->SetInAt(0, Location::RequiresRegister());
2311 if (check->HasUses()) {
2312 locations->SetOut(Location::SameAsFirstInput());
2313 }
2314}
2315
2316void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002317 // We assume the class to not be null.
2318 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
2319 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002320 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002321 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002322}
2323
2324void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2325 LocationSummary* locations =
2326 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2327 locations->SetInAt(0, Location::RequiresRegister());
2328 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2329}
2330
2331void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2332 LocationSummary* locations = instruction->GetLocations();
2333 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
2334 CpuRegister out = locations->Out().As<CpuRegister>();
2335 size_t offset = instruction->GetFieldOffset().SizeValue();
2336
2337 switch (instruction->GetType()) {
2338 case Primitive::kPrimBoolean: {
2339 __ movzxb(out, Address(cls, offset));
2340 break;
2341 }
2342
2343 case Primitive::kPrimByte: {
2344 __ movsxb(out, Address(cls, offset));
2345 break;
2346 }
2347
2348 case Primitive::kPrimShort: {
2349 __ movsxw(out, Address(cls, offset));
2350 break;
2351 }
2352
2353 case Primitive::kPrimChar: {
2354 __ movzxw(out, Address(cls, offset));
2355 break;
2356 }
2357
2358 case Primitive::kPrimInt:
2359 case Primitive::kPrimNot: {
2360 __ movl(out, Address(cls, offset));
2361 break;
2362 }
2363
2364 case Primitive::kPrimLong: {
2365 __ movq(out, Address(cls, offset));
2366 break;
2367 }
2368
2369 case Primitive::kPrimFloat:
2370 case Primitive::kPrimDouble:
2371 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
2372 UNREACHABLE();
2373 case Primitive::kPrimVoid:
2374 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2375 UNREACHABLE();
2376 }
2377}
2378
2379void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2380 LocationSummary* locations =
2381 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2382 Primitive::Type field_type = instruction->GetFieldType();
2383 bool is_object_type = field_type == Primitive::kPrimNot;
2384 locations->SetInAt(0, Location::RequiresRegister());
2385 locations->SetInAt(1, Location::RequiresRegister());
2386 if (is_object_type) {
2387 // Temporary registers for the write barrier.
2388 locations->AddTemp(Location::RequiresRegister());
2389 locations->AddTemp(Location::RequiresRegister());
2390 }
2391}
2392
2393void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2394 LocationSummary* locations = instruction->GetLocations();
2395 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
2396 CpuRegister value = locations->InAt(1).As<CpuRegister>();
2397 size_t offset = instruction->GetFieldOffset().SizeValue();
2398 Primitive::Type field_type = instruction->GetFieldType();
2399
2400 switch (field_type) {
2401 case Primitive::kPrimBoolean:
2402 case Primitive::kPrimByte: {
2403 __ movb(Address(cls, offset), value);
2404 break;
2405 }
2406
2407 case Primitive::kPrimShort:
2408 case Primitive::kPrimChar: {
2409 __ movw(Address(cls, offset), value);
2410 break;
2411 }
2412
2413 case Primitive::kPrimInt:
2414 case Primitive::kPrimNot: {
2415 __ movl(Address(cls, offset), value);
2416 if (field_type == Primitive::kPrimNot) {
2417 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
2418 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
2419 codegen_->MarkGCCard(temp, card, cls, value);
2420 }
2421 break;
2422 }
2423
2424 case Primitive::kPrimLong: {
2425 __ movq(Address(cls, offset), value);
2426 break;
2427 }
2428
2429 case Primitive::kPrimFloat:
2430 case Primitive::kPrimDouble:
2431 LOG(FATAL) << "Unimplemented register type " << field_type;
2432 UNREACHABLE();
2433 case Primitive::kPrimVoid:
2434 LOG(FATAL) << "Unreachable type " << field_type;
2435 UNREACHABLE();
2436 }
2437}
2438
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002439void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
2440 LocationSummary* locations =
2441 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2442 locations->SetOut(Location::RequiresRegister());
2443}
2444
2445void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
2446 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
2447 codegen_->AddSlowPath(slow_path);
2448
2449 CpuRegister out = load->GetLocations()->Out().As<CpuRegister>();
2450 codegen_->LoadCurrentMethod(CpuRegister(out));
2451 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
2452 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2453 __ testl(out, out);
2454 __ j(kEqual, slow_path->GetEntryLabel());
2455 __ Bind(slow_path->GetExitLabel());
2456}
2457
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002458} // namespace x86_64
2459} // namespace art