blob: 1a95f418bccda112472e3077f6774bf2c338b68c [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();
605 int32_t value;
606 if (constant->IsIntConstant()) {
607 value = constant->AsIntConstant()->GetValue();
608 } else {
609 DCHECK(constant->IsFloatConstant());
610 value = bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue());
611 }
612 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100613 } else {
614 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100615 __ pushl(Address(ESP, source.GetStackIndex()));
616 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100617 }
618 }
619}
620
621void CodeGeneratorX86::Move64(Location destination, Location source) {
622 if (source.Equals(destination)) {
623 return;
624 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100625 if (destination.IsRegisterPair()) {
626 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000627 EmitParallelMoves(
628 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
629 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
630 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
631 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100632 } else if (source.IsFpuRegister()) {
633 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100634 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000635 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100636 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100637 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
638 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100639 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
640 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100641 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500642 if (source.IsFpuRegister()) {
643 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
644 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000645 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100646 } else {
647 LOG(FATAL) << "Unimplemented";
648 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100649 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000650 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100651 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000652 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100653 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100654 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100655 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100656 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000657 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100658 } else {
659 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000660 EmitParallelMoves(
661 Location::StackSlot(source.GetStackIndex()),
662 Location::StackSlot(destination.GetStackIndex()),
663 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
664 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100665 }
666 }
667}
668
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100669void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000670 LocationSummary* locations = instruction->GetLocations();
671 if (locations != nullptr && locations->Out().Equals(location)) {
672 return;
673 }
674
675 if (locations != nullptr && locations->Out().IsConstant()) {
676 HConstant* const_to_move = locations->Out().GetConstant();
677 if (const_to_move->IsIntConstant()) {
678 Immediate imm(const_to_move->AsIntConstant()->GetValue());
679 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000680 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000681 } else if (location.IsStackSlot()) {
682 __ movl(Address(ESP, location.GetStackIndex()), imm);
683 } else {
684 DCHECK(location.IsConstant());
685 DCHECK_EQ(location.GetConstant(), const_to_move);
686 }
687 } else if (const_to_move->IsLongConstant()) {
688 int64_t value = const_to_move->AsLongConstant()->GetValue();
689 if (location.IsRegisterPair()) {
690 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
691 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
692 } else if (location.IsDoubleStackSlot()) {
693 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000694 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
695 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000696 } else {
697 DCHECK(location.IsConstant());
698 DCHECK_EQ(location.GetConstant(), instruction);
699 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100700 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000701 } else if (instruction->IsTemporary()) {
702 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000703 if (temp_location.IsStackSlot()) {
704 Move32(location, temp_location);
705 } else {
706 DCHECK(temp_location.IsDoubleStackSlot());
707 Move64(location, temp_location);
708 }
Roland Levillain476df552014-10-09 17:51:36 +0100709 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100710 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100711 switch (instruction->GetType()) {
712 case Primitive::kPrimBoolean:
713 case Primitive::kPrimByte:
714 case Primitive::kPrimChar:
715 case Primitive::kPrimShort:
716 case Primitive::kPrimInt:
717 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100718 case Primitive::kPrimFloat:
719 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100720 break;
721
722 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100723 case Primitive::kPrimDouble:
724 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100725 break;
726
727 default:
728 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
729 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000730 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100731 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100732 switch (instruction->GetType()) {
733 case Primitive::kPrimBoolean:
734 case Primitive::kPrimByte:
735 case Primitive::kPrimChar:
736 case Primitive::kPrimShort:
737 case Primitive::kPrimInt:
738 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100739 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000740 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100741 break;
742
743 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100744 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000745 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100746 break;
747
748 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100749 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100750 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000751 }
752}
753
754void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000755 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000756}
757
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000758void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000759 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100760 DCHECK(!successor->IsExitBlock());
761
762 HBasicBlock* block = got->GetBlock();
763 HInstruction* previous = got->GetPrevious();
764
765 HLoopInformation* info = block->GetLoopInformation();
766 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
767 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
768 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
769 return;
770 }
771
772 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
773 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
774 }
775 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000776 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000777 }
778}
779
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000780void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000781 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000782}
783
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000784void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700785 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000786 if (kIsDebugBuild) {
787 __ Comment("Unreachable");
788 __ int3();
789 }
790}
791
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000792void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100793 LocationSummary* locations =
794 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100795 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100796 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100797 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100798 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000799}
800
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000801void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700802 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100803 if (cond->IsIntConstant()) {
804 // Constant condition, statically compared against 1.
805 int32_t cond_value = cond->AsIntConstant()->GetValue();
806 if (cond_value == 1) {
807 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
808 if_instr->IfTrueSuccessor())) {
809 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100810 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100811 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100812 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100813 DCHECK_EQ(cond_value, 0);
814 }
815 } else {
816 bool materialized =
817 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
818 // Moves do not affect the eflags register, so if the condition is
819 // evaluated just before the if, we don't need to evaluate it
820 // again.
821 bool eflags_set = cond->IsCondition()
822 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
823 if (materialized) {
824 if (!eflags_set) {
825 // Materialized condition, compare against 0.
826 Location lhs = if_instr->GetLocations()->InAt(0);
827 if (lhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000828 __ cmpl(lhs.AsRegister<Register>(), Immediate(0));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100829 } else {
830 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
831 }
832 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
833 } else {
834 __ j(X86Condition(cond->AsCondition()->GetCondition()),
835 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
836 }
837 } else {
838 Location lhs = cond->GetLocations()->InAt(0);
839 Location rhs = cond->GetLocations()->InAt(1);
840 // LHS is guaranteed to be in a register (see
841 // LocationsBuilderX86::VisitCondition).
842 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000843 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100844 } else if (rhs.IsConstant()) {
845 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
846 Immediate imm(instruction->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000847 __ cmpl(lhs.AsRegister<Register>(), imm);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100848 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000849 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100850 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100851 __ j(X86Condition(cond->AsCondition()->GetCondition()),
852 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700853 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100854 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100855 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
856 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700857 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000858 }
859}
860
861void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000862 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000863}
864
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000865void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
866 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000867}
868
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000869void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100870 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000871}
872
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000873void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100874 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700875 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000876}
877
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100878void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100879 LocationSummary* locations =
880 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100881 switch (store->InputAt(1)->GetType()) {
882 case Primitive::kPrimBoolean:
883 case Primitive::kPrimByte:
884 case Primitive::kPrimChar:
885 case Primitive::kPrimShort:
886 case Primitive::kPrimInt:
887 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100888 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100889 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
890 break;
891
892 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100893 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100894 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
895 break;
896
897 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100898 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100899 }
900 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000901}
902
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000903void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700904 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000905}
906
Dave Allison20dfc792014-06-16 20:44:29 -0700907void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100908 LocationSummary* locations =
909 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100910 locations->SetInAt(0, Location::RequiresRegister());
911 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100912 if (comp->NeedsMaterialization()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000913 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100914 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000915}
916
Dave Allison20dfc792014-06-16 20:44:29 -0700917void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
918 if (comp->NeedsMaterialization()) {
919 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000920 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100921 // Clear register: setcc only sets the low byte.
922 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700923 if (locations->InAt(1).IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000924 __ cmpl(locations->InAt(0).AsRegister<Register>(),
925 locations->InAt(1).AsRegister<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100926 } else if (locations->InAt(1).IsConstant()) {
927 HConstant* instruction = locations->InAt(1).GetConstant();
928 Immediate imm(instruction->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000929 __ cmpl(locations->InAt(0).AsRegister<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700930 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000931 __ cmpl(locations->InAt(0).AsRegister<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700932 Address(ESP, locations->InAt(1).GetStackIndex()));
933 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000934 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100935 }
Dave Allison20dfc792014-06-16 20:44:29 -0700936}
937
938void LocationsBuilderX86::VisitEqual(HEqual* comp) {
939 VisitCondition(comp);
940}
941
942void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
943 VisitCondition(comp);
944}
945
946void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
947 VisitCondition(comp);
948}
949
950void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
951 VisitCondition(comp);
952}
953
954void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
955 VisitCondition(comp);
956}
957
958void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
959 VisitCondition(comp);
960}
961
962void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
963 VisitCondition(comp);
964}
965
966void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
967 VisitCondition(comp);
968}
969
970void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
971 VisitCondition(comp);
972}
973
974void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
975 VisitCondition(comp);
976}
977
978void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
979 VisitCondition(comp);
980}
981
982void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
983 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000984}
985
986void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100987 LocationSummary* locations =
988 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100989 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000990}
991
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000992void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100993 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700994 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000995}
996
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100997void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100998 LocationSummary* locations =
999 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001000 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001001}
1002
1003void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1004 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001005 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001006}
1007
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001008void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1009 LocationSummary* locations =
1010 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1011 locations->SetOut(Location::ConstantLocation(constant));
1012}
1013
1014void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1015 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001016 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001017}
1018
1019void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1020 LocationSummary* locations =
1021 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1022 locations->SetOut(Location::ConstantLocation(constant));
1023}
1024
1025void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1026 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001027 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001028}
1029
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001030void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001031 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001032}
1033
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001034void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001035 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001036 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001037 __ ret();
1038}
1039
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001040void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001041 LocationSummary* locations =
1042 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001043 switch (ret->InputAt(0)->GetType()) {
1044 case Primitive::kPrimBoolean:
1045 case Primitive::kPrimByte:
1046 case Primitive::kPrimChar:
1047 case Primitive::kPrimShort:
1048 case Primitive::kPrimInt:
1049 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001050 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001051 break;
1052
1053 case Primitive::kPrimLong:
1054 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001055 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001056 break;
1057
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001058 case Primitive::kPrimFloat:
1059 case Primitive::kPrimDouble:
1060 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001061 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001062 break;
1063
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001064 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001065 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001066 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001067}
1068
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001069void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001070 if (kIsDebugBuild) {
1071 switch (ret->InputAt(0)->GetType()) {
1072 case Primitive::kPrimBoolean:
1073 case Primitive::kPrimByte:
1074 case Primitive::kPrimChar:
1075 case Primitive::kPrimShort:
1076 case Primitive::kPrimInt:
1077 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001078 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001079 break;
1080
1081 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001082 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1083 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001084 break;
1085
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001086 case Primitive::kPrimFloat:
1087 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001088 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001089 break;
1090
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001091 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001092 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001093 }
1094 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001095 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001096 __ ret();
1097}
1098
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001099void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001100 HandleInvoke(invoke);
1101}
1102
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001103void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001104 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001105
1106 // TODO: Implement all kinds of calls:
1107 // 1) boot -> boot
1108 // 2) app -> boot
1109 // 3) app -> app
1110 //
1111 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1112
1113 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001114 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001115 if (!invoke->IsRecursive()) {
1116 // temp = temp->dex_cache_resolved_methods_;
1117 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
1118 // temp = temp[index_in_cache]
1119 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
1120 // (temp + offset_of_quick_compiled_code)()
1121 __ call(Address(
1122 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
1123 } else {
1124 __ call(codegen_->GetFrameEntryLabel());
1125 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001126
1127 DCHECK(!codegen_->IsLeafMethod());
1128 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1129}
1130
1131void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1132 HandleInvoke(invoke);
1133}
1134
1135void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001136 LocationSummary* locations =
1137 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001138 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001139
1140 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001141 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001142 HInstruction* input = invoke->InputAt(i);
1143 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1144 }
1145
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001146 switch (invoke->GetType()) {
1147 case Primitive::kPrimBoolean:
1148 case Primitive::kPrimByte:
1149 case Primitive::kPrimChar:
1150 case Primitive::kPrimShort:
1151 case Primitive::kPrimInt:
1152 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001153 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001154 break;
1155
1156 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001157 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001158 break;
1159
1160 case Primitive::kPrimVoid:
1161 break;
1162
1163 case Primitive::kPrimDouble:
1164 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001165 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001166 break;
1167 }
1168
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001169 invoke->SetLocations(locations);
1170}
1171
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001172void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001173 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001174 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1175 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1176 LocationSummary* locations = invoke->GetLocations();
1177 Location receiver = locations->InAt(0);
1178 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1179 // temp = object->GetClass();
1180 if (receiver.IsStackSlot()) {
1181 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1182 __ movl(temp, Address(temp, class_offset));
1183 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001184 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001185 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001186 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001187 // temp = temp->GetMethodAt(method_offset);
1188 __ movl(temp, Address(temp, method_offset));
1189 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001190 __ call(Address(
1191 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001192
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001193 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001194 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001195}
1196
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001197void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1198 HandleInvoke(invoke);
1199 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001200 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001201}
1202
1203void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1204 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001205 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001206 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1207 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1208 LocationSummary* locations = invoke->GetLocations();
1209 Location receiver = locations->InAt(0);
1210 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1211
1212 // Set the hidden argument.
1213 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001214 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001215
1216 // temp = object->GetClass();
1217 if (receiver.IsStackSlot()) {
1218 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1219 __ movl(temp, Address(temp, class_offset));
1220 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001221 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001222 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001223 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001224 // temp = temp->GetImtEntryAt(method_offset);
1225 __ movl(temp, Address(temp, method_offset));
1226 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001227 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001228 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001229
1230 DCHECK(!codegen_->IsLeafMethod());
1231 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1232}
1233
Roland Levillain88cb1752014-10-20 16:36:47 +01001234void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1235 LocationSummary* locations =
1236 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1237 switch (neg->GetResultType()) {
1238 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001239 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001240 locations->SetInAt(0, Location::RequiresRegister());
1241 locations->SetOut(Location::SameAsFirstInput());
1242 break;
1243
Roland Levillain88cb1752014-10-20 16:36:47 +01001244 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001245 locations->SetInAt(0, Location::RequiresFpuRegister());
1246 locations->SetOut(Location::SameAsFirstInput());
1247 locations->AddTemp(Location::RequiresRegister());
1248 locations->AddTemp(Location::RequiresFpuRegister());
1249 break;
1250
Roland Levillain88cb1752014-10-20 16:36:47 +01001251 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001252 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001253 locations->SetOut(Location::SameAsFirstInput());
1254 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001255 break;
1256
1257 default:
1258 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1259 }
1260}
1261
1262void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1263 LocationSummary* locations = neg->GetLocations();
1264 Location out = locations->Out();
1265 Location in = locations->InAt(0);
1266 switch (neg->GetResultType()) {
1267 case Primitive::kPrimInt:
1268 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001269 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001270 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001271 break;
1272
1273 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001274 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001275 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001276 __ negl(out.AsRegisterPairLow<Register>());
1277 // Negation is similar to subtraction from zero. The least
1278 // significant byte triggers a borrow when it is different from
1279 // zero; to take it into account, add 1 to the most significant
1280 // byte if the carry flag (CF) is set to 1 after the first NEGL
1281 // operation.
1282 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1283 __ negl(out.AsRegisterPairHigh<Register>());
1284 break;
1285
Roland Levillain5368c212014-11-27 15:03:41 +00001286 case Primitive::kPrimFloat: {
1287 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001288 Register constant = locations->GetTemp(0).AsRegister<Register>();
1289 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001290 // Implement float negation with an exclusive or with value
1291 // 0x80000000 (mask for bit 31, representing the sign of a
1292 // single-precision floating-point number).
1293 __ movl(constant, Immediate(INT32_C(0x80000000)));
1294 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001295 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001296 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001297 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001298
Roland Levillain5368c212014-11-27 15:03:41 +00001299 case Primitive::kPrimDouble: {
1300 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001301 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001302 // Implement double negation with an exclusive or with value
1303 // 0x8000000000000000 (mask for bit 63, representing the sign of
1304 // a double-precision floating-point number).
1305 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001306 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001307 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001308 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001309
1310 default:
1311 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1312 }
1313}
1314
Roland Levillaindff1f282014-11-05 14:15:05 +00001315void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001316 Primitive::Type result_type = conversion->GetResultType();
1317 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001318 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001319
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001320 // The float-to-long and double-to-long type conversions rely on a
1321 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001322 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001323 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1324 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001325 ? LocationSummary::kCall
1326 : LocationSummary::kNoCall;
1327 LocationSummary* locations =
1328 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1329
Roland Levillaindff1f282014-11-05 14:15:05 +00001330 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001331 case Primitive::kPrimByte:
1332 switch (input_type) {
1333 case Primitive::kPrimShort:
1334 case Primitive::kPrimInt:
1335 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001336 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001337 locations->SetInAt(0, Location::Any());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001338 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1339 break;
1340
1341 default:
1342 LOG(FATAL) << "Unexpected type conversion from " << input_type
1343 << " to " << result_type;
1344 }
1345 break;
1346
Roland Levillain01a8d712014-11-14 16:27:39 +00001347 case Primitive::kPrimShort:
1348 switch (input_type) {
1349 case Primitive::kPrimByte:
1350 case Primitive::kPrimInt:
1351 case Primitive::kPrimChar:
1352 // Processing a Dex `int-to-short' instruction.
1353 locations->SetInAt(0, Location::Any());
1354 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1355 break;
1356
1357 default:
1358 LOG(FATAL) << "Unexpected type conversion from " << input_type
1359 << " to " << result_type;
1360 }
1361 break;
1362
Roland Levillain946e1432014-11-11 17:35:19 +00001363 case Primitive::kPrimInt:
1364 switch (input_type) {
1365 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001366 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001367 locations->SetInAt(0, Location::Any());
1368 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1369 break;
1370
1371 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001372 // Processing a Dex `float-to-int' instruction.
1373 locations->SetInAt(0, Location::RequiresFpuRegister());
1374 locations->SetOut(Location::RequiresRegister());
1375 locations->AddTemp(Location::RequiresFpuRegister());
1376 break;
1377
Roland Levillain946e1432014-11-11 17:35:19 +00001378 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001379 // Processing a Dex `double-to-int' instruction.
1380 locations->SetInAt(0, Location::RequiresFpuRegister());
1381 locations->SetOut(Location::RequiresRegister());
1382 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001383 break;
1384
1385 default:
1386 LOG(FATAL) << "Unexpected type conversion from " << input_type
1387 << " to " << result_type;
1388 }
1389 break;
1390
Roland Levillaindff1f282014-11-05 14:15:05 +00001391 case Primitive::kPrimLong:
1392 switch (input_type) {
1393 case Primitive::kPrimByte:
1394 case Primitive::kPrimShort:
1395 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001396 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001397 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001398 locations->SetInAt(0, Location::RegisterLocation(EAX));
1399 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1400 break;
1401
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001402 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001403 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001404 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001405 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001406 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1407 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1408
Vladimir Marko949c91f2015-01-27 10:48:44 +00001409 // The runtime helper puts the result in EAX, EDX.
1410 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001411 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001412 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001413
1414 default:
1415 LOG(FATAL) << "Unexpected type conversion from " << input_type
1416 << " to " << result_type;
1417 }
1418 break;
1419
Roland Levillain981e4542014-11-14 11:47:14 +00001420 case Primitive::kPrimChar:
1421 switch (input_type) {
1422 case Primitive::kPrimByte:
1423 case Primitive::kPrimShort:
1424 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001425 // Processing a Dex `int-to-char' instruction.
1426 locations->SetInAt(0, Location::Any());
1427 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1428 break;
1429
1430 default:
1431 LOG(FATAL) << "Unexpected type conversion from " << input_type
1432 << " to " << result_type;
1433 }
1434 break;
1435
Roland Levillaindff1f282014-11-05 14:15:05 +00001436 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001437 switch (input_type) {
1438 case Primitive::kPrimByte:
1439 case Primitive::kPrimShort:
1440 case Primitive::kPrimInt:
1441 case Primitive::kPrimChar:
1442 // Processing a Dex `int-to-float' instruction.
1443 locations->SetInAt(0, Location::RequiresRegister());
1444 locations->SetOut(Location::RequiresFpuRegister());
1445 break;
1446
1447 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001448 // Processing a Dex `long-to-float' instruction.
1449 locations->SetInAt(0, Location::RequiresRegister());
1450 locations->SetOut(Location::RequiresFpuRegister());
1451 locations->AddTemp(Location::RequiresFpuRegister());
1452 locations->AddTemp(Location::RequiresFpuRegister());
1453 break;
1454
Roland Levillaincff13742014-11-17 14:32:17 +00001455 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001456 // Processing a Dex `double-to-float' instruction.
1457 locations->SetInAt(0, Location::RequiresFpuRegister());
1458 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001459 break;
1460
1461 default:
1462 LOG(FATAL) << "Unexpected type conversion from " << input_type
1463 << " to " << result_type;
1464 };
1465 break;
1466
Roland Levillaindff1f282014-11-05 14:15:05 +00001467 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001468 switch (input_type) {
1469 case Primitive::kPrimByte:
1470 case Primitive::kPrimShort:
1471 case Primitive::kPrimInt:
1472 case Primitive::kPrimChar:
1473 // Processing a Dex `int-to-double' instruction.
1474 locations->SetInAt(0, Location::RequiresRegister());
1475 locations->SetOut(Location::RequiresFpuRegister());
1476 break;
1477
1478 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001479 // Processing a Dex `long-to-double' instruction.
1480 locations->SetInAt(0, Location::RequiresRegister());
1481 locations->SetOut(Location::RequiresFpuRegister());
1482 locations->AddTemp(Location::RequiresFpuRegister());
1483 locations->AddTemp(Location::RequiresFpuRegister());
1484 break;
1485
Roland Levillaincff13742014-11-17 14:32:17 +00001486 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001487 // Processing a Dex `float-to-double' instruction.
1488 locations->SetInAt(0, Location::RequiresFpuRegister());
1489 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001490 break;
1491
1492 default:
1493 LOG(FATAL) << "Unexpected type conversion from " << input_type
1494 << " to " << result_type;
1495 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001496 break;
1497
1498 default:
1499 LOG(FATAL) << "Unexpected type conversion from " << input_type
1500 << " to " << result_type;
1501 }
1502}
1503
1504void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1505 LocationSummary* locations = conversion->GetLocations();
1506 Location out = locations->Out();
1507 Location in = locations->InAt(0);
1508 Primitive::Type result_type = conversion->GetResultType();
1509 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001510 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001511 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001512 case Primitive::kPrimByte:
1513 switch (input_type) {
1514 case Primitive::kPrimShort:
1515 case Primitive::kPrimInt:
1516 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001517 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001518 if (in.IsRegister()) {
1519 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
1520 } else if (in.IsStackSlot()) {
1521 __ movsxb(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
1522 } else {
1523 DCHECK(in.GetConstant()->IsIntConstant());
1524 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1525 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1526 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001527 break;
1528
1529 default:
1530 LOG(FATAL) << "Unexpected type conversion from " << input_type
1531 << " to " << result_type;
1532 }
1533 break;
1534
Roland Levillain01a8d712014-11-14 16:27:39 +00001535 case Primitive::kPrimShort:
1536 switch (input_type) {
1537 case Primitive::kPrimByte:
1538 case Primitive::kPrimInt:
1539 case Primitive::kPrimChar:
1540 // Processing a Dex `int-to-short' instruction.
1541 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001542 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001543 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001544 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001545 } else {
1546 DCHECK(in.GetConstant()->IsIntConstant());
1547 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001548 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001549 }
1550 break;
1551
1552 default:
1553 LOG(FATAL) << "Unexpected type conversion from " << input_type
1554 << " to " << result_type;
1555 }
1556 break;
1557
Roland Levillain946e1432014-11-11 17:35:19 +00001558 case Primitive::kPrimInt:
1559 switch (input_type) {
1560 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001561 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001562 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001563 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001564 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001565 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001566 } else {
1567 DCHECK(in.IsConstant());
1568 DCHECK(in.GetConstant()->IsLongConstant());
1569 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001570 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001571 }
1572 break;
1573
Roland Levillain3f8f9362014-12-02 17:45:01 +00001574 case Primitive::kPrimFloat: {
1575 // Processing a Dex `float-to-int' instruction.
1576 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1577 Register output = out.AsRegister<Register>();
1578 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1579 Label done, nan;
1580
1581 __ movl(output, Immediate(kPrimIntMax));
1582 // temp = int-to-float(output)
1583 __ cvtsi2ss(temp, output);
1584 // if input >= temp goto done
1585 __ comiss(input, temp);
1586 __ j(kAboveEqual, &done);
1587 // if input == NaN goto nan
1588 __ j(kUnordered, &nan);
1589 // output = float-to-int-truncate(input)
1590 __ cvttss2si(output, input);
1591 __ jmp(&done);
1592 __ Bind(&nan);
1593 // output = 0
1594 __ xorl(output, output);
1595 __ Bind(&done);
1596 break;
1597 }
1598
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001599 case Primitive::kPrimDouble: {
1600 // Processing a Dex `double-to-int' instruction.
1601 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1602 Register output = out.AsRegister<Register>();
1603 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1604 Label done, nan;
1605
1606 __ movl(output, Immediate(kPrimIntMax));
1607 // temp = int-to-double(output)
1608 __ cvtsi2sd(temp, output);
1609 // if input >= temp goto done
1610 __ comisd(input, temp);
1611 __ j(kAboveEqual, &done);
1612 // if input == NaN goto nan
1613 __ j(kUnordered, &nan);
1614 // output = double-to-int-truncate(input)
1615 __ cvttsd2si(output, input);
1616 __ jmp(&done);
1617 __ Bind(&nan);
1618 // output = 0
1619 __ xorl(output, output);
1620 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001621 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001622 }
Roland Levillain946e1432014-11-11 17:35:19 +00001623
1624 default:
1625 LOG(FATAL) << "Unexpected type conversion from " << input_type
1626 << " to " << result_type;
1627 }
1628 break;
1629
Roland Levillaindff1f282014-11-05 14:15:05 +00001630 case Primitive::kPrimLong:
1631 switch (input_type) {
1632 case Primitive::kPrimByte:
1633 case Primitive::kPrimShort:
1634 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001635 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001636 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001637 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1638 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001639 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001640 __ cdq();
1641 break;
1642
1643 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001644 // Processing a Dex `float-to-long' instruction.
1645 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001646 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1647 break;
1648
Roland Levillaindff1f282014-11-05 14:15:05 +00001649 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001650 // Processing a Dex `double-to-long' instruction.
1651 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1652 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001653 break;
1654
1655 default:
1656 LOG(FATAL) << "Unexpected type conversion from " << input_type
1657 << " to " << result_type;
1658 }
1659 break;
1660
Roland Levillain981e4542014-11-14 11:47:14 +00001661 case Primitive::kPrimChar:
1662 switch (input_type) {
1663 case Primitive::kPrimByte:
1664 case Primitive::kPrimShort:
1665 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001666 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1667 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001668 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001669 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001670 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001671 } else {
1672 DCHECK(in.GetConstant()->IsIntConstant());
1673 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001674 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001675 }
1676 break;
1677
1678 default:
1679 LOG(FATAL) << "Unexpected type conversion from " << input_type
1680 << " to " << result_type;
1681 }
1682 break;
1683
Roland Levillaindff1f282014-11-05 14:15:05 +00001684 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001685 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001686 case Primitive::kPrimByte:
1687 case Primitive::kPrimShort:
1688 case Primitive::kPrimInt:
1689 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001690 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001691 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001692 break;
1693
Roland Levillain6d0e4832014-11-27 18:31:21 +00001694 case Primitive::kPrimLong: {
1695 // Processing a Dex `long-to-float' instruction.
1696 Register low = in.AsRegisterPairLow<Register>();
1697 Register high = in.AsRegisterPairHigh<Register>();
1698 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1699 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1700 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1701
1702 // Operations use doubles for precision reasons (each 32-bit
1703 // half of a long fits in the 53-bit mantissa of a double,
1704 // but not in the 24-bit mantissa of a float). This is
1705 // especially important for the low bits. The result is
1706 // eventually converted to float.
1707
1708 // low = low - 2^31 (to prevent bit 31 of `low` to be
1709 // interpreted as a sign bit)
1710 __ subl(low, Immediate(0x80000000));
1711 // temp = int-to-double(high)
1712 __ cvtsi2sd(temp, high);
1713 // temp = temp * 2^32
1714 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
1715 __ mulsd(temp, constant);
1716 // result = int-to-double(low)
1717 __ cvtsi2sd(result, low);
1718 // result = result + 2^31 (restore the original value of `low`)
1719 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
1720 __ addsd(result, constant);
1721 // result = result + temp
1722 __ addsd(result, temp);
1723 // result = double-to-float(result)
1724 __ cvtsd2ss(result, result);
1725 break;
1726 }
1727
Roland Levillaincff13742014-11-17 14:32:17 +00001728 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001729 // Processing a Dex `double-to-float' instruction.
1730 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001731 break;
1732
1733 default:
1734 LOG(FATAL) << "Unexpected type conversion from " << input_type
1735 << " to " << result_type;
1736 };
1737 break;
1738
Roland Levillaindff1f282014-11-05 14:15:05 +00001739 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001740 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001741 case Primitive::kPrimByte:
1742 case Primitive::kPrimShort:
1743 case Primitive::kPrimInt:
1744 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001745 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001746 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001747 break;
1748
Roland Levillain647b9ed2014-11-27 12:06:00 +00001749 case Primitive::kPrimLong: {
1750 // Processing a Dex `long-to-double' instruction.
1751 Register low = in.AsRegisterPairLow<Register>();
1752 Register high = in.AsRegisterPairHigh<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001753 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1754 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1755 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001756
Roland Levillain647b9ed2014-11-27 12:06:00 +00001757 // low = low - 2^31 (to prevent bit 31 of `low` to be
1758 // interpreted as a sign bit)
1759 __ subl(low, Immediate(0x80000000));
1760 // temp = int-to-double(high)
1761 __ cvtsi2sd(temp, high);
1762 // temp = temp * 2^32
Roland Levillain6d0e4832014-11-27 18:31:21 +00001763 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001764 __ mulsd(temp, constant);
1765 // result = int-to-double(low)
1766 __ cvtsi2sd(result, low);
1767 // result = result + 2^31 (restore the original value of `low`)
Roland Levillain6d0e4832014-11-27 18:31:21 +00001768 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001769 __ addsd(result, constant);
1770 // result = result + temp
1771 __ addsd(result, temp);
1772 break;
1773 }
1774
Roland Levillaincff13742014-11-17 14:32:17 +00001775 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001776 // Processing a Dex `float-to-double' instruction.
1777 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001778 break;
1779
1780 default:
1781 LOG(FATAL) << "Unexpected type conversion from " << input_type
1782 << " to " << result_type;
1783 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001784 break;
1785
1786 default:
1787 LOG(FATAL) << "Unexpected type conversion from " << input_type
1788 << " to " << result_type;
1789 }
1790}
1791
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001792void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001793 LocationSummary* locations =
1794 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001795 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001796 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001797 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001798 locations->SetInAt(0, Location::RequiresRegister());
1799 locations->SetInAt(1, Location::Any());
1800 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001801 break;
1802 }
1803
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001804 case Primitive::kPrimFloat:
1805 case Primitive::kPrimDouble: {
1806 locations->SetInAt(0, Location::RequiresFpuRegister());
1807 locations->SetInAt(1, Location::Any());
1808 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001809 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001810 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001811
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001812 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001813 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1814 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001815 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001816}
1817
1818void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1819 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001820 Location first = locations->InAt(0);
1821 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001822 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001823 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001824 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001825 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001826 __ addl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001827 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001828 __ addl(first.AsRegister<Register>(),
1829 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001830 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001831 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001832 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001833 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001834 }
1835
1836 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001837 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001838 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1839 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001840 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001841 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1842 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001843 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001844 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001845 break;
1846 }
1847
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001848 case Primitive::kPrimFloat: {
1849 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001850 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001851 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001852 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001853 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001854 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001855 }
1856
1857 case Primitive::kPrimDouble: {
1858 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001859 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001860 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001861 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001862 }
1863 break;
1864 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001865
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001866 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001867 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001868 }
1869}
1870
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001871void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001872 LocationSummary* locations =
1873 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001874 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001875 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001876 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001877 locations->SetInAt(0, Location::RequiresRegister());
1878 locations->SetInAt(1, Location::Any());
1879 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001880 break;
1881 }
Calin Juravle11351682014-10-23 15:38:15 +01001882 case Primitive::kPrimFloat:
1883 case Primitive::kPrimDouble: {
1884 locations->SetInAt(0, Location::RequiresFpuRegister());
1885 locations->SetInAt(1, Location::RequiresFpuRegister());
1886 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001887 break;
Calin Juravle11351682014-10-23 15:38:15 +01001888 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001889
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001890 default:
Calin Juravle11351682014-10-23 15:38:15 +01001891 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001892 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001893}
1894
1895void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1896 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001897 Location first = locations->InAt(0);
1898 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001899 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001900 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001901 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001902 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001903 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001904 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001905 __ subl(first.AsRegister<Register>(),
1906 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001907 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001908 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001909 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001910 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001911 }
1912
1913 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001914 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01001915 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1916 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001917 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001918 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001919 __ sbbl(first.AsRegisterPairHigh<Register>(),
1920 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001921 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001922 break;
1923 }
1924
Calin Juravle11351682014-10-23 15:38:15 +01001925 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001926 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001927 break;
Calin Juravle11351682014-10-23 15:38:15 +01001928 }
1929
1930 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001931 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001932 break;
1933 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001934
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001935 default:
Calin Juravle11351682014-10-23 15:38:15 +01001936 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001937 }
1938}
1939
Calin Juravle34bacdf2014-10-07 20:23:36 +01001940void LocationsBuilderX86::VisitMul(HMul* mul) {
1941 LocationSummary* locations =
1942 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1943 switch (mul->GetResultType()) {
1944 case Primitive::kPrimInt:
1945 locations->SetInAt(0, Location::RequiresRegister());
1946 locations->SetInAt(1, Location::Any());
1947 locations->SetOut(Location::SameAsFirstInput());
1948 break;
1949 case Primitive::kPrimLong: {
1950 locations->SetInAt(0, Location::RequiresRegister());
1951 // TODO: Currently this handles only stack operands:
1952 // - we don't have enough registers because we currently use Quick ABI.
1953 // - by the time we have a working register allocator we will probably change the ABI
1954 // and fix the above.
1955 // - we don't have a way yet to request operands on stack but the base line compiler
1956 // will leave the operands on the stack with Any().
1957 locations->SetInAt(1, Location::Any());
1958 locations->SetOut(Location::SameAsFirstInput());
1959 // Needed for imul on 32bits with 64bits output.
1960 locations->AddTemp(Location::RegisterLocation(EAX));
1961 locations->AddTemp(Location::RegisterLocation(EDX));
1962 break;
1963 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001964 case Primitive::kPrimFloat:
1965 case Primitive::kPrimDouble: {
1966 locations->SetInAt(0, Location::RequiresFpuRegister());
1967 locations->SetInAt(1, Location::RequiresFpuRegister());
1968 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001969 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001970 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001971
1972 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001973 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001974 }
1975}
1976
1977void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1978 LocationSummary* locations = mul->GetLocations();
1979 Location first = locations->InAt(0);
1980 Location second = locations->InAt(1);
1981 DCHECK(first.Equals(locations->Out()));
1982
1983 switch (mul->GetResultType()) {
1984 case Primitive::kPrimInt: {
1985 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001986 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001987 } else if (second.IsConstant()) {
1988 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001989 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001990 } else {
1991 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001992 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01001993 }
1994 break;
1995 }
1996
1997 case Primitive::kPrimLong: {
1998 DCHECK(second.IsDoubleStackSlot());
1999
2000 Register in1_hi = first.AsRegisterPairHigh<Register>();
2001 Register in1_lo = first.AsRegisterPairLow<Register>();
2002 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2003 Address in2_lo(ESP, second.GetStackIndex());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002004 Register eax = locations->GetTemp(0).AsRegister<Register>();
2005 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002006
2007 DCHECK_EQ(EAX, eax);
2008 DCHECK_EQ(EDX, edx);
2009
2010 // input: in1 - 64 bits, in2 - 64 bits
2011 // output: in1
2012 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2013 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2014 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
2015
2016 __ movl(eax, in2_hi);
2017 // eax <- in1.lo * in2.hi
2018 __ imull(eax, in1_lo);
2019 // in1.hi <- in1.hi * in2.lo
2020 __ imull(in1_hi, in2_lo);
2021 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2022 __ addl(in1_hi, eax);
2023 // move in1_lo to eax to prepare for double precision
2024 __ movl(eax, in1_lo);
2025 // edx:eax <- in1.lo * in2.lo
2026 __ mull(in2_lo);
2027 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2028 __ addl(in1_hi, edx);
2029 // in1.lo <- (in1.lo * in2.lo)[31:0];
2030 __ movl(in1_lo, eax);
2031
2032 break;
2033 }
2034
Calin Juravleb5bfa962014-10-21 18:02:24 +01002035 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002036 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002037 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002038 }
2039
2040 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002041 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002042 break;
2043 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002044
2045 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002046 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002047 }
2048}
2049
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002050void InstructionCodeGeneratorX86::PushOntoFPStack(Location source, uint32_t temp_offset,
2051 uint32_t stack_adjustment, bool is_float) {
2052 if (source.IsStackSlot()) {
2053 DCHECK(is_float);
2054 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2055 } else if (source.IsDoubleStackSlot()) {
2056 DCHECK(!is_float);
2057 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2058 } else {
2059 // Write the value to the temporary location on the stack and load to FP stack.
2060 if (is_float) {
2061 Location stack_temp = Location::StackSlot(temp_offset);
2062 codegen_->Move32(stack_temp, source);
2063 __ flds(Address(ESP, temp_offset));
2064 } else {
2065 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2066 codegen_->Move64(stack_temp, source);
2067 __ fldl(Address(ESP, temp_offset));
2068 }
2069 }
2070}
2071
2072void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2073 Primitive::Type type = rem->GetResultType();
2074 bool is_float = type == Primitive::kPrimFloat;
2075 size_t elem_size = Primitive::ComponentSize(type);
2076 LocationSummary* locations = rem->GetLocations();
2077 Location first = locations->InAt(0);
2078 Location second = locations->InAt(1);
2079 Location out = locations->Out();
2080
2081 // Create stack space for 2 elements.
2082 // TODO: enhance register allocator to ask for stack temporaries.
2083 __ subl(ESP, Immediate(2 * elem_size));
2084
2085 // Load the values to the FP stack in reverse order, using temporaries if needed.
2086 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2087 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2088
2089 // Loop doing FPREM until we stabilize.
2090 Label retry;
2091 __ Bind(&retry);
2092 __ fprem();
2093
2094 // Move FP status to AX.
2095 __ fstsw();
2096
2097 // And see if the argument reduction is complete. This is signaled by the
2098 // C2 FPU flag bit set to 0.
2099 __ andl(EAX, Immediate(kC2ConditionMask));
2100 __ j(kNotEqual, &retry);
2101
2102 // We have settled on the final value. Retrieve it into an XMM register.
2103 // Store FP top of stack to real stack.
2104 if (is_float) {
2105 __ fsts(Address(ESP, 0));
2106 } else {
2107 __ fstl(Address(ESP, 0));
2108 }
2109
2110 // Pop the 2 items from the FP stack.
2111 __ fucompp();
2112
2113 // Load the value from the stack into an XMM register.
2114 DCHECK(out.IsFpuRegister()) << out;
2115 if (is_float) {
2116 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2117 } else {
2118 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2119 }
2120
2121 // And remove the temporary stack space we allocated.
2122 __ addl(ESP, Immediate(2 * elem_size));
2123}
2124
Calin Juravlebacfec32014-11-14 15:54:36 +00002125void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2126 DCHECK(instruction->IsDiv() || instruction->IsRem());
2127
2128 LocationSummary* locations = instruction->GetLocations();
2129 Location out = locations->Out();
2130 Location first = locations->InAt(0);
2131 Location second = locations->InAt(1);
2132 bool is_div = instruction->IsDiv();
2133
2134 switch (instruction->GetResultType()) {
2135 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002136 Register second_reg = second.AsRegister<Register>();
2137 DCHECK_EQ(EAX, first.AsRegister<Register>());
2138 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002139
2140 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002141 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
2142 is_div);
Calin Juravlebacfec32014-11-14 15:54:36 +00002143 codegen_->AddSlowPath(slow_path);
2144
2145 // 0x80000000/-1 triggers an arithmetic exception!
2146 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2147 // it's safe to just use negl instead of more complex comparisons.
2148
2149 __ cmpl(second_reg, Immediate(-1));
2150 __ j(kEqual, slow_path->GetEntryLabel());
2151
2152 // edx:eax <- sign-extended of eax
2153 __ cdq();
2154 // eax = quotient, edx = remainder
2155 __ idivl(second_reg);
2156
2157 __ Bind(slow_path->GetExitLabel());
2158 break;
2159 }
2160
2161 case Primitive::kPrimLong: {
2162 InvokeRuntimeCallingConvention calling_convention;
2163 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2164 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2165 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2166 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2167 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2168 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2169
2170 if (is_div) {
2171 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2172 } else {
2173 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2174 }
2175 uint32_t dex_pc = is_div
2176 ? instruction->AsDiv()->GetDexPc()
2177 : instruction->AsRem()->GetDexPc();
2178 codegen_->RecordPcInfo(instruction, dex_pc);
2179
2180 break;
2181 }
2182
2183 default:
2184 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2185 }
2186}
2187
Calin Juravle7c4954d2014-10-28 16:57:40 +00002188void LocationsBuilderX86::VisitDiv(HDiv* div) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002189 LocationSummary::CallKind call_kind = div->GetResultType() == Primitive::kPrimLong
2190 ? LocationSummary::kCall
2191 : LocationSummary::kNoCall;
2192 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2193
Calin Juravle7c4954d2014-10-28 16:57:40 +00002194 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002195 case Primitive::kPrimInt: {
2196 locations->SetInAt(0, Location::RegisterLocation(EAX));
2197 locations->SetInAt(1, Location::RequiresRegister());
2198 locations->SetOut(Location::SameAsFirstInput());
2199 // Intel uses edx:eax as the dividend.
2200 locations->AddTemp(Location::RegisterLocation(EDX));
2201 break;
2202 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002203 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002204 InvokeRuntimeCallingConvention calling_convention;
2205 locations->SetInAt(0, Location::RegisterPairLocation(
2206 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2207 locations->SetInAt(1, Location::RegisterPairLocation(
2208 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2209 // Runtime helper puts the result in EAX, EDX.
2210 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002211 break;
2212 }
2213 case Primitive::kPrimFloat:
2214 case Primitive::kPrimDouble: {
2215 locations->SetInAt(0, Location::RequiresFpuRegister());
2216 locations->SetInAt(1, Location::RequiresFpuRegister());
2217 locations->SetOut(Location::SameAsFirstInput());
2218 break;
2219 }
2220
2221 default:
2222 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2223 }
2224}
2225
2226void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2227 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002228 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002229 Location first = locations->InAt(0);
2230 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002231
2232 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002233 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002234 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002235 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002236 break;
2237 }
2238
2239 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002240 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002241 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002242 break;
2243 }
2244
2245 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002246 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002247 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002248 break;
2249 }
2250
2251 default:
2252 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2253 }
2254}
2255
Calin Juravlebacfec32014-11-14 15:54:36 +00002256void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002257 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002258 LocationSummary* locations =
2259 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravlebacfec32014-11-14 15:54:36 +00002260
Calin Juravled2ec87d2014-12-08 14:24:46 +00002261 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002262 case Primitive::kPrimInt: {
2263 locations->SetInAt(0, Location::RegisterLocation(EAX));
2264 locations->SetInAt(1, Location::RequiresRegister());
2265 locations->SetOut(Location::RegisterLocation(EDX));
2266 break;
2267 }
2268 case Primitive::kPrimLong: {
2269 InvokeRuntimeCallingConvention calling_convention;
2270 locations->SetInAt(0, Location::RegisterPairLocation(
2271 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2272 locations->SetInAt(1, Location::RegisterPairLocation(
2273 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2274 // Runtime helper puts the result in EAX, EDX.
2275 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2276 break;
2277 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002278 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002279 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002280 locations->SetInAt(0, Location::Any());
2281 locations->SetInAt(1, Location::Any());
2282 locations->SetOut(Location::RequiresFpuRegister());
2283 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002284 break;
2285 }
2286
2287 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002288 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002289 }
2290}
2291
2292void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2293 Primitive::Type type = rem->GetResultType();
2294 switch (type) {
2295 case Primitive::kPrimInt:
2296 case Primitive::kPrimLong: {
2297 GenerateDivRemIntegral(rem);
2298 break;
2299 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002300 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002301 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002302 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002303 break;
2304 }
2305 default:
2306 LOG(FATAL) << "Unexpected rem type " << type;
2307 }
2308}
2309
Calin Juravled0d48522014-11-04 16:40:20 +00002310void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2311 LocationSummary* locations =
2312 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002313 switch (instruction->GetType()) {
2314 case Primitive::kPrimInt: {
2315 locations->SetInAt(0, Location::Any());
2316 break;
2317 }
2318 case Primitive::kPrimLong: {
2319 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2320 if (!instruction->IsConstant()) {
2321 locations->AddTemp(Location::RequiresRegister());
2322 }
2323 break;
2324 }
2325 default:
2326 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2327 }
Calin Juravled0d48522014-11-04 16:40:20 +00002328 if (instruction->HasUses()) {
2329 locations->SetOut(Location::SameAsFirstInput());
2330 }
2331}
2332
2333void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2334 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2335 codegen_->AddSlowPath(slow_path);
2336
2337 LocationSummary* locations = instruction->GetLocations();
2338 Location value = locations->InAt(0);
2339
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002340 switch (instruction->GetType()) {
2341 case Primitive::kPrimInt: {
2342 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002343 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002344 __ j(kEqual, slow_path->GetEntryLabel());
2345 } else if (value.IsStackSlot()) {
2346 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2347 __ j(kEqual, slow_path->GetEntryLabel());
2348 } else {
2349 DCHECK(value.IsConstant()) << value;
2350 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2351 __ jmp(slow_path->GetEntryLabel());
2352 }
2353 }
2354 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002355 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002356 case Primitive::kPrimLong: {
2357 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002358 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002359 __ movl(temp, value.AsRegisterPairLow<Register>());
2360 __ orl(temp, value.AsRegisterPairHigh<Register>());
2361 __ j(kEqual, slow_path->GetEntryLabel());
2362 } else {
2363 DCHECK(value.IsConstant()) << value;
2364 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2365 __ jmp(slow_path->GetEntryLabel());
2366 }
2367 }
2368 break;
2369 }
2370 default:
2371 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002372 }
Calin Juravled0d48522014-11-04 16:40:20 +00002373}
2374
Calin Juravle9aec02f2014-11-18 23:06:35 +00002375void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2376 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2377
2378 LocationSummary* locations =
2379 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2380
2381 switch (op->GetResultType()) {
2382 case Primitive::kPrimInt: {
2383 locations->SetInAt(0, Location::RequiresRegister());
2384 // The shift count needs to be in CL.
2385 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2386 locations->SetOut(Location::SameAsFirstInput());
2387 break;
2388 }
2389 case Primitive::kPrimLong: {
2390 locations->SetInAt(0, Location::RequiresRegister());
2391 // The shift count needs to be in CL.
2392 locations->SetInAt(1, Location::RegisterLocation(ECX));
2393 locations->SetOut(Location::SameAsFirstInput());
2394 break;
2395 }
2396 default:
2397 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2398 }
2399}
2400
2401void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2402 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2403
2404 LocationSummary* locations = op->GetLocations();
2405 Location first = locations->InAt(0);
2406 Location second = locations->InAt(1);
2407 DCHECK(first.Equals(locations->Out()));
2408
2409 switch (op->GetResultType()) {
2410 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002411 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002412 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002413 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002414 DCHECK_EQ(ECX, second_reg);
2415 if (op->IsShl()) {
2416 __ shll(first_reg, second_reg);
2417 } else if (op->IsShr()) {
2418 __ sarl(first_reg, second_reg);
2419 } else {
2420 __ shrl(first_reg, second_reg);
2421 }
2422 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002423 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002424 if (op->IsShl()) {
2425 __ shll(first_reg, imm);
2426 } else if (op->IsShr()) {
2427 __ sarl(first_reg, imm);
2428 } else {
2429 __ shrl(first_reg, imm);
2430 }
2431 }
2432 break;
2433 }
2434 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002435 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002436 DCHECK_EQ(ECX, second_reg);
2437 if (op->IsShl()) {
2438 GenerateShlLong(first, second_reg);
2439 } else if (op->IsShr()) {
2440 GenerateShrLong(first, second_reg);
2441 } else {
2442 GenerateUShrLong(first, second_reg);
2443 }
2444 break;
2445 }
2446 default:
2447 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2448 }
2449}
2450
2451void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2452 Label done;
2453 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2454 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2455 __ testl(shifter, Immediate(32));
2456 __ j(kEqual, &done);
2457 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2458 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2459 __ Bind(&done);
2460}
2461
2462void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2463 Label done;
2464 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2465 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2466 __ testl(shifter, Immediate(32));
2467 __ j(kEqual, &done);
2468 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2469 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2470 __ Bind(&done);
2471}
2472
2473void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2474 Label done;
2475 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2476 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2477 __ testl(shifter, Immediate(32));
2478 __ j(kEqual, &done);
2479 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2480 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2481 __ Bind(&done);
2482}
2483
2484void LocationsBuilderX86::VisitShl(HShl* shl) {
2485 HandleShift(shl);
2486}
2487
2488void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2489 HandleShift(shl);
2490}
2491
2492void LocationsBuilderX86::VisitShr(HShr* shr) {
2493 HandleShift(shr);
2494}
2495
2496void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2497 HandleShift(shr);
2498}
2499
2500void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2501 HandleShift(ushr);
2502}
2503
2504void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2505 HandleShift(ushr);
2506}
2507
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002508void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002509 LocationSummary* locations =
2510 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002511 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002512 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002513 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2514 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002515}
2516
2517void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2518 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002519 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002520 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002521
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002522 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002523
Nicolas Geoffray39468442014-09-02 15:17:15 +01002524 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002525 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002526}
2527
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002528void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2529 LocationSummary* locations =
2530 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2531 locations->SetOut(Location::RegisterLocation(EAX));
2532 InvokeRuntimeCallingConvention calling_convention;
2533 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002534 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2535 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002536}
2537
2538void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2539 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002540 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002541 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2542
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002543 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002544
2545 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2546 DCHECK(!codegen_->IsLeafMethod());
2547}
2548
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002549void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002550 LocationSummary* locations =
2551 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002552 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2553 if (location.IsStackSlot()) {
2554 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2555 } else if (location.IsDoubleStackSlot()) {
2556 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002557 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002558 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002559}
2560
2561void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002562 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002563}
2564
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002565void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002566 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002567 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002568 locations->SetInAt(0, Location::RequiresRegister());
2569 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002570}
2571
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002572void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2573 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002574 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002575 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002576 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002577 switch (not_->InputAt(0)->GetType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002578 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002579 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002580 break;
2581
2582 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002583 __ notl(out.AsRegisterPairLow<Register>());
2584 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002585 break;
2586
2587 default:
2588 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2589 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002590}
2591
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002592void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002593 LocationSummary* locations =
2594 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002595 switch (compare->InputAt(0)->GetType()) {
2596 case Primitive::kPrimLong: {
2597 locations->SetInAt(0, Location::RequiresRegister());
2598 // TODO: we set any here but we don't handle constants
2599 locations->SetInAt(1, Location::Any());
2600 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2601 break;
2602 }
2603 case Primitive::kPrimFloat:
2604 case Primitive::kPrimDouble: {
2605 locations->SetInAt(0, Location::RequiresFpuRegister());
2606 locations->SetInAt(1, Location::RequiresFpuRegister());
2607 locations->SetOut(Location::RequiresRegister());
2608 break;
2609 }
2610 default:
2611 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2612 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002613}
2614
2615void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002616 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002617 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002618 Location left = locations->InAt(0);
2619 Location right = locations->InAt(1);
2620
2621 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002622 switch (compare->InputAt(0)->GetType()) {
2623 case Primitive::kPrimLong: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002624 if (right.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002625 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002626 } else {
2627 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002628 __ cmpl(left.AsRegisterPairHigh<Register>(),
2629 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002630 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002631 __ j(kLess, &less); // Signed compare.
2632 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002633 if (right.IsRegisterPair()) {
2634 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002635 } else {
2636 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002637 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002638 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002639 break;
2640 }
2641 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002642 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002643 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
2644 break;
2645 }
2646 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002647 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002648 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002649 break;
2650 }
2651 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002652 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002653 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002654 __ movl(out, Immediate(0));
2655 __ j(kEqual, &done);
2656 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
2657
2658 __ Bind(&greater);
2659 __ movl(out, Immediate(1));
2660 __ jmp(&done);
2661
2662 __ Bind(&less);
2663 __ movl(out, Immediate(-1));
2664
2665 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002666}
2667
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002668void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002669 LocationSummary* locations =
2670 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002671 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2672 locations->SetInAt(i, Location::Any());
2673 }
2674 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002675}
2676
2677void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002678 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002679 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002680}
2681
Calin Juravle52c48962014-12-16 17:02:57 +00002682void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
2683 /*
2684 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2685 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2686 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2687 */
2688 switch (kind) {
2689 case MemBarrierKind::kAnyAny: {
2690 __ mfence();
2691 break;
2692 }
2693 case MemBarrierKind::kAnyStore:
2694 case MemBarrierKind::kLoadAny:
2695 case MemBarrierKind::kStoreStore: {
2696 // nop
2697 break;
2698 }
2699 default:
2700 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002701 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002702}
2703
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002704
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002705void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002706 Label is_null;
2707 __ testl(value, value);
2708 __ j(kEqual, &is_null);
2709 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2710 __ movl(temp, object);
2711 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002712 __ movb(Address(temp, card, TIMES_1, 0),
2713 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002714 __ Bind(&is_null);
2715}
2716
Calin Juravle52c48962014-12-16 17:02:57 +00002717void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
2718 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002719 LocationSummary* locations =
2720 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002721 locations->SetInAt(0, Location::RequiresRegister());
2722 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle52c48962014-12-16 17:02:57 +00002723
2724 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
2725 // Long values can be loaded atomically into an XMM using movsd.
2726 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
2727 // and then copy the XMM into the output 32bits at a time).
2728 locations->AddTemp(Location::RequiresFpuRegister());
2729 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002730}
2731
Calin Juravle52c48962014-12-16 17:02:57 +00002732void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
2733 const FieldInfo& field_info) {
2734 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002735
Calin Juravle52c48962014-12-16 17:02:57 +00002736 LocationSummary* locations = instruction->GetLocations();
2737 Register base = locations->InAt(0).AsRegister<Register>();
2738 Location out = locations->Out();
2739 bool is_volatile = field_info.IsVolatile();
2740 Primitive::Type field_type = field_info.GetFieldType();
2741 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2742
2743 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002744 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00002745 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002746 break;
2747 }
2748
2749 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002750 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002751 break;
2752 }
2753
2754 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00002755 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002756 break;
2757 }
2758
2759 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002760 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002761 break;
2762 }
2763
2764 case Primitive::kPrimInt:
2765 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002766 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002767 break;
2768 }
2769
2770 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00002771 if (is_volatile) {
2772 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2773 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002774 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002775 __ movd(out.AsRegisterPairLow<Register>(), temp);
2776 __ psrlq(temp, Immediate(32));
2777 __ movd(out.AsRegisterPairHigh<Register>(), temp);
2778 } else {
2779 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002780 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002781 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
2782 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002783 break;
2784 }
2785
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002786 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002787 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002788 break;
2789 }
2790
2791 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002792 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002793 break;
2794 }
2795
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002796 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00002797 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002798 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002799 }
Calin Juravle52c48962014-12-16 17:02:57 +00002800
Calin Juravle77520bc2015-01-12 18:45:46 +00002801 // Longs are handled in the switch.
2802 if (field_type != Primitive::kPrimLong) {
2803 codegen_->MaybeRecordImplicitNullCheck(instruction);
2804 }
2805
Calin Juravle52c48962014-12-16 17:02:57 +00002806 if (is_volatile) {
2807 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2808 }
2809}
2810
2811void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
2812 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2813
2814 LocationSummary* locations =
2815 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2816 locations->SetInAt(0, Location::RequiresRegister());
2817 bool is_volatile = field_info.IsVolatile();
2818 Primitive::Type field_type = field_info.GetFieldType();
2819 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2820 || (field_type == Primitive::kPrimByte);
2821
2822 // The register allocator does not support multiple
2823 // inputs that die at entry with one in a specific register.
2824 if (is_byte_type) {
2825 // Ensure the value is in a byte register.
2826 locations->SetInAt(1, Location::RegisterLocation(EAX));
2827 } else {
2828 locations->SetInAt(1, Location::RequiresRegister());
2829 }
2830 // Temporary registers for the write barrier.
2831 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2832 locations->AddTemp(Location::RequiresRegister());
2833 // Ensure the card is in a byte register.
2834 locations->AddTemp(Location::RegisterLocation(ECX));
2835 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
2836 // 64bits value can be atomically written to an address with movsd and an XMM register.
2837 // We need two XMM registers because there's no easier way to (bit) copy a register pair
2838 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
2839 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
2840 // isolated cases when we need this it isn't worth adding the extra complexity.
2841 locations->AddTemp(Location::RequiresFpuRegister());
2842 locations->AddTemp(Location::RequiresFpuRegister());
2843 }
2844}
2845
2846void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
2847 const FieldInfo& field_info) {
2848 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2849
2850 LocationSummary* locations = instruction->GetLocations();
2851 Register base = locations->InAt(0).AsRegister<Register>();
2852 Location value = locations->InAt(1);
2853 bool is_volatile = field_info.IsVolatile();
2854 Primitive::Type field_type = field_info.GetFieldType();
2855 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2856
2857 if (is_volatile) {
2858 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2859 }
2860
2861 switch (field_type) {
2862 case Primitive::kPrimBoolean:
2863 case Primitive::kPrimByte: {
2864 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
2865 break;
2866 }
2867
2868 case Primitive::kPrimShort:
2869 case Primitive::kPrimChar: {
2870 __ movw(Address(base, offset), value.AsRegister<Register>());
2871 break;
2872 }
2873
2874 case Primitive::kPrimInt:
2875 case Primitive::kPrimNot: {
2876 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00002877 break;
2878 }
2879
2880 case Primitive::kPrimLong: {
2881 if (is_volatile) {
2882 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2883 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
2884 __ movd(temp1, value.AsRegisterPairLow<Register>());
2885 __ movd(temp2, value.AsRegisterPairHigh<Register>());
2886 __ punpckldq(temp1, temp2);
2887 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00002888 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002889 } else {
2890 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00002891 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002892 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
2893 }
2894 break;
2895 }
2896
2897 case Primitive::kPrimFloat: {
2898 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
2899 break;
2900 }
2901
2902 case Primitive::kPrimDouble: {
2903 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
2904 break;
2905 }
2906
2907 case Primitive::kPrimVoid:
2908 LOG(FATAL) << "Unreachable type " << field_type;
2909 UNREACHABLE();
2910 }
2911
Calin Juravle77520bc2015-01-12 18:45:46 +00002912 // Longs are handled in the switch.
2913 if (field_type != Primitive::kPrimLong) {
2914 codegen_->MaybeRecordImplicitNullCheck(instruction);
2915 }
2916
2917 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2918 Register temp = locations->GetTemp(0).AsRegister<Register>();
2919 Register card = locations->GetTemp(1).AsRegister<Register>();
2920 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
2921 }
2922
Calin Juravle52c48962014-12-16 17:02:57 +00002923 if (is_volatile) {
2924 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2925 }
2926}
2927
2928void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2929 HandleFieldGet(instruction, instruction->GetFieldInfo());
2930}
2931
2932void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2933 HandleFieldGet(instruction, instruction->GetFieldInfo());
2934}
2935
2936void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2937 HandleFieldSet(instruction, instruction->GetFieldInfo());
2938}
2939
2940void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2941 HandleFieldSet(instruction, instruction->GetFieldInfo());
2942}
2943
2944void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2945 HandleFieldSet(instruction, instruction->GetFieldInfo());
2946}
2947
2948void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2949 HandleFieldSet(instruction, instruction->GetFieldInfo());
2950}
2951
2952void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2953 HandleFieldGet(instruction, instruction->GetFieldInfo());
2954}
2955
2956void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2957 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002958}
2959
2960void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002961 LocationSummary* locations =
2962 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002963 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
2964 ? Location::RequiresRegister()
2965 : Location::Any();
2966 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002967 if (instruction->HasUses()) {
2968 locations->SetOut(Location::SameAsFirstInput());
2969 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002970}
2971
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002972void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002973 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2974 return;
2975 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002976 LocationSummary* locations = instruction->GetLocations();
2977 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002978
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002979 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
2980 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2981}
2982
2983void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002984 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002985 codegen_->AddSlowPath(slow_path);
2986
2987 LocationSummary* locations = instruction->GetLocations();
2988 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002989
2990 if (obj.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002991 __ cmpl(obj.AsRegister<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002992 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002993 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002994 } else {
2995 DCHECK(obj.IsConstant()) << obj;
2996 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2997 __ jmp(slow_path->GetEntryLabel());
2998 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002999 }
3000 __ j(kEqual, slow_path->GetEntryLabel());
3001}
3002
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003003void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3004 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3005 GenerateImplicitNullCheck(instruction);
3006 } else {
3007 GenerateExplicitNullCheck(instruction);
3008 }
3009}
3010
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003011void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003012 LocationSummary* locations =
3013 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003014 locations->SetInAt(0, Location::RequiresRegister());
3015 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3016 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003017}
3018
3019void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3020 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003021 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003022 Location index = locations->InAt(1);
3023
Calin Juravle77520bc2015-01-12 18:45:46 +00003024 Primitive::Type type = instruction->GetType();
3025 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003026 case Primitive::kPrimBoolean: {
3027 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003028 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003029 if (index.IsConstant()) {
3030 __ movzxb(out, Address(obj,
3031 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3032 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003033 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003034 }
3035 break;
3036 }
3037
3038 case Primitive::kPrimByte: {
3039 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003040 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003041 if (index.IsConstant()) {
3042 __ movsxb(out, Address(obj,
3043 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3044 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003045 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003046 }
3047 break;
3048 }
3049
3050 case Primitive::kPrimShort: {
3051 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003052 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003053 if (index.IsConstant()) {
3054 __ movsxw(out, Address(obj,
3055 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3056 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003057 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003058 }
3059 break;
3060 }
3061
3062 case Primitive::kPrimChar: {
3063 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003064 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003065 if (index.IsConstant()) {
3066 __ movzxw(out, Address(obj,
3067 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3068 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003069 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003070 }
3071 break;
3072 }
3073
3074 case Primitive::kPrimInt:
3075 case Primitive::kPrimNot: {
3076 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003077 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003078 if (index.IsConstant()) {
3079 __ movl(out, Address(obj,
3080 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3081 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003082 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003083 }
3084 break;
3085 }
3086
3087 case Primitive::kPrimLong: {
3088 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003089 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003090 if (index.IsConstant()) {
3091 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003092 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003093 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003094 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003095 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003096 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003097 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003098 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003099 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003100 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003101 }
3102 break;
3103 }
3104
Mark Mendell7c8d0092015-01-26 11:21:33 -05003105 case Primitive::kPrimFloat: {
3106 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3107 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3108 if (index.IsConstant()) {
3109 __ movss(out, Address(obj,
3110 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3111 } else {
3112 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3113 }
3114 break;
3115 }
3116
3117 case Primitive::kPrimDouble: {
3118 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3119 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3120 if (index.IsConstant()) {
3121 __ movsd(out, Address(obj,
3122 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3123 } else {
3124 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3125 }
3126 break;
3127 }
3128
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003129 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003130 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003131 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003132 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003133
3134 if (type != Primitive::kPrimLong) {
3135 codegen_->MaybeRecordImplicitNullCheck(instruction);
3136 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003137}
3138
3139void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003140 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003141 bool needs_write_barrier =
3142 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3143
3144 DCHECK(kFollowsQuickABI);
3145 bool not_enough_registers = needs_write_barrier
3146 && !instruction->GetValue()->IsConstant()
3147 && !instruction->GetIndex()->IsConstant();
3148 bool needs_runtime_call = instruction->NeedsTypeCheck() || not_enough_registers;
3149
Nicolas Geoffray39468442014-09-02 15:17:15 +01003150 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3151 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003152 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003153
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003154 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003155 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003156 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3157 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3158 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003159 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003160 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3161 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003162 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003163 // In case of a byte operation, the register allocator does not support multiple
3164 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003165 locations->SetInAt(0, Location::RequiresRegister());
3166 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003167 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003168 // Ensure the value is in a byte register.
3169 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003170 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003171 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003172 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003173 // Temporary registers for the write barrier.
3174 if (needs_write_barrier) {
3175 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003176 // Ensure the card is in a byte register.
3177 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003178 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003179 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003180}
3181
3182void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3183 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003184 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003185 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003186 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003187 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003188 bool needs_runtime_call = locations->WillCall();
3189 bool needs_write_barrier =
3190 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003191
3192 switch (value_type) {
3193 case Primitive::kPrimBoolean:
3194 case Primitive::kPrimByte: {
3195 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003196 if (index.IsConstant()) {
3197 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003198 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003199 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003200 } else {
3201 __ movb(Address(obj, offset),
3202 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3203 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003204 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003205 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003206 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003207 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003208 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003209 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003210 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3211 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003212 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003213 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003214 break;
3215 }
3216
3217 case Primitive::kPrimShort:
3218 case Primitive::kPrimChar: {
3219 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003220 if (index.IsConstant()) {
3221 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003222 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003223 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003224 } else {
3225 __ movw(Address(obj, offset),
3226 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3227 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003228 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003229 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003230 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3231 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003232 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003233 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003234 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3235 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003236 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003237 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003238 break;
3239 }
3240
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003241 case Primitive::kPrimInt:
3242 case Primitive::kPrimNot: {
3243 if (!needs_runtime_call) {
3244 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3245 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003246 size_t offset =
3247 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003248 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003249 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003250 } else {
3251 DCHECK(value.IsConstant()) << value;
3252 __ movl(Address(obj, offset),
3253 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3254 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003255 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003256 DCHECK(index.IsRegister()) << index;
3257 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003258 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3259 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003260 } else {
3261 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003262 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003263 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3264 }
3265 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003266 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003267
3268 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003269 Register temp = locations->GetTemp(0).AsRegister<Register>();
3270 Register card = locations->GetTemp(1).AsRegister<Register>();
3271 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003272 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003273 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003274 DCHECK_EQ(value_type, Primitive::kPrimNot);
3275 DCHECK(!codegen_->IsLeafMethod());
3276 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3277 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003278 }
3279 break;
3280 }
3281
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003282 case Primitive::kPrimLong: {
3283 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003284 if (index.IsConstant()) {
3285 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003286 if (value.IsRegisterPair()) {
3287 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003288 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003289 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003290 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003291 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003292 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3293 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003294 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003295 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3296 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003297 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003298 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003299 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003300 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003301 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003302 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003303 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003304 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003305 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003306 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003307 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003308 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003309 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003310 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003311 Immediate(High32Bits(val)));
3312 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003313 }
3314 break;
3315 }
3316
Mark Mendell7c8d0092015-01-26 11:21:33 -05003317 case Primitive::kPrimFloat: {
3318 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3319 DCHECK(value.IsFpuRegister());
3320 if (index.IsConstant()) {
3321 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3322 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3323 } else {
3324 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3325 value.AsFpuRegister<XmmRegister>());
3326 }
3327 break;
3328 }
3329
3330 case Primitive::kPrimDouble: {
3331 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3332 DCHECK(value.IsFpuRegister());
3333 if (index.IsConstant()) {
3334 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3335 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3336 } else {
3337 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3338 value.AsFpuRegister<XmmRegister>());
3339 }
3340 break;
3341 }
3342
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003343 case Primitive::kPrimVoid:
3344 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003345 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003346 }
3347}
3348
3349void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3350 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003351 locations->SetInAt(0, Location::RequiresRegister());
3352 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003353 instruction->SetLocations(locations);
3354}
3355
3356void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3357 LocationSummary* locations = instruction->GetLocations();
3358 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003359 Register obj = locations->InAt(0).AsRegister<Register>();
3360 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003361 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003362 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003363}
3364
3365void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003366 LocationSummary* locations =
3367 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003368 locations->SetInAt(0, Location::RequiresRegister());
3369 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003370 if (instruction->HasUses()) {
3371 locations->SetOut(Location::SameAsFirstInput());
3372 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003373}
3374
3375void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3376 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003377 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01003378 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003379 codegen_->AddSlowPath(slow_path);
3380
Roland Levillain271ab9c2014-11-27 15:23:57 +00003381 Register index = locations->InAt(0).AsRegister<Register>();
3382 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003383
3384 __ cmpl(index, length);
3385 __ j(kAboveEqual, slow_path->GetEntryLabel());
3386}
3387
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003388void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3389 temp->SetLocations(nullptr);
3390}
3391
3392void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3393 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003394 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003395}
3396
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003397void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003398 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003399 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003400}
3401
3402void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003403 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3404}
3405
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003406void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3407 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3408}
3409
3410void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003411 HBasicBlock* block = instruction->GetBlock();
3412 if (block->GetLoopInformation() != nullptr) {
3413 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3414 // The back edge will generate the suspend check.
3415 return;
3416 }
3417 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3418 // The goto will generate the suspend check.
3419 return;
3420 }
3421 GenerateSuspendCheck(instruction, nullptr);
3422}
3423
3424void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3425 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003426 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003427 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003428 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003429 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003430 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003431 if (successor == nullptr) {
3432 __ j(kNotEqual, slow_path->GetEntryLabel());
3433 __ Bind(slow_path->GetReturnLabel());
3434 } else {
3435 __ j(kEqual, codegen_->GetLabelOf(successor));
3436 __ jmp(slow_path->GetEntryLabel());
3437 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003438}
3439
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003440X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3441 return codegen_->GetAssembler();
3442}
3443
Mark Mendell7c8d0092015-01-26 11:21:33 -05003444void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003445 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003446 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003447 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003448 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05003449 __ movl(temp_reg, Address(ESP, src + stack_offset));
3450 __ movl(Address(ESP, dst + stack_offset), temp_reg);
3451}
3452
3453void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
3454 ScratchRegisterScope ensure_scratch(
3455 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3456 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3457 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3458 __ movl(temp_reg, Address(ESP, src + stack_offset));
3459 __ movl(Address(ESP, dst + stack_offset), temp_reg);
3460 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
3461 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003462}
3463
3464void ParallelMoveResolverX86::EmitMove(size_t index) {
3465 MoveOperands* move = moves_.Get(index);
3466 Location source = move->GetSource();
3467 Location destination = move->GetDestination();
3468
3469 if (source.IsRegister()) {
3470 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003471 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003472 } else {
3473 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003474 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003475 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003476 } else if (source.IsFpuRegister()) {
3477 if (destination.IsFpuRegister()) {
3478 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3479 } else if (destination.IsStackSlot()) {
3480 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3481 } else {
3482 DCHECK(destination.IsDoubleStackSlot());
3483 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3484 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003485 } else if (source.IsStackSlot()) {
3486 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003487 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003488 } else if (destination.IsFpuRegister()) {
3489 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003490 } else {
3491 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003492 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
3493 }
3494 } else if (source.IsDoubleStackSlot()) {
3495 if (destination.IsFpuRegister()) {
3496 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
3497 } else {
3498 DCHECK(destination.IsDoubleStackSlot()) << destination;
3499 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003500 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003501 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003502 HConstant* constant = source.GetConstant();
3503 if (constant->IsIntConstant()) {
3504 Immediate imm(constant->AsIntConstant()->GetValue());
3505 if (destination.IsRegister()) {
3506 __ movl(destination.AsRegister<Register>(), imm);
3507 } else {
3508 DCHECK(destination.IsStackSlot()) << destination;
3509 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3510 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003511 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003512 DCHECK(constant->IsFloatConstant());
3513 float value = constant->AsFloatConstant()->GetValue();
3514 Immediate imm(bit_cast<float, int32_t>(value));
3515 if (destination.IsFpuRegister()) {
3516 ScratchRegisterScope ensure_scratch(
3517 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3518 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
3519 __ movl(temp, imm);
3520 __ movd(destination.AsFpuRegister<XmmRegister>(), temp);
3521 } else {
3522 DCHECK(destination.IsStackSlot()) << destination;
3523 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3524 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003525 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003526 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003527 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003528 }
3529}
3530
3531void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003532 Register suggested_scratch = reg == EAX ? EBX : EAX;
3533 ScratchRegisterScope ensure_scratch(
3534 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3535
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003536 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3537 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
3538 __ movl(Address(ESP, mem + stack_offset), reg);
3539 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
3540}
3541
Mark Mendell7c8d0092015-01-26 11:21:33 -05003542void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
3543 ScratchRegisterScope ensure_scratch(
3544 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3545
3546 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3547 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3548 __ movl(temp_reg, Address(ESP, mem + stack_offset));
3549 __ movss(Address(ESP, mem + stack_offset), reg);
3550 __ movd(reg, temp_reg);
3551}
3552
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003553void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
3554 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003555 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3556
3557 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003558 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003559 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3560
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003561 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
3562 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
3563 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
3564 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
3565 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
3566 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
3567}
3568
3569void ParallelMoveResolverX86::EmitSwap(size_t index) {
3570 MoveOperands* move = moves_.Get(index);
3571 Location source = move->GetSource();
3572 Location destination = move->GetDestination();
3573
3574 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003575 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003576 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003577 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003578 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003579 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003580 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3581 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003582 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
3583 // Use XOR Swap algorithm to avoid a temporary.
3584 DCHECK_NE(source.reg(), destination.reg());
3585 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3586 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3587 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3588 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
3589 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
3590 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
3591 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003592 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003593 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003594 }
3595}
3596
3597void ParallelMoveResolverX86::SpillScratch(int reg) {
3598 __ pushl(static_cast<Register>(reg));
3599}
3600
3601void ParallelMoveResolverX86::RestoreScratch(int reg) {
3602 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003603}
3604
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003605void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003606 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3607 ? LocationSummary::kCallOnSlowPath
3608 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003609 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003610 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003611 locations->SetOut(Location::RequiresRegister());
3612}
3613
3614void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003615 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003616 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003617 DCHECK(!cls->CanCallRuntime());
3618 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003619 codegen_->LoadCurrentMethod(out);
3620 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3621 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003622 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003623 codegen_->LoadCurrentMethod(out);
3624 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3625 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003626
3627 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3628 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3629 codegen_->AddSlowPath(slow_path);
3630 __ testl(out, out);
3631 __ j(kEqual, slow_path->GetEntryLabel());
3632 if (cls->MustGenerateClinitCheck()) {
3633 GenerateClassInitializationCheck(slow_path, out);
3634 } else {
3635 __ Bind(slow_path->GetExitLabel());
3636 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003637 }
3638}
3639
3640void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
3641 LocationSummary* locations =
3642 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3643 locations->SetInAt(0, Location::RequiresRegister());
3644 if (check->HasUses()) {
3645 locations->SetOut(Location::SameAsFirstInput());
3646 }
3647}
3648
3649void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003650 // We assume the class to not be null.
3651 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3652 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003653 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003654 GenerateClassInitializationCheck(slow_path,
3655 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003656}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003657
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003658void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
3659 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003660 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3661 Immediate(mirror::Class::kStatusInitialized));
3662 __ j(kLess, slow_path->GetEntryLabel());
3663 __ Bind(slow_path->GetExitLabel());
3664 // No need for memory fence, thanks to the X86 memory model.
3665}
3666
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003667void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
3668 LocationSummary* locations =
3669 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3670 locations->SetOut(Location::RequiresRegister());
3671}
3672
3673void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
3674 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
3675 codegen_->AddSlowPath(slow_path);
3676
Roland Levillain271ab9c2014-11-27 15:23:57 +00003677 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003678 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08003679 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3680 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003681 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3682 __ testl(out, out);
3683 __ j(kEqual, slow_path->GetEntryLabel());
3684 __ Bind(slow_path->GetExitLabel());
3685}
3686
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003687void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
3688 LocationSummary* locations =
3689 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3690 locations->SetOut(Location::RequiresRegister());
3691}
3692
3693void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
3694 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003695 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003696 __ fs()->movl(address, Immediate(0));
3697}
3698
3699void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
3700 LocationSummary* locations =
3701 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3702 InvokeRuntimeCallingConvention calling_convention;
3703 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3704}
3705
3706void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
3707 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
3708 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3709}
3710
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003711void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003712 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3713 ? LocationSummary::kNoCall
3714 : LocationSummary::kCallOnSlowPath;
3715 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3716 locations->SetInAt(0, Location::RequiresRegister());
3717 locations->SetInAt(1, Location::Any());
3718 locations->SetOut(Location::RequiresRegister());
3719}
3720
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003721void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003722 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003723 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003724 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003725 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003726 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3727 Label done, zero;
3728 SlowPathCodeX86* slow_path = nullptr;
3729
3730 // Return 0 if `obj` is null.
3731 // TODO: avoid this check if we know obj is not null.
3732 __ testl(obj, obj);
3733 __ j(kEqual, &zero);
3734 __ movl(out, Address(obj, class_offset));
3735 // Compare the class of `obj` with `cls`.
3736 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003737 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003738 } else {
3739 DCHECK(cls.IsStackSlot()) << cls;
3740 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
3741 }
3742
3743 if (instruction->IsClassFinal()) {
3744 // Classes must be equal for the instanceof to succeed.
3745 __ j(kNotEqual, &zero);
3746 __ movl(out, Immediate(1));
3747 __ jmp(&done);
3748 } else {
3749 // If the classes are not equal, we go into a slow path.
3750 DCHECK(locations->OnlyCallsOnSlowPath());
3751 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003752 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003753 codegen_->AddSlowPath(slow_path);
3754 __ j(kNotEqual, slow_path->GetEntryLabel());
3755 __ movl(out, Immediate(1));
3756 __ jmp(&done);
3757 }
3758 __ Bind(&zero);
3759 __ movl(out, Immediate(0));
3760 if (slow_path != nullptr) {
3761 __ Bind(slow_path->GetExitLabel());
3762 }
3763 __ Bind(&done);
3764}
3765
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003766void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
3767 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3768 instruction, LocationSummary::kCallOnSlowPath);
3769 locations->SetInAt(0, Location::RequiresRegister());
3770 locations->SetInAt(1, Location::Any());
3771 locations->AddTemp(Location::RequiresRegister());
3772}
3773
3774void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
3775 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003776 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003777 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003778 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003779 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3780 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
3781 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3782 codegen_->AddSlowPath(slow_path);
3783
3784 // TODO: avoid this check if we know obj is not null.
3785 __ testl(obj, obj);
3786 __ j(kEqual, slow_path->GetExitLabel());
3787 __ movl(temp, Address(obj, class_offset));
3788
3789 // Compare the class of `obj` with `cls`.
3790 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003791 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003792 } else {
3793 DCHECK(cls.IsStackSlot()) << cls;
3794 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
3795 }
3796
3797 __ j(kNotEqual, slow_path->GetEntryLabel());
3798 __ Bind(slow_path->GetExitLabel());
3799}
3800
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003801void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3802 LocationSummary* locations =
3803 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3804 InvokeRuntimeCallingConvention calling_convention;
3805 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3806}
3807
3808void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3809 __ fs()->call(Address::Absolute(instruction->IsEnter()
3810 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
3811 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
3812 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3813}
3814
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003815void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3816void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3817void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3818
3819void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3820 LocationSummary* locations =
3821 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3822 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3823 || instruction->GetResultType() == Primitive::kPrimLong);
3824 locations->SetInAt(0, Location::RequiresRegister());
3825 locations->SetInAt(1, Location::Any());
3826 locations->SetOut(Location::SameAsFirstInput());
3827}
3828
3829void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
3830 HandleBitwiseOperation(instruction);
3831}
3832
3833void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
3834 HandleBitwiseOperation(instruction);
3835}
3836
3837void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
3838 HandleBitwiseOperation(instruction);
3839}
3840
3841void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3842 LocationSummary* locations = instruction->GetLocations();
3843 Location first = locations->InAt(0);
3844 Location second = locations->InAt(1);
3845 DCHECK(first.Equals(locations->Out()));
3846
3847 if (instruction->GetResultType() == Primitive::kPrimInt) {
3848 if (second.IsRegister()) {
3849 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003850 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003851 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003852 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003853 } else {
3854 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003855 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003856 }
3857 } else if (second.IsConstant()) {
3858 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003859 __ andl(first.AsRegister<Register>(),
3860 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003861 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003862 __ orl(first.AsRegister<Register>(),
3863 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003864 } else {
3865 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00003866 __ xorl(first.AsRegister<Register>(),
3867 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003868 }
3869 } else {
3870 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003871 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003872 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003873 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003874 } else {
3875 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003876 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003877 }
3878 }
3879 } else {
3880 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3881 if (second.IsRegisterPair()) {
3882 if (instruction->IsAnd()) {
3883 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3884 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3885 } else if (instruction->IsOr()) {
3886 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3887 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3888 } else {
3889 DCHECK(instruction->IsXor());
3890 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3891 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3892 }
3893 } else {
3894 if (instruction->IsAnd()) {
3895 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3896 __ andl(first.AsRegisterPairHigh<Register>(),
3897 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3898 } else if (instruction->IsOr()) {
3899 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3900 __ orl(first.AsRegisterPairHigh<Register>(),
3901 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3902 } else {
3903 DCHECK(instruction->IsXor());
3904 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3905 __ xorl(first.AsRegisterPairHigh<Register>(),
3906 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3907 }
3908 }
3909 }
3910}
3911
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003912} // namespace x86
3913} // namespace art