blob: 38dc8c564660d6c15cf9b2109a3515372e152c0b [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;
218 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(0));
219 __ movl(calling_convention.GetRegisterAt(1), Immediate(instruction_->GetStringIndex()));
220 __ 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
440 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000441 DCHECK(kFollowsQuickABI);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100442 blocked_core_registers_[EBP] = true;
443 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()),
646 Location::RegisterLocation(calling_convention.GetRegisterAt(register_index)),
647 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
648 Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index + 1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100649 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100650 } else if (destination.IsFpuRegister()) {
651 if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000652 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100653 } else {
654 LOG(FATAL) << "Unimplemented";
655 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100656 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000657 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100658 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000659 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100660 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100661 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100662 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100663 } else if (source.IsQuickParameter()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000664 // No conflict possible, so just do the move.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100665 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000666 uint16_t register_index = source.GetQuickParameterRegisterIndex();
667 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000668 // Just move the low part. The only time a source is a quick parameter is
669 // when moving the parameter to its stack locations. And the (Java) caller
670 // of this method has already done that.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100671 __ movl(Address(ESP, destination.GetStackIndex()),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000672 calling_convention.GetRegisterAt(register_index));
673 DCHECK_EQ(calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100674 static_cast<size_t>(destination.GetHighStackIndex(kX86WordSize)));
675 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000676 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100677 } else {
678 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000679 EmitParallelMoves(
680 Location::StackSlot(source.GetStackIndex()),
681 Location::StackSlot(destination.GetStackIndex()),
682 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
683 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100684 }
685 }
686}
687
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100688void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000689 LocationSummary* locations = instruction->GetLocations();
690 if (locations != nullptr && locations->Out().Equals(location)) {
691 return;
692 }
693
694 if (locations != nullptr && locations->Out().IsConstant()) {
695 HConstant* const_to_move = locations->Out().GetConstant();
696 if (const_to_move->IsIntConstant()) {
697 Immediate imm(const_to_move->AsIntConstant()->GetValue());
698 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000699 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000700 } else if (location.IsStackSlot()) {
701 __ movl(Address(ESP, location.GetStackIndex()), imm);
702 } else {
703 DCHECK(location.IsConstant());
704 DCHECK_EQ(location.GetConstant(), const_to_move);
705 }
706 } else if (const_to_move->IsLongConstant()) {
707 int64_t value = const_to_move->AsLongConstant()->GetValue();
708 if (location.IsRegisterPair()) {
709 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
710 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
711 } else if (location.IsDoubleStackSlot()) {
712 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000713 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
714 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000715 } else {
716 DCHECK(location.IsConstant());
717 DCHECK_EQ(location.GetConstant(), instruction);
718 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100719 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000720 } else if (instruction->IsTemporary()) {
721 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000722 if (temp_location.IsStackSlot()) {
723 Move32(location, temp_location);
724 } else {
725 DCHECK(temp_location.IsDoubleStackSlot());
726 Move64(location, temp_location);
727 }
Roland Levillain476df552014-10-09 17:51:36 +0100728 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100729 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100730 switch (instruction->GetType()) {
731 case Primitive::kPrimBoolean:
732 case Primitive::kPrimByte:
733 case Primitive::kPrimChar:
734 case Primitive::kPrimShort:
735 case Primitive::kPrimInt:
736 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100737 case Primitive::kPrimFloat:
738 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100739 break;
740
741 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100742 case Primitive::kPrimDouble:
743 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100744 break;
745
746 default:
747 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
748 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000749 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100750 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100751 switch (instruction->GetType()) {
752 case Primitive::kPrimBoolean:
753 case Primitive::kPrimByte:
754 case Primitive::kPrimChar:
755 case Primitive::kPrimShort:
756 case Primitive::kPrimInt:
757 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100758 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000759 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100760 break;
761
762 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100763 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000764 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100765 break;
766
767 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100768 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100769 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000770 }
771}
772
773void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000774 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000775}
776
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000777void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000778 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100779 DCHECK(!successor->IsExitBlock());
780
781 HBasicBlock* block = got->GetBlock();
782 HInstruction* previous = got->GetPrevious();
783
784 HLoopInformation* info = block->GetLoopInformation();
785 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
786 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
787 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
788 return;
789 }
790
791 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
792 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
793 }
794 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000795 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000796 }
797}
798
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000799void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000800 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000801}
802
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000803void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700804 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000805 if (kIsDebugBuild) {
806 __ Comment("Unreachable");
807 __ int3();
808 }
809}
810
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000811void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100812 LocationSummary* locations =
813 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100814 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100815 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100816 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100817 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000818}
819
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000820void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700821 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100822 if (cond->IsIntConstant()) {
823 // Constant condition, statically compared against 1.
824 int32_t cond_value = cond->AsIntConstant()->GetValue();
825 if (cond_value == 1) {
826 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
827 if_instr->IfTrueSuccessor())) {
828 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100829 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100830 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100831 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100832 DCHECK_EQ(cond_value, 0);
833 }
834 } else {
835 bool materialized =
836 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
837 // Moves do not affect the eflags register, so if the condition is
838 // evaluated just before the if, we don't need to evaluate it
839 // again.
840 bool eflags_set = cond->IsCondition()
841 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
842 if (materialized) {
843 if (!eflags_set) {
844 // Materialized condition, compare against 0.
845 Location lhs = if_instr->GetLocations()->InAt(0);
846 if (lhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000847 __ cmpl(lhs.AsRegister<Register>(), Immediate(0));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100848 } else {
849 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
850 }
851 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
852 } else {
853 __ j(X86Condition(cond->AsCondition()->GetCondition()),
854 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
855 }
856 } else {
857 Location lhs = cond->GetLocations()->InAt(0);
858 Location rhs = cond->GetLocations()->InAt(1);
859 // LHS is guaranteed to be in a register (see
860 // LocationsBuilderX86::VisitCondition).
861 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000862 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100863 } else if (rhs.IsConstant()) {
864 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
865 Immediate imm(instruction->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000866 __ cmpl(lhs.AsRegister<Register>(), imm);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100867 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000868 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100869 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100870 __ j(X86Condition(cond->AsCondition()->GetCondition()),
871 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700872 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100873 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100874 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
875 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700876 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000877 }
878}
879
880void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000881 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000882}
883
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000884void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
885 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000886}
887
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000888void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100889 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000890}
891
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000892void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100893 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700894 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000895}
896
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100897void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100898 LocationSummary* locations =
899 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100900 switch (store->InputAt(1)->GetType()) {
901 case Primitive::kPrimBoolean:
902 case Primitive::kPrimByte:
903 case Primitive::kPrimChar:
904 case Primitive::kPrimShort:
905 case Primitive::kPrimInt:
906 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100907 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100908 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
909 break;
910
911 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100912 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100913 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
914 break;
915
916 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100917 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100918 }
919 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000920}
921
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000922void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700923 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000924}
925
Dave Allison20dfc792014-06-16 20:44:29 -0700926void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100927 LocationSummary* locations =
928 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100929 locations->SetInAt(0, Location::RequiresRegister());
930 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100931 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100932 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100933 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000934}
935
Dave Allison20dfc792014-06-16 20:44:29 -0700936void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
937 if (comp->NeedsMaterialization()) {
938 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000939 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100940 // Clear register: setcc only sets the low byte.
941 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700942 if (locations->InAt(1).IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000943 __ cmpl(locations->InAt(0).AsRegister<Register>(),
944 locations->InAt(1).AsRegister<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100945 } else if (locations->InAt(1).IsConstant()) {
946 HConstant* instruction = locations->InAt(1).GetConstant();
947 Immediate imm(instruction->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000948 __ cmpl(locations->InAt(0).AsRegister<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700949 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000950 __ cmpl(locations->InAt(0).AsRegister<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700951 Address(ESP, locations->InAt(1).GetStackIndex()));
952 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100953 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100954 }
Dave Allison20dfc792014-06-16 20:44:29 -0700955}
956
957void LocationsBuilderX86::VisitEqual(HEqual* comp) {
958 VisitCondition(comp);
959}
960
961void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
962 VisitCondition(comp);
963}
964
965void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
966 VisitCondition(comp);
967}
968
969void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
970 VisitCondition(comp);
971}
972
973void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
974 VisitCondition(comp);
975}
976
977void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
978 VisitCondition(comp);
979}
980
981void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
982 VisitCondition(comp);
983}
984
985void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
986 VisitCondition(comp);
987}
988
989void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
990 VisitCondition(comp);
991}
992
993void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
994 VisitCondition(comp);
995}
996
997void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
998 VisitCondition(comp);
999}
1000
1001void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1002 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001003}
1004
1005void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001006 LocationSummary* locations =
1007 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001008 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001009}
1010
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001011void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001012 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001013 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001014}
1015
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001016void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001017 LocationSummary* locations =
1018 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001019 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001020}
1021
1022void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1023 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001024 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001025}
1026
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001027void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1028 LocationSummary* locations =
1029 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1030 locations->SetOut(Location::ConstantLocation(constant));
1031}
1032
1033void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1034 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001035 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001036}
1037
1038void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1039 LocationSummary* locations =
1040 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1041 locations->SetOut(Location::ConstantLocation(constant));
1042}
1043
1044void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1045 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001046 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001047}
1048
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001049void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001050 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001051}
1052
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001053void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001054 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001055 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001056 __ ret();
1057}
1058
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001059void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001060 LocationSummary* locations =
1061 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001062 switch (ret->InputAt(0)->GetType()) {
1063 case Primitive::kPrimBoolean:
1064 case Primitive::kPrimByte:
1065 case Primitive::kPrimChar:
1066 case Primitive::kPrimShort:
1067 case Primitive::kPrimInt:
1068 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001069 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001070 break;
1071
1072 case Primitive::kPrimLong:
1073 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001074 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001075 break;
1076
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001077 case Primitive::kPrimFloat:
1078 case Primitive::kPrimDouble:
1079 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001080 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001081 break;
1082
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001083 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001084 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001085 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001086}
1087
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001088void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001089 if (kIsDebugBuild) {
1090 switch (ret->InputAt(0)->GetType()) {
1091 case Primitive::kPrimBoolean:
1092 case Primitive::kPrimByte:
1093 case Primitive::kPrimChar:
1094 case Primitive::kPrimShort:
1095 case Primitive::kPrimInt:
1096 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001097 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001098 break;
1099
1100 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001101 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1102 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001103 break;
1104
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001105 case Primitive::kPrimFloat:
1106 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001107 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001108 break;
1109
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001110 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001111 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001112 }
1113 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001114 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001115 __ ret();
1116}
1117
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001118void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001119 HandleInvoke(invoke);
1120}
1121
1122void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001123 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001124
1125 // TODO: Implement all kinds of calls:
1126 // 1) boot -> boot
1127 // 2) app -> boot
1128 // 3) app -> app
1129 //
1130 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1131
1132 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001133 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001134 // temp = temp->dex_cache_resolved_methods_;
1135 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
1136 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001137 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001138 // (temp + offset_of_quick_compiled_code)()
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001139 __ call(Address(
1140 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001141
1142 DCHECK(!codegen_->IsLeafMethod());
1143 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1144}
1145
1146void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1147 HandleInvoke(invoke);
1148}
1149
1150void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001151 LocationSummary* locations =
1152 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001153 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001154
1155 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001156 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001157 HInstruction* input = invoke->InputAt(i);
1158 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1159 }
1160
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001161 switch (invoke->GetType()) {
1162 case Primitive::kPrimBoolean:
1163 case Primitive::kPrimByte:
1164 case Primitive::kPrimChar:
1165 case Primitive::kPrimShort:
1166 case Primitive::kPrimInt:
1167 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001168 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001169 break;
1170
1171 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001172 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001173 break;
1174
1175 case Primitive::kPrimVoid:
1176 break;
1177
1178 case Primitive::kPrimDouble:
1179 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001180 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001181 break;
1182 }
1183
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001184 invoke->SetLocations(locations);
1185}
1186
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001187void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001188 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001189 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1190 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1191 LocationSummary* locations = invoke->GetLocations();
1192 Location receiver = locations->InAt(0);
1193 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1194 // temp = object->GetClass();
1195 if (receiver.IsStackSlot()) {
1196 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1197 __ movl(temp, Address(temp, class_offset));
1198 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001199 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001200 }
1201 // temp = temp->GetMethodAt(method_offset);
1202 __ movl(temp, Address(temp, method_offset));
1203 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001204 __ call(Address(
1205 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001206
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001207 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001208 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001209}
1210
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001211void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1212 HandleInvoke(invoke);
1213 // Add the hidden argument.
1214 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM0));
1215}
1216
1217void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1218 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001219 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001220 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1221 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1222 LocationSummary* locations = invoke->GetLocations();
1223 Location receiver = locations->InAt(0);
1224 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1225
1226 // Set the hidden argument.
1227 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001228 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001229
1230 // temp = object->GetClass();
1231 if (receiver.IsStackSlot()) {
1232 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1233 __ movl(temp, Address(temp, class_offset));
1234 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001235 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001236 }
1237 // temp = temp->GetImtEntryAt(method_offset);
1238 __ movl(temp, Address(temp, method_offset));
1239 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001240 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001241 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001242
1243 DCHECK(!codegen_->IsLeafMethod());
1244 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1245}
1246
Roland Levillain88cb1752014-10-20 16:36:47 +01001247void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1248 LocationSummary* locations =
1249 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1250 switch (neg->GetResultType()) {
1251 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001252 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001253 locations->SetInAt(0, Location::RequiresRegister());
1254 locations->SetOut(Location::SameAsFirstInput());
1255 break;
1256
Roland Levillain88cb1752014-10-20 16:36:47 +01001257 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001258 locations->SetInAt(0, Location::RequiresFpuRegister());
1259 locations->SetOut(Location::SameAsFirstInput());
1260 locations->AddTemp(Location::RequiresRegister());
1261 locations->AddTemp(Location::RequiresFpuRegister());
1262 break;
1263
Roland Levillain88cb1752014-10-20 16:36:47 +01001264 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001265 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001266 locations->SetOut(Location::SameAsFirstInput());
1267 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001268 break;
1269
1270 default:
1271 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1272 }
1273}
1274
1275void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1276 LocationSummary* locations = neg->GetLocations();
1277 Location out = locations->Out();
1278 Location in = locations->InAt(0);
1279 switch (neg->GetResultType()) {
1280 case Primitive::kPrimInt:
1281 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001282 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001283 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001284 break;
1285
1286 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001287 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001288 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001289 __ negl(out.AsRegisterPairLow<Register>());
1290 // Negation is similar to subtraction from zero. The least
1291 // significant byte triggers a borrow when it is different from
1292 // zero; to take it into account, add 1 to the most significant
1293 // byte if the carry flag (CF) is set to 1 after the first NEGL
1294 // operation.
1295 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1296 __ negl(out.AsRegisterPairHigh<Register>());
1297 break;
1298
Roland Levillain5368c212014-11-27 15:03:41 +00001299 case Primitive::kPrimFloat: {
1300 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001301 Register constant = locations->GetTemp(0).AsRegister<Register>();
1302 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001303 // Implement float negation with an exclusive or with value
1304 // 0x80000000 (mask for bit 31, representing the sign of a
1305 // single-precision floating-point number).
1306 __ movl(constant, Immediate(INT32_C(0x80000000)));
1307 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001308 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001309 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001310 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001311
Roland Levillain5368c212014-11-27 15:03:41 +00001312 case Primitive::kPrimDouble: {
1313 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001314 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001315 // Implement double negation with an exclusive or with value
1316 // 0x8000000000000000 (mask for bit 63, representing the sign of
1317 // a double-precision floating-point number).
1318 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001319 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001320 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001321 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001322
1323 default:
1324 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1325 }
1326}
1327
Roland Levillaindff1f282014-11-05 14:15:05 +00001328void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001329 Primitive::Type result_type = conversion->GetResultType();
1330 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001331 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001332
1333 // Float-to-long conversions invoke the runtime.
1334 LocationSummary::CallKind call_kind =
1335 (input_type == Primitive::kPrimFloat && result_type == Primitive::kPrimLong)
1336 ? LocationSummary::kCall
1337 : LocationSummary::kNoCall;
1338 LocationSummary* locations =
1339 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1340
Roland Levillaindff1f282014-11-05 14:15:05 +00001341 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001342 case Primitive::kPrimByte:
1343 switch (input_type) {
1344 case Primitive::kPrimShort:
1345 case Primitive::kPrimInt:
1346 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001347 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001348 locations->SetInAt(0, Location::Any());
1349 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1350 break;
1351
1352 default:
1353 LOG(FATAL) << "Unexpected type conversion from " << input_type
1354 << " to " << result_type;
1355 }
1356 break;
1357
Roland Levillain01a8d712014-11-14 16:27:39 +00001358 case Primitive::kPrimShort:
1359 switch (input_type) {
1360 case Primitive::kPrimByte:
1361 case Primitive::kPrimInt:
1362 case Primitive::kPrimChar:
1363 // Processing a Dex `int-to-short' instruction.
1364 locations->SetInAt(0, Location::Any());
1365 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1366 break;
1367
1368 default:
1369 LOG(FATAL) << "Unexpected type conversion from " << input_type
1370 << " to " << result_type;
1371 }
1372 break;
1373
Roland Levillain946e1432014-11-11 17:35:19 +00001374 case Primitive::kPrimInt:
1375 switch (input_type) {
1376 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001377 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001378 locations->SetInAt(0, Location::Any());
1379 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1380 break;
1381
1382 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001383 // Processing a Dex `float-to-int' instruction.
1384 locations->SetInAt(0, Location::RequiresFpuRegister());
1385 locations->SetOut(Location::RequiresRegister());
1386 locations->AddTemp(Location::RequiresFpuRegister());
1387 break;
1388
Roland Levillain946e1432014-11-11 17:35:19 +00001389 case Primitive::kPrimDouble:
1390 LOG(FATAL) << "Type conversion from " << input_type
1391 << " to " << result_type << " not yet implemented";
1392 break;
1393
1394 default:
1395 LOG(FATAL) << "Unexpected type conversion from " << input_type
1396 << " to " << result_type;
1397 }
1398 break;
1399
Roland Levillaindff1f282014-11-05 14:15:05 +00001400 case Primitive::kPrimLong:
1401 switch (input_type) {
1402 case Primitive::kPrimByte:
1403 case Primitive::kPrimShort:
1404 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001405 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001406 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001407 locations->SetInAt(0, Location::RegisterLocation(EAX));
1408 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1409 break;
1410
Roland Levillain624279f2014-12-04 11:54:28 +00001411 case Primitive::kPrimFloat: {
1412 // Processing a Dex `float-to-long' instruction.
1413 InvokeRuntimeCallingConvention calling_convention;
1414 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1415 // The runtime helper puts the result in EAX, EDX.
1416 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1417 break;
1418 }
1419
Roland Levillaindff1f282014-11-05 14:15:05 +00001420 case Primitive::kPrimDouble:
1421 LOG(FATAL) << "Type conversion from " << input_type << " to "
1422 << result_type << " not yet implemented";
1423 break;
1424
1425 default:
1426 LOG(FATAL) << "Unexpected type conversion from " << input_type
1427 << " to " << result_type;
1428 }
1429 break;
1430
Roland Levillain981e4542014-11-14 11:47:14 +00001431 case Primitive::kPrimChar:
1432 switch (input_type) {
1433 case Primitive::kPrimByte:
1434 case Primitive::kPrimShort:
1435 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001436 // Processing a Dex `int-to-char' instruction.
1437 locations->SetInAt(0, Location::Any());
1438 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1439 break;
1440
1441 default:
1442 LOG(FATAL) << "Unexpected type conversion from " << input_type
1443 << " to " << result_type;
1444 }
1445 break;
1446
Roland Levillaindff1f282014-11-05 14:15:05 +00001447 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001448 switch (input_type) {
1449 case Primitive::kPrimByte:
1450 case Primitive::kPrimShort:
1451 case Primitive::kPrimInt:
1452 case Primitive::kPrimChar:
1453 // Processing a Dex `int-to-float' instruction.
1454 locations->SetInAt(0, Location::RequiresRegister());
1455 locations->SetOut(Location::RequiresFpuRegister());
1456 break;
1457
1458 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001459 // Processing a Dex `long-to-float' instruction.
1460 locations->SetInAt(0, Location::RequiresRegister());
1461 locations->SetOut(Location::RequiresFpuRegister());
1462 locations->AddTemp(Location::RequiresFpuRegister());
1463 locations->AddTemp(Location::RequiresFpuRegister());
1464 break;
1465
Roland Levillaincff13742014-11-17 14:32:17 +00001466 case Primitive::kPrimDouble:
1467 LOG(FATAL) << "Type conversion from " << input_type
1468 << " to " << result_type << " not yet implemented";
1469 break;
1470
1471 default:
1472 LOG(FATAL) << "Unexpected type conversion from " << input_type
1473 << " to " << result_type;
1474 };
1475 break;
1476
Roland Levillaindff1f282014-11-05 14:15:05 +00001477 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001478 switch (input_type) {
1479 case Primitive::kPrimByte:
1480 case Primitive::kPrimShort:
1481 case Primitive::kPrimInt:
1482 case Primitive::kPrimChar:
1483 // Processing a Dex `int-to-double' instruction.
1484 locations->SetInAt(0, Location::RequiresRegister());
1485 locations->SetOut(Location::RequiresFpuRegister());
1486 break;
1487
1488 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001489 // Processing a Dex `long-to-double' instruction.
1490 locations->SetInAt(0, Location::RequiresRegister());
1491 locations->SetOut(Location::RequiresFpuRegister());
1492 locations->AddTemp(Location::RequiresFpuRegister());
1493 locations->AddTemp(Location::RequiresFpuRegister());
1494 break;
1495
Roland Levillaincff13742014-11-17 14:32:17 +00001496 case Primitive::kPrimFloat:
1497 LOG(FATAL) << "Type conversion from " << input_type
1498 << " to " << result_type << " not yet implemented";
1499 break;
1500
1501 default:
1502 LOG(FATAL) << "Unexpected type conversion from " << input_type
1503 << " to " << result_type;
1504 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001505 break;
1506
1507 default:
1508 LOG(FATAL) << "Unexpected type conversion from " << input_type
1509 << " to " << result_type;
1510 }
1511}
1512
1513void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1514 LocationSummary* locations = conversion->GetLocations();
1515 Location out = locations->Out();
1516 Location in = locations->InAt(0);
1517 Primitive::Type result_type = conversion->GetResultType();
1518 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001519 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001520 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001521 case Primitive::kPrimByte:
1522 switch (input_type) {
1523 case Primitive::kPrimShort:
1524 case Primitive::kPrimInt:
1525 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001526 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001527 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001528 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001529 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001530 __ movsxb(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain51d3fc42014-11-13 14:11:42 +00001531 } else {
1532 DCHECK(in.GetConstant()->IsIntConstant());
1533 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001534 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
Roland Levillain51d3fc42014-11-13 14:11:42 +00001535 }
1536 break;
1537
1538 default:
1539 LOG(FATAL) << "Unexpected type conversion from " << input_type
1540 << " to " << result_type;
1541 }
1542 break;
1543
Roland Levillain01a8d712014-11-14 16:27:39 +00001544 case Primitive::kPrimShort:
1545 switch (input_type) {
1546 case Primitive::kPrimByte:
1547 case Primitive::kPrimInt:
1548 case Primitive::kPrimChar:
1549 // Processing a Dex `int-to-short' instruction.
1550 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001551 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001552 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001553 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001554 } else {
1555 DCHECK(in.GetConstant()->IsIntConstant());
1556 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001557 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001558 }
1559 break;
1560
1561 default:
1562 LOG(FATAL) << "Unexpected type conversion from " << input_type
1563 << " to " << result_type;
1564 }
1565 break;
1566
Roland Levillain946e1432014-11-11 17:35:19 +00001567 case Primitive::kPrimInt:
1568 switch (input_type) {
1569 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001570 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001571 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001572 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001573 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001574 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001575 } else {
1576 DCHECK(in.IsConstant());
1577 DCHECK(in.GetConstant()->IsLongConstant());
1578 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001579 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001580 }
1581 break;
1582
Roland Levillain3f8f9362014-12-02 17:45:01 +00001583 case Primitive::kPrimFloat: {
1584 // Processing a Dex `float-to-int' instruction.
1585 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1586 Register output = out.AsRegister<Register>();
1587 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1588 Label done, nan;
1589
1590 __ movl(output, Immediate(kPrimIntMax));
1591 // temp = int-to-float(output)
1592 __ cvtsi2ss(temp, output);
1593 // if input >= temp goto done
1594 __ comiss(input, temp);
1595 __ j(kAboveEqual, &done);
1596 // if input == NaN goto nan
1597 __ j(kUnordered, &nan);
1598 // output = float-to-int-truncate(input)
1599 __ cvttss2si(output, input);
1600 __ jmp(&done);
1601 __ Bind(&nan);
1602 // output = 0
1603 __ xorl(output, output);
1604 __ Bind(&done);
1605 break;
1606 }
1607
Roland Levillain946e1432014-11-11 17:35:19 +00001608 case Primitive::kPrimDouble:
1609 LOG(FATAL) << "Type conversion from " << input_type
1610 << " to " << result_type << " not yet implemented";
1611 break;
1612
1613 default:
1614 LOG(FATAL) << "Unexpected type conversion from " << input_type
1615 << " to " << result_type;
1616 }
1617 break;
1618
Roland Levillaindff1f282014-11-05 14:15:05 +00001619 case Primitive::kPrimLong:
1620 switch (input_type) {
1621 case Primitive::kPrimByte:
1622 case Primitive::kPrimShort:
1623 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001624 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001625 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001626 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1627 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001628 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001629 __ cdq();
1630 break;
1631
1632 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001633 // Processing a Dex `float-to-long' instruction.
1634 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
1635 // This call does not actually record PC information.
1636 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1637 break;
1638
Roland Levillaindff1f282014-11-05 14:15:05 +00001639 case Primitive::kPrimDouble:
1640 LOG(FATAL) << "Type conversion from " << input_type << " to "
1641 << result_type << " not yet implemented";
1642 break;
1643
1644 default:
1645 LOG(FATAL) << "Unexpected type conversion from " << input_type
1646 << " to " << result_type;
1647 }
1648 break;
1649
Roland Levillain981e4542014-11-14 11:47:14 +00001650 case Primitive::kPrimChar:
1651 switch (input_type) {
1652 case Primitive::kPrimByte:
1653 case Primitive::kPrimShort:
1654 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001655 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1656 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001657 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001658 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001659 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001660 } else {
1661 DCHECK(in.GetConstant()->IsIntConstant());
1662 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001663 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001664 }
1665 break;
1666
1667 default:
1668 LOG(FATAL) << "Unexpected type conversion from " << input_type
1669 << " to " << result_type;
1670 }
1671 break;
1672
Roland Levillaindff1f282014-11-05 14:15:05 +00001673 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001674 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001675 case Primitive::kPrimByte:
1676 case Primitive::kPrimShort:
1677 case Primitive::kPrimInt:
1678 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001679 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001680 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001681 break;
1682
Roland Levillain6d0e4832014-11-27 18:31:21 +00001683 case Primitive::kPrimLong: {
1684 // Processing a Dex `long-to-float' instruction.
1685 Register low = in.AsRegisterPairLow<Register>();
1686 Register high = in.AsRegisterPairHigh<Register>();
1687 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1688 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1689 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1690
1691 // Operations use doubles for precision reasons (each 32-bit
1692 // half of a long fits in the 53-bit mantissa of a double,
1693 // but not in the 24-bit mantissa of a float). This is
1694 // especially important for the low bits. The result is
1695 // eventually converted to float.
1696
1697 // low = low - 2^31 (to prevent bit 31 of `low` to be
1698 // interpreted as a sign bit)
1699 __ subl(low, Immediate(0x80000000));
1700 // temp = int-to-double(high)
1701 __ cvtsi2sd(temp, high);
1702 // temp = temp * 2^32
1703 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
1704 __ mulsd(temp, constant);
1705 // result = int-to-double(low)
1706 __ cvtsi2sd(result, low);
1707 // result = result + 2^31 (restore the original value of `low`)
1708 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
1709 __ addsd(result, constant);
1710 // result = result + temp
1711 __ addsd(result, temp);
1712 // result = double-to-float(result)
1713 __ cvtsd2ss(result, result);
1714 break;
1715 }
1716
Roland Levillaincff13742014-11-17 14:32:17 +00001717 case Primitive::kPrimDouble:
1718 LOG(FATAL) << "Type conversion from " << input_type
1719 << " to " << result_type << " not yet implemented";
1720 break;
1721
1722 default:
1723 LOG(FATAL) << "Unexpected type conversion from " << input_type
1724 << " to " << result_type;
1725 };
1726 break;
1727
Roland Levillaindff1f282014-11-05 14:15:05 +00001728 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001729 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001730 case Primitive::kPrimByte:
1731 case Primitive::kPrimShort:
1732 case Primitive::kPrimInt:
1733 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001734 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001735 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001736 break;
1737
Roland Levillain647b9ed2014-11-27 12:06:00 +00001738 case Primitive::kPrimLong: {
1739 // Processing a Dex `long-to-double' instruction.
1740 Register low = in.AsRegisterPairLow<Register>();
1741 Register high = in.AsRegisterPairHigh<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001742 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1743 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1744 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001745
Roland Levillain647b9ed2014-11-27 12:06:00 +00001746 // low = low - 2^31 (to prevent bit 31 of `low` to be
1747 // interpreted as a sign bit)
1748 __ subl(low, Immediate(0x80000000));
1749 // temp = int-to-double(high)
1750 __ cvtsi2sd(temp, high);
1751 // temp = temp * 2^32
Roland Levillain6d0e4832014-11-27 18:31:21 +00001752 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001753 __ mulsd(temp, constant);
1754 // result = int-to-double(low)
1755 __ cvtsi2sd(result, low);
1756 // result = result + 2^31 (restore the original value of `low`)
Roland Levillain6d0e4832014-11-27 18:31:21 +00001757 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001758 __ addsd(result, constant);
1759 // result = result + temp
1760 __ addsd(result, temp);
1761 break;
1762 }
1763
Roland Levillaincff13742014-11-17 14:32:17 +00001764 case Primitive::kPrimFloat:
1765 LOG(FATAL) << "Type conversion from " << input_type
1766 << " to " << result_type << " not yet implemented";
1767 break;
1768
1769 default:
1770 LOG(FATAL) << "Unexpected type conversion from " << input_type
1771 << " to " << result_type;
1772 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001773 break;
1774
1775 default:
1776 LOG(FATAL) << "Unexpected type conversion from " << input_type
1777 << " to " << result_type;
1778 }
1779}
1780
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001781void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001782 LocationSummary* locations =
1783 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001784 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001785 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001786 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001787 locations->SetInAt(0, Location::RequiresRegister());
1788 locations->SetInAt(1, Location::Any());
1789 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001790 break;
1791 }
1792
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001793 case Primitive::kPrimFloat:
1794 case Primitive::kPrimDouble: {
1795 locations->SetInAt(0, Location::RequiresFpuRegister());
1796 locations->SetInAt(1, Location::Any());
1797 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001798 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001799 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001800
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001801 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001802 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1803 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001804 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001805}
1806
1807void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1808 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001809 Location first = locations->InAt(0);
1810 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001811 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001812 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001813 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001814 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001815 __ addl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001816 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001817 __ addl(first.AsRegister<Register>(),
1818 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001819 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001820 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001821 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001822 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001823 }
1824
1825 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001826 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001827 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1828 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001829 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001830 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1831 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001832 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001833 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001834 break;
1835 }
1836
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001837 case Primitive::kPrimFloat: {
1838 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001839 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001840 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001841 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001842 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001843 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001844 }
1845
1846 case Primitive::kPrimDouble: {
1847 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001848 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001849 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001850 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001851 }
1852 break;
1853 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001854
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001855 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001856 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001857 }
1858}
1859
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001860void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001861 LocationSummary* locations =
1862 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001863 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001864 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001865 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001866 locations->SetInAt(0, Location::RequiresRegister());
1867 locations->SetInAt(1, Location::Any());
1868 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001869 break;
1870 }
Calin Juravle11351682014-10-23 15:38:15 +01001871 case Primitive::kPrimFloat:
1872 case Primitive::kPrimDouble: {
1873 locations->SetInAt(0, Location::RequiresFpuRegister());
1874 locations->SetInAt(1, Location::RequiresFpuRegister());
1875 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001876 break;
Calin Juravle11351682014-10-23 15:38:15 +01001877 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001878
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001879 default:
Calin Juravle11351682014-10-23 15:38:15 +01001880 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001881 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001882}
1883
1884void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1885 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001886 Location first = locations->InAt(0);
1887 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001888 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001889 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001890 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001891 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001892 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001893 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001894 __ subl(first.AsRegister<Register>(),
1895 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001896 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001897 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001898 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001899 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001900 }
1901
1902 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001903 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01001904 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1905 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001906 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001907 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001908 __ sbbl(first.AsRegisterPairHigh<Register>(),
1909 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001910 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001911 break;
1912 }
1913
Calin Juravle11351682014-10-23 15:38:15 +01001914 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001915 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001916 break;
Calin Juravle11351682014-10-23 15:38:15 +01001917 }
1918
1919 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001920 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001921 break;
1922 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001923
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001924 default:
Calin Juravle11351682014-10-23 15:38:15 +01001925 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001926 }
1927}
1928
Calin Juravle34bacdf2014-10-07 20:23:36 +01001929void LocationsBuilderX86::VisitMul(HMul* mul) {
1930 LocationSummary* locations =
1931 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1932 switch (mul->GetResultType()) {
1933 case Primitive::kPrimInt:
1934 locations->SetInAt(0, Location::RequiresRegister());
1935 locations->SetInAt(1, Location::Any());
1936 locations->SetOut(Location::SameAsFirstInput());
1937 break;
1938 case Primitive::kPrimLong: {
1939 locations->SetInAt(0, Location::RequiresRegister());
1940 // TODO: Currently this handles only stack operands:
1941 // - we don't have enough registers because we currently use Quick ABI.
1942 // - by the time we have a working register allocator we will probably change the ABI
1943 // and fix the above.
1944 // - we don't have a way yet to request operands on stack but the base line compiler
1945 // will leave the operands on the stack with Any().
1946 locations->SetInAt(1, Location::Any());
1947 locations->SetOut(Location::SameAsFirstInput());
1948 // Needed for imul on 32bits with 64bits output.
1949 locations->AddTemp(Location::RegisterLocation(EAX));
1950 locations->AddTemp(Location::RegisterLocation(EDX));
1951 break;
1952 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001953 case Primitive::kPrimFloat:
1954 case Primitive::kPrimDouble: {
1955 locations->SetInAt(0, Location::RequiresFpuRegister());
1956 locations->SetInAt(1, Location::RequiresFpuRegister());
1957 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001958 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001959 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001960
1961 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001962 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001963 }
1964}
1965
1966void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1967 LocationSummary* locations = mul->GetLocations();
1968 Location first = locations->InAt(0);
1969 Location second = locations->InAt(1);
1970 DCHECK(first.Equals(locations->Out()));
1971
1972 switch (mul->GetResultType()) {
1973 case Primitive::kPrimInt: {
1974 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001975 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001976 } else if (second.IsConstant()) {
1977 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001978 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001979 } else {
1980 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001981 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01001982 }
1983 break;
1984 }
1985
1986 case Primitive::kPrimLong: {
1987 DCHECK(second.IsDoubleStackSlot());
1988
1989 Register in1_hi = first.AsRegisterPairHigh<Register>();
1990 Register in1_lo = first.AsRegisterPairLow<Register>();
1991 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
1992 Address in2_lo(ESP, second.GetStackIndex());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001993 Register eax = locations->GetTemp(0).AsRegister<Register>();
1994 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001995
1996 DCHECK_EQ(EAX, eax);
1997 DCHECK_EQ(EDX, edx);
1998
1999 // input: in1 - 64 bits, in2 - 64 bits
2000 // output: in1
2001 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2002 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2003 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
2004
2005 __ movl(eax, in2_hi);
2006 // eax <- in1.lo * in2.hi
2007 __ imull(eax, in1_lo);
2008 // in1.hi <- in1.hi * in2.lo
2009 __ imull(in1_hi, in2_lo);
2010 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2011 __ addl(in1_hi, eax);
2012 // move in1_lo to eax to prepare for double precision
2013 __ movl(eax, in1_lo);
2014 // edx:eax <- in1.lo * in2.lo
2015 __ mull(in2_lo);
2016 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2017 __ addl(in1_hi, edx);
2018 // in1.lo <- (in1.lo * in2.lo)[31:0];
2019 __ movl(in1_lo, eax);
2020
2021 break;
2022 }
2023
Calin Juravleb5bfa962014-10-21 18:02:24 +01002024 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002025 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002026 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002027 }
2028
2029 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002030 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002031 break;
2032 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002033
2034 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002035 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002036 }
2037}
2038
Calin Juravlebacfec32014-11-14 15:54:36 +00002039void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2040 DCHECK(instruction->IsDiv() || instruction->IsRem());
2041
2042 LocationSummary* locations = instruction->GetLocations();
2043 Location out = locations->Out();
2044 Location first = locations->InAt(0);
2045 Location second = locations->InAt(1);
2046 bool is_div = instruction->IsDiv();
2047
2048 switch (instruction->GetResultType()) {
2049 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002050 Register second_reg = second.AsRegister<Register>();
2051 DCHECK_EQ(EAX, first.AsRegister<Register>());
2052 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002053
2054 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002055 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
2056 is_div);
Calin Juravlebacfec32014-11-14 15:54:36 +00002057 codegen_->AddSlowPath(slow_path);
2058
2059 // 0x80000000/-1 triggers an arithmetic exception!
2060 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2061 // it's safe to just use negl instead of more complex comparisons.
2062
2063 __ cmpl(second_reg, Immediate(-1));
2064 __ j(kEqual, slow_path->GetEntryLabel());
2065
2066 // edx:eax <- sign-extended of eax
2067 __ cdq();
2068 // eax = quotient, edx = remainder
2069 __ idivl(second_reg);
2070
2071 __ Bind(slow_path->GetExitLabel());
2072 break;
2073 }
2074
2075 case Primitive::kPrimLong: {
2076 InvokeRuntimeCallingConvention calling_convention;
2077 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2078 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2079 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2080 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2081 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2082 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2083
2084 if (is_div) {
2085 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2086 } else {
2087 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2088 }
2089 uint32_t dex_pc = is_div
2090 ? instruction->AsDiv()->GetDexPc()
2091 : instruction->AsRem()->GetDexPc();
2092 codegen_->RecordPcInfo(instruction, dex_pc);
2093
2094 break;
2095 }
2096
2097 default:
2098 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2099 }
2100}
2101
Calin Juravle7c4954d2014-10-28 16:57:40 +00002102void LocationsBuilderX86::VisitDiv(HDiv* div) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002103 LocationSummary::CallKind call_kind = div->GetResultType() == Primitive::kPrimLong
2104 ? LocationSummary::kCall
2105 : LocationSummary::kNoCall;
2106 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2107
Calin Juravle7c4954d2014-10-28 16:57:40 +00002108 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002109 case Primitive::kPrimInt: {
2110 locations->SetInAt(0, Location::RegisterLocation(EAX));
2111 locations->SetInAt(1, Location::RequiresRegister());
2112 locations->SetOut(Location::SameAsFirstInput());
2113 // Intel uses edx:eax as the dividend.
2114 locations->AddTemp(Location::RegisterLocation(EDX));
2115 break;
2116 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002117 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002118 InvokeRuntimeCallingConvention calling_convention;
2119 locations->SetInAt(0, Location::RegisterPairLocation(
2120 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2121 locations->SetInAt(1, Location::RegisterPairLocation(
2122 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2123 // Runtime helper puts the result in EAX, EDX.
2124 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002125 break;
2126 }
2127 case Primitive::kPrimFloat:
2128 case Primitive::kPrimDouble: {
2129 locations->SetInAt(0, Location::RequiresFpuRegister());
2130 locations->SetInAt(1, Location::RequiresFpuRegister());
2131 locations->SetOut(Location::SameAsFirstInput());
2132 break;
2133 }
2134
2135 default:
2136 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2137 }
2138}
2139
2140void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2141 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002142 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002143 Location first = locations->InAt(0);
2144 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002145
2146 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002147 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002148 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002149 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002150 break;
2151 }
2152
2153 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002154 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002155 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002156 break;
2157 }
2158
2159 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002160 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002161 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002162 break;
2163 }
2164
2165 default:
2166 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2167 }
2168}
2169
Calin Juravlebacfec32014-11-14 15:54:36 +00002170void LocationsBuilderX86::VisitRem(HRem* rem) {
2171 LocationSummary::CallKind call_kind = rem->GetResultType() == Primitive::kPrimLong
2172 ? LocationSummary::kCall
2173 : LocationSummary::kNoCall;
2174 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2175
2176 switch (rem->GetResultType()) {
2177 case Primitive::kPrimInt: {
2178 locations->SetInAt(0, Location::RegisterLocation(EAX));
2179 locations->SetInAt(1, Location::RequiresRegister());
2180 locations->SetOut(Location::RegisterLocation(EDX));
2181 break;
2182 }
2183 case Primitive::kPrimLong: {
2184 InvokeRuntimeCallingConvention calling_convention;
2185 locations->SetInAt(0, Location::RegisterPairLocation(
2186 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2187 locations->SetInAt(1, Location::RegisterPairLocation(
2188 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2189 // Runtime helper puts the result in EAX, EDX.
2190 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2191 break;
2192 }
2193 case Primitive::kPrimFloat:
2194 case Primitive::kPrimDouble: {
2195 LOG(FATAL) << "Unimplemented rem type " << rem->GetResultType();
2196 break;
2197 }
2198
2199 default:
2200 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2201 }
2202}
2203
2204void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2205 Primitive::Type type = rem->GetResultType();
2206 switch (type) {
2207 case Primitive::kPrimInt:
2208 case Primitive::kPrimLong: {
2209 GenerateDivRemIntegral(rem);
2210 break;
2211 }
2212 case Primitive::kPrimFloat:
2213 case Primitive::kPrimDouble: {
2214 LOG(FATAL) << "Unimplemented rem type " << type;
2215 break;
2216 }
2217 default:
2218 LOG(FATAL) << "Unexpected rem type " << type;
2219 }
2220}
2221
Calin Juravled0d48522014-11-04 16:40:20 +00002222void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2223 LocationSummary* locations =
2224 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002225 switch (instruction->GetType()) {
2226 case Primitive::kPrimInt: {
2227 locations->SetInAt(0, Location::Any());
2228 break;
2229 }
2230 case Primitive::kPrimLong: {
2231 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2232 if (!instruction->IsConstant()) {
2233 locations->AddTemp(Location::RequiresRegister());
2234 }
2235 break;
2236 }
2237 default:
2238 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2239 }
Calin Juravled0d48522014-11-04 16:40:20 +00002240 if (instruction->HasUses()) {
2241 locations->SetOut(Location::SameAsFirstInput());
2242 }
2243}
2244
2245void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2246 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2247 codegen_->AddSlowPath(slow_path);
2248
2249 LocationSummary* locations = instruction->GetLocations();
2250 Location value = locations->InAt(0);
2251
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002252 switch (instruction->GetType()) {
2253 case Primitive::kPrimInt: {
2254 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002255 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002256 __ j(kEqual, slow_path->GetEntryLabel());
2257 } else if (value.IsStackSlot()) {
2258 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2259 __ j(kEqual, slow_path->GetEntryLabel());
2260 } else {
2261 DCHECK(value.IsConstant()) << value;
2262 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2263 __ jmp(slow_path->GetEntryLabel());
2264 }
2265 }
2266 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002267 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002268 case Primitive::kPrimLong: {
2269 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002270 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002271 __ movl(temp, value.AsRegisterPairLow<Register>());
2272 __ orl(temp, value.AsRegisterPairHigh<Register>());
2273 __ j(kEqual, slow_path->GetEntryLabel());
2274 } else {
2275 DCHECK(value.IsConstant()) << value;
2276 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2277 __ jmp(slow_path->GetEntryLabel());
2278 }
2279 }
2280 break;
2281 }
2282 default:
2283 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002284 }
Calin Juravled0d48522014-11-04 16:40:20 +00002285}
2286
Calin Juravle9aec02f2014-11-18 23:06:35 +00002287void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2288 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2289
2290 LocationSummary* locations =
2291 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2292
2293 switch (op->GetResultType()) {
2294 case Primitive::kPrimInt: {
2295 locations->SetInAt(0, Location::RequiresRegister());
2296 // The shift count needs to be in CL.
2297 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2298 locations->SetOut(Location::SameAsFirstInput());
2299 break;
2300 }
2301 case Primitive::kPrimLong: {
2302 locations->SetInAt(0, Location::RequiresRegister());
2303 // The shift count needs to be in CL.
2304 locations->SetInAt(1, Location::RegisterLocation(ECX));
2305 locations->SetOut(Location::SameAsFirstInput());
2306 break;
2307 }
2308 default:
2309 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2310 }
2311}
2312
2313void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2314 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2315
2316 LocationSummary* locations = op->GetLocations();
2317 Location first = locations->InAt(0);
2318 Location second = locations->InAt(1);
2319 DCHECK(first.Equals(locations->Out()));
2320
2321 switch (op->GetResultType()) {
2322 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002323 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002324 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002325 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002326 DCHECK_EQ(ECX, second_reg);
2327 if (op->IsShl()) {
2328 __ shll(first_reg, second_reg);
2329 } else if (op->IsShr()) {
2330 __ sarl(first_reg, second_reg);
2331 } else {
2332 __ shrl(first_reg, second_reg);
2333 }
2334 } else {
2335 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
2336 if (op->IsShl()) {
2337 __ shll(first_reg, imm);
2338 } else if (op->IsShr()) {
2339 __ sarl(first_reg, imm);
2340 } else {
2341 __ shrl(first_reg, imm);
2342 }
2343 }
2344 break;
2345 }
2346 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002347 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002348 DCHECK_EQ(ECX, second_reg);
2349 if (op->IsShl()) {
2350 GenerateShlLong(first, second_reg);
2351 } else if (op->IsShr()) {
2352 GenerateShrLong(first, second_reg);
2353 } else {
2354 GenerateUShrLong(first, second_reg);
2355 }
2356 break;
2357 }
2358 default:
2359 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2360 }
2361}
2362
2363void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2364 Label done;
2365 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2366 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2367 __ testl(shifter, Immediate(32));
2368 __ j(kEqual, &done);
2369 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2370 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2371 __ Bind(&done);
2372}
2373
2374void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2375 Label done;
2376 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2377 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2378 __ testl(shifter, Immediate(32));
2379 __ j(kEqual, &done);
2380 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2381 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2382 __ Bind(&done);
2383}
2384
2385void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2386 Label done;
2387 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2388 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2389 __ testl(shifter, Immediate(32));
2390 __ j(kEqual, &done);
2391 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2392 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2393 __ Bind(&done);
2394}
2395
2396void LocationsBuilderX86::VisitShl(HShl* shl) {
2397 HandleShift(shl);
2398}
2399
2400void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2401 HandleShift(shl);
2402}
2403
2404void LocationsBuilderX86::VisitShr(HShr* shr) {
2405 HandleShift(shr);
2406}
2407
2408void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2409 HandleShift(shr);
2410}
2411
2412void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2413 HandleShift(ushr);
2414}
2415
2416void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2417 HandleShift(ushr);
2418}
2419
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002420void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002421 LocationSummary* locations =
2422 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002423 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002424 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002425 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2426 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002427}
2428
2429void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2430 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002431 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002432 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002433
Nicolas Geoffray707c8092014-04-04 10:50:14 +01002434 __ fs()->call(
2435 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002436
Nicolas Geoffray39468442014-09-02 15:17:15 +01002437 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002438 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002439}
2440
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002441void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2442 LocationSummary* locations =
2443 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2444 locations->SetOut(Location::RegisterLocation(EAX));
2445 InvokeRuntimeCallingConvention calling_convention;
2446 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2447 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2448 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2449}
2450
2451void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2452 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002453 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002454 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2455
2456 __ fs()->call(
2457 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
2458
2459 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2460 DCHECK(!codegen_->IsLeafMethod());
2461}
2462
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002463void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002464 LocationSummary* locations =
2465 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002466 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2467 if (location.IsStackSlot()) {
2468 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2469 } else if (location.IsDoubleStackSlot()) {
2470 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002471 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002472 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002473}
2474
2475void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002476 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002477}
2478
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002479void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002480 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002481 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002482 locations->SetInAt(0, Location::RequiresRegister());
2483 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002484}
2485
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002486void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2487 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002488 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002489 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002490 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002491 switch (not_->InputAt(0)->GetType()) {
2492 case Primitive::kPrimBoolean:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002493 __ xorl(out.AsRegister<Register>(), Immediate(1));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002494 break;
2495
2496 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002497 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002498 break;
2499
2500 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002501 __ notl(out.AsRegisterPairLow<Register>());
2502 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002503 break;
2504
2505 default:
2506 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2507 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002508}
2509
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002510void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002511 LocationSummary* locations =
2512 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002513 switch (compare->InputAt(0)->GetType()) {
2514 case Primitive::kPrimLong: {
2515 locations->SetInAt(0, Location::RequiresRegister());
2516 // TODO: we set any here but we don't handle constants
2517 locations->SetInAt(1, Location::Any());
2518 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2519 break;
2520 }
2521 case Primitive::kPrimFloat:
2522 case Primitive::kPrimDouble: {
2523 locations->SetInAt(0, Location::RequiresFpuRegister());
2524 locations->SetInAt(1, Location::RequiresFpuRegister());
2525 locations->SetOut(Location::RequiresRegister());
2526 break;
2527 }
2528 default:
2529 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2530 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002531}
2532
2533void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002534 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002535 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002536 Location left = locations->InAt(0);
2537 Location right = locations->InAt(1);
2538
2539 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002540 switch (compare->InputAt(0)->GetType()) {
2541 case Primitive::kPrimLong: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002542 if (right.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002543 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002544 } else {
2545 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002546 __ cmpl(left.AsRegisterPairHigh<Register>(),
2547 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002548 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002549 __ j(kLess, &less); // Signed compare.
2550 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002551 if (right.IsRegisterPair()) {
2552 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002553 } else {
2554 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002555 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002556 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002557 break;
2558 }
2559 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002560 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002561 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
2562 break;
2563 }
2564 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002565 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002566 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002567 break;
2568 }
2569 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002570 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002571 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002572 __ movl(out, Immediate(0));
2573 __ j(kEqual, &done);
2574 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
2575
2576 __ Bind(&greater);
2577 __ movl(out, Immediate(1));
2578 __ jmp(&done);
2579
2580 __ Bind(&less);
2581 __ movl(out, Immediate(-1));
2582
2583 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002584}
2585
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002586void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002587 LocationSummary* locations =
2588 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002589 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2590 locations->SetInAt(i, Location::Any());
2591 }
2592 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002593}
2594
2595void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002596 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002597 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002598}
2599
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002600void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002601 LocationSummary* locations =
2602 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002603 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002604 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002605 bool needs_write_barrier =
2606 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
2607
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002608 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2609 || (field_type == Primitive::kPrimByte);
2610 // The register allocator does not support multiple
2611 // inputs that die at entry with one in a specific register.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002612 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002613 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002614 locations->SetInAt(1, Location::RegisterLocation(EAX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002615 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002616 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002617 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002618 // Temporary registers for the write barrier.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002619 if (needs_write_barrier) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002620 locations->AddTemp(Location::RequiresRegister());
2621 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002622 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002623 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002624}
2625
2626void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2627 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002628 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002629 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01002630 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002631
2632 switch (field_type) {
2633 case Primitive::kPrimBoolean:
2634 case Primitive::kPrimByte: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002635 ByteRegister value = locations->InAt(1).AsRegister<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002636 __ movb(Address(obj, offset), value);
2637 break;
2638 }
2639
2640 case Primitive::kPrimShort:
2641 case Primitive::kPrimChar: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002642 Register value = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002643 __ movw(Address(obj, offset), value);
2644 break;
2645 }
2646
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002647 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002648 case Primitive::kPrimNot: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002649 Register value = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002650 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002651
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002652 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002653 Register temp = locations->GetTemp(0).AsRegister<Register>();
2654 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002655 codegen_->MarkGCCard(temp, card, obj, value);
2656 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002657 break;
2658 }
2659
2660 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002661 Location value = locations->InAt(1);
2662 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
2663 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002664 break;
2665 }
2666
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002667 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002668 XmmRegister value = locations->InAt(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002669 __ movss(Address(obj, offset), value);
2670 break;
2671 }
2672
2673 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002674 XmmRegister value = locations->InAt(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002675 __ movsd(Address(obj, offset), value);
2676 break;
2677 }
2678
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002679 case Primitive::kPrimVoid:
2680 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002681 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002682 }
2683}
2684
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002685void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
2686 Label is_null;
2687 __ testl(value, value);
2688 __ j(kEqual, &is_null);
2689 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2690 __ movl(temp, object);
2691 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
2692 __ movb(Address(temp, card, TIMES_1, 0),
2693 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
2694 __ Bind(&is_null);
2695}
2696
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002697void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002698 LocationSummary* locations =
2699 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002700 locations->SetInAt(0, Location::RequiresRegister());
2701 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002702}
2703
2704void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2705 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002706 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002707 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2708
2709 switch (instruction->GetType()) {
2710 case Primitive::kPrimBoolean: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002711 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002712 __ movzxb(out, Address(obj, offset));
2713 break;
2714 }
2715
2716 case Primitive::kPrimByte: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002717 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002718 __ movsxb(out, Address(obj, offset));
2719 break;
2720 }
2721
2722 case Primitive::kPrimShort: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002723 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002724 __ movsxw(out, Address(obj, offset));
2725 break;
2726 }
2727
2728 case Primitive::kPrimChar: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002729 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002730 __ movzxw(out, Address(obj, offset));
2731 break;
2732 }
2733
2734 case Primitive::kPrimInt:
2735 case Primitive::kPrimNot: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002736 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002737 __ movl(out, Address(obj, offset));
2738 break;
2739 }
2740
2741 case Primitive::kPrimLong: {
2742 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002743 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
2744 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002745 break;
2746 }
2747
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002748 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002749 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002750 __ movss(out, Address(obj, offset));
2751 break;
2752 }
2753
2754 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002755 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002756 __ movsd(out, Address(obj, offset));
2757 break;
2758 }
2759
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002760 case Primitive::kPrimVoid:
2761 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002762 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002763 }
2764}
2765
2766void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002767 LocationSummary* locations =
2768 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002769 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002770 if (instruction->HasUses()) {
2771 locations->SetOut(Location::SameAsFirstInput());
2772 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002773}
2774
2775void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002776 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002777 codegen_->AddSlowPath(slow_path);
2778
2779 LocationSummary* locations = instruction->GetLocations();
2780 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002781
2782 if (obj.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002783 __ cmpl(obj.AsRegister<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002784 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002785 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002786 } else {
2787 DCHECK(obj.IsConstant()) << obj;
2788 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2789 __ jmp(slow_path->GetEntryLabel());
2790 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002791 }
2792 __ j(kEqual, slow_path->GetEntryLabel());
2793}
2794
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002795void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002796 LocationSummary* locations =
2797 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002798 locations->SetInAt(0, Location::RequiresRegister());
2799 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2800 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002801}
2802
2803void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
2804 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002805 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002806 Location index = locations->InAt(1);
2807
2808 switch (instruction->GetType()) {
2809 case Primitive::kPrimBoolean: {
2810 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002811 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002812 if (index.IsConstant()) {
2813 __ movzxb(out, Address(obj,
2814 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2815 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002816 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002817 }
2818 break;
2819 }
2820
2821 case Primitive::kPrimByte: {
2822 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002823 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002824 if (index.IsConstant()) {
2825 __ movsxb(out, Address(obj,
2826 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2827 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002828 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002829 }
2830 break;
2831 }
2832
2833 case Primitive::kPrimShort: {
2834 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002835 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002836 if (index.IsConstant()) {
2837 __ movsxw(out, Address(obj,
2838 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2839 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002840 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002841 }
2842 break;
2843 }
2844
2845 case Primitive::kPrimChar: {
2846 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002847 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002848 if (index.IsConstant()) {
2849 __ movzxw(out, Address(obj,
2850 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2851 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002852 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002853 }
2854 break;
2855 }
2856
2857 case Primitive::kPrimInt:
2858 case Primitive::kPrimNot: {
2859 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002860 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002861 if (index.IsConstant()) {
2862 __ movl(out, Address(obj,
2863 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2864 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002865 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002866 }
2867 break;
2868 }
2869
2870 case Primitive::kPrimLong: {
2871 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002872 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002873 if (index.IsConstant()) {
2874 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002875 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
2876 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002877 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002878 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00002879 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002880 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00002881 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002882 }
2883 break;
2884 }
2885
2886 case Primitive::kPrimFloat:
2887 case Primitive::kPrimDouble:
2888 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002889 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002890 case Primitive::kPrimVoid:
2891 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002892 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002893 }
2894}
2895
2896void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002897 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002898 bool needs_write_barrier =
2899 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2900
2901 DCHECK(kFollowsQuickABI);
2902 bool not_enough_registers = needs_write_barrier
2903 && !instruction->GetValue()->IsConstant()
2904 && !instruction->GetIndex()->IsConstant();
2905 bool needs_runtime_call = instruction->NeedsTypeCheck() || not_enough_registers;
2906
Nicolas Geoffray39468442014-09-02 15:17:15 +01002907 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2908 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002909 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002910
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002911 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002912 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002913 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2914 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2915 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002916 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002917 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
2918 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002919 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002920 // In case of a byte operation, the register allocator does not support multiple
2921 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002922 locations->SetInAt(0, Location::RequiresRegister());
2923 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002924 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002925 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002926 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002927 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002928 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002929 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002930 // Temporary registers for the write barrier.
2931 if (needs_write_barrier) {
2932 locations->AddTemp(Location::RequiresRegister());
2933 // Ensure the card is in a byte register.
2934 locations->AddTemp(Location::RegisterLocation(ECX));
2935 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002936 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002937}
2938
2939void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
2940 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002941 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002942 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002943 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002944 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002945 bool needs_runtime_call = locations->WillCall();
2946 bool needs_write_barrier =
2947 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002948
2949 switch (value_type) {
2950 case Primitive::kPrimBoolean:
2951 case Primitive::kPrimByte: {
2952 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002953 if (index.IsConstant()) {
2954 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002955 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002956 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002957 } else {
2958 __ movb(Address(obj, offset),
2959 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2960 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002961 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002962 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002963 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
2964 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002965 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002966 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002967 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2968 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002969 }
2970 break;
2971 }
2972
2973 case Primitive::kPrimShort:
2974 case Primitive::kPrimChar: {
2975 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002976 if (index.IsConstant()) {
2977 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002978 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002979 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002980 } else {
2981 __ movw(Address(obj, offset),
2982 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2983 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002984 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002985 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002986 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
2987 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002988 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002989 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002990 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2991 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002992 }
2993 break;
2994 }
2995
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002996 case Primitive::kPrimInt:
2997 case Primitive::kPrimNot: {
2998 if (!needs_runtime_call) {
2999 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3000 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003001 size_t offset =
3002 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003003 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003004 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003005 } else {
3006 DCHECK(value.IsConstant()) << value;
3007 __ movl(Address(obj, offset),
3008 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3009 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003010 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003011 DCHECK(index.IsRegister()) << index;
3012 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003013 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3014 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003015 } else {
3016 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003017 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003018 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3019 }
3020 }
3021
3022 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003023 Register temp = locations->GetTemp(0).AsRegister<Register>();
3024 Register card = locations->GetTemp(1).AsRegister<Register>();
3025 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003026 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003027 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003028 DCHECK_EQ(value_type, Primitive::kPrimNot);
3029 DCHECK(!codegen_->IsLeafMethod());
3030 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3031 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003032 }
3033 break;
3034 }
3035
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003036 case Primitive::kPrimLong: {
3037 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003038 if (index.IsConstant()) {
3039 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003040 if (value.IsRegisterPair()) {
3041 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
3042 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003043 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003044 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003045 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3046 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
3047 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3048 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003049 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003050 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003051 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003052 value.AsRegisterPairLow<Register>());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003053 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003054 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003055 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003056 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003057 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003058 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003059 Immediate(Low32Bits(val)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003060 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003061 Immediate(High32Bits(val)));
3062 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003063 }
3064 break;
3065 }
3066
3067 case Primitive::kPrimFloat:
3068 case Primitive::kPrimDouble:
3069 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003070 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003071 case Primitive::kPrimVoid:
3072 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003073 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003074 }
3075}
3076
3077void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3078 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003079 locations->SetInAt(0, Location::RequiresRegister());
3080 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003081 instruction->SetLocations(locations);
3082}
3083
3084void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3085 LocationSummary* locations = instruction->GetLocations();
3086 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003087 Register obj = locations->InAt(0).AsRegister<Register>();
3088 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003089 __ movl(out, Address(obj, offset));
3090}
3091
3092void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003093 LocationSummary* locations =
3094 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003095 locations->SetInAt(0, Location::RequiresRegister());
3096 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003097 if (instruction->HasUses()) {
3098 locations->SetOut(Location::SameAsFirstInput());
3099 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003100}
3101
3102void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3103 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003104 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01003105 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003106 codegen_->AddSlowPath(slow_path);
3107
Roland Levillain271ab9c2014-11-27 15:23:57 +00003108 Register index = locations->InAt(0).AsRegister<Register>();
3109 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003110
3111 __ cmpl(index, length);
3112 __ j(kAboveEqual, slow_path->GetEntryLabel());
3113}
3114
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003115void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3116 temp->SetLocations(nullptr);
3117}
3118
3119void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3120 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003121 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003122}
3123
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003124void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003125 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003126 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003127}
3128
3129void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003130 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3131}
3132
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003133void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3134 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3135}
3136
3137void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003138 HBasicBlock* block = instruction->GetBlock();
3139 if (block->GetLoopInformation() != nullptr) {
3140 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3141 // The back edge will generate the suspend check.
3142 return;
3143 }
3144 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3145 // The goto will generate the suspend check.
3146 return;
3147 }
3148 GenerateSuspendCheck(instruction, nullptr);
3149}
3150
3151void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3152 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003153 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003154 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003155 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003156 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003157 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003158 if (successor == nullptr) {
3159 __ j(kNotEqual, slow_path->GetEntryLabel());
3160 __ Bind(slow_path->GetReturnLabel());
3161 } else {
3162 __ j(kEqual, codegen_->GetLabelOf(successor));
3163 __ jmp(slow_path->GetEntryLabel());
3164 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003165}
3166
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003167X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3168 return codegen_->GetAssembler();
3169}
3170
3171void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
3172 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003173 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003174 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3175 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
3176 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
3177}
3178
3179void ParallelMoveResolverX86::EmitMove(size_t index) {
3180 MoveOperands* move = moves_.Get(index);
3181 Location source = move->GetSource();
3182 Location destination = move->GetDestination();
3183
3184 if (source.IsRegister()) {
3185 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003186 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003187 } else {
3188 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003189 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003190 }
3191 } else if (source.IsStackSlot()) {
3192 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003193 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003194 } else {
3195 DCHECK(destination.IsStackSlot());
3196 MoveMemoryToMemory(destination.GetStackIndex(),
3197 source.GetStackIndex());
3198 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003199 } else if (source.IsConstant()) {
3200 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
3201 Immediate imm(instruction->AsIntConstant()->GetValue());
3202 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003203 __ movl(destination.AsRegister<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003204 } else {
3205 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3206 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003207 } else {
3208 LOG(FATAL) << "Unimplemented";
3209 }
3210}
3211
3212void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003213 Register suggested_scratch = reg == EAX ? EBX : EAX;
3214 ScratchRegisterScope ensure_scratch(
3215 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3216
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003217 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3218 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
3219 __ movl(Address(ESP, mem + stack_offset), reg);
3220 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
3221}
3222
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003223void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
3224 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003225 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3226
3227 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003228 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003229 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3230
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003231 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
3232 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
3233 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
3234 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
3235 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
3236 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
3237}
3238
3239void ParallelMoveResolverX86::EmitSwap(size_t index) {
3240 MoveOperands* move = moves_.Get(index);
3241 Location source = move->GetSource();
3242 Location destination = move->GetDestination();
3243
3244 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003245 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003246 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003247 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003248 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003249 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003250 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3251 Exchange(destination.GetStackIndex(), source.GetStackIndex());
3252 } else {
3253 LOG(FATAL) << "Unimplemented";
3254 }
3255}
3256
3257void ParallelMoveResolverX86::SpillScratch(int reg) {
3258 __ pushl(static_cast<Register>(reg));
3259}
3260
3261void ParallelMoveResolverX86::RestoreScratch(int reg) {
3262 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003263}
3264
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003265void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003266 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3267 ? LocationSummary::kCallOnSlowPath
3268 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003269 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003270 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003271 locations->SetOut(Location::RequiresRegister());
3272}
3273
3274void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003275 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003276 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003277 DCHECK(!cls->CanCallRuntime());
3278 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003279 codegen_->LoadCurrentMethod(out);
3280 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3281 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003282 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003283 codegen_->LoadCurrentMethod(out);
3284 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3285 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003286
3287 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3288 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3289 codegen_->AddSlowPath(slow_path);
3290 __ testl(out, out);
3291 __ j(kEqual, slow_path->GetEntryLabel());
3292 if (cls->MustGenerateClinitCheck()) {
3293 GenerateClassInitializationCheck(slow_path, out);
3294 } else {
3295 __ Bind(slow_path->GetExitLabel());
3296 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003297 }
3298}
3299
3300void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
3301 LocationSummary* locations =
3302 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3303 locations->SetInAt(0, Location::RequiresRegister());
3304 if (check->HasUses()) {
3305 locations->SetOut(Location::SameAsFirstInput());
3306 }
3307}
3308
3309void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003310 // We assume the class to not be null.
3311 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3312 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003313 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003314 GenerateClassInitializationCheck(slow_path,
3315 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003316}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003317
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003318void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
3319 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003320 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3321 Immediate(mirror::Class::kStatusInitialized));
3322 __ j(kLess, slow_path->GetEntryLabel());
3323 __ Bind(slow_path->GetExitLabel());
3324 // No need for memory fence, thanks to the X86 memory model.
3325}
3326
3327void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3328 LocationSummary* locations =
3329 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3330 locations->SetInAt(0, Location::RequiresRegister());
3331 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3332}
3333
3334void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3335 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003336 Register cls = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003337 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
3338
3339 switch (instruction->GetType()) {
3340 case Primitive::kPrimBoolean: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003341 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003342 __ movzxb(out, Address(cls, offset));
3343 break;
3344 }
3345
3346 case Primitive::kPrimByte: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003347 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003348 __ movsxb(out, Address(cls, offset));
3349 break;
3350 }
3351
3352 case Primitive::kPrimShort: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003353 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003354 __ movsxw(out, Address(cls, offset));
3355 break;
3356 }
3357
3358 case Primitive::kPrimChar: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003359 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003360 __ movzxw(out, Address(cls, offset));
3361 break;
3362 }
3363
3364 case Primitive::kPrimInt:
3365 case Primitive::kPrimNot: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003366 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003367 __ movl(out, Address(cls, offset));
3368 break;
3369 }
3370
3371 case Primitive::kPrimLong: {
3372 // TODO: support volatile.
3373 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(cls, offset));
3374 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(cls, kX86WordSize + offset));
3375 break;
3376 }
3377
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003378 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003379 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003380 __ movss(out, Address(cls, offset));
3381 break;
3382 }
3383
3384 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003385 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003386 __ movsd(out, Address(cls, offset));
3387 break;
3388 }
3389
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003390 case Primitive::kPrimVoid:
3391 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3392 UNREACHABLE();
3393 }
3394}
3395
3396void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3397 LocationSummary* locations =
3398 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3399 locations->SetInAt(0, Location::RequiresRegister());
3400 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003401 bool needs_write_barrier =
3402 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003403 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3404 || (field_type == Primitive::kPrimByte);
3405 // The register allocator does not support multiple
3406 // inputs that die at entry with one in a specific register.
3407 if (is_byte_type) {
3408 // Ensure the value is in a byte register.
3409 locations->SetInAt(1, Location::RegisterLocation(EAX));
3410 } else {
3411 locations->SetInAt(1, Location::RequiresRegister());
3412 }
3413 // Temporary registers for the write barrier.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003414 if (needs_write_barrier) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003415 locations->AddTemp(Location::RequiresRegister());
3416 // Ensure the card is in a byte register.
3417 locations->AddTemp(Location::RegisterLocation(ECX));
3418 }
3419}
3420
3421void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3422 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003423 Register cls = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003424 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
3425 Primitive::Type field_type = instruction->GetFieldType();
3426
3427 switch (field_type) {
3428 case Primitive::kPrimBoolean:
3429 case Primitive::kPrimByte: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003430 ByteRegister value = locations->InAt(1).AsRegister<ByteRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003431 __ movb(Address(cls, offset), value);
3432 break;
3433 }
3434
3435 case Primitive::kPrimShort:
3436 case Primitive::kPrimChar: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003437 Register value = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003438 __ movw(Address(cls, offset), value);
3439 break;
3440 }
3441
3442 case Primitive::kPrimInt:
3443 case Primitive::kPrimNot: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003444 Register value = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003445 __ movl(Address(cls, offset), value);
3446
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003447 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003448 Register temp = locations->GetTemp(0).AsRegister<Register>();
3449 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003450 codegen_->MarkGCCard(temp, card, cls, value);
3451 }
3452 break;
3453 }
3454
3455 case Primitive::kPrimLong: {
3456 Location value = locations->InAt(1);
3457 __ movl(Address(cls, offset), value.AsRegisterPairLow<Register>());
3458 __ movl(Address(cls, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3459 break;
3460 }
3461
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003462 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003463 XmmRegister value = locations->InAt(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003464 __ movss(Address(cls, offset), value);
3465 break;
3466 }
3467
3468 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003469 XmmRegister value = locations->InAt(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003470 __ movsd(Address(cls, offset), value);
3471 break;
3472 }
3473
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003474 case Primitive::kPrimVoid:
3475 LOG(FATAL) << "Unreachable type " << field_type;
3476 UNREACHABLE();
3477 }
3478}
3479
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003480void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
3481 LocationSummary* locations =
3482 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3483 locations->SetOut(Location::RequiresRegister());
3484}
3485
3486void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
3487 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
3488 codegen_->AddSlowPath(slow_path);
3489
Roland Levillain271ab9c2014-11-27 15:23:57 +00003490 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003491 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08003492 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3493 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003494 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3495 __ testl(out, out);
3496 __ j(kEqual, slow_path->GetEntryLabel());
3497 __ Bind(slow_path->GetExitLabel());
3498}
3499
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003500void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
3501 LocationSummary* locations =
3502 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3503 locations->SetOut(Location::RequiresRegister());
3504}
3505
3506void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
3507 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003508 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003509 __ fs()->movl(address, Immediate(0));
3510}
3511
3512void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
3513 LocationSummary* locations =
3514 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3515 InvokeRuntimeCallingConvention calling_convention;
3516 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3517}
3518
3519void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
3520 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
3521 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3522}
3523
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003524void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003525 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3526 ? LocationSummary::kNoCall
3527 : LocationSummary::kCallOnSlowPath;
3528 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3529 locations->SetInAt(0, Location::RequiresRegister());
3530 locations->SetInAt(1, Location::Any());
3531 locations->SetOut(Location::RequiresRegister());
3532}
3533
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003534void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003535 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003536 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003537 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003538 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003539 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3540 Label done, zero;
3541 SlowPathCodeX86* slow_path = nullptr;
3542
3543 // Return 0 if `obj` is null.
3544 // TODO: avoid this check if we know obj is not null.
3545 __ testl(obj, obj);
3546 __ j(kEqual, &zero);
3547 __ movl(out, Address(obj, class_offset));
3548 // Compare the class of `obj` with `cls`.
3549 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003550 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003551 } else {
3552 DCHECK(cls.IsStackSlot()) << cls;
3553 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
3554 }
3555
3556 if (instruction->IsClassFinal()) {
3557 // Classes must be equal for the instanceof to succeed.
3558 __ j(kNotEqual, &zero);
3559 __ movl(out, Immediate(1));
3560 __ jmp(&done);
3561 } else {
3562 // If the classes are not equal, we go into a slow path.
3563 DCHECK(locations->OnlyCallsOnSlowPath());
3564 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003565 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003566 codegen_->AddSlowPath(slow_path);
3567 __ j(kNotEqual, slow_path->GetEntryLabel());
3568 __ movl(out, Immediate(1));
3569 __ jmp(&done);
3570 }
3571 __ Bind(&zero);
3572 __ movl(out, Immediate(0));
3573 if (slow_path != nullptr) {
3574 __ Bind(slow_path->GetExitLabel());
3575 }
3576 __ Bind(&done);
3577}
3578
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003579void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
3580 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3581 instruction, LocationSummary::kCallOnSlowPath);
3582 locations->SetInAt(0, Location::RequiresRegister());
3583 locations->SetInAt(1, Location::Any());
3584 locations->AddTemp(Location::RequiresRegister());
3585}
3586
3587void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
3588 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003589 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003590 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003591 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003592 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3593 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
3594 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3595 codegen_->AddSlowPath(slow_path);
3596
3597 // TODO: avoid this check if we know obj is not null.
3598 __ testl(obj, obj);
3599 __ j(kEqual, slow_path->GetExitLabel());
3600 __ movl(temp, Address(obj, class_offset));
3601
3602 // Compare the class of `obj` with `cls`.
3603 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003604 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003605 } else {
3606 DCHECK(cls.IsStackSlot()) << cls;
3607 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
3608 }
3609
3610 __ j(kNotEqual, slow_path->GetEntryLabel());
3611 __ Bind(slow_path->GetExitLabel());
3612}
3613
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003614void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3615 LocationSummary* locations =
3616 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3617 InvokeRuntimeCallingConvention calling_convention;
3618 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3619}
3620
3621void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3622 __ fs()->call(Address::Absolute(instruction->IsEnter()
3623 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
3624 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
3625 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3626}
3627
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003628void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3629void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3630void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3631
3632void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3633 LocationSummary* locations =
3634 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3635 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3636 || instruction->GetResultType() == Primitive::kPrimLong);
3637 locations->SetInAt(0, Location::RequiresRegister());
3638 locations->SetInAt(1, Location::Any());
3639 locations->SetOut(Location::SameAsFirstInput());
3640}
3641
3642void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
3643 HandleBitwiseOperation(instruction);
3644}
3645
3646void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
3647 HandleBitwiseOperation(instruction);
3648}
3649
3650void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
3651 HandleBitwiseOperation(instruction);
3652}
3653
3654void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3655 LocationSummary* locations = instruction->GetLocations();
3656 Location first = locations->InAt(0);
3657 Location second = locations->InAt(1);
3658 DCHECK(first.Equals(locations->Out()));
3659
3660 if (instruction->GetResultType() == Primitive::kPrimInt) {
3661 if (second.IsRegister()) {
3662 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003663 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003664 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003665 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003666 } else {
3667 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003668 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003669 }
3670 } else if (second.IsConstant()) {
3671 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003672 __ andl(first.AsRegister<Register>(),
3673 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003674 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003675 __ orl(first.AsRegister<Register>(),
3676 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003677 } else {
3678 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00003679 __ xorl(first.AsRegister<Register>(),
3680 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003681 }
3682 } else {
3683 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003684 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003685 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003686 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003687 } else {
3688 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003689 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003690 }
3691 }
3692 } else {
3693 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3694 if (second.IsRegisterPair()) {
3695 if (instruction->IsAnd()) {
3696 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3697 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3698 } else if (instruction->IsOr()) {
3699 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3700 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3701 } else {
3702 DCHECK(instruction->IsXor());
3703 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3704 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3705 }
3706 } else {
3707 if (instruction->IsAnd()) {
3708 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3709 __ andl(first.AsRegisterPairHigh<Register>(),
3710 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3711 } else if (instruction->IsOr()) {
3712 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3713 __ orl(first.AsRegisterPairHigh<Register>(),
3714 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3715 } else {
3716 DCHECK(instruction->IsXor());
3717 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3718 __ xorl(first.AsRegisterPairHigh<Register>(),
3719 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3720 }
3721 }
3722 }
3723}
3724
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003725} // namespace x86
3726} // namespace art