blob: 98f93a418a46477f0f48302a3b57ad1b981d94ad [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
19#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000020#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010021#include "gc/accounting/card_table.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070022#include "mirror/array-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010023#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010024#include "mirror/class.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010025#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000026#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010027#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000028#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010029#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010032
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033namespace x86 {
34
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010035static constexpr int kCurrentMethodStackOffset = 0;
36
Calin Juravled6fb6cf2014-11-11 19:07:44 +000037static constexpr Register kRuntimeParameterCoreRegisters[] = { EAX, ECX, EDX, EBX };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010038static constexpr size_t kRuntimeParameterCoreRegistersLength =
39 arraysize(kRuntimeParameterCoreRegisters);
Mark P Mendell966c3ae2015-01-27 15:45:27 +000040static constexpr XmmRegister kRuntimeParameterFpuRegisters[] = { XMM0, XMM1, XMM2, XMM3 };
41static constexpr size_t kRuntimeParameterFpuRegistersLength =
42 arraysize(kRuntimeParameterFpuRegisters);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010043
Mark Mendell24f2dfa2015-01-14 19:51:45 -050044static constexpr int kC2ConditionMask = 0x400;
45
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000046// Marker for places that can be updated once we don't follow the quick ABI.
47static constexpr bool kFollowsQuickABI = true;
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000048static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000049
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010050class InvokeRuntimeCallingConvention : public CallingConvention<Register, XmmRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010051 public:
52 InvokeRuntimeCallingConvention()
53 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010054 kRuntimeParameterCoreRegistersLength,
55 kRuntimeParameterFpuRegisters,
56 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010057
58 private:
59 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
60};
61
Nicolas Geoffraye5038322014-07-04 09:41:32 +010062#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
63
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010064class SlowPathCodeX86 : public SlowPathCode {
65 public:
66 SlowPathCodeX86() : entry_label_(), exit_label_() {}
67
68 Label* GetEntryLabel() { return &entry_label_; }
69 Label* GetExitLabel() { return &exit_label_; }
70
71 private:
72 Label entry_label_;
73 Label exit_label_;
74
75 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86);
76};
77
78class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010080 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010081
82 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
83 __ Bind(GetEntryLabel());
84 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010085 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010086 }
87
88 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010089 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010090 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
91};
92
Calin Juravled0d48522014-11-04 16:40:20 +000093class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
94 public:
95 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
96
97 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
98 __ Bind(GetEntryLabel());
99 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
100 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
101 }
102
103 private:
104 HDivZeroCheck* const instruction_;
105 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
106};
107
Calin Juravlebacfec32014-11-14 15:54:36 +0000108class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +0000109 public:
Calin Juravlebacfec32014-11-14 15:54:36 +0000110 explicit DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000111
112 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
113 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000114 if (is_div_) {
115 __ negl(reg_);
116 } else {
117 __ movl(reg_, Immediate(0));
118 }
Calin Juravled0d48522014-11-04 16:40:20 +0000119 __ jmp(GetExitLabel());
120 }
121
122 private:
123 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000124 bool is_div_;
125 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000126};
127
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100128class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100129 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100130 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
131 Location index_location,
132 Location length_location)
Roland Levillain199f3362014-11-27 17:15:16 +0000133 : instruction_(instruction),
134 index_location_(index_location),
135 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100136
137 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100138 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100139 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000140 // We're moving two locations to locations that could overlap, so we need a parallel
141 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100142 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000143 x86_codegen->EmitParallelMoves(
144 index_location_,
145 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
146 length_location_,
147 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100148 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100149 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100150 }
151
152 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100153 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100154 const Location index_location_;
155 const Location length_location_;
156
157 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
158};
159
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100160class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000161 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100162 explicit SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
163 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000164
165 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100166 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000167 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100168 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000169 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
170 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100171 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100172 if (successor_ == nullptr) {
173 __ jmp(GetReturnLabel());
174 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100175 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100176 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000177 }
178
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100179 Label* GetReturnLabel() {
180 DCHECK(successor_ == nullptr);
181 return &return_label_;
182 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000183
184 private:
185 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100186 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000187 Label return_label_;
188
189 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
190};
191
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000192class LoadStringSlowPathX86 : public SlowPathCodeX86 {
193 public:
194 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
195
196 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
197 LocationSummary* locations = instruction_->GetLocations();
198 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
199
200 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
201 __ Bind(GetEntryLabel());
202 codegen->SaveLiveRegisters(locations);
203
204 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800205 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
206 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000207 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
208 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
209 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
210 codegen->RestoreLiveRegisters(locations);
211
212 __ jmp(GetExitLabel());
213 }
214
215 private:
216 HLoadString* const instruction_;
217
218 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
219};
220
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000221class LoadClassSlowPathX86 : public SlowPathCodeX86 {
222 public:
223 LoadClassSlowPathX86(HLoadClass* cls,
224 HInstruction* at,
225 uint32_t dex_pc,
226 bool do_clinit)
227 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
228 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
229 }
230
231 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
232 LocationSummary* locations = at_->GetLocations();
233 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
234 __ Bind(GetEntryLabel());
235 codegen->SaveLiveRegisters(locations);
236
237 InvokeRuntimeCallingConvention calling_convention;
238 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
239 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
240 __ fs()->call(Address::Absolute(do_clinit_
241 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
242 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
243 codegen->RecordPcInfo(at_, dex_pc_);
244
245 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000246 Location out = locations->Out();
247 if (out.IsValid()) {
248 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
249 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000250 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000251
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000252 codegen->RestoreLiveRegisters(locations);
253 __ jmp(GetExitLabel());
254 }
255
256 private:
257 // The class this slow path will load.
258 HLoadClass* const cls_;
259
260 // The instruction where this slow path is happening.
261 // (Might be the load class or an initialization check).
262 HInstruction* const at_;
263
264 // The dex PC of `at_`.
265 const uint32_t dex_pc_;
266
267 // Whether to initialize the class.
268 const bool do_clinit_;
269
270 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
271};
272
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000273class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
274 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000275 TypeCheckSlowPathX86(HInstruction* instruction,
276 Location class_to_check,
277 Location object_class,
278 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000279 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000280 class_to_check_(class_to_check),
281 object_class_(object_class),
282 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000283
284 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
285 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000286 DCHECK(instruction_->IsCheckCast()
287 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000288
289 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
290 __ Bind(GetEntryLabel());
291 codegen->SaveLiveRegisters(locations);
292
293 // We're moving two locations to locations that could overlap, so we need a parallel
294 // move resolver.
295 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000296 x86_codegen->EmitParallelMoves(
297 class_to_check_,
298 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
299 object_class_,
300 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000301
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000302 if (instruction_->IsInstanceOf()) {
Roland Levillain199f3362014-11-27 17:15:16 +0000303 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize,
304 pInstanceofNonTrivial)));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000305 } else {
306 DCHECK(instruction_->IsCheckCast());
307 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
308 }
309
310 codegen->RecordPcInfo(instruction_, dex_pc_);
311 if (instruction_->IsInstanceOf()) {
312 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
313 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000314 codegen->RestoreLiveRegisters(locations);
315
316 __ jmp(GetExitLabel());
317 }
318
319 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000320 HInstruction* const instruction_;
321 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000322 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000323 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324
325 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
326};
327
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100328#undef __
329#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
330
Dave Allison20dfc792014-06-16 20:44:29 -0700331inline Condition X86Condition(IfCondition cond) {
332 switch (cond) {
333 case kCondEQ: return kEqual;
334 case kCondNE: return kNotEqual;
335 case kCondLT: return kLess;
336 case kCondLE: return kLessEqual;
337 case kCondGT: return kGreater;
338 case kCondGE: return kGreaterEqual;
339 default:
340 LOG(FATAL) << "Unknown if condition";
341 }
342 return kEqual;
343}
344
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100345void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
346 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
347}
348
349void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
350 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
351}
352
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100353size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
354 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
355 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100356}
357
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100358size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
359 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
360 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100361}
362
Mark Mendell7c8d0092015-01-26 11:21:33 -0500363size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
364 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
365 return GetFloatingPointSpillSlotSize();
366}
367
368size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
369 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
370 return GetFloatingPointSpillSlotSize();
371}
372
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000373CodeGeneratorX86::CodeGeneratorX86(HGraph* graph, const CompilerOptions& compiler_options)
374 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfXmmRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000375 kNumberOfRegisterPairs, (1 << kFakeReturnRegister), 0, compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100376 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100377 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100378 instruction_visitor_(graph, this),
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000379 move_resolver_(graph->GetArena(), this) {
380 // Use a fake return address register to mimic Quick.
381 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100382}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100383
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100384Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100385 switch (type) {
386 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100387 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100388 X86ManagedRegister pair =
389 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100390 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
391 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100392 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
393 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100394 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100395 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100396 }
397
398 case Primitive::kPrimByte:
399 case Primitive::kPrimBoolean:
400 case Primitive::kPrimChar:
401 case Primitive::kPrimShort:
402 case Primitive::kPrimInt:
403 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100404 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100405 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100406 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100407 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
408 X86ManagedRegister current =
409 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
410 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100411 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100412 }
413 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100414 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100415 }
416
417 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100418 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100419 return Location::FpuRegisterLocation(
420 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100421 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100422
423 case Primitive::kPrimVoid:
424 LOG(FATAL) << "Unreachable type " << type;
425 }
426
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100427 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100428}
429
Nicolas Geoffray98893962015-01-21 12:32:32 +0000430void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline ATTRIBUTE_UNUSED) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100431 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100432 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100433
434 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100435 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100436
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000437 // TODO: We currently don't use Quick's callee saved registers.
438 DCHECK(kFollowsQuickABI);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100439 blocked_core_registers_[EBP] = true;
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000440 blocked_core_registers_[ESI] = true;
441 blocked_core_registers_[EDI] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100442
443 UpdateBlockedPairRegisters();
444}
445
446void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
447 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
448 X86ManagedRegister current =
449 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
450 if (blocked_core_registers_[current.AsRegisterPairLow()]
451 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
452 blocked_register_pairs_[i] = true;
453 }
454 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100455}
456
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100457InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
458 : HGraphVisitor(graph),
459 assembler_(codegen->GetAssembler()),
460 codegen_(codegen) {}
461
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000462void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000463 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000464 bool skip_overflow_check =
465 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000466 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000467
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000468 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100469 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100470 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100471 }
472
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000473 __ subl(ESP, Immediate(GetFrameSize() - FrameEntrySpillSize()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100474 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000475}
476
477void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000478 __ addl(ESP, Immediate(GetFrameSize() - FrameEntrySpillSize()));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000479}
480
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100481void CodeGeneratorX86::Bind(HBasicBlock* block) {
482 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000483}
484
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100485void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100486 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000487}
488
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100489Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
490 switch (load->GetType()) {
491 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100492 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100493 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
494 break;
495
496 case Primitive::kPrimInt:
497 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100498 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100499 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100500
501 case Primitive::kPrimBoolean:
502 case Primitive::kPrimByte:
503 case Primitive::kPrimChar:
504 case Primitive::kPrimShort:
505 case Primitive::kPrimVoid:
506 LOG(FATAL) << "Unexpected type " << load->GetType();
507 }
508
509 LOG(FATAL) << "Unreachable";
510 return Location();
511}
512
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100513Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
514 switch (type) {
515 case Primitive::kPrimBoolean:
516 case Primitive::kPrimByte:
517 case Primitive::kPrimChar:
518 case Primitive::kPrimShort:
519 case Primitive::kPrimInt:
520 case Primitive::kPrimNot: {
521 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000522 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100523 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100524 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100525 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000526 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100527 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100528 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100529
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000530 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100531 uint32_t index = gp_index_;
532 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000533 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100534 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100535 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
536 calling_convention.GetRegisterPairAt(index));
537 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100538 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000539 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
540 }
541 }
542
543 case Primitive::kPrimFloat: {
544 uint32_t index = fp_index_++;
545 stack_index_++;
546 if (index < calling_convention.GetNumberOfFpuRegisters()) {
547 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
548 } else {
549 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
550 }
551 }
552
553 case Primitive::kPrimDouble: {
554 uint32_t index = fp_index_++;
555 stack_index_ += 2;
556 if (index < calling_convention.GetNumberOfFpuRegisters()) {
557 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
558 } else {
559 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100560 }
561 }
562
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100563 case Primitive::kPrimVoid:
564 LOG(FATAL) << "Unexpected parameter type " << type;
565 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100566 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100567 return Location();
568}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100569
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100570void CodeGeneratorX86::Move32(Location destination, Location source) {
571 if (source.Equals(destination)) {
572 return;
573 }
574 if (destination.IsRegister()) {
575 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000576 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100577 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000578 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100579 } else {
580 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000581 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100582 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100583 } else if (destination.IsFpuRegister()) {
584 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000585 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100586 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000587 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100588 } else {
589 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000590 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100591 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100592 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000593 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100594 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000595 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100596 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000597 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500598 } else if (source.IsConstant()) {
599 HConstant* constant = source.GetConstant();
600 int32_t value;
601 if (constant->IsIntConstant()) {
602 value = constant->AsIntConstant()->GetValue();
603 } else {
604 DCHECK(constant->IsFloatConstant());
605 value = bit_cast<float, int32_t>(constant->AsFloatConstant()->GetValue());
606 }
607 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100608 } else {
609 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100610 __ pushl(Address(ESP, source.GetStackIndex()));
611 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100612 }
613 }
614}
615
616void CodeGeneratorX86::Move64(Location destination, Location source) {
617 if (source.Equals(destination)) {
618 return;
619 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100620 if (destination.IsRegisterPair()) {
621 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000622 EmitParallelMoves(
623 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
624 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
625 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
626 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100627 } else if (source.IsFpuRegister()) {
628 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100629 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000630 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100631 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100632 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
633 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100634 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
635 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100636 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500637 if (source.IsFpuRegister()) {
638 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
639 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000640 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100641 } else {
642 LOG(FATAL) << "Unimplemented";
643 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100644 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000645 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100646 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000647 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100648 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100649 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100650 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100651 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000652 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100653 } else {
654 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000655 EmitParallelMoves(
656 Location::StackSlot(source.GetStackIndex()),
657 Location::StackSlot(destination.GetStackIndex()),
658 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
659 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100660 }
661 }
662}
663
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100664void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000665 LocationSummary* locations = instruction->GetLocations();
666 if (locations != nullptr && locations->Out().Equals(location)) {
667 return;
668 }
669
670 if (locations != nullptr && locations->Out().IsConstant()) {
671 HConstant* const_to_move = locations->Out().GetConstant();
672 if (const_to_move->IsIntConstant()) {
673 Immediate imm(const_to_move->AsIntConstant()->GetValue());
674 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000675 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000676 } else if (location.IsStackSlot()) {
677 __ movl(Address(ESP, location.GetStackIndex()), imm);
678 } else {
679 DCHECK(location.IsConstant());
680 DCHECK_EQ(location.GetConstant(), const_to_move);
681 }
682 } else if (const_to_move->IsLongConstant()) {
683 int64_t value = const_to_move->AsLongConstant()->GetValue();
684 if (location.IsRegisterPair()) {
685 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
686 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
687 } else if (location.IsDoubleStackSlot()) {
688 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000689 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
690 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000691 } else {
692 DCHECK(location.IsConstant());
693 DCHECK_EQ(location.GetConstant(), instruction);
694 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100695 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000696 } else if (instruction->IsTemporary()) {
697 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000698 if (temp_location.IsStackSlot()) {
699 Move32(location, temp_location);
700 } else {
701 DCHECK(temp_location.IsDoubleStackSlot());
702 Move64(location, temp_location);
703 }
Roland Levillain476df552014-10-09 17:51:36 +0100704 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100705 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100706 switch (instruction->GetType()) {
707 case Primitive::kPrimBoolean:
708 case Primitive::kPrimByte:
709 case Primitive::kPrimChar:
710 case Primitive::kPrimShort:
711 case Primitive::kPrimInt:
712 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100713 case Primitive::kPrimFloat:
714 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100715 break;
716
717 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100718 case Primitive::kPrimDouble:
719 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100720 break;
721
722 default:
723 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
724 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000725 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100726 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100727 switch (instruction->GetType()) {
728 case Primitive::kPrimBoolean:
729 case Primitive::kPrimByte:
730 case Primitive::kPrimChar:
731 case Primitive::kPrimShort:
732 case Primitive::kPrimInt:
733 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100734 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000735 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100736 break;
737
738 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100739 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000740 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100741 break;
742
743 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100744 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100745 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000746 }
747}
748
749void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000750 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000751}
752
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000753void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000754 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100755 DCHECK(!successor->IsExitBlock());
756
757 HBasicBlock* block = got->GetBlock();
758 HInstruction* previous = got->GetPrevious();
759
760 HLoopInformation* info = block->GetLoopInformation();
761 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
762 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
763 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
764 return;
765 }
766
767 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
768 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
769 }
770 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000771 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000772 }
773}
774
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000775void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000776 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000777}
778
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000779void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700780 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000781 if (kIsDebugBuild) {
782 __ Comment("Unreachable");
783 __ int3();
784 }
785}
786
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000787void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100788 LocationSummary* locations =
789 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100790 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100791 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100792 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100793 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000794}
795
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000796void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700797 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100798 if (cond->IsIntConstant()) {
799 // Constant condition, statically compared against 1.
800 int32_t cond_value = cond->AsIntConstant()->GetValue();
801 if (cond_value == 1) {
802 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
803 if_instr->IfTrueSuccessor())) {
804 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100805 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100806 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100807 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100808 DCHECK_EQ(cond_value, 0);
809 }
810 } else {
811 bool materialized =
812 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
813 // Moves do not affect the eflags register, so if the condition is
814 // evaluated just before the if, we don't need to evaluate it
815 // again.
816 bool eflags_set = cond->IsCondition()
817 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
818 if (materialized) {
819 if (!eflags_set) {
820 // Materialized condition, compare against 0.
821 Location lhs = if_instr->GetLocations()->InAt(0);
822 if (lhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000823 __ cmpl(lhs.AsRegister<Register>(), Immediate(0));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100824 } else {
825 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
826 }
827 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
828 } else {
829 __ j(X86Condition(cond->AsCondition()->GetCondition()),
830 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
831 }
832 } else {
833 Location lhs = cond->GetLocations()->InAt(0);
834 Location rhs = cond->GetLocations()->InAt(1);
835 // LHS is guaranteed to be in a register (see
836 // LocationsBuilderX86::VisitCondition).
837 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000838 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100839 } else if (rhs.IsConstant()) {
840 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
841 Immediate imm(instruction->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000842 __ cmpl(lhs.AsRegister<Register>(), imm);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100843 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000844 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100845 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100846 __ j(X86Condition(cond->AsCondition()->GetCondition()),
847 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700848 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100849 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100850 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
851 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700852 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000853 }
854}
855
856void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000857 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000858}
859
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000860void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
861 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000862}
863
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000864void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100865 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000866}
867
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000868void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100869 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700870 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000871}
872
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100873void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100874 LocationSummary* locations =
875 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100876 switch (store->InputAt(1)->GetType()) {
877 case Primitive::kPrimBoolean:
878 case Primitive::kPrimByte:
879 case Primitive::kPrimChar:
880 case Primitive::kPrimShort:
881 case Primitive::kPrimInt:
882 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100883 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100884 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
885 break;
886
887 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100888 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100889 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
890 break;
891
892 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100893 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100894 }
895 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000896}
897
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000898void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700899 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000900}
901
Dave Allison20dfc792014-06-16 20:44:29 -0700902void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100903 LocationSummary* locations =
904 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100905 locations->SetInAt(0, Location::RequiresRegister());
906 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100907 if (comp->NeedsMaterialization()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000908 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100909 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000910}
911
Dave Allison20dfc792014-06-16 20:44:29 -0700912void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
913 if (comp->NeedsMaterialization()) {
914 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000915 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100916 // Clear register: setcc only sets the low byte.
917 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700918 if (locations->InAt(1).IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000919 __ cmpl(locations->InAt(0).AsRegister<Register>(),
920 locations->InAt(1).AsRegister<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100921 } else if (locations->InAt(1).IsConstant()) {
922 HConstant* instruction = locations->InAt(1).GetConstant();
923 Immediate imm(instruction->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000924 __ cmpl(locations->InAt(0).AsRegister<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700925 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000926 __ cmpl(locations->InAt(0).AsRegister<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700927 Address(ESP, locations->InAt(1).GetStackIndex()));
928 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000929 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100930 }
Dave Allison20dfc792014-06-16 20:44:29 -0700931}
932
933void LocationsBuilderX86::VisitEqual(HEqual* comp) {
934 VisitCondition(comp);
935}
936
937void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
938 VisitCondition(comp);
939}
940
941void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
942 VisitCondition(comp);
943}
944
945void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
946 VisitCondition(comp);
947}
948
949void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
950 VisitCondition(comp);
951}
952
953void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
954 VisitCondition(comp);
955}
956
957void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
958 VisitCondition(comp);
959}
960
961void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
962 VisitCondition(comp);
963}
964
965void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
966 VisitCondition(comp);
967}
968
969void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
970 VisitCondition(comp);
971}
972
973void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
974 VisitCondition(comp);
975}
976
977void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
978 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000979}
980
981void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100982 LocationSummary* locations =
983 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100984 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000985}
986
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000987void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100988 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700989 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000990}
991
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100992void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100993 LocationSummary* locations =
994 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100995 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100996}
997
998void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
999 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001000 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001001}
1002
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001003void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1004 LocationSummary* locations =
1005 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1006 locations->SetOut(Location::ConstantLocation(constant));
1007}
1008
1009void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1010 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001011 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001012}
1013
1014void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1015 LocationSummary* locations =
1016 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1017 locations->SetOut(Location::ConstantLocation(constant));
1018}
1019
1020void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1021 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001022 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001023}
1024
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001025void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001026 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001027}
1028
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001029void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001030 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001031 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001032 __ ret();
1033}
1034
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001035void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001036 LocationSummary* locations =
1037 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001038 switch (ret->InputAt(0)->GetType()) {
1039 case Primitive::kPrimBoolean:
1040 case Primitive::kPrimByte:
1041 case Primitive::kPrimChar:
1042 case Primitive::kPrimShort:
1043 case Primitive::kPrimInt:
1044 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001045 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001046 break;
1047
1048 case Primitive::kPrimLong:
1049 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001050 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001051 break;
1052
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001053 case Primitive::kPrimFloat:
1054 case Primitive::kPrimDouble:
1055 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001056 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001057 break;
1058
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001059 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001060 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001061 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001062}
1063
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001064void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001065 if (kIsDebugBuild) {
1066 switch (ret->InputAt(0)->GetType()) {
1067 case Primitive::kPrimBoolean:
1068 case Primitive::kPrimByte:
1069 case Primitive::kPrimChar:
1070 case Primitive::kPrimShort:
1071 case Primitive::kPrimInt:
1072 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001073 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001074 break;
1075
1076 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001077 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1078 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001079 break;
1080
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001081 case Primitive::kPrimFloat:
1082 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001083 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001084 break;
1085
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001086 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001087 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001088 }
1089 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001090 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001091 __ ret();
1092}
1093
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001094void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001095 HandleInvoke(invoke);
1096}
1097
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001098void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001099 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001100
1101 // TODO: Implement all kinds of calls:
1102 // 1) boot -> boot
1103 // 2) app -> boot
1104 // 3) app -> app
1105 //
1106 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1107
1108 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001109 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001110 if (!invoke->IsRecursive()) {
1111 // temp = temp->dex_cache_resolved_methods_;
1112 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
1113 // temp = temp[index_in_cache]
1114 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
1115 // (temp + offset_of_quick_compiled_code)()
1116 __ call(Address(
1117 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
1118 } else {
1119 __ call(codegen_->GetFrameEntryLabel());
1120 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001121
1122 DCHECK(!codegen_->IsLeafMethod());
1123 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1124}
1125
1126void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1127 HandleInvoke(invoke);
1128}
1129
1130void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001131 LocationSummary* locations =
1132 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001133 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001134
1135 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001136 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001137 HInstruction* input = invoke->InputAt(i);
1138 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1139 }
1140
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001141 switch (invoke->GetType()) {
1142 case Primitive::kPrimBoolean:
1143 case Primitive::kPrimByte:
1144 case Primitive::kPrimChar:
1145 case Primitive::kPrimShort:
1146 case Primitive::kPrimInt:
1147 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001148 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001149 break;
1150
1151 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001152 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001153 break;
1154
1155 case Primitive::kPrimVoid:
1156 break;
1157
1158 case Primitive::kPrimDouble:
1159 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001160 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001161 break;
1162 }
1163
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001164 invoke->SetLocations(locations);
1165}
1166
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001167void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001168 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001169 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1170 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1171 LocationSummary* locations = invoke->GetLocations();
1172 Location receiver = locations->InAt(0);
1173 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1174 // temp = object->GetClass();
1175 if (receiver.IsStackSlot()) {
1176 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1177 __ movl(temp, Address(temp, class_offset));
1178 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001179 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001180 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001181 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001182 // temp = temp->GetMethodAt(method_offset);
1183 __ movl(temp, Address(temp, method_offset));
1184 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001185 __ call(Address(
1186 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001187
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001188 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001189 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001190}
1191
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001192void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1193 HandleInvoke(invoke);
1194 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001195 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001196}
1197
1198void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1199 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001200 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001201 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1202 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1203 LocationSummary* locations = invoke->GetLocations();
1204 Location receiver = locations->InAt(0);
1205 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1206
1207 // Set the hidden argument.
1208 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001209 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001210
1211 // temp = object->GetClass();
1212 if (receiver.IsStackSlot()) {
1213 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1214 __ movl(temp, Address(temp, class_offset));
1215 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001216 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001217 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001218 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001219 // temp = temp->GetImtEntryAt(method_offset);
1220 __ movl(temp, Address(temp, method_offset));
1221 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001222 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001223 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001224
1225 DCHECK(!codegen_->IsLeafMethod());
1226 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1227}
1228
Roland Levillain88cb1752014-10-20 16:36:47 +01001229void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1230 LocationSummary* locations =
1231 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1232 switch (neg->GetResultType()) {
1233 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001234 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001235 locations->SetInAt(0, Location::RequiresRegister());
1236 locations->SetOut(Location::SameAsFirstInput());
1237 break;
1238
Roland Levillain88cb1752014-10-20 16:36:47 +01001239 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001240 locations->SetInAt(0, Location::RequiresFpuRegister());
1241 locations->SetOut(Location::SameAsFirstInput());
1242 locations->AddTemp(Location::RequiresRegister());
1243 locations->AddTemp(Location::RequiresFpuRegister());
1244 break;
1245
Roland Levillain88cb1752014-10-20 16:36:47 +01001246 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001247 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001248 locations->SetOut(Location::SameAsFirstInput());
1249 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001250 break;
1251
1252 default:
1253 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1254 }
1255}
1256
1257void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1258 LocationSummary* locations = neg->GetLocations();
1259 Location out = locations->Out();
1260 Location in = locations->InAt(0);
1261 switch (neg->GetResultType()) {
1262 case Primitive::kPrimInt:
1263 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001264 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001265 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001266 break;
1267
1268 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001269 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001270 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001271 __ negl(out.AsRegisterPairLow<Register>());
1272 // Negation is similar to subtraction from zero. The least
1273 // significant byte triggers a borrow when it is different from
1274 // zero; to take it into account, add 1 to the most significant
1275 // byte if the carry flag (CF) is set to 1 after the first NEGL
1276 // operation.
1277 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1278 __ negl(out.AsRegisterPairHigh<Register>());
1279 break;
1280
Roland Levillain5368c212014-11-27 15:03:41 +00001281 case Primitive::kPrimFloat: {
1282 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001283 Register constant = locations->GetTemp(0).AsRegister<Register>();
1284 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001285 // Implement float negation with an exclusive or with value
1286 // 0x80000000 (mask for bit 31, representing the sign of a
1287 // single-precision floating-point number).
1288 __ movl(constant, Immediate(INT32_C(0x80000000)));
1289 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001290 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001291 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001292 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001293
Roland Levillain5368c212014-11-27 15:03:41 +00001294 case Primitive::kPrimDouble: {
1295 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001296 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001297 // Implement double negation with an exclusive or with value
1298 // 0x8000000000000000 (mask for bit 63, representing the sign of
1299 // a double-precision floating-point number).
1300 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001301 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001302 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001303 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001304
1305 default:
1306 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1307 }
1308}
1309
Roland Levillaindff1f282014-11-05 14:15:05 +00001310void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001311 Primitive::Type result_type = conversion->GetResultType();
1312 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001313 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001314
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001315 // The float-to-long and double-to-long type conversions rely on a
1316 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001317 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001318 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1319 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001320 ? LocationSummary::kCall
1321 : LocationSummary::kNoCall;
1322 LocationSummary* locations =
1323 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1324
Roland Levillaindff1f282014-11-05 14:15:05 +00001325 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001326 case Primitive::kPrimByte:
1327 switch (input_type) {
1328 case Primitive::kPrimShort:
1329 case Primitive::kPrimInt:
1330 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001331 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001332 locations->SetInAt(0, Location::Any());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001333 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1334 break;
1335
1336 default:
1337 LOG(FATAL) << "Unexpected type conversion from " << input_type
1338 << " to " << result_type;
1339 }
1340 break;
1341
Roland Levillain01a8d712014-11-14 16:27:39 +00001342 case Primitive::kPrimShort:
1343 switch (input_type) {
1344 case Primitive::kPrimByte:
1345 case Primitive::kPrimInt:
1346 case Primitive::kPrimChar:
1347 // Processing a Dex `int-to-short' instruction.
1348 locations->SetInAt(0, Location::Any());
1349 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 Levillain946e1432014-11-11 17:35:19 +00001358 case Primitive::kPrimInt:
1359 switch (input_type) {
1360 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001361 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001362 locations->SetInAt(0, Location::Any());
1363 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1364 break;
1365
1366 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001367 // Processing a Dex `float-to-int' instruction.
1368 locations->SetInAt(0, Location::RequiresFpuRegister());
1369 locations->SetOut(Location::RequiresRegister());
1370 locations->AddTemp(Location::RequiresFpuRegister());
1371 break;
1372
Roland Levillain946e1432014-11-11 17:35:19 +00001373 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001374 // Processing a Dex `double-to-int' instruction.
1375 locations->SetInAt(0, Location::RequiresFpuRegister());
1376 locations->SetOut(Location::RequiresRegister());
1377 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001378 break;
1379
1380 default:
1381 LOG(FATAL) << "Unexpected type conversion from " << input_type
1382 << " to " << result_type;
1383 }
1384 break;
1385
Roland Levillaindff1f282014-11-05 14:15:05 +00001386 case Primitive::kPrimLong:
1387 switch (input_type) {
1388 case Primitive::kPrimByte:
1389 case Primitive::kPrimShort:
1390 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001391 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001392 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001393 locations->SetInAt(0, Location::RegisterLocation(EAX));
1394 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1395 break;
1396
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001397 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001398 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001399 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001400 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001401 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1402 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1403
Vladimir Marko949c91f2015-01-27 10:48:44 +00001404 // The runtime helper puts the result in EAX, EDX.
1405 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001406 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001407 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001408
1409 default:
1410 LOG(FATAL) << "Unexpected type conversion from " << input_type
1411 << " to " << result_type;
1412 }
1413 break;
1414
Roland Levillain981e4542014-11-14 11:47:14 +00001415 case Primitive::kPrimChar:
1416 switch (input_type) {
1417 case Primitive::kPrimByte:
1418 case Primitive::kPrimShort:
1419 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001420 // Processing a Dex `int-to-char' instruction.
1421 locations->SetInAt(0, Location::Any());
1422 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1423 break;
1424
1425 default:
1426 LOG(FATAL) << "Unexpected type conversion from " << input_type
1427 << " to " << result_type;
1428 }
1429 break;
1430
Roland Levillaindff1f282014-11-05 14:15:05 +00001431 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001432 switch (input_type) {
1433 case Primitive::kPrimByte:
1434 case Primitive::kPrimShort:
1435 case Primitive::kPrimInt:
1436 case Primitive::kPrimChar:
1437 // Processing a Dex `int-to-float' instruction.
1438 locations->SetInAt(0, Location::RequiresRegister());
1439 locations->SetOut(Location::RequiresFpuRegister());
1440 break;
1441
1442 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001443 // Processing a Dex `long-to-float' instruction.
1444 locations->SetInAt(0, Location::RequiresRegister());
1445 locations->SetOut(Location::RequiresFpuRegister());
1446 locations->AddTemp(Location::RequiresFpuRegister());
1447 locations->AddTemp(Location::RequiresFpuRegister());
1448 break;
1449
Roland Levillaincff13742014-11-17 14:32:17 +00001450 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001451 // Processing a Dex `double-to-float' instruction.
1452 locations->SetInAt(0, Location::RequiresFpuRegister());
1453 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001454 break;
1455
1456 default:
1457 LOG(FATAL) << "Unexpected type conversion from " << input_type
1458 << " to " << result_type;
1459 };
1460 break;
1461
Roland Levillaindff1f282014-11-05 14:15:05 +00001462 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001463 switch (input_type) {
1464 case Primitive::kPrimByte:
1465 case Primitive::kPrimShort:
1466 case Primitive::kPrimInt:
1467 case Primitive::kPrimChar:
1468 // Processing a Dex `int-to-double' instruction.
1469 locations->SetInAt(0, Location::RequiresRegister());
1470 locations->SetOut(Location::RequiresFpuRegister());
1471 break;
1472
1473 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001474 // Processing a Dex `long-to-double' instruction.
1475 locations->SetInAt(0, Location::RequiresRegister());
1476 locations->SetOut(Location::RequiresFpuRegister());
1477 locations->AddTemp(Location::RequiresFpuRegister());
1478 locations->AddTemp(Location::RequiresFpuRegister());
1479 break;
1480
Roland Levillaincff13742014-11-17 14:32:17 +00001481 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001482 // Processing a Dex `float-to-double' instruction.
1483 locations->SetInAt(0, Location::RequiresFpuRegister());
1484 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001485 break;
1486
1487 default:
1488 LOG(FATAL) << "Unexpected type conversion from " << input_type
1489 << " to " << result_type;
1490 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001491 break;
1492
1493 default:
1494 LOG(FATAL) << "Unexpected type conversion from " << input_type
1495 << " to " << result_type;
1496 }
1497}
1498
1499void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1500 LocationSummary* locations = conversion->GetLocations();
1501 Location out = locations->Out();
1502 Location in = locations->InAt(0);
1503 Primitive::Type result_type = conversion->GetResultType();
1504 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001505 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001506 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001507 case Primitive::kPrimByte:
1508 switch (input_type) {
1509 case Primitive::kPrimShort:
1510 case Primitive::kPrimInt:
1511 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001512 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001513 if (in.IsRegister()) {
1514 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
1515 } else if (in.IsStackSlot()) {
1516 __ movsxb(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
1517 } else {
1518 DCHECK(in.GetConstant()->IsIntConstant());
1519 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1520 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1521 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001522 break;
1523
1524 default:
1525 LOG(FATAL) << "Unexpected type conversion from " << input_type
1526 << " to " << result_type;
1527 }
1528 break;
1529
Roland Levillain01a8d712014-11-14 16:27:39 +00001530 case Primitive::kPrimShort:
1531 switch (input_type) {
1532 case Primitive::kPrimByte:
1533 case Primitive::kPrimInt:
1534 case Primitive::kPrimChar:
1535 // Processing a Dex `int-to-short' instruction.
1536 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001537 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001538 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001539 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001540 } else {
1541 DCHECK(in.GetConstant()->IsIntConstant());
1542 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001543 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001544 }
1545 break;
1546
1547 default:
1548 LOG(FATAL) << "Unexpected type conversion from " << input_type
1549 << " to " << result_type;
1550 }
1551 break;
1552
Roland Levillain946e1432014-11-11 17:35:19 +00001553 case Primitive::kPrimInt:
1554 switch (input_type) {
1555 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001556 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001557 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001558 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001559 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001560 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001561 } else {
1562 DCHECK(in.IsConstant());
1563 DCHECK(in.GetConstant()->IsLongConstant());
1564 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001565 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001566 }
1567 break;
1568
Roland Levillain3f8f9362014-12-02 17:45:01 +00001569 case Primitive::kPrimFloat: {
1570 // Processing a Dex `float-to-int' instruction.
1571 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1572 Register output = out.AsRegister<Register>();
1573 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1574 Label done, nan;
1575
1576 __ movl(output, Immediate(kPrimIntMax));
1577 // temp = int-to-float(output)
1578 __ cvtsi2ss(temp, output);
1579 // if input >= temp goto done
1580 __ comiss(input, temp);
1581 __ j(kAboveEqual, &done);
1582 // if input == NaN goto nan
1583 __ j(kUnordered, &nan);
1584 // output = float-to-int-truncate(input)
1585 __ cvttss2si(output, input);
1586 __ jmp(&done);
1587 __ Bind(&nan);
1588 // output = 0
1589 __ xorl(output, output);
1590 __ Bind(&done);
1591 break;
1592 }
1593
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001594 case Primitive::kPrimDouble: {
1595 // Processing a Dex `double-to-int' instruction.
1596 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1597 Register output = out.AsRegister<Register>();
1598 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1599 Label done, nan;
1600
1601 __ movl(output, Immediate(kPrimIntMax));
1602 // temp = int-to-double(output)
1603 __ cvtsi2sd(temp, output);
1604 // if input >= temp goto done
1605 __ comisd(input, temp);
1606 __ j(kAboveEqual, &done);
1607 // if input == NaN goto nan
1608 __ j(kUnordered, &nan);
1609 // output = double-to-int-truncate(input)
1610 __ cvttsd2si(output, input);
1611 __ jmp(&done);
1612 __ Bind(&nan);
1613 // output = 0
1614 __ xorl(output, output);
1615 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001616 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001617 }
Roland Levillain946e1432014-11-11 17:35:19 +00001618
1619 default:
1620 LOG(FATAL) << "Unexpected type conversion from " << input_type
1621 << " to " << result_type;
1622 }
1623 break;
1624
Roland Levillaindff1f282014-11-05 14:15:05 +00001625 case Primitive::kPrimLong:
1626 switch (input_type) {
1627 case Primitive::kPrimByte:
1628 case Primitive::kPrimShort:
1629 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001630 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001631 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001632 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1633 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001634 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001635 __ cdq();
1636 break;
1637
1638 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001639 // Processing a Dex `float-to-long' instruction.
1640 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001641 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1642 break;
1643
Roland Levillaindff1f282014-11-05 14:15:05 +00001644 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001645 // Processing a Dex `double-to-long' instruction.
1646 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1647 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001648 break;
1649
1650 default:
1651 LOG(FATAL) << "Unexpected type conversion from " << input_type
1652 << " to " << result_type;
1653 }
1654 break;
1655
Roland Levillain981e4542014-11-14 11:47:14 +00001656 case Primitive::kPrimChar:
1657 switch (input_type) {
1658 case Primitive::kPrimByte:
1659 case Primitive::kPrimShort:
1660 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001661 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1662 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001663 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001664 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001665 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001666 } else {
1667 DCHECK(in.GetConstant()->IsIntConstant());
1668 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001669 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001670 }
1671 break;
1672
1673 default:
1674 LOG(FATAL) << "Unexpected type conversion from " << input_type
1675 << " to " << result_type;
1676 }
1677 break;
1678
Roland Levillaindff1f282014-11-05 14:15:05 +00001679 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001680 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001681 case Primitive::kPrimByte:
1682 case Primitive::kPrimShort:
1683 case Primitive::kPrimInt:
1684 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001685 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001686 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001687 break;
1688
Roland Levillain6d0e4832014-11-27 18:31:21 +00001689 case Primitive::kPrimLong: {
1690 // Processing a Dex `long-to-float' instruction.
1691 Register low = in.AsRegisterPairLow<Register>();
1692 Register high = in.AsRegisterPairHigh<Register>();
1693 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1694 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1695 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1696
1697 // Operations use doubles for precision reasons (each 32-bit
1698 // half of a long fits in the 53-bit mantissa of a double,
1699 // but not in the 24-bit mantissa of a float). This is
1700 // especially important for the low bits. The result is
1701 // eventually converted to float.
1702
1703 // low = low - 2^31 (to prevent bit 31 of `low` to be
1704 // interpreted as a sign bit)
1705 __ subl(low, Immediate(0x80000000));
1706 // temp = int-to-double(high)
1707 __ cvtsi2sd(temp, high);
1708 // temp = temp * 2^32
1709 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
1710 __ mulsd(temp, constant);
1711 // result = int-to-double(low)
1712 __ cvtsi2sd(result, low);
1713 // result = result + 2^31 (restore the original value of `low`)
1714 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
1715 __ addsd(result, constant);
1716 // result = result + temp
1717 __ addsd(result, temp);
1718 // result = double-to-float(result)
1719 __ cvtsd2ss(result, result);
1720 break;
1721 }
1722
Roland Levillaincff13742014-11-17 14:32:17 +00001723 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001724 // Processing a Dex `double-to-float' instruction.
1725 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001726 break;
1727
1728 default:
1729 LOG(FATAL) << "Unexpected type conversion from " << input_type
1730 << " to " << result_type;
1731 };
1732 break;
1733
Roland Levillaindff1f282014-11-05 14:15:05 +00001734 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001735 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001736 case Primitive::kPrimByte:
1737 case Primitive::kPrimShort:
1738 case Primitive::kPrimInt:
1739 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001740 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001741 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001742 break;
1743
Roland Levillain647b9ed2014-11-27 12:06:00 +00001744 case Primitive::kPrimLong: {
1745 // Processing a Dex `long-to-double' instruction.
1746 Register low = in.AsRegisterPairLow<Register>();
1747 Register high = in.AsRegisterPairHigh<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001748 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1749 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1750 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001751
Roland Levillain647b9ed2014-11-27 12:06:00 +00001752 // low = low - 2^31 (to prevent bit 31 of `low` to be
1753 // interpreted as a sign bit)
1754 __ subl(low, Immediate(0x80000000));
1755 // temp = int-to-double(high)
1756 __ cvtsi2sd(temp, high);
1757 // temp = temp * 2^32
Roland Levillain6d0e4832014-11-27 18:31:21 +00001758 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001759 __ mulsd(temp, constant);
1760 // result = int-to-double(low)
1761 __ cvtsi2sd(result, low);
1762 // result = result + 2^31 (restore the original value of `low`)
Roland Levillain6d0e4832014-11-27 18:31:21 +00001763 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001764 __ addsd(result, constant);
1765 // result = result + temp
1766 __ addsd(result, temp);
1767 break;
1768 }
1769
Roland Levillaincff13742014-11-17 14:32:17 +00001770 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001771 // Processing a Dex `float-to-double' instruction.
1772 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001773 break;
1774
1775 default:
1776 LOG(FATAL) << "Unexpected type conversion from " << input_type
1777 << " to " << result_type;
1778 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001779 break;
1780
1781 default:
1782 LOG(FATAL) << "Unexpected type conversion from " << input_type
1783 << " to " << result_type;
1784 }
1785}
1786
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001787void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001788 LocationSummary* locations =
1789 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001790 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001791 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001792 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001793 locations->SetInAt(0, Location::RequiresRegister());
1794 locations->SetInAt(1, Location::Any());
1795 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001796 break;
1797 }
1798
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001799 case Primitive::kPrimFloat:
1800 case Primitive::kPrimDouble: {
1801 locations->SetInAt(0, Location::RequiresFpuRegister());
1802 locations->SetInAt(1, Location::Any());
1803 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001804 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001805 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001806
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001807 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001808 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1809 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001810 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001811}
1812
1813void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1814 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001815 Location first = locations->InAt(0);
1816 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001817 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001818 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001819 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001820 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001821 __ addl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001822 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001823 __ addl(first.AsRegister<Register>(),
1824 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001825 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001826 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001827 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001828 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001829 }
1830
1831 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001832 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001833 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1834 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001835 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001836 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1837 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001838 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001839 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001840 break;
1841 }
1842
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001843 case Primitive::kPrimFloat: {
1844 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001845 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001846 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001847 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001848 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001849 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001850 }
1851
1852 case Primitive::kPrimDouble: {
1853 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001854 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001855 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001856 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001857 }
1858 break;
1859 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001860
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001861 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001862 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001863 }
1864}
1865
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001866void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001867 LocationSummary* locations =
1868 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001869 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001870 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001871 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001872 locations->SetInAt(0, Location::RequiresRegister());
1873 locations->SetInAt(1, Location::Any());
1874 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001875 break;
1876 }
Calin Juravle11351682014-10-23 15:38:15 +01001877 case Primitive::kPrimFloat:
1878 case Primitive::kPrimDouble: {
1879 locations->SetInAt(0, Location::RequiresFpuRegister());
1880 locations->SetInAt(1, Location::RequiresFpuRegister());
1881 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001882 break;
Calin Juravle11351682014-10-23 15:38:15 +01001883 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001884
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001885 default:
Calin Juravle11351682014-10-23 15:38:15 +01001886 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001887 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001888}
1889
1890void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1891 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001892 Location first = locations->InAt(0);
1893 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001894 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001895 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001896 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001897 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001898 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001899 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001900 __ subl(first.AsRegister<Register>(),
1901 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001902 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001903 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001904 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001905 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001906 }
1907
1908 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001909 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01001910 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1911 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001912 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001913 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001914 __ sbbl(first.AsRegisterPairHigh<Register>(),
1915 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001916 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001917 break;
1918 }
1919
Calin Juravle11351682014-10-23 15:38:15 +01001920 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001921 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001922 break;
Calin Juravle11351682014-10-23 15:38:15 +01001923 }
1924
1925 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001926 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001927 break;
1928 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001929
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001930 default:
Calin Juravle11351682014-10-23 15:38:15 +01001931 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001932 }
1933}
1934
Calin Juravle34bacdf2014-10-07 20:23:36 +01001935void LocationsBuilderX86::VisitMul(HMul* mul) {
1936 LocationSummary* locations =
1937 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1938 switch (mul->GetResultType()) {
1939 case Primitive::kPrimInt:
1940 locations->SetInAt(0, Location::RequiresRegister());
1941 locations->SetInAt(1, Location::Any());
1942 locations->SetOut(Location::SameAsFirstInput());
1943 break;
1944 case Primitive::kPrimLong: {
1945 locations->SetInAt(0, Location::RequiresRegister());
1946 // TODO: Currently this handles only stack operands:
1947 // - we don't have enough registers because we currently use Quick ABI.
1948 // - by the time we have a working register allocator we will probably change the ABI
1949 // and fix the above.
1950 // - we don't have a way yet to request operands on stack but the base line compiler
1951 // will leave the operands on the stack with Any().
1952 locations->SetInAt(1, Location::Any());
1953 locations->SetOut(Location::SameAsFirstInput());
1954 // Needed for imul on 32bits with 64bits output.
1955 locations->AddTemp(Location::RegisterLocation(EAX));
1956 locations->AddTemp(Location::RegisterLocation(EDX));
1957 break;
1958 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001959 case Primitive::kPrimFloat:
1960 case Primitive::kPrimDouble: {
1961 locations->SetInAt(0, Location::RequiresFpuRegister());
1962 locations->SetInAt(1, Location::RequiresFpuRegister());
1963 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001964 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001965 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001966
1967 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001968 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001969 }
1970}
1971
1972void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1973 LocationSummary* locations = mul->GetLocations();
1974 Location first = locations->InAt(0);
1975 Location second = locations->InAt(1);
1976 DCHECK(first.Equals(locations->Out()));
1977
1978 switch (mul->GetResultType()) {
1979 case Primitive::kPrimInt: {
1980 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001981 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001982 } else if (second.IsConstant()) {
1983 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001984 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001985 } else {
1986 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001987 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01001988 }
1989 break;
1990 }
1991
1992 case Primitive::kPrimLong: {
1993 DCHECK(second.IsDoubleStackSlot());
1994
1995 Register in1_hi = first.AsRegisterPairHigh<Register>();
1996 Register in1_lo = first.AsRegisterPairLow<Register>();
1997 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
1998 Address in2_lo(ESP, second.GetStackIndex());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001999 Register eax = locations->GetTemp(0).AsRegister<Register>();
2000 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002001
2002 DCHECK_EQ(EAX, eax);
2003 DCHECK_EQ(EDX, edx);
2004
2005 // input: in1 - 64 bits, in2 - 64 bits
2006 // output: in1
2007 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2008 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2009 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
2010
2011 __ movl(eax, in2_hi);
2012 // eax <- in1.lo * in2.hi
2013 __ imull(eax, in1_lo);
2014 // in1.hi <- in1.hi * in2.lo
2015 __ imull(in1_hi, in2_lo);
2016 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2017 __ addl(in1_hi, eax);
2018 // move in1_lo to eax to prepare for double precision
2019 __ movl(eax, in1_lo);
2020 // edx:eax <- in1.lo * in2.lo
2021 __ mull(in2_lo);
2022 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2023 __ addl(in1_hi, edx);
2024 // in1.lo <- (in1.lo * in2.lo)[31:0];
2025 __ movl(in1_lo, eax);
2026
2027 break;
2028 }
2029
Calin Juravleb5bfa962014-10-21 18:02:24 +01002030 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002031 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002032 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002033 }
2034
2035 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002036 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002037 break;
2038 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002039
2040 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002041 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002042 }
2043}
2044
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002045void InstructionCodeGeneratorX86::PushOntoFPStack(Location source, uint32_t temp_offset,
2046 uint32_t stack_adjustment, bool is_float) {
2047 if (source.IsStackSlot()) {
2048 DCHECK(is_float);
2049 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2050 } else if (source.IsDoubleStackSlot()) {
2051 DCHECK(!is_float);
2052 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2053 } else {
2054 // Write the value to the temporary location on the stack and load to FP stack.
2055 if (is_float) {
2056 Location stack_temp = Location::StackSlot(temp_offset);
2057 codegen_->Move32(stack_temp, source);
2058 __ flds(Address(ESP, temp_offset));
2059 } else {
2060 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2061 codegen_->Move64(stack_temp, source);
2062 __ fldl(Address(ESP, temp_offset));
2063 }
2064 }
2065}
2066
2067void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2068 Primitive::Type type = rem->GetResultType();
2069 bool is_float = type == Primitive::kPrimFloat;
2070 size_t elem_size = Primitive::ComponentSize(type);
2071 LocationSummary* locations = rem->GetLocations();
2072 Location first = locations->InAt(0);
2073 Location second = locations->InAt(1);
2074 Location out = locations->Out();
2075
2076 // Create stack space for 2 elements.
2077 // TODO: enhance register allocator to ask for stack temporaries.
2078 __ subl(ESP, Immediate(2 * elem_size));
2079
2080 // Load the values to the FP stack in reverse order, using temporaries if needed.
2081 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2082 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2083
2084 // Loop doing FPREM until we stabilize.
2085 Label retry;
2086 __ Bind(&retry);
2087 __ fprem();
2088
2089 // Move FP status to AX.
2090 __ fstsw();
2091
2092 // And see if the argument reduction is complete. This is signaled by the
2093 // C2 FPU flag bit set to 0.
2094 __ andl(EAX, Immediate(kC2ConditionMask));
2095 __ j(kNotEqual, &retry);
2096
2097 // We have settled on the final value. Retrieve it into an XMM register.
2098 // Store FP top of stack to real stack.
2099 if (is_float) {
2100 __ fsts(Address(ESP, 0));
2101 } else {
2102 __ fstl(Address(ESP, 0));
2103 }
2104
2105 // Pop the 2 items from the FP stack.
2106 __ fucompp();
2107
2108 // Load the value from the stack into an XMM register.
2109 DCHECK(out.IsFpuRegister()) << out;
2110 if (is_float) {
2111 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2112 } else {
2113 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2114 }
2115
2116 // And remove the temporary stack space we allocated.
2117 __ addl(ESP, Immediate(2 * elem_size));
2118}
2119
Calin Juravlebacfec32014-11-14 15:54:36 +00002120void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2121 DCHECK(instruction->IsDiv() || instruction->IsRem());
2122
2123 LocationSummary* locations = instruction->GetLocations();
2124 Location out = locations->Out();
2125 Location first = locations->InAt(0);
2126 Location second = locations->InAt(1);
2127 bool is_div = instruction->IsDiv();
2128
2129 switch (instruction->GetResultType()) {
2130 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002131 Register second_reg = second.AsRegister<Register>();
2132 DCHECK_EQ(EAX, first.AsRegister<Register>());
2133 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002134
2135 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002136 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
2137 is_div);
Calin Juravlebacfec32014-11-14 15:54:36 +00002138 codegen_->AddSlowPath(slow_path);
2139
2140 // 0x80000000/-1 triggers an arithmetic exception!
2141 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2142 // it's safe to just use negl instead of more complex comparisons.
2143
2144 __ cmpl(second_reg, Immediate(-1));
2145 __ j(kEqual, slow_path->GetEntryLabel());
2146
2147 // edx:eax <- sign-extended of eax
2148 __ cdq();
2149 // eax = quotient, edx = remainder
2150 __ idivl(second_reg);
2151
2152 __ Bind(slow_path->GetExitLabel());
2153 break;
2154 }
2155
2156 case Primitive::kPrimLong: {
2157 InvokeRuntimeCallingConvention calling_convention;
2158 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2159 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2160 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2161 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2162 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2163 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2164
2165 if (is_div) {
2166 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2167 } else {
2168 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2169 }
2170 uint32_t dex_pc = is_div
2171 ? instruction->AsDiv()->GetDexPc()
2172 : instruction->AsRem()->GetDexPc();
2173 codegen_->RecordPcInfo(instruction, dex_pc);
2174
2175 break;
2176 }
2177
2178 default:
2179 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2180 }
2181}
2182
Calin Juravle7c4954d2014-10-28 16:57:40 +00002183void LocationsBuilderX86::VisitDiv(HDiv* div) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002184 LocationSummary::CallKind call_kind = div->GetResultType() == Primitive::kPrimLong
2185 ? LocationSummary::kCall
2186 : LocationSummary::kNoCall;
2187 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2188
Calin Juravle7c4954d2014-10-28 16:57:40 +00002189 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002190 case Primitive::kPrimInt: {
2191 locations->SetInAt(0, Location::RegisterLocation(EAX));
2192 locations->SetInAt(1, Location::RequiresRegister());
2193 locations->SetOut(Location::SameAsFirstInput());
2194 // Intel uses edx:eax as the dividend.
2195 locations->AddTemp(Location::RegisterLocation(EDX));
2196 break;
2197 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002198 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002199 InvokeRuntimeCallingConvention calling_convention;
2200 locations->SetInAt(0, Location::RegisterPairLocation(
2201 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2202 locations->SetInAt(1, Location::RegisterPairLocation(
2203 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2204 // Runtime helper puts the result in EAX, EDX.
2205 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002206 break;
2207 }
2208 case Primitive::kPrimFloat:
2209 case Primitive::kPrimDouble: {
2210 locations->SetInAt(0, Location::RequiresFpuRegister());
2211 locations->SetInAt(1, Location::RequiresFpuRegister());
2212 locations->SetOut(Location::SameAsFirstInput());
2213 break;
2214 }
2215
2216 default:
2217 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2218 }
2219}
2220
2221void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2222 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002223 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002224 Location first = locations->InAt(0);
2225 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002226
2227 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002228 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002229 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002230 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002231 break;
2232 }
2233
2234 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002235 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002236 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002237 break;
2238 }
2239
2240 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002241 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002242 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002243 break;
2244 }
2245
2246 default:
2247 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2248 }
2249}
2250
Calin Juravlebacfec32014-11-14 15:54:36 +00002251void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002252 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002253 LocationSummary* locations =
2254 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravlebacfec32014-11-14 15:54:36 +00002255
Calin Juravled2ec87d2014-12-08 14:24:46 +00002256 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002257 case Primitive::kPrimInt: {
2258 locations->SetInAt(0, Location::RegisterLocation(EAX));
2259 locations->SetInAt(1, Location::RequiresRegister());
2260 locations->SetOut(Location::RegisterLocation(EDX));
2261 break;
2262 }
2263 case Primitive::kPrimLong: {
2264 InvokeRuntimeCallingConvention calling_convention;
2265 locations->SetInAt(0, Location::RegisterPairLocation(
2266 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2267 locations->SetInAt(1, Location::RegisterPairLocation(
2268 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2269 // Runtime helper puts the result in EAX, EDX.
2270 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2271 break;
2272 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002273 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002274 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002275 locations->SetInAt(0, Location::Any());
2276 locations->SetInAt(1, Location::Any());
2277 locations->SetOut(Location::RequiresFpuRegister());
2278 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002279 break;
2280 }
2281
2282 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002283 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002284 }
2285}
2286
2287void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2288 Primitive::Type type = rem->GetResultType();
2289 switch (type) {
2290 case Primitive::kPrimInt:
2291 case Primitive::kPrimLong: {
2292 GenerateDivRemIntegral(rem);
2293 break;
2294 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002295 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002296 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002297 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002298 break;
2299 }
2300 default:
2301 LOG(FATAL) << "Unexpected rem type " << type;
2302 }
2303}
2304
Calin Juravled0d48522014-11-04 16:40:20 +00002305void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2306 LocationSummary* locations =
2307 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002308 switch (instruction->GetType()) {
2309 case Primitive::kPrimInt: {
2310 locations->SetInAt(0, Location::Any());
2311 break;
2312 }
2313 case Primitive::kPrimLong: {
2314 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2315 if (!instruction->IsConstant()) {
2316 locations->AddTemp(Location::RequiresRegister());
2317 }
2318 break;
2319 }
2320 default:
2321 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2322 }
Calin Juravled0d48522014-11-04 16:40:20 +00002323 if (instruction->HasUses()) {
2324 locations->SetOut(Location::SameAsFirstInput());
2325 }
2326}
2327
2328void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2329 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2330 codegen_->AddSlowPath(slow_path);
2331
2332 LocationSummary* locations = instruction->GetLocations();
2333 Location value = locations->InAt(0);
2334
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002335 switch (instruction->GetType()) {
2336 case Primitive::kPrimInt: {
2337 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002338 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002339 __ j(kEqual, slow_path->GetEntryLabel());
2340 } else if (value.IsStackSlot()) {
2341 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2342 __ j(kEqual, slow_path->GetEntryLabel());
2343 } else {
2344 DCHECK(value.IsConstant()) << value;
2345 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2346 __ jmp(slow_path->GetEntryLabel());
2347 }
2348 }
2349 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002350 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002351 case Primitive::kPrimLong: {
2352 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002353 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002354 __ movl(temp, value.AsRegisterPairLow<Register>());
2355 __ orl(temp, value.AsRegisterPairHigh<Register>());
2356 __ j(kEqual, slow_path->GetEntryLabel());
2357 } else {
2358 DCHECK(value.IsConstant()) << value;
2359 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2360 __ jmp(slow_path->GetEntryLabel());
2361 }
2362 }
2363 break;
2364 }
2365 default:
2366 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002367 }
Calin Juravled0d48522014-11-04 16:40:20 +00002368}
2369
Calin Juravle9aec02f2014-11-18 23:06:35 +00002370void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2371 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2372
2373 LocationSummary* locations =
2374 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2375
2376 switch (op->GetResultType()) {
2377 case Primitive::kPrimInt: {
2378 locations->SetInAt(0, Location::RequiresRegister());
2379 // The shift count needs to be in CL.
2380 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2381 locations->SetOut(Location::SameAsFirstInput());
2382 break;
2383 }
2384 case Primitive::kPrimLong: {
2385 locations->SetInAt(0, Location::RequiresRegister());
2386 // The shift count needs to be in CL.
2387 locations->SetInAt(1, Location::RegisterLocation(ECX));
2388 locations->SetOut(Location::SameAsFirstInput());
2389 break;
2390 }
2391 default:
2392 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2393 }
2394}
2395
2396void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2397 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2398
2399 LocationSummary* locations = op->GetLocations();
2400 Location first = locations->InAt(0);
2401 Location second = locations->InAt(1);
2402 DCHECK(first.Equals(locations->Out()));
2403
2404 switch (op->GetResultType()) {
2405 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002406 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002407 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002408 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002409 DCHECK_EQ(ECX, second_reg);
2410 if (op->IsShl()) {
2411 __ shll(first_reg, second_reg);
2412 } else if (op->IsShr()) {
2413 __ sarl(first_reg, second_reg);
2414 } else {
2415 __ shrl(first_reg, second_reg);
2416 }
2417 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002418 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002419 if (op->IsShl()) {
2420 __ shll(first_reg, imm);
2421 } else if (op->IsShr()) {
2422 __ sarl(first_reg, imm);
2423 } else {
2424 __ shrl(first_reg, imm);
2425 }
2426 }
2427 break;
2428 }
2429 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002430 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002431 DCHECK_EQ(ECX, second_reg);
2432 if (op->IsShl()) {
2433 GenerateShlLong(first, second_reg);
2434 } else if (op->IsShr()) {
2435 GenerateShrLong(first, second_reg);
2436 } else {
2437 GenerateUShrLong(first, second_reg);
2438 }
2439 break;
2440 }
2441 default:
2442 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2443 }
2444}
2445
2446void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2447 Label done;
2448 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2449 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2450 __ testl(shifter, Immediate(32));
2451 __ j(kEqual, &done);
2452 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2453 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2454 __ Bind(&done);
2455}
2456
2457void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2458 Label done;
2459 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2460 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2461 __ testl(shifter, Immediate(32));
2462 __ j(kEqual, &done);
2463 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2464 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2465 __ Bind(&done);
2466}
2467
2468void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2469 Label done;
2470 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2471 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2472 __ testl(shifter, Immediate(32));
2473 __ j(kEqual, &done);
2474 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2475 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2476 __ Bind(&done);
2477}
2478
2479void LocationsBuilderX86::VisitShl(HShl* shl) {
2480 HandleShift(shl);
2481}
2482
2483void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2484 HandleShift(shl);
2485}
2486
2487void LocationsBuilderX86::VisitShr(HShr* shr) {
2488 HandleShift(shr);
2489}
2490
2491void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2492 HandleShift(shr);
2493}
2494
2495void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2496 HandleShift(ushr);
2497}
2498
2499void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2500 HandleShift(ushr);
2501}
2502
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002503void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002504 LocationSummary* locations =
2505 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002506 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002507 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002508 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2509 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002510}
2511
2512void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2513 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002514 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002515 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002516
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002517 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002518
Nicolas Geoffray39468442014-09-02 15:17:15 +01002519 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002520 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002521}
2522
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002523void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2524 LocationSummary* locations =
2525 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2526 locations->SetOut(Location::RegisterLocation(EAX));
2527 InvokeRuntimeCallingConvention calling_convention;
2528 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002529 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2530 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002531}
2532
2533void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2534 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002535 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002536 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2537
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002538 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002539
2540 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2541 DCHECK(!codegen_->IsLeafMethod());
2542}
2543
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002544void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002545 LocationSummary* locations =
2546 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002547 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2548 if (location.IsStackSlot()) {
2549 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2550 } else if (location.IsDoubleStackSlot()) {
2551 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002552 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002553 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002554}
2555
2556void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002557 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002558}
2559
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002560void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002561 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002562 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002563 locations->SetInAt(0, Location::RequiresRegister());
2564 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002565}
2566
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002567void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2568 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002569 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002570 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002571 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002572 switch (not_->InputAt(0)->GetType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002573 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002574 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002575 break;
2576
2577 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002578 __ notl(out.AsRegisterPairLow<Register>());
2579 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002580 break;
2581
2582 default:
2583 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2584 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002585}
2586
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002587void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002588 LocationSummary* locations =
2589 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002590 switch (compare->InputAt(0)->GetType()) {
2591 case Primitive::kPrimLong: {
2592 locations->SetInAt(0, Location::RequiresRegister());
2593 // TODO: we set any here but we don't handle constants
2594 locations->SetInAt(1, Location::Any());
2595 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2596 break;
2597 }
2598 case Primitive::kPrimFloat:
2599 case Primitive::kPrimDouble: {
2600 locations->SetInAt(0, Location::RequiresFpuRegister());
2601 locations->SetInAt(1, Location::RequiresFpuRegister());
2602 locations->SetOut(Location::RequiresRegister());
2603 break;
2604 }
2605 default:
2606 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2607 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002608}
2609
2610void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002611 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002612 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002613 Location left = locations->InAt(0);
2614 Location right = locations->InAt(1);
2615
2616 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002617 switch (compare->InputAt(0)->GetType()) {
2618 case Primitive::kPrimLong: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002619 if (right.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002620 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002621 } else {
2622 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002623 __ cmpl(left.AsRegisterPairHigh<Register>(),
2624 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002625 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002626 __ j(kLess, &less); // Signed compare.
2627 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002628 if (right.IsRegisterPair()) {
2629 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002630 } else {
2631 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002632 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002633 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002634 break;
2635 }
2636 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002637 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002638 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
2639 break;
2640 }
2641 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002642 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002643 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002644 break;
2645 }
2646 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002647 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002648 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002649 __ movl(out, Immediate(0));
2650 __ j(kEqual, &done);
2651 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
2652
2653 __ Bind(&greater);
2654 __ movl(out, Immediate(1));
2655 __ jmp(&done);
2656
2657 __ Bind(&less);
2658 __ movl(out, Immediate(-1));
2659
2660 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002661}
2662
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002663void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002664 LocationSummary* locations =
2665 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002666 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2667 locations->SetInAt(i, Location::Any());
2668 }
2669 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002670}
2671
2672void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002673 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002674 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002675}
2676
Calin Juravle52c48962014-12-16 17:02:57 +00002677void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
2678 /*
2679 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2680 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2681 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2682 */
2683 switch (kind) {
2684 case MemBarrierKind::kAnyAny: {
2685 __ mfence();
2686 break;
2687 }
2688 case MemBarrierKind::kAnyStore:
2689 case MemBarrierKind::kLoadAny:
2690 case MemBarrierKind::kStoreStore: {
2691 // nop
2692 break;
2693 }
2694 default:
2695 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002696 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002697}
2698
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002699
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002700void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002701 Label is_null;
2702 __ testl(value, value);
2703 __ j(kEqual, &is_null);
2704 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2705 __ movl(temp, object);
2706 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002707 __ movb(Address(temp, card, TIMES_1, 0),
2708 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002709 __ Bind(&is_null);
2710}
2711
Calin Juravle52c48962014-12-16 17:02:57 +00002712void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
2713 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002714 LocationSummary* locations =
2715 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002716 locations->SetInAt(0, Location::RequiresRegister());
2717 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle52c48962014-12-16 17:02:57 +00002718
2719 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
2720 // Long values can be loaded atomically into an XMM using movsd.
2721 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
2722 // and then copy the XMM into the output 32bits at a time).
2723 locations->AddTemp(Location::RequiresFpuRegister());
2724 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002725}
2726
Calin Juravle52c48962014-12-16 17:02:57 +00002727void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
2728 const FieldInfo& field_info) {
2729 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002730
Calin Juravle52c48962014-12-16 17:02:57 +00002731 LocationSummary* locations = instruction->GetLocations();
2732 Register base = locations->InAt(0).AsRegister<Register>();
2733 Location out = locations->Out();
2734 bool is_volatile = field_info.IsVolatile();
2735 Primitive::Type field_type = field_info.GetFieldType();
2736 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2737
2738 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002739 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00002740 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002741 break;
2742 }
2743
2744 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002745 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002746 break;
2747 }
2748
2749 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00002750 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002751 break;
2752 }
2753
2754 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002755 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002756 break;
2757 }
2758
2759 case Primitive::kPrimInt:
2760 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002761 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002762 break;
2763 }
2764
2765 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00002766 if (is_volatile) {
2767 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2768 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002769 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002770 __ movd(out.AsRegisterPairLow<Register>(), temp);
2771 __ psrlq(temp, Immediate(32));
2772 __ movd(out.AsRegisterPairHigh<Register>(), temp);
2773 } else {
2774 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002775 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002776 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
2777 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002778 break;
2779 }
2780
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002781 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002782 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002783 break;
2784 }
2785
2786 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002787 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002788 break;
2789 }
2790
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002791 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00002792 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002793 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002794 }
Calin Juravle52c48962014-12-16 17:02:57 +00002795
Calin Juravle77520bc2015-01-12 18:45:46 +00002796 // Longs are handled in the switch.
2797 if (field_type != Primitive::kPrimLong) {
2798 codegen_->MaybeRecordImplicitNullCheck(instruction);
2799 }
2800
Calin Juravle52c48962014-12-16 17:02:57 +00002801 if (is_volatile) {
2802 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2803 }
2804}
2805
2806void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
2807 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2808
2809 LocationSummary* locations =
2810 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2811 locations->SetInAt(0, Location::RequiresRegister());
2812 bool is_volatile = field_info.IsVolatile();
2813 Primitive::Type field_type = field_info.GetFieldType();
2814 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2815 || (field_type == Primitive::kPrimByte);
2816
2817 // The register allocator does not support multiple
2818 // inputs that die at entry with one in a specific register.
2819 if (is_byte_type) {
2820 // Ensure the value is in a byte register.
2821 locations->SetInAt(1, Location::RegisterLocation(EAX));
2822 } else {
2823 locations->SetInAt(1, Location::RequiresRegister());
2824 }
2825 // Temporary registers for the write barrier.
2826 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2827 locations->AddTemp(Location::RequiresRegister());
2828 // Ensure the card is in a byte register.
2829 locations->AddTemp(Location::RegisterLocation(ECX));
2830 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
2831 // 64bits value can be atomically written to an address with movsd and an XMM register.
2832 // We need two XMM registers because there's no easier way to (bit) copy a register pair
2833 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
2834 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
2835 // isolated cases when we need this it isn't worth adding the extra complexity.
2836 locations->AddTemp(Location::RequiresFpuRegister());
2837 locations->AddTemp(Location::RequiresFpuRegister());
2838 }
2839}
2840
2841void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
2842 const FieldInfo& field_info) {
2843 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2844
2845 LocationSummary* locations = instruction->GetLocations();
2846 Register base = locations->InAt(0).AsRegister<Register>();
2847 Location value = locations->InAt(1);
2848 bool is_volatile = field_info.IsVolatile();
2849 Primitive::Type field_type = field_info.GetFieldType();
2850 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2851
2852 if (is_volatile) {
2853 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2854 }
2855
2856 switch (field_type) {
2857 case Primitive::kPrimBoolean:
2858 case Primitive::kPrimByte: {
2859 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
2860 break;
2861 }
2862
2863 case Primitive::kPrimShort:
2864 case Primitive::kPrimChar: {
2865 __ movw(Address(base, offset), value.AsRegister<Register>());
2866 break;
2867 }
2868
2869 case Primitive::kPrimInt:
2870 case Primitive::kPrimNot: {
2871 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00002872 break;
2873 }
2874
2875 case Primitive::kPrimLong: {
2876 if (is_volatile) {
2877 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2878 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
2879 __ movd(temp1, value.AsRegisterPairLow<Register>());
2880 __ movd(temp2, value.AsRegisterPairHigh<Register>());
2881 __ punpckldq(temp1, temp2);
2882 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00002883 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002884 } else {
2885 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00002886 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002887 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
2888 }
2889 break;
2890 }
2891
2892 case Primitive::kPrimFloat: {
2893 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
2894 break;
2895 }
2896
2897 case Primitive::kPrimDouble: {
2898 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
2899 break;
2900 }
2901
2902 case Primitive::kPrimVoid:
2903 LOG(FATAL) << "Unreachable type " << field_type;
2904 UNREACHABLE();
2905 }
2906
Calin Juravle77520bc2015-01-12 18:45:46 +00002907 // Longs are handled in the switch.
2908 if (field_type != Primitive::kPrimLong) {
2909 codegen_->MaybeRecordImplicitNullCheck(instruction);
2910 }
2911
2912 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2913 Register temp = locations->GetTemp(0).AsRegister<Register>();
2914 Register card = locations->GetTemp(1).AsRegister<Register>();
2915 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
2916 }
2917
Calin Juravle52c48962014-12-16 17:02:57 +00002918 if (is_volatile) {
2919 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2920 }
2921}
2922
2923void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2924 HandleFieldGet(instruction, instruction->GetFieldInfo());
2925}
2926
2927void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2928 HandleFieldGet(instruction, instruction->GetFieldInfo());
2929}
2930
2931void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2932 HandleFieldSet(instruction, instruction->GetFieldInfo());
2933}
2934
2935void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2936 HandleFieldSet(instruction, instruction->GetFieldInfo());
2937}
2938
2939void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2940 HandleFieldSet(instruction, instruction->GetFieldInfo());
2941}
2942
2943void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2944 HandleFieldSet(instruction, instruction->GetFieldInfo());
2945}
2946
2947void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2948 HandleFieldGet(instruction, instruction->GetFieldInfo());
2949}
2950
2951void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2952 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002953}
2954
2955void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002956 LocationSummary* locations =
2957 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002958 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
2959 ? Location::RequiresRegister()
2960 : Location::Any();
2961 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002962 if (instruction->HasUses()) {
2963 locations->SetOut(Location::SameAsFirstInput());
2964 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002965}
2966
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002967void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002968 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2969 return;
2970 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002971 LocationSummary* locations = instruction->GetLocations();
2972 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002973
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002974 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
2975 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2976}
2977
2978void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002979 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002980 codegen_->AddSlowPath(slow_path);
2981
2982 LocationSummary* locations = instruction->GetLocations();
2983 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002984
2985 if (obj.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002986 __ cmpl(obj.AsRegister<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002987 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002988 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002989 } else {
2990 DCHECK(obj.IsConstant()) << obj;
2991 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2992 __ jmp(slow_path->GetEntryLabel());
2993 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002994 }
2995 __ j(kEqual, slow_path->GetEntryLabel());
2996}
2997
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002998void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
2999 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3000 GenerateImplicitNullCheck(instruction);
3001 } else {
3002 GenerateExplicitNullCheck(instruction);
3003 }
3004}
3005
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003006void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003007 LocationSummary* locations =
3008 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003009 locations->SetInAt(0, Location::RequiresRegister());
3010 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3011 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003012}
3013
3014void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3015 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003016 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003017 Location index = locations->InAt(1);
3018
Calin Juravle77520bc2015-01-12 18:45:46 +00003019 Primitive::Type type = instruction->GetType();
3020 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003021 case Primitive::kPrimBoolean: {
3022 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003023 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003024 if (index.IsConstant()) {
3025 __ movzxb(out, Address(obj,
3026 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3027 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003028 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003029 }
3030 break;
3031 }
3032
3033 case Primitive::kPrimByte: {
3034 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003035 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003036 if (index.IsConstant()) {
3037 __ movsxb(out, Address(obj,
3038 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3039 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003040 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003041 }
3042 break;
3043 }
3044
3045 case Primitive::kPrimShort: {
3046 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003047 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003048 if (index.IsConstant()) {
3049 __ movsxw(out, Address(obj,
3050 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3051 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003052 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003053 }
3054 break;
3055 }
3056
3057 case Primitive::kPrimChar: {
3058 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003059 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003060 if (index.IsConstant()) {
3061 __ movzxw(out, Address(obj,
3062 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3063 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003064 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003065 }
3066 break;
3067 }
3068
3069 case Primitive::kPrimInt:
3070 case Primitive::kPrimNot: {
3071 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003072 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003073 if (index.IsConstant()) {
3074 __ movl(out, Address(obj,
3075 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3076 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003077 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003078 }
3079 break;
3080 }
3081
3082 case Primitive::kPrimLong: {
3083 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003084 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003085 if (index.IsConstant()) {
3086 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003087 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003088 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003089 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003090 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003091 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003092 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003093 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003094 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003095 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003096 }
3097 break;
3098 }
3099
Mark Mendell7c8d0092015-01-26 11:21:33 -05003100 case Primitive::kPrimFloat: {
3101 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3102 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3103 if (index.IsConstant()) {
3104 __ movss(out, Address(obj,
3105 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3106 } else {
3107 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3108 }
3109 break;
3110 }
3111
3112 case Primitive::kPrimDouble: {
3113 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3114 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3115 if (index.IsConstant()) {
3116 __ movsd(out, Address(obj,
3117 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3118 } else {
3119 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3120 }
3121 break;
3122 }
3123
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003124 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003125 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003126 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003127 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003128
3129 if (type != Primitive::kPrimLong) {
3130 codegen_->MaybeRecordImplicitNullCheck(instruction);
3131 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003132}
3133
3134void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003135 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003136 bool needs_write_barrier =
3137 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3138
3139 DCHECK(kFollowsQuickABI);
3140 bool not_enough_registers = needs_write_barrier
3141 && !instruction->GetValue()->IsConstant()
3142 && !instruction->GetIndex()->IsConstant();
3143 bool needs_runtime_call = instruction->NeedsTypeCheck() || not_enough_registers;
3144
Nicolas Geoffray39468442014-09-02 15:17:15 +01003145 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3146 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003147 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003148
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003149 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003150 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003151 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3152 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3153 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003154 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003155 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3156 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003157 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003158 // In case of a byte operation, the register allocator does not support multiple
3159 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003160 locations->SetInAt(0, Location::RequiresRegister());
3161 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003162 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003163 // Ensure the value is in a byte register.
3164 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003165 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003166 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003167 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003168 // Temporary registers for the write barrier.
3169 if (needs_write_barrier) {
3170 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003171 // Ensure the card is in a byte register.
3172 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003173 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003174 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003175}
3176
3177void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3178 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003179 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003180 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003181 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003182 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003183 bool needs_runtime_call = locations->WillCall();
3184 bool needs_write_barrier =
3185 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003186
3187 switch (value_type) {
3188 case Primitive::kPrimBoolean:
3189 case Primitive::kPrimByte: {
3190 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003191 if (index.IsConstant()) {
3192 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003193 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003194 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003195 } else {
3196 __ movb(Address(obj, offset),
3197 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3198 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003199 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003200 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003201 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003202 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003203 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003204 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003205 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3206 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003207 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003208 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003209 break;
3210 }
3211
3212 case Primitive::kPrimShort:
3213 case Primitive::kPrimChar: {
3214 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003215 if (index.IsConstant()) {
3216 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003217 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003218 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003219 } else {
3220 __ movw(Address(obj, offset),
3221 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3222 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003223 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003224 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003225 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3226 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003227 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003228 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003229 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3230 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003231 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003232 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003233 break;
3234 }
3235
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003236 case Primitive::kPrimInt:
3237 case Primitive::kPrimNot: {
3238 if (!needs_runtime_call) {
3239 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3240 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003241 size_t offset =
3242 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003243 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003244 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003245 } else {
3246 DCHECK(value.IsConstant()) << value;
3247 __ movl(Address(obj, offset),
3248 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3249 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003250 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003251 DCHECK(index.IsRegister()) << index;
3252 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003253 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3254 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003255 } else {
3256 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003257 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003258 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3259 }
3260 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003261 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003262
3263 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003264 Register temp = locations->GetTemp(0).AsRegister<Register>();
3265 Register card = locations->GetTemp(1).AsRegister<Register>();
3266 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003267 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003268 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003269 DCHECK_EQ(value_type, Primitive::kPrimNot);
3270 DCHECK(!codegen_->IsLeafMethod());
3271 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3272 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003273 }
3274 break;
3275 }
3276
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003277 case Primitive::kPrimLong: {
3278 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003279 if (index.IsConstant()) {
3280 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003281 if (value.IsRegisterPair()) {
3282 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003283 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003284 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003285 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003286 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003287 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3288 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003289 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003290 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3291 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003292 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003293 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003294 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003295 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003296 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003297 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003298 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003299 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003300 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003301 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003302 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003303 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003304 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003305 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003306 Immediate(High32Bits(val)));
3307 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003308 }
3309 break;
3310 }
3311
Mark Mendell7c8d0092015-01-26 11:21:33 -05003312 case Primitive::kPrimFloat: {
3313 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3314 DCHECK(value.IsFpuRegister());
3315 if (index.IsConstant()) {
3316 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3317 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3318 } else {
3319 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3320 value.AsFpuRegister<XmmRegister>());
3321 }
3322 break;
3323 }
3324
3325 case Primitive::kPrimDouble: {
3326 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3327 DCHECK(value.IsFpuRegister());
3328 if (index.IsConstant()) {
3329 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3330 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3331 } else {
3332 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3333 value.AsFpuRegister<XmmRegister>());
3334 }
3335 break;
3336 }
3337
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003338 case Primitive::kPrimVoid:
3339 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003340 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003341 }
3342}
3343
3344void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3345 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003346 locations->SetInAt(0, Location::RequiresRegister());
3347 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003348 instruction->SetLocations(locations);
3349}
3350
3351void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3352 LocationSummary* locations = instruction->GetLocations();
3353 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003354 Register obj = locations->InAt(0).AsRegister<Register>();
3355 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003356 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003357 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003358}
3359
3360void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003361 LocationSummary* locations =
3362 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003363 locations->SetInAt(0, Location::RequiresRegister());
3364 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003365 if (instruction->HasUses()) {
3366 locations->SetOut(Location::SameAsFirstInput());
3367 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003368}
3369
3370void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3371 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003372 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01003373 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003374 codegen_->AddSlowPath(slow_path);
3375
Roland Levillain271ab9c2014-11-27 15:23:57 +00003376 Register index = locations->InAt(0).AsRegister<Register>();
3377 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003378
3379 __ cmpl(index, length);
3380 __ j(kAboveEqual, slow_path->GetEntryLabel());
3381}
3382
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003383void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3384 temp->SetLocations(nullptr);
3385}
3386
3387void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3388 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003389 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003390}
3391
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003392void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003393 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003394 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003395}
3396
3397void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003398 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3399}
3400
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003401void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3402 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3403}
3404
3405void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003406 HBasicBlock* block = instruction->GetBlock();
3407 if (block->GetLoopInformation() != nullptr) {
3408 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3409 // The back edge will generate the suspend check.
3410 return;
3411 }
3412 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3413 // The goto will generate the suspend check.
3414 return;
3415 }
3416 GenerateSuspendCheck(instruction, nullptr);
3417}
3418
3419void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3420 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003421 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003422 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003423 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003424 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003425 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003426 if (successor == nullptr) {
3427 __ j(kNotEqual, slow_path->GetEntryLabel());
3428 __ Bind(slow_path->GetReturnLabel());
3429 } else {
3430 __ j(kEqual, codegen_->GetLabelOf(successor));
3431 __ jmp(slow_path->GetEntryLabel());
3432 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003433}
3434
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003435X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3436 return codegen_->GetAssembler();
3437}
3438
Mark Mendell7c8d0092015-01-26 11:21:33 -05003439void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003440 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003441 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003442 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003443 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05003444 __ movl(temp_reg, Address(ESP, src + stack_offset));
3445 __ movl(Address(ESP, dst + stack_offset), temp_reg);
3446}
3447
3448void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
3449 ScratchRegisterScope ensure_scratch(
3450 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3451 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3452 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3453 __ movl(temp_reg, Address(ESP, src + stack_offset));
3454 __ movl(Address(ESP, dst + stack_offset), temp_reg);
3455 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
3456 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003457}
3458
3459void ParallelMoveResolverX86::EmitMove(size_t index) {
3460 MoveOperands* move = moves_.Get(index);
3461 Location source = move->GetSource();
3462 Location destination = move->GetDestination();
3463
3464 if (source.IsRegister()) {
3465 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003466 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003467 } else {
3468 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003469 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003470 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003471 } else if (source.IsFpuRegister()) {
3472 if (destination.IsFpuRegister()) {
3473 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3474 } else if (destination.IsStackSlot()) {
3475 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3476 } else {
3477 DCHECK(destination.IsDoubleStackSlot());
3478 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3479 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003480 } else if (source.IsStackSlot()) {
3481 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003482 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003483 } else if (destination.IsFpuRegister()) {
3484 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003485 } else {
3486 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003487 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
3488 }
3489 } else if (source.IsDoubleStackSlot()) {
3490 if (destination.IsFpuRegister()) {
3491 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
3492 } else {
3493 DCHECK(destination.IsDoubleStackSlot()) << destination;
3494 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003495 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003496 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003497 HConstant* constant = source.GetConstant();
3498 if (constant->IsIntConstant()) {
3499 Immediate imm(constant->AsIntConstant()->GetValue());
3500 if (destination.IsRegister()) {
3501 __ movl(destination.AsRegister<Register>(), imm);
3502 } else {
3503 DCHECK(destination.IsStackSlot()) << destination;
3504 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3505 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003506 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003507 DCHECK(constant->IsFloatConstant());
3508 float value = constant->AsFloatConstant()->GetValue();
3509 Immediate imm(bit_cast<float, int32_t>(value));
3510 if (destination.IsFpuRegister()) {
3511 ScratchRegisterScope ensure_scratch(
3512 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3513 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
3514 __ movl(temp, imm);
3515 __ movd(destination.AsFpuRegister<XmmRegister>(), temp);
3516 } else {
3517 DCHECK(destination.IsStackSlot()) << destination;
3518 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3519 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003520 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003521 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003522 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003523 }
3524}
3525
3526void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003527 Register suggested_scratch = reg == EAX ? EBX : EAX;
3528 ScratchRegisterScope ensure_scratch(
3529 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3530
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003531 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3532 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
3533 __ movl(Address(ESP, mem + stack_offset), reg);
3534 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
3535}
3536
Mark Mendell7c8d0092015-01-26 11:21:33 -05003537void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
3538 ScratchRegisterScope ensure_scratch(
3539 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3540
3541 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3542 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3543 __ movl(temp_reg, Address(ESP, mem + stack_offset));
3544 __ movss(Address(ESP, mem + stack_offset), reg);
3545 __ movd(reg, temp_reg);
3546}
3547
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003548void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
3549 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003550 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3551
3552 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003553 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003554 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3555
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003556 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
3557 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
3558 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
3559 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
3560 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
3561 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
3562}
3563
3564void ParallelMoveResolverX86::EmitSwap(size_t index) {
3565 MoveOperands* move = moves_.Get(index);
3566 Location source = move->GetSource();
3567 Location destination = move->GetDestination();
3568
3569 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003570 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003571 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003572 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003573 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003574 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003575 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3576 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003577 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
3578 // Use XOR Swap algorithm to avoid a temporary.
3579 DCHECK_NE(source.reg(), destination.reg());
3580 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3581 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3582 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3583 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
3584 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
3585 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
3586 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003587 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003588 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003589 }
3590}
3591
3592void ParallelMoveResolverX86::SpillScratch(int reg) {
3593 __ pushl(static_cast<Register>(reg));
3594}
3595
3596void ParallelMoveResolverX86::RestoreScratch(int reg) {
3597 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003598}
3599
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003600void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003601 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3602 ? LocationSummary::kCallOnSlowPath
3603 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003604 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003605 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003606 locations->SetOut(Location::RequiresRegister());
3607}
3608
3609void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003610 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003611 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003612 DCHECK(!cls->CanCallRuntime());
3613 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003614 codegen_->LoadCurrentMethod(out);
3615 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3616 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003617 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003618 codegen_->LoadCurrentMethod(out);
3619 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3620 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003621
3622 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3623 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3624 codegen_->AddSlowPath(slow_path);
3625 __ testl(out, out);
3626 __ j(kEqual, slow_path->GetEntryLabel());
3627 if (cls->MustGenerateClinitCheck()) {
3628 GenerateClassInitializationCheck(slow_path, out);
3629 } else {
3630 __ Bind(slow_path->GetExitLabel());
3631 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003632 }
3633}
3634
3635void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
3636 LocationSummary* locations =
3637 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3638 locations->SetInAt(0, Location::RequiresRegister());
3639 if (check->HasUses()) {
3640 locations->SetOut(Location::SameAsFirstInput());
3641 }
3642}
3643
3644void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003645 // We assume the class to not be null.
3646 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3647 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003648 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003649 GenerateClassInitializationCheck(slow_path,
3650 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003651}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003652
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003653void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
3654 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003655 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3656 Immediate(mirror::Class::kStatusInitialized));
3657 __ j(kLess, slow_path->GetEntryLabel());
3658 __ Bind(slow_path->GetExitLabel());
3659 // No need for memory fence, thanks to the X86 memory model.
3660}
3661
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003662void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
3663 LocationSummary* locations =
3664 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3665 locations->SetOut(Location::RequiresRegister());
3666}
3667
3668void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
3669 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
3670 codegen_->AddSlowPath(slow_path);
3671
Roland Levillain271ab9c2014-11-27 15:23:57 +00003672 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003673 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08003674 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3675 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003676 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3677 __ testl(out, out);
3678 __ j(kEqual, slow_path->GetEntryLabel());
3679 __ Bind(slow_path->GetExitLabel());
3680}
3681
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003682void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
3683 LocationSummary* locations =
3684 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3685 locations->SetOut(Location::RequiresRegister());
3686}
3687
3688void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
3689 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003690 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003691 __ fs()->movl(address, Immediate(0));
3692}
3693
3694void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
3695 LocationSummary* locations =
3696 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3697 InvokeRuntimeCallingConvention calling_convention;
3698 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3699}
3700
3701void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
3702 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
3703 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3704}
3705
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003706void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003707 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3708 ? LocationSummary::kNoCall
3709 : LocationSummary::kCallOnSlowPath;
3710 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3711 locations->SetInAt(0, Location::RequiresRegister());
3712 locations->SetInAt(1, Location::Any());
3713 locations->SetOut(Location::RequiresRegister());
3714}
3715
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003716void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003717 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003718 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003719 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003720 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003721 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3722 Label done, zero;
3723 SlowPathCodeX86* slow_path = nullptr;
3724
3725 // Return 0 if `obj` is null.
3726 // TODO: avoid this check if we know obj is not null.
3727 __ testl(obj, obj);
3728 __ j(kEqual, &zero);
3729 __ movl(out, Address(obj, class_offset));
3730 // Compare the class of `obj` with `cls`.
3731 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003732 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003733 } else {
3734 DCHECK(cls.IsStackSlot()) << cls;
3735 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
3736 }
3737
3738 if (instruction->IsClassFinal()) {
3739 // Classes must be equal for the instanceof to succeed.
3740 __ j(kNotEqual, &zero);
3741 __ movl(out, Immediate(1));
3742 __ jmp(&done);
3743 } else {
3744 // If the classes are not equal, we go into a slow path.
3745 DCHECK(locations->OnlyCallsOnSlowPath());
3746 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003747 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003748 codegen_->AddSlowPath(slow_path);
3749 __ j(kNotEqual, slow_path->GetEntryLabel());
3750 __ movl(out, Immediate(1));
3751 __ jmp(&done);
3752 }
3753 __ Bind(&zero);
3754 __ movl(out, Immediate(0));
3755 if (slow_path != nullptr) {
3756 __ Bind(slow_path->GetExitLabel());
3757 }
3758 __ Bind(&done);
3759}
3760
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003761void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
3762 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3763 instruction, LocationSummary::kCallOnSlowPath);
3764 locations->SetInAt(0, Location::RequiresRegister());
3765 locations->SetInAt(1, Location::Any());
3766 locations->AddTemp(Location::RequiresRegister());
3767}
3768
3769void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
3770 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003771 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003772 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003773 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003774 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3775 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
3776 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3777 codegen_->AddSlowPath(slow_path);
3778
3779 // TODO: avoid this check if we know obj is not null.
3780 __ testl(obj, obj);
3781 __ j(kEqual, slow_path->GetExitLabel());
3782 __ movl(temp, Address(obj, class_offset));
3783
3784 // Compare the class of `obj` with `cls`.
3785 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003786 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003787 } else {
3788 DCHECK(cls.IsStackSlot()) << cls;
3789 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
3790 }
3791
3792 __ j(kNotEqual, slow_path->GetEntryLabel());
3793 __ Bind(slow_path->GetExitLabel());
3794}
3795
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003796void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3797 LocationSummary* locations =
3798 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3799 InvokeRuntimeCallingConvention calling_convention;
3800 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3801}
3802
3803void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3804 __ fs()->call(Address::Absolute(instruction->IsEnter()
3805 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
3806 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
3807 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3808}
3809
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003810void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3811void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3812void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3813
3814void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3815 LocationSummary* locations =
3816 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3817 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3818 || instruction->GetResultType() == Primitive::kPrimLong);
3819 locations->SetInAt(0, Location::RequiresRegister());
3820 locations->SetInAt(1, Location::Any());
3821 locations->SetOut(Location::SameAsFirstInput());
3822}
3823
3824void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
3825 HandleBitwiseOperation(instruction);
3826}
3827
3828void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
3829 HandleBitwiseOperation(instruction);
3830}
3831
3832void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
3833 HandleBitwiseOperation(instruction);
3834}
3835
3836void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3837 LocationSummary* locations = instruction->GetLocations();
3838 Location first = locations->InAt(0);
3839 Location second = locations->InAt(1);
3840 DCHECK(first.Equals(locations->Out()));
3841
3842 if (instruction->GetResultType() == Primitive::kPrimInt) {
3843 if (second.IsRegister()) {
3844 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003845 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003846 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003847 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003848 } else {
3849 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003850 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003851 }
3852 } else if (second.IsConstant()) {
3853 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003854 __ andl(first.AsRegister<Register>(),
3855 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003856 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003857 __ orl(first.AsRegister<Register>(),
3858 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003859 } else {
3860 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00003861 __ xorl(first.AsRegister<Register>(),
3862 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003863 }
3864 } else {
3865 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003866 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003867 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003868 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003869 } else {
3870 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003871 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003872 }
3873 }
3874 } else {
3875 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3876 if (second.IsRegisterPair()) {
3877 if (instruction->IsAnd()) {
3878 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3879 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3880 } else if (instruction->IsOr()) {
3881 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3882 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3883 } else {
3884 DCHECK(instruction->IsXor());
3885 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3886 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3887 }
3888 } else {
3889 if (instruction->IsAnd()) {
3890 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3891 __ andl(first.AsRegisterPairHigh<Register>(),
3892 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3893 } else if (instruction->IsOr()) {
3894 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3895 __ orl(first.AsRegisterPairHigh<Register>(),
3896 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3897 } else {
3898 DCHECK(instruction->IsXor());
3899 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3900 __ xorl(first.AsRegisterPairHigh<Register>(),
3901 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3902 }
3903 }
3904 }
3905}
3906
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003907} // namespace x86
3908} // namespace art