blob: 8a0c2deab9e6a42b87edb995541c6ac6c5cc68a4 [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
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001333 // The float-to-long and double-to-long type conversions rely on a
1334 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001335 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001336 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1337 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001338 ? LocationSummary::kCall
1339 : LocationSummary::kNoCall;
1340 LocationSummary* locations =
1341 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1342
Roland Levillaindff1f282014-11-05 14:15:05 +00001343 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001344 case Primitive::kPrimByte:
1345 switch (input_type) {
1346 case Primitive::kPrimShort:
1347 case Primitive::kPrimInt:
1348 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001349 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001350 locations->SetInAt(0, Location::Any());
1351 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1352 break;
1353
1354 default:
1355 LOG(FATAL) << "Unexpected type conversion from " << input_type
1356 << " to " << result_type;
1357 }
1358 break;
1359
Roland Levillain01a8d712014-11-14 16:27:39 +00001360 case Primitive::kPrimShort:
1361 switch (input_type) {
1362 case Primitive::kPrimByte:
1363 case Primitive::kPrimInt:
1364 case Primitive::kPrimChar:
1365 // Processing a Dex `int-to-short' instruction.
1366 locations->SetInAt(0, Location::Any());
1367 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1368 break;
1369
1370 default:
1371 LOG(FATAL) << "Unexpected type conversion from " << input_type
1372 << " to " << result_type;
1373 }
1374 break;
1375
Roland Levillain946e1432014-11-11 17:35:19 +00001376 case Primitive::kPrimInt:
1377 switch (input_type) {
1378 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001379 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001380 locations->SetInAt(0, Location::Any());
1381 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1382 break;
1383
1384 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001385 // Processing a Dex `float-to-int' instruction.
1386 locations->SetInAt(0, Location::RequiresFpuRegister());
1387 locations->SetOut(Location::RequiresRegister());
1388 locations->AddTemp(Location::RequiresFpuRegister());
1389 break;
1390
Roland Levillain946e1432014-11-11 17:35:19 +00001391 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001392 // Processing a Dex `double-to-int' instruction.
1393 locations->SetInAt(0, Location::RequiresFpuRegister());
1394 locations->SetOut(Location::RequiresRegister());
1395 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001396 break;
1397
1398 default:
1399 LOG(FATAL) << "Unexpected type conversion from " << input_type
1400 << " to " << result_type;
1401 }
1402 break;
1403
Roland Levillaindff1f282014-11-05 14:15:05 +00001404 case Primitive::kPrimLong:
1405 switch (input_type) {
1406 case Primitive::kPrimByte:
1407 case Primitive::kPrimShort:
1408 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001409 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001410 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001411 locations->SetInAt(0, Location::RegisterLocation(EAX));
1412 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1413 break;
1414
Roland Levillain624279f2014-12-04 11:54:28 +00001415 case Primitive::kPrimFloat: {
1416 // Processing a Dex `float-to-long' instruction.
1417 InvokeRuntimeCallingConvention calling_convention;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001418 // Note that on x86 floating-point parameters are passed
1419 // through core registers (here, EAX).
1420 locations->SetInAt(0, Location::RegisterLocation(
1421 calling_convention.GetRegisterAt(0)));
Roland Levillain624279f2014-12-04 11:54:28 +00001422 // The runtime helper puts the result in EAX, EDX.
1423 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1424 break;
1425 }
1426
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001427 case Primitive::kPrimDouble: {
1428 // Processing a Dex `double-to-long' instruction.
1429 InvokeRuntimeCallingConvention calling_convention;
1430 // Note that on x86 floating-point parameters are passed
1431 // through core registers (here, EAX and ECX).
1432 locations->SetInAt(0, Location::RegisterPairLocation(
1433 calling_convention.GetRegisterAt(0),
1434 calling_convention.GetRegisterAt(1)));
1435 // The runtime helper puts the result in EAX, EDX.
1436 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1437 break;
1438 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001439 break;
1440
1441 default:
1442 LOG(FATAL) << "Unexpected type conversion from " << input_type
1443 << " to " << result_type;
1444 }
1445 break;
1446
Roland Levillain981e4542014-11-14 11:47:14 +00001447 case Primitive::kPrimChar:
1448 switch (input_type) {
1449 case Primitive::kPrimByte:
1450 case Primitive::kPrimShort:
1451 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001452 // Processing a Dex `int-to-char' instruction.
1453 locations->SetInAt(0, Location::Any());
1454 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1455 break;
1456
1457 default:
1458 LOG(FATAL) << "Unexpected type conversion from " << input_type
1459 << " to " << result_type;
1460 }
1461 break;
1462
Roland Levillaindff1f282014-11-05 14:15:05 +00001463 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001464 switch (input_type) {
1465 case Primitive::kPrimByte:
1466 case Primitive::kPrimShort:
1467 case Primitive::kPrimInt:
1468 case Primitive::kPrimChar:
1469 // Processing a Dex `int-to-float' instruction.
1470 locations->SetInAt(0, Location::RequiresRegister());
1471 locations->SetOut(Location::RequiresFpuRegister());
1472 break;
1473
1474 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001475 // Processing a Dex `long-to-float' instruction.
1476 locations->SetInAt(0, Location::RequiresRegister());
1477 locations->SetOut(Location::RequiresFpuRegister());
1478 locations->AddTemp(Location::RequiresFpuRegister());
1479 locations->AddTemp(Location::RequiresFpuRegister());
1480 break;
1481
Roland Levillaincff13742014-11-17 14:32:17 +00001482 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001483 // Processing a Dex `double-to-float' instruction.
1484 locations->SetInAt(0, Location::RequiresFpuRegister());
1485 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001486 break;
1487
1488 default:
1489 LOG(FATAL) << "Unexpected type conversion from " << input_type
1490 << " to " << result_type;
1491 };
1492 break;
1493
Roland Levillaindff1f282014-11-05 14:15:05 +00001494 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001495 switch (input_type) {
1496 case Primitive::kPrimByte:
1497 case Primitive::kPrimShort:
1498 case Primitive::kPrimInt:
1499 case Primitive::kPrimChar:
1500 // Processing a Dex `int-to-double' instruction.
1501 locations->SetInAt(0, Location::RequiresRegister());
1502 locations->SetOut(Location::RequiresFpuRegister());
1503 break;
1504
1505 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001506 // Processing a Dex `long-to-double' instruction.
1507 locations->SetInAt(0, Location::RequiresRegister());
1508 locations->SetOut(Location::RequiresFpuRegister());
1509 locations->AddTemp(Location::RequiresFpuRegister());
1510 locations->AddTemp(Location::RequiresFpuRegister());
1511 break;
1512
Roland Levillaincff13742014-11-17 14:32:17 +00001513 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001514 // Processing a Dex `float-to-double' instruction.
1515 locations->SetInAt(0, Location::RequiresFpuRegister());
1516 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001517 break;
1518
1519 default:
1520 LOG(FATAL) << "Unexpected type conversion from " << input_type
1521 << " to " << result_type;
1522 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001523 break;
1524
1525 default:
1526 LOG(FATAL) << "Unexpected type conversion from " << input_type
1527 << " to " << result_type;
1528 }
1529}
1530
1531void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1532 LocationSummary* locations = conversion->GetLocations();
1533 Location out = locations->Out();
1534 Location in = locations->InAt(0);
1535 Primitive::Type result_type = conversion->GetResultType();
1536 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001537 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001538 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001539 case Primitive::kPrimByte:
1540 switch (input_type) {
1541 case Primitive::kPrimShort:
1542 case Primitive::kPrimInt:
1543 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001544 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001545 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001546 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001547 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001548 __ movsxb(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain51d3fc42014-11-13 14:11:42 +00001549 } else {
1550 DCHECK(in.GetConstant()->IsIntConstant());
1551 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001552 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
Roland Levillain51d3fc42014-11-13 14:11:42 +00001553 }
1554 break;
1555
1556 default:
1557 LOG(FATAL) << "Unexpected type conversion from " << input_type
1558 << " to " << result_type;
1559 }
1560 break;
1561
Roland Levillain01a8d712014-11-14 16:27:39 +00001562 case Primitive::kPrimShort:
1563 switch (input_type) {
1564 case Primitive::kPrimByte:
1565 case Primitive::kPrimInt:
1566 case Primitive::kPrimChar:
1567 // Processing a Dex `int-to-short' instruction.
1568 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001569 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001570 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001571 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001572 } else {
1573 DCHECK(in.GetConstant()->IsIntConstant());
1574 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001575 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001576 }
1577 break;
1578
1579 default:
1580 LOG(FATAL) << "Unexpected type conversion from " << input_type
1581 << " to " << result_type;
1582 }
1583 break;
1584
Roland Levillain946e1432014-11-11 17:35:19 +00001585 case Primitive::kPrimInt:
1586 switch (input_type) {
1587 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001588 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001589 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001590 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001591 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001592 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001593 } else {
1594 DCHECK(in.IsConstant());
1595 DCHECK(in.GetConstant()->IsLongConstant());
1596 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001597 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001598 }
1599 break;
1600
Roland Levillain3f8f9362014-12-02 17:45:01 +00001601 case Primitive::kPrimFloat: {
1602 // Processing a Dex `float-to-int' instruction.
1603 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1604 Register output = out.AsRegister<Register>();
1605 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1606 Label done, nan;
1607
1608 __ movl(output, Immediate(kPrimIntMax));
1609 // temp = int-to-float(output)
1610 __ cvtsi2ss(temp, output);
1611 // if input >= temp goto done
1612 __ comiss(input, temp);
1613 __ j(kAboveEqual, &done);
1614 // if input == NaN goto nan
1615 __ j(kUnordered, &nan);
1616 // output = float-to-int-truncate(input)
1617 __ cvttss2si(output, input);
1618 __ jmp(&done);
1619 __ Bind(&nan);
1620 // output = 0
1621 __ xorl(output, output);
1622 __ Bind(&done);
1623 break;
1624 }
1625
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001626 case Primitive::kPrimDouble: {
1627 // Processing a Dex `double-to-int' instruction.
1628 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1629 Register output = out.AsRegister<Register>();
1630 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1631 Label done, nan;
1632
1633 __ movl(output, Immediate(kPrimIntMax));
1634 // temp = int-to-double(output)
1635 __ cvtsi2sd(temp, output);
1636 // if input >= temp goto done
1637 __ comisd(input, temp);
1638 __ j(kAboveEqual, &done);
1639 // if input == NaN goto nan
1640 __ j(kUnordered, &nan);
1641 // output = double-to-int-truncate(input)
1642 __ cvttsd2si(output, input);
1643 __ jmp(&done);
1644 __ Bind(&nan);
1645 // output = 0
1646 __ xorl(output, output);
1647 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001648 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001649 }
Roland Levillain946e1432014-11-11 17:35:19 +00001650
1651 default:
1652 LOG(FATAL) << "Unexpected type conversion from " << input_type
1653 << " to " << result_type;
1654 }
1655 break;
1656
Roland Levillaindff1f282014-11-05 14:15:05 +00001657 case Primitive::kPrimLong:
1658 switch (input_type) {
1659 case Primitive::kPrimByte:
1660 case Primitive::kPrimShort:
1661 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001662 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001663 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001664 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1665 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001666 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001667 __ cdq();
1668 break;
1669
1670 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001671 // Processing a Dex `float-to-long' instruction.
1672 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001673 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1674 break;
1675
Roland Levillaindff1f282014-11-05 14:15:05 +00001676 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001677 // Processing a Dex `double-to-long' instruction.
1678 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1679 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001680 break;
1681
1682 default:
1683 LOG(FATAL) << "Unexpected type conversion from " << input_type
1684 << " to " << result_type;
1685 }
1686 break;
1687
Roland Levillain981e4542014-11-14 11:47:14 +00001688 case Primitive::kPrimChar:
1689 switch (input_type) {
1690 case Primitive::kPrimByte:
1691 case Primitive::kPrimShort:
1692 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001693 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1694 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001695 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001696 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001697 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001698 } else {
1699 DCHECK(in.GetConstant()->IsIntConstant());
1700 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001701 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001702 }
1703 break;
1704
1705 default:
1706 LOG(FATAL) << "Unexpected type conversion from " << input_type
1707 << " to " << result_type;
1708 }
1709 break;
1710
Roland Levillaindff1f282014-11-05 14:15:05 +00001711 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001712 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001713 case Primitive::kPrimByte:
1714 case Primitive::kPrimShort:
1715 case Primitive::kPrimInt:
1716 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001717 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001718 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001719 break;
1720
Roland Levillain6d0e4832014-11-27 18:31:21 +00001721 case Primitive::kPrimLong: {
1722 // Processing a Dex `long-to-float' instruction.
1723 Register low = in.AsRegisterPairLow<Register>();
1724 Register high = in.AsRegisterPairHigh<Register>();
1725 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1726 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1727 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1728
1729 // Operations use doubles for precision reasons (each 32-bit
1730 // half of a long fits in the 53-bit mantissa of a double,
1731 // but not in the 24-bit mantissa of a float). This is
1732 // especially important for the low bits. The result is
1733 // eventually converted to float.
1734
1735 // low = low - 2^31 (to prevent bit 31 of `low` to be
1736 // interpreted as a sign bit)
1737 __ subl(low, Immediate(0x80000000));
1738 // temp = int-to-double(high)
1739 __ cvtsi2sd(temp, high);
1740 // temp = temp * 2^32
1741 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
1742 __ mulsd(temp, constant);
1743 // result = int-to-double(low)
1744 __ cvtsi2sd(result, low);
1745 // result = result + 2^31 (restore the original value of `low`)
1746 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
1747 __ addsd(result, constant);
1748 // result = result + temp
1749 __ addsd(result, temp);
1750 // result = double-to-float(result)
1751 __ cvtsd2ss(result, result);
1752 break;
1753 }
1754
Roland Levillaincff13742014-11-17 14:32:17 +00001755 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001756 // Processing a Dex `double-to-float' instruction.
1757 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001758 break;
1759
1760 default:
1761 LOG(FATAL) << "Unexpected type conversion from " << input_type
1762 << " to " << result_type;
1763 };
1764 break;
1765
Roland Levillaindff1f282014-11-05 14:15:05 +00001766 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001767 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001768 case Primitive::kPrimByte:
1769 case Primitive::kPrimShort:
1770 case Primitive::kPrimInt:
1771 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001772 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001773 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001774 break;
1775
Roland Levillain647b9ed2014-11-27 12:06:00 +00001776 case Primitive::kPrimLong: {
1777 // Processing a Dex `long-to-double' instruction.
1778 Register low = in.AsRegisterPairLow<Register>();
1779 Register high = in.AsRegisterPairHigh<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001780 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1781 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1782 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001783
Roland Levillain647b9ed2014-11-27 12:06:00 +00001784 // low = low - 2^31 (to prevent bit 31 of `low` to be
1785 // interpreted as a sign bit)
1786 __ subl(low, Immediate(0x80000000));
1787 // temp = int-to-double(high)
1788 __ cvtsi2sd(temp, high);
1789 // temp = temp * 2^32
Roland Levillain6d0e4832014-11-27 18:31:21 +00001790 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001791 __ mulsd(temp, constant);
1792 // result = int-to-double(low)
1793 __ cvtsi2sd(result, low);
1794 // result = result + 2^31 (restore the original value of `low`)
Roland Levillain6d0e4832014-11-27 18:31:21 +00001795 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001796 __ addsd(result, constant);
1797 // result = result + temp
1798 __ addsd(result, temp);
1799 break;
1800 }
1801
Roland Levillaincff13742014-11-17 14:32:17 +00001802 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001803 // Processing a Dex `float-to-double' instruction.
1804 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001805 break;
1806
1807 default:
1808 LOG(FATAL) << "Unexpected type conversion from " << input_type
1809 << " to " << result_type;
1810 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001811 break;
1812
1813 default:
1814 LOG(FATAL) << "Unexpected type conversion from " << input_type
1815 << " to " << result_type;
1816 }
1817}
1818
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001819void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001820 LocationSummary* locations =
1821 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001822 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001823 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001824 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001825 locations->SetInAt(0, Location::RequiresRegister());
1826 locations->SetInAt(1, Location::Any());
1827 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001828 break;
1829 }
1830
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001831 case Primitive::kPrimFloat:
1832 case Primitive::kPrimDouble: {
1833 locations->SetInAt(0, Location::RequiresFpuRegister());
1834 locations->SetInAt(1, Location::Any());
1835 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001836 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001837 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001838
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001839 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001840 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1841 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001842 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001843}
1844
1845void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1846 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001847 Location first = locations->InAt(0);
1848 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001849 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001850 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001851 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001852 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001853 __ addl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001854 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001855 __ addl(first.AsRegister<Register>(),
1856 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001857 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001858 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001859 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001860 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001861 }
1862
1863 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001864 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001865 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1866 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001867 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001868 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1869 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001870 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001871 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001872 break;
1873 }
1874
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001875 case Primitive::kPrimFloat: {
1876 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001877 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001878 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001879 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001880 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001881 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001882 }
1883
1884 case Primitive::kPrimDouble: {
1885 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001886 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001887 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001888 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001889 }
1890 break;
1891 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001892
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001893 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001894 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001895 }
1896}
1897
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001898void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001899 LocationSummary* locations =
1900 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001901 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001902 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001903 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001904 locations->SetInAt(0, Location::RequiresRegister());
1905 locations->SetInAt(1, Location::Any());
1906 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001907 break;
1908 }
Calin Juravle11351682014-10-23 15:38:15 +01001909 case Primitive::kPrimFloat:
1910 case Primitive::kPrimDouble: {
1911 locations->SetInAt(0, Location::RequiresFpuRegister());
1912 locations->SetInAt(1, Location::RequiresFpuRegister());
1913 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001914 break;
Calin Juravle11351682014-10-23 15:38:15 +01001915 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001916
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001917 default:
Calin Juravle11351682014-10-23 15:38:15 +01001918 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001919 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001920}
1921
1922void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1923 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001924 Location first = locations->InAt(0);
1925 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001926 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001927 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001928 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001929 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001930 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001931 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001932 __ subl(first.AsRegister<Register>(),
1933 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001934 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001935 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001936 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001937 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001938 }
1939
1940 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001941 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01001942 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1943 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001944 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001945 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001946 __ sbbl(first.AsRegisterPairHigh<Register>(),
1947 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001948 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001949 break;
1950 }
1951
Calin Juravle11351682014-10-23 15:38:15 +01001952 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001953 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001954 break;
Calin Juravle11351682014-10-23 15:38:15 +01001955 }
1956
1957 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001958 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001959 break;
1960 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001961
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001962 default:
Calin Juravle11351682014-10-23 15:38:15 +01001963 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001964 }
1965}
1966
Calin Juravle34bacdf2014-10-07 20:23:36 +01001967void LocationsBuilderX86::VisitMul(HMul* mul) {
1968 LocationSummary* locations =
1969 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1970 switch (mul->GetResultType()) {
1971 case Primitive::kPrimInt:
1972 locations->SetInAt(0, Location::RequiresRegister());
1973 locations->SetInAt(1, Location::Any());
1974 locations->SetOut(Location::SameAsFirstInput());
1975 break;
1976 case Primitive::kPrimLong: {
1977 locations->SetInAt(0, Location::RequiresRegister());
1978 // TODO: Currently this handles only stack operands:
1979 // - we don't have enough registers because we currently use Quick ABI.
1980 // - by the time we have a working register allocator we will probably change the ABI
1981 // and fix the above.
1982 // - we don't have a way yet to request operands on stack but the base line compiler
1983 // will leave the operands on the stack with Any().
1984 locations->SetInAt(1, Location::Any());
1985 locations->SetOut(Location::SameAsFirstInput());
1986 // Needed for imul on 32bits with 64bits output.
1987 locations->AddTemp(Location::RegisterLocation(EAX));
1988 locations->AddTemp(Location::RegisterLocation(EDX));
1989 break;
1990 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001991 case Primitive::kPrimFloat:
1992 case Primitive::kPrimDouble: {
1993 locations->SetInAt(0, Location::RequiresFpuRegister());
1994 locations->SetInAt(1, Location::RequiresFpuRegister());
1995 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001996 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001997 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001998
1999 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002000 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002001 }
2002}
2003
2004void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2005 LocationSummary* locations = mul->GetLocations();
2006 Location first = locations->InAt(0);
2007 Location second = locations->InAt(1);
2008 DCHECK(first.Equals(locations->Out()));
2009
2010 switch (mul->GetResultType()) {
2011 case Primitive::kPrimInt: {
2012 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002013 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002014 } else if (second.IsConstant()) {
2015 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002016 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002017 } else {
2018 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002019 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002020 }
2021 break;
2022 }
2023
2024 case Primitive::kPrimLong: {
2025 DCHECK(second.IsDoubleStackSlot());
2026
2027 Register in1_hi = first.AsRegisterPairHigh<Register>();
2028 Register in1_lo = first.AsRegisterPairLow<Register>();
2029 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2030 Address in2_lo(ESP, second.GetStackIndex());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002031 Register eax = locations->GetTemp(0).AsRegister<Register>();
2032 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002033
2034 DCHECK_EQ(EAX, eax);
2035 DCHECK_EQ(EDX, edx);
2036
2037 // input: in1 - 64 bits, in2 - 64 bits
2038 // output: in1
2039 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2040 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2041 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
2042
2043 __ movl(eax, in2_hi);
2044 // eax <- in1.lo * in2.hi
2045 __ imull(eax, in1_lo);
2046 // in1.hi <- in1.hi * in2.lo
2047 __ imull(in1_hi, in2_lo);
2048 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2049 __ addl(in1_hi, eax);
2050 // move in1_lo to eax to prepare for double precision
2051 __ movl(eax, in1_lo);
2052 // edx:eax <- in1.lo * in2.lo
2053 __ mull(in2_lo);
2054 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2055 __ addl(in1_hi, edx);
2056 // in1.lo <- (in1.lo * in2.lo)[31:0];
2057 __ movl(in1_lo, eax);
2058
2059 break;
2060 }
2061
Calin Juravleb5bfa962014-10-21 18:02:24 +01002062 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002063 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002064 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002065 }
2066
2067 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002068 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002069 break;
2070 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002071
2072 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002073 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002074 }
2075}
2076
Calin Juravlebacfec32014-11-14 15:54:36 +00002077void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2078 DCHECK(instruction->IsDiv() || instruction->IsRem());
2079
2080 LocationSummary* locations = instruction->GetLocations();
2081 Location out = locations->Out();
2082 Location first = locations->InAt(0);
2083 Location second = locations->InAt(1);
2084 bool is_div = instruction->IsDiv();
2085
2086 switch (instruction->GetResultType()) {
2087 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002088 Register second_reg = second.AsRegister<Register>();
2089 DCHECK_EQ(EAX, first.AsRegister<Register>());
2090 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002091
2092 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002093 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
2094 is_div);
Calin Juravlebacfec32014-11-14 15:54:36 +00002095 codegen_->AddSlowPath(slow_path);
2096
2097 // 0x80000000/-1 triggers an arithmetic exception!
2098 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2099 // it's safe to just use negl instead of more complex comparisons.
2100
2101 __ cmpl(second_reg, Immediate(-1));
2102 __ j(kEqual, slow_path->GetEntryLabel());
2103
2104 // edx:eax <- sign-extended of eax
2105 __ cdq();
2106 // eax = quotient, edx = remainder
2107 __ idivl(second_reg);
2108
2109 __ Bind(slow_path->GetExitLabel());
2110 break;
2111 }
2112
2113 case Primitive::kPrimLong: {
2114 InvokeRuntimeCallingConvention calling_convention;
2115 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2116 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2117 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2118 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2119 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2120 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2121
2122 if (is_div) {
2123 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2124 } else {
2125 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2126 }
2127 uint32_t dex_pc = is_div
2128 ? instruction->AsDiv()->GetDexPc()
2129 : instruction->AsRem()->GetDexPc();
2130 codegen_->RecordPcInfo(instruction, dex_pc);
2131
2132 break;
2133 }
2134
2135 default:
2136 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2137 }
2138}
2139
Calin Juravle7c4954d2014-10-28 16:57:40 +00002140void LocationsBuilderX86::VisitDiv(HDiv* div) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002141 LocationSummary::CallKind call_kind = div->GetResultType() == Primitive::kPrimLong
2142 ? LocationSummary::kCall
2143 : LocationSummary::kNoCall;
2144 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2145
Calin Juravle7c4954d2014-10-28 16:57:40 +00002146 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002147 case Primitive::kPrimInt: {
2148 locations->SetInAt(0, Location::RegisterLocation(EAX));
2149 locations->SetInAt(1, Location::RequiresRegister());
2150 locations->SetOut(Location::SameAsFirstInput());
2151 // Intel uses edx:eax as the dividend.
2152 locations->AddTemp(Location::RegisterLocation(EDX));
2153 break;
2154 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002155 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002156 InvokeRuntimeCallingConvention calling_convention;
2157 locations->SetInAt(0, Location::RegisterPairLocation(
2158 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2159 locations->SetInAt(1, Location::RegisterPairLocation(
2160 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2161 // Runtime helper puts the result in EAX, EDX.
2162 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002163 break;
2164 }
2165 case Primitive::kPrimFloat:
2166 case Primitive::kPrimDouble: {
2167 locations->SetInAt(0, Location::RequiresFpuRegister());
2168 locations->SetInAt(1, Location::RequiresFpuRegister());
2169 locations->SetOut(Location::SameAsFirstInput());
2170 break;
2171 }
2172
2173 default:
2174 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2175 }
2176}
2177
2178void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2179 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002180 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002181 Location first = locations->InAt(0);
2182 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002183
2184 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002185 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002186 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002187 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002188 break;
2189 }
2190
2191 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002192 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002193 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002194 break;
2195 }
2196
2197 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002198 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002199 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002200 break;
2201 }
2202
2203 default:
2204 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2205 }
2206}
2207
Calin Juravlebacfec32014-11-14 15:54:36 +00002208void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002209 Primitive::Type type = rem->GetResultType();
2210 LocationSummary::CallKind call_kind = type == Primitive::kPrimInt
2211 ? LocationSummary::kNoCall
2212 : LocationSummary::kCall;
Calin Juravlebacfec32014-11-14 15:54:36 +00002213 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2214
Calin Juravled2ec87d2014-12-08 14:24:46 +00002215 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002216 case Primitive::kPrimInt: {
2217 locations->SetInAt(0, Location::RegisterLocation(EAX));
2218 locations->SetInAt(1, Location::RequiresRegister());
2219 locations->SetOut(Location::RegisterLocation(EDX));
2220 break;
2221 }
2222 case Primitive::kPrimLong: {
2223 InvokeRuntimeCallingConvention calling_convention;
2224 locations->SetInAt(0, Location::RegisterPairLocation(
2225 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2226 locations->SetInAt(1, Location::RegisterPairLocation(
2227 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2228 // Runtime helper puts the result in EAX, EDX.
2229 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2230 break;
2231 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00002232 case Primitive::kPrimFloat: {
2233 InvokeRuntimeCallingConvention calling_convention;
2234 // x86 floating-point parameters are passed through core registers (EAX, ECX).
2235 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2236 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2237 // The runtime helper puts the result in XMM0.
2238 locations->SetOut(Location::FpuRegisterLocation(XMM0));
2239 break;
2240 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002241 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002242 InvokeRuntimeCallingConvention calling_convention;
2243 // x86 floating-point parameters are passed through core registers (EAX_ECX, EDX_EBX).
2244 locations->SetInAt(0, Location::RegisterPairLocation(
2245 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2246 locations->SetInAt(1, Location::RegisterPairLocation(
2247 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2248 // The runtime helper puts the result in XMM0.
2249 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Calin Juravlebacfec32014-11-14 15:54:36 +00002250 break;
2251 }
2252
2253 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002254 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002255 }
2256}
2257
2258void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2259 Primitive::Type type = rem->GetResultType();
2260 switch (type) {
2261 case Primitive::kPrimInt:
2262 case Primitive::kPrimLong: {
2263 GenerateDivRemIntegral(rem);
2264 break;
2265 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00002266 case Primitive::kPrimFloat: {
2267 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pFmodf)));
2268 codegen_->RecordPcInfo(rem, rem->GetDexPc());
2269 break;
2270 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002271 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002272 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pFmod)));
2273 codegen_->RecordPcInfo(rem, rem->GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00002274 break;
2275 }
2276 default:
2277 LOG(FATAL) << "Unexpected rem type " << type;
2278 }
2279}
2280
Calin Juravled0d48522014-11-04 16:40:20 +00002281void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2282 LocationSummary* locations =
2283 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002284 switch (instruction->GetType()) {
2285 case Primitive::kPrimInt: {
2286 locations->SetInAt(0, Location::Any());
2287 break;
2288 }
2289 case Primitive::kPrimLong: {
2290 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2291 if (!instruction->IsConstant()) {
2292 locations->AddTemp(Location::RequiresRegister());
2293 }
2294 break;
2295 }
2296 default:
2297 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2298 }
Calin Juravled0d48522014-11-04 16:40:20 +00002299 if (instruction->HasUses()) {
2300 locations->SetOut(Location::SameAsFirstInput());
2301 }
2302}
2303
2304void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2305 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2306 codegen_->AddSlowPath(slow_path);
2307
2308 LocationSummary* locations = instruction->GetLocations();
2309 Location value = locations->InAt(0);
2310
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002311 switch (instruction->GetType()) {
2312 case Primitive::kPrimInt: {
2313 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002314 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002315 __ j(kEqual, slow_path->GetEntryLabel());
2316 } else if (value.IsStackSlot()) {
2317 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2318 __ j(kEqual, slow_path->GetEntryLabel());
2319 } else {
2320 DCHECK(value.IsConstant()) << value;
2321 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2322 __ jmp(slow_path->GetEntryLabel());
2323 }
2324 }
2325 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002326 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002327 case Primitive::kPrimLong: {
2328 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002329 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002330 __ movl(temp, value.AsRegisterPairLow<Register>());
2331 __ orl(temp, value.AsRegisterPairHigh<Register>());
2332 __ j(kEqual, slow_path->GetEntryLabel());
2333 } else {
2334 DCHECK(value.IsConstant()) << value;
2335 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2336 __ jmp(slow_path->GetEntryLabel());
2337 }
2338 }
2339 break;
2340 }
2341 default:
2342 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002343 }
Calin Juravled0d48522014-11-04 16:40:20 +00002344}
2345
Calin Juravle9aec02f2014-11-18 23:06:35 +00002346void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2347 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2348
2349 LocationSummary* locations =
2350 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2351
2352 switch (op->GetResultType()) {
2353 case Primitive::kPrimInt: {
2354 locations->SetInAt(0, Location::RequiresRegister());
2355 // The shift count needs to be in CL.
2356 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2357 locations->SetOut(Location::SameAsFirstInput());
2358 break;
2359 }
2360 case Primitive::kPrimLong: {
2361 locations->SetInAt(0, Location::RequiresRegister());
2362 // The shift count needs to be in CL.
2363 locations->SetInAt(1, Location::RegisterLocation(ECX));
2364 locations->SetOut(Location::SameAsFirstInput());
2365 break;
2366 }
2367 default:
2368 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2369 }
2370}
2371
2372void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2373 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2374
2375 LocationSummary* locations = op->GetLocations();
2376 Location first = locations->InAt(0);
2377 Location second = locations->InAt(1);
2378 DCHECK(first.Equals(locations->Out()));
2379
2380 switch (op->GetResultType()) {
2381 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002382 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002383 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002384 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002385 DCHECK_EQ(ECX, second_reg);
2386 if (op->IsShl()) {
2387 __ shll(first_reg, second_reg);
2388 } else if (op->IsShr()) {
2389 __ sarl(first_reg, second_reg);
2390 } else {
2391 __ shrl(first_reg, second_reg);
2392 }
2393 } else {
2394 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
2395 if (op->IsShl()) {
2396 __ shll(first_reg, imm);
2397 } else if (op->IsShr()) {
2398 __ sarl(first_reg, imm);
2399 } else {
2400 __ shrl(first_reg, imm);
2401 }
2402 }
2403 break;
2404 }
2405 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002406 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002407 DCHECK_EQ(ECX, second_reg);
2408 if (op->IsShl()) {
2409 GenerateShlLong(first, second_reg);
2410 } else if (op->IsShr()) {
2411 GenerateShrLong(first, second_reg);
2412 } else {
2413 GenerateUShrLong(first, second_reg);
2414 }
2415 break;
2416 }
2417 default:
2418 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2419 }
2420}
2421
2422void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2423 Label done;
2424 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2425 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2426 __ testl(shifter, Immediate(32));
2427 __ j(kEqual, &done);
2428 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2429 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2430 __ Bind(&done);
2431}
2432
2433void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2434 Label done;
2435 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2436 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2437 __ testl(shifter, Immediate(32));
2438 __ j(kEqual, &done);
2439 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2440 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2441 __ Bind(&done);
2442}
2443
2444void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2445 Label done;
2446 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2447 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2448 __ testl(shifter, Immediate(32));
2449 __ j(kEqual, &done);
2450 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2451 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2452 __ Bind(&done);
2453}
2454
2455void LocationsBuilderX86::VisitShl(HShl* shl) {
2456 HandleShift(shl);
2457}
2458
2459void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2460 HandleShift(shl);
2461}
2462
2463void LocationsBuilderX86::VisitShr(HShr* shr) {
2464 HandleShift(shr);
2465}
2466
2467void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2468 HandleShift(shr);
2469}
2470
2471void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2472 HandleShift(ushr);
2473}
2474
2475void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2476 HandleShift(ushr);
2477}
2478
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002479void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002480 LocationSummary* locations =
2481 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002482 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002483 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002484 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2485 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002486}
2487
2488void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2489 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002490 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002491 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002492
Nicolas Geoffray707c8092014-04-04 10:50:14 +01002493 __ fs()->call(
2494 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002495
Nicolas Geoffray39468442014-09-02 15:17:15 +01002496 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002497 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002498}
2499
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002500void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2501 LocationSummary* locations =
2502 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2503 locations->SetOut(Location::RegisterLocation(EAX));
2504 InvokeRuntimeCallingConvention calling_convention;
2505 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2506 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2507 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2508}
2509
2510void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2511 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002512 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002513 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2514
2515 __ fs()->call(
2516 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
2517
2518 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2519 DCHECK(!codegen_->IsLeafMethod());
2520}
2521
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002522void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002523 LocationSummary* locations =
2524 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002525 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2526 if (location.IsStackSlot()) {
2527 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2528 } else if (location.IsDoubleStackSlot()) {
2529 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002530 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002531 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002532}
2533
2534void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002535 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002536}
2537
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002538void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002539 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002540 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002541 locations->SetInAt(0, Location::RequiresRegister());
2542 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002543}
2544
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002545void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2546 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002547 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002548 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002549 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002550 switch (not_->InputAt(0)->GetType()) {
2551 case Primitive::kPrimBoolean:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002552 __ xorl(out.AsRegister<Register>(), Immediate(1));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002553 break;
2554
2555 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002556 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002557 break;
2558
2559 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002560 __ notl(out.AsRegisterPairLow<Register>());
2561 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002562 break;
2563
2564 default:
2565 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2566 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002567}
2568
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002569void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002570 LocationSummary* locations =
2571 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002572 switch (compare->InputAt(0)->GetType()) {
2573 case Primitive::kPrimLong: {
2574 locations->SetInAt(0, Location::RequiresRegister());
2575 // TODO: we set any here but we don't handle constants
2576 locations->SetInAt(1, Location::Any());
2577 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2578 break;
2579 }
2580 case Primitive::kPrimFloat:
2581 case Primitive::kPrimDouble: {
2582 locations->SetInAt(0, Location::RequiresFpuRegister());
2583 locations->SetInAt(1, Location::RequiresFpuRegister());
2584 locations->SetOut(Location::RequiresRegister());
2585 break;
2586 }
2587 default:
2588 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2589 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002590}
2591
2592void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002593 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002594 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002595 Location left = locations->InAt(0);
2596 Location right = locations->InAt(1);
2597
2598 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002599 switch (compare->InputAt(0)->GetType()) {
2600 case Primitive::kPrimLong: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002601 if (right.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002602 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002603 } else {
2604 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002605 __ cmpl(left.AsRegisterPairHigh<Register>(),
2606 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002607 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002608 __ j(kLess, &less); // Signed compare.
2609 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002610 if (right.IsRegisterPair()) {
2611 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002612 } else {
2613 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002614 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002615 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002616 break;
2617 }
2618 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002619 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002620 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
2621 break;
2622 }
2623 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002624 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002625 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002626 break;
2627 }
2628 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002629 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002630 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002631 __ movl(out, Immediate(0));
2632 __ j(kEqual, &done);
2633 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
2634
2635 __ Bind(&greater);
2636 __ movl(out, Immediate(1));
2637 __ jmp(&done);
2638
2639 __ Bind(&less);
2640 __ movl(out, Immediate(-1));
2641
2642 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002643}
2644
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002645void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002646 LocationSummary* locations =
2647 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002648 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2649 locations->SetInAt(i, Location::Any());
2650 }
2651 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002652}
2653
2654void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002655 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002656 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002657}
2658
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002659void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002660 LocationSummary* locations =
2661 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002662 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002663 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002664 bool needs_write_barrier =
2665 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
2666
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002667 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2668 || (field_type == Primitive::kPrimByte);
2669 // The register allocator does not support multiple
2670 // inputs that die at entry with one in a specific register.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002671 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002672 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002673 locations->SetInAt(1, Location::RegisterLocation(EAX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002674 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002675 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002676 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002677 // Temporary registers for the write barrier.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002678 if (needs_write_barrier) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002679 locations->AddTemp(Location::RequiresRegister());
2680 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002681 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002682 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002683}
2684
2685void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2686 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002687 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002688 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01002689 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002690
2691 switch (field_type) {
2692 case Primitive::kPrimBoolean:
2693 case Primitive::kPrimByte: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002694 ByteRegister value = locations->InAt(1).AsRegister<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002695 __ movb(Address(obj, offset), value);
2696 break;
2697 }
2698
2699 case Primitive::kPrimShort:
2700 case Primitive::kPrimChar: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002701 Register value = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002702 __ movw(Address(obj, offset), value);
2703 break;
2704 }
2705
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002706 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002707 case Primitive::kPrimNot: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002708 Register value = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002709 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002710
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002711 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002712 Register temp = locations->GetTemp(0).AsRegister<Register>();
2713 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002714 codegen_->MarkGCCard(temp, card, obj, value);
2715 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002716 break;
2717 }
2718
2719 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002720 Location value = locations->InAt(1);
2721 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
2722 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002723 break;
2724 }
2725
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002726 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002727 XmmRegister value = locations->InAt(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002728 __ movss(Address(obj, offset), value);
2729 break;
2730 }
2731
2732 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002733 XmmRegister value = locations->InAt(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002734 __ movsd(Address(obj, offset), value);
2735 break;
2736 }
2737
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002738 case Primitive::kPrimVoid:
2739 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002740 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002741 }
2742}
2743
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002744void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
2745 Label is_null;
2746 __ testl(value, value);
2747 __ j(kEqual, &is_null);
2748 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2749 __ movl(temp, object);
2750 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
2751 __ movb(Address(temp, card, TIMES_1, 0),
2752 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
2753 __ Bind(&is_null);
2754}
2755
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002756void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002757 LocationSummary* locations =
2758 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002759 locations->SetInAt(0, Location::RequiresRegister());
2760 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002761}
2762
2763void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2764 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002765 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002766 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2767
2768 switch (instruction->GetType()) {
2769 case Primitive::kPrimBoolean: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002770 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002771 __ movzxb(out, Address(obj, offset));
2772 break;
2773 }
2774
2775 case Primitive::kPrimByte: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002776 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002777 __ movsxb(out, Address(obj, offset));
2778 break;
2779 }
2780
2781 case Primitive::kPrimShort: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002782 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002783 __ movsxw(out, Address(obj, offset));
2784 break;
2785 }
2786
2787 case Primitive::kPrimChar: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002788 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002789 __ movzxw(out, Address(obj, offset));
2790 break;
2791 }
2792
2793 case Primitive::kPrimInt:
2794 case Primitive::kPrimNot: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002795 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002796 __ movl(out, Address(obj, offset));
2797 break;
2798 }
2799
2800 case Primitive::kPrimLong: {
2801 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002802 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
2803 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002804 break;
2805 }
2806
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002807 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002808 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002809 __ movss(out, Address(obj, offset));
2810 break;
2811 }
2812
2813 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002814 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002815 __ movsd(out, Address(obj, offset));
2816 break;
2817 }
2818
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002819 case Primitive::kPrimVoid:
2820 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002821 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002822 }
2823}
2824
2825void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002826 LocationSummary* locations =
2827 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002828 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002829 if (instruction->HasUses()) {
2830 locations->SetOut(Location::SameAsFirstInput());
2831 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002832}
2833
2834void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002835 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002836 codegen_->AddSlowPath(slow_path);
2837
2838 LocationSummary* locations = instruction->GetLocations();
2839 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002840
2841 if (obj.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002842 __ cmpl(obj.AsRegister<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002843 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002844 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002845 } else {
2846 DCHECK(obj.IsConstant()) << obj;
2847 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2848 __ jmp(slow_path->GetEntryLabel());
2849 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002850 }
2851 __ j(kEqual, slow_path->GetEntryLabel());
2852}
2853
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002854void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002855 LocationSummary* locations =
2856 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002857 locations->SetInAt(0, Location::RequiresRegister());
2858 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2859 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002860}
2861
2862void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
2863 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002864 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002865 Location index = locations->InAt(1);
2866
2867 switch (instruction->GetType()) {
2868 case Primitive::kPrimBoolean: {
2869 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002870 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002871 if (index.IsConstant()) {
2872 __ movzxb(out, Address(obj,
2873 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2874 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002875 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002876 }
2877 break;
2878 }
2879
2880 case Primitive::kPrimByte: {
2881 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002882 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002883 if (index.IsConstant()) {
2884 __ movsxb(out, Address(obj,
2885 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2886 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002887 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002888 }
2889 break;
2890 }
2891
2892 case Primitive::kPrimShort: {
2893 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002894 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002895 if (index.IsConstant()) {
2896 __ movsxw(out, Address(obj,
2897 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2898 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002899 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002900 }
2901 break;
2902 }
2903
2904 case Primitive::kPrimChar: {
2905 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002906 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002907 if (index.IsConstant()) {
2908 __ movzxw(out, Address(obj,
2909 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2910 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002911 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002912 }
2913 break;
2914 }
2915
2916 case Primitive::kPrimInt:
2917 case Primitive::kPrimNot: {
2918 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002919 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002920 if (index.IsConstant()) {
2921 __ movl(out, Address(obj,
2922 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2923 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002924 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002925 }
2926 break;
2927 }
2928
2929 case Primitive::kPrimLong: {
2930 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002931 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002932 if (index.IsConstant()) {
2933 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002934 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
2935 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002936 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002937 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00002938 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002939 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00002940 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002941 }
2942 break;
2943 }
2944
2945 case Primitive::kPrimFloat:
2946 case Primitive::kPrimDouble:
2947 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002948 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002949 case Primitive::kPrimVoid:
2950 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002951 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002952 }
2953}
2954
2955void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002956 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002957 bool needs_write_barrier =
2958 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2959
2960 DCHECK(kFollowsQuickABI);
2961 bool not_enough_registers = needs_write_barrier
2962 && !instruction->GetValue()->IsConstant()
2963 && !instruction->GetIndex()->IsConstant();
2964 bool needs_runtime_call = instruction->NeedsTypeCheck() || not_enough_registers;
2965
Nicolas Geoffray39468442014-09-02 15:17:15 +01002966 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2967 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002968 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002969
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002970 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002971 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002972 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2973 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2974 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002975 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002976 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
2977 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002978 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002979 // In case of a byte operation, the register allocator does not support multiple
2980 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002981 locations->SetInAt(0, Location::RequiresRegister());
2982 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002983 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002984 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002985 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002986 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002987 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002988 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002989 // Temporary registers for the write barrier.
2990 if (needs_write_barrier) {
2991 locations->AddTemp(Location::RequiresRegister());
2992 // Ensure the card is in a byte register.
2993 locations->AddTemp(Location::RegisterLocation(ECX));
2994 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002995 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002996}
2997
2998void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
2999 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003000 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003001 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003002 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003003 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003004 bool needs_runtime_call = locations->WillCall();
3005 bool needs_write_barrier =
3006 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003007
3008 switch (value_type) {
3009 case Primitive::kPrimBoolean:
3010 case Primitive::kPrimByte: {
3011 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003012 if (index.IsConstant()) {
3013 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003014 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003015 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003016 } else {
3017 __ movb(Address(obj, offset),
3018 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3019 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003020 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003021 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003022 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
3023 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003024 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003025 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003026 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3027 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003028 }
3029 break;
3030 }
3031
3032 case Primitive::kPrimShort:
3033 case Primitive::kPrimChar: {
3034 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003035 if (index.IsConstant()) {
3036 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003037 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003038 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003039 } else {
3040 __ movw(Address(obj, offset),
3041 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3042 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003043 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003044 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003045 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3046 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003047 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003048 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003049 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3050 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003051 }
3052 break;
3053 }
3054
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003055 case Primitive::kPrimInt:
3056 case Primitive::kPrimNot: {
3057 if (!needs_runtime_call) {
3058 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3059 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003060 size_t offset =
3061 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003062 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003063 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003064 } else {
3065 DCHECK(value.IsConstant()) << value;
3066 __ movl(Address(obj, offset),
3067 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3068 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003069 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003070 DCHECK(index.IsRegister()) << index;
3071 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003072 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3073 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003074 } else {
3075 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003076 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003077 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3078 }
3079 }
3080
3081 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003082 Register temp = locations->GetTemp(0).AsRegister<Register>();
3083 Register card = locations->GetTemp(1).AsRegister<Register>();
3084 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003085 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003086 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003087 DCHECK_EQ(value_type, Primitive::kPrimNot);
3088 DCHECK(!codegen_->IsLeafMethod());
3089 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3090 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003091 }
3092 break;
3093 }
3094
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003095 case Primitive::kPrimLong: {
3096 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003097 if (index.IsConstant()) {
3098 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003099 if (value.IsRegisterPair()) {
3100 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
3101 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003102 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003103 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003104 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3105 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
3106 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3107 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003108 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003109 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003110 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003111 value.AsRegisterPairLow<Register>());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003112 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003113 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003114 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003115 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003116 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003117 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003118 Immediate(Low32Bits(val)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003119 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003120 Immediate(High32Bits(val)));
3121 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003122 }
3123 break;
3124 }
3125
3126 case Primitive::kPrimFloat:
3127 case Primitive::kPrimDouble:
3128 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003129 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003130 case Primitive::kPrimVoid:
3131 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003132 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003133 }
3134}
3135
3136void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3137 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003138 locations->SetInAt(0, Location::RequiresRegister());
3139 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003140 instruction->SetLocations(locations);
3141}
3142
3143void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3144 LocationSummary* locations = instruction->GetLocations();
3145 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003146 Register obj = locations->InAt(0).AsRegister<Register>();
3147 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003148 __ movl(out, Address(obj, offset));
3149}
3150
3151void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003152 LocationSummary* locations =
3153 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003154 locations->SetInAt(0, Location::RequiresRegister());
3155 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003156 if (instruction->HasUses()) {
3157 locations->SetOut(Location::SameAsFirstInput());
3158 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003159}
3160
3161void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3162 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003163 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01003164 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003165 codegen_->AddSlowPath(slow_path);
3166
Roland Levillain271ab9c2014-11-27 15:23:57 +00003167 Register index = locations->InAt(0).AsRegister<Register>();
3168 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003169
3170 __ cmpl(index, length);
3171 __ j(kAboveEqual, slow_path->GetEntryLabel());
3172}
3173
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003174void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3175 temp->SetLocations(nullptr);
3176}
3177
3178void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3179 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003180 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003181}
3182
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003183void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003184 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003185 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003186}
3187
3188void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003189 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3190}
3191
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003192void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3193 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3194}
3195
3196void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003197 HBasicBlock* block = instruction->GetBlock();
3198 if (block->GetLoopInformation() != nullptr) {
3199 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3200 // The back edge will generate the suspend check.
3201 return;
3202 }
3203 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3204 // The goto will generate the suspend check.
3205 return;
3206 }
3207 GenerateSuspendCheck(instruction, nullptr);
3208}
3209
3210void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3211 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003212 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003213 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003214 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003215 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003216 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003217 if (successor == nullptr) {
3218 __ j(kNotEqual, slow_path->GetEntryLabel());
3219 __ Bind(slow_path->GetReturnLabel());
3220 } else {
3221 __ j(kEqual, codegen_->GetLabelOf(successor));
3222 __ jmp(slow_path->GetEntryLabel());
3223 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003224}
3225
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003226X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3227 return codegen_->GetAssembler();
3228}
3229
3230void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
3231 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003232 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003233 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3234 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
3235 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
3236}
3237
3238void ParallelMoveResolverX86::EmitMove(size_t index) {
3239 MoveOperands* move = moves_.Get(index);
3240 Location source = move->GetSource();
3241 Location destination = move->GetDestination();
3242
3243 if (source.IsRegister()) {
3244 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003245 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003246 } else {
3247 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003248 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003249 }
3250 } else if (source.IsStackSlot()) {
3251 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003252 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003253 } else {
3254 DCHECK(destination.IsStackSlot());
3255 MoveMemoryToMemory(destination.GetStackIndex(),
3256 source.GetStackIndex());
3257 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003258 } else if (source.IsConstant()) {
3259 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
3260 Immediate imm(instruction->AsIntConstant()->GetValue());
3261 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003262 __ movl(destination.AsRegister<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003263 } else {
3264 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3265 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003266 } else {
3267 LOG(FATAL) << "Unimplemented";
3268 }
3269}
3270
3271void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003272 Register suggested_scratch = reg == EAX ? EBX : EAX;
3273 ScratchRegisterScope ensure_scratch(
3274 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3275
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003276 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3277 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
3278 __ movl(Address(ESP, mem + stack_offset), reg);
3279 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
3280}
3281
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003282void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
3283 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003284 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3285
3286 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003287 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003288 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3289
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003290 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
3291 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
3292 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
3293 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
3294 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
3295 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
3296}
3297
3298void ParallelMoveResolverX86::EmitSwap(size_t index) {
3299 MoveOperands* move = moves_.Get(index);
3300 Location source = move->GetSource();
3301 Location destination = move->GetDestination();
3302
3303 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003304 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003305 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003306 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003307 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003308 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003309 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3310 Exchange(destination.GetStackIndex(), source.GetStackIndex());
3311 } else {
3312 LOG(FATAL) << "Unimplemented";
3313 }
3314}
3315
3316void ParallelMoveResolverX86::SpillScratch(int reg) {
3317 __ pushl(static_cast<Register>(reg));
3318}
3319
3320void ParallelMoveResolverX86::RestoreScratch(int reg) {
3321 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003322}
3323
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003324void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003325 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3326 ? LocationSummary::kCallOnSlowPath
3327 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003328 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003329 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003330 locations->SetOut(Location::RequiresRegister());
3331}
3332
3333void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003334 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003335 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003336 DCHECK(!cls->CanCallRuntime());
3337 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003338 codegen_->LoadCurrentMethod(out);
3339 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3340 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003341 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003342 codegen_->LoadCurrentMethod(out);
3343 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3344 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003345
3346 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3347 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3348 codegen_->AddSlowPath(slow_path);
3349 __ testl(out, out);
3350 __ j(kEqual, slow_path->GetEntryLabel());
3351 if (cls->MustGenerateClinitCheck()) {
3352 GenerateClassInitializationCheck(slow_path, out);
3353 } else {
3354 __ Bind(slow_path->GetExitLabel());
3355 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003356 }
3357}
3358
3359void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
3360 LocationSummary* locations =
3361 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3362 locations->SetInAt(0, Location::RequiresRegister());
3363 if (check->HasUses()) {
3364 locations->SetOut(Location::SameAsFirstInput());
3365 }
3366}
3367
3368void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003369 // We assume the class to not be null.
3370 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3371 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003372 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003373 GenerateClassInitializationCheck(slow_path,
3374 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003375}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003376
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003377void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
3378 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003379 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3380 Immediate(mirror::Class::kStatusInitialized));
3381 __ j(kLess, slow_path->GetEntryLabel());
3382 __ Bind(slow_path->GetExitLabel());
3383 // No need for memory fence, thanks to the X86 memory model.
3384}
3385
3386void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3387 LocationSummary* locations =
3388 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3389 locations->SetInAt(0, Location::RequiresRegister());
3390 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3391}
3392
3393void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3394 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003395 Register cls = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003396 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
3397
3398 switch (instruction->GetType()) {
3399 case Primitive::kPrimBoolean: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003400 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003401 __ movzxb(out, Address(cls, offset));
3402 break;
3403 }
3404
3405 case Primitive::kPrimByte: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003406 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003407 __ movsxb(out, Address(cls, offset));
3408 break;
3409 }
3410
3411 case Primitive::kPrimShort: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003412 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003413 __ movsxw(out, Address(cls, offset));
3414 break;
3415 }
3416
3417 case Primitive::kPrimChar: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003418 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003419 __ movzxw(out, Address(cls, offset));
3420 break;
3421 }
3422
3423 case Primitive::kPrimInt:
3424 case Primitive::kPrimNot: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003425 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003426 __ movl(out, Address(cls, offset));
3427 break;
3428 }
3429
3430 case Primitive::kPrimLong: {
3431 // TODO: support volatile.
3432 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(cls, offset));
3433 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(cls, kX86WordSize + offset));
3434 break;
3435 }
3436
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003437 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003438 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003439 __ movss(out, Address(cls, offset));
3440 break;
3441 }
3442
3443 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003444 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003445 __ movsd(out, Address(cls, offset));
3446 break;
3447 }
3448
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003449 case Primitive::kPrimVoid:
3450 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3451 UNREACHABLE();
3452 }
3453}
3454
3455void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3456 LocationSummary* locations =
3457 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3458 locations->SetInAt(0, Location::RequiresRegister());
3459 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003460 bool needs_write_barrier =
3461 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003462 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3463 || (field_type == Primitive::kPrimByte);
3464 // The register allocator does not support multiple
3465 // inputs that die at entry with one in a specific register.
3466 if (is_byte_type) {
3467 // Ensure the value is in a byte register.
3468 locations->SetInAt(1, Location::RegisterLocation(EAX));
3469 } else {
3470 locations->SetInAt(1, Location::RequiresRegister());
3471 }
3472 // Temporary registers for the write barrier.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003473 if (needs_write_barrier) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003474 locations->AddTemp(Location::RequiresRegister());
3475 // Ensure the card is in a byte register.
3476 locations->AddTemp(Location::RegisterLocation(ECX));
3477 }
3478}
3479
3480void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3481 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003482 Register cls = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003483 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
3484 Primitive::Type field_type = instruction->GetFieldType();
3485
3486 switch (field_type) {
3487 case Primitive::kPrimBoolean:
3488 case Primitive::kPrimByte: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003489 ByteRegister value = locations->InAt(1).AsRegister<ByteRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003490 __ movb(Address(cls, offset), value);
3491 break;
3492 }
3493
3494 case Primitive::kPrimShort:
3495 case Primitive::kPrimChar: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003496 Register value = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003497 __ movw(Address(cls, offset), value);
3498 break;
3499 }
3500
3501 case Primitive::kPrimInt:
3502 case Primitive::kPrimNot: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003503 Register value = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003504 __ movl(Address(cls, offset), value);
3505
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003506 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003507 Register temp = locations->GetTemp(0).AsRegister<Register>();
3508 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003509 codegen_->MarkGCCard(temp, card, cls, value);
3510 }
3511 break;
3512 }
3513
3514 case Primitive::kPrimLong: {
3515 Location value = locations->InAt(1);
3516 __ movl(Address(cls, offset), value.AsRegisterPairLow<Register>());
3517 __ movl(Address(cls, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3518 break;
3519 }
3520
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003521 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003522 XmmRegister value = locations->InAt(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003523 __ movss(Address(cls, offset), value);
3524 break;
3525 }
3526
3527 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003528 XmmRegister value = locations->InAt(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003529 __ movsd(Address(cls, offset), value);
3530 break;
3531 }
3532
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003533 case Primitive::kPrimVoid:
3534 LOG(FATAL) << "Unreachable type " << field_type;
3535 UNREACHABLE();
3536 }
3537}
3538
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003539void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
3540 LocationSummary* locations =
3541 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3542 locations->SetOut(Location::RequiresRegister());
3543}
3544
3545void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
3546 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
3547 codegen_->AddSlowPath(slow_path);
3548
Roland Levillain271ab9c2014-11-27 15:23:57 +00003549 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003550 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08003551 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3552 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003553 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3554 __ testl(out, out);
3555 __ j(kEqual, slow_path->GetEntryLabel());
3556 __ Bind(slow_path->GetExitLabel());
3557}
3558
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003559void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
3560 LocationSummary* locations =
3561 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3562 locations->SetOut(Location::RequiresRegister());
3563}
3564
3565void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
3566 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003567 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003568 __ fs()->movl(address, Immediate(0));
3569}
3570
3571void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
3572 LocationSummary* locations =
3573 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3574 InvokeRuntimeCallingConvention calling_convention;
3575 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3576}
3577
3578void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
3579 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
3580 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3581}
3582
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003583void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003584 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3585 ? LocationSummary::kNoCall
3586 : LocationSummary::kCallOnSlowPath;
3587 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3588 locations->SetInAt(0, Location::RequiresRegister());
3589 locations->SetInAt(1, Location::Any());
3590 locations->SetOut(Location::RequiresRegister());
3591}
3592
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003593void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003594 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003595 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003596 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003597 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003598 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3599 Label done, zero;
3600 SlowPathCodeX86* slow_path = nullptr;
3601
3602 // Return 0 if `obj` is null.
3603 // TODO: avoid this check if we know obj is not null.
3604 __ testl(obj, obj);
3605 __ j(kEqual, &zero);
3606 __ movl(out, Address(obj, class_offset));
3607 // Compare the class of `obj` with `cls`.
3608 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003609 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003610 } else {
3611 DCHECK(cls.IsStackSlot()) << cls;
3612 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
3613 }
3614
3615 if (instruction->IsClassFinal()) {
3616 // Classes must be equal for the instanceof to succeed.
3617 __ j(kNotEqual, &zero);
3618 __ movl(out, Immediate(1));
3619 __ jmp(&done);
3620 } else {
3621 // If the classes are not equal, we go into a slow path.
3622 DCHECK(locations->OnlyCallsOnSlowPath());
3623 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003624 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003625 codegen_->AddSlowPath(slow_path);
3626 __ j(kNotEqual, slow_path->GetEntryLabel());
3627 __ movl(out, Immediate(1));
3628 __ jmp(&done);
3629 }
3630 __ Bind(&zero);
3631 __ movl(out, Immediate(0));
3632 if (slow_path != nullptr) {
3633 __ Bind(slow_path->GetExitLabel());
3634 }
3635 __ Bind(&done);
3636}
3637
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003638void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
3639 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3640 instruction, LocationSummary::kCallOnSlowPath);
3641 locations->SetInAt(0, Location::RequiresRegister());
3642 locations->SetInAt(1, Location::Any());
3643 locations->AddTemp(Location::RequiresRegister());
3644}
3645
3646void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
3647 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003648 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003649 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003650 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003651 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3652 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
3653 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3654 codegen_->AddSlowPath(slow_path);
3655
3656 // TODO: avoid this check if we know obj is not null.
3657 __ testl(obj, obj);
3658 __ j(kEqual, slow_path->GetExitLabel());
3659 __ movl(temp, Address(obj, class_offset));
3660
3661 // Compare the class of `obj` with `cls`.
3662 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003663 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003664 } else {
3665 DCHECK(cls.IsStackSlot()) << cls;
3666 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
3667 }
3668
3669 __ j(kNotEqual, slow_path->GetEntryLabel());
3670 __ Bind(slow_path->GetExitLabel());
3671}
3672
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003673void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3674 LocationSummary* locations =
3675 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3676 InvokeRuntimeCallingConvention calling_convention;
3677 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3678}
3679
3680void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3681 __ fs()->call(Address::Absolute(instruction->IsEnter()
3682 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
3683 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
3684 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3685}
3686
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003687void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3688void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3689void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3690
3691void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3692 LocationSummary* locations =
3693 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3694 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3695 || instruction->GetResultType() == Primitive::kPrimLong);
3696 locations->SetInAt(0, Location::RequiresRegister());
3697 locations->SetInAt(1, Location::Any());
3698 locations->SetOut(Location::SameAsFirstInput());
3699}
3700
3701void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
3702 HandleBitwiseOperation(instruction);
3703}
3704
3705void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
3706 HandleBitwiseOperation(instruction);
3707}
3708
3709void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
3710 HandleBitwiseOperation(instruction);
3711}
3712
3713void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3714 LocationSummary* locations = instruction->GetLocations();
3715 Location first = locations->InAt(0);
3716 Location second = locations->InAt(1);
3717 DCHECK(first.Equals(locations->Out()));
3718
3719 if (instruction->GetResultType() == Primitive::kPrimInt) {
3720 if (second.IsRegister()) {
3721 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003722 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003723 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003724 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003725 } else {
3726 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003727 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003728 }
3729 } else if (second.IsConstant()) {
3730 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003731 __ andl(first.AsRegister<Register>(),
3732 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003733 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003734 __ orl(first.AsRegister<Register>(),
3735 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003736 } else {
3737 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00003738 __ xorl(first.AsRegister<Register>(),
3739 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003740 }
3741 } else {
3742 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003743 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003744 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003745 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003746 } else {
3747 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003748 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003749 }
3750 }
3751 } else {
3752 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3753 if (second.IsRegisterPair()) {
3754 if (instruction->IsAnd()) {
3755 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3756 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3757 } else if (instruction->IsOr()) {
3758 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3759 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3760 } else {
3761 DCHECK(instruction->IsXor());
3762 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3763 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3764 }
3765 } else {
3766 if (instruction->IsAnd()) {
3767 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3768 __ andl(first.AsRegisterPairHigh<Register>(),
3769 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3770 } else if (instruction->IsOr()) {
3771 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3772 __ orl(first.AsRegisterPairHigh<Register>(),
3773 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3774 } else {
3775 DCHECK(instruction->IsXor());
3776 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3777 __ xorl(first.AsRegisterPairHigh<Register>(),
3778 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3779 }
3780 }
3781 }
3782}
3783
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003784} // namespace x86
3785} // namespace art