blob: 754dd1088d84f6c79476f5acdebc87285e9129e7 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
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.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
19#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000020#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010021#include "gc/accounting/card_table.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070022#include "mirror/array-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010023#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010024#include "mirror/class.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010025#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000026#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010027#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000028#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010029#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010032
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033namespace x86 {
34
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010035static constexpr int kCurrentMethodStackOffset = 0;
36
Calin Juravled6fb6cf2014-11-11 19:07:44 +000037static constexpr Register kRuntimeParameterCoreRegisters[] = { EAX, ECX, EDX, EBX };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010038static constexpr size_t kRuntimeParameterCoreRegistersLength =
39 arraysize(kRuntimeParameterCoreRegisters);
Mark Mendell5f874182015-03-04 15:42:45 -050040static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Mark P Mendell966c3ae2015-01-27 15:45:27 +000041static constexpr XmmRegister kRuntimeParameterFpuRegisters[] = { XMM0, XMM1, XMM2, XMM3 };
42static constexpr size_t kRuntimeParameterFpuRegistersLength =
43 arraysize(kRuntimeParameterFpuRegisters);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010044
Mark Mendell24f2dfa2015-01-14 19:51:45 -050045static constexpr int kC2ConditionMask = 0x400;
46
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000047static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000048
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010049class InvokeRuntimeCallingConvention : public CallingConvention<Register, XmmRegister> {
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};
60
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
62
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010063class SlowPathCodeX86 : public SlowPathCode {
64 public:
65 SlowPathCodeX86() : 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);
75};
76
77class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010078 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010079 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080
Alexandre Rames2ed20af2015-03-06 13:55:35 +000081 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010082 __ Bind(GetEntryLabel());
83 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010084 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010085 }
86
87 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010088 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010089 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
90};
91
Calin Juravled0d48522014-11-04 16:40:20 +000092class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
93 public:
94 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
95
Alexandre Rames2ed20af2015-03-06 13:55:35 +000096 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +000097 __ Bind(GetEntryLabel());
98 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
99 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
100 }
101
102 private:
103 HDivZeroCheck* const instruction_;
104 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
105};
106
Calin Juravlebacfec32014-11-14 15:54:36 +0000107class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +0000108 public:
Calin Juravlebacfec32014-11-14 15:54:36 +0000109 explicit DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000110
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000111 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000112 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000113 if (is_div_) {
114 __ negl(reg_);
115 } else {
116 __ movl(reg_, Immediate(0));
117 }
Calin Juravled0d48522014-11-04 16:40:20 +0000118 __ jmp(GetExitLabel());
119 }
120
121 private:
122 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000123 bool is_div_;
124 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000125};
126
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100127class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100128 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100129 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
130 Location index_location,
131 Location length_location)
Roland Levillain199f3362014-11-27 17:15:16 +0000132 : instruction_(instruction),
133 index_location_(index_location),
134 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100135
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000136 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100137 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100138 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000139 // We're moving two locations to locations that could overlap, so we need a parallel
140 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100141 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000142 x86_codegen->EmitParallelMoves(
143 index_location_,
144 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
145 length_location_,
146 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100147 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100148 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100149 }
150
151 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100152 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100153 const Location index_location_;
154 const Location length_location_;
155
156 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
157};
158
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100159class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000160 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000161 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100162 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000163
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000164 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100165 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000166 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000167 SaveLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000168 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
169 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000170 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100171 if (successor_ == nullptr) {
172 __ jmp(GetReturnLabel());
173 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100174 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100175 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000176 }
177
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100178 Label* GetReturnLabel() {
179 DCHECK(successor_ == nullptr);
180 return &return_label_;
181 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000182
183 private:
184 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100185 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000186 Label return_label_;
187
188 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
189};
190
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000191class LoadStringSlowPathX86 : public SlowPathCodeX86 {
192 public:
193 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
194
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000195 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000196 LocationSummary* locations = instruction_->GetLocations();
197 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
198
199 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
200 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000201 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000202
203 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800204 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
205 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000206 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000207 RecordPcInfo(codegen, instruction_, instruction_->GetDexPc());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000208 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000209 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000210
211 __ jmp(GetExitLabel());
212 }
213
214 private:
215 HLoadString* const instruction_;
216
217 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
218};
219
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000220class LoadClassSlowPathX86 : public SlowPathCodeX86 {
221 public:
222 LoadClassSlowPathX86(HLoadClass* cls,
223 HInstruction* at,
224 uint32_t dex_pc,
225 bool do_clinit)
226 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
227 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
228 }
229
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000230 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000231 LocationSummary* locations = at_->GetLocations();
232 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
233 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000234 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000235
236 InvokeRuntimeCallingConvention calling_convention;
237 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
238 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
239 __ fs()->call(Address::Absolute(do_clinit_
240 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
241 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000242 RecordPcInfo(codegen, at_, dex_pc_);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000243
244 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000245 Location out = locations->Out();
246 if (out.IsValid()) {
247 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
248 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000249 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000250
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000251 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000252 __ jmp(GetExitLabel());
253 }
254
255 private:
256 // The class this slow path will load.
257 HLoadClass* const cls_;
258
259 // The instruction where this slow path is happening.
260 // (Might be the load class or an initialization check).
261 HInstruction* const at_;
262
263 // The dex PC of `at_`.
264 const uint32_t dex_pc_;
265
266 // Whether to initialize the class.
267 const bool do_clinit_;
268
269 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
270};
271
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000272class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
273 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000274 TypeCheckSlowPathX86(HInstruction* instruction,
275 Location class_to_check,
276 Location object_class,
277 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000278 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000279 class_to_check_(class_to_check),
280 object_class_(object_class),
281 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000282
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000283 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000284 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000285 DCHECK(instruction_->IsCheckCast()
286 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000287
288 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
289 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000290 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000291
292 // We're moving two locations to locations that could overlap, so we need a parallel
293 // move resolver.
294 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000295 x86_codegen->EmitParallelMoves(
296 class_to_check_,
297 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
298 object_class_,
299 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000300
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000301 if (instruction_->IsInstanceOf()) {
Roland Levillain199f3362014-11-27 17:15:16 +0000302 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize,
303 pInstanceofNonTrivial)));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000304 } else {
305 DCHECK(instruction_->IsCheckCast());
306 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
307 }
308
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000309 RecordPcInfo(codegen, instruction_, dex_pc_);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000310 if (instruction_->IsInstanceOf()) {
311 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
312 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000313 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000314
315 __ jmp(GetExitLabel());
316 }
317
318 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000319 HInstruction* const instruction_;
320 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000321 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000322 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000323
324 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
325};
326
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100327#undef __
328#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
329
Dave Allison20dfc792014-06-16 20:44:29 -0700330inline Condition X86Condition(IfCondition cond) {
331 switch (cond) {
332 case kCondEQ: return kEqual;
333 case kCondNE: return kNotEqual;
334 case kCondLT: return kLess;
335 case kCondLE: return kLessEqual;
336 case kCondGT: return kGreater;
337 case kCondGE: return kGreaterEqual;
338 default:
339 LOG(FATAL) << "Unknown if condition";
340 }
341 return kEqual;
342}
343
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100344void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
345 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
346}
347
348void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
349 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
350}
351
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100352size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
353 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
354 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100355}
356
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100357size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
358 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
359 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100360}
361
Mark Mendell7c8d0092015-01-26 11:21:33 -0500362size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
363 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
364 return GetFloatingPointSpillSlotSize();
365}
366
367size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
368 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
369 return GetFloatingPointSpillSlotSize();
370}
371
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000372CodeGeneratorX86::CodeGeneratorX86(HGraph* graph, const CompilerOptions& compiler_options)
Mark Mendell5f874182015-03-04 15:42:45 -0500373 : CodeGenerator(graph,
374 kNumberOfCpuRegisters,
375 kNumberOfXmmRegisters,
376 kNumberOfRegisterPairs,
377 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
378 arraysize(kCoreCalleeSaves))
379 | (1 << kFakeReturnRegister),
380 0,
381 compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100382 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100383 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100384 instruction_visitor_(graph, this),
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000385 move_resolver_(graph->GetArena(), this) {
386 // Use a fake return address register to mimic Quick.
387 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100388}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100389
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100390Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100391 switch (type) {
392 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100393 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100394 X86ManagedRegister pair =
395 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100396 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
397 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100398 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
399 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100400 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100401 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100402 }
403
404 case Primitive::kPrimByte:
405 case Primitive::kPrimBoolean:
406 case Primitive::kPrimChar:
407 case Primitive::kPrimShort:
408 case Primitive::kPrimInt:
409 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100410 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100411 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100412 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100413 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
414 X86ManagedRegister current =
415 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
416 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100417 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100418 }
419 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100420 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100421 }
422
423 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100424 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100425 return Location::FpuRegisterLocation(
426 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100427 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100428
429 case Primitive::kPrimVoid:
430 LOG(FATAL) << "Unreachable type " << type;
431 }
432
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100433 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100434}
435
Mark Mendell5f874182015-03-04 15:42:45 -0500436void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100437 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100438 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100439
440 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100441 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100442
Mark Mendell5f874182015-03-04 15:42:45 -0500443 if (is_baseline) {
444 blocked_core_registers_[EBP] = true;
445 blocked_core_registers_[ESI] = true;
446 blocked_core_registers_[EDI] = true;
447 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100448
449 UpdateBlockedPairRegisters();
450}
451
452void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
453 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
454 X86ManagedRegister current =
455 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
456 if (blocked_core_registers_[current.AsRegisterPairLow()]
457 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
458 blocked_register_pairs_[i] = true;
459 }
460 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100461}
462
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100463InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
464 : HGraphVisitor(graph),
465 assembler_(codegen->GetAssembler()),
466 codegen_(codegen) {}
467
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000468void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000469 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000470 bool skip_overflow_check =
471 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000472 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000473
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000474 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100475 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100476 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100477 }
478
Mark Mendell5f874182015-03-04 15:42:45 -0500479 if (HasEmptyFrame()) {
480 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000481 }
Mark Mendell5f874182015-03-04 15:42:45 -0500482
483 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
484 Register reg = kCoreCalleeSaves[i];
485 if (allocated_registers_.ContainsCoreRegister(reg)) {
486 __ pushl(reg);
487 }
488 }
489
490 __ subl(ESP, Immediate(GetFrameSize() - FrameEntrySpillSize()));
491 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000492}
493
494void CodeGeneratorX86::GenerateFrameExit() {
Mark Mendell5f874182015-03-04 15:42:45 -0500495 if (HasEmptyFrame()) {
496 return;
497 }
498
499 __ addl(ESP, Immediate(GetFrameSize() - FrameEntrySpillSize()));
500
501 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
502 Register reg = kCoreCalleeSaves[i];
503 if (allocated_registers_.ContainsCoreRegister(reg)) {
504 __ popl(reg);
505 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000506 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000507}
508
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100509void CodeGeneratorX86::Bind(HBasicBlock* block) {
510 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000511}
512
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100513void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000514 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100515 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000516}
517
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100518Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
519 switch (load->GetType()) {
520 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100521 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100522 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
523 break;
524
525 case Primitive::kPrimInt:
526 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100527 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100528 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100529
530 case Primitive::kPrimBoolean:
531 case Primitive::kPrimByte:
532 case Primitive::kPrimChar:
533 case Primitive::kPrimShort:
534 case Primitive::kPrimVoid:
535 LOG(FATAL) << "Unexpected type " << load->GetType();
536 }
537
538 LOG(FATAL) << "Unreachable";
539 return Location();
540}
541
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100542Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
543 switch (type) {
544 case Primitive::kPrimBoolean:
545 case Primitive::kPrimByte:
546 case Primitive::kPrimChar:
547 case Primitive::kPrimShort:
548 case Primitive::kPrimInt:
549 case Primitive::kPrimNot: {
550 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000551 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100552 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100553 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100554 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000555 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100556 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100557 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100558
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000559 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100560 uint32_t index = gp_index_;
561 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000562 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100563 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100564 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
565 calling_convention.GetRegisterPairAt(index));
566 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100567 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000568 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
569 }
570 }
571
572 case Primitive::kPrimFloat: {
573 uint32_t index = fp_index_++;
574 stack_index_++;
575 if (index < calling_convention.GetNumberOfFpuRegisters()) {
576 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
577 } else {
578 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
579 }
580 }
581
582 case Primitive::kPrimDouble: {
583 uint32_t index = fp_index_++;
584 stack_index_ += 2;
585 if (index < calling_convention.GetNumberOfFpuRegisters()) {
586 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
587 } else {
588 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100589 }
590 }
591
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100592 case Primitive::kPrimVoid:
593 LOG(FATAL) << "Unexpected parameter type " << type;
594 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100595 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100596 return Location();
597}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100598
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100599void CodeGeneratorX86::Move32(Location destination, Location source) {
600 if (source.Equals(destination)) {
601 return;
602 }
603 if (destination.IsRegister()) {
604 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000605 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100606 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000607 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100608 } else {
609 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000610 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100611 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100612 } else if (destination.IsFpuRegister()) {
613 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000614 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100615 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000616 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100617 } else {
618 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000619 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100620 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100621 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000622 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100623 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000624 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100625 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000626 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500627 } else if (source.IsConstant()) {
628 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000629 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500630 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100631 } else {
632 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100633 __ pushl(Address(ESP, source.GetStackIndex()));
634 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100635 }
636 }
637}
638
639void CodeGeneratorX86::Move64(Location destination, Location source) {
640 if (source.Equals(destination)) {
641 return;
642 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100643 if (destination.IsRegisterPair()) {
644 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000645 EmitParallelMoves(
646 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
647 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
648 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
649 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100650 } else if (source.IsFpuRegister()) {
651 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100652 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000653 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100654 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100655 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
656 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100657 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
658 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100659 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500660 if (source.IsFpuRegister()) {
661 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
662 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000663 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100664 } else {
665 LOG(FATAL) << "Unimplemented";
666 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100667 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000668 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100669 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000670 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100671 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100672 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100673 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100674 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000675 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000676 } else if (source.IsConstant()) {
677 HConstant* constant = source.GetConstant();
678 int64_t value;
679 if (constant->IsLongConstant()) {
680 value = constant->AsLongConstant()->GetValue();
681 } else {
682 DCHECK(constant->IsDoubleConstant());
683 value = bit_cast<double, int64_t>(constant->AsDoubleConstant()->GetValue());
684 }
685 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
686 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100687 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000688 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000689 EmitParallelMoves(
690 Location::StackSlot(source.GetStackIndex()),
691 Location::StackSlot(destination.GetStackIndex()),
692 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
693 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100694 }
695 }
696}
697
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100698void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000699 LocationSummary* locations = instruction->GetLocations();
700 if (locations != nullptr && locations->Out().Equals(location)) {
701 return;
702 }
703
704 if (locations != nullptr && locations->Out().IsConstant()) {
705 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000706 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
707 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000708 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000709 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000710 } else if (location.IsStackSlot()) {
711 __ movl(Address(ESP, location.GetStackIndex()), imm);
712 } else {
713 DCHECK(location.IsConstant());
714 DCHECK_EQ(location.GetConstant(), const_to_move);
715 }
716 } else if (const_to_move->IsLongConstant()) {
717 int64_t value = const_to_move->AsLongConstant()->GetValue();
718 if (location.IsRegisterPair()) {
719 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
720 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
721 } else if (location.IsDoubleStackSlot()) {
722 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000723 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
724 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000725 } else {
726 DCHECK(location.IsConstant());
727 DCHECK_EQ(location.GetConstant(), instruction);
728 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100729 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000730 } else if (instruction->IsTemporary()) {
731 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000732 if (temp_location.IsStackSlot()) {
733 Move32(location, temp_location);
734 } else {
735 DCHECK(temp_location.IsDoubleStackSlot());
736 Move64(location, temp_location);
737 }
Roland Levillain476df552014-10-09 17:51:36 +0100738 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100739 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100740 switch (instruction->GetType()) {
741 case Primitive::kPrimBoolean:
742 case Primitive::kPrimByte:
743 case Primitive::kPrimChar:
744 case Primitive::kPrimShort:
745 case Primitive::kPrimInt:
746 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100747 case Primitive::kPrimFloat:
748 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100749 break;
750
751 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100752 case Primitive::kPrimDouble:
753 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100754 break;
755
756 default:
757 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
758 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000759 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100760 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100761 switch (instruction->GetType()) {
762 case Primitive::kPrimBoolean:
763 case Primitive::kPrimByte:
764 case Primitive::kPrimChar:
765 case Primitive::kPrimShort:
766 case Primitive::kPrimInt:
767 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100768 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000769 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100770 break;
771
772 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100773 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000774 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100775 break;
776
777 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100778 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100779 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000780 }
781}
782
783void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000784 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000785}
786
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000787void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000788 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100789 DCHECK(!successor->IsExitBlock());
790
791 HBasicBlock* block = got->GetBlock();
792 HInstruction* previous = got->GetPrevious();
793
794 HLoopInformation* info = block->GetLoopInformation();
795 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
796 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
797 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
798 return;
799 }
800
801 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
802 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
803 }
804 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000805 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000806 }
807}
808
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000809void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000810 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000811}
812
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000813void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700814 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000815}
816
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000817void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100818 LocationSummary* locations =
819 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100820 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100821 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100822 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100823 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000824}
825
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000826void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700827 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100828 if (cond->IsIntConstant()) {
829 // Constant condition, statically compared against 1.
830 int32_t cond_value = cond->AsIntConstant()->GetValue();
831 if (cond_value == 1) {
832 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
833 if_instr->IfTrueSuccessor())) {
834 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100835 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100836 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100837 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100838 DCHECK_EQ(cond_value, 0);
839 }
840 } else {
841 bool materialized =
842 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
843 // Moves do not affect the eflags register, so if the condition is
844 // evaluated just before the if, we don't need to evaluate it
845 // again.
846 bool eflags_set = cond->IsCondition()
847 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
848 if (materialized) {
849 if (!eflags_set) {
850 // Materialized condition, compare against 0.
851 Location lhs = if_instr->GetLocations()->InAt(0);
852 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500853 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100854 } else {
855 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
856 }
857 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
858 } else {
859 __ j(X86Condition(cond->AsCondition()->GetCondition()),
860 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
861 }
862 } else {
863 Location lhs = cond->GetLocations()->InAt(0);
864 Location rhs = cond->GetLocations()->InAt(1);
865 // LHS is guaranteed to be in a register (see
866 // LocationsBuilderX86::VisitCondition).
867 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000868 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100869 } else if (rhs.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500870 int32_t constant = rhs.GetConstant()->AsIntConstant()->GetValue();
871 if (constant == 0) {
872 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
873 } else {
874 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
875 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100876 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000877 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100878 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100879 __ j(X86Condition(cond->AsCondition()->GetCondition()),
880 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700881 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100882 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100883 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
884 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700885 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000886 }
887}
888
889void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000890 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000891}
892
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000893void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
894 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000895}
896
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000897void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100898 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000899}
900
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000901void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100902 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700903 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000904}
905
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100906void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100907 LocationSummary* locations =
908 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100909 switch (store->InputAt(1)->GetType()) {
910 case Primitive::kPrimBoolean:
911 case Primitive::kPrimByte:
912 case Primitive::kPrimChar:
913 case Primitive::kPrimShort:
914 case Primitive::kPrimInt:
915 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100916 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100917 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
918 break;
919
920 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100921 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100922 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
923 break;
924
925 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100926 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100927 }
928 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000929}
930
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000931void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700932 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000933}
934
Dave Allison20dfc792014-06-16 20:44:29 -0700935void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100936 LocationSummary* locations =
937 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100938 locations->SetInAt(0, Location::RequiresRegister());
939 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100940 if (comp->NeedsMaterialization()) {
Mark Mendell5f874182015-03-04 15:42:45 -0500941 // We need a byte register.
942 locations->SetOut(Location::RegisterLocation(ECX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100943 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000944}
945
Dave Allison20dfc792014-06-16 20:44:29 -0700946void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
947 if (comp->NeedsMaterialization()) {
948 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000949 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100950 // Clear register: setcc only sets the low byte.
951 __ xorl(reg, reg);
Mark Mendell09b84632015-02-13 17:48:38 -0500952 Location lhs = locations->InAt(0);
953 Location rhs = locations->InAt(1);
954 if (rhs.IsRegister()) {
955 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
956 } else if (rhs.IsConstant()) {
Mingyao Yang8928cab2015-03-03 16:15:23 -0800957 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -0500958 if (constant == 0) {
959 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
960 } else {
961 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
962 }
Dave Allison20dfc792014-06-16 20:44:29 -0700963 } else {
Mark Mendell09b84632015-02-13 17:48:38 -0500964 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Dave Allison20dfc792014-06-16 20:44:29 -0700965 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000966 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100967 }
Dave Allison20dfc792014-06-16 20:44:29 -0700968}
969
970void LocationsBuilderX86::VisitEqual(HEqual* comp) {
971 VisitCondition(comp);
972}
973
974void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
975 VisitCondition(comp);
976}
977
978void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
979 VisitCondition(comp);
980}
981
982void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
983 VisitCondition(comp);
984}
985
986void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
987 VisitCondition(comp);
988}
989
990void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
991 VisitCondition(comp);
992}
993
994void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
995 VisitCondition(comp);
996}
997
998void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
999 VisitCondition(comp);
1000}
1001
1002void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1003 VisitCondition(comp);
1004}
1005
1006void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1007 VisitCondition(comp);
1008}
1009
1010void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1011 VisitCondition(comp);
1012}
1013
1014void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1015 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001016}
1017
1018void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001019 LocationSummary* locations =
1020 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001021 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001022}
1023
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001024void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001025 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001026 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001027}
1028
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001029void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1030 LocationSummary* locations =
1031 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1032 locations->SetOut(Location::ConstantLocation(constant));
1033}
1034
1035void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1036 // Will be generated at use site.
1037 UNUSED(constant);
1038}
1039
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001040void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001041 LocationSummary* locations =
1042 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001043 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001044}
1045
1046void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1047 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001048 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001049}
1050
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001051void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1052 LocationSummary* locations =
1053 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1054 locations->SetOut(Location::ConstantLocation(constant));
1055}
1056
1057void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1058 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001059 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001060}
1061
1062void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1063 LocationSummary* locations =
1064 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1065 locations->SetOut(Location::ConstantLocation(constant));
1066}
1067
1068void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1069 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001070 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001071}
1072
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001073void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001074 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001075}
1076
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001077void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001078 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001079 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001080 __ ret();
1081}
1082
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001083void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001084 LocationSummary* locations =
1085 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001086 switch (ret->InputAt(0)->GetType()) {
1087 case Primitive::kPrimBoolean:
1088 case Primitive::kPrimByte:
1089 case Primitive::kPrimChar:
1090 case Primitive::kPrimShort:
1091 case Primitive::kPrimInt:
1092 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001093 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001094 break;
1095
1096 case Primitive::kPrimLong:
1097 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001098 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001099 break;
1100
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001101 case Primitive::kPrimFloat:
1102 case Primitive::kPrimDouble:
1103 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001104 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001105 break;
1106
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001107 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001108 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001109 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001110}
1111
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001112void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001113 if (kIsDebugBuild) {
1114 switch (ret->InputAt(0)->GetType()) {
1115 case Primitive::kPrimBoolean:
1116 case Primitive::kPrimByte:
1117 case Primitive::kPrimChar:
1118 case Primitive::kPrimShort:
1119 case Primitive::kPrimInt:
1120 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001121 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001122 break;
1123
1124 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001125 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1126 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001127 break;
1128
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001129 case Primitive::kPrimFloat:
1130 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001131 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001132 break;
1133
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001134 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001135 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001136 }
1137 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001138 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001139 __ ret();
1140}
1141
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001142void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001143 HandleInvoke(invoke);
1144}
1145
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001146void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001147 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001148
1149 // TODO: Implement all kinds of calls:
1150 // 1) boot -> boot
1151 // 2) app -> boot
1152 // 3) app -> app
1153 //
1154 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1155
1156 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001157 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001158 if (!invoke->IsRecursive()) {
1159 // temp = temp->dex_cache_resolved_methods_;
1160 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
1161 // temp = temp[index_in_cache]
1162 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
1163 // (temp + offset_of_quick_compiled_code)()
1164 __ call(Address(
1165 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
1166 } else {
1167 __ call(codegen_->GetFrameEntryLabel());
1168 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001169
1170 DCHECK(!codegen_->IsLeafMethod());
1171 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1172}
1173
1174void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1175 HandleInvoke(invoke);
1176}
1177
1178void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001179 LocationSummary* locations =
1180 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001181 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001182
1183 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001184 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001185 HInstruction* input = invoke->InputAt(i);
1186 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1187 }
1188
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001189 switch (invoke->GetType()) {
1190 case Primitive::kPrimBoolean:
1191 case Primitive::kPrimByte:
1192 case Primitive::kPrimChar:
1193 case Primitive::kPrimShort:
1194 case Primitive::kPrimInt:
1195 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001196 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001197 break;
1198
1199 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001200 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001201 break;
1202
1203 case Primitive::kPrimVoid:
1204 break;
1205
1206 case Primitive::kPrimDouble:
1207 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001208 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001209 break;
1210 }
1211
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001212 invoke->SetLocations(locations);
1213}
1214
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001215void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001216 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001217 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1218 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1219 LocationSummary* locations = invoke->GetLocations();
1220 Location receiver = locations->InAt(0);
1221 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1222 // temp = object->GetClass();
1223 if (receiver.IsStackSlot()) {
1224 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1225 __ movl(temp, Address(temp, class_offset));
1226 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001227 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001228 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001229 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001230 // temp = temp->GetMethodAt(method_offset);
1231 __ movl(temp, Address(temp, method_offset));
1232 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001233 __ call(Address(
1234 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001235
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001236 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001237 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001238}
1239
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001240void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1241 HandleInvoke(invoke);
1242 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001243 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001244}
1245
1246void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1247 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001248 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001249 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1250 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1251 LocationSummary* locations = invoke->GetLocations();
1252 Location receiver = locations->InAt(0);
1253 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1254
1255 // Set the hidden argument.
1256 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001257 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001258
1259 // temp = object->GetClass();
1260 if (receiver.IsStackSlot()) {
1261 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1262 __ movl(temp, Address(temp, class_offset));
1263 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001264 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001265 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001266 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001267 // temp = temp->GetImtEntryAt(method_offset);
1268 __ movl(temp, Address(temp, method_offset));
1269 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001270 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001271 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001272
1273 DCHECK(!codegen_->IsLeafMethod());
1274 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1275}
1276
Roland Levillain88cb1752014-10-20 16:36:47 +01001277void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1278 LocationSummary* locations =
1279 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1280 switch (neg->GetResultType()) {
1281 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001282 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001283 locations->SetInAt(0, Location::RequiresRegister());
1284 locations->SetOut(Location::SameAsFirstInput());
1285 break;
1286
Roland Levillain88cb1752014-10-20 16:36:47 +01001287 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001288 locations->SetInAt(0, Location::RequiresFpuRegister());
1289 locations->SetOut(Location::SameAsFirstInput());
1290 locations->AddTemp(Location::RequiresRegister());
1291 locations->AddTemp(Location::RequiresFpuRegister());
1292 break;
1293
Roland Levillain88cb1752014-10-20 16:36:47 +01001294 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001295 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001296 locations->SetOut(Location::SameAsFirstInput());
1297 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001298 break;
1299
1300 default:
1301 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1302 }
1303}
1304
1305void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1306 LocationSummary* locations = neg->GetLocations();
1307 Location out = locations->Out();
1308 Location in = locations->InAt(0);
1309 switch (neg->GetResultType()) {
1310 case Primitive::kPrimInt:
1311 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001312 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001313 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001314 break;
1315
1316 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001317 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001318 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001319 __ negl(out.AsRegisterPairLow<Register>());
1320 // Negation is similar to subtraction from zero. The least
1321 // significant byte triggers a borrow when it is different from
1322 // zero; to take it into account, add 1 to the most significant
1323 // byte if the carry flag (CF) is set to 1 after the first NEGL
1324 // operation.
1325 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1326 __ negl(out.AsRegisterPairHigh<Register>());
1327 break;
1328
Roland Levillain5368c212014-11-27 15:03:41 +00001329 case Primitive::kPrimFloat: {
1330 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001331 Register constant = locations->GetTemp(0).AsRegister<Register>();
1332 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001333 // Implement float negation with an exclusive or with value
1334 // 0x80000000 (mask for bit 31, representing the sign of a
1335 // single-precision floating-point number).
1336 __ movl(constant, Immediate(INT32_C(0x80000000)));
1337 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001338 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001339 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001340 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001341
Roland Levillain5368c212014-11-27 15:03:41 +00001342 case Primitive::kPrimDouble: {
1343 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001344 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001345 // Implement double negation with an exclusive or with value
1346 // 0x8000000000000000 (mask for bit 63, representing the sign of
1347 // a double-precision floating-point number).
1348 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001349 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001350 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001351 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001352
1353 default:
1354 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1355 }
1356}
1357
Roland Levillaindff1f282014-11-05 14:15:05 +00001358void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001359 Primitive::Type result_type = conversion->GetResultType();
1360 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001361 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001362
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001363 // The float-to-long and double-to-long type conversions rely on a
1364 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001365 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001366 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1367 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001368 ? LocationSummary::kCall
1369 : LocationSummary::kNoCall;
1370 LocationSummary* locations =
1371 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1372
Roland Levillaindff1f282014-11-05 14:15:05 +00001373 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001374 case Primitive::kPrimByte:
1375 switch (input_type) {
1376 case Primitive::kPrimShort:
1377 case Primitive::kPrimInt:
1378 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001379 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001380 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1381 // Make the output overlap to please the register allocator. This greatly simplifies
1382 // the validation of the linear scan implementation
1383 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001384 break;
1385
1386 default:
1387 LOG(FATAL) << "Unexpected type conversion from " << input_type
1388 << " to " << result_type;
1389 }
1390 break;
1391
Roland Levillain01a8d712014-11-14 16:27:39 +00001392 case Primitive::kPrimShort:
1393 switch (input_type) {
1394 case Primitive::kPrimByte:
1395 case Primitive::kPrimInt:
1396 case Primitive::kPrimChar:
1397 // Processing a Dex `int-to-short' instruction.
1398 locations->SetInAt(0, Location::Any());
1399 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1400 break;
1401
1402 default:
1403 LOG(FATAL) << "Unexpected type conversion from " << input_type
1404 << " to " << result_type;
1405 }
1406 break;
1407
Roland Levillain946e1432014-11-11 17:35:19 +00001408 case Primitive::kPrimInt:
1409 switch (input_type) {
1410 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001411 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001412 locations->SetInAt(0, Location::Any());
1413 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1414 break;
1415
1416 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001417 // Processing a Dex `float-to-int' instruction.
1418 locations->SetInAt(0, Location::RequiresFpuRegister());
1419 locations->SetOut(Location::RequiresRegister());
1420 locations->AddTemp(Location::RequiresFpuRegister());
1421 break;
1422
Roland Levillain946e1432014-11-11 17:35:19 +00001423 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001424 // Processing a Dex `double-to-int' instruction.
1425 locations->SetInAt(0, Location::RequiresFpuRegister());
1426 locations->SetOut(Location::RequiresRegister());
1427 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001428 break;
1429
1430 default:
1431 LOG(FATAL) << "Unexpected type conversion from " << input_type
1432 << " to " << result_type;
1433 }
1434 break;
1435
Roland Levillaindff1f282014-11-05 14:15:05 +00001436 case Primitive::kPrimLong:
1437 switch (input_type) {
1438 case Primitive::kPrimByte:
1439 case Primitive::kPrimShort:
1440 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001441 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001442 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001443 locations->SetInAt(0, Location::RegisterLocation(EAX));
1444 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1445 break;
1446
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001447 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001448 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001449 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001450 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001451 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1452 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1453
Vladimir Marko949c91f2015-01-27 10:48:44 +00001454 // The runtime helper puts the result in EAX, EDX.
1455 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001456 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001457 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001458
1459 default:
1460 LOG(FATAL) << "Unexpected type conversion from " << input_type
1461 << " to " << result_type;
1462 }
1463 break;
1464
Roland Levillain981e4542014-11-14 11:47:14 +00001465 case Primitive::kPrimChar:
1466 switch (input_type) {
1467 case Primitive::kPrimByte:
1468 case Primitive::kPrimShort:
1469 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001470 // Processing a Dex `int-to-char' instruction.
1471 locations->SetInAt(0, Location::Any());
1472 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
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::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001482 switch (input_type) {
1483 case Primitive::kPrimByte:
1484 case Primitive::kPrimShort:
1485 case Primitive::kPrimInt:
1486 case Primitive::kPrimChar:
1487 // Processing a Dex `int-to-float' instruction.
1488 locations->SetInAt(0, Location::RequiresRegister());
1489 locations->SetOut(Location::RequiresFpuRegister());
1490 break;
1491
1492 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001493 // Processing a Dex `long-to-float' instruction.
1494 locations->SetInAt(0, Location::RequiresRegister());
1495 locations->SetOut(Location::RequiresFpuRegister());
1496 locations->AddTemp(Location::RequiresFpuRegister());
1497 locations->AddTemp(Location::RequiresFpuRegister());
1498 break;
1499
Roland Levillaincff13742014-11-17 14:32:17 +00001500 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001501 // Processing a Dex `double-to-float' instruction.
1502 locations->SetInAt(0, Location::RequiresFpuRegister());
1503 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001504 break;
1505
1506 default:
1507 LOG(FATAL) << "Unexpected type conversion from " << input_type
1508 << " to " << result_type;
1509 };
1510 break;
1511
Roland Levillaindff1f282014-11-05 14:15:05 +00001512 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001513 switch (input_type) {
1514 case Primitive::kPrimByte:
1515 case Primitive::kPrimShort:
1516 case Primitive::kPrimInt:
1517 case Primitive::kPrimChar:
1518 // Processing a Dex `int-to-double' instruction.
1519 locations->SetInAt(0, Location::RequiresRegister());
1520 locations->SetOut(Location::RequiresFpuRegister());
1521 break;
1522
1523 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001524 // Processing a Dex `long-to-double' instruction.
1525 locations->SetInAt(0, Location::RequiresRegister());
1526 locations->SetOut(Location::RequiresFpuRegister());
1527 locations->AddTemp(Location::RequiresFpuRegister());
1528 locations->AddTemp(Location::RequiresFpuRegister());
1529 break;
1530
Roland Levillaincff13742014-11-17 14:32:17 +00001531 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001532 // Processing a Dex `float-to-double' instruction.
1533 locations->SetInAt(0, Location::RequiresFpuRegister());
1534 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001535 break;
1536
1537 default:
1538 LOG(FATAL) << "Unexpected type conversion from " << input_type
1539 << " to " << result_type;
1540 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001541 break;
1542
1543 default:
1544 LOG(FATAL) << "Unexpected type conversion from " << input_type
1545 << " to " << result_type;
1546 }
1547}
1548
1549void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1550 LocationSummary* locations = conversion->GetLocations();
1551 Location out = locations->Out();
1552 Location in = locations->InAt(0);
1553 Primitive::Type result_type = conversion->GetResultType();
1554 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001555 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001556 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001557 case Primitive::kPrimByte:
1558 switch (input_type) {
1559 case Primitive::kPrimShort:
1560 case Primitive::kPrimInt:
1561 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001562 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001563 if (in.IsRegister()) {
1564 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001565 } else {
1566 DCHECK(in.GetConstant()->IsIntConstant());
1567 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1568 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1569 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001570 break;
1571
1572 default:
1573 LOG(FATAL) << "Unexpected type conversion from " << input_type
1574 << " to " << result_type;
1575 }
1576 break;
1577
Roland Levillain01a8d712014-11-14 16:27:39 +00001578 case Primitive::kPrimShort:
1579 switch (input_type) {
1580 case Primitive::kPrimByte:
1581 case Primitive::kPrimInt:
1582 case Primitive::kPrimChar:
1583 // Processing a Dex `int-to-short' instruction.
1584 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001585 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001586 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001587 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001588 } else {
1589 DCHECK(in.GetConstant()->IsIntConstant());
1590 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001591 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001592 }
1593 break;
1594
1595 default:
1596 LOG(FATAL) << "Unexpected type conversion from " << input_type
1597 << " to " << result_type;
1598 }
1599 break;
1600
Roland Levillain946e1432014-11-11 17:35:19 +00001601 case Primitive::kPrimInt:
1602 switch (input_type) {
1603 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001604 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001605 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001606 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001607 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001608 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001609 } else {
1610 DCHECK(in.IsConstant());
1611 DCHECK(in.GetConstant()->IsLongConstant());
1612 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001613 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001614 }
1615 break;
1616
Roland Levillain3f8f9362014-12-02 17:45:01 +00001617 case Primitive::kPrimFloat: {
1618 // Processing a Dex `float-to-int' instruction.
1619 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1620 Register output = out.AsRegister<Register>();
1621 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1622 Label done, nan;
1623
1624 __ movl(output, Immediate(kPrimIntMax));
1625 // temp = int-to-float(output)
1626 __ cvtsi2ss(temp, output);
1627 // if input >= temp goto done
1628 __ comiss(input, temp);
1629 __ j(kAboveEqual, &done);
1630 // if input == NaN goto nan
1631 __ j(kUnordered, &nan);
1632 // output = float-to-int-truncate(input)
1633 __ cvttss2si(output, input);
1634 __ jmp(&done);
1635 __ Bind(&nan);
1636 // output = 0
1637 __ xorl(output, output);
1638 __ Bind(&done);
1639 break;
1640 }
1641
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001642 case Primitive::kPrimDouble: {
1643 // Processing a Dex `double-to-int' instruction.
1644 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1645 Register output = out.AsRegister<Register>();
1646 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1647 Label done, nan;
1648
1649 __ movl(output, Immediate(kPrimIntMax));
1650 // temp = int-to-double(output)
1651 __ cvtsi2sd(temp, output);
1652 // if input >= temp goto done
1653 __ comisd(input, temp);
1654 __ j(kAboveEqual, &done);
1655 // if input == NaN goto nan
1656 __ j(kUnordered, &nan);
1657 // output = double-to-int-truncate(input)
1658 __ cvttsd2si(output, input);
1659 __ jmp(&done);
1660 __ Bind(&nan);
1661 // output = 0
1662 __ xorl(output, output);
1663 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001664 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001665 }
Roland Levillain946e1432014-11-11 17:35:19 +00001666
1667 default:
1668 LOG(FATAL) << "Unexpected type conversion from " << input_type
1669 << " to " << result_type;
1670 }
1671 break;
1672
Roland Levillaindff1f282014-11-05 14:15:05 +00001673 case Primitive::kPrimLong:
1674 switch (input_type) {
1675 case Primitive::kPrimByte:
1676 case Primitive::kPrimShort:
1677 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001678 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001679 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001680 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1681 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001682 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001683 __ cdq();
1684 break;
1685
1686 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001687 // Processing a Dex `float-to-long' instruction.
1688 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001689 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1690 break;
1691
Roland Levillaindff1f282014-11-05 14:15:05 +00001692 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001693 // Processing a Dex `double-to-long' instruction.
1694 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1695 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001696 break;
1697
1698 default:
1699 LOG(FATAL) << "Unexpected type conversion from " << input_type
1700 << " to " << result_type;
1701 }
1702 break;
1703
Roland Levillain981e4542014-11-14 11:47:14 +00001704 case Primitive::kPrimChar:
1705 switch (input_type) {
1706 case Primitive::kPrimByte:
1707 case Primitive::kPrimShort:
1708 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001709 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1710 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001711 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001712 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001713 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001714 } else {
1715 DCHECK(in.GetConstant()->IsIntConstant());
1716 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001717 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001718 }
1719 break;
1720
1721 default:
1722 LOG(FATAL) << "Unexpected type conversion from " << input_type
1723 << " to " << result_type;
1724 }
1725 break;
1726
Roland Levillaindff1f282014-11-05 14:15:05 +00001727 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001728 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001729 case Primitive::kPrimByte:
1730 case Primitive::kPrimShort:
1731 case Primitive::kPrimInt:
1732 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001733 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001734 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001735 break;
1736
Roland Levillain6d0e4832014-11-27 18:31:21 +00001737 case Primitive::kPrimLong: {
1738 // Processing a Dex `long-to-float' instruction.
1739 Register low = in.AsRegisterPairLow<Register>();
1740 Register high = in.AsRegisterPairHigh<Register>();
1741 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1742 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1743 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1744
1745 // Operations use doubles for precision reasons (each 32-bit
1746 // half of a long fits in the 53-bit mantissa of a double,
1747 // but not in the 24-bit mantissa of a float). This is
1748 // especially important for the low bits. The result is
1749 // eventually converted to float.
1750
1751 // low = low - 2^31 (to prevent bit 31 of `low` to be
1752 // interpreted as a sign bit)
1753 __ subl(low, Immediate(0x80000000));
1754 // temp = int-to-double(high)
1755 __ cvtsi2sd(temp, high);
1756 // temp = temp * 2^32
1757 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
1758 __ mulsd(temp, constant);
1759 // result = int-to-double(low)
1760 __ cvtsi2sd(result, low);
1761 // result = result + 2^31 (restore the original value of `low`)
1762 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
1763 __ addsd(result, constant);
1764 // result = result + temp
1765 __ addsd(result, temp);
1766 // result = double-to-float(result)
1767 __ cvtsd2ss(result, result);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001768 // Restore low.
1769 __ addl(low, Immediate(0x80000000));
Roland Levillain6d0e4832014-11-27 18:31:21 +00001770 break;
1771 }
1772
Roland Levillaincff13742014-11-17 14:32:17 +00001773 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001774 // Processing a Dex `double-to-float' instruction.
1775 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001776 break;
1777
1778 default:
1779 LOG(FATAL) << "Unexpected type conversion from " << input_type
1780 << " to " << result_type;
1781 };
1782 break;
1783
Roland Levillaindff1f282014-11-05 14:15:05 +00001784 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001785 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001786 case Primitive::kPrimByte:
1787 case Primitive::kPrimShort:
1788 case Primitive::kPrimInt:
1789 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001790 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001791 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001792 break;
1793
Roland Levillain647b9ed2014-11-27 12:06:00 +00001794 case Primitive::kPrimLong: {
1795 // Processing a Dex `long-to-double' instruction.
1796 Register low = in.AsRegisterPairLow<Register>();
1797 Register high = in.AsRegisterPairHigh<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001798 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1799 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1800 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001801
Roland Levillain647b9ed2014-11-27 12:06:00 +00001802 // low = low - 2^31 (to prevent bit 31 of `low` to be
1803 // interpreted as a sign bit)
1804 __ subl(low, Immediate(0x80000000));
1805 // temp = int-to-double(high)
1806 __ cvtsi2sd(temp, high);
1807 // temp = temp * 2^32
Roland Levillain6d0e4832014-11-27 18:31:21 +00001808 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001809 __ mulsd(temp, constant);
1810 // result = int-to-double(low)
1811 __ cvtsi2sd(result, low);
1812 // result = result + 2^31 (restore the original value of `low`)
Roland Levillain6d0e4832014-11-27 18:31:21 +00001813 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001814 __ addsd(result, constant);
1815 // result = result + temp
1816 __ addsd(result, temp);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001817 // Restore low.
1818 __ addl(low, Immediate(0x80000000));
Roland Levillain647b9ed2014-11-27 12:06:00 +00001819 break;
1820 }
1821
Roland Levillaincff13742014-11-17 14:32:17 +00001822 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001823 // Processing a Dex `float-to-double' instruction.
1824 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001825 break;
1826
1827 default:
1828 LOG(FATAL) << "Unexpected type conversion from " << input_type
1829 << " to " << result_type;
1830 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001831 break;
1832
1833 default:
1834 LOG(FATAL) << "Unexpected type conversion from " << input_type
1835 << " to " << result_type;
1836 }
1837}
1838
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001839void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001840 LocationSummary* locations =
1841 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001842 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001843 case Primitive::kPrimInt: {
1844 locations->SetInAt(0, Location::RequiresRegister());
1845 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1846 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1847 break;
1848 }
1849
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001850 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001851 locations->SetInAt(0, Location::RequiresRegister());
1852 locations->SetInAt(1, Location::Any());
1853 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001854 break;
1855 }
1856
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001857 case Primitive::kPrimFloat:
1858 case Primitive::kPrimDouble: {
1859 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00001860 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001861 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001862 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001863 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001864
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001865 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001866 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1867 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001868 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001869}
1870
1871void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1872 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001873 Location first = locations->InAt(0);
1874 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05001875 Location out = locations->Out();
1876
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001877 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001878 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001879 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001880 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1881 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
1882 } else {
1883 __ leal(out.AsRegister<Register>(), Address(
1884 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
1885 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001886 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001887 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
1888 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1889 __ addl(out.AsRegister<Register>(), Immediate(value));
1890 } else {
1891 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
1892 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001893 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001894 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001895 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001896 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001897 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001898 }
1899
1900 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001901 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001902 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1903 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001904 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001905 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1906 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001907 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001908 } else {
1909 DCHECK(second.IsConstant()) << second;
1910 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
1911 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1912 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001913 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001914 break;
1915 }
1916
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001917 case Primitive::kPrimFloat: {
1918 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001919 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001920 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001921 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001922 }
1923
1924 case Primitive::kPrimDouble: {
1925 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001926 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001927 }
1928 break;
1929 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001930
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001931 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001932 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001933 }
1934}
1935
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001936void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001937 LocationSummary* locations =
1938 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001939 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001940 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001941 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001942 locations->SetInAt(0, Location::RequiresRegister());
1943 locations->SetInAt(1, Location::Any());
1944 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001945 break;
1946 }
Calin Juravle11351682014-10-23 15:38:15 +01001947 case Primitive::kPrimFloat:
1948 case Primitive::kPrimDouble: {
1949 locations->SetInAt(0, Location::RequiresFpuRegister());
1950 locations->SetInAt(1, Location::RequiresFpuRegister());
1951 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001952 break;
Calin Juravle11351682014-10-23 15:38:15 +01001953 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001954
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001955 default:
Calin Juravle11351682014-10-23 15:38:15 +01001956 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001957 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001958}
1959
1960void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1961 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001962 Location first = locations->InAt(0);
1963 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001964 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001965 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001966 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001967 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001968 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001969 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001970 __ subl(first.AsRegister<Register>(),
1971 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001972 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001973 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001974 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001975 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001976 }
1977
1978 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001979 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01001980 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1981 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001982 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01001983 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001984 __ sbbl(first.AsRegisterPairHigh<Register>(),
1985 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001986 } else {
1987 DCHECK(second.IsConstant()) << second;
1988 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
1989 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1990 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001991 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001992 break;
1993 }
1994
Calin Juravle11351682014-10-23 15:38:15 +01001995 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001996 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001997 break;
Calin Juravle11351682014-10-23 15:38:15 +01001998 }
1999
2000 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002001 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002002 break;
2003 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002004
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002005 default:
Calin Juravle11351682014-10-23 15:38:15 +01002006 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002007 }
2008}
2009
Calin Juravle34bacdf2014-10-07 20:23:36 +01002010void LocationsBuilderX86::VisitMul(HMul* mul) {
2011 LocationSummary* locations =
2012 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2013 switch (mul->GetResultType()) {
2014 case Primitive::kPrimInt:
2015 locations->SetInAt(0, Location::RequiresRegister());
2016 locations->SetInAt(1, Location::Any());
2017 locations->SetOut(Location::SameAsFirstInput());
2018 break;
2019 case Primitive::kPrimLong: {
2020 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002021 locations->SetInAt(1, Location::Any());
2022 locations->SetOut(Location::SameAsFirstInput());
2023 // Needed for imul on 32bits with 64bits output.
2024 locations->AddTemp(Location::RegisterLocation(EAX));
2025 locations->AddTemp(Location::RegisterLocation(EDX));
2026 break;
2027 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002028 case Primitive::kPrimFloat:
2029 case Primitive::kPrimDouble: {
2030 locations->SetInAt(0, Location::RequiresFpuRegister());
2031 locations->SetInAt(1, Location::RequiresFpuRegister());
2032 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002033 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002034 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002035
2036 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002037 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002038 }
2039}
2040
2041void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2042 LocationSummary* locations = mul->GetLocations();
2043 Location first = locations->InAt(0);
2044 Location second = locations->InAt(1);
2045 DCHECK(first.Equals(locations->Out()));
2046
2047 switch (mul->GetResultType()) {
2048 case Primitive::kPrimInt: {
2049 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002050 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002051 } else if (second.IsConstant()) {
2052 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002053 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002054 } else {
2055 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002056 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002057 }
2058 break;
2059 }
2060
2061 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002062 Register in1_hi = first.AsRegisterPairHigh<Register>();
2063 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002064 Register eax = locations->GetTemp(0).AsRegister<Register>();
2065 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002066
2067 DCHECK_EQ(EAX, eax);
2068 DCHECK_EQ(EDX, edx);
2069
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002070 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002071 // output: in1
2072 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2073 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2074 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002075 if (second.IsConstant()) {
2076 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002077
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002078 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2079 int32_t low_value = Low32Bits(value);
2080 int32_t high_value = High32Bits(value);
2081 Immediate low(low_value);
2082 Immediate high(high_value);
2083
2084 __ movl(eax, high);
2085 // eax <- in1.lo * in2.hi
2086 __ imull(eax, in1_lo);
2087 // in1.hi <- in1.hi * in2.lo
2088 __ imull(in1_hi, low);
2089 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2090 __ addl(in1_hi, eax);
2091 // move in2_lo to eax to prepare for double precision
2092 __ movl(eax, low);
2093 // edx:eax <- in1.lo * in2.lo
2094 __ mull(in1_lo);
2095 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2096 __ addl(in1_hi, edx);
2097 // in1.lo <- (in1.lo * in2.lo)[31:0];
2098 __ movl(in1_lo, eax);
2099 } else if (second.IsRegisterPair()) {
2100 Register in2_hi = second.AsRegisterPairHigh<Register>();
2101 Register in2_lo = second.AsRegisterPairLow<Register>();
2102
2103 __ movl(eax, in2_hi);
2104 // eax <- in1.lo * in2.hi
2105 __ imull(eax, in1_lo);
2106 // in1.hi <- in1.hi * in2.lo
2107 __ imull(in1_hi, in2_lo);
2108 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2109 __ addl(in1_hi, eax);
2110 // move in1_lo to eax to prepare for double precision
2111 __ movl(eax, in1_lo);
2112 // edx:eax <- in1.lo * in2.lo
2113 __ mull(in2_lo);
2114 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2115 __ addl(in1_hi, edx);
2116 // in1.lo <- (in1.lo * in2.lo)[31:0];
2117 __ movl(in1_lo, eax);
2118 } else {
2119 DCHECK(second.IsDoubleStackSlot()) << second;
2120 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2121 Address in2_lo(ESP, second.GetStackIndex());
2122
2123 __ movl(eax, in2_hi);
2124 // eax <- in1.lo * in2.hi
2125 __ imull(eax, in1_lo);
2126 // in1.hi <- in1.hi * in2.lo
2127 __ imull(in1_hi, in2_lo);
2128 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2129 __ addl(in1_hi, eax);
2130 // move in1_lo to eax to prepare for double precision
2131 __ movl(eax, in1_lo);
2132 // edx:eax <- in1.lo * in2.lo
2133 __ mull(in2_lo);
2134 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2135 __ addl(in1_hi, edx);
2136 // in1.lo <- (in1.lo * in2.lo)[31:0];
2137 __ movl(in1_lo, eax);
2138 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002139
2140 break;
2141 }
2142
Calin Juravleb5bfa962014-10-21 18:02:24 +01002143 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002144 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002145 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002146 }
2147
2148 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002149 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002150 break;
2151 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002152
2153 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002154 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002155 }
2156}
2157
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002158void InstructionCodeGeneratorX86::PushOntoFPStack(Location source, uint32_t temp_offset,
2159 uint32_t stack_adjustment, bool is_float) {
2160 if (source.IsStackSlot()) {
2161 DCHECK(is_float);
2162 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2163 } else if (source.IsDoubleStackSlot()) {
2164 DCHECK(!is_float);
2165 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2166 } else {
2167 // Write the value to the temporary location on the stack and load to FP stack.
2168 if (is_float) {
2169 Location stack_temp = Location::StackSlot(temp_offset);
2170 codegen_->Move32(stack_temp, source);
2171 __ flds(Address(ESP, temp_offset));
2172 } else {
2173 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2174 codegen_->Move64(stack_temp, source);
2175 __ fldl(Address(ESP, temp_offset));
2176 }
2177 }
2178}
2179
2180void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2181 Primitive::Type type = rem->GetResultType();
2182 bool is_float = type == Primitive::kPrimFloat;
2183 size_t elem_size = Primitive::ComponentSize(type);
2184 LocationSummary* locations = rem->GetLocations();
2185 Location first = locations->InAt(0);
2186 Location second = locations->InAt(1);
2187 Location out = locations->Out();
2188
2189 // Create stack space for 2 elements.
2190 // TODO: enhance register allocator to ask for stack temporaries.
2191 __ subl(ESP, Immediate(2 * elem_size));
2192
2193 // Load the values to the FP stack in reverse order, using temporaries if needed.
2194 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2195 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2196
2197 // Loop doing FPREM until we stabilize.
2198 Label retry;
2199 __ Bind(&retry);
2200 __ fprem();
2201
2202 // Move FP status to AX.
2203 __ fstsw();
2204
2205 // And see if the argument reduction is complete. This is signaled by the
2206 // C2 FPU flag bit set to 0.
2207 __ andl(EAX, Immediate(kC2ConditionMask));
2208 __ j(kNotEqual, &retry);
2209
2210 // We have settled on the final value. Retrieve it into an XMM register.
2211 // Store FP top of stack to real stack.
2212 if (is_float) {
2213 __ fsts(Address(ESP, 0));
2214 } else {
2215 __ fstl(Address(ESP, 0));
2216 }
2217
2218 // Pop the 2 items from the FP stack.
2219 __ fucompp();
2220
2221 // Load the value from the stack into an XMM register.
2222 DCHECK(out.IsFpuRegister()) << out;
2223 if (is_float) {
2224 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2225 } else {
2226 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2227 }
2228
2229 // And remove the temporary stack space we allocated.
2230 __ addl(ESP, Immediate(2 * elem_size));
2231}
2232
Calin Juravlebacfec32014-11-14 15:54:36 +00002233void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2234 DCHECK(instruction->IsDiv() || instruction->IsRem());
2235
2236 LocationSummary* locations = instruction->GetLocations();
2237 Location out = locations->Out();
2238 Location first = locations->InAt(0);
2239 Location second = locations->InAt(1);
2240 bool is_div = instruction->IsDiv();
2241
2242 switch (instruction->GetResultType()) {
2243 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002244 Register second_reg = second.AsRegister<Register>();
2245 DCHECK_EQ(EAX, first.AsRegister<Register>());
2246 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002247
2248 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002249 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
2250 is_div);
Calin Juravlebacfec32014-11-14 15:54:36 +00002251 codegen_->AddSlowPath(slow_path);
2252
2253 // 0x80000000/-1 triggers an arithmetic exception!
2254 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2255 // it's safe to just use negl instead of more complex comparisons.
2256
2257 __ cmpl(second_reg, Immediate(-1));
2258 __ j(kEqual, slow_path->GetEntryLabel());
2259
2260 // edx:eax <- sign-extended of eax
2261 __ cdq();
2262 // eax = quotient, edx = remainder
2263 __ idivl(second_reg);
2264
2265 __ Bind(slow_path->GetExitLabel());
2266 break;
2267 }
2268
2269 case Primitive::kPrimLong: {
2270 InvokeRuntimeCallingConvention calling_convention;
2271 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2272 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2273 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2274 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2275 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2276 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2277
2278 if (is_div) {
2279 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2280 } else {
2281 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2282 }
2283 uint32_t dex_pc = is_div
2284 ? instruction->AsDiv()->GetDexPc()
2285 : instruction->AsRem()->GetDexPc();
2286 codegen_->RecordPcInfo(instruction, dex_pc);
2287
2288 break;
2289 }
2290
2291 default:
2292 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2293 }
2294}
2295
Calin Juravle7c4954d2014-10-28 16:57:40 +00002296void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002297 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002298 ? LocationSummary::kCall
2299 : LocationSummary::kNoCall;
2300 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2301
Calin Juravle7c4954d2014-10-28 16:57:40 +00002302 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002303 case Primitive::kPrimInt: {
2304 locations->SetInAt(0, Location::RegisterLocation(EAX));
2305 locations->SetInAt(1, Location::RequiresRegister());
2306 locations->SetOut(Location::SameAsFirstInput());
2307 // Intel uses edx:eax as the dividend.
2308 locations->AddTemp(Location::RegisterLocation(EDX));
2309 break;
2310 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002311 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002312 InvokeRuntimeCallingConvention calling_convention;
2313 locations->SetInAt(0, Location::RegisterPairLocation(
2314 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2315 locations->SetInAt(1, Location::RegisterPairLocation(
2316 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2317 // Runtime helper puts the result in EAX, EDX.
2318 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002319 break;
2320 }
2321 case Primitive::kPrimFloat:
2322 case Primitive::kPrimDouble: {
2323 locations->SetInAt(0, Location::RequiresFpuRegister());
2324 locations->SetInAt(1, Location::RequiresFpuRegister());
2325 locations->SetOut(Location::SameAsFirstInput());
2326 break;
2327 }
2328
2329 default:
2330 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2331 }
2332}
2333
2334void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2335 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002336 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002337 Location first = locations->InAt(0);
2338 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002339
2340 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002341 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002342 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002343 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002344 break;
2345 }
2346
2347 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002348 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002349 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002350 break;
2351 }
2352
2353 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002354 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002355 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002356 break;
2357 }
2358
2359 default:
2360 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2361 }
2362}
2363
Calin Juravlebacfec32014-11-14 15:54:36 +00002364void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002365 Primitive::Type type = rem->GetResultType();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002366 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
2367 ? LocationSummary::kCall
2368 : LocationSummary::kNoCall;
2369 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00002370
Calin Juravled2ec87d2014-12-08 14:24:46 +00002371 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002372 case Primitive::kPrimInt: {
2373 locations->SetInAt(0, Location::RegisterLocation(EAX));
2374 locations->SetInAt(1, Location::RequiresRegister());
2375 locations->SetOut(Location::RegisterLocation(EDX));
2376 break;
2377 }
2378 case Primitive::kPrimLong: {
2379 InvokeRuntimeCallingConvention calling_convention;
2380 locations->SetInAt(0, Location::RegisterPairLocation(
2381 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2382 locations->SetInAt(1, Location::RegisterPairLocation(
2383 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2384 // Runtime helper puts the result in EAX, EDX.
2385 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2386 break;
2387 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002388 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002389 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002390 locations->SetInAt(0, Location::Any());
2391 locations->SetInAt(1, Location::Any());
2392 locations->SetOut(Location::RequiresFpuRegister());
2393 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002394 break;
2395 }
2396
2397 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002398 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002399 }
2400}
2401
2402void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2403 Primitive::Type type = rem->GetResultType();
2404 switch (type) {
2405 case Primitive::kPrimInt:
2406 case Primitive::kPrimLong: {
2407 GenerateDivRemIntegral(rem);
2408 break;
2409 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002410 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002411 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002412 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002413 break;
2414 }
2415 default:
2416 LOG(FATAL) << "Unexpected rem type " << type;
2417 }
2418}
2419
Calin Juravled0d48522014-11-04 16:40:20 +00002420void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2421 LocationSummary* locations =
2422 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002423 switch (instruction->GetType()) {
2424 case Primitive::kPrimInt: {
2425 locations->SetInAt(0, Location::Any());
2426 break;
2427 }
2428 case Primitive::kPrimLong: {
2429 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2430 if (!instruction->IsConstant()) {
2431 locations->AddTemp(Location::RequiresRegister());
2432 }
2433 break;
2434 }
2435 default:
2436 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2437 }
Calin Juravled0d48522014-11-04 16:40:20 +00002438 if (instruction->HasUses()) {
2439 locations->SetOut(Location::SameAsFirstInput());
2440 }
2441}
2442
2443void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2444 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2445 codegen_->AddSlowPath(slow_path);
2446
2447 LocationSummary* locations = instruction->GetLocations();
2448 Location value = locations->InAt(0);
2449
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002450 switch (instruction->GetType()) {
2451 case Primitive::kPrimInt: {
2452 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002453 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002454 __ j(kEqual, slow_path->GetEntryLabel());
2455 } else if (value.IsStackSlot()) {
2456 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2457 __ j(kEqual, slow_path->GetEntryLabel());
2458 } else {
2459 DCHECK(value.IsConstant()) << value;
2460 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2461 __ jmp(slow_path->GetEntryLabel());
2462 }
2463 }
2464 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002465 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002466 case Primitive::kPrimLong: {
2467 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002468 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002469 __ movl(temp, value.AsRegisterPairLow<Register>());
2470 __ orl(temp, value.AsRegisterPairHigh<Register>());
2471 __ j(kEqual, slow_path->GetEntryLabel());
2472 } else {
2473 DCHECK(value.IsConstant()) << value;
2474 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2475 __ jmp(slow_path->GetEntryLabel());
2476 }
2477 }
2478 break;
2479 }
2480 default:
2481 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002482 }
Calin Juravled0d48522014-11-04 16:40:20 +00002483}
2484
Calin Juravle9aec02f2014-11-18 23:06:35 +00002485void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2486 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2487
2488 LocationSummary* locations =
2489 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2490
2491 switch (op->GetResultType()) {
2492 case Primitive::kPrimInt: {
2493 locations->SetInAt(0, Location::RequiresRegister());
2494 // The shift count needs to be in CL.
2495 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2496 locations->SetOut(Location::SameAsFirstInput());
2497 break;
2498 }
2499 case Primitive::kPrimLong: {
2500 locations->SetInAt(0, Location::RequiresRegister());
2501 // The shift count needs to be in CL.
2502 locations->SetInAt(1, Location::RegisterLocation(ECX));
2503 locations->SetOut(Location::SameAsFirstInput());
2504 break;
2505 }
2506 default:
2507 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2508 }
2509}
2510
2511void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2512 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2513
2514 LocationSummary* locations = op->GetLocations();
2515 Location first = locations->InAt(0);
2516 Location second = locations->InAt(1);
2517 DCHECK(first.Equals(locations->Out()));
2518
2519 switch (op->GetResultType()) {
2520 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002521 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002522 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002523 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002524 DCHECK_EQ(ECX, second_reg);
2525 if (op->IsShl()) {
2526 __ shll(first_reg, second_reg);
2527 } else if (op->IsShr()) {
2528 __ sarl(first_reg, second_reg);
2529 } else {
2530 __ shrl(first_reg, second_reg);
2531 }
2532 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002533 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002534 if (op->IsShl()) {
2535 __ shll(first_reg, imm);
2536 } else if (op->IsShr()) {
2537 __ sarl(first_reg, imm);
2538 } else {
2539 __ shrl(first_reg, imm);
2540 }
2541 }
2542 break;
2543 }
2544 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002545 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002546 DCHECK_EQ(ECX, second_reg);
2547 if (op->IsShl()) {
2548 GenerateShlLong(first, second_reg);
2549 } else if (op->IsShr()) {
2550 GenerateShrLong(first, second_reg);
2551 } else {
2552 GenerateUShrLong(first, second_reg);
2553 }
2554 break;
2555 }
2556 default:
2557 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2558 }
2559}
2560
2561void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2562 Label done;
2563 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2564 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2565 __ testl(shifter, Immediate(32));
2566 __ j(kEqual, &done);
2567 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2568 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2569 __ Bind(&done);
2570}
2571
2572void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2573 Label done;
2574 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2575 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2576 __ testl(shifter, Immediate(32));
2577 __ j(kEqual, &done);
2578 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2579 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2580 __ Bind(&done);
2581}
2582
2583void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2584 Label done;
2585 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2586 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2587 __ testl(shifter, Immediate(32));
2588 __ j(kEqual, &done);
2589 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2590 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2591 __ Bind(&done);
2592}
2593
2594void LocationsBuilderX86::VisitShl(HShl* shl) {
2595 HandleShift(shl);
2596}
2597
2598void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2599 HandleShift(shl);
2600}
2601
2602void LocationsBuilderX86::VisitShr(HShr* shr) {
2603 HandleShift(shr);
2604}
2605
2606void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2607 HandleShift(shr);
2608}
2609
2610void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2611 HandleShift(ushr);
2612}
2613
2614void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2615 HandleShift(ushr);
2616}
2617
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002618void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002619 LocationSummary* locations =
2620 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002621 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002622 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002623 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2624 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002625}
2626
2627void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2628 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002629 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002630 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002631
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002632 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002633
Nicolas Geoffray39468442014-09-02 15:17:15 +01002634 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002635 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002636}
2637
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002638void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2639 LocationSummary* locations =
2640 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2641 locations->SetOut(Location::RegisterLocation(EAX));
2642 InvokeRuntimeCallingConvention calling_convention;
2643 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002644 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2645 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002646}
2647
2648void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2649 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002650 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002651 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2652
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002653 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002654
2655 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2656 DCHECK(!codegen_->IsLeafMethod());
2657}
2658
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002659void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002660 LocationSummary* locations =
2661 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002662 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2663 if (location.IsStackSlot()) {
2664 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2665 } else if (location.IsDoubleStackSlot()) {
2666 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002667 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002668 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002669}
2670
2671void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002672 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002673}
2674
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002675void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002676 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002677 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002678 locations->SetInAt(0, Location::RequiresRegister());
2679 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002680}
2681
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002682void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2683 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002684 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002685 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002686 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002687 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002688 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002689 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002690 break;
2691
2692 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002693 __ notl(out.AsRegisterPairLow<Register>());
2694 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002695 break;
2696
2697 default:
2698 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2699 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002700}
2701
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002702void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002703 LocationSummary* locations =
2704 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002705 switch (compare->InputAt(0)->GetType()) {
2706 case Primitive::kPrimLong: {
2707 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00002708 locations->SetInAt(1, Location::Any());
2709 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2710 break;
2711 }
2712 case Primitive::kPrimFloat:
2713 case Primitive::kPrimDouble: {
2714 locations->SetInAt(0, Location::RequiresFpuRegister());
2715 locations->SetInAt(1, Location::RequiresFpuRegister());
2716 locations->SetOut(Location::RequiresRegister());
2717 break;
2718 }
2719 default:
2720 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2721 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002722}
2723
2724void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002725 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002726 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002727 Location left = locations->InAt(0);
2728 Location right = locations->InAt(1);
2729
2730 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002731 switch (compare->InputAt(0)->GetType()) {
2732 case Primitive::kPrimLong: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002733 if (right.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002734 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002735 } else if (right.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002736 __ cmpl(left.AsRegisterPairHigh<Register>(),
2737 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002738 } else {
2739 DCHECK(right.IsConstant()) << right;
2740 __ cmpl(left.AsRegisterPairHigh<Register>(),
2741 Immediate(High32Bits(right.GetConstant()->AsLongConstant()->GetValue())));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002742 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002743 __ j(kLess, &less); // Signed compare.
2744 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002745 if (right.IsRegisterPair()) {
2746 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002747 } else if (right.IsDoubleStackSlot()) {
Nicolas Geoffray154552e2015-03-06 16:10:14 +00002748 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002749 } else {
2750 DCHECK(right.IsConstant()) << right;
2751 __ cmpl(left.AsRegisterPairLow<Register>(),
2752 Immediate(Low32Bits(right.GetConstant()->AsLongConstant()->GetValue())));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002753 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002754 break;
2755 }
2756 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002757 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002758 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
2759 break;
2760 }
2761 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002762 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002763 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002764 break;
2765 }
2766 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002767 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002768 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002769 __ movl(out, Immediate(0));
2770 __ j(kEqual, &done);
2771 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
2772
2773 __ Bind(&greater);
2774 __ movl(out, Immediate(1));
2775 __ jmp(&done);
2776
2777 __ Bind(&less);
2778 __ movl(out, Immediate(-1));
2779
2780 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002781}
2782
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002783void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002784 LocationSummary* locations =
2785 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002786 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2787 locations->SetInAt(i, Location::Any());
2788 }
2789 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002790}
2791
2792void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002793 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002794 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002795}
2796
Calin Juravle52c48962014-12-16 17:02:57 +00002797void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
2798 /*
2799 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2800 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2801 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2802 */
2803 switch (kind) {
2804 case MemBarrierKind::kAnyAny: {
2805 __ mfence();
2806 break;
2807 }
2808 case MemBarrierKind::kAnyStore:
2809 case MemBarrierKind::kLoadAny:
2810 case MemBarrierKind::kStoreStore: {
2811 // nop
2812 break;
2813 }
2814 default:
2815 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002816 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002817}
2818
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002819
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002820void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002821 Label is_null;
2822 __ testl(value, value);
2823 __ j(kEqual, &is_null);
2824 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2825 __ movl(temp, object);
2826 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002827 __ movb(Address(temp, card, TIMES_1, 0),
2828 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002829 __ Bind(&is_null);
2830}
2831
Calin Juravle52c48962014-12-16 17:02:57 +00002832void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
2833 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002834 LocationSummary* locations =
2835 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002836 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002837
2838 // The output overlaps in case of long: we don't want the low move to overwrite
2839 // the object's location.
2840 locations->SetOut(Location::RequiresRegister(),
2841 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
2842 : Location::kNoOutputOverlap);
Calin Juravle52c48962014-12-16 17:02:57 +00002843
2844 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
2845 // Long values can be loaded atomically into an XMM using movsd.
2846 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
2847 // and then copy the XMM into the output 32bits at a time).
2848 locations->AddTemp(Location::RequiresFpuRegister());
2849 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002850}
2851
Calin Juravle52c48962014-12-16 17:02:57 +00002852void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
2853 const FieldInfo& field_info) {
2854 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002855
Calin Juravle52c48962014-12-16 17:02:57 +00002856 LocationSummary* locations = instruction->GetLocations();
2857 Register base = locations->InAt(0).AsRegister<Register>();
2858 Location out = locations->Out();
2859 bool is_volatile = field_info.IsVolatile();
2860 Primitive::Type field_type = field_info.GetFieldType();
2861 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2862
2863 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002864 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00002865 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002866 break;
2867 }
2868
2869 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002870 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002871 break;
2872 }
2873
2874 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00002875 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002876 break;
2877 }
2878
2879 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002880 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002881 break;
2882 }
2883
2884 case Primitive::kPrimInt:
2885 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002886 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002887 break;
2888 }
2889
2890 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00002891 if (is_volatile) {
2892 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2893 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002894 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002895 __ movd(out.AsRegisterPairLow<Register>(), temp);
2896 __ psrlq(temp, Immediate(32));
2897 __ movd(out.AsRegisterPairHigh<Register>(), temp);
2898 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002899 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00002900 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002901 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002902 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
2903 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002904 break;
2905 }
2906
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002907 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002908 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002909 break;
2910 }
2911
2912 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002913 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002914 break;
2915 }
2916
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002917 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00002918 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002919 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002920 }
Calin Juravle52c48962014-12-16 17:02:57 +00002921
Calin Juravle77520bc2015-01-12 18:45:46 +00002922 // Longs are handled in the switch.
2923 if (field_type != Primitive::kPrimLong) {
2924 codegen_->MaybeRecordImplicitNullCheck(instruction);
2925 }
2926
Calin Juravle52c48962014-12-16 17:02:57 +00002927 if (is_volatile) {
2928 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2929 }
2930}
2931
2932void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
2933 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2934
2935 LocationSummary* locations =
2936 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2937 locations->SetInAt(0, Location::RequiresRegister());
2938 bool is_volatile = field_info.IsVolatile();
2939 Primitive::Type field_type = field_info.GetFieldType();
2940 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2941 || (field_type == Primitive::kPrimByte);
2942
2943 // The register allocator does not support multiple
2944 // inputs that die at entry with one in a specific register.
2945 if (is_byte_type) {
2946 // Ensure the value is in a byte register.
2947 locations->SetInAt(1, Location::RegisterLocation(EAX));
2948 } else {
2949 locations->SetInAt(1, Location::RequiresRegister());
2950 }
2951 // Temporary registers for the write barrier.
2952 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2953 locations->AddTemp(Location::RequiresRegister());
2954 // Ensure the card is in a byte register.
2955 locations->AddTemp(Location::RegisterLocation(ECX));
2956 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
2957 // 64bits value can be atomically written to an address with movsd and an XMM register.
2958 // We need two XMM registers because there's no easier way to (bit) copy a register pair
2959 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
2960 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
2961 // isolated cases when we need this it isn't worth adding the extra complexity.
2962 locations->AddTemp(Location::RequiresFpuRegister());
2963 locations->AddTemp(Location::RequiresFpuRegister());
2964 }
2965}
2966
2967void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
2968 const FieldInfo& field_info) {
2969 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2970
2971 LocationSummary* locations = instruction->GetLocations();
2972 Register base = locations->InAt(0).AsRegister<Register>();
2973 Location value = locations->InAt(1);
2974 bool is_volatile = field_info.IsVolatile();
2975 Primitive::Type field_type = field_info.GetFieldType();
2976 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2977
2978 if (is_volatile) {
2979 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2980 }
2981
2982 switch (field_type) {
2983 case Primitive::kPrimBoolean:
2984 case Primitive::kPrimByte: {
2985 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
2986 break;
2987 }
2988
2989 case Primitive::kPrimShort:
2990 case Primitive::kPrimChar: {
2991 __ movw(Address(base, offset), value.AsRegister<Register>());
2992 break;
2993 }
2994
2995 case Primitive::kPrimInt:
2996 case Primitive::kPrimNot: {
2997 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00002998 break;
2999 }
3000
3001 case Primitive::kPrimLong: {
3002 if (is_volatile) {
3003 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3004 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
3005 __ movd(temp1, value.AsRegisterPairLow<Register>());
3006 __ movd(temp2, value.AsRegisterPairHigh<Register>());
3007 __ punpckldq(temp1, temp2);
3008 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00003009 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003010 } else {
3011 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003012 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003013 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3014 }
3015 break;
3016 }
3017
3018 case Primitive::kPrimFloat: {
3019 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3020 break;
3021 }
3022
3023 case Primitive::kPrimDouble: {
3024 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
3025 break;
3026 }
3027
3028 case Primitive::kPrimVoid:
3029 LOG(FATAL) << "Unreachable type " << field_type;
3030 UNREACHABLE();
3031 }
3032
Calin Juravle77520bc2015-01-12 18:45:46 +00003033 // Longs are handled in the switch.
3034 if (field_type != Primitive::kPrimLong) {
3035 codegen_->MaybeRecordImplicitNullCheck(instruction);
3036 }
3037
3038 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
3039 Register temp = locations->GetTemp(0).AsRegister<Register>();
3040 Register card = locations->GetTemp(1).AsRegister<Register>();
3041 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
3042 }
3043
Calin Juravle52c48962014-12-16 17:02:57 +00003044 if (is_volatile) {
3045 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
3046 }
3047}
3048
3049void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3050 HandleFieldGet(instruction, instruction->GetFieldInfo());
3051}
3052
3053void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3054 HandleFieldGet(instruction, instruction->GetFieldInfo());
3055}
3056
3057void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3058 HandleFieldSet(instruction, instruction->GetFieldInfo());
3059}
3060
3061void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3062 HandleFieldSet(instruction, instruction->GetFieldInfo());
3063}
3064
3065void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3066 HandleFieldSet(instruction, instruction->GetFieldInfo());
3067}
3068
3069void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
3070 HandleFieldSet(instruction, instruction->GetFieldInfo());
3071}
3072
3073void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3074 HandleFieldGet(instruction, instruction->GetFieldInfo());
3075}
3076
3077void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
3078 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003079}
3080
3081void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003082 LocationSummary* locations =
3083 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003084 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3085 ? Location::RequiresRegister()
3086 : Location::Any();
3087 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003088 if (instruction->HasUses()) {
3089 locations->SetOut(Location::SameAsFirstInput());
3090 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003091}
3092
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003093void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003094 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3095 return;
3096 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003097 LocationSummary* locations = instruction->GetLocations();
3098 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003099
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003100 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3101 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3102}
3103
3104void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003105 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003106 codegen_->AddSlowPath(slow_path);
3107
3108 LocationSummary* locations = instruction->GetLocations();
3109 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003110
3111 if (obj.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003112 __ cmpl(obj.AsRegister<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003113 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003114 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003115 } else {
3116 DCHECK(obj.IsConstant()) << obj;
3117 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3118 __ jmp(slow_path->GetEntryLabel());
3119 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003120 }
3121 __ j(kEqual, slow_path->GetEntryLabel());
3122}
3123
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003124void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3125 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3126 GenerateImplicitNullCheck(instruction);
3127 } else {
3128 GenerateExplicitNullCheck(instruction);
3129 }
3130}
3131
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003132void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003133 LocationSummary* locations =
3134 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003135 locations->SetInAt(0, Location::RequiresRegister());
3136 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003137 // The output overlaps in case of long: we don't want the low move to overwrite
3138 // the array's location.
3139 locations->SetOut(Location::RequiresRegister(),
3140 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3141 : Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003142}
3143
3144void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3145 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003146 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003147 Location index = locations->InAt(1);
3148
Calin Juravle77520bc2015-01-12 18:45:46 +00003149 Primitive::Type type = instruction->GetType();
3150 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003151 case Primitive::kPrimBoolean: {
3152 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003153 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003154 if (index.IsConstant()) {
3155 __ movzxb(out, Address(obj,
3156 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3157 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003158 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003159 }
3160 break;
3161 }
3162
3163 case Primitive::kPrimByte: {
3164 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003165 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003166 if (index.IsConstant()) {
3167 __ movsxb(out, Address(obj,
3168 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3169 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003170 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003171 }
3172 break;
3173 }
3174
3175 case Primitive::kPrimShort: {
3176 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003177 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003178 if (index.IsConstant()) {
3179 __ movsxw(out, Address(obj,
3180 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3181 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003182 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003183 }
3184 break;
3185 }
3186
3187 case Primitive::kPrimChar: {
3188 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003189 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003190 if (index.IsConstant()) {
3191 __ movzxw(out, Address(obj,
3192 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3193 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003194 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003195 }
3196 break;
3197 }
3198
3199 case Primitive::kPrimInt:
3200 case Primitive::kPrimNot: {
3201 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003202 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003203 if (index.IsConstant()) {
3204 __ movl(out, Address(obj,
3205 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3206 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003207 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003208 }
3209 break;
3210 }
3211
3212 case Primitive::kPrimLong: {
3213 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003214 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003215 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003216 if (index.IsConstant()) {
3217 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003218 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003219 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003220 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003221 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003222 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003223 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003224 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003225 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003226 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003227 }
3228 break;
3229 }
3230
Mark Mendell7c8d0092015-01-26 11:21:33 -05003231 case Primitive::kPrimFloat: {
3232 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3233 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3234 if (index.IsConstant()) {
3235 __ movss(out, Address(obj,
3236 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3237 } else {
3238 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3239 }
3240 break;
3241 }
3242
3243 case Primitive::kPrimDouble: {
3244 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3245 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3246 if (index.IsConstant()) {
3247 __ movsd(out, Address(obj,
3248 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3249 } else {
3250 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3251 }
3252 break;
3253 }
3254
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003255 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003256 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003257 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003258 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003259
3260 if (type != Primitive::kPrimLong) {
3261 codegen_->MaybeRecordImplicitNullCheck(instruction);
3262 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003263}
3264
3265void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05003266 // This location builder might end up asking to up to four registers, which is
3267 // not currently possible for baseline. The situation in which we need four
3268 // registers cannot be met by baseline though, because it has not run any
3269 // optimization.
3270
Nicolas Geoffray39468442014-09-02 15:17:15 +01003271 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003272 bool needs_write_barrier =
3273 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3274
Mark Mendell5f874182015-03-04 15:42:45 -05003275 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003276
Nicolas Geoffray39468442014-09-02 15:17:15 +01003277 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3278 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003279 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003280
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003281 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003282 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003283 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3284 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3285 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003286 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003287 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3288 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003289 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003290 // In case of a byte operation, the register allocator does not support multiple
3291 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003292 locations->SetInAt(0, Location::RequiresRegister());
3293 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003294 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003295 // Ensure the value is in a byte register.
3296 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003297 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003298 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003299 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003300 // Temporary registers for the write barrier.
3301 if (needs_write_barrier) {
3302 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003303 // Ensure the card is in a byte register.
3304 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003305 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003306 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003307}
3308
3309void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3310 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003311 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003312 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003313 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003314 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003315 bool needs_runtime_call = locations->WillCall();
3316 bool needs_write_barrier =
3317 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003318
3319 switch (value_type) {
3320 case Primitive::kPrimBoolean:
3321 case Primitive::kPrimByte: {
3322 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003323 if (index.IsConstant()) {
3324 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003325 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003326 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003327 } else {
3328 __ movb(Address(obj, offset),
3329 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3330 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003331 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003332 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003333 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003334 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003335 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003336 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003337 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3338 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003339 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003340 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003341 break;
3342 }
3343
3344 case Primitive::kPrimShort:
3345 case Primitive::kPrimChar: {
3346 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003347 if (index.IsConstant()) {
3348 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003349 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003350 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003351 } else {
3352 __ movw(Address(obj, offset),
3353 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3354 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003355 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003356 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003357 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3358 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003359 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003360 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003361 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3362 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003363 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003364 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003365 break;
3366 }
3367
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003368 case Primitive::kPrimInt:
3369 case Primitive::kPrimNot: {
3370 if (!needs_runtime_call) {
3371 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3372 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003373 size_t offset =
3374 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003375 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003376 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003377 } else {
3378 DCHECK(value.IsConstant()) << value;
3379 __ movl(Address(obj, offset),
3380 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3381 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003382 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003383 DCHECK(index.IsRegister()) << index;
3384 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003385 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3386 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003387 } else {
3388 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003389 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003390 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3391 }
3392 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003393 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003394
3395 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003396 Register temp = locations->GetTemp(0).AsRegister<Register>();
3397 Register card = locations->GetTemp(1).AsRegister<Register>();
3398 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003399 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003400 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003401 DCHECK_EQ(value_type, Primitive::kPrimNot);
3402 DCHECK(!codegen_->IsLeafMethod());
3403 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3404 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003405 }
3406 break;
3407 }
3408
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003409 case Primitive::kPrimLong: {
3410 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003411 if (index.IsConstant()) {
3412 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003413 if (value.IsRegisterPair()) {
3414 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003415 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003416 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003417 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003418 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003419 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3420 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003421 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003422 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3423 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003424 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003425 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003426 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003427 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003428 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003429 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003430 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003431 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003432 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003433 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003434 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003435 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003436 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003437 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003438 Immediate(High32Bits(val)));
3439 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003440 }
3441 break;
3442 }
3443
Mark Mendell7c8d0092015-01-26 11:21:33 -05003444 case Primitive::kPrimFloat: {
3445 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3446 DCHECK(value.IsFpuRegister());
3447 if (index.IsConstant()) {
3448 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3449 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3450 } else {
3451 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3452 value.AsFpuRegister<XmmRegister>());
3453 }
3454 break;
3455 }
3456
3457 case Primitive::kPrimDouble: {
3458 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3459 DCHECK(value.IsFpuRegister());
3460 if (index.IsConstant()) {
3461 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3462 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3463 } else {
3464 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3465 value.AsFpuRegister<XmmRegister>());
3466 }
3467 break;
3468 }
3469
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003470 case Primitive::kPrimVoid:
3471 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003472 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003473 }
3474}
3475
3476void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3477 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003478 locations->SetInAt(0, Location::RequiresRegister());
3479 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003480 instruction->SetLocations(locations);
3481}
3482
3483void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3484 LocationSummary* locations = instruction->GetLocations();
3485 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003486 Register obj = locations->InAt(0).AsRegister<Register>();
3487 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003488 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003489 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003490}
3491
3492void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003493 LocationSummary* locations =
3494 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003495 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003496 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003497 if (instruction->HasUses()) {
3498 locations->SetOut(Location::SameAsFirstInput());
3499 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003500}
3501
3502void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3503 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003504 Location index_loc = locations->InAt(0);
3505 Location length_loc = locations->InAt(1);
3506 SlowPathCodeX86* slow_path =
3507 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003508 codegen_->AddSlowPath(slow_path);
3509
Mark Mendellf60c90b2015-03-04 15:12:59 -05003510 Register length = length_loc.AsRegister<Register>();
3511 if (index_loc.IsConstant()) {
3512 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3513 __ cmpl(length, Immediate(value));
3514 } else {
3515 __ cmpl(length, index_loc.AsRegister<Register>());
3516 }
3517 __ j(kBelowEqual, slow_path->GetEntryLabel());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003518}
3519
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003520void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3521 temp->SetLocations(nullptr);
3522}
3523
3524void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3525 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003526 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003527}
3528
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003529void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003530 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003531 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003532}
3533
3534void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003535 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3536}
3537
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003538void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3539 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3540}
3541
3542void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003543 HBasicBlock* block = instruction->GetBlock();
3544 if (block->GetLoopInformation() != nullptr) {
3545 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3546 // The back edge will generate the suspend check.
3547 return;
3548 }
3549 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3550 // The goto will generate the suspend check.
3551 return;
3552 }
3553 GenerateSuspendCheck(instruction, nullptr);
3554}
3555
3556void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3557 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003558 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003559 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003560 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003561 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003562 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003563 if (successor == nullptr) {
3564 __ j(kNotEqual, slow_path->GetEntryLabel());
3565 __ Bind(slow_path->GetReturnLabel());
3566 } else {
3567 __ j(kEqual, codegen_->GetLabelOf(successor));
3568 __ jmp(slow_path->GetEntryLabel());
3569 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003570}
3571
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003572X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3573 return codegen_->GetAssembler();
3574}
3575
Mark Mendell7c8d0092015-01-26 11:21:33 -05003576void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003577 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003578 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003579 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003580 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05003581 __ movl(temp_reg, Address(ESP, src + stack_offset));
3582 __ movl(Address(ESP, dst + stack_offset), temp_reg);
3583}
3584
3585void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
3586 ScratchRegisterScope ensure_scratch(
3587 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3588 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3589 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3590 __ movl(temp_reg, Address(ESP, src + stack_offset));
3591 __ movl(Address(ESP, dst + stack_offset), temp_reg);
3592 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
3593 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003594}
3595
3596void ParallelMoveResolverX86::EmitMove(size_t index) {
3597 MoveOperands* move = moves_.Get(index);
3598 Location source = move->GetSource();
3599 Location destination = move->GetDestination();
3600
3601 if (source.IsRegister()) {
3602 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003603 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003604 } else {
3605 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003606 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003607 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003608 } else if (source.IsFpuRegister()) {
3609 if (destination.IsFpuRegister()) {
3610 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3611 } else if (destination.IsStackSlot()) {
3612 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3613 } else {
3614 DCHECK(destination.IsDoubleStackSlot());
3615 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3616 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003617 } else if (source.IsStackSlot()) {
3618 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003619 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003620 } else if (destination.IsFpuRegister()) {
3621 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003622 } else {
3623 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003624 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
3625 }
3626 } else if (source.IsDoubleStackSlot()) {
3627 if (destination.IsFpuRegister()) {
3628 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
3629 } else {
3630 DCHECK(destination.IsDoubleStackSlot()) << destination;
3631 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003632 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003633 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003634 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003635 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003636 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003637 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003638 if (value == 0) {
3639 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
3640 } else {
3641 __ movl(destination.AsRegister<Register>(), Immediate(value));
3642 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003643 } else {
3644 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05003645 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003646 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003647 } else if (constant->IsFloatConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003648 float value = constant->AsFloatConstant()->GetValue();
3649 Immediate imm(bit_cast<float, int32_t>(value));
3650 if (destination.IsFpuRegister()) {
3651 ScratchRegisterScope ensure_scratch(
3652 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3653 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
3654 __ movl(temp, imm);
3655 __ movd(destination.AsFpuRegister<XmmRegister>(), temp);
3656 } else {
3657 DCHECK(destination.IsStackSlot()) << destination;
3658 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3659 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003660 } else if (constant->IsLongConstant()) {
3661 int64_t value = constant->AsLongConstant()->GetValue();
3662 int32_t low_value = Low32Bits(value);
3663 int32_t high_value = High32Bits(value);
3664 Immediate low(low_value);
3665 Immediate high(high_value);
3666 if (destination.IsDoubleStackSlot()) {
3667 __ movl(Address(ESP, destination.GetStackIndex()), low);
3668 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
3669 } else {
3670 __ movl(destination.AsRegisterPairLow<Register>(), low);
3671 __ movl(destination.AsRegisterPairHigh<Register>(), high);
3672 }
3673 } else {
3674 DCHECK(constant->IsDoubleConstant());
3675 double dbl_value = constant->AsDoubleConstant()->GetValue();
3676 int64_t value = bit_cast<double, int64_t>(dbl_value);
3677 int32_t low_value = Low32Bits(value);
3678 int32_t high_value = High32Bits(value);
3679 Immediate low(low_value);
3680 Immediate high(high_value);
3681 if (destination.IsFpuRegister()) {
3682 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
3683 if (value == 0) {
3684 // Easy handling of 0.0.
3685 __ xorpd(dest, dest);
3686 } else {
3687 __ pushl(high);
3688 __ pushl(low);
3689 __ movsd(dest, Address(ESP, 0));
3690 __ addl(ESP, Immediate(8));
3691 }
3692 } else {
3693 DCHECK(destination.IsDoubleStackSlot()) << destination;
3694 __ movl(Address(ESP, destination.GetStackIndex()), low);
3695 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
3696 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003697 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003698 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003699 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003700 }
3701}
3702
3703void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003704 Register suggested_scratch = reg == EAX ? EBX : EAX;
3705 ScratchRegisterScope ensure_scratch(
3706 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3707
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003708 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3709 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
3710 __ movl(Address(ESP, mem + stack_offset), reg);
3711 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
3712}
3713
Mark Mendell7c8d0092015-01-26 11:21:33 -05003714void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
3715 ScratchRegisterScope ensure_scratch(
3716 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3717
3718 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3719 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3720 __ movl(temp_reg, Address(ESP, mem + stack_offset));
3721 __ movss(Address(ESP, mem + stack_offset), reg);
3722 __ movd(reg, temp_reg);
3723}
3724
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003725void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
3726 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003727 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3728
3729 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003730 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003731 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3732
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003733 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
3734 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
3735 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
3736 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
3737 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
3738 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
3739}
3740
3741void ParallelMoveResolverX86::EmitSwap(size_t index) {
3742 MoveOperands* move = moves_.Get(index);
3743 Location source = move->GetSource();
3744 Location destination = move->GetDestination();
3745
3746 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003747 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003748 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003749 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003750 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003751 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003752 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3753 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003754 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
3755 // Use XOR Swap algorithm to avoid a temporary.
3756 DCHECK_NE(source.reg(), destination.reg());
3757 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3758 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3759 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3760 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
3761 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
3762 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
3763 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003764 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
3765 // Take advantage of the 16 bytes in the XMM register.
3766 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
3767 Address stack(ESP, destination.GetStackIndex());
3768 // Load the double into the high doubleword.
3769 __ movhpd(reg, stack);
3770
3771 // Store the low double into the destination.
3772 __ movsd(stack, reg);
3773
3774 // Move the high double to the low double.
3775 __ psrldq(reg, Immediate(8));
3776 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
3777 // Take advantage of the 16 bytes in the XMM register.
3778 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
3779 Address stack(ESP, source.GetStackIndex());
3780 // Load the double into the high doubleword.
3781 __ movhpd(reg, stack);
3782
3783 // Store the low double into the destination.
3784 __ movsd(stack, reg);
3785
3786 // Move the high double to the low double.
3787 __ psrldq(reg, Immediate(8));
3788 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
3789 Exchange(destination.GetStackIndex(), source.GetStackIndex());
3790 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003791 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003792 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003793 }
3794}
3795
3796void ParallelMoveResolverX86::SpillScratch(int reg) {
3797 __ pushl(static_cast<Register>(reg));
3798}
3799
3800void ParallelMoveResolverX86::RestoreScratch(int reg) {
3801 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003802}
3803
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003804void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003805 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3806 ? LocationSummary::kCallOnSlowPath
3807 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003808 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003809 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003810 locations->SetOut(Location::RequiresRegister());
3811}
3812
3813void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003814 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003815 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003816 DCHECK(!cls->CanCallRuntime());
3817 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003818 codegen_->LoadCurrentMethod(out);
3819 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3820 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003821 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003822 codegen_->LoadCurrentMethod(out);
3823 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3824 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003825
3826 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3827 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3828 codegen_->AddSlowPath(slow_path);
3829 __ testl(out, out);
3830 __ j(kEqual, slow_path->GetEntryLabel());
3831 if (cls->MustGenerateClinitCheck()) {
3832 GenerateClassInitializationCheck(slow_path, out);
3833 } else {
3834 __ Bind(slow_path->GetExitLabel());
3835 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003836 }
3837}
3838
3839void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
3840 LocationSummary* locations =
3841 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3842 locations->SetInAt(0, Location::RequiresRegister());
3843 if (check->HasUses()) {
3844 locations->SetOut(Location::SameAsFirstInput());
3845 }
3846}
3847
3848void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003849 // We assume the class to not be null.
3850 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3851 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003852 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003853 GenerateClassInitializationCheck(slow_path,
3854 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003855}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003856
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003857void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
3858 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003859 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3860 Immediate(mirror::Class::kStatusInitialized));
3861 __ j(kLess, slow_path->GetEntryLabel());
3862 __ Bind(slow_path->GetExitLabel());
3863 // No need for memory fence, thanks to the X86 memory model.
3864}
3865
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003866void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
3867 LocationSummary* locations =
3868 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3869 locations->SetOut(Location::RequiresRegister());
3870}
3871
3872void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
3873 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
3874 codegen_->AddSlowPath(slow_path);
3875
Roland Levillain271ab9c2014-11-27 15:23:57 +00003876 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003877 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08003878 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3879 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003880 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3881 __ testl(out, out);
3882 __ j(kEqual, slow_path->GetEntryLabel());
3883 __ Bind(slow_path->GetExitLabel());
3884}
3885
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003886void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
3887 LocationSummary* locations =
3888 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3889 locations->SetOut(Location::RequiresRegister());
3890}
3891
3892void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
3893 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003894 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003895 __ fs()->movl(address, Immediate(0));
3896}
3897
3898void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
3899 LocationSummary* locations =
3900 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3901 InvokeRuntimeCallingConvention calling_convention;
3902 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3903}
3904
3905void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
3906 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
3907 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3908}
3909
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003910void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003911 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3912 ? LocationSummary::kNoCall
3913 : LocationSummary::kCallOnSlowPath;
3914 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3915 locations->SetInAt(0, Location::RequiresRegister());
3916 locations->SetInAt(1, Location::Any());
3917 locations->SetOut(Location::RequiresRegister());
3918}
3919
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003920void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003921 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003922 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003923 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003924 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003925 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3926 Label done, zero;
3927 SlowPathCodeX86* slow_path = nullptr;
3928
3929 // Return 0 if `obj` is null.
3930 // TODO: avoid this check if we know obj is not null.
3931 __ testl(obj, obj);
3932 __ j(kEqual, &zero);
3933 __ movl(out, Address(obj, class_offset));
3934 // Compare the class of `obj` with `cls`.
3935 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003936 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003937 } else {
3938 DCHECK(cls.IsStackSlot()) << cls;
3939 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
3940 }
3941
3942 if (instruction->IsClassFinal()) {
3943 // Classes must be equal for the instanceof to succeed.
3944 __ j(kNotEqual, &zero);
3945 __ movl(out, Immediate(1));
3946 __ jmp(&done);
3947 } else {
3948 // If the classes are not equal, we go into a slow path.
3949 DCHECK(locations->OnlyCallsOnSlowPath());
3950 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003951 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003952 codegen_->AddSlowPath(slow_path);
3953 __ j(kNotEqual, slow_path->GetEntryLabel());
3954 __ movl(out, Immediate(1));
3955 __ jmp(&done);
3956 }
3957 __ Bind(&zero);
3958 __ movl(out, Immediate(0));
3959 if (slow_path != nullptr) {
3960 __ Bind(slow_path->GetExitLabel());
3961 }
3962 __ Bind(&done);
3963}
3964
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003965void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
3966 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3967 instruction, LocationSummary::kCallOnSlowPath);
3968 locations->SetInAt(0, Location::RequiresRegister());
3969 locations->SetInAt(1, Location::Any());
3970 locations->AddTemp(Location::RequiresRegister());
3971}
3972
3973void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
3974 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003975 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003976 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003977 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003978 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3979 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
3980 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3981 codegen_->AddSlowPath(slow_path);
3982
3983 // TODO: avoid this check if we know obj is not null.
3984 __ testl(obj, obj);
3985 __ j(kEqual, slow_path->GetExitLabel());
3986 __ movl(temp, Address(obj, class_offset));
3987
3988 // Compare the class of `obj` with `cls`.
3989 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003990 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003991 } else {
3992 DCHECK(cls.IsStackSlot()) << cls;
3993 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
3994 }
3995
3996 __ j(kNotEqual, slow_path->GetEntryLabel());
3997 __ Bind(slow_path->GetExitLabel());
3998}
3999
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00004000void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4001 LocationSummary* locations =
4002 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4003 InvokeRuntimeCallingConvention calling_convention;
4004 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4005}
4006
4007void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
4008 __ fs()->call(Address::Absolute(instruction->IsEnter()
4009 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
4010 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
4011 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4012}
4013
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004014void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
4015void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
4016void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
4017
4018void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4019 LocationSummary* locations =
4020 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4021 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
4022 || instruction->GetResultType() == Primitive::kPrimLong);
4023 locations->SetInAt(0, Location::RequiresRegister());
4024 locations->SetInAt(1, Location::Any());
4025 locations->SetOut(Location::SameAsFirstInput());
4026}
4027
4028void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
4029 HandleBitwiseOperation(instruction);
4030}
4031
4032void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
4033 HandleBitwiseOperation(instruction);
4034}
4035
4036void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
4037 HandleBitwiseOperation(instruction);
4038}
4039
4040void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
4041 LocationSummary* locations = instruction->GetLocations();
4042 Location first = locations->InAt(0);
4043 Location second = locations->InAt(1);
4044 DCHECK(first.Equals(locations->Out()));
4045
4046 if (instruction->GetResultType() == Primitive::kPrimInt) {
4047 if (second.IsRegister()) {
4048 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004049 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004050 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004051 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004052 } else {
4053 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004054 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004055 }
4056 } else if (second.IsConstant()) {
4057 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004058 __ andl(first.AsRegister<Register>(),
4059 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004060 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004061 __ orl(first.AsRegister<Register>(),
4062 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004063 } else {
4064 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00004065 __ xorl(first.AsRegister<Register>(),
4066 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004067 }
4068 } else {
4069 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004070 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004071 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004072 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004073 } else {
4074 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004075 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004076 }
4077 }
4078 } else {
4079 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
4080 if (second.IsRegisterPair()) {
4081 if (instruction->IsAnd()) {
4082 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4083 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4084 } else if (instruction->IsOr()) {
4085 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4086 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4087 } else {
4088 DCHECK(instruction->IsXor());
4089 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
4090 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
4091 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004092 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004093 if (instruction->IsAnd()) {
4094 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4095 __ andl(first.AsRegisterPairHigh<Register>(),
4096 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4097 } else if (instruction->IsOr()) {
4098 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4099 __ orl(first.AsRegisterPairHigh<Register>(),
4100 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4101 } else {
4102 DCHECK(instruction->IsXor());
4103 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
4104 __ xorl(first.AsRegisterPairHigh<Register>(),
4105 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
4106 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004107 } else {
4108 DCHECK(second.IsConstant()) << second;
4109 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
4110 Immediate low(Low32Bits(value));
4111 Immediate high(High32Bits(value));
4112 if (instruction->IsAnd()) {
4113 __ andl(first.AsRegisterPairLow<Register>(), low);
4114 __ andl(first.AsRegisterPairHigh<Register>(), high);
4115 } else if (instruction->IsOr()) {
4116 __ orl(first.AsRegisterPairLow<Register>(), low);
4117 __ orl(first.AsRegisterPairHigh<Register>(), high);
4118 } else {
4119 DCHECK(instruction->IsXor());
4120 __ xorl(first.AsRegisterPairLow<Register>(), low);
4121 __ xorl(first.AsRegisterPairHigh<Register>(), high);
4122 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004123 }
4124 }
4125}
4126
Calin Juravleb1498f62015-02-16 13:13:29 +00004127void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
4128 // Nothing to do, this should be removed during prepare for register allocator.
4129 UNUSED(instruction);
4130 LOG(FATAL) << "Unreachable";
4131}
4132
4133void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
4134 // Nothing to do, this should be removed during prepare for register allocator.
4135 UNUSED(instruction);
4136 LOG(FATAL) << "Unreachable";
4137}
4138
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00004139} // namespace x86
4140} // namespace art