blob: 2aa121d04b1073947b49d10212be37ce61216ff5 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
19#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010020#include "gc/accounting/card_table.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070021#include "mirror/array-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010022#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010023#include "mirror/class.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010024#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000025#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010026#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000027#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010028#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010031
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032namespace x86 {
33
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010034static constexpr bool kExplicitStackOverflowCheck = false;
35
36static constexpr int kNumberOfPushedRegistersAtEntry = 1;
37static constexpr int kCurrentMethodStackOffset = 0;
38
Calin Juravled6fb6cf2014-11-11 19:07:44 +000039static constexpr Register kRuntimeParameterCoreRegisters[] = { EAX, ECX, EDX, EBX };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010040static constexpr size_t kRuntimeParameterCoreRegistersLength =
41 arraysize(kRuntimeParameterCoreRegisters);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010042static constexpr XmmRegister kRuntimeParameterFpuRegisters[] = { };
43static constexpr size_t kRuntimeParameterFpuRegistersLength = 0;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010044
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000045// Marker for places that can be updated once we don't follow the quick ABI.
46static constexpr bool kFollowsQuickABI = true;
47
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010048class InvokeRuntimeCallingConvention : public CallingConvention<Register, XmmRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010049 public:
50 InvokeRuntimeCallingConvention()
51 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010052 kRuntimeParameterCoreRegistersLength,
53 kRuntimeParameterFpuRegisters,
54 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010055
56 private:
57 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
58};
59
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
61
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010062class SlowPathCodeX86 : public SlowPathCode {
63 public:
64 SlowPathCodeX86() : entry_label_(), exit_label_() {}
65
66 Label* GetEntryLabel() { return &entry_label_; }
67 Label* GetExitLabel() { return &exit_label_; }
68
69 private:
70 Label entry_label_;
71 Label exit_label_;
72
73 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86);
74};
75
76class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010077 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010078 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079
80 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
81 __ Bind(GetEntryLabel());
82 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010083 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010084 }
85
86 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010087 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010088 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
89};
90
Calin Juravled0d48522014-11-04 16:40:20 +000091class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
92 public:
93 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
94
95 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
96 __ Bind(GetEntryLabel());
97 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
98 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
99 }
100
101 private:
102 HDivZeroCheck* const instruction_;
103 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
104};
105
Calin Juravlebacfec32014-11-14 15:54:36 +0000106class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +0000107 public:
Calin Juravlebacfec32014-11-14 15:54:36 +0000108 explicit DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000109
110 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
111 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000112 if (is_div_) {
113 __ negl(reg_);
114 } else {
115 __ movl(reg_, Immediate(0));
116 }
Calin Juravled0d48522014-11-04 16:40:20 +0000117 __ jmp(GetExitLabel());
118 }
119
120 private:
121 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000122 bool is_div_;
123 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000124};
125
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100126class StackOverflowCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100127 public:
128 StackOverflowCheckSlowPathX86() {}
129
130 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
131 __ Bind(GetEntryLabel());
132 __ addl(ESP,
133 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
134 __ fs()->jmp(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowStackOverflow)));
135 }
136
137 private:
138 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86);
139};
140
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100141class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100142 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100143 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
144 Location index_location,
145 Location length_location)
Roland Levillain199f3362014-11-27 17:15:16 +0000146 : instruction_(instruction),
147 index_location_(index_location),
148 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100149
150 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100151 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100152 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000153 // We're moving two locations to locations that could overlap, so we need a parallel
154 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100155 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000156 x86_codegen->EmitParallelMoves(
157 index_location_,
158 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
159 length_location_,
160 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100161 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100162 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100163 }
164
165 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100166 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100167 const Location index_location_;
168 const Location length_location_;
169
170 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
171};
172
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100173class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000174 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100175 explicit SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
176 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000177
178 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100179 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000180 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100181 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000182 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
183 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100184 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100185 if (successor_ == nullptr) {
186 __ jmp(GetReturnLabel());
187 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100188 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100189 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000190 }
191
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100192 Label* GetReturnLabel() {
193 DCHECK(successor_ == nullptr);
194 return &return_label_;
195 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000196
197 private:
198 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100199 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000200 Label return_label_;
201
202 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
203};
204
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000205class LoadStringSlowPathX86 : public SlowPathCodeX86 {
206 public:
207 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
208
209 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
210 LocationSummary* locations = instruction_->GetLocations();
211 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
212
213 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
214 __ Bind(GetEntryLabel());
215 codegen->SaveLiveRegisters(locations);
216
217 InvokeRuntimeCallingConvention calling_convention;
218 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(0));
219 __ movl(calling_convention.GetRegisterAt(1), Immediate(instruction_->GetStringIndex()));
220 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
221 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
222 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
223 codegen->RestoreLiveRegisters(locations);
224
225 __ jmp(GetExitLabel());
226 }
227
228 private:
229 HLoadString* const instruction_;
230
231 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
232};
233
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000234class LoadClassSlowPathX86 : public SlowPathCodeX86 {
235 public:
236 LoadClassSlowPathX86(HLoadClass* cls,
237 HInstruction* at,
238 uint32_t dex_pc,
239 bool do_clinit)
240 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
241 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
242 }
243
244 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
245 LocationSummary* locations = at_->GetLocations();
246 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
247 __ Bind(GetEntryLabel());
248 codegen->SaveLiveRegisters(locations);
249
250 InvokeRuntimeCallingConvention calling_convention;
251 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
252 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
253 __ fs()->call(Address::Absolute(do_clinit_
254 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
255 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
256 codegen->RecordPcInfo(at_, dex_pc_);
257
258 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000259 Location out = locations->Out();
260 if (out.IsValid()) {
261 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
262 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000263 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000264
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000265 codegen->RestoreLiveRegisters(locations);
266 __ jmp(GetExitLabel());
267 }
268
269 private:
270 // The class this slow path will load.
271 HLoadClass* const cls_;
272
273 // The instruction where this slow path is happening.
274 // (Might be the load class or an initialization check).
275 HInstruction* const at_;
276
277 // The dex PC of `at_`.
278 const uint32_t dex_pc_;
279
280 // Whether to initialize the class.
281 const bool do_clinit_;
282
283 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
284};
285
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000286class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
287 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000288 TypeCheckSlowPathX86(HInstruction* instruction,
289 Location class_to_check,
290 Location object_class,
291 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000292 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000293 class_to_check_(class_to_check),
294 object_class_(object_class),
295 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000296
297 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
298 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000299 DCHECK(instruction_->IsCheckCast()
300 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000301
302 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
303 __ Bind(GetEntryLabel());
304 codegen->SaveLiveRegisters(locations);
305
306 // We're moving two locations to locations that could overlap, so we need a parallel
307 // move resolver.
308 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000309 x86_codegen->EmitParallelMoves(
310 class_to_check_,
311 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
312 object_class_,
313 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000314
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000315 if (instruction_->IsInstanceOf()) {
Roland Levillain199f3362014-11-27 17:15:16 +0000316 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize,
317 pInstanceofNonTrivial)));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000318 } else {
319 DCHECK(instruction_->IsCheckCast());
320 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
321 }
322
323 codegen->RecordPcInfo(instruction_, dex_pc_);
324 if (instruction_->IsInstanceOf()) {
325 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
326 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000327 codegen->RestoreLiveRegisters(locations);
328
329 __ jmp(GetExitLabel());
330 }
331
332 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000333 HInstruction* const instruction_;
334 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000335 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000336 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000337
338 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
339};
340
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100341#undef __
342#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
343
Dave Allison20dfc792014-06-16 20:44:29 -0700344inline Condition X86Condition(IfCondition cond) {
345 switch (cond) {
346 case kCondEQ: return kEqual;
347 case kCondNE: return kNotEqual;
348 case kCondLT: return kLess;
349 case kCondLE: return kLessEqual;
350 case kCondGT: return kGreater;
351 case kCondGE: return kGreaterEqual;
352 default:
353 LOG(FATAL) << "Unknown if condition";
354 }
355 return kEqual;
356}
357
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100358void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
359 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
360}
361
362void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
363 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
364}
365
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100366size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
367 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
368 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100369}
370
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100371size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
372 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
373 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100374}
375
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100376CodeGeneratorX86::CodeGeneratorX86(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100377 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfXmmRegisters, kNumberOfRegisterPairs),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100378 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100379 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100380 instruction_visitor_(graph, this),
381 move_resolver_(graph->GetArena(), this) {}
382
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100383size_t CodeGeneratorX86::FrameEntrySpillSize() const {
384 return kNumberOfPushedRegistersAtEntry * kX86WordSize;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100385}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100386
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100387Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100388 switch (type) {
389 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100390 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100391 X86ManagedRegister pair =
392 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100393 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
394 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100395 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
396 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100397 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100398 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100399 }
400
401 case Primitive::kPrimByte:
402 case Primitive::kPrimBoolean:
403 case Primitive::kPrimChar:
404 case Primitive::kPrimShort:
405 case Primitive::kPrimInt:
406 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100407 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100408 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100409 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100410 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
411 X86ManagedRegister current =
412 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
413 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100414 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100415 }
416 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100417 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100418 }
419
420 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100421 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100422 return Location::FpuRegisterLocation(
423 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100424 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100425
426 case Primitive::kPrimVoid:
427 LOG(FATAL) << "Unreachable type " << type;
428 }
429
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100430 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100431}
432
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100433void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100434 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100435 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100436
437 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100438 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100439
440 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000441 DCHECK(kFollowsQuickABI);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100442 blocked_core_registers_[EBP] = true;
443 blocked_core_registers_[ESI] = true;
444 blocked_core_registers_[EDI] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100445
446 UpdateBlockedPairRegisters();
447}
448
449void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
450 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
451 X86ManagedRegister current =
452 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
453 if (blocked_core_registers_[current.AsRegisterPairLow()]
454 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
455 blocked_register_pairs_[i] = true;
456 }
457 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100458}
459
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100460InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
461 : HGraphVisitor(graph),
462 assembler_(codegen->GetAssembler()),
463 codegen_(codegen) {}
464
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000465void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000466 // Create a fake register to mimic Quick.
467 static const int kFakeReturnRegister = 8;
468 core_spill_mask_ |= (1 << kFakeReturnRegister);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000469
Roland Levillain199f3362014-11-27 17:15:16 +0000470 bool skip_overflow_check =
471 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100472 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
473 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100474 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100475 }
476
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100477 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100478 __ subl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100479
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100480 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100481 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86();
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100482 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100483
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100484 __ fs()->cmpl(ESP, Address::Absolute(Thread::StackEndOffset<kX86WordSize>()));
485 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100486 }
487
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100488 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000489}
490
491void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100492 __ addl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000493}
494
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100495void CodeGeneratorX86::Bind(HBasicBlock* block) {
496 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000497}
498
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100499void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100500 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000501}
502
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100503Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
504 switch (load->GetType()) {
505 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100506 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100507 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
508 break;
509
510 case Primitive::kPrimInt:
511 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100512 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100513 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100514
515 case Primitive::kPrimBoolean:
516 case Primitive::kPrimByte:
517 case Primitive::kPrimChar:
518 case Primitive::kPrimShort:
519 case Primitive::kPrimVoid:
520 LOG(FATAL) << "Unexpected type " << load->GetType();
521 }
522
523 LOG(FATAL) << "Unreachable";
524 return Location();
525}
526
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100527Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
528 switch (type) {
529 case Primitive::kPrimBoolean:
530 case Primitive::kPrimByte:
531 case Primitive::kPrimChar:
532 case Primitive::kPrimShort:
533 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100534 case Primitive::kPrimFloat:
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100535 case Primitive::kPrimNot: {
536 uint32_t index = gp_index_++;
537 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100538 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100539 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100540 return Location::StackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100541 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100542 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100543
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100544 case Primitive::kPrimLong:
545 case Primitive::kPrimDouble: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100546 uint32_t index = gp_index_;
547 gp_index_ += 2;
548 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100549 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
550 calling_convention.GetRegisterPairAt(index));
551 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100552 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000553 // On X86, the register index and stack index of a quick parameter is the same, since
554 // we are passing floating pointer values in core registers.
555 return Location::QuickParameter(index, index);
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100556 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100557 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100558 }
559 }
560
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100561 case Primitive::kPrimVoid:
562 LOG(FATAL) << "Unexpected parameter type " << type;
563 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100564 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100565 return Location();
566}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100567
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100568void CodeGeneratorX86::Move32(Location destination, Location source) {
569 if (source.Equals(destination)) {
570 return;
571 }
572 if (destination.IsRegister()) {
573 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000574 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100575 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000576 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100577 } else {
578 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000579 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100580 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100581 } else if (destination.IsFpuRegister()) {
582 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000583 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100584 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000585 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100586 } else {
587 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000588 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100589 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100590 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000591 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100592 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000593 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100594 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000595 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100596 } else {
597 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100598 __ pushl(Address(ESP, source.GetStackIndex()));
599 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100600 }
601 }
602}
603
604void CodeGeneratorX86::Move64(Location destination, Location source) {
605 if (source.Equals(destination)) {
606 return;
607 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100608 if (destination.IsRegisterPair()) {
609 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000610 EmitParallelMoves(
611 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
612 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
613 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
614 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100615 } else if (source.IsFpuRegister()) {
616 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100617 } else if (source.IsQuickParameter()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000618 uint16_t register_index = source.GetQuickParameterRegisterIndex();
619 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100620 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000621 EmitParallelMoves(
622 Location::RegisterLocation(calling_convention.GetRegisterAt(register_index)),
623 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
624 Location::StackSlot(
625 calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize()),
626 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100627 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000628 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100629 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100630 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
631 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100632 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
633 }
634 } else if (destination.IsQuickParameter()) {
635 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000636 uint16_t register_index = destination.GetQuickParameterRegisterIndex();
637 uint16_t stack_index = destination.GetQuickParameterStackIndex();
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000638 if (source.IsRegisterPair()) {
639 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100640 } else if (source.IsFpuRegister()) {
641 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100642 } else {
643 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000644 EmitParallelMoves(
645 Location::StackSlot(source.GetStackIndex()),
646 Location::RegisterLocation(calling_convention.GetRegisterAt(register_index)),
647 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
648 Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index + 1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100649 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100650 } else if (destination.IsFpuRegister()) {
651 if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000652 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100653 } else {
654 LOG(FATAL) << "Unimplemented";
655 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100656 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000657 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100658 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000659 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100660 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100661 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100662 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100663 } else if (source.IsQuickParameter()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000664 // No conflict possible, so just do the move.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100665 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000666 uint16_t register_index = source.GetQuickParameterRegisterIndex();
667 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000668 // Just move the low part. The only time a source is a quick parameter is
669 // when moving the parameter to its stack locations. And the (Java) caller
670 // of this method has already done that.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100671 __ movl(Address(ESP, destination.GetStackIndex()),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000672 calling_convention.GetRegisterAt(register_index));
673 DCHECK_EQ(calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100674 static_cast<size_t>(destination.GetHighStackIndex(kX86WordSize)));
675 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000676 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100677 } else {
678 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000679 EmitParallelMoves(
680 Location::StackSlot(source.GetStackIndex()),
681 Location::StackSlot(destination.GetStackIndex()),
682 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
683 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100684 }
685 }
686}
687
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100688void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000689 LocationSummary* locations = instruction->GetLocations();
690 if (locations != nullptr && locations->Out().Equals(location)) {
691 return;
692 }
693
694 if (locations != nullptr && locations->Out().IsConstant()) {
695 HConstant* const_to_move = locations->Out().GetConstant();
696 if (const_to_move->IsIntConstant()) {
697 Immediate imm(const_to_move->AsIntConstant()->GetValue());
698 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000699 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000700 } else if (location.IsStackSlot()) {
701 __ movl(Address(ESP, location.GetStackIndex()), imm);
702 } else {
703 DCHECK(location.IsConstant());
704 DCHECK_EQ(location.GetConstant(), const_to_move);
705 }
706 } else if (const_to_move->IsLongConstant()) {
707 int64_t value = const_to_move->AsLongConstant()->GetValue();
708 if (location.IsRegisterPair()) {
709 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
710 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
711 } else if (location.IsDoubleStackSlot()) {
712 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000713 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
714 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000715 } else {
716 DCHECK(location.IsConstant());
717 DCHECK_EQ(location.GetConstant(), instruction);
718 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100719 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000720 } else if (instruction->IsTemporary()) {
721 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000722 if (temp_location.IsStackSlot()) {
723 Move32(location, temp_location);
724 } else {
725 DCHECK(temp_location.IsDoubleStackSlot());
726 Move64(location, temp_location);
727 }
Roland Levillain476df552014-10-09 17:51:36 +0100728 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100729 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100730 switch (instruction->GetType()) {
731 case Primitive::kPrimBoolean:
732 case Primitive::kPrimByte:
733 case Primitive::kPrimChar:
734 case Primitive::kPrimShort:
735 case Primitive::kPrimInt:
736 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100737 case Primitive::kPrimFloat:
738 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100739 break;
740
741 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100742 case Primitive::kPrimDouble:
743 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100744 break;
745
746 default:
747 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
748 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000749 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100750 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100751 switch (instruction->GetType()) {
752 case Primitive::kPrimBoolean:
753 case Primitive::kPrimByte:
754 case Primitive::kPrimChar:
755 case Primitive::kPrimShort:
756 case Primitive::kPrimInt:
757 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100758 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000759 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100760 break;
761
762 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100763 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000764 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100765 break;
766
767 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100768 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100769 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000770 }
771}
772
773void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000774 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000775}
776
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000777void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000778 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100779 DCHECK(!successor->IsExitBlock());
780
781 HBasicBlock* block = got->GetBlock();
782 HInstruction* previous = got->GetPrevious();
783
784 HLoopInformation* info = block->GetLoopInformation();
785 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
786 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
787 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
788 return;
789 }
790
791 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
792 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
793 }
794 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000795 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000796 }
797}
798
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000799void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000800 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000801}
802
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000803void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700804 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000805 if (kIsDebugBuild) {
806 __ Comment("Unreachable");
807 __ int3();
808 }
809}
810
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000811void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100812 LocationSummary* locations =
813 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100814 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100815 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100816 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100817 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000818}
819
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000820void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700821 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100822 if (cond->IsIntConstant()) {
823 // Constant condition, statically compared against 1.
824 int32_t cond_value = cond->AsIntConstant()->GetValue();
825 if (cond_value == 1) {
826 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
827 if_instr->IfTrueSuccessor())) {
828 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100829 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100830 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100831 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100832 DCHECK_EQ(cond_value, 0);
833 }
834 } else {
835 bool materialized =
836 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
837 // Moves do not affect the eflags register, so if the condition is
838 // evaluated just before the if, we don't need to evaluate it
839 // again.
840 bool eflags_set = cond->IsCondition()
841 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
842 if (materialized) {
843 if (!eflags_set) {
844 // Materialized condition, compare against 0.
845 Location lhs = if_instr->GetLocations()->InAt(0);
846 if (lhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000847 __ cmpl(lhs.AsRegister<Register>(), Immediate(0));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100848 } else {
849 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
850 }
851 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
852 } else {
853 __ j(X86Condition(cond->AsCondition()->GetCondition()),
854 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
855 }
856 } else {
857 Location lhs = cond->GetLocations()->InAt(0);
858 Location rhs = cond->GetLocations()->InAt(1);
859 // LHS is guaranteed to be in a register (see
860 // LocationsBuilderX86::VisitCondition).
861 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000862 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100863 } else if (rhs.IsConstant()) {
864 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
865 Immediate imm(instruction->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000866 __ cmpl(lhs.AsRegister<Register>(), imm);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100867 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000868 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100869 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100870 __ j(X86Condition(cond->AsCondition()->GetCondition()),
871 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700872 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100873 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100874 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
875 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700876 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000877 }
878}
879
880void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000881 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000882}
883
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000884void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
885 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000886}
887
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000888void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100889 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000890}
891
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000892void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100893 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700894 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000895}
896
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100897void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100898 LocationSummary* locations =
899 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100900 switch (store->InputAt(1)->GetType()) {
901 case Primitive::kPrimBoolean:
902 case Primitive::kPrimByte:
903 case Primitive::kPrimChar:
904 case Primitive::kPrimShort:
905 case Primitive::kPrimInt:
906 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100907 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100908 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
909 break;
910
911 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100912 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100913 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
914 break;
915
916 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100917 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100918 }
919 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000920}
921
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000922void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700923 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000924}
925
Dave Allison20dfc792014-06-16 20:44:29 -0700926void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100927 LocationSummary* locations =
928 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100929 locations->SetInAt(0, Location::RequiresRegister());
930 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100931 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100932 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100933 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000934}
935
Dave Allison20dfc792014-06-16 20:44:29 -0700936void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
937 if (comp->NeedsMaterialization()) {
938 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000939 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100940 // Clear register: setcc only sets the low byte.
941 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700942 if (locations->InAt(1).IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000943 __ cmpl(locations->InAt(0).AsRegister<Register>(),
944 locations->InAt(1).AsRegister<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100945 } else if (locations->InAt(1).IsConstant()) {
946 HConstant* instruction = locations->InAt(1).GetConstant();
947 Immediate imm(instruction->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000948 __ cmpl(locations->InAt(0).AsRegister<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700949 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000950 __ cmpl(locations->InAt(0).AsRegister<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700951 Address(ESP, locations->InAt(1).GetStackIndex()));
952 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100953 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100954 }
Dave Allison20dfc792014-06-16 20:44:29 -0700955}
956
957void LocationsBuilderX86::VisitEqual(HEqual* comp) {
958 VisitCondition(comp);
959}
960
961void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
962 VisitCondition(comp);
963}
964
965void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
966 VisitCondition(comp);
967}
968
969void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
970 VisitCondition(comp);
971}
972
973void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
974 VisitCondition(comp);
975}
976
977void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
978 VisitCondition(comp);
979}
980
981void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
982 VisitCondition(comp);
983}
984
985void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
986 VisitCondition(comp);
987}
988
989void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
990 VisitCondition(comp);
991}
992
993void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
994 VisitCondition(comp);
995}
996
997void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
998 VisitCondition(comp);
999}
1000
1001void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1002 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001003}
1004
1005void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001006 LocationSummary* locations =
1007 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001008 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001009}
1010
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001011void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001012 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001013 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001014}
1015
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001016void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001017 LocationSummary* locations =
1018 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001019 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001020}
1021
1022void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1023 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001024 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001025}
1026
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001027void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1028 LocationSummary* locations =
1029 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1030 locations->SetOut(Location::ConstantLocation(constant));
1031}
1032
1033void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1034 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001035 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001036}
1037
1038void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1039 LocationSummary* locations =
1040 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1041 locations->SetOut(Location::ConstantLocation(constant));
1042}
1043
1044void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1045 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001046 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001047}
1048
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001049void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001050 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001051}
1052
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001053void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001054 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001055 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001056 __ ret();
1057}
1058
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001059void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001060 LocationSummary* locations =
1061 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001062 switch (ret->InputAt(0)->GetType()) {
1063 case Primitive::kPrimBoolean:
1064 case Primitive::kPrimByte:
1065 case Primitive::kPrimChar:
1066 case Primitive::kPrimShort:
1067 case Primitive::kPrimInt:
1068 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001069 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001070 break;
1071
1072 case Primitive::kPrimLong:
1073 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001074 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001075 break;
1076
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001077 case Primitive::kPrimFloat:
1078 case Primitive::kPrimDouble:
1079 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001080 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001081 break;
1082
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001083 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001084 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001085 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001086}
1087
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001088void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001089 if (kIsDebugBuild) {
1090 switch (ret->InputAt(0)->GetType()) {
1091 case Primitive::kPrimBoolean:
1092 case Primitive::kPrimByte:
1093 case Primitive::kPrimChar:
1094 case Primitive::kPrimShort:
1095 case Primitive::kPrimInt:
1096 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001097 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001098 break;
1099
1100 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001101 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1102 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001103 break;
1104
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001105 case Primitive::kPrimFloat:
1106 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001107 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001108 break;
1109
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001110 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001111 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001112 }
1113 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001114 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001115 __ ret();
1116}
1117
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001118void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001119 HandleInvoke(invoke);
1120}
1121
1122void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001123 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001124
1125 // TODO: Implement all kinds of calls:
1126 // 1) boot -> boot
1127 // 2) app -> boot
1128 // 3) app -> app
1129 //
1130 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1131
1132 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001133 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001134 // temp = temp->dex_cache_resolved_methods_;
1135 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
1136 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001137 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001138 // (temp + offset_of_quick_compiled_code)()
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001139 __ call(Address(
1140 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001141
1142 DCHECK(!codegen_->IsLeafMethod());
1143 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1144}
1145
1146void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1147 HandleInvoke(invoke);
1148}
1149
1150void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001151 LocationSummary* locations =
1152 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001153 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001154
1155 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001156 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001157 HInstruction* input = invoke->InputAt(i);
1158 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1159 }
1160
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001161 switch (invoke->GetType()) {
1162 case Primitive::kPrimBoolean:
1163 case Primitive::kPrimByte:
1164 case Primitive::kPrimChar:
1165 case Primitive::kPrimShort:
1166 case Primitive::kPrimInt:
1167 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001168 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001169 break;
1170
1171 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001172 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001173 break;
1174
1175 case Primitive::kPrimVoid:
1176 break;
1177
1178 case Primitive::kPrimDouble:
1179 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001180 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001181 break;
1182 }
1183
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001184 invoke->SetLocations(locations);
1185}
1186
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001187void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001188 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001189 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1190 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1191 LocationSummary* locations = invoke->GetLocations();
1192 Location receiver = locations->InAt(0);
1193 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1194 // temp = object->GetClass();
1195 if (receiver.IsStackSlot()) {
1196 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1197 __ movl(temp, Address(temp, class_offset));
1198 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001199 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001200 }
1201 // temp = temp->GetMethodAt(method_offset);
1202 __ movl(temp, Address(temp, method_offset));
1203 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001204 __ call(Address(
1205 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001206
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001207 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001208 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001209}
1210
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001211void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1212 HandleInvoke(invoke);
1213 // Add the hidden argument.
1214 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM0));
1215}
1216
1217void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1218 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001219 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001220 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1221 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1222 LocationSummary* locations = invoke->GetLocations();
1223 Location receiver = locations->InAt(0);
1224 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1225
1226 // Set the hidden argument.
1227 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001228 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001229
1230 // temp = object->GetClass();
1231 if (receiver.IsStackSlot()) {
1232 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1233 __ movl(temp, Address(temp, class_offset));
1234 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001235 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001236 }
1237 // temp = temp->GetImtEntryAt(method_offset);
1238 __ movl(temp, Address(temp, method_offset));
1239 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001240 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001241 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001242
1243 DCHECK(!codegen_->IsLeafMethod());
1244 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1245}
1246
Roland Levillain88cb1752014-10-20 16:36:47 +01001247void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1248 LocationSummary* locations =
1249 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1250 switch (neg->GetResultType()) {
1251 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001252 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001253 locations->SetInAt(0, Location::RequiresRegister());
1254 locations->SetOut(Location::SameAsFirstInput());
1255 break;
1256
Roland Levillain88cb1752014-10-20 16:36:47 +01001257 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001258 locations->SetInAt(0, Location::RequiresFpuRegister());
1259 locations->SetOut(Location::SameAsFirstInput());
1260 locations->AddTemp(Location::RequiresRegister());
1261 locations->AddTemp(Location::RequiresFpuRegister());
1262 break;
1263
Roland Levillain88cb1752014-10-20 16:36:47 +01001264 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001265 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001266 locations->SetOut(Location::SameAsFirstInput());
1267 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001268 break;
1269
1270 default:
1271 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1272 }
1273}
1274
1275void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1276 LocationSummary* locations = neg->GetLocations();
1277 Location out = locations->Out();
1278 Location in = locations->InAt(0);
1279 switch (neg->GetResultType()) {
1280 case Primitive::kPrimInt:
1281 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001282 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001283 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001284 break;
1285
1286 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001287 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001288 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001289 __ negl(out.AsRegisterPairLow<Register>());
1290 // Negation is similar to subtraction from zero. The least
1291 // significant byte triggers a borrow when it is different from
1292 // zero; to take it into account, add 1 to the most significant
1293 // byte if the carry flag (CF) is set to 1 after the first NEGL
1294 // operation.
1295 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1296 __ negl(out.AsRegisterPairHigh<Register>());
1297 break;
1298
Roland Levillain5368c212014-11-27 15:03:41 +00001299 case Primitive::kPrimFloat: {
1300 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001301 Register constant = locations->GetTemp(0).AsRegister<Register>();
1302 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001303 // Implement float negation with an exclusive or with value
1304 // 0x80000000 (mask for bit 31, representing the sign of a
1305 // single-precision floating-point number).
1306 __ movl(constant, Immediate(INT32_C(0x80000000)));
1307 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001308 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001309 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001310 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001311
Roland Levillain5368c212014-11-27 15:03:41 +00001312 case Primitive::kPrimDouble: {
1313 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001314 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001315 // Implement double negation with an exclusive or with value
1316 // 0x8000000000000000 (mask for bit 63, representing the sign of
1317 // a double-precision floating-point number).
1318 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001319 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001320 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001321 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001322
1323 default:
1324 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1325 }
1326}
1327
Roland Levillaindff1f282014-11-05 14:15:05 +00001328void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001329 Primitive::Type result_type = conversion->GetResultType();
1330 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001331 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001332
1333 // Float-to-long conversions invoke the runtime.
1334 LocationSummary::CallKind call_kind =
1335 (input_type == Primitive::kPrimFloat && result_type == Primitive::kPrimLong)
1336 ? LocationSummary::kCall
1337 : LocationSummary::kNoCall;
1338 LocationSummary* locations =
1339 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1340
Roland Levillaindff1f282014-11-05 14:15:05 +00001341 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001342 case Primitive::kPrimByte:
1343 switch (input_type) {
1344 case Primitive::kPrimShort:
1345 case Primitive::kPrimInt:
1346 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001347 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001348 locations->SetInAt(0, Location::Any());
1349 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1350 break;
1351
1352 default:
1353 LOG(FATAL) << "Unexpected type conversion from " << input_type
1354 << " to " << result_type;
1355 }
1356 break;
1357
Roland Levillain01a8d712014-11-14 16:27:39 +00001358 case Primitive::kPrimShort:
1359 switch (input_type) {
1360 case Primitive::kPrimByte:
1361 case Primitive::kPrimInt:
1362 case Primitive::kPrimChar:
1363 // Processing a Dex `int-to-short' instruction.
1364 locations->SetInAt(0, Location::Any());
1365 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1366 break;
1367
1368 default:
1369 LOG(FATAL) << "Unexpected type conversion from " << input_type
1370 << " to " << result_type;
1371 }
1372 break;
1373
Roland Levillain946e1432014-11-11 17:35:19 +00001374 case Primitive::kPrimInt:
1375 switch (input_type) {
1376 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001377 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001378 locations->SetInAt(0, Location::Any());
1379 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1380 break;
1381
1382 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001383 // Processing a Dex `float-to-int' instruction.
1384 locations->SetInAt(0, Location::RequiresFpuRegister());
1385 locations->SetOut(Location::RequiresRegister());
1386 locations->AddTemp(Location::RequiresFpuRegister());
1387 break;
1388
Roland Levillain946e1432014-11-11 17:35:19 +00001389 case Primitive::kPrimDouble:
1390 LOG(FATAL) << "Type conversion from " << input_type
1391 << " to " << result_type << " not yet implemented";
1392 break;
1393
1394 default:
1395 LOG(FATAL) << "Unexpected type conversion from " << input_type
1396 << " to " << result_type;
1397 }
1398 break;
1399
Roland Levillaindff1f282014-11-05 14:15:05 +00001400 case Primitive::kPrimLong:
1401 switch (input_type) {
1402 case Primitive::kPrimByte:
1403 case Primitive::kPrimShort:
1404 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001405 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001406 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001407 locations->SetInAt(0, Location::RegisterLocation(EAX));
1408 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1409 break;
1410
Roland Levillain624279f2014-12-04 11:54:28 +00001411 case Primitive::kPrimFloat: {
1412 // Processing a Dex `float-to-long' instruction.
1413 InvokeRuntimeCallingConvention calling_convention;
1414 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1415 // The runtime helper puts the result in EAX, EDX.
1416 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1417 break;
1418 }
1419
Roland Levillaindff1f282014-11-05 14:15:05 +00001420 case Primitive::kPrimDouble:
1421 LOG(FATAL) << "Type conversion from " << input_type << " to "
1422 << result_type << " not yet implemented";
1423 break;
1424
1425 default:
1426 LOG(FATAL) << "Unexpected type conversion from " << input_type
1427 << " to " << result_type;
1428 }
1429 break;
1430
Roland Levillain981e4542014-11-14 11:47:14 +00001431 case Primitive::kPrimChar:
1432 switch (input_type) {
1433 case Primitive::kPrimByte:
1434 case Primitive::kPrimShort:
1435 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001436 // Processing a Dex `int-to-char' instruction.
1437 locations->SetInAt(0, Location::Any());
1438 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1439 break;
1440
1441 default:
1442 LOG(FATAL) << "Unexpected type conversion from " << input_type
1443 << " to " << result_type;
1444 }
1445 break;
1446
Roland Levillaindff1f282014-11-05 14:15:05 +00001447 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001448 switch (input_type) {
1449 case Primitive::kPrimByte:
1450 case Primitive::kPrimShort:
1451 case Primitive::kPrimInt:
1452 case Primitive::kPrimChar:
1453 // Processing a Dex `int-to-float' instruction.
1454 locations->SetInAt(0, Location::RequiresRegister());
1455 locations->SetOut(Location::RequiresFpuRegister());
1456 break;
1457
1458 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001459 // Processing a Dex `long-to-float' instruction.
1460 locations->SetInAt(0, Location::RequiresRegister());
1461 locations->SetOut(Location::RequiresFpuRegister());
1462 locations->AddTemp(Location::RequiresFpuRegister());
1463 locations->AddTemp(Location::RequiresFpuRegister());
1464 break;
1465
Roland Levillaincff13742014-11-17 14:32:17 +00001466 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001467 // Processing a Dex `double-to-float' instruction.
1468 locations->SetInAt(0, Location::RequiresFpuRegister());
1469 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001470 break;
1471
1472 default:
1473 LOG(FATAL) << "Unexpected type conversion from " << input_type
1474 << " to " << result_type;
1475 };
1476 break;
1477
Roland Levillaindff1f282014-11-05 14:15:05 +00001478 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001479 switch (input_type) {
1480 case Primitive::kPrimByte:
1481 case Primitive::kPrimShort:
1482 case Primitive::kPrimInt:
1483 case Primitive::kPrimChar:
1484 // Processing a Dex `int-to-double' instruction.
1485 locations->SetInAt(0, Location::RequiresRegister());
1486 locations->SetOut(Location::RequiresFpuRegister());
1487 break;
1488
1489 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001490 // Processing a Dex `long-to-double' instruction.
1491 locations->SetInAt(0, Location::RequiresRegister());
1492 locations->SetOut(Location::RequiresFpuRegister());
1493 locations->AddTemp(Location::RequiresFpuRegister());
1494 locations->AddTemp(Location::RequiresFpuRegister());
1495 break;
1496
Roland Levillaincff13742014-11-17 14:32:17 +00001497 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001498 // Processing a Dex `float-to-double' instruction.
1499 locations->SetInAt(0, Location::RequiresFpuRegister());
1500 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001501 break;
1502
1503 default:
1504 LOG(FATAL) << "Unexpected type conversion from " << input_type
1505 << " to " << result_type;
1506 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001507 break;
1508
1509 default:
1510 LOG(FATAL) << "Unexpected type conversion from " << input_type
1511 << " to " << result_type;
1512 }
1513}
1514
1515void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1516 LocationSummary* locations = conversion->GetLocations();
1517 Location out = locations->Out();
1518 Location in = locations->InAt(0);
1519 Primitive::Type result_type = conversion->GetResultType();
1520 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001521 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001522 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001523 case Primitive::kPrimByte:
1524 switch (input_type) {
1525 case Primitive::kPrimShort:
1526 case Primitive::kPrimInt:
1527 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001528 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001529 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001530 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001531 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001532 __ movsxb(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain51d3fc42014-11-13 14:11:42 +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<int8_t>(value)));
Roland Levillain51d3fc42014-11-13 14:11:42 +00001537 }
1538 break;
1539
1540 default:
1541 LOG(FATAL) << "Unexpected type conversion from " << input_type
1542 << " to " << result_type;
1543 }
1544 break;
1545
Roland Levillain01a8d712014-11-14 16:27:39 +00001546 case Primitive::kPrimShort:
1547 switch (input_type) {
1548 case Primitive::kPrimByte:
1549 case Primitive::kPrimInt:
1550 case Primitive::kPrimChar:
1551 // Processing a Dex `int-to-short' instruction.
1552 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001553 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001554 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001555 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001556 } else {
1557 DCHECK(in.GetConstant()->IsIntConstant());
1558 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001559 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001560 }
1561 break;
1562
1563 default:
1564 LOG(FATAL) << "Unexpected type conversion from " << input_type
1565 << " to " << result_type;
1566 }
1567 break;
1568
Roland Levillain946e1432014-11-11 17:35:19 +00001569 case Primitive::kPrimInt:
1570 switch (input_type) {
1571 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001572 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001573 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001574 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001575 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001576 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001577 } else {
1578 DCHECK(in.IsConstant());
1579 DCHECK(in.GetConstant()->IsLongConstant());
1580 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001581 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001582 }
1583 break;
1584
Roland Levillain3f8f9362014-12-02 17:45:01 +00001585 case Primitive::kPrimFloat: {
1586 // Processing a Dex `float-to-int' instruction.
1587 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1588 Register output = out.AsRegister<Register>();
1589 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1590 Label done, nan;
1591
1592 __ movl(output, Immediate(kPrimIntMax));
1593 // temp = int-to-float(output)
1594 __ cvtsi2ss(temp, output);
1595 // if input >= temp goto done
1596 __ comiss(input, temp);
1597 __ j(kAboveEqual, &done);
1598 // if input == NaN goto nan
1599 __ j(kUnordered, &nan);
1600 // output = float-to-int-truncate(input)
1601 __ cvttss2si(output, input);
1602 __ jmp(&done);
1603 __ Bind(&nan);
1604 // output = 0
1605 __ xorl(output, output);
1606 __ Bind(&done);
1607 break;
1608 }
1609
Roland Levillain946e1432014-11-11 17:35:19 +00001610 case Primitive::kPrimDouble:
1611 LOG(FATAL) << "Type conversion from " << input_type
1612 << " to " << result_type << " not yet implemented";
1613 break;
1614
1615 default:
1616 LOG(FATAL) << "Unexpected type conversion from " << input_type
1617 << " to " << result_type;
1618 }
1619 break;
1620
Roland Levillaindff1f282014-11-05 14:15:05 +00001621 case Primitive::kPrimLong:
1622 switch (input_type) {
1623 case Primitive::kPrimByte:
1624 case Primitive::kPrimShort:
1625 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001626 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001627 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001628 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1629 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001630 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001631 __ cdq();
1632 break;
1633
1634 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001635 // Processing a Dex `float-to-long' instruction.
1636 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
1637 // This call does not actually record PC information.
1638 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1639 break;
1640
Roland Levillaindff1f282014-11-05 14:15:05 +00001641 case Primitive::kPrimDouble:
1642 LOG(FATAL) << "Type conversion from " << input_type << " to "
1643 << result_type << " not yet implemented";
1644 break;
1645
1646 default:
1647 LOG(FATAL) << "Unexpected type conversion from " << input_type
1648 << " to " << result_type;
1649 }
1650 break;
1651
Roland Levillain981e4542014-11-14 11:47:14 +00001652 case Primitive::kPrimChar:
1653 switch (input_type) {
1654 case Primitive::kPrimByte:
1655 case Primitive::kPrimShort:
1656 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001657 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1658 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001659 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001660 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001661 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001662 } else {
1663 DCHECK(in.GetConstant()->IsIntConstant());
1664 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001665 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001666 }
1667 break;
1668
1669 default:
1670 LOG(FATAL) << "Unexpected type conversion from " << input_type
1671 << " to " << result_type;
1672 }
1673 break;
1674
Roland Levillaindff1f282014-11-05 14:15:05 +00001675 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001676 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001677 case Primitive::kPrimByte:
1678 case Primitive::kPrimShort:
1679 case Primitive::kPrimInt:
1680 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001681 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001682 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001683 break;
1684
Roland Levillain6d0e4832014-11-27 18:31:21 +00001685 case Primitive::kPrimLong: {
1686 // Processing a Dex `long-to-float' instruction.
1687 Register low = in.AsRegisterPairLow<Register>();
1688 Register high = in.AsRegisterPairHigh<Register>();
1689 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1690 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1691 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1692
1693 // Operations use doubles for precision reasons (each 32-bit
1694 // half of a long fits in the 53-bit mantissa of a double,
1695 // but not in the 24-bit mantissa of a float). This is
1696 // especially important for the low bits. The result is
1697 // eventually converted to float.
1698
1699 // low = low - 2^31 (to prevent bit 31 of `low` to be
1700 // interpreted as a sign bit)
1701 __ subl(low, Immediate(0x80000000));
1702 // temp = int-to-double(high)
1703 __ cvtsi2sd(temp, high);
1704 // temp = temp * 2^32
1705 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
1706 __ mulsd(temp, constant);
1707 // result = int-to-double(low)
1708 __ cvtsi2sd(result, low);
1709 // result = result + 2^31 (restore the original value of `low`)
1710 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
1711 __ addsd(result, constant);
1712 // result = result + temp
1713 __ addsd(result, temp);
1714 // result = double-to-float(result)
1715 __ cvtsd2ss(result, result);
1716 break;
1717 }
1718
Roland Levillaincff13742014-11-17 14:32:17 +00001719 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001720 // Processing a Dex `double-to-float' instruction.
1721 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001722 break;
1723
1724 default:
1725 LOG(FATAL) << "Unexpected type conversion from " << input_type
1726 << " to " << result_type;
1727 };
1728 break;
1729
Roland Levillaindff1f282014-11-05 14:15:05 +00001730 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001731 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001732 case Primitive::kPrimByte:
1733 case Primitive::kPrimShort:
1734 case Primitive::kPrimInt:
1735 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001736 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001737 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001738 break;
1739
Roland Levillain647b9ed2014-11-27 12:06:00 +00001740 case Primitive::kPrimLong: {
1741 // Processing a Dex `long-to-double' instruction.
1742 Register low = in.AsRegisterPairLow<Register>();
1743 Register high = in.AsRegisterPairHigh<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001744 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1745 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1746 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001747
Roland Levillain647b9ed2014-11-27 12:06:00 +00001748 // low = low - 2^31 (to prevent bit 31 of `low` to be
1749 // interpreted as a sign bit)
1750 __ subl(low, Immediate(0x80000000));
1751 // temp = int-to-double(high)
1752 __ cvtsi2sd(temp, high);
1753 // temp = temp * 2^32
Roland Levillain6d0e4832014-11-27 18:31:21 +00001754 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001755 __ mulsd(temp, constant);
1756 // result = int-to-double(low)
1757 __ cvtsi2sd(result, low);
1758 // result = result + 2^31 (restore the original value of `low`)
Roland Levillain6d0e4832014-11-27 18:31:21 +00001759 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001760 __ addsd(result, constant);
1761 // result = result + temp
1762 __ addsd(result, temp);
1763 break;
1764 }
1765
Roland Levillaincff13742014-11-17 14:32:17 +00001766 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001767 // Processing a Dex `float-to-double' instruction.
1768 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001769 break;
1770
1771 default:
1772 LOG(FATAL) << "Unexpected type conversion from " << input_type
1773 << " to " << result_type;
1774 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001775 break;
1776
1777 default:
1778 LOG(FATAL) << "Unexpected type conversion from " << input_type
1779 << " to " << result_type;
1780 }
1781}
1782
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001783void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001784 LocationSummary* locations =
1785 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001786 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001787 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001788 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001789 locations->SetInAt(0, Location::RequiresRegister());
1790 locations->SetInAt(1, Location::Any());
1791 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001792 break;
1793 }
1794
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001795 case Primitive::kPrimFloat:
1796 case Primitive::kPrimDouble: {
1797 locations->SetInAt(0, Location::RequiresFpuRegister());
1798 locations->SetInAt(1, Location::Any());
1799 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001800 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001801 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001802
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001803 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001804 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1805 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001806 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001807}
1808
1809void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1810 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001811 Location first = locations->InAt(0);
1812 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001813 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001814 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001815 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001816 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001817 __ addl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001818 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001819 __ addl(first.AsRegister<Register>(),
1820 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001821 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001822 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001823 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001824 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001825 }
1826
1827 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001828 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001829 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1830 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001831 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001832 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1833 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001834 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001835 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001836 break;
1837 }
1838
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001839 case Primitive::kPrimFloat: {
1840 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001841 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001842 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001843 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001844 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001845 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001846 }
1847
1848 case Primitive::kPrimDouble: {
1849 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001850 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001851 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001852 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001853 }
1854 break;
1855 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001856
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001857 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001858 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001859 }
1860}
1861
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001862void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001863 LocationSummary* locations =
1864 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001865 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001866 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001867 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001868 locations->SetInAt(0, Location::RequiresRegister());
1869 locations->SetInAt(1, Location::Any());
1870 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001871 break;
1872 }
Calin Juravle11351682014-10-23 15:38:15 +01001873 case Primitive::kPrimFloat:
1874 case Primitive::kPrimDouble: {
1875 locations->SetInAt(0, Location::RequiresFpuRegister());
1876 locations->SetInAt(1, Location::RequiresFpuRegister());
1877 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001878 break;
Calin Juravle11351682014-10-23 15:38:15 +01001879 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001880
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001881 default:
Calin Juravle11351682014-10-23 15:38:15 +01001882 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001883 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001884}
1885
1886void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1887 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001888 Location first = locations->InAt(0);
1889 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001890 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001891 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001892 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001893 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001894 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001895 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001896 __ subl(first.AsRegister<Register>(),
1897 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001898 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001899 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001900 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001901 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001902 }
1903
1904 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001905 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01001906 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1907 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001908 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001909 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001910 __ sbbl(first.AsRegisterPairHigh<Register>(),
1911 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001912 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001913 break;
1914 }
1915
Calin Juravle11351682014-10-23 15:38:15 +01001916 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001917 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001918 break;
Calin Juravle11351682014-10-23 15:38:15 +01001919 }
1920
1921 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001922 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001923 break;
1924 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001925
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001926 default:
Calin Juravle11351682014-10-23 15:38:15 +01001927 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001928 }
1929}
1930
Calin Juravle34bacdf2014-10-07 20:23:36 +01001931void LocationsBuilderX86::VisitMul(HMul* mul) {
1932 LocationSummary* locations =
1933 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1934 switch (mul->GetResultType()) {
1935 case Primitive::kPrimInt:
1936 locations->SetInAt(0, Location::RequiresRegister());
1937 locations->SetInAt(1, Location::Any());
1938 locations->SetOut(Location::SameAsFirstInput());
1939 break;
1940 case Primitive::kPrimLong: {
1941 locations->SetInAt(0, Location::RequiresRegister());
1942 // TODO: Currently this handles only stack operands:
1943 // - we don't have enough registers because we currently use Quick ABI.
1944 // - by the time we have a working register allocator we will probably change the ABI
1945 // and fix the above.
1946 // - we don't have a way yet to request operands on stack but the base line compiler
1947 // will leave the operands on the stack with Any().
1948 locations->SetInAt(1, Location::Any());
1949 locations->SetOut(Location::SameAsFirstInput());
1950 // Needed for imul on 32bits with 64bits output.
1951 locations->AddTemp(Location::RegisterLocation(EAX));
1952 locations->AddTemp(Location::RegisterLocation(EDX));
1953 break;
1954 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001955 case Primitive::kPrimFloat:
1956 case Primitive::kPrimDouble: {
1957 locations->SetInAt(0, Location::RequiresFpuRegister());
1958 locations->SetInAt(1, Location::RequiresFpuRegister());
1959 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001960 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001961 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001962
1963 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001964 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001965 }
1966}
1967
1968void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1969 LocationSummary* locations = mul->GetLocations();
1970 Location first = locations->InAt(0);
1971 Location second = locations->InAt(1);
1972 DCHECK(first.Equals(locations->Out()));
1973
1974 switch (mul->GetResultType()) {
1975 case Primitive::kPrimInt: {
1976 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001977 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001978 } else if (second.IsConstant()) {
1979 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001980 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001981 } else {
1982 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001983 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01001984 }
1985 break;
1986 }
1987
1988 case Primitive::kPrimLong: {
1989 DCHECK(second.IsDoubleStackSlot());
1990
1991 Register in1_hi = first.AsRegisterPairHigh<Register>();
1992 Register in1_lo = first.AsRegisterPairLow<Register>();
1993 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
1994 Address in2_lo(ESP, second.GetStackIndex());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001995 Register eax = locations->GetTemp(0).AsRegister<Register>();
1996 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001997
1998 DCHECK_EQ(EAX, eax);
1999 DCHECK_EQ(EDX, edx);
2000
2001 // input: in1 - 64 bits, in2 - 64 bits
2002 // output: in1
2003 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2004 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2005 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
2006
2007 __ movl(eax, in2_hi);
2008 // eax <- in1.lo * in2.hi
2009 __ imull(eax, in1_lo);
2010 // in1.hi <- in1.hi * in2.lo
2011 __ imull(in1_hi, in2_lo);
2012 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2013 __ addl(in1_hi, eax);
2014 // move in1_lo to eax to prepare for double precision
2015 __ movl(eax, in1_lo);
2016 // edx:eax <- in1.lo * in2.lo
2017 __ mull(in2_lo);
2018 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2019 __ addl(in1_hi, edx);
2020 // in1.lo <- (in1.lo * in2.lo)[31:0];
2021 __ movl(in1_lo, eax);
2022
2023 break;
2024 }
2025
Calin Juravleb5bfa962014-10-21 18:02:24 +01002026 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002027 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002028 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002029 }
2030
2031 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002032 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002033 break;
2034 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002035
2036 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002037 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002038 }
2039}
2040
Calin Juravlebacfec32014-11-14 15:54:36 +00002041void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2042 DCHECK(instruction->IsDiv() || instruction->IsRem());
2043
2044 LocationSummary* locations = instruction->GetLocations();
2045 Location out = locations->Out();
2046 Location first = locations->InAt(0);
2047 Location second = locations->InAt(1);
2048 bool is_div = instruction->IsDiv();
2049
2050 switch (instruction->GetResultType()) {
2051 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002052 Register second_reg = second.AsRegister<Register>();
2053 DCHECK_EQ(EAX, first.AsRegister<Register>());
2054 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002055
2056 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002057 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
2058 is_div);
Calin Juravlebacfec32014-11-14 15:54:36 +00002059 codegen_->AddSlowPath(slow_path);
2060
2061 // 0x80000000/-1 triggers an arithmetic exception!
2062 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2063 // it's safe to just use negl instead of more complex comparisons.
2064
2065 __ cmpl(second_reg, Immediate(-1));
2066 __ j(kEqual, slow_path->GetEntryLabel());
2067
2068 // edx:eax <- sign-extended of eax
2069 __ cdq();
2070 // eax = quotient, edx = remainder
2071 __ idivl(second_reg);
2072
2073 __ Bind(slow_path->GetExitLabel());
2074 break;
2075 }
2076
2077 case Primitive::kPrimLong: {
2078 InvokeRuntimeCallingConvention calling_convention;
2079 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2080 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2081 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2082 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2083 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2084 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2085
2086 if (is_div) {
2087 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2088 } else {
2089 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2090 }
2091 uint32_t dex_pc = is_div
2092 ? instruction->AsDiv()->GetDexPc()
2093 : instruction->AsRem()->GetDexPc();
2094 codegen_->RecordPcInfo(instruction, dex_pc);
2095
2096 break;
2097 }
2098
2099 default:
2100 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2101 }
2102}
2103
Calin Juravle7c4954d2014-10-28 16:57:40 +00002104void LocationsBuilderX86::VisitDiv(HDiv* div) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002105 LocationSummary::CallKind call_kind = div->GetResultType() == Primitive::kPrimLong
2106 ? LocationSummary::kCall
2107 : LocationSummary::kNoCall;
2108 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2109
Calin Juravle7c4954d2014-10-28 16:57:40 +00002110 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002111 case Primitive::kPrimInt: {
2112 locations->SetInAt(0, Location::RegisterLocation(EAX));
2113 locations->SetInAt(1, Location::RequiresRegister());
2114 locations->SetOut(Location::SameAsFirstInput());
2115 // Intel uses edx:eax as the dividend.
2116 locations->AddTemp(Location::RegisterLocation(EDX));
2117 break;
2118 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002119 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002120 InvokeRuntimeCallingConvention calling_convention;
2121 locations->SetInAt(0, Location::RegisterPairLocation(
2122 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2123 locations->SetInAt(1, Location::RegisterPairLocation(
2124 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2125 // Runtime helper puts the result in EAX, EDX.
2126 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002127 break;
2128 }
2129 case Primitive::kPrimFloat:
2130 case Primitive::kPrimDouble: {
2131 locations->SetInAt(0, Location::RequiresFpuRegister());
2132 locations->SetInAt(1, Location::RequiresFpuRegister());
2133 locations->SetOut(Location::SameAsFirstInput());
2134 break;
2135 }
2136
2137 default:
2138 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2139 }
2140}
2141
2142void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2143 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002144 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002145 Location first = locations->InAt(0);
2146 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002147
2148 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002149 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002150 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002151 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002152 break;
2153 }
2154
2155 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002156 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002157 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002158 break;
2159 }
2160
2161 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002162 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002163 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002164 break;
2165 }
2166
2167 default:
2168 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2169 }
2170}
2171
Calin Juravlebacfec32014-11-14 15:54:36 +00002172void LocationsBuilderX86::VisitRem(HRem* rem) {
2173 LocationSummary::CallKind call_kind = rem->GetResultType() == Primitive::kPrimLong
2174 ? LocationSummary::kCall
2175 : LocationSummary::kNoCall;
2176 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2177
2178 switch (rem->GetResultType()) {
2179 case Primitive::kPrimInt: {
2180 locations->SetInAt(0, Location::RegisterLocation(EAX));
2181 locations->SetInAt(1, Location::RequiresRegister());
2182 locations->SetOut(Location::RegisterLocation(EDX));
2183 break;
2184 }
2185 case Primitive::kPrimLong: {
2186 InvokeRuntimeCallingConvention calling_convention;
2187 locations->SetInAt(0, Location::RegisterPairLocation(
2188 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2189 locations->SetInAt(1, Location::RegisterPairLocation(
2190 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2191 // Runtime helper puts the result in EAX, EDX.
2192 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2193 break;
2194 }
2195 case Primitive::kPrimFloat:
2196 case Primitive::kPrimDouble: {
2197 LOG(FATAL) << "Unimplemented rem type " << rem->GetResultType();
2198 break;
2199 }
2200
2201 default:
2202 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2203 }
2204}
2205
2206void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2207 Primitive::Type type = rem->GetResultType();
2208 switch (type) {
2209 case Primitive::kPrimInt:
2210 case Primitive::kPrimLong: {
2211 GenerateDivRemIntegral(rem);
2212 break;
2213 }
2214 case Primitive::kPrimFloat:
2215 case Primitive::kPrimDouble: {
2216 LOG(FATAL) << "Unimplemented rem type " << type;
2217 break;
2218 }
2219 default:
2220 LOG(FATAL) << "Unexpected rem type " << type;
2221 }
2222}
2223
Calin Juravled0d48522014-11-04 16:40:20 +00002224void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2225 LocationSummary* locations =
2226 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002227 switch (instruction->GetType()) {
2228 case Primitive::kPrimInt: {
2229 locations->SetInAt(0, Location::Any());
2230 break;
2231 }
2232 case Primitive::kPrimLong: {
2233 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2234 if (!instruction->IsConstant()) {
2235 locations->AddTemp(Location::RequiresRegister());
2236 }
2237 break;
2238 }
2239 default:
2240 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2241 }
Calin Juravled0d48522014-11-04 16:40:20 +00002242 if (instruction->HasUses()) {
2243 locations->SetOut(Location::SameAsFirstInput());
2244 }
2245}
2246
2247void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2248 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2249 codegen_->AddSlowPath(slow_path);
2250
2251 LocationSummary* locations = instruction->GetLocations();
2252 Location value = locations->InAt(0);
2253
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002254 switch (instruction->GetType()) {
2255 case Primitive::kPrimInt: {
2256 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002257 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002258 __ j(kEqual, slow_path->GetEntryLabel());
2259 } else if (value.IsStackSlot()) {
2260 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2261 __ j(kEqual, slow_path->GetEntryLabel());
2262 } else {
2263 DCHECK(value.IsConstant()) << value;
2264 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2265 __ jmp(slow_path->GetEntryLabel());
2266 }
2267 }
2268 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002269 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002270 case Primitive::kPrimLong: {
2271 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002272 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002273 __ movl(temp, value.AsRegisterPairLow<Register>());
2274 __ orl(temp, value.AsRegisterPairHigh<Register>());
2275 __ j(kEqual, slow_path->GetEntryLabel());
2276 } else {
2277 DCHECK(value.IsConstant()) << value;
2278 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2279 __ jmp(slow_path->GetEntryLabel());
2280 }
2281 }
2282 break;
2283 }
2284 default:
2285 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002286 }
Calin Juravled0d48522014-11-04 16:40:20 +00002287}
2288
Calin Juravle9aec02f2014-11-18 23:06:35 +00002289void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2290 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2291
2292 LocationSummary* locations =
2293 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2294
2295 switch (op->GetResultType()) {
2296 case Primitive::kPrimInt: {
2297 locations->SetInAt(0, Location::RequiresRegister());
2298 // The shift count needs to be in CL.
2299 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2300 locations->SetOut(Location::SameAsFirstInput());
2301 break;
2302 }
2303 case Primitive::kPrimLong: {
2304 locations->SetInAt(0, Location::RequiresRegister());
2305 // The shift count needs to be in CL.
2306 locations->SetInAt(1, Location::RegisterLocation(ECX));
2307 locations->SetOut(Location::SameAsFirstInput());
2308 break;
2309 }
2310 default:
2311 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2312 }
2313}
2314
2315void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2316 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2317
2318 LocationSummary* locations = op->GetLocations();
2319 Location first = locations->InAt(0);
2320 Location second = locations->InAt(1);
2321 DCHECK(first.Equals(locations->Out()));
2322
2323 switch (op->GetResultType()) {
2324 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002325 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002326 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002327 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002328 DCHECK_EQ(ECX, second_reg);
2329 if (op->IsShl()) {
2330 __ shll(first_reg, second_reg);
2331 } else if (op->IsShr()) {
2332 __ sarl(first_reg, second_reg);
2333 } else {
2334 __ shrl(first_reg, second_reg);
2335 }
2336 } else {
2337 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
2338 if (op->IsShl()) {
2339 __ shll(first_reg, imm);
2340 } else if (op->IsShr()) {
2341 __ sarl(first_reg, imm);
2342 } else {
2343 __ shrl(first_reg, imm);
2344 }
2345 }
2346 break;
2347 }
2348 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002349 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002350 DCHECK_EQ(ECX, second_reg);
2351 if (op->IsShl()) {
2352 GenerateShlLong(first, second_reg);
2353 } else if (op->IsShr()) {
2354 GenerateShrLong(first, second_reg);
2355 } else {
2356 GenerateUShrLong(first, second_reg);
2357 }
2358 break;
2359 }
2360 default:
2361 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2362 }
2363}
2364
2365void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2366 Label done;
2367 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2368 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2369 __ testl(shifter, Immediate(32));
2370 __ j(kEqual, &done);
2371 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2372 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2373 __ Bind(&done);
2374}
2375
2376void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2377 Label done;
2378 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2379 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2380 __ testl(shifter, Immediate(32));
2381 __ j(kEqual, &done);
2382 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2383 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2384 __ Bind(&done);
2385}
2386
2387void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2388 Label done;
2389 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2390 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2391 __ testl(shifter, Immediate(32));
2392 __ j(kEqual, &done);
2393 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2394 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2395 __ Bind(&done);
2396}
2397
2398void LocationsBuilderX86::VisitShl(HShl* shl) {
2399 HandleShift(shl);
2400}
2401
2402void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2403 HandleShift(shl);
2404}
2405
2406void LocationsBuilderX86::VisitShr(HShr* shr) {
2407 HandleShift(shr);
2408}
2409
2410void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2411 HandleShift(shr);
2412}
2413
2414void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2415 HandleShift(ushr);
2416}
2417
2418void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2419 HandleShift(ushr);
2420}
2421
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002422void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002423 LocationSummary* locations =
2424 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002425 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002426 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002427 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2428 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002429}
2430
2431void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2432 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002433 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002434 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002435
Nicolas Geoffray707c8092014-04-04 10:50:14 +01002436 __ fs()->call(
2437 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002438
Nicolas Geoffray39468442014-09-02 15:17:15 +01002439 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002440 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002441}
2442
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002443void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2444 LocationSummary* locations =
2445 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2446 locations->SetOut(Location::RegisterLocation(EAX));
2447 InvokeRuntimeCallingConvention calling_convention;
2448 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2449 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2450 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2451}
2452
2453void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2454 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002455 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002456 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2457
2458 __ fs()->call(
2459 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
2460
2461 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2462 DCHECK(!codegen_->IsLeafMethod());
2463}
2464
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002465void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002466 LocationSummary* locations =
2467 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002468 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2469 if (location.IsStackSlot()) {
2470 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2471 } else if (location.IsDoubleStackSlot()) {
2472 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002473 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002474 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002475}
2476
2477void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002478 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002479}
2480
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002481void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002482 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002483 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002484 locations->SetInAt(0, Location::RequiresRegister());
2485 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002486}
2487
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002488void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2489 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002490 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002491 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002492 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002493 switch (not_->InputAt(0)->GetType()) {
2494 case Primitive::kPrimBoolean:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002495 __ xorl(out.AsRegister<Register>(), Immediate(1));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002496 break;
2497
2498 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002499 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002500 break;
2501
2502 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002503 __ notl(out.AsRegisterPairLow<Register>());
2504 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002505 break;
2506
2507 default:
2508 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2509 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002510}
2511
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002512void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002513 LocationSummary* locations =
2514 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002515 switch (compare->InputAt(0)->GetType()) {
2516 case Primitive::kPrimLong: {
2517 locations->SetInAt(0, Location::RequiresRegister());
2518 // TODO: we set any here but we don't handle constants
2519 locations->SetInAt(1, Location::Any());
2520 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2521 break;
2522 }
2523 case Primitive::kPrimFloat:
2524 case Primitive::kPrimDouble: {
2525 locations->SetInAt(0, Location::RequiresFpuRegister());
2526 locations->SetInAt(1, Location::RequiresFpuRegister());
2527 locations->SetOut(Location::RequiresRegister());
2528 break;
2529 }
2530 default:
2531 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2532 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002533}
2534
2535void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002536 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002537 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002538 Location left = locations->InAt(0);
2539 Location right = locations->InAt(1);
2540
2541 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002542 switch (compare->InputAt(0)->GetType()) {
2543 case Primitive::kPrimLong: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002544 if (right.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002545 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002546 } else {
2547 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002548 __ cmpl(left.AsRegisterPairHigh<Register>(),
2549 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002550 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002551 __ j(kLess, &less); // Signed compare.
2552 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002553 if (right.IsRegisterPair()) {
2554 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002555 } else {
2556 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002557 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002558 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002559 break;
2560 }
2561 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002562 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002563 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
2564 break;
2565 }
2566 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002567 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002568 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002569 break;
2570 }
2571 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002572 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002573 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002574 __ movl(out, Immediate(0));
2575 __ j(kEqual, &done);
2576 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
2577
2578 __ Bind(&greater);
2579 __ movl(out, Immediate(1));
2580 __ jmp(&done);
2581
2582 __ Bind(&less);
2583 __ movl(out, Immediate(-1));
2584
2585 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002586}
2587
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002588void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002589 LocationSummary* locations =
2590 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002591 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2592 locations->SetInAt(i, Location::Any());
2593 }
2594 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002595}
2596
2597void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002598 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002599 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002600}
2601
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002602void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002603 LocationSummary* locations =
2604 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002605 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002606 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002607 bool needs_write_barrier =
2608 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
2609
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002610 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2611 || (field_type == Primitive::kPrimByte);
2612 // The register allocator does not support multiple
2613 // inputs that die at entry with one in a specific register.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002614 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002615 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002616 locations->SetInAt(1, Location::RegisterLocation(EAX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002617 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002618 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002619 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002620 // Temporary registers for the write barrier.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002621 if (needs_write_barrier) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002622 locations->AddTemp(Location::RequiresRegister());
2623 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002624 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002625 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002626}
2627
2628void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2629 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002630 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002631 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01002632 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002633
2634 switch (field_type) {
2635 case Primitive::kPrimBoolean:
2636 case Primitive::kPrimByte: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002637 ByteRegister value = locations->InAt(1).AsRegister<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002638 __ movb(Address(obj, offset), value);
2639 break;
2640 }
2641
2642 case Primitive::kPrimShort:
2643 case Primitive::kPrimChar: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002644 Register value = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002645 __ movw(Address(obj, offset), value);
2646 break;
2647 }
2648
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002649 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002650 case Primitive::kPrimNot: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002651 Register value = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002652 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002653
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002654 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002655 Register temp = locations->GetTemp(0).AsRegister<Register>();
2656 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002657 codegen_->MarkGCCard(temp, card, obj, value);
2658 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002659 break;
2660 }
2661
2662 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002663 Location value = locations->InAt(1);
2664 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
2665 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002666 break;
2667 }
2668
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002669 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002670 XmmRegister value = locations->InAt(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002671 __ movss(Address(obj, offset), value);
2672 break;
2673 }
2674
2675 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002676 XmmRegister value = locations->InAt(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002677 __ movsd(Address(obj, offset), value);
2678 break;
2679 }
2680
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002681 case Primitive::kPrimVoid:
2682 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002683 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002684 }
2685}
2686
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002687void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
2688 Label is_null;
2689 __ testl(value, value);
2690 __ j(kEqual, &is_null);
2691 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2692 __ movl(temp, object);
2693 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
2694 __ movb(Address(temp, card, TIMES_1, 0),
2695 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
2696 __ Bind(&is_null);
2697}
2698
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002699void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002700 LocationSummary* locations =
2701 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002702 locations->SetInAt(0, Location::RequiresRegister());
2703 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002704}
2705
2706void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2707 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002708 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002709 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2710
2711 switch (instruction->GetType()) {
2712 case Primitive::kPrimBoolean: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002713 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002714 __ movzxb(out, Address(obj, offset));
2715 break;
2716 }
2717
2718 case Primitive::kPrimByte: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002719 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002720 __ movsxb(out, Address(obj, offset));
2721 break;
2722 }
2723
2724 case Primitive::kPrimShort: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002725 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002726 __ movsxw(out, Address(obj, offset));
2727 break;
2728 }
2729
2730 case Primitive::kPrimChar: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002731 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002732 __ movzxw(out, Address(obj, offset));
2733 break;
2734 }
2735
2736 case Primitive::kPrimInt:
2737 case Primitive::kPrimNot: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002738 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002739 __ movl(out, Address(obj, offset));
2740 break;
2741 }
2742
2743 case Primitive::kPrimLong: {
2744 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002745 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
2746 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002747 break;
2748 }
2749
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002750 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002751 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002752 __ movss(out, Address(obj, offset));
2753 break;
2754 }
2755
2756 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002757 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002758 __ movsd(out, Address(obj, offset));
2759 break;
2760 }
2761
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002762 case Primitive::kPrimVoid:
2763 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002764 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002765 }
2766}
2767
2768void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002769 LocationSummary* locations =
2770 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002771 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002772 if (instruction->HasUses()) {
2773 locations->SetOut(Location::SameAsFirstInput());
2774 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002775}
2776
2777void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002778 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002779 codegen_->AddSlowPath(slow_path);
2780
2781 LocationSummary* locations = instruction->GetLocations();
2782 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002783
2784 if (obj.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002785 __ cmpl(obj.AsRegister<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002786 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002787 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002788 } else {
2789 DCHECK(obj.IsConstant()) << obj;
2790 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2791 __ jmp(slow_path->GetEntryLabel());
2792 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002793 }
2794 __ j(kEqual, slow_path->GetEntryLabel());
2795}
2796
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002797void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002798 LocationSummary* locations =
2799 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002800 locations->SetInAt(0, Location::RequiresRegister());
2801 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2802 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002803}
2804
2805void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
2806 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002807 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002808 Location index = locations->InAt(1);
2809
2810 switch (instruction->GetType()) {
2811 case Primitive::kPrimBoolean: {
2812 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002813 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002814 if (index.IsConstant()) {
2815 __ movzxb(out, Address(obj,
2816 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2817 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002818 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002819 }
2820 break;
2821 }
2822
2823 case Primitive::kPrimByte: {
2824 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002825 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002826 if (index.IsConstant()) {
2827 __ movsxb(out, Address(obj,
2828 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2829 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002830 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002831 }
2832 break;
2833 }
2834
2835 case Primitive::kPrimShort: {
2836 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002837 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002838 if (index.IsConstant()) {
2839 __ movsxw(out, Address(obj,
2840 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2841 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002842 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002843 }
2844 break;
2845 }
2846
2847 case Primitive::kPrimChar: {
2848 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002849 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002850 if (index.IsConstant()) {
2851 __ movzxw(out, Address(obj,
2852 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2853 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002854 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002855 }
2856 break;
2857 }
2858
2859 case Primitive::kPrimInt:
2860 case Primitive::kPrimNot: {
2861 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002862 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002863 if (index.IsConstant()) {
2864 __ movl(out, Address(obj,
2865 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2866 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002867 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002868 }
2869 break;
2870 }
2871
2872 case Primitive::kPrimLong: {
2873 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002874 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002875 if (index.IsConstant()) {
2876 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002877 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
2878 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002879 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002880 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00002881 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002882 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00002883 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002884 }
2885 break;
2886 }
2887
2888 case Primitive::kPrimFloat:
2889 case Primitive::kPrimDouble:
2890 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002891 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002892 case Primitive::kPrimVoid:
2893 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002894 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002895 }
2896}
2897
2898void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002899 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002900 bool needs_write_barrier =
2901 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2902
2903 DCHECK(kFollowsQuickABI);
2904 bool not_enough_registers = needs_write_barrier
2905 && !instruction->GetValue()->IsConstant()
2906 && !instruction->GetIndex()->IsConstant();
2907 bool needs_runtime_call = instruction->NeedsTypeCheck() || not_enough_registers;
2908
Nicolas Geoffray39468442014-09-02 15:17:15 +01002909 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2910 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002911 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002912
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002913 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002914 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002915 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2916 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2917 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002918 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002919 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
2920 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002921 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002922 // In case of a byte operation, the register allocator does not support multiple
2923 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002924 locations->SetInAt(0, Location::RequiresRegister());
2925 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002926 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002927 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002928 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002929 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002930 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002931 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002932 // Temporary registers for the write barrier.
2933 if (needs_write_barrier) {
2934 locations->AddTemp(Location::RequiresRegister());
2935 // Ensure the card is in a byte register.
2936 locations->AddTemp(Location::RegisterLocation(ECX));
2937 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002938 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002939}
2940
2941void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
2942 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002943 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002944 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002945 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002946 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002947 bool needs_runtime_call = locations->WillCall();
2948 bool needs_write_barrier =
2949 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002950
2951 switch (value_type) {
2952 case Primitive::kPrimBoolean:
2953 case Primitive::kPrimByte: {
2954 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002955 if (index.IsConstant()) {
2956 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002957 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002958 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002959 } else {
2960 __ movb(Address(obj, offset),
2961 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2962 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002963 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002964 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002965 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
2966 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002967 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002968 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002969 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2970 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002971 }
2972 break;
2973 }
2974
2975 case Primitive::kPrimShort:
2976 case Primitive::kPrimChar: {
2977 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002978 if (index.IsConstant()) {
2979 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002980 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002981 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002982 } else {
2983 __ movw(Address(obj, offset),
2984 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2985 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002986 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002987 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002988 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
2989 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002990 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002991 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002992 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2993 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002994 }
2995 break;
2996 }
2997
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002998 case Primitive::kPrimInt:
2999 case Primitive::kPrimNot: {
3000 if (!needs_runtime_call) {
3001 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3002 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003003 size_t offset =
3004 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003005 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003006 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003007 } else {
3008 DCHECK(value.IsConstant()) << value;
3009 __ movl(Address(obj, offset),
3010 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3011 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003012 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003013 DCHECK(index.IsRegister()) << index;
3014 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003015 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3016 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003017 } else {
3018 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003019 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003020 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3021 }
3022 }
3023
3024 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003025 Register temp = locations->GetTemp(0).AsRegister<Register>();
3026 Register card = locations->GetTemp(1).AsRegister<Register>();
3027 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003028 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003029 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003030 DCHECK_EQ(value_type, Primitive::kPrimNot);
3031 DCHECK(!codegen_->IsLeafMethod());
3032 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3033 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003034 }
3035 break;
3036 }
3037
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003038 case Primitive::kPrimLong: {
3039 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003040 if (index.IsConstant()) {
3041 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003042 if (value.IsRegisterPair()) {
3043 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
3044 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003045 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003046 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003047 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3048 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
3049 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3050 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003051 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003052 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003053 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003054 value.AsRegisterPairLow<Register>());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003055 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003056 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003057 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003058 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003059 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003060 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003061 Immediate(Low32Bits(val)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003062 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003063 Immediate(High32Bits(val)));
3064 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003065 }
3066 break;
3067 }
3068
3069 case Primitive::kPrimFloat:
3070 case Primitive::kPrimDouble:
3071 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003072 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003073 case Primitive::kPrimVoid:
3074 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003075 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003076 }
3077}
3078
3079void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3080 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003081 locations->SetInAt(0, Location::RequiresRegister());
3082 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003083 instruction->SetLocations(locations);
3084}
3085
3086void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3087 LocationSummary* locations = instruction->GetLocations();
3088 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003089 Register obj = locations->InAt(0).AsRegister<Register>();
3090 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003091 __ movl(out, Address(obj, offset));
3092}
3093
3094void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003095 LocationSummary* locations =
3096 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003097 locations->SetInAt(0, Location::RequiresRegister());
3098 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003099 if (instruction->HasUses()) {
3100 locations->SetOut(Location::SameAsFirstInput());
3101 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003102}
3103
3104void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3105 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003106 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01003107 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003108 codegen_->AddSlowPath(slow_path);
3109
Roland Levillain271ab9c2014-11-27 15:23:57 +00003110 Register index = locations->InAt(0).AsRegister<Register>();
3111 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003112
3113 __ cmpl(index, length);
3114 __ j(kAboveEqual, slow_path->GetEntryLabel());
3115}
3116
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003117void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3118 temp->SetLocations(nullptr);
3119}
3120
3121void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3122 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003123 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003124}
3125
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003126void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003127 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003128 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003129}
3130
3131void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003132 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3133}
3134
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003135void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3136 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3137}
3138
3139void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003140 HBasicBlock* block = instruction->GetBlock();
3141 if (block->GetLoopInformation() != nullptr) {
3142 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3143 // The back edge will generate the suspend check.
3144 return;
3145 }
3146 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3147 // The goto will generate the suspend check.
3148 return;
3149 }
3150 GenerateSuspendCheck(instruction, nullptr);
3151}
3152
3153void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3154 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003155 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003156 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003157 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003158 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003159 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003160 if (successor == nullptr) {
3161 __ j(kNotEqual, slow_path->GetEntryLabel());
3162 __ Bind(slow_path->GetReturnLabel());
3163 } else {
3164 __ j(kEqual, codegen_->GetLabelOf(successor));
3165 __ jmp(slow_path->GetEntryLabel());
3166 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003167}
3168
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003169X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3170 return codegen_->GetAssembler();
3171}
3172
3173void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
3174 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003175 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003176 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3177 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
3178 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
3179}
3180
3181void ParallelMoveResolverX86::EmitMove(size_t index) {
3182 MoveOperands* move = moves_.Get(index);
3183 Location source = move->GetSource();
3184 Location destination = move->GetDestination();
3185
3186 if (source.IsRegister()) {
3187 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003188 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003189 } else {
3190 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003191 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003192 }
3193 } else if (source.IsStackSlot()) {
3194 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003195 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003196 } else {
3197 DCHECK(destination.IsStackSlot());
3198 MoveMemoryToMemory(destination.GetStackIndex(),
3199 source.GetStackIndex());
3200 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003201 } else if (source.IsConstant()) {
3202 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
3203 Immediate imm(instruction->AsIntConstant()->GetValue());
3204 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003205 __ movl(destination.AsRegister<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003206 } else {
3207 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3208 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003209 } else {
3210 LOG(FATAL) << "Unimplemented";
3211 }
3212}
3213
3214void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003215 Register suggested_scratch = reg == EAX ? EBX : EAX;
3216 ScratchRegisterScope ensure_scratch(
3217 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3218
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003219 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3220 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
3221 __ movl(Address(ESP, mem + stack_offset), reg);
3222 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
3223}
3224
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003225void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
3226 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003227 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3228
3229 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003230 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003231 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3232
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003233 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
3234 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
3235 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
3236 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
3237 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
3238 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
3239}
3240
3241void ParallelMoveResolverX86::EmitSwap(size_t index) {
3242 MoveOperands* move = moves_.Get(index);
3243 Location source = move->GetSource();
3244 Location destination = move->GetDestination();
3245
3246 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003247 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003248 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003249 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003250 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003251 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003252 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3253 Exchange(destination.GetStackIndex(), source.GetStackIndex());
3254 } else {
3255 LOG(FATAL) << "Unimplemented";
3256 }
3257}
3258
3259void ParallelMoveResolverX86::SpillScratch(int reg) {
3260 __ pushl(static_cast<Register>(reg));
3261}
3262
3263void ParallelMoveResolverX86::RestoreScratch(int reg) {
3264 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003265}
3266
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003267void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003268 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3269 ? LocationSummary::kCallOnSlowPath
3270 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003271 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003272 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003273 locations->SetOut(Location::RequiresRegister());
3274}
3275
3276void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003277 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003278 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003279 DCHECK(!cls->CanCallRuntime());
3280 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003281 codegen_->LoadCurrentMethod(out);
3282 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3283 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003284 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003285 codegen_->LoadCurrentMethod(out);
3286 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3287 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003288
3289 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3290 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3291 codegen_->AddSlowPath(slow_path);
3292 __ testl(out, out);
3293 __ j(kEqual, slow_path->GetEntryLabel());
3294 if (cls->MustGenerateClinitCheck()) {
3295 GenerateClassInitializationCheck(slow_path, out);
3296 } else {
3297 __ Bind(slow_path->GetExitLabel());
3298 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003299 }
3300}
3301
3302void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
3303 LocationSummary* locations =
3304 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3305 locations->SetInAt(0, Location::RequiresRegister());
3306 if (check->HasUses()) {
3307 locations->SetOut(Location::SameAsFirstInput());
3308 }
3309}
3310
3311void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003312 // We assume the class to not be null.
3313 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3314 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003315 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003316 GenerateClassInitializationCheck(slow_path,
3317 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003318}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003319
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003320void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
3321 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003322 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3323 Immediate(mirror::Class::kStatusInitialized));
3324 __ j(kLess, slow_path->GetEntryLabel());
3325 __ Bind(slow_path->GetExitLabel());
3326 // No need for memory fence, thanks to the X86 memory model.
3327}
3328
3329void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3330 LocationSummary* locations =
3331 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3332 locations->SetInAt(0, Location::RequiresRegister());
3333 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3334}
3335
3336void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3337 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003338 Register cls = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003339 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
3340
3341 switch (instruction->GetType()) {
3342 case Primitive::kPrimBoolean: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003343 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003344 __ movzxb(out, Address(cls, offset));
3345 break;
3346 }
3347
3348 case Primitive::kPrimByte: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003349 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003350 __ movsxb(out, Address(cls, offset));
3351 break;
3352 }
3353
3354 case Primitive::kPrimShort: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003355 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003356 __ movsxw(out, Address(cls, offset));
3357 break;
3358 }
3359
3360 case Primitive::kPrimChar: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003361 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003362 __ movzxw(out, Address(cls, offset));
3363 break;
3364 }
3365
3366 case Primitive::kPrimInt:
3367 case Primitive::kPrimNot: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003368 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003369 __ movl(out, Address(cls, offset));
3370 break;
3371 }
3372
3373 case Primitive::kPrimLong: {
3374 // TODO: support volatile.
3375 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(cls, offset));
3376 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(cls, kX86WordSize + offset));
3377 break;
3378 }
3379
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003380 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003381 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003382 __ movss(out, Address(cls, offset));
3383 break;
3384 }
3385
3386 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003387 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003388 __ movsd(out, Address(cls, offset));
3389 break;
3390 }
3391
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003392 case Primitive::kPrimVoid:
3393 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3394 UNREACHABLE();
3395 }
3396}
3397
3398void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3399 LocationSummary* locations =
3400 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3401 locations->SetInAt(0, Location::RequiresRegister());
3402 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003403 bool needs_write_barrier =
3404 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003405 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3406 || (field_type == Primitive::kPrimByte);
3407 // The register allocator does not support multiple
3408 // inputs that die at entry with one in a specific register.
3409 if (is_byte_type) {
3410 // Ensure the value is in a byte register.
3411 locations->SetInAt(1, Location::RegisterLocation(EAX));
3412 } else {
3413 locations->SetInAt(1, Location::RequiresRegister());
3414 }
3415 // Temporary registers for the write barrier.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003416 if (needs_write_barrier) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003417 locations->AddTemp(Location::RequiresRegister());
3418 // Ensure the card is in a byte register.
3419 locations->AddTemp(Location::RegisterLocation(ECX));
3420 }
3421}
3422
3423void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3424 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003425 Register cls = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003426 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
3427 Primitive::Type field_type = instruction->GetFieldType();
3428
3429 switch (field_type) {
3430 case Primitive::kPrimBoolean:
3431 case Primitive::kPrimByte: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003432 ByteRegister value = locations->InAt(1).AsRegister<ByteRegister>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003433 __ movb(Address(cls, offset), value);
3434 break;
3435 }
3436
3437 case Primitive::kPrimShort:
3438 case Primitive::kPrimChar: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003439 Register value = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003440 __ movw(Address(cls, offset), value);
3441 break;
3442 }
3443
3444 case Primitive::kPrimInt:
3445 case Primitive::kPrimNot: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003446 Register value = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003447 __ movl(Address(cls, offset), value);
3448
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003449 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003450 Register temp = locations->GetTemp(0).AsRegister<Register>();
3451 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003452 codegen_->MarkGCCard(temp, card, cls, value);
3453 }
3454 break;
3455 }
3456
3457 case Primitive::kPrimLong: {
3458 Location value = locations->InAt(1);
3459 __ movl(Address(cls, offset), value.AsRegisterPairLow<Register>());
3460 __ movl(Address(cls, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3461 break;
3462 }
3463
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003464 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003465 XmmRegister value = locations->InAt(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003466 __ movss(Address(cls, offset), value);
3467 break;
3468 }
3469
3470 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003471 XmmRegister value = locations->InAt(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003472 __ movsd(Address(cls, offset), value);
3473 break;
3474 }
3475
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003476 case Primitive::kPrimVoid:
3477 LOG(FATAL) << "Unreachable type " << field_type;
3478 UNREACHABLE();
3479 }
3480}
3481
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003482void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
3483 LocationSummary* locations =
3484 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3485 locations->SetOut(Location::RequiresRegister());
3486}
3487
3488void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
3489 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
3490 codegen_->AddSlowPath(slow_path);
3491
Roland Levillain271ab9c2014-11-27 15:23:57 +00003492 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003493 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08003494 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3495 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003496 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3497 __ testl(out, out);
3498 __ j(kEqual, slow_path->GetEntryLabel());
3499 __ Bind(slow_path->GetExitLabel());
3500}
3501
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003502void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
3503 LocationSummary* locations =
3504 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3505 locations->SetOut(Location::RequiresRegister());
3506}
3507
3508void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
3509 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003510 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003511 __ fs()->movl(address, Immediate(0));
3512}
3513
3514void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
3515 LocationSummary* locations =
3516 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3517 InvokeRuntimeCallingConvention calling_convention;
3518 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3519}
3520
3521void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
3522 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
3523 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3524}
3525
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003526void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003527 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3528 ? LocationSummary::kNoCall
3529 : LocationSummary::kCallOnSlowPath;
3530 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3531 locations->SetInAt(0, Location::RequiresRegister());
3532 locations->SetInAt(1, Location::Any());
3533 locations->SetOut(Location::RequiresRegister());
3534}
3535
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003536void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003537 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003538 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003539 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003540 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003541 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3542 Label done, zero;
3543 SlowPathCodeX86* slow_path = nullptr;
3544
3545 // Return 0 if `obj` is null.
3546 // TODO: avoid this check if we know obj is not null.
3547 __ testl(obj, obj);
3548 __ j(kEqual, &zero);
3549 __ movl(out, Address(obj, class_offset));
3550 // Compare the class of `obj` with `cls`.
3551 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003552 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003553 } else {
3554 DCHECK(cls.IsStackSlot()) << cls;
3555 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
3556 }
3557
3558 if (instruction->IsClassFinal()) {
3559 // Classes must be equal for the instanceof to succeed.
3560 __ j(kNotEqual, &zero);
3561 __ movl(out, Immediate(1));
3562 __ jmp(&done);
3563 } else {
3564 // If the classes are not equal, we go into a slow path.
3565 DCHECK(locations->OnlyCallsOnSlowPath());
3566 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003567 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003568 codegen_->AddSlowPath(slow_path);
3569 __ j(kNotEqual, slow_path->GetEntryLabel());
3570 __ movl(out, Immediate(1));
3571 __ jmp(&done);
3572 }
3573 __ Bind(&zero);
3574 __ movl(out, Immediate(0));
3575 if (slow_path != nullptr) {
3576 __ Bind(slow_path->GetExitLabel());
3577 }
3578 __ Bind(&done);
3579}
3580
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003581void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
3582 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3583 instruction, LocationSummary::kCallOnSlowPath);
3584 locations->SetInAt(0, Location::RequiresRegister());
3585 locations->SetInAt(1, Location::Any());
3586 locations->AddTemp(Location::RequiresRegister());
3587}
3588
3589void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
3590 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003591 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003592 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003593 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003594 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3595 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
3596 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3597 codegen_->AddSlowPath(slow_path);
3598
3599 // TODO: avoid this check if we know obj is not null.
3600 __ testl(obj, obj);
3601 __ j(kEqual, slow_path->GetExitLabel());
3602 __ movl(temp, Address(obj, class_offset));
3603
3604 // Compare the class of `obj` with `cls`.
3605 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003606 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003607 } else {
3608 DCHECK(cls.IsStackSlot()) << cls;
3609 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
3610 }
3611
3612 __ j(kNotEqual, slow_path->GetEntryLabel());
3613 __ Bind(slow_path->GetExitLabel());
3614}
3615
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003616void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3617 LocationSummary* locations =
3618 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3619 InvokeRuntimeCallingConvention calling_convention;
3620 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3621}
3622
3623void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3624 __ fs()->call(Address::Absolute(instruction->IsEnter()
3625 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
3626 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
3627 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3628}
3629
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003630void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3631void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3632void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3633
3634void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3635 LocationSummary* locations =
3636 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3637 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3638 || instruction->GetResultType() == Primitive::kPrimLong);
3639 locations->SetInAt(0, Location::RequiresRegister());
3640 locations->SetInAt(1, Location::Any());
3641 locations->SetOut(Location::SameAsFirstInput());
3642}
3643
3644void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
3645 HandleBitwiseOperation(instruction);
3646}
3647
3648void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
3649 HandleBitwiseOperation(instruction);
3650}
3651
3652void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
3653 HandleBitwiseOperation(instruction);
3654}
3655
3656void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3657 LocationSummary* locations = instruction->GetLocations();
3658 Location first = locations->InAt(0);
3659 Location second = locations->InAt(1);
3660 DCHECK(first.Equals(locations->Out()));
3661
3662 if (instruction->GetResultType() == Primitive::kPrimInt) {
3663 if (second.IsRegister()) {
3664 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003665 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003666 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003667 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003668 } else {
3669 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003670 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003671 }
3672 } else if (second.IsConstant()) {
3673 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003674 __ andl(first.AsRegister<Register>(),
3675 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003676 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003677 __ orl(first.AsRegister<Register>(),
3678 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003679 } else {
3680 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00003681 __ xorl(first.AsRegister<Register>(),
3682 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003683 }
3684 } else {
3685 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003686 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003687 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003688 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003689 } else {
3690 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003691 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003692 }
3693 }
3694 } else {
3695 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3696 if (second.IsRegisterPair()) {
3697 if (instruction->IsAnd()) {
3698 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3699 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3700 } else if (instruction->IsOr()) {
3701 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3702 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3703 } else {
3704 DCHECK(instruction->IsXor());
3705 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3706 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3707 }
3708 } else {
3709 if (instruction->IsAnd()) {
3710 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3711 __ andl(first.AsRegisterPairHigh<Register>(),
3712 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3713 } else if (instruction->IsOr()) {
3714 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3715 __ orl(first.AsRegisterPairHigh<Register>(),
3716 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3717 } else {
3718 DCHECK(instruction->IsXor());
3719 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3720 __ xorl(first.AsRegisterPairHigh<Register>(),
3721 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3722 }
3723 }
3724 }
3725}
3726
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003727} // namespace x86
3728} // namespace art