blob: 2e897f1ef57ff5cb0f83ec1ee7bcf8b1c10d09b3 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
19#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000020#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010021#include "gc/accounting/card_table.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070022#include "mirror/array-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010023#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010024#include "mirror/class.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010025#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000026#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010027#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000028#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010029#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010032
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033namespace x86 {
34
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010035static constexpr int kCurrentMethodStackOffset = 0;
36
Calin Juravled6fb6cf2014-11-11 19:07:44 +000037static constexpr Register kRuntimeParameterCoreRegisters[] = { EAX, ECX, EDX, EBX };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010038static constexpr size_t kRuntimeParameterCoreRegistersLength =
39 arraysize(kRuntimeParameterCoreRegisters);
Mark P Mendell966c3ae2015-01-27 15:45:27 +000040static constexpr XmmRegister kRuntimeParameterFpuRegisters[] = { XMM0, XMM1, XMM2, XMM3 };
41static constexpr size_t kRuntimeParameterFpuRegistersLength =
42 arraysize(kRuntimeParameterFpuRegisters);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010043
Mark Mendell24f2dfa2015-01-14 19:51:45 -050044static constexpr int kC2ConditionMask = 0x400;
45
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000046// Marker for places that can be updated once we don't follow the quick ABI.
47static constexpr bool kFollowsQuickABI = true;
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000048static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000049
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010050class InvokeRuntimeCallingConvention : public CallingConvention<Register, XmmRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010051 public:
52 InvokeRuntimeCallingConvention()
53 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010054 kRuntimeParameterCoreRegistersLength,
55 kRuntimeParameterFpuRegisters,
56 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010057
58 private:
59 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
60};
61
Nicolas Geoffraye5038322014-07-04 09:41:32 +010062#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
63
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010064class SlowPathCodeX86 : public SlowPathCode {
65 public:
66 SlowPathCodeX86() : entry_label_(), exit_label_() {}
67
68 Label* GetEntryLabel() { return &entry_label_; }
69 Label* GetExitLabel() { return &exit_label_; }
70
71 private:
72 Label entry_label_;
73 Label exit_label_;
74
75 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86);
76};
77
78class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010080 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010081
82 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
83 __ Bind(GetEntryLabel());
84 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010085 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010086 }
87
88 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010089 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010090 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
91};
92
Calin Juravled0d48522014-11-04 16:40:20 +000093class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
94 public:
95 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
96
97 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
98 __ Bind(GetEntryLabel());
99 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
100 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
101 }
102
103 private:
104 HDivZeroCheck* const instruction_;
105 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
106};
107
Calin Juravlebacfec32014-11-14 15:54:36 +0000108class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +0000109 public:
Calin Juravlebacfec32014-11-14 15:54:36 +0000110 explicit DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000111
112 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
113 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000114 if (is_div_) {
115 __ negl(reg_);
116 } else {
117 __ movl(reg_, Immediate(0));
118 }
Calin Juravled0d48522014-11-04 16:40:20 +0000119 __ jmp(GetExitLabel());
120 }
121
122 private:
123 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000124 bool is_div_;
125 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000126};
127
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100128class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100129 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100130 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
131 Location index_location,
132 Location length_location)
Roland Levillain199f3362014-11-27 17:15:16 +0000133 : instruction_(instruction),
134 index_location_(index_location),
135 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100136
137 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100138 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100139 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000140 // We're moving two locations to locations that could overlap, so we need a parallel
141 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100142 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000143 x86_codegen->EmitParallelMoves(
144 index_location_,
145 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
146 length_location_,
147 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100148 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100149 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100150 }
151
152 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100153 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100154 const Location index_location_;
155 const Location length_location_;
156
157 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
158};
159
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100160class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000161 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100162 explicit SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
163 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000164
165 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100166 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000167 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100168 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000169 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
170 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100171 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100172 if (successor_ == nullptr) {
173 __ jmp(GetReturnLabel());
174 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100175 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100176 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000177 }
178
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100179 Label* GetReturnLabel() {
180 DCHECK(successor_ == nullptr);
181 return &return_label_;
182 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000183
184 private:
185 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100186 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000187 Label return_label_;
188
189 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
190};
191
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000192class LoadStringSlowPathX86 : public SlowPathCodeX86 {
193 public:
194 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
195
196 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
197 LocationSummary* locations = instruction_->GetLocations();
198 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
199
200 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
201 __ Bind(GetEntryLabel());
202 codegen->SaveLiveRegisters(locations);
203
204 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800205 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
206 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000207 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
208 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
209 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
210 codegen->RestoreLiveRegisters(locations);
211
212 __ jmp(GetExitLabel());
213 }
214
215 private:
216 HLoadString* const instruction_;
217
218 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
219};
220
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000221class LoadClassSlowPathX86 : public SlowPathCodeX86 {
222 public:
223 LoadClassSlowPathX86(HLoadClass* cls,
224 HInstruction* at,
225 uint32_t dex_pc,
226 bool do_clinit)
227 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
228 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
229 }
230
231 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
232 LocationSummary* locations = at_->GetLocations();
233 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
234 __ Bind(GetEntryLabel());
235 codegen->SaveLiveRegisters(locations);
236
237 InvokeRuntimeCallingConvention calling_convention;
238 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
239 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
240 __ fs()->call(Address::Absolute(do_clinit_
241 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
242 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
243 codegen->RecordPcInfo(at_, dex_pc_);
244
245 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000246 Location out = locations->Out();
247 if (out.IsValid()) {
248 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
249 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000250 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000251
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000252 codegen->RestoreLiveRegisters(locations);
253 __ jmp(GetExitLabel());
254 }
255
256 private:
257 // The class this slow path will load.
258 HLoadClass* const cls_;
259
260 // The instruction where this slow path is happening.
261 // (Might be the load class or an initialization check).
262 HInstruction* const at_;
263
264 // The dex PC of `at_`.
265 const uint32_t dex_pc_;
266
267 // Whether to initialize the class.
268 const bool do_clinit_;
269
270 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
271};
272
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000273class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
274 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000275 TypeCheckSlowPathX86(HInstruction* instruction,
276 Location class_to_check,
277 Location object_class,
278 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000279 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000280 class_to_check_(class_to_check),
281 object_class_(object_class),
282 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000283
284 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
285 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000286 DCHECK(instruction_->IsCheckCast()
287 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000288
289 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
290 __ Bind(GetEntryLabel());
291 codegen->SaveLiveRegisters(locations);
292
293 // We're moving two locations to locations that could overlap, so we need a parallel
294 // move resolver.
295 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000296 x86_codegen->EmitParallelMoves(
297 class_to_check_,
298 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
299 object_class_,
300 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000301
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000302 if (instruction_->IsInstanceOf()) {
Roland Levillain199f3362014-11-27 17:15:16 +0000303 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize,
304 pInstanceofNonTrivial)));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000305 } else {
306 DCHECK(instruction_->IsCheckCast());
307 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
308 }
309
310 codegen->RecordPcInfo(instruction_, dex_pc_);
311 if (instruction_->IsInstanceOf()) {
312 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
313 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000314 codegen->RestoreLiveRegisters(locations);
315
316 __ jmp(GetExitLabel());
317 }
318
319 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000320 HInstruction* const instruction_;
321 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000322 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000323 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324
325 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
326};
327
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100328#undef __
329#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
330
Dave Allison20dfc792014-06-16 20:44:29 -0700331inline Condition X86Condition(IfCondition cond) {
332 switch (cond) {
333 case kCondEQ: return kEqual;
334 case kCondNE: return kNotEqual;
335 case kCondLT: return kLess;
336 case kCondLE: return kLessEqual;
337 case kCondGT: return kGreater;
338 case kCondGE: return kGreaterEqual;
339 default:
340 LOG(FATAL) << "Unknown if condition";
341 }
342 return kEqual;
343}
344
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100345void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
346 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
347}
348
349void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
350 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
351}
352
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100353size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
354 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
355 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100356}
357
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100358size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
359 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
360 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100361}
362
Mark Mendell7c8d0092015-01-26 11:21:33 -0500363size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
364 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
365 return GetFloatingPointSpillSlotSize();
366}
367
368size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
369 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
370 return GetFloatingPointSpillSlotSize();
371}
372
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000373CodeGeneratorX86::CodeGeneratorX86(HGraph* graph, const CompilerOptions& compiler_options)
374 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfXmmRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000375 kNumberOfRegisterPairs, (1 << kFakeReturnRegister), 0, compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100376 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100377 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100378 instruction_visitor_(graph, this),
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000379 move_resolver_(graph->GetArena(), this) {
380 // Use a fake return address register to mimic Quick.
381 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100382}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100383
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100384Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100385 switch (type) {
386 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100387 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100388 X86ManagedRegister pair =
389 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100390 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
391 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100392 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
393 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100394 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100395 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100396 }
397
398 case Primitive::kPrimByte:
399 case Primitive::kPrimBoolean:
400 case Primitive::kPrimChar:
401 case Primitive::kPrimShort:
402 case Primitive::kPrimInt:
403 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100404 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100405 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100406 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100407 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
408 X86ManagedRegister current =
409 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
410 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100411 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100412 }
413 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100414 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100415 }
416
417 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100418 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100419 return Location::FpuRegisterLocation(
420 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100421 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100422
423 case Primitive::kPrimVoid:
424 LOG(FATAL) << "Unreachable type " << type;
425 }
426
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100427 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100428}
429
Nicolas Geoffray98893962015-01-21 12:32:32 +0000430void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline ATTRIBUTE_UNUSED) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100431 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100432 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100433
434 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100435 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100436
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000437 // TODO: We currently don't use Quick's callee saved registers.
438 DCHECK(kFollowsQuickABI);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100439 blocked_core_registers_[EBP] = true;
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000440 blocked_core_registers_[ESI] = true;
441 blocked_core_registers_[EDI] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100442
443 UpdateBlockedPairRegisters();
444}
445
446void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
447 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
448 X86ManagedRegister current =
449 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
450 if (blocked_core_registers_[current.AsRegisterPairLow()]
451 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
452 blocked_register_pairs_[i] = true;
453 }
454 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100455}
456
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100457InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
458 : HGraphVisitor(graph),
459 assembler_(codegen->GetAssembler()),
460 codegen_(codegen) {}
461
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000462void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000463 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000464 bool skip_overflow_check =
465 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000466 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000467
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000468 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100469 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100470 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100471 }
472
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000473 if (!HasEmptyFrame()) {
474 __ subl(ESP, Immediate(GetFrameSize() - FrameEntrySpillSize()));
475 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
476 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000477}
478
479void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000480 if (!HasEmptyFrame()) {
481 __ addl(ESP, Immediate(GetFrameSize() - FrameEntrySpillSize()));
482 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000483}
484
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100485void CodeGeneratorX86::Bind(HBasicBlock* block) {
486 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000487}
488
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100489void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000490 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100491 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000492}
493
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100494Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
495 switch (load->GetType()) {
496 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100497 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100498 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
499 break;
500
501 case Primitive::kPrimInt:
502 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100503 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100504 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100505
506 case Primitive::kPrimBoolean:
507 case Primitive::kPrimByte:
508 case Primitive::kPrimChar:
509 case Primitive::kPrimShort:
510 case Primitive::kPrimVoid:
511 LOG(FATAL) << "Unexpected type " << load->GetType();
512 }
513
514 LOG(FATAL) << "Unreachable";
515 return Location();
516}
517
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100518Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
519 switch (type) {
520 case Primitive::kPrimBoolean:
521 case Primitive::kPrimByte:
522 case Primitive::kPrimChar:
523 case Primitive::kPrimShort:
524 case Primitive::kPrimInt:
525 case Primitive::kPrimNot: {
526 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000527 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100528 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100529 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100530 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000531 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100532 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100533 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100534
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000535 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100536 uint32_t index = gp_index_;
537 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000538 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100539 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100540 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
541 calling_convention.GetRegisterPairAt(index));
542 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100543 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000544 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
545 }
546 }
547
548 case Primitive::kPrimFloat: {
549 uint32_t index = fp_index_++;
550 stack_index_++;
551 if (index < calling_convention.GetNumberOfFpuRegisters()) {
552 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
553 } else {
554 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
555 }
556 }
557
558 case Primitive::kPrimDouble: {
559 uint32_t index = fp_index_++;
560 stack_index_ += 2;
561 if (index < calling_convention.GetNumberOfFpuRegisters()) {
562 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
563 } else {
564 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100565 }
566 }
567
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100568 case Primitive::kPrimVoid:
569 LOG(FATAL) << "Unexpected parameter type " << type;
570 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100571 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100572 return Location();
573}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100574
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100575void CodeGeneratorX86::Move32(Location destination, Location source) {
576 if (source.Equals(destination)) {
577 return;
578 }
579 if (destination.IsRegister()) {
580 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000581 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100582 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000583 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100584 } else {
585 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000586 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100587 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100588 } else if (destination.IsFpuRegister()) {
589 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000590 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100591 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000592 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100593 } else {
594 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000595 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100596 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100597 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000598 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100599 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000600 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100601 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000602 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500603 } else if (source.IsConstant()) {
604 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000605 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500606 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100607 } else {
608 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100609 __ pushl(Address(ESP, source.GetStackIndex()));
610 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100611 }
612 }
613}
614
615void CodeGeneratorX86::Move64(Location destination, Location source) {
616 if (source.Equals(destination)) {
617 return;
618 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100619 if (destination.IsRegisterPair()) {
620 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000621 EmitParallelMoves(
622 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
623 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
624 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
625 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100626 } else if (source.IsFpuRegister()) {
627 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100628 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000629 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100630 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100631 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
632 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100633 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
634 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100635 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500636 if (source.IsFpuRegister()) {
637 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
638 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000639 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100640 } else {
641 LOG(FATAL) << "Unimplemented";
642 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100643 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000644 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100645 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000646 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100647 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100648 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100649 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100650 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000651 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100652 } else {
653 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000654 EmitParallelMoves(
655 Location::StackSlot(source.GetStackIndex()),
656 Location::StackSlot(destination.GetStackIndex()),
657 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
658 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100659 }
660 }
661}
662
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100663void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000664 LocationSummary* locations = instruction->GetLocations();
665 if (locations != nullptr && locations->Out().Equals(location)) {
666 return;
667 }
668
669 if (locations != nullptr && locations->Out().IsConstant()) {
670 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000671 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
672 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000673 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000674 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000675 } else if (location.IsStackSlot()) {
676 __ movl(Address(ESP, location.GetStackIndex()), imm);
677 } else {
678 DCHECK(location.IsConstant());
679 DCHECK_EQ(location.GetConstant(), const_to_move);
680 }
681 } else if (const_to_move->IsLongConstant()) {
682 int64_t value = const_to_move->AsLongConstant()->GetValue();
683 if (location.IsRegisterPair()) {
684 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
685 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
686 } else if (location.IsDoubleStackSlot()) {
687 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000688 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
689 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000690 } else {
691 DCHECK(location.IsConstant());
692 DCHECK_EQ(location.GetConstant(), instruction);
693 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100694 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000695 } else if (instruction->IsTemporary()) {
696 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000697 if (temp_location.IsStackSlot()) {
698 Move32(location, temp_location);
699 } else {
700 DCHECK(temp_location.IsDoubleStackSlot());
701 Move64(location, temp_location);
702 }
Roland Levillain476df552014-10-09 17:51:36 +0100703 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100704 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100705 switch (instruction->GetType()) {
706 case Primitive::kPrimBoolean:
707 case Primitive::kPrimByte:
708 case Primitive::kPrimChar:
709 case Primitive::kPrimShort:
710 case Primitive::kPrimInt:
711 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100712 case Primitive::kPrimFloat:
713 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100714 break;
715
716 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100717 case Primitive::kPrimDouble:
718 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100719 break;
720
721 default:
722 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
723 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000724 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100725 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100726 switch (instruction->GetType()) {
727 case Primitive::kPrimBoolean:
728 case Primitive::kPrimByte:
729 case Primitive::kPrimChar:
730 case Primitive::kPrimShort:
731 case Primitive::kPrimInt:
732 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100733 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000734 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100735 break;
736
737 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100738 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000739 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100740 break;
741
742 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100743 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100744 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000745 }
746}
747
748void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000749 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000750}
751
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000752void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000753 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100754 DCHECK(!successor->IsExitBlock());
755
756 HBasicBlock* block = got->GetBlock();
757 HInstruction* previous = got->GetPrevious();
758
759 HLoopInformation* info = block->GetLoopInformation();
760 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
761 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
762 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
763 return;
764 }
765
766 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
767 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
768 }
769 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000770 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000771 }
772}
773
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000774void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000775 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000776}
777
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000778void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700779 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000780 if (kIsDebugBuild) {
781 __ Comment("Unreachable");
782 __ int3();
783 }
784}
785
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000786void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100787 LocationSummary* locations =
788 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100789 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100790 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100791 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100792 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000793}
794
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000795void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700796 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100797 if (cond->IsIntConstant()) {
798 // Constant condition, statically compared against 1.
799 int32_t cond_value = cond->AsIntConstant()->GetValue();
800 if (cond_value == 1) {
801 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
802 if_instr->IfTrueSuccessor())) {
803 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100804 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100805 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100806 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100807 DCHECK_EQ(cond_value, 0);
808 }
809 } else {
810 bool materialized =
811 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
812 // Moves do not affect the eflags register, so if the condition is
813 // evaluated just before the if, we don't need to evaluate it
814 // again.
815 bool eflags_set = cond->IsCondition()
816 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
817 if (materialized) {
818 if (!eflags_set) {
819 // Materialized condition, compare against 0.
820 Location lhs = if_instr->GetLocations()->InAt(0);
821 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500822 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100823 } else {
824 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
825 }
826 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
827 } else {
828 __ j(X86Condition(cond->AsCondition()->GetCondition()),
829 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
830 }
831 } else {
832 Location lhs = cond->GetLocations()->InAt(0);
833 Location rhs = cond->GetLocations()->InAt(1);
834 // LHS is guaranteed to be in a register (see
835 // LocationsBuilderX86::VisitCondition).
836 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000837 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100838 } else if (rhs.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -0500839 int32_t constant = rhs.GetConstant()->AsIntConstant()->GetValue();
840 if (constant == 0) {
841 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
842 } else {
843 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
844 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100845 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000846 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100847 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100848 __ j(X86Condition(cond->AsCondition()->GetCondition()),
849 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700850 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100851 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100852 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
853 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700854 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000855 }
856}
857
858void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000859 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000860}
861
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000862void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
863 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000864}
865
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000866void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100867 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000868}
869
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000870void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100871 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700872 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000873}
874
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100875void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100876 LocationSummary* locations =
877 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100878 switch (store->InputAt(1)->GetType()) {
879 case Primitive::kPrimBoolean:
880 case Primitive::kPrimByte:
881 case Primitive::kPrimChar:
882 case Primitive::kPrimShort:
883 case Primitive::kPrimInt:
884 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100885 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100886 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
887 break;
888
889 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100890 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100891 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
892 break;
893
894 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100895 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100896 }
897 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000898}
899
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000900void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700901 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000902}
903
Dave Allison20dfc792014-06-16 20:44:29 -0700904void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100905 LocationSummary* locations =
906 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100907 locations->SetInAt(0, Location::RequiresRegister());
908 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100909 if (comp->NeedsMaterialization()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000910 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100911 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000912}
913
Dave Allison20dfc792014-06-16 20:44:29 -0700914void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
915 if (comp->NeedsMaterialization()) {
916 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000917 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100918 // Clear register: setcc only sets the low byte.
919 __ xorl(reg, reg);
Mark Mendell09b84632015-02-13 17:48:38 -0500920 Location lhs = locations->InAt(0);
921 Location rhs = locations->InAt(1);
922 if (rhs.IsRegister()) {
923 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
924 } else if (rhs.IsConstant()) {
Mingyao Yang8928cab2015-03-03 16:15:23 -0800925 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -0500926 if (constant == 0) {
927 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
928 } else {
929 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
930 }
Dave Allison20dfc792014-06-16 20:44:29 -0700931 } else {
Mark Mendell09b84632015-02-13 17:48:38 -0500932 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Dave Allison20dfc792014-06-16 20:44:29 -0700933 }
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 Geoffrayd6138ef2015-02-18 14:48:53 +0000997void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
998 LocationSummary* locations =
999 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1000 locations->SetOut(Location::ConstantLocation(constant));
1001}
1002
1003void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1004 // Will be generated at use site.
1005 UNUSED(constant);
1006}
1007
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001008void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001009 LocationSummary* locations =
1010 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001011 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001012}
1013
1014void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1015 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001016 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001017}
1018
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001019void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1020 LocationSummary* locations =
1021 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1022 locations->SetOut(Location::ConstantLocation(constant));
1023}
1024
1025void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* 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
1030void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1031 LocationSummary* locations =
1032 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1033 locations->SetOut(Location::ConstantLocation(constant));
1034}
1035
1036void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1037 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001038 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001039}
1040
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001041void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001042 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001043}
1044
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001045void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001046 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001047 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001048 __ ret();
1049}
1050
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001051void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001052 LocationSummary* locations =
1053 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001054 switch (ret->InputAt(0)->GetType()) {
1055 case Primitive::kPrimBoolean:
1056 case Primitive::kPrimByte:
1057 case Primitive::kPrimChar:
1058 case Primitive::kPrimShort:
1059 case Primitive::kPrimInt:
1060 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001061 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001062 break;
1063
1064 case Primitive::kPrimLong:
1065 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001066 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001067 break;
1068
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001069 case Primitive::kPrimFloat:
1070 case Primitive::kPrimDouble:
1071 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001072 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001073 break;
1074
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001075 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001076 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001077 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001078}
1079
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001080void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001081 if (kIsDebugBuild) {
1082 switch (ret->InputAt(0)->GetType()) {
1083 case Primitive::kPrimBoolean:
1084 case Primitive::kPrimByte:
1085 case Primitive::kPrimChar:
1086 case Primitive::kPrimShort:
1087 case Primitive::kPrimInt:
1088 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001089 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001090 break;
1091
1092 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001093 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1094 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001095 break;
1096
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001097 case Primitive::kPrimFloat:
1098 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001099 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001100 break;
1101
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001102 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001103 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001104 }
1105 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001106 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001107 __ ret();
1108}
1109
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001110void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001111 HandleInvoke(invoke);
1112}
1113
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001114void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001115 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001116
1117 // TODO: Implement all kinds of calls:
1118 // 1) boot -> boot
1119 // 2) app -> boot
1120 // 3) app -> app
1121 //
1122 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1123
1124 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001125 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001126 if (!invoke->IsRecursive()) {
1127 // temp = temp->dex_cache_resolved_methods_;
1128 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
1129 // temp = temp[index_in_cache]
1130 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
1131 // (temp + offset_of_quick_compiled_code)()
1132 __ call(Address(
1133 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
1134 } else {
1135 __ call(codegen_->GetFrameEntryLabel());
1136 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001137
1138 DCHECK(!codegen_->IsLeafMethod());
1139 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1140}
1141
1142void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1143 HandleInvoke(invoke);
1144}
1145
1146void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001147 LocationSummary* locations =
1148 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001149 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001150
1151 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001152 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001153 HInstruction* input = invoke->InputAt(i);
1154 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1155 }
1156
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001157 switch (invoke->GetType()) {
1158 case Primitive::kPrimBoolean:
1159 case Primitive::kPrimByte:
1160 case Primitive::kPrimChar:
1161 case Primitive::kPrimShort:
1162 case Primitive::kPrimInt:
1163 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001164 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001165 break;
1166
1167 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001168 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001169 break;
1170
1171 case Primitive::kPrimVoid:
1172 break;
1173
1174 case Primitive::kPrimDouble:
1175 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001176 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001177 break;
1178 }
1179
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001180 invoke->SetLocations(locations);
1181}
1182
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001183void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001184 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001185 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1186 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1187 LocationSummary* locations = invoke->GetLocations();
1188 Location receiver = locations->InAt(0);
1189 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1190 // temp = object->GetClass();
1191 if (receiver.IsStackSlot()) {
1192 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1193 __ movl(temp, Address(temp, class_offset));
1194 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001195 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001196 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001197 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001198 // temp = temp->GetMethodAt(method_offset);
1199 __ movl(temp, Address(temp, method_offset));
1200 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001201 __ call(Address(
1202 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001203
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001204 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001205 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001206}
1207
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001208void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1209 HandleInvoke(invoke);
1210 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001211 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001212}
1213
1214void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1215 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001216 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001217 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1218 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1219 LocationSummary* locations = invoke->GetLocations();
1220 Location receiver = locations->InAt(0);
1221 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1222
1223 // Set the hidden argument.
1224 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001225 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001226
1227 // temp = object->GetClass();
1228 if (receiver.IsStackSlot()) {
1229 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1230 __ movl(temp, Address(temp, class_offset));
1231 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001232 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001233 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001234 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001235 // temp = temp->GetImtEntryAt(method_offset);
1236 __ movl(temp, Address(temp, method_offset));
1237 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001238 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001239 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001240
1241 DCHECK(!codegen_->IsLeafMethod());
1242 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1243}
1244
Roland Levillain88cb1752014-10-20 16:36:47 +01001245void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1246 LocationSummary* locations =
1247 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1248 switch (neg->GetResultType()) {
1249 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001250 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001251 locations->SetInAt(0, Location::RequiresRegister());
1252 locations->SetOut(Location::SameAsFirstInput());
1253 break;
1254
Roland Levillain88cb1752014-10-20 16:36:47 +01001255 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001256 locations->SetInAt(0, Location::RequiresFpuRegister());
1257 locations->SetOut(Location::SameAsFirstInput());
1258 locations->AddTemp(Location::RequiresRegister());
1259 locations->AddTemp(Location::RequiresFpuRegister());
1260 break;
1261
Roland Levillain88cb1752014-10-20 16:36:47 +01001262 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001263 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001264 locations->SetOut(Location::SameAsFirstInput());
1265 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001266 break;
1267
1268 default:
1269 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1270 }
1271}
1272
1273void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1274 LocationSummary* locations = neg->GetLocations();
1275 Location out = locations->Out();
1276 Location in = locations->InAt(0);
1277 switch (neg->GetResultType()) {
1278 case Primitive::kPrimInt:
1279 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001280 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001281 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001282 break;
1283
1284 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001285 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001286 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001287 __ negl(out.AsRegisterPairLow<Register>());
1288 // Negation is similar to subtraction from zero. The least
1289 // significant byte triggers a borrow when it is different from
1290 // zero; to take it into account, add 1 to the most significant
1291 // byte if the carry flag (CF) is set to 1 after the first NEGL
1292 // operation.
1293 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1294 __ negl(out.AsRegisterPairHigh<Register>());
1295 break;
1296
Roland Levillain5368c212014-11-27 15:03:41 +00001297 case Primitive::kPrimFloat: {
1298 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001299 Register constant = locations->GetTemp(0).AsRegister<Register>();
1300 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001301 // Implement float negation with an exclusive or with value
1302 // 0x80000000 (mask for bit 31, representing the sign of a
1303 // single-precision floating-point number).
1304 __ movl(constant, Immediate(INT32_C(0x80000000)));
1305 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001306 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001307 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001308 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001309
Roland Levillain5368c212014-11-27 15:03:41 +00001310 case Primitive::kPrimDouble: {
1311 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001312 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001313 // Implement double negation with an exclusive or with value
1314 // 0x8000000000000000 (mask for bit 63, representing the sign of
1315 // a double-precision floating-point number).
1316 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001317 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001318 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001319 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001320
1321 default:
1322 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1323 }
1324}
1325
Roland Levillaindff1f282014-11-05 14:15:05 +00001326void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001327 Primitive::Type result_type = conversion->GetResultType();
1328 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001329 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001330
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001331 // The float-to-long and double-to-long type conversions rely on a
1332 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001333 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001334 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1335 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001336 ? LocationSummary::kCall
1337 : LocationSummary::kNoCall;
1338 LocationSummary* locations =
1339 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1340
Roland Levillaindff1f282014-11-05 14:15:05 +00001341 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001342 case Primitive::kPrimByte:
1343 switch (input_type) {
1344 case Primitive::kPrimShort:
1345 case Primitive::kPrimInt:
1346 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001347 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001348 locations->SetInAt(0, Location::Any());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001349 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1350 break;
1351
1352 default:
1353 LOG(FATAL) << "Unexpected type conversion from " << input_type
1354 << " to " << result_type;
1355 }
1356 break;
1357
Roland Levillain01a8d712014-11-14 16:27:39 +00001358 case Primitive::kPrimShort:
1359 switch (input_type) {
1360 case Primitive::kPrimByte:
1361 case Primitive::kPrimInt:
1362 case Primitive::kPrimChar:
1363 // Processing a Dex `int-to-short' instruction.
1364 locations->SetInAt(0, Location::Any());
1365 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1366 break;
1367
1368 default:
1369 LOG(FATAL) << "Unexpected type conversion from " << input_type
1370 << " to " << result_type;
1371 }
1372 break;
1373
Roland Levillain946e1432014-11-11 17:35:19 +00001374 case Primitive::kPrimInt:
1375 switch (input_type) {
1376 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001377 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001378 locations->SetInAt(0, Location::Any());
1379 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1380 break;
1381
1382 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001383 // Processing a Dex `float-to-int' instruction.
1384 locations->SetInAt(0, Location::RequiresFpuRegister());
1385 locations->SetOut(Location::RequiresRegister());
1386 locations->AddTemp(Location::RequiresFpuRegister());
1387 break;
1388
Roland Levillain946e1432014-11-11 17:35:19 +00001389 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001390 // Processing a Dex `double-to-int' instruction.
1391 locations->SetInAt(0, Location::RequiresFpuRegister());
1392 locations->SetOut(Location::RequiresRegister());
1393 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001394 break;
1395
1396 default:
1397 LOG(FATAL) << "Unexpected type conversion from " << input_type
1398 << " to " << result_type;
1399 }
1400 break;
1401
Roland Levillaindff1f282014-11-05 14:15:05 +00001402 case Primitive::kPrimLong:
1403 switch (input_type) {
1404 case Primitive::kPrimByte:
1405 case Primitive::kPrimShort:
1406 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001407 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001408 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001409 locations->SetInAt(0, Location::RegisterLocation(EAX));
1410 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1411 break;
1412
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001413 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001414 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001415 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001416 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001417 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1418 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1419
Vladimir Marko949c91f2015-01-27 10:48:44 +00001420 // The runtime helper puts the result in EAX, EDX.
1421 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001422 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001423 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001424
1425 default:
1426 LOG(FATAL) << "Unexpected type conversion from " << input_type
1427 << " to " << result_type;
1428 }
1429 break;
1430
Roland Levillain981e4542014-11-14 11:47:14 +00001431 case Primitive::kPrimChar:
1432 switch (input_type) {
1433 case Primitive::kPrimByte:
1434 case Primitive::kPrimShort:
1435 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001436 // Processing a Dex `int-to-char' instruction.
1437 locations->SetInAt(0, Location::Any());
1438 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1439 break;
1440
1441 default:
1442 LOG(FATAL) << "Unexpected type conversion from " << input_type
1443 << " to " << result_type;
1444 }
1445 break;
1446
Roland Levillaindff1f282014-11-05 14:15:05 +00001447 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001448 switch (input_type) {
1449 case Primitive::kPrimByte:
1450 case Primitive::kPrimShort:
1451 case Primitive::kPrimInt:
1452 case Primitive::kPrimChar:
1453 // Processing a Dex `int-to-float' instruction.
1454 locations->SetInAt(0, Location::RequiresRegister());
1455 locations->SetOut(Location::RequiresFpuRegister());
1456 break;
1457
1458 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001459 // Processing a Dex `long-to-float' instruction.
1460 locations->SetInAt(0, Location::RequiresRegister());
1461 locations->SetOut(Location::RequiresFpuRegister());
1462 locations->AddTemp(Location::RequiresFpuRegister());
1463 locations->AddTemp(Location::RequiresFpuRegister());
1464 break;
1465
Roland Levillaincff13742014-11-17 14:32:17 +00001466 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001467 // Processing a Dex `double-to-float' instruction.
1468 locations->SetInAt(0, Location::RequiresFpuRegister());
1469 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001470 break;
1471
1472 default:
1473 LOG(FATAL) << "Unexpected type conversion from " << input_type
1474 << " to " << result_type;
1475 };
1476 break;
1477
Roland Levillaindff1f282014-11-05 14:15:05 +00001478 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001479 switch (input_type) {
1480 case Primitive::kPrimByte:
1481 case Primitive::kPrimShort:
1482 case Primitive::kPrimInt:
1483 case Primitive::kPrimChar:
1484 // Processing a Dex `int-to-double' instruction.
1485 locations->SetInAt(0, Location::RequiresRegister());
1486 locations->SetOut(Location::RequiresFpuRegister());
1487 break;
1488
1489 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001490 // Processing a Dex `long-to-double' instruction.
1491 locations->SetInAt(0, Location::RequiresRegister());
1492 locations->SetOut(Location::RequiresFpuRegister());
1493 locations->AddTemp(Location::RequiresFpuRegister());
1494 locations->AddTemp(Location::RequiresFpuRegister());
1495 break;
1496
Roland Levillaincff13742014-11-17 14:32:17 +00001497 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001498 // Processing a Dex `float-to-double' instruction.
1499 locations->SetInAt(0, Location::RequiresFpuRegister());
1500 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001501 break;
1502
1503 default:
1504 LOG(FATAL) << "Unexpected type conversion from " << input_type
1505 << " to " << result_type;
1506 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001507 break;
1508
1509 default:
1510 LOG(FATAL) << "Unexpected type conversion from " << input_type
1511 << " to " << result_type;
1512 }
1513}
1514
1515void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1516 LocationSummary* locations = conversion->GetLocations();
1517 Location out = locations->Out();
1518 Location in = locations->InAt(0);
1519 Primitive::Type result_type = conversion->GetResultType();
1520 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001521 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001522 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001523 case Primitive::kPrimByte:
1524 switch (input_type) {
1525 case Primitive::kPrimShort:
1526 case Primitive::kPrimInt:
1527 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001528 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001529 if (in.IsRegister()) {
1530 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
1531 } else if (in.IsStackSlot()) {
1532 __ movsxb(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
1533 } else {
1534 DCHECK(in.GetConstant()->IsIntConstant());
1535 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1536 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1537 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001538 break;
1539
1540 default:
1541 LOG(FATAL) << "Unexpected type conversion from " << input_type
1542 << " to " << result_type;
1543 }
1544 break;
1545
Roland Levillain01a8d712014-11-14 16:27:39 +00001546 case Primitive::kPrimShort:
1547 switch (input_type) {
1548 case Primitive::kPrimByte:
1549 case Primitive::kPrimInt:
1550 case Primitive::kPrimChar:
1551 // Processing a Dex `int-to-short' instruction.
1552 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001553 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001554 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001555 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001556 } else {
1557 DCHECK(in.GetConstant()->IsIntConstant());
1558 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001559 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001560 }
1561 break;
1562
1563 default:
1564 LOG(FATAL) << "Unexpected type conversion from " << input_type
1565 << " to " << result_type;
1566 }
1567 break;
1568
Roland Levillain946e1432014-11-11 17:35:19 +00001569 case Primitive::kPrimInt:
1570 switch (input_type) {
1571 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001572 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001573 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001574 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001575 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001576 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001577 } else {
1578 DCHECK(in.IsConstant());
1579 DCHECK(in.GetConstant()->IsLongConstant());
1580 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001581 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001582 }
1583 break;
1584
Roland Levillain3f8f9362014-12-02 17:45:01 +00001585 case Primitive::kPrimFloat: {
1586 // Processing a Dex `float-to-int' instruction.
1587 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1588 Register output = out.AsRegister<Register>();
1589 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1590 Label done, nan;
1591
1592 __ movl(output, Immediate(kPrimIntMax));
1593 // temp = int-to-float(output)
1594 __ cvtsi2ss(temp, output);
1595 // if input >= temp goto done
1596 __ comiss(input, temp);
1597 __ j(kAboveEqual, &done);
1598 // if input == NaN goto nan
1599 __ j(kUnordered, &nan);
1600 // output = float-to-int-truncate(input)
1601 __ cvttss2si(output, input);
1602 __ jmp(&done);
1603 __ Bind(&nan);
1604 // output = 0
1605 __ xorl(output, output);
1606 __ Bind(&done);
1607 break;
1608 }
1609
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001610 case Primitive::kPrimDouble: {
1611 // Processing a Dex `double-to-int' instruction.
1612 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1613 Register output = out.AsRegister<Register>();
1614 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1615 Label done, nan;
1616
1617 __ movl(output, Immediate(kPrimIntMax));
1618 // temp = int-to-double(output)
1619 __ cvtsi2sd(temp, output);
1620 // if input >= temp goto done
1621 __ comisd(input, temp);
1622 __ j(kAboveEqual, &done);
1623 // if input == NaN goto nan
1624 __ j(kUnordered, &nan);
1625 // output = double-to-int-truncate(input)
1626 __ cvttsd2si(output, input);
1627 __ jmp(&done);
1628 __ Bind(&nan);
1629 // output = 0
1630 __ xorl(output, output);
1631 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001632 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001633 }
Roland Levillain946e1432014-11-11 17:35:19 +00001634
1635 default:
1636 LOG(FATAL) << "Unexpected type conversion from " << input_type
1637 << " to " << result_type;
1638 }
1639 break;
1640
Roland Levillaindff1f282014-11-05 14:15:05 +00001641 case Primitive::kPrimLong:
1642 switch (input_type) {
1643 case Primitive::kPrimByte:
1644 case Primitive::kPrimShort:
1645 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001646 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001647 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001648 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1649 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001650 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001651 __ cdq();
1652 break;
1653
1654 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001655 // Processing a Dex `float-to-long' instruction.
1656 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001657 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1658 break;
1659
Roland Levillaindff1f282014-11-05 14:15:05 +00001660 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001661 // Processing a Dex `double-to-long' instruction.
1662 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1663 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001664 break;
1665
1666 default:
1667 LOG(FATAL) << "Unexpected type conversion from " << input_type
1668 << " to " << result_type;
1669 }
1670 break;
1671
Roland Levillain981e4542014-11-14 11:47:14 +00001672 case Primitive::kPrimChar:
1673 switch (input_type) {
1674 case Primitive::kPrimByte:
1675 case Primitive::kPrimShort:
1676 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001677 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1678 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001679 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001680 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001681 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001682 } else {
1683 DCHECK(in.GetConstant()->IsIntConstant());
1684 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001685 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001686 }
1687 break;
1688
1689 default:
1690 LOG(FATAL) << "Unexpected type conversion from " << input_type
1691 << " to " << result_type;
1692 }
1693 break;
1694
Roland Levillaindff1f282014-11-05 14:15:05 +00001695 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001696 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001697 case Primitive::kPrimByte:
1698 case Primitive::kPrimShort:
1699 case Primitive::kPrimInt:
1700 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001701 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001702 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001703 break;
1704
Roland Levillain6d0e4832014-11-27 18:31:21 +00001705 case Primitive::kPrimLong: {
1706 // Processing a Dex `long-to-float' instruction.
1707 Register low = in.AsRegisterPairLow<Register>();
1708 Register high = in.AsRegisterPairHigh<Register>();
1709 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1710 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1711 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1712
1713 // Operations use doubles for precision reasons (each 32-bit
1714 // half of a long fits in the 53-bit mantissa of a double,
1715 // but not in the 24-bit mantissa of a float). This is
1716 // especially important for the low bits. The result is
1717 // eventually converted to float.
1718
1719 // low = low - 2^31 (to prevent bit 31 of `low` to be
1720 // interpreted as a sign bit)
1721 __ subl(low, Immediate(0x80000000));
1722 // temp = int-to-double(high)
1723 __ cvtsi2sd(temp, high);
1724 // temp = temp * 2^32
1725 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
1726 __ mulsd(temp, constant);
1727 // result = int-to-double(low)
1728 __ cvtsi2sd(result, low);
1729 // result = result + 2^31 (restore the original value of `low`)
1730 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
1731 __ addsd(result, constant);
1732 // result = result + temp
1733 __ addsd(result, temp);
1734 // result = double-to-float(result)
1735 __ cvtsd2ss(result, result);
1736 break;
1737 }
1738
Roland Levillaincff13742014-11-17 14:32:17 +00001739 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001740 // Processing a Dex `double-to-float' instruction.
1741 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001742 break;
1743
1744 default:
1745 LOG(FATAL) << "Unexpected type conversion from " << input_type
1746 << " to " << result_type;
1747 };
1748 break;
1749
Roland Levillaindff1f282014-11-05 14:15:05 +00001750 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001751 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001752 case Primitive::kPrimByte:
1753 case Primitive::kPrimShort:
1754 case Primitive::kPrimInt:
1755 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001756 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001757 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001758 break;
1759
Roland Levillain647b9ed2014-11-27 12:06:00 +00001760 case Primitive::kPrimLong: {
1761 // Processing a Dex `long-to-double' instruction.
1762 Register low = in.AsRegisterPairLow<Register>();
1763 Register high = in.AsRegisterPairHigh<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001764 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1765 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1766 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001767
Roland Levillain647b9ed2014-11-27 12:06:00 +00001768 // low = low - 2^31 (to prevent bit 31 of `low` to be
1769 // interpreted as a sign bit)
1770 __ subl(low, Immediate(0x80000000));
1771 // temp = int-to-double(high)
1772 __ cvtsi2sd(temp, high);
1773 // temp = temp * 2^32
Roland Levillain6d0e4832014-11-27 18:31:21 +00001774 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001775 __ mulsd(temp, constant);
1776 // result = int-to-double(low)
1777 __ cvtsi2sd(result, low);
1778 // result = result + 2^31 (restore the original value of `low`)
Roland Levillain6d0e4832014-11-27 18:31:21 +00001779 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001780 __ addsd(result, constant);
1781 // result = result + temp
1782 __ addsd(result, temp);
1783 break;
1784 }
1785
Roland Levillaincff13742014-11-17 14:32:17 +00001786 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001787 // Processing a Dex `float-to-double' instruction.
1788 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001789 break;
1790
1791 default:
1792 LOG(FATAL) << "Unexpected type conversion from " << input_type
1793 << " to " << result_type;
1794 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001795 break;
1796
1797 default:
1798 LOG(FATAL) << "Unexpected type conversion from " << input_type
1799 << " to " << result_type;
1800 }
1801}
1802
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001803void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001804 LocationSummary* locations =
1805 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001806 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001807 case Primitive::kPrimInt: {
1808 locations->SetInAt(0, Location::RequiresRegister());
1809 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
1810 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1811 break;
1812 }
1813
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001814 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001815 locations->SetInAt(0, Location::RequiresRegister());
1816 locations->SetInAt(1, Location::Any());
1817 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001818 break;
1819 }
1820
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001821 case Primitive::kPrimFloat:
1822 case Primitive::kPrimDouble: {
1823 locations->SetInAt(0, Location::RequiresFpuRegister());
Calin Juravle3173c8a2015-02-23 15:53:39 +00001824 locations->SetInAt(1, Location::RequiresFpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001825 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001826 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001827 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001828
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001829 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001830 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1831 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001832 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001833}
1834
1835void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1836 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001837 Location first = locations->InAt(0);
1838 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05001839 Location out = locations->Out();
1840
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001841 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001842 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001843 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001844 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1845 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
1846 } else {
1847 __ leal(out.AsRegister<Register>(), Address(
1848 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
1849 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001850 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001851 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
1852 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
1853 __ addl(out.AsRegister<Register>(), Immediate(value));
1854 } else {
1855 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
1856 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001857 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05001858 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001859 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001860 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001861 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001862 }
1863
1864 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001865 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001866 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1867 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001868 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001869 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1870 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001871 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001872 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001873 break;
1874 }
1875
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001876 case Primitive::kPrimFloat: {
1877 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001878 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001879 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001880 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001881 }
1882
1883 case Primitive::kPrimDouble: {
1884 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001885 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001886 }
1887 break;
1888 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001889
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001890 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001891 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001892 }
1893}
1894
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001895void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001896 LocationSummary* locations =
1897 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001898 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001899 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001900 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001901 locations->SetInAt(0, Location::RequiresRegister());
1902 locations->SetInAt(1, Location::Any());
1903 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001904 break;
1905 }
Calin Juravle11351682014-10-23 15:38:15 +01001906 case Primitive::kPrimFloat:
1907 case Primitive::kPrimDouble: {
1908 locations->SetInAt(0, Location::RequiresFpuRegister());
1909 locations->SetInAt(1, Location::RequiresFpuRegister());
1910 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001911 break;
Calin Juravle11351682014-10-23 15:38:15 +01001912 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001913
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001914 default:
Calin Juravle11351682014-10-23 15:38:15 +01001915 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001916 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001917}
1918
1919void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1920 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001921 Location first = locations->InAt(0);
1922 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001923 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001924 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001925 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001926 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001927 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001928 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001929 __ subl(first.AsRegister<Register>(),
1930 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001931 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001932 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001933 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001934 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001935 }
1936
1937 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001938 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01001939 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1940 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001941 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001942 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001943 __ sbbl(first.AsRegisterPairHigh<Register>(),
1944 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001945 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001946 break;
1947 }
1948
Calin Juravle11351682014-10-23 15:38:15 +01001949 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001950 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001951 break;
Calin Juravle11351682014-10-23 15:38:15 +01001952 }
1953
1954 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001955 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001956 break;
1957 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001958
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001959 default:
Calin Juravle11351682014-10-23 15:38:15 +01001960 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001961 }
1962}
1963
Calin Juravle34bacdf2014-10-07 20:23:36 +01001964void LocationsBuilderX86::VisitMul(HMul* mul) {
1965 LocationSummary* locations =
1966 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1967 switch (mul->GetResultType()) {
1968 case Primitive::kPrimInt:
1969 locations->SetInAt(0, Location::RequiresRegister());
1970 locations->SetInAt(1, Location::Any());
1971 locations->SetOut(Location::SameAsFirstInput());
1972 break;
1973 case Primitive::kPrimLong: {
1974 locations->SetInAt(0, Location::RequiresRegister());
1975 // TODO: Currently this handles only stack operands:
1976 // - we don't have enough registers because we currently use Quick ABI.
1977 // - by the time we have a working register allocator we will probably change the ABI
1978 // and fix the above.
1979 // - we don't have a way yet to request operands on stack but the base line compiler
1980 // will leave the operands on the stack with Any().
1981 locations->SetInAt(1, Location::Any());
1982 locations->SetOut(Location::SameAsFirstInput());
1983 // Needed for imul on 32bits with 64bits output.
1984 locations->AddTemp(Location::RegisterLocation(EAX));
1985 locations->AddTemp(Location::RegisterLocation(EDX));
1986 break;
1987 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001988 case Primitive::kPrimFloat:
1989 case Primitive::kPrimDouble: {
1990 locations->SetInAt(0, Location::RequiresFpuRegister());
1991 locations->SetInAt(1, Location::RequiresFpuRegister());
1992 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001993 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001994 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001995
1996 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001997 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001998 }
1999}
2000
2001void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2002 LocationSummary* locations = mul->GetLocations();
2003 Location first = locations->InAt(0);
2004 Location second = locations->InAt(1);
2005 DCHECK(first.Equals(locations->Out()));
2006
2007 switch (mul->GetResultType()) {
2008 case Primitive::kPrimInt: {
2009 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002010 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002011 } else if (second.IsConstant()) {
2012 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002013 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002014 } else {
2015 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002016 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002017 }
2018 break;
2019 }
2020
2021 case Primitive::kPrimLong: {
2022 DCHECK(second.IsDoubleStackSlot());
2023
2024 Register in1_hi = first.AsRegisterPairHigh<Register>();
2025 Register in1_lo = first.AsRegisterPairLow<Register>();
2026 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2027 Address in2_lo(ESP, second.GetStackIndex());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002028 Register eax = locations->GetTemp(0).AsRegister<Register>();
2029 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002030
2031 DCHECK_EQ(EAX, eax);
2032 DCHECK_EQ(EDX, edx);
2033
2034 // input: in1 - 64 bits, in2 - 64 bits
2035 // output: in1
2036 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2037 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2038 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
2039
2040 __ movl(eax, in2_hi);
2041 // eax <- in1.lo * in2.hi
2042 __ imull(eax, in1_lo);
2043 // in1.hi <- in1.hi * in2.lo
2044 __ imull(in1_hi, in2_lo);
2045 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2046 __ addl(in1_hi, eax);
2047 // move in1_lo to eax to prepare for double precision
2048 __ movl(eax, in1_lo);
2049 // edx:eax <- in1.lo * in2.lo
2050 __ mull(in2_lo);
2051 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2052 __ addl(in1_hi, edx);
2053 // in1.lo <- (in1.lo * in2.lo)[31:0];
2054 __ movl(in1_lo, eax);
2055
2056 break;
2057 }
2058
Calin Juravleb5bfa962014-10-21 18:02:24 +01002059 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002060 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002061 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002062 }
2063
2064 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002065 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002066 break;
2067 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002068
2069 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002070 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002071 }
2072}
2073
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002074void InstructionCodeGeneratorX86::PushOntoFPStack(Location source, uint32_t temp_offset,
2075 uint32_t stack_adjustment, bool is_float) {
2076 if (source.IsStackSlot()) {
2077 DCHECK(is_float);
2078 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2079 } else if (source.IsDoubleStackSlot()) {
2080 DCHECK(!is_float);
2081 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2082 } else {
2083 // Write the value to the temporary location on the stack and load to FP stack.
2084 if (is_float) {
2085 Location stack_temp = Location::StackSlot(temp_offset);
2086 codegen_->Move32(stack_temp, source);
2087 __ flds(Address(ESP, temp_offset));
2088 } else {
2089 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2090 codegen_->Move64(stack_temp, source);
2091 __ fldl(Address(ESP, temp_offset));
2092 }
2093 }
2094}
2095
2096void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2097 Primitive::Type type = rem->GetResultType();
2098 bool is_float = type == Primitive::kPrimFloat;
2099 size_t elem_size = Primitive::ComponentSize(type);
2100 LocationSummary* locations = rem->GetLocations();
2101 Location first = locations->InAt(0);
2102 Location second = locations->InAt(1);
2103 Location out = locations->Out();
2104
2105 // Create stack space for 2 elements.
2106 // TODO: enhance register allocator to ask for stack temporaries.
2107 __ subl(ESP, Immediate(2 * elem_size));
2108
2109 // Load the values to the FP stack in reverse order, using temporaries if needed.
2110 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2111 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2112
2113 // Loop doing FPREM until we stabilize.
2114 Label retry;
2115 __ Bind(&retry);
2116 __ fprem();
2117
2118 // Move FP status to AX.
2119 __ fstsw();
2120
2121 // And see if the argument reduction is complete. This is signaled by the
2122 // C2 FPU flag bit set to 0.
2123 __ andl(EAX, Immediate(kC2ConditionMask));
2124 __ j(kNotEqual, &retry);
2125
2126 // We have settled on the final value. Retrieve it into an XMM register.
2127 // Store FP top of stack to real stack.
2128 if (is_float) {
2129 __ fsts(Address(ESP, 0));
2130 } else {
2131 __ fstl(Address(ESP, 0));
2132 }
2133
2134 // Pop the 2 items from the FP stack.
2135 __ fucompp();
2136
2137 // Load the value from the stack into an XMM register.
2138 DCHECK(out.IsFpuRegister()) << out;
2139 if (is_float) {
2140 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2141 } else {
2142 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2143 }
2144
2145 // And remove the temporary stack space we allocated.
2146 __ addl(ESP, Immediate(2 * elem_size));
2147}
2148
Calin Juravlebacfec32014-11-14 15:54:36 +00002149void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2150 DCHECK(instruction->IsDiv() || instruction->IsRem());
2151
2152 LocationSummary* locations = instruction->GetLocations();
2153 Location out = locations->Out();
2154 Location first = locations->InAt(0);
2155 Location second = locations->InAt(1);
2156 bool is_div = instruction->IsDiv();
2157
2158 switch (instruction->GetResultType()) {
2159 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002160 Register second_reg = second.AsRegister<Register>();
2161 DCHECK_EQ(EAX, first.AsRegister<Register>());
2162 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002163
2164 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002165 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
2166 is_div);
Calin Juravlebacfec32014-11-14 15:54:36 +00002167 codegen_->AddSlowPath(slow_path);
2168
2169 // 0x80000000/-1 triggers an arithmetic exception!
2170 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2171 // it's safe to just use negl instead of more complex comparisons.
2172
2173 __ cmpl(second_reg, Immediate(-1));
2174 __ j(kEqual, slow_path->GetEntryLabel());
2175
2176 // edx:eax <- sign-extended of eax
2177 __ cdq();
2178 // eax = quotient, edx = remainder
2179 __ idivl(second_reg);
2180
2181 __ Bind(slow_path->GetExitLabel());
2182 break;
2183 }
2184
2185 case Primitive::kPrimLong: {
2186 InvokeRuntimeCallingConvention calling_convention;
2187 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2188 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2189 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2190 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2191 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2192 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2193
2194 if (is_div) {
2195 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2196 } else {
2197 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2198 }
2199 uint32_t dex_pc = is_div
2200 ? instruction->AsDiv()->GetDexPc()
2201 : instruction->AsRem()->GetDexPc();
2202 codegen_->RecordPcInfo(instruction, dex_pc);
2203
2204 break;
2205 }
2206
2207 default:
2208 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2209 }
2210}
2211
Calin Juravle7c4954d2014-10-28 16:57:40 +00002212void LocationsBuilderX86::VisitDiv(HDiv* div) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002213 LocationSummary::CallKind call_kind = div->GetResultType() == Primitive::kPrimLong
2214 ? LocationSummary::kCall
2215 : LocationSummary::kNoCall;
2216 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2217
Calin Juravle7c4954d2014-10-28 16:57:40 +00002218 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002219 case Primitive::kPrimInt: {
2220 locations->SetInAt(0, Location::RegisterLocation(EAX));
2221 locations->SetInAt(1, Location::RequiresRegister());
2222 locations->SetOut(Location::SameAsFirstInput());
2223 // Intel uses edx:eax as the dividend.
2224 locations->AddTemp(Location::RegisterLocation(EDX));
2225 break;
2226 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002227 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002228 InvokeRuntimeCallingConvention calling_convention;
2229 locations->SetInAt(0, Location::RegisterPairLocation(
2230 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2231 locations->SetInAt(1, Location::RegisterPairLocation(
2232 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2233 // Runtime helper puts the result in EAX, EDX.
2234 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002235 break;
2236 }
2237 case Primitive::kPrimFloat:
2238 case Primitive::kPrimDouble: {
2239 locations->SetInAt(0, Location::RequiresFpuRegister());
2240 locations->SetInAt(1, Location::RequiresFpuRegister());
2241 locations->SetOut(Location::SameAsFirstInput());
2242 break;
2243 }
2244
2245 default:
2246 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2247 }
2248}
2249
2250void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2251 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002252 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002253 Location first = locations->InAt(0);
2254 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002255
2256 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002257 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002258 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002259 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002260 break;
2261 }
2262
2263 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002264 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002265 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002266 break;
2267 }
2268
2269 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002270 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002271 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002272 break;
2273 }
2274
2275 default:
2276 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2277 }
2278}
2279
Calin Juravlebacfec32014-11-14 15:54:36 +00002280void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002281 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002282 LocationSummary* locations =
2283 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravlebacfec32014-11-14 15:54:36 +00002284
Calin Juravled2ec87d2014-12-08 14:24:46 +00002285 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002286 case Primitive::kPrimInt: {
2287 locations->SetInAt(0, Location::RegisterLocation(EAX));
2288 locations->SetInAt(1, Location::RequiresRegister());
2289 locations->SetOut(Location::RegisterLocation(EDX));
2290 break;
2291 }
2292 case Primitive::kPrimLong: {
2293 InvokeRuntimeCallingConvention calling_convention;
2294 locations->SetInAt(0, Location::RegisterPairLocation(
2295 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2296 locations->SetInAt(1, Location::RegisterPairLocation(
2297 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2298 // Runtime helper puts the result in EAX, EDX.
2299 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2300 break;
2301 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002302 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002303 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002304 locations->SetInAt(0, Location::Any());
2305 locations->SetInAt(1, Location::Any());
2306 locations->SetOut(Location::RequiresFpuRegister());
2307 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002308 break;
2309 }
2310
2311 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002312 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002313 }
2314}
2315
2316void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2317 Primitive::Type type = rem->GetResultType();
2318 switch (type) {
2319 case Primitive::kPrimInt:
2320 case Primitive::kPrimLong: {
2321 GenerateDivRemIntegral(rem);
2322 break;
2323 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002324 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002325 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002326 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002327 break;
2328 }
2329 default:
2330 LOG(FATAL) << "Unexpected rem type " << type;
2331 }
2332}
2333
Calin Juravled0d48522014-11-04 16:40:20 +00002334void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2335 LocationSummary* locations =
2336 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002337 switch (instruction->GetType()) {
2338 case Primitive::kPrimInt: {
2339 locations->SetInAt(0, Location::Any());
2340 break;
2341 }
2342 case Primitive::kPrimLong: {
2343 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2344 if (!instruction->IsConstant()) {
2345 locations->AddTemp(Location::RequiresRegister());
2346 }
2347 break;
2348 }
2349 default:
2350 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2351 }
Calin Juravled0d48522014-11-04 16:40:20 +00002352 if (instruction->HasUses()) {
2353 locations->SetOut(Location::SameAsFirstInput());
2354 }
2355}
2356
2357void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2358 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2359 codegen_->AddSlowPath(slow_path);
2360
2361 LocationSummary* locations = instruction->GetLocations();
2362 Location value = locations->InAt(0);
2363
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002364 switch (instruction->GetType()) {
2365 case Primitive::kPrimInt: {
2366 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002367 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002368 __ j(kEqual, slow_path->GetEntryLabel());
2369 } else if (value.IsStackSlot()) {
2370 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2371 __ j(kEqual, slow_path->GetEntryLabel());
2372 } else {
2373 DCHECK(value.IsConstant()) << value;
2374 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2375 __ jmp(slow_path->GetEntryLabel());
2376 }
2377 }
2378 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002379 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002380 case Primitive::kPrimLong: {
2381 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002382 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002383 __ movl(temp, value.AsRegisterPairLow<Register>());
2384 __ orl(temp, value.AsRegisterPairHigh<Register>());
2385 __ j(kEqual, slow_path->GetEntryLabel());
2386 } else {
2387 DCHECK(value.IsConstant()) << value;
2388 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2389 __ jmp(slow_path->GetEntryLabel());
2390 }
2391 }
2392 break;
2393 }
2394 default:
2395 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002396 }
Calin Juravled0d48522014-11-04 16:40:20 +00002397}
2398
Calin Juravle9aec02f2014-11-18 23:06:35 +00002399void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2400 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2401
2402 LocationSummary* locations =
2403 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2404
2405 switch (op->GetResultType()) {
2406 case Primitive::kPrimInt: {
2407 locations->SetInAt(0, Location::RequiresRegister());
2408 // The shift count needs to be in CL.
2409 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2410 locations->SetOut(Location::SameAsFirstInput());
2411 break;
2412 }
2413 case Primitive::kPrimLong: {
2414 locations->SetInAt(0, Location::RequiresRegister());
2415 // The shift count needs to be in CL.
2416 locations->SetInAt(1, Location::RegisterLocation(ECX));
2417 locations->SetOut(Location::SameAsFirstInput());
2418 break;
2419 }
2420 default:
2421 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2422 }
2423}
2424
2425void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2426 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2427
2428 LocationSummary* locations = op->GetLocations();
2429 Location first = locations->InAt(0);
2430 Location second = locations->InAt(1);
2431 DCHECK(first.Equals(locations->Out()));
2432
2433 switch (op->GetResultType()) {
2434 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002435 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002436 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002437 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002438 DCHECK_EQ(ECX, second_reg);
2439 if (op->IsShl()) {
2440 __ shll(first_reg, second_reg);
2441 } else if (op->IsShr()) {
2442 __ sarl(first_reg, second_reg);
2443 } else {
2444 __ shrl(first_reg, second_reg);
2445 }
2446 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002447 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002448 if (op->IsShl()) {
2449 __ shll(first_reg, imm);
2450 } else if (op->IsShr()) {
2451 __ sarl(first_reg, imm);
2452 } else {
2453 __ shrl(first_reg, imm);
2454 }
2455 }
2456 break;
2457 }
2458 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002459 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002460 DCHECK_EQ(ECX, second_reg);
2461 if (op->IsShl()) {
2462 GenerateShlLong(first, second_reg);
2463 } else if (op->IsShr()) {
2464 GenerateShrLong(first, second_reg);
2465 } else {
2466 GenerateUShrLong(first, second_reg);
2467 }
2468 break;
2469 }
2470 default:
2471 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2472 }
2473}
2474
2475void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2476 Label done;
2477 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2478 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2479 __ testl(shifter, Immediate(32));
2480 __ j(kEqual, &done);
2481 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2482 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2483 __ Bind(&done);
2484}
2485
2486void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2487 Label done;
2488 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2489 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2490 __ testl(shifter, Immediate(32));
2491 __ j(kEqual, &done);
2492 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2493 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2494 __ Bind(&done);
2495}
2496
2497void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2498 Label done;
2499 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2500 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2501 __ testl(shifter, Immediate(32));
2502 __ j(kEqual, &done);
2503 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2504 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2505 __ Bind(&done);
2506}
2507
2508void LocationsBuilderX86::VisitShl(HShl* shl) {
2509 HandleShift(shl);
2510}
2511
2512void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2513 HandleShift(shl);
2514}
2515
2516void LocationsBuilderX86::VisitShr(HShr* shr) {
2517 HandleShift(shr);
2518}
2519
2520void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2521 HandleShift(shr);
2522}
2523
2524void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2525 HandleShift(ushr);
2526}
2527
2528void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2529 HandleShift(ushr);
2530}
2531
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002532void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002533 LocationSummary* locations =
2534 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002535 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002536 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002537 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2538 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002539}
2540
2541void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2542 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002543 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002544 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002545
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002546 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002547
Nicolas Geoffray39468442014-09-02 15:17:15 +01002548 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002549 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002550}
2551
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002552void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2553 LocationSummary* locations =
2554 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2555 locations->SetOut(Location::RegisterLocation(EAX));
2556 InvokeRuntimeCallingConvention calling_convention;
2557 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002558 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2559 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002560}
2561
2562void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2563 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002564 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002565 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2566
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002567 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002568
2569 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2570 DCHECK(!codegen_->IsLeafMethod());
2571}
2572
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002573void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002574 LocationSummary* locations =
2575 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002576 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2577 if (location.IsStackSlot()) {
2578 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2579 } else if (location.IsDoubleStackSlot()) {
2580 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002581 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002582 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002583}
2584
2585void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002586 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002587}
2588
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002589void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002590 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002591 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002592 locations->SetInAt(0, Location::RequiresRegister());
2593 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002594}
2595
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002596void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2597 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002598 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002599 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002600 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00002601 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002602 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002603 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002604 break;
2605
2606 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002607 __ notl(out.AsRegisterPairLow<Register>());
2608 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002609 break;
2610
2611 default:
2612 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2613 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002614}
2615
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002616void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002617 LocationSummary* locations =
2618 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002619 switch (compare->InputAt(0)->GetType()) {
2620 case Primitive::kPrimLong: {
2621 locations->SetInAt(0, Location::RequiresRegister());
2622 // TODO: we set any here but we don't handle constants
2623 locations->SetInAt(1, Location::Any());
2624 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2625 break;
2626 }
2627 case Primitive::kPrimFloat:
2628 case Primitive::kPrimDouble: {
2629 locations->SetInAt(0, Location::RequiresFpuRegister());
2630 locations->SetInAt(1, Location::RequiresFpuRegister());
2631 locations->SetOut(Location::RequiresRegister());
2632 break;
2633 }
2634 default:
2635 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2636 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002637}
2638
2639void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002640 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002641 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002642 Location left = locations->InAt(0);
2643 Location right = locations->InAt(1);
2644
2645 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002646 switch (compare->InputAt(0)->GetType()) {
2647 case Primitive::kPrimLong: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002648 if (right.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002649 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002650 } else {
2651 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002652 __ cmpl(left.AsRegisterPairHigh<Register>(),
2653 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002654 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002655 __ j(kLess, &less); // Signed compare.
2656 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002657 if (right.IsRegisterPair()) {
2658 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002659 } else {
2660 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002661 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002662 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002663 break;
2664 }
2665 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002666 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002667 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
2668 break;
2669 }
2670 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002671 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002672 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002673 break;
2674 }
2675 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002676 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002677 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002678 __ movl(out, Immediate(0));
2679 __ j(kEqual, &done);
2680 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
2681
2682 __ Bind(&greater);
2683 __ movl(out, Immediate(1));
2684 __ jmp(&done);
2685
2686 __ Bind(&less);
2687 __ movl(out, Immediate(-1));
2688
2689 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002690}
2691
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002692void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002693 LocationSummary* locations =
2694 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002695 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2696 locations->SetInAt(i, Location::Any());
2697 }
2698 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002699}
2700
2701void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002702 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002703 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002704}
2705
Calin Juravle52c48962014-12-16 17:02:57 +00002706void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
2707 /*
2708 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2709 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2710 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2711 */
2712 switch (kind) {
2713 case MemBarrierKind::kAnyAny: {
2714 __ mfence();
2715 break;
2716 }
2717 case MemBarrierKind::kAnyStore:
2718 case MemBarrierKind::kLoadAny:
2719 case MemBarrierKind::kStoreStore: {
2720 // nop
2721 break;
2722 }
2723 default:
2724 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002725 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002726}
2727
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002728
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002729void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002730 Label is_null;
2731 __ testl(value, value);
2732 __ j(kEqual, &is_null);
2733 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2734 __ movl(temp, object);
2735 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002736 __ movb(Address(temp, card, TIMES_1, 0),
2737 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002738 __ Bind(&is_null);
2739}
2740
Calin Juravle52c48962014-12-16 17:02:57 +00002741void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
2742 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002743 LocationSummary* locations =
2744 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002745 locations->SetInAt(0, Location::RequiresRegister());
2746 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle52c48962014-12-16 17:02:57 +00002747
2748 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
2749 // Long values can be loaded atomically into an XMM using movsd.
2750 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
2751 // and then copy the XMM into the output 32bits at a time).
2752 locations->AddTemp(Location::RequiresFpuRegister());
2753 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002754}
2755
Calin Juravle52c48962014-12-16 17:02:57 +00002756void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
2757 const FieldInfo& field_info) {
2758 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002759
Calin Juravle52c48962014-12-16 17:02:57 +00002760 LocationSummary* locations = instruction->GetLocations();
2761 Register base = locations->InAt(0).AsRegister<Register>();
2762 Location out = locations->Out();
2763 bool is_volatile = field_info.IsVolatile();
2764 Primitive::Type field_type = field_info.GetFieldType();
2765 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2766
2767 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002768 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00002769 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002770 break;
2771 }
2772
2773 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002774 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002775 break;
2776 }
2777
2778 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00002779 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002780 break;
2781 }
2782
2783 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002784 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002785 break;
2786 }
2787
2788 case Primitive::kPrimInt:
2789 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002790 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002791 break;
2792 }
2793
2794 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00002795 if (is_volatile) {
2796 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2797 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002798 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002799 __ movd(out.AsRegisterPairLow<Register>(), temp);
2800 __ psrlq(temp, Immediate(32));
2801 __ movd(out.AsRegisterPairHigh<Register>(), temp);
2802 } else {
2803 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002804 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002805 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
2806 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002807 break;
2808 }
2809
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002810 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002811 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002812 break;
2813 }
2814
2815 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002816 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002817 break;
2818 }
2819
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002820 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00002821 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002822 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002823 }
Calin Juravle52c48962014-12-16 17:02:57 +00002824
Calin Juravle77520bc2015-01-12 18:45:46 +00002825 // Longs are handled in the switch.
2826 if (field_type != Primitive::kPrimLong) {
2827 codegen_->MaybeRecordImplicitNullCheck(instruction);
2828 }
2829
Calin Juravle52c48962014-12-16 17:02:57 +00002830 if (is_volatile) {
2831 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2832 }
2833}
2834
2835void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
2836 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2837
2838 LocationSummary* locations =
2839 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2840 locations->SetInAt(0, Location::RequiresRegister());
2841 bool is_volatile = field_info.IsVolatile();
2842 Primitive::Type field_type = field_info.GetFieldType();
2843 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2844 || (field_type == Primitive::kPrimByte);
2845
2846 // The register allocator does not support multiple
2847 // inputs that die at entry with one in a specific register.
2848 if (is_byte_type) {
2849 // Ensure the value is in a byte register.
2850 locations->SetInAt(1, Location::RegisterLocation(EAX));
2851 } else {
2852 locations->SetInAt(1, Location::RequiresRegister());
2853 }
2854 // Temporary registers for the write barrier.
2855 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2856 locations->AddTemp(Location::RequiresRegister());
2857 // Ensure the card is in a byte register.
2858 locations->AddTemp(Location::RegisterLocation(ECX));
2859 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
2860 // 64bits value can be atomically written to an address with movsd and an XMM register.
2861 // We need two XMM registers because there's no easier way to (bit) copy a register pair
2862 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
2863 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
2864 // isolated cases when we need this it isn't worth adding the extra complexity.
2865 locations->AddTemp(Location::RequiresFpuRegister());
2866 locations->AddTemp(Location::RequiresFpuRegister());
2867 }
2868}
2869
2870void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
2871 const FieldInfo& field_info) {
2872 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2873
2874 LocationSummary* locations = instruction->GetLocations();
2875 Register base = locations->InAt(0).AsRegister<Register>();
2876 Location value = locations->InAt(1);
2877 bool is_volatile = field_info.IsVolatile();
2878 Primitive::Type field_type = field_info.GetFieldType();
2879 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2880
2881 if (is_volatile) {
2882 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2883 }
2884
2885 switch (field_type) {
2886 case Primitive::kPrimBoolean:
2887 case Primitive::kPrimByte: {
2888 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
2889 break;
2890 }
2891
2892 case Primitive::kPrimShort:
2893 case Primitive::kPrimChar: {
2894 __ movw(Address(base, offset), value.AsRegister<Register>());
2895 break;
2896 }
2897
2898 case Primitive::kPrimInt:
2899 case Primitive::kPrimNot: {
2900 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00002901 break;
2902 }
2903
2904 case Primitive::kPrimLong: {
2905 if (is_volatile) {
2906 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2907 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
2908 __ movd(temp1, value.AsRegisterPairLow<Register>());
2909 __ movd(temp2, value.AsRegisterPairHigh<Register>());
2910 __ punpckldq(temp1, temp2);
2911 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00002912 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002913 } else {
2914 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00002915 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002916 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
2917 }
2918 break;
2919 }
2920
2921 case Primitive::kPrimFloat: {
2922 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
2923 break;
2924 }
2925
2926 case Primitive::kPrimDouble: {
2927 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
2928 break;
2929 }
2930
2931 case Primitive::kPrimVoid:
2932 LOG(FATAL) << "Unreachable type " << field_type;
2933 UNREACHABLE();
2934 }
2935
Calin Juravle77520bc2015-01-12 18:45:46 +00002936 // Longs are handled in the switch.
2937 if (field_type != Primitive::kPrimLong) {
2938 codegen_->MaybeRecordImplicitNullCheck(instruction);
2939 }
2940
2941 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2942 Register temp = locations->GetTemp(0).AsRegister<Register>();
2943 Register card = locations->GetTemp(1).AsRegister<Register>();
2944 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
2945 }
2946
Calin Juravle52c48962014-12-16 17:02:57 +00002947 if (is_volatile) {
2948 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2949 }
2950}
2951
2952void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2953 HandleFieldGet(instruction, instruction->GetFieldInfo());
2954}
2955
2956void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2957 HandleFieldGet(instruction, instruction->GetFieldInfo());
2958}
2959
2960void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2961 HandleFieldSet(instruction, instruction->GetFieldInfo());
2962}
2963
2964void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2965 HandleFieldSet(instruction, instruction->GetFieldInfo());
2966}
2967
2968void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2969 HandleFieldSet(instruction, instruction->GetFieldInfo());
2970}
2971
2972void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2973 HandleFieldSet(instruction, instruction->GetFieldInfo());
2974}
2975
2976void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2977 HandleFieldGet(instruction, instruction->GetFieldInfo());
2978}
2979
2980void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2981 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002982}
2983
2984void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002985 LocationSummary* locations =
2986 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002987 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
2988 ? Location::RequiresRegister()
2989 : Location::Any();
2990 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002991 if (instruction->HasUses()) {
2992 locations->SetOut(Location::SameAsFirstInput());
2993 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002994}
2995
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002996void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002997 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2998 return;
2999 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003000 LocationSummary* locations = instruction->GetLocations();
3001 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003002
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003003 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3004 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3005}
3006
3007void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003008 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003009 codegen_->AddSlowPath(slow_path);
3010
3011 LocationSummary* locations = instruction->GetLocations();
3012 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003013
3014 if (obj.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003015 __ cmpl(obj.AsRegister<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003016 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003017 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003018 } else {
3019 DCHECK(obj.IsConstant()) << obj;
3020 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3021 __ jmp(slow_path->GetEntryLabel());
3022 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003023 }
3024 __ j(kEqual, slow_path->GetEntryLabel());
3025}
3026
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003027void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3028 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3029 GenerateImplicitNullCheck(instruction);
3030 } else {
3031 GenerateExplicitNullCheck(instruction);
3032 }
3033}
3034
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003035void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003036 LocationSummary* locations =
3037 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003038 locations->SetInAt(0, Location::RequiresRegister());
3039 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3040 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003041}
3042
3043void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3044 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003045 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003046 Location index = locations->InAt(1);
3047
Calin Juravle77520bc2015-01-12 18:45:46 +00003048 Primitive::Type type = instruction->GetType();
3049 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003050 case Primitive::kPrimBoolean: {
3051 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_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 __ movzxb(out, Address(obj,
3055 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3056 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003057 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003058 }
3059 break;
3060 }
3061
3062 case Primitive::kPrimByte: {
3063 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_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 __ movsxb(out, Address(obj,
3067 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3068 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003069 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003070 }
3071 break;
3072 }
3073
3074 case Primitive::kPrimShort: {
3075 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003076 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003077 if (index.IsConstant()) {
3078 __ movsxw(out, Address(obj,
3079 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3080 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003081 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003082 }
3083 break;
3084 }
3085
3086 case Primitive::kPrimChar: {
3087 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003088 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003089 if (index.IsConstant()) {
3090 __ movzxw(out, Address(obj,
3091 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3092 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003093 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003094 }
3095 break;
3096 }
3097
3098 case Primitive::kPrimInt:
3099 case Primitive::kPrimNot: {
3100 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003101 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003102 if (index.IsConstant()) {
3103 __ movl(out, Address(obj,
3104 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3105 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003106 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003107 }
3108 break;
3109 }
3110
3111 case Primitive::kPrimLong: {
3112 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003113 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003114 if (index.IsConstant()) {
3115 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003116 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003117 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003118 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003119 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003120 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003121 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003122 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003123 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003124 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003125 }
3126 break;
3127 }
3128
Mark Mendell7c8d0092015-01-26 11:21:33 -05003129 case Primitive::kPrimFloat: {
3130 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3131 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3132 if (index.IsConstant()) {
3133 __ movss(out, Address(obj,
3134 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3135 } else {
3136 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3137 }
3138 break;
3139 }
3140
3141 case Primitive::kPrimDouble: {
3142 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3143 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3144 if (index.IsConstant()) {
3145 __ movsd(out, Address(obj,
3146 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3147 } else {
3148 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3149 }
3150 break;
3151 }
3152
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003153 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003154 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003155 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003156 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003157
3158 if (type != Primitive::kPrimLong) {
3159 codegen_->MaybeRecordImplicitNullCheck(instruction);
3160 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003161}
3162
3163void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003164 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003165 bool needs_write_barrier =
3166 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3167
3168 DCHECK(kFollowsQuickABI);
3169 bool not_enough_registers = needs_write_barrier
3170 && !instruction->GetValue()->IsConstant()
3171 && !instruction->GetIndex()->IsConstant();
3172 bool needs_runtime_call = instruction->NeedsTypeCheck() || not_enough_registers;
3173
Nicolas Geoffray39468442014-09-02 15:17:15 +01003174 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3175 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003176 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003177
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003178 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003179 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003180 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3181 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3182 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003183 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003184 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3185 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003186 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003187 // In case of a byte operation, the register allocator does not support multiple
3188 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003189 locations->SetInAt(0, Location::RequiresRegister());
3190 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003191 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003192 // Ensure the value is in a byte register.
3193 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003194 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003195 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003196 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003197 // Temporary registers for the write barrier.
3198 if (needs_write_barrier) {
3199 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003200 // Ensure the card is in a byte register.
3201 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003202 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003203 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003204}
3205
3206void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3207 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003208 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003209 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003210 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003211 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003212 bool needs_runtime_call = locations->WillCall();
3213 bool needs_write_barrier =
3214 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003215
3216 switch (value_type) {
3217 case Primitive::kPrimBoolean:
3218 case Primitive::kPrimByte: {
3219 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003220 if (index.IsConstant()) {
3221 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003222 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003223 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003224 } else {
3225 __ movb(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 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003231 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003232 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003233 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, 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
3241 case Primitive::kPrimShort:
3242 case Primitive::kPrimChar: {
3243 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003244 if (index.IsConstant()) {
3245 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003246 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003247 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003248 } else {
3249 __ movw(Address(obj, offset),
3250 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3251 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003252 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003253 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003254 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3255 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003256 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003257 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003258 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3259 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003260 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003261 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003262 break;
3263 }
3264
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003265 case Primitive::kPrimInt:
3266 case Primitive::kPrimNot: {
3267 if (!needs_runtime_call) {
3268 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3269 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003270 size_t offset =
3271 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003272 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003273 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003274 } else {
3275 DCHECK(value.IsConstant()) << value;
3276 __ movl(Address(obj, offset),
3277 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3278 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003279 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003280 DCHECK(index.IsRegister()) << index;
3281 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003282 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3283 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003284 } else {
3285 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003286 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003287 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3288 }
3289 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003290 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003291
3292 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003293 Register temp = locations->GetTemp(0).AsRegister<Register>();
3294 Register card = locations->GetTemp(1).AsRegister<Register>();
3295 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003296 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003297 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003298 DCHECK_EQ(value_type, Primitive::kPrimNot);
3299 DCHECK(!codegen_->IsLeafMethod());
3300 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3301 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003302 }
3303 break;
3304 }
3305
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003306 case Primitive::kPrimLong: {
3307 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003308 if (index.IsConstant()) {
3309 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003310 if (value.IsRegisterPair()) {
3311 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003312 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003313 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003314 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003315 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003316 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3317 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003318 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003319 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3320 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003321 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003322 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003323 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003324 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003325 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003326 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003327 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003328 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003329 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003330 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003331 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003332 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003333 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003334 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003335 Immediate(High32Bits(val)));
3336 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003337 }
3338 break;
3339 }
3340
Mark Mendell7c8d0092015-01-26 11:21:33 -05003341 case Primitive::kPrimFloat: {
3342 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3343 DCHECK(value.IsFpuRegister());
3344 if (index.IsConstant()) {
3345 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3346 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3347 } else {
3348 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3349 value.AsFpuRegister<XmmRegister>());
3350 }
3351 break;
3352 }
3353
3354 case Primitive::kPrimDouble: {
3355 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3356 DCHECK(value.IsFpuRegister());
3357 if (index.IsConstant()) {
3358 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3359 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3360 } else {
3361 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3362 value.AsFpuRegister<XmmRegister>());
3363 }
3364 break;
3365 }
3366
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003367 case Primitive::kPrimVoid:
3368 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003369 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003370 }
3371}
3372
3373void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3374 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003375 locations->SetInAt(0, Location::RequiresRegister());
3376 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003377 instruction->SetLocations(locations);
3378}
3379
3380void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3381 LocationSummary* locations = instruction->GetLocations();
3382 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003383 Register obj = locations->InAt(0).AsRegister<Register>();
3384 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003385 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003386 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003387}
3388
3389void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003390 LocationSummary* locations =
3391 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Mark Mendellf60c90b2015-03-04 15:12:59 -05003392 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003393 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003394 if (instruction->HasUses()) {
3395 locations->SetOut(Location::SameAsFirstInput());
3396 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003397}
3398
3399void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3400 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05003401 Location index_loc = locations->InAt(0);
3402 Location length_loc = locations->InAt(1);
3403 SlowPathCodeX86* slow_path =
3404 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction, index_loc, length_loc);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003405 codegen_->AddSlowPath(slow_path);
3406
Mark Mendellf60c90b2015-03-04 15:12:59 -05003407 Register length = length_loc.AsRegister<Register>();
3408 if (index_loc.IsConstant()) {
3409 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
3410 __ cmpl(length, Immediate(value));
3411 } else {
3412 __ cmpl(length, index_loc.AsRegister<Register>());
3413 }
3414 __ j(kBelowEqual, slow_path->GetEntryLabel());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003415}
3416
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003417void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3418 temp->SetLocations(nullptr);
3419}
3420
3421void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3422 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003423 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003424}
3425
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003426void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003427 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003428 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003429}
3430
3431void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003432 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3433}
3434
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003435void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3436 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3437}
3438
3439void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003440 HBasicBlock* block = instruction->GetBlock();
3441 if (block->GetLoopInformation() != nullptr) {
3442 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3443 // The back edge will generate the suspend check.
3444 return;
3445 }
3446 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3447 // The goto will generate the suspend check.
3448 return;
3449 }
3450 GenerateSuspendCheck(instruction, nullptr);
3451}
3452
3453void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3454 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003455 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003456 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003457 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003458 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003459 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003460 if (successor == nullptr) {
3461 __ j(kNotEqual, slow_path->GetEntryLabel());
3462 __ Bind(slow_path->GetReturnLabel());
3463 } else {
3464 __ j(kEqual, codegen_->GetLabelOf(successor));
3465 __ jmp(slow_path->GetEntryLabel());
3466 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003467}
3468
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003469X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3470 return codegen_->GetAssembler();
3471}
3472
Mark Mendell7c8d0092015-01-26 11:21:33 -05003473void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003474 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003475 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003476 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003477 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05003478 __ movl(temp_reg, Address(ESP, src + stack_offset));
3479 __ movl(Address(ESP, dst + stack_offset), temp_reg);
3480}
3481
3482void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
3483 ScratchRegisterScope ensure_scratch(
3484 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3485 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3486 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3487 __ movl(temp_reg, Address(ESP, src + stack_offset));
3488 __ movl(Address(ESP, dst + stack_offset), temp_reg);
3489 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
3490 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003491}
3492
3493void ParallelMoveResolverX86::EmitMove(size_t index) {
3494 MoveOperands* move = moves_.Get(index);
3495 Location source = move->GetSource();
3496 Location destination = move->GetDestination();
3497
3498 if (source.IsRegister()) {
3499 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003500 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003501 } else {
3502 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003503 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003504 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003505 } else if (source.IsFpuRegister()) {
3506 if (destination.IsFpuRegister()) {
3507 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3508 } else if (destination.IsStackSlot()) {
3509 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3510 } else {
3511 DCHECK(destination.IsDoubleStackSlot());
3512 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3513 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003514 } else if (source.IsStackSlot()) {
3515 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003516 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003517 } else if (destination.IsFpuRegister()) {
3518 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003519 } else {
3520 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003521 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
3522 }
3523 } else if (source.IsDoubleStackSlot()) {
3524 if (destination.IsFpuRegister()) {
3525 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
3526 } else {
3527 DCHECK(destination.IsDoubleStackSlot()) << destination;
3528 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003529 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003530 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003531 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003532 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003533 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05003534 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003535 if (value == 0) {
3536 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
3537 } else {
3538 __ movl(destination.AsRegister<Register>(), Immediate(value));
3539 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003540 } else {
3541 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05003542 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003543 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003544 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003545 DCHECK(constant->IsFloatConstant());
3546 float value = constant->AsFloatConstant()->GetValue();
3547 Immediate imm(bit_cast<float, int32_t>(value));
3548 if (destination.IsFpuRegister()) {
3549 ScratchRegisterScope ensure_scratch(
3550 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3551 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
3552 __ movl(temp, imm);
3553 __ movd(destination.AsFpuRegister<XmmRegister>(), temp);
3554 } else {
3555 DCHECK(destination.IsStackSlot()) << destination;
3556 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3557 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003558 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003559 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003560 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003561 }
3562}
3563
3564void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003565 Register suggested_scratch = reg == EAX ? EBX : EAX;
3566 ScratchRegisterScope ensure_scratch(
3567 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3568
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003569 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3570 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
3571 __ movl(Address(ESP, mem + stack_offset), reg);
3572 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
3573}
3574
Mark Mendell7c8d0092015-01-26 11:21:33 -05003575void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
3576 ScratchRegisterScope ensure_scratch(
3577 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3578
3579 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3580 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3581 __ movl(temp_reg, Address(ESP, mem + stack_offset));
3582 __ movss(Address(ESP, mem + stack_offset), reg);
3583 __ movd(reg, temp_reg);
3584}
3585
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003586void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
3587 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003588 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3589
3590 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003591 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003592 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3593
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003594 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
3595 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
3596 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
3597 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
3598 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
3599 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
3600}
3601
3602void ParallelMoveResolverX86::EmitSwap(size_t index) {
3603 MoveOperands* move = moves_.Get(index);
3604 Location source = move->GetSource();
3605 Location destination = move->GetDestination();
3606
3607 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003608 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003609 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003610 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003611 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003612 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003613 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3614 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003615 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
3616 // Use XOR Swap algorithm to avoid a temporary.
3617 DCHECK_NE(source.reg(), destination.reg());
3618 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3619 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3620 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3621 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
3622 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
3623 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
3624 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003625 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003626 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003627 }
3628}
3629
3630void ParallelMoveResolverX86::SpillScratch(int reg) {
3631 __ pushl(static_cast<Register>(reg));
3632}
3633
3634void ParallelMoveResolverX86::RestoreScratch(int reg) {
3635 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003636}
3637
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003638void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003639 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3640 ? LocationSummary::kCallOnSlowPath
3641 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003642 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003643 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003644 locations->SetOut(Location::RequiresRegister());
3645}
3646
3647void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003648 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003649 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003650 DCHECK(!cls->CanCallRuntime());
3651 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003652 codegen_->LoadCurrentMethod(out);
3653 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3654 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003655 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003656 codegen_->LoadCurrentMethod(out);
3657 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3658 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003659
3660 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3661 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3662 codegen_->AddSlowPath(slow_path);
3663 __ testl(out, out);
3664 __ j(kEqual, slow_path->GetEntryLabel());
3665 if (cls->MustGenerateClinitCheck()) {
3666 GenerateClassInitializationCheck(slow_path, out);
3667 } else {
3668 __ Bind(slow_path->GetExitLabel());
3669 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003670 }
3671}
3672
3673void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
3674 LocationSummary* locations =
3675 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3676 locations->SetInAt(0, Location::RequiresRegister());
3677 if (check->HasUses()) {
3678 locations->SetOut(Location::SameAsFirstInput());
3679 }
3680}
3681
3682void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003683 // We assume the class to not be null.
3684 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3685 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003686 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003687 GenerateClassInitializationCheck(slow_path,
3688 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003689}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003690
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003691void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
3692 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003693 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3694 Immediate(mirror::Class::kStatusInitialized));
3695 __ j(kLess, slow_path->GetEntryLabel());
3696 __ Bind(slow_path->GetExitLabel());
3697 // No need for memory fence, thanks to the X86 memory model.
3698}
3699
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003700void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
3701 LocationSummary* locations =
3702 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3703 locations->SetOut(Location::RequiresRegister());
3704}
3705
3706void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
3707 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
3708 codegen_->AddSlowPath(slow_path);
3709
Roland Levillain271ab9c2014-11-27 15:23:57 +00003710 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003711 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08003712 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3713 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003714 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3715 __ testl(out, out);
3716 __ j(kEqual, slow_path->GetEntryLabel());
3717 __ Bind(slow_path->GetExitLabel());
3718}
3719
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003720void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
3721 LocationSummary* locations =
3722 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3723 locations->SetOut(Location::RequiresRegister());
3724}
3725
3726void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
3727 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003728 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003729 __ fs()->movl(address, Immediate(0));
3730}
3731
3732void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
3733 LocationSummary* locations =
3734 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3735 InvokeRuntimeCallingConvention calling_convention;
3736 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3737}
3738
3739void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
3740 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
3741 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3742}
3743
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003744void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003745 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3746 ? LocationSummary::kNoCall
3747 : LocationSummary::kCallOnSlowPath;
3748 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3749 locations->SetInAt(0, Location::RequiresRegister());
3750 locations->SetInAt(1, Location::Any());
3751 locations->SetOut(Location::RequiresRegister());
3752}
3753
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003754void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003755 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003756 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003757 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003758 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003759 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3760 Label done, zero;
3761 SlowPathCodeX86* slow_path = nullptr;
3762
3763 // Return 0 if `obj` is null.
3764 // TODO: avoid this check if we know obj is not null.
3765 __ testl(obj, obj);
3766 __ j(kEqual, &zero);
3767 __ movl(out, Address(obj, class_offset));
3768 // Compare the class of `obj` with `cls`.
3769 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003770 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003771 } else {
3772 DCHECK(cls.IsStackSlot()) << cls;
3773 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
3774 }
3775
3776 if (instruction->IsClassFinal()) {
3777 // Classes must be equal for the instanceof to succeed.
3778 __ j(kNotEqual, &zero);
3779 __ movl(out, Immediate(1));
3780 __ jmp(&done);
3781 } else {
3782 // If the classes are not equal, we go into a slow path.
3783 DCHECK(locations->OnlyCallsOnSlowPath());
3784 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003785 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003786 codegen_->AddSlowPath(slow_path);
3787 __ j(kNotEqual, slow_path->GetEntryLabel());
3788 __ movl(out, Immediate(1));
3789 __ jmp(&done);
3790 }
3791 __ Bind(&zero);
3792 __ movl(out, Immediate(0));
3793 if (slow_path != nullptr) {
3794 __ Bind(slow_path->GetExitLabel());
3795 }
3796 __ Bind(&done);
3797}
3798
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003799void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
3800 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3801 instruction, LocationSummary::kCallOnSlowPath);
3802 locations->SetInAt(0, Location::RequiresRegister());
3803 locations->SetInAt(1, Location::Any());
3804 locations->AddTemp(Location::RequiresRegister());
3805}
3806
3807void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
3808 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003809 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003810 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003811 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003812 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3813 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
3814 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3815 codegen_->AddSlowPath(slow_path);
3816
3817 // TODO: avoid this check if we know obj is not null.
3818 __ testl(obj, obj);
3819 __ j(kEqual, slow_path->GetExitLabel());
3820 __ movl(temp, Address(obj, class_offset));
3821
3822 // Compare the class of `obj` with `cls`.
3823 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003824 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003825 } else {
3826 DCHECK(cls.IsStackSlot()) << cls;
3827 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
3828 }
3829
3830 __ j(kNotEqual, slow_path->GetEntryLabel());
3831 __ Bind(slow_path->GetExitLabel());
3832}
3833
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003834void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3835 LocationSummary* locations =
3836 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3837 InvokeRuntimeCallingConvention calling_convention;
3838 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3839}
3840
3841void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3842 __ fs()->call(Address::Absolute(instruction->IsEnter()
3843 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
3844 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
3845 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3846}
3847
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003848void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3849void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3850void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3851
3852void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3853 LocationSummary* locations =
3854 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3855 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3856 || instruction->GetResultType() == Primitive::kPrimLong);
3857 locations->SetInAt(0, Location::RequiresRegister());
3858 locations->SetInAt(1, Location::Any());
3859 locations->SetOut(Location::SameAsFirstInput());
3860}
3861
3862void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
3863 HandleBitwiseOperation(instruction);
3864}
3865
3866void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
3867 HandleBitwiseOperation(instruction);
3868}
3869
3870void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
3871 HandleBitwiseOperation(instruction);
3872}
3873
3874void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3875 LocationSummary* locations = instruction->GetLocations();
3876 Location first = locations->InAt(0);
3877 Location second = locations->InAt(1);
3878 DCHECK(first.Equals(locations->Out()));
3879
3880 if (instruction->GetResultType() == Primitive::kPrimInt) {
3881 if (second.IsRegister()) {
3882 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003883 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003884 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003885 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003886 } else {
3887 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003888 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003889 }
3890 } else if (second.IsConstant()) {
3891 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003892 __ andl(first.AsRegister<Register>(),
3893 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003894 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003895 __ orl(first.AsRegister<Register>(),
3896 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003897 } else {
3898 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00003899 __ xorl(first.AsRegister<Register>(),
3900 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003901 }
3902 } else {
3903 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003904 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003905 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003906 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003907 } else {
3908 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003909 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003910 }
3911 }
3912 } else {
3913 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3914 if (second.IsRegisterPair()) {
3915 if (instruction->IsAnd()) {
3916 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3917 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3918 } else if (instruction->IsOr()) {
3919 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3920 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3921 } else {
3922 DCHECK(instruction->IsXor());
3923 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3924 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3925 }
3926 } else {
3927 if (instruction->IsAnd()) {
3928 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3929 __ andl(first.AsRegisterPairHigh<Register>(),
3930 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3931 } else if (instruction->IsOr()) {
3932 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3933 __ orl(first.AsRegisterPairHigh<Register>(),
3934 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3935 } else {
3936 DCHECK(instruction->IsXor());
3937 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3938 __ xorl(first.AsRegisterPairHigh<Register>(),
3939 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3940 }
3941 }
3942 }
3943}
3944
Calin Juravleb1498f62015-02-16 13:13:29 +00003945void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
3946 // Nothing to do, this should be removed during prepare for register allocator.
3947 UNUSED(instruction);
3948 LOG(FATAL) << "Unreachable";
3949}
3950
3951void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
3952 // Nothing to do, this should be removed during prepare for register allocator.
3953 UNUSED(instruction);
3954 LOG(FATAL) << "Unreachable";
3955}
3956
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003957} // namespace x86
3958} // namespace art