blob: fd794f95d1105780d3653ca2d7f9599cbf87c7e8 [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) {
1329 LocationSummary* locations =
1330 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1331 Primitive::Type result_type = conversion->GetResultType();
1332 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001333 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001334 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001335 case Primitive::kPrimByte:
1336 switch (input_type) {
1337 case Primitive::kPrimShort:
1338 case Primitive::kPrimInt:
1339 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001340 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001341 locations->SetInAt(0, Location::Any());
1342 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1343 break;
1344
1345 default:
1346 LOG(FATAL) << "Unexpected type conversion from " << input_type
1347 << " to " << result_type;
1348 }
1349 break;
1350
Roland Levillain01a8d712014-11-14 16:27:39 +00001351 case Primitive::kPrimShort:
1352 switch (input_type) {
1353 case Primitive::kPrimByte:
1354 case Primitive::kPrimInt:
1355 case Primitive::kPrimChar:
1356 // Processing a Dex `int-to-short' instruction.
1357 locations->SetInAt(0, Location::Any());
1358 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1359 break;
1360
1361 default:
1362 LOG(FATAL) << "Unexpected type conversion from " << input_type
1363 << " to " << result_type;
1364 }
1365 break;
1366
Roland Levillain946e1432014-11-11 17:35:19 +00001367 case Primitive::kPrimInt:
1368 switch (input_type) {
1369 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001370 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001371 locations->SetInAt(0, Location::Any());
1372 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1373 break;
1374
1375 case Primitive::kPrimFloat:
1376 case Primitive::kPrimDouble:
1377 LOG(FATAL) << "Type conversion from " << input_type
1378 << " to " << result_type << " not yet implemented";
1379 break;
1380
1381 default:
1382 LOG(FATAL) << "Unexpected type conversion from " << input_type
1383 << " to " << result_type;
1384 }
1385 break;
1386
Roland Levillaindff1f282014-11-05 14:15:05 +00001387 case Primitive::kPrimLong:
1388 switch (input_type) {
1389 case Primitive::kPrimByte:
1390 case Primitive::kPrimShort:
1391 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001392 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001393 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001394 locations->SetInAt(0, Location::RegisterLocation(EAX));
1395 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1396 break;
1397
1398 case Primitive::kPrimFloat:
1399 case Primitive::kPrimDouble:
1400 LOG(FATAL) << "Type conversion from " << input_type << " to "
1401 << result_type << " not yet implemented";
1402 break;
1403
1404 default:
1405 LOG(FATAL) << "Unexpected type conversion from " << input_type
1406 << " to " << result_type;
1407 }
1408 break;
1409
Roland Levillain981e4542014-11-14 11:47:14 +00001410 case Primitive::kPrimChar:
1411 switch (input_type) {
1412 case Primitive::kPrimByte:
1413 case Primitive::kPrimShort:
1414 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001415 // Processing a Dex `int-to-char' instruction.
1416 locations->SetInAt(0, Location::Any());
1417 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1418 break;
1419
1420 default:
1421 LOG(FATAL) << "Unexpected type conversion from " << input_type
1422 << " to " << result_type;
1423 }
1424 break;
1425
Roland Levillaindff1f282014-11-05 14:15:05 +00001426 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001427 switch (input_type) {
1428 case Primitive::kPrimByte:
1429 case Primitive::kPrimShort:
1430 case Primitive::kPrimInt:
1431 case Primitive::kPrimChar:
1432 // Processing a Dex `int-to-float' instruction.
1433 locations->SetInAt(0, Location::RequiresRegister());
1434 locations->SetOut(Location::RequiresFpuRegister());
1435 break;
1436
1437 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001438 // Processing a Dex `long-to-float' instruction.
1439 locations->SetInAt(0, Location::RequiresRegister());
1440 locations->SetOut(Location::RequiresFpuRegister());
1441 locations->AddTemp(Location::RequiresFpuRegister());
1442 locations->AddTemp(Location::RequiresFpuRegister());
1443 break;
1444
Roland Levillaincff13742014-11-17 14:32:17 +00001445 case Primitive::kPrimDouble:
1446 LOG(FATAL) << "Type conversion from " << input_type
1447 << " to " << result_type << " not yet implemented";
1448 break;
1449
1450 default:
1451 LOG(FATAL) << "Unexpected type conversion from " << input_type
1452 << " to " << result_type;
1453 };
1454 break;
1455
Roland Levillaindff1f282014-11-05 14:15:05 +00001456 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001457 switch (input_type) {
1458 case Primitive::kPrimByte:
1459 case Primitive::kPrimShort:
1460 case Primitive::kPrimInt:
1461 case Primitive::kPrimChar:
1462 // Processing a Dex `int-to-double' instruction.
1463 locations->SetInAt(0, Location::RequiresRegister());
1464 locations->SetOut(Location::RequiresFpuRegister());
1465 break;
1466
1467 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001468 // Processing a Dex `long-to-double' instruction.
1469 locations->SetInAt(0, Location::RequiresRegister());
1470 locations->SetOut(Location::RequiresFpuRegister());
1471 locations->AddTemp(Location::RequiresFpuRegister());
1472 locations->AddTemp(Location::RequiresFpuRegister());
1473 break;
1474
Roland Levillaincff13742014-11-17 14:32:17 +00001475 case Primitive::kPrimFloat:
1476 LOG(FATAL) << "Type conversion from " << input_type
1477 << " to " << result_type << " not yet implemented";
1478 break;
1479
1480 default:
1481 LOG(FATAL) << "Unexpected type conversion from " << input_type
1482 << " to " << result_type;
1483 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001484 break;
1485
1486 default:
1487 LOG(FATAL) << "Unexpected type conversion from " << input_type
1488 << " to " << result_type;
1489 }
1490}
1491
1492void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1493 LocationSummary* locations = conversion->GetLocations();
1494 Location out = locations->Out();
1495 Location in = locations->InAt(0);
1496 Primitive::Type result_type = conversion->GetResultType();
1497 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001498 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001499 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001500 case Primitive::kPrimByte:
1501 switch (input_type) {
1502 case Primitive::kPrimShort:
1503 case Primitive::kPrimInt:
1504 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001505 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001506 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001507 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001508 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001509 __ movsxb(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain51d3fc42014-11-13 14:11:42 +00001510 } else {
1511 DCHECK(in.GetConstant()->IsIntConstant());
1512 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001513 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
Roland Levillain51d3fc42014-11-13 14:11:42 +00001514 }
1515 break;
1516
1517 default:
1518 LOG(FATAL) << "Unexpected type conversion from " << input_type
1519 << " to " << result_type;
1520 }
1521 break;
1522
Roland Levillain01a8d712014-11-14 16:27:39 +00001523 case Primitive::kPrimShort:
1524 switch (input_type) {
1525 case Primitive::kPrimByte:
1526 case Primitive::kPrimInt:
1527 case Primitive::kPrimChar:
1528 // Processing a Dex `int-to-short' instruction.
1529 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001530 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001531 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001532 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001533 } else {
1534 DCHECK(in.GetConstant()->IsIntConstant());
1535 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001536 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001537 }
1538 break;
1539
1540 default:
1541 LOG(FATAL) << "Unexpected type conversion from " << input_type
1542 << " to " << result_type;
1543 }
1544 break;
1545
Roland Levillain946e1432014-11-11 17:35:19 +00001546 case Primitive::kPrimInt:
1547 switch (input_type) {
1548 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001549 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001550 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001551 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001552 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001553 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001554 } else {
1555 DCHECK(in.IsConstant());
1556 DCHECK(in.GetConstant()->IsLongConstant());
1557 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001558 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001559 }
1560 break;
1561
1562 case Primitive::kPrimFloat:
1563 case Primitive::kPrimDouble:
1564 LOG(FATAL) << "Type conversion from " << input_type
1565 << " to " << result_type << " not yet implemented";
1566 break;
1567
1568 default:
1569 LOG(FATAL) << "Unexpected type conversion from " << input_type
1570 << " to " << result_type;
1571 }
1572 break;
1573
Roland Levillaindff1f282014-11-05 14:15:05 +00001574 case Primitive::kPrimLong:
1575 switch (input_type) {
1576 case Primitive::kPrimByte:
1577 case Primitive::kPrimShort:
1578 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001579 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001580 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001581 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1582 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001583 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001584 __ cdq();
1585 break;
1586
1587 case Primitive::kPrimFloat:
1588 case Primitive::kPrimDouble:
1589 LOG(FATAL) << "Type conversion from " << input_type << " to "
1590 << result_type << " not yet implemented";
1591 break;
1592
1593 default:
1594 LOG(FATAL) << "Unexpected type conversion from " << input_type
1595 << " to " << result_type;
1596 }
1597 break;
1598
Roland Levillain981e4542014-11-14 11:47:14 +00001599 case Primitive::kPrimChar:
1600 switch (input_type) {
1601 case Primitive::kPrimByte:
1602 case Primitive::kPrimShort:
1603 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001604 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1605 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001606 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001607 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001608 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001609 } else {
1610 DCHECK(in.GetConstant()->IsIntConstant());
1611 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001612 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001613 }
1614 break;
1615
1616 default:
1617 LOG(FATAL) << "Unexpected type conversion from " << input_type
1618 << " to " << result_type;
1619 }
1620 break;
1621
Roland Levillaindff1f282014-11-05 14:15:05 +00001622 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001623 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001624 case Primitive::kPrimByte:
1625 case Primitive::kPrimShort:
1626 case Primitive::kPrimInt:
1627 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001628 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001629 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001630 break;
1631
Roland Levillain6d0e4832014-11-27 18:31:21 +00001632 case Primitive::kPrimLong: {
1633 // Processing a Dex `long-to-float' instruction.
1634 Register low = in.AsRegisterPairLow<Register>();
1635 Register high = in.AsRegisterPairHigh<Register>();
1636 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1637 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1638 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1639
1640 // Operations use doubles for precision reasons (each 32-bit
1641 // half of a long fits in the 53-bit mantissa of a double,
1642 // but not in the 24-bit mantissa of a float). This is
1643 // especially important for the low bits. The result is
1644 // eventually converted to float.
1645
1646 // low = low - 2^31 (to prevent bit 31 of `low` to be
1647 // interpreted as a sign bit)
1648 __ subl(low, Immediate(0x80000000));
1649 // temp = int-to-double(high)
1650 __ cvtsi2sd(temp, high);
1651 // temp = temp * 2^32
1652 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
1653 __ mulsd(temp, constant);
1654 // result = int-to-double(low)
1655 __ cvtsi2sd(result, low);
1656 // result = result + 2^31 (restore the original value of `low`)
1657 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
1658 __ addsd(result, constant);
1659 // result = result + temp
1660 __ addsd(result, temp);
1661 // result = double-to-float(result)
1662 __ cvtsd2ss(result, result);
1663 break;
1664 }
1665
Roland Levillaincff13742014-11-17 14:32:17 +00001666 case Primitive::kPrimDouble:
1667 LOG(FATAL) << "Type conversion from " << input_type
1668 << " to " << result_type << " not yet implemented";
1669 break;
1670
1671 default:
1672 LOG(FATAL) << "Unexpected type conversion from " << input_type
1673 << " to " << result_type;
1674 };
1675 break;
1676
Roland Levillaindff1f282014-11-05 14:15:05 +00001677 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001678 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001679 case Primitive::kPrimByte:
1680 case Primitive::kPrimShort:
1681 case Primitive::kPrimInt:
1682 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001683 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001684 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001685 break;
1686
Roland Levillain647b9ed2014-11-27 12:06:00 +00001687 case Primitive::kPrimLong: {
1688 // Processing a Dex `long-to-double' instruction.
1689 Register low = in.AsRegisterPairLow<Register>();
1690 Register high = in.AsRegisterPairHigh<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001691 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1692 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1693 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001694
Roland Levillain647b9ed2014-11-27 12:06:00 +00001695 // low = low - 2^31 (to prevent bit 31 of `low` to be
1696 // interpreted as a sign bit)
1697 __ subl(low, Immediate(0x80000000));
1698 // temp = int-to-double(high)
1699 __ cvtsi2sd(temp, high);
1700 // temp = temp * 2^32
Roland Levillain6d0e4832014-11-27 18:31:21 +00001701 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001702 __ mulsd(temp, constant);
1703 // result = int-to-double(low)
1704 __ cvtsi2sd(result, low);
1705 // result = result + 2^31 (restore the original value of `low`)
Roland Levillain6d0e4832014-11-27 18:31:21 +00001706 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001707 __ addsd(result, constant);
1708 // result = result + temp
1709 __ addsd(result, temp);
1710 break;
1711 }
1712
Roland Levillaincff13742014-11-17 14:32:17 +00001713 case Primitive::kPrimFloat:
1714 LOG(FATAL) << "Type conversion from " << input_type
1715 << " to " << result_type << " not yet implemented";
1716 break;
1717
1718 default:
1719 LOG(FATAL) << "Unexpected type conversion from " << input_type
1720 << " to " << result_type;
1721 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001722 break;
1723
1724 default:
1725 LOG(FATAL) << "Unexpected type conversion from " << input_type
1726 << " to " << result_type;
1727 }
1728}
1729
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001730void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001731 LocationSummary* locations =
1732 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001733 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001734 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001735 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001736 locations->SetInAt(0, Location::RequiresRegister());
1737 locations->SetInAt(1, Location::Any());
1738 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001739 break;
1740 }
1741
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001742 case Primitive::kPrimFloat:
1743 case Primitive::kPrimDouble: {
1744 locations->SetInAt(0, Location::RequiresFpuRegister());
1745 locations->SetInAt(1, Location::Any());
1746 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001747 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001748 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001749
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001750 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001751 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1752 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001753 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001754}
1755
1756void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1757 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001758 Location first = locations->InAt(0);
1759 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001760 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001761 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001762 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001763 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001764 __ addl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001765 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001766 __ addl(first.AsRegister<Register>(),
1767 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001768 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001769 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001770 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001771 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001772 }
1773
1774 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001775 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001776 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1777 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001778 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001779 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1780 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001781 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001782 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001783 break;
1784 }
1785
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001786 case Primitive::kPrimFloat: {
1787 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001788 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001789 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001790 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001791 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001792 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001793 }
1794
1795 case Primitive::kPrimDouble: {
1796 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001797 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001798 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001799 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001800 }
1801 break;
1802 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001803
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001804 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001805 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001806 }
1807}
1808
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001809void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001810 LocationSummary* locations =
1811 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001812 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001813 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001814 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001815 locations->SetInAt(0, Location::RequiresRegister());
1816 locations->SetInAt(1, Location::Any());
1817 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001818 break;
1819 }
Calin Juravle11351682014-10-23 15:38:15 +01001820 case Primitive::kPrimFloat:
1821 case Primitive::kPrimDouble: {
1822 locations->SetInAt(0, Location::RequiresFpuRegister());
1823 locations->SetInAt(1, Location::RequiresFpuRegister());
1824 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001825 break;
Calin Juravle11351682014-10-23 15:38:15 +01001826 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001827
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001828 default:
Calin Juravle11351682014-10-23 15:38:15 +01001829 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001830 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001831}
1832
1833void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1834 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001835 Location first = locations->InAt(0);
1836 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001837 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001838 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001839 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001840 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001841 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001842 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001843 __ subl(first.AsRegister<Register>(),
1844 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001845 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001846 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001847 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001848 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001849 }
1850
1851 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001852 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01001853 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1854 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001855 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001856 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001857 __ sbbl(first.AsRegisterPairHigh<Register>(),
1858 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001859 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001860 break;
1861 }
1862
Calin Juravle11351682014-10-23 15:38:15 +01001863 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001864 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001865 break;
Calin Juravle11351682014-10-23 15:38:15 +01001866 }
1867
1868 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001869 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001870 break;
1871 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001872
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001873 default:
Calin Juravle11351682014-10-23 15:38:15 +01001874 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001875 }
1876}
1877
Calin Juravle34bacdf2014-10-07 20:23:36 +01001878void LocationsBuilderX86::VisitMul(HMul* mul) {
1879 LocationSummary* locations =
1880 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1881 switch (mul->GetResultType()) {
1882 case Primitive::kPrimInt:
1883 locations->SetInAt(0, Location::RequiresRegister());
1884 locations->SetInAt(1, Location::Any());
1885 locations->SetOut(Location::SameAsFirstInput());
1886 break;
1887 case Primitive::kPrimLong: {
1888 locations->SetInAt(0, Location::RequiresRegister());
1889 // TODO: Currently this handles only stack operands:
1890 // - we don't have enough registers because we currently use Quick ABI.
1891 // - by the time we have a working register allocator we will probably change the ABI
1892 // and fix the above.
1893 // - we don't have a way yet to request operands on stack but the base line compiler
1894 // will leave the operands on the stack with Any().
1895 locations->SetInAt(1, Location::Any());
1896 locations->SetOut(Location::SameAsFirstInput());
1897 // Needed for imul on 32bits with 64bits output.
1898 locations->AddTemp(Location::RegisterLocation(EAX));
1899 locations->AddTemp(Location::RegisterLocation(EDX));
1900 break;
1901 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001902 case Primitive::kPrimFloat:
1903 case Primitive::kPrimDouble: {
1904 locations->SetInAt(0, Location::RequiresFpuRegister());
1905 locations->SetInAt(1, Location::RequiresFpuRegister());
1906 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001907 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001908 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001909
1910 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001911 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001912 }
1913}
1914
1915void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1916 LocationSummary* locations = mul->GetLocations();
1917 Location first = locations->InAt(0);
1918 Location second = locations->InAt(1);
1919 DCHECK(first.Equals(locations->Out()));
1920
1921 switch (mul->GetResultType()) {
1922 case Primitive::kPrimInt: {
1923 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001924 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001925 } else if (second.IsConstant()) {
1926 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001927 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001928 } else {
1929 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001930 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01001931 }
1932 break;
1933 }
1934
1935 case Primitive::kPrimLong: {
1936 DCHECK(second.IsDoubleStackSlot());
1937
1938 Register in1_hi = first.AsRegisterPairHigh<Register>();
1939 Register in1_lo = first.AsRegisterPairLow<Register>();
1940 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
1941 Address in2_lo(ESP, second.GetStackIndex());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001942 Register eax = locations->GetTemp(0).AsRegister<Register>();
1943 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001944
1945 DCHECK_EQ(EAX, eax);
1946 DCHECK_EQ(EDX, edx);
1947
1948 // input: in1 - 64 bits, in2 - 64 bits
1949 // output: in1
1950 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
1951 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
1952 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
1953
1954 __ movl(eax, in2_hi);
1955 // eax <- in1.lo * in2.hi
1956 __ imull(eax, in1_lo);
1957 // in1.hi <- in1.hi * in2.lo
1958 __ imull(in1_hi, in2_lo);
1959 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
1960 __ addl(in1_hi, eax);
1961 // move in1_lo to eax to prepare for double precision
1962 __ movl(eax, in1_lo);
1963 // edx:eax <- in1.lo * in2.lo
1964 __ mull(in2_lo);
1965 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
1966 __ addl(in1_hi, edx);
1967 // in1.lo <- (in1.lo * in2.lo)[31:0];
1968 __ movl(in1_lo, eax);
1969
1970 break;
1971 }
1972
Calin Juravleb5bfa962014-10-21 18:02:24 +01001973 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001974 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001975 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001976 }
1977
1978 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001979 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01001980 break;
1981 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001982
1983 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001984 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001985 }
1986}
1987
Calin Juravlebacfec32014-11-14 15:54:36 +00001988void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
1989 DCHECK(instruction->IsDiv() || instruction->IsRem());
1990
1991 LocationSummary* locations = instruction->GetLocations();
1992 Location out = locations->Out();
1993 Location first = locations->InAt(0);
1994 Location second = locations->InAt(1);
1995 bool is_div = instruction->IsDiv();
1996
1997 switch (instruction->GetResultType()) {
1998 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001999 Register second_reg = second.AsRegister<Register>();
2000 DCHECK_EQ(EAX, first.AsRegister<Register>());
2001 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002002
2003 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002004 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
2005 is_div);
Calin Juravlebacfec32014-11-14 15:54:36 +00002006 codegen_->AddSlowPath(slow_path);
2007
2008 // 0x80000000/-1 triggers an arithmetic exception!
2009 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2010 // it's safe to just use negl instead of more complex comparisons.
2011
2012 __ cmpl(second_reg, Immediate(-1));
2013 __ j(kEqual, slow_path->GetEntryLabel());
2014
2015 // edx:eax <- sign-extended of eax
2016 __ cdq();
2017 // eax = quotient, edx = remainder
2018 __ idivl(second_reg);
2019
2020 __ Bind(slow_path->GetExitLabel());
2021 break;
2022 }
2023
2024 case Primitive::kPrimLong: {
2025 InvokeRuntimeCallingConvention calling_convention;
2026 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2027 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2028 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2029 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2030 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2031 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2032
2033 if (is_div) {
2034 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2035 } else {
2036 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2037 }
2038 uint32_t dex_pc = is_div
2039 ? instruction->AsDiv()->GetDexPc()
2040 : instruction->AsRem()->GetDexPc();
2041 codegen_->RecordPcInfo(instruction, dex_pc);
2042
2043 break;
2044 }
2045
2046 default:
2047 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2048 }
2049}
2050
Calin Juravle7c4954d2014-10-28 16:57:40 +00002051void LocationsBuilderX86::VisitDiv(HDiv* div) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002052 LocationSummary::CallKind call_kind = div->GetResultType() == Primitive::kPrimLong
2053 ? LocationSummary::kCall
2054 : LocationSummary::kNoCall;
2055 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2056
Calin Juravle7c4954d2014-10-28 16:57:40 +00002057 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002058 case Primitive::kPrimInt: {
2059 locations->SetInAt(0, Location::RegisterLocation(EAX));
2060 locations->SetInAt(1, Location::RequiresRegister());
2061 locations->SetOut(Location::SameAsFirstInput());
2062 // Intel uses edx:eax as the dividend.
2063 locations->AddTemp(Location::RegisterLocation(EDX));
2064 break;
2065 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002066 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002067 InvokeRuntimeCallingConvention calling_convention;
2068 locations->SetInAt(0, Location::RegisterPairLocation(
2069 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2070 locations->SetInAt(1, Location::RegisterPairLocation(
2071 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2072 // Runtime helper puts the result in EAX, EDX.
2073 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002074 break;
2075 }
2076 case Primitive::kPrimFloat:
2077 case Primitive::kPrimDouble: {
2078 locations->SetInAt(0, Location::RequiresFpuRegister());
2079 locations->SetInAt(1, Location::RequiresFpuRegister());
2080 locations->SetOut(Location::SameAsFirstInput());
2081 break;
2082 }
2083
2084 default:
2085 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2086 }
2087}
2088
2089void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2090 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002091 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002092 Location first = locations->InAt(0);
2093 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002094
2095 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002096 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002097 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002098 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002099 break;
2100 }
2101
2102 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002103 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002104 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002105 break;
2106 }
2107
2108 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002109 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002110 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002111 break;
2112 }
2113
2114 default:
2115 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2116 }
2117}
2118
Calin Juravlebacfec32014-11-14 15:54:36 +00002119void LocationsBuilderX86::VisitRem(HRem* rem) {
2120 LocationSummary::CallKind call_kind = rem->GetResultType() == Primitive::kPrimLong
2121 ? LocationSummary::kCall
2122 : LocationSummary::kNoCall;
2123 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2124
2125 switch (rem->GetResultType()) {
2126 case Primitive::kPrimInt: {
2127 locations->SetInAt(0, Location::RegisterLocation(EAX));
2128 locations->SetInAt(1, Location::RequiresRegister());
2129 locations->SetOut(Location::RegisterLocation(EDX));
2130 break;
2131 }
2132 case Primitive::kPrimLong: {
2133 InvokeRuntimeCallingConvention calling_convention;
2134 locations->SetInAt(0, Location::RegisterPairLocation(
2135 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2136 locations->SetInAt(1, Location::RegisterPairLocation(
2137 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2138 // Runtime helper puts the result in EAX, EDX.
2139 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2140 break;
2141 }
2142 case Primitive::kPrimFloat:
2143 case Primitive::kPrimDouble: {
2144 LOG(FATAL) << "Unimplemented rem type " << rem->GetResultType();
2145 break;
2146 }
2147
2148 default:
2149 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2150 }
2151}
2152
2153void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2154 Primitive::Type type = rem->GetResultType();
2155 switch (type) {
2156 case Primitive::kPrimInt:
2157 case Primitive::kPrimLong: {
2158 GenerateDivRemIntegral(rem);
2159 break;
2160 }
2161 case Primitive::kPrimFloat:
2162 case Primitive::kPrimDouble: {
2163 LOG(FATAL) << "Unimplemented rem type " << type;
2164 break;
2165 }
2166 default:
2167 LOG(FATAL) << "Unexpected rem type " << type;
2168 }
2169}
2170
Calin Juravled0d48522014-11-04 16:40:20 +00002171void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2172 LocationSummary* locations =
2173 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002174 switch (instruction->GetType()) {
2175 case Primitive::kPrimInt: {
2176 locations->SetInAt(0, Location::Any());
2177 break;
2178 }
2179 case Primitive::kPrimLong: {
2180 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2181 if (!instruction->IsConstant()) {
2182 locations->AddTemp(Location::RequiresRegister());
2183 }
2184 break;
2185 }
2186 default:
2187 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2188 }
Calin Juravled0d48522014-11-04 16:40:20 +00002189 if (instruction->HasUses()) {
2190 locations->SetOut(Location::SameAsFirstInput());
2191 }
2192}
2193
2194void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2195 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2196 codegen_->AddSlowPath(slow_path);
2197
2198 LocationSummary* locations = instruction->GetLocations();
2199 Location value = locations->InAt(0);
2200
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002201 switch (instruction->GetType()) {
2202 case Primitive::kPrimInt: {
2203 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002204 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002205 __ j(kEqual, slow_path->GetEntryLabel());
2206 } else if (value.IsStackSlot()) {
2207 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2208 __ j(kEqual, slow_path->GetEntryLabel());
2209 } else {
2210 DCHECK(value.IsConstant()) << value;
2211 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2212 __ jmp(slow_path->GetEntryLabel());
2213 }
2214 }
2215 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002216 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002217 case Primitive::kPrimLong: {
2218 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002219 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002220 __ movl(temp, value.AsRegisterPairLow<Register>());
2221 __ orl(temp, value.AsRegisterPairHigh<Register>());
2222 __ j(kEqual, slow_path->GetEntryLabel());
2223 } else {
2224 DCHECK(value.IsConstant()) << value;
2225 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2226 __ jmp(slow_path->GetEntryLabel());
2227 }
2228 }
2229 break;
2230 }
2231 default:
2232 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002233 }
Calin Juravled0d48522014-11-04 16:40:20 +00002234}
2235
Calin Juravle9aec02f2014-11-18 23:06:35 +00002236void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2237 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2238
2239 LocationSummary* locations =
2240 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2241
2242 switch (op->GetResultType()) {
2243 case Primitive::kPrimInt: {
2244 locations->SetInAt(0, Location::RequiresRegister());
2245 // The shift count needs to be in CL.
2246 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2247 locations->SetOut(Location::SameAsFirstInput());
2248 break;
2249 }
2250 case Primitive::kPrimLong: {
2251 locations->SetInAt(0, Location::RequiresRegister());
2252 // The shift count needs to be in CL.
2253 locations->SetInAt(1, Location::RegisterLocation(ECX));
2254 locations->SetOut(Location::SameAsFirstInput());
2255 break;
2256 }
2257 default:
2258 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2259 }
2260}
2261
2262void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2263 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2264
2265 LocationSummary* locations = op->GetLocations();
2266 Location first = locations->InAt(0);
2267 Location second = locations->InAt(1);
2268 DCHECK(first.Equals(locations->Out()));
2269
2270 switch (op->GetResultType()) {
2271 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002272 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002273 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002274 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002275 DCHECK_EQ(ECX, second_reg);
2276 if (op->IsShl()) {
2277 __ shll(first_reg, second_reg);
2278 } else if (op->IsShr()) {
2279 __ sarl(first_reg, second_reg);
2280 } else {
2281 __ shrl(first_reg, second_reg);
2282 }
2283 } else {
2284 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
2285 if (op->IsShl()) {
2286 __ shll(first_reg, imm);
2287 } else if (op->IsShr()) {
2288 __ sarl(first_reg, imm);
2289 } else {
2290 __ shrl(first_reg, imm);
2291 }
2292 }
2293 break;
2294 }
2295 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002296 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002297 DCHECK_EQ(ECX, second_reg);
2298 if (op->IsShl()) {
2299 GenerateShlLong(first, second_reg);
2300 } else if (op->IsShr()) {
2301 GenerateShrLong(first, second_reg);
2302 } else {
2303 GenerateUShrLong(first, second_reg);
2304 }
2305 break;
2306 }
2307 default:
2308 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2309 }
2310}
2311
2312void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2313 Label done;
2314 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2315 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2316 __ testl(shifter, Immediate(32));
2317 __ j(kEqual, &done);
2318 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2319 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2320 __ Bind(&done);
2321}
2322
2323void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2324 Label done;
2325 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2326 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2327 __ testl(shifter, Immediate(32));
2328 __ j(kEqual, &done);
2329 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2330 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2331 __ Bind(&done);
2332}
2333
2334void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2335 Label done;
2336 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2337 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2338 __ testl(shifter, Immediate(32));
2339 __ j(kEqual, &done);
2340 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2341 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2342 __ Bind(&done);
2343}
2344
2345void LocationsBuilderX86::VisitShl(HShl* shl) {
2346 HandleShift(shl);
2347}
2348
2349void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2350 HandleShift(shl);
2351}
2352
2353void LocationsBuilderX86::VisitShr(HShr* shr) {
2354 HandleShift(shr);
2355}
2356
2357void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2358 HandleShift(shr);
2359}
2360
2361void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2362 HandleShift(ushr);
2363}
2364
2365void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2366 HandleShift(ushr);
2367}
2368
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002369void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002370 LocationSummary* locations =
2371 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002372 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002373 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002374 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2375 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002376}
2377
2378void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2379 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002380 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002381 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002382
Nicolas Geoffray707c8092014-04-04 10:50:14 +01002383 __ fs()->call(
2384 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002385
Nicolas Geoffray39468442014-09-02 15:17:15 +01002386 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002387 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002388}
2389
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002390void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2391 LocationSummary* locations =
2392 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2393 locations->SetOut(Location::RegisterLocation(EAX));
2394 InvokeRuntimeCallingConvention calling_convention;
2395 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2396 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2397 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2398}
2399
2400void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2401 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002402 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002403 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2404
2405 __ fs()->call(
2406 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
2407
2408 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2409 DCHECK(!codegen_->IsLeafMethod());
2410}
2411
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002412void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002413 LocationSummary* locations =
2414 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002415 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2416 if (location.IsStackSlot()) {
2417 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2418 } else if (location.IsDoubleStackSlot()) {
2419 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002420 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002421 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002422}
2423
2424void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002425 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002426}
2427
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002428void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002429 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002430 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002431 locations->SetInAt(0, Location::RequiresRegister());
2432 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002433}
2434
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002435void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2436 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002437 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002438 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002439 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002440 switch (not_->InputAt(0)->GetType()) {
2441 case Primitive::kPrimBoolean:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002442 __ xorl(out.AsRegister<Register>(), Immediate(1));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002443 break;
2444
2445 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002446 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002447 break;
2448
2449 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002450 __ notl(out.AsRegisterPairLow<Register>());
2451 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002452 break;
2453
2454 default:
2455 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2456 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002457}
2458
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002459void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002460 LocationSummary* locations =
2461 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002462 switch (compare->InputAt(0)->GetType()) {
2463 case Primitive::kPrimLong: {
2464 locations->SetInAt(0, Location::RequiresRegister());
2465 // TODO: we set any here but we don't handle constants
2466 locations->SetInAt(1, Location::Any());
2467 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2468 break;
2469 }
2470 case Primitive::kPrimFloat:
2471 case Primitive::kPrimDouble: {
2472 locations->SetInAt(0, Location::RequiresFpuRegister());
2473 locations->SetInAt(1, Location::RequiresFpuRegister());
2474 locations->SetOut(Location::RequiresRegister());
2475 break;
2476 }
2477 default:
2478 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2479 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002480}
2481
2482void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002483 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002484 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002485 Location left = locations->InAt(0);
2486 Location right = locations->InAt(1);
2487
2488 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002489 switch (compare->InputAt(0)->GetType()) {
2490 case Primitive::kPrimLong: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002491 if (right.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002492 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002493 } else {
2494 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002495 __ cmpl(left.AsRegisterPairHigh<Register>(),
2496 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002497 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002498 __ j(kLess, &less); // Signed compare.
2499 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002500 if (right.IsRegisterPair()) {
2501 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002502 } else {
2503 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002504 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002505 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002506 break;
2507 }
2508 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002509 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002510 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
2511 break;
2512 }
2513 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002514 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002515 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002516 break;
2517 }
2518 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002519 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002520 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002521 __ movl(out, Immediate(0));
2522 __ j(kEqual, &done);
2523 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
2524
2525 __ Bind(&greater);
2526 __ movl(out, Immediate(1));
2527 __ jmp(&done);
2528
2529 __ Bind(&less);
2530 __ movl(out, Immediate(-1));
2531
2532 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002533}
2534
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002535void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002536 LocationSummary* locations =
2537 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002538 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2539 locations->SetInAt(i, Location::Any());
2540 }
2541 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002542}
2543
2544void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002545 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002546 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002547}
2548
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002549void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002550 LocationSummary* locations =
2551 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002552 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002553 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002554 bool needs_write_barrier =
2555 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
2556
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002557 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2558 || (field_type == Primitive::kPrimByte);
2559 // The register allocator does not support multiple
2560 // inputs that die at entry with one in a specific register.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002561 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002562 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002563 locations->SetInAt(1, Location::RegisterLocation(EAX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002564 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002565 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002566 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002567 // Temporary registers for the write barrier.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002568 if (needs_write_barrier) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002569 locations->AddTemp(Location::RequiresRegister());
2570 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002571 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002572 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002573}
2574
2575void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2576 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002577 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002578 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01002579 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002580
2581 switch (field_type) {
2582 case Primitive::kPrimBoolean:
2583 case Primitive::kPrimByte: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002584 ByteRegister value = locations->InAt(1).AsRegister<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002585 __ movb(Address(obj, offset), value);
2586 break;
2587 }
2588
2589 case Primitive::kPrimShort:
2590 case Primitive::kPrimChar: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002591 Register value = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002592 __ movw(Address(obj, offset), value);
2593 break;
2594 }
2595
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002596 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002597 case Primitive::kPrimNot: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002598 Register value = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002599 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002600
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002601 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002602 Register temp = locations->GetTemp(0).AsRegister<Register>();
2603 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002604 codegen_->MarkGCCard(temp, card, obj, value);
2605 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002606 break;
2607 }
2608
2609 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002610 Location value = locations->InAt(1);
2611 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
2612 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002613 break;
2614 }
2615
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002616 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002617 XmmRegister value = locations->InAt(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002618 __ movss(Address(obj, offset), value);
2619 break;
2620 }
2621
2622 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002623 XmmRegister value = locations->InAt(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002624 __ movsd(Address(obj, offset), value);
2625 break;
2626 }
2627
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002628 case Primitive::kPrimVoid:
2629 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002630 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002631 }
2632}
2633
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002634void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
2635 Label is_null;
2636 __ testl(value, value);
2637 __ j(kEqual, &is_null);
2638 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2639 __ movl(temp, object);
2640 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
2641 __ movb(Address(temp, card, TIMES_1, 0),
2642 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
2643 __ Bind(&is_null);
2644}
2645
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002646void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002647 LocationSummary* locations =
2648 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002649 locations->SetInAt(0, Location::RequiresRegister());
2650 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002651}
2652
2653void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2654 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002655 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002656 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2657
2658 switch (instruction->GetType()) {
2659 case Primitive::kPrimBoolean: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002660 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002661 __ movzxb(out, Address(obj, offset));
2662 break;
2663 }
2664
2665 case Primitive::kPrimByte: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002666 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002667 __ movsxb(out, Address(obj, offset));
2668 break;
2669 }
2670
2671 case Primitive::kPrimShort: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002672 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002673 __ movsxw(out, Address(obj, offset));
2674 break;
2675 }
2676
2677 case Primitive::kPrimChar: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002678 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002679 __ movzxw(out, Address(obj, offset));
2680 break;
2681 }
2682
2683 case Primitive::kPrimInt:
2684 case Primitive::kPrimNot: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002685 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002686 __ movl(out, Address(obj, offset));
2687 break;
2688 }
2689
2690 case Primitive::kPrimLong: {
2691 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002692 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
2693 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002694 break;
2695 }
2696
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002697 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002698 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002699 __ movss(out, Address(obj, offset));
2700 break;
2701 }
2702
2703 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002704 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002705 __ movsd(out, Address(obj, offset));
2706 break;
2707 }
2708
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002709 case Primitive::kPrimVoid:
2710 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002711 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002712 }
2713}
2714
2715void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002716 LocationSummary* locations =
2717 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002718 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002719 if (instruction->HasUses()) {
2720 locations->SetOut(Location::SameAsFirstInput());
2721 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002722}
2723
2724void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002725 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002726 codegen_->AddSlowPath(slow_path);
2727
2728 LocationSummary* locations = instruction->GetLocations();
2729 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002730
2731 if (obj.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002732 __ cmpl(obj.AsRegister<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002733 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002734 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002735 } else {
2736 DCHECK(obj.IsConstant()) << obj;
2737 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2738 __ jmp(slow_path->GetEntryLabel());
2739 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002740 }
2741 __ j(kEqual, slow_path->GetEntryLabel());
2742}
2743
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002744void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002745 LocationSummary* locations =
2746 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002747 locations->SetInAt(0, Location::RequiresRegister());
2748 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2749 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002750}
2751
2752void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
2753 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002754 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002755 Location index = locations->InAt(1);
2756
2757 switch (instruction->GetType()) {
2758 case Primitive::kPrimBoolean: {
2759 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002760 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002761 if (index.IsConstant()) {
2762 __ movzxb(out, Address(obj,
2763 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2764 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002765 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002766 }
2767 break;
2768 }
2769
2770 case Primitive::kPrimByte: {
2771 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002772 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002773 if (index.IsConstant()) {
2774 __ movsxb(out, Address(obj,
2775 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2776 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002777 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002778 }
2779 break;
2780 }
2781
2782 case Primitive::kPrimShort: {
2783 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002784 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002785 if (index.IsConstant()) {
2786 __ movsxw(out, Address(obj,
2787 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2788 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002789 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002790 }
2791 break;
2792 }
2793
2794 case Primitive::kPrimChar: {
2795 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002796 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002797 if (index.IsConstant()) {
2798 __ movzxw(out, Address(obj,
2799 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2800 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002801 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002802 }
2803 break;
2804 }
2805
2806 case Primitive::kPrimInt:
2807 case Primitive::kPrimNot: {
2808 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002809 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002810 if (index.IsConstant()) {
2811 __ movl(out, Address(obj,
2812 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2813 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002814 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002815 }
2816 break;
2817 }
2818
2819 case Primitive::kPrimLong: {
2820 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002821 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002822 if (index.IsConstant()) {
2823 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002824 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
2825 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002826 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002827 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00002828 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002829 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00002830 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002831 }
2832 break;
2833 }
2834
2835 case Primitive::kPrimFloat:
2836 case Primitive::kPrimDouble:
2837 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002838 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002839 case Primitive::kPrimVoid:
2840 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002841 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002842 }
2843}
2844
2845void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002846 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002847 bool needs_write_barrier =
2848 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2849
2850 DCHECK(kFollowsQuickABI);
2851 bool not_enough_registers = needs_write_barrier
2852 && !instruction->GetValue()->IsConstant()
2853 && !instruction->GetIndex()->IsConstant();
2854 bool needs_runtime_call = instruction->NeedsTypeCheck() || not_enough_registers;
2855
Nicolas Geoffray39468442014-09-02 15:17:15 +01002856 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2857 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002858 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002859
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002860 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002861 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002862 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2863 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2864 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002865 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002866 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
2867 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002868 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002869 // In case of a byte operation, the register allocator does not support multiple
2870 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002871 locations->SetInAt(0, Location::RequiresRegister());
2872 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002873 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002874 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002875 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002876 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002877 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002878 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002879 // Temporary registers for the write barrier.
2880 if (needs_write_barrier) {
2881 locations->AddTemp(Location::RequiresRegister());
2882 // Ensure the card is in a byte register.
2883 locations->AddTemp(Location::RegisterLocation(ECX));
2884 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002885 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002886}
2887
2888void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
2889 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002890 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002891 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002892 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002893 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002894 bool needs_runtime_call = locations->WillCall();
2895 bool needs_write_barrier =
2896 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002897
2898 switch (value_type) {
2899 case Primitive::kPrimBoolean:
2900 case Primitive::kPrimByte: {
2901 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002902 if (index.IsConstant()) {
2903 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002904 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002905 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002906 } else {
2907 __ movb(Address(obj, offset),
2908 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2909 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002910 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002911 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002912 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
2913 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002914 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002915 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002916 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2917 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002918 }
2919 break;
2920 }
2921
2922 case Primitive::kPrimShort:
2923 case Primitive::kPrimChar: {
2924 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002925 if (index.IsConstant()) {
2926 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002927 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002928 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002929 } else {
2930 __ movw(Address(obj, offset),
2931 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2932 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002933 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002934 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002935 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
2936 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002937 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002938 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002939 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2940 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002941 }
2942 break;
2943 }
2944
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002945 case Primitive::kPrimInt:
2946 case Primitive::kPrimNot: {
2947 if (!needs_runtime_call) {
2948 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2949 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002950 size_t offset =
2951 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002952 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002953 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002954 } else {
2955 DCHECK(value.IsConstant()) << value;
2956 __ movl(Address(obj, offset),
2957 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2958 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002959 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002960 DCHECK(index.IsRegister()) << index;
2961 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002962 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
2963 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002964 } else {
2965 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00002966 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002967 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2968 }
2969 }
2970
2971 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002972 Register temp = locations->GetTemp(0).AsRegister<Register>();
2973 Register card = locations->GetTemp(1).AsRegister<Register>();
2974 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002975 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002976 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002977 DCHECK_EQ(value_type, Primitive::kPrimNot);
2978 DCHECK(!codegen_->IsLeafMethod());
2979 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
2980 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002981 }
2982 break;
2983 }
2984
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002985 case Primitive::kPrimLong: {
2986 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002987 if (index.IsConstant()) {
2988 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002989 if (value.IsRegisterPair()) {
2990 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
2991 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002992 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002993 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002994 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
2995 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
2996 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
2997 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002998 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002999 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003000 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003001 value.AsRegisterPairLow<Register>());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003002 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003003 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003004 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003005 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003006 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003007 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003008 Immediate(Low32Bits(val)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003009 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003010 Immediate(High32Bits(val)));
3011 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003012 }
3013 break;
3014 }
3015
3016 case Primitive::kPrimFloat:
3017 case Primitive::kPrimDouble:
3018 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003019 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003020 case Primitive::kPrimVoid:
3021 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003022 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003023 }
3024}
3025
3026void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3027 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003028 locations->SetInAt(0, Location::RequiresRegister());
3029 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003030 instruction->SetLocations(locations);
3031}
3032
3033void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3034 LocationSummary* locations = instruction->GetLocations();
3035 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003036 Register obj = locations->InAt(0).AsRegister<Register>();
3037 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003038 __ movl(out, Address(obj, offset));
3039}
3040
3041void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003042 LocationSummary* locations =
3043 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003044 locations->SetInAt(0, Location::RequiresRegister());
3045 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003046 if (instruction->HasUses()) {
3047 locations->SetOut(Location::SameAsFirstInput());
3048 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003049}
3050
3051void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3052 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003053 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01003054 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003055 codegen_->AddSlowPath(slow_path);
3056
Roland Levillain271ab9c2014-11-27 15:23:57 +00003057 Register index = locations->InAt(0).AsRegister<Register>();
3058 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003059
3060 __ cmpl(index, length);
3061 __ j(kAboveEqual, slow_path->GetEntryLabel());
3062}
3063
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003064void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3065 temp->SetLocations(nullptr);
3066}
3067
3068void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3069 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003070 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003071}
3072
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003073void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003074 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003075 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003076}
3077
3078void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003079 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3080}
3081
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003082void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3083 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3084}
3085
3086void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003087 HBasicBlock* block = instruction->GetBlock();
3088 if (block->GetLoopInformation() != nullptr) {
3089 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3090 // The back edge will generate the suspend check.
3091 return;
3092 }
3093 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3094 // The goto will generate the suspend check.
3095 return;
3096 }
3097 GenerateSuspendCheck(instruction, nullptr);
3098}
3099
3100void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3101 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003102 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003103 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003104 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003105 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003106 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003107 if (successor == nullptr) {
3108 __ j(kNotEqual, slow_path->GetEntryLabel());
3109 __ Bind(slow_path->GetReturnLabel());
3110 } else {
3111 __ j(kEqual, codegen_->GetLabelOf(successor));
3112 __ jmp(slow_path->GetEntryLabel());
3113 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003114}
3115
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003116X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3117 return codegen_->GetAssembler();
3118}
3119
3120void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
3121 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003122 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003123 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3124 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
3125 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
3126}
3127
3128void ParallelMoveResolverX86::EmitMove(size_t index) {
3129 MoveOperands* move = moves_.Get(index);
3130 Location source = move->GetSource();
3131 Location destination = move->GetDestination();
3132
3133 if (source.IsRegister()) {
3134 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003135 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003136 } else {
3137 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003138 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003139 }
3140 } else if (source.IsStackSlot()) {
3141 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003142 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003143 } else {
3144 DCHECK(destination.IsStackSlot());
3145 MoveMemoryToMemory(destination.GetStackIndex(),
3146 source.GetStackIndex());
3147 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003148 } else if (source.IsConstant()) {
3149 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
3150 Immediate imm(instruction->AsIntConstant()->GetValue());
3151 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003152 __ movl(destination.AsRegister<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003153 } else {
3154 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3155 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003156 } else {
3157 LOG(FATAL) << "Unimplemented";
3158 }
3159}
3160
3161void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003162 Register suggested_scratch = reg == EAX ? EBX : EAX;
3163 ScratchRegisterScope ensure_scratch(
3164 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3165
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003166 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3167 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
3168 __ movl(Address(ESP, mem + stack_offset), reg);
3169 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
3170}
3171
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003172void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
3173 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003174 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3175
3176 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003177 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003178 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3179
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003180 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
3181 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
3182 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
3183 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
3184 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
3185 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
3186}
3187
3188void ParallelMoveResolverX86::EmitSwap(size_t index) {
3189 MoveOperands* move = moves_.Get(index);
3190 Location source = move->GetSource();
3191 Location destination = move->GetDestination();
3192
3193 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003194 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003195 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003196 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003197 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003198 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003199 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3200 Exchange(destination.GetStackIndex(), source.GetStackIndex());
3201 } else {
3202 LOG(FATAL) << "Unimplemented";
3203 }
3204}
3205
3206void ParallelMoveResolverX86::SpillScratch(int reg) {
3207 __ pushl(static_cast<Register>(reg));
3208}
3209
3210void ParallelMoveResolverX86::RestoreScratch(int reg) {
3211 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003212}
3213
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003214void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003215 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3216 ? LocationSummary::kCallOnSlowPath
3217 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003218 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003219 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003220 locations->SetOut(Location::RequiresRegister());
3221}
3222
3223void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003224 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003225 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003226 DCHECK(!cls->CanCallRuntime());
3227 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003228 codegen_->LoadCurrentMethod(out);
3229 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3230 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003231 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003232 codegen_->LoadCurrentMethod(out);
3233 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3234 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003235
3236 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3237 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3238 codegen_->AddSlowPath(slow_path);
3239 __ testl(out, out);
3240 __ j(kEqual, slow_path->GetEntryLabel());
3241 if (cls->MustGenerateClinitCheck()) {
3242 GenerateClassInitializationCheck(slow_path, out);
3243 } else {
3244 __ Bind(slow_path->GetExitLabel());
3245 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003246 }
3247}
3248
3249void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
3250 LocationSummary* locations =
3251 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3252 locations->SetInAt(0, Location::RequiresRegister());
3253 if (check->HasUses()) {
3254 locations->SetOut(Location::SameAsFirstInput());
3255 }
3256}
3257
3258void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003259 // We assume the class to not be null.
3260 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3261 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003262 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003263 GenerateClassInitializationCheck(slow_path,
3264 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003265}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003266
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003267void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
3268 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003269 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3270 Immediate(mirror::Class::kStatusInitialized));
3271 __ j(kLess, slow_path->GetEntryLabel());
3272 __ Bind(slow_path->GetExitLabel());
3273 // No need for memory fence, thanks to the X86 memory model.
3274}
3275
3276void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3277 LocationSummary* locations =
3278 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3279 locations->SetInAt(0, Location::RequiresRegister());
3280 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3281}
3282
3283void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3284 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003285 Register cls = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003286 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
3287
3288 switch (instruction->GetType()) {
3289 case Primitive::kPrimBoolean: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003290 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003291 __ movzxb(out, Address(cls, offset));
3292 break;
3293 }
3294
3295 case Primitive::kPrimByte: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003296 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003297 __ movsxb(out, Address(cls, offset));
3298 break;
3299 }
3300
3301 case Primitive::kPrimShort: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003302 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003303 __ movsxw(out, Address(cls, offset));
3304 break;
3305 }
3306
3307 case Primitive::kPrimChar: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003308 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003309 __ movzxw(out, Address(cls, offset));
3310 break;
3311 }
3312
3313 case Primitive::kPrimInt:
3314 case Primitive::kPrimNot: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003315 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003316 __ movl(out, Address(cls, offset));
3317 break;
3318 }
3319
3320 case Primitive::kPrimLong: {
3321 // TODO: support volatile.
3322 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(cls, offset));
3323 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(cls, kX86WordSize + offset));
3324 break;
3325 }
3326
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003327 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003328 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003329 __ movss(out, Address(cls, offset));
3330 break;
3331 }
3332
3333 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003334 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003335 __ movsd(out, Address(cls, offset));
3336 break;
3337 }
3338
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003339 case Primitive::kPrimVoid:
3340 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3341 UNREACHABLE();
3342 }
3343}
3344
3345void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3346 LocationSummary* locations =
3347 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3348 locations->SetInAt(0, Location::RequiresRegister());
3349 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003350 bool needs_write_barrier =
3351 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003352 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3353 || (field_type == Primitive::kPrimByte);
3354 // The register allocator does not support multiple
3355 // inputs that die at entry with one in a specific register.
3356 if (is_byte_type) {
3357 // Ensure the value is in a byte register.
3358 locations->SetInAt(1, Location::RegisterLocation(EAX));
3359 } else {
3360 locations->SetInAt(1, Location::RequiresRegister());
3361 }
3362 // Temporary registers for the write barrier.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003363 if (needs_write_barrier) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003364 locations->AddTemp(Location::RequiresRegister());
3365 // Ensure the card is in a byte register.
3366 locations->AddTemp(Location::RegisterLocation(ECX));
3367 }
3368}
3369
3370void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3371 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003372 Register cls = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003373 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
3374 Primitive::Type field_type = instruction->GetFieldType();
3375
3376 switch (field_type) {
3377 case Primitive::kPrimBoolean:
3378 case Primitive::kPrimByte: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003379 ByteRegister value = locations->InAt(1).AsRegister<ByteRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003380 __ movb(Address(cls, offset), value);
3381 break;
3382 }
3383
3384 case Primitive::kPrimShort:
3385 case Primitive::kPrimChar: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003386 Register value = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003387 __ movw(Address(cls, offset), value);
3388 break;
3389 }
3390
3391 case Primitive::kPrimInt:
3392 case Primitive::kPrimNot: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003393 Register value = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003394 __ movl(Address(cls, offset), value);
3395
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003396 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003397 Register temp = locations->GetTemp(0).AsRegister<Register>();
3398 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003399 codegen_->MarkGCCard(temp, card, cls, value);
3400 }
3401 break;
3402 }
3403
3404 case Primitive::kPrimLong: {
3405 Location value = locations->InAt(1);
3406 __ movl(Address(cls, offset), value.AsRegisterPairLow<Register>());
3407 __ movl(Address(cls, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3408 break;
3409 }
3410
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003411 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003412 XmmRegister value = locations->InAt(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003413 __ movss(Address(cls, offset), value);
3414 break;
3415 }
3416
3417 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003418 XmmRegister value = locations->InAt(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003419 __ movsd(Address(cls, offset), value);
3420 break;
3421 }
3422
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003423 case Primitive::kPrimVoid:
3424 LOG(FATAL) << "Unreachable type " << field_type;
3425 UNREACHABLE();
3426 }
3427}
3428
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003429void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
3430 LocationSummary* locations =
3431 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3432 locations->SetOut(Location::RequiresRegister());
3433}
3434
3435void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
3436 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
3437 codegen_->AddSlowPath(slow_path);
3438
Roland Levillain271ab9c2014-11-27 15:23:57 +00003439 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003440 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08003441 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3442 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003443 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3444 __ testl(out, out);
3445 __ j(kEqual, slow_path->GetEntryLabel());
3446 __ Bind(slow_path->GetExitLabel());
3447}
3448
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003449void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
3450 LocationSummary* locations =
3451 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3452 locations->SetOut(Location::RequiresRegister());
3453}
3454
3455void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
3456 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003457 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003458 __ fs()->movl(address, Immediate(0));
3459}
3460
3461void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
3462 LocationSummary* locations =
3463 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3464 InvokeRuntimeCallingConvention calling_convention;
3465 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3466}
3467
3468void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
3469 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
3470 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3471}
3472
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003473void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003474 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3475 ? LocationSummary::kNoCall
3476 : LocationSummary::kCallOnSlowPath;
3477 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3478 locations->SetInAt(0, Location::RequiresRegister());
3479 locations->SetInAt(1, Location::Any());
3480 locations->SetOut(Location::RequiresRegister());
3481}
3482
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003483void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003484 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003485 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003486 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003487 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003488 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3489 Label done, zero;
3490 SlowPathCodeX86* slow_path = nullptr;
3491
3492 // Return 0 if `obj` is null.
3493 // TODO: avoid this check if we know obj is not null.
3494 __ testl(obj, obj);
3495 __ j(kEqual, &zero);
3496 __ movl(out, Address(obj, class_offset));
3497 // Compare the class of `obj` with `cls`.
3498 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003499 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003500 } else {
3501 DCHECK(cls.IsStackSlot()) << cls;
3502 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
3503 }
3504
3505 if (instruction->IsClassFinal()) {
3506 // Classes must be equal for the instanceof to succeed.
3507 __ j(kNotEqual, &zero);
3508 __ movl(out, Immediate(1));
3509 __ jmp(&done);
3510 } else {
3511 // If the classes are not equal, we go into a slow path.
3512 DCHECK(locations->OnlyCallsOnSlowPath());
3513 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003514 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003515 codegen_->AddSlowPath(slow_path);
3516 __ j(kNotEqual, slow_path->GetEntryLabel());
3517 __ movl(out, Immediate(1));
3518 __ jmp(&done);
3519 }
3520 __ Bind(&zero);
3521 __ movl(out, Immediate(0));
3522 if (slow_path != nullptr) {
3523 __ Bind(slow_path->GetExitLabel());
3524 }
3525 __ Bind(&done);
3526}
3527
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003528void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
3529 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3530 instruction, LocationSummary::kCallOnSlowPath);
3531 locations->SetInAt(0, Location::RequiresRegister());
3532 locations->SetInAt(1, Location::Any());
3533 locations->AddTemp(Location::RequiresRegister());
3534}
3535
3536void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
3537 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003538 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003539 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003540 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003541 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3542 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
3543 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3544 codegen_->AddSlowPath(slow_path);
3545
3546 // TODO: avoid this check if we know obj is not null.
3547 __ testl(obj, obj);
3548 __ j(kEqual, slow_path->GetExitLabel());
3549 __ movl(temp, Address(obj, class_offset));
3550
3551 // Compare the class of `obj` with `cls`.
3552 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003553 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003554 } else {
3555 DCHECK(cls.IsStackSlot()) << cls;
3556 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
3557 }
3558
3559 __ j(kNotEqual, slow_path->GetEntryLabel());
3560 __ Bind(slow_path->GetExitLabel());
3561}
3562
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003563void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3564 LocationSummary* locations =
3565 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3566 InvokeRuntimeCallingConvention calling_convention;
3567 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3568}
3569
3570void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3571 __ fs()->call(Address::Absolute(instruction->IsEnter()
3572 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
3573 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
3574 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3575}
3576
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003577void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3578void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3579void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3580
3581void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3582 LocationSummary* locations =
3583 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3584 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3585 || instruction->GetResultType() == Primitive::kPrimLong);
3586 locations->SetInAt(0, Location::RequiresRegister());
3587 locations->SetInAt(1, Location::Any());
3588 locations->SetOut(Location::SameAsFirstInput());
3589}
3590
3591void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
3592 HandleBitwiseOperation(instruction);
3593}
3594
3595void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
3596 HandleBitwiseOperation(instruction);
3597}
3598
3599void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
3600 HandleBitwiseOperation(instruction);
3601}
3602
3603void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3604 LocationSummary* locations = instruction->GetLocations();
3605 Location first = locations->InAt(0);
3606 Location second = locations->InAt(1);
3607 DCHECK(first.Equals(locations->Out()));
3608
3609 if (instruction->GetResultType() == Primitive::kPrimInt) {
3610 if (second.IsRegister()) {
3611 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003612 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003613 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003614 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003615 } else {
3616 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003617 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003618 }
3619 } else if (second.IsConstant()) {
3620 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003621 __ andl(first.AsRegister<Register>(),
3622 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003623 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003624 __ orl(first.AsRegister<Register>(),
3625 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003626 } else {
3627 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00003628 __ xorl(first.AsRegister<Register>(),
3629 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003630 }
3631 } else {
3632 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003633 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003634 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003635 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003636 } else {
3637 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003638 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003639 }
3640 }
3641 } else {
3642 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3643 if (second.IsRegisterPair()) {
3644 if (instruction->IsAnd()) {
3645 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3646 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3647 } else if (instruction->IsOr()) {
3648 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3649 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3650 } else {
3651 DCHECK(instruction->IsXor());
3652 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3653 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3654 }
3655 } else {
3656 if (instruction->IsAnd()) {
3657 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3658 __ andl(first.AsRegisterPairHigh<Register>(),
3659 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3660 } else if (instruction->IsOr()) {
3661 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3662 __ orl(first.AsRegisterPairHigh<Register>(),
3663 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3664 } else {
3665 DCHECK(instruction->IsXor());
3666 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3667 __ xorl(first.AsRegisterPairHigh<Register>(),
3668 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3669 }
3670 }
3671 }
3672}
3673
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003674} // namespace x86
3675} // namespace art