blob: 8a73eb4d7c049fb4c3c67b7fdc89acc7a9a49737 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
19#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000020#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010021#include "gc/accounting/card_table.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070022#include "mirror/array-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010023#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010024#include "mirror/class.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010025#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000026#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010027#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000028#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010029#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010032
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033namespace x86 {
34
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010035static constexpr int kCurrentMethodStackOffset = 0;
36
Calin Juravled6fb6cf2014-11-11 19:07:44 +000037static constexpr Register kRuntimeParameterCoreRegisters[] = { EAX, ECX, EDX, EBX };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010038static constexpr size_t kRuntimeParameterCoreRegistersLength =
39 arraysize(kRuntimeParameterCoreRegisters);
Mark P Mendell966c3ae2015-01-27 15:45:27 +000040static constexpr XmmRegister kRuntimeParameterFpuRegisters[] = { XMM0, XMM1, XMM2, XMM3 };
41static constexpr size_t kRuntimeParameterFpuRegistersLength =
42 arraysize(kRuntimeParameterFpuRegisters);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010043
Mark Mendell24f2dfa2015-01-14 19:51:45 -050044static constexpr int kC2ConditionMask = 0x400;
45
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000046// Marker for places that can be updated once we don't follow the quick ABI.
47static constexpr bool kFollowsQuickABI = true;
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000048static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000049
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010050class InvokeRuntimeCallingConvention : public CallingConvention<Register, XmmRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010051 public:
52 InvokeRuntimeCallingConvention()
53 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010054 kRuntimeParameterCoreRegistersLength,
55 kRuntimeParameterFpuRegisters,
56 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010057
58 private:
59 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
60};
61
Nicolas Geoffraye5038322014-07-04 09:41:32 +010062#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
63
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010064class SlowPathCodeX86 : public SlowPathCode {
65 public:
66 SlowPathCodeX86() : entry_label_(), exit_label_() {}
67
68 Label* GetEntryLabel() { return &entry_label_; }
69 Label* GetExitLabel() { return &exit_label_; }
70
71 private:
72 Label entry_label_;
73 Label exit_label_;
74
75 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86);
76};
77
78class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010080 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010081
82 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
83 __ Bind(GetEntryLabel());
84 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010085 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010086 }
87
88 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010089 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010090 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
91};
92
Calin Juravled0d48522014-11-04 16:40:20 +000093class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
94 public:
95 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
96
97 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
98 __ Bind(GetEntryLabel());
99 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
100 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
101 }
102
103 private:
104 HDivZeroCheck* const instruction_;
105 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
106};
107
Calin Juravlebacfec32014-11-14 15:54:36 +0000108class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +0000109 public:
Calin Juravlebacfec32014-11-14 15:54:36 +0000110 explicit DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000111
112 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
113 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000114 if (is_div_) {
115 __ negl(reg_);
116 } else {
117 __ movl(reg_, Immediate(0));
118 }
Calin Juravled0d48522014-11-04 16:40:20 +0000119 __ jmp(GetExitLabel());
120 }
121
122 private:
123 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000124 bool is_div_;
125 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000126};
127
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100128class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100129 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100130 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
131 Location index_location,
132 Location length_location)
Roland Levillain199f3362014-11-27 17:15:16 +0000133 : instruction_(instruction),
134 index_location_(index_location),
135 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100136
137 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100138 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100139 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000140 // We're moving two locations to locations that could overlap, so we need a parallel
141 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100142 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000143 x86_codegen->EmitParallelMoves(
144 index_location_,
145 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
146 length_location_,
147 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100148 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100149 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100150 }
151
152 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100153 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100154 const Location index_location_;
155 const Location length_location_;
156
157 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
158};
159
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100160class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000161 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100162 explicit SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
163 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000164
165 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100166 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000167 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100168 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000169 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
170 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100171 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100172 if (successor_ == nullptr) {
173 __ jmp(GetReturnLabel());
174 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100175 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100176 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000177 }
178
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100179 Label* GetReturnLabel() {
180 DCHECK(successor_ == nullptr);
181 return &return_label_;
182 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000183
184 private:
185 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100186 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000187 Label return_label_;
188
189 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
190};
191
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000192class LoadStringSlowPathX86 : public SlowPathCodeX86 {
193 public:
194 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
195
196 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
197 LocationSummary* locations = instruction_->GetLocations();
198 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
199
200 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
201 __ Bind(GetEntryLabel());
202 codegen->SaveLiveRegisters(locations);
203
204 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800205 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
206 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000207 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
208 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
209 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
210 codegen->RestoreLiveRegisters(locations);
211
212 __ jmp(GetExitLabel());
213 }
214
215 private:
216 HLoadString* const instruction_;
217
218 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
219};
220
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000221class LoadClassSlowPathX86 : public SlowPathCodeX86 {
222 public:
223 LoadClassSlowPathX86(HLoadClass* cls,
224 HInstruction* at,
225 uint32_t dex_pc,
226 bool do_clinit)
227 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
228 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
229 }
230
231 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
232 LocationSummary* locations = at_->GetLocations();
233 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
234 __ Bind(GetEntryLabel());
235 codegen->SaveLiveRegisters(locations);
236
237 InvokeRuntimeCallingConvention calling_convention;
238 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
239 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
240 __ fs()->call(Address::Absolute(do_clinit_
241 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
242 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
243 codegen->RecordPcInfo(at_, dex_pc_);
244
245 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000246 Location out = locations->Out();
247 if (out.IsValid()) {
248 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
249 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000250 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000251
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000252 codegen->RestoreLiveRegisters(locations);
253 __ jmp(GetExitLabel());
254 }
255
256 private:
257 // The class this slow path will load.
258 HLoadClass* const cls_;
259
260 // The instruction where this slow path is happening.
261 // (Might be the load class or an initialization check).
262 HInstruction* const at_;
263
264 // The dex PC of `at_`.
265 const uint32_t dex_pc_;
266
267 // Whether to initialize the class.
268 const bool do_clinit_;
269
270 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
271};
272
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000273class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
274 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000275 TypeCheckSlowPathX86(HInstruction* instruction,
276 Location class_to_check,
277 Location object_class,
278 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000279 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000280 class_to_check_(class_to_check),
281 object_class_(object_class),
282 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000283
284 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
285 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000286 DCHECK(instruction_->IsCheckCast()
287 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000288
289 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
290 __ Bind(GetEntryLabel());
291 codegen->SaveLiveRegisters(locations);
292
293 // We're moving two locations to locations that could overlap, so we need a parallel
294 // move resolver.
295 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000296 x86_codegen->EmitParallelMoves(
297 class_to_check_,
298 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
299 object_class_,
300 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000301
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000302 if (instruction_->IsInstanceOf()) {
Roland Levillain199f3362014-11-27 17:15:16 +0000303 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize,
304 pInstanceofNonTrivial)));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000305 } else {
306 DCHECK(instruction_->IsCheckCast());
307 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
308 }
309
310 codegen->RecordPcInfo(instruction_, dex_pc_);
311 if (instruction_->IsInstanceOf()) {
312 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
313 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000314 codegen->RestoreLiveRegisters(locations);
315
316 __ jmp(GetExitLabel());
317 }
318
319 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000320 HInstruction* const instruction_;
321 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000322 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000323 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324
325 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
326};
327
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100328#undef __
329#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
330
Dave Allison20dfc792014-06-16 20:44:29 -0700331inline Condition X86Condition(IfCondition cond) {
332 switch (cond) {
333 case kCondEQ: return kEqual;
334 case kCondNE: return kNotEqual;
335 case kCondLT: return kLess;
336 case kCondLE: return kLessEqual;
337 case kCondGT: return kGreater;
338 case kCondGE: return kGreaterEqual;
339 default:
340 LOG(FATAL) << "Unknown if condition";
341 }
342 return kEqual;
343}
344
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100345void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
346 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
347}
348
349void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
350 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
351}
352
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100353size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
354 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
355 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100356}
357
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100358size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
359 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
360 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100361}
362
Mark Mendell7c8d0092015-01-26 11:21:33 -0500363size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
364 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
365 return GetFloatingPointSpillSlotSize();
366}
367
368size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
369 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
370 return GetFloatingPointSpillSlotSize();
371}
372
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000373CodeGeneratorX86::CodeGeneratorX86(HGraph* graph, const CompilerOptions& compiler_options)
374 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfXmmRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000375 kNumberOfRegisterPairs, (1 << kFakeReturnRegister), 0, compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100376 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100377 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100378 instruction_visitor_(graph, this),
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000379 move_resolver_(graph->GetArena(), this) {
380 // Use a fake return address register to mimic Quick.
381 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100382}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100383
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100384Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100385 switch (type) {
386 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100387 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100388 X86ManagedRegister pair =
389 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100390 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
391 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100392 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
393 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100394 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100395 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100396 }
397
398 case Primitive::kPrimByte:
399 case Primitive::kPrimBoolean:
400 case Primitive::kPrimChar:
401 case Primitive::kPrimShort:
402 case Primitive::kPrimInt:
403 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100404 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100405 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100406 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100407 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
408 X86ManagedRegister current =
409 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
410 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100411 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100412 }
413 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100414 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100415 }
416
417 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100418 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100419 return Location::FpuRegisterLocation(
420 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100421 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100422
423 case Primitive::kPrimVoid:
424 LOG(FATAL) << "Unreachable type " << type;
425 }
426
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100427 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100428}
429
Nicolas Geoffray98893962015-01-21 12:32:32 +0000430void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline ATTRIBUTE_UNUSED) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100431 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100432 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100433
434 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100435 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100436
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000437 // TODO: We currently don't use Quick's callee saved registers.
438 DCHECK(kFollowsQuickABI);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100439 blocked_core_registers_[EBP] = true;
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000440 blocked_core_registers_[ESI] = true;
441 blocked_core_registers_[EDI] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100442
443 UpdateBlockedPairRegisters();
444}
445
446void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
447 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
448 X86ManagedRegister current =
449 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
450 if (blocked_core_registers_[current.AsRegisterPairLow()]
451 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
452 blocked_register_pairs_[i] = true;
453 }
454 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100455}
456
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100457InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
458 : HGraphVisitor(graph),
459 assembler_(codegen->GetAssembler()),
460 codegen_(codegen) {}
461
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000462void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000463 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000464 bool skip_overflow_check =
465 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000466 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000467
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000468 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100469 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100470 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100471 }
472
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000473 if (!HasEmptyFrame()) {
474 __ subl(ESP, Immediate(GetFrameSize() - FrameEntrySpillSize()));
475 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
476 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000477}
478
479void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000480 if (!HasEmptyFrame()) {
481 __ addl(ESP, Immediate(GetFrameSize() - FrameEntrySpillSize()));
482 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000483}
484
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100485void CodeGeneratorX86::Bind(HBasicBlock* block) {
486 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000487}
488
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100489void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000490 DCHECK(RequiresCurrentMethod());
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100491 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000492}
493
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100494Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
495 switch (load->GetType()) {
496 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100497 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100498 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
499 break;
500
501 case Primitive::kPrimInt:
502 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100503 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100504 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100505
506 case Primitive::kPrimBoolean:
507 case Primitive::kPrimByte:
508 case Primitive::kPrimChar:
509 case Primitive::kPrimShort:
510 case Primitive::kPrimVoid:
511 LOG(FATAL) << "Unexpected type " << load->GetType();
512 }
513
514 LOG(FATAL) << "Unreachable";
515 return Location();
516}
517
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100518Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
519 switch (type) {
520 case Primitive::kPrimBoolean:
521 case Primitive::kPrimByte:
522 case Primitive::kPrimChar:
523 case Primitive::kPrimShort:
524 case Primitive::kPrimInt:
525 case Primitive::kPrimNot: {
526 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000527 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100528 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100529 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100530 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000531 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100532 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100533 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100534
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000535 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100536 uint32_t index = gp_index_;
537 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000538 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100539 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100540 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
541 calling_convention.GetRegisterPairAt(index));
542 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100543 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000544 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
545 }
546 }
547
548 case Primitive::kPrimFloat: {
549 uint32_t index = fp_index_++;
550 stack_index_++;
551 if (index < calling_convention.GetNumberOfFpuRegisters()) {
552 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
553 } else {
554 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
555 }
556 }
557
558 case Primitive::kPrimDouble: {
559 uint32_t index = fp_index_++;
560 stack_index_ += 2;
561 if (index < calling_convention.GetNumberOfFpuRegisters()) {
562 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
563 } else {
564 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100565 }
566 }
567
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100568 case Primitive::kPrimVoid:
569 LOG(FATAL) << "Unexpected parameter type " << type;
570 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100571 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100572 return Location();
573}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100574
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100575void CodeGeneratorX86::Move32(Location destination, Location source) {
576 if (source.Equals(destination)) {
577 return;
578 }
579 if (destination.IsRegister()) {
580 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000581 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100582 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000583 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100584 } else {
585 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000586 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100587 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100588 } else if (destination.IsFpuRegister()) {
589 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000590 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100591 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000592 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100593 } else {
594 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000595 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100596 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100597 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000598 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100599 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000600 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100601 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000602 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500603 } else if (source.IsConstant()) {
604 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000605 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500606 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100607 } else {
608 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100609 __ pushl(Address(ESP, source.GetStackIndex()));
610 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100611 }
612 }
613}
614
615void CodeGeneratorX86::Move64(Location destination, Location source) {
616 if (source.Equals(destination)) {
617 return;
618 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100619 if (destination.IsRegisterPair()) {
620 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000621 EmitParallelMoves(
622 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
623 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
624 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
625 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100626 } else if (source.IsFpuRegister()) {
627 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100628 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000629 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100630 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100631 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
632 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100633 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
634 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100635 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500636 if (source.IsFpuRegister()) {
637 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
638 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000639 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100640 } else {
641 LOG(FATAL) << "Unimplemented";
642 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100643 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000644 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100645 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000646 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100647 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100648 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100649 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100650 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000651 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100652 } else {
653 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000654 EmitParallelMoves(
655 Location::StackSlot(source.GetStackIndex()),
656 Location::StackSlot(destination.GetStackIndex()),
657 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
658 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100659 }
660 }
661}
662
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100663void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000664 LocationSummary* locations = instruction->GetLocations();
665 if (locations != nullptr && locations->Out().Equals(location)) {
666 return;
667 }
668
669 if (locations != nullptr && locations->Out().IsConstant()) {
670 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000671 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
672 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000673 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000674 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000675 } else if (location.IsStackSlot()) {
676 __ movl(Address(ESP, location.GetStackIndex()), imm);
677 } else {
678 DCHECK(location.IsConstant());
679 DCHECK_EQ(location.GetConstant(), const_to_move);
680 }
681 } else if (const_to_move->IsLongConstant()) {
682 int64_t value = const_to_move->AsLongConstant()->GetValue();
683 if (location.IsRegisterPair()) {
684 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
685 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
686 } else if (location.IsDoubleStackSlot()) {
687 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000688 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
689 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000690 } else {
691 DCHECK(location.IsConstant());
692 DCHECK_EQ(location.GetConstant(), instruction);
693 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100694 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000695 } else if (instruction->IsTemporary()) {
696 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000697 if (temp_location.IsStackSlot()) {
698 Move32(location, temp_location);
699 } else {
700 DCHECK(temp_location.IsDoubleStackSlot());
701 Move64(location, temp_location);
702 }
Roland Levillain476df552014-10-09 17:51:36 +0100703 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100704 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100705 switch (instruction->GetType()) {
706 case Primitive::kPrimBoolean:
707 case Primitive::kPrimByte:
708 case Primitive::kPrimChar:
709 case Primitive::kPrimShort:
710 case Primitive::kPrimInt:
711 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100712 case Primitive::kPrimFloat:
713 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100714 break;
715
716 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100717 case Primitive::kPrimDouble:
718 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100719 break;
720
721 default:
722 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
723 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000724 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100725 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100726 switch (instruction->GetType()) {
727 case Primitive::kPrimBoolean:
728 case Primitive::kPrimByte:
729 case Primitive::kPrimChar:
730 case Primitive::kPrimShort:
731 case Primitive::kPrimInt:
732 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100733 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000734 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100735 break;
736
737 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100738 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000739 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100740 break;
741
742 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100743 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100744 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000745 }
746}
747
748void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000749 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000750}
751
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000752void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000753 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100754 DCHECK(!successor->IsExitBlock());
755
756 HBasicBlock* block = got->GetBlock();
757 HInstruction* previous = got->GetPrevious();
758
759 HLoopInformation* info = block->GetLoopInformation();
760 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
761 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
762 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
763 return;
764 }
765
766 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
767 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
768 }
769 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000770 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000771 }
772}
773
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000774void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000775 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000776}
777
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000778void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700779 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000780 if (kIsDebugBuild) {
781 __ Comment("Unreachable");
782 __ int3();
783 }
784}
785
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000786void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100787 LocationSummary* locations =
788 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100789 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100790 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100791 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100792 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000793}
794
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000795void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700796 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100797 if (cond->IsIntConstant()) {
798 // Constant condition, statically compared against 1.
799 int32_t cond_value = cond->AsIntConstant()->GetValue();
800 if (cond_value == 1) {
801 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
802 if_instr->IfTrueSuccessor())) {
803 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100804 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100805 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100806 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100807 DCHECK_EQ(cond_value, 0);
808 }
809 } else {
810 bool materialized =
811 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
812 // Moves do not affect the eflags register, so if the condition is
813 // evaluated just before the if, we don't need to evaluate it
814 // again.
815 bool eflags_set = cond->IsCondition()
816 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
817 if (materialized) {
818 if (!eflags_set) {
819 // Materialized condition, compare against 0.
820 Location lhs = if_instr->GetLocations()->InAt(0);
821 if (lhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000822 __ cmpl(lhs.AsRegister<Register>(), Immediate(0));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100823 } else {
824 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
825 }
826 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
827 } else {
828 __ j(X86Condition(cond->AsCondition()->GetCondition()),
829 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
830 }
831 } else {
832 Location lhs = cond->GetLocations()->InAt(0);
833 Location rhs = cond->GetLocations()->InAt(1);
834 // LHS is guaranteed to be in a register (see
835 // LocationsBuilderX86::VisitCondition).
836 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000837 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100838 } else if (rhs.IsConstant()) {
839 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
840 Immediate imm(instruction->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000841 __ cmpl(lhs.AsRegister<Register>(), imm);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100842 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000843 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100844 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100845 __ j(X86Condition(cond->AsCondition()->GetCondition()),
846 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700847 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100848 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100849 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
850 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700851 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000852 }
853}
854
855void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000856 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000857}
858
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000859void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
860 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000861}
862
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000863void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100864 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000865}
866
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000867void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100868 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700869 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000870}
871
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100872void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100873 LocationSummary* locations =
874 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100875 switch (store->InputAt(1)->GetType()) {
876 case Primitive::kPrimBoolean:
877 case Primitive::kPrimByte:
878 case Primitive::kPrimChar:
879 case Primitive::kPrimShort:
880 case Primitive::kPrimInt:
881 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100882 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100883 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
884 break;
885
886 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100887 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100888 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
889 break;
890
891 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100892 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100893 }
894 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000895}
896
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000897void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700898 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000899}
900
Dave Allison20dfc792014-06-16 20:44:29 -0700901void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100902 LocationSummary* locations =
903 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100904 locations->SetInAt(0, Location::RequiresRegister());
905 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100906 if (comp->NeedsMaterialization()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000907 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100908 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000909}
910
Dave Allison20dfc792014-06-16 20:44:29 -0700911void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
912 if (comp->NeedsMaterialization()) {
913 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000914 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100915 // Clear register: setcc only sets the low byte.
916 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700917 if (locations->InAt(1).IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000918 __ cmpl(locations->InAt(0).AsRegister<Register>(),
919 locations->InAt(1).AsRegister<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100920 } else if (locations->InAt(1).IsConstant()) {
921 HConstant* instruction = locations->InAt(1).GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000922 Immediate imm(CodeGenerator::GetInt32ValueOf(instruction));
Roland Levillain271ab9c2014-11-27 15:23:57 +0000923 __ cmpl(locations->InAt(0).AsRegister<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700924 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000925 __ cmpl(locations->InAt(0).AsRegister<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700926 Address(ESP, locations->InAt(1).GetStackIndex()));
927 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000928 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100929 }
Dave Allison20dfc792014-06-16 20:44:29 -0700930}
931
932void LocationsBuilderX86::VisitEqual(HEqual* comp) {
933 VisitCondition(comp);
934}
935
936void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
937 VisitCondition(comp);
938}
939
940void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
941 VisitCondition(comp);
942}
943
944void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
945 VisitCondition(comp);
946}
947
948void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
949 VisitCondition(comp);
950}
951
952void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
953 VisitCondition(comp);
954}
955
956void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
957 VisitCondition(comp);
958}
959
960void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
961 VisitCondition(comp);
962}
963
964void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
965 VisitCondition(comp);
966}
967
968void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
969 VisitCondition(comp);
970}
971
972void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
973 VisitCondition(comp);
974}
975
976void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
977 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000978}
979
980void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100981 LocationSummary* locations =
982 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100983 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000984}
985
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000986void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100987 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700988 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000989}
990
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000991void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
992 LocationSummary* locations =
993 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
994 locations->SetOut(Location::ConstantLocation(constant));
995}
996
997void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
998 // Will be generated at use site.
999 UNUSED(constant);
1000}
1001
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001002void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001003 LocationSummary* locations =
1004 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001005 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001006}
1007
1008void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1009 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001010 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001011}
1012
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001013void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1014 LocationSummary* locations =
1015 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1016 locations->SetOut(Location::ConstantLocation(constant));
1017}
1018
1019void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1020 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001021 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001022}
1023
1024void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1025 LocationSummary* locations =
1026 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1027 locations->SetOut(Location::ConstantLocation(constant));
1028}
1029
1030void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1031 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001032 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001033}
1034
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001035void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001036 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001037}
1038
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001039void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001040 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001041 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001042 __ ret();
1043}
1044
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001045void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001046 LocationSummary* locations =
1047 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001048 switch (ret->InputAt(0)->GetType()) {
1049 case Primitive::kPrimBoolean:
1050 case Primitive::kPrimByte:
1051 case Primitive::kPrimChar:
1052 case Primitive::kPrimShort:
1053 case Primitive::kPrimInt:
1054 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001055 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001056 break;
1057
1058 case Primitive::kPrimLong:
1059 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001060 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001061 break;
1062
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001063 case Primitive::kPrimFloat:
1064 case Primitive::kPrimDouble:
1065 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001066 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001067 break;
1068
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001069 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001070 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001071 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001072}
1073
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001074void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001075 if (kIsDebugBuild) {
1076 switch (ret->InputAt(0)->GetType()) {
1077 case Primitive::kPrimBoolean:
1078 case Primitive::kPrimByte:
1079 case Primitive::kPrimChar:
1080 case Primitive::kPrimShort:
1081 case Primitive::kPrimInt:
1082 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001083 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001084 break;
1085
1086 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001087 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1088 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001089 break;
1090
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001091 case Primitive::kPrimFloat:
1092 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001093 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001094 break;
1095
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001096 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001097 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001098 }
1099 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001100 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001101 __ ret();
1102}
1103
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001104void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001105 HandleInvoke(invoke);
1106}
1107
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001108void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001109 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001110
1111 // TODO: Implement all kinds of calls:
1112 // 1) boot -> boot
1113 // 2) app -> boot
1114 // 3) app -> app
1115 //
1116 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1117
1118 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001119 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001120 if (!invoke->IsRecursive()) {
1121 // temp = temp->dex_cache_resolved_methods_;
1122 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
1123 // temp = temp[index_in_cache]
1124 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
1125 // (temp + offset_of_quick_compiled_code)()
1126 __ call(Address(
1127 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
1128 } else {
1129 __ call(codegen_->GetFrameEntryLabel());
1130 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001131
1132 DCHECK(!codegen_->IsLeafMethod());
1133 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1134}
1135
1136void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1137 HandleInvoke(invoke);
1138}
1139
1140void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001141 LocationSummary* locations =
1142 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001143 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001144
1145 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001146 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001147 HInstruction* input = invoke->InputAt(i);
1148 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1149 }
1150
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001151 switch (invoke->GetType()) {
1152 case Primitive::kPrimBoolean:
1153 case Primitive::kPrimByte:
1154 case Primitive::kPrimChar:
1155 case Primitive::kPrimShort:
1156 case Primitive::kPrimInt:
1157 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001158 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001159 break;
1160
1161 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001162 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001163 break;
1164
1165 case Primitive::kPrimVoid:
1166 break;
1167
1168 case Primitive::kPrimDouble:
1169 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001170 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001171 break;
1172 }
1173
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001174 invoke->SetLocations(locations);
1175}
1176
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001177void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001178 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001179 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1180 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1181 LocationSummary* locations = invoke->GetLocations();
1182 Location receiver = locations->InAt(0);
1183 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1184 // temp = object->GetClass();
1185 if (receiver.IsStackSlot()) {
1186 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1187 __ movl(temp, Address(temp, class_offset));
1188 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001189 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001190 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001191 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001192 // temp = temp->GetMethodAt(method_offset);
1193 __ movl(temp, Address(temp, method_offset));
1194 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001195 __ call(Address(
1196 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001197
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001198 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001199 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001200}
1201
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001202void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1203 HandleInvoke(invoke);
1204 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001205 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001206}
1207
1208void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1209 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001210 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001211 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1212 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1213 LocationSummary* locations = invoke->GetLocations();
1214 Location receiver = locations->InAt(0);
1215 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1216
1217 // Set the hidden argument.
1218 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001219 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001220
1221 // temp = object->GetClass();
1222 if (receiver.IsStackSlot()) {
1223 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1224 __ movl(temp, Address(temp, class_offset));
1225 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001226 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001227 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001228 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001229 // temp = temp->GetImtEntryAt(method_offset);
1230 __ movl(temp, Address(temp, method_offset));
1231 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001232 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001233 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001234
1235 DCHECK(!codegen_->IsLeafMethod());
1236 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1237}
1238
Roland Levillain88cb1752014-10-20 16:36:47 +01001239void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1240 LocationSummary* locations =
1241 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1242 switch (neg->GetResultType()) {
1243 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001244 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001245 locations->SetInAt(0, Location::RequiresRegister());
1246 locations->SetOut(Location::SameAsFirstInput());
1247 break;
1248
Roland Levillain88cb1752014-10-20 16:36:47 +01001249 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001250 locations->SetInAt(0, Location::RequiresFpuRegister());
1251 locations->SetOut(Location::SameAsFirstInput());
1252 locations->AddTemp(Location::RequiresRegister());
1253 locations->AddTemp(Location::RequiresFpuRegister());
1254 break;
1255
Roland Levillain88cb1752014-10-20 16:36:47 +01001256 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001257 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001258 locations->SetOut(Location::SameAsFirstInput());
1259 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001260 break;
1261
1262 default:
1263 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1264 }
1265}
1266
1267void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1268 LocationSummary* locations = neg->GetLocations();
1269 Location out = locations->Out();
1270 Location in = locations->InAt(0);
1271 switch (neg->GetResultType()) {
1272 case Primitive::kPrimInt:
1273 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001274 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001275 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001276 break;
1277
1278 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001279 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001280 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001281 __ negl(out.AsRegisterPairLow<Register>());
1282 // Negation is similar to subtraction from zero. The least
1283 // significant byte triggers a borrow when it is different from
1284 // zero; to take it into account, add 1 to the most significant
1285 // byte if the carry flag (CF) is set to 1 after the first NEGL
1286 // operation.
1287 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1288 __ negl(out.AsRegisterPairHigh<Register>());
1289 break;
1290
Roland Levillain5368c212014-11-27 15:03:41 +00001291 case Primitive::kPrimFloat: {
1292 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001293 Register constant = locations->GetTemp(0).AsRegister<Register>();
1294 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001295 // Implement float negation with an exclusive or with value
1296 // 0x80000000 (mask for bit 31, representing the sign of a
1297 // single-precision floating-point number).
1298 __ movl(constant, Immediate(INT32_C(0x80000000)));
1299 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001300 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001301 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001302 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001303
Roland Levillain5368c212014-11-27 15:03:41 +00001304 case Primitive::kPrimDouble: {
1305 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001306 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001307 // Implement double negation with an exclusive or with value
1308 // 0x8000000000000000 (mask for bit 63, representing the sign of
1309 // a double-precision floating-point number).
1310 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001311 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001312 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001313 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001314
1315 default:
1316 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1317 }
1318}
1319
Roland Levillaindff1f282014-11-05 14:15:05 +00001320void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001321 Primitive::Type result_type = conversion->GetResultType();
1322 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001323 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001324
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001325 // The float-to-long and double-to-long type conversions rely on a
1326 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001327 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001328 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1329 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001330 ? LocationSummary::kCall
1331 : LocationSummary::kNoCall;
1332 LocationSummary* locations =
1333 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1334
Roland Levillaindff1f282014-11-05 14:15:05 +00001335 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001336 case Primitive::kPrimByte:
1337 switch (input_type) {
1338 case Primitive::kPrimShort:
1339 case Primitive::kPrimInt:
1340 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001341 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001342 locations->SetInAt(0, Location::Any());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001343 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1344 break;
1345
1346 default:
1347 LOG(FATAL) << "Unexpected type conversion from " << input_type
1348 << " to " << result_type;
1349 }
1350 break;
1351
Roland Levillain01a8d712014-11-14 16:27:39 +00001352 case Primitive::kPrimShort:
1353 switch (input_type) {
1354 case Primitive::kPrimByte:
1355 case Primitive::kPrimInt:
1356 case Primitive::kPrimChar:
1357 // Processing a Dex `int-to-short' instruction.
1358 locations->SetInAt(0, Location::Any());
1359 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1360 break;
1361
1362 default:
1363 LOG(FATAL) << "Unexpected type conversion from " << input_type
1364 << " to " << result_type;
1365 }
1366 break;
1367
Roland Levillain946e1432014-11-11 17:35:19 +00001368 case Primitive::kPrimInt:
1369 switch (input_type) {
1370 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001371 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001372 locations->SetInAt(0, Location::Any());
1373 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1374 break;
1375
1376 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001377 // Processing a Dex `float-to-int' instruction.
1378 locations->SetInAt(0, Location::RequiresFpuRegister());
1379 locations->SetOut(Location::RequiresRegister());
1380 locations->AddTemp(Location::RequiresFpuRegister());
1381 break;
1382
Roland Levillain946e1432014-11-11 17:35:19 +00001383 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001384 // Processing a Dex `double-to-int' instruction.
1385 locations->SetInAt(0, Location::RequiresFpuRegister());
1386 locations->SetOut(Location::RequiresRegister());
1387 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001388 break;
1389
1390 default:
1391 LOG(FATAL) << "Unexpected type conversion from " << input_type
1392 << " to " << result_type;
1393 }
1394 break;
1395
Roland Levillaindff1f282014-11-05 14:15:05 +00001396 case Primitive::kPrimLong:
1397 switch (input_type) {
1398 case Primitive::kPrimByte:
1399 case Primitive::kPrimShort:
1400 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001401 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001402 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001403 locations->SetInAt(0, Location::RegisterLocation(EAX));
1404 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1405 break;
1406
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001407 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001408 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001409 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001410 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001411 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1412 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1413
Vladimir Marko949c91f2015-01-27 10:48:44 +00001414 // The runtime helper puts the result in EAX, EDX.
1415 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001416 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001417 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001418
1419 default:
1420 LOG(FATAL) << "Unexpected type conversion from " << input_type
1421 << " to " << result_type;
1422 }
1423 break;
1424
Roland Levillain981e4542014-11-14 11:47:14 +00001425 case Primitive::kPrimChar:
1426 switch (input_type) {
1427 case Primitive::kPrimByte:
1428 case Primitive::kPrimShort:
1429 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001430 // Processing a Dex `int-to-char' instruction.
1431 locations->SetInAt(0, Location::Any());
1432 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1433 break;
1434
1435 default:
1436 LOG(FATAL) << "Unexpected type conversion from " << input_type
1437 << " to " << result_type;
1438 }
1439 break;
1440
Roland Levillaindff1f282014-11-05 14:15:05 +00001441 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001442 switch (input_type) {
1443 case Primitive::kPrimByte:
1444 case Primitive::kPrimShort:
1445 case Primitive::kPrimInt:
1446 case Primitive::kPrimChar:
1447 // Processing a Dex `int-to-float' instruction.
1448 locations->SetInAt(0, Location::RequiresRegister());
1449 locations->SetOut(Location::RequiresFpuRegister());
1450 break;
1451
1452 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001453 // Processing a Dex `long-to-float' instruction.
1454 locations->SetInAt(0, Location::RequiresRegister());
1455 locations->SetOut(Location::RequiresFpuRegister());
1456 locations->AddTemp(Location::RequiresFpuRegister());
1457 locations->AddTemp(Location::RequiresFpuRegister());
1458 break;
1459
Roland Levillaincff13742014-11-17 14:32:17 +00001460 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001461 // Processing a Dex `double-to-float' instruction.
1462 locations->SetInAt(0, Location::RequiresFpuRegister());
1463 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001464 break;
1465
1466 default:
1467 LOG(FATAL) << "Unexpected type conversion from " << input_type
1468 << " to " << result_type;
1469 };
1470 break;
1471
Roland Levillaindff1f282014-11-05 14:15:05 +00001472 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001473 switch (input_type) {
1474 case Primitive::kPrimByte:
1475 case Primitive::kPrimShort:
1476 case Primitive::kPrimInt:
1477 case Primitive::kPrimChar:
1478 // Processing a Dex `int-to-double' instruction.
1479 locations->SetInAt(0, Location::RequiresRegister());
1480 locations->SetOut(Location::RequiresFpuRegister());
1481 break;
1482
1483 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001484 // Processing a Dex `long-to-double' instruction.
1485 locations->SetInAt(0, Location::RequiresRegister());
1486 locations->SetOut(Location::RequiresFpuRegister());
1487 locations->AddTemp(Location::RequiresFpuRegister());
1488 locations->AddTemp(Location::RequiresFpuRegister());
1489 break;
1490
Roland Levillaincff13742014-11-17 14:32:17 +00001491 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001492 // Processing a Dex `float-to-double' instruction.
1493 locations->SetInAt(0, Location::RequiresFpuRegister());
1494 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001495 break;
1496
1497 default:
1498 LOG(FATAL) << "Unexpected type conversion from " << input_type
1499 << " to " << result_type;
1500 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001501 break;
1502
1503 default:
1504 LOG(FATAL) << "Unexpected type conversion from " << input_type
1505 << " to " << result_type;
1506 }
1507}
1508
1509void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1510 LocationSummary* locations = conversion->GetLocations();
1511 Location out = locations->Out();
1512 Location in = locations->InAt(0);
1513 Primitive::Type result_type = conversion->GetResultType();
1514 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001515 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001516 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001517 case Primitive::kPrimByte:
1518 switch (input_type) {
1519 case Primitive::kPrimShort:
1520 case Primitive::kPrimInt:
1521 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001522 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001523 if (in.IsRegister()) {
1524 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
1525 } else if (in.IsStackSlot()) {
1526 __ movsxb(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
1527 } else {
1528 DCHECK(in.GetConstant()->IsIntConstant());
1529 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1530 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1531 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001532 break;
1533
1534 default:
1535 LOG(FATAL) << "Unexpected type conversion from " << input_type
1536 << " to " << result_type;
1537 }
1538 break;
1539
Roland Levillain01a8d712014-11-14 16:27:39 +00001540 case Primitive::kPrimShort:
1541 switch (input_type) {
1542 case Primitive::kPrimByte:
1543 case Primitive::kPrimInt:
1544 case Primitive::kPrimChar:
1545 // Processing a Dex `int-to-short' instruction.
1546 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001547 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001548 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001549 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001550 } else {
1551 DCHECK(in.GetConstant()->IsIntConstant());
1552 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001553 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001554 }
1555 break;
1556
1557 default:
1558 LOG(FATAL) << "Unexpected type conversion from " << input_type
1559 << " to " << result_type;
1560 }
1561 break;
1562
Roland Levillain946e1432014-11-11 17:35:19 +00001563 case Primitive::kPrimInt:
1564 switch (input_type) {
1565 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001566 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001567 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001568 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001569 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001570 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001571 } else {
1572 DCHECK(in.IsConstant());
1573 DCHECK(in.GetConstant()->IsLongConstant());
1574 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001575 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001576 }
1577 break;
1578
Roland Levillain3f8f9362014-12-02 17:45:01 +00001579 case Primitive::kPrimFloat: {
1580 // Processing a Dex `float-to-int' instruction.
1581 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1582 Register output = out.AsRegister<Register>();
1583 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1584 Label done, nan;
1585
1586 __ movl(output, Immediate(kPrimIntMax));
1587 // temp = int-to-float(output)
1588 __ cvtsi2ss(temp, output);
1589 // if input >= temp goto done
1590 __ comiss(input, temp);
1591 __ j(kAboveEqual, &done);
1592 // if input == NaN goto nan
1593 __ j(kUnordered, &nan);
1594 // output = float-to-int-truncate(input)
1595 __ cvttss2si(output, input);
1596 __ jmp(&done);
1597 __ Bind(&nan);
1598 // output = 0
1599 __ xorl(output, output);
1600 __ Bind(&done);
1601 break;
1602 }
1603
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001604 case Primitive::kPrimDouble: {
1605 // Processing a Dex `double-to-int' instruction.
1606 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1607 Register output = out.AsRegister<Register>();
1608 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1609 Label done, nan;
1610
1611 __ movl(output, Immediate(kPrimIntMax));
1612 // temp = int-to-double(output)
1613 __ cvtsi2sd(temp, output);
1614 // if input >= temp goto done
1615 __ comisd(input, temp);
1616 __ j(kAboveEqual, &done);
1617 // if input == NaN goto nan
1618 __ j(kUnordered, &nan);
1619 // output = double-to-int-truncate(input)
1620 __ cvttsd2si(output, input);
1621 __ jmp(&done);
1622 __ Bind(&nan);
1623 // output = 0
1624 __ xorl(output, output);
1625 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001626 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001627 }
Roland Levillain946e1432014-11-11 17:35:19 +00001628
1629 default:
1630 LOG(FATAL) << "Unexpected type conversion from " << input_type
1631 << " to " << result_type;
1632 }
1633 break;
1634
Roland Levillaindff1f282014-11-05 14:15:05 +00001635 case Primitive::kPrimLong:
1636 switch (input_type) {
1637 case Primitive::kPrimByte:
1638 case Primitive::kPrimShort:
1639 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001640 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001641 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001642 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1643 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001644 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001645 __ cdq();
1646 break;
1647
1648 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001649 // Processing a Dex `float-to-long' instruction.
1650 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001651 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1652 break;
1653
Roland Levillaindff1f282014-11-05 14:15:05 +00001654 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001655 // Processing a Dex `double-to-long' instruction.
1656 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1657 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001658 break;
1659
1660 default:
1661 LOG(FATAL) << "Unexpected type conversion from " << input_type
1662 << " to " << result_type;
1663 }
1664 break;
1665
Roland Levillain981e4542014-11-14 11:47:14 +00001666 case Primitive::kPrimChar:
1667 switch (input_type) {
1668 case Primitive::kPrimByte:
1669 case Primitive::kPrimShort:
1670 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001671 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1672 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001673 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001674 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001675 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001676 } else {
1677 DCHECK(in.GetConstant()->IsIntConstant());
1678 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001679 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001680 }
1681 break;
1682
1683 default:
1684 LOG(FATAL) << "Unexpected type conversion from " << input_type
1685 << " to " << result_type;
1686 }
1687 break;
1688
Roland Levillaindff1f282014-11-05 14:15:05 +00001689 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001690 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001691 case Primitive::kPrimByte:
1692 case Primitive::kPrimShort:
1693 case Primitive::kPrimInt:
1694 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001695 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001696 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001697 break;
1698
Roland Levillain6d0e4832014-11-27 18:31:21 +00001699 case Primitive::kPrimLong: {
1700 // Processing a Dex `long-to-float' instruction.
1701 Register low = in.AsRegisterPairLow<Register>();
1702 Register high = in.AsRegisterPairHigh<Register>();
1703 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1704 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1705 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1706
1707 // Operations use doubles for precision reasons (each 32-bit
1708 // half of a long fits in the 53-bit mantissa of a double,
1709 // but not in the 24-bit mantissa of a float). This is
1710 // especially important for the low bits. The result is
1711 // eventually converted to float.
1712
1713 // low = low - 2^31 (to prevent bit 31 of `low` to be
1714 // interpreted as a sign bit)
1715 __ subl(low, Immediate(0x80000000));
1716 // temp = int-to-double(high)
1717 __ cvtsi2sd(temp, high);
1718 // temp = temp * 2^32
1719 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
1720 __ mulsd(temp, constant);
1721 // result = int-to-double(low)
1722 __ cvtsi2sd(result, low);
1723 // result = result + 2^31 (restore the original value of `low`)
1724 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
1725 __ addsd(result, constant);
1726 // result = result + temp
1727 __ addsd(result, temp);
1728 // result = double-to-float(result)
1729 __ cvtsd2ss(result, result);
1730 break;
1731 }
1732
Roland Levillaincff13742014-11-17 14:32:17 +00001733 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001734 // Processing a Dex `double-to-float' instruction.
1735 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001736 break;
1737
1738 default:
1739 LOG(FATAL) << "Unexpected type conversion from " << input_type
1740 << " to " << result_type;
1741 };
1742 break;
1743
Roland Levillaindff1f282014-11-05 14:15:05 +00001744 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001745 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001746 case Primitive::kPrimByte:
1747 case Primitive::kPrimShort:
1748 case Primitive::kPrimInt:
1749 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001750 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001751 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001752 break;
1753
Roland Levillain647b9ed2014-11-27 12:06:00 +00001754 case Primitive::kPrimLong: {
1755 // Processing a Dex `long-to-double' instruction.
1756 Register low = in.AsRegisterPairLow<Register>();
1757 Register high = in.AsRegisterPairHigh<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001758 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1759 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1760 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001761
Roland Levillain647b9ed2014-11-27 12:06:00 +00001762 // low = low - 2^31 (to prevent bit 31 of `low` to be
1763 // interpreted as a sign bit)
1764 __ subl(low, Immediate(0x80000000));
1765 // temp = int-to-double(high)
1766 __ cvtsi2sd(temp, high);
1767 // temp = temp * 2^32
Roland Levillain6d0e4832014-11-27 18:31:21 +00001768 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001769 __ mulsd(temp, constant);
1770 // result = int-to-double(low)
1771 __ cvtsi2sd(result, low);
1772 // result = result + 2^31 (restore the original value of `low`)
Roland Levillain6d0e4832014-11-27 18:31:21 +00001773 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001774 __ addsd(result, constant);
1775 // result = result + temp
1776 __ addsd(result, temp);
1777 break;
1778 }
1779
Roland Levillaincff13742014-11-17 14:32:17 +00001780 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001781 // Processing a Dex `float-to-double' instruction.
1782 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001783 break;
1784
1785 default:
1786 LOG(FATAL) << "Unexpected type conversion from " << input_type
1787 << " to " << result_type;
1788 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001789 break;
1790
1791 default:
1792 LOG(FATAL) << "Unexpected type conversion from " << input_type
1793 << " to " << result_type;
1794 }
1795}
1796
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001797void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001798 LocationSummary* locations =
1799 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001800 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001801 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001802 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001803 locations->SetInAt(0, Location::RequiresRegister());
1804 locations->SetInAt(1, Location::Any());
1805 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001806 break;
1807 }
1808
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001809 case Primitive::kPrimFloat:
1810 case Primitive::kPrimDouble: {
1811 locations->SetInAt(0, Location::RequiresFpuRegister());
1812 locations->SetInAt(1, Location::Any());
1813 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001814 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001815 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001816
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001817 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001818 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1819 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001820 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001821}
1822
1823void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1824 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001825 Location first = locations->InAt(0);
1826 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001827 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001828 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001829 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001830 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001831 __ addl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001832 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001833 __ addl(first.AsRegister<Register>(),
1834 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001835 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001836 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001837 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001838 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001839 }
1840
1841 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001842 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001843 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1844 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001845 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001846 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1847 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001848 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001849 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001850 break;
1851 }
1852
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001853 case Primitive::kPrimFloat: {
1854 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001855 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001856 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001857 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001858 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001859 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001860 }
1861
1862 case Primitive::kPrimDouble: {
1863 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001864 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001865 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001866 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001867 }
1868 break;
1869 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001870
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001871 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001872 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001873 }
1874}
1875
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001876void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001877 LocationSummary* locations =
1878 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001879 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001880 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001881 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001882 locations->SetInAt(0, Location::RequiresRegister());
1883 locations->SetInAt(1, Location::Any());
1884 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001885 break;
1886 }
Calin Juravle11351682014-10-23 15:38:15 +01001887 case Primitive::kPrimFloat:
1888 case Primitive::kPrimDouble: {
1889 locations->SetInAt(0, Location::RequiresFpuRegister());
1890 locations->SetInAt(1, Location::RequiresFpuRegister());
1891 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001892 break;
Calin Juravle11351682014-10-23 15:38:15 +01001893 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001894
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001895 default:
Calin Juravle11351682014-10-23 15:38:15 +01001896 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001897 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001898}
1899
1900void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1901 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001902 Location first = locations->InAt(0);
1903 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001904 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001905 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001906 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001907 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001908 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001909 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001910 __ subl(first.AsRegister<Register>(),
1911 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001912 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001913 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001914 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001915 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001916 }
1917
1918 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001919 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01001920 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1921 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001922 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001923 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001924 __ sbbl(first.AsRegisterPairHigh<Register>(),
1925 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001926 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001927 break;
1928 }
1929
Calin Juravle11351682014-10-23 15:38:15 +01001930 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001931 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001932 break;
Calin Juravle11351682014-10-23 15:38:15 +01001933 }
1934
1935 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001936 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001937 break;
1938 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001939
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001940 default:
Calin Juravle11351682014-10-23 15:38:15 +01001941 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001942 }
1943}
1944
Calin Juravle34bacdf2014-10-07 20:23:36 +01001945void LocationsBuilderX86::VisitMul(HMul* mul) {
1946 LocationSummary* locations =
1947 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1948 switch (mul->GetResultType()) {
1949 case Primitive::kPrimInt:
1950 locations->SetInAt(0, Location::RequiresRegister());
1951 locations->SetInAt(1, Location::Any());
1952 locations->SetOut(Location::SameAsFirstInput());
1953 break;
1954 case Primitive::kPrimLong: {
1955 locations->SetInAt(0, Location::RequiresRegister());
1956 // TODO: Currently this handles only stack operands:
1957 // - we don't have enough registers because we currently use Quick ABI.
1958 // - by the time we have a working register allocator we will probably change the ABI
1959 // and fix the above.
1960 // - we don't have a way yet to request operands on stack but the base line compiler
1961 // will leave the operands on the stack with Any().
1962 locations->SetInAt(1, Location::Any());
1963 locations->SetOut(Location::SameAsFirstInput());
1964 // Needed for imul on 32bits with 64bits output.
1965 locations->AddTemp(Location::RegisterLocation(EAX));
1966 locations->AddTemp(Location::RegisterLocation(EDX));
1967 break;
1968 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001969 case Primitive::kPrimFloat:
1970 case Primitive::kPrimDouble: {
1971 locations->SetInAt(0, Location::RequiresFpuRegister());
1972 locations->SetInAt(1, Location::RequiresFpuRegister());
1973 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001974 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001975 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001976
1977 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001978 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001979 }
1980}
1981
1982void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1983 LocationSummary* locations = mul->GetLocations();
1984 Location first = locations->InAt(0);
1985 Location second = locations->InAt(1);
1986 DCHECK(first.Equals(locations->Out()));
1987
1988 switch (mul->GetResultType()) {
1989 case Primitive::kPrimInt: {
1990 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001991 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001992 } else if (second.IsConstant()) {
1993 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001994 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001995 } else {
1996 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001997 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01001998 }
1999 break;
2000 }
2001
2002 case Primitive::kPrimLong: {
2003 DCHECK(second.IsDoubleStackSlot());
2004
2005 Register in1_hi = first.AsRegisterPairHigh<Register>();
2006 Register in1_lo = first.AsRegisterPairLow<Register>();
2007 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2008 Address in2_lo(ESP, second.GetStackIndex());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002009 Register eax = locations->GetTemp(0).AsRegister<Register>();
2010 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002011
2012 DCHECK_EQ(EAX, eax);
2013 DCHECK_EQ(EDX, edx);
2014
2015 // input: in1 - 64 bits, in2 - 64 bits
2016 // output: in1
2017 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2018 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2019 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
2020
2021 __ movl(eax, in2_hi);
2022 // eax <- in1.lo * in2.hi
2023 __ imull(eax, in1_lo);
2024 // in1.hi <- in1.hi * in2.lo
2025 __ imull(in1_hi, in2_lo);
2026 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2027 __ addl(in1_hi, eax);
2028 // move in1_lo to eax to prepare for double precision
2029 __ movl(eax, in1_lo);
2030 // edx:eax <- in1.lo * in2.lo
2031 __ mull(in2_lo);
2032 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2033 __ addl(in1_hi, edx);
2034 // in1.lo <- (in1.lo * in2.lo)[31:0];
2035 __ movl(in1_lo, eax);
2036
2037 break;
2038 }
2039
Calin Juravleb5bfa962014-10-21 18:02:24 +01002040 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002041 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002042 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002043 }
2044
2045 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002046 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002047 break;
2048 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002049
2050 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002051 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002052 }
2053}
2054
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002055void InstructionCodeGeneratorX86::PushOntoFPStack(Location source, uint32_t temp_offset,
2056 uint32_t stack_adjustment, bool is_float) {
2057 if (source.IsStackSlot()) {
2058 DCHECK(is_float);
2059 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2060 } else if (source.IsDoubleStackSlot()) {
2061 DCHECK(!is_float);
2062 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2063 } else {
2064 // Write the value to the temporary location on the stack and load to FP stack.
2065 if (is_float) {
2066 Location stack_temp = Location::StackSlot(temp_offset);
2067 codegen_->Move32(stack_temp, source);
2068 __ flds(Address(ESP, temp_offset));
2069 } else {
2070 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2071 codegen_->Move64(stack_temp, source);
2072 __ fldl(Address(ESP, temp_offset));
2073 }
2074 }
2075}
2076
2077void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2078 Primitive::Type type = rem->GetResultType();
2079 bool is_float = type == Primitive::kPrimFloat;
2080 size_t elem_size = Primitive::ComponentSize(type);
2081 LocationSummary* locations = rem->GetLocations();
2082 Location first = locations->InAt(0);
2083 Location second = locations->InAt(1);
2084 Location out = locations->Out();
2085
2086 // Create stack space for 2 elements.
2087 // TODO: enhance register allocator to ask for stack temporaries.
2088 __ subl(ESP, Immediate(2 * elem_size));
2089
2090 // Load the values to the FP stack in reverse order, using temporaries if needed.
2091 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2092 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2093
2094 // Loop doing FPREM until we stabilize.
2095 Label retry;
2096 __ Bind(&retry);
2097 __ fprem();
2098
2099 // Move FP status to AX.
2100 __ fstsw();
2101
2102 // And see if the argument reduction is complete. This is signaled by the
2103 // C2 FPU flag bit set to 0.
2104 __ andl(EAX, Immediate(kC2ConditionMask));
2105 __ j(kNotEqual, &retry);
2106
2107 // We have settled on the final value. Retrieve it into an XMM register.
2108 // Store FP top of stack to real stack.
2109 if (is_float) {
2110 __ fsts(Address(ESP, 0));
2111 } else {
2112 __ fstl(Address(ESP, 0));
2113 }
2114
2115 // Pop the 2 items from the FP stack.
2116 __ fucompp();
2117
2118 // Load the value from the stack into an XMM register.
2119 DCHECK(out.IsFpuRegister()) << out;
2120 if (is_float) {
2121 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2122 } else {
2123 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2124 }
2125
2126 // And remove the temporary stack space we allocated.
2127 __ addl(ESP, Immediate(2 * elem_size));
2128}
2129
Calin Juravlebacfec32014-11-14 15:54:36 +00002130void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2131 DCHECK(instruction->IsDiv() || instruction->IsRem());
2132
2133 LocationSummary* locations = instruction->GetLocations();
2134 Location out = locations->Out();
2135 Location first = locations->InAt(0);
2136 Location second = locations->InAt(1);
2137 bool is_div = instruction->IsDiv();
2138
2139 switch (instruction->GetResultType()) {
2140 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002141 Register second_reg = second.AsRegister<Register>();
2142 DCHECK_EQ(EAX, first.AsRegister<Register>());
2143 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002144
2145 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002146 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
2147 is_div);
Calin Juravlebacfec32014-11-14 15:54:36 +00002148 codegen_->AddSlowPath(slow_path);
2149
2150 // 0x80000000/-1 triggers an arithmetic exception!
2151 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2152 // it's safe to just use negl instead of more complex comparisons.
2153
2154 __ cmpl(second_reg, Immediate(-1));
2155 __ j(kEqual, slow_path->GetEntryLabel());
2156
2157 // edx:eax <- sign-extended of eax
2158 __ cdq();
2159 // eax = quotient, edx = remainder
2160 __ idivl(second_reg);
2161
2162 __ Bind(slow_path->GetExitLabel());
2163 break;
2164 }
2165
2166 case Primitive::kPrimLong: {
2167 InvokeRuntimeCallingConvention calling_convention;
2168 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2169 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2170 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2171 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2172 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2173 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2174
2175 if (is_div) {
2176 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2177 } else {
2178 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2179 }
2180 uint32_t dex_pc = is_div
2181 ? instruction->AsDiv()->GetDexPc()
2182 : instruction->AsRem()->GetDexPc();
2183 codegen_->RecordPcInfo(instruction, dex_pc);
2184
2185 break;
2186 }
2187
2188 default:
2189 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2190 }
2191}
2192
Calin Juravle7c4954d2014-10-28 16:57:40 +00002193void LocationsBuilderX86::VisitDiv(HDiv* div) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002194 LocationSummary::CallKind call_kind = div->GetResultType() == Primitive::kPrimLong
2195 ? LocationSummary::kCall
2196 : LocationSummary::kNoCall;
2197 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2198
Calin Juravle7c4954d2014-10-28 16:57:40 +00002199 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002200 case Primitive::kPrimInt: {
2201 locations->SetInAt(0, Location::RegisterLocation(EAX));
2202 locations->SetInAt(1, Location::RequiresRegister());
2203 locations->SetOut(Location::SameAsFirstInput());
2204 // Intel uses edx:eax as the dividend.
2205 locations->AddTemp(Location::RegisterLocation(EDX));
2206 break;
2207 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002208 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002209 InvokeRuntimeCallingConvention calling_convention;
2210 locations->SetInAt(0, Location::RegisterPairLocation(
2211 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2212 locations->SetInAt(1, Location::RegisterPairLocation(
2213 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2214 // Runtime helper puts the result in EAX, EDX.
2215 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002216 break;
2217 }
2218 case Primitive::kPrimFloat:
2219 case Primitive::kPrimDouble: {
2220 locations->SetInAt(0, Location::RequiresFpuRegister());
2221 locations->SetInAt(1, Location::RequiresFpuRegister());
2222 locations->SetOut(Location::SameAsFirstInput());
2223 break;
2224 }
2225
2226 default:
2227 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2228 }
2229}
2230
2231void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2232 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002233 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002234 Location first = locations->InAt(0);
2235 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002236
2237 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002238 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002239 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002240 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002241 break;
2242 }
2243
2244 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002245 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002246 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002247 break;
2248 }
2249
2250 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002251 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002252 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002253 break;
2254 }
2255
2256 default:
2257 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2258 }
2259}
2260
Calin Juravlebacfec32014-11-14 15:54:36 +00002261void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002262 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002263 LocationSummary* locations =
2264 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravlebacfec32014-11-14 15:54:36 +00002265
Calin Juravled2ec87d2014-12-08 14:24:46 +00002266 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002267 case Primitive::kPrimInt: {
2268 locations->SetInAt(0, Location::RegisterLocation(EAX));
2269 locations->SetInAt(1, Location::RequiresRegister());
2270 locations->SetOut(Location::RegisterLocation(EDX));
2271 break;
2272 }
2273 case Primitive::kPrimLong: {
2274 InvokeRuntimeCallingConvention calling_convention;
2275 locations->SetInAt(0, Location::RegisterPairLocation(
2276 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2277 locations->SetInAt(1, Location::RegisterPairLocation(
2278 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2279 // Runtime helper puts the result in EAX, EDX.
2280 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2281 break;
2282 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002283 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002284 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002285 locations->SetInAt(0, Location::Any());
2286 locations->SetInAt(1, Location::Any());
2287 locations->SetOut(Location::RequiresFpuRegister());
2288 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002289 break;
2290 }
2291
2292 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002293 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002294 }
2295}
2296
2297void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2298 Primitive::Type type = rem->GetResultType();
2299 switch (type) {
2300 case Primitive::kPrimInt:
2301 case Primitive::kPrimLong: {
2302 GenerateDivRemIntegral(rem);
2303 break;
2304 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002305 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002306 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002307 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002308 break;
2309 }
2310 default:
2311 LOG(FATAL) << "Unexpected rem type " << type;
2312 }
2313}
2314
Calin Juravled0d48522014-11-04 16:40:20 +00002315void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2316 LocationSummary* locations =
2317 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002318 switch (instruction->GetType()) {
2319 case Primitive::kPrimInt: {
2320 locations->SetInAt(0, Location::Any());
2321 break;
2322 }
2323 case Primitive::kPrimLong: {
2324 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2325 if (!instruction->IsConstant()) {
2326 locations->AddTemp(Location::RequiresRegister());
2327 }
2328 break;
2329 }
2330 default:
2331 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2332 }
Calin Juravled0d48522014-11-04 16:40:20 +00002333 if (instruction->HasUses()) {
2334 locations->SetOut(Location::SameAsFirstInput());
2335 }
2336}
2337
2338void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2339 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2340 codegen_->AddSlowPath(slow_path);
2341
2342 LocationSummary* locations = instruction->GetLocations();
2343 Location value = locations->InAt(0);
2344
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002345 switch (instruction->GetType()) {
2346 case Primitive::kPrimInt: {
2347 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002348 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002349 __ j(kEqual, slow_path->GetEntryLabel());
2350 } else if (value.IsStackSlot()) {
2351 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2352 __ j(kEqual, slow_path->GetEntryLabel());
2353 } else {
2354 DCHECK(value.IsConstant()) << value;
2355 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2356 __ jmp(slow_path->GetEntryLabel());
2357 }
2358 }
2359 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002360 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002361 case Primitive::kPrimLong: {
2362 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002363 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002364 __ movl(temp, value.AsRegisterPairLow<Register>());
2365 __ orl(temp, value.AsRegisterPairHigh<Register>());
2366 __ j(kEqual, slow_path->GetEntryLabel());
2367 } else {
2368 DCHECK(value.IsConstant()) << value;
2369 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2370 __ jmp(slow_path->GetEntryLabel());
2371 }
2372 }
2373 break;
2374 }
2375 default:
2376 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002377 }
Calin Juravled0d48522014-11-04 16:40:20 +00002378}
2379
Calin Juravle9aec02f2014-11-18 23:06:35 +00002380void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2381 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2382
2383 LocationSummary* locations =
2384 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2385
2386 switch (op->GetResultType()) {
2387 case Primitive::kPrimInt: {
2388 locations->SetInAt(0, Location::RequiresRegister());
2389 // The shift count needs to be in CL.
2390 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2391 locations->SetOut(Location::SameAsFirstInput());
2392 break;
2393 }
2394 case Primitive::kPrimLong: {
2395 locations->SetInAt(0, Location::RequiresRegister());
2396 // The shift count needs to be in CL.
2397 locations->SetInAt(1, Location::RegisterLocation(ECX));
2398 locations->SetOut(Location::SameAsFirstInput());
2399 break;
2400 }
2401 default:
2402 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2403 }
2404}
2405
2406void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2407 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2408
2409 LocationSummary* locations = op->GetLocations();
2410 Location first = locations->InAt(0);
2411 Location second = locations->InAt(1);
2412 DCHECK(first.Equals(locations->Out()));
2413
2414 switch (op->GetResultType()) {
2415 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002416 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002417 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002418 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002419 DCHECK_EQ(ECX, second_reg);
2420 if (op->IsShl()) {
2421 __ shll(first_reg, second_reg);
2422 } else if (op->IsShr()) {
2423 __ sarl(first_reg, second_reg);
2424 } else {
2425 __ shrl(first_reg, second_reg);
2426 }
2427 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002428 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002429 if (op->IsShl()) {
2430 __ shll(first_reg, imm);
2431 } else if (op->IsShr()) {
2432 __ sarl(first_reg, imm);
2433 } else {
2434 __ shrl(first_reg, imm);
2435 }
2436 }
2437 break;
2438 }
2439 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002440 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002441 DCHECK_EQ(ECX, second_reg);
2442 if (op->IsShl()) {
2443 GenerateShlLong(first, second_reg);
2444 } else if (op->IsShr()) {
2445 GenerateShrLong(first, second_reg);
2446 } else {
2447 GenerateUShrLong(first, second_reg);
2448 }
2449 break;
2450 }
2451 default:
2452 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2453 }
2454}
2455
2456void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2457 Label done;
2458 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2459 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2460 __ testl(shifter, Immediate(32));
2461 __ j(kEqual, &done);
2462 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2463 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2464 __ Bind(&done);
2465}
2466
2467void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2468 Label done;
2469 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2470 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2471 __ testl(shifter, Immediate(32));
2472 __ j(kEqual, &done);
2473 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2474 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2475 __ Bind(&done);
2476}
2477
2478void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2479 Label done;
2480 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2481 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2482 __ testl(shifter, Immediate(32));
2483 __ j(kEqual, &done);
2484 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2485 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2486 __ Bind(&done);
2487}
2488
2489void LocationsBuilderX86::VisitShl(HShl* shl) {
2490 HandleShift(shl);
2491}
2492
2493void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2494 HandleShift(shl);
2495}
2496
2497void LocationsBuilderX86::VisitShr(HShr* shr) {
2498 HandleShift(shr);
2499}
2500
2501void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2502 HandleShift(shr);
2503}
2504
2505void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2506 HandleShift(ushr);
2507}
2508
2509void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2510 HandleShift(ushr);
2511}
2512
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002513void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002514 LocationSummary* locations =
2515 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002516 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002517 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002518 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2519 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002520}
2521
2522void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2523 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002524 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002525 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002526
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002527 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002528
Nicolas Geoffray39468442014-09-02 15:17:15 +01002529 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002530 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002531}
2532
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002533void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2534 LocationSummary* locations =
2535 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2536 locations->SetOut(Location::RegisterLocation(EAX));
2537 InvokeRuntimeCallingConvention calling_convention;
2538 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002539 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2540 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002541}
2542
2543void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2544 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002545 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002546 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2547
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002548 __ fs()->call(Address::Absolute(GetThreadOffset<kX86WordSize>(instruction->GetEntrypoint())));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002549
2550 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2551 DCHECK(!codegen_->IsLeafMethod());
2552}
2553
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002554void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002555 LocationSummary* locations =
2556 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002557 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2558 if (location.IsStackSlot()) {
2559 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2560 } else if (location.IsDoubleStackSlot()) {
2561 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002562 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002563 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002564}
2565
2566void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002567 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002568}
2569
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002570void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002571 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002572 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002573 locations->SetInAt(0, Location::RequiresRegister());
2574 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002575}
2576
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002577void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2578 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002579 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002580 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002581 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002582 switch (not_->InputAt(0)->GetType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002583 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002584 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002585 break;
2586
2587 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002588 __ notl(out.AsRegisterPairLow<Register>());
2589 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002590 break;
2591
2592 default:
2593 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2594 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002595}
2596
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002597void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002598 LocationSummary* locations =
2599 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002600 switch (compare->InputAt(0)->GetType()) {
2601 case Primitive::kPrimLong: {
2602 locations->SetInAt(0, Location::RequiresRegister());
2603 // TODO: we set any here but we don't handle constants
2604 locations->SetInAt(1, Location::Any());
2605 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2606 break;
2607 }
2608 case Primitive::kPrimFloat:
2609 case Primitive::kPrimDouble: {
2610 locations->SetInAt(0, Location::RequiresFpuRegister());
2611 locations->SetInAt(1, Location::RequiresFpuRegister());
2612 locations->SetOut(Location::RequiresRegister());
2613 break;
2614 }
2615 default:
2616 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2617 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002618}
2619
2620void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002621 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002622 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002623 Location left = locations->InAt(0);
2624 Location right = locations->InAt(1);
2625
2626 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002627 switch (compare->InputAt(0)->GetType()) {
2628 case Primitive::kPrimLong: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002629 if (right.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002630 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002631 } else {
2632 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002633 __ cmpl(left.AsRegisterPairHigh<Register>(),
2634 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002635 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002636 __ j(kLess, &less); // Signed compare.
2637 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002638 if (right.IsRegisterPair()) {
2639 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002640 } else {
2641 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002642 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002643 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002644 break;
2645 }
2646 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002647 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002648 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
2649 break;
2650 }
2651 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002652 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002653 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002654 break;
2655 }
2656 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002657 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002658 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002659 __ movl(out, Immediate(0));
2660 __ j(kEqual, &done);
2661 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
2662
2663 __ Bind(&greater);
2664 __ movl(out, Immediate(1));
2665 __ jmp(&done);
2666
2667 __ Bind(&less);
2668 __ movl(out, Immediate(-1));
2669
2670 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002671}
2672
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002673void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002674 LocationSummary* locations =
2675 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002676 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2677 locations->SetInAt(i, Location::Any());
2678 }
2679 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002680}
2681
2682void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002683 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002684 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002685}
2686
Calin Juravle52c48962014-12-16 17:02:57 +00002687void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
2688 /*
2689 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2690 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2691 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2692 */
2693 switch (kind) {
2694 case MemBarrierKind::kAnyAny: {
2695 __ mfence();
2696 break;
2697 }
2698 case MemBarrierKind::kAnyStore:
2699 case MemBarrierKind::kLoadAny:
2700 case MemBarrierKind::kStoreStore: {
2701 // nop
2702 break;
2703 }
2704 default:
2705 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002706 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002707}
2708
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002709
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002710void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002711 Label is_null;
2712 __ testl(value, value);
2713 __ j(kEqual, &is_null);
2714 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2715 __ movl(temp, object);
2716 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002717 __ movb(Address(temp, card, TIMES_1, 0),
2718 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002719 __ Bind(&is_null);
2720}
2721
Calin Juravle52c48962014-12-16 17:02:57 +00002722void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
2723 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002724 LocationSummary* locations =
2725 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002726 locations->SetInAt(0, Location::RequiresRegister());
2727 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle52c48962014-12-16 17:02:57 +00002728
2729 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
2730 // Long values can be loaded atomically into an XMM using movsd.
2731 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
2732 // and then copy the XMM into the output 32bits at a time).
2733 locations->AddTemp(Location::RequiresFpuRegister());
2734 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002735}
2736
Calin Juravle52c48962014-12-16 17:02:57 +00002737void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
2738 const FieldInfo& field_info) {
2739 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002740
Calin Juravle52c48962014-12-16 17:02:57 +00002741 LocationSummary* locations = instruction->GetLocations();
2742 Register base = locations->InAt(0).AsRegister<Register>();
2743 Location out = locations->Out();
2744 bool is_volatile = field_info.IsVolatile();
2745 Primitive::Type field_type = field_info.GetFieldType();
2746 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2747
2748 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002749 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00002750 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002751 break;
2752 }
2753
2754 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002755 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002756 break;
2757 }
2758
2759 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00002760 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002761 break;
2762 }
2763
2764 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002765 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002766 break;
2767 }
2768
2769 case Primitive::kPrimInt:
2770 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002771 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002772 break;
2773 }
2774
2775 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00002776 if (is_volatile) {
2777 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2778 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002779 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002780 __ movd(out.AsRegisterPairLow<Register>(), temp);
2781 __ psrlq(temp, Immediate(32));
2782 __ movd(out.AsRegisterPairHigh<Register>(), temp);
2783 } else {
2784 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002785 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002786 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
2787 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002788 break;
2789 }
2790
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002791 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002792 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002793 break;
2794 }
2795
2796 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002797 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002798 break;
2799 }
2800
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002801 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00002802 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002803 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002804 }
Calin Juravle52c48962014-12-16 17:02:57 +00002805
Calin Juravle77520bc2015-01-12 18:45:46 +00002806 // Longs are handled in the switch.
2807 if (field_type != Primitive::kPrimLong) {
2808 codegen_->MaybeRecordImplicitNullCheck(instruction);
2809 }
2810
Calin Juravle52c48962014-12-16 17:02:57 +00002811 if (is_volatile) {
2812 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2813 }
2814}
2815
2816void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
2817 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2818
2819 LocationSummary* locations =
2820 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2821 locations->SetInAt(0, Location::RequiresRegister());
2822 bool is_volatile = field_info.IsVolatile();
2823 Primitive::Type field_type = field_info.GetFieldType();
2824 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2825 || (field_type == Primitive::kPrimByte);
2826
2827 // The register allocator does not support multiple
2828 // inputs that die at entry with one in a specific register.
2829 if (is_byte_type) {
2830 // Ensure the value is in a byte register.
2831 locations->SetInAt(1, Location::RegisterLocation(EAX));
2832 } else {
2833 locations->SetInAt(1, Location::RequiresRegister());
2834 }
2835 // Temporary registers for the write barrier.
2836 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2837 locations->AddTemp(Location::RequiresRegister());
2838 // Ensure the card is in a byte register.
2839 locations->AddTemp(Location::RegisterLocation(ECX));
2840 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
2841 // 64bits value can be atomically written to an address with movsd and an XMM register.
2842 // We need two XMM registers because there's no easier way to (bit) copy a register pair
2843 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
2844 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
2845 // isolated cases when we need this it isn't worth adding the extra complexity.
2846 locations->AddTemp(Location::RequiresFpuRegister());
2847 locations->AddTemp(Location::RequiresFpuRegister());
2848 }
2849}
2850
2851void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
2852 const FieldInfo& field_info) {
2853 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2854
2855 LocationSummary* locations = instruction->GetLocations();
2856 Register base = locations->InAt(0).AsRegister<Register>();
2857 Location value = locations->InAt(1);
2858 bool is_volatile = field_info.IsVolatile();
2859 Primitive::Type field_type = field_info.GetFieldType();
2860 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2861
2862 if (is_volatile) {
2863 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2864 }
2865
2866 switch (field_type) {
2867 case Primitive::kPrimBoolean:
2868 case Primitive::kPrimByte: {
2869 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
2870 break;
2871 }
2872
2873 case Primitive::kPrimShort:
2874 case Primitive::kPrimChar: {
2875 __ movw(Address(base, offset), value.AsRegister<Register>());
2876 break;
2877 }
2878
2879 case Primitive::kPrimInt:
2880 case Primitive::kPrimNot: {
2881 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00002882 break;
2883 }
2884
2885 case Primitive::kPrimLong: {
2886 if (is_volatile) {
2887 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2888 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
2889 __ movd(temp1, value.AsRegisterPairLow<Register>());
2890 __ movd(temp2, value.AsRegisterPairHigh<Register>());
2891 __ punpckldq(temp1, temp2);
2892 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00002893 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002894 } else {
2895 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00002896 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002897 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
2898 }
2899 break;
2900 }
2901
2902 case Primitive::kPrimFloat: {
2903 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
2904 break;
2905 }
2906
2907 case Primitive::kPrimDouble: {
2908 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
2909 break;
2910 }
2911
2912 case Primitive::kPrimVoid:
2913 LOG(FATAL) << "Unreachable type " << field_type;
2914 UNREACHABLE();
2915 }
2916
Calin Juravle77520bc2015-01-12 18:45:46 +00002917 // Longs are handled in the switch.
2918 if (field_type != Primitive::kPrimLong) {
2919 codegen_->MaybeRecordImplicitNullCheck(instruction);
2920 }
2921
2922 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2923 Register temp = locations->GetTemp(0).AsRegister<Register>();
2924 Register card = locations->GetTemp(1).AsRegister<Register>();
2925 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
2926 }
2927
Calin Juravle52c48962014-12-16 17:02:57 +00002928 if (is_volatile) {
2929 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2930 }
2931}
2932
2933void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2934 HandleFieldGet(instruction, instruction->GetFieldInfo());
2935}
2936
2937void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2938 HandleFieldGet(instruction, instruction->GetFieldInfo());
2939}
2940
2941void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2942 HandleFieldSet(instruction, instruction->GetFieldInfo());
2943}
2944
2945void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2946 HandleFieldSet(instruction, instruction->GetFieldInfo());
2947}
2948
2949void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2950 HandleFieldSet(instruction, instruction->GetFieldInfo());
2951}
2952
2953void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2954 HandleFieldSet(instruction, instruction->GetFieldInfo());
2955}
2956
2957void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2958 HandleFieldGet(instruction, instruction->GetFieldInfo());
2959}
2960
2961void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2962 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002963}
2964
2965void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002966 LocationSummary* locations =
2967 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002968 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
2969 ? Location::RequiresRegister()
2970 : Location::Any();
2971 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002972 if (instruction->HasUses()) {
2973 locations->SetOut(Location::SameAsFirstInput());
2974 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002975}
2976
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002977void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002978 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2979 return;
2980 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002981 LocationSummary* locations = instruction->GetLocations();
2982 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002983
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002984 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
2985 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2986}
2987
2988void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002989 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002990 codegen_->AddSlowPath(slow_path);
2991
2992 LocationSummary* locations = instruction->GetLocations();
2993 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002994
2995 if (obj.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002996 __ cmpl(obj.AsRegister<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002997 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002998 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002999 } else {
3000 DCHECK(obj.IsConstant()) << obj;
3001 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3002 __ jmp(slow_path->GetEntryLabel());
3003 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003004 }
3005 __ j(kEqual, slow_path->GetEntryLabel());
3006}
3007
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003008void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3009 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3010 GenerateImplicitNullCheck(instruction);
3011 } else {
3012 GenerateExplicitNullCheck(instruction);
3013 }
3014}
3015
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003016void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003017 LocationSummary* locations =
3018 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003019 locations->SetInAt(0, Location::RequiresRegister());
3020 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3021 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003022}
3023
3024void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3025 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003026 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003027 Location index = locations->InAt(1);
3028
Calin Juravle77520bc2015-01-12 18:45:46 +00003029 Primitive::Type type = instruction->GetType();
3030 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003031 case Primitive::kPrimBoolean: {
3032 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003033 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003034 if (index.IsConstant()) {
3035 __ movzxb(out, Address(obj,
3036 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3037 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003038 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003039 }
3040 break;
3041 }
3042
3043 case Primitive::kPrimByte: {
3044 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003045 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003046 if (index.IsConstant()) {
3047 __ movsxb(out, Address(obj,
3048 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3049 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003050 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003051 }
3052 break;
3053 }
3054
3055 case Primitive::kPrimShort: {
3056 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003057 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003058 if (index.IsConstant()) {
3059 __ movsxw(out, Address(obj,
3060 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3061 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003062 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003063 }
3064 break;
3065 }
3066
3067 case Primitive::kPrimChar: {
3068 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003069 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003070 if (index.IsConstant()) {
3071 __ movzxw(out, Address(obj,
3072 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3073 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003074 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003075 }
3076 break;
3077 }
3078
3079 case Primitive::kPrimInt:
3080 case Primitive::kPrimNot: {
3081 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003082 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003083 if (index.IsConstant()) {
3084 __ movl(out, Address(obj,
3085 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3086 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003087 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003088 }
3089 break;
3090 }
3091
3092 case Primitive::kPrimLong: {
3093 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003094 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003095 if (index.IsConstant()) {
3096 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003097 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003098 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003099 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003100 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003101 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003102 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003103 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003104 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003105 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003106 }
3107 break;
3108 }
3109
Mark Mendell7c8d0092015-01-26 11:21:33 -05003110 case Primitive::kPrimFloat: {
3111 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3112 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3113 if (index.IsConstant()) {
3114 __ movss(out, Address(obj,
3115 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3116 } else {
3117 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
3118 }
3119 break;
3120 }
3121
3122 case Primitive::kPrimDouble: {
3123 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3124 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
3125 if (index.IsConstant()) {
3126 __ movsd(out, Address(obj,
3127 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
3128 } else {
3129 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
3130 }
3131 break;
3132 }
3133
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003134 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003135 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003136 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003137 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003138
3139 if (type != Primitive::kPrimLong) {
3140 codegen_->MaybeRecordImplicitNullCheck(instruction);
3141 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003142}
3143
3144void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003145 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003146 bool needs_write_barrier =
3147 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3148
3149 DCHECK(kFollowsQuickABI);
3150 bool not_enough_registers = needs_write_barrier
3151 && !instruction->GetValue()->IsConstant()
3152 && !instruction->GetIndex()->IsConstant();
3153 bool needs_runtime_call = instruction->NeedsTypeCheck() || not_enough_registers;
3154
Nicolas Geoffray39468442014-09-02 15:17:15 +01003155 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3156 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003157 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003158
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003159 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003160 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003161 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3162 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3163 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003164 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003165 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3166 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003167 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003168 // In case of a byte operation, the register allocator does not support multiple
3169 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003170 locations->SetInAt(0, Location::RequiresRegister());
3171 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003172 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003173 // Ensure the value is in a byte register.
3174 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003175 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003176 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003177 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003178 // Temporary registers for the write barrier.
3179 if (needs_write_barrier) {
3180 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003181 // Ensure the card is in a byte register.
3182 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003183 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003184 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003185}
3186
3187void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3188 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003189 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003190 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003191 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003192 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003193 bool needs_runtime_call = locations->WillCall();
3194 bool needs_write_barrier =
3195 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003196
3197 switch (value_type) {
3198 case Primitive::kPrimBoolean:
3199 case Primitive::kPrimByte: {
3200 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003201 if (index.IsConstant()) {
3202 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003203 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003204 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003205 } else {
3206 __ movb(Address(obj, offset),
3207 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3208 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003209 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003210 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003211 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003212 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003213 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003214 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003215 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3216 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003217 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003218 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003219 break;
3220 }
3221
3222 case Primitive::kPrimShort:
3223 case Primitive::kPrimChar: {
3224 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003225 if (index.IsConstant()) {
3226 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003227 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003228 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003229 } else {
3230 __ movw(Address(obj, offset),
3231 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3232 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003233 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003234 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003235 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3236 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003237 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003238 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003239 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3240 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003241 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003242 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003243 break;
3244 }
3245
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003246 case Primitive::kPrimInt:
3247 case Primitive::kPrimNot: {
3248 if (!needs_runtime_call) {
3249 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3250 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003251 size_t offset =
3252 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003253 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003254 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003255 } else {
3256 DCHECK(value.IsConstant()) << value;
3257 __ movl(Address(obj, offset),
3258 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3259 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003260 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003261 DCHECK(index.IsRegister()) << index;
3262 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003263 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3264 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003265 } else {
3266 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003267 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003268 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3269 }
3270 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003271 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003272
3273 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003274 Register temp = locations->GetTemp(0).AsRegister<Register>();
3275 Register card = locations->GetTemp(1).AsRegister<Register>();
3276 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003277 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003278 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003279 DCHECK_EQ(value_type, Primitive::kPrimNot);
3280 DCHECK(!codegen_->IsLeafMethod());
3281 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3282 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003283 }
3284 break;
3285 }
3286
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003287 case Primitive::kPrimLong: {
3288 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003289 if (index.IsConstant()) {
3290 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003291 if (value.IsRegisterPair()) {
3292 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003293 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003294 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003295 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003296 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003297 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3298 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003299 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003300 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3301 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003302 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003303 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003304 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003305 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003306 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003307 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003308 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003309 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003310 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003311 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003312 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003313 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003314 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003315 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003316 Immediate(High32Bits(val)));
3317 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003318 }
3319 break;
3320 }
3321
Mark Mendell7c8d0092015-01-26 11:21:33 -05003322 case Primitive::kPrimFloat: {
3323 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
3324 DCHECK(value.IsFpuRegister());
3325 if (index.IsConstant()) {
3326 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
3327 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3328 } else {
3329 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3330 value.AsFpuRegister<XmmRegister>());
3331 }
3332 break;
3333 }
3334
3335 case Primitive::kPrimDouble: {
3336 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
3337 DCHECK(value.IsFpuRegister());
3338 if (index.IsConstant()) {
3339 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
3340 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
3341 } else {
3342 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
3343 value.AsFpuRegister<XmmRegister>());
3344 }
3345 break;
3346 }
3347
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003348 case Primitive::kPrimVoid:
3349 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003350 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003351 }
3352}
3353
3354void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3355 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003356 locations->SetInAt(0, Location::RequiresRegister());
3357 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003358 instruction->SetLocations(locations);
3359}
3360
3361void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3362 LocationSummary* locations = instruction->GetLocations();
3363 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003364 Register obj = locations->InAt(0).AsRegister<Register>();
3365 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003366 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003367 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003368}
3369
3370void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003371 LocationSummary* locations =
3372 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003373 locations->SetInAt(0, Location::RequiresRegister());
3374 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003375 if (instruction->HasUses()) {
3376 locations->SetOut(Location::SameAsFirstInput());
3377 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003378}
3379
3380void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3381 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003382 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01003383 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003384 codegen_->AddSlowPath(slow_path);
3385
Roland Levillain271ab9c2014-11-27 15:23:57 +00003386 Register index = locations->InAt(0).AsRegister<Register>();
3387 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003388
3389 __ cmpl(index, length);
3390 __ j(kAboveEqual, slow_path->GetEntryLabel());
3391}
3392
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003393void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3394 temp->SetLocations(nullptr);
3395}
3396
3397void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3398 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003399 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003400}
3401
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003402void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003403 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003404 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003405}
3406
3407void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003408 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3409}
3410
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003411void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3412 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3413}
3414
3415void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003416 HBasicBlock* block = instruction->GetBlock();
3417 if (block->GetLoopInformation() != nullptr) {
3418 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3419 // The back edge will generate the suspend check.
3420 return;
3421 }
3422 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3423 // The goto will generate the suspend check.
3424 return;
3425 }
3426 GenerateSuspendCheck(instruction, nullptr);
3427}
3428
3429void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3430 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003431 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003432 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003433 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003434 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003435 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003436 if (successor == nullptr) {
3437 __ j(kNotEqual, slow_path->GetEntryLabel());
3438 __ Bind(slow_path->GetReturnLabel());
3439 } else {
3440 __ j(kEqual, codegen_->GetLabelOf(successor));
3441 __ jmp(slow_path->GetEntryLabel());
3442 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003443}
3444
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003445X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3446 return codegen_->GetAssembler();
3447}
3448
Mark Mendell7c8d0092015-01-26 11:21:33 -05003449void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003450 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003451 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003452 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003453 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05003454 __ movl(temp_reg, Address(ESP, src + stack_offset));
3455 __ movl(Address(ESP, dst + stack_offset), temp_reg);
3456}
3457
3458void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
3459 ScratchRegisterScope ensure_scratch(
3460 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3461 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3462 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3463 __ movl(temp_reg, Address(ESP, src + stack_offset));
3464 __ movl(Address(ESP, dst + stack_offset), temp_reg);
3465 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
3466 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003467}
3468
3469void ParallelMoveResolverX86::EmitMove(size_t index) {
3470 MoveOperands* move = moves_.Get(index);
3471 Location source = move->GetSource();
3472 Location destination = move->GetDestination();
3473
3474 if (source.IsRegister()) {
3475 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003476 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003477 } else {
3478 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003479 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003480 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05003481 } else if (source.IsFpuRegister()) {
3482 if (destination.IsFpuRegister()) {
3483 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3484 } else if (destination.IsStackSlot()) {
3485 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3486 } else {
3487 DCHECK(destination.IsDoubleStackSlot());
3488 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
3489 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003490 } else if (source.IsStackSlot()) {
3491 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003492 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003493 } else if (destination.IsFpuRegister()) {
3494 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003495 } else {
3496 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003497 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
3498 }
3499 } else if (source.IsDoubleStackSlot()) {
3500 if (destination.IsFpuRegister()) {
3501 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
3502 } else {
3503 DCHECK(destination.IsDoubleStackSlot()) << destination;
3504 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003505 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003506 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003507 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003508 if (constant->IsIntConstant() || constant->IsNullConstant()) {
3509 Immediate imm(CodeGenerator::GetInt32ValueOf(constant));
Mark Mendell7c8d0092015-01-26 11:21:33 -05003510 if (destination.IsRegister()) {
3511 __ movl(destination.AsRegister<Register>(), imm);
3512 } else {
3513 DCHECK(destination.IsStackSlot()) << destination;
3514 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3515 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003516 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003517 DCHECK(constant->IsFloatConstant());
3518 float value = constant->AsFloatConstant()->GetValue();
3519 Immediate imm(bit_cast<float, int32_t>(value));
3520 if (destination.IsFpuRegister()) {
3521 ScratchRegisterScope ensure_scratch(
3522 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3523 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
3524 __ movl(temp, imm);
3525 __ movd(destination.AsFpuRegister<XmmRegister>(), temp);
3526 } else {
3527 DCHECK(destination.IsStackSlot()) << destination;
3528 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3529 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003530 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003531 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003532 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003533 }
3534}
3535
3536void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003537 Register suggested_scratch = reg == EAX ? EBX : EAX;
3538 ScratchRegisterScope ensure_scratch(
3539 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3540
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003541 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3542 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
3543 __ movl(Address(ESP, mem + stack_offset), reg);
3544 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
3545}
3546
Mark Mendell7c8d0092015-01-26 11:21:33 -05003547void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
3548 ScratchRegisterScope ensure_scratch(
3549 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3550
3551 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
3552 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3553 __ movl(temp_reg, Address(ESP, mem + stack_offset));
3554 __ movss(Address(ESP, mem + stack_offset), reg);
3555 __ movd(reg, temp_reg);
3556}
3557
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003558void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
3559 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003560 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3561
3562 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003563 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003564 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3565
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003566 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
3567 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
3568 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
3569 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
3570 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
3571 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
3572}
3573
3574void ParallelMoveResolverX86::EmitSwap(size_t index) {
3575 MoveOperands* move = moves_.Get(index);
3576 Location source = move->GetSource();
3577 Location destination = move->GetDestination();
3578
3579 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003580 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003581 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003582 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003583 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003584 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003585 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3586 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05003587 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
3588 // Use XOR Swap algorithm to avoid a temporary.
3589 DCHECK_NE(source.reg(), destination.reg());
3590 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3591 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
3592 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
3593 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
3594 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
3595 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
3596 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003597 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05003598 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003599 }
3600}
3601
3602void ParallelMoveResolverX86::SpillScratch(int reg) {
3603 __ pushl(static_cast<Register>(reg));
3604}
3605
3606void ParallelMoveResolverX86::RestoreScratch(int reg) {
3607 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003608}
3609
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003610void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003611 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3612 ? LocationSummary::kCallOnSlowPath
3613 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003614 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003615 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003616 locations->SetOut(Location::RequiresRegister());
3617}
3618
3619void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003620 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003621 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003622 DCHECK(!cls->CanCallRuntime());
3623 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003624 codegen_->LoadCurrentMethod(out);
3625 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3626 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003627 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003628 codegen_->LoadCurrentMethod(out);
3629 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3630 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003631
3632 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3633 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3634 codegen_->AddSlowPath(slow_path);
3635 __ testl(out, out);
3636 __ j(kEqual, slow_path->GetEntryLabel());
3637 if (cls->MustGenerateClinitCheck()) {
3638 GenerateClassInitializationCheck(slow_path, out);
3639 } else {
3640 __ Bind(slow_path->GetExitLabel());
3641 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003642 }
3643}
3644
3645void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
3646 LocationSummary* locations =
3647 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3648 locations->SetInAt(0, Location::RequiresRegister());
3649 if (check->HasUses()) {
3650 locations->SetOut(Location::SameAsFirstInput());
3651 }
3652}
3653
3654void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003655 // We assume the class to not be null.
3656 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3657 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003658 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003659 GenerateClassInitializationCheck(slow_path,
3660 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003661}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003662
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003663void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
3664 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003665 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3666 Immediate(mirror::Class::kStatusInitialized));
3667 __ j(kLess, slow_path->GetEntryLabel());
3668 __ Bind(slow_path->GetExitLabel());
3669 // No need for memory fence, thanks to the X86 memory model.
3670}
3671
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003672void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
3673 LocationSummary* locations =
3674 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3675 locations->SetOut(Location::RequiresRegister());
3676}
3677
3678void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
3679 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
3680 codegen_->AddSlowPath(slow_path);
3681
Roland Levillain271ab9c2014-11-27 15:23:57 +00003682 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003683 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08003684 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3685 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003686 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3687 __ testl(out, out);
3688 __ j(kEqual, slow_path->GetEntryLabel());
3689 __ Bind(slow_path->GetExitLabel());
3690}
3691
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003692void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
3693 LocationSummary* locations =
3694 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3695 locations->SetOut(Location::RequiresRegister());
3696}
3697
3698void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
3699 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003700 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003701 __ fs()->movl(address, Immediate(0));
3702}
3703
3704void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
3705 LocationSummary* locations =
3706 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3707 InvokeRuntimeCallingConvention calling_convention;
3708 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3709}
3710
3711void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
3712 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
3713 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3714}
3715
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003716void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003717 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3718 ? LocationSummary::kNoCall
3719 : LocationSummary::kCallOnSlowPath;
3720 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3721 locations->SetInAt(0, Location::RequiresRegister());
3722 locations->SetInAt(1, Location::Any());
3723 locations->SetOut(Location::RequiresRegister());
3724}
3725
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003726void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003727 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003728 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003729 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003730 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003731 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3732 Label done, zero;
3733 SlowPathCodeX86* slow_path = nullptr;
3734
3735 // Return 0 if `obj` is null.
3736 // TODO: avoid this check if we know obj is not null.
3737 __ testl(obj, obj);
3738 __ j(kEqual, &zero);
3739 __ movl(out, Address(obj, class_offset));
3740 // Compare the class of `obj` with `cls`.
3741 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003742 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003743 } else {
3744 DCHECK(cls.IsStackSlot()) << cls;
3745 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
3746 }
3747
3748 if (instruction->IsClassFinal()) {
3749 // Classes must be equal for the instanceof to succeed.
3750 __ j(kNotEqual, &zero);
3751 __ movl(out, Immediate(1));
3752 __ jmp(&done);
3753 } else {
3754 // If the classes are not equal, we go into a slow path.
3755 DCHECK(locations->OnlyCallsOnSlowPath());
3756 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003757 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003758 codegen_->AddSlowPath(slow_path);
3759 __ j(kNotEqual, slow_path->GetEntryLabel());
3760 __ movl(out, Immediate(1));
3761 __ jmp(&done);
3762 }
3763 __ Bind(&zero);
3764 __ movl(out, Immediate(0));
3765 if (slow_path != nullptr) {
3766 __ Bind(slow_path->GetExitLabel());
3767 }
3768 __ Bind(&done);
3769}
3770
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003771void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
3772 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3773 instruction, LocationSummary::kCallOnSlowPath);
3774 locations->SetInAt(0, Location::RequiresRegister());
3775 locations->SetInAt(1, Location::Any());
3776 locations->AddTemp(Location::RequiresRegister());
3777}
3778
3779void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
3780 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003781 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003782 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003783 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003784 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3785 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
3786 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3787 codegen_->AddSlowPath(slow_path);
3788
3789 // TODO: avoid this check if we know obj is not null.
3790 __ testl(obj, obj);
3791 __ j(kEqual, slow_path->GetExitLabel());
3792 __ movl(temp, Address(obj, class_offset));
3793
3794 // Compare the class of `obj` with `cls`.
3795 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003796 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003797 } else {
3798 DCHECK(cls.IsStackSlot()) << cls;
3799 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
3800 }
3801
3802 __ j(kNotEqual, slow_path->GetEntryLabel());
3803 __ Bind(slow_path->GetExitLabel());
3804}
3805
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003806void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3807 LocationSummary* locations =
3808 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3809 InvokeRuntimeCallingConvention calling_convention;
3810 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3811}
3812
3813void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3814 __ fs()->call(Address::Absolute(instruction->IsEnter()
3815 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
3816 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
3817 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3818}
3819
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003820void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3821void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3822void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3823
3824void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3825 LocationSummary* locations =
3826 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3827 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3828 || instruction->GetResultType() == Primitive::kPrimLong);
3829 locations->SetInAt(0, Location::RequiresRegister());
3830 locations->SetInAt(1, Location::Any());
3831 locations->SetOut(Location::SameAsFirstInput());
3832}
3833
3834void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
3835 HandleBitwiseOperation(instruction);
3836}
3837
3838void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
3839 HandleBitwiseOperation(instruction);
3840}
3841
3842void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
3843 HandleBitwiseOperation(instruction);
3844}
3845
3846void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3847 LocationSummary* locations = instruction->GetLocations();
3848 Location first = locations->InAt(0);
3849 Location second = locations->InAt(1);
3850 DCHECK(first.Equals(locations->Out()));
3851
3852 if (instruction->GetResultType() == Primitive::kPrimInt) {
3853 if (second.IsRegister()) {
3854 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003855 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003856 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003857 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003858 } else {
3859 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003860 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003861 }
3862 } else if (second.IsConstant()) {
3863 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003864 __ andl(first.AsRegister<Register>(),
3865 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003866 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003867 __ orl(first.AsRegister<Register>(),
3868 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003869 } else {
3870 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00003871 __ xorl(first.AsRegister<Register>(),
3872 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003873 }
3874 } else {
3875 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003876 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003877 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003878 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003879 } else {
3880 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003881 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003882 }
3883 }
3884 } else {
3885 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3886 if (second.IsRegisterPair()) {
3887 if (instruction->IsAnd()) {
3888 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3889 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3890 } else if (instruction->IsOr()) {
3891 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3892 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3893 } else {
3894 DCHECK(instruction->IsXor());
3895 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3896 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3897 }
3898 } else {
3899 if (instruction->IsAnd()) {
3900 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3901 __ andl(first.AsRegisterPairHigh<Register>(),
3902 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3903 } else if (instruction->IsOr()) {
3904 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3905 __ orl(first.AsRegisterPairHigh<Register>(),
3906 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3907 } else {
3908 DCHECK(instruction->IsXor());
3909 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3910 __ xorl(first.AsRegisterPairHigh<Register>(),
3911 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3912 }
3913 }
3914 }
3915}
3916
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003917} // namespace x86
3918} // namespace art