blob: f7cdee9c681ef1365dd44313375ea75771e52497 [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
Calin Juravled0d48522014-11-04 16:40:20 +000093class DivZeroCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
94 public:
95 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : instruction_(instruction) {}
96
97 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
98 __ Bind(GetEntryLabel());
99 __ gs()->call(
100 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowDivZero), true));
101 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
102 }
103
104 private:
105 HDivZeroCheck* const instruction_;
106 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
107};
108
109class DivMinusOneSlowPathX86_64 : public SlowPathCodeX86_64 {
110 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000111 explicit DivMinusOneSlowPathX86_64(Register reg, Primitive::Type type)
112 : reg_(reg), type_(type) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000113
114 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
115 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000116 if (type_ == Primitive::kPrimInt) {
117 __ negl(CpuRegister(reg_));
118 } else {
119 DCHECK_EQ(Primitive::kPrimLong, type_);
120 __ negq(CpuRegister(reg_));
121 }
Calin Juravled0d48522014-11-04 16:40:20 +0000122 __ jmp(GetExitLabel());
123 }
124
125 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000126 const Register reg_;
127 const Primitive::Type type_;
Calin Juravled0d48522014-11-04 16:40:20 +0000128 DISALLOW_COPY_AND_ASSIGN(DivMinusOneSlowPathX86_64);
129};
130
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100131class StackOverflowCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100132 public:
133 StackOverflowCheckSlowPathX86_64() {}
134
135 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
136 __ Bind(GetEntryLabel());
137 __ addq(CpuRegister(RSP),
138 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
139 __ gs()->jmp(
140 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowStackOverflow), true));
141 }
142
143 private:
144 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86_64);
145};
146
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100147class SuspendCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000148 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100149 explicit SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
150 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000151
152 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100153 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000154 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100155 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000156 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pTestSuspend), true));
157 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100158 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100159 if (successor_ == nullptr) {
160 __ jmp(GetReturnLabel());
161 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100162 __ jmp(x64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100163 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000164 }
165
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100166 Label* GetReturnLabel() {
167 DCHECK(successor_ == nullptr);
168 return &return_label_;
169 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000170
171 private:
172 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100173 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000174 Label return_label_;
175
176 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
177};
178
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100179class BoundsCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100180 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100181 BoundsCheckSlowPathX86_64(HBoundsCheck* instruction,
182 Location index_location,
183 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100184 : instruction_(instruction),
185 index_location_(index_location),
186 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100187
188 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100189 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000190 // We're moving two locations to locations that could overlap, so we need a parallel
191 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100192 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000193 codegen->EmitParallelMoves(
194 index_location_,
195 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
196 length_location_,
197 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100198 __ gs()->call(Address::Absolute(
199 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pThrowArrayBounds), true));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100200 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100201 }
202
203 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100204 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100205 const Location index_location_;
206 const Location length_location_;
207
208 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
209};
210
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000211class LoadClassSlowPathX86_64 : public SlowPathCodeX86_64 {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100212 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000213 LoadClassSlowPathX86_64(HLoadClass* cls,
214 HInstruction* at,
215 uint32_t dex_pc,
216 bool do_clinit)
217 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
218 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
219 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100220
221 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000222 LocationSummary* locations = at_->GetLocations();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100223 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
224 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100225
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000226 codegen->SaveLiveRegisters(locations);
227
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100228 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000229 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100230 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000231 __ gs()->call(Address::Absolute((do_clinit_
232 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeStaticStorage)
233 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInitializeType)) , true));
234 codegen->RecordPcInfo(at_, dex_pc_);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100235
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000236 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000237 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000238 if (out.IsValid()) {
239 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
240 x64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000241 }
242
243 codegen->RestoreLiveRegisters(locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100244 __ jmp(GetExitLabel());
245 }
246
247 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000248 // The class this slow path will load.
249 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100250
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000251 // The instruction where this slow path is happening.
252 // (Might be the load class or an initialization check).
253 HInstruction* const at_;
254
255 // The dex PC of `at_`.
256 const uint32_t dex_pc_;
257
258 // Whether to initialize the class.
259 const bool do_clinit_;
260
261 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100262};
263
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000264class LoadStringSlowPathX86_64 : public SlowPathCodeX86_64 {
265 public:
266 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
267
268 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
269 LocationSummary* locations = instruction_->GetLocations();
270 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
271
272 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
273 __ Bind(GetEntryLabel());
274 codegen->SaveLiveRegisters(locations);
275
276 InvokeRuntimeCallingConvention calling_convention;
277 x64_codegen->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(0)));
278 __ movl(CpuRegister(calling_convention.GetRegisterAt(1)),
279 Immediate(instruction_->GetStringIndex()));
280 __ gs()->call(Address::Absolute(
281 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pResolveString), true));
282 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
283 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
284 codegen->RestoreLiveRegisters(locations);
285 __ jmp(GetExitLabel());
286 }
287
288 private:
289 HLoadString* const instruction_;
290
291 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
292};
293
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000294class TypeCheckSlowPathX86_64 : public SlowPathCodeX86_64 {
295 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000296 TypeCheckSlowPathX86_64(HInstruction* instruction,
297 Location class_to_check,
298 Location object_class,
299 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000300 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000301 class_to_check_(class_to_check),
302 object_class_(object_class),
303 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000304
305 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
306 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000307 DCHECK(instruction_->IsCheckCast()
308 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000309
310 CodeGeneratorX86_64* x64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
311 __ Bind(GetEntryLabel());
312 codegen->SaveLiveRegisters(locations);
313
314 // We're moving two locations to locations that could overlap, so we need a parallel
315 // move resolver.
316 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000317 codegen->EmitParallelMoves(
318 class_to_check_,
319 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
320 object_class_,
321 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000322
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000323 if (instruction_->IsInstanceOf()) {
324 __ gs()->call(
325 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pInstanceofNonTrivial), true));
326 } else {
327 DCHECK(instruction_->IsCheckCast());
328 __ gs()->call(
329 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pCheckCast), true));
330 }
331 codegen->RecordPcInfo(instruction_, dex_pc_);
332
333 if (instruction_->IsInstanceOf()) {
334 x64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
335 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000336
337 codegen->RestoreLiveRegisters(locations);
338 __ jmp(GetExitLabel());
339 }
340
341 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000342 HInstruction* const instruction_;
343 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000344 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000345 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000346
347 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
348};
349
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100350#undef __
351#define __ reinterpret_cast<X86_64Assembler*>(GetAssembler())->
352
Dave Allison20dfc792014-06-16 20:44:29 -0700353inline Condition X86_64Condition(IfCondition cond) {
354 switch (cond) {
355 case kCondEQ: return kEqual;
356 case kCondNE: return kNotEqual;
357 case kCondLT: return kLess;
358 case kCondLE: return kLessEqual;
359 case kCondGT: return kGreater;
360 case kCondGE: return kGreaterEqual;
361 default:
362 LOG(FATAL) << "Unknown if condition";
363 }
364 return kEqual;
365}
366
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100367void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
368 stream << X86_64ManagedRegister::FromCpuRegister(Register(reg));
369}
370
371void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
372 stream << X86_64ManagedRegister::FromXmmRegister(FloatRegister(reg));
373}
374
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100375size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
376 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
377 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100378}
379
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100380size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
381 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
382 return kX86_64WordSize;
383}
384
385size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
386 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
387 return kX86_64WordSize;
388}
389
390size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
391 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
392 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100393}
394
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100395CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100396 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfFloatRegisters, 0),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100397 block_labels_(graph->GetArena(), 0),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100398 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000399 instruction_visitor_(graph, this),
400 move_resolver_(graph->GetArena(), this) {}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100401
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100402size_t CodeGeneratorX86_64::FrameEntrySpillSize() const {
403 return kNumberOfPushedRegistersAtEntry * kX86_64WordSize;
404}
405
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100406InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
407 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100408 : HGraphVisitor(graph),
409 assembler_(codegen->GetAssembler()),
410 codegen_(codegen) {}
411
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100412Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100413 switch (type) {
414 case Primitive::kPrimLong:
415 case Primitive::kPrimByte:
416 case Primitive::kPrimBoolean:
417 case Primitive::kPrimChar:
418 case Primitive::kPrimShort:
419 case Primitive::kPrimInt:
420 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100421 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100422 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100423 }
424
425 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100426 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100427 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100428 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100429 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100430
431 case Primitive::kPrimVoid:
432 LOG(FATAL) << "Unreachable type " << type;
433 }
434
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100435 return Location();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100436}
437
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100438void CodeGeneratorX86_64::SetupBlockedRegisters() const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100439 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100440 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100441
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000442 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100443 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000444
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100445 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100446 blocked_core_registers_[RBX] = true;
447 blocked_core_registers_[RBP] = true;
448 blocked_core_registers_[R12] = true;
449 blocked_core_registers_[R13] = true;
450 blocked_core_registers_[R14] = true;
451 blocked_core_registers_[R15] = true;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100452
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100453 blocked_fpu_registers_[XMM12] = true;
454 blocked_fpu_registers_[XMM13] = true;
455 blocked_fpu_registers_[XMM14] = true;
456 blocked_fpu_registers_[XMM15] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100457}
458
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100459void CodeGeneratorX86_64::GenerateFrameEntry() {
460 // Create a fake register to mimic Quick.
461 static const int kFakeReturnRegister = 16;
462 core_spill_mask_ |= (1 << kFakeReturnRegister);
463
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100464 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700465 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100466
467 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
468 __ testq(CpuRegister(RAX), Address(
469 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100470 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100471 }
472
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100473 // The return PC has already been pushed on the stack.
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100474 __ subq(CpuRegister(RSP),
475 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
476
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100477 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100478 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86_64();
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100479 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100480
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100481 __ gs()->cmpq(CpuRegister(RSP),
482 Address::Absolute(Thread::StackEndOffset<kX86_64WordSize>(), true));
483 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100484 }
485
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100486 __ movl(Address(CpuRegister(RSP), kCurrentMethodStackOffset), CpuRegister(RDI));
487}
488
489void CodeGeneratorX86_64::GenerateFrameExit() {
490 __ addq(CpuRegister(RSP),
491 Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86_64WordSize));
492}
493
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100494void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
495 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100496}
497
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100498void CodeGeneratorX86_64::LoadCurrentMethod(CpuRegister reg) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100499 __ movl(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
500}
501
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100502Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
503 switch (load->GetType()) {
504 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100505 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100506 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
507 break;
508
509 case Primitive::kPrimInt:
510 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100511 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100512 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100513
514 case Primitive::kPrimBoolean:
515 case Primitive::kPrimByte:
516 case Primitive::kPrimChar:
517 case Primitive::kPrimShort:
518 case Primitive::kPrimVoid:
519 LOG(FATAL) << "Unexpected type " << load->GetType();
520 }
521
522 LOG(FATAL) << "Unreachable";
523 return Location();
524}
525
526void CodeGeneratorX86_64::Move(Location destination, Location source) {
527 if (source.Equals(destination)) {
528 return;
529 }
530 if (destination.IsRegister()) {
531 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100532 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100533 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100534 __ movd(destination.As<CpuRegister>(), source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100535 } else if (source.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100536 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100537 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100538 } else {
539 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100540 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100541 Address(CpuRegister(RSP), source.GetStackIndex()));
542 }
543 } else if (destination.IsFpuRegister()) {
544 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100545 __ movd(destination.As<XmmRegister>(), source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100546 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100547 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100548 } else if (source.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100549 __ movss(destination.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100550 Address(CpuRegister(RSP), source.GetStackIndex()));
551 } else {
552 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100553 __ movsd(destination.As<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100554 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100555 }
556 } else if (destination.IsStackSlot()) {
557 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100558 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100559 source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100560 } else if (source.IsFpuRegister()) {
561 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100562 source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100563 } else {
564 DCHECK(source.IsStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000565 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
566 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100567 }
568 } else {
569 DCHECK(destination.IsDoubleStackSlot());
570 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100571 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100572 source.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100573 } else if (source.IsFpuRegister()) {
574 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100575 source.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100576 } else {
577 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000578 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
579 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100580 }
581 }
582}
583
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100584void CodeGeneratorX86_64::Move(HInstruction* instruction,
585 Location location,
586 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000587 LocationSummary* locations = instruction->GetLocations();
588 if (locations != nullptr && locations->Out().Equals(location)) {
589 return;
590 }
591
592 if (locations != nullptr && locations->Out().IsConstant()) {
593 HConstant* const_to_move = locations->Out().GetConstant();
594 if (const_to_move->IsIntConstant()) {
595 Immediate imm(const_to_move->AsIntConstant()->GetValue());
596 if (location.IsRegister()) {
597 __ movl(location.As<CpuRegister>(), imm);
598 } else if (location.IsStackSlot()) {
599 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
600 } else {
601 DCHECK(location.IsConstant());
602 DCHECK_EQ(location.GetConstant(), const_to_move);
603 }
604 } else if (const_to_move->IsLongConstant()) {
605 int64_t value = const_to_move->AsLongConstant()->GetValue();
606 if (location.IsRegister()) {
607 __ movq(location.As<CpuRegister>(), Immediate(value));
608 } else if (location.IsDoubleStackSlot()) {
609 __ movq(CpuRegister(TMP), Immediate(value));
610 __ movq(Address(CpuRegister(RSP), location.GetStackIndex()), CpuRegister(TMP));
611 } else {
612 DCHECK(location.IsConstant());
613 DCHECK_EQ(location.GetConstant(), const_to_move);
614 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100615 }
Roland Levillain476df552014-10-09 17:51:36 +0100616 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100617 switch (instruction->GetType()) {
618 case Primitive::kPrimBoolean:
619 case Primitive::kPrimByte:
620 case Primitive::kPrimChar:
621 case Primitive::kPrimShort:
622 case Primitive::kPrimInt:
623 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100624 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100625 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
626 break;
627
628 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100629 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100630 Move(location, Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
631 break;
632
633 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100634 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100635 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000636 } else if (instruction->IsTemporary()) {
637 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
638 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100639 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100640 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100641 switch (instruction->GetType()) {
642 case Primitive::kPrimBoolean:
643 case Primitive::kPrimByte:
644 case Primitive::kPrimChar:
645 case Primitive::kPrimShort:
646 case Primitive::kPrimInt:
647 case Primitive::kPrimNot:
648 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100649 case Primitive::kPrimFloat:
650 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000651 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100652 break;
653
654 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100655 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100656 }
657 }
658}
659
660void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
661 got->SetLocations(nullptr);
662}
663
664void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
665 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100666 DCHECK(!successor->IsExitBlock());
667
668 HBasicBlock* block = got->GetBlock();
669 HInstruction* previous = got->GetPrevious();
670
671 HLoopInformation* info = block->GetLoopInformation();
672 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
673 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
674 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
675 return;
676 }
677
678 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
679 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
680 }
681 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100682 __ jmp(codegen_->GetLabelOf(successor));
683 }
684}
685
686void LocationsBuilderX86_64::VisitExit(HExit* exit) {
687 exit->SetLocations(nullptr);
688}
689
690void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700691 UNUSED(exit);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100692 if (kIsDebugBuild) {
693 __ Comment("Unreachable");
694 __ int3();
695 }
696}
697
698void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100699 LocationSummary* locations =
700 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100701 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100702 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100703 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100704 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100705}
706
707void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700708 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100709 if (cond->IsIntConstant()) {
710 // Constant condition, statically compared against 1.
711 int32_t cond_value = cond->AsIntConstant()->GetValue();
712 if (cond_value == 1) {
713 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
714 if_instr->IfTrueSuccessor())) {
715 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100716 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100717 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100718 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100719 DCHECK_EQ(cond_value, 0);
720 }
721 } else {
722 bool materialized =
723 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
724 // Moves do not affect the eflags register, so if the condition is
725 // evaluated just before the if, we don't need to evaluate it
726 // again.
727 bool eflags_set = cond->IsCondition()
728 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
729 if (materialized) {
730 if (!eflags_set) {
731 // Materialized condition, compare against 0.
732 Location lhs = if_instr->GetLocations()->InAt(0);
733 if (lhs.IsRegister()) {
734 __ cmpl(lhs.As<CpuRegister>(), Immediate(0));
735 } else {
736 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()),
737 Immediate(0));
738 }
739 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
740 } else {
741 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
742 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
743 }
744 } else {
745 Location lhs = cond->GetLocations()->InAt(0);
746 Location rhs = cond->GetLocations()->InAt(1);
747 if (rhs.IsRegister()) {
748 __ cmpl(lhs.As<CpuRegister>(), rhs.As<CpuRegister>());
749 } else if (rhs.IsConstant()) {
750 __ cmpl(lhs.As<CpuRegister>(),
751 Immediate(rhs.GetConstant()->AsIntConstant()->GetValue()));
752 } else {
753 __ cmpl(lhs.As<CpuRegister>(),
754 Address(CpuRegister(RSP), rhs.GetStackIndex()));
755 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100756 __ j(X86_64Condition(cond->AsCondition()->GetCondition()),
757 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700758 }
Dave Allison20dfc792014-06-16 20:44:29 -0700759 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100760 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
761 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700762 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100763 }
764}
765
766void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
767 local->SetLocations(nullptr);
768}
769
770void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
771 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
772}
773
774void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
775 local->SetLocations(nullptr);
776}
777
778void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load) {
779 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700780 UNUSED(load);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100781}
782
783void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100784 LocationSummary* locations =
785 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100786 switch (store->InputAt(1)->GetType()) {
787 case Primitive::kPrimBoolean:
788 case Primitive::kPrimByte:
789 case Primitive::kPrimChar:
790 case Primitive::kPrimShort:
791 case Primitive::kPrimInt:
792 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100793 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100794 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
795 break;
796
797 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100798 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100799 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
800 break;
801
802 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100803 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100804 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100805}
806
807void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700808 UNUSED(store);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100809}
810
Dave Allison20dfc792014-06-16 20:44:29 -0700811void LocationsBuilderX86_64::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100812 LocationSummary* locations =
813 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100814 locations->SetInAt(0, Location::RequiresRegister());
815 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100816 if (comp->NeedsMaterialization()) {
817 locations->SetOut(Location::RequiresRegister());
818 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100819}
820
Dave Allison20dfc792014-06-16 20:44:29 -0700821void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* comp) {
822 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100823 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100824 CpuRegister reg = locations->Out().As<CpuRegister>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100825 // Clear register: setcc only sets the low byte.
826 __ xorq(reg, reg);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100827 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100828 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100829 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100830 } else if (locations->InAt(1).IsConstant()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100831 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100832 Immediate(locations->InAt(1).GetConstant()->AsIntConstant()->GetValue()));
833 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100834 __ cmpl(locations->InAt(0).As<CpuRegister>(),
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100835 Address(CpuRegister(RSP), locations->InAt(1).GetStackIndex()));
836 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100837 __ setcc(X86_64Condition(comp->GetCondition()), reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700838 }
839}
840
841void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
842 VisitCondition(comp);
843}
844
845void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
846 VisitCondition(comp);
847}
848
849void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
850 VisitCondition(comp);
851}
852
853void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
854 VisitCondition(comp);
855}
856
857void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
858 VisitCondition(comp);
859}
860
861void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
862 VisitCondition(comp);
863}
864
865void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
866 VisitCondition(comp);
867}
868
869void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
870 VisitCondition(comp);
871}
872
873void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
874 VisitCondition(comp);
875}
876
877void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
878 VisitCondition(comp);
879}
880
881void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
882 VisitCondition(comp);
883}
884
885void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
886 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100887}
888
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100889void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100890 LocationSummary* locations =
891 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100892 locations->SetInAt(0, Location::RequiresRegister());
893 locations->SetInAt(1, Location::RequiresRegister());
894 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100895}
896
897void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
898 Label greater, done;
899 LocationSummary* locations = compare->GetLocations();
900 switch (compare->InputAt(0)->GetType()) {
901 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100902 __ cmpq(locations->InAt(0).As<CpuRegister>(),
903 locations->InAt(1).As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100904 break;
905 default:
906 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
907 }
908
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100909 CpuRegister output = locations->Out().As<CpuRegister>();
910 __ movl(output, Immediate(0));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100911 __ j(kEqual, &done);
912 __ j(kGreater, &greater);
913
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100914 __ movl(output, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100915 __ jmp(&done);
916
917 __ Bind(&greater);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100918 __ movl(output, Immediate(1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100919
920 __ Bind(&done);
921}
922
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100923void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100924 LocationSummary* locations =
925 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100926 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100927}
928
929void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100930 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700931 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100932}
933
934void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100935 LocationSummary* locations =
936 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100937 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100938}
939
940void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100941 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700942 UNUSED(constant);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100943}
944
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100945void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
946 LocationSummary* locations =
947 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
948 locations->SetOut(Location::ConstantLocation(constant));
949}
950
951void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant) {
952 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700953 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100954}
955
956void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
957 LocationSummary* locations =
958 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
959 locations->SetOut(Location::ConstantLocation(constant));
960}
961
962void InstructionCodeGeneratorX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
963 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700964 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100965}
966
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100967void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
968 ret->SetLocations(nullptr);
969}
970
971void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700972 UNUSED(ret);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100973 codegen_->GenerateFrameExit();
974 __ ret();
975}
976
977void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100978 LocationSummary* locations =
979 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100980 switch (ret->InputAt(0)->GetType()) {
981 case Primitive::kPrimBoolean:
982 case Primitive::kPrimByte:
983 case Primitive::kPrimChar:
984 case Primitive::kPrimShort:
985 case Primitive::kPrimInt:
986 case Primitive::kPrimNot:
987 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100988 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100989 break;
990
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100991 case Primitive::kPrimFloat:
992 case Primitive::kPrimDouble:
993 locations->SetInAt(0,
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100994 Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100995 break;
996
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100997 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100998 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100999 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001000}
1001
1002void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1003 if (kIsDebugBuild) {
1004 switch (ret->InputAt(0)->GetType()) {
1005 case Primitive::kPrimBoolean:
1006 case Primitive::kPrimByte:
1007 case Primitive::kPrimChar:
1008 case Primitive::kPrimShort:
1009 case Primitive::kPrimInt:
1010 case Primitive::kPrimNot:
1011 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001012 DCHECK_EQ(ret->GetLocations()->InAt(0).As<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001013 break;
1014
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001015 case Primitive::kPrimFloat:
1016 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001017 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001018 XMM0);
1019 break;
1020
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001021 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001022 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001023 }
1024 }
1025 codegen_->GenerateFrameExit();
1026 __ ret();
1027}
1028
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001029Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
1030 switch (type) {
1031 case Primitive::kPrimBoolean:
1032 case Primitive::kPrimByte:
1033 case Primitive::kPrimChar:
1034 case Primitive::kPrimShort:
1035 case Primitive::kPrimInt:
1036 case Primitive::kPrimNot: {
1037 uint32_t index = gp_index_++;
1038 stack_index_++;
1039 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001040 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001041 } else {
1042 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1043 }
1044 }
1045
1046 case Primitive::kPrimLong: {
1047 uint32_t index = gp_index_;
1048 stack_index_ += 2;
1049 if (index < calling_convention.GetNumberOfRegisters()) {
1050 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001051 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001052 } else {
1053 gp_index_ += 2;
1054 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1055 }
1056 }
1057
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001058 case Primitive::kPrimFloat: {
1059 uint32_t index = fp_index_++;
1060 stack_index_++;
1061 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001062 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001063 } else {
1064 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1065 }
1066 }
1067
1068 case Primitive::kPrimDouble: {
1069 uint32_t index = fp_index_++;
1070 stack_index_ += 2;
1071 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001072 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001073 } else {
1074 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1075 }
1076 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001077
1078 case Primitive::kPrimVoid:
1079 LOG(FATAL) << "Unexpected parameter type " << type;
1080 break;
1081 }
1082 return Location();
1083}
1084
1085void LocationsBuilderX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001086 HandleInvoke(invoke);
1087}
1088
1089void InstructionCodeGeneratorX86_64::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001090 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001091 // TODO: Implement all kinds of calls:
1092 // 1) boot -> boot
1093 // 2) app -> boot
1094 // 3) app -> app
1095 //
1096 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1097
1098 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001099 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001100 // temp = temp->dex_cache_resolved_methods_;
1101 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().SizeValue()));
1102 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001103 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001104 // (temp + offset_of_quick_compiled_code)()
1105 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
1106
1107 DCHECK(!codegen_->IsLeafMethod());
1108 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1109}
1110
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001111void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001112 LocationSummary* locations =
1113 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001114 locations->AddTemp(Location::RegisterLocation(RDI));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001115
1116 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001117 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001118 HInstruction* input = invoke->InputAt(i);
1119 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1120 }
1121
1122 switch (invoke->GetType()) {
1123 case Primitive::kPrimBoolean:
1124 case Primitive::kPrimByte:
1125 case Primitive::kPrimChar:
1126 case Primitive::kPrimShort:
1127 case Primitive::kPrimInt:
1128 case Primitive::kPrimNot:
1129 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001130 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001131 break;
1132
1133 case Primitive::kPrimVoid:
1134 break;
1135
1136 case Primitive::kPrimDouble:
1137 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001138 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001139 break;
1140 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001141}
1142
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001143void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1144 HandleInvoke(invoke);
1145}
1146
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001147void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001148 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001149 size_t method_offset = mirror::Class::EmbeddedVTableOffset().SizeValue() +
1150 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1151 LocationSummary* locations = invoke->GetLocations();
1152 Location receiver = locations->InAt(0);
1153 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1154 // temp = object->GetClass();
1155 if (receiver.IsStackSlot()) {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001156 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1157 __ movl(temp, Address(temp, class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001158 } else {
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001159 __ movl(temp, Address(receiver.As<CpuRegister>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001160 }
1161 // temp = temp->GetMethodAt(method_offset);
1162 __ movl(temp, Address(temp, method_offset));
1163 // call temp->GetEntryPoint();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001164 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
1165
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001166 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001167 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001168}
1169
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001170void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1171 HandleInvoke(invoke);
1172 // Add the hidden argument.
1173 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
1174}
1175
1176void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
1177 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1178 CpuRegister temp = invoke->GetLocations()->GetTemp(0).As<CpuRegister>();
1179 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1180 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1181 LocationSummary* locations = invoke->GetLocations();
1182 Location receiver = locations->InAt(0);
1183 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
1184
1185 // Set the hidden argument.
1186 __ movq(invoke->GetLocations()->GetTemp(1).As<CpuRegister>(),
1187 Immediate(invoke->GetDexMethodIndex()));
1188
1189 // temp = object->GetClass();
1190 if (receiver.IsStackSlot()) {
1191 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
1192 __ movl(temp, Address(temp, class_offset));
1193 } else {
1194 __ movl(temp, Address(receiver.As<CpuRegister>(), class_offset));
1195 }
1196 // temp = temp->GetImtEntryAt(method_offset);
1197 __ movl(temp, Address(temp, method_offset));
1198 // call temp->GetEntryPoint();
1199 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().SizeValue()));
1200
1201 DCHECK(!codegen_->IsLeafMethod());
1202 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1203}
1204
Roland Levillain88cb1752014-10-20 16:36:47 +01001205void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
1206 LocationSummary* locations =
1207 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1208 switch (neg->GetResultType()) {
1209 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001210 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001211 locations->SetInAt(0, Location::RequiresRegister());
1212 locations->SetOut(Location::SameAsFirstInput());
1213 break;
1214
Roland Levillain88cb1752014-10-20 16:36:47 +01001215 case Primitive::kPrimFloat:
1216 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001217 locations->SetInAt(0, Location::RequiresFpuRegister());
1218 // Output overlaps as we need a fresh (zero-initialized)
1219 // register to perform subtraction from zero.
1220 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001221 break;
1222
1223 default:
1224 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1225 }
1226}
1227
1228void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
1229 LocationSummary* locations = neg->GetLocations();
1230 Location out = locations->Out();
1231 Location in = locations->InAt(0);
1232 switch (neg->GetResultType()) {
1233 case Primitive::kPrimInt:
1234 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001235 DCHECK(in.Equals(out));
Roland Levillain88cb1752014-10-20 16:36:47 +01001236 __ negl(out.As<CpuRegister>());
1237 break;
1238
1239 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001240 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001241 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001242 __ negq(out.As<CpuRegister>());
1243 break;
1244
Roland Levillain88cb1752014-10-20 16:36:47 +01001245 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001246 DCHECK(in.IsFpuRegister());
1247 DCHECK(out.IsFpuRegister());
1248 DCHECK(!in.Equals(out));
1249 // TODO: Instead of computing negation as a subtraction from
1250 // zero, implement it with an exclusive or with value 0x80000000
1251 // (mask for bit 31, representing the sign of a single-precision
1252 // floating-point number), fetched from a constant pool:
1253 //
1254 // xorps out, [RIP:...] // value at RIP is 0x80 00 00 00
1255
1256 // out = 0
1257 __ xorps(out.As<XmmRegister>(), out.As<XmmRegister>());
1258 // out = out - in
1259 __ subss(out.As<XmmRegister>(), in.As<XmmRegister>());
1260 break;
1261
Roland Levillain88cb1752014-10-20 16:36:47 +01001262 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001263 DCHECK(in.IsFpuRegister());
1264 DCHECK(out.IsFpuRegister());
1265 DCHECK(!in.Equals(out));
1266 // TODO: Instead of computing negation as a subtraction from
1267 // zero, implement it with an exclusive or with value
1268 // 0x8000000000000000 (mask for bit 63, representing the sign of
1269 // a double-precision floating-point number), fetched from a
1270 // constant pool:
1271 //
1272 // xorpd out, [RIP:...] // value at RIP is 0x80 00 00 00 00 00 00 00
1273
1274 // out = 0
1275 __ xorpd(out.As<XmmRegister>(), out.As<XmmRegister>());
1276 // out = out - in
1277 __ subsd(out.As<XmmRegister>(), in.As<XmmRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001278 break;
1279
1280 default:
1281 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1282 }
1283}
1284
Roland Levillaindff1f282014-11-05 14:15:05 +00001285void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1286 LocationSummary* locations =
1287 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1288 Primitive::Type result_type = conversion->GetResultType();
1289 Primitive::Type input_type = conversion->GetInputType();
1290 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001291 case Primitive::kPrimByte:
1292 switch (input_type) {
1293 case Primitive::kPrimShort:
1294 case Primitive::kPrimInt:
1295 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001296 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001297 locations->SetInAt(0, Location::Any());
1298 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1299 break;
1300
1301 default:
1302 LOG(FATAL) << "Unexpected type conversion from " << input_type
1303 << " to " << result_type;
1304 }
1305 break;
1306
Roland Levillain01a8d712014-11-14 16:27:39 +00001307 case Primitive::kPrimShort:
1308 switch (input_type) {
1309 case Primitive::kPrimByte:
1310 case Primitive::kPrimInt:
1311 case Primitive::kPrimChar:
1312 // Processing a Dex `int-to-short' instruction.
1313 locations->SetInAt(0, Location::Any());
1314 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1315 break;
1316
1317 default:
1318 LOG(FATAL) << "Unexpected type conversion from " << input_type
1319 << " to " << result_type;
1320 }
1321 break;
1322
Roland Levillain946e1432014-11-11 17:35:19 +00001323 case Primitive::kPrimInt:
1324 switch (input_type) {
1325 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001326 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001327 locations->SetInAt(0, Location::Any());
1328 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1329 break;
1330
1331 case Primitive::kPrimFloat:
1332 case Primitive::kPrimDouble:
1333 LOG(FATAL) << "Type conversion from " << input_type
1334 << " to " << result_type << " not yet implemented";
1335 break;
1336
1337 default:
1338 LOG(FATAL) << "Unexpected type conversion from " << input_type
1339 << " to " << result_type;
1340 }
1341 break;
1342
Roland Levillaindff1f282014-11-05 14:15:05 +00001343 case Primitive::kPrimLong:
1344 switch (input_type) {
1345 case Primitive::kPrimByte:
1346 case Primitive::kPrimShort:
1347 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001348 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001349 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001350 // TODO: We would benefit from a (to-be-implemented)
1351 // Location::RegisterOrStackSlot requirement for this input.
1352 locations->SetInAt(0, Location::RequiresRegister());
1353 locations->SetOut(Location::RequiresRegister());
1354 break;
1355
1356 case Primitive::kPrimFloat:
1357 case Primitive::kPrimDouble:
1358 LOG(FATAL) << "Type conversion from " << input_type << " to "
1359 << result_type << " not yet implemented";
1360 break;
1361
1362 default:
1363 LOG(FATAL) << "Unexpected type conversion from " << input_type
1364 << " to " << result_type;
1365 }
1366 break;
1367
Roland Levillain981e4542014-11-14 11:47:14 +00001368 case Primitive::kPrimChar:
1369 switch (input_type) {
1370 case Primitive::kPrimByte:
1371 case Primitive::kPrimShort:
1372 case Primitive::kPrimInt:
1373 case Primitive::kPrimChar:
1374 // Processing a Dex `int-to-char' instruction.
1375 locations->SetInAt(0, Location::Any());
1376 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1377 break;
1378
1379 default:
1380 LOG(FATAL) << "Unexpected type conversion from " << input_type
1381 << " to " << result_type;
1382 }
1383 break;
1384
Roland Levillaindff1f282014-11-05 14:15:05 +00001385 case Primitive::kPrimFloat:
1386 case Primitive::kPrimDouble:
1387 LOG(FATAL) << "Type conversion from " << input_type
1388 << " to " << result_type << " not yet implemented";
1389 break;
1390
1391 default:
1392 LOG(FATAL) << "Unexpected type conversion from " << input_type
1393 << " to " << result_type;
1394 }
1395}
1396
1397void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
1398 LocationSummary* locations = conversion->GetLocations();
1399 Location out = locations->Out();
1400 Location in = locations->InAt(0);
1401 Primitive::Type result_type = conversion->GetResultType();
1402 Primitive::Type input_type = conversion->GetInputType();
1403 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001404 case Primitive::kPrimByte:
1405 switch (input_type) {
1406 case Primitive::kPrimShort:
1407 case Primitive::kPrimInt:
1408 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001409 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001410 if (in.IsRegister()) {
1411 __ movsxb(out.As<CpuRegister>(), in.As<CpuRegister>());
1412 } else if (in.IsStackSlot()) {
1413 __ movsxb(out.As<CpuRegister>(),
1414 Address(CpuRegister(RSP), in.GetStackIndex()));
1415 } else {
1416 DCHECK(in.GetConstant()->IsIntConstant());
1417 __ movl(out.As<CpuRegister>(),
1418 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1419 }
1420 break;
1421
1422 default:
1423 LOG(FATAL) << "Unexpected type conversion from " << input_type
1424 << " to " << result_type;
1425 }
1426 break;
1427
Roland Levillain01a8d712014-11-14 16:27:39 +00001428 case Primitive::kPrimShort:
1429 switch (input_type) {
1430 case Primitive::kPrimByte:
1431 case Primitive::kPrimInt:
1432 case Primitive::kPrimChar:
1433 // Processing a Dex `int-to-short' instruction.
1434 if (in.IsRegister()) {
1435 __ movsxw(out.As<CpuRegister>(), in.As<CpuRegister>());
1436 } else if (in.IsStackSlot()) {
1437 __ movsxw(out.As<CpuRegister>(),
1438 Address(CpuRegister(RSP), in.GetStackIndex()));
1439 } else {
1440 DCHECK(in.GetConstant()->IsIntConstant());
1441 __ movl(out.As<CpuRegister>(),
1442 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1443 }
1444 break;
1445
1446 default:
1447 LOG(FATAL) << "Unexpected type conversion from " << input_type
1448 << " to " << result_type;
1449 }
1450 break;
1451
Roland Levillain946e1432014-11-11 17:35:19 +00001452 case Primitive::kPrimInt:
1453 switch (input_type) {
1454 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001455 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001456 if (in.IsRegister()) {
1457 __ movl(out.As<CpuRegister>(), in.As<CpuRegister>());
1458 } else if (in.IsDoubleStackSlot()) {
1459 __ movl(out.As<CpuRegister>(),
1460 Address(CpuRegister(RSP), in.GetStackIndex()));
1461 } else {
1462 DCHECK(in.IsConstant());
1463 DCHECK(in.GetConstant()->IsLongConstant());
1464 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
1465 __ movl(out.As<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1466 }
1467 break;
1468
1469 case Primitive::kPrimFloat:
1470 case Primitive::kPrimDouble:
1471 LOG(FATAL) << "Type conversion from " << input_type
1472 << " to " << result_type << " not yet implemented";
1473 break;
1474
1475 default:
1476 LOG(FATAL) << "Unexpected type conversion from " << input_type
1477 << " to " << result_type;
1478 }
1479 break;
1480
Roland Levillaindff1f282014-11-05 14:15:05 +00001481 case Primitive::kPrimLong:
1482 switch (input_type) {
1483 DCHECK(out.IsRegister());
1484 case Primitive::kPrimByte:
1485 case Primitive::kPrimShort:
1486 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001487 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001488 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001489 DCHECK(in.IsRegister());
1490 __ movsxd(out.As<CpuRegister>(), in.As<CpuRegister>());
1491 break;
1492
1493 case Primitive::kPrimFloat:
1494 case Primitive::kPrimDouble:
1495 LOG(FATAL) << "Type conversion from " << input_type << " to "
1496 << result_type << " not yet implemented";
1497 break;
1498
1499 default:
1500 LOG(FATAL) << "Unexpected type conversion from " << input_type
1501 << " to " << result_type;
1502 }
1503 break;
1504
Roland Levillain981e4542014-11-14 11:47:14 +00001505 case Primitive::kPrimChar:
1506 switch (input_type) {
1507 case Primitive::kPrimByte:
1508 case Primitive::kPrimShort:
1509 case Primitive::kPrimInt:
1510 case Primitive::kPrimChar:
1511 // Processing a Dex `int-to-char' instruction.
1512 if (in.IsRegister()) {
1513 __ movzxw(out.As<CpuRegister>(), in.As<CpuRegister>());
1514 } else if (in.IsStackSlot()) {
1515 __ movzxw(out.As<CpuRegister>(),
1516 Address(CpuRegister(RSP), in.GetStackIndex()));
1517 } else {
1518 DCHECK(in.GetConstant()->IsIntConstant());
1519 __ movl(out.As<CpuRegister>(),
1520 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
1521 }
1522 break;
1523
1524 default:
1525 LOG(FATAL) << "Unexpected type conversion from " << input_type
1526 << " to " << result_type;
1527 }
1528 break;
1529
Roland Levillaindff1f282014-11-05 14:15:05 +00001530 case Primitive::kPrimFloat:
1531 case Primitive::kPrimDouble:
1532 LOG(FATAL) << "Type conversion from " << input_type
1533 << " to " << result_type << " not yet implemented";
1534 break;
1535
1536 default:
1537 LOG(FATAL) << "Unexpected type conversion from " << input_type
1538 << " to " << result_type;
1539 }
1540}
1541
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001542void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001543 LocationSummary* locations =
1544 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001545 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001546 case Primitive::kPrimInt: {
1547 locations->SetInAt(0, Location::RequiresRegister());
1548 locations->SetInAt(1, Location::Any());
1549 locations->SetOut(Location::SameAsFirstInput());
1550 break;
1551 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001552
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001553 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001554 locations->SetInAt(0, Location::RequiresRegister());
1555 locations->SetInAt(1, Location::RequiresRegister());
1556 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001557 break;
1558 }
1559
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001560 case Primitive::kPrimDouble:
1561 case Primitive::kPrimFloat: {
1562 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001563 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001564 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001565 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001566 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001567
1568 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001569 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001570 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001571}
1572
1573void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
1574 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001575 Location first = locations->InAt(0);
1576 Location second = locations->InAt(1);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001577 DCHECK(first.Equals(locations->Out()));
Calin Juravle11351682014-10-23 15:38:15 +01001578
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001579 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001580 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001581 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001582 __ addl(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001583 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001584 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001585 __ addl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001586 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001587 __ addl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001588 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001589 break;
1590 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001591
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001592 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001593 __ addq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001594 break;
1595 }
1596
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001597 case Primitive::kPrimFloat: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001598 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001599 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001600 }
1601
1602 case Primitive::kPrimDouble: {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001603 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001604 break;
1605 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001606
1607 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001608 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001609 }
1610}
1611
1612void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001613 LocationSummary* locations =
1614 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001615 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001616 case Primitive::kPrimInt: {
1617 locations->SetInAt(0, Location::RequiresRegister());
1618 locations->SetInAt(1, Location::Any());
1619 locations->SetOut(Location::SameAsFirstInput());
1620 break;
1621 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001622 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001623 locations->SetInAt(0, Location::RequiresRegister());
1624 locations->SetInAt(1, Location::RequiresRegister());
1625 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001626 break;
1627 }
Calin Juravle11351682014-10-23 15:38:15 +01001628 case Primitive::kPrimFloat:
1629 case Primitive::kPrimDouble: {
1630 locations->SetInAt(0, Location::RequiresFpuRegister());
1631 locations->SetInAt(1, Location::RequiresFpuRegister());
1632 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001633 break;
Calin Juravle11351682014-10-23 15:38:15 +01001634 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001635 default:
Calin Juravle11351682014-10-23 15:38:15 +01001636 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001637 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001638}
1639
1640void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
1641 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01001642 Location first = locations->InAt(0);
1643 Location second = locations->InAt(1);
1644 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001645 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001646 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01001647 if (second.IsRegister()) {
1648 __ subl(first.As<CpuRegister>(), second.As<CpuRegister>());
1649 } else if (second.IsConstant()) {
1650 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1651 __ subl(first.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001652 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001653 __ subl(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001654 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001655 break;
1656 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001657 case Primitive::kPrimLong: {
Calin Juravle11351682014-10-23 15:38:15 +01001658 __ subq(first.As<CpuRegister>(), second.As<CpuRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001659 break;
1660 }
1661
Calin Juravle11351682014-10-23 15:38:15 +01001662 case Primitive::kPrimFloat: {
1663 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001664 break;
Calin Juravle11351682014-10-23 15:38:15 +01001665 }
1666
1667 case Primitive::kPrimDouble: {
1668 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1669 break;
1670 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001671
1672 default:
Calin Juravle11351682014-10-23 15:38:15 +01001673 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001674 }
1675}
1676
Calin Juravle34bacdf2014-10-07 20:23:36 +01001677void LocationsBuilderX86_64::VisitMul(HMul* mul) {
1678 LocationSummary* locations =
1679 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1680 switch (mul->GetResultType()) {
1681 case Primitive::kPrimInt: {
1682 locations->SetInAt(0, Location::RequiresRegister());
1683 locations->SetInAt(1, Location::Any());
1684 locations->SetOut(Location::SameAsFirstInput());
1685 break;
1686 }
1687 case Primitive::kPrimLong: {
1688 locations->SetInAt(0, Location::RequiresRegister());
1689 locations->SetInAt(1, Location::RequiresRegister());
1690 locations->SetOut(Location::SameAsFirstInput());
1691 break;
1692 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001693 case Primitive::kPrimFloat:
1694 case Primitive::kPrimDouble: {
1695 locations->SetInAt(0, Location::RequiresFpuRegister());
1696 locations->SetInAt(1, Location::RequiresFpuRegister());
1697 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001698 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001699 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001700
1701 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001702 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001703 }
1704}
1705
1706void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
1707 LocationSummary* locations = mul->GetLocations();
1708 Location first = locations->InAt(0);
1709 Location second = locations->InAt(1);
1710 DCHECK(first.Equals(locations->Out()));
1711 switch (mul->GetResultType()) {
1712 case Primitive::kPrimInt: {
1713 if (second.IsRegister()) {
1714 __ imull(first.As<CpuRegister>(), second.As<CpuRegister>());
1715 } else if (second.IsConstant()) {
1716 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1717 __ imull(first.As<CpuRegister>(), imm);
1718 } else {
1719 DCHECK(second.IsStackSlot());
1720 __ imull(first.As<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
1721 }
1722 break;
1723 }
1724 case Primitive::kPrimLong: {
1725 __ imulq(first.As<CpuRegister>(), second.As<CpuRegister>());
1726 break;
1727 }
1728
Calin Juravleb5bfa962014-10-21 18:02:24 +01001729 case Primitive::kPrimFloat: {
1730 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001731 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001732 }
1733
1734 case Primitive::kPrimDouble: {
1735 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1736 break;
1737 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001738
1739 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001740 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001741 }
1742}
1743
Calin Juravle7c4954d2014-10-28 16:57:40 +00001744void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
1745 LocationSummary* locations =
1746 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1747 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001748 case Primitive::kPrimInt:
1749 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00001750 locations->SetInAt(0, Location::RegisterLocation(RAX));
1751 locations->SetInAt(1, Location::RequiresRegister());
1752 locations->SetOut(Location::SameAsFirstInput());
1753 // Intel uses edx:eax as the dividend.
1754 locations->AddTemp(Location::RegisterLocation(RDX));
1755 break;
1756 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001757
Calin Juravle7c4954d2014-10-28 16:57:40 +00001758 case Primitive::kPrimFloat:
1759 case Primitive::kPrimDouble: {
1760 locations->SetInAt(0, Location::RequiresFpuRegister());
1761 locations->SetInAt(1, Location::RequiresFpuRegister());
1762 locations->SetOut(Location::SameAsFirstInput());
1763 break;
1764 }
1765
1766 default:
1767 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1768 }
1769}
1770
1771void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
1772 LocationSummary* locations = div->GetLocations();
1773 Location first = locations->InAt(0);
1774 Location second = locations->InAt(1);
1775 DCHECK(first.Equals(locations->Out()));
1776
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001777 Primitive::Type type = div->GetResultType();
1778 switch (type) {
1779 case Primitive::kPrimInt:
1780 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00001781 CpuRegister first_reg = first.As<CpuRegister>();
1782 CpuRegister second_reg = second.As<CpuRegister>();
1783 DCHECK_EQ(RAX, first_reg.AsRegister());
1784 DCHECK_EQ(RDX, locations->GetTemp(0).As<CpuRegister>().AsRegister());
1785
1786 SlowPathCodeX86_64* slow_path =
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001787 new (GetGraph()->GetArena()) DivMinusOneSlowPathX86_64(first_reg.AsRegister(), type);
Calin Juravled0d48522014-11-04 16:40:20 +00001788 codegen_->AddSlowPath(slow_path);
1789
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001790 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
1791 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
1792 // so it's safe to just use negl instead of more complex comparisons.
Calin Juravled0d48522014-11-04 16:40:20 +00001793
1794 __ cmpl(second_reg, Immediate(-1));
1795 __ j(kEqual, slow_path->GetEntryLabel());
1796
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001797 if (type == Primitive::kPrimInt) {
1798 // edx:eax <- sign-extended of eax
1799 __ cdq();
1800 // eax = quotient, edx = remainder
1801 __ idivl(second_reg);
1802 } else {
1803 // rdx:rax <- sign-extended of rax
1804 __ cqo();
1805 // rax = quotient, rdx = remainder
1806 __ idivq(second_reg);
1807 }
Calin Juravled0d48522014-11-04 16:40:20 +00001808
1809 __ Bind(slow_path->GetExitLabel());
1810 break;
1811 }
1812
Calin Juravle7c4954d2014-10-28 16:57:40 +00001813 case Primitive::kPrimFloat: {
1814 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1815 break;
1816 }
1817
1818 case Primitive::kPrimDouble: {
1819 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1820 break;
1821 }
1822
1823 default:
1824 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1825 }
1826}
1827
Calin Juravled0d48522014-11-04 16:40:20 +00001828void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1829 LocationSummary* locations =
1830 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1831 locations->SetInAt(0, Location::Any());
1832 if (instruction->HasUses()) {
1833 locations->SetOut(Location::SameAsFirstInput());
1834 }
1835}
1836
1837void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1838 SlowPathCodeX86_64* slow_path =
1839 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
1840 codegen_->AddSlowPath(slow_path);
1841
1842 LocationSummary* locations = instruction->GetLocations();
1843 Location value = locations->InAt(0);
1844
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001845 switch (instruction->GetType()) {
1846 case Primitive::kPrimInt: {
1847 if (value.IsRegister()) {
1848 __ testl(value.As<CpuRegister>(), value.As<CpuRegister>());
1849 __ j(kEqual, slow_path->GetEntryLabel());
1850 } else if (value.IsStackSlot()) {
1851 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
1852 __ j(kEqual, slow_path->GetEntryLabel());
1853 } else {
1854 DCHECK(value.IsConstant()) << value;
1855 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
1856 __ jmp(slow_path->GetEntryLabel());
1857 }
1858 }
1859 break;
Calin Juravled0d48522014-11-04 16:40:20 +00001860 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001861 case Primitive::kPrimLong: {
1862 if (value.IsRegister()) {
1863 __ testq(value.As<CpuRegister>(), value.As<CpuRegister>());
1864 __ j(kEqual, slow_path->GetEntryLabel());
1865 } else if (value.IsDoubleStackSlot()) {
1866 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
1867 __ j(kEqual, slow_path->GetEntryLabel());
1868 } else {
1869 DCHECK(value.IsConstant()) << value;
1870 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
1871 __ jmp(slow_path->GetEntryLabel());
1872 }
1873 }
1874 break;
1875 }
1876 default:
1877 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00001878 }
Calin Juravled0d48522014-11-04 16:40:20 +00001879}
1880
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001881void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001882 LocationSummary* locations =
1883 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001884 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001885 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1886 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1887 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001888}
1889
1890void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
1891 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001892 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001893 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1894
1895 __ gs()->call(Address::Absolute(
1896 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocObjectWithAccessCheck), true));
1897
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001898 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001899 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001900}
1901
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001902void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
1903 LocationSummary* locations =
1904 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1905 InvokeRuntimeCallingConvention calling_convention;
1906 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1907 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1908 locations->SetOut(Location::RegisterLocation(RAX));
1909 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1910}
1911
1912void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
1913 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001914 codegen_->LoadCurrentMethod(CpuRegister(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001915 __ movq(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(instruction->GetTypeIndex()));
1916
1917 __ gs()->call(Address::Absolute(
1918 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocArrayWithAccessCheck), true));
1919
1920 DCHECK(!codegen_->IsLeafMethod());
1921 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1922}
1923
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001924void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001925 LocationSummary* locations =
1926 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001927 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1928 if (location.IsStackSlot()) {
1929 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1930 } else if (location.IsDoubleStackSlot()) {
1931 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1932 }
1933 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001934}
1935
1936void InstructionCodeGeneratorX86_64::VisitParameterValue(HParameterValue* instruction) {
1937 // Nothing to do, the parameter is already at its location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001938 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001939}
1940
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001941void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001942 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001943 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001944 locations->SetInAt(0, Location::RequiresRegister());
1945 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001946}
1947
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001948void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
1949 LocationSummary* locations = not_->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001950 DCHECK_EQ(locations->InAt(0).As<CpuRegister>().AsRegister(),
1951 locations->Out().As<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001952 Location out = locations->Out();
1953 switch (not_->InputAt(0)->GetType()) {
1954 case Primitive::kPrimBoolean:
1955 __ xorq(out.As<CpuRegister>(), Immediate(1));
1956 break;
1957
1958 case Primitive::kPrimInt:
1959 __ notl(out.As<CpuRegister>());
1960 break;
1961
1962 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001963 __ notq(out.As<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001964 break;
1965
1966 default:
1967 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1968 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001969}
1970
1971void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001972 LocationSummary* locations =
1973 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001974 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1975 locations->SetInAt(i, Location::Any());
1976 }
1977 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001978}
1979
1980void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001981 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001982 LOG(FATAL) << "Unimplemented";
1983}
1984
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001985void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001986 LocationSummary* locations =
1987 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001988 Primitive::Type field_type = instruction->GetFieldType();
1989 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001990 locations->SetInAt(0, Location::RequiresRegister());
1991 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001992 if (is_object_type) {
1993 // Temporary registers for the write barrier.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001994 locations->AddTemp(Location::RequiresRegister());
1995 locations->AddTemp(Location::RequiresRegister());
1996 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001997}
1998
1999void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2000 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002001 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002002 size_t offset = instruction->GetFieldOffset().SizeValue();
Nicolas Geoffray39468442014-09-02 15:17:15 +01002003 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002004
2005 switch (field_type) {
2006 case Primitive::kPrimBoolean:
2007 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002008 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002009 __ movb(Address(obj, offset), value);
2010 break;
2011 }
2012
2013 case Primitive::kPrimShort:
2014 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002015 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002016 __ movw(Address(obj, offset), value);
2017 break;
2018 }
2019
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002020 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002021 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002022 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002023 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002024 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002025 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
2026 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002027 codegen_->MarkGCCard(temp, card, obj, value);
2028 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002029 break;
2030 }
2031
2032 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002033 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002034 __ movq(Address(obj, offset), value);
2035 break;
2036 }
2037
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002038 case Primitive::kPrimFloat: {
2039 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2040 __ movss(Address(obj, offset), value);
2041 break;
2042 }
2043
2044 case Primitive::kPrimDouble: {
2045 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2046 __ movsd(Address(obj, offset), value);
2047 break;
2048 }
2049
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002050 case Primitive::kPrimVoid:
2051 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002052 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002053 }
2054}
2055
2056void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002057 LocationSummary* locations =
2058 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002059 locations->SetInAt(0, Location::RequiresRegister());
2060 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002061}
2062
2063void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2064 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002065 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002066 size_t offset = instruction->GetFieldOffset().SizeValue();
2067
2068 switch (instruction->GetType()) {
2069 case Primitive::kPrimBoolean: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002070 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002071 __ movzxb(out, Address(obj, offset));
2072 break;
2073 }
2074
2075 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002076 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002077 __ movsxb(out, Address(obj, offset));
2078 break;
2079 }
2080
2081 case Primitive::kPrimShort: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002082 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002083 __ movsxw(out, Address(obj, offset));
2084 break;
2085 }
2086
2087 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002088 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002089 __ movzxw(out, Address(obj, offset));
2090 break;
2091 }
2092
2093 case Primitive::kPrimInt:
2094 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002095 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002096 __ movl(out, Address(obj, offset));
2097 break;
2098 }
2099
2100 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002101 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002102 __ movq(out, Address(obj, offset));
2103 break;
2104 }
2105
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002106 case Primitive::kPrimFloat: {
2107 XmmRegister out = locations->Out().As<XmmRegister>();
2108 __ movss(out, Address(obj, offset));
2109 break;
2110 }
2111
2112 case Primitive::kPrimDouble: {
2113 XmmRegister out = locations->Out().As<XmmRegister>();
2114 __ movsd(out, Address(obj, offset));
2115 break;
2116 }
2117
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002118 case Primitive::kPrimVoid:
2119 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002120 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002121 }
2122}
2123
2124void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002125 LocationSummary* locations =
2126 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002127 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002128 if (instruction->HasUses()) {
2129 locations->SetOut(Location::SameAsFirstInput());
2130 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002131}
2132
2133void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002134 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002135 codegen_->AddSlowPath(slow_path);
2136
2137 LocationSummary* locations = instruction->GetLocations();
2138 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002139
2140 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002141 __ cmpl(obj.As<CpuRegister>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002142 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002143 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002144 } else {
2145 DCHECK(obj.IsConstant()) << obj;
2146 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2147 __ jmp(slow_path->GetEntryLabel());
2148 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002149 }
2150 __ j(kEqual, slow_path->GetEntryLabel());
2151}
2152
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002153void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002154 LocationSummary* locations =
2155 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002156 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002157 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002158 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2159 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002160}
2161
2162void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
2163 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002164 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002165 Location index = locations->InAt(1);
2166
2167 switch (instruction->GetType()) {
2168 case Primitive::kPrimBoolean: {
2169 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002170 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002171 if (index.IsConstant()) {
2172 __ movzxb(out, Address(obj,
2173 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2174 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002175 __ movzxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002176 }
2177 break;
2178 }
2179
2180 case Primitive::kPrimByte: {
2181 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002182 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002183 if (index.IsConstant()) {
2184 __ movsxb(out, Address(obj,
2185 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2186 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002187 __ movsxb(out, Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002188 }
2189 break;
2190 }
2191
2192 case Primitive::kPrimShort: {
2193 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002194 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002195 if (index.IsConstant()) {
2196 __ movsxw(out, Address(obj,
2197 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2198 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002199 __ movsxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002200 }
2201 break;
2202 }
2203
2204 case Primitive::kPrimChar: {
2205 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002206 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002207 if (index.IsConstant()) {
2208 __ movzxw(out, Address(obj,
2209 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2210 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002211 __ movzxw(out, Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002212 }
2213 break;
2214 }
2215
2216 case Primitive::kPrimInt:
2217 case Primitive::kPrimNot: {
2218 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
2219 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002220 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002221 if (index.IsConstant()) {
2222 __ movl(out, Address(obj,
2223 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2224 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002225 __ movl(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002226 }
2227 break;
2228 }
2229
2230 case Primitive::kPrimLong: {
2231 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002232 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002233 if (index.IsConstant()) {
2234 __ movq(out, Address(obj,
2235 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2236 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002237 __ movq(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002238 }
2239 break;
2240 }
2241
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002242 case Primitive::kPrimFloat: {
2243 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2244 XmmRegister out = locations->Out().As<XmmRegister>();
2245 if (index.IsConstant()) {
2246 __ movss(out, Address(obj,
2247 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2248 } else {
2249 __ movss(out, Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset));
2250 }
2251 break;
2252 }
2253
2254 case Primitive::kPrimDouble: {
2255 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2256 XmmRegister out = locations->Out().As<XmmRegister>();
2257 if (index.IsConstant()) {
2258 __ movsd(out, Address(obj,
2259 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
2260 } else {
2261 __ movsd(out, Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset));
2262 }
2263 break;
2264 }
2265
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002266 case Primitive::kPrimVoid:
2267 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002268 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002269 }
2270}
2271
2272void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002273 Primitive::Type value_type = instruction->GetComponentType();
2274 bool is_object = value_type == Primitive::kPrimNot;
2275 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2276 instruction, is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
2277 if (is_object) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002278 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002279 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2280 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2281 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002282 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002283 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002284 locations->SetInAt(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002285 1, Location::RegisterOrConstant(instruction->InputAt(1)));
2286 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002287 if (value_type == Primitive::kPrimLong) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002288 locations->SetInAt(2, Location::RequiresRegister());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002289 } else if (value_type == Primitive::kPrimFloat || value_type == Primitive::kPrimDouble) {
2290 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002291 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002292 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002293 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002294 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002295}
2296
2297void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
2298 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002299 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002300 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002301 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002302 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002303
2304 switch (value_type) {
2305 case Primitive::kPrimBoolean:
2306 case Primitive::kPrimByte: {
2307 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002308 if (index.IsConstant()) {
2309 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002310 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002311 __ movb(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002312 } else {
2313 __ movb(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2314 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002315 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002316 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002317 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
2318 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002319 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002320 __ movb(Address(obj, index.As<CpuRegister>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002321 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2322 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002323 }
2324 break;
2325 }
2326
2327 case Primitive::kPrimShort:
2328 case Primitive::kPrimChar: {
2329 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002330 if (index.IsConstant()) {
2331 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002332 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002333 __ movw(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002334 } else {
2335 __ movw(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2336 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002337 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002338 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002339 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
2340 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002341 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002342 __ movw(Address(obj, index.As<CpuRegister>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002343 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2344 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002345 }
2346 break;
2347 }
2348
2349 case Primitive::kPrimInt: {
2350 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002351 if (index.IsConstant()) {
2352 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002353 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002354 __ movl(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002355 } else {
2356 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2357 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002358 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002359 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002360 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2361 value.As<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002362 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002363 DCHECK(value.IsConstant()) << value;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002364 __ movl(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002365 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2366 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002367 }
2368 break;
2369 }
2370
2371 case Primitive::kPrimNot: {
2372 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAputObject), true));
2373 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002374 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002375 break;
2376 }
2377
2378 case Primitive::kPrimLong: {
2379 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002380 if (index.IsConstant()) {
2381 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002382 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002383 __ movq(Address(obj, offset), value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002384 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002385 DCHECK(value.IsRegister());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002386 __ movq(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
2387 value.As<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002388 }
2389 break;
2390 }
2391
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002392 case Primitive::kPrimFloat: {
2393 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
2394 if (index.IsConstant()) {
2395 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2396 DCHECK(value.IsFpuRegister());
2397 __ movss(Address(obj, offset), value.As<XmmRegister>());
2398 } else {
2399 DCHECK(value.IsFpuRegister());
2400 __ movss(Address(obj, index.As<CpuRegister>(), TIMES_4, data_offset),
2401 value.As<XmmRegister>());
2402 }
2403 break;
2404 }
2405
2406 case Primitive::kPrimDouble: {
2407 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
2408 if (index.IsConstant()) {
2409 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
2410 DCHECK(value.IsFpuRegister());
2411 __ movsd(Address(obj, offset), value.As<XmmRegister>());
2412 } else {
2413 DCHECK(value.IsFpuRegister());
2414 __ movsd(Address(obj, index.As<CpuRegister>(), TIMES_8, data_offset),
2415 value.As<XmmRegister>());
2416 }
2417 break;
2418 }
2419
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002420 case Primitive::kPrimVoid:
2421 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002422 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002423 }
2424}
2425
2426void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002427 LocationSummary* locations =
2428 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002429 locations->SetInAt(0, Location::RequiresRegister());
2430 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002431}
2432
2433void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
2434 LocationSummary* locations = instruction->GetLocations();
2435 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002436 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2437 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002438 __ movl(out, Address(obj, offset));
2439}
2440
2441void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002442 LocationSummary* locations =
2443 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002444 locations->SetInAt(0, Location::RequiresRegister());
2445 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002446 if (instruction->HasUses()) {
2447 locations->SetOut(Location::SameAsFirstInput());
2448 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002449}
2450
2451void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
2452 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002453 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002454 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002455 codegen_->AddSlowPath(slow_path);
2456
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002457 CpuRegister index = locations->InAt(0).As<CpuRegister>();
2458 CpuRegister length = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002459
2460 __ cmpl(index, length);
2461 __ j(kAboveEqual, slow_path->GetEntryLabel());
2462}
2463
2464void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
2465 CpuRegister card,
2466 CpuRegister object,
2467 CpuRegister value) {
2468 Label is_null;
2469 __ testl(value, value);
2470 __ j(kEqual, &is_null);
2471 __ gs()->movq(card, Address::Absolute(
2472 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
2473 __ movq(temp, object);
2474 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
2475 __ movb(Address(temp, card, TIMES_1, 0), card);
2476 __ Bind(&is_null);
2477}
2478
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002479void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
2480 temp->SetLocations(nullptr);
2481}
2482
2483void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp) {
2484 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002485 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002486}
2487
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002488void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002489 UNUSED(instruction);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002490 LOG(FATAL) << "Unimplemented";
2491}
2492
2493void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002494 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2495}
2496
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002497void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
2498 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2499}
2500
2501void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002502 HBasicBlock* block = instruction->GetBlock();
2503 if (block->GetLoopInformation() != nullptr) {
2504 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2505 // The back edge will generate the suspend check.
2506 return;
2507 }
2508 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2509 // The goto will generate the suspend check.
2510 return;
2511 }
2512 GenerateSuspendCheck(instruction, nullptr);
2513}
2514
2515void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
2516 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002517 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002518 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002519 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002520 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002521 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002522 if (successor == nullptr) {
2523 __ j(kNotEqual, slow_path->GetEntryLabel());
2524 __ Bind(slow_path->GetReturnLabel());
2525 } else {
2526 __ j(kEqual, codegen_->GetLabelOf(successor));
2527 __ jmp(slow_path->GetEntryLabel());
2528 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002529}
2530
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002531X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
2532 return codegen_->GetAssembler();
2533}
2534
2535void ParallelMoveResolverX86_64::EmitMove(size_t index) {
2536 MoveOperands* move = moves_.Get(index);
2537 Location source = move->GetSource();
2538 Location destination = move->GetDestination();
2539
2540 if (source.IsRegister()) {
2541 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002542 __ movq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002543 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002544 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002545 source.As<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002546 } else {
2547 DCHECK(destination.IsDoubleStackSlot());
2548 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002549 source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002550 }
2551 } else if (source.IsStackSlot()) {
2552 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002553 __ movl(destination.As<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002554 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002555 } else if (destination.IsFpuRegister()) {
2556 __ movss(destination.As<XmmRegister>(),
2557 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002558 } else {
2559 DCHECK(destination.IsStackSlot());
2560 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2561 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2562 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002563 } else if (source.IsDoubleStackSlot()) {
2564 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002565 __ movq(destination.As<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002566 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002567 } else if (destination.IsFpuRegister()) {
2568 __ movsd(destination.As<XmmRegister>(), Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002569 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01002570 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002571 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
2572 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2573 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002574 } else if (source.IsConstant()) {
2575 HConstant* constant = source.GetConstant();
2576 if (constant->IsIntConstant()) {
2577 Immediate imm(constant->AsIntConstant()->GetValue());
2578 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002579 __ movl(destination.As<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002580 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002581 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002582 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2583 }
2584 } else if (constant->IsLongConstant()) {
2585 int64_t value = constant->AsLongConstant()->GetValue();
2586 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002587 __ movq(destination.As<CpuRegister>(), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002588 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002589 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002590 __ movq(CpuRegister(TMP), Immediate(value));
2591 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2592 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002593 } else if (constant->IsFloatConstant()) {
2594 Immediate imm(bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue()));
2595 if (destination.IsFpuRegister()) {
2596 __ movl(CpuRegister(TMP), imm);
2597 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2598 } else {
2599 DCHECK(destination.IsStackSlot()) << destination;
2600 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
2601 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002602 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002603 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
2604 Immediate imm(bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue()));
2605 if (destination.IsFpuRegister()) {
2606 __ movq(CpuRegister(TMP), imm);
2607 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2608 } else {
2609 DCHECK(destination.IsDoubleStackSlot()) << destination;
2610 __ movq(CpuRegister(TMP), imm);
2611 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
2612 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002613 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002614 } else if (source.IsFpuRegister()) {
2615 if (destination.IsFpuRegister()) {
2616 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
2617 } else if (destination.IsStackSlot()) {
2618 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
2619 source.As<XmmRegister>());
2620 } else {
2621 DCHECK(destination.IsDoubleStackSlot());
2622 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
2623 source.As<XmmRegister>());
2624 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002625 }
2626}
2627
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002628void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002629 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002630 __ movl(Address(CpuRegister(RSP), mem), reg);
2631 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002632}
2633
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002634void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002635 ScratchRegisterScope ensure_scratch(
2636 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2637
2638 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2639 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2640 __ movl(CpuRegister(ensure_scratch.GetRegister()),
2641 Address(CpuRegister(RSP), mem2 + stack_offset));
2642 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2643 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
2644 CpuRegister(ensure_scratch.GetRegister()));
2645}
2646
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002647void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
2648 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2649 __ movq(Address(CpuRegister(RSP), mem), reg);
2650 __ movq(reg, CpuRegister(TMP));
2651}
2652
2653void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
2654 ScratchRegisterScope ensure_scratch(
2655 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
2656
2657 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
2658 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
2659 __ movq(CpuRegister(ensure_scratch.GetRegister()),
2660 Address(CpuRegister(RSP), mem2 + stack_offset));
2661 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
2662 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
2663 CpuRegister(ensure_scratch.GetRegister()));
2664}
2665
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002666void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
2667 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2668 __ movss(Address(CpuRegister(RSP), mem), reg);
2669 __ movd(reg, CpuRegister(TMP));
2670}
2671
2672void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
2673 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
2674 __ movsd(Address(CpuRegister(RSP), mem), reg);
2675 __ movd(reg, CpuRegister(TMP));
2676}
2677
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002678void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
2679 MoveOperands* move = moves_.Get(index);
2680 Location source = move->GetSource();
2681 Location destination = move->GetDestination();
2682
2683 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002684 __ xchgq(destination.As<CpuRegister>(), source.As<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002685 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002686 Exchange32(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002687 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002688 Exchange32(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002689 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002690 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
2691 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002692 Exchange64(source.As<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002693 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002694 Exchange64(destination.As<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002695 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
2696 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002697 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
2698 __ movd(CpuRegister(TMP), source.As<XmmRegister>());
2699 __ movaps(source.As<XmmRegister>(), destination.As<XmmRegister>());
2700 __ movd(destination.As<XmmRegister>(), CpuRegister(TMP));
2701 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
2702 Exchange32(source.As<XmmRegister>(), destination.GetStackIndex());
2703 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
2704 Exchange32(destination.As<XmmRegister>(), source.GetStackIndex());
2705 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
2706 Exchange64(source.As<XmmRegister>(), destination.GetStackIndex());
2707 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
2708 Exchange64(destination.As<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002709 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002710 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002711 }
2712}
2713
2714
2715void ParallelMoveResolverX86_64::SpillScratch(int reg) {
2716 __ pushq(CpuRegister(reg));
2717}
2718
2719
2720void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
2721 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002722}
2723
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002724void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
2725 SlowPathCodeX86_64* slow_path, CpuRegister class_reg) {
2726 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2727 Immediate(mirror::Class::kStatusInitialized));
2728 __ j(kLess, slow_path->GetEntryLabel());
2729 __ Bind(slow_path->GetExitLabel());
2730 // No need for memory fence, thanks to the X86_64 memory model.
2731}
2732
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002733void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002734 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2735 ? LocationSummary::kCallOnSlowPath
2736 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002737 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002738 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002739 locations->SetOut(Location::RequiresRegister());
2740}
2741
2742void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
2743 CpuRegister out = cls->GetLocations()->Out().As<CpuRegister>();
2744 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002745 DCHECK(!cls->CanCallRuntime());
2746 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002747 codegen_->LoadCurrentMethod(out);
2748 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2749 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002750 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002751 codegen_->LoadCurrentMethod(out);
2752 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2753 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002754 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
2755 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2756 codegen_->AddSlowPath(slow_path);
2757 __ testl(out, out);
2758 __ j(kEqual, slow_path->GetEntryLabel());
2759 if (cls->MustGenerateClinitCheck()) {
2760 GenerateClassInitializationCheck(slow_path, out);
2761 } else {
2762 __ Bind(slow_path->GetExitLabel());
2763 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002764 }
2765}
2766
2767void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
2768 LocationSummary* locations =
2769 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2770 locations->SetInAt(0, Location::RequiresRegister());
2771 if (check->HasUses()) {
2772 locations->SetOut(Location::SameAsFirstInput());
2773 }
2774}
2775
2776void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002777 // We assume the class to not be null.
2778 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
2779 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002780 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002781 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002782}
2783
2784void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2785 LocationSummary* locations =
2786 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2787 locations->SetInAt(0, Location::RequiresRegister());
2788 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2789}
2790
2791void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2792 LocationSummary* locations = instruction->GetLocations();
2793 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002794 size_t offset = instruction->GetFieldOffset().SizeValue();
2795
2796 switch (instruction->GetType()) {
2797 case Primitive::kPrimBoolean: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002798 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002799 __ movzxb(out, Address(cls, offset));
2800 break;
2801 }
2802
2803 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002804 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002805 __ movsxb(out, Address(cls, offset));
2806 break;
2807 }
2808
2809 case Primitive::kPrimShort: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002810 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002811 __ movsxw(out, Address(cls, offset));
2812 break;
2813 }
2814
2815 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002816 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002817 __ movzxw(out, Address(cls, offset));
2818 break;
2819 }
2820
2821 case Primitive::kPrimInt:
2822 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002823 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002824 __ movl(out, Address(cls, offset));
2825 break;
2826 }
2827
2828 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002829 CpuRegister out = locations->Out().As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002830 __ movq(out, Address(cls, offset));
2831 break;
2832 }
2833
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002834 case Primitive::kPrimFloat: {
2835 XmmRegister out = locations->Out().As<XmmRegister>();
2836 __ movss(out, Address(cls, offset));
2837 break;
2838 }
2839
2840 case Primitive::kPrimDouble: {
2841 XmmRegister out = locations->Out().As<XmmRegister>();
2842 __ movsd(out, Address(cls, offset));
2843 break;
2844 }
2845
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002846 case Primitive::kPrimVoid:
2847 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2848 UNREACHABLE();
2849 }
2850}
2851
2852void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2853 LocationSummary* locations =
2854 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2855 Primitive::Type field_type = instruction->GetFieldType();
2856 bool is_object_type = field_type == Primitive::kPrimNot;
2857 locations->SetInAt(0, Location::RequiresRegister());
2858 locations->SetInAt(1, Location::RequiresRegister());
2859 if (is_object_type) {
2860 // Temporary registers for the write barrier.
2861 locations->AddTemp(Location::RequiresRegister());
2862 locations->AddTemp(Location::RequiresRegister());
2863 }
2864}
2865
2866void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2867 LocationSummary* locations = instruction->GetLocations();
2868 CpuRegister cls = locations->InAt(0).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002869 size_t offset = instruction->GetFieldOffset().SizeValue();
2870 Primitive::Type field_type = instruction->GetFieldType();
2871
2872 switch (field_type) {
2873 case Primitive::kPrimBoolean:
2874 case Primitive::kPrimByte: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002875 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002876 __ movb(Address(cls, offset), value);
2877 break;
2878 }
2879
2880 case Primitive::kPrimShort:
2881 case Primitive::kPrimChar: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002882 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002883 __ movw(Address(cls, offset), value);
2884 break;
2885 }
2886
2887 case Primitive::kPrimInt:
2888 case Primitive::kPrimNot: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002889 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002890 __ movl(Address(cls, offset), value);
2891 if (field_type == Primitive::kPrimNot) {
2892 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
2893 CpuRegister card = locations->GetTemp(1).As<CpuRegister>();
2894 codegen_->MarkGCCard(temp, card, cls, value);
2895 }
2896 break;
2897 }
2898
2899 case Primitive::kPrimLong: {
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002900 CpuRegister value = locations->InAt(1).As<CpuRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002901 __ movq(Address(cls, offset), value);
2902 break;
2903 }
2904
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002905 case Primitive::kPrimFloat: {
2906 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2907 __ movss(Address(cls, offset), value);
2908 break;
2909 }
2910
2911 case Primitive::kPrimDouble: {
2912 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2913 __ movsd(Address(cls, offset), value);
2914 break;
2915 }
2916
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002917 case Primitive::kPrimVoid:
2918 LOG(FATAL) << "Unreachable type " << field_type;
2919 UNREACHABLE();
2920 }
2921}
2922
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002923void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
2924 LocationSummary* locations =
2925 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2926 locations->SetOut(Location::RequiresRegister());
2927}
2928
2929void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
2930 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
2931 codegen_->AddSlowPath(slow_path);
2932
2933 CpuRegister out = load->GetLocations()->Out().As<CpuRegister>();
2934 codegen_->LoadCurrentMethod(CpuRegister(out));
2935 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
2936 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2937 __ testl(out, out);
2938 __ j(kEqual, slow_path->GetEntryLabel());
2939 __ Bind(slow_path->GetExitLabel());
2940}
2941
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002942void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
2943 LocationSummary* locations =
2944 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2945 locations->SetOut(Location::RequiresRegister());
2946}
2947
2948void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
2949 Address address = Address::Absolute(
2950 Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
2951 __ gs()->movl(load->GetLocations()->Out().As<CpuRegister>(), address);
2952 __ gs()->movl(address, Immediate(0));
2953}
2954
2955void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
2956 LocationSummary* locations =
2957 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2958 InvokeRuntimeCallingConvention calling_convention;
2959 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2960}
2961
2962void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
2963 __ gs()->call(
2964 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pDeliverException), true));
2965 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2966}
2967
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002968void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002969 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
2970 ? LocationSummary::kNoCall
2971 : LocationSummary::kCallOnSlowPath;
2972 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2973 locations->SetInAt(0, Location::RequiresRegister());
2974 locations->SetInAt(1, Location::Any());
2975 locations->SetOut(Location::RequiresRegister());
2976}
2977
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002978void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002979 LocationSummary* locations = instruction->GetLocations();
2980 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
2981 Location cls = locations->InAt(1);
2982 CpuRegister out = locations->Out().As<CpuRegister>();
2983 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2984 Label done, zero;
2985 SlowPathCodeX86_64* slow_path = nullptr;
2986
2987 // Return 0 if `obj` is null.
2988 // TODO: avoid this check if we know obj is not null.
2989 __ testl(obj, obj);
2990 __ j(kEqual, &zero);
2991 // Compare the class of `obj` with `cls`.
2992 __ movl(out, Address(obj, class_offset));
2993 if (cls.IsRegister()) {
2994 __ cmpl(out, cls.As<CpuRegister>());
2995 } else {
2996 DCHECK(cls.IsStackSlot()) << cls;
2997 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
2998 }
2999 if (instruction->IsClassFinal()) {
3000 // Classes must be equal for the instanceof to succeed.
3001 __ j(kNotEqual, &zero);
3002 __ movl(out, Immediate(1));
3003 __ jmp(&done);
3004 } else {
3005 // If the classes are not equal, we go into a slow path.
3006 DCHECK(locations->OnlyCallsOnSlowPath());
3007 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003008 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003009 codegen_->AddSlowPath(slow_path);
3010 __ j(kNotEqual, slow_path->GetEntryLabel());
3011 __ movl(out, Immediate(1));
3012 __ jmp(&done);
3013 }
3014 __ Bind(&zero);
3015 __ movl(out, Immediate(0));
3016 if (slow_path != nullptr) {
3017 __ Bind(slow_path->GetExitLabel());
3018 }
3019 __ Bind(&done);
3020}
3021
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003022void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
3023 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3024 instruction, LocationSummary::kCallOnSlowPath);
3025 locations->SetInAt(0, Location::RequiresRegister());
3026 locations->SetInAt(1, Location::Any());
3027 locations->AddTemp(Location::RequiresRegister());
3028}
3029
3030void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
3031 LocationSummary* locations = instruction->GetLocations();
3032 CpuRegister obj = locations->InAt(0).As<CpuRegister>();
3033 Location cls = locations->InAt(1);
3034 CpuRegister temp = locations->GetTemp(0).As<CpuRegister>();
3035 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3036 SlowPathCodeX86_64* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(
3037 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3038 codegen_->AddSlowPath(slow_path);
3039
3040 // TODO: avoid this check if we know obj is not null.
3041 __ testl(obj, obj);
3042 __ j(kEqual, slow_path->GetExitLabel());
3043 // Compare the class of `obj` with `cls`.
3044 __ movl(temp, Address(obj, class_offset));
3045 if (cls.IsRegister()) {
3046 __ cmpl(temp, cls.As<CpuRegister>());
3047 } else {
3048 DCHECK(cls.IsStackSlot()) << cls;
3049 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
3050 }
3051 // Classes must be equal for the checkcast to succeed.
3052 __ j(kNotEqual, slow_path->GetEntryLabel());
3053 __ Bind(slow_path->GetExitLabel());
3054}
3055
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003056void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3057 LocationSummary* locations =
3058 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3059 InvokeRuntimeCallingConvention calling_convention;
3060 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3061}
3062
3063void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
3064 __ gs()->call(Address::Absolute(instruction->IsEnter()
3065 ? QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pLockObject)
3066 : QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pUnlockObject),
3067 true));
3068 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3069}
3070
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003071void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3072void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3073void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3074
3075void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3076 LocationSummary* locations =
3077 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3078 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3079 || instruction->GetResultType() == Primitive::kPrimLong);
3080 locations->SetInAt(0, Location::RequiresRegister());
3081 if (instruction->GetType() == Primitive::kPrimInt) {
3082 locations->SetInAt(1, Location::Any());
3083 } else {
3084 // Request a register to avoid loading a 64bits constant.
3085 locations->SetInAt(1, Location::RequiresRegister());
3086 }
3087 locations->SetOut(Location::SameAsFirstInput());
3088}
3089
3090void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
3091 HandleBitwiseOperation(instruction);
3092}
3093
3094void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
3095 HandleBitwiseOperation(instruction);
3096}
3097
3098void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
3099 HandleBitwiseOperation(instruction);
3100}
3101
3102void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
3103 LocationSummary* locations = instruction->GetLocations();
3104 Location first = locations->InAt(0);
3105 Location second = locations->InAt(1);
3106 DCHECK(first.Equals(locations->Out()));
3107
3108 if (instruction->GetResultType() == Primitive::kPrimInt) {
3109 if (second.IsRegister()) {
3110 if (instruction->IsAnd()) {
3111 __ andl(first.As<CpuRegister>(), second.As<CpuRegister>());
3112 } else if (instruction->IsOr()) {
3113 __ orl(first.As<CpuRegister>(), second.As<CpuRegister>());
3114 } else {
3115 DCHECK(instruction->IsXor());
3116 __ xorl(first.As<CpuRegister>(), second.As<CpuRegister>());
3117 }
3118 } else if (second.IsConstant()) {
3119 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
3120 if (instruction->IsAnd()) {
3121 __ andl(first.As<CpuRegister>(), imm);
3122 } else if (instruction->IsOr()) {
3123 __ orl(first.As<CpuRegister>(), imm);
3124 } else {
3125 DCHECK(instruction->IsXor());
3126 __ xorl(first.As<CpuRegister>(), imm);
3127 }
3128 } else {
3129 Address address(CpuRegister(RSP), second.GetStackIndex());
3130 if (instruction->IsAnd()) {
3131 __ andl(first.As<CpuRegister>(), address);
3132 } else if (instruction->IsOr()) {
3133 __ orl(first.As<CpuRegister>(), address);
3134 } else {
3135 DCHECK(instruction->IsXor());
3136 __ xorl(first.As<CpuRegister>(), address);
3137 }
3138 }
3139 } else {
3140 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3141 if (instruction->IsAnd()) {
3142 __ andq(first.As<CpuRegister>(), second.As<CpuRegister>());
3143 } else if (instruction->IsOr()) {
3144 __ orq(first.As<CpuRegister>(), second.As<CpuRegister>());
3145 } else {
3146 DCHECK(instruction->IsXor());
3147 __ xorq(first.As<CpuRegister>(), second.As<CpuRegister>());
3148 }
3149 }
3150}
3151
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003152} // namespace x86_64
3153} // namespace art