blob: ac6fdbcfe9573fb2a9c58d20d5d0dcf20ed985de [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 int kNumberOfPushedRegistersAtEntry = 1;
35static constexpr int kCurrentMethodStackOffset = 0;
36
Calin Juravled6fb6cf2014-11-11 19:07:44 +000037static constexpr Register kRuntimeParameterCoreRegisters[] = { EAX, ECX, EDX, EBX };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010038static constexpr size_t kRuntimeParameterCoreRegistersLength =
39 arraysize(kRuntimeParameterCoreRegisters);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010040static constexpr XmmRegister kRuntimeParameterFpuRegisters[] = { };
41static constexpr size_t kRuntimeParameterFpuRegistersLength = 0;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010042
Mark Mendell24f2dfa2015-01-14 19:51:45 -050043static constexpr int kC2ConditionMask = 0x400;
44
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000045// Marker for places that can be updated once we don't follow the quick ABI.
46static constexpr bool kFollowsQuickABI = true;
47
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010048class InvokeRuntimeCallingConvention : public CallingConvention<Register, XmmRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010049 public:
50 InvokeRuntimeCallingConvention()
51 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010052 kRuntimeParameterCoreRegistersLength,
53 kRuntimeParameterFpuRegisters,
54 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010055
56 private:
57 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
58};
59
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
61
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010062class SlowPathCodeX86 : public SlowPathCode {
63 public:
64 SlowPathCodeX86() : entry_label_(), exit_label_() {}
65
66 Label* GetEntryLabel() { return &entry_label_; }
67 Label* GetExitLabel() { return &exit_label_; }
68
69 private:
70 Label entry_label_;
71 Label exit_label_;
72
73 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86);
74};
75
76class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010077 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010078 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079
80 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
81 __ Bind(GetEntryLabel());
82 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010083 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010084 }
85
86 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010087 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010088 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
89};
90
Calin Juravled0d48522014-11-04 16:40:20 +000091class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
92 public:
93 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
94
95 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
96 __ Bind(GetEntryLabel());
97 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
98 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
99 }
100
101 private:
102 HDivZeroCheck* const instruction_;
103 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
104};
105
Calin Juravlebacfec32014-11-14 15:54:36 +0000106class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +0000107 public:
Calin Juravlebacfec32014-11-14 15:54:36 +0000108 explicit DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000109
110 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
111 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000112 if (is_div_) {
113 __ negl(reg_);
114 } else {
115 __ movl(reg_, Immediate(0));
116 }
Calin Juravled0d48522014-11-04 16:40:20 +0000117 __ jmp(GetExitLabel());
118 }
119
120 private:
121 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000122 bool is_div_;
123 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000124};
125
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100126class StackOverflowCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100127 public:
128 StackOverflowCheckSlowPathX86() {}
129
130 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
131 __ Bind(GetEntryLabel());
132 __ addl(ESP,
133 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
134 __ fs()->jmp(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowStackOverflow)));
135 }
136
137 private:
138 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86);
139};
140
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100141class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100142 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100143 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
144 Location index_location,
145 Location length_location)
Roland Levillain199f3362014-11-27 17:15:16 +0000146 : instruction_(instruction),
147 index_location_(index_location),
148 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100149
150 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100151 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100152 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000153 // We're moving two locations to locations that could overlap, so we need a parallel
154 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100155 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000156 x86_codegen->EmitParallelMoves(
157 index_location_,
158 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
159 length_location_,
160 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100161 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100162 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100163 }
164
165 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100166 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100167 const Location index_location_;
168 const Location length_location_;
169
170 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
171};
172
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100173class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000174 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100175 explicit SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
176 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000177
178 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100179 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000180 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100181 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000182 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
183 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100184 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100185 if (successor_ == nullptr) {
186 __ jmp(GetReturnLabel());
187 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100188 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100189 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000190 }
191
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100192 Label* GetReturnLabel() {
193 DCHECK(successor_ == nullptr);
194 return &return_label_;
195 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000196
197 private:
198 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100199 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000200 Label return_label_;
201
202 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
203};
204
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000205class LoadStringSlowPathX86 : public SlowPathCodeX86 {
206 public:
207 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
208
209 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
210 LocationSummary* locations = instruction_->GetLocations();
211 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
212
213 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
214 __ Bind(GetEntryLabel());
215 codegen->SaveLiveRegisters(locations);
216
217 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800218 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
219 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000220 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
221 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
222 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
223 codegen->RestoreLiveRegisters(locations);
224
225 __ jmp(GetExitLabel());
226 }
227
228 private:
229 HLoadString* const instruction_;
230
231 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
232};
233
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000234class LoadClassSlowPathX86 : public SlowPathCodeX86 {
235 public:
236 LoadClassSlowPathX86(HLoadClass* cls,
237 HInstruction* at,
238 uint32_t dex_pc,
239 bool do_clinit)
240 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
241 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
242 }
243
244 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
245 LocationSummary* locations = at_->GetLocations();
246 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
247 __ Bind(GetEntryLabel());
248 codegen->SaveLiveRegisters(locations);
249
250 InvokeRuntimeCallingConvention calling_convention;
251 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
252 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
253 __ fs()->call(Address::Absolute(do_clinit_
254 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
255 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
256 codegen->RecordPcInfo(at_, dex_pc_);
257
258 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000259 Location out = locations->Out();
260 if (out.IsValid()) {
261 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
262 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000263 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000264
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000265 codegen->RestoreLiveRegisters(locations);
266 __ jmp(GetExitLabel());
267 }
268
269 private:
270 // The class this slow path will load.
271 HLoadClass* const cls_;
272
273 // The instruction where this slow path is happening.
274 // (Might be the load class or an initialization check).
275 HInstruction* const at_;
276
277 // The dex PC of `at_`.
278 const uint32_t dex_pc_;
279
280 // Whether to initialize the class.
281 const bool do_clinit_;
282
283 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
284};
285
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000286class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
287 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000288 TypeCheckSlowPathX86(HInstruction* instruction,
289 Location class_to_check,
290 Location object_class,
291 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000292 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000293 class_to_check_(class_to_check),
294 object_class_(object_class),
295 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000296
297 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
298 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000299 DCHECK(instruction_->IsCheckCast()
300 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000301
302 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
303 __ Bind(GetEntryLabel());
304 codegen->SaveLiveRegisters(locations);
305
306 // We're moving two locations to locations that could overlap, so we need a parallel
307 // move resolver.
308 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000309 x86_codegen->EmitParallelMoves(
310 class_to_check_,
311 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
312 object_class_,
313 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000314
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000315 if (instruction_->IsInstanceOf()) {
Roland Levillain199f3362014-11-27 17:15:16 +0000316 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize,
317 pInstanceofNonTrivial)));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000318 } else {
319 DCHECK(instruction_->IsCheckCast());
320 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
321 }
322
323 codegen->RecordPcInfo(instruction_, dex_pc_);
324 if (instruction_->IsInstanceOf()) {
325 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
326 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000327 codegen->RestoreLiveRegisters(locations);
328
329 __ jmp(GetExitLabel());
330 }
331
332 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000333 HInstruction* const instruction_;
334 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000335 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000336 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000337
338 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
339};
340
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100341#undef __
342#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
343
Dave Allison20dfc792014-06-16 20:44:29 -0700344inline Condition X86Condition(IfCondition cond) {
345 switch (cond) {
346 case kCondEQ: return kEqual;
347 case kCondNE: return kNotEqual;
348 case kCondLT: return kLess;
349 case kCondLE: return kLessEqual;
350 case kCondGT: return kGreater;
351 case kCondGE: return kGreaterEqual;
352 default:
353 LOG(FATAL) << "Unknown if condition";
354 }
355 return kEqual;
356}
357
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100358void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
359 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
360}
361
362void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
363 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
364}
365
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100366size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
367 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
368 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100369}
370
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100371size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
372 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
373 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100374}
375
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000376CodeGeneratorX86::CodeGeneratorX86(HGraph* graph, const CompilerOptions& compiler_options)
377 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfXmmRegisters,
378 kNumberOfRegisterPairs, compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100379 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100380 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100381 instruction_visitor_(graph, this),
382 move_resolver_(graph->GetArena(), this) {}
383
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100384size_t CodeGeneratorX86::FrameEntrySpillSize() const {
385 return kNumberOfPushedRegistersAtEntry * kX86WordSize;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100386}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100387
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100388Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100389 switch (type) {
390 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100391 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100392 X86ManagedRegister pair =
393 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100394 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
395 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100396 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
397 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100398 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100399 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100400 }
401
402 case Primitive::kPrimByte:
403 case Primitive::kPrimBoolean:
404 case Primitive::kPrimChar:
405 case Primitive::kPrimShort:
406 case Primitive::kPrimInt:
407 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100408 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100409 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100410 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100411 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
412 X86ManagedRegister current =
413 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
414 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100415 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100416 }
417 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100418 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100419 }
420
421 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100422 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100423 return Location::FpuRegisterLocation(
424 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100425 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100426
427 case Primitive::kPrimVoid:
428 LOG(FATAL) << "Unreachable type " << type;
429 }
430
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100431 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100432}
433
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100434void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100435 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100436 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100437
438 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100439 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100440
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000441 // TODO: We currently don't use Quick's callee saved registers.
442 DCHECK(kFollowsQuickABI);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100443 blocked_core_registers_[EBP] = true;
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000444 blocked_core_registers_[ESI] = true;
445 blocked_core_registers_[EDI] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100446
447 UpdateBlockedPairRegisters();
448}
449
450void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
451 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
452 X86ManagedRegister current =
453 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
454 if (blocked_core_registers_[current.AsRegisterPairLow()]
455 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
456 blocked_register_pairs_[i] = true;
457 }
458 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100459}
460
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100461InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
462 : HGraphVisitor(graph),
463 assembler_(codegen->GetAssembler()),
464 codegen_(codegen) {}
465
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000466void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000467 // Create a fake register to mimic Quick.
468 static const int kFakeReturnRegister = 8;
469 core_spill_mask_ |= (1 << kFakeReturnRegister);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000470
Roland Levillain199f3362014-11-27 17:15:16 +0000471 bool skip_overflow_check =
472 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Calin Juravle93edf732015-01-20 20:14:07 +0000473 bool implicitStackOverflowChecks = GetCompilerOptions().GetImplicitStackOverflowChecks();
474
475 if (!skip_overflow_check && implicitStackOverflowChecks) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100476 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100477 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100478 }
479
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100480 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100481 __ subl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100482
Calin Juravle93edf732015-01-20 20:14:07 +0000483 if (!skip_overflow_check && !implicitStackOverflowChecks) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100484 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86();
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100485 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100486
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100487 __ fs()->cmpl(ESP, Address::Absolute(Thread::StackEndOffset<kX86WordSize>()));
488 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100489 }
490
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100491 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000492}
493
494void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100495 __ addl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000496}
497
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100498void CodeGeneratorX86::Bind(HBasicBlock* block) {
499 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000500}
501
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100502void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100503 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000504}
505
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100506Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
507 switch (load->GetType()) {
508 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100509 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100510 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
511 break;
512
513 case Primitive::kPrimInt:
514 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100515 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100516 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100517
518 case Primitive::kPrimBoolean:
519 case Primitive::kPrimByte:
520 case Primitive::kPrimChar:
521 case Primitive::kPrimShort:
522 case Primitive::kPrimVoid:
523 LOG(FATAL) << "Unexpected type " << load->GetType();
524 }
525
526 LOG(FATAL) << "Unreachable";
527 return Location();
528}
529
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100530Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
531 switch (type) {
532 case Primitive::kPrimBoolean:
533 case Primitive::kPrimByte:
534 case Primitive::kPrimChar:
535 case Primitive::kPrimShort:
536 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100537 case Primitive::kPrimFloat:
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100538 case Primitive::kPrimNot: {
539 uint32_t index = gp_index_++;
540 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100541 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100542 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100543 return Location::StackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100544 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100545 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100546
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100547 case Primitive::kPrimLong:
548 case Primitive::kPrimDouble: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100549 uint32_t index = gp_index_;
550 gp_index_ += 2;
551 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100552 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
553 calling_convention.GetRegisterPairAt(index));
554 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100555 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000556 // On X86, the register index and stack index of a quick parameter is the same, since
557 // we are passing floating pointer values in core registers.
558 return Location::QuickParameter(index, index);
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100559 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100560 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100561 }
562 }
563
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100564 case Primitive::kPrimVoid:
565 LOG(FATAL) << "Unexpected parameter type " << type;
566 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100567 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100568 return Location();
569}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100570
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100571void CodeGeneratorX86::Move32(Location destination, Location source) {
572 if (source.Equals(destination)) {
573 return;
574 }
575 if (destination.IsRegister()) {
576 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000577 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100578 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000579 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100580 } else {
581 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000582 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100583 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100584 } else if (destination.IsFpuRegister()) {
585 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000586 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100587 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000588 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100589 } else {
590 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000591 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100592 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100593 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000594 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100595 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000596 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100597 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000598 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100599 } else {
600 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100601 __ pushl(Address(ESP, source.GetStackIndex()));
602 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100603 }
604 }
605}
606
607void CodeGeneratorX86::Move64(Location destination, Location source) {
608 if (source.Equals(destination)) {
609 return;
610 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100611 if (destination.IsRegisterPair()) {
612 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000613 EmitParallelMoves(
614 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
615 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
616 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
617 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100618 } else if (source.IsFpuRegister()) {
619 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100620 } else if (source.IsQuickParameter()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000621 uint16_t register_index = source.GetQuickParameterRegisterIndex();
622 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100623 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000624 EmitParallelMoves(
625 Location::RegisterLocation(calling_convention.GetRegisterAt(register_index)),
626 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
627 Location::StackSlot(
628 calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize()),
629 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100630 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000631 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100632 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100633 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
634 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100635 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
636 }
637 } else if (destination.IsQuickParameter()) {
638 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000639 uint16_t register_index = destination.GetQuickParameterRegisterIndex();
640 uint16_t stack_index = destination.GetQuickParameterStackIndex();
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000641 if (source.IsRegisterPair()) {
642 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100643 } else if (source.IsFpuRegister()) {
644 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100645 } else {
646 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000647 EmitParallelMoves(
648 Location::StackSlot(source.GetStackIndex()),
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000649 Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index)),
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000650 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
651 Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index + 1)));
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000652 __ movl(calling_convention.GetRegisterAt(register_index), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100653 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100654 } else if (destination.IsFpuRegister()) {
655 if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000656 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100657 } else {
658 LOG(FATAL) << "Unimplemented";
659 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100660 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000661 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100662 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000663 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100664 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100665 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100666 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100667 } else if (source.IsQuickParameter()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000668 // No conflict possible, so just do the move.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100669 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000670 uint16_t register_index = source.GetQuickParameterRegisterIndex();
671 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000672 // Just move the low part. The only time a source is a quick parameter is
673 // when moving the parameter to its stack locations. And the (Java) caller
674 // of this method has already done that.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100675 __ movl(Address(ESP, destination.GetStackIndex()),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000676 calling_convention.GetRegisterAt(register_index));
677 DCHECK_EQ(calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100678 static_cast<size_t>(destination.GetHighStackIndex(kX86WordSize)));
679 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000680 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100681 } else {
682 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000683 EmitParallelMoves(
684 Location::StackSlot(source.GetStackIndex()),
685 Location::StackSlot(destination.GetStackIndex()),
686 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
687 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100688 }
689 }
690}
691
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100692void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000693 LocationSummary* locations = instruction->GetLocations();
694 if (locations != nullptr && locations->Out().Equals(location)) {
695 return;
696 }
697
698 if (locations != nullptr && locations->Out().IsConstant()) {
699 HConstant* const_to_move = locations->Out().GetConstant();
700 if (const_to_move->IsIntConstant()) {
701 Immediate imm(const_to_move->AsIntConstant()->GetValue());
702 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000703 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000704 } else if (location.IsStackSlot()) {
705 __ movl(Address(ESP, location.GetStackIndex()), imm);
706 } else {
707 DCHECK(location.IsConstant());
708 DCHECK_EQ(location.GetConstant(), const_to_move);
709 }
710 } else if (const_to_move->IsLongConstant()) {
711 int64_t value = const_to_move->AsLongConstant()->GetValue();
712 if (location.IsRegisterPair()) {
713 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
714 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
715 } else if (location.IsDoubleStackSlot()) {
716 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000717 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
718 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000719 } else {
720 DCHECK(location.IsConstant());
721 DCHECK_EQ(location.GetConstant(), instruction);
722 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100723 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000724 } else if (instruction->IsTemporary()) {
725 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000726 if (temp_location.IsStackSlot()) {
727 Move32(location, temp_location);
728 } else {
729 DCHECK(temp_location.IsDoubleStackSlot());
730 Move64(location, temp_location);
731 }
Roland Levillain476df552014-10-09 17:51:36 +0100732 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100733 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100734 switch (instruction->GetType()) {
735 case Primitive::kPrimBoolean:
736 case Primitive::kPrimByte:
737 case Primitive::kPrimChar:
738 case Primitive::kPrimShort:
739 case Primitive::kPrimInt:
740 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100741 case Primitive::kPrimFloat:
742 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100743 break;
744
745 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100746 case Primitive::kPrimDouble:
747 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100748 break;
749
750 default:
751 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
752 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000753 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100754 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100755 switch (instruction->GetType()) {
756 case Primitive::kPrimBoolean:
757 case Primitive::kPrimByte:
758 case Primitive::kPrimChar:
759 case Primitive::kPrimShort:
760 case Primitive::kPrimInt:
761 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100762 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000763 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100764 break;
765
766 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100767 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000768 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100769 break;
770
771 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100772 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100773 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000774 }
775}
776
777void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000778 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000779}
780
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000781void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000782 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100783 DCHECK(!successor->IsExitBlock());
784
785 HBasicBlock* block = got->GetBlock();
786 HInstruction* previous = got->GetPrevious();
787
788 HLoopInformation* info = block->GetLoopInformation();
789 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
790 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
791 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
792 return;
793 }
794
795 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
796 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
797 }
798 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000799 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000800 }
801}
802
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000803void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000804 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000805}
806
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000807void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700808 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000809 if (kIsDebugBuild) {
810 __ Comment("Unreachable");
811 __ int3();
812 }
813}
814
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000815void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100816 LocationSummary* locations =
817 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100818 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100819 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100820 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100821 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000822}
823
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000824void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700825 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100826 if (cond->IsIntConstant()) {
827 // Constant condition, statically compared against 1.
828 int32_t cond_value = cond->AsIntConstant()->GetValue();
829 if (cond_value == 1) {
830 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
831 if_instr->IfTrueSuccessor())) {
832 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100833 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100834 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100835 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100836 DCHECK_EQ(cond_value, 0);
837 }
838 } else {
839 bool materialized =
840 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
841 // Moves do not affect the eflags register, so if the condition is
842 // evaluated just before the if, we don't need to evaluate it
843 // again.
844 bool eflags_set = cond->IsCondition()
845 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
846 if (materialized) {
847 if (!eflags_set) {
848 // Materialized condition, compare against 0.
849 Location lhs = if_instr->GetLocations()->InAt(0);
850 if (lhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000851 __ cmpl(lhs.AsRegister<Register>(), Immediate(0));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100852 } else {
853 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
854 }
855 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
856 } else {
857 __ j(X86Condition(cond->AsCondition()->GetCondition()),
858 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
859 }
860 } else {
861 Location lhs = cond->GetLocations()->InAt(0);
862 Location rhs = cond->GetLocations()->InAt(1);
863 // LHS is guaranteed to be in a register (see
864 // LocationsBuilderX86::VisitCondition).
865 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000866 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100867 } else if (rhs.IsConstant()) {
868 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
869 Immediate imm(instruction->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000870 __ cmpl(lhs.AsRegister<Register>(), imm);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100871 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000872 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100873 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100874 __ j(X86Condition(cond->AsCondition()->GetCondition()),
875 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700876 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100877 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100878 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
879 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700880 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000881 }
882}
883
884void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000885 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000886}
887
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000888void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
889 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000890}
891
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000892void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100893 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000894}
895
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000896void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100897 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700898 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000899}
900
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100901void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100902 LocationSummary* locations =
903 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100904 switch (store->InputAt(1)->GetType()) {
905 case Primitive::kPrimBoolean:
906 case Primitive::kPrimByte:
907 case Primitive::kPrimChar:
908 case Primitive::kPrimShort:
909 case Primitive::kPrimInt:
910 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100911 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100912 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
913 break;
914
915 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100916 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100917 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
918 break;
919
920 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100921 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100922 }
923 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000924}
925
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000926void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700927 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000928}
929
Dave Allison20dfc792014-06-16 20:44:29 -0700930void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100931 LocationSummary* locations =
932 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100933 locations->SetInAt(0, Location::RequiresRegister());
934 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100935 if (comp->NeedsMaterialization()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000936 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100937 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000938}
939
Dave Allison20dfc792014-06-16 20:44:29 -0700940void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
941 if (comp->NeedsMaterialization()) {
942 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000943 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100944 // Clear register: setcc only sets the low byte.
945 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700946 if (locations->InAt(1).IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000947 __ cmpl(locations->InAt(0).AsRegister<Register>(),
948 locations->InAt(1).AsRegister<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100949 } else if (locations->InAt(1).IsConstant()) {
950 HConstant* instruction = locations->InAt(1).GetConstant();
951 Immediate imm(instruction->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000952 __ cmpl(locations->InAt(0).AsRegister<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700953 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000954 __ cmpl(locations->InAt(0).AsRegister<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700955 Address(ESP, locations->InAt(1).GetStackIndex()));
956 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000957 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100958 }
Dave Allison20dfc792014-06-16 20:44:29 -0700959}
960
961void LocationsBuilderX86::VisitEqual(HEqual* comp) {
962 VisitCondition(comp);
963}
964
965void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
966 VisitCondition(comp);
967}
968
969void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
970 VisitCondition(comp);
971}
972
973void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
974 VisitCondition(comp);
975}
976
977void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
978 VisitCondition(comp);
979}
980
981void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
982 VisitCondition(comp);
983}
984
985void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
986 VisitCondition(comp);
987}
988
989void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
990 VisitCondition(comp);
991}
992
993void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
994 VisitCondition(comp);
995}
996
997void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
998 VisitCondition(comp);
999}
1000
1001void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1002 VisitCondition(comp);
1003}
1004
1005void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1006 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001007}
1008
1009void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001010 LocationSummary* locations =
1011 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001012 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001013}
1014
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001015void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001016 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001017 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001018}
1019
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001020void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001021 LocationSummary* locations =
1022 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001023 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001024}
1025
1026void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1027 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001028 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001029}
1030
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001031void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1032 LocationSummary* locations =
1033 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1034 locations->SetOut(Location::ConstantLocation(constant));
1035}
1036
1037void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1038 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001039 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001040}
1041
1042void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1043 LocationSummary* locations =
1044 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1045 locations->SetOut(Location::ConstantLocation(constant));
1046}
1047
1048void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1049 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001050 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001051}
1052
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001053void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001054 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001055}
1056
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001057void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001058 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001059 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001060 __ ret();
1061}
1062
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001063void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001064 LocationSummary* locations =
1065 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001066 switch (ret->InputAt(0)->GetType()) {
1067 case Primitive::kPrimBoolean:
1068 case Primitive::kPrimByte:
1069 case Primitive::kPrimChar:
1070 case Primitive::kPrimShort:
1071 case Primitive::kPrimInt:
1072 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001073 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001074 break;
1075
1076 case Primitive::kPrimLong:
1077 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001078 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001079 break;
1080
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001081 case Primitive::kPrimFloat:
1082 case Primitive::kPrimDouble:
1083 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001084 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001085 break;
1086
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001087 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001088 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001089 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001090}
1091
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001092void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001093 if (kIsDebugBuild) {
1094 switch (ret->InputAt(0)->GetType()) {
1095 case Primitive::kPrimBoolean:
1096 case Primitive::kPrimByte:
1097 case Primitive::kPrimChar:
1098 case Primitive::kPrimShort:
1099 case Primitive::kPrimInt:
1100 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001101 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001102 break;
1103
1104 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001105 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1106 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001107 break;
1108
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001109 case Primitive::kPrimFloat:
1110 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001111 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001112 break;
1113
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001114 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001115 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001116 }
1117 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001118 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001119 __ ret();
1120}
1121
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001122void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001123 HandleInvoke(invoke);
1124}
1125
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001126void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001127 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001128
1129 // TODO: Implement all kinds of calls:
1130 // 1) boot -> boot
1131 // 2) app -> boot
1132 // 3) app -> app
1133 //
1134 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1135
1136 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001137 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffray4e44c822014-12-17 12:25:12 +00001138 // temp = temp->dex_cache_resolved_methods_;
1139 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
1140 // temp = temp[index_in_cache]
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001141 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001142 // (temp + offset_of_quick_compiled_code)()
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001143 __ call(Address(
1144 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001145
1146 DCHECK(!codegen_->IsLeafMethod());
1147 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1148}
1149
1150void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1151 HandleInvoke(invoke);
1152}
1153
1154void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001155 LocationSummary* locations =
1156 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001157 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001158
1159 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001160 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001161 HInstruction* input = invoke->InputAt(i);
1162 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1163 }
1164
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001165 switch (invoke->GetType()) {
1166 case Primitive::kPrimBoolean:
1167 case Primitive::kPrimByte:
1168 case Primitive::kPrimChar:
1169 case Primitive::kPrimShort:
1170 case Primitive::kPrimInt:
1171 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001172 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001173 break;
1174
1175 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001176 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001177 break;
1178
1179 case Primitive::kPrimVoid:
1180 break;
1181
1182 case Primitive::kPrimDouble:
1183 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001184 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001185 break;
1186 }
1187
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001188 invoke->SetLocations(locations);
1189}
1190
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001191void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001192 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001193 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1194 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1195 LocationSummary* locations = invoke->GetLocations();
1196 Location receiver = locations->InAt(0);
1197 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1198 // temp = object->GetClass();
1199 if (receiver.IsStackSlot()) {
1200 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1201 __ movl(temp, Address(temp, class_offset));
1202 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001203 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001204 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001205 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001206 // temp = temp->GetMethodAt(method_offset);
1207 __ movl(temp, Address(temp, method_offset));
1208 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001209 __ call(Address(
1210 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001211
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001212 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001213 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001214}
1215
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001216void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1217 HandleInvoke(invoke);
1218 // Add the hidden argument.
1219 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM0));
1220}
1221
1222void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1223 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001224 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001225 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1226 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1227 LocationSummary* locations = invoke->GetLocations();
1228 Location receiver = locations->InAt(0);
1229 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1230
1231 // Set the hidden argument.
1232 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001233 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001234
1235 // temp = object->GetClass();
1236 if (receiver.IsStackSlot()) {
1237 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1238 __ movl(temp, Address(temp, class_offset));
1239 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001240 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001241 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001242 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001243 // temp = temp->GetImtEntryAt(method_offset);
1244 __ movl(temp, Address(temp, method_offset));
1245 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001246 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001247 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001248
1249 DCHECK(!codegen_->IsLeafMethod());
1250 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1251}
1252
Roland Levillain88cb1752014-10-20 16:36:47 +01001253void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1254 LocationSummary* locations =
1255 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1256 switch (neg->GetResultType()) {
1257 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001258 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001259 locations->SetInAt(0, Location::RequiresRegister());
1260 locations->SetOut(Location::SameAsFirstInput());
1261 break;
1262
Roland Levillain88cb1752014-10-20 16:36:47 +01001263 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001264 locations->SetInAt(0, Location::RequiresFpuRegister());
1265 locations->SetOut(Location::SameAsFirstInput());
1266 locations->AddTemp(Location::RequiresRegister());
1267 locations->AddTemp(Location::RequiresFpuRegister());
1268 break;
1269
Roland Levillain88cb1752014-10-20 16:36:47 +01001270 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001271 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001272 locations->SetOut(Location::SameAsFirstInput());
1273 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001274 break;
1275
1276 default:
1277 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1278 }
1279}
1280
1281void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1282 LocationSummary* locations = neg->GetLocations();
1283 Location out = locations->Out();
1284 Location in = locations->InAt(0);
1285 switch (neg->GetResultType()) {
1286 case Primitive::kPrimInt:
1287 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001288 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001289 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001290 break;
1291
1292 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001293 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001294 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001295 __ negl(out.AsRegisterPairLow<Register>());
1296 // Negation is similar to subtraction from zero. The least
1297 // significant byte triggers a borrow when it is different from
1298 // zero; to take it into account, add 1 to the most significant
1299 // byte if the carry flag (CF) is set to 1 after the first NEGL
1300 // operation.
1301 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1302 __ negl(out.AsRegisterPairHigh<Register>());
1303 break;
1304
Roland Levillain5368c212014-11-27 15:03:41 +00001305 case Primitive::kPrimFloat: {
1306 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001307 Register constant = locations->GetTemp(0).AsRegister<Register>();
1308 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001309 // Implement float negation with an exclusive or with value
1310 // 0x80000000 (mask for bit 31, representing the sign of a
1311 // single-precision floating-point number).
1312 __ movl(constant, Immediate(INT32_C(0x80000000)));
1313 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001314 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001315 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001316 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001317
Roland Levillain5368c212014-11-27 15:03:41 +00001318 case Primitive::kPrimDouble: {
1319 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001320 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001321 // Implement double negation with an exclusive or with value
1322 // 0x8000000000000000 (mask for bit 63, representing the sign of
1323 // a double-precision floating-point number).
1324 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001325 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001326 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001327 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001328
1329 default:
1330 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1331 }
1332}
1333
Roland Levillaindff1f282014-11-05 14:15:05 +00001334void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001335 Primitive::Type result_type = conversion->GetResultType();
1336 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001337 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001338
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001339 // The float-to-long and double-to-long type conversions rely on a
1340 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001341 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001342 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1343 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001344 ? LocationSummary::kCall
1345 : LocationSummary::kNoCall;
1346 LocationSummary* locations =
1347 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1348
Roland Levillaindff1f282014-11-05 14:15:05 +00001349 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001350 case Primitive::kPrimByte:
1351 switch (input_type) {
1352 case Primitive::kPrimShort:
1353 case Primitive::kPrimInt:
1354 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001355 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001356 locations->SetInAt(0, Location::Any());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001357 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1358 break;
1359
1360 default:
1361 LOG(FATAL) << "Unexpected type conversion from " << input_type
1362 << " to " << result_type;
1363 }
1364 break;
1365
Roland Levillain01a8d712014-11-14 16:27:39 +00001366 case Primitive::kPrimShort:
1367 switch (input_type) {
1368 case Primitive::kPrimByte:
1369 case Primitive::kPrimInt:
1370 case Primitive::kPrimChar:
1371 // Processing a Dex `int-to-short' instruction.
1372 locations->SetInAt(0, Location::Any());
1373 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1374 break;
1375
1376 default:
1377 LOG(FATAL) << "Unexpected type conversion from " << input_type
1378 << " to " << result_type;
1379 }
1380 break;
1381
Roland Levillain946e1432014-11-11 17:35:19 +00001382 case Primitive::kPrimInt:
1383 switch (input_type) {
1384 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001385 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001386 locations->SetInAt(0, Location::Any());
1387 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1388 break;
1389
1390 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001391 // Processing a Dex `float-to-int' instruction.
1392 locations->SetInAt(0, Location::RequiresFpuRegister());
1393 locations->SetOut(Location::RequiresRegister());
1394 locations->AddTemp(Location::RequiresFpuRegister());
1395 break;
1396
Roland Levillain946e1432014-11-11 17:35:19 +00001397 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001398 // Processing a Dex `double-to-int' instruction.
1399 locations->SetInAt(0, Location::RequiresFpuRegister());
1400 locations->SetOut(Location::RequiresRegister());
1401 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001402 break;
1403
1404 default:
1405 LOG(FATAL) << "Unexpected type conversion from " << input_type
1406 << " to " << result_type;
1407 }
1408 break;
1409
Roland Levillaindff1f282014-11-05 14:15:05 +00001410 case Primitive::kPrimLong:
1411 switch (input_type) {
1412 case Primitive::kPrimByte:
1413 case Primitive::kPrimShort:
1414 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001415 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001416 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001417 locations->SetInAt(0, Location::RegisterLocation(EAX));
1418 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1419 break;
1420
Roland Levillain624279f2014-12-04 11:54:28 +00001421 case Primitive::kPrimFloat: {
1422 // Processing a Dex `float-to-long' instruction.
1423 InvokeRuntimeCallingConvention calling_convention;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001424 // Note that on x86 floating-point parameters are passed
1425 // through core registers (here, EAX).
1426 locations->SetInAt(0, Location::RegisterLocation(
1427 calling_convention.GetRegisterAt(0)));
Roland Levillain624279f2014-12-04 11:54:28 +00001428 // The runtime helper puts the result in EAX, EDX.
1429 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1430 break;
1431 }
1432
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001433 case Primitive::kPrimDouble: {
1434 // Processing a Dex `double-to-long' instruction.
1435 InvokeRuntimeCallingConvention calling_convention;
1436 // Note that on x86 floating-point parameters are passed
1437 // through core registers (here, EAX and ECX).
1438 locations->SetInAt(0, Location::RegisterPairLocation(
1439 calling_convention.GetRegisterAt(0),
1440 calling_convention.GetRegisterAt(1)));
1441 // The runtime helper puts the result in EAX, EDX.
1442 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1443 break;
1444 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001445 break;
1446
1447 default:
1448 LOG(FATAL) << "Unexpected type conversion from " << input_type
1449 << " to " << result_type;
1450 }
1451 break;
1452
Roland Levillain981e4542014-11-14 11:47:14 +00001453 case Primitive::kPrimChar:
1454 switch (input_type) {
1455 case Primitive::kPrimByte:
1456 case Primitive::kPrimShort:
1457 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001458 // Processing a Dex `int-to-char' instruction.
1459 locations->SetInAt(0, Location::Any());
1460 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1461 break;
1462
1463 default:
1464 LOG(FATAL) << "Unexpected type conversion from " << input_type
1465 << " to " << result_type;
1466 }
1467 break;
1468
Roland Levillaindff1f282014-11-05 14:15:05 +00001469 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001470 switch (input_type) {
1471 case Primitive::kPrimByte:
1472 case Primitive::kPrimShort:
1473 case Primitive::kPrimInt:
1474 case Primitive::kPrimChar:
1475 // Processing a Dex `int-to-float' instruction.
1476 locations->SetInAt(0, Location::RequiresRegister());
1477 locations->SetOut(Location::RequiresFpuRegister());
1478 break;
1479
1480 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001481 // Processing a Dex `long-to-float' instruction.
1482 locations->SetInAt(0, Location::RequiresRegister());
1483 locations->SetOut(Location::RequiresFpuRegister());
1484 locations->AddTemp(Location::RequiresFpuRegister());
1485 locations->AddTemp(Location::RequiresFpuRegister());
1486 break;
1487
Roland Levillaincff13742014-11-17 14:32:17 +00001488 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001489 // Processing a Dex `double-to-float' instruction.
1490 locations->SetInAt(0, Location::RequiresFpuRegister());
1491 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001492 break;
1493
1494 default:
1495 LOG(FATAL) << "Unexpected type conversion from " << input_type
1496 << " to " << result_type;
1497 };
1498 break;
1499
Roland Levillaindff1f282014-11-05 14:15:05 +00001500 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001501 switch (input_type) {
1502 case Primitive::kPrimByte:
1503 case Primitive::kPrimShort:
1504 case Primitive::kPrimInt:
1505 case Primitive::kPrimChar:
1506 // Processing a Dex `int-to-double' instruction.
1507 locations->SetInAt(0, Location::RequiresRegister());
1508 locations->SetOut(Location::RequiresFpuRegister());
1509 break;
1510
1511 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001512 // Processing a Dex `long-to-double' instruction.
1513 locations->SetInAt(0, Location::RequiresRegister());
1514 locations->SetOut(Location::RequiresFpuRegister());
1515 locations->AddTemp(Location::RequiresFpuRegister());
1516 locations->AddTemp(Location::RequiresFpuRegister());
1517 break;
1518
Roland Levillaincff13742014-11-17 14:32:17 +00001519 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001520 // Processing a Dex `float-to-double' instruction.
1521 locations->SetInAt(0, Location::RequiresFpuRegister());
1522 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001523 break;
1524
1525 default:
1526 LOG(FATAL) << "Unexpected type conversion from " << input_type
1527 << " to " << result_type;
1528 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001529 break;
1530
1531 default:
1532 LOG(FATAL) << "Unexpected type conversion from " << input_type
1533 << " to " << result_type;
1534 }
1535}
1536
1537void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1538 LocationSummary* locations = conversion->GetLocations();
1539 Location out = locations->Out();
1540 Location in = locations->InAt(0);
1541 Primitive::Type result_type = conversion->GetResultType();
1542 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001543 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001544 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001545 case Primitive::kPrimByte:
1546 switch (input_type) {
1547 case Primitive::kPrimShort:
1548 case Primitive::kPrimInt:
1549 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001550 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001551 if (in.IsRegister()) {
1552 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
1553 } else if (in.IsStackSlot()) {
1554 __ movsxb(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
1555 } else {
1556 DCHECK(in.GetConstant()->IsIntConstant());
1557 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1558 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1559 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001560 break;
1561
1562 default:
1563 LOG(FATAL) << "Unexpected type conversion from " << input_type
1564 << " to " << result_type;
1565 }
1566 break;
1567
Roland Levillain01a8d712014-11-14 16:27:39 +00001568 case Primitive::kPrimShort:
1569 switch (input_type) {
1570 case Primitive::kPrimByte:
1571 case Primitive::kPrimInt:
1572 case Primitive::kPrimChar:
1573 // Processing a Dex `int-to-short' instruction.
1574 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001575 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001576 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001577 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001578 } else {
1579 DCHECK(in.GetConstant()->IsIntConstant());
1580 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001581 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001582 }
1583 break;
1584
1585 default:
1586 LOG(FATAL) << "Unexpected type conversion from " << input_type
1587 << " to " << result_type;
1588 }
1589 break;
1590
Roland Levillain946e1432014-11-11 17:35:19 +00001591 case Primitive::kPrimInt:
1592 switch (input_type) {
1593 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001594 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001595 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001596 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001597 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001598 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001599 } else {
1600 DCHECK(in.IsConstant());
1601 DCHECK(in.GetConstant()->IsLongConstant());
1602 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001603 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001604 }
1605 break;
1606
Roland Levillain3f8f9362014-12-02 17:45:01 +00001607 case Primitive::kPrimFloat: {
1608 // Processing a Dex `float-to-int' instruction.
1609 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1610 Register output = out.AsRegister<Register>();
1611 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1612 Label done, nan;
1613
1614 __ movl(output, Immediate(kPrimIntMax));
1615 // temp = int-to-float(output)
1616 __ cvtsi2ss(temp, output);
1617 // if input >= temp goto done
1618 __ comiss(input, temp);
1619 __ j(kAboveEqual, &done);
1620 // if input == NaN goto nan
1621 __ j(kUnordered, &nan);
1622 // output = float-to-int-truncate(input)
1623 __ cvttss2si(output, input);
1624 __ jmp(&done);
1625 __ Bind(&nan);
1626 // output = 0
1627 __ xorl(output, output);
1628 __ Bind(&done);
1629 break;
1630 }
1631
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001632 case Primitive::kPrimDouble: {
1633 // Processing a Dex `double-to-int' instruction.
1634 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1635 Register output = out.AsRegister<Register>();
1636 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1637 Label done, nan;
1638
1639 __ movl(output, Immediate(kPrimIntMax));
1640 // temp = int-to-double(output)
1641 __ cvtsi2sd(temp, output);
1642 // if input >= temp goto done
1643 __ comisd(input, temp);
1644 __ j(kAboveEqual, &done);
1645 // if input == NaN goto nan
1646 __ j(kUnordered, &nan);
1647 // output = double-to-int-truncate(input)
1648 __ cvttsd2si(output, input);
1649 __ jmp(&done);
1650 __ Bind(&nan);
1651 // output = 0
1652 __ xorl(output, output);
1653 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001654 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001655 }
Roland Levillain946e1432014-11-11 17:35:19 +00001656
1657 default:
1658 LOG(FATAL) << "Unexpected type conversion from " << input_type
1659 << " to " << result_type;
1660 }
1661 break;
1662
Roland Levillaindff1f282014-11-05 14:15:05 +00001663 case Primitive::kPrimLong:
1664 switch (input_type) {
1665 case Primitive::kPrimByte:
1666 case Primitive::kPrimShort:
1667 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001668 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001669 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001670 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1671 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001672 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001673 __ cdq();
1674 break;
1675
1676 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001677 // Processing a Dex `float-to-long' instruction.
1678 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001679 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1680 break;
1681
Roland Levillaindff1f282014-11-05 14:15:05 +00001682 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001683 // Processing a Dex `double-to-long' instruction.
1684 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1685 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001686 break;
1687
1688 default:
1689 LOG(FATAL) << "Unexpected type conversion from " << input_type
1690 << " to " << result_type;
1691 }
1692 break;
1693
Roland Levillain981e4542014-11-14 11:47:14 +00001694 case Primitive::kPrimChar:
1695 switch (input_type) {
1696 case Primitive::kPrimByte:
1697 case Primitive::kPrimShort:
1698 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001699 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1700 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001701 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001702 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001703 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001704 } else {
1705 DCHECK(in.GetConstant()->IsIntConstant());
1706 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001707 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001708 }
1709 break;
1710
1711 default:
1712 LOG(FATAL) << "Unexpected type conversion from " << input_type
1713 << " to " << result_type;
1714 }
1715 break;
1716
Roland Levillaindff1f282014-11-05 14:15:05 +00001717 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001718 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001719 case Primitive::kPrimByte:
1720 case Primitive::kPrimShort:
1721 case Primitive::kPrimInt:
1722 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001723 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001724 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001725 break;
1726
Roland Levillain6d0e4832014-11-27 18:31:21 +00001727 case Primitive::kPrimLong: {
1728 // Processing a Dex `long-to-float' instruction.
1729 Register low = in.AsRegisterPairLow<Register>();
1730 Register high = in.AsRegisterPairHigh<Register>();
1731 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1732 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1733 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1734
1735 // Operations use doubles for precision reasons (each 32-bit
1736 // half of a long fits in the 53-bit mantissa of a double,
1737 // but not in the 24-bit mantissa of a float). This is
1738 // especially important for the low bits. The result is
1739 // eventually converted to float.
1740
1741 // low = low - 2^31 (to prevent bit 31 of `low` to be
1742 // interpreted as a sign bit)
1743 __ subl(low, Immediate(0x80000000));
1744 // temp = int-to-double(high)
1745 __ cvtsi2sd(temp, high);
1746 // temp = temp * 2^32
1747 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
1748 __ mulsd(temp, constant);
1749 // result = int-to-double(low)
1750 __ cvtsi2sd(result, low);
1751 // result = result + 2^31 (restore the original value of `low`)
1752 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
1753 __ addsd(result, constant);
1754 // result = result + temp
1755 __ addsd(result, temp);
1756 // result = double-to-float(result)
1757 __ cvtsd2ss(result, result);
1758 break;
1759 }
1760
Roland Levillaincff13742014-11-17 14:32:17 +00001761 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001762 // Processing a Dex `double-to-float' instruction.
1763 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001764 break;
1765
1766 default:
1767 LOG(FATAL) << "Unexpected type conversion from " << input_type
1768 << " to " << result_type;
1769 };
1770 break;
1771
Roland Levillaindff1f282014-11-05 14:15:05 +00001772 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001773 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001774 case Primitive::kPrimByte:
1775 case Primitive::kPrimShort:
1776 case Primitive::kPrimInt:
1777 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001778 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001779 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001780 break;
1781
Roland Levillain647b9ed2014-11-27 12:06:00 +00001782 case Primitive::kPrimLong: {
1783 // Processing a Dex `long-to-double' instruction.
1784 Register low = in.AsRegisterPairLow<Register>();
1785 Register high = in.AsRegisterPairHigh<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001786 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1787 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1788 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001789
Roland Levillain647b9ed2014-11-27 12:06:00 +00001790 // low = low - 2^31 (to prevent bit 31 of `low` to be
1791 // interpreted as a sign bit)
1792 __ subl(low, Immediate(0x80000000));
1793 // temp = int-to-double(high)
1794 __ cvtsi2sd(temp, high);
1795 // temp = temp * 2^32
Roland Levillain6d0e4832014-11-27 18:31:21 +00001796 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001797 __ mulsd(temp, constant);
1798 // result = int-to-double(low)
1799 __ cvtsi2sd(result, low);
1800 // result = result + 2^31 (restore the original value of `low`)
Roland Levillain6d0e4832014-11-27 18:31:21 +00001801 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001802 __ addsd(result, constant);
1803 // result = result + temp
1804 __ addsd(result, temp);
1805 break;
1806 }
1807
Roland Levillaincff13742014-11-17 14:32:17 +00001808 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001809 // Processing a Dex `float-to-double' instruction.
1810 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001811 break;
1812
1813 default:
1814 LOG(FATAL) << "Unexpected type conversion from " << input_type
1815 << " to " << result_type;
1816 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001817 break;
1818
1819 default:
1820 LOG(FATAL) << "Unexpected type conversion from " << input_type
1821 << " to " << result_type;
1822 }
1823}
1824
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001825void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001826 LocationSummary* locations =
1827 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001828 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001829 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001830 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001831 locations->SetInAt(0, Location::RequiresRegister());
1832 locations->SetInAt(1, Location::Any());
1833 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001834 break;
1835 }
1836
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001837 case Primitive::kPrimFloat:
1838 case Primitive::kPrimDouble: {
1839 locations->SetInAt(0, Location::RequiresFpuRegister());
1840 locations->SetInAt(1, Location::Any());
1841 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001842 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001843 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001844
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001845 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001846 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1847 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001848 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001849}
1850
1851void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1852 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001853 Location first = locations->InAt(0);
1854 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001855 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001856 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001857 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001858 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001859 __ addl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001860 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001861 __ addl(first.AsRegister<Register>(),
1862 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001863 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001864 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001865 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001866 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001867 }
1868
1869 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001870 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001871 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1872 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001873 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001874 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1875 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001876 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001877 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001878 break;
1879 }
1880
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001881 case Primitive::kPrimFloat: {
1882 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001883 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001884 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001885 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001886 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001887 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001888 }
1889
1890 case Primitive::kPrimDouble: {
1891 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001892 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001893 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001894 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001895 }
1896 break;
1897 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001898
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001899 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001900 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001901 }
1902}
1903
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001904void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001905 LocationSummary* locations =
1906 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001907 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001908 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001909 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001910 locations->SetInAt(0, Location::RequiresRegister());
1911 locations->SetInAt(1, Location::Any());
1912 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001913 break;
1914 }
Calin Juravle11351682014-10-23 15:38:15 +01001915 case Primitive::kPrimFloat:
1916 case Primitive::kPrimDouble: {
1917 locations->SetInAt(0, Location::RequiresFpuRegister());
1918 locations->SetInAt(1, Location::RequiresFpuRegister());
1919 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001920 break;
Calin Juravle11351682014-10-23 15:38:15 +01001921 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001922
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001923 default:
Calin Juravle11351682014-10-23 15:38:15 +01001924 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001925 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001926}
1927
1928void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1929 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001930 Location first = locations->InAt(0);
1931 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001932 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001933 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001934 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001935 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001936 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001937 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001938 __ subl(first.AsRegister<Register>(),
1939 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001940 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001941 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001942 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001943 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001944 }
1945
1946 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001947 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01001948 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1949 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001950 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001951 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001952 __ sbbl(first.AsRegisterPairHigh<Register>(),
1953 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001954 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001955 break;
1956 }
1957
Calin Juravle11351682014-10-23 15:38:15 +01001958 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001959 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001960 break;
Calin Juravle11351682014-10-23 15:38:15 +01001961 }
1962
1963 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001964 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001965 break;
1966 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001967
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001968 default:
Calin Juravle11351682014-10-23 15:38:15 +01001969 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001970 }
1971}
1972
Calin Juravle34bacdf2014-10-07 20:23:36 +01001973void LocationsBuilderX86::VisitMul(HMul* mul) {
1974 LocationSummary* locations =
1975 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1976 switch (mul->GetResultType()) {
1977 case Primitive::kPrimInt:
1978 locations->SetInAt(0, Location::RequiresRegister());
1979 locations->SetInAt(1, Location::Any());
1980 locations->SetOut(Location::SameAsFirstInput());
1981 break;
1982 case Primitive::kPrimLong: {
1983 locations->SetInAt(0, Location::RequiresRegister());
1984 // TODO: Currently this handles only stack operands:
1985 // - we don't have enough registers because we currently use Quick ABI.
1986 // - by the time we have a working register allocator we will probably change the ABI
1987 // and fix the above.
1988 // - we don't have a way yet to request operands on stack but the base line compiler
1989 // will leave the operands on the stack with Any().
1990 locations->SetInAt(1, Location::Any());
1991 locations->SetOut(Location::SameAsFirstInput());
1992 // Needed for imul on 32bits with 64bits output.
1993 locations->AddTemp(Location::RegisterLocation(EAX));
1994 locations->AddTemp(Location::RegisterLocation(EDX));
1995 break;
1996 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001997 case Primitive::kPrimFloat:
1998 case Primitive::kPrimDouble: {
1999 locations->SetInAt(0, Location::RequiresFpuRegister());
2000 locations->SetInAt(1, Location::RequiresFpuRegister());
2001 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002002 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002003 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002004
2005 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002006 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002007 }
2008}
2009
2010void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2011 LocationSummary* locations = mul->GetLocations();
2012 Location first = locations->InAt(0);
2013 Location second = locations->InAt(1);
2014 DCHECK(first.Equals(locations->Out()));
2015
2016 switch (mul->GetResultType()) {
2017 case Primitive::kPrimInt: {
2018 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002019 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002020 } else if (second.IsConstant()) {
2021 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002022 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002023 } else {
2024 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002025 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002026 }
2027 break;
2028 }
2029
2030 case Primitive::kPrimLong: {
2031 DCHECK(second.IsDoubleStackSlot());
2032
2033 Register in1_hi = first.AsRegisterPairHigh<Register>();
2034 Register in1_lo = first.AsRegisterPairLow<Register>();
2035 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2036 Address in2_lo(ESP, second.GetStackIndex());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002037 Register eax = locations->GetTemp(0).AsRegister<Register>();
2038 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002039
2040 DCHECK_EQ(EAX, eax);
2041 DCHECK_EQ(EDX, edx);
2042
2043 // input: in1 - 64 bits, in2 - 64 bits
2044 // output: in1
2045 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2046 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2047 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
2048
2049 __ movl(eax, in2_hi);
2050 // eax <- in1.lo * in2.hi
2051 __ imull(eax, in1_lo);
2052 // in1.hi <- in1.hi * in2.lo
2053 __ imull(in1_hi, in2_lo);
2054 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2055 __ addl(in1_hi, eax);
2056 // move in1_lo to eax to prepare for double precision
2057 __ movl(eax, in1_lo);
2058 // edx:eax <- in1.lo * in2.lo
2059 __ mull(in2_lo);
2060 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2061 __ addl(in1_hi, edx);
2062 // in1.lo <- (in1.lo * in2.lo)[31:0];
2063 __ movl(in1_lo, eax);
2064
2065 break;
2066 }
2067
Calin Juravleb5bfa962014-10-21 18:02:24 +01002068 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002069 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002070 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002071 }
2072
2073 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002074 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002075 break;
2076 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002077
2078 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002079 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002080 }
2081}
2082
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002083void InstructionCodeGeneratorX86::PushOntoFPStack(Location source, uint32_t temp_offset,
2084 uint32_t stack_adjustment, bool is_float) {
2085 if (source.IsStackSlot()) {
2086 DCHECK(is_float);
2087 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2088 } else if (source.IsDoubleStackSlot()) {
2089 DCHECK(!is_float);
2090 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2091 } else {
2092 // Write the value to the temporary location on the stack and load to FP stack.
2093 if (is_float) {
2094 Location stack_temp = Location::StackSlot(temp_offset);
2095 codegen_->Move32(stack_temp, source);
2096 __ flds(Address(ESP, temp_offset));
2097 } else {
2098 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2099 codegen_->Move64(stack_temp, source);
2100 __ fldl(Address(ESP, temp_offset));
2101 }
2102 }
2103}
2104
2105void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2106 Primitive::Type type = rem->GetResultType();
2107 bool is_float = type == Primitive::kPrimFloat;
2108 size_t elem_size = Primitive::ComponentSize(type);
2109 LocationSummary* locations = rem->GetLocations();
2110 Location first = locations->InAt(0);
2111 Location second = locations->InAt(1);
2112 Location out = locations->Out();
2113
2114 // Create stack space for 2 elements.
2115 // TODO: enhance register allocator to ask for stack temporaries.
2116 __ subl(ESP, Immediate(2 * elem_size));
2117
2118 // Load the values to the FP stack in reverse order, using temporaries if needed.
2119 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2120 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2121
2122 // Loop doing FPREM until we stabilize.
2123 Label retry;
2124 __ Bind(&retry);
2125 __ fprem();
2126
2127 // Move FP status to AX.
2128 __ fstsw();
2129
2130 // And see if the argument reduction is complete. This is signaled by the
2131 // C2 FPU flag bit set to 0.
2132 __ andl(EAX, Immediate(kC2ConditionMask));
2133 __ j(kNotEqual, &retry);
2134
2135 // We have settled on the final value. Retrieve it into an XMM register.
2136 // Store FP top of stack to real stack.
2137 if (is_float) {
2138 __ fsts(Address(ESP, 0));
2139 } else {
2140 __ fstl(Address(ESP, 0));
2141 }
2142
2143 // Pop the 2 items from the FP stack.
2144 __ fucompp();
2145
2146 // Load the value from the stack into an XMM register.
2147 DCHECK(out.IsFpuRegister()) << out;
2148 if (is_float) {
2149 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2150 } else {
2151 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2152 }
2153
2154 // And remove the temporary stack space we allocated.
2155 __ addl(ESP, Immediate(2 * elem_size));
2156}
2157
Calin Juravlebacfec32014-11-14 15:54:36 +00002158void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2159 DCHECK(instruction->IsDiv() || instruction->IsRem());
2160
2161 LocationSummary* locations = instruction->GetLocations();
2162 Location out = locations->Out();
2163 Location first = locations->InAt(0);
2164 Location second = locations->InAt(1);
2165 bool is_div = instruction->IsDiv();
2166
2167 switch (instruction->GetResultType()) {
2168 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002169 Register second_reg = second.AsRegister<Register>();
2170 DCHECK_EQ(EAX, first.AsRegister<Register>());
2171 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002172
2173 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002174 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
2175 is_div);
Calin Juravlebacfec32014-11-14 15:54:36 +00002176 codegen_->AddSlowPath(slow_path);
2177
2178 // 0x80000000/-1 triggers an arithmetic exception!
2179 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2180 // it's safe to just use negl instead of more complex comparisons.
2181
2182 __ cmpl(second_reg, Immediate(-1));
2183 __ j(kEqual, slow_path->GetEntryLabel());
2184
2185 // edx:eax <- sign-extended of eax
2186 __ cdq();
2187 // eax = quotient, edx = remainder
2188 __ idivl(second_reg);
2189
2190 __ Bind(slow_path->GetExitLabel());
2191 break;
2192 }
2193
2194 case Primitive::kPrimLong: {
2195 InvokeRuntimeCallingConvention calling_convention;
2196 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2197 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2198 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2199 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2200 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2201 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2202
2203 if (is_div) {
2204 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2205 } else {
2206 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2207 }
2208 uint32_t dex_pc = is_div
2209 ? instruction->AsDiv()->GetDexPc()
2210 : instruction->AsRem()->GetDexPc();
2211 codegen_->RecordPcInfo(instruction, dex_pc);
2212
2213 break;
2214 }
2215
2216 default:
2217 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2218 }
2219}
2220
Calin Juravle7c4954d2014-10-28 16:57:40 +00002221void LocationsBuilderX86::VisitDiv(HDiv* div) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002222 LocationSummary::CallKind call_kind = div->GetResultType() == Primitive::kPrimLong
2223 ? LocationSummary::kCall
2224 : LocationSummary::kNoCall;
2225 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2226
Calin Juravle7c4954d2014-10-28 16:57:40 +00002227 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002228 case Primitive::kPrimInt: {
2229 locations->SetInAt(0, Location::RegisterLocation(EAX));
2230 locations->SetInAt(1, Location::RequiresRegister());
2231 locations->SetOut(Location::SameAsFirstInput());
2232 // Intel uses edx:eax as the dividend.
2233 locations->AddTemp(Location::RegisterLocation(EDX));
2234 break;
2235 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002236 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002237 InvokeRuntimeCallingConvention calling_convention;
2238 locations->SetInAt(0, Location::RegisterPairLocation(
2239 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2240 locations->SetInAt(1, Location::RegisterPairLocation(
2241 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2242 // Runtime helper puts the result in EAX, EDX.
2243 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002244 break;
2245 }
2246 case Primitive::kPrimFloat:
2247 case Primitive::kPrimDouble: {
2248 locations->SetInAt(0, Location::RequiresFpuRegister());
2249 locations->SetInAt(1, Location::RequiresFpuRegister());
2250 locations->SetOut(Location::SameAsFirstInput());
2251 break;
2252 }
2253
2254 default:
2255 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2256 }
2257}
2258
2259void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2260 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002261 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002262 Location first = locations->InAt(0);
2263 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002264
2265 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002266 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002267 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002268 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002269 break;
2270 }
2271
2272 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002273 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002274 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002275 break;
2276 }
2277
2278 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002279 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002280 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002281 break;
2282 }
2283
2284 default:
2285 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2286 }
2287}
2288
Calin Juravlebacfec32014-11-14 15:54:36 +00002289void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002290 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002291 LocationSummary* locations =
2292 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravlebacfec32014-11-14 15:54:36 +00002293
Calin Juravled2ec87d2014-12-08 14:24:46 +00002294 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002295 case Primitive::kPrimInt: {
2296 locations->SetInAt(0, Location::RegisterLocation(EAX));
2297 locations->SetInAt(1, Location::RequiresRegister());
2298 locations->SetOut(Location::RegisterLocation(EDX));
2299 break;
2300 }
2301 case Primitive::kPrimLong: {
2302 InvokeRuntimeCallingConvention calling_convention;
2303 locations->SetInAt(0, Location::RegisterPairLocation(
2304 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2305 locations->SetInAt(1, Location::RegisterPairLocation(
2306 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2307 // Runtime helper puts the result in EAX, EDX.
2308 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2309 break;
2310 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002311 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002312 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002313 locations->SetInAt(0, Location::Any());
2314 locations->SetInAt(1, Location::Any());
2315 locations->SetOut(Location::RequiresFpuRegister());
2316 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002317 break;
2318 }
2319
2320 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002321 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002322 }
2323}
2324
2325void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2326 Primitive::Type type = rem->GetResultType();
2327 switch (type) {
2328 case Primitive::kPrimInt:
2329 case Primitive::kPrimLong: {
2330 GenerateDivRemIntegral(rem);
2331 break;
2332 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002333 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002334 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002335 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002336 break;
2337 }
2338 default:
2339 LOG(FATAL) << "Unexpected rem type " << type;
2340 }
2341}
2342
Calin Juravled0d48522014-11-04 16:40:20 +00002343void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2344 LocationSummary* locations =
2345 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002346 switch (instruction->GetType()) {
2347 case Primitive::kPrimInt: {
2348 locations->SetInAt(0, Location::Any());
2349 break;
2350 }
2351 case Primitive::kPrimLong: {
2352 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2353 if (!instruction->IsConstant()) {
2354 locations->AddTemp(Location::RequiresRegister());
2355 }
2356 break;
2357 }
2358 default:
2359 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2360 }
Calin Juravled0d48522014-11-04 16:40:20 +00002361 if (instruction->HasUses()) {
2362 locations->SetOut(Location::SameAsFirstInput());
2363 }
2364}
2365
2366void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2367 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2368 codegen_->AddSlowPath(slow_path);
2369
2370 LocationSummary* locations = instruction->GetLocations();
2371 Location value = locations->InAt(0);
2372
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002373 switch (instruction->GetType()) {
2374 case Primitive::kPrimInt: {
2375 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002376 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002377 __ j(kEqual, slow_path->GetEntryLabel());
2378 } else if (value.IsStackSlot()) {
2379 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2380 __ j(kEqual, slow_path->GetEntryLabel());
2381 } else {
2382 DCHECK(value.IsConstant()) << value;
2383 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2384 __ jmp(slow_path->GetEntryLabel());
2385 }
2386 }
2387 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002388 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002389 case Primitive::kPrimLong: {
2390 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002391 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002392 __ movl(temp, value.AsRegisterPairLow<Register>());
2393 __ orl(temp, value.AsRegisterPairHigh<Register>());
2394 __ j(kEqual, slow_path->GetEntryLabel());
2395 } else {
2396 DCHECK(value.IsConstant()) << value;
2397 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2398 __ jmp(slow_path->GetEntryLabel());
2399 }
2400 }
2401 break;
2402 }
2403 default:
2404 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002405 }
Calin Juravled0d48522014-11-04 16:40:20 +00002406}
2407
Calin Juravle9aec02f2014-11-18 23:06:35 +00002408void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2409 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2410
2411 LocationSummary* locations =
2412 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2413
2414 switch (op->GetResultType()) {
2415 case Primitive::kPrimInt: {
2416 locations->SetInAt(0, Location::RequiresRegister());
2417 // The shift count needs to be in CL.
2418 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2419 locations->SetOut(Location::SameAsFirstInput());
2420 break;
2421 }
2422 case Primitive::kPrimLong: {
2423 locations->SetInAt(0, Location::RequiresRegister());
2424 // The shift count needs to be in CL.
2425 locations->SetInAt(1, Location::RegisterLocation(ECX));
2426 locations->SetOut(Location::SameAsFirstInput());
2427 break;
2428 }
2429 default:
2430 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2431 }
2432}
2433
2434void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2435 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2436
2437 LocationSummary* locations = op->GetLocations();
2438 Location first = locations->InAt(0);
2439 Location second = locations->InAt(1);
2440 DCHECK(first.Equals(locations->Out()));
2441
2442 switch (op->GetResultType()) {
2443 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002444 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002445 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002446 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002447 DCHECK_EQ(ECX, second_reg);
2448 if (op->IsShl()) {
2449 __ shll(first_reg, second_reg);
2450 } else if (op->IsShr()) {
2451 __ sarl(first_reg, second_reg);
2452 } else {
2453 __ shrl(first_reg, second_reg);
2454 }
2455 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002456 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002457 if (op->IsShl()) {
2458 __ shll(first_reg, imm);
2459 } else if (op->IsShr()) {
2460 __ sarl(first_reg, imm);
2461 } else {
2462 __ shrl(first_reg, imm);
2463 }
2464 }
2465 break;
2466 }
2467 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002468 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002469 DCHECK_EQ(ECX, second_reg);
2470 if (op->IsShl()) {
2471 GenerateShlLong(first, second_reg);
2472 } else if (op->IsShr()) {
2473 GenerateShrLong(first, second_reg);
2474 } else {
2475 GenerateUShrLong(first, second_reg);
2476 }
2477 break;
2478 }
2479 default:
2480 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2481 }
2482}
2483
2484void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2485 Label done;
2486 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2487 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2488 __ testl(shifter, Immediate(32));
2489 __ j(kEqual, &done);
2490 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2491 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2492 __ Bind(&done);
2493}
2494
2495void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2496 Label done;
2497 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2498 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2499 __ testl(shifter, Immediate(32));
2500 __ j(kEqual, &done);
2501 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2502 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2503 __ Bind(&done);
2504}
2505
2506void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2507 Label done;
2508 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2509 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2510 __ testl(shifter, Immediate(32));
2511 __ j(kEqual, &done);
2512 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2513 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2514 __ Bind(&done);
2515}
2516
2517void LocationsBuilderX86::VisitShl(HShl* shl) {
2518 HandleShift(shl);
2519}
2520
2521void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2522 HandleShift(shl);
2523}
2524
2525void LocationsBuilderX86::VisitShr(HShr* shr) {
2526 HandleShift(shr);
2527}
2528
2529void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2530 HandleShift(shr);
2531}
2532
2533void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2534 HandleShift(ushr);
2535}
2536
2537void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2538 HandleShift(ushr);
2539}
2540
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002541void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002542 LocationSummary* locations =
2543 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002544 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002545 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002546 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2547 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002548}
2549
2550void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2551 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002552 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002553 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002554
Nicolas Geoffray707c8092014-04-04 10:50:14 +01002555 __ fs()->call(
2556 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002557
Nicolas Geoffray39468442014-09-02 15:17:15 +01002558 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002559 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002560}
2561
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002562void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2563 LocationSummary* locations =
2564 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2565 locations->SetOut(Location::RegisterLocation(EAX));
2566 InvokeRuntimeCallingConvention calling_convention;
2567 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002568 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2569 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002570}
2571
2572void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2573 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002574 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002575 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2576
2577 __ fs()->call(
2578 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
2579
2580 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2581 DCHECK(!codegen_->IsLeafMethod());
2582}
2583
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002584void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002585 LocationSummary* locations =
2586 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002587 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2588 if (location.IsStackSlot()) {
2589 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2590 } else if (location.IsDoubleStackSlot()) {
2591 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002592 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002593 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002594}
2595
2596void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002597 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002598}
2599
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002600void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002601 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002602 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002603 locations->SetInAt(0, Location::RequiresRegister());
2604 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002605}
2606
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002607void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2608 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002609 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002610 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002611 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002612 switch (not_->InputAt(0)->GetType()) {
2613 case Primitive::kPrimBoolean:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002614 __ xorl(out.AsRegister<Register>(), Immediate(1));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002615 break;
2616
2617 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002618 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002619 break;
2620
2621 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002622 __ notl(out.AsRegisterPairLow<Register>());
2623 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002624 break;
2625
2626 default:
2627 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2628 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002629}
2630
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002631void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002632 LocationSummary* locations =
2633 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002634 switch (compare->InputAt(0)->GetType()) {
2635 case Primitive::kPrimLong: {
2636 locations->SetInAt(0, Location::RequiresRegister());
2637 // TODO: we set any here but we don't handle constants
2638 locations->SetInAt(1, Location::Any());
2639 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2640 break;
2641 }
2642 case Primitive::kPrimFloat:
2643 case Primitive::kPrimDouble: {
2644 locations->SetInAt(0, Location::RequiresFpuRegister());
2645 locations->SetInAt(1, Location::RequiresFpuRegister());
2646 locations->SetOut(Location::RequiresRegister());
2647 break;
2648 }
2649 default:
2650 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2651 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002652}
2653
2654void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002655 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002656 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002657 Location left = locations->InAt(0);
2658 Location right = locations->InAt(1);
2659
2660 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002661 switch (compare->InputAt(0)->GetType()) {
2662 case Primitive::kPrimLong: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002663 if (right.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002664 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002665 } else {
2666 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002667 __ cmpl(left.AsRegisterPairHigh<Register>(),
2668 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002669 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002670 __ j(kLess, &less); // Signed compare.
2671 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002672 if (right.IsRegisterPair()) {
2673 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002674 } else {
2675 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002676 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002677 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002678 break;
2679 }
2680 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002681 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002682 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
2683 break;
2684 }
2685 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002686 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002687 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002688 break;
2689 }
2690 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002691 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002692 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002693 __ movl(out, Immediate(0));
2694 __ j(kEqual, &done);
2695 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
2696
2697 __ Bind(&greater);
2698 __ movl(out, Immediate(1));
2699 __ jmp(&done);
2700
2701 __ Bind(&less);
2702 __ movl(out, Immediate(-1));
2703
2704 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002705}
2706
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002707void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002708 LocationSummary* locations =
2709 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002710 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2711 locations->SetInAt(i, Location::Any());
2712 }
2713 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002714}
2715
2716void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002717 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002718 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002719}
2720
Calin Juravle52c48962014-12-16 17:02:57 +00002721void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
2722 /*
2723 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2724 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2725 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2726 */
2727 switch (kind) {
2728 case MemBarrierKind::kAnyAny: {
2729 __ mfence();
2730 break;
2731 }
2732 case MemBarrierKind::kAnyStore:
2733 case MemBarrierKind::kLoadAny:
2734 case MemBarrierKind::kStoreStore: {
2735 // nop
2736 break;
2737 }
2738 default:
2739 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002740 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002741}
2742
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002743
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002744void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002745 Label is_null;
2746 __ testl(value, value);
2747 __ j(kEqual, &is_null);
2748 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2749 __ movl(temp, object);
2750 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002751 __ movb(Address(temp, card, TIMES_1, 0),
2752 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002753 __ Bind(&is_null);
2754}
2755
Calin Juravle52c48962014-12-16 17:02:57 +00002756void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
2757 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002758 LocationSummary* locations =
2759 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002760 locations->SetInAt(0, Location::RequiresRegister());
2761 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle52c48962014-12-16 17:02:57 +00002762
2763 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
2764 // Long values can be loaded atomically into an XMM using movsd.
2765 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
2766 // and then copy the XMM into the output 32bits at a time).
2767 locations->AddTemp(Location::RequiresFpuRegister());
2768 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002769}
2770
Calin Juravle52c48962014-12-16 17:02:57 +00002771void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
2772 const FieldInfo& field_info) {
2773 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002774
Calin Juravle52c48962014-12-16 17:02:57 +00002775 LocationSummary* locations = instruction->GetLocations();
2776 Register base = locations->InAt(0).AsRegister<Register>();
2777 Location out = locations->Out();
2778 bool is_volatile = field_info.IsVolatile();
2779 Primitive::Type field_type = field_info.GetFieldType();
2780 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2781
2782 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002783 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00002784 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002785 break;
2786 }
2787
2788 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002789 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002790 break;
2791 }
2792
2793 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00002794 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002795 break;
2796 }
2797
2798 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002799 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002800 break;
2801 }
2802
2803 case Primitive::kPrimInt:
2804 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002805 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002806 break;
2807 }
2808
2809 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00002810 if (is_volatile) {
2811 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2812 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002813 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002814 __ movd(out.AsRegisterPairLow<Register>(), temp);
2815 __ psrlq(temp, Immediate(32));
2816 __ movd(out.AsRegisterPairHigh<Register>(), temp);
2817 } else {
2818 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002819 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002820 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
2821 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002822 break;
2823 }
2824
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002825 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002826 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002827 break;
2828 }
2829
2830 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002831 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002832 break;
2833 }
2834
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002835 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00002836 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002837 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002838 }
Calin Juravle52c48962014-12-16 17:02:57 +00002839
Calin Juravle77520bc2015-01-12 18:45:46 +00002840 // Longs are handled in the switch.
2841 if (field_type != Primitive::kPrimLong) {
2842 codegen_->MaybeRecordImplicitNullCheck(instruction);
2843 }
2844
Calin Juravle52c48962014-12-16 17:02:57 +00002845 if (is_volatile) {
2846 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2847 }
2848}
2849
2850void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
2851 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2852
2853 LocationSummary* locations =
2854 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2855 locations->SetInAt(0, Location::RequiresRegister());
2856 bool is_volatile = field_info.IsVolatile();
2857 Primitive::Type field_type = field_info.GetFieldType();
2858 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2859 || (field_type == Primitive::kPrimByte);
2860
2861 // The register allocator does not support multiple
2862 // inputs that die at entry with one in a specific register.
2863 if (is_byte_type) {
2864 // Ensure the value is in a byte register.
2865 locations->SetInAt(1, Location::RegisterLocation(EAX));
2866 } else {
2867 locations->SetInAt(1, Location::RequiresRegister());
2868 }
2869 // Temporary registers for the write barrier.
2870 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2871 locations->AddTemp(Location::RequiresRegister());
2872 // Ensure the card is in a byte register.
2873 locations->AddTemp(Location::RegisterLocation(ECX));
2874 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
2875 // 64bits value can be atomically written to an address with movsd and an XMM register.
2876 // We need two XMM registers because there's no easier way to (bit) copy a register pair
2877 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
2878 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
2879 // isolated cases when we need this it isn't worth adding the extra complexity.
2880 locations->AddTemp(Location::RequiresFpuRegister());
2881 locations->AddTemp(Location::RequiresFpuRegister());
2882 }
2883}
2884
2885void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
2886 const FieldInfo& field_info) {
2887 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2888
2889 LocationSummary* locations = instruction->GetLocations();
2890 Register base = locations->InAt(0).AsRegister<Register>();
2891 Location value = locations->InAt(1);
2892 bool is_volatile = field_info.IsVolatile();
2893 Primitive::Type field_type = field_info.GetFieldType();
2894 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2895
2896 if (is_volatile) {
2897 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2898 }
2899
2900 switch (field_type) {
2901 case Primitive::kPrimBoolean:
2902 case Primitive::kPrimByte: {
2903 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
2904 break;
2905 }
2906
2907 case Primitive::kPrimShort:
2908 case Primitive::kPrimChar: {
2909 __ movw(Address(base, offset), value.AsRegister<Register>());
2910 break;
2911 }
2912
2913 case Primitive::kPrimInt:
2914 case Primitive::kPrimNot: {
2915 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00002916 break;
2917 }
2918
2919 case Primitive::kPrimLong: {
2920 if (is_volatile) {
2921 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2922 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
2923 __ movd(temp1, value.AsRegisterPairLow<Register>());
2924 __ movd(temp2, value.AsRegisterPairHigh<Register>());
2925 __ punpckldq(temp1, temp2);
2926 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00002927 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002928 } else {
2929 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00002930 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002931 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
2932 }
2933 break;
2934 }
2935
2936 case Primitive::kPrimFloat: {
2937 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
2938 break;
2939 }
2940
2941 case Primitive::kPrimDouble: {
2942 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
2943 break;
2944 }
2945
2946 case Primitive::kPrimVoid:
2947 LOG(FATAL) << "Unreachable type " << field_type;
2948 UNREACHABLE();
2949 }
2950
Calin Juravle77520bc2015-01-12 18:45:46 +00002951 // Longs are handled in the switch.
2952 if (field_type != Primitive::kPrimLong) {
2953 codegen_->MaybeRecordImplicitNullCheck(instruction);
2954 }
2955
2956 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2957 Register temp = locations->GetTemp(0).AsRegister<Register>();
2958 Register card = locations->GetTemp(1).AsRegister<Register>();
2959 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
2960 }
2961
Calin Juravle52c48962014-12-16 17:02:57 +00002962 if (is_volatile) {
2963 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2964 }
2965}
2966
2967void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2968 HandleFieldGet(instruction, instruction->GetFieldInfo());
2969}
2970
2971void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2972 HandleFieldGet(instruction, instruction->GetFieldInfo());
2973}
2974
2975void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2976 HandleFieldSet(instruction, instruction->GetFieldInfo());
2977}
2978
2979void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2980 HandleFieldSet(instruction, instruction->GetFieldInfo());
2981}
2982
2983void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2984 HandleFieldSet(instruction, instruction->GetFieldInfo());
2985}
2986
2987void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2988 HandleFieldSet(instruction, instruction->GetFieldInfo());
2989}
2990
2991void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2992 HandleFieldGet(instruction, instruction->GetFieldInfo());
2993}
2994
2995void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2996 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002997}
2998
2999void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003000 LocationSummary* locations =
3001 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003002 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
3003 ? Location::RequiresRegister()
3004 : Location::Any();
3005 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003006 if (instruction->HasUses()) {
3007 locations->SetOut(Location::SameAsFirstInput());
3008 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003009}
3010
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003011void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00003012 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3013 return;
3014 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003015 LocationSummary* locations = instruction->GetLocations();
3016 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00003017
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003018 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
3019 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3020}
3021
3022void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003023 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003024 codegen_->AddSlowPath(slow_path);
3025
3026 LocationSummary* locations = instruction->GetLocations();
3027 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003028
3029 if (obj.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003030 __ cmpl(obj.AsRegister<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003031 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003032 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003033 } else {
3034 DCHECK(obj.IsConstant()) << obj;
3035 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3036 __ jmp(slow_path->GetEntryLabel());
3037 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003038 }
3039 __ j(kEqual, slow_path->GetEntryLabel());
3040}
3041
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003042void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3043 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3044 GenerateImplicitNullCheck(instruction);
3045 } else {
3046 GenerateExplicitNullCheck(instruction);
3047 }
3048}
3049
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003050void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003051 LocationSummary* locations =
3052 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003053 locations->SetInAt(0, Location::RequiresRegister());
3054 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3055 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003056}
3057
3058void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3059 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003060 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003061 Location index = locations->InAt(1);
3062
Calin Juravle77520bc2015-01-12 18:45:46 +00003063 Primitive::Type type = instruction->GetType();
3064 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003065 case Primitive::kPrimBoolean: {
3066 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003067 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003068 if (index.IsConstant()) {
3069 __ movzxb(out, Address(obj,
3070 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3071 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003072 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003073 }
3074 break;
3075 }
3076
3077 case Primitive::kPrimByte: {
3078 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003079 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003080 if (index.IsConstant()) {
3081 __ movsxb(out, Address(obj,
3082 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3083 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003084 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003085 }
3086 break;
3087 }
3088
3089 case Primitive::kPrimShort: {
3090 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003091 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003092 if (index.IsConstant()) {
3093 __ movsxw(out, Address(obj,
3094 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3095 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003096 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003097 }
3098 break;
3099 }
3100
3101 case Primitive::kPrimChar: {
3102 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003103 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003104 if (index.IsConstant()) {
3105 __ movzxw(out, Address(obj,
3106 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3107 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003108 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003109 }
3110 break;
3111 }
3112
3113 case Primitive::kPrimInt:
3114 case Primitive::kPrimNot: {
3115 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003116 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003117 if (index.IsConstant()) {
3118 __ movl(out, Address(obj,
3119 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3120 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003121 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003122 }
3123 break;
3124 }
3125
3126 case Primitive::kPrimLong: {
3127 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003128 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003129 if (index.IsConstant()) {
3130 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003131 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003132 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003133 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003134 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003135 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003136 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003137 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003138 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003139 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003140 }
3141 break;
3142 }
3143
3144 case Primitive::kPrimFloat:
3145 case Primitive::kPrimDouble:
Calin Juravle77520bc2015-01-12 18:45:46 +00003146 LOG(FATAL) << "Unimplemented register type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003147 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003148 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003149 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003150 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003151 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003152
3153 if (type != Primitive::kPrimLong) {
3154 codegen_->MaybeRecordImplicitNullCheck(instruction);
3155 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003156}
3157
3158void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003159 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003160 bool needs_write_barrier =
3161 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3162
3163 DCHECK(kFollowsQuickABI);
3164 bool not_enough_registers = needs_write_barrier
3165 && !instruction->GetValue()->IsConstant()
3166 && !instruction->GetIndex()->IsConstant();
3167 bool needs_runtime_call = instruction->NeedsTypeCheck() || not_enough_registers;
3168
Nicolas Geoffray39468442014-09-02 15:17:15 +01003169 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3170 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003171 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003172
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003173 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003174 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003175 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3176 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3177 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003178 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003179 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3180 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003181 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003182 // In case of a byte operation, the register allocator does not support multiple
3183 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003184 locations->SetInAt(0, Location::RequiresRegister());
3185 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003186 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003187 // Ensure the value is in a byte register.
3188 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003189 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003190 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003191 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003192 // Temporary registers for the write barrier.
3193 if (needs_write_barrier) {
3194 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003195 // Ensure the card is in a byte register.
3196 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003197 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003198 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003199}
3200
3201void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3202 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003203 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003204 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003205 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003206 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003207 bool needs_runtime_call = locations->WillCall();
3208 bool needs_write_barrier =
3209 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003210
3211 switch (value_type) {
3212 case Primitive::kPrimBoolean:
3213 case Primitive::kPrimByte: {
3214 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003215 if (index.IsConstant()) {
3216 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003217 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003218 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003219 } else {
3220 __ movb(Address(obj, offset),
3221 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3222 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003223 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003224 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003225 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003226 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003227 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003228 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003229 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3230 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003231 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003232 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003233 break;
3234 }
3235
3236 case Primitive::kPrimShort:
3237 case Primitive::kPrimChar: {
3238 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003239 if (index.IsConstant()) {
3240 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003241 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003242 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003243 } else {
3244 __ movw(Address(obj, offset),
3245 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3246 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003247 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003248 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003249 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3250 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003251 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003252 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003253 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3254 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003255 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003256 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003257 break;
3258 }
3259
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003260 case Primitive::kPrimInt:
3261 case Primitive::kPrimNot: {
3262 if (!needs_runtime_call) {
3263 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3264 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003265 size_t offset =
3266 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003267 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003268 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003269 } else {
3270 DCHECK(value.IsConstant()) << value;
3271 __ movl(Address(obj, offset),
3272 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3273 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003274 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003275 DCHECK(index.IsRegister()) << index;
3276 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003277 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3278 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003279 } else {
3280 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003281 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003282 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3283 }
3284 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003285 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003286
3287 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003288 Register temp = locations->GetTemp(0).AsRegister<Register>();
3289 Register card = locations->GetTemp(1).AsRegister<Register>();
3290 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003291 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003292 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003293 DCHECK_EQ(value_type, Primitive::kPrimNot);
3294 DCHECK(!codegen_->IsLeafMethod());
3295 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3296 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003297 }
3298 break;
3299 }
3300
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003301 case Primitive::kPrimLong: {
3302 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003303 if (index.IsConstant()) {
3304 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003305 if (value.IsRegisterPair()) {
3306 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003307 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003308 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003309 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003310 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003311 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3312 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003313 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003314 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3315 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003316 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003317 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003318 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003319 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003320 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003321 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003322 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003323 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003324 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003325 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003326 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003327 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003328 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003329 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003330 Immediate(High32Bits(val)));
3331 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003332 }
3333 break;
3334 }
3335
3336 case Primitive::kPrimFloat:
3337 case Primitive::kPrimDouble:
3338 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003339 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003340 case Primitive::kPrimVoid:
3341 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003342 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003343 }
3344}
3345
3346void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3347 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003348 locations->SetInAt(0, Location::RequiresRegister());
3349 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003350 instruction->SetLocations(locations);
3351}
3352
3353void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3354 LocationSummary* locations = instruction->GetLocations();
3355 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003356 Register obj = locations->InAt(0).AsRegister<Register>();
3357 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003358 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003359 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003360}
3361
3362void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003363 LocationSummary* locations =
3364 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003365 locations->SetInAt(0, Location::RequiresRegister());
3366 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003367 if (instruction->HasUses()) {
3368 locations->SetOut(Location::SameAsFirstInput());
3369 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003370}
3371
3372void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3373 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003374 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01003375 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003376 codegen_->AddSlowPath(slow_path);
3377
Roland Levillain271ab9c2014-11-27 15:23:57 +00003378 Register index = locations->InAt(0).AsRegister<Register>();
3379 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003380
3381 __ cmpl(index, length);
3382 __ j(kAboveEqual, slow_path->GetEntryLabel());
3383}
3384
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003385void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3386 temp->SetLocations(nullptr);
3387}
3388
3389void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3390 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003391 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003392}
3393
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003394void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003395 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003396 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003397}
3398
3399void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003400 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3401}
3402
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003403void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3404 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3405}
3406
3407void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003408 HBasicBlock* block = instruction->GetBlock();
3409 if (block->GetLoopInformation() != nullptr) {
3410 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3411 // The back edge will generate the suspend check.
3412 return;
3413 }
3414 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3415 // The goto will generate the suspend check.
3416 return;
3417 }
3418 GenerateSuspendCheck(instruction, nullptr);
3419}
3420
3421void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3422 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003423 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003424 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003425 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003426 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003427 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003428 if (successor == nullptr) {
3429 __ j(kNotEqual, slow_path->GetEntryLabel());
3430 __ Bind(slow_path->GetReturnLabel());
3431 } else {
3432 __ j(kEqual, codegen_->GetLabelOf(successor));
3433 __ jmp(slow_path->GetEntryLabel());
3434 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003435}
3436
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003437X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3438 return codegen_->GetAssembler();
3439}
3440
3441void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
3442 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003443 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003444 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3445 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
3446 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
3447}
3448
3449void ParallelMoveResolverX86::EmitMove(size_t index) {
3450 MoveOperands* move = moves_.Get(index);
3451 Location source = move->GetSource();
3452 Location destination = move->GetDestination();
3453
3454 if (source.IsRegister()) {
3455 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003456 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003457 } else {
3458 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003459 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003460 }
3461 } else if (source.IsStackSlot()) {
3462 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003463 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003464 } else {
3465 DCHECK(destination.IsStackSlot());
3466 MoveMemoryToMemory(destination.GetStackIndex(),
3467 source.GetStackIndex());
3468 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003469 } else if (source.IsConstant()) {
3470 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
3471 Immediate imm(instruction->AsIntConstant()->GetValue());
3472 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003473 __ movl(destination.AsRegister<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003474 } else {
3475 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3476 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003477 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003478 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003479 }
3480}
3481
3482void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003483 Register suggested_scratch = reg == EAX ? EBX : EAX;
3484 ScratchRegisterScope ensure_scratch(
3485 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3486
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003487 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3488 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
3489 __ movl(Address(ESP, mem + stack_offset), reg);
3490 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
3491}
3492
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003493void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
3494 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003495 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3496
3497 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003498 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003499 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3500
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003501 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
3502 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
3503 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
3504 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
3505 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
3506 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
3507}
3508
3509void ParallelMoveResolverX86::EmitSwap(size_t index) {
3510 MoveOperands* move = moves_.Get(index);
3511 Location source = move->GetSource();
3512 Location destination = move->GetDestination();
3513
3514 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003515 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003516 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003517 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003518 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003519 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003520 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3521 Exchange(destination.GetStackIndex(), source.GetStackIndex());
3522 } else {
3523 LOG(FATAL) << "Unimplemented";
3524 }
3525}
3526
3527void ParallelMoveResolverX86::SpillScratch(int reg) {
3528 __ pushl(static_cast<Register>(reg));
3529}
3530
3531void ParallelMoveResolverX86::RestoreScratch(int reg) {
3532 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003533}
3534
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003535void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003536 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3537 ? LocationSummary::kCallOnSlowPath
3538 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003539 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003540 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003541 locations->SetOut(Location::RequiresRegister());
3542}
3543
3544void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003545 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003546 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003547 DCHECK(!cls->CanCallRuntime());
3548 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003549 codegen_->LoadCurrentMethod(out);
3550 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3551 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003552 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003553 codegen_->LoadCurrentMethod(out);
3554 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3555 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003556
3557 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3558 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3559 codegen_->AddSlowPath(slow_path);
3560 __ testl(out, out);
3561 __ j(kEqual, slow_path->GetEntryLabel());
3562 if (cls->MustGenerateClinitCheck()) {
3563 GenerateClassInitializationCheck(slow_path, out);
3564 } else {
3565 __ Bind(slow_path->GetExitLabel());
3566 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003567 }
3568}
3569
3570void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
3571 LocationSummary* locations =
3572 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3573 locations->SetInAt(0, Location::RequiresRegister());
3574 if (check->HasUses()) {
3575 locations->SetOut(Location::SameAsFirstInput());
3576 }
3577}
3578
3579void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003580 // We assume the class to not be null.
3581 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3582 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003583 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003584 GenerateClassInitializationCheck(slow_path,
3585 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003586}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003587
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003588void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
3589 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003590 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3591 Immediate(mirror::Class::kStatusInitialized));
3592 __ j(kLess, slow_path->GetEntryLabel());
3593 __ Bind(slow_path->GetExitLabel());
3594 // No need for memory fence, thanks to the X86 memory model.
3595}
3596
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003597void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
3598 LocationSummary* locations =
3599 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3600 locations->SetOut(Location::RequiresRegister());
3601}
3602
3603void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
3604 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
3605 codegen_->AddSlowPath(slow_path);
3606
Roland Levillain271ab9c2014-11-27 15:23:57 +00003607 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003608 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08003609 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3610 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003611 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3612 __ testl(out, out);
3613 __ j(kEqual, slow_path->GetEntryLabel());
3614 __ Bind(slow_path->GetExitLabel());
3615}
3616
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003617void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
3618 LocationSummary* locations =
3619 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3620 locations->SetOut(Location::RequiresRegister());
3621}
3622
3623void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
3624 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003625 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003626 __ fs()->movl(address, Immediate(0));
3627}
3628
3629void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
3630 LocationSummary* locations =
3631 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3632 InvokeRuntimeCallingConvention calling_convention;
3633 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3634}
3635
3636void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
3637 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
3638 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3639}
3640
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003641void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003642 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3643 ? LocationSummary::kNoCall
3644 : LocationSummary::kCallOnSlowPath;
3645 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3646 locations->SetInAt(0, Location::RequiresRegister());
3647 locations->SetInAt(1, Location::Any());
3648 locations->SetOut(Location::RequiresRegister());
3649}
3650
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003651void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003652 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003653 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003654 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003655 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003656 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3657 Label done, zero;
3658 SlowPathCodeX86* slow_path = nullptr;
3659
3660 // Return 0 if `obj` is null.
3661 // TODO: avoid this check if we know obj is not null.
3662 __ testl(obj, obj);
3663 __ j(kEqual, &zero);
3664 __ movl(out, Address(obj, class_offset));
3665 // Compare the class of `obj` with `cls`.
3666 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003667 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003668 } else {
3669 DCHECK(cls.IsStackSlot()) << cls;
3670 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
3671 }
3672
3673 if (instruction->IsClassFinal()) {
3674 // Classes must be equal for the instanceof to succeed.
3675 __ j(kNotEqual, &zero);
3676 __ movl(out, Immediate(1));
3677 __ jmp(&done);
3678 } else {
3679 // If the classes are not equal, we go into a slow path.
3680 DCHECK(locations->OnlyCallsOnSlowPath());
3681 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003682 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003683 codegen_->AddSlowPath(slow_path);
3684 __ j(kNotEqual, slow_path->GetEntryLabel());
3685 __ movl(out, Immediate(1));
3686 __ jmp(&done);
3687 }
3688 __ Bind(&zero);
3689 __ movl(out, Immediate(0));
3690 if (slow_path != nullptr) {
3691 __ Bind(slow_path->GetExitLabel());
3692 }
3693 __ Bind(&done);
3694}
3695
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003696void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
3697 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3698 instruction, LocationSummary::kCallOnSlowPath);
3699 locations->SetInAt(0, Location::RequiresRegister());
3700 locations->SetInAt(1, Location::Any());
3701 locations->AddTemp(Location::RequiresRegister());
3702}
3703
3704void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
3705 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003706 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003707 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003708 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003709 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3710 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
3711 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3712 codegen_->AddSlowPath(slow_path);
3713
3714 // TODO: avoid this check if we know obj is not null.
3715 __ testl(obj, obj);
3716 __ j(kEqual, slow_path->GetExitLabel());
3717 __ movl(temp, Address(obj, class_offset));
3718
3719 // Compare the class of `obj` with `cls`.
3720 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003721 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003722 } else {
3723 DCHECK(cls.IsStackSlot()) << cls;
3724 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
3725 }
3726
3727 __ j(kNotEqual, slow_path->GetEntryLabel());
3728 __ Bind(slow_path->GetExitLabel());
3729}
3730
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003731void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3732 LocationSummary* locations =
3733 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3734 InvokeRuntimeCallingConvention calling_convention;
3735 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3736}
3737
3738void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3739 __ fs()->call(Address::Absolute(instruction->IsEnter()
3740 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
3741 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
3742 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3743}
3744
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003745void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3746void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3747void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3748
3749void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3750 LocationSummary* locations =
3751 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3752 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3753 || instruction->GetResultType() == Primitive::kPrimLong);
3754 locations->SetInAt(0, Location::RequiresRegister());
3755 locations->SetInAt(1, Location::Any());
3756 locations->SetOut(Location::SameAsFirstInput());
3757}
3758
3759void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
3760 HandleBitwiseOperation(instruction);
3761}
3762
3763void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
3764 HandleBitwiseOperation(instruction);
3765}
3766
3767void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
3768 HandleBitwiseOperation(instruction);
3769}
3770
3771void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3772 LocationSummary* locations = instruction->GetLocations();
3773 Location first = locations->InAt(0);
3774 Location second = locations->InAt(1);
3775 DCHECK(first.Equals(locations->Out()));
3776
3777 if (instruction->GetResultType() == Primitive::kPrimInt) {
3778 if (second.IsRegister()) {
3779 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003780 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003781 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003782 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003783 } else {
3784 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003785 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003786 }
3787 } else if (second.IsConstant()) {
3788 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003789 __ andl(first.AsRegister<Register>(),
3790 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003791 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003792 __ orl(first.AsRegister<Register>(),
3793 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003794 } else {
3795 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00003796 __ xorl(first.AsRegister<Register>(),
3797 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003798 }
3799 } else {
3800 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003801 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003802 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003803 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003804 } else {
3805 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003806 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003807 }
3808 }
3809 } else {
3810 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3811 if (second.IsRegisterPair()) {
3812 if (instruction->IsAnd()) {
3813 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3814 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3815 } else if (instruction->IsOr()) {
3816 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3817 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3818 } else {
3819 DCHECK(instruction->IsXor());
3820 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3821 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3822 }
3823 } else {
3824 if (instruction->IsAnd()) {
3825 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3826 __ andl(first.AsRegisterPairHigh<Register>(),
3827 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3828 } else if (instruction->IsOr()) {
3829 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3830 __ orl(first.AsRegisterPairHigh<Register>(),
3831 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3832 } else {
3833 DCHECK(instruction->IsXor());
3834 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3835 __ xorl(first.AsRegisterPairHigh<Register>(),
3836 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3837 }
3838 }
3839 }
3840}
3841
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003842} // namespace x86
3843} // namespace art