blob: c840793743d28416606433867986c3cb3aff8c5c [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 P Mendell966c3ae2015-01-27 15:45:27 +000040static constexpr XmmRegister kRuntimeParameterFpuRegisters[] = { XMM0, XMM1, XMM2, XMM3 };
41static constexpr size_t kRuntimeParameterFpuRegistersLength =
42 arraysize(kRuntimeParameterFpuRegisters);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010043
Mark Mendell24f2dfa2015-01-14 19:51:45 -050044static constexpr int kC2ConditionMask = 0x400;
45
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000046// Marker for places that can be updated once we don't follow the quick ABI.
47static constexpr bool kFollowsQuickABI = true;
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000048static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000049
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010050class InvokeRuntimeCallingConvention : public CallingConvention<Register, XmmRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010051 public:
52 InvokeRuntimeCallingConvention()
53 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010054 kRuntimeParameterCoreRegistersLength,
55 kRuntimeParameterFpuRegisters,
56 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010057
58 private:
59 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
60};
61
Nicolas Geoffraye5038322014-07-04 09:41:32 +010062#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
63
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010064class SlowPathCodeX86 : public SlowPathCode {
65 public:
66 SlowPathCodeX86() : entry_label_(), exit_label_() {}
67
68 Label* GetEntryLabel() { return &entry_label_; }
69 Label* GetExitLabel() { return &exit_label_; }
70
71 private:
72 Label entry_label_;
73 Label exit_label_;
74
75 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86);
76};
77
78class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010080 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010081
82 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
83 __ Bind(GetEntryLabel());
84 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010085 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010086 }
87
88 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010089 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010090 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
91};
92
Calin Juravled0d48522014-11-04 16:40:20 +000093class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
94 public:
95 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
96
97 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
98 __ Bind(GetEntryLabel());
99 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
100 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
101 }
102
103 private:
104 HDivZeroCheck* const instruction_;
105 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
106};
107
Calin Juravlebacfec32014-11-14 15:54:36 +0000108class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +0000109 public:
Calin Juravlebacfec32014-11-14 15:54:36 +0000110 explicit DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000111
112 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
113 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000114 if (is_div_) {
115 __ negl(reg_);
116 } else {
117 __ movl(reg_, Immediate(0));
118 }
Calin Juravled0d48522014-11-04 16:40:20 +0000119 __ jmp(GetExitLabel());
120 }
121
122 private:
123 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000124 bool is_div_;
125 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000126};
127
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100128class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100129 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100130 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
131 Location index_location,
132 Location length_location)
Roland Levillain199f3362014-11-27 17:15:16 +0000133 : instruction_(instruction),
134 index_location_(index_location),
135 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100136
137 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100138 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100139 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000140 // We're moving two locations to locations that could overlap, so we need a parallel
141 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100142 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000143 x86_codegen->EmitParallelMoves(
144 index_location_,
145 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
146 length_location_,
147 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100148 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100149 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100150 }
151
152 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100153 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100154 const Location index_location_;
155 const Location length_location_;
156
157 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
158};
159
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100160class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000161 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100162 explicit SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
163 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000164
165 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100166 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000167 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100168 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000169 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
170 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100171 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100172 if (successor_ == nullptr) {
173 __ jmp(GetReturnLabel());
174 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100175 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100176 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000177 }
178
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100179 Label* GetReturnLabel() {
180 DCHECK(successor_ == nullptr);
181 return &return_label_;
182 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000183
184 private:
185 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100186 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000187 Label return_label_;
188
189 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
190};
191
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000192class LoadStringSlowPathX86 : public SlowPathCodeX86 {
193 public:
194 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
195
196 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
197 LocationSummary* locations = instruction_->GetLocations();
198 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
199
200 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
201 __ Bind(GetEntryLabel());
202 codegen->SaveLiveRegisters(locations);
203
204 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800205 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
206 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000207 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
208 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
209 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
210 codegen->RestoreLiveRegisters(locations);
211
212 __ jmp(GetExitLabel());
213 }
214
215 private:
216 HLoadString* const instruction_;
217
218 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
219};
220
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000221class LoadClassSlowPathX86 : public SlowPathCodeX86 {
222 public:
223 LoadClassSlowPathX86(HLoadClass* cls,
224 HInstruction* at,
225 uint32_t dex_pc,
226 bool do_clinit)
227 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
228 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
229 }
230
231 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
232 LocationSummary* locations = at_->GetLocations();
233 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
234 __ Bind(GetEntryLabel());
235 codegen->SaveLiveRegisters(locations);
236
237 InvokeRuntimeCallingConvention calling_convention;
238 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
239 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
240 __ fs()->call(Address::Absolute(do_clinit_
241 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
242 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
243 codegen->RecordPcInfo(at_, dex_pc_);
244
245 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000246 Location out = locations->Out();
247 if (out.IsValid()) {
248 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
249 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000250 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000251
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000252 codegen->RestoreLiveRegisters(locations);
253 __ jmp(GetExitLabel());
254 }
255
256 private:
257 // The class this slow path will load.
258 HLoadClass* const cls_;
259
260 // The instruction where this slow path is happening.
261 // (Might be the load class or an initialization check).
262 HInstruction* const at_;
263
264 // The dex PC of `at_`.
265 const uint32_t dex_pc_;
266
267 // Whether to initialize the class.
268 const bool do_clinit_;
269
270 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
271};
272
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000273class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
274 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000275 TypeCheckSlowPathX86(HInstruction* instruction,
276 Location class_to_check,
277 Location object_class,
278 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000279 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000280 class_to_check_(class_to_check),
281 object_class_(object_class),
282 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000283
284 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
285 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000286 DCHECK(instruction_->IsCheckCast()
287 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000288
289 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
290 __ Bind(GetEntryLabel());
291 codegen->SaveLiveRegisters(locations);
292
293 // We're moving two locations to locations that could overlap, so we need a parallel
294 // move resolver.
295 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000296 x86_codegen->EmitParallelMoves(
297 class_to_check_,
298 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
299 object_class_,
300 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000301
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000302 if (instruction_->IsInstanceOf()) {
Roland Levillain199f3362014-11-27 17:15:16 +0000303 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize,
304 pInstanceofNonTrivial)));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000305 } else {
306 DCHECK(instruction_->IsCheckCast());
307 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
308 }
309
310 codegen->RecordPcInfo(instruction_, dex_pc_);
311 if (instruction_->IsInstanceOf()) {
312 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
313 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000314 codegen->RestoreLiveRegisters(locations);
315
316 __ jmp(GetExitLabel());
317 }
318
319 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000320 HInstruction* const instruction_;
321 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000322 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000323 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324
325 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
326};
327
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100328#undef __
329#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
330
Dave Allison20dfc792014-06-16 20:44:29 -0700331inline Condition X86Condition(IfCondition cond) {
332 switch (cond) {
333 case kCondEQ: return kEqual;
334 case kCondNE: return kNotEqual;
335 case kCondLT: return kLess;
336 case kCondLE: return kLessEqual;
337 case kCondGT: return kGreater;
338 case kCondGE: return kGreaterEqual;
339 default:
340 LOG(FATAL) << "Unknown if condition";
341 }
342 return kEqual;
343}
344
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100345void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
346 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
347}
348
349void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
350 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
351}
352
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100353size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
354 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
355 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100356}
357
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100358size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
359 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
360 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100361}
362
Mark Mendell7c8d0092015-01-26 11:21:33 -0500363size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
364 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
365 return GetFloatingPointSpillSlotSize();
366}
367
368size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
369 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
370 return GetFloatingPointSpillSlotSize();
371}
372
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000373CodeGeneratorX86::CodeGeneratorX86(HGraph* graph, const CompilerOptions& compiler_options)
374 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfXmmRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000375 kNumberOfRegisterPairs, (1 << kFakeReturnRegister), 0, compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100376 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100377 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100378 instruction_visitor_(graph, this),
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000379 move_resolver_(graph->GetArena(), this) {
380 // Use a fake return address register to mimic Quick.
381 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100382}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100383
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100384Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100385 switch (type) {
386 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100387 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100388 X86ManagedRegister pair =
389 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100390 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
391 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100392 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
393 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100394 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100395 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100396 }
397
398 case Primitive::kPrimByte:
399 case Primitive::kPrimBoolean:
400 case Primitive::kPrimChar:
401 case Primitive::kPrimShort:
402 case Primitive::kPrimInt:
403 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100404 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100405 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100406 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100407 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
408 X86ManagedRegister current =
409 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
410 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100411 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100412 }
413 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100414 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100415 }
416
417 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100418 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100419 return Location::FpuRegisterLocation(
420 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100421 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100422
423 case Primitive::kPrimVoid:
424 LOG(FATAL) << "Unreachable type " << type;
425 }
426
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100427 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100428}
429
Nicolas Geoffray98893962015-01-21 12:32:32 +0000430void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline ATTRIBUTE_UNUSED) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100431 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100432 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100433
434 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100435 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100436
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000437 // TODO: We currently don't use Quick's callee saved registers.
438 DCHECK(kFollowsQuickABI);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100439 blocked_core_registers_[EBP] = true;
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000440 blocked_core_registers_[ESI] = true;
441 blocked_core_registers_[EDI] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100442
443 UpdateBlockedPairRegisters();
444}
445
446void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
447 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
448 X86ManagedRegister current =
449 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
450 if (blocked_core_registers_[current.AsRegisterPairLow()]
451 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
452 blocked_register_pairs_[i] = true;
453 }
454 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100455}
456
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100457InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
458 : HGraphVisitor(graph),
459 assembler_(codegen->GetAssembler()),
460 codegen_(codegen) {}
461
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000462void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000463 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000464 bool skip_overflow_check =
465 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000466 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000467
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000468 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100469 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100470 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100471 }
472
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000473 __ subl(ESP, Immediate(GetFrameSize() - FrameEntrySpillSize()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100474 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000475}
476
477void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000478 __ addl(ESP, Immediate(GetFrameSize() - FrameEntrySpillSize()));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000479}
480
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100481void CodeGeneratorX86::Bind(HBasicBlock* block) {
482 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000483}
484
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100485void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100486 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000487}
488
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100489Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
490 switch (load->GetType()) {
491 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100492 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100493 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
494 break;
495
496 case Primitive::kPrimInt:
497 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100498 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100499 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100500
501 case Primitive::kPrimBoolean:
502 case Primitive::kPrimByte:
503 case Primitive::kPrimChar:
504 case Primitive::kPrimShort:
505 case Primitive::kPrimVoid:
506 LOG(FATAL) << "Unexpected type " << load->GetType();
507 }
508
509 LOG(FATAL) << "Unreachable";
510 return Location();
511}
512
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100513Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
514 switch (type) {
515 case Primitive::kPrimBoolean:
516 case Primitive::kPrimByte:
517 case Primitive::kPrimChar:
518 case Primitive::kPrimShort:
519 case Primitive::kPrimInt:
520 case Primitive::kPrimNot: {
521 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000522 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100523 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100524 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100525 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000526 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100527 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100528 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100529
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000530 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100531 uint32_t index = gp_index_;
532 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000533 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100534 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100535 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
536 calling_convention.GetRegisterPairAt(index));
537 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100538 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000539 // stack_index_ is the right offset for the memory.
540 return Location::QuickParameter(index, stack_index_ - 2);
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100541 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000542 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
543 }
544 }
545
546 case Primitive::kPrimFloat: {
547 uint32_t index = fp_index_++;
548 stack_index_++;
549 if (index < calling_convention.GetNumberOfFpuRegisters()) {
550 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
551 } else {
552 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
553 }
554 }
555
556 case Primitive::kPrimDouble: {
557 uint32_t index = fp_index_++;
558 stack_index_ += 2;
559 if (index < calling_convention.GetNumberOfFpuRegisters()) {
560 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
561 } else {
562 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100563 }
564 }
565
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100566 case Primitive::kPrimVoid:
567 LOG(FATAL) << "Unexpected parameter type " << type;
568 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100569 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100570 return Location();
571}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100572
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100573void CodeGeneratorX86::Move32(Location destination, Location source) {
574 if (source.Equals(destination)) {
575 return;
576 }
577 if (destination.IsRegister()) {
578 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000579 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100580 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000581 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100582 } else {
583 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000584 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100585 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100586 } else if (destination.IsFpuRegister()) {
587 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000588 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100589 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000590 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100591 } else {
592 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000593 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100594 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100595 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000596 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100597 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000598 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100599 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000600 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500601 } else if (source.IsConstant()) {
602 HConstant* constant = source.GetConstant();
603 int32_t value;
604 if (constant->IsIntConstant()) {
605 value = constant->AsIntConstant()->GetValue();
606 } else {
607 DCHECK(constant->IsFloatConstant());
608 value = bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue());
609 }
610 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100611 } else {
612 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100613 __ pushl(Address(ESP, source.GetStackIndex()));
614 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100615 }
616 }
617}
618
619void CodeGeneratorX86::Move64(Location destination, Location source) {
620 if (source.Equals(destination)) {
621 return;
622 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100623 if (destination.IsRegisterPair()) {
624 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000625 EmitParallelMoves(
626 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
627 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
628 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
629 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100630 } else if (source.IsFpuRegister()) {
631 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100632 } else if (source.IsQuickParameter()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000633 uint16_t register_index = source.GetQuickParameterRegisterIndex();
634 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100635 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000636 EmitParallelMoves(
637 Location::RegisterLocation(calling_convention.GetRegisterAt(register_index)),
638 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
639 Location::StackSlot(
640 calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize()),
641 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100642 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000643 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100644 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100645 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
646 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100647 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
648 }
649 } else if (destination.IsQuickParameter()) {
650 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000651 uint16_t register_index = destination.GetQuickParameterRegisterIndex();
652 uint16_t stack_index = destination.GetQuickParameterStackIndex();
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000653 if (source.IsRegisterPair()) {
654 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100655 } else if (source.IsFpuRegister()) {
656 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100657 } else {
658 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000659 EmitParallelMoves(
660 Location::StackSlot(source.GetStackIndex()),
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000661 Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index)),
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000662 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
663 Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index + 1)));
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000664 __ movl(calling_convention.GetRegisterAt(register_index), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100665 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100666 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500667 if (source.IsFpuRegister()) {
668 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
669 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000670 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100671 } else {
672 LOG(FATAL) << "Unimplemented";
673 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100674 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000675 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100676 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000677 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100678 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100679 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100680 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100681 } else if (source.IsQuickParameter()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000682 // No conflict possible, so just do the move.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100683 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000684 uint16_t register_index = source.GetQuickParameterRegisterIndex();
685 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000686 // Just move the low part. The only time a source is a quick parameter is
687 // when moving the parameter to its stack locations. And the (Java) caller
688 // of this method has already done that.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100689 __ movl(Address(ESP, destination.GetStackIndex()),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000690 calling_convention.GetRegisterAt(register_index));
691 DCHECK_EQ(calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100692 static_cast<size_t>(destination.GetHighStackIndex(kX86WordSize)));
693 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000694 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100695 } else {
696 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000697 EmitParallelMoves(
698 Location::StackSlot(source.GetStackIndex()),
699 Location::StackSlot(destination.GetStackIndex()),
700 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
701 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100702 }
703 }
704}
705
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100706void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000707 LocationSummary* locations = instruction->GetLocations();
708 if (locations != nullptr && locations->Out().Equals(location)) {
709 return;
710 }
711
712 if (locations != nullptr && locations->Out().IsConstant()) {
713 HConstant* const_to_move = locations->Out().GetConstant();
714 if (const_to_move->IsIntConstant()) {
715 Immediate imm(const_to_move->AsIntConstant()->GetValue());
716 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000717 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000718 } else if (location.IsStackSlot()) {
719 __ movl(Address(ESP, location.GetStackIndex()), imm);
720 } else {
721 DCHECK(location.IsConstant());
722 DCHECK_EQ(location.GetConstant(), const_to_move);
723 }
724 } else if (const_to_move->IsLongConstant()) {
725 int64_t value = const_to_move->AsLongConstant()->GetValue();
726 if (location.IsRegisterPair()) {
727 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
728 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
729 } else if (location.IsDoubleStackSlot()) {
730 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000731 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
732 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000733 } else {
734 DCHECK(location.IsConstant());
735 DCHECK_EQ(location.GetConstant(), instruction);
736 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100737 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000738 } else if (instruction->IsTemporary()) {
739 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000740 if (temp_location.IsStackSlot()) {
741 Move32(location, temp_location);
742 } else {
743 DCHECK(temp_location.IsDoubleStackSlot());
744 Move64(location, temp_location);
745 }
Roland Levillain476df552014-10-09 17:51:36 +0100746 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100747 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100748 switch (instruction->GetType()) {
749 case Primitive::kPrimBoolean:
750 case Primitive::kPrimByte:
751 case Primitive::kPrimChar:
752 case Primitive::kPrimShort:
753 case Primitive::kPrimInt:
754 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100755 case Primitive::kPrimFloat:
756 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100757 break;
758
759 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100760 case Primitive::kPrimDouble:
761 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100762 break;
763
764 default:
765 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
766 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000767 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100768 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100769 switch (instruction->GetType()) {
770 case Primitive::kPrimBoolean:
771 case Primitive::kPrimByte:
772 case Primitive::kPrimChar:
773 case Primitive::kPrimShort:
774 case Primitive::kPrimInt:
775 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100776 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000777 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100778 break;
779
780 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100781 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000782 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100783 break;
784
785 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100786 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100787 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000788 }
789}
790
791void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000792 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000793}
794
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000795void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000796 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100797 DCHECK(!successor->IsExitBlock());
798
799 HBasicBlock* block = got->GetBlock();
800 HInstruction* previous = got->GetPrevious();
801
802 HLoopInformation* info = block->GetLoopInformation();
803 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
804 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
805 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
806 return;
807 }
808
809 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
810 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
811 }
812 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000813 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000814 }
815}
816
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000817void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000818 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000819}
820
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000821void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700822 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000823 if (kIsDebugBuild) {
824 __ Comment("Unreachable");
825 __ int3();
826 }
827}
828
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000829void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100830 LocationSummary* locations =
831 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100832 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100833 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100834 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100835 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000836}
837
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000838void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700839 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100840 if (cond->IsIntConstant()) {
841 // Constant condition, statically compared against 1.
842 int32_t cond_value = cond->AsIntConstant()->GetValue();
843 if (cond_value == 1) {
844 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
845 if_instr->IfTrueSuccessor())) {
846 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100847 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100848 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100849 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100850 DCHECK_EQ(cond_value, 0);
851 }
852 } else {
853 bool materialized =
854 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
855 // Moves do not affect the eflags register, so if the condition is
856 // evaluated just before the if, we don't need to evaluate it
857 // again.
858 bool eflags_set = cond->IsCondition()
859 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
860 if (materialized) {
861 if (!eflags_set) {
862 // Materialized condition, compare against 0.
863 Location lhs = if_instr->GetLocations()->InAt(0);
864 if (lhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000865 __ cmpl(lhs.AsRegister<Register>(), Immediate(0));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100866 } else {
867 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
868 }
869 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
870 } else {
871 __ j(X86Condition(cond->AsCondition()->GetCondition()),
872 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
873 }
874 } else {
875 Location lhs = cond->GetLocations()->InAt(0);
876 Location rhs = cond->GetLocations()->InAt(1);
877 // LHS is guaranteed to be in a register (see
878 // LocationsBuilderX86::VisitCondition).
879 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000880 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100881 } else if (rhs.IsConstant()) {
882 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
883 Immediate imm(instruction->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000884 __ cmpl(lhs.AsRegister<Register>(), imm);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100885 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000886 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100887 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100888 __ j(X86Condition(cond->AsCondition()->GetCondition()),
889 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700890 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100891 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100892 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
893 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700894 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000895 }
896}
897
898void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000899 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000900}
901
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000902void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
903 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000904}
905
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000906void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100907 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000908}
909
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000910void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100911 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700912 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000913}
914
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100915void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100916 LocationSummary* locations =
917 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100918 switch (store->InputAt(1)->GetType()) {
919 case Primitive::kPrimBoolean:
920 case Primitive::kPrimByte:
921 case Primitive::kPrimChar:
922 case Primitive::kPrimShort:
923 case Primitive::kPrimInt:
924 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100925 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100926 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
927 break;
928
929 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100930 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100931 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
932 break;
933
934 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100935 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100936 }
937 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000938}
939
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000940void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700941 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000942}
943
Dave Allison20dfc792014-06-16 20:44:29 -0700944void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100945 LocationSummary* locations =
946 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100947 locations->SetInAt(0, Location::RequiresRegister());
948 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100949 if (comp->NeedsMaterialization()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000950 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100951 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000952}
953
Dave Allison20dfc792014-06-16 20:44:29 -0700954void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
955 if (comp->NeedsMaterialization()) {
956 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000957 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100958 // Clear register: setcc only sets the low byte.
959 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700960 if (locations->InAt(1).IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000961 __ cmpl(locations->InAt(0).AsRegister<Register>(),
962 locations->InAt(1).AsRegister<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100963 } else if (locations->InAt(1).IsConstant()) {
964 HConstant* instruction = locations->InAt(1).GetConstant();
965 Immediate imm(instruction->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000966 __ cmpl(locations->InAt(0).AsRegister<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700967 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000968 __ cmpl(locations->InAt(0).AsRegister<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700969 Address(ESP, locations->InAt(1).GetStackIndex()));
970 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000971 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100972 }
Dave Allison20dfc792014-06-16 20:44:29 -0700973}
974
975void LocationsBuilderX86::VisitEqual(HEqual* comp) {
976 VisitCondition(comp);
977}
978
979void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
980 VisitCondition(comp);
981}
982
983void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
984 VisitCondition(comp);
985}
986
987void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
988 VisitCondition(comp);
989}
990
991void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
992 VisitCondition(comp);
993}
994
995void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
996 VisitCondition(comp);
997}
998
999void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1000 VisitCondition(comp);
1001}
1002
1003void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1004 VisitCondition(comp);
1005}
1006
1007void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1008 VisitCondition(comp);
1009}
1010
1011void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1012 VisitCondition(comp);
1013}
1014
1015void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1016 VisitCondition(comp);
1017}
1018
1019void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1020 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001021}
1022
1023void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001024 LocationSummary* locations =
1025 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001026 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001027}
1028
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001029void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001030 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001031 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001032}
1033
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001034void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001035 LocationSummary* locations =
1036 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001037 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001038}
1039
1040void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1041 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001042 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001043}
1044
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001045void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1046 LocationSummary* locations =
1047 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1048 locations->SetOut(Location::ConstantLocation(constant));
1049}
1050
1051void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1052 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001053 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001054}
1055
1056void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1057 LocationSummary* locations =
1058 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1059 locations->SetOut(Location::ConstantLocation(constant));
1060}
1061
1062void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1063 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001064 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001065}
1066
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001067void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001068 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001069}
1070
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001071void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001072 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001073 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001074 __ ret();
1075}
1076
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001077void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001078 LocationSummary* locations =
1079 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001080 switch (ret->InputAt(0)->GetType()) {
1081 case Primitive::kPrimBoolean:
1082 case Primitive::kPrimByte:
1083 case Primitive::kPrimChar:
1084 case Primitive::kPrimShort:
1085 case Primitive::kPrimInt:
1086 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001087 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001088 break;
1089
1090 case Primitive::kPrimLong:
1091 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001092 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001093 break;
1094
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001095 case Primitive::kPrimFloat:
1096 case Primitive::kPrimDouble:
1097 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001098 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001099 break;
1100
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001101 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001102 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001103 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001104}
1105
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001106void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001107 if (kIsDebugBuild) {
1108 switch (ret->InputAt(0)->GetType()) {
1109 case Primitive::kPrimBoolean:
1110 case Primitive::kPrimByte:
1111 case Primitive::kPrimChar:
1112 case Primitive::kPrimShort:
1113 case Primitive::kPrimInt:
1114 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001115 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001116 break;
1117
1118 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001119 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1120 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001121 break;
1122
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001123 case Primitive::kPrimFloat:
1124 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001125 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001126 break;
1127
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001128 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001129 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001130 }
1131 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001132 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001133 __ ret();
1134}
1135
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001136void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001137 HandleInvoke(invoke);
1138}
1139
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001140void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001141 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001142
1143 // TODO: Implement all kinds of calls:
1144 // 1) boot -> boot
1145 // 2) app -> boot
1146 // 3) app -> app
1147 //
1148 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1149
1150 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001151 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001152 if (!invoke->IsRecursive()) {
1153 // temp = temp->dex_cache_resolved_methods_;
1154 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
1155 // temp = temp[index_in_cache]
1156 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
1157 // (temp + offset_of_quick_compiled_code)()
1158 __ call(Address(
1159 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
1160 } else {
1161 __ call(codegen_->GetFrameEntryLabel());
1162 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001163
1164 DCHECK(!codegen_->IsLeafMethod());
1165 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1166}
1167
1168void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1169 HandleInvoke(invoke);
1170}
1171
1172void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001173 LocationSummary* locations =
1174 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001175 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001176
1177 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001178 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001179 HInstruction* input = invoke->InputAt(i);
1180 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1181 }
1182
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001183 switch (invoke->GetType()) {
1184 case Primitive::kPrimBoolean:
1185 case Primitive::kPrimByte:
1186 case Primitive::kPrimChar:
1187 case Primitive::kPrimShort:
1188 case Primitive::kPrimInt:
1189 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001190 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001191 break;
1192
1193 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001194 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001195 break;
1196
1197 case Primitive::kPrimVoid:
1198 break;
1199
1200 case Primitive::kPrimDouble:
1201 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001202 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001203 break;
1204 }
1205
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001206 invoke->SetLocations(locations);
1207}
1208
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001209void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001210 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001211 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1212 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1213 LocationSummary* locations = invoke->GetLocations();
1214 Location receiver = locations->InAt(0);
1215 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1216 // temp = object->GetClass();
1217 if (receiver.IsStackSlot()) {
1218 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1219 __ movl(temp, Address(temp, class_offset));
1220 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001221 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001222 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001223 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001224 // temp = temp->GetMethodAt(method_offset);
1225 __ movl(temp, Address(temp, method_offset));
1226 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001227 __ call(Address(
1228 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001229
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001230 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001231 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001232}
1233
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001234void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1235 HandleInvoke(invoke);
1236 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001237 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001238}
1239
1240void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1241 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001242 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001243 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1244 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1245 LocationSummary* locations = invoke->GetLocations();
1246 Location receiver = locations->InAt(0);
1247 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1248
1249 // Set the hidden argument.
1250 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001251 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001252
1253 // temp = object->GetClass();
1254 if (receiver.IsStackSlot()) {
1255 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1256 __ movl(temp, Address(temp, class_offset));
1257 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001258 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001259 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001260 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001261 // temp = temp->GetImtEntryAt(method_offset);
1262 __ movl(temp, Address(temp, method_offset));
1263 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001264 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001265 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001266
1267 DCHECK(!codegen_->IsLeafMethod());
1268 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1269}
1270
Roland Levillain88cb1752014-10-20 16:36:47 +01001271void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1272 LocationSummary* locations =
1273 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1274 switch (neg->GetResultType()) {
1275 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001276 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001277 locations->SetInAt(0, Location::RequiresRegister());
1278 locations->SetOut(Location::SameAsFirstInput());
1279 break;
1280
Roland Levillain88cb1752014-10-20 16:36:47 +01001281 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001282 locations->SetInAt(0, Location::RequiresFpuRegister());
1283 locations->SetOut(Location::SameAsFirstInput());
1284 locations->AddTemp(Location::RequiresRegister());
1285 locations->AddTemp(Location::RequiresFpuRegister());
1286 break;
1287
Roland Levillain88cb1752014-10-20 16:36:47 +01001288 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001289 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001290 locations->SetOut(Location::SameAsFirstInput());
1291 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001292 break;
1293
1294 default:
1295 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1296 }
1297}
1298
1299void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1300 LocationSummary* locations = neg->GetLocations();
1301 Location out = locations->Out();
1302 Location in = locations->InAt(0);
1303 switch (neg->GetResultType()) {
1304 case Primitive::kPrimInt:
1305 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001306 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001307 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001308 break;
1309
1310 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001311 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001312 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001313 __ negl(out.AsRegisterPairLow<Register>());
1314 // Negation is similar to subtraction from zero. The least
1315 // significant byte triggers a borrow when it is different from
1316 // zero; to take it into account, add 1 to the most significant
1317 // byte if the carry flag (CF) is set to 1 after the first NEGL
1318 // operation.
1319 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1320 __ negl(out.AsRegisterPairHigh<Register>());
1321 break;
1322
Roland Levillain5368c212014-11-27 15:03:41 +00001323 case Primitive::kPrimFloat: {
1324 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001325 Register constant = locations->GetTemp(0).AsRegister<Register>();
1326 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001327 // Implement float negation with an exclusive or with value
1328 // 0x80000000 (mask for bit 31, representing the sign of a
1329 // single-precision floating-point number).
1330 __ movl(constant, Immediate(INT32_C(0x80000000)));
1331 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001332 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001333 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001334 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001335
Roland Levillain5368c212014-11-27 15:03:41 +00001336 case Primitive::kPrimDouble: {
1337 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001338 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001339 // Implement double negation with an exclusive or with value
1340 // 0x8000000000000000 (mask for bit 63, representing the sign of
1341 // a double-precision floating-point number).
1342 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001343 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001344 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001345 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001346
1347 default:
1348 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1349 }
1350}
1351
Roland Levillaindff1f282014-11-05 14:15:05 +00001352void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001353 Primitive::Type result_type = conversion->GetResultType();
1354 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001355 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001356
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001357 // The float-to-long and double-to-long type conversions rely on a
1358 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001359 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001360 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1361 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001362 ? LocationSummary::kCall
1363 : LocationSummary::kNoCall;
1364 LocationSummary* locations =
1365 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1366
Roland Levillaindff1f282014-11-05 14:15:05 +00001367 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001368 case Primitive::kPrimByte:
1369 switch (input_type) {
1370 case Primitive::kPrimShort:
1371 case Primitive::kPrimInt:
1372 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001373 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001374 locations->SetInAt(0, Location::Any());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001375 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1376 break;
1377
1378 default:
1379 LOG(FATAL) << "Unexpected type conversion from " << input_type
1380 << " to " << result_type;
1381 }
1382 break;
1383
Roland Levillain01a8d712014-11-14 16:27:39 +00001384 case Primitive::kPrimShort:
1385 switch (input_type) {
1386 case Primitive::kPrimByte:
1387 case Primitive::kPrimInt:
1388 case Primitive::kPrimChar:
1389 // Processing a Dex `int-to-short' instruction.
1390 locations->SetInAt(0, Location::Any());
1391 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1392 break;
1393
1394 default:
1395 LOG(FATAL) << "Unexpected type conversion from " << input_type
1396 << " to " << result_type;
1397 }
1398 break;
1399
Roland Levillain946e1432014-11-11 17:35:19 +00001400 case Primitive::kPrimInt:
1401 switch (input_type) {
1402 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001403 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001404 locations->SetInAt(0, Location::Any());
1405 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1406 break;
1407
1408 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001409 // Processing a Dex `float-to-int' instruction.
1410 locations->SetInAt(0, Location::RequiresFpuRegister());
1411 locations->SetOut(Location::RequiresRegister());
1412 locations->AddTemp(Location::RequiresFpuRegister());
1413 break;
1414
Roland Levillain946e1432014-11-11 17:35:19 +00001415 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001416 // Processing a Dex `double-to-int' instruction.
1417 locations->SetInAt(0, Location::RequiresFpuRegister());
1418 locations->SetOut(Location::RequiresRegister());
1419 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001420 break;
1421
1422 default:
1423 LOG(FATAL) << "Unexpected type conversion from " << input_type
1424 << " to " << result_type;
1425 }
1426 break;
1427
Roland Levillaindff1f282014-11-05 14:15:05 +00001428 case Primitive::kPrimLong:
1429 switch (input_type) {
1430 case Primitive::kPrimByte:
1431 case Primitive::kPrimShort:
1432 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001433 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001434 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001435 locations->SetInAt(0, Location::RegisterLocation(EAX));
1436 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1437 break;
1438
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001439 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001440 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001441 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001442 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001443 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1444 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1445
Vladimir Marko949c91f2015-01-27 10:48:44 +00001446 // The runtime helper puts the result in EAX, EDX.
1447 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001448 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001449 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001450
1451 default:
1452 LOG(FATAL) << "Unexpected type conversion from " << input_type
1453 << " to " << result_type;
1454 }
1455 break;
1456
Roland Levillain981e4542014-11-14 11:47:14 +00001457 case Primitive::kPrimChar:
1458 switch (input_type) {
1459 case Primitive::kPrimByte:
1460 case Primitive::kPrimShort:
1461 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001462 // Processing a Dex `int-to-char' instruction.
1463 locations->SetInAt(0, Location::Any());
1464 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1465 break;
1466
1467 default:
1468 LOG(FATAL) << "Unexpected type conversion from " << input_type
1469 << " to " << result_type;
1470 }
1471 break;
1472
Roland Levillaindff1f282014-11-05 14:15:05 +00001473 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001474 switch (input_type) {
1475 case Primitive::kPrimByte:
1476 case Primitive::kPrimShort:
1477 case Primitive::kPrimInt:
1478 case Primitive::kPrimChar:
1479 // Processing a Dex `int-to-float' instruction.
1480 locations->SetInAt(0, Location::RequiresRegister());
1481 locations->SetOut(Location::RequiresFpuRegister());
1482 break;
1483
1484 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001485 // Processing a Dex `long-to-float' instruction.
1486 locations->SetInAt(0, Location::RequiresRegister());
1487 locations->SetOut(Location::RequiresFpuRegister());
1488 locations->AddTemp(Location::RequiresFpuRegister());
1489 locations->AddTemp(Location::RequiresFpuRegister());
1490 break;
1491
Roland Levillaincff13742014-11-17 14:32:17 +00001492 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001493 // Processing a Dex `double-to-float' instruction.
1494 locations->SetInAt(0, Location::RequiresFpuRegister());
1495 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001496 break;
1497
1498 default:
1499 LOG(FATAL) << "Unexpected type conversion from " << input_type
1500 << " to " << result_type;
1501 };
1502 break;
1503
Roland Levillaindff1f282014-11-05 14:15:05 +00001504 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001505 switch (input_type) {
1506 case Primitive::kPrimByte:
1507 case Primitive::kPrimShort:
1508 case Primitive::kPrimInt:
1509 case Primitive::kPrimChar:
1510 // Processing a Dex `int-to-double' instruction.
1511 locations->SetInAt(0, Location::RequiresRegister());
1512 locations->SetOut(Location::RequiresFpuRegister());
1513 break;
1514
1515 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001516 // Processing a Dex `long-to-double' instruction.
1517 locations->SetInAt(0, Location::RequiresRegister());
1518 locations->SetOut(Location::RequiresFpuRegister());
1519 locations->AddTemp(Location::RequiresFpuRegister());
1520 locations->AddTemp(Location::RequiresFpuRegister());
1521 break;
1522
Roland Levillaincff13742014-11-17 14:32:17 +00001523 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001524 // Processing a Dex `float-to-double' instruction.
1525 locations->SetInAt(0, Location::RequiresFpuRegister());
1526 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001527 break;
1528
1529 default:
1530 LOG(FATAL) << "Unexpected type conversion from " << input_type
1531 << " to " << result_type;
1532 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001533 break;
1534
1535 default:
1536 LOG(FATAL) << "Unexpected type conversion from " << input_type
1537 << " to " << result_type;
1538 }
1539}
1540
1541void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1542 LocationSummary* locations = conversion->GetLocations();
1543 Location out = locations->Out();
1544 Location in = locations->InAt(0);
1545 Primitive::Type result_type = conversion->GetResultType();
1546 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001547 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001548 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001549 case Primitive::kPrimByte:
1550 switch (input_type) {
1551 case Primitive::kPrimShort:
1552 case Primitive::kPrimInt:
1553 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001554 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001555 if (in.IsRegister()) {
1556 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
1557 } else if (in.IsStackSlot()) {
1558 __ movsxb(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
1559 } else {
1560 DCHECK(in.GetConstant()->IsIntConstant());
1561 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1562 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1563 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001564 break;
1565
1566 default:
1567 LOG(FATAL) << "Unexpected type conversion from " << input_type
1568 << " to " << result_type;
1569 }
1570 break;
1571
Roland Levillain01a8d712014-11-14 16:27:39 +00001572 case Primitive::kPrimShort:
1573 switch (input_type) {
1574 case Primitive::kPrimByte:
1575 case Primitive::kPrimInt:
1576 case Primitive::kPrimChar:
1577 // Processing a Dex `int-to-short' instruction.
1578 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001579 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001580 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001581 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001582 } else {
1583 DCHECK(in.GetConstant()->IsIntConstant());
1584 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001585 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001586 }
1587 break;
1588
1589 default:
1590 LOG(FATAL) << "Unexpected type conversion from " << input_type
1591 << " to " << result_type;
1592 }
1593 break;
1594
Roland Levillain946e1432014-11-11 17:35:19 +00001595 case Primitive::kPrimInt:
1596 switch (input_type) {
1597 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001598 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001599 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001600 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001601 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001602 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001603 } else {
1604 DCHECK(in.IsConstant());
1605 DCHECK(in.GetConstant()->IsLongConstant());
1606 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001607 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001608 }
1609 break;
1610
Roland Levillain3f8f9362014-12-02 17:45:01 +00001611 case Primitive::kPrimFloat: {
1612 // Processing a Dex `float-to-int' instruction.
1613 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1614 Register output = out.AsRegister<Register>();
1615 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1616 Label done, nan;
1617
1618 __ movl(output, Immediate(kPrimIntMax));
1619 // temp = int-to-float(output)
1620 __ cvtsi2ss(temp, output);
1621 // if input >= temp goto done
1622 __ comiss(input, temp);
1623 __ j(kAboveEqual, &done);
1624 // if input == NaN goto nan
1625 __ j(kUnordered, &nan);
1626 // output = float-to-int-truncate(input)
1627 __ cvttss2si(output, input);
1628 __ jmp(&done);
1629 __ Bind(&nan);
1630 // output = 0
1631 __ xorl(output, output);
1632 __ Bind(&done);
1633 break;
1634 }
1635
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001636 case Primitive::kPrimDouble: {
1637 // Processing a Dex `double-to-int' instruction.
1638 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1639 Register output = out.AsRegister<Register>();
1640 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1641 Label done, nan;
1642
1643 __ movl(output, Immediate(kPrimIntMax));
1644 // temp = int-to-double(output)
1645 __ cvtsi2sd(temp, output);
1646 // if input >= temp goto done
1647 __ comisd(input, temp);
1648 __ j(kAboveEqual, &done);
1649 // if input == NaN goto nan
1650 __ j(kUnordered, &nan);
1651 // output = double-to-int-truncate(input)
1652 __ cvttsd2si(output, input);
1653 __ jmp(&done);
1654 __ Bind(&nan);
1655 // output = 0
1656 __ xorl(output, output);
1657 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001658 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001659 }
Roland Levillain946e1432014-11-11 17:35:19 +00001660
1661 default:
1662 LOG(FATAL) << "Unexpected type conversion from " << input_type
1663 << " to " << result_type;
1664 }
1665 break;
1666
Roland Levillaindff1f282014-11-05 14:15:05 +00001667 case Primitive::kPrimLong:
1668 switch (input_type) {
1669 case Primitive::kPrimByte:
1670 case Primitive::kPrimShort:
1671 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001672 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001673 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001674 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1675 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001676 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001677 __ cdq();
1678 break;
1679
1680 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001681 // Processing a Dex `float-to-long' instruction.
1682 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001683 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1684 break;
1685
Roland Levillaindff1f282014-11-05 14:15:05 +00001686 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001687 // Processing a Dex `double-to-long' instruction.
1688 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1689 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001690 break;
1691
1692 default:
1693 LOG(FATAL) << "Unexpected type conversion from " << input_type
1694 << " to " << result_type;
1695 }
1696 break;
1697
Roland Levillain981e4542014-11-14 11:47:14 +00001698 case Primitive::kPrimChar:
1699 switch (input_type) {
1700 case Primitive::kPrimByte:
1701 case Primitive::kPrimShort:
1702 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001703 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1704 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001705 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001706 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001707 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001708 } else {
1709 DCHECK(in.GetConstant()->IsIntConstant());
1710 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001711 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001712 }
1713 break;
1714
1715 default:
1716 LOG(FATAL) << "Unexpected type conversion from " << input_type
1717 << " to " << result_type;
1718 }
1719 break;
1720
Roland Levillaindff1f282014-11-05 14:15:05 +00001721 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001722 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001723 case Primitive::kPrimByte:
1724 case Primitive::kPrimShort:
1725 case Primitive::kPrimInt:
1726 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001727 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001728 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001729 break;
1730
Roland Levillain6d0e4832014-11-27 18:31:21 +00001731 case Primitive::kPrimLong: {
1732 // Processing a Dex `long-to-float' instruction.
1733 Register low = in.AsRegisterPairLow<Register>();
1734 Register high = in.AsRegisterPairHigh<Register>();
1735 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1736 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1737 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1738
1739 // Operations use doubles for precision reasons (each 32-bit
1740 // half of a long fits in the 53-bit mantissa of a double,
1741 // but not in the 24-bit mantissa of a float). This is
1742 // especially important for the low bits. The result is
1743 // eventually converted to float.
1744
1745 // low = low - 2^31 (to prevent bit 31 of `low` to be
1746 // interpreted as a sign bit)
1747 __ subl(low, Immediate(0x80000000));
1748 // temp = int-to-double(high)
1749 __ cvtsi2sd(temp, high);
1750 // temp = temp * 2^32
1751 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
1752 __ mulsd(temp, constant);
1753 // result = int-to-double(low)
1754 __ cvtsi2sd(result, low);
1755 // result = result + 2^31 (restore the original value of `low`)
1756 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
1757 __ addsd(result, constant);
1758 // result = result + temp
1759 __ addsd(result, temp);
1760 // result = double-to-float(result)
1761 __ cvtsd2ss(result, result);
1762 break;
1763 }
1764
Roland Levillaincff13742014-11-17 14:32:17 +00001765 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001766 // Processing a Dex `double-to-float' instruction.
1767 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001768 break;
1769
1770 default:
1771 LOG(FATAL) << "Unexpected type conversion from " << input_type
1772 << " to " << result_type;
1773 };
1774 break;
1775
Roland Levillaindff1f282014-11-05 14:15:05 +00001776 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001777 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001778 case Primitive::kPrimByte:
1779 case Primitive::kPrimShort:
1780 case Primitive::kPrimInt:
1781 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001782 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001783 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001784 break;
1785
Roland Levillain647b9ed2014-11-27 12:06:00 +00001786 case Primitive::kPrimLong: {
1787 // Processing a Dex `long-to-double' instruction.
1788 Register low = in.AsRegisterPairLow<Register>();
1789 Register high = in.AsRegisterPairHigh<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001790 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1791 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1792 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001793
Roland Levillain647b9ed2014-11-27 12:06:00 +00001794 // low = low - 2^31 (to prevent bit 31 of `low` to be
1795 // interpreted as a sign bit)
1796 __ subl(low, Immediate(0x80000000));
1797 // temp = int-to-double(high)
1798 __ cvtsi2sd(temp, high);
1799 // temp = temp * 2^32
Roland Levillain6d0e4832014-11-27 18:31:21 +00001800 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001801 __ mulsd(temp, constant);
1802 // result = int-to-double(low)
1803 __ cvtsi2sd(result, low);
1804 // result = result + 2^31 (restore the original value of `low`)
Roland Levillain6d0e4832014-11-27 18:31:21 +00001805 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001806 __ addsd(result, constant);
1807 // result = result + temp
1808 __ addsd(result, temp);
1809 break;
1810 }
1811
Roland Levillaincff13742014-11-17 14:32:17 +00001812 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001813 // Processing a Dex `float-to-double' instruction.
1814 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001815 break;
1816
1817 default:
1818 LOG(FATAL) << "Unexpected type conversion from " << input_type
1819 << " to " << result_type;
1820 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001821 break;
1822
1823 default:
1824 LOG(FATAL) << "Unexpected type conversion from " << input_type
1825 << " to " << result_type;
1826 }
1827}
1828
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001829void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001830 LocationSummary* locations =
1831 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001832 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001833 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001834 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001835 locations->SetInAt(0, Location::RequiresRegister());
1836 locations->SetInAt(1, Location::Any());
1837 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001838 break;
1839 }
1840
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001841 case Primitive::kPrimFloat:
1842 case Primitive::kPrimDouble: {
1843 locations->SetInAt(0, Location::RequiresFpuRegister());
1844 locations->SetInAt(1, Location::Any());
1845 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001846 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001847 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001848
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001849 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001850 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1851 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001852 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001853}
1854
1855void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1856 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001857 Location first = locations->InAt(0);
1858 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001859 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001860 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001861 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001862 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001863 __ addl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001864 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001865 __ addl(first.AsRegister<Register>(),
1866 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001867 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001868 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001869 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001870 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001871 }
1872
1873 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001874 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001875 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1876 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001877 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001878 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1879 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001880 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001881 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001882 break;
1883 }
1884
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001885 case Primitive::kPrimFloat: {
1886 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001887 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001888 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001889 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001890 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001891 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001892 }
1893
1894 case Primitive::kPrimDouble: {
1895 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001896 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001897 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001898 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001899 }
1900 break;
1901 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001902
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001903 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001904 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001905 }
1906}
1907
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001908void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001909 LocationSummary* locations =
1910 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001911 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001912 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001913 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001914 locations->SetInAt(0, Location::RequiresRegister());
1915 locations->SetInAt(1, Location::Any());
1916 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001917 break;
1918 }
Calin Juravle11351682014-10-23 15:38:15 +01001919 case Primitive::kPrimFloat:
1920 case Primitive::kPrimDouble: {
1921 locations->SetInAt(0, Location::RequiresFpuRegister());
1922 locations->SetInAt(1, Location::RequiresFpuRegister());
1923 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001924 break;
Calin Juravle11351682014-10-23 15:38:15 +01001925 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001926
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001927 default:
Calin Juravle11351682014-10-23 15:38:15 +01001928 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001929 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001930}
1931
1932void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1933 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001934 Location first = locations->InAt(0);
1935 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001936 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001937 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001938 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001939 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001940 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001941 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001942 __ subl(first.AsRegister<Register>(),
1943 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001944 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001945 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001946 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001947 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001948 }
1949
1950 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001951 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01001952 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1953 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001954 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001955 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001956 __ sbbl(first.AsRegisterPairHigh<Register>(),
1957 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001958 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001959 break;
1960 }
1961
Calin Juravle11351682014-10-23 15:38:15 +01001962 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001963 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001964 break;
Calin Juravle11351682014-10-23 15:38:15 +01001965 }
1966
1967 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001968 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001969 break;
1970 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001971
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001972 default:
Calin Juravle11351682014-10-23 15:38:15 +01001973 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001974 }
1975}
1976
Calin Juravle34bacdf2014-10-07 20:23:36 +01001977void LocationsBuilderX86::VisitMul(HMul* mul) {
1978 LocationSummary* locations =
1979 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1980 switch (mul->GetResultType()) {
1981 case Primitive::kPrimInt:
1982 locations->SetInAt(0, Location::RequiresRegister());
1983 locations->SetInAt(1, Location::Any());
1984 locations->SetOut(Location::SameAsFirstInput());
1985 break;
1986 case Primitive::kPrimLong: {
1987 locations->SetInAt(0, Location::RequiresRegister());
1988 // TODO: Currently this handles only stack operands:
1989 // - we don't have enough registers because we currently use Quick ABI.
1990 // - by the time we have a working register allocator we will probably change the ABI
1991 // and fix the above.
1992 // - we don't have a way yet to request operands on stack but the base line compiler
1993 // will leave the operands on the stack with Any().
1994 locations->SetInAt(1, Location::Any());
1995 locations->SetOut(Location::SameAsFirstInput());
1996 // Needed for imul on 32bits with 64bits output.
1997 locations->AddTemp(Location::RegisterLocation(EAX));
1998 locations->AddTemp(Location::RegisterLocation(EDX));
1999 break;
2000 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002001 case Primitive::kPrimFloat:
2002 case Primitive::kPrimDouble: {
2003 locations->SetInAt(0, Location::RequiresFpuRegister());
2004 locations->SetInAt(1, Location::RequiresFpuRegister());
2005 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002006 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002007 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002008
2009 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002010 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002011 }
2012}
2013
2014void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2015 LocationSummary* locations = mul->GetLocations();
2016 Location first = locations->InAt(0);
2017 Location second = locations->InAt(1);
2018 DCHECK(first.Equals(locations->Out()));
2019
2020 switch (mul->GetResultType()) {
2021 case Primitive::kPrimInt: {
2022 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002023 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002024 } else if (second.IsConstant()) {
2025 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002026 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002027 } else {
2028 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002029 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002030 }
2031 break;
2032 }
2033
2034 case Primitive::kPrimLong: {
2035 DCHECK(second.IsDoubleStackSlot());
2036
2037 Register in1_hi = first.AsRegisterPairHigh<Register>();
2038 Register in1_lo = first.AsRegisterPairLow<Register>();
2039 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2040 Address in2_lo(ESP, second.GetStackIndex());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002041 Register eax = locations->GetTemp(0).AsRegister<Register>();
2042 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002043
2044 DCHECK_EQ(EAX, eax);
2045 DCHECK_EQ(EDX, edx);
2046
2047 // input: in1 - 64 bits, in2 - 64 bits
2048 // output: in1
2049 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2050 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2051 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
2052
2053 __ movl(eax, in2_hi);
2054 // eax <- in1.lo * in2.hi
2055 __ imull(eax, in1_lo);
2056 // in1.hi <- in1.hi * in2.lo
2057 __ imull(in1_hi, in2_lo);
2058 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2059 __ addl(in1_hi, eax);
2060 // move in1_lo to eax to prepare for double precision
2061 __ movl(eax, in1_lo);
2062 // edx:eax <- in1.lo * in2.lo
2063 __ mull(in2_lo);
2064 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2065 __ addl(in1_hi, edx);
2066 // in1.lo <- (in1.lo * in2.lo)[31:0];
2067 __ movl(in1_lo, eax);
2068
2069 break;
2070 }
2071
Calin Juravleb5bfa962014-10-21 18:02:24 +01002072 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002073 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002074 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002075 }
2076
2077 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002078 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002079 break;
2080 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002081
2082 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002083 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002084 }
2085}
2086
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002087void InstructionCodeGeneratorX86::PushOntoFPStack(Location source, uint32_t temp_offset,
2088 uint32_t stack_adjustment, bool is_float) {
2089 if (source.IsStackSlot()) {
2090 DCHECK(is_float);
2091 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2092 } else if (source.IsDoubleStackSlot()) {
2093 DCHECK(!is_float);
2094 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2095 } else {
2096 // Write the value to the temporary location on the stack and load to FP stack.
2097 if (is_float) {
2098 Location stack_temp = Location::StackSlot(temp_offset);
2099 codegen_->Move32(stack_temp, source);
2100 __ flds(Address(ESP, temp_offset));
2101 } else {
2102 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2103 codegen_->Move64(stack_temp, source);
2104 __ fldl(Address(ESP, temp_offset));
2105 }
2106 }
2107}
2108
2109void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2110 Primitive::Type type = rem->GetResultType();
2111 bool is_float = type == Primitive::kPrimFloat;
2112 size_t elem_size = Primitive::ComponentSize(type);
2113 LocationSummary* locations = rem->GetLocations();
2114 Location first = locations->InAt(0);
2115 Location second = locations->InAt(1);
2116 Location out = locations->Out();
2117
2118 // Create stack space for 2 elements.
2119 // TODO: enhance register allocator to ask for stack temporaries.
2120 __ subl(ESP, Immediate(2 * elem_size));
2121
2122 // Load the values to the FP stack in reverse order, using temporaries if needed.
2123 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2124 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2125
2126 // Loop doing FPREM until we stabilize.
2127 Label retry;
2128 __ Bind(&retry);
2129 __ fprem();
2130
2131 // Move FP status to AX.
2132 __ fstsw();
2133
2134 // And see if the argument reduction is complete. This is signaled by the
2135 // C2 FPU flag bit set to 0.
2136 __ andl(EAX, Immediate(kC2ConditionMask));
2137 __ j(kNotEqual, &retry);
2138
2139 // We have settled on the final value. Retrieve it into an XMM register.
2140 // Store FP top of stack to real stack.
2141 if (is_float) {
2142 __ fsts(Address(ESP, 0));
2143 } else {
2144 __ fstl(Address(ESP, 0));
2145 }
2146
2147 // Pop the 2 items from the FP stack.
2148 __ fucompp();
2149
2150 // Load the value from the stack into an XMM register.
2151 DCHECK(out.IsFpuRegister()) << out;
2152 if (is_float) {
2153 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2154 } else {
2155 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2156 }
2157
2158 // And remove the temporary stack space we allocated.
2159 __ addl(ESP, Immediate(2 * elem_size));
2160}
2161
Calin Juravlebacfec32014-11-14 15:54:36 +00002162void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2163 DCHECK(instruction->IsDiv() || instruction->IsRem());
2164
2165 LocationSummary* locations = instruction->GetLocations();
2166 Location out = locations->Out();
2167 Location first = locations->InAt(0);
2168 Location second = locations->InAt(1);
2169 bool is_div = instruction->IsDiv();
2170
2171 switch (instruction->GetResultType()) {
2172 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002173 Register second_reg = second.AsRegister<Register>();
2174 DCHECK_EQ(EAX, first.AsRegister<Register>());
2175 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002176
2177 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002178 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
2179 is_div);
Calin Juravlebacfec32014-11-14 15:54:36 +00002180 codegen_->AddSlowPath(slow_path);
2181
2182 // 0x80000000/-1 triggers an arithmetic exception!
2183 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2184 // it's safe to just use negl instead of more complex comparisons.
2185
2186 __ cmpl(second_reg, Immediate(-1));
2187 __ j(kEqual, slow_path->GetEntryLabel());
2188
2189 // edx:eax <- sign-extended of eax
2190 __ cdq();
2191 // eax = quotient, edx = remainder
2192 __ idivl(second_reg);
2193
2194 __ Bind(slow_path->GetExitLabel());
2195 break;
2196 }
2197
2198 case Primitive::kPrimLong: {
2199 InvokeRuntimeCallingConvention calling_convention;
2200 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2201 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2202 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2203 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2204 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2205 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2206
2207 if (is_div) {
2208 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2209 } else {
2210 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2211 }
2212 uint32_t dex_pc = is_div
2213 ? instruction->AsDiv()->GetDexPc()
2214 : instruction->AsRem()->GetDexPc();
2215 codegen_->RecordPcInfo(instruction, dex_pc);
2216
2217 break;
2218 }
2219
2220 default:
2221 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2222 }
2223}
2224
Calin Juravle7c4954d2014-10-28 16:57:40 +00002225void LocationsBuilderX86::VisitDiv(HDiv* div) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002226 LocationSummary::CallKind call_kind = div->GetResultType() == Primitive::kPrimLong
2227 ? LocationSummary::kCall
2228 : LocationSummary::kNoCall;
2229 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2230
Calin Juravle7c4954d2014-10-28 16:57:40 +00002231 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002232 case Primitive::kPrimInt: {
2233 locations->SetInAt(0, Location::RegisterLocation(EAX));
2234 locations->SetInAt(1, Location::RequiresRegister());
2235 locations->SetOut(Location::SameAsFirstInput());
2236 // Intel uses edx:eax as the dividend.
2237 locations->AddTemp(Location::RegisterLocation(EDX));
2238 break;
2239 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002240 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002241 InvokeRuntimeCallingConvention calling_convention;
2242 locations->SetInAt(0, Location::RegisterPairLocation(
2243 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2244 locations->SetInAt(1, Location::RegisterPairLocation(
2245 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2246 // Runtime helper puts the result in EAX, EDX.
2247 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002248 break;
2249 }
2250 case Primitive::kPrimFloat:
2251 case Primitive::kPrimDouble: {
2252 locations->SetInAt(0, Location::RequiresFpuRegister());
2253 locations->SetInAt(1, Location::RequiresFpuRegister());
2254 locations->SetOut(Location::SameAsFirstInput());
2255 break;
2256 }
2257
2258 default:
2259 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2260 }
2261}
2262
2263void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2264 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002265 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002266 Location first = locations->InAt(0);
2267 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002268
2269 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002270 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002271 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002272 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002273 break;
2274 }
2275
2276 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002277 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002278 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002279 break;
2280 }
2281
2282 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002283 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002284 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002285 break;
2286 }
2287
2288 default:
2289 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2290 }
2291}
2292
Calin Juravlebacfec32014-11-14 15:54:36 +00002293void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002294 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002295 LocationSummary* locations =
2296 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravlebacfec32014-11-14 15:54:36 +00002297
Calin Juravled2ec87d2014-12-08 14:24:46 +00002298 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002299 case Primitive::kPrimInt: {
2300 locations->SetInAt(0, Location::RegisterLocation(EAX));
2301 locations->SetInAt(1, Location::RequiresRegister());
2302 locations->SetOut(Location::RegisterLocation(EDX));
2303 break;
2304 }
2305 case Primitive::kPrimLong: {
2306 InvokeRuntimeCallingConvention calling_convention;
2307 locations->SetInAt(0, Location::RegisterPairLocation(
2308 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2309 locations->SetInAt(1, Location::RegisterPairLocation(
2310 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2311 // Runtime helper puts the result in EAX, EDX.
2312 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2313 break;
2314 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002315 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002316 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002317 locations->SetInAt(0, Location::Any());
2318 locations->SetInAt(1, Location::Any());
2319 locations->SetOut(Location::RequiresFpuRegister());
2320 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002321 break;
2322 }
2323
2324 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002325 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002326 }
2327}
2328
2329void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2330 Primitive::Type type = rem->GetResultType();
2331 switch (type) {
2332 case Primitive::kPrimInt:
2333 case Primitive::kPrimLong: {
2334 GenerateDivRemIntegral(rem);
2335 break;
2336 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002337 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002338 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002339 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002340 break;
2341 }
2342 default:
2343 LOG(FATAL) << "Unexpected rem type " << type;
2344 }
2345}
2346
Calin Juravled0d48522014-11-04 16:40:20 +00002347void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2348 LocationSummary* locations =
2349 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002350 switch (instruction->GetType()) {
2351 case Primitive::kPrimInt: {
2352 locations->SetInAt(0, Location::Any());
2353 break;
2354 }
2355 case Primitive::kPrimLong: {
2356 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2357 if (!instruction->IsConstant()) {
2358 locations->AddTemp(Location::RequiresRegister());
2359 }
2360 break;
2361 }
2362 default:
2363 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2364 }
Calin Juravled0d48522014-11-04 16:40:20 +00002365 if (instruction->HasUses()) {
2366 locations->SetOut(Location::SameAsFirstInput());
2367 }
2368}
2369
2370void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2371 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2372 codegen_->AddSlowPath(slow_path);
2373
2374 LocationSummary* locations = instruction->GetLocations();
2375 Location value = locations->InAt(0);
2376
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002377 switch (instruction->GetType()) {
2378 case Primitive::kPrimInt: {
2379 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002380 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002381 __ j(kEqual, slow_path->GetEntryLabel());
2382 } else if (value.IsStackSlot()) {
2383 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2384 __ j(kEqual, slow_path->GetEntryLabel());
2385 } else {
2386 DCHECK(value.IsConstant()) << value;
2387 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2388 __ jmp(slow_path->GetEntryLabel());
2389 }
2390 }
2391 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002392 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002393 case Primitive::kPrimLong: {
2394 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002395 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002396 __ movl(temp, value.AsRegisterPairLow<Register>());
2397 __ orl(temp, value.AsRegisterPairHigh<Register>());
2398 __ j(kEqual, slow_path->GetEntryLabel());
2399 } else {
2400 DCHECK(value.IsConstant()) << value;
2401 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2402 __ jmp(slow_path->GetEntryLabel());
2403 }
2404 }
2405 break;
2406 }
2407 default:
2408 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002409 }
Calin Juravled0d48522014-11-04 16:40:20 +00002410}
2411
Calin Juravle9aec02f2014-11-18 23:06:35 +00002412void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2413 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2414
2415 LocationSummary* locations =
2416 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2417
2418 switch (op->GetResultType()) {
2419 case Primitive::kPrimInt: {
2420 locations->SetInAt(0, Location::RequiresRegister());
2421 // The shift count needs to be in CL.
2422 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2423 locations->SetOut(Location::SameAsFirstInput());
2424 break;
2425 }
2426 case Primitive::kPrimLong: {
2427 locations->SetInAt(0, Location::RequiresRegister());
2428 // The shift count needs to be in CL.
2429 locations->SetInAt(1, Location::RegisterLocation(ECX));
2430 locations->SetOut(Location::SameAsFirstInput());
2431 break;
2432 }
2433 default:
2434 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2435 }
2436}
2437
2438void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2439 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2440
2441 LocationSummary* locations = op->GetLocations();
2442 Location first = locations->InAt(0);
2443 Location second = locations->InAt(1);
2444 DCHECK(first.Equals(locations->Out()));
2445
2446 switch (op->GetResultType()) {
2447 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002448 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002449 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002450 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002451 DCHECK_EQ(ECX, second_reg);
2452 if (op->IsShl()) {
2453 __ shll(first_reg, second_reg);
2454 } else if (op->IsShr()) {
2455 __ sarl(first_reg, second_reg);
2456 } else {
2457 __ shrl(first_reg, second_reg);
2458 }
2459 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002460 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002461 if (op->IsShl()) {
2462 __ shll(first_reg, imm);
2463 } else if (op->IsShr()) {
2464 __ sarl(first_reg, imm);
2465 } else {
2466 __ shrl(first_reg, imm);
2467 }
2468 }
2469 break;
2470 }
2471 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002472 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002473 DCHECK_EQ(ECX, second_reg);
2474 if (op->IsShl()) {
2475 GenerateShlLong(first, second_reg);
2476 } else if (op->IsShr()) {
2477 GenerateShrLong(first, second_reg);
2478 } else {
2479 GenerateUShrLong(first, second_reg);
2480 }
2481 break;
2482 }
2483 default:
2484 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2485 }
2486}
2487
2488void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2489 Label done;
2490 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2491 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2492 __ testl(shifter, Immediate(32));
2493 __ j(kEqual, &done);
2494 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2495 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2496 __ Bind(&done);
2497}
2498
2499void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2500 Label done;
2501 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2502 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2503 __ testl(shifter, Immediate(32));
2504 __ j(kEqual, &done);
2505 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2506 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2507 __ Bind(&done);
2508}
2509
2510void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2511 Label done;
2512 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2513 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2514 __ testl(shifter, Immediate(32));
2515 __ j(kEqual, &done);
2516 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2517 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2518 __ Bind(&done);
2519}
2520
2521void LocationsBuilderX86::VisitShl(HShl* shl) {
2522 HandleShift(shl);
2523}
2524
2525void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2526 HandleShift(shl);
2527}
2528
2529void LocationsBuilderX86::VisitShr(HShr* shr) {
2530 HandleShift(shr);
2531}
2532
2533void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2534 HandleShift(shr);
2535}
2536
2537void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2538 HandleShift(ushr);
2539}
2540
2541void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2542 HandleShift(ushr);
2543}
2544
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002545void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002546 LocationSummary* locations =
2547 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002548 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002549 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002550 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2551 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002552}
2553
2554void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2555 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002556 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002557 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002558
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002559 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002560
Nicolas Geoffray39468442014-09-02 15:17:15 +01002561 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002562 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002563}
2564
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002565void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2566 LocationSummary* locations =
2567 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2568 locations->SetOut(Location::RegisterLocation(EAX));
2569 InvokeRuntimeCallingConvention calling_convention;
2570 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002571 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2572 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002573}
2574
2575void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2576 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002577 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002578 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2579
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002580 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002581
2582 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2583 DCHECK(!codegen_->IsLeafMethod());
2584}
2585
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002586void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002587 LocationSummary* locations =
2588 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002589 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2590 if (location.IsStackSlot()) {
2591 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2592 } else if (location.IsDoubleStackSlot()) {
2593 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002594 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002595 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002596}
2597
2598void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002599 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002600}
2601
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002602void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002603 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002604 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002605 locations->SetInAt(0, Location::RequiresRegister());
2606 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002607}
2608
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002609void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2610 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002611 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002612 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002613 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002614 switch (not_->InputAt(0)->GetType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002615 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002616 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002617 break;
2618
2619 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002620 __ notl(out.AsRegisterPairLow<Register>());
2621 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002622 break;
2623
2624 default:
2625 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2626 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002627}
2628
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002629void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002630 LocationSummary* locations =
2631 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002632 switch (compare->InputAt(0)->GetType()) {
2633 case Primitive::kPrimLong: {
2634 locations->SetInAt(0, Location::RequiresRegister());
2635 // TODO: we set any here but we don't handle constants
2636 locations->SetInAt(1, Location::Any());
2637 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2638 break;
2639 }
2640 case Primitive::kPrimFloat:
2641 case Primitive::kPrimDouble: {
2642 locations->SetInAt(0, Location::RequiresFpuRegister());
2643 locations->SetInAt(1, Location::RequiresFpuRegister());
2644 locations->SetOut(Location::RequiresRegister());
2645 break;
2646 }
2647 default:
2648 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2649 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002650}
2651
2652void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002653 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002654 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002655 Location left = locations->InAt(0);
2656 Location right = locations->InAt(1);
2657
2658 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002659 switch (compare->InputAt(0)->GetType()) {
2660 case Primitive::kPrimLong: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002661 if (right.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002662 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002663 } else {
2664 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002665 __ cmpl(left.AsRegisterPairHigh<Register>(),
2666 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002667 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002668 __ j(kLess, &less); // Signed compare.
2669 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002670 if (right.IsRegisterPair()) {
2671 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002672 } else {
2673 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002674 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002675 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002676 break;
2677 }
2678 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002679 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002680 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
2681 break;
2682 }
2683 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002684 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002685 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002686 break;
2687 }
2688 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002689 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002690 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002691 __ movl(out, Immediate(0));
2692 __ j(kEqual, &done);
2693 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
2694
2695 __ Bind(&greater);
2696 __ movl(out, Immediate(1));
2697 __ jmp(&done);
2698
2699 __ Bind(&less);
2700 __ movl(out, Immediate(-1));
2701
2702 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002703}
2704
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002705void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002706 LocationSummary* locations =
2707 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002708 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2709 locations->SetInAt(i, Location::Any());
2710 }
2711 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002712}
2713
2714void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002715 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002716 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002717}
2718
Calin Juravle52c48962014-12-16 17:02:57 +00002719void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
2720 /*
2721 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2722 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2723 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2724 */
2725 switch (kind) {
2726 case MemBarrierKind::kAnyAny: {
2727 __ mfence();
2728 break;
2729 }
2730 case MemBarrierKind::kAnyStore:
2731 case MemBarrierKind::kLoadAny:
2732 case MemBarrierKind::kStoreStore: {
2733 // nop
2734 break;
2735 }
2736 default:
2737 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002738 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002739}
2740
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002741
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002742void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002743 Label is_null;
2744 __ testl(value, value);
2745 __ j(kEqual, &is_null);
2746 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2747 __ movl(temp, object);
2748 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002749 __ movb(Address(temp, card, TIMES_1, 0),
2750 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002751 __ Bind(&is_null);
2752}
2753
Calin Juravle52c48962014-12-16 17:02:57 +00002754void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
2755 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002756 LocationSummary* locations =
2757 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002758 locations->SetInAt(0, Location::RequiresRegister());
2759 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle52c48962014-12-16 17:02:57 +00002760
2761 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
2762 // Long values can be loaded atomically into an XMM using movsd.
2763 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
2764 // and then copy the XMM into the output 32bits at a time).
2765 locations->AddTemp(Location::RequiresFpuRegister());
2766 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002767}
2768
Calin Juravle52c48962014-12-16 17:02:57 +00002769void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
2770 const FieldInfo& field_info) {
2771 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002772
Calin Juravle52c48962014-12-16 17:02:57 +00002773 LocationSummary* locations = instruction->GetLocations();
2774 Register base = locations->InAt(0).AsRegister<Register>();
2775 Location out = locations->Out();
2776 bool is_volatile = field_info.IsVolatile();
2777 Primitive::Type field_type = field_info.GetFieldType();
2778 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2779
2780 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002781 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00002782 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002783 break;
2784 }
2785
2786 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002787 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002788 break;
2789 }
2790
2791 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00002792 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002793 break;
2794 }
2795
2796 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002797 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002798 break;
2799 }
2800
2801 case Primitive::kPrimInt:
2802 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002803 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002804 break;
2805 }
2806
2807 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00002808 if (is_volatile) {
2809 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2810 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002811 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002812 __ movd(out.AsRegisterPairLow<Register>(), temp);
2813 __ psrlq(temp, Immediate(32));
2814 __ movd(out.AsRegisterPairHigh<Register>(), temp);
2815 } else {
2816 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002817 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002818 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
2819 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002820 break;
2821 }
2822
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002823 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002824 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002825 break;
2826 }
2827
2828 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002829 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002830 break;
2831 }
2832
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002833 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00002834 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002835 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002836 }
Calin Juravle52c48962014-12-16 17:02:57 +00002837
Calin Juravle77520bc2015-01-12 18:45:46 +00002838 // Longs are handled in the switch.
2839 if (field_type != Primitive::kPrimLong) {
2840 codegen_->MaybeRecordImplicitNullCheck(instruction);
2841 }
2842
Calin Juravle52c48962014-12-16 17:02:57 +00002843 if (is_volatile) {
2844 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2845 }
2846}
2847
2848void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
2849 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2850
2851 LocationSummary* locations =
2852 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2853 locations->SetInAt(0, Location::RequiresRegister());
2854 bool is_volatile = field_info.IsVolatile();
2855 Primitive::Type field_type = field_info.GetFieldType();
2856 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2857 || (field_type == Primitive::kPrimByte);
2858
2859 // The register allocator does not support multiple
2860 // inputs that die at entry with one in a specific register.
2861 if (is_byte_type) {
2862 // Ensure the value is in a byte register.
2863 locations->SetInAt(1, Location::RegisterLocation(EAX));
2864 } else {
2865 locations->SetInAt(1, Location::RequiresRegister());
2866 }
2867 // Temporary registers for the write barrier.
2868 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2869 locations->AddTemp(Location::RequiresRegister());
2870 // Ensure the card is in a byte register.
2871 locations->AddTemp(Location::RegisterLocation(ECX));
2872 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
2873 // 64bits value can be atomically written to an address with movsd and an XMM register.
2874 // We need two XMM registers because there's no easier way to (bit) copy a register pair
2875 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
2876 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
2877 // isolated cases when we need this it isn't worth adding the extra complexity.
2878 locations->AddTemp(Location::RequiresFpuRegister());
2879 locations->AddTemp(Location::RequiresFpuRegister());
2880 }
2881}
2882
2883void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
2884 const FieldInfo& field_info) {
2885 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2886
2887 LocationSummary* locations = instruction->GetLocations();
2888 Register base = locations->InAt(0).AsRegister<Register>();
2889 Location value = locations->InAt(1);
2890 bool is_volatile = field_info.IsVolatile();
2891 Primitive::Type field_type = field_info.GetFieldType();
2892 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2893
2894 if (is_volatile) {
2895 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2896 }
2897
2898 switch (field_type) {
2899 case Primitive::kPrimBoolean:
2900 case Primitive::kPrimByte: {
2901 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
2902 break;
2903 }
2904
2905 case Primitive::kPrimShort:
2906 case Primitive::kPrimChar: {
2907 __ movw(Address(base, offset), value.AsRegister<Register>());
2908 break;
2909 }
2910
2911 case Primitive::kPrimInt:
2912 case Primitive::kPrimNot: {
2913 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00002914 break;
2915 }
2916
2917 case Primitive::kPrimLong: {
2918 if (is_volatile) {
2919 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2920 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
2921 __ movd(temp1, value.AsRegisterPairLow<Register>());
2922 __ movd(temp2, value.AsRegisterPairHigh<Register>());
2923 __ punpckldq(temp1, temp2);
2924 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00002925 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002926 } else {
2927 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00002928 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002929 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
2930 }
2931 break;
2932 }
2933
2934 case Primitive::kPrimFloat: {
2935 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
2936 break;
2937 }
2938
2939 case Primitive::kPrimDouble: {
2940 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
2941 break;
2942 }
2943
2944 case Primitive::kPrimVoid:
2945 LOG(FATAL) << "Unreachable type " << field_type;
2946 UNREACHABLE();
2947 }
2948
Calin Juravle77520bc2015-01-12 18:45:46 +00002949 // Longs are handled in the switch.
2950 if (field_type != Primitive::kPrimLong) {
2951 codegen_->MaybeRecordImplicitNullCheck(instruction);
2952 }
2953
2954 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2955 Register temp = locations->GetTemp(0).AsRegister<Register>();
2956 Register card = locations->GetTemp(1).AsRegister<Register>();
2957 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
2958 }
2959
Calin Juravle52c48962014-12-16 17:02:57 +00002960 if (is_volatile) {
2961 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2962 }
2963}
2964
2965void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2966 HandleFieldGet(instruction, instruction->GetFieldInfo());
2967}
2968
2969void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2970 HandleFieldGet(instruction, instruction->GetFieldInfo());
2971}
2972
2973void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2974 HandleFieldSet(instruction, instruction->GetFieldInfo());
2975}
2976
2977void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2978 HandleFieldSet(instruction, instruction->GetFieldInfo());
2979}
2980
2981void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2982 HandleFieldSet(instruction, instruction->GetFieldInfo());
2983}
2984
2985void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2986 HandleFieldSet(instruction, instruction->GetFieldInfo());
2987}
2988
2989void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2990 HandleFieldGet(instruction, instruction->GetFieldInfo());
2991}
2992
2993void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2994 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002995}
2996
2997void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002998 LocationSummary* locations =
2999 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003000 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3001 ? Location::RequiresRegister()
3002 : Location::Any();
3003 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003004 if (instruction->HasUses()) {
3005 locations->SetOut(Location::SameAsFirstInput());
3006 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003007}
3008
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003009void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003010 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3011 return;
3012 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003013 LocationSummary* locations = instruction->GetLocations();
3014 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003015
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003016 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3017 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3018}
3019
3020void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003021 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003022 codegen_->AddSlowPath(slow_path);
3023
3024 LocationSummary* locations = instruction->GetLocations();
3025 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003026
3027 if (obj.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003028 __ cmpl(obj.AsRegister<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003029 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003030 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003031 } else {
3032 DCHECK(obj.IsConstant()) << obj;
3033 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3034 __ jmp(slow_path->GetEntryLabel());
3035 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003036 }
3037 __ j(kEqual, slow_path->GetEntryLabel());
3038}
3039
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003040void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3041 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3042 GenerateImplicitNullCheck(instruction);
3043 } else {
3044 GenerateExplicitNullCheck(instruction);
3045 }
3046}
3047
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003048void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003049 LocationSummary* locations =
3050 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003051 locations->SetInAt(0, Location::RequiresRegister());
3052 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3053 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003054}
3055
3056void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3057 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003058 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003059 Location index = locations->InAt(1);
3060
Calin Juravle77520bc2015-01-12 18:45:46 +00003061 Primitive::Type type = instruction->GetType();
3062 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003063 case Primitive::kPrimBoolean: {
3064 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003065 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003066 if (index.IsConstant()) {
3067 __ movzxb(out, Address(obj,
3068 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3069 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003070 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003071 }
3072 break;
3073 }
3074
3075 case Primitive::kPrimByte: {
3076 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003077 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003078 if (index.IsConstant()) {
3079 __ movsxb(out, Address(obj,
3080 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3081 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003082 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003083 }
3084 break;
3085 }
3086
3087 case Primitive::kPrimShort: {
3088 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003089 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003090 if (index.IsConstant()) {
3091 __ movsxw(out, Address(obj,
3092 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3093 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003094 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003095 }
3096 break;
3097 }
3098
3099 case Primitive::kPrimChar: {
3100 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003101 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003102 if (index.IsConstant()) {
3103 __ movzxw(out, Address(obj,
3104 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3105 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003106 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003107 }
3108 break;
3109 }
3110
3111 case Primitive::kPrimInt:
3112 case Primitive::kPrimNot: {
3113 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003114 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003115 if (index.IsConstant()) {
3116 __ movl(out, Address(obj,
3117 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3118 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003119 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003120 }
3121 break;
3122 }
3123
3124 case Primitive::kPrimLong: {
3125 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003126 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003127 if (index.IsConstant()) {
3128 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003129 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003130 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003131 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003132 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003133 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003134 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003135 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003136 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003137 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003138 }
3139 break;
3140 }
3141
Mark Mendell7c8d0092015-01-26 11:21:33 -05003142 case Primitive::kPrimFloat: {
3143 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3144 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3145 if (index.IsConstant()) {
3146 __ movss(out, Address(obj,
3147 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3148 } else {
3149 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3150 }
3151 break;
3152 }
3153
3154 case Primitive::kPrimDouble: {
3155 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3156 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3157 if (index.IsConstant()) {
3158 __ movsd(out, Address(obj,
3159 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3160 } else {
3161 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3162 }
3163 break;
3164 }
3165
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003166 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003167 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003168 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003169 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003170
3171 if (type != Primitive::kPrimLong) {
3172 codegen_->MaybeRecordImplicitNullCheck(instruction);
3173 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003174}
3175
3176void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003177 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003178 bool needs_write_barrier =
3179 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3180
3181 DCHECK(kFollowsQuickABI);
3182 bool not_enough_registers = needs_write_barrier
3183 && !instruction->GetValue()->IsConstant()
3184 && !instruction->GetIndex()->IsConstant();
3185 bool needs_runtime_call = instruction->NeedsTypeCheck() || not_enough_registers;
3186
Nicolas Geoffray39468442014-09-02 15:17:15 +01003187 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3188 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003189 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003190
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003191 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003192 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003193 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3194 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3195 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003196 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003197 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3198 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003199 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003200 // In case of a byte operation, the register allocator does not support multiple
3201 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003202 locations->SetInAt(0, Location::RequiresRegister());
3203 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003204 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003205 // Ensure the value is in a byte register.
3206 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003207 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003208 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003209 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003210 // Temporary registers for the write barrier.
3211 if (needs_write_barrier) {
3212 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003213 // Ensure the card is in a byte register.
3214 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003215 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003216 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003217}
3218
3219void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3220 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003221 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003222 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003223 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003224 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003225 bool needs_runtime_call = locations->WillCall();
3226 bool needs_write_barrier =
3227 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003228
3229 switch (value_type) {
3230 case Primitive::kPrimBoolean:
3231 case Primitive::kPrimByte: {
3232 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003233 if (index.IsConstant()) {
3234 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003235 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003236 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003237 } else {
3238 __ movb(Address(obj, offset),
3239 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3240 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003241 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003242 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003243 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003244 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003245 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003246 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003247 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3248 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003249 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003250 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003251 break;
3252 }
3253
3254 case Primitive::kPrimShort:
3255 case Primitive::kPrimChar: {
3256 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003257 if (index.IsConstant()) {
3258 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003259 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003260 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003261 } else {
3262 __ movw(Address(obj, offset),
3263 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3264 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003265 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003266 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003267 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3268 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003269 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003270 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003271 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3272 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003273 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003274 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003275 break;
3276 }
3277
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003278 case Primitive::kPrimInt:
3279 case Primitive::kPrimNot: {
3280 if (!needs_runtime_call) {
3281 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3282 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003283 size_t offset =
3284 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003285 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003286 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003287 } else {
3288 DCHECK(value.IsConstant()) << value;
3289 __ movl(Address(obj, offset),
3290 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3291 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003292 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003293 DCHECK(index.IsRegister()) << index;
3294 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003295 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3296 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003297 } else {
3298 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003299 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003300 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3301 }
3302 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003303 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003304
3305 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003306 Register temp = locations->GetTemp(0).AsRegister<Register>();
3307 Register card = locations->GetTemp(1).AsRegister<Register>();
3308 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003309 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003310 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003311 DCHECK_EQ(value_type, Primitive::kPrimNot);
3312 DCHECK(!codegen_->IsLeafMethod());
3313 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3314 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003315 }
3316 break;
3317 }
3318
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003319 case Primitive::kPrimLong: {
3320 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003321 if (index.IsConstant()) {
3322 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003323 if (value.IsRegisterPair()) {
3324 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003325 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003326 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003327 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003328 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003329 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3330 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003331 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003332 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3333 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003334 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003335 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003336 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003337 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003338 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003339 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003340 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003341 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003342 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003343 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003344 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003345 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003346 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003347 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003348 Immediate(High32Bits(val)));
3349 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003350 }
3351 break;
3352 }
3353
Mark Mendell7c8d0092015-01-26 11:21:33 -05003354 case Primitive::kPrimFloat: {
3355 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3356 DCHECK(value.IsFpuRegister());
3357 if (index.IsConstant()) {
3358 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3359 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3360 } else {
3361 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3362 value.AsFpuRegister<XmmRegister>());
3363 }
3364 break;
3365 }
3366
3367 case Primitive::kPrimDouble: {
3368 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3369 DCHECK(value.IsFpuRegister());
3370 if (index.IsConstant()) {
3371 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3372 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3373 } else {
3374 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3375 value.AsFpuRegister<XmmRegister>());
3376 }
3377 break;
3378 }
3379
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003380 case Primitive::kPrimVoid:
3381 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003382 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003383 }
3384}
3385
3386void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3387 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003388 locations->SetInAt(0, Location::RequiresRegister());
3389 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003390 instruction->SetLocations(locations);
3391}
3392
3393void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3394 LocationSummary* locations = instruction->GetLocations();
3395 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003396 Register obj = locations->InAt(0).AsRegister<Register>();
3397 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003398 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003399 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003400}
3401
3402void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003403 LocationSummary* locations =
3404 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003405 locations->SetInAt(0, Location::RequiresRegister());
3406 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003407 if (instruction->HasUses()) {
3408 locations->SetOut(Location::SameAsFirstInput());
3409 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003410}
3411
3412void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3413 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003414 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01003415 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003416 codegen_->AddSlowPath(slow_path);
3417
Roland Levillain271ab9c2014-11-27 15:23:57 +00003418 Register index = locations->InAt(0).AsRegister<Register>();
3419 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003420
3421 __ cmpl(index, length);
3422 __ j(kAboveEqual, slow_path->GetEntryLabel());
3423}
3424
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003425void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3426 temp->SetLocations(nullptr);
3427}
3428
3429void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3430 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003431 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003432}
3433
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003434void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003435 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003436 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003437}
3438
3439void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003440 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3441}
3442
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003443void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3444 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3445}
3446
3447void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003448 HBasicBlock* block = instruction->GetBlock();
3449 if (block->GetLoopInformation() != nullptr) {
3450 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3451 // The back edge will generate the suspend check.
3452 return;
3453 }
3454 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3455 // The goto will generate the suspend check.
3456 return;
3457 }
3458 GenerateSuspendCheck(instruction, nullptr);
3459}
3460
3461void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3462 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003463 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003464 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003465 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003466 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003467 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003468 if (successor == nullptr) {
3469 __ j(kNotEqual, slow_path->GetEntryLabel());
3470 __ Bind(slow_path->GetReturnLabel());
3471 } else {
3472 __ j(kEqual, codegen_->GetLabelOf(successor));
3473 __ jmp(slow_path->GetEntryLabel());
3474 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003475}
3476
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003477X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3478 return codegen_->GetAssembler();
3479}
3480
Mark Mendell7c8d0092015-01-26 11:21:33 -05003481void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003482 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003483 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003484 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003485 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05003486 __ movl(temp_reg, Address(ESP, src + stack_offset));
3487 __ movl(Address(ESP, dst + stack_offset), temp_reg);
3488}
3489
3490void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
3491 ScratchRegisterScope ensure_scratch(
3492 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3493 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3494 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3495 __ movl(temp_reg, Address(ESP, src + stack_offset));
3496 __ movl(Address(ESP, dst + stack_offset), temp_reg);
3497 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
3498 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003499}
3500
3501void ParallelMoveResolverX86::EmitMove(size_t index) {
3502 MoveOperands* move = moves_.Get(index);
3503 Location source = move->GetSource();
3504 Location destination = move->GetDestination();
3505
3506 if (source.IsRegister()) {
3507 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003508 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003509 } else {
3510 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003511 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003512 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003513 } else if (source.IsFpuRegister()) {
3514 if (destination.IsFpuRegister()) {
3515 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3516 } else if (destination.IsStackSlot()) {
3517 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3518 } else {
3519 DCHECK(destination.IsDoubleStackSlot());
3520 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3521 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003522 } else if (source.IsStackSlot()) {
3523 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003524 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003525 } else if (destination.IsFpuRegister()) {
3526 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003527 } else {
3528 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003529 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
3530 }
3531 } else if (source.IsDoubleStackSlot()) {
3532 if (destination.IsFpuRegister()) {
3533 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
3534 } else {
3535 DCHECK(destination.IsDoubleStackSlot()) << destination;
3536 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003537 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003538 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003539 HConstant* constant = source.GetConstant();
3540 if (constant->IsIntConstant()) {
3541 Immediate imm(constant->AsIntConstant()->GetValue());
3542 if (destination.IsRegister()) {
3543 __ movl(destination.AsRegister<Register>(), imm);
3544 } else {
3545 DCHECK(destination.IsStackSlot()) << destination;
3546 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3547 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003548 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003549 DCHECK(constant->IsFloatConstant());
3550 float value = constant->AsFloatConstant()->GetValue();
3551 Immediate imm(bit_cast<float, int32_t>(value));
3552 if (destination.IsFpuRegister()) {
3553 ScratchRegisterScope ensure_scratch(
3554 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3555 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
3556 __ movl(temp, imm);
3557 __ movd(destination.AsFpuRegister<XmmRegister>(), temp);
3558 } else {
3559 DCHECK(destination.IsStackSlot()) << destination;
3560 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3561 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003562 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003563 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003564 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003565 }
3566}
3567
3568void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003569 Register suggested_scratch = reg == EAX ? EBX : EAX;
3570 ScratchRegisterScope ensure_scratch(
3571 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3572
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003573 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3574 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
3575 __ movl(Address(ESP, mem + stack_offset), reg);
3576 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
3577}
3578
Mark Mendell7c8d0092015-01-26 11:21:33 -05003579void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
3580 ScratchRegisterScope ensure_scratch(
3581 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3582
3583 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3584 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3585 __ movl(temp_reg, Address(ESP, mem + stack_offset));
3586 __ movss(Address(ESP, mem + stack_offset), reg);
3587 __ movd(reg, temp_reg);
3588}
3589
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003590void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
3591 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003592 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3593
3594 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003595 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003596 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3597
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003598 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
3599 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
3600 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
3601 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
3602 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
3603 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
3604}
3605
3606void ParallelMoveResolverX86::EmitSwap(size_t index) {
3607 MoveOperands* move = moves_.Get(index);
3608 Location source = move->GetSource();
3609 Location destination = move->GetDestination();
3610
3611 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003612 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003613 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003614 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003615 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003616 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003617 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3618 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003619 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
3620 // Use XOR Swap algorithm to avoid a temporary.
3621 DCHECK_NE(source.reg(), destination.reg());
3622 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3623 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3624 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3625 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
3626 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
3627 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
3628 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003629 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003630 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003631 }
3632}
3633
3634void ParallelMoveResolverX86::SpillScratch(int reg) {
3635 __ pushl(static_cast<Register>(reg));
3636}
3637
3638void ParallelMoveResolverX86::RestoreScratch(int reg) {
3639 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003640}
3641
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003642void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003643 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3644 ? LocationSummary::kCallOnSlowPath
3645 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003646 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003647 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003648 locations->SetOut(Location::RequiresRegister());
3649}
3650
3651void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003652 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003653 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003654 DCHECK(!cls->CanCallRuntime());
3655 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003656 codegen_->LoadCurrentMethod(out);
3657 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3658 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003659 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003660 codegen_->LoadCurrentMethod(out);
3661 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3662 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003663
3664 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3665 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3666 codegen_->AddSlowPath(slow_path);
3667 __ testl(out, out);
3668 __ j(kEqual, slow_path->GetEntryLabel());
3669 if (cls->MustGenerateClinitCheck()) {
3670 GenerateClassInitializationCheck(slow_path, out);
3671 } else {
3672 __ Bind(slow_path->GetExitLabel());
3673 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003674 }
3675}
3676
3677void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
3678 LocationSummary* locations =
3679 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3680 locations->SetInAt(0, Location::RequiresRegister());
3681 if (check->HasUses()) {
3682 locations->SetOut(Location::SameAsFirstInput());
3683 }
3684}
3685
3686void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003687 // We assume the class to not be null.
3688 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3689 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003690 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003691 GenerateClassInitializationCheck(slow_path,
3692 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003693}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003694
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003695void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
3696 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003697 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3698 Immediate(mirror::Class::kStatusInitialized));
3699 __ j(kLess, slow_path->GetEntryLabel());
3700 __ Bind(slow_path->GetExitLabel());
3701 // No need for memory fence, thanks to the X86 memory model.
3702}
3703
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003704void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
3705 LocationSummary* locations =
3706 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3707 locations->SetOut(Location::RequiresRegister());
3708}
3709
3710void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
3711 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
3712 codegen_->AddSlowPath(slow_path);
3713
Roland Levillain271ab9c2014-11-27 15:23:57 +00003714 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003715 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08003716 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3717 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003718 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3719 __ testl(out, out);
3720 __ j(kEqual, slow_path->GetEntryLabel());
3721 __ Bind(slow_path->GetExitLabel());
3722}
3723
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003724void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
3725 LocationSummary* locations =
3726 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3727 locations->SetOut(Location::RequiresRegister());
3728}
3729
3730void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
3731 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003732 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003733 __ fs()->movl(address, Immediate(0));
3734}
3735
3736void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
3737 LocationSummary* locations =
3738 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3739 InvokeRuntimeCallingConvention calling_convention;
3740 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3741}
3742
3743void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
3744 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
3745 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3746}
3747
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003748void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003749 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3750 ? LocationSummary::kNoCall
3751 : LocationSummary::kCallOnSlowPath;
3752 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3753 locations->SetInAt(0, Location::RequiresRegister());
3754 locations->SetInAt(1, Location::Any());
3755 locations->SetOut(Location::RequiresRegister());
3756}
3757
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003758void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003759 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003760 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003761 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003762 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003763 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3764 Label done, zero;
3765 SlowPathCodeX86* slow_path = nullptr;
3766
3767 // Return 0 if `obj` is null.
3768 // TODO: avoid this check if we know obj is not null.
3769 __ testl(obj, obj);
3770 __ j(kEqual, &zero);
3771 __ movl(out, Address(obj, class_offset));
3772 // Compare the class of `obj` with `cls`.
3773 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003774 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003775 } else {
3776 DCHECK(cls.IsStackSlot()) << cls;
3777 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
3778 }
3779
3780 if (instruction->IsClassFinal()) {
3781 // Classes must be equal for the instanceof to succeed.
3782 __ j(kNotEqual, &zero);
3783 __ movl(out, Immediate(1));
3784 __ jmp(&done);
3785 } else {
3786 // If the classes are not equal, we go into a slow path.
3787 DCHECK(locations->OnlyCallsOnSlowPath());
3788 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003789 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003790 codegen_->AddSlowPath(slow_path);
3791 __ j(kNotEqual, slow_path->GetEntryLabel());
3792 __ movl(out, Immediate(1));
3793 __ jmp(&done);
3794 }
3795 __ Bind(&zero);
3796 __ movl(out, Immediate(0));
3797 if (slow_path != nullptr) {
3798 __ Bind(slow_path->GetExitLabel());
3799 }
3800 __ Bind(&done);
3801}
3802
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003803void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
3804 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3805 instruction, LocationSummary::kCallOnSlowPath);
3806 locations->SetInAt(0, Location::RequiresRegister());
3807 locations->SetInAt(1, Location::Any());
3808 locations->AddTemp(Location::RequiresRegister());
3809}
3810
3811void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
3812 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003813 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003814 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003815 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003816 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3817 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
3818 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3819 codegen_->AddSlowPath(slow_path);
3820
3821 // TODO: avoid this check if we know obj is not null.
3822 __ testl(obj, obj);
3823 __ j(kEqual, slow_path->GetExitLabel());
3824 __ movl(temp, Address(obj, class_offset));
3825
3826 // Compare the class of `obj` with `cls`.
3827 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003828 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003829 } else {
3830 DCHECK(cls.IsStackSlot()) << cls;
3831 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
3832 }
3833
3834 __ j(kNotEqual, slow_path->GetEntryLabel());
3835 __ Bind(slow_path->GetExitLabel());
3836}
3837
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003838void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3839 LocationSummary* locations =
3840 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3841 InvokeRuntimeCallingConvention calling_convention;
3842 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3843}
3844
3845void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3846 __ fs()->call(Address::Absolute(instruction->IsEnter()
3847 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
3848 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
3849 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3850}
3851
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003852void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3853void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3854void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3855
3856void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3857 LocationSummary* locations =
3858 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3859 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3860 || instruction->GetResultType() == Primitive::kPrimLong);
3861 locations->SetInAt(0, Location::RequiresRegister());
3862 locations->SetInAt(1, Location::Any());
3863 locations->SetOut(Location::SameAsFirstInput());
3864}
3865
3866void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
3867 HandleBitwiseOperation(instruction);
3868}
3869
3870void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
3871 HandleBitwiseOperation(instruction);
3872}
3873
3874void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
3875 HandleBitwiseOperation(instruction);
3876}
3877
3878void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3879 LocationSummary* locations = instruction->GetLocations();
3880 Location first = locations->InAt(0);
3881 Location second = locations->InAt(1);
3882 DCHECK(first.Equals(locations->Out()));
3883
3884 if (instruction->GetResultType() == Primitive::kPrimInt) {
3885 if (second.IsRegister()) {
3886 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003887 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003888 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003889 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003890 } else {
3891 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003892 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003893 }
3894 } else if (second.IsConstant()) {
3895 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003896 __ andl(first.AsRegister<Register>(),
3897 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003898 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003899 __ orl(first.AsRegister<Register>(),
3900 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003901 } else {
3902 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00003903 __ xorl(first.AsRegister<Register>(),
3904 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003905 }
3906 } else {
3907 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003908 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003909 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003910 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003911 } else {
3912 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003913 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003914 }
3915 }
3916 } else {
3917 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3918 if (second.IsRegisterPair()) {
3919 if (instruction->IsAnd()) {
3920 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3921 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3922 } else if (instruction->IsOr()) {
3923 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3924 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3925 } else {
3926 DCHECK(instruction->IsXor());
3927 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3928 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3929 }
3930 } else {
3931 if (instruction->IsAnd()) {
3932 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3933 __ andl(first.AsRegisterPairHigh<Register>(),
3934 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3935 } else if (instruction->IsOr()) {
3936 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3937 __ orl(first.AsRegisterPairHigh<Register>(),
3938 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3939 } else {
3940 DCHECK(instruction->IsXor());
3941 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3942 __ xorl(first.AsRegisterPairHigh<Register>(),
3943 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3944 }
3945 }
3946 }
3947}
3948
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003949} // namespace x86
3950} // namespace art