blob: 04e36cc58a201ab9252f6a0971c5f55d009a386e [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 Geoffray1a43dd72014-07-17 15:15:34 +010020#include "gc/accounting/card_table.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070021#include "mirror/array-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010022#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010023#include "mirror/class.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010024#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000025#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010026#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000027#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010028#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010031
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032namespace x86 {
33
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010034static constexpr bool kExplicitStackOverflowCheck = false;
35
36static constexpr int kNumberOfPushedRegistersAtEntry = 1;
37static constexpr int kCurrentMethodStackOffset = 0;
38
Calin Juravled6fb6cf2014-11-11 19:07:44 +000039static constexpr Register kRuntimeParameterCoreRegisters[] = { EAX, ECX, EDX, EBX };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010040static constexpr size_t kRuntimeParameterCoreRegistersLength =
41 arraysize(kRuntimeParameterCoreRegisters);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010042static constexpr XmmRegister kRuntimeParameterFpuRegisters[] = { };
43static constexpr size_t kRuntimeParameterFpuRegistersLength = 0;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010044
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000045// Marker for places that can be updated once we don't follow the quick ABI.
46static constexpr bool kFollowsQuickABI = true;
47
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010048class InvokeRuntimeCallingConvention : public CallingConvention<Register, XmmRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010049 public:
50 InvokeRuntimeCallingConvention()
51 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010052 kRuntimeParameterCoreRegistersLength,
53 kRuntimeParameterFpuRegisters,
54 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010055
56 private:
57 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
58};
59
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
61
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010062class SlowPathCodeX86 : public SlowPathCode {
63 public:
64 SlowPathCodeX86() : entry_label_(), exit_label_() {}
65
66 Label* GetEntryLabel() { return &entry_label_; }
67 Label* GetExitLabel() { return &exit_label_; }
68
69 private:
70 Label entry_label_;
71 Label exit_label_;
72
73 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86);
74};
75
76class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010077 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010078 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079
80 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
81 __ Bind(GetEntryLabel());
82 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010083 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010084 }
85
86 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010087 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010088 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
89};
90
Calin Juravled0d48522014-11-04 16:40:20 +000091class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
92 public:
93 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
94
95 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
96 __ Bind(GetEntryLabel());
97 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
98 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
99 }
100
101 private:
102 HDivZeroCheck* const instruction_;
103 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
104};
105
Calin Juravlebacfec32014-11-14 15:54:36 +0000106class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +0000107 public:
Calin Juravlebacfec32014-11-14 15:54:36 +0000108 explicit DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000109
110 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
111 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000112 if (is_div_) {
113 __ negl(reg_);
114 } else {
115 __ movl(reg_, Immediate(0));
116 }
Calin Juravled0d48522014-11-04 16:40:20 +0000117 __ jmp(GetExitLabel());
118 }
119
120 private:
121 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000122 bool is_div_;
123 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000124};
125
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100126class StackOverflowCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100127 public:
128 StackOverflowCheckSlowPathX86() {}
129
130 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
131 __ Bind(GetEntryLabel());
132 __ addl(ESP,
133 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
134 __ fs()->jmp(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowStackOverflow)));
135 }
136
137 private:
138 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86);
139};
140
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100141class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100142 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100143 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
144 Location index_location,
145 Location length_location)
Roland Levillain199f3362014-11-27 17:15:16 +0000146 : instruction_(instruction),
147 index_location_(index_location),
148 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100149
150 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100151 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100152 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000153 // We're moving two locations to locations that could overlap, so we need a parallel
154 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100155 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000156 x86_codegen->EmitParallelMoves(
157 index_location_,
158 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
159 length_location_,
160 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100161 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100162 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100163 }
164
165 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100166 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100167 const Location index_location_;
168 const Location length_location_;
169
170 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
171};
172
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100173class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000174 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100175 explicit SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
176 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000177
178 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100179 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000180 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100181 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000182 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
183 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100184 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100185 if (successor_ == nullptr) {
186 __ jmp(GetReturnLabel());
187 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100188 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100189 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000190 }
191
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100192 Label* GetReturnLabel() {
193 DCHECK(successor_ == nullptr);
194 return &return_label_;
195 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000196
197 private:
198 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100199 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000200 Label return_label_;
201
202 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
203};
204
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000205class LoadStringSlowPathX86 : public SlowPathCodeX86 {
206 public:
207 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
208
209 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
210 LocationSummary* locations = instruction_->GetLocations();
211 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
212
213 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
214 __ Bind(GetEntryLabel());
215 codegen->SaveLiveRegisters(locations);
216
217 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800218 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
219 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000220 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
221 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
222 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
223 codegen->RestoreLiveRegisters(locations);
224
225 __ jmp(GetExitLabel());
226 }
227
228 private:
229 HLoadString* const instruction_;
230
231 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
232};
233
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000234class LoadClassSlowPathX86 : public SlowPathCodeX86 {
235 public:
236 LoadClassSlowPathX86(HLoadClass* cls,
237 HInstruction* at,
238 uint32_t dex_pc,
239 bool do_clinit)
240 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
241 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
242 }
243
244 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
245 LocationSummary* locations = at_->GetLocations();
246 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
247 __ Bind(GetEntryLabel());
248 codegen->SaveLiveRegisters(locations);
249
250 InvokeRuntimeCallingConvention calling_convention;
251 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
252 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
253 __ fs()->call(Address::Absolute(do_clinit_
254 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
255 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
256 codegen->RecordPcInfo(at_, dex_pc_);
257
258 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000259 Location out = locations->Out();
260 if (out.IsValid()) {
261 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
262 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000263 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000264
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000265 codegen->RestoreLiveRegisters(locations);
266 __ jmp(GetExitLabel());
267 }
268
269 private:
270 // The class this slow path will load.
271 HLoadClass* const cls_;
272
273 // The instruction where this slow path is happening.
274 // (Might be the load class or an initialization check).
275 HInstruction* const at_;
276
277 // The dex PC of `at_`.
278 const uint32_t dex_pc_;
279
280 // Whether to initialize the class.
281 const bool do_clinit_;
282
283 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
284};
285
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000286class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
287 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000288 TypeCheckSlowPathX86(HInstruction* instruction,
289 Location class_to_check,
290 Location object_class,
291 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000292 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000293 class_to_check_(class_to_check),
294 object_class_(object_class),
295 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000296
297 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
298 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000299 DCHECK(instruction_->IsCheckCast()
300 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000301
302 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
303 __ Bind(GetEntryLabel());
304 codegen->SaveLiveRegisters(locations);
305
306 // We're moving two locations to locations that could overlap, so we need a parallel
307 // move resolver.
308 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000309 x86_codegen->EmitParallelMoves(
310 class_to_check_,
311 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
312 object_class_,
313 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000314
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000315 if (instruction_->IsInstanceOf()) {
Roland Levillain199f3362014-11-27 17:15:16 +0000316 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize,
317 pInstanceofNonTrivial)));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000318 } else {
319 DCHECK(instruction_->IsCheckCast());
320 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
321 }
322
323 codegen->RecordPcInfo(instruction_, dex_pc_);
324 if (instruction_->IsInstanceOf()) {
325 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
326 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000327 codegen->RestoreLiveRegisters(locations);
328
329 __ jmp(GetExitLabel());
330 }
331
332 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000333 HInstruction* const instruction_;
334 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000335 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000336 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000337
338 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
339};
340
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100341#undef __
342#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
343
Dave Allison20dfc792014-06-16 20:44:29 -0700344inline Condition X86Condition(IfCondition cond) {
345 switch (cond) {
346 case kCondEQ: return kEqual;
347 case kCondNE: return kNotEqual;
348 case kCondLT: return kLess;
349 case kCondLE: return kLessEqual;
350 case kCondGT: return kGreater;
351 case kCondGE: return kGreaterEqual;
352 default:
353 LOG(FATAL) << "Unknown if condition";
354 }
355 return kEqual;
356}
357
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100358void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
359 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
360}
361
362void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
363 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
364}
365
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100366size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
367 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
368 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100369}
370
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100371size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
372 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
373 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100374}
375
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100376CodeGeneratorX86::CodeGeneratorX86(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100377 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfXmmRegisters, kNumberOfRegisterPairs),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100378 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100379 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100380 instruction_visitor_(graph, this),
381 move_resolver_(graph->GetArena(), this) {}
382
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100383size_t CodeGeneratorX86::FrameEntrySpillSize() const {
384 return kNumberOfPushedRegistersAtEntry * kX86WordSize;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100385}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100386
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100387Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100388 switch (type) {
389 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100390 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100391 X86ManagedRegister pair =
392 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100393 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
394 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100395 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
396 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100397 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100398 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100399 }
400
401 case Primitive::kPrimByte:
402 case Primitive::kPrimBoolean:
403 case Primitive::kPrimChar:
404 case Primitive::kPrimShort:
405 case Primitive::kPrimInt:
406 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100407 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100408 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100409 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100410 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
411 X86ManagedRegister current =
412 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
413 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100414 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100415 }
416 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100417 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100418 }
419
420 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100421 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100422 return Location::FpuRegisterLocation(
423 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100424 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100425
426 case Primitive::kPrimVoid:
427 LOG(FATAL) << "Unreachable type " << type;
428 }
429
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100430 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100431}
432
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100433void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100434 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100435 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100436
437 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100438 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100439
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000440 // TODO: We currently don't use Quick's callee saved registers.
441 DCHECK(kFollowsQuickABI);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100442 blocked_core_registers_[EBP] = true;
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000443 blocked_core_registers_[ESI] = true;
444 blocked_core_registers_[EDI] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100445
446 UpdateBlockedPairRegisters();
447}
448
449void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
450 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
451 X86ManagedRegister current =
452 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
453 if (blocked_core_registers_[current.AsRegisterPairLow()]
454 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
455 blocked_register_pairs_[i] = true;
456 }
457 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100458}
459
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100460InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
461 : HGraphVisitor(graph),
462 assembler_(codegen->GetAssembler()),
463 codegen_(codegen) {}
464
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000465void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000466 // Create a fake register to mimic Quick.
467 static const int kFakeReturnRegister = 8;
468 core_spill_mask_ |= (1 << kFakeReturnRegister);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000469
Roland Levillain199f3362014-11-27 17:15:16 +0000470 bool skip_overflow_check =
471 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100472 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
473 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100474 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100475 }
476
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100477 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100478 __ subl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100479
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100480 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100481 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86();
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100482 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100483
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100484 __ fs()->cmpl(ESP, Address::Absolute(Thread::StackEndOffset<kX86WordSize>()));
485 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100486 }
487
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100488 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000489}
490
491void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100492 __ addl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000493}
494
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100495void CodeGeneratorX86::Bind(HBasicBlock* block) {
496 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000497}
498
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100499void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100500 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000501}
502
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100503Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
504 switch (load->GetType()) {
505 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100506 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100507 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
508 break;
509
510 case Primitive::kPrimInt:
511 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100512 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100513 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100514
515 case Primitive::kPrimBoolean:
516 case Primitive::kPrimByte:
517 case Primitive::kPrimChar:
518 case Primitive::kPrimShort:
519 case Primitive::kPrimVoid:
520 LOG(FATAL) << "Unexpected type " << load->GetType();
521 }
522
523 LOG(FATAL) << "Unreachable";
524 return Location();
525}
526
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100527Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
528 switch (type) {
529 case Primitive::kPrimBoolean:
530 case Primitive::kPrimByte:
531 case Primitive::kPrimChar:
532 case Primitive::kPrimShort:
533 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100534 case Primitive::kPrimFloat:
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100535 case Primitive::kPrimNot: {
536 uint32_t index = gp_index_++;
537 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100538 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100539 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100540 return Location::StackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100541 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100542 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100543
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100544 case Primitive::kPrimLong:
545 case Primitive::kPrimDouble: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100546 uint32_t index = gp_index_;
547 gp_index_ += 2;
548 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100549 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
550 calling_convention.GetRegisterPairAt(index));
551 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100552 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000553 // On X86, the register index and stack index of a quick parameter is the same, since
554 // we are passing floating pointer values in core registers.
555 return Location::QuickParameter(index, index);
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100556 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100557 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100558 }
559 }
560
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100561 case Primitive::kPrimVoid:
562 LOG(FATAL) << "Unexpected parameter type " << type;
563 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100564 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100565 return Location();
566}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100567
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100568void CodeGeneratorX86::Move32(Location destination, Location source) {
569 if (source.Equals(destination)) {
570 return;
571 }
572 if (destination.IsRegister()) {
573 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000574 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100575 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000576 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100577 } else {
578 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000579 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100580 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100581 } else if (destination.IsFpuRegister()) {
582 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000583 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100584 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000585 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100586 } else {
587 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000588 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100589 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100590 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000591 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100592 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000593 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100594 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000595 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100596 } else {
597 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100598 __ pushl(Address(ESP, source.GetStackIndex()));
599 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100600 }
601 }
602}
603
604void CodeGeneratorX86::Move64(Location destination, Location source) {
605 if (source.Equals(destination)) {
606 return;
607 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100608 if (destination.IsRegisterPair()) {
609 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000610 EmitParallelMoves(
611 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
612 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
613 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
614 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100615 } else if (source.IsFpuRegister()) {
616 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100617 } else if (source.IsQuickParameter()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000618 uint16_t register_index = source.GetQuickParameterRegisterIndex();
619 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100620 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000621 EmitParallelMoves(
622 Location::RegisterLocation(calling_convention.GetRegisterAt(register_index)),
623 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
624 Location::StackSlot(
625 calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize()),
626 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100627 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000628 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100629 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100630 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
631 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100632 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
633 }
634 } else if (destination.IsQuickParameter()) {
635 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000636 uint16_t register_index = destination.GetQuickParameterRegisterIndex();
637 uint16_t stack_index = destination.GetQuickParameterStackIndex();
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000638 if (source.IsRegisterPair()) {
639 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100640 } else if (source.IsFpuRegister()) {
641 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100642 } else {
643 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000644 EmitParallelMoves(
645 Location::StackSlot(source.GetStackIndex()),
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000646 Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index)),
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000647 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
648 Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index + 1)));
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000649 __ movl(calling_convention.GetRegisterAt(register_index), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100650 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100651 } else if (destination.IsFpuRegister()) {
652 if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000653 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100654 } else {
655 LOG(FATAL) << "Unimplemented";
656 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100657 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000658 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100659 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000660 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100661 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100662 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100663 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100664 } else if (source.IsQuickParameter()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000665 // No conflict possible, so just do the move.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100666 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000667 uint16_t register_index = source.GetQuickParameterRegisterIndex();
668 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000669 // Just move the low part. The only time a source is a quick parameter is
670 // when moving the parameter to its stack locations. And the (Java) caller
671 // of this method has already done that.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100672 __ movl(Address(ESP, destination.GetStackIndex()),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000673 calling_convention.GetRegisterAt(register_index));
674 DCHECK_EQ(calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100675 static_cast<size_t>(destination.GetHighStackIndex(kX86WordSize)));
676 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000677 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100678 } else {
679 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000680 EmitParallelMoves(
681 Location::StackSlot(source.GetStackIndex()),
682 Location::StackSlot(destination.GetStackIndex()),
683 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
684 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100685 }
686 }
687}
688
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100689void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000690 LocationSummary* locations = instruction->GetLocations();
691 if (locations != nullptr && locations->Out().Equals(location)) {
692 return;
693 }
694
695 if (locations != nullptr && locations->Out().IsConstant()) {
696 HConstant* const_to_move = locations->Out().GetConstant();
697 if (const_to_move->IsIntConstant()) {
698 Immediate imm(const_to_move->AsIntConstant()->GetValue());
699 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000700 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000701 } else if (location.IsStackSlot()) {
702 __ movl(Address(ESP, location.GetStackIndex()), imm);
703 } else {
704 DCHECK(location.IsConstant());
705 DCHECK_EQ(location.GetConstant(), const_to_move);
706 }
707 } else if (const_to_move->IsLongConstant()) {
708 int64_t value = const_to_move->AsLongConstant()->GetValue();
709 if (location.IsRegisterPair()) {
710 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
711 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
712 } else if (location.IsDoubleStackSlot()) {
713 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000714 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
715 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000716 } else {
717 DCHECK(location.IsConstant());
718 DCHECK_EQ(location.GetConstant(), instruction);
719 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100720 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000721 } else if (instruction->IsTemporary()) {
722 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000723 if (temp_location.IsStackSlot()) {
724 Move32(location, temp_location);
725 } else {
726 DCHECK(temp_location.IsDoubleStackSlot());
727 Move64(location, temp_location);
728 }
Roland Levillain476df552014-10-09 17:51:36 +0100729 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100730 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100731 switch (instruction->GetType()) {
732 case Primitive::kPrimBoolean:
733 case Primitive::kPrimByte:
734 case Primitive::kPrimChar:
735 case Primitive::kPrimShort:
736 case Primitive::kPrimInt:
737 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100738 case Primitive::kPrimFloat:
739 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100740 break;
741
742 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100743 case Primitive::kPrimDouble:
744 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100745 break;
746
747 default:
748 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
749 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000750 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100751 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100752 switch (instruction->GetType()) {
753 case Primitive::kPrimBoolean:
754 case Primitive::kPrimByte:
755 case Primitive::kPrimChar:
756 case Primitive::kPrimShort:
757 case Primitive::kPrimInt:
758 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100759 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000760 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100761 break;
762
763 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100764 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000765 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100766 break;
767
768 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100769 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100770 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000771 }
772}
773
774void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000775 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000776}
777
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000778void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000779 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100780 DCHECK(!successor->IsExitBlock());
781
782 HBasicBlock* block = got->GetBlock();
783 HInstruction* previous = got->GetPrevious();
784
785 HLoopInformation* info = block->GetLoopInformation();
786 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
787 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
788 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
789 return;
790 }
791
792 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
793 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
794 }
795 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000796 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000797 }
798}
799
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000800void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000801 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000802}
803
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000804void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700805 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000806 if (kIsDebugBuild) {
807 __ Comment("Unreachable");
808 __ int3();
809 }
810}
811
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000812void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100813 LocationSummary* locations =
814 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100815 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100816 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100817 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100818 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000819}
820
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000821void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700822 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100823 if (cond->IsIntConstant()) {
824 // Constant condition, statically compared against 1.
825 int32_t cond_value = cond->AsIntConstant()->GetValue();
826 if (cond_value == 1) {
827 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
828 if_instr->IfTrueSuccessor())) {
829 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100830 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100831 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100832 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100833 DCHECK_EQ(cond_value, 0);
834 }
835 } else {
836 bool materialized =
837 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
838 // Moves do not affect the eflags register, so if the condition is
839 // evaluated just before the if, we don't need to evaluate it
840 // again.
841 bool eflags_set = cond->IsCondition()
842 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
843 if (materialized) {
844 if (!eflags_set) {
845 // Materialized condition, compare against 0.
846 Location lhs = if_instr->GetLocations()->InAt(0);
847 if (lhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000848 __ cmpl(lhs.AsRegister<Register>(), Immediate(0));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100849 } else {
850 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
851 }
852 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
853 } else {
854 __ j(X86Condition(cond->AsCondition()->GetCondition()),
855 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
856 }
857 } else {
858 Location lhs = cond->GetLocations()->InAt(0);
859 Location rhs = cond->GetLocations()->InAt(1);
860 // LHS is guaranteed to be in a register (see
861 // LocationsBuilderX86::VisitCondition).
862 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000863 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100864 } else if (rhs.IsConstant()) {
865 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
866 Immediate imm(instruction->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000867 __ cmpl(lhs.AsRegister<Register>(), imm);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100868 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000869 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100870 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100871 __ j(X86Condition(cond->AsCondition()->GetCondition()),
872 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700873 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100874 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100875 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
876 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700877 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000878 }
879}
880
881void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000882 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000883}
884
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000885void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
886 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000887}
888
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000889void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100890 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000891}
892
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000893void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100894 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700895 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000896}
897
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100898void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100899 LocationSummary* locations =
900 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100901 switch (store->InputAt(1)->GetType()) {
902 case Primitive::kPrimBoolean:
903 case Primitive::kPrimByte:
904 case Primitive::kPrimChar:
905 case Primitive::kPrimShort:
906 case Primitive::kPrimInt:
907 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100908 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100909 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
910 break;
911
912 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100913 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100914 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
915 break;
916
917 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100918 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100919 }
920 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000921}
922
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000923void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700924 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000925}
926
Dave Allison20dfc792014-06-16 20:44:29 -0700927void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100928 LocationSummary* locations =
929 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100930 locations->SetInAt(0, Location::RequiresRegister());
931 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100932 if (comp->NeedsMaterialization()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000933 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100934 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000935}
936
Dave Allison20dfc792014-06-16 20:44:29 -0700937void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
938 if (comp->NeedsMaterialization()) {
939 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000940 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100941 // Clear register: setcc only sets the low byte.
942 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700943 if (locations->InAt(1).IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000944 __ cmpl(locations->InAt(0).AsRegister<Register>(),
945 locations->InAt(1).AsRegister<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100946 } else if (locations->InAt(1).IsConstant()) {
947 HConstant* instruction = locations->InAt(1).GetConstant();
948 Immediate imm(instruction->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000949 __ cmpl(locations->InAt(0).AsRegister<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700950 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000951 __ cmpl(locations->InAt(0).AsRegister<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700952 Address(ESP, locations->InAt(1).GetStackIndex()));
953 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000954 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100955 }
Dave Allison20dfc792014-06-16 20:44:29 -0700956}
957
958void LocationsBuilderX86::VisitEqual(HEqual* comp) {
959 VisitCondition(comp);
960}
961
962void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
963 VisitCondition(comp);
964}
965
966void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
967 VisitCondition(comp);
968}
969
970void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
971 VisitCondition(comp);
972}
973
974void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
975 VisitCondition(comp);
976}
977
978void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
979 VisitCondition(comp);
980}
981
982void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
983 VisitCondition(comp);
984}
985
986void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
987 VisitCondition(comp);
988}
989
990void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
991 VisitCondition(comp);
992}
993
994void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
995 VisitCondition(comp);
996}
997
998void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
999 VisitCondition(comp);
1000}
1001
1002void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1003 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001004}
1005
1006void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001007 LocationSummary* locations =
1008 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001009 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001010}
1011
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001012void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001013 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001014 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001015}
1016
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001017void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001018 LocationSummary* locations =
1019 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001020 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001021}
1022
1023void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1024 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001025 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001026}
1027
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001028void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1029 LocationSummary* locations =
1030 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1031 locations->SetOut(Location::ConstantLocation(constant));
1032}
1033
1034void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1035 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001036 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001037}
1038
1039void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1040 LocationSummary* locations =
1041 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1042 locations->SetOut(Location::ConstantLocation(constant));
1043}
1044
1045void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1046 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001047 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001048}
1049
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001050void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001051 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001052}
1053
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001054void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001055 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001056 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001057 __ ret();
1058}
1059
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001060void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001061 LocationSummary* locations =
1062 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001063 switch (ret->InputAt(0)->GetType()) {
1064 case Primitive::kPrimBoolean:
1065 case Primitive::kPrimByte:
1066 case Primitive::kPrimChar:
1067 case Primitive::kPrimShort:
1068 case Primitive::kPrimInt:
1069 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001070 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001071 break;
1072
1073 case Primitive::kPrimLong:
1074 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001075 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001076 break;
1077
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001078 case Primitive::kPrimFloat:
1079 case Primitive::kPrimDouble:
1080 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001081 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001082 break;
1083
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001084 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001085 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001086 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001087}
1088
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001089void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001090 if (kIsDebugBuild) {
1091 switch (ret->InputAt(0)->GetType()) {
1092 case Primitive::kPrimBoolean:
1093 case Primitive::kPrimByte:
1094 case Primitive::kPrimChar:
1095 case Primitive::kPrimShort:
1096 case Primitive::kPrimInt:
1097 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001098 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001099 break;
1100
1101 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001102 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1103 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001104 break;
1105
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001106 case Primitive::kPrimFloat:
1107 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001108 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001109 break;
1110
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001111 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001112 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001113 }
1114 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001115 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001116 __ ret();
1117}
1118
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001119void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001120 HandleInvoke(invoke);
1121}
1122
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001123void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001124 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001125
1126 // TODO: Implement all kinds of calls:
1127 // 1) boot -> boot
1128 // 2) app -> boot
1129 // 3) app -> app
1130 //
1131 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1132
1133 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001134 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffray4e44c822014-12-17 12:25:12 +00001135 // temp = temp->dex_cache_resolved_methods_;
1136 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
1137 // temp = temp[index_in_cache]
1138 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001139 // (temp + offset_of_quick_compiled_code)()
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001140 __ call(Address(
1141 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001142
1143 DCHECK(!codegen_->IsLeafMethod());
1144 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1145}
1146
1147void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1148 HandleInvoke(invoke);
1149}
1150
1151void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001152 LocationSummary* locations =
1153 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001154 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001155
1156 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001157 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001158 HInstruction* input = invoke->InputAt(i);
1159 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1160 }
1161
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001162 switch (invoke->GetType()) {
1163 case Primitive::kPrimBoolean:
1164 case Primitive::kPrimByte:
1165 case Primitive::kPrimChar:
1166 case Primitive::kPrimShort:
1167 case Primitive::kPrimInt:
1168 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001169 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001170 break;
1171
1172 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001173 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001174 break;
1175
1176 case Primitive::kPrimVoid:
1177 break;
1178
1179 case Primitive::kPrimDouble:
1180 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001181 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001182 break;
1183 }
1184
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001185 invoke->SetLocations(locations);
1186}
1187
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001188void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001189 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001190 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1191 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1192 LocationSummary* locations = invoke->GetLocations();
1193 Location receiver = locations->InAt(0);
1194 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1195 // temp = object->GetClass();
1196 if (receiver.IsStackSlot()) {
1197 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1198 __ movl(temp, Address(temp, class_offset));
1199 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001200 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001201 }
1202 // temp = temp->GetMethodAt(method_offset);
1203 __ movl(temp, Address(temp, method_offset));
1204 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001205 __ call(Address(
1206 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001207
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001208 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001209 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001210}
1211
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001212void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1213 HandleInvoke(invoke);
1214 // Add the hidden argument.
1215 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM0));
1216}
1217
1218void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1219 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001220 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001221 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1222 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1223 LocationSummary* locations = invoke->GetLocations();
1224 Location receiver = locations->InAt(0);
1225 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1226
1227 // Set the hidden argument.
1228 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001229 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001230
1231 // temp = object->GetClass();
1232 if (receiver.IsStackSlot()) {
1233 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1234 __ movl(temp, Address(temp, class_offset));
1235 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001236 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001237 }
1238 // temp = temp->GetImtEntryAt(method_offset);
1239 __ movl(temp, Address(temp, method_offset));
1240 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001241 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001242 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001243
1244 DCHECK(!codegen_->IsLeafMethod());
1245 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1246}
1247
Roland Levillain88cb1752014-10-20 16:36:47 +01001248void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1249 LocationSummary* locations =
1250 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1251 switch (neg->GetResultType()) {
1252 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001253 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001254 locations->SetInAt(0, Location::RequiresRegister());
1255 locations->SetOut(Location::SameAsFirstInput());
1256 break;
1257
Roland Levillain88cb1752014-10-20 16:36:47 +01001258 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001259 locations->SetInAt(0, Location::RequiresFpuRegister());
1260 locations->SetOut(Location::SameAsFirstInput());
1261 locations->AddTemp(Location::RequiresRegister());
1262 locations->AddTemp(Location::RequiresFpuRegister());
1263 break;
1264
Roland Levillain88cb1752014-10-20 16:36:47 +01001265 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001266 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001267 locations->SetOut(Location::SameAsFirstInput());
1268 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001269 break;
1270
1271 default:
1272 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1273 }
1274}
1275
1276void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1277 LocationSummary* locations = neg->GetLocations();
1278 Location out = locations->Out();
1279 Location in = locations->InAt(0);
1280 switch (neg->GetResultType()) {
1281 case Primitive::kPrimInt:
1282 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001283 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001284 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001285 break;
1286
1287 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001288 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001289 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001290 __ negl(out.AsRegisterPairLow<Register>());
1291 // Negation is similar to subtraction from zero. The least
1292 // significant byte triggers a borrow when it is different from
1293 // zero; to take it into account, add 1 to the most significant
1294 // byte if the carry flag (CF) is set to 1 after the first NEGL
1295 // operation.
1296 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1297 __ negl(out.AsRegisterPairHigh<Register>());
1298 break;
1299
Roland Levillain5368c212014-11-27 15:03:41 +00001300 case Primitive::kPrimFloat: {
1301 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001302 Register constant = locations->GetTemp(0).AsRegister<Register>();
1303 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001304 // Implement float negation with an exclusive or with value
1305 // 0x80000000 (mask for bit 31, representing the sign of a
1306 // single-precision floating-point number).
1307 __ movl(constant, Immediate(INT32_C(0x80000000)));
1308 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001309 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001310 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001311 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001312
Roland Levillain5368c212014-11-27 15:03:41 +00001313 case Primitive::kPrimDouble: {
1314 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001315 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001316 // Implement double negation with an exclusive or with value
1317 // 0x8000000000000000 (mask for bit 63, representing the sign of
1318 // a double-precision floating-point number).
1319 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001320 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001321 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001322 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001323
1324 default:
1325 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1326 }
1327}
1328
Roland Levillaindff1f282014-11-05 14:15:05 +00001329void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001330 Primitive::Type result_type = conversion->GetResultType();
1331 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001332 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001333
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001334 // The float-to-long and double-to-long type conversions rely on a
1335 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001336 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001337 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1338 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001339 ? LocationSummary::kCall
1340 : LocationSummary::kNoCall;
1341 LocationSummary* locations =
1342 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1343
Roland Levillaindff1f282014-11-05 14:15:05 +00001344 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001345 case Primitive::kPrimByte:
1346 switch (input_type) {
1347 case Primitive::kPrimShort:
1348 case Primitive::kPrimInt:
1349 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001350 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001351 locations->SetInAt(0, Location::Any());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001352 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1353 break;
1354
1355 default:
1356 LOG(FATAL) << "Unexpected type conversion from " << input_type
1357 << " to " << result_type;
1358 }
1359 break;
1360
Roland Levillain01a8d712014-11-14 16:27:39 +00001361 case Primitive::kPrimShort:
1362 switch (input_type) {
1363 case Primitive::kPrimByte:
1364 case Primitive::kPrimInt:
1365 case Primitive::kPrimChar:
1366 // Processing a Dex `int-to-short' instruction.
1367 locations->SetInAt(0, Location::Any());
1368 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1369 break;
1370
1371 default:
1372 LOG(FATAL) << "Unexpected type conversion from " << input_type
1373 << " to " << result_type;
1374 }
1375 break;
1376
Roland Levillain946e1432014-11-11 17:35:19 +00001377 case Primitive::kPrimInt:
1378 switch (input_type) {
1379 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001380 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001381 locations->SetInAt(0, Location::Any());
1382 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1383 break;
1384
1385 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001386 // Processing a Dex `float-to-int' instruction.
1387 locations->SetInAt(0, Location::RequiresFpuRegister());
1388 locations->SetOut(Location::RequiresRegister());
1389 locations->AddTemp(Location::RequiresFpuRegister());
1390 break;
1391
Roland Levillain946e1432014-11-11 17:35:19 +00001392 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001393 // Processing a Dex `double-to-int' instruction.
1394 locations->SetInAt(0, Location::RequiresFpuRegister());
1395 locations->SetOut(Location::RequiresRegister());
1396 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001397 break;
1398
1399 default:
1400 LOG(FATAL) << "Unexpected type conversion from " << input_type
1401 << " to " << result_type;
1402 }
1403 break;
1404
Roland Levillaindff1f282014-11-05 14:15:05 +00001405 case Primitive::kPrimLong:
1406 switch (input_type) {
1407 case Primitive::kPrimByte:
1408 case Primitive::kPrimShort:
1409 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001410 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001411 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001412 locations->SetInAt(0, Location::RegisterLocation(EAX));
1413 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1414 break;
1415
Roland Levillain624279f2014-12-04 11:54:28 +00001416 case Primitive::kPrimFloat: {
1417 // Processing a Dex `float-to-long' instruction.
1418 InvokeRuntimeCallingConvention calling_convention;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001419 // Note that on x86 floating-point parameters are passed
1420 // through core registers (here, EAX).
1421 locations->SetInAt(0, Location::RegisterLocation(
1422 calling_convention.GetRegisterAt(0)));
Roland Levillain624279f2014-12-04 11:54:28 +00001423 // The runtime helper puts the result in EAX, EDX.
1424 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1425 break;
1426 }
1427
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001428 case Primitive::kPrimDouble: {
1429 // Processing a Dex `double-to-long' instruction.
1430 InvokeRuntimeCallingConvention calling_convention;
1431 // Note that on x86 floating-point parameters are passed
1432 // through core registers (here, EAX and ECX).
1433 locations->SetInAt(0, Location::RegisterPairLocation(
1434 calling_convention.GetRegisterAt(0),
1435 calling_convention.GetRegisterAt(1)));
1436 // The runtime helper puts the result in EAX, EDX.
1437 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1438 break;
1439 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001440 break;
1441
1442 default:
1443 LOG(FATAL) << "Unexpected type conversion from " << input_type
1444 << " to " << result_type;
1445 }
1446 break;
1447
Roland Levillain981e4542014-11-14 11:47:14 +00001448 case Primitive::kPrimChar:
1449 switch (input_type) {
1450 case Primitive::kPrimByte:
1451 case Primitive::kPrimShort:
1452 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001453 // Processing a Dex `int-to-char' instruction.
1454 locations->SetInAt(0, Location::Any());
1455 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1456 break;
1457
1458 default:
1459 LOG(FATAL) << "Unexpected type conversion from " << input_type
1460 << " to " << result_type;
1461 }
1462 break;
1463
Roland Levillaindff1f282014-11-05 14:15:05 +00001464 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001465 switch (input_type) {
1466 case Primitive::kPrimByte:
1467 case Primitive::kPrimShort:
1468 case Primitive::kPrimInt:
1469 case Primitive::kPrimChar:
1470 // Processing a Dex `int-to-float' instruction.
1471 locations->SetInAt(0, Location::RequiresRegister());
1472 locations->SetOut(Location::RequiresFpuRegister());
1473 break;
1474
1475 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001476 // Processing a Dex `long-to-float' instruction.
1477 locations->SetInAt(0, Location::RequiresRegister());
1478 locations->SetOut(Location::RequiresFpuRegister());
1479 locations->AddTemp(Location::RequiresFpuRegister());
1480 locations->AddTemp(Location::RequiresFpuRegister());
1481 break;
1482
Roland Levillaincff13742014-11-17 14:32:17 +00001483 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001484 // Processing a Dex `double-to-float' instruction.
1485 locations->SetInAt(0, Location::RequiresFpuRegister());
1486 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001487 break;
1488
1489 default:
1490 LOG(FATAL) << "Unexpected type conversion from " << input_type
1491 << " to " << result_type;
1492 };
1493 break;
1494
Roland Levillaindff1f282014-11-05 14:15:05 +00001495 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001496 switch (input_type) {
1497 case Primitive::kPrimByte:
1498 case Primitive::kPrimShort:
1499 case Primitive::kPrimInt:
1500 case Primitive::kPrimChar:
1501 // Processing a Dex `int-to-double' instruction.
1502 locations->SetInAt(0, Location::RequiresRegister());
1503 locations->SetOut(Location::RequiresFpuRegister());
1504 break;
1505
1506 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001507 // Processing a Dex `long-to-double' instruction.
1508 locations->SetInAt(0, Location::RequiresRegister());
1509 locations->SetOut(Location::RequiresFpuRegister());
1510 locations->AddTemp(Location::RequiresFpuRegister());
1511 locations->AddTemp(Location::RequiresFpuRegister());
1512 break;
1513
Roland Levillaincff13742014-11-17 14:32:17 +00001514 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001515 // Processing a Dex `float-to-double' instruction.
1516 locations->SetInAt(0, Location::RequiresFpuRegister());
1517 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001518 break;
1519
1520 default:
1521 LOG(FATAL) << "Unexpected type conversion from " << input_type
1522 << " to " << result_type;
1523 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001524 break;
1525
1526 default:
1527 LOG(FATAL) << "Unexpected type conversion from " << input_type
1528 << " to " << result_type;
1529 }
1530}
1531
1532void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1533 LocationSummary* locations = conversion->GetLocations();
1534 Location out = locations->Out();
1535 Location in = locations->InAt(0);
1536 Primitive::Type result_type = conversion->GetResultType();
1537 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001538 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001539 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001540 case Primitive::kPrimByte:
1541 switch (input_type) {
1542 case Primitive::kPrimShort:
1543 case Primitive::kPrimInt:
1544 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001545 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001546 if (in.IsRegister()) {
1547 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
1548 } else if (in.IsStackSlot()) {
1549 __ movsxb(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
1550 } else {
1551 DCHECK(in.GetConstant()->IsIntConstant());
1552 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1553 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1554 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001555 break;
1556
1557 default:
1558 LOG(FATAL) << "Unexpected type conversion from " << input_type
1559 << " to " << result_type;
1560 }
1561 break;
1562
Roland Levillain01a8d712014-11-14 16:27:39 +00001563 case Primitive::kPrimShort:
1564 switch (input_type) {
1565 case Primitive::kPrimByte:
1566 case Primitive::kPrimInt:
1567 case Primitive::kPrimChar:
1568 // Processing a Dex `int-to-short' instruction.
1569 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001570 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001571 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001572 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001573 } else {
1574 DCHECK(in.GetConstant()->IsIntConstant());
1575 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001576 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001577 }
1578 break;
1579
1580 default:
1581 LOG(FATAL) << "Unexpected type conversion from " << input_type
1582 << " to " << result_type;
1583 }
1584 break;
1585
Roland Levillain946e1432014-11-11 17:35:19 +00001586 case Primitive::kPrimInt:
1587 switch (input_type) {
1588 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001589 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001590 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001591 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001592 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001593 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001594 } else {
1595 DCHECK(in.IsConstant());
1596 DCHECK(in.GetConstant()->IsLongConstant());
1597 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001598 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001599 }
1600 break;
1601
Roland Levillain3f8f9362014-12-02 17:45:01 +00001602 case Primitive::kPrimFloat: {
1603 // Processing a Dex `float-to-int' instruction.
1604 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1605 Register output = out.AsRegister<Register>();
1606 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1607 Label done, nan;
1608
1609 __ movl(output, Immediate(kPrimIntMax));
1610 // temp = int-to-float(output)
1611 __ cvtsi2ss(temp, output);
1612 // if input >= temp goto done
1613 __ comiss(input, temp);
1614 __ j(kAboveEqual, &done);
1615 // if input == NaN goto nan
1616 __ j(kUnordered, &nan);
1617 // output = float-to-int-truncate(input)
1618 __ cvttss2si(output, input);
1619 __ jmp(&done);
1620 __ Bind(&nan);
1621 // output = 0
1622 __ xorl(output, output);
1623 __ Bind(&done);
1624 break;
1625 }
1626
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001627 case Primitive::kPrimDouble: {
1628 // Processing a Dex `double-to-int' instruction.
1629 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1630 Register output = out.AsRegister<Register>();
1631 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1632 Label done, nan;
1633
1634 __ movl(output, Immediate(kPrimIntMax));
1635 // temp = int-to-double(output)
1636 __ cvtsi2sd(temp, output);
1637 // if input >= temp goto done
1638 __ comisd(input, temp);
1639 __ j(kAboveEqual, &done);
1640 // if input == NaN goto nan
1641 __ j(kUnordered, &nan);
1642 // output = double-to-int-truncate(input)
1643 __ cvttsd2si(output, input);
1644 __ jmp(&done);
1645 __ Bind(&nan);
1646 // output = 0
1647 __ xorl(output, output);
1648 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001649 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001650 }
Roland Levillain946e1432014-11-11 17:35:19 +00001651
1652 default:
1653 LOG(FATAL) << "Unexpected type conversion from " << input_type
1654 << " to " << result_type;
1655 }
1656 break;
1657
Roland Levillaindff1f282014-11-05 14:15:05 +00001658 case Primitive::kPrimLong:
1659 switch (input_type) {
1660 case Primitive::kPrimByte:
1661 case Primitive::kPrimShort:
1662 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001663 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001664 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001665 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1666 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001667 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001668 __ cdq();
1669 break;
1670
1671 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001672 // Processing a Dex `float-to-long' instruction.
1673 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001674 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1675 break;
1676
Roland Levillaindff1f282014-11-05 14:15:05 +00001677 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001678 // Processing a Dex `double-to-long' instruction.
1679 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1680 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001681 break;
1682
1683 default:
1684 LOG(FATAL) << "Unexpected type conversion from " << input_type
1685 << " to " << result_type;
1686 }
1687 break;
1688
Roland Levillain981e4542014-11-14 11:47:14 +00001689 case Primitive::kPrimChar:
1690 switch (input_type) {
1691 case Primitive::kPrimByte:
1692 case Primitive::kPrimShort:
1693 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001694 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1695 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001696 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001697 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001698 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001699 } else {
1700 DCHECK(in.GetConstant()->IsIntConstant());
1701 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001702 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001703 }
1704 break;
1705
1706 default:
1707 LOG(FATAL) << "Unexpected type conversion from " << input_type
1708 << " to " << result_type;
1709 }
1710 break;
1711
Roland Levillaindff1f282014-11-05 14:15:05 +00001712 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001713 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001714 case Primitive::kPrimByte:
1715 case Primitive::kPrimShort:
1716 case Primitive::kPrimInt:
1717 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001718 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001719 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001720 break;
1721
Roland Levillain6d0e4832014-11-27 18:31:21 +00001722 case Primitive::kPrimLong: {
1723 // Processing a Dex `long-to-float' instruction.
1724 Register low = in.AsRegisterPairLow<Register>();
1725 Register high = in.AsRegisterPairHigh<Register>();
1726 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1727 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1728 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1729
1730 // Operations use doubles for precision reasons (each 32-bit
1731 // half of a long fits in the 53-bit mantissa of a double,
1732 // but not in the 24-bit mantissa of a float). This is
1733 // especially important for the low bits. The result is
1734 // eventually converted to float.
1735
1736 // low = low - 2^31 (to prevent bit 31 of `low` to be
1737 // interpreted as a sign bit)
1738 __ subl(low, Immediate(0x80000000));
1739 // temp = int-to-double(high)
1740 __ cvtsi2sd(temp, high);
1741 // temp = temp * 2^32
1742 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
1743 __ mulsd(temp, constant);
1744 // result = int-to-double(low)
1745 __ cvtsi2sd(result, low);
1746 // result = result + 2^31 (restore the original value of `low`)
1747 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
1748 __ addsd(result, constant);
1749 // result = result + temp
1750 __ addsd(result, temp);
1751 // result = double-to-float(result)
1752 __ cvtsd2ss(result, result);
1753 break;
1754 }
1755
Roland Levillaincff13742014-11-17 14:32:17 +00001756 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001757 // Processing a Dex `double-to-float' instruction.
1758 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001759 break;
1760
1761 default:
1762 LOG(FATAL) << "Unexpected type conversion from " << input_type
1763 << " to " << result_type;
1764 };
1765 break;
1766
Roland Levillaindff1f282014-11-05 14:15:05 +00001767 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001768 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001769 case Primitive::kPrimByte:
1770 case Primitive::kPrimShort:
1771 case Primitive::kPrimInt:
1772 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001773 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001774 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001775 break;
1776
Roland Levillain647b9ed2014-11-27 12:06:00 +00001777 case Primitive::kPrimLong: {
1778 // Processing a Dex `long-to-double' instruction.
1779 Register low = in.AsRegisterPairLow<Register>();
1780 Register high = in.AsRegisterPairHigh<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001781 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1782 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1783 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001784
Roland Levillain647b9ed2014-11-27 12:06:00 +00001785 // low = low - 2^31 (to prevent bit 31 of `low` to be
1786 // interpreted as a sign bit)
1787 __ subl(low, Immediate(0x80000000));
1788 // temp = int-to-double(high)
1789 __ cvtsi2sd(temp, high);
1790 // temp = temp * 2^32
Roland Levillain6d0e4832014-11-27 18:31:21 +00001791 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001792 __ mulsd(temp, constant);
1793 // result = int-to-double(low)
1794 __ cvtsi2sd(result, low);
1795 // result = result + 2^31 (restore the original value of `low`)
Roland Levillain6d0e4832014-11-27 18:31:21 +00001796 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001797 __ addsd(result, constant);
1798 // result = result + temp
1799 __ addsd(result, temp);
1800 break;
1801 }
1802
Roland Levillaincff13742014-11-17 14:32:17 +00001803 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001804 // Processing a Dex `float-to-double' instruction.
1805 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001806 break;
1807
1808 default:
1809 LOG(FATAL) << "Unexpected type conversion from " << input_type
1810 << " to " << result_type;
1811 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001812 break;
1813
1814 default:
1815 LOG(FATAL) << "Unexpected type conversion from " << input_type
1816 << " to " << result_type;
1817 }
1818}
1819
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001820void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001821 LocationSummary* locations =
1822 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001823 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001824 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001825 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001826 locations->SetInAt(0, Location::RequiresRegister());
1827 locations->SetInAt(1, Location::Any());
1828 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001829 break;
1830 }
1831
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001832 case Primitive::kPrimFloat:
1833 case Primitive::kPrimDouble: {
1834 locations->SetInAt(0, Location::RequiresFpuRegister());
1835 locations->SetInAt(1, Location::Any());
1836 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001837 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001838 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001839
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001840 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001841 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1842 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001843 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001844}
1845
1846void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1847 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001848 Location first = locations->InAt(0);
1849 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001850 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001851 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001852 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001853 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001854 __ addl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001855 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001856 __ addl(first.AsRegister<Register>(),
1857 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001858 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001859 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001860 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001861 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001862 }
1863
1864 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001865 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001866 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1867 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001868 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001869 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1870 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001871 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001872 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001873 break;
1874 }
1875
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001876 case Primitive::kPrimFloat: {
1877 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001878 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001879 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001880 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001881 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001882 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001883 }
1884
1885 case Primitive::kPrimDouble: {
1886 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001887 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001888 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001889 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001890 }
1891 break;
1892 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001893
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001894 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001895 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001896 }
1897}
1898
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001899void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001900 LocationSummary* locations =
1901 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001902 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001903 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001904 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001905 locations->SetInAt(0, Location::RequiresRegister());
1906 locations->SetInAt(1, Location::Any());
1907 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001908 break;
1909 }
Calin Juravle11351682014-10-23 15:38:15 +01001910 case Primitive::kPrimFloat:
1911 case Primitive::kPrimDouble: {
1912 locations->SetInAt(0, Location::RequiresFpuRegister());
1913 locations->SetInAt(1, Location::RequiresFpuRegister());
1914 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001915 break;
Calin Juravle11351682014-10-23 15:38:15 +01001916 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001917
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001918 default:
Calin Juravle11351682014-10-23 15:38:15 +01001919 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001920 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001921}
1922
1923void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1924 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001925 Location first = locations->InAt(0);
1926 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001927 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001928 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001929 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001930 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001931 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001932 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001933 __ subl(first.AsRegister<Register>(),
1934 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001935 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001936 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001937 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001938 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001939 }
1940
1941 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001942 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01001943 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1944 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001945 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001946 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001947 __ sbbl(first.AsRegisterPairHigh<Register>(),
1948 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001949 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001950 break;
1951 }
1952
Calin Juravle11351682014-10-23 15:38:15 +01001953 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001954 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001955 break;
Calin Juravle11351682014-10-23 15:38:15 +01001956 }
1957
1958 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001959 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001960 break;
1961 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001962
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001963 default:
Calin Juravle11351682014-10-23 15:38:15 +01001964 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001965 }
1966}
1967
Calin Juravle34bacdf2014-10-07 20:23:36 +01001968void LocationsBuilderX86::VisitMul(HMul* mul) {
1969 LocationSummary* locations =
1970 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1971 switch (mul->GetResultType()) {
1972 case Primitive::kPrimInt:
1973 locations->SetInAt(0, Location::RequiresRegister());
1974 locations->SetInAt(1, Location::Any());
1975 locations->SetOut(Location::SameAsFirstInput());
1976 break;
1977 case Primitive::kPrimLong: {
1978 locations->SetInAt(0, Location::RequiresRegister());
1979 // TODO: Currently this handles only stack operands:
1980 // - we don't have enough registers because we currently use Quick ABI.
1981 // - by the time we have a working register allocator we will probably change the ABI
1982 // and fix the above.
1983 // - we don't have a way yet to request operands on stack but the base line compiler
1984 // will leave the operands on the stack with Any().
1985 locations->SetInAt(1, Location::Any());
1986 locations->SetOut(Location::SameAsFirstInput());
1987 // Needed for imul on 32bits with 64bits output.
1988 locations->AddTemp(Location::RegisterLocation(EAX));
1989 locations->AddTemp(Location::RegisterLocation(EDX));
1990 break;
1991 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001992 case Primitive::kPrimFloat:
1993 case Primitive::kPrimDouble: {
1994 locations->SetInAt(0, Location::RequiresFpuRegister());
1995 locations->SetInAt(1, Location::RequiresFpuRegister());
1996 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001997 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001998 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001999
2000 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002001 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002002 }
2003}
2004
2005void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2006 LocationSummary* locations = mul->GetLocations();
2007 Location first = locations->InAt(0);
2008 Location second = locations->InAt(1);
2009 DCHECK(first.Equals(locations->Out()));
2010
2011 switch (mul->GetResultType()) {
2012 case Primitive::kPrimInt: {
2013 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002014 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002015 } else if (second.IsConstant()) {
2016 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002017 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002018 } else {
2019 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002020 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002021 }
2022 break;
2023 }
2024
2025 case Primitive::kPrimLong: {
2026 DCHECK(second.IsDoubleStackSlot());
2027
2028 Register in1_hi = first.AsRegisterPairHigh<Register>();
2029 Register in1_lo = first.AsRegisterPairLow<Register>();
2030 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2031 Address in2_lo(ESP, second.GetStackIndex());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002032 Register eax = locations->GetTemp(0).AsRegister<Register>();
2033 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002034
2035 DCHECK_EQ(EAX, eax);
2036 DCHECK_EQ(EDX, edx);
2037
2038 // input: in1 - 64 bits, in2 - 64 bits
2039 // output: in1
2040 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2041 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2042 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
2043
2044 __ movl(eax, in2_hi);
2045 // eax <- in1.lo * in2.hi
2046 __ imull(eax, in1_lo);
2047 // in1.hi <- in1.hi * in2.lo
2048 __ imull(in1_hi, in2_lo);
2049 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2050 __ addl(in1_hi, eax);
2051 // move in1_lo to eax to prepare for double precision
2052 __ movl(eax, in1_lo);
2053 // edx:eax <- in1.lo * in2.lo
2054 __ mull(in2_lo);
2055 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2056 __ addl(in1_hi, edx);
2057 // in1.lo <- (in1.lo * in2.lo)[31:0];
2058 __ movl(in1_lo, eax);
2059
2060 break;
2061 }
2062
Calin Juravleb5bfa962014-10-21 18:02:24 +01002063 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002064 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002065 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002066 }
2067
2068 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002069 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002070 break;
2071 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002072
2073 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002074 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002075 }
2076}
2077
Calin Juravlebacfec32014-11-14 15:54:36 +00002078void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2079 DCHECK(instruction->IsDiv() || instruction->IsRem());
2080
2081 LocationSummary* locations = instruction->GetLocations();
2082 Location out = locations->Out();
2083 Location first = locations->InAt(0);
2084 Location second = locations->InAt(1);
2085 bool is_div = instruction->IsDiv();
2086
2087 switch (instruction->GetResultType()) {
2088 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002089 Register second_reg = second.AsRegister<Register>();
2090 DCHECK_EQ(EAX, first.AsRegister<Register>());
2091 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002092
2093 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002094 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
2095 is_div);
Calin Juravlebacfec32014-11-14 15:54:36 +00002096 codegen_->AddSlowPath(slow_path);
2097
2098 // 0x80000000/-1 triggers an arithmetic exception!
2099 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2100 // it's safe to just use negl instead of more complex comparisons.
2101
2102 __ cmpl(second_reg, Immediate(-1));
2103 __ j(kEqual, slow_path->GetEntryLabel());
2104
2105 // edx:eax <- sign-extended of eax
2106 __ cdq();
2107 // eax = quotient, edx = remainder
2108 __ idivl(second_reg);
2109
2110 __ Bind(slow_path->GetExitLabel());
2111 break;
2112 }
2113
2114 case Primitive::kPrimLong: {
2115 InvokeRuntimeCallingConvention calling_convention;
2116 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2117 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2118 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2119 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2120 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2121 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2122
2123 if (is_div) {
2124 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2125 } else {
2126 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2127 }
2128 uint32_t dex_pc = is_div
2129 ? instruction->AsDiv()->GetDexPc()
2130 : instruction->AsRem()->GetDexPc();
2131 codegen_->RecordPcInfo(instruction, dex_pc);
2132
2133 break;
2134 }
2135
2136 default:
2137 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2138 }
2139}
2140
Calin Juravle7c4954d2014-10-28 16:57:40 +00002141void LocationsBuilderX86::VisitDiv(HDiv* div) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002142 LocationSummary::CallKind call_kind = div->GetResultType() == Primitive::kPrimLong
2143 ? LocationSummary::kCall
2144 : LocationSummary::kNoCall;
2145 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2146
Calin Juravle7c4954d2014-10-28 16:57:40 +00002147 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002148 case Primitive::kPrimInt: {
2149 locations->SetInAt(0, Location::RegisterLocation(EAX));
2150 locations->SetInAt(1, Location::RequiresRegister());
2151 locations->SetOut(Location::SameAsFirstInput());
2152 // Intel uses edx:eax as the dividend.
2153 locations->AddTemp(Location::RegisterLocation(EDX));
2154 break;
2155 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002156 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002157 InvokeRuntimeCallingConvention calling_convention;
2158 locations->SetInAt(0, Location::RegisterPairLocation(
2159 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2160 locations->SetInAt(1, Location::RegisterPairLocation(
2161 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2162 // Runtime helper puts the result in EAX, EDX.
2163 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002164 break;
2165 }
2166 case Primitive::kPrimFloat:
2167 case Primitive::kPrimDouble: {
2168 locations->SetInAt(0, Location::RequiresFpuRegister());
2169 locations->SetInAt(1, Location::RequiresFpuRegister());
2170 locations->SetOut(Location::SameAsFirstInput());
2171 break;
2172 }
2173
2174 default:
2175 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2176 }
2177}
2178
2179void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2180 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002181 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002182 Location first = locations->InAt(0);
2183 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002184
2185 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002186 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002187 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002188 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002189 break;
2190 }
2191
2192 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002193 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002194 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002195 break;
2196 }
2197
2198 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002199 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002200 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002201 break;
2202 }
2203
2204 default:
2205 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2206 }
2207}
2208
Calin Juravlebacfec32014-11-14 15:54:36 +00002209void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002210 Primitive::Type type = rem->GetResultType();
2211 LocationSummary::CallKind call_kind = type == Primitive::kPrimInt
2212 ? LocationSummary::kNoCall
2213 : LocationSummary::kCall;
Calin Juravlebacfec32014-11-14 15:54:36 +00002214 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2215
Calin Juravled2ec87d2014-12-08 14:24:46 +00002216 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002217 case Primitive::kPrimInt: {
2218 locations->SetInAt(0, Location::RegisterLocation(EAX));
2219 locations->SetInAt(1, Location::RequiresRegister());
2220 locations->SetOut(Location::RegisterLocation(EDX));
2221 break;
2222 }
2223 case Primitive::kPrimLong: {
2224 InvokeRuntimeCallingConvention calling_convention;
2225 locations->SetInAt(0, Location::RegisterPairLocation(
2226 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2227 locations->SetInAt(1, Location::RegisterPairLocation(
2228 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2229 // Runtime helper puts the result in EAX, EDX.
2230 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2231 break;
2232 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00002233 case Primitive::kPrimFloat: {
2234 InvokeRuntimeCallingConvention calling_convention;
2235 // x86 floating-point parameters are passed through core registers (EAX, ECX).
2236 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2237 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2238 // The runtime helper puts the result in XMM0.
2239 locations->SetOut(Location::FpuRegisterLocation(XMM0));
2240 break;
2241 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002242 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002243 InvokeRuntimeCallingConvention calling_convention;
2244 // x86 floating-point parameters are passed through core registers (EAX_ECX, EDX_EBX).
2245 locations->SetInAt(0, Location::RegisterPairLocation(
2246 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2247 locations->SetInAt(1, Location::RegisterPairLocation(
2248 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2249 // The runtime helper puts the result in XMM0.
2250 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Calin Juravlebacfec32014-11-14 15:54:36 +00002251 break;
2252 }
2253
2254 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002255 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002256 }
2257}
2258
2259void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2260 Primitive::Type type = rem->GetResultType();
2261 switch (type) {
2262 case Primitive::kPrimInt:
2263 case Primitive::kPrimLong: {
2264 GenerateDivRemIntegral(rem);
2265 break;
2266 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00002267 case Primitive::kPrimFloat: {
2268 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pFmodf)));
2269 codegen_->RecordPcInfo(rem, rem->GetDexPc());
2270 break;
2271 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002272 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002273 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pFmod)));
2274 codegen_->RecordPcInfo(rem, rem->GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00002275 break;
2276 }
2277 default:
2278 LOG(FATAL) << "Unexpected rem type " << type;
2279 }
2280}
2281
Calin Juravled0d48522014-11-04 16:40:20 +00002282void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2283 LocationSummary* locations =
2284 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002285 switch (instruction->GetType()) {
2286 case Primitive::kPrimInt: {
2287 locations->SetInAt(0, Location::Any());
2288 break;
2289 }
2290 case Primitive::kPrimLong: {
2291 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2292 if (!instruction->IsConstant()) {
2293 locations->AddTemp(Location::RequiresRegister());
2294 }
2295 break;
2296 }
2297 default:
2298 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2299 }
Calin Juravled0d48522014-11-04 16:40:20 +00002300 if (instruction->HasUses()) {
2301 locations->SetOut(Location::SameAsFirstInput());
2302 }
2303}
2304
2305void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2306 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2307 codegen_->AddSlowPath(slow_path);
2308
2309 LocationSummary* locations = instruction->GetLocations();
2310 Location value = locations->InAt(0);
2311
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002312 switch (instruction->GetType()) {
2313 case Primitive::kPrimInt: {
2314 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002315 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002316 __ j(kEqual, slow_path->GetEntryLabel());
2317 } else if (value.IsStackSlot()) {
2318 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2319 __ j(kEqual, slow_path->GetEntryLabel());
2320 } else {
2321 DCHECK(value.IsConstant()) << value;
2322 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2323 __ jmp(slow_path->GetEntryLabel());
2324 }
2325 }
2326 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002327 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002328 case Primitive::kPrimLong: {
2329 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002330 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002331 __ movl(temp, value.AsRegisterPairLow<Register>());
2332 __ orl(temp, value.AsRegisterPairHigh<Register>());
2333 __ j(kEqual, slow_path->GetEntryLabel());
2334 } else {
2335 DCHECK(value.IsConstant()) << value;
2336 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2337 __ jmp(slow_path->GetEntryLabel());
2338 }
2339 }
2340 break;
2341 }
2342 default:
2343 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002344 }
Calin Juravled0d48522014-11-04 16:40:20 +00002345}
2346
Calin Juravle9aec02f2014-11-18 23:06:35 +00002347void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2348 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2349
2350 LocationSummary* locations =
2351 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2352
2353 switch (op->GetResultType()) {
2354 case Primitive::kPrimInt: {
2355 locations->SetInAt(0, Location::RequiresRegister());
2356 // The shift count needs to be in CL.
2357 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2358 locations->SetOut(Location::SameAsFirstInput());
2359 break;
2360 }
2361 case Primitive::kPrimLong: {
2362 locations->SetInAt(0, Location::RequiresRegister());
2363 // The shift count needs to be in CL.
2364 locations->SetInAt(1, Location::RegisterLocation(ECX));
2365 locations->SetOut(Location::SameAsFirstInput());
2366 break;
2367 }
2368 default:
2369 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2370 }
2371}
2372
2373void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2374 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2375
2376 LocationSummary* locations = op->GetLocations();
2377 Location first = locations->InAt(0);
2378 Location second = locations->InAt(1);
2379 DCHECK(first.Equals(locations->Out()));
2380
2381 switch (op->GetResultType()) {
2382 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002383 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002384 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002385 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002386 DCHECK_EQ(ECX, second_reg);
2387 if (op->IsShl()) {
2388 __ shll(first_reg, second_reg);
2389 } else if (op->IsShr()) {
2390 __ sarl(first_reg, second_reg);
2391 } else {
2392 __ shrl(first_reg, second_reg);
2393 }
2394 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002395 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002396 if (op->IsShl()) {
2397 __ shll(first_reg, imm);
2398 } else if (op->IsShr()) {
2399 __ sarl(first_reg, imm);
2400 } else {
2401 __ shrl(first_reg, imm);
2402 }
2403 }
2404 break;
2405 }
2406 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002407 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002408 DCHECK_EQ(ECX, second_reg);
2409 if (op->IsShl()) {
2410 GenerateShlLong(first, second_reg);
2411 } else if (op->IsShr()) {
2412 GenerateShrLong(first, second_reg);
2413 } else {
2414 GenerateUShrLong(first, second_reg);
2415 }
2416 break;
2417 }
2418 default:
2419 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2420 }
2421}
2422
2423void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2424 Label done;
2425 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2426 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2427 __ testl(shifter, Immediate(32));
2428 __ j(kEqual, &done);
2429 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2430 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2431 __ Bind(&done);
2432}
2433
2434void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2435 Label done;
2436 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2437 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2438 __ testl(shifter, Immediate(32));
2439 __ j(kEqual, &done);
2440 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2441 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2442 __ Bind(&done);
2443}
2444
2445void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2446 Label done;
2447 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2448 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2449 __ testl(shifter, Immediate(32));
2450 __ j(kEqual, &done);
2451 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2452 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2453 __ Bind(&done);
2454}
2455
2456void LocationsBuilderX86::VisitShl(HShl* shl) {
2457 HandleShift(shl);
2458}
2459
2460void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2461 HandleShift(shl);
2462}
2463
2464void LocationsBuilderX86::VisitShr(HShr* shr) {
2465 HandleShift(shr);
2466}
2467
2468void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2469 HandleShift(shr);
2470}
2471
2472void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2473 HandleShift(ushr);
2474}
2475
2476void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2477 HandleShift(ushr);
2478}
2479
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002480void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002481 LocationSummary* locations =
2482 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002483 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002484 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002485 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2486 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002487}
2488
2489void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2490 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002491 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002492 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002493
Nicolas Geoffray707c8092014-04-04 10:50:14 +01002494 __ fs()->call(
2495 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002496
Nicolas Geoffray39468442014-09-02 15:17:15 +01002497 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002498 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002499}
2500
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002501void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2502 LocationSummary* locations =
2503 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2504 locations->SetOut(Location::RegisterLocation(EAX));
2505 InvokeRuntimeCallingConvention calling_convention;
2506 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002507 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2508 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002509}
2510
2511void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2512 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002513 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002514 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2515
2516 __ fs()->call(
2517 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
2518
2519 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2520 DCHECK(!codegen_->IsLeafMethod());
2521}
2522
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002523void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002524 LocationSummary* locations =
2525 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002526 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2527 if (location.IsStackSlot()) {
2528 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2529 } else if (location.IsDoubleStackSlot()) {
2530 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002531 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002532 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002533}
2534
2535void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002536 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002537}
2538
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002539void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002540 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002541 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002542 locations->SetInAt(0, Location::RequiresRegister());
2543 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002544}
2545
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002546void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2547 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002548 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002549 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002550 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002551 switch (not_->InputAt(0)->GetType()) {
2552 case Primitive::kPrimBoolean:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002553 __ xorl(out.AsRegister<Register>(), Immediate(1));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002554 break;
2555
2556 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002557 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002558 break;
2559
2560 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002561 __ notl(out.AsRegisterPairLow<Register>());
2562 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002563 break;
2564
2565 default:
2566 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2567 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002568}
2569
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002570void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002571 LocationSummary* locations =
2572 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002573 switch (compare->InputAt(0)->GetType()) {
2574 case Primitive::kPrimLong: {
2575 locations->SetInAt(0, Location::RequiresRegister());
2576 // TODO: we set any here but we don't handle constants
2577 locations->SetInAt(1, Location::Any());
2578 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2579 break;
2580 }
2581 case Primitive::kPrimFloat:
2582 case Primitive::kPrimDouble: {
2583 locations->SetInAt(0, Location::RequiresFpuRegister());
2584 locations->SetInAt(1, Location::RequiresFpuRegister());
2585 locations->SetOut(Location::RequiresRegister());
2586 break;
2587 }
2588 default:
2589 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2590 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002591}
2592
2593void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002594 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002595 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002596 Location left = locations->InAt(0);
2597 Location right = locations->InAt(1);
2598
2599 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002600 switch (compare->InputAt(0)->GetType()) {
2601 case Primitive::kPrimLong: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002602 if (right.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002603 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002604 } else {
2605 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002606 __ cmpl(left.AsRegisterPairHigh<Register>(),
2607 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002608 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002609 __ j(kLess, &less); // Signed compare.
2610 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002611 if (right.IsRegisterPair()) {
2612 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002613 } else {
2614 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002615 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002616 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002617 break;
2618 }
2619 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002620 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002621 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
2622 break;
2623 }
2624 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002625 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002626 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002627 break;
2628 }
2629 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002630 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002631 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002632 __ movl(out, Immediate(0));
2633 __ j(kEqual, &done);
2634 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
2635
2636 __ Bind(&greater);
2637 __ movl(out, Immediate(1));
2638 __ jmp(&done);
2639
2640 __ Bind(&less);
2641 __ movl(out, Immediate(-1));
2642
2643 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002644}
2645
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002646void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002647 LocationSummary* locations =
2648 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002649 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2650 locations->SetInAt(i, Location::Any());
2651 }
2652 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002653}
2654
2655void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002656 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002657 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002658}
2659
Calin Juravle52c48962014-12-16 17:02:57 +00002660void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
2661 /*
2662 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2663 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2664 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2665 */
2666 switch (kind) {
2667 case MemBarrierKind::kAnyAny: {
2668 __ mfence();
2669 break;
2670 }
2671 case MemBarrierKind::kAnyStore:
2672 case MemBarrierKind::kLoadAny:
2673 case MemBarrierKind::kStoreStore: {
2674 // nop
2675 break;
2676 }
2677 default:
2678 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002679 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002680}
2681
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002682
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002683void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002684 Label is_null;
2685 __ testl(value, value);
2686 __ j(kEqual, &is_null);
2687 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2688 __ movl(temp, object);
2689 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002690 __ movb(Address(temp, card, TIMES_1, 0),
2691 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002692 __ Bind(&is_null);
2693}
2694
Calin Juravle52c48962014-12-16 17:02:57 +00002695void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
2696 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002697 LocationSummary* locations =
2698 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002699 locations->SetInAt(0, Location::RequiresRegister());
2700 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle52c48962014-12-16 17:02:57 +00002701
2702 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
2703 // Long values can be loaded atomically into an XMM using movsd.
2704 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
2705 // and then copy the XMM into the output 32bits at a time).
2706 locations->AddTemp(Location::RequiresFpuRegister());
2707 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002708}
2709
Calin Juravle52c48962014-12-16 17:02:57 +00002710void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
2711 const FieldInfo& field_info) {
2712 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002713
Calin Juravle52c48962014-12-16 17:02:57 +00002714 LocationSummary* locations = instruction->GetLocations();
2715 Register base = locations->InAt(0).AsRegister<Register>();
2716 Location out = locations->Out();
2717 bool is_volatile = field_info.IsVolatile();
2718 Primitive::Type field_type = field_info.GetFieldType();
2719 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2720
2721 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002722 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00002723 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002724 break;
2725 }
2726
2727 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002728 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002729 break;
2730 }
2731
2732 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00002733 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002734 break;
2735 }
2736
2737 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002738 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002739 break;
2740 }
2741
2742 case Primitive::kPrimInt:
2743 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002744 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002745 break;
2746 }
2747
2748 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00002749 if (is_volatile) {
2750 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2751 __ movsd(temp, Address(base, offset));
2752 __ movd(out.AsRegisterPairLow<Register>(), temp);
2753 __ psrlq(temp, Immediate(32));
2754 __ movd(out.AsRegisterPairHigh<Register>(), temp);
2755 } else {
2756 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
2757 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
2758 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002759 break;
2760 }
2761
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002762 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002763 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002764 break;
2765 }
2766
2767 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002768 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002769 break;
2770 }
2771
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002772 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00002773 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002774 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002775 }
Calin Juravle52c48962014-12-16 17:02:57 +00002776
2777 if (is_volatile) {
2778 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2779 }
2780}
2781
2782void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
2783 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2784
2785 LocationSummary* locations =
2786 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2787 locations->SetInAt(0, Location::RequiresRegister());
2788 bool is_volatile = field_info.IsVolatile();
2789 Primitive::Type field_type = field_info.GetFieldType();
2790 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2791 || (field_type == Primitive::kPrimByte);
2792
2793 // The register allocator does not support multiple
2794 // inputs that die at entry with one in a specific register.
2795 if (is_byte_type) {
2796 // Ensure the value is in a byte register.
2797 locations->SetInAt(1, Location::RegisterLocation(EAX));
2798 } else {
2799 locations->SetInAt(1, Location::RequiresRegister());
2800 }
2801 // Temporary registers for the write barrier.
2802 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2803 locations->AddTemp(Location::RequiresRegister());
2804 // Ensure the card is in a byte register.
2805 locations->AddTemp(Location::RegisterLocation(ECX));
2806 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
2807 // 64bits value can be atomically written to an address with movsd and an XMM register.
2808 // We need two XMM registers because there's no easier way to (bit) copy a register pair
2809 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
2810 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
2811 // isolated cases when we need this it isn't worth adding the extra complexity.
2812 locations->AddTemp(Location::RequiresFpuRegister());
2813 locations->AddTemp(Location::RequiresFpuRegister());
2814 }
2815}
2816
2817void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
2818 const FieldInfo& field_info) {
2819 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2820
2821 LocationSummary* locations = instruction->GetLocations();
2822 Register base = locations->InAt(0).AsRegister<Register>();
2823 Location value = locations->InAt(1);
2824 bool is_volatile = field_info.IsVolatile();
2825 Primitive::Type field_type = field_info.GetFieldType();
2826 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2827
2828 if (is_volatile) {
2829 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2830 }
2831
2832 switch (field_type) {
2833 case Primitive::kPrimBoolean:
2834 case Primitive::kPrimByte: {
2835 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
2836 break;
2837 }
2838
2839 case Primitive::kPrimShort:
2840 case Primitive::kPrimChar: {
2841 __ movw(Address(base, offset), value.AsRegister<Register>());
2842 break;
2843 }
2844
2845 case Primitive::kPrimInt:
2846 case Primitive::kPrimNot: {
2847 __ movl(Address(base, offset), value.AsRegister<Register>());
2848
2849 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2850 Register temp = locations->GetTemp(0).AsRegister<Register>();
2851 Register card = locations->GetTemp(1).AsRegister<Register>();
2852 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
2853 }
2854 break;
2855 }
2856
2857 case Primitive::kPrimLong: {
2858 if (is_volatile) {
2859 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2860 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
2861 __ movd(temp1, value.AsRegisterPairLow<Register>());
2862 __ movd(temp2, value.AsRegisterPairHigh<Register>());
2863 __ punpckldq(temp1, temp2);
2864 __ movsd(Address(base, offset), temp1);
2865 } else {
2866 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
2867 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
2868 }
2869 break;
2870 }
2871
2872 case Primitive::kPrimFloat: {
2873 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
2874 break;
2875 }
2876
2877 case Primitive::kPrimDouble: {
2878 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
2879 break;
2880 }
2881
2882 case Primitive::kPrimVoid:
2883 LOG(FATAL) << "Unreachable type " << field_type;
2884 UNREACHABLE();
2885 }
2886
2887 if (is_volatile) {
2888 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2889 }
2890}
2891
2892void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2893 HandleFieldGet(instruction, instruction->GetFieldInfo());
2894}
2895
2896void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2897 HandleFieldGet(instruction, instruction->GetFieldInfo());
2898}
2899
2900void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2901 HandleFieldSet(instruction, instruction->GetFieldInfo());
2902}
2903
2904void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2905 HandleFieldSet(instruction, instruction->GetFieldInfo());
2906}
2907
2908void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2909 HandleFieldSet(instruction, instruction->GetFieldInfo());
2910}
2911
2912void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2913 HandleFieldSet(instruction, instruction->GetFieldInfo());
2914}
2915
2916void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2917 HandleFieldGet(instruction, instruction->GetFieldInfo());
2918}
2919
2920void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2921 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002922}
2923
2924void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002925 LocationSummary* locations =
2926 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002927 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002928 if (instruction->HasUses()) {
2929 locations->SetOut(Location::SameAsFirstInput());
2930 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002931}
2932
2933void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002934 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002935 codegen_->AddSlowPath(slow_path);
2936
2937 LocationSummary* locations = instruction->GetLocations();
2938 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002939
2940 if (obj.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002941 __ cmpl(obj.AsRegister<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002942 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002943 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002944 } else {
2945 DCHECK(obj.IsConstant()) << obj;
2946 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2947 __ jmp(slow_path->GetEntryLabel());
2948 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002949 }
2950 __ j(kEqual, slow_path->GetEntryLabel());
2951}
2952
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002953void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002954 LocationSummary* locations =
2955 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002956 locations->SetInAt(0, Location::RequiresRegister());
2957 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2958 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002959}
2960
2961void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
2962 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002963 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002964 Location index = locations->InAt(1);
2965
2966 switch (instruction->GetType()) {
2967 case Primitive::kPrimBoolean: {
2968 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002969 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002970 if (index.IsConstant()) {
2971 __ movzxb(out, Address(obj,
2972 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2973 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002974 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002975 }
2976 break;
2977 }
2978
2979 case Primitive::kPrimByte: {
2980 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002981 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002982 if (index.IsConstant()) {
2983 __ movsxb(out, Address(obj,
2984 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2985 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002986 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002987 }
2988 break;
2989 }
2990
2991 case Primitive::kPrimShort: {
2992 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002993 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002994 if (index.IsConstant()) {
2995 __ movsxw(out, Address(obj,
2996 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2997 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002998 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002999 }
3000 break;
3001 }
3002
3003 case Primitive::kPrimChar: {
3004 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003005 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003006 if (index.IsConstant()) {
3007 __ movzxw(out, Address(obj,
3008 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3009 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003010 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003011 }
3012 break;
3013 }
3014
3015 case Primitive::kPrimInt:
3016 case Primitive::kPrimNot: {
3017 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003018 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003019 if (index.IsConstant()) {
3020 __ movl(out, Address(obj,
3021 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3022 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003023 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003024 }
3025 break;
3026 }
3027
3028 case Primitive::kPrimLong: {
3029 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003030 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003031 if (index.IsConstant()) {
3032 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003033 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
3034 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003035 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003036 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003037 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003038 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003039 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003040 }
3041 break;
3042 }
3043
3044 case Primitive::kPrimFloat:
3045 case Primitive::kPrimDouble:
3046 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003047 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003048 case Primitive::kPrimVoid:
3049 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003050 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003051 }
3052}
3053
3054void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003055 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003056 bool needs_write_barrier =
3057 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3058
3059 DCHECK(kFollowsQuickABI);
3060 bool not_enough_registers = needs_write_barrier
3061 && !instruction->GetValue()->IsConstant()
3062 && !instruction->GetIndex()->IsConstant();
3063 bool needs_runtime_call = instruction->NeedsTypeCheck() || not_enough_registers;
3064
Nicolas Geoffray39468442014-09-02 15:17:15 +01003065 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3066 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003067 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003068
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003069 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003070 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003071 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3072 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3073 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003074 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003075 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3076 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003077 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003078 // In case of a byte operation, the register allocator does not support multiple
3079 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003080 locations->SetInAt(0, Location::RequiresRegister());
3081 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003082 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003083 // Ensure the value is in a byte register.
3084 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003085 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003086 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003087 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003088 // Temporary registers for the write barrier.
3089 if (needs_write_barrier) {
3090 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003091 // Ensure the card is in a byte register.
3092 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003093 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003094 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003095}
3096
3097void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3098 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003099 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003100 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003101 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003102 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003103 bool needs_runtime_call = locations->WillCall();
3104 bool needs_write_barrier =
3105 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003106
3107 switch (value_type) {
3108 case Primitive::kPrimBoolean:
3109 case Primitive::kPrimByte: {
3110 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003111 if (index.IsConstant()) {
3112 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003113 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003114 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003115 } else {
3116 __ movb(Address(obj, offset),
3117 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3118 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003119 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003120 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003121 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003122 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003123 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003124 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003125 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3126 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003127 }
3128 break;
3129 }
3130
3131 case Primitive::kPrimShort:
3132 case Primitive::kPrimChar: {
3133 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003134 if (index.IsConstant()) {
3135 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003136 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003137 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003138 } else {
3139 __ movw(Address(obj, offset),
3140 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3141 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003142 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003143 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003144 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3145 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003146 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003147 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003148 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3149 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003150 }
3151 break;
3152 }
3153
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003154 case Primitive::kPrimInt:
3155 case Primitive::kPrimNot: {
3156 if (!needs_runtime_call) {
3157 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3158 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003159 size_t offset =
3160 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003161 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003162 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003163 } else {
3164 DCHECK(value.IsConstant()) << value;
3165 __ movl(Address(obj, offset),
3166 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3167 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003168 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003169 DCHECK(index.IsRegister()) << index;
3170 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003171 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3172 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003173 } else {
3174 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003175 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003176 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3177 }
3178 }
3179
3180 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003181 Register temp = locations->GetTemp(0).AsRegister<Register>();
3182 Register card = locations->GetTemp(1).AsRegister<Register>();
3183 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003184 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003185 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003186 DCHECK_EQ(value_type, Primitive::kPrimNot);
3187 DCHECK(!codegen_->IsLeafMethod());
3188 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3189 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003190 }
3191 break;
3192 }
3193
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003194 case Primitive::kPrimLong: {
3195 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003196 if (index.IsConstant()) {
3197 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003198 if (value.IsRegisterPair()) {
3199 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
3200 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003201 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003202 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003203 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3204 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
3205 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3206 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003207 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003208 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003209 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003210 value.AsRegisterPairLow<Register>());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003211 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003212 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003213 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003214 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003215 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003216 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003217 Immediate(Low32Bits(val)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003218 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003219 Immediate(High32Bits(val)));
3220 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003221 }
3222 break;
3223 }
3224
3225 case Primitive::kPrimFloat:
3226 case Primitive::kPrimDouble:
3227 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003228 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003229 case Primitive::kPrimVoid:
3230 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003231 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003232 }
3233}
3234
3235void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3236 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003237 locations->SetInAt(0, Location::RequiresRegister());
3238 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003239 instruction->SetLocations(locations);
3240}
3241
3242void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3243 LocationSummary* locations = instruction->GetLocations();
3244 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003245 Register obj = locations->InAt(0).AsRegister<Register>();
3246 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003247 __ movl(out, Address(obj, offset));
3248}
3249
3250void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003251 LocationSummary* locations =
3252 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003253 locations->SetInAt(0, Location::RequiresRegister());
3254 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003255 if (instruction->HasUses()) {
3256 locations->SetOut(Location::SameAsFirstInput());
3257 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003258}
3259
3260void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3261 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003262 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01003263 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003264 codegen_->AddSlowPath(slow_path);
3265
Roland Levillain271ab9c2014-11-27 15:23:57 +00003266 Register index = locations->InAt(0).AsRegister<Register>();
3267 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003268
3269 __ cmpl(index, length);
3270 __ j(kAboveEqual, slow_path->GetEntryLabel());
3271}
3272
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003273void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3274 temp->SetLocations(nullptr);
3275}
3276
3277void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3278 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003279 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003280}
3281
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003282void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003283 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003284 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003285}
3286
3287void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003288 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3289}
3290
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003291void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3292 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3293}
3294
3295void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003296 HBasicBlock* block = instruction->GetBlock();
3297 if (block->GetLoopInformation() != nullptr) {
3298 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3299 // The back edge will generate the suspend check.
3300 return;
3301 }
3302 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3303 // The goto will generate the suspend check.
3304 return;
3305 }
3306 GenerateSuspendCheck(instruction, nullptr);
3307}
3308
3309void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3310 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003311 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003312 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003313 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003314 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003315 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003316 if (successor == nullptr) {
3317 __ j(kNotEqual, slow_path->GetEntryLabel());
3318 __ Bind(slow_path->GetReturnLabel());
3319 } else {
3320 __ j(kEqual, codegen_->GetLabelOf(successor));
3321 __ jmp(slow_path->GetEntryLabel());
3322 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003323}
3324
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003325X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3326 return codegen_->GetAssembler();
3327}
3328
3329void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
3330 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003331 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003332 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3333 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
3334 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
3335}
3336
3337void ParallelMoveResolverX86::EmitMove(size_t index) {
3338 MoveOperands* move = moves_.Get(index);
3339 Location source = move->GetSource();
3340 Location destination = move->GetDestination();
3341
3342 if (source.IsRegister()) {
3343 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003344 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003345 } else {
3346 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003347 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003348 }
3349 } else if (source.IsStackSlot()) {
3350 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003351 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003352 } else {
3353 DCHECK(destination.IsStackSlot());
3354 MoveMemoryToMemory(destination.GetStackIndex(),
3355 source.GetStackIndex());
3356 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003357 } else if (source.IsConstant()) {
3358 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
3359 Immediate imm(instruction->AsIntConstant()->GetValue());
3360 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003361 __ movl(destination.AsRegister<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003362 } else {
3363 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3364 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003365 } else {
3366 LOG(FATAL) << "Unimplemented";
3367 }
3368}
3369
3370void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003371 Register suggested_scratch = reg == EAX ? EBX : EAX;
3372 ScratchRegisterScope ensure_scratch(
3373 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3374
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003375 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3376 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
3377 __ movl(Address(ESP, mem + stack_offset), reg);
3378 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
3379}
3380
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003381void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
3382 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003383 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3384
3385 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003386 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003387 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3388
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003389 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
3390 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
3391 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
3392 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
3393 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
3394 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
3395}
3396
3397void ParallelMoveResolverX86::EmitSwap(size_t index) {
3398 MoveOperands* move = moves_.Get(index);
3399 Location source = move->GetSource();
3400 Location destination = move->GetDestination();
3401
3402 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003403 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003404 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003405 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003406 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003407 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003408 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3409 Exchange(destination.GetStackIndex(), source.GetStackIndex());
3410 } else {
3411 LOG(FATAL) << "Unimplemented";
3412 }
3413}
3414
3415void ParallelMoveResolverX86::SpillScratch(int reg) {
3416 __ pushl(static_cast<Register>(reg));
3417}
3418
3419void ParallelMoveResolverX86::RestoreScratch(int reg) {
3420 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003421}
3422
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003423void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003424 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3425 ? LocationSummary::kCallOnSlowPath
3426 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003427 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003428 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003429 locations->SetOut(Location::RequiresRegister());
3430}
3431
3432void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003433 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003434 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003435 DCHECK(!cls->CanCallRuntime());
3436 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003437 codegen_->LoadCurrentMethod(out);
3438 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3439 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003440 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003441 codegen_->LoadCurrentMethod(out);
3442 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3443 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003444
3445 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3446 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3447 codegen_->AddSlowPath(slow_path);
3448 __ testl(out, out);
3449 __ j(kEqual, slow_path->GetEntryLabel());
3450 if (cls->MustGenerateClinitCheck()) {
3451 GenerateClassInitializationCheck(slow_path, out);
3452 } else {
3453 __ Bind(slow_path->GetExitLabel());
3454 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003455 }
3456}
3457
3458void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
3459 LocationSummary* locations =
3460 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3461 locations->SetInAt(0, Location::RequiresRegister());
3462 if (check->HasUses()) {
3463 locations->SetOut(Location::SameAsFirstInput());
3464 }
3465}
3466
3467void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003468 // We assume the class to not be null.
3469 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3470 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003471 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003472 GenerateClassInitializationCheck(slow_path,
3473 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003474}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003475
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003476void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
3477 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003478 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3479 Immediate(mirror::Class::kStatusInitialized));
3480 __ j(kLess, slow_path->GetEntryLabel());
3481 __ Bind(slow_path->GetExitLabel());
3482 // No need for memory fence, thanks to the X86 memory model.
3483}
3484
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003485void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
3486 LocationSummary* locations =
3487 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3488 locations->SetOut(Location::RequiresRegister());
3489}
3490
3491void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
3492 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
3493 codegen_->AddSlowPath(slow_path);
3494
Roland Levillain271ab9c2014-11-27 15:23:57 +00003495 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003496 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08003497 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3498 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003499 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3500 __ testl(out, out);
3501 __ j(kEqual, slow_path->GetEntryLabel());
3502 __ Bind(slow_path->GetExitLabel());
3503}
3504
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003505void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
3506 LocationSummary* locations =
3507 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3508 locations->SetOut(Location::RequiresRegister());
3509}
3510
3511void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
3512 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003513 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003514 __ fs()->movl(address, Immediate(0));
3515}
3516
3517void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
3518 LocationSummary* locations =
3519 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3520 InvokeRuntimeCallingConvention calling_convention;
3521 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3522}
3523
3524void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
3525 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
3526 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3527}
3528
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003529void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003530 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3531 ? LocationSummary::kNoCall
3532 : LocationSummary::kCallOnSlowPath;
3533 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3534 locations->SetInAt(0, Location::RequiresRegister());
3535 locations->SetInAt(1, Location::Any());
3536 locations->SetOut(Location::RequiresRegister());
3537}
3538
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003539void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003540 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003541 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003542 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003543 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003544 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3545 Label done, zero;
3546 SlowPathCodeX86* slow_path = nullptr;
3547
3548 // Return 0 if `obj` is null.
3549 // TODO: avoid this check if we know obj is not null.
3550 __ testl(obj, obj);
3551 __ j(kEqual, &zero);
3552 __ movl(out, Address(obj, class_offset));
3553 // Compare the class of `obj` with `cls`.
3554 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003555 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003556 } else {
3557 DCHECK(cls.IsStackSlot()) << cls;
3558 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
3559 }
3560
3561 if (instruction->IsClassFinal()) {
3562 // Classes must be equal for the instanceof to succeed.
3563 __ j(kNotEqual, &zero);
3564 __ movl(out, Immediate(1));
3565 __ jmp(&done);
3566 } else {
3567 // If the classes are not equal, we go into a slow path.
3568 DCHECK(locations->OnlyCallsOnSlowPath());
3569 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003570 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003571 codegen_->AddSlowPath(slow_path);
3572 __ j(kNotEqual, slow_path->GetEntryLabel());
3573 __ movl(out, Immediate(1));
3574 __ jmp(&done);
3575 }
3576 __ Bind(&zero);
3577 __ movl(out, Immediate(0));
3578 if (slow_path != nullptr) {
3579 __ Bind(slow_path->GetExitLabel());
3580 }
3581 __ Bind(&done);
3582}
3583
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003584void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
3585 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3586 instruction, LocationSummary::kCallOnSlowPath);
3587 locations->SetInAt(0, Location::RequiresRegister());
3588 locations->SetInAt(1, Location::Any());
3589 locations->AddTemp(Location::RequiresRegister());
3590}
3591
3592void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
3593 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003594 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003595 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003596 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003597 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3598 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
3599 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3600 codegen_->AddSlowPath(slow_path);
3601
3602 // TODO: avoid this check if we know obj is not null.
3603 __ testl(obj, obj);
3604 __ j(kEqual, slow_path->GetExitLabel());
3605 __ movl(temp, Address(obj, class_offset));
3606
3607 // Compare the class of `obj` with `cls`.
3608 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003609 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003610 } else {
3611 DCHECK(cls.IsStackSlot()) << cls;
3612 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
3613 }
3614
3615 __ j(kNotEqual, slow_path->GetEntryLabel());
3616 __ Bind(slow_path->GetExitLabel());
3617}
3618
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003619void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3620 LocationSummary* locations =
3621 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3622 InvokeRuntimeCallingConvention calling_convention;
3623 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3624}
3625
3626void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3627 __ fs()->call(Address::Absolute(instruction->IsEnter()
3628 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
3629 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
3630 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3631}
3632
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003633void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3634void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3635void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3636
3637void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3638 LocationSummary* locations =
3639 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3640 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3641 || instruction->GetResultType() == Primitive::kPrimLong);
3642 locations->SetInAt(0, Location::RequiresRegister());
3643 locations->SetInAt(1, Location::Any());
3644 locations->SetOut(Location::SameAsFirstInput());
3645}
3646
3647void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
3648 HandleBitwiseOperation(instruction);
3649}
3650
3651void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
3652 HandleBitwiseOperation(instruction);
3653}
3654
3655void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
3656 HandleBitwiseOperation(instruction);
3657}
3658
3659void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3660 LocationSummary* locations = instruction->GetLocations();
3661 Location first = locations->InAt(0);
3662 Location second = locations->InAt(1);
3663 DCHECK(first.Equals(locations->Out()));
3664
3665 if (instruction->GetResultType() == Primitive::kPrimInt) {
3666 if (second.IsRegister()) {
3667 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003668 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003669 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003670 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003671 } else {
3672 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003673 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003674 }
3675 } else if (second.IsConstant()) {
3676 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003677 __ andl(first.AsRegister<Register>(),
3678 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003679 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003680 __ orl(first.AsRegister<Register>(),
3681 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003682 } else {
3683 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00003684 __ xorl(first.AsRegister<Register>(),
3685 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003686 }
3687 } else {
3688 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003689 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003690 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003691 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003692 } else {
3693 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003694 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003695 }
3696 }
3697 } else {
3698 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3699 if (second.IsRegisterPair()) {
3700 if (instruction->IsAnd()) {
3701 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3702 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3703 } else if (instruction->IsOr()) {
3704 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3705 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3706 } else {
3707 DCHECK(instruction->IsXor());
3708 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3709 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3710 }
3711 } else {
3712 if (instruction->IsAnd()) {
3713 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3714 __ andl(first.AsRegisterPairHigh<Register>(),
3715 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3716 } else if (instruction->IsOr()) {
3717 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3718 __ orl(first.AsRegisterPairHigh<Register>(),
3719 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3720 } else {
3721 DCHECK(instruction->IsXor());
3722 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3723 __ xorl(first.AsRegisterPairHigh<Register>(),
3724 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3725 }
3726 }
3727 }
3728}
3729
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003730} // namespace x86
3731} // namespace art