blob: 7b35cfded8dea1a80b6e3a3dbb18e7b3989ad1f8 [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 Geoffrayc0572a42015-02-06 14:35:25 +0000473 if (!HasEmptyFrame()) {
474 __ subl(ESP, Immediate(GetFrameSize() - FrameEntrySpillSize()));
475 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
476 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000477}
478
479void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000480 if (!HasEmptyFrame()) {
481 __ addl(ESP, Immediate(GetFrameSize() - FrameEntrySpillSize()));
482 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000483}
484
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100485void CodeGeneratorX86::Bind(HBasicBlock* block) {
486 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000487}
488
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100489void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000490 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100491 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000492}
493
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100494Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
495 switch (load->GetType()) {
496 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100497 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100498 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
499 break;
500
501 case Primitive::kPrimInt:
502 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100503 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100504 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100505
506 case Primitive::kPrimBoolean:
507 case Primitive::kPrimByte:
508 case Primitive::kPrimChar:
509 case Primitive::kPrimShort:
510 case Primitive::kPrimVoid:
511 LOG(FATAL) << "Unexpected type " << load->GetType();
512 }
513
514 LOG(FATAL) << "Unreachable";
515 return Location();
516}
517
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100518Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
519 switch (type) {
520 case Primitive::kPrimBoolean:
521 case Primitive::kPrimByte:
522 case Primitive::kPrimChar:
523 case Primitive::kPrimShort:
524 case Primitive::kPrimInt:
525 case Primitive::kPrimNot: {
526 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000527 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100528 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100529 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100530 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000531 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100532 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100533 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100534
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000535 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100536 uint32_t index = gp_index_;
537 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000538 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100539 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100540 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
541 calling_convention.GetRegisterPairAt(index));
542 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100543 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000544 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
545 }
546 }
547
548 case Primitive::kPrimFloat: {
549 uint32_t index = fp_index_++;
550 stack_index_++;
551 if (index < calling_convention.GetNumberOfFpuRegisters()) {
552 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
553 } else {
554 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
555 }
556 }
557
558 case Primitive::kPrimDouble: {
559 uint32_t index = fp_index_++;
560 stack_index_ += 2;
561 if (index < calling_convention.GetNumberOfFpuRegisters()) {
562 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
563 } else {
564 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100565 }
566 }
567
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100568 case Primitive::kPrimVoid:
569 LOG(FATAL) << "Unexpected parameter type " << type;
570 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100571 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100572 return Location();
573}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100574
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100575void CodeGeneratorX86::Move32(Location destination, Location source) {
576 if (source.Equals(destination)) {
577 return;
578 }
579 if (destination.IsRegister()) {
580 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000581 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100582 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000583 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100584 } else {
585 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000586 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100587 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100588 } else if (destination.IsFpuRegister()) {
589 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000590 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100591 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000592 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100593 } else {
594 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000595 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100596 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100597 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000598 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100599 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000600 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100601 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000602 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500603 } else if (source.IsConstant()) {
604 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000605 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500606 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100607 } else {
608 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100609 __ pushl(Address(ESP, source.GetStackIndex()));
610 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100611 }
612 }
613}
614
615void CodeGeneratorX86::Move64(Location destination, Location source) {
616 if (source.Equals(destination)) {
617 return;
618 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100619 if (destination.IsRegisterPair()) {
620 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000621 EmitParallelMoves(
622 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
623 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
624 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
625 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100626 } else if (source.IsFpuRegister()) {
627 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100628 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000629 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100630 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100631 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
632 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100633 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
634 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100635 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500636 if (source.IsFpuRegister()) {
637 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
638 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000639 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100640 } else {
641 LOG(FATAL) << "Unimplemented";
642 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100643 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000644 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100645 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000646 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100647 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100648 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100649 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100650 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000651 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100652 } else {
653 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000654 EmitParallelMoves(
655 Location::StackSlot(source.GetStackIndex()),
656 Location::StackSlot(destination.GetStackIndex()),
657 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
658 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100659 }
660 }
661}
662
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100663void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000664 LocationSummary* locations = instruction->GetLocations();
665 if (locations != nullptr && locations->Out().Equals(location)) {
666 return;
667 }
668
669 if (locations != nullptr && locations->Out().IsConstant()) {
670 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000671 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
672 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000673 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000674 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000675 } else if (location.IsStackSlot()) {
676 __ movl(Address(ESP, location.GetStackIndex()), imm);
677 } else {
678 DCHECK(location.IsConstant());
679 DCHECK_EQ(location.GetConstant(), const_to_move);
680 }
681 } else if (const_to_move->IsLongConstant()) {
682 int64_t value = const_to_move->AsLongConstant()->GetValue();
683 if (location.IsRegisterPair()) {
684 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
685 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
686 } else if (location.IsDoubleStackSlot()) {
687 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000688 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
689 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000690 } else {
691 DCHECK(location.IsConstant());
692 DCHECK_EQ(location.GetConstant(), instruction);
693 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100694 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000695 } else if (instruction->IsTemporary()) {
696 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000697 if (temp_location.IsStackSlot()) {
698 Move32(location, temp_location);
699 } else {
700 DCHECK(temp_location.IsDoubleStackSlot());
701 Move64(location, temp_location);
702 }
Roland Levillain476df552014-10-09 17:51:36 +0100703 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100704 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100705 switch (instruction->GetType()) {
706 case Primitive::kPrimBoolean:
707 case Primitive::kPrimByte:
708 case Primitive::kPrimChar:
709 case Primitive::kPrimShort:
710 case Primitive::kPrimInt:
711 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100712 case Primitive::kPrimFloat:
713 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100714 break;
715
716 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100717 case Primitive::kPrimDouble:
718 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100719 break;
720
721 default:
722 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
723 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000724 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100725 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100726 switch (instruction->GetType()) {
727 case Primitive::kPrimBoolean:
728 case Primitive::kPrimByte:
729 case Primitive::kPrimChar:
730 case Primitive::kPrimShort:
731 case Primitive::kPrimInt:
732 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100733 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000734 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100735 break;
736
737 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100738 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000739 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100740 break;
741
742 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100743 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100744 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000745 }
746}
747
748void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000749 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000750}
751
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000752void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000753 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100754 DCHECK(!successor->IsExitBlock());
755
756 HBasicBlock* block = got->GetBlock();
757 HInstruction* previous = got->GetPrevious();
758
759 HLoopInformation* info = block->GetLoopInformation();
760 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
761 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
762 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
763 return;
764 }
765
766 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
767 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
768 }
769 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000770 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000771 }
772}
773
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000774void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000775 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000776}
777
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000778void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700779 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000780 if (kIsDebugBuild) {
781 __ Comment("Unreachable");
782 __ int3();
783 }
784}
785
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000786void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100787 LocationSummary* locations =
788 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100789 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100790 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100791 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100792 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000793}
794
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000795void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700796 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100797 if (cond->IsIntConstant()) {
798 // Constant condition, statically compared against 1.
799 int32_t cond_value = cond->AsIntConstant()->GetValue();
800 if (cond_value == 1) {
801 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
802 if_instr->IfTrueSuccessor())) {
803 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100804 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100805 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100806 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100807 DCHECK_EQ(cond_value, 0);
808 }
809 } else {
810 bool materialized =
811 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
812 // Moves do not affect the eflags register, so if the condition is
813 // evaluated just before the if, we don't need to evaluate it
814 // again.
815 bool eflags_set = cond->IsCondition()
816 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
817 if (materialized) {
818 if (!eflags_set) {
819 // Materialized condition, compare against 0.
820 Location lhs = if_instr->GetLocations()->InAt(0);
821 if (lhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000822 __ cmpl(lhs.AsRegister<Register>(), Immediate(0));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100823 } else {
824 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
825 }
826 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
827 } else {
828 __ j(X86Condition(cond->AsCondition()->GetCondition()),
829 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
830 }
831 } else {
832 Location lhs = cond->GetLocations()->InAt(0);
833 Location rhs = cond->GetLocations()->InAt(1);
834 // LHS is guaranteed to be in a register (see
835 // LocationsBuilderX86::VisitCondition).
836 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000837 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100838 } else if (rhs.IsConstant()) {
839 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
840 Immediate imm(instruction->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000841 __ cmpl(lhs.AsRegister<Register>(), imm);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100842 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000843 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100844 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100845 __ j(X86Condition(cond->AsCondition()->GetCondition()),
846 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700847 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100848 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100849 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
850 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700851 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000852 }
853}
854
855void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000856 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000857}
858
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000859void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
860 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000861}
862
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000863void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100864 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000865}
866
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000867void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100868 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700869 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000870}
871
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100872void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100873 LocationSummary* locations =
874 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100875 switch (store->InputAt(1)->GetType()) {
876 case Primitive::kPrimBoolean:
877 case Primitive::kPrimByte:
878 case Primitive::kPrimChar:
879 case Primitive::kPrimShort:
880 case Primitive::kPrimInt:
881 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100882 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100883 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
884 break;
885
886 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100887 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100888 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
889 break;
890
891 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100892 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100893 }
894 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000895}
896
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000897void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700898 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000899}
900
Dave Allison20dfc792014-06-16 20:44:29 -0700901void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100902 LocationSummary* locations =
903 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100904 locations->SetInAt(0, Location::RequiresRegister());
905 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100906 if (comp->NeedsMaterialization()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000907 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100908 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000909}
910
Dave Allison20dfc792014-06-16 20:44:29 -0700911void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
912 if (comp->NeedsMaterialization()) {
913 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000914 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100915 // Clear register: setcc only sets the low byte.
916 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700917 if (locations->InAt(1).IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000918 __ cmpl(locations->InAt(0).AsRegister<Register>(),
919 locations->InAt(1).AsRegister<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100920 } else if (locations->InAt(1).IsConstant()) {
921 HConstant* instruction = locations->InAt(1).GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000922 Immediate imm(CodeGenerator::GetInt32ValueOf(instruction));
Roland Levillain271ab9c2014-11-27 15:23:57 +0000923 __ cmpl(locations->InAt(0).AsRegister<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700924 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000925 __ cmpl(locations->InAt(0).AsRegister<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700926 Address(ESP, locations->InAt(1).GetStackIndex()));
927 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000928 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100929 }
Dave Allison20dfc792014-06-16 20:44:29 -0700930}
931
932void LocationsBuilderX86::VisitEqual(HEqual* comp) {
933 VisitCondition(comp);
934}
935
936void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
937 VisitCondition(comp);
938}
939
940void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
941 VisitCondition(comp);
942}
943
944void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
945 VisitCondition(comp);
946}
947
948void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
949 VisitCondition(comp);
950}
951
952void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
953 VisitCondition(comp);
954}
955
956void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
957 VisitCondition(comp);
958}
959
960void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
961 VisitCondition(comp);
962}
963
964void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
965 VisitCondition(comp);
966}
967
968void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
969 VisitCondition(comp);
970}
971
972void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
973 VisitCondition(comp);
974}
975
976void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
977 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000978}
979
980void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100981 LocationSummary* locations =
982 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100983 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000984}
985
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000986void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100987 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700988 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000989}
990
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000991void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
992 LocationSummary* locations =
993 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
994 locations->SetOut(Location::ConstantLocation(constant));
995}
996
997void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
998 // Will be generated at use site.
999 UNUSED(constant);
1000}
1001
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001002void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001003 LocationSummary* locations =
1004 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001005 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001006}
1007
1008void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1009 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001010 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001011}
1012
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001013void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1014 LocationSummary* locations =
1015 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1016 locations->SetOut(Location::ConstantLocation(constant));
1017}
1018
1019void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1020 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001021 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001022}
1023
1024void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1025 LocationSummary* locations =
1026 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1027 locations->SetOut(Location::ConstantLocation(constant));
1028}
1029
1030void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1031 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001032 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001033}
1034
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001035void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001036 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001037}
1038
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001039void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001040 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001041 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001042 __ ret();
1043}
1044
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001045void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001046 LocationSummary* locations =
1047 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001048 switch (ret->InputAt(0)->GetType()) {
1049 case Primitive::kPrimBoolean:
1050 case Primitive::kPrimByte:
1051 case Primitive::kPrimChar:
1052 case Primitive::kPrimShort:
1053 case Primitive::kPrimInt:
1054 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001055 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001056 break;
1057
1058 case Primitive::kPrimLong:
1059 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001060 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001061 break;
1062
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001063 case Primitive::kPrimFloat:
1064 case Primitive::kPrimDouble:
1065 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001066 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001067 break;
1068
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001069 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001070 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001071 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001072}
1073
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001074void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001075 if (kIsDebugBuild) {
1076 switch (ret->InputAt(0)->GetType()) {
1077 case Primitive::kPrimBoolean:
1078 case Primitive::kPrimByte:
1079 case Primitive::kPrimChar:
1080 case Primitive::kPrimShort:
1081 case Primitive::kPrimInt:
1082 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001083 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001084 break;
1085
1086 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001087 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1088 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001089 break;
1090
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001091 case Primitive::kPrimFloat:
1092 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001093 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001094 break;
1095
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001096 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001097 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001098 }
1099 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001100 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001101 __ ret();
1102}
1103
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001104void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001105 HandleInvoke(invoke);
1106}
1107
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001108void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001109 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001110
1111 // TODO: Implement all kinds of calls:
1112 // 1) boot -> boot
1113 // 2) app -> boot
1114 // 3) app -> app
1115 //
1116 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1117
1118 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001119 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001120 if (!invoke->IsRecursive()) {
1121 // temp = temp->dex_cache_resolved_methods_;
1122 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
1123 // temp = temp[index_in_cache]
1124 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
1125 // (temp + offset_of_quick_compiled_code)()
1126 __ call(Address(
1127 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
1128 } else {
1129 __ call(codegen_->GetFrameEntryLabel());
1130 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001131
1132 DCHECK(!codegen_->IsLeafMethod());
1133 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1134}
1135
1136void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1137 HandleInvoke(invoke);
1138}
1139
1140void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001141 LocationSummary* locations =
1142 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001143 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001144
1145 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001146 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001147 HInstruction* input = invoke->InputAt(i);
1148 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1149 }
1150
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001151 switch (invoke->GetType()) {
1152 case Primitive::kPrimBoolean:
1153 case Primitive::kPrimByte:
1154 case Primitive::kPrimChar:
1155 case Primitive::kPrimShort:
1156 case Primitive::kPrimInt:
1157 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001158 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001159 break;
1160
1161 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001162 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001163 break;
1164
1165 case Primitive::kPrimVoid:
1166 break;
1167
1168 case Primitive::kPrimDouble:
1169 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001170 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001171 break;
1172 }
1173
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001174 invoke->SetLocations(locations);
1175}
1176
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001177void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001178 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001179 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1180 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1181 LocationSummary* locations = invoke->GetLocations();
1182 Location receiver = locations->InAt(0);
1183 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1184 // temp = object->GetClass();
1185 if (receiver.IsStackSlot()) {
1186 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1187 __ movl(temp, Address(temp, class_offset));
1188 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001189 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001190 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001191 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001192 // temp = temp->GetMethodAt(method_offset);
1193 __ movl(temp, Address(temp, method_offset));
1194 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001195 __ call(Address(
1196 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001197
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001198 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001199 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001200}
1201
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001202void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1203 HandleInvoke(invoke);
1204 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001205 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001206}
1207
1208void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1209 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001210 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001211 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1212 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1213 LocationSummary* locations = invoke->GetLocations();
1214 Location receiver = locations->InAt(0);
1215 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1216
1217 // Set the hidden argument.
1218 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001219 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001220
1221 // temp = object->GetClass();
1222 if (receiver.IsStackSlot()) {
1223 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1224 __ movl(temp, Address(temp, class_offset));
1225 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001226 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001227 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001228 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001229 // temp = temp->GetImtEntryAt(method_offset);
1230 __ movl(temp, Address(temp, method_offset));
1231 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001232 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001233 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001234
1235 DCHECK(!codegen_->IsLeafMethod());
1236 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1237}
1238
Roland Levillain88cb1752014-10-20 16:36:47 +01001239void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1240 LocationSummary* locations =
1241 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1242 switch (neg->GetResultType()) {
1243 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001244 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001245 locations->SetInAt(0, Location::RequiresRegister());
1246 locations->SetOut(Location::SameAsFirstInput());
1247 break;
1248
Roland Levillain88cb1752014-10-20 16:36:47 +01001249 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001250 locations->SetInAt(0, Location::RequiresFpuRegister());
1251 locations->SetOut(Location::SameAsFirstInput());
1252 locations->AddTemp(Location::RequiresRegister());
1253 locations->AddTemp(Location::RequiresFpuRegister());
1254 break;
1255
Roland Levillain88cb1752014-10-20 16:36:47 +01001256 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001257 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001258 locations->SetOut(Location::SameAsFirstInput());
1259 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001260 break;
1261
1262 default:
1263 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1264 }
1265}
1266
1267void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1268 LocationSummary* locations = neg->GetLocations();
1269 Location out = locations->Out();
1270 Location in = locations->InAt(0);
1271 switch (neg->GetResultType()) {
1272 case Primitive::kPrimInt:
1273 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001274 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001275 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001276 break;
1277
1278 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001279 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001280 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001281 __ negl(out.AsRegisterPairLow<Register>());
1282 // Negation is similar to subtraction from zero. The least
1283 // significant byte triggers a borrow when it is different from
1284 // zero; to take it into account, add 1 to the most significant
1285 // byte if the carry flag (CF) is set to 1 after the first NEGL
1286 // operation.
1287 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1288 __ negl(out.AsRegisterPairHigh<Register>());
1289 break;
1290
Roland Levillain5368c212014-11-27 15:03:41 +00001291 case Primitive::kPrimFloat: {
1292 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001293 Register constant = locations->GetTemp(0).AsRegister<Register>();
1294 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001295 // Implement float negation with an exclusive or with value
1296 // 0x80000000 (mask for bit 31, representing the sign of a
1297 // single-precision floating-point number).
1298 __ movl(constant, Immediate(INT32_C(0x80000000)));
1299 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001300 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001301 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001302 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001303
Roland Levillain5368c212014-11-27 15:03:41 +00001304 case Primitive::kPrimDouble: {
1305 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001306 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001307 // Implement double negation with an exclusive or with value
1308 // 0x8000000000000000 (mask for bit 63, representing the sign of
1309 // a double-precision floating-point number).
1310 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001311 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001312 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001313 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001314
1315 default:
1316 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1317 }
1318}
1319
Roland Levillaindff1f282014-11-05 14:15:05 +00001320void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001321 Primitive::Type result_type = conversion->GetResultType();
1322 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001323 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001324
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001325 // The float-to-long and double-to-long type conversions rely on a
1326 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001327 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001328 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1329 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001330 ? LocationSummary::kCall
1331 : LocationSummary::kNoCall;
1332 LocationSummary* locations =
1333 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1334
Roland Levillaindff1f282014-11-05 14:15:05 +00001335 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001336 case Primitive::kPrimByte:
1337 switch (input_type) {
1338 case Primitive::kPrimShort:
1339 case Primitive::kPrimInt:
1340 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001341 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001342 locations->SetInAt(0, Location::Any());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001343 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1344 break;
1345
1346 default:
1347 LOG(FATAL) << "Unexpected type conversion from " << input_type
1348 << " to " << result_type;
1349 }
1350 break;
1351
Roland Levillain01a8d712014-11-14 16:27:39 +00001352 case Primitive::kPrimShort:
1353 switch (input_type) {
1354 case Primitive::kPrimByte:
1355 case Primitive::kPrimInt:
1356 case Primitive::kPrimChar:
1357 // Processing a Dex `int-to-short' instruction.
1358 locations->SetInAt(0, Location::Any());
1359 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1360 break;
1361
1362 default:
1363 LOG(FATAL) << "Unexpected type conversion from " << input_type
1364 << " to " << result_type;
1365 }
1366 break;
1367
Roland Levillain946e1432014-11-11 17:35:19 +00001368 case Primitive::kPrimInt:
1369 switch (input_type) {
1370 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001371 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001372 locations->SetInAt(0, Location::Any());
1373 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1374 break;
1375
1376 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001377 // Processing a Dex `float-to-int' instruction.
1378 locations->SetInAt(0, Location::RequiresFpuRegister());
1379 locations->SetOut(Location::RequiresRegister());
1380 locations->AddTemp(Location::RequiresFpuRegister());
1381 break;
1382
Roland Levillain946e1432014-11-11 17:35:19 +00001383 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001384 // Processing a Dex `double-to-int' instruction.
1385 locations->SetInAt(0, Location::RequiresFpuRegister());
1386 locations->SetOut(Location::RequiresRegister());
1387 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001388 break;
1389
1390 default:
1391 LOG(FATAL) << "Unexpected type conversion from " << input_type
1392 << " to " << result_type;
1393 }
1394 break;
1395
Roland Levillaindff1f282014-11-05 14:15:05 +00001396 case Primitive::kPrimLong:
1397 switch (input_type) {
1398 case Primitive::kPrimByte:
1399 case Primitive::kPrimShort:
1400 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001401 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001402 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001403 locations->SetInAt(0, Location::RegisterLocation(EAX));
1404 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1405 break;
1406
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001407 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001408 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001409 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001410 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001411 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1412 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1413
Vladimir Marko949c91f2015-01-27 10:48:44 +00001414 // The runtime helper puts the result in EAX, EDX.
1415 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001416 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001417 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001418
1419 default:
1420 LOG(FATAL) << "Unexpected type conversion from " << input_type
1421 << " to " << result_type;
1422 }
1423 break;
1424
Roland Levillain981e4542014-11-14 11:47:14 +00001425 case Primitive::kPrimChar:
1426 switch (input_type) {
1427 case Primitive::kPrimByte:
1428 case Primitive::kPrimShort:
1429 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001430 // Processing a Dex `int-to-char' instruction.
1431 locations->SetInAt(0, Location::Any());
1432 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1433 break;
1434
1435 default:
1436 LOG(FATAL) << "Unexpected type conversion from " << input_type
1437 << " to " << result_type;
1438 }
1439 break;
1440
Roland Levillaindff1f282014-11-05 14:15:05 +00001441 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001442 switch (input_type) {
1443 case Primitive::kPrimByte:
1444 case Primitive::kPrimShort:
1445 case Primitive::kPrimInt:
1446 case Primitive::kPrimChar:
1447 // Processing a Dex `int-to-float' instruction.
1448 locations->SetInAt(0, Location::RequiresRegister());
1449 locations->SetOut(Location::RequiresFpuRegister());
1450 break;
1451
1452 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001453 // Processing a Dex `long-to-float' instruction.
1454 locations->SetInAt(0, Location::RequiresRegister());
1455 locations->SetOut(Location::RequiresFpuRegister());
1456 locations->AddTemp(Location::RequiresFpuRegister());
1457 locations->AddTemp(Location::RequiresFpuRegister());
1458 break;
1459
Roland Levillaincff13742014-11-17 14:32:17 +00001460 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001461 // Processing a Dex `double-to-float' instruction.
1462 locations->SetInAt(0, Location::RequiresFpuRegister());
1463 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001464 break;
1465
1466 default:
1467 LOG(FATAL) << "Unexpected type conversion from " << input_type
1468 << " to " << result_type;
1469 };
1470 break;
1471
Roland Levillaindff1f282014-11-05 14:15:05 +00001472 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001473 switch (input_type) {
1474 case Primitive::kPrimByte:
1475 case Primitive::kPrimShort:
1476 case Primitive::kPrimInt:
1477 case Primitive::kPrimChar:
1478 // Processing a Dex `int-to-double' instruction.
1479 locations->SetInAt(0, Location::RequiresRegister());
1480 locations->SetOut(Location::RequiresFpuRegister());
1481 break;
1482
1483 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001484 // Processing a Dex `long-to-double' instruction.
1485 locations->SetInAt(0, Location::RequiresRegister());
1486 locations->SetOut(Location::RequiresFpuRegister());
1487 locations->AddTemp(Location::RequiresFpuRegister());
1488 locations->AddTemp(Location::RequiresFpuRegister());
1489 break;
1490
Roland Levillaincff13742014-11-17 14:32:17 +00001491 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001492 // Processing a Dex `float-to-double' instruction.
1493 locations->SetInAt(0, Location::RequiresFpuRegister());
1494 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001495 break;
1496
1497 default:
1498 LOG(FATAL) << "Unexpected type conversion from " << input_type
1499 << " to " << result_type;
1500 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001501 break;
1502
1503 default:
1504 LOG(FATAL) << "Unexpected type conversion from " << input_type
1505 << " to " << result_type;
1506 }
1507}
1508
1509void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1510 LocationSummary* locations = conversion->GetLocations();
1511 Location out = locations->Out();
1512 Location in = locations->InAt(0);
1513 Primitive::Type result_type = conversion->GetResultType();
1514 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001515 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001516 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001517 case Primitive::kPrimByte:
1518 switch (input_type) {
1519 case Primitive::kPrimShort:
1520 case Primitive::kPrimInt:
1521 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001522 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001523 if (in.IsRegister()) {
1524 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
1525 } else if (in.IsStackSlot()) {
1526 __ movsxb(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
1527 } else {
1528 DCHECK(in.GetConstant()->IsIntConstant());
1529 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1530 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1531 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001532 break;
1533
1534 default:
1535 LOG(FATAL) << "Unexpected type conversion from " << input_type
1536 << " to " << result_type;
1537 }
1538 break;
1539
Roland Levillain01a8d712014-11-14 16:27:39 +00001540 case Primitive::kPrimShort:
1541 switch (input_type) {
1542 case Primitive::kPrimByte:
1543 case Primitive::kPrimInt:
1544 case Primitive::kPrimChar:
1545 // Processing a Dex `int-to-short' instruction.
1546 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001547 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001548 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001549 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001550 } else {
1551 DCHECK(in.GetConstant()->IsIntConstant());
1552 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001553 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001554 }
1555 break;
1556
1557 default:
1558 LOG(FATAL) << "Unexpected type conversion from " << input_type
1559 << " to " << result_type;
1560 }
1561 break;
1562
Roland Levillain946e1432014-11-11 17:35:19 +00001563 case Primitive::kPrimInt:
1564 switch (input_type) {
1565 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001566 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001567 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001568 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001569 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001570 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001571 } else {
1572 DCHECK(in.IsConstant());
1573 DCHECK(in.GetConstant()->IsLongConstant());
1574 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001575 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001576 }
1577 break;
1578
Roland Levillain3f8f9362014-12-02 17:45:01 +00001579 case Primitive::kPrimFloat: {
1580 // Processing a Dex `float-to-int' instruction.
1581 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1582 Register output = out.AsRegister<Register>();
1583 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1584 Label done, nan;
1585
1586 __ movl(output, Immediate(kPrimIntMax));
1587 // temp = int-to-float(output)
1588 __ cvtsi2ss(temp, output);
1589 // if input >= temp goto done
1590 __ comiss(input, temp);
1591 __ j(kAboveEqual, &done);
1592 // if input == NaN goto nan
1593 __ j(kUnordered, &nan);
1594 // output = float-to-int-truncate(input)
1595 __ cvttss2si(output, input);
1596 __ jmp(&done);
1597 __ Bind(&nan);
1598 // output = 0
1599 __ xorl(output, output);
1600 __ Bind(&done);
1601 break;
1602 }
1603
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001604 case Primitive::kPrimDouble: {
1605 // Processing a Dex `double-to-int' instruction.
1606 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1607 Register output = out.AsRegister<Register>();
1608 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1609 Label done, nan;
1610
1611 __ movl(output, Immediate(kPrimIntMax));
1612 // temp = int-to-double(output)
1613 __ cvtsi2sd(temp, output);
1614 // if input >= temp goto done
1615 __ comisd(input, temp);
1616 __ j(kAboveEqual, &done);
1617 // if input == NaN goto nan
1618 __ j(kUnordered, &nan);
1619 // output = double-to-int-truncate(input)
1620 __ cvttsd2si(output, input);
1621 __ jmp(&done);
1622 __ Bind(&nan);
1623 // output = 0
1624 __ xorl(output, output);
1625 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001626 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001627 }
Roland Levillain946e1432014-11-11 17:35:19 +00001628
1629 default:
1630 LOG(FATAL) << "Unexpected type conversion from " << input_type
1631 << " to " << result_type;
1632 }
1633 break;
1634
Roland Levillaindff1f282014-11-05 14:15:05 +00001635 case Primitive::kPrimLong:
1636 switch (input_type) {
1637 case Primitive::kPrimByte:
1638 case Primitive::kPrimShort:
1639 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001640 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001641 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001642 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1643 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001644 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001645 __ cdq();
1646 break;
1647
1648 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001649 // Processing a Dex `float-to-long' instruction.
1650 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001651 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1652 break;
1653
Roland Levillaindff1f282014-11-05 14:15:05 +00001654 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001655 // Processing a Dex `double-to-long' instruction.
1656 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1657 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001658 break;
1659
1660 default:
1661 LOG(FATAL) << "Unexpected type conversion from " << input_type
1662 << " to " << result_type;
1663 }
1664 break;
1665
Roland Levillain981e4542014-11-14 11:47:14 +00001666 case Primitive::kPrimChar:
1667 switch (input_type) {
1668 case Primitive::kPrimByte:
1669 case Primitive::kPrimShort:
1670 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001671 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1672 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001673 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001674 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001675 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001676 } else {
1677 DCHECK(in.GetConstant()->IsIntConstant());
1678 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001679 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001680 }
1681 break;
1682
1683 default:
1684 LOG(FATAL) << "Unexpected type conversion from " << input_type
1685 << " to " << result_type;
1686 }
1687 break;
1688
Roland Levillaindff1f282014-11-05 14:15:05 +00001689 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001690 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001691 case Primitive::kPrimByte:
1692 case Primitive::kPrimShort:
1693 case Primitive::kPrimInt:
1694 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001695 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001696 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001697 break;
1698
Roland Levillain6d0e4832014-11-27 18:31:21 +00001699 case Primitive::kPrimLong: {
1700 // Processing a Dex `long-to-float' instruction.
1701 Register low = in.AsRegisterPairLow<Register>();
1702 Register high = in.AsRegisterPairHigh<Register>();
1703 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1704 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1705 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1706
1707 // Operations use doubles for precision reasons (each 32-bit
1708 // half of a long fits in the 53-bit mantissa of a double,
1709 // but not in the 24-bit mantissa of a float). This is
1710 // especially important for the low bits. The result is
1711 // eventually converted to float.
1712
1713 // low = low - 2^31 (to prevent bit 31 of `low` to be
1714 // interpreted as a sign bit)
1715 __ subl(low, Immediate(0x80000000));
1716 // temp = int-to-double(high)
1717 __ cvtsi2sd(temp, high);
1718 // temp = temp * 2^32
1719 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
1720 __ mulsd(temp, constant);
1721 // result = int-to-double(low)
1722 __ cvtsi2sd(result, low);
1723 // result = result + 2^31 (restore the original value of `low`)
1724 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
1725 __ addsd(result, constant);
1726 // result = result + temp
1727 __ addsd(result, temp);
1728 // result = double-to-float(result)
1729 __ cvtsd2ss(result, result);
1730 break;
1731 }
1732
Roland Levillaincff13742014-11-17 14:32:17 +00001733 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001734 // Processing a Dex `double-to-float' instruction.
1735 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001736 break;
1737
1738 default:
1739 LOG(FATAL) << "Unexpected type conversion from " << input_type
1740 << " to " << result_type;
1741 };
1742 break;
1743
Roland Levillaindff1f282014-11-05 14:15:05 +00001744 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001745 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001746 case Primitive::kPrimByte:
1747 case Primitive::kPrimShort:
1748 case Primitive::kPrimInt:
1749 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001750 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001751 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001752 break;
1753
Roland Levillain647b9ed2014-11-27 12:06:00 +00001754 case Primitive::kPrimLong: {
1755 // Processing a Dex `long-to-double' instruction.
1756 Register low = in.AsRegisterPairLow<Register>();
1757 Register high = in.AsRegisterPairHigh<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001758 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1759 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1760 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001761
Roland Levillain647b9ed2014-11-27 12:06:00 +00001762 // low = low - 2^31 (to prevent bit 31 of `low` to be
1763 // interpreted as a sign bit)
1764 __ subl(low, Immediate(0x80000000));
1765 // temp = int-to-double(high)
1766 __ cvtsi2sd(temp, high);
1767 // temp = temp * 2^32
Roland Levillain6d0e4832014-11-27 18:31:21 +00001768 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001769 __ mulsd(temp, constant);
1770 // result = int-to-double(low)
1771 __ cvtsi2sd(result, low);
1772 // result = result + 2^31 (restore the original value of `low`)
Roland Levillain6d0e4832014-11-27 18:31:21 +00001773 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001774 __ addsd(result, constant);
1775 // result = result + temp
1776 __ addsd(result, temp);
1777 break;
1778 }
1779
Roland Levillaincff13742014-11-17 14:32:17 +00001780 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001781 // Processing a Dex `float-to-double' instruction.
1782 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001783 break;
1784
1785 default:
1786 LOG(FATAL) << "Unexpected type conversion from " << input_type
1787 << " to " << result_type;
1788 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001789 break;
1790
1791 default:
1792 LOG(FATAL) << "Unexpected type conversion from " << input_type
1793 << " to " << result_type;
1794 }
1795}
1796
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001797void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001798 LocationSummary* locations =
1799 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001800 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001801 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001802 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001803 locations->SetInAt(0, Location::RequiresRegister());
1804 locations->SetInAt(1, Location::Any());
1805 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001806 break;
1807 }
1808
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001809 case Primitive::kPrimFloat:
1810 case Primitive::kPrimDouble: {
1811 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00001812 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001813 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001814 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001815 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001816
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001817 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001818 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1819 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001820 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001821}
1822
1823void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1824 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001825 Location first = locations->InAt(0);
1826 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001827 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001828 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001829 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001830 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001831 __ addl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001832 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001833 __ addl(first.AsRegister<Register>(),
1834 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001835 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001836 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001837 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001838 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001839 }
1840
1841 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001842 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001843 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1844 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001845 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001846 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1847 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001848 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001849 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001850 break;
1851 }
1852
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001853 case Primitive::kPrimFloat: {
1854 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001855 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001856 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001857 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001858 }
1859
1860 case Primitive::kPrimDouble: {
1861 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001862 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001863 }
1864 break;
1865 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001866
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001867 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001868 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001869 }
1870}
1871
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001872void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001873 LocationSummary* locations =
1874 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001875 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001876 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001877 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001878 locations->SetInAt(0, Location::RequiresRegister());
1879 locations->SetInAt(1, Location::Any());
1880 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001881 break;
1882 }
Calin Juravle11351682014-10-23 15:38:15 +01001883 case Primitive::kPrimFloat:
1884 case Primitive::kPrimDouble: {
1885 locations->SetInAt(0, Location::RequiresFpuRegister());
1886 locations->SetInAt(1, Location::RequiresFpuRegister());
1887 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001888 break;
Calin Juravle11351682014-10-23 15:38:15 +01001889 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001890
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001891 default:
Calin Juravle11351682014-10-23 15:38:15 +01001892 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001893 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001894}
1895
1896void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1897 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001898 Location first = locations->InAt(0);
1899 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001900 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001901 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001902 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001903 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001904 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001905 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001906 __ subl(first.AsRegister<Register>(),
1907 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001908 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001909 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001910 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001911 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001912 }
1913
1914 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001915 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01001916 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1917 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001918 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001919 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001920 __ sbbl(first.AsRegisterPairHigh<Register>(),
1921 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001922 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001923 break;
1924 }
1925
Calin Juravle11351682014-10-23 15:38:15 +01001926 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001927 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001928 break;
Calin Juravle11351682014-10-23 15:38:15 +01001929 }
1930
1931 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001932 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001933 break;
1934 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001935
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001936 default:
Calin Juravle11351682014-10-23 15:38:15 +01001937 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001938 }
1939}
1940
Calin Juravle34bacdf2014-10-07 20:23:36 +01001941void LocationsBuilderX86::VisitMul(HMul* mul) {
1942 LocationSummary* locations =
1943 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1944 switch (mul->GetResultType()) {
1945 case Primitive::kPrimInt:
1946 locations->SetInAt(0, Location::RequiresRegister());
1947 locations->SetInAt(1, Location::Any());
1948 locations->SetOut(Location::SameAsFirstInput());
1949 break;
1950 case Primitive::kPrimLong: {
1951 locations->SetInAt(0, Location::RequiresRegister());
1952 // TODO: Currently this handles only stack operands:
1953 // - we don't have enough registers because we currently use Quick ABI.
1954 // - by the time we have a working register allocator we will probably change the ABI
1955 // and fix the above.
1956 // - we don't have a way yet to request operands on stack but the base line compiler
1957 // will leave the operands on the stack with Any().
1958 locations->SetInAt(1, Location::Any());
1959 locations->SetOut(Location::SameAsFirstInput());
1960 // Needed for imul on 32bits with 64bits output.
1961 locations->AddTemp(Location::RegisterLocation(EAX));
1962 locations->AddTemp(Location::RegisterLocation(EDX));
1963 break;
1964 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001965 case Primitive::kPrimFloat:
1966 case Primitive::kPrimDouble: {
1967 locations->SetInAt(0, Location::RequiresFpuRegister());
1968 locations->SetInAt(1, Location::RequiresFpuRegister());
1969 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001970 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001971 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001972
1973 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001974 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001975 }
1976}
1977
1978void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1979 LocationSummary* locations = mul->GetLocations();
1980 Location first = locations->InAt(0);
1981 Location second = locations->InAt(1);
1982 DCHECK(first.Equals(locations->Out()));
1983
1984 switch (mul->GetResultType()) {
1985 case Primitive::kPrimInt: {
1986 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001987 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001988 } else if (second.IsConstant()) {
1989 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001990 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001991 } else {
1992 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001993 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01001994 }
1995 break;
1996 }
1997
1998 case Primitive::kPrimLong: {
1999 DCHECK(second.IsDoubleStackSlot());
2000
2001 Register in1_hi = first.AsRegisterPairHigh<Register>();
2002 Register in1_lo = first.AsRegisterPairLow<Register>();
2003 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2004 Address in2_lo(ESP, second.GetStackIndex());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002005 Register eax = locations->GetTemp(0).AsRegister<Register>();
2006 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002007
2008 DCHECK_EQ(EAX, eax);
2009 DCHECK_EQ(EDX, edx);
2010
2011 // input: in1 - 64 bits, in2 - 64 bits
2012 // output: in1
2013 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2014 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2015 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
2016
2017 __ movl(eax, in2_hi);
2018 // eax <- in1.lo * in2.hi
2019 __ imull(eax, in1_lo);
2020 // in1.hi <- in1.hi * in2.lo
2021 __ imull(in1_hi, in2_lo);
2022 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2023 __ addl(in1_hi, eax);
2024 // move in1_lo to eax to prepare for double precision
2025 __ movl(eax, in1_lo);
2026 // edx:eax <- in1.lo * in2.lo
2027 __ mull(in2_lo);
2028 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2029 __ addl(in1_hi, edx);
2030 // in1.lo <- (in1.lo * in2.lo)[31:0];
2031 __ movl(in1_lo, eax);
2032
2033 break;
2034 }
2035
Calin Juravleb5bfa962014-10-21 18:02:24 +01002036 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002037 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002038 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002039 }
2040
2041 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002042 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002043 break;
2044 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002045
2046 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002047 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002048 }
2049}
2050
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002051void InstructionCodeGeneratorX86::PushOntoFPStack(Location source, uint32_t temp_offset,
2052 uint32_t stack_adjustment, bool is_float) {
2053 if (source.IsStackSlot()) {
2054 DCHECK(is_float);
2055 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2056 } else if (source.IsDoubleStackSlot()) {
2057 DCHECK(!is_float);
2058 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2059 } else {
2060 // Write the value to the temporary location on the stack and load to FP stack.
2061 if (is_float) {
2062 Location stack_temp = Location::StackSlot(temp_offset);
2063 codegen_->Move32(stack_temp, source);
2064 __ flds(Address(ESP, temp_offset));
2065 } else {
2066 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2067 codegen_->Move64(stack_temp, source);
2068 __ fldl(Address(ESP, temp_offset));
2069 }
2070 }
2071}
2072
2073void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2074 Primitive::Type type = rem->GetResultType();
2075 bool is_float = type == Primitive::kPrimFloat;
2076 size_t elem_size = Primitive::ComponentSize(type);
2077 LocationSummary* locations = rem->GetLocations();
2078 Location first = locations->InAt(0);
2079 Location second = locations->InAt(1);
2080 Location out = locations->Out();
2081
2082 // Create stack space for 2 elements.
2083 // TODO: enhance register allocator to ask for stack temporaries.
2084 __ subl(ESP, Immediate(2 * elem_size));
2085
2086 // Load the values to the FP stack in reverse order, using temporaries if needed.
2087 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2088 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2089
2090 // Loop doing FPREM until we stabilize.
2091 Label retry;
2092 __ Bind(&retry);
2093 __ fprem();
2094
2095 // Move FP status to AX.
2096 __ fstsw();
2097
2098 // And see if the argument reduction is complete. This is signaled by the
2099 // C2 FPU flag bit set to 0.
2100 __ andl(EAX, Immediate(kC2ConditionMask));
2101 __ j(kNotEqual, &retry);
2102
2103 // We have settled on the final value. Retrieve it into an XMM register.
2104 // Store FP top of stack to real stack.
2105 if (is_float) {
2106 __ fsts(Address(ESP, 0));
2107 } else {
2108 __ fstl(Address(ESP, 0));
2109 }
2110
2111 // Pop the 2 items from the FP stack.
2112 __ fucompp();
2113
2114 // Load the value from the stack into an XMM register.
2115 DCHECK(out.IsFpuRegister()) << out;
2116 if (is_float) {
2117 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2118 } else {
2119 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2120 }
2121
2122 // And remove the temporary stack space we allocated.
2123 __ addl(ESP, Immediate(2 * elem_size));
2124}
2125
Calin Juravlebacfec32014-11-14 15:54:36 +00002126void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2127 DCHECK(instruction->IsDiv() || instruction->IsRem());
2128
2129 LocationSummary* locations = instruction->GetLocations();
2130 Location out = locations->Out();
2131 Location first = locations->InAt(0);
2132 Location second = locations->InAt(1);
2133 bool is_div = instruction->IsDiv();
2134
2135 switch (instruction->GetResultType()) {
2136 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002137 Register second_reg = second.AsRegister<Register>();
2138 DCHECK_EQ(EAX, first.AsRegister<Register>());
2139 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002140
2141 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002142 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
2143 is_div);
Calin Juravlebacfec32014-11-14 15:54:36 +00002144 codegen_->AddSlowPath(slow_path);
2145
2146 // 0x80000000/-1 triggers an arithmetic exception!
2147 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2148 // it's safe to just use negl instead of more complex comparisons.
2149
2150 __ cmpl(second_reg, Immediate(-1));
2151 __ j(kEqual, slow_path->GetEntryLabel());
2152
2153 // edx:eax <- sign-extended of eax
2154 __ cdq();
2155 // eax = quotient, edx = remainder
2156 __ idivl(second_reg);
2157
2158 __ Bind(slow_path->GetExitLabel());
2159 break;
2160 }
2161
2162 case Primitive::kPrimLong: {
2163 InvokeRuntimeCallingConvention calling_convention;
2164 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2165 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2166 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2167 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2168 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2169 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2170
2171 if (is_div) {
2172 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2173 } else {
2174 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2175 }
2176 uint32_t dex_pc = is_div
2177 ? instruction->AsDiv()->GetDexPc()
2178 : instruction->AsRem()->GetDexPc();
2179 codegen_->RecordPcInfo(instruction, dex_pc);
2180
2181 break;
2182 }
2183
2184 default:
2185 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2186 }
2187}
2188
Calin Juravle7c4954d2014-10-28 16:57:40 +00002189void LocationsBuilderX86::VisitDiv(HDiv* div) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002190 LocationSummary::CallKind call_kind = div->GetResultType() == Primitive::kPrimLong
2191 ? LocationSummary::kCall
2192 : LocationSummary::kNoCall;
2193 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2194
Calin Juravle7c4954d2014-10-28 16:57:40 +00002195 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002196 case Primitive::kPrimInt: {
2197 locations->SetInAt(0, Location::RegisterLocation(EAX));
2198 locations->SetInAt(1, Location::RequiresRegister());
2199 locations->SetOut(Location::SameAsFirstInput());
2200 // Intel uses edx:eax as the dividend.
2201 locations->AddTemp(Location::RegisterLocation(EDX));
2202 break;
2203 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002204 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002205 InvokeRuntimeCallingConvention calling_convention;
2206 locations->SetInAt(0, Location::RegisterPairLocation(
2207 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2208 locations->SetInAt(1, Location::RegisterPairLocation(
2209 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2210 // Runtime helper puts the result in EAX, EDX.
2211 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002212 break;
2213 }
2214 case Primitive::kPrimFloat:
2215 case Primitive::kPrimDouble: {
2216 locations->SetInAt(0, Location::RequiresFpuRegister());
2217 locations->SetInAt(1, Location::RequiresFpuRegister());
2218 locations->SetOut(Location::SameAsFirstInput());
2219 break;
2220 }
2221
2222 default:
2223 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2224 }
2225}
2226
2227void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2228 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002229 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002230 Location first = locations->InAt(0);
2231 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002232
2233 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002234 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002235 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002236 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002237 break;
2238 }
2239
2240 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002241 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002242 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002243 break;
2244 }
2245
2246 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002247 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002248 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002249 break;
2250 }
2251
2252 default:
2253 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2254 }
2255}
2256
Calin Juravlebacfec32014-11-14 15:54:36 +00002257void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002258 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002259 LocationSummary* locations =
2260 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravlebacfec32014-11-14 15:54:36 +00002261
Calin Juravled2ec87d2014-12-08 14:24:46 +00002262 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002263 case Primitive::kPrimInt: {
2264 locations->SetInAt(0, Location::RegisterLocation(EAX));
2265 locations->SetInAt(1, Location::RequiresRegister());
2266 locations->SetOut(Location::RegisterLocation(EDX));
2267 break;
2268 }
2269 case Primitive::kPrimLong: {
2270 InvokeRuntimeCallingConvention calling_convention;
2271 locations->SetInAt(0, Location::RegisterPairLocation(
2272 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2273 locations->SetInAt(1, Location::RegisterPairLocation(
2274 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2275 // Runtime helper puts the result in EAX, EDX.
2276 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2277 break;
2278 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002279 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002280 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002281 locations->SetInAt(0, Location::Any());
2282 locations->SetInAt(1, Location::Any());
2283 locations->SetOut(Location::RequiresFpuRegister());
2284 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002285 break;
2286 }
2287
2288 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002289 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002290 }
2291}
2292
2293void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2294 Primitive::Type type = rem->GetResultType();
2295 switch (type) {
2296 case Primitive::kPrimInt:
2297 case Primitive::kPrimLong: {
2298 GenerateDivRemIntegral(rem);
2299 break;
2300 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002301 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002302 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002303 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002304 break;
2305 }
2306 default:
2307 LOG(FATAL) << "Unexpected rem type " << type;
2308 }
2309}
2310
Calin Juravled0d48522014-11-04 16:40:20 +00002311void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2312 LocationSummary* locations =
2313 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002314 switch (instruction->GetType()) {
2315 case Primitive::kPrimInt: {
2316 locations->SetInAt(0, Location::Any());
2317 break;
2318 }
2319 case Primitive::kPrimLong: {
2320 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2321 if (!instruction->IsConstant()) {
2322 locations->AddTemp(Location::RequiresRegister());
2323 }
2324 break;
2325 }
2326 default:
2327 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2328 }
Calin Juravled0d48522014-11-04 16:40:20 +00002329 if (instruction->HasUses()) {
2330 locations->SetOut(Location::SameAsFirstInput());
2331 }
2332}
2333
2334void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2335 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2336 codegen_->AddSlowPath(slow_path);
2337
2338 LocationSummary* locations = instruction->GetLocations();
2339 Location value = locations->InAt(0);
2340
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002341 switch (instruction->GetType()) {
2342 case Primitive::kPrimInt: {
2343 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002344 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002345 __ j(kEqual, slow_path->GetEntryLabel());
2346 } else if (value.IsStackSlot()) {
2347 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2348 __ j(kEqual, slow_path->GetEntryLabel());
2349 } else {
2350 DCHECK(value.IsConstant()) << value;
2351 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2352 __ jmp(slow_path->GetEntryLabel());
2353 }
2354 }
2355 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002356 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002357 case Primitive::kPrimLong: {
2358 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002359 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002360 __ movl(temp, value.AsRegisterPairLow<Register>());
2361 __ orl(temp, value.AsRegisterPairHigh<Register>());
2362 __ j(kEqual, slow_path->GetEntryLabel());
2363 } else {
2364 DCHECK(value.IsConstant()) << value;
2365 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2366 __ jmp(slow_path->GetEntryLabel());
2367 }
2368 }
2369 break;
2370 }
2371 default:
2372 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002373 }
Calin Juravled0d48522014-11-04 16:40:20 +00002374}
2375
Calin Juravle9aec02f2014-11-18 23:06:35 +00002376void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2377 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2378
2379 LocationSummary* locations =
2380 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2381
2382 switch (op->GetResultType()) {
2383 case Primitive::kPrimInt: {
2384 locations->SetInAt(0, Location::RequiresRegister());
2385 // The shift count needs to be in CL.
2386 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2387 locations->SetOut(Location::SameAsFirstInput());
2388 break;
2389 }
2390 case Primitive::kPrimLong: {
2391 locations->SetInAt(0, Location::RequiresRegister());
2392 // The shift count needs to be in CL.
2393 locations->SetInAt(1, Location::RegisterLocation(ECX));
2394 locations->SetOut(Location::SameAsFirstInput());
2395 break;
2396 }
2397 default:
2398 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2399 }
2400}
2401
2402void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2403 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2404
2405 LocationSummary* locations = op->GetLocations();
2406 Location first = locations->InAt(0);
2407 Location second = locations->InAt(1);
2408 DCHECK(first.Equals(locations->Out()));
2409
2410 switch (op->GetResultType()) {
2411 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002412 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002413 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002414 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002415 DCHECK_EQ(ECX, second_reg);
2416 if (op->IsShl()) {
2417 __ shll(first_reg, second_reg);
2418 } else if (op->IsShr()) {
2419 __ sarl(first_reg, second_reg);
2420 } else {
2421 __ shrl(first_reg, second_reg);
2422 }
2423 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002424 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002425 if (op->IsShl()) {
2426 __ shll(first_reg, imm);
2427 } else if (op->IsShr()) {
2428 __ sarl(first_reg, imm);
2429 } else {
2430 __ shrl(first_reg, imm);
2431 }
2432 }
2433 break;
2434 }
2435 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002436 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002437 DCHECK_EQ(ECX, second_reg);
2438 if (op->IsShl()) {
2439 GenerateShlLong(first, second_reg);
2440 } else if (op->IsShr()) {
2441 GenerateShrLong(first, second_reg);
2442 } else {
2443 GenerateUShrLong(first, second_reg);
2444 }
2445 break;
2446 }
2447 default:
2448 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2449 }
2450}
2451
2452void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2453 Label done;
2454 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2455 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2456 __ testl(shifter, Immediate(32));
2457 __ j(kEqual, &done);
2458 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2459 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2460 __ Bind(&done);
2461}
2462
2463void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2464 Label done;
2465 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2466 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2467 __ testl(shifter, Immediate(32));
2468 __ j(kEqual, &done);
2469 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2470 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2471 __ Bind(&done);
2472}
2473
2474void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2475 Label done;
2476 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2477 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2478 __ testl(shifter, Immediate(32));
2479 __ j(kEqual, &done);
2480 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2481 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2482 __ Bind(&done);
2483}
2484
2485void LocationsBuilderX86::VisitShl(HShl* shl) {
2486 HandleShift(shl);
2487}
2488
2489void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2490 HandleShift(shl);
2491}
2492
2493void LocationsBuilderX86::VisitShr(HShr* shr) {
2494 HandleShift(shr);
2495}
2496
2497void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2498 HandleShift(shr);
2499}
2500
2501void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2502 HandleShift(ushr);
2503}
2504
2505void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2506 HandleShift(ushr);
2507}
2508
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002509void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002510 LocationSummary* locations =
2511 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002512 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002513 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002514 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2515 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002516}
2517
2518void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2519 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002520 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002521 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002522
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002523 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002524
Nicolas Geoffray39468442014-09-02 15:17:15 +01002525 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002526 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002527}
2528
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002529void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2530 LocationSummary* locations =
2531 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2532 locations->SetOut(Location::RegisterLocation(EAX));
2533 InvokeRuntimeCallingConvention calling_convention;
2534 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002535 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2536 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002537}
2538
2539void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2540 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002541 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002542 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2543
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002544 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002545
2546 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2547 DCHECK(!codegen_->IsLeafMethod());
2548}
2549
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002550void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002551 LocationSummary* locations =
2552 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002553 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2554 if (location.IsStackSlot()) {
2555 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2556 } else if (location.IsDoubleStackSlot()) {
2557 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002558 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002559 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002560}
2561
2562void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002563 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002564}
2565
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002566void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002567 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002568 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002569 locations->SetInAt(0, Location::RequiresRegister());
2570 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002571}
2572
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002573void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2574 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002575 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002576 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002577 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002578 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002579 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002580 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002581 break;
2582
2583 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002584 __ notl(out.AsRegisterPairLow<Register>());
2585 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002586 break;
2587
2588 default:
2589 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2590 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002591}
2592
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002593void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002594 LocationSummary* locations =
2595 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002596 switch (compare->InputAt(0)->GetType()) {
2597 case Primitive::kPrimLong: {
2598 locations->SetInAt(0, Location::RequiresRegister());
2599 // TODO: we set any here but we don't handle constants
2600 locations->SetInAt(1, Location::Any());
2601 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2602 break;
2603 }
2604 case Primitive::kPrimFloat:
2605 case Primitive::kPrimDouble: {
2606 locations->SetInAt(0, Location::RequiresFpuRegister());
2607 locations->SetInAt(1, Location::RequiresFpuRegister());
2608 locations->SetOut(Location::RequiresRegister());
2609 break;
2610 }
2611 default:
2612 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2613 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002614}
2615
2616void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002617 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002618 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002619 Location left = locations->InAt(0);
2620 Location right = locations->InAt(1);
2621
2622 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002623 switch (compare->InputAt(0)->GetType()) {
2624 case Primitive::kPrimLong: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002625 if (right.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002626 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002627 } else {
2628 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002629 __ cmpl(left.AsRegisterPairHigh<Register>(),
2630 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002631 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002632 __ j(kLess, &less); // Signed compare.
2633 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002634 if (right.IsRegisterPair()) {
2635 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002636 } else {
2637 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002638 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002639 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002640 break;
2641 }
2642 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002643 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002644 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
2645 break;
2646 }
2647 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002648 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002649 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002650 break;
2651 }
2652 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002653 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002654 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002655 __ movl(out, Immediate(0));
2656 __ j(kEqual, &done);
2657 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
2658
2659 __ Bind(&greater);
2660 __ movl(out, Immediate(1));
2661 __ jmp(&done);
2662
2663 __ Bind(&less);
2664 __ movl(out, Immediate(-1));
2665
2666 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002667}
2668
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002669void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002670 LocationSummary* locations =
2671 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002672 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2673 locations->SetInAt(i, Location::Any());
2674 }
2675 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002676}
2677
2678void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002679 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002680 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002681}
2682
Calin Juravle52c48962014-12-16 17:02:57 +00002683void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
2684 /*
2685 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2686 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2687 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2688 */
2689 switch (kind) {
2690 case MemBarrierKind::kAnyAny: {
2691 __ mfence();
2692 break;
2693 }
2694 case MemBarrierKind::kAnyStore:
2695 case MemBarrierKind::kLoadAny:
2696 case MemBarrierKind::kStoreStore: {
2697 // nop
2698 break;
2699 }
2700 default:
2701 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002702 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002703}
2704
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002705
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002706void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002707 Label is_null;
2708 __ testl(value, value);
2709 __ j(kEqual, &is_null);
2710 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2711 __ movl(temp, object);
2712 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002713 __ movb(Address(temp, card, TIMES_1, 0),
2714 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002715 __ Bind(&is_null);
2716}
2717
Calin Juravle52c48962014-12-16 17:02:57 +00002718void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
2719 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002720 LocationSummary* locations =
2721 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002722 locations->SetInAt(0, Location::RequiresRegister());
2723 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle52c48962014-12-16 17:02:57 +00002724
2725 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
2726 // Long values can be loaded atomically into an XMM using movsd.
2727 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
2728 // and then copy the XMM into the output 32bits at a time).
2729 locations->AddTemp(Location::RequiresFpuRegister());
2730 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002731}
2732
Calin Juravle52c48962014-12-16 17:02:57 +00002733void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
2734 const FieldInfo& field_info) {
2735 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002736
Calin Juravle52c48962014-12-16 17:02:57 +00002737 LocationSummary* locations = instruction->GetLocations();
2738 Register base = locations->InAt(0).AsRegister<Register>();
2739 Location out = locations->Out();
2740 bool is_volatile = field_info.IsVolatile();
2741 Primitive::Type field_type = field_info.GetFieldType();
2742 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2743
2744 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002745 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00002746 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002747 break;
2748 }
2749
2750 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002751 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002752 break;
2753 }
2754
2755 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00002756 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002757 break;
2758 }
2759
2760 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002761 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002762 break;
2763 }
2764
2765 case Primitive::kPrimInt:
2766 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002767 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002768 break;
2769 }
2770
2771 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00002772 if (is_volatile) {
2773 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2774 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002775 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002776 __ movd(out.AsRegisterPairLow<Register>(), temp);
2777 __ psrlq(temp, Immediate(32));
2778 __ movd(out.AsRegisterPairHigh<Register>(), temp);
2779 } else {
2780 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002781 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002782 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
2783 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002784 break;
2785 }
2786
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002787 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002788 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002789 break;
2790 }
2791
2792 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002793 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002794 break;
2795 }
2796
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002797 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00002798 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002799 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002800 }
Calin Juravle52c48962014-12-16 17:02:57 +00002801
Calin Juravle77520bc2015-01-12 18:45:46 +00002802 // Longs are handled in the switch.
2803 if (field_type != Primitive::kPrimLong) {
2804 codegen_->MaybeRecordImplicitNullCheck(instruction);
2805 }
2806
Calin Juravle52c48962014-12-16 17:02:57 +00002807 if (is_volatile) {
2808 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2809 }
2810}
2811
2812void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
2813 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2814
2815 LocationSummary* locations =
2816 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2817 locations->SetInAt(0, Location::RequiresRegister());
2818 bool is_volatile = field_info.IsVolatile();
2819 Primitive::Type field_type = field_info.GetFieldType();
2820 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2821 || (field_type == Primitive::kPrimByte);
2822
2823 // The register allocator does not support multiple
2824 // inputs that die at entry with one in a specific register.
2825 if (is_byte_type) {
2826 // Ensure the value is in a byte register.
2827 locations->SetInAt(1, Location::RegisterLocation(EAX));
2828 } else {
2829 locations->SetInAt(1, Location::RequiresRegister());
2830 }
2831 // Temporary registers for the write barrier.
2832 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2833 locations->AddTemp(Location::RequiresRegister());
2834 // Ensure the card is in a byte register.
2835 locations->AddTemp(Location::RegisterLocation(ECX));
2836 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
2837 // 64bits value can be atomically written to an address with movsd and an XMM register.
2838 // We need two XMM registers because there's no easier way to (bit) copy a register pair
2839 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
2840 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
2841 // isolated cases when we need this it isn't worth adding the extra complexity.
2842 locations->AddTemp(Location::RequiresFpuRegister());
2843 locations->AddTemp(Location::RequiresFpuRegister());
2844 }
2845}
2846
2847void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
2848 const FieldInfo& field_info) {
2849 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2850
2851 LocationSummary* locations = instruction->GetLocations();
2852 Register base = locations->InAt(0).AsRegister<Register>();
2853 Location value = locations->InAt(1);
2854 bool is_volatile = field_info.IsVolatile();
2855 Primitive::Type field_type = field_info.GetFieldType();
2856 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2857
2858 if (is_volatile) {
2859 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2860 }
2861
2862 switch (field_type) {
2863 case Primitive::kPrimBoolean:
2864 case Primitive::kPrimByte: {
2865 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
2866 break;
2867 }
2868
2869 case Primitive::kPrimShort:
2870 case Primitive::kPrimChar: {
2871 __ movw(Address(base, offset), value.AsRegister<Register>());
2872 break;
2873 }
2874
2875 case Primitive::kPrimInt:
2876 case Primitive::kPrimNot: {
2877 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00002878 break;
2879 }
2880
2881 case Primitive::kPrimLong: {
2882 if (is_volatile) {
2883 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2884 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
2885 __ movd(temp1, value.AsRegisterPairLow<Register>());
2886 __ movd(temp2, value.AsRegisterPairHigh<Register>());
2887 __ punpckldq(temp1, temp2);
2888 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00002889 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002890 } else {
2891 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00002892 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002893 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
2894 }
2895 break;
2896 }
2897
2898 case Primitive::kPrimFloat: {
2899 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
2900 break;
2901 }
2902
2903 case Primitive::kPrimDouble: {
2904 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
2905 break;
2906 }
2907
2908 case Primitive::kPrimVoid:
2909 LOG(FATAL) << "Unreachable type " << field_type;
2910 UNREACHABLE();
2911 }
2912
Calin Juravle77520bc2015-01-12 18:45:46 +00002913 // Longs are handled in the switch.
2914 if (field_type != Primitive::kPrimLong) {
2915 codegen_->MaybeRecordImplicitNullCheck(instruction);
2916 }
2917
2918 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2919 Register temp = locations->GetTemp(0).AsRegister<Register>();
2920 Register card = locations->GetTemp(1).AsRegister<Register>();
2921 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
2922 }
2923
Calin Juravle52c48962014-12-16 17:02:57 +00002924 if (is_volatile) {
2925 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2926 }
2927}
2928
2929void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2930 HandleFieldGet(instruction, instruction->GetFieldInfo());
2931}
2932
2933void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2934 HandleFieldGet(instruction, instruction->GetFieldInfo());
2935}
2936
2937void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2938 HandleFieldSet(instruction, instruction->GetFieldInfo());
2939}
2940
2941void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2942 HandleFieldSet(instruction, instruction->GetFieldInfo());
2943}
2944
2945void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2946 HandleFieldSet(instruction, instruction->GetFieldInfo());
2947}
2948
2949void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2950 HandleFieldSet(instruction, instruction->GetFieldInfo());
2951}
2952
2953void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2954 HandleFieldGet(instruction, instruction->GetFieldInfo());
2955}
2956
2957void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2958 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002959}
2960
2961void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002962 LocationSummary* locations =
2963 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002964 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
2965 ? Location::RequiresRegister()
2966 : Location::Any();
2967 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002968 if (instruction->HasUses()) {
2969 locations->SetOut(Location::SameAsFirstInput());
2970 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002971}
2972
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002973void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002974 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2975 return;
2976 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002977 LocationSummary* locations = instruction->GetLocations();
2978 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002979
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002980 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
2981 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2982}
2983
2984void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002985 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002986 codegen_->AddSlowPath(slow_path);
2987
2988 LocationSummary* locations = instruction->GetLocations();
2989 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002990
2991 if (obj.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002992 __ cmpl(obj.AsRegister<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002993 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002994 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002995 } else {
2996 DCHECK(obj.IsConstant()) << obj;
2997 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2998 __ jmp(slow_path->GetEntryLabel());
2999 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003000 }
3001 __ j(kEqual, slow_path->GetEntryLabel());
3002}
3003
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003004void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3005 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3006 GenerateImplicitNullCheck(instruction);
3007 } else {
3008 GenerateExplicitNullCheck(instruction);
3009 }
3010}
3011
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003012void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003013 LocationSummary* locations =
3014 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003015 locations->SetInAt(0, Location::RequiresRegister());
3016 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3017 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003018}
3019
3020void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3021 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003022 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003023 Location index = locations->InAt(1);
3024
Calin Juravle77520bc2015-01-12 18:45:46 +00003025 Primitive::Type type = instruction->GetType();
3026 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003027 case Primitive::kPrimBoolean: {
3028 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003029 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003030 if (index.IsConstant()) {
3031 __ movzxb(out, Address(obj,
3032 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3033 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003034 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003035 }
3036 break;
3037 }
3038
3039 case Primitive::kPrimByte: {
3040 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003041 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003042 if (index.IsConstant()) {
3043 __ movsxb(out, Address(obj,
3044 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3045 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003046 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003047 }
3048 break;
3049 }
3050
3051 case Primitive::kPrimShort: {
3052 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003053 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003054 if (index.IsConstant()) {
3055 __ movsxw(out, Address(obj,
3056 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3057 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003058 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003059 }
3060 break;
3061 }
3062
3063 case Primitive::kPrimChar: {
3064 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_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 __ movzxw(out, Address(obj,
3068 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3069 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003070 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003071 }
3072 break;
3073 }
3074
3075 case Primitive::kPrimInt:
3076 case Primitive::kPrimNot: {
3077 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003078 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003079 if (index.IsConstant()) {
3080 __ movl(out, Address(obj,
3081 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3082 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003083 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003084 }
3085 break;
3086 }
3087
3088 case Primitive::kPrimLong: {
3089 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003090 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003091 if (index.IsConstant()) {
3092 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003093 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003094 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003095 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003096 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003097 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003098 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003099 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003100 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003101 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003102 }
3103 break;
3104 }
3105
Mark Mendell7c8d0092015-01-26 11:21:33 -05003106 case Primitive::kPrimFloat: {
3107 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3108 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3109 if (index.IsConstant()) {
3110 __ movss(out, Address(obj,
3111 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3112 } else {
3113 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3114 }
3115 break;
3116 }
3117
3118 case Primitive::kPrimDouble: {
3119 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3120 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3121 if (index.IsConstant()) {
3122 __ movsd(out, Address(obj,
3123 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3124 } else {
3125 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3126 }
3127 break;
3128 }
3129
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003130 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003131 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003132 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003133 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003134
3135 if (type != Primitive::kPrimLong) {
3136 codegen_->MaybeRecordImplicitNullCheck(instruction);
3137 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003138}
3139
3140void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003141 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003142 bool needs_write_barrier =
3143 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3144
3145 DCHECK(kFollowsQuickABI);
3146 bool not_enough_registers = needs_write_barrier
3147 && !instruction->GetValue()->IsConstant()
3148 && !instruction->GetIndex()->IsConstant();
3149 bool needs_runtime_call = instruction->NeedsTypeCheck() || not_enough_registers;
3150
Nicolas Geoffray39468442014-09-02 15:17:15 +01003151 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3152 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003153 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003154
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003155 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003156 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003157 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3158 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3159 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003160 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003161 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3162 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003163 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003164 // In case of a byte operation, the register allocator does not support multiple
3165 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003166 locations->SetInAt(0, Location::RequiresRegister());
3167 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003168 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003169 // Ensure the value is in a byte register.
3170 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003171 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003172 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003173 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003174 // Temporary registers for the write barrier.
3175 if (needs_write_barrier) {
3176 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003177 // Ensure the card is in a byte register.
3178 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003179 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003180 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003181}
3182
3183void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3184 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003185 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003186 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003187 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003188 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003189 bool needs_runtime_call = locations->WillCall();
3190 bool needs_write_barrier =
3191 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003192
3193 switch (value_type) {
3194 case Primitive::kPrimBoolean:
3195 case Primitive::kPrimByte: {
3196 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003197 if (index.IsConstant()) {
3198 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003199 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003200 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003201 } else {
3202 __ movb(Address(obj, offset),
3203 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3204 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003205 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003206 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003207 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003208 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003209 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003210 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003211 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3212 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003213 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003214 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003215 break;
3216 }
3217
3218 case Primitive::kPrimShort:
3219 case Primitive::kPrimChar: {
3220 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003221 if (index.IsConstant()) {
3222 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003223 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003224 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003225 } else {
3226 __ movw(Address(obj, offset),
3227 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3228 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003229 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003230 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003231 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3232 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003233 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003234 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003235 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3236 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003237 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003238 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003239 break;
3240 }
3241
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003242 case Primitive::kPrimInt:
3243 case Primitive::kPrimNot: {
3244 if (!needs_runtime_call) {
3245 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3246 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003247 size_t offset =
3248 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003249 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003250 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003251 } else {
3252 DCHECK(value.IsConstant()) << value;
3253 __ movl(Address(obj, offset),
3254 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3255 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003256 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003257 DCHECK(index.IsRegister()) << index;
3258 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003259 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3260 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003261 } else {
3262 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003263 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003264 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3265 }
3266 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003267 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003268
3269 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003270 Register temp = locations->GetTemp(0).AsRegister<Register>();
3271 Register card = locations->GetTemp(1).AsRegister<Register>();
3272 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003273 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003274 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003275 DCHECK_EQ(value_type, Primitive::kPrimNot);
3276 DCHECK(!codegen_->IsLeafMethod());
3277 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3278 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003279 }
3280 break;
3281 }
3282
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003283 case Primitive::kPrimLong: {
3284 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003285 if (index.IsConstant()) {
3286 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003287 if (value.IsRegisterPair()) {
3288 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003289 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003290 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003291 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003292 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003293 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3294 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003295 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003296 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3297 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003298 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003299 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003300 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003301 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003302 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003303 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003304 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003305 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003306 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003307 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003308 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003309 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003310 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003311 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003312 Immediate(High32Bits(val)));
3313 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003314 }
3315 break;
3316 }
3317
Mark Mendell7c8d0092015-01-26 11:21:33 -05003318 case Primitive::kPrimFloat: {
3319 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3320 DCHECK(value.IsFpuRegister());
3321 if (index.IsConstant()) {
3322 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3323 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3324 } else {
3325 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3326 value.AsFpuRegister<XmmRegister>());
3327 }
3328 break;
3329 }
3330
3331 case Primitive::kPrimDouble: {
3332 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3333 DCHECK(value.IsFpuRegister());
3334 if (index.IsConstant()) {
3335 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3336 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3337 } else {
3338 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3339 value.AsFpuRegister<XmmRegister>());
3340 }
3341 break;
3342 }
3343
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003344 case Primitive::kPrimVoid:
3345 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003346 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003347 }
3348}
3349
3350void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3351 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003352 locations->SetInAt(0, Location::RequiresRegister());
3353 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003354 instruction->SetLocations(locations);
3355}
3356
3357void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3358 LocationSummary* locations = instruction->GetLocations();
3359 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003360 Register obj = locations->InAt(0).AsRegister<Register>();
3361 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003362 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003363 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003364}
3365
3366void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003367 LocationSummary* locations =
3368 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003369 locations->SetInAt(0, Location::RequiresRegister());
3370 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003371 if (instruction->HasUses()) {
3372 locations->SetOut(Location::SameAsFirstInput());
3373 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003374}
3375
3376void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3377 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003378 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01003379 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003380 codegen_->AddSlowPath(slow_path);
3381
Roland Levillain271ab9c2014-11-27 15:23:57 +00003382 Register index = locations->InAt(0).AsRegister<Register>();
3383 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003384
3385 __ cmpl(index, length);
3386 __ j(kAboveEqual, slow_path->GetEntryLabel());
3387}
3388
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003389void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3390 temp->SetLocations(nullptr);
3391}
3392
3393void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3394 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003395 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003396}
3397
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003398void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003399 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003400 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003401}
3402
3403void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003404 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3405}
3406
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003407void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3408 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3409}
3410
3411void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003412 HBasicBlock* block = instruction->GetBlock();
3413 if (block->GetLoopInformation() != nullptr) {
3414 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3415 // The back edge will generate the suspend check.
3416 return;
3417 }
3418 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3419 // The goto will generate the suspend check.
3420 return;
3421 }
3422 GenerateSuspendCheck(instruction, nullptr);
3423}
3424
3425void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3426 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003427 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003428 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003429 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003430 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003431 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003432 if (successor == nullptr) {
3433 __ j(kNotEqual, slow_path->GetEntryLabel());
3434 __ Bind(slow_path->GetReturnLabel());
3435 } else {
3436 __ j(kEqual, codegen_->GetLabelOf(successor));
3437 __ jmp(slow_path->GetEntryLabel());
3438 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003439}
3440
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003441X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3442 return codegen_->GetAssembler();
3443}
3444
Mark Mendell7c8d0092015-01-26 11:21:33 -05003445void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003446 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003447 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003448 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003449 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05003450 __ movl(temp_reg, Address(ESP, src + stack_offset));
3451 __ movl(Address(ESP, dst + stack_offset), temp_reg);
3452}
3453
3454void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
3455 ScratchRegisterScope ensure_scratch(
3456 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3457 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3458 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3459 __ movl(temp_reg, Address(ESP, src + stack_offset));
3460 __ movl(Address(ESP, dst + stack_offset), temp_reg);
3461 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
3462 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003463}
3464
3465void ParallelMoveResolverX86::EmitMove(size_t index) {
3466 MoveOperands* move = moves_.Get(index);
3467 Location source = move->GetSource();
3468 Location destination = move->GetDestination();
3469
3470 if (source.IsRegister()) {
3471 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003472 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003473 } else {
3474 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003475 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003476 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003477 } else if (source.IsFpuRegister()) {
3478 if (destination.IsFpuRegister()) {
3479 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3480 } else if (destination.IsStackSlot()) {
3481 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3482 } else {
3483 DCHECK(destination.IsDoubleStackSlot());
3484 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3485 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003486 } else if (source.IsStackSlot()) {
3487 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003488 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003489 } else if (destination.IsFpuRegister()) {
3490 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003491 } else {
3492 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003493 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
3494 }
3495 } else if (source.IsDoubleStackSlot()) {
3496 if (destination.IsFpuRegister()) {
3497 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
3498 } else {
3499 DCHECK(destination.IsDoubleStackSlot()) << destination;
3500 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003501 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003502 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003503 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003504 if (constant->IsIntConstant() || constant->IsNullConstant()) {
3505 Immediate imm(CodeGenerator::GetInt32ValueOf(constant));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003506 if (destination.IsRegister()) {
3507 __ movl(destination.AsRegister<Register>(), imm);
3508 } else {
3509 DCHECK(destination.IsStackSlot()) << destination;
3510 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3511 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003512 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003513 DCHECK(constant->IsFloatConstant());
3514 float value = constant->AsFloatConstant()->GetValue();
3515 Immediate imm(bit_cast<float, int32_t>(value));
3516 if (destination.IsFpuRegister()) {
3517 ScratchRegisterScope ensure_scratch(
3518 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3519 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
3520 __ movl(temp, imm);
3521 __ movd(destination.AsFpuRegister<XmmRegister>(), temp);
3522 } else {
3523 DCHECK(destination.IsStackSlot()) << destination;
3524 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3525 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003526 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003527 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003528 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003529 }
3530}
3531
3532void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003533 Register suggested_scratch = reg == EAX ? EBX : EAX;
3534 ScratchRegisterScope ensure_scratch(
3535 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3536
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003537 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3538 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
3539 __ movl(Address(ESP, mem + stack_offset), reg);
3540 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
3541}
3542
Mark Mendell7c8d0092015-01-26 11:21:33 -05003543void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
3544 ScratchRegisterScope ensure_scratch(
3545 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3546
3547 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3548 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3549 __ movl(temp_reg, Address(ESP, mem + stack_offset));
3550 __ movss(Address(ESP, mem + stack_offset), reg);
3551 __ movd(reg, temp_reg);
3552}
3553
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003554void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
3555 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003556 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3557
3558 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003559 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003560 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3561
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003562 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
3563 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
3564 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
3565 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
3566 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
3567 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
3568}
3569
3570void ParallelMoveResolverX86::EmitSwap(size_t index) {
3571 MoveOperands* move = moves_.Get(index);
3572 Location source = move->GetSource();
3573 Location destination = move->GetDestination();
3574
3575 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003576 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003577 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003578 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003579 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003580 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003581 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3582 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003583 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
3584 // Use XOR Swap algorithm to avoid a temporary.
3585 DCHECK_NE(source.reg(), destination.reg());
3586 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3587 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3588 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3589 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
3590 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
3591 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
3592 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003593 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003594 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003595 }
3596}
3597
3598void ParallelMoveResolverX86::SpillScratch(int reg) {
3599 __ pushl(static_cast<Register>(reg));
3600}
3601
3602void ParallelMoveResolverX86::RestoreScratch(int reg) {
3603 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003604}
3605
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003606void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003607 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3608 ? LocationSummary::kCallOnSlowPath
3609 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003610 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003611 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003612 locations->SetOut(Location::RequiresRegister());
3613}
3614
3615void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003616 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003617 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003618 DCHECK(!cls->CanCallRuntime());
3619 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003620 codegen_->LoadCurrentMethod(out);
3621 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3622 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003623 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003624 codegen_->LoadCurrentMethod(out);
3625 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3626 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003627
3628 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3629 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3630 codegen_->AddSlowPath(slow_path);
3631 __ testl(out, out);
3632 __ j(kEqual, slow_path->GetEntryLabel());
3633 if (cls->MustGenerateClinitCheck()) {
3634 GenerateClassInitializationCheck(slow_path, out);
3635 } else {
3636 __ Bind(slow_path->GetExitLabel());
3637 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003638 }
3639}
3640
3641void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
3642 LocationSummary* locations =
3643 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3644 locations->SetInAt(0, Location::RequiresRegister());
3645 if (check->HasUses()) {
3646 locations->SetOut(Location::SameAsFirstInput());
3647 }
3648}
3649
3650void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003651 // We assume the class to not be null.
3652 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3653 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003654 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003655 GenerateClassInitializationCheck(slow_path,
3656 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003657}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003658
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003659void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
3660 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003661 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3662 Immediate(mirror::Class::kStatusInitialized));
3663 __ j(kLess, slow_path->GetEntryLabel());
3664 __ Bind(slow_path->GetExitLabel());
3665 // No need for memory fence, thanks to the X86 memory model.
3666}
3667
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003668void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
3669 LocationSummary* locations =
3670 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3671 locations->SetOut(Location::RequiresRegister());
3672}
3673
3674void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
3675 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
3676 codegen_->AddSlowPath(slow_path);
3677
Roland Levillain271ab9c2014-11-27 15:23:57 +00003678 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003679 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08003680 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3681 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003682 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3683 __ testl(out, out);
3684 __ j(kEqual, slow_path->GetEntryLabel());
3685 __ Bind(slow_path->GetExitLabel());
3686}
3687
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003688void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
3689 LocationSummary* locations =
3690 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3691 locations->SetOut(Location::RequiresRegister());
3692}
3693
3694void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
3695 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003696 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003697 __ fs()->movl(address, Immediate(0));
3698}
3699
3700void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
3701 LocationSummary* locations =
3702 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3703 InvokeRuntimeCallingConvention calling_convention;
3704 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3705}
3706
3707void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
3708 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
3709 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3710}
3711
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003712void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003713 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3714 ? LocationSummary::kNoCall
3715 : LocationSummary::kCallOnSlowPath;
3716 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3717 locations->SetInAt(0, Location::RequiresRegister());
3718 locations->SetInAt(1, Location::Any());
3719 locations->SetOut(Location::RequiresRegister());
3720}
3721
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003722void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003723 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003724 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003725 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003726 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003727 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3728 Label done, zero;
3729 SlowPathCodeX86* slow_path = nullptr;
3730
3731 // Return 0 if `obj` is null.
3732 // TODO: avoid this check if we know obj is not null.
3733 __ testl(obj, obj);
3734 __ j(kEqual, &zero);
3735 __ movl(out, Address(obj, class_offset));
3736 // Compare the class of `obj` with `cls`.
3737 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003738 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003739 } else {
3740 DCHECK(cls.IsStackSlot()) << cls;
3741 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
3742 }
3743
3744 if (instruction->IsClassFinal()) {
3745 // Classes must be equal for the instanceof to succeed.
3746 __ j(kNotEqual, &zero);
3747 __ movl(out, Immediate(1));
3748 __ jmp(&done);
3749 } else {
3750 // If the classes are not equal, we go into a slow path.
3751 DCHECK(locations->OnlyCallsOnSlowPath());
3752 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003753 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003754 codegen_->AddSlowPath(slow_path);
3755 __ j(kNotEqual, slow_path->GetEntryLabel());
3756 __ movl(out, Immediate(1));
3757 __ jmp(&done);
3758 }
3759 __ Bind(&zero);
3760 __ movl(out, Immediate(0));
3761 if (slow_path != nullptr) {
3762 __ Bind(slow_path->GetExitLabel());
3763 }
3764 __ Bind(&done);
3765}
3766
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003767void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
3768 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3769 instruction, LocationSummary::kCallOnSlowPath);
3770 locations->SetInAt(0, Location::RequiresRegister());
3771 locations->SetInAt(1, Location::Any());
3772 locations->AddTemp(Location::RequiresRegister());
3773}
3774
3775void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
3776 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003777 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003778 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003779 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003780 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3781 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
3782 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3783 codegen_->AddSlowPath(slow_path);
3784
3785 // TODO: avoid this check if we know obj is not null.
3786 __ testl(obj, obj);
3787 __ j(kEqual, slow_path->GetExitLabel());
3788 __ movl(temp, Address(obj, class_offset));
3789
3790 // Compare the class of `obj` with `cls`.
3791 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003792 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003793 } else {
3794 DCHECK(cls.IsStackSlot()) << cls;
3795 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
3796 }
3797
3798 __ j(kNotEqual, slow_path->GetEntryLabel());
3799 __ Bind(slow_path->GetExitLabel());
3800}
3801
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003802void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3803 LocationSummary* locations =
3804 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3805 InvokeRuntimeCallingConvention calling_convention;
3806 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3807}
3808
3809void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3810 __ fs()->call(Address::Absolute(instruction->IsEnter()
3811 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
3812 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
3813 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3814}
3815
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003816void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3817void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3818void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3819
3820void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3821 LocationSummary* locations =
3822 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3823 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3824 || instruction->GetResultType() == Primitive::kPrimLong);
3825 locations->SetInAt(0, Location::RequiresRegister());
3826 locations->SetInAt(1, Location::Any());
3827 locations->SetOut(Location::SameAsFirstInput());
3828}
3829
3830void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
3831 HandleBitwiseOperation(instruction);
3832}
3833
3834void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
3835 HandleBitwiseOperation(instruction);
3836}
3837
3838void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
3839 HandleBitwiseOperation(instruction);
3840}
3841
3842void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3843 LocationSummary* locations = instruction->GetLocations();
3844 Location first = locations->InAt(0);
3845 Location second = locations->InAt(1);
3846 DCHECK(first.Equals(locations->Out()));
3847
3848 if (instruction->GetResultType() == Primitive::kPrimInt) {
3849 if (second.IsRegister()) {
3850 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003851 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003852 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003853 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003854 } else {
3855 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003856 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003857 }
3858 } else if (second.IsConstant()) {
3859 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003860 __ andl(first.AsRegister<Register>(),
3861 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003862 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003863 __ orl(first.AsRegister<Register>(),
3864 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003865 } else {
3866 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00003867 __ xorl(first.AsRegister<Register>(),
3868 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003869 }
3870 } else {
3871 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003872 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003873 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003874 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003875 } else {
3876 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003877 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003878 }
3879 }
3880 } else {
3881 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3882 if (second.IsRegisterPair()) {
3883 if (instruction->IsAnd()) {
3884 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3885 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3886 } else if (instruction->IsOr()) {
3887 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3888 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3889 } else {
3890 DCHECK(instruction->IsXor());
3891 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3892 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3893 }
3894 } else {
3895 if (instruction->IsAnd()) {
3896 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3897 __ andl(first.AsRegisterPairHigh<Register>(),
3898 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3899 } else if (instruction->IsOr()) {
3900 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3901 __ orl(first.AsRegisterPairHigh<Register>(),
3902 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3903 } else {
3904 DCHECK(instruction->IsXor());
3905 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3906 __ xorl(first.AsRegisterPairHigh<Register>(),
3907 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3908 }
3909 }
3910 }
3911}
3912
Calin Juravleb1498f62015-02-16 13:13:29 +00003913void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
3914 // Nothing to do, this should be removed during prepare for register allocator.
3915 UNUSED(instruction);
3916 LOG(FATAL) << "Unreachable";
3917}
3918
3919void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
3920 // Nothing to do, this should be removed during prepare for register allocator.
3921 UNUSED(instruction);
3922 LOG(FATAL) << "Unreachable";
3923}
3924
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003925} // namespace x86
3926} // namespace art