blob: a22c91a3dca74a6770b1f88ad78b3819e64b66e0 [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 kCurrentMethodStackOffset = 0;
35
Calin Juravled6fb6cf2014-11-11 19:07:44 +000036static constexpr Register kRuntimeParameterCoreRegisters[] = { EAX, ECX, EDX, EBX };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010037static constexpr size_t kRuntimeParameterCoreRegistersLength =
38 arraysize(kRuntimeParameterCoreRegisters);
Mark P Mendell966c3ae2015-01-27 15:45:27 +000039static constexpr XmmRegister kRuntimeParameterFpuRegisters[] = { XMM0, XMM1, XMM2, XMM3 };
40static constexpr size_t kRuntimeParameterFpuRegistersLength =
41 arraysize(kRuntimeParameterFpuRegisters);
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;
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000047static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000048
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010049class InvokeRuntimeCallingConvention : public CallingConvention<Register, XmmRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010050 public:
51 InvokeRuntimeCallingConvention()
52 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010053 kRuntimeParameterCoreRegistersLength,
54 kRuntimeParameterFpuRegisters,
55 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010056
57 private:
58 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
59};
60
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
62
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010063class SlowPathCodeX86 : public SlowPathCode {
64 public:
65 SlowPathCodeX86() : entry_label_(), exit_label_() {}
66
67 Label* GetEntryLabel() { return &entry_label_; }
68 Label* GetExitLabel() { return &exit_label_; }
69
70 private:
71 Label entry_label_;
72 Label exit_label_;
73
74 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86);
75};
76
77class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010078 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010079 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080
81 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
82 __ Bind(GetEntryLabel());
83 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010084 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010085 }
86
87 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010088 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010089 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
90};
91
Calin Juravled0d48522014-11-04 16:40:20 +000092class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
93 public:
94 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
95
96 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
97 __ Bind(GetEntryLabel());
98 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
99 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
100 }
101
102 private:
103 HDivZeroCheck* const instruction_;
104 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
105};
106
Calin Juravlebacfec32014-11-14 15:54:36 +0000107class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +0000108 public:
Calin Juravlebacfec32014-11-14 15:54:36 +0000109 explicit DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000110
111 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
112 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000113 if (is_div_) {
114 __ negl(reg_);
115 } else {
116 __ movl(reg_, Immediate(0));
117 }
Calin Juravled0d48522014-11-04 16:40:20 +0000118 __ jmp(GetExitLabel());
119 }
120
121 private:
122 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000123 bool is_div_;
124 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000125};
126
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100127class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100128 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100129 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
130 Location index_location,
131 Location length_location)
Roland Levillain199f3362014-11-27 17:15:16 +0000132 : instruction_(instruction),
133 index_location_(index_location),
134 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100135
136 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100137 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100138 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000139 // We're moving two locations to locations that could overlap, so we need a parallel
140 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100141 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000142 x86_codegen->EmitParallelMoves(
143 index_location_,
144 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
145 length_location_,
146 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100147 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100148 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100149 }
150
151 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100152 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100153 const Location index_location_;
154 const Location length_location_;
155
156 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
157};
158
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100159class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000160 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100161 explicit SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
162 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000163
164 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100165 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000166 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100167 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000168 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
169 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100170 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100171 if (successor_ == nullptr) {
172 __ jmp(GetReturnLabel());
173 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100174 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100175 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000176 }
177
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100178 Label* GetReturnLabel() {
179 DCHECK(successor_ == nullptr);
180 return &return_label_;
181 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000182
183 private:
184 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100185 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000186 Label return_label_;
187
188 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
189};
190
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000191class LoadStringSlowPathX86 : public SlowPathCodeX86 {
192 public:
193 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
194
195 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
196 LocationSummary* locations = instruction_->GetLocations();
197 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
198
199 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
200 __ Bind(GetEntryLabel());
201 codegen->SaveLiveRegisters(locations);
202
203 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800204 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
205 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000206 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
207 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
208 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
209 codegen->RestoreLiveRegisters(locations);
210
211 __ jmp(GetExitLabel());
212 }
213
214 private:
215 HLoadString* const instruction_;
216
217 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
218};
219
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000220class LoadClassSlowPathX86 : public SlowPathCodeX86 {
221 public:
222 LoadClassSlowPathX86(HLoadClass* cls,
223 HInstruction* at,
224 uint32_t dex_pc,
225 bool do_clinit)
226 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
227 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
228 }
229
230 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
231 LocationSummary* locations = at_->GetLocations();
232 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
233 __ Bind(GetEntryLabel());
234 codegen->SaveLiveRegisters(locations);
235
236 InvokeRuntimeCallingConvention calling_convention;
237 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
238 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
239 __ fs()->call(Address::Absolute(do_clinit_
240 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
241 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
242 codegen->RecordPcInfo(at_, dex_pc_);
243
244 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000245 Location out = locations->Out();
246 if (out.IsValid()) {
247 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
248 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000249 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000250
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000251 codegen->RestoreLiveRegisters(locations);
252 __ jmp(GetExitLabel());
253 }
254
255 private:
256 // The class this slow path will load.
257 HLoadClass* const cls_;
258
259 // The instruction where this slow path is happening.
260 // (Might be the load class or an initialization check).
261 HInstruction* const at_;
262
263 // The dex PC of `at_`.
264 const uint32_t dex_pc_;
265
266 // Whether to initialize the class.
267 const bool do_clinit_;
268
269 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
270};
271
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000272class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
273 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000274 TypeCheckSlowPathX86(HInstruction* instruction,
275 Location class_to_check,
276 Location object_class,
277 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000278 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000279 class_to_check_(class_to_check),
280 object_class_(object_class),
281 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000282
283 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
284 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000285 DCHECK(instruction_->IsCheckCast()
286 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000287
288 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
289 __ Bind(GetEntryLabel());
290 codegen->SaveLiveRegisters(locations);
291
292 // We're moving two locations to locations that could overlap, so we need a parallel
293 // move resolver.
294 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000295 x86_codegen->EmitParallelMoves(
296 class_to_check_,
297 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
298 object_class_,
299 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000300
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000301 if (instruction_->IsInstanceOf()) {
Roland Levillain199f3362014-11-27 17:15:16 +0000302 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize,
303 pInstanceofNonTrivial)));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000304 } else {
305 DCHECK(instruction_->IsCheckCast());
306 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
307 }
308
309 codegen->RecordPcInfo(instruction_, dex_pc_);
310 if (instruction_->IsInstanceOf()) {
311 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
312 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000313 codegen->RestoreLiveRegisters(locations);
314
315 __ jmp(GetExitLabel());
316 }
317
318 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000319 HInstruction* const instruction_;
320 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000321 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000322 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000323
324 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
325};
326
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100327#undef __
328#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
329
Dave Allison20dfc792014-06-16 20:44:29 -0700330inline Condition X86Condition(IfCondition cond) {
331 switch (cond) {
332 case kCondEQ: return kEqual;
333 case kCondNE: return kNotEqual;
334 case kCondLT: return kLess;
335 case kCondLE: return kLessEqual;
336 case kCondGT: return kGreater;
337 case kCondGE: return kGreaterEqual;
338 default:
339 LOG(FATAL) << "Unknown if condition";
340 }
341 return kEqual;
342}
343
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100344void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
345 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
346}
347
348void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
349 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
350}
351
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100352size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
353 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
354 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100355}
356
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100357size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
358 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
359 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100360}
361
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000362CodeGeneratorX86::CodeGeneratorX86(HGraph* graph, const CompilerOptions& compiler_options)
363 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfXmmRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000364 kNumberOfRegisterPairs, (1 << kFakeReturnRegister), 0, compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100365 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100366 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100367 instruction_visitor_(graph, this),
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000368 move_resolver_(graph->GetArena(), this) {
369 // Use a fake return address register to mimic Quick.
370 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100371}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100372
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100373Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100374 switch (type) {
375 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100376 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100377 X86ManagedRegister pair =
378 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100379 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
380 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100381 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
382 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100383 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100384 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100385 }
386
387 case Primitive::kPrimByte:
388 case Primitive::kPrimBoolean:
389 case Primitive::kPrimChar:
390 case Primitive::kPrimShort:
391 case Primitive::kPrimInt:
392 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100393 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100394 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100395 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100396 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
397 X86ManagedRegister current =
398 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
399 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100400 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100401 }
402 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100403 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100404 }
405
406 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100407 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100408 return Location::FpuRegisterLocation(
409 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100410 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100411
412 case Primitive::kPrimVoid:
413 LOG(FATAL) << "Unreachable type " << type;
414 }
415
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100416 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100417}
418
Nicolas Geoffray98893962015-01-21 12:32:32 +0000419void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline ATTRIBUTE_UNUSED) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100420 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100421 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100422
423 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100424 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100425
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000426 // TODO: We currently don't use Quick's callee saved registers.
427 DCHECK(kFollowsQuickABI);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100428 blocked_core_registers_[EBP] = true;
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000429 blocked_core_registers_[ESI] = true;
430 blocked_core_registers_[EDI] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100431
432 UpdateBlockedPairRegisters();
433}
434
435void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
436 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
437 X86ManagedRegister current =
438 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
439 if (blocked_core_registers_[current.AsRegisterPairLow()]
440 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
441 blocked_register_pairs_[i] = true;
442 }
443 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100444}
445
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100446InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
447 : HGraphVisitor(graph),
448 assembler_(codegen->GetAssembler()),
449 codegen_(codegen) {}
450
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000451void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000452 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000453 bool skip_overflow_check =
454 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000455 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000456
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000457 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100458 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100459 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100460 }
461
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000462 __ subl(ESP, Immediate(GetFrameSize() - FrameEntrySpillSize()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100463 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000464}
465
466void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000467 __ addl(ESP, Immediate(GetFrameSize() - FrameEntrySpillSize()));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000468}
469
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100470void CodeGeneratorX86::Bind(HBasicBlock* block) {
471 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000472}
473
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100474void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100475 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000476}
477
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100478Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
479 switch (load->GetType()) {
480 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100481 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100482 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
483 break;
484
485 case Primitive::kPrimInt:
486 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100487 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100488 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100489
490 case Primitive::kPrimBoolean:
491 case Primitive::kPrimByte:
492 case Primitive::kPrimChar:
493 case Primitive::kPrimShort:
494 case Primitive::kPrimVoid:
495 LOG(FATAL) << "Unexpected type " << load->GetType();
496 }
497
498 LOG(FATAL) << "Unreachable";
499 return Location();
500}
501
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100502Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
503 switch (type) {
504 case Primitive::kPrimBoolean:
505 case Primitive::kPrimByte:
506 case Primitive::kPrimChar:
507 case Primitive::kPrimShort:
508 case Primitive::kPrimInt:
509 case Primitive::kPrimNot: {
510 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000511 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100512 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100513 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100514 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000515 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100516 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100517 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100518
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000519 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100520 uint32_t index = gp_index_;
521 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000522 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100523 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100524 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
525 calling_convention.GetRegisterPairAt(index));
526 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100527 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000528 // stack_index_ is the right offset for the memory.
529 return Location::QuickParameter(index, stack_index_ - 2);
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100530 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000531 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
532 }
533 }
534
535 case Primitive::kPrimFloat: {
536 uint32_t index = fp_index_++;
537 stack_index_++;
538 if (index < calling_convention.GetNumberOfFpuRegisters()) {
539 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
540 } else {
541 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
542 }
543 }
544
545 case Primitive::kPrimDouble: {
546 uint32_t index = fp_index_++;
547 stack_index_ += 2;
548 if (index < calling_convention.GetNumberOfFpuRegisters()) {
549 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
550 } else {
551 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100552 }
553 }
554
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100555 case Primitive::kPrimVoid:
556 LOG(FATAL) << "Unexpected parameter type " << type;
557 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100558 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100559 return Location();
560}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100561
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100562void CodeGeneratorX86::Move32(Location destination, Location source) {
563 if (source.Equals(destination)) {
564 return;
565 }
566 if (destination.IsRegister()) {
567 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000568 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100569 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000570 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100571 } else {
572 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000573 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100574 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100575 } else if (destination.IsFpuRegister()) {
576 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000577 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100578 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000579 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100580 } else {
581 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000582 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100583 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100584 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000585 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100586 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000587 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100588 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000589 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100590 } else {
591 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100592 __ pushl(Address(ESP, source.GetStackIndex()));
593 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100594 }
595 }
596}
597
598void CodeGeneratorX86::Move64(Location destination, Location source) {
599 if (source.Equals(destination)) {
600 return;
601 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100602 if (destination.IsRegisterPair()) {
603 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000604 EmitParallelMoves(
605 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
606 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
607 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
608 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100609 } else if (source.IsFpuRegister()) {
610 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100611 } else if (source.IsQuickParameter()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000612 uint16_t register_index = source.GetQuickParameterRegisterIndex();
613 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100614 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000615 EmitParallelMoves(
616 Location::RegisterLocation(calling_convention.GetRegisterAt(register_index)),
617 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
618 Location::StackSlot(
619 calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize()),
620 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100621 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000622 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100623 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100624 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
625 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100626 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
627 }
628 } else if (destination.IsQuickParameter()) {
629 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000630 uint16_t register_index = destination.GetQuickParameterRegisterIndex();
631 uint16_t stack_index = destination.GetQuickParameterStackIndex();
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000632 if (source.IsRegisterPair()) {
633 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100634 } else if (source.IsFpuRegister()) {
635 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100636 } else {
637 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000638 EmitParallelMoves(
639 Location::StackSlot(source.GetStackIndex()),
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000640 Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index)),
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000641 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
642 Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index + 1)));
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000643 __ movl(calling_convention.GetRegisterAt(register_index), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100644 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100645 } else if (destination.IsFpuRegister()) {
646 if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000647 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100648 } else {
649 LOG(FATAL) << "Unimplemented";
650 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100651 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000652 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100653 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000654 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100655 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100656 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100657 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100658 } else if (source.IsQuickParameter()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000659 // No conflict possible, so just do the move.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100660 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000661 uint16_t register_index = source.GetQuickParameterRegisterIndex();
662 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000663 // Just move the low part. The only time a source is a quick parameter is
664 // when moving the parameter to its stack locations. And the (Java) caller
665 // of this method has already done that.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100666 __ movl(Address(ESP, destination.GetStackIndex()),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000667 calling_convention.GetRegisterAt(register_index));
668 DCHECK_EQ(calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100669 static_cast<size_t>(destination.GetHighStackIndex(kX86WordSize)));
670 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000671 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100672 } else {
673 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000674 EmitParallelMoves(
675 Location::StackSlot(source.GetStackIndex()),
676 Location::StackSlot(destination.GetStackIndex()),
677 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
678 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100679 }
680 }
681}
682
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100683void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000684 LocationSummary* locations = instruction->GetLocations();
685 if (locations != nullptr && locations->Out().Equals(location)) {
686 return;
687 }
688
689 if (locations != nullptr && locations->Out().IsConstant()) {
690 HConstant* const_to_move = locations->Out().GetConstant();
691 if (const_to_move->IsIntConstant()) {
692 Immediate imm(const_to_move->AsIntConstant()->GetValue());
693 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000694 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000695 } else if (location.IsStackSlot()) {
696 __ movl(Address(ESP, location.GetStackIndex()), imm);
697 } else {
698 DCHECK(location.IsConstant());
699 DCHECK_EQ(location.GetConstant(), const_to_move);
700 }
701 } else if (const_to_move->IsLongConstant()) {
702 int64_t value = const_to_move->AsLongConstant()->GetValue();
703 if (location.IsRegisterPair()) {
704 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
705 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
706 } else if (location.IsDoubleStackSlot()) {
707 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000708 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
709 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000710 } else {
711 DCHECK(location.IsConstant());
712 DCHECK_EQ(location.GetConstant(), instruction);
713 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100714 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000715 } else if (instruction->IsTemporary()) {
716 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000717 if (temp_location.IsStackSlot()) {
718 Move32(location, temp_location);
719 } else {
720 DCHECK(temp_location.IsDoubleStackSlot());
721 Move64(location, temp_location);
722 }
Roland Levillain476df552014-10-09 17:51:36 +0100723 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100724 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100725 switch (instruction->GetType()) {
726 case Primitive::kPrimBoolean:
727 case Primitive::kPrimByte:
728 case Primitive::kPrimChar:
729 case Primitive::kPrimShort:
730 case Primitive::kPrimInt:
731 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100732 case Primitive::kPrimFloat:
733 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100734 break;
735
736 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100737 case Primitive::kPrimDouble:
738 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100739 break;
740
741 default:
742 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
743 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000744 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100745 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100746 switch (instruction->GetType()) {
747 case Primitive::kPrimBoolean:
748 case Primitive::kPrimByte:
749 case Primitive::kPrimChar:
750 case Primitive::kPrimShort:
751 case Primitive::kPrimInt:
752 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100753 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000754 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100755 break;
756
757 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100758 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000759 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100760 break;
761
762 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100763 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100764 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000765 }
766}
767
768void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000769 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000770}
771
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000772void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000773 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100774 DCHECK(!successor->IsExitBlock());
775
776 HBasicBlock* block = got->GetBlock();
777 HInstruction* previous = got->GetPrevious();
778
779 HLoopInformation* info = block->GetLoopInformation();
780 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
781 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
782 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
783 return;
784 }
785
786 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
787 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
788 }
789 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000790 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000791 }
792}
793
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000794void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000795 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000796}
797
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000798void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700799 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000800 if (kIsDebugBuild) {
801 __ Comment("Unreachable");
802 __ int3();
803 }
804}
805
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000806void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100807 LocationSummary* locations =
808 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100809 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100810 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100811 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100812 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000813}
814
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000815void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700816 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100817 if (cond->IsIntConstant()) {
818 // Constant condition, statically compared against 1.
819 int32_t cond_value = cond->AsIntConstant()->GetValue();
820 if (cond_value == 1) {
821 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
822 if_instr->IfTrueSuccessor())) {
823 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100824 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100825 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100826 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100827 DCHECK_EQ(cond_value, 0);
828 }
829 } else {
830 bool materialized =
831 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
832 // Moves do not affect the eflags register, so if the condition is
833 // evaluated just before the if, we don't need to evaluate it
834 // again.
835 bool eflags_set = cond->IsCondition()
836 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
837 if (materialized) {
838 if (!eflags_set) {
839 // Materialized condition, compare against 0.
840 Location lhs = if_instr->GetLocations()->InAt(0);
841 if (lhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000842 __ cmpl(lhs.AsRegister<Register>(), Immediate(0));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100843 } else {
844 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
845 }
846 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
847 } else {
848 __ j(X86Condition(cond->AsCondition()->GetCondition()),
849 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
850 }
851 } else {
852 Location lhs = cond->GetLocations()->InAt(0);
853 Location rhs = cond->GetLocations()->InAt(1);
854 // LHS is guaranteed to be in a register (see
855 // LocationsBuilderX86::VisitCondition).
856 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000857 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100858 } else if (rhs.IsConstant()) {
859 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
860 Immediate imm(instruction->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000861 __ cmpl(lhs.AsRegister<Register>(), imm);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100862 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000863 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100864 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100865 __ j(X86Condition(cond->AsCondition()->GetCondition()),
866 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700867 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100868 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100869 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
870 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700871 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000872 }
873}
874
875void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000876 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000877}
878
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000879void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
880 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000881}
882
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000883void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100884 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000885}
886
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000887void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100888 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700889 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000890}
891
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100892void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100893 LocationSummary* locations =
894 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100895 switch (store->InputAt(1)->GetType()) {
896 case Primitive::kPrimBoolean:
897 case Primitive::kPrimByte:
898 case Primitive::kPrimChar:
899 case Primitive::kPrimShort:
900 case Primitive::kPrimInt:
901 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100902 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100903 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
904 break;
905
906 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100907 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100908 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
909 break;
910
911 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100912 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100913 }
914 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000915}
916
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000917void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700918 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000919}
920
Dave Allison20dfc792014-06-16 20:44:29 -0700921void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100922 LocationSummary* locations =
923 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100924 locations->SetInAt(0, Location::RequiresRegister());
925 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100926 if (comp->NeedsMaterialization()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000927 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100928 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000929}
930
Dave Allison20dfc792014-06-16 20:44:29 -0700931void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
932 if (comp->NeedsMaterialization()) {
933 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000934 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100935 // Clear register: setcc only sets the low byte.
936 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700937 if (locations->InAt(1).IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000938 __ cmpl(locations->InAt(0).AsRegister<Register>(),
939 locations->InAt(1).AsRegister<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100940 } else if (locations->InAt(1).IsConstant()) {
941 HConstant* instruction = locations->InAt(1).GetConstant();
942 Immediate imm(instruction->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000943 __ cmpl(locations->InAt(0).AsRegister<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700944 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000945 __ cmpl(locations->InAt(0).AsRegister<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700946 Address(ESP, locations->InAt(1).GetStackIndex()));
947 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000948 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100949 }
Dave Allison20dfc792014-06-16 20:44:29 -0700950}
951
952void LocationsBuilderX86::VisitEqual(HEqual* comp) {
953 VisitCondition(comp);
954}
955
956void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
957 VisitCondition(comp);
958}
959
960void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
961 VisitCondition(comp);
962}
963
964void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
965 VisitCondition(comp);
966}
967
968void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
969 VisitCondition(comp);
970}
971
972void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
973 VisitCondition(comp);
974}
975
976void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
977 VisitCondition(comp);
978}
979
980void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
981 VisitCondition(comp);
982}
983
984void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
985 VisitCondition(comp);
986}
987
988void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
989 VisitCondition(comp);
990}
991
992void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
993 VisitCondition(comp);
994}
995
996void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
997 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000998}
999
1000void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001001 LocationSummary* locations =
1002 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001003 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001004}
1005
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001006void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001007 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001008 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001009}
1010
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001011void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001012 LocationSummary* locations =
1013 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001014 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001015}
1016
1017void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1018 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001019 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001020}
1021
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001022void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1023 LocationSummary* locations =
1024 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1025 locations->SetOut(Location::ConstantLocation(constant));
1026}
1027
1028void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1029 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001030 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001031}
1032
1033void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1034 LocationSummary* locations =
1035 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1036 locations->SetOut(Location::ConstantLocation(constant));
1037}
1038
1039void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1040 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001041 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001042}
1043
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001044void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001045 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001046}
1047
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001048void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001049 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001050 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001051 __ ret();
1052}
1053
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001054void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001055 LocationSummary* locations =
1056 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001057 switch (ret->InputAt(0)->GetType()) {
1058 case Primitive::kPrimBoolean:
1059 case Primitive::kPrimByte:
1060 case Primitive::kPrimChar:
1061 case Primitive::kPrimShort:
1062 case Primitive::kPrimInt:
1063 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001064 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001065 break;
1066
1067 case Primitive::kPrimLong:
1068 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001069 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001070 break;
1071
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001072 case Primitive::kPrimFloat:
1073 case Primitive::kPrimDouble:
1074 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001075 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001076 break;
1077
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001078 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001079 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001080 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001081}
1082
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001083void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001084 if (kIsDebugBuild) {
1085 switch (ret->InputAt(0)->GetType()) {
1086 case Primitive::kPrimBoolean:
1087 case Primitive::kPrimByte:
1088 case Primitive::kPrimChar:
1089 case Primitive::kPrimShort:
1090 case Primitive::kPrimInt:
1091 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001092 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001093 break;
1094
1095 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001096 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1097 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001098 break;
1099
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001100 case Primitive::kPrimFloat:
1101 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001102 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001103 break;
1104
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001105 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001106 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001107 }
1108 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001109 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001110 __ ret();
1111}
1112
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001113void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001114 HandleInvoke(invoke);
1115}
1116
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001117void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001118 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001119
1120 // TODO: Implement all kinds of calls:
1121 // 1) boot -> boot
1122 // 2) app -> boot
1123 // 3) app -> app
1124 //
1125 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1126
1127 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001128 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001129 if (!invoke->IsRecursive()) {
1130 // temp = temp->dex_cache_resolved_methods_;
1131 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
1132 // temp = temp[index_in_cache]
1133 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
1134 // (temp + offset_of_quick_compiled_code)()
1135 __ call(Address(
1136 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
1137 } else {
1138 __ call(codegen_->GetFrameEntryLabel());
1139 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001140
1141 DCHECK(!codegen_->IsLeafMethod());
1142 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1143}
1144
1145void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1146 HandleInvoke(invoke);
1147}
1148
1149void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001150 LocationSummary* locations =
1151 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001152 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001153
1154 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001155 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001156 HInstruction* input = invoke->InputAt(i);
1157 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1158 }
1159
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001160 switch (invoke->GetType()) {
1161 case Primitive::kPrimBoolean:
1162 case Primitive::kPrimByte:
1163 case Primitive::kPrimChar:
1164 case Primitive::kPrimShort:
1165 case Primitive::kPrimInt:
1166 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001167 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001168 break;
1169
1170 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001171 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001172 break;
1173
1174 case Primitive::kPrimVoid:
1175 break;
1176
1177 case Primitive::kPrimDouble:
1178 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001179 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001180 break;
1181 }
1182
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001183 invoke->SetLocations(locations);
1184}
1185
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001186void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001187 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001188 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1189 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1190 LocationSummary* locations = invoke->GetLocations();
1191 Location receiver = locations->InAt(0);
1192 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1193 // temp = object->GetClass();
1194 if (receiver.IsStackSlot()) {
1195 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1196 __ movl(temp, Address(temp, class_offset));
1197 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001198 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001199 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001200 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001201 // temp = temp->GetMethodAt(method_offset);
1202 __ movl(temp, Address(temp, method_offset));
1203 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001204 __ call(Address(
1205 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001206
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001207 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001208 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001209}
1210
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001211void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1212 HandleInvoke(invoke);
1213 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001214 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001215}
1216
1217void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1218 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001219 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001220 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1221 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1222 LocationSummary* locations = invoke->GetLocations();
1223 Location receiver = locations->InAt(0);
1224 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1225
1226 // Set the hidden argument.
1227 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001228 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001229
1230 // temp = object->GetClass();
1231 if (receiver.IsStackSlot()) {
1232 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1233 __ movl(temp, Address(temp, class_offset));
1234 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001235 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001236 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001237 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001238 // temp = temp->GetImtEntryAt(method_offset);
1239 __ movl(temp, Address(temp, method_offset));
1240 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001241 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001242 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001243
1244 DCHECK(!codegen_->IsLeafMethod());
1245 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1246}
1247
Roland Levillain88cb1752014-10-20 16:36:47 +01001248void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1249 LocationSummary* locations =
1250 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1251 switch (neg->GetResultType()) {
1252 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001253 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001254 locations->SetInAt(0, Location::RequiresRegister());
1255 locations->SetOut(Location::SameAsFirstInput());
1256 break;
1257
Roland Levillain88cb1752014-10-20 16:36:47 +01001258 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001259 locations->SetInAt(0, Location::RequiresFpuRegister());
1260 locations->SetOut(Location::SameAsFirstInput());
1261 locations->AddTemp(Location::RequiresRegister());
1262 locations->AddTemp(Location::RequiresFpuRegister());
1263 break;
1264
Roland Levillain88cb1752014-10-20 16:36:47 +01001265 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001266 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001267 locations->SetOut(Location::SameAsFirstInput());
1268 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001269 break;
1270
1271 default:
1272 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1273 }
1274}
1275
1276void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1277 LocationSummary* locations = neg->GetLocations();
1278 Location out = locations->Out();
1279 Location in = locations->InAt(0);
1280 switch (neg->GetResultType()) {
1281 case Primitive::kPrimInt:
1282 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001283 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001284 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001285 break;
1286
1287 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001288 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001289 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001290 __ negl(out.AsRegisterPairLow<Register>());
1291 // Negation is similar to subtraction from zero. The least
1292 // significant byte triggers a borrow when it is different from
1293 // zero; to take it into account, add 1 to the most significant
1294 // byte if the carry flag (CF) is set to 1 after the first NEGL
1295 // operation.
1296 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1297 __ negl(out.AsRegisterPairHigh<Register>());
1298 break;
1299
Roland Levillain5368c212014-11-27 15:03:41 +00001300 case Primitive::kPrimFloat: {
1301 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001302 Register constant = locations->GetTemp(0).AsRegister<Register>();
1303 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001304 // Implement float negation with an exclusive or with value
1305 // 0x80000000 (mask for bit 31, representing the sign of a
1306 // single-precision floating-point number).
1307 __ movl(constant, Immediate(INT32_C(0x80000000)));
1308 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001309 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001310 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001311 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001312
Roland Levillain5368c212014-11-27 15:03:41 +00001313 case Primitive::kPrimDouble: {
1314 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001315 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001316 // Implement double negation with an exclusive or with value
1317 // 0x8000000000000000 (mask for bit 63, representing the sign of
1318 // a double-precision floating-point number).
1319 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001320 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001321 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001322 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001323
1324 default:
1325 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1326 }
1327}
1328
Roland Levillaindff1f282014-11-05 14:15:05 +00001329void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001330 Primitive::Type result_type = conversion->GetResultType();
1331 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001332 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001333
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001334 // The float-to-long and double-to-long type conversions rely on a
1335 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001336 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001337 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1338 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001339 ? LocationSummary::kCall
1340 : LocationSummary::kNoCall;
1341 LocationSummary* locations =
1342 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1343
Roland Levillaindff1f282014-11-05 14:15:05 +00001344 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001345 case Primitive::kPrimByte:
1346 switch (input_type) {
1347 case Primitive::kPrimShort:
1348 case Primitive::kPrimInt:
1349 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001350 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001351 locations->SetInAt(0, Location::Any());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001352 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1353 break;
1354
1355 default:
1356 LOG(FATAL) << "Unexpected type conversion from " << input_type
1357 << " to " << result_type;
1358 }
1359 break;
1360
Roland Levillain01a8d712014-11-14 16:27:39 +00001361 case Primitive::kPrimShort:
1362 switch (input_type) {
1363 case Primitive::kPrimByte:
1364 case Primitive::kPrimInt:
1365 case Primitive::kPrimChar:
1366 // Processing a Dex `int-to-short' instruction.
1367 locations->SetInAt(0, Location::Any());
1368 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1369 break;
1370
1371 default:
1372 LOG(FATAL) << "Unexpected type conversion from " << input_type
1373 << " to " << result_type;
1374 }
1375 break;
1376
Roland Levillain946e1432014-11-11 17:35:19 +00001377 case Primitive::kPrimInt:
1378 switch (input_type) {
1379 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001380 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001381 locations->SetInAt(0, Location::Any());
1382 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1383 break;
1384
1385 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001386 // Processing a Dex `float-to-int' instruction.
1387 locations->SetInAt(0, Location::RequiresFpuRegister());
1388 locations->SetOut(Location::RequiresRegister());
1389 locations->AddTemp(Location::RequiresFpuRegister());
1390 break;
1391
Roland Levillain946e1432014-11-11 17:35:19 +00001392 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001393 // Processing a Dex `double-to-int' instruction.
1394 locations->SetInAt(0, Location::RequiresFpuRegister());
1395 locations->SetOut(Location::RequiresRegister());
1396 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001397 break;
1398
1399 default:
1400 LOG(FATAL) << "Unexpected type conversion from " << input_type
1401 << " to " << result_type;
1402 }
1403 break;
1404
Roland Levillaindff1f282014-11-05 14:15:05 +00001405 case Primitive::kPrimLong:
1406 switch (input_type) {
1407 case Primitive::kPrimByte:
1408 case Primitive::kPrimShort:
1409 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001410 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001411 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001412 locations->SetInAt(0, Location::RegisterLocation(EAX));
1413 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1414 break;
1415
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001416 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001417 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001418 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001419 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001420 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1421 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1422
Vladimir Marko949c91f2015-01-27 10:48:44 +00001423 // The runtime helper puts the result in EAX, EDX.
1424 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001425 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001426 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001427
1428 default:
1429 LOG(FATAL) << "Unexpected type conversion from " << input_type
1430 << " to " << result_type;
1431 }
1432 break;
1433
Roland Levillain981e4542014-11-14 11:47:14 +00001434 case Primitive::kPrimChar:
1435 switch (input_type) {
1436 case Primitive::kPrimByte:
1437 case Primitive::kPrimShort:
1438 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001439 // Processing a Dex `int-to-char' instruction.
1440 locations->SetInAt(0, Location::Any());
1441 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1442 break;
1443
1444 default:
1445 LOG(FATAL) << "Unexpected type conversion from " << input_type
1446 << " to " << result_type;
1447 }
1448 break;
1449
Roland Levillaindff1f282014-11-05 14:15:05 +00001450 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001451 switch (input_type) {
1452 case Primitive::kPrimByte:
1453 case Primitive::kPrimShort:
1454 case Primitive::kPrimInt:
1455 case Primitive::kPrimChar:
1456 // Processing a Dex `int-to-float' instruction.
1457 locations->SetInAt(0, Location::RequiresRegister());
1458 locations->SetOut(Location::RequiresFpuRegister());
1459 break;
1460
1461 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001462 // Processing a Dex `long-to-float' instruction.
1463 locations->SetInAt(0, Location::RequiresRegister());
1464 locations->SetOut(Location::RequiresFpuRegister());
1465 locations->AddTemp(Location::RequiresFpuRegister());
1466 locations->AddTemp(Location::RequiresFpuRegister());
1467 break;
1468
Roland Levillaincff13742014-11-17 14:32:17 +00001469 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001470 // Processing a Dex `double-to-float' instruction.
1471 locations->SetInAt(0, Location::RequiresFpuRegister());
1472 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001473 break;
1474
1475 default:
1476 LOG(FATAL) << "Unexpected type conversion from " << input_type
1477 << " to " << result_type;
1478 };
1479 break;
1480
Roland Levillaindff1f282014-11-05 14:15:05 +00001481 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001482 switch (input_type) {
1483 case Primitive::kPrimByte:
1484 case Primitive::kPrimShort:
1485 case Primitive::kPrimInt:
1486 case Primitive::kPrimChar:
1487 // Processing a Dex `int-to-double' instruction.
1488 locations->SetInAt(0, Location::RequiresRegister());
1489 locations->SetOut(Location::RequiresFpuRegister());
1490 break;
1491
1492 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001493 // Processing a Dex `long-to-double' instruction.
1494 locations->SetInAt(0, Location::RequiresRegister());
1495 locations->SetOut(Location::RequiresFpuRegister());
1496 locations->AddTemp(Location::RequiresFpuRegister());
1497 locations->AddTemp(Location::RequiresFpuRegister());
1498 break;
1499
Roland Levillaincff13742014-11-17 14:32:17 +00001500 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001501 // Processing a Dex `float-to-double' instruction.
1502 locations->SetInAt(0, Location::RequiresFpuRegister());
1503 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001504 break;
1505
1506 default:
1507 LOG(FATAL) << "Unexpected type conversion from " << input_type
1508 << " to " << result_type;
1509 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001510 break;
1511
1512 default:
1513 LOG(FATAL) << "Unexpected type conversion from " << input_type
1514 << " to " << result_type;
1515 }
1516}
1517
1518void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1519 LocationSummary* locations = conversion->GetLocations();
1520 Location out = locations->Out();
1521 Location in = locations->InAt(0);
1522 Primitive::Type result_type = conversion->GetResultType();
1523 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001524 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001525 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001526 case Primitive::kPrimByte:
1527 switch (input_type) {
1528 case Primitive::kPrimShort:
1529 case Primitive::kPrimInt:
1530 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001531 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001532 if (in.IsRegister()) {
1533 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
1534 } else if (in.IsStackSlot()) {
1535 __ movsxb(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
1536 } else {
1537 DCHECK(in.GetConstant()->IsIntConstant());
1538 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1539 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1540 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001541 break;
1542
1543 default:
1544 LOG(FATAL) << "Unexpected type conversion from " << input_type
1545 << " to " << result_type;
1546 }
1547 break;
1548
Roland Levillain01a8d712014-11-14 16:27:39 +00001549 case Primitive::kPrimShort:
1550 switch (input_type) {
1551 case Primitive::kPrimByte:
1552 case Primitive::kPrimInt:
1553 case Primitive::kPrimChar:
1554 // Processing a Dex `int-to-short' instruction.
1555 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001556 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001557 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001558 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001559 } else {
1560 DCHECK(in.GetConstant()->IsIntConstant());
1561 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001562 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001563 }
1564 break;
1565
1566 default:
1567 LOG(FATAL) << "Unexpected type conversion from " << input_type
1568 << " to " << result_type;
1569 }
1570 break;
1571
Roland Levillain946e1432014-11-11 17:35:19 +00001572 case Primitive::kPrimInt:
1573 switch (input_type) {
1574 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001575 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001576 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001577 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001578 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001579 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001580 } else {
1581 DCHECK(in.IsConstant());
1582 DCHECK(in.GetConstant()->IsLongConstant());
1583 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001584 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001585 }
1586 break;
1587
Roland Levillain3f8f9362014-12-02 17:45:01 +00001588 case Primitive::kPrimFloat: {
1589 // Processing a Dex `float-to-int' instruction.
1590 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1591 Register output = out.AsRegister<Register>();
1592 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1593 Label done, nan;
1594
1595 __ movl(output, Immediate(kPrimIntMax));
1596 // temp = int-to-float(output)
1597 __ cvtsi2ss(temp, output);
1598 // if input >= temp goto done
1599 __ comiss(input, temp);
1600 __ j(kAboveEqual, &done);
1601 // if input == NaN goto nan
1602 __ j(kUnordered, &nan);
1603 // output = float-to-int-truncate(input)
1604 __ cvttss2si(output, input);
1605 __ jmp(&done);
1606 __ Bind(&nan);
1607 // output = 0
1608 __ xorl(output, output);
1609 __ Bind(&done);
1610 break;
1611 }
1612
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001613 case Primitive::kPrimDouble: {
1614 // Processing a Dex `double-to-int' instruction.
1615 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1616 Register output = out.AsRegister<Register>();
1617 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1618 Label done, nan;
1619
1620 __ movl(output, Immediate(kPrimIntMax));
1621 // temp = int-to-double(output)
1622 __ cvtsi2sd(temp, output);
1623 // if input >= temp goto done
1624 __ comisd(input, temp);
1625 __ j(kAboveEqual, &done);
1626 // if input == NaN goto nan
1627 __ j(kUnordered, &nan);
1628 // output = double-to-int-truncate(input)
1629 __ cvttsd2si(output, input);
1630 __ jmp(&done);
1631 __ Bind(&nan);
1632 // output = 0
1633 __ xorl(output, output);
1634 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001635 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001636 }
Roland Levillain946e1432014-11-11 17:35:19 +00001637
1638 default:
1639 LOG(FATAL) << "Unexpected type conversion from " << input_type
1640 << " to " << result_type;
1641 }
1642 break;
1643
Roland Levillaindff1f282014-11-05 14:15:05 +00001644 case Primitive::kPrimLong:
1645 switch (input_type) {
1646 case Primitive::kPrimByte:
1647 case Primitive::kPrimShort:
1648 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001649 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001650 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001651 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1652 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001653 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001654 __ cdq();
1655 break;
1656
1657 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001658 // Processing a Dex `float-to-long' instruction.
1659 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001660 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1661 break;
1662
Roland Levillaindff1f282014-11-05 14:15:05 +00001663 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001664 // Processing a Dex `double-to-long' instruction.
1665 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1666 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001667 break;
1668
1669 default:
1670 LOG(FATAL) << "Unexpected type conversion from " << input_type
1671 << " to " << result_type;
1672 }
1673 break;
1674
Roland Levillain981e4542014-11-14 11:47:14 +00001675 case Primitive::kPrimChar:
1676 switch (input_type) {
1677 case Primitive::kPrimByte:
1678 case Primitive::kPrimShort:
1679 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001680 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1681 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001682 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001683 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001684 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001685 } else {
1686 DCHECK(in.GetConstant()->IsIntConstant());
1687 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001688 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001689 }
1690 break;
1691
1692 default:
1693 LOG(FATAL) << "Unexpected type conversion from " << input_type
1694 << " to " << result_type;
1695 }
1696 break;
1697
Roland Levillaindff1f282014-11-05 14:15:05 +00001698 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001699 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001700 case Primitive::kPrimByte:
1701 case Primitive::kPrimShort:
1702 case Primitive::kPrimInt:
1703 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001704 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001705 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001706 break;
1707
Roland Levillain6d0e4832014-11-27 18:31:21 +00001708 case Primitive::kPrimLong: {
1709 // Processing a Dex `long-to-float' instruction.
1710 Register low = in.AsRegisterPairLow<Register>();
1711 Register high = in.AsRegisterPairHigh<Register>();
1712 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1713 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1714 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1715
1716 // Operations use doubles for precision reasons (each 32-bit
1717 // half of a long fits in the 53-bit mantissa of a double,
1718 // but not in the 24-bit mantissa of a float). This is
1719 // especially important for the low bits. The result is
1720 // eventually converted to float.
1721
1722 // low = low - 2^31 (to prevent bit 31 of `low` to be
1723 // interpreted as a sign bit)
1724 __ subl(low, Immediate(0x80000000));
1725 // temp = int-to-double(high)
1726 __ cvtsi2sd(temp, high);
1727 // temp = temp * 2^32
1728 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
1729 __ mulsd(temp, constant);
1730 // result = int-to-double(low)
1731 __ cvtsi2sd(result, low);
1732 // result = result + 2^31 (restore the original value of `low`)
1733 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
1734 __ addsd(result, constant);
1735 // result = result + temp
1736 __ addsd(result, temp);
1737 // result = double-to-float(result)
1738 __ cvtsd2ss(result, result);
1739 break;
1740 }
1741
Roland Levillaincff13742014-11-17 14:32:17 +00001742 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001743 // Processing a Dex `double-to-float' instruction.
1744 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001745 break;
1746
1747 default:
1748 LOG(FATAL) << "Unexpected type conversion from " << input_type
1749 << " to " << result_type;
1750 };
1751 break;
1752
Roland Levillaindff1f282014-11-05 14:15:05 +00001753 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001754 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001755 case Primitive::kPrimByte:
1756 case Primitive::kPrimShort:
1757 case Primitive::kPrimInt:
1758 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001759 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001760 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001761 break;
1762
Roland Levillain647b9ed2014-11-27 12:06:00 +00001763 case Primitive::kPrimLong: {
1764 // Processing a Dex `long-to-double' instruction.
1765 Register low = in.AsRegisterPairLow<Register>();
1766 Register high = in.AsRegisterPairHigh<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001767 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1768 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1769 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001770
Roland Levillain647b9ed2014-11-27 12:06:00 +00001771 // low = low - 2^31 (to prevent bit 31 of `low` to be
1772 // interpreted as a sign bit)
1773 __ subl(low, Immediate(0x80000000));
1774 // temp = int-to-double(high)
1775 __ cvtsi2sd(temp, high);
1776 // temp = temp * 2^32
Roland Levillain6d0e4832014-11-27 18:31:21 +00001777 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001778 __ mulsd(temp, constant);
1779 // result = int-to-double(low)
1780 __ cvtsi2sd(result, low);
1781 // result = result + 2^31 (restore the original value of `low`)
Roland Levillain6d0e4832014-11-27 18:31:21 +00001782 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001783 __ addsd(result, constant);
1784 // result = result + temp
1785 __ addsd(result, temp);
1786 break;
1787 }
1788
Roland Levillaincff13742014-11-17 14:32:17 +00001789 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001790 // Processing a Dex `float-to-double' instruction.
1791 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001792 break;
1793
1794 default:
1795 LOG(FATAL) << "Unexpected type conversion from " << input_type
1796 << " to " << result_type;
1797 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001798 break;
1799
1800 default:
1801 LOG(FATAL) << "Unexpected type conversion from " << input_type
1802 << " to " << result_type;
1803 }
1804}
1805
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001806void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001807 LocationSummary* locations =
1808 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001809 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001810 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001811 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001812 locations->SetInAt(0, Location::RequiresRegister());
1813 locations->SetInAt(1, Location::Any());
1814 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001815 break;
1816 }
1817
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001818 case Primitive::kPrimFloat:
1819 case Primitive::kPrimDouble: {
1820 locations->SetInAt(0, Location::RequiresFpuRegister());
1821 locations->SetInAt(1, Location::Any());
1822 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001823 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001824 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001825
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001826 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001827 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1828 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001829 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001830}
1831
1832void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1833 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001834 Location first = locations->InAt(0);
1835 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001836 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001837 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001838 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001839 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001840 __ addl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001841 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001842 __ addl(first.AsRegister<Register>(),
1843 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001844 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001845 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001846 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001847 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001848 }
1849
1850 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001851 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001852 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1853 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001854 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001855 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1856 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001857 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001858 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001859 break;
1860 }
1861
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001862 case Primitive::kPrimFloat: {
1863 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001864 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001865 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001866 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001867 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001868 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001869 }
1870
1871 case Primitive::kPrimDouble: {
1872 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001873 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001874 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001875 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001876 }
1877 break;
1878 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001879
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001880 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001881 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001882 }
1883}
1884
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001885void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001886 LocationSummary* locations =
1887 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001888 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001889 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001890 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001891 locations->SetInAt(0, Location::RequiresRegister());
1892 locations->SetInAt(1, Location::Any());
1893 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001894 break;
1895 }
Calin Juravle11351682014-10-23 15:38:15 +01001896 case Primitive::kPrimFloat:
1897 case Primitive::kPrimDouble: {
1898 locations->SetInAt(0, Location::RequiresFpuRegister());
1899 locations->SetInAt(1, Location::RequiresFpuRegister());
1900 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001901 break;
Calin Juravle11351682014-10-23 15:38:15 +01001902 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001903
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001904 default:
Calin Juravle11351682014-10-23 15:38:15 +01001905 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001906 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001907}
1908
1909void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1910 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001911 Location first = locations->InAt(0);
1912 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001913 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001914 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001915 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001916 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001917 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001918 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001919 __ subl(first.AsRegister<Register>(),
1920 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001921 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001922 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001923 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001924 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001925 }
1926
1927 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001928 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01001929 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1930 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001931 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001932 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001933 __ sbbl(first.AsRegisterPairHigh<Register>(),
1934 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001935 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001936 break;
1937 }
1938
Calin Juravle11351682014-10-23 15:38:15 +01001939 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001940 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001941 break;
Calin Juravle11351682014-10-23 15:38:15 +01001942 }
1943
1944 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001945 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001946 break;
1947 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001948
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001949 default:
Calin Juravle11351682014-10-23 15:38:15 +01001950 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001951 }
1952}
1953
Calin Juravle34bacdf2014-10-07 20:23:36 +01001954void LocationsBuilderX86::VisitMul(HMul* mul) {
1955 LocationSummary* locations =
1956 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1957 switch (mul->GetResultType()) {
1958 case Primitive::kPrimInt:
1959 locations->SetInAt(0, Location::RequiresRegister());
1960 locations->SetInAt(1, Location::Any());
1961 locations->SetOut(Location::SameAsFirstInput());
1962 break;
1963 case Primitive::kPrimLong: {
1964 locations->SetInAt(0, Location::RequiresRegister());
1965 // TODO: Currently this handles only stack operands:
1966 // - we don't have enough registers because we currently use Quick ABI.
1967 // - by the time we have a working register allocator we will probably change the ABI
1968 // and fix the above.
1969 // - we don't have a way yet to request operands on stack but the base line compiler
1970 // will leave the operands on the stack with Any().
1971 locations->SetInAt(1, Location::Any());
1972 locations->SetOut(Location::SameAsFirstInput());
1973 // Needed for imul on 32bits with 64bits output.
1974 locations->AddTemp(Location::RegisterLocation(EAX));
1975 locations->AddTemp(Location::RegisterLocation(EDX));
1976 break;
1977 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001978 case Primitive::kPrimFloat:
1979 case Primitive::kPrimDouble: {
1980 locations->SetInAt(0, Location::RequiresFpuRegister());
1981 locations->SetInAt(1, Location::RequiresFpuRegister());
1982 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001983 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001984 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001985
1986 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001987 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001988 }
1989}
1990
1991void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1992 LocationSummary* locations = mul->GetLocations();
1993 Location first = locations->InAt(0);
1994 Location second = locations->InAt(1);
1995 DCHECK(first.Equals(locations->Out()));
1996
1997 switch (mul->GetResultType()) {
1998 case Primitive::kPrimInt: {
1999 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002000 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002001 } else if (second.IsConstant()) {
2002 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002003 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002004 } else {
2005 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002006 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002007 }
2008 break;
2009 }
2010
2011 case Primitive::kPrimLong: {
2012 DCHECK(second.IsDoubleStackSlot());
2013
2014 Register in1_hi = first.AsRegisterPairHigh<Register>();
2015 Register in1_lo = first.AsRegisterPairLow<Register>();
2016 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2017 Address in2_lo(ESP, second.GetStackIndex());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002018 Register eax = locations->GetTemp(0).AsRegister<Register>();
2019 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002020
2021 DCHECK_EQ(EAX, eax);
2022 DCHECK_EQ(EDX, edx);
2023
2024 // input: in1 - 64 bits, in2 - 64 bits
2025 // output: in1
2026 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2027 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2028 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
2029
2030 __ movl(eax, in2_hi);
2031 // eax <- in1.lo * in2.hi
2032 __ imull(eax, in1_lo);
2033 // in1.hi <- in1.hi * in2.lo
2034 __ imull(in1_hi, in2_lo);
2035 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2036 __ addl(in1_hi, eax);
2037 // move in1_lo to eax to prepare for double precision
2038 __ movl(eax, in1_lo);
2039 // edx:eax <- in1.lo * in2.lo
2040 __ mull(in2_lo);
2041 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2042 __ addl(in1_hi, edx);
2043 // in1.lo <- (in1.lo * in2.lo)[31:0];
2044 __ movl(in1_lo, eax);
2045
2046 break;
2047 }
2048
Calin Juravleb5bfa962014-10-21 18:02:24 +01002049 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002050 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002051 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002052 }
2053
2054 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002055 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002056 break;
2057 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002058
2059 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002060 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002061 }
2062}
2063
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002064void InstructionCodeGeneratorX86::PushOntoFPStack(Location source, uint32_t temp_offset,
2065 uint32_t stack_adjustment, bool is_float) {
2066 if (source.IsStackSlot()) {
2067 DCHECK(is_float);
2068 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2069 } else if (source.IsDoubleStackSlot()) {
2070 DCHECK(!is_float);
2071 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2072 } else {
2073 // Write the value to the temporary location on the stack and load to FP stack.
2074 if (is_float) {
2075 Location stack_temp = Location::StackSlot(temp_offset);
2076 codegen_->Move32(stack_temp, source);
2077 __ flds(Address(ESP, temp_offset));
2078 } else {
2079 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2080 codegen_->Move64(stack_temp, source);
2081 __ fldl(Address(ESP, temp_offset));
2082 }
2083 }
2084}
2085
2086void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2087 Primitive::Type type = rem->GetResultType();
2088 bool is_float = type == Primitive::kPrimFloat;
2089 size_t elem_size = Primitive::ComponentSize(type);
2090 LocationSummary* locations = rem->GetLocations();
2091 Location first = locations->InAt(0);
2092 Location second = locations->InAt(1);
2093 Location out = locations->Out();
2094
2095 // Create stack space for 2 elements.
2096 // TODO: enhance register allocator to ask for stack temporaries.
2097 __ subl(ESP, Immediate(2 * elem_size));
2098
2099 // Load the values to the FP stack in reverse order, using temporaries if needed.
2100 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2101 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2102
2103 // Loop doing FPREM until we stabilize.
2104 Label retry;
2105 __ Bind(&retry);
2106 __ fprem();
2107
2108 // Move FP status to AX.
2109 __ fstsw();
2110
2111 // And see if the argument reduction is complete. This is signaled by the
2112 // C2 FPU flag bit set to 0.
2113 __ andl(EAX, Immediate(kC2ConditionMask));
2114 __ j(kNotEqual, &retry);
2115
2116 // We have settled on the final value. Retrieve it into an XMM register.
2117 // Store FP top of stack to real stack.
2118 if (is_float) {
2119 __ fsts(Address(ESP, 0));
2120 } else {
2121 __ fstl(Address(ESP, 0));
2122 }
2123
2124 // Pop the 2 items from the FP stack.
2125 __ fucompp();
2126
2127 // Load the value from the stack into an XMM register.
2128 DCHECK(out.IsFpuRegister()) << out;
2129 if (is_float) {
2130 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2131 } else {
2132 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2133 }
2134
2135 // And remove the temporary stack space we allocated.
2136 __ addl(ESP, Immediate(2 * elem_size));
2137}
2138
Calin Juravlebacfec32014-11-14 15:54:36 +00002139void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2140 DCHECK(instruction->IsDiv() || instruction->IsRem());
2141
2142 LocationSummary* locations = instruction->GetLocations();
2143 Location out = locations->Out();
2144 Location first = locations->InAt(0);
2145 Location second = locations->InAt(1);
2146 bool is_div = instruction->IsDiv();
2147
2148 switch (instruction->GetResultType()) {
2149 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002150 Register second_reg = second.AsRegister<Register>();
2151 DCHECK_EQ(EAX, first.AsRegister<Register>());
2152 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002153
2154 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002155 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
2156 is_div);
Calin Juravlebacfec32014-11-14 15:54:36 +00002157 codegen_->AddSlowPath(slow_path);
2158
2159 // 0x80000000/-1 triggers an arithmetic exception!
2160 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2161 // it's safe to just use negl instead of more complex comparisons.
2162
2163 __ cmpl(second_reg, Immediate(-1));
2164 __ j(kEqual, slow_path->GetEntryLabel());
2165
2166 // edx:eax <- sign-extended of eax
2167 __ cdq();
2168 // eax = quotient, edx = remainder
2169 __ idivl(second_reg);
2170
2171 __ Bind(slow_path->GetExitLabel());
2172 break;
2173 }
2174
2175 case Primitive::kPrimLong: {
2176 InvokeRuntimeCallingConvention calling_convention;
2177 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2178 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2179 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2180 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2181 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2182 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2183
2184 if (is_div) {
2185 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2186 } else {
2187 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2188 }
2189 uint32_t dex_pc = is_div
2190 ? instruction->AsDiv()->GetDexPc()
2191 : instruction->AsRem()->GetDexPc();
2192 codegen_->RecordPcInfo(instruction, dex_pc);
2193
2194 break;
2195 }
2196
2197 default:
2198 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2199 }
2200}
2201
Calin Juravle7c4954d2014-10-28 16:57:40 +00002202void LocationsBuilderX86::VisitDiv(HDiv* div) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002203 LocationSummary::CallKind call_kind = div->GetResultType() == Primitive::kPrimLong
2204 ? LocationSummary::kCall
2205 : LocationSummary::kNoCall;
2206 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2207
Calin Juravle7c4954d2014-10-28 16:57:40 +00002208 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002209 case Primitive::kPrimInt: {
2210 locations->SetInAt(0, Location::RegisterLocation(EAX));
2211 locations->SetInAt(1, Location::RequiresRegister());
2212 locations->SetOut(Location::SameAsFirstInput());
2213 // Intel uses edx:eax as the dividend.
2214 locations->AddTemp(Location::RegisterLocation(EDX));
2215 break;
2216 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002217 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002218 InvokeRuntimeCallingConvention calling_convention;
2219 locations->SetInAt(0, Location::RegisterPairLocation(
2220 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2221 locations->SetInAt(1, Location::RegisterPairLocation(
2222 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2223 // Runtime helper puts the result in EAX, EDX.
2224 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002225 break;
2226 }
2227 case Primitive::kPrimFloat:
2228 case Primitive::kPrimDouble: {
2229 locations->SetInAt(0, Location::RequiresFpuRegister());
2230 locations->SetInAt(1, Location::RequiresFpuRegister());
2231 locations->SetOut(Location::SameAsFirstInput());
2232 break;
2233 }
2234
2235 default:
2236 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2237 }
2238}
2239
2240void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2241 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002242 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002243 Location first = locations->InAt(0);
2244 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002245
2246 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002247 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002248 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002249 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002250 break;
2251 }
2252
2253 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002254 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002255 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002256 break;
2257 }
2258
2259 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002260 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002261 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002262 break;
2263 }
2264
2265 default:
2266 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2267 }
2268}
2269
Calin Juravlebacfec32014-11-14 15:54:36 +00002270void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002271 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002272 LocationSummary* locations =
2273 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravlebacfec32014-11-14 15:54:36 +00002274
Calin Juravled2ec87d2014-12-08 14:24:46 +00002275 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002276 case Primitive::kPrimInt: {
2277 locations->SetInAt(0, Location::RegisterLocation(EAX));
2278 locations->SetInAt(1, Location::RequiresRegister());
2279 locations->SetOut(Location::RegisterLocation(EDX));
2280 break;
2281 }
2282 case Primitive::kPrimLong: {
2283 InvokeRuntimeCallingConvention calling_convention;
2284 locations->SetInAt(0, Location::RegisterPairLocation(
2285 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2286 locations->SetInAt(1, Location::RegisterPairLocation(
2287 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2288 // Runtime helper puts the result in EAX, EDX.
2289 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2290 break;
2291 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002292 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002293 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002294 locations->SetInAt(0, Location::Any());
2295 locations->SetInAt(1, Location::Any());
2296 locations->SetOut(Location::RequiresFpuRegister());
2297 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002298 break;
2299 }
2300
2301 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002302 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002303 }
2304}
2305
2306void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2307 Primitive::Type type = rem->GetResultType();
2308 switch (type) {
2309 case Primitive::kPrimInt:
2310 case Primitive::kPrimLong: {
2311 GenerateDivRemIntegral(rem);
2312 break;
2313 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002314 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002315 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002316 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002317 break;
2318 }
2319 default:
2320 LOG(FATAL) << "Unexpected rem type " << type;
2321 }
2322}
2323
Calin Juravled0d48522014-11-04 16:40:20 +00002324void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2325 LocationSummary* locations =
2326 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002327 switch (instruction->GetType()) {
2328 case Primitive::kPrimInt: {
2329 locations->SetInAt(0, Location::Any());
2330 break;
2331 }
2332 case Primitive::kPrimLong: {
2333 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2334 if (!instruction->IsConstant()) {
2335 locations->AddTemp(Location::RequiresRegister());
2336 }
2337 break;
2338 }
2339 default:
2340 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2341 }
Calin Juravled0d48522014-11-04 16:40:20 +00002342 if (instruction->HasUses()) {
2343 locations->SetOut(Location::SameAsFirstInput());
2344 }
2345}
2346
2347void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2348 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2349 codegen_->AddSlowPath(slow_path);
2350
2351 LocationSummary* locations = instruction->GetLocations();
2352 Location value = locations->InAt(0);
2353
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002354 switch (instruction->GetType()) {
2355 case Primitive::kPrimInt: {
2356 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002357 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002358 __ j(kEqual, slow_path->GetEntryLabel());
2359 } else if (value.IsStackSlot()) {
2360 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2361 __ j(kEqual, slow_path->GetEntryLabel());
2362 } else {
2363 DCHECK(value.IsConstant()) << value;
2364 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2365 __ jmp(slow_path->GetEntryLabel());
2366 }
2367 }
2368 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002369 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002370 case Primitive::kPrimLong: {
2371 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002372 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002373 __ movl(temp, value.AsRegisterPairLow<Register>());
2374 __ orl(temp, value.AsRegisterPairHigh<Register>());
2375 __ j(kEqual, slow_path->GetEntryLabel());
2376 } else {
2377 DCHECK(value.IsConstant()) << value;
2378 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2379 __ jmp(slow_path->GetEntryLabel());
2380 }
2381 }
2382 break;
2383 }
2384 default:
2385 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002386 }
Calin Juravled0d48522014-11-04 16:40:20 +00002387}
2388
Calin Juravle9aec02f2014-11-18 23:06:35 +00002389void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2390 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2391
2392 LocationSummary* locations =
2393 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2394
2395 switch (op->GetResultType()) {
2396 case Primitive::kPrimInt: {
2397 locations->SetInAt(0, Location::RequiresRegister());
2398 // The shift count needs to be in CL.
2399 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2400 locations->SetOut(Location::SameAsFirstInput());
2401 break;
2402 }
2403 case Primitive::kPrimLong: {
2404 locations->SetInAt(0, Location::RequiresRegister());
2405 // The shift count needs to be in CL.
2406 locations->SetInAt(1, Location::RegisterLocation(ECX));
2407 locations->SetOut(Location::SameAsFirstInput());
2408 break;
2409 }
2410 default:
2411 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2412 }
2413}
2414
2415void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2416 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2417
2418 LocationSummary* locations = op->GetLocations();
2419 Location first = locations->InAt(0);
2420 Location second = locations->InAt(1);
2421 DCHECK(first.Equals(locations->Out()));
2422
2423 switch (op->GetResultType()) {
2424 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002425 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002426 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002427 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002428 DCHECK_EQ(ECX, second_reg);
2429 if (op->IsShl()) {
2430 __ shll(first_reg, second_reg);
2431 } else if (op->IsShr()) {
2432 __ sarl(first_reg, second_reg);
2433 } else {
2434 __ shrl(first_reg, second_reg);
2435 }
2436 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002437 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002438 if (op->IsShl()) {
2439 __ shll(first_reg, imm);
2440 } else if (op->IsShr()) {
2441 __ sarl(first_reg, imm);
2442 } else {
2443 __ shrl(first_reg, imm);
2444 }
2445 }
2446 break;
2447 }
2448 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002449 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002450 DCHECK_EQ(ECX, second_reg);
2451 if (op->IsShl()) {
2452 GenerateShlLong(first, second_reg);
2453 } else if (op->IsShr()) {
2454 GenerateShrLong(first, second_reg);
2455 } else {
2456 GenerateUShrLong(first, second_reg);
2457 }
2458 break;
2459 }
2460 default:
2461 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2462 }
2463}
2464
2465void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2466 Label done;
2467 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2468 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2469 __ testl(shifter, Immediate(32));
2470 __ j(kEqual, &done);
2471 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2472 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2473 __ Bind(&done);
2474}
2475
2476void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2477 Label done;
2478 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2479 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2480 __ testl(shifter, Immediate(32));
2481 __ j(kEqual, &done);
2482 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2483 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2484 __ Bind(&done);
2485}
2486
2487void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2488 Label done;
2489 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2490 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2491 __ testl(shifter, Immediate(32));
2492 __ j(kEqual, &done);
2493 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2494 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2495 __ Bind(&done);
2496}
2497
2498void LocationsBuilderX86::VisitShl(HShl* shl) {
2499 HandleShift(shl);
2500}
2501
2502void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2503 HandleShift(shl);
2504}
2505
2506void LocationsBuilderX86::VisitShr(HShr* shr) {
2507 HandleShift(shr);
2508}
2509
2510void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2511 HandleShift(shr);
2512}
2513
2514void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2515 HandleShift(ushr);
2516}
2517
2518void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2519 HandleShift(ushr);
2520}
2521
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002522void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002523 LocationSummary* locations =
2524 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002525 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002526 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002527 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2528 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002529}
2530
2531void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2532 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002533 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002534 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002535
Nicolas Geoffray707c8092014-04-04 10:50:14 +01002536 __ fs()->call(
2537 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002538
Nicolas Geoffray39468442014-09-02 15:17:15 +01002539 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002540 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002541}
2542
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002543void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2544 LocationSummary* locations =
2545 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2546 locations->SetOut(Location::RegisterLocation(EAX));
2547 InvokeRuntimeCallingConvention calling_convention;
2548 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002549 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2550 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002551}
2552
2553void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2554 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002555 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002556 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2557
2558 __ fs()->call(
2559 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
2560
2561 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2562 DCHECK(!codegen_->IsLeafMethod());
2563}
2564
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002565void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002566 LocationSummary* locations =
2567 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002568 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2569 if (location.IsStackSlot()) {
2570 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2571 } else if (location.IsDoubleStackSlot()) {
2572 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002573 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002574 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002575}
2576
2577void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002578 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002579}
2580
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002581void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002582 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002583 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002584 locations->SetInAt(0, Location::RequiresRegister());
2585 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002586}
2587
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002588void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2589 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002590 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002591 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002592 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002593 switch (not_->InputAt(0)->GetType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002594 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002595 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002596 break;
2597
2598 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002599 __ notl(out.AsRegisterPairLow<Register>());
2600 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002601 break;
2602
2603 default:
2604 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2605 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002606}
2607
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002608void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002609 LocationSummary* locations =
2610 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002611 switch (compare->InputAt(0)->GetType()) {
2612 case Primitive::kPrimLong: {
2613 locations->SetInAt(0, Location::RequiresRegister());
2614 // TODO: we set any here but we don't handle constants
2615 locations->SetInAt(1, Location::Any());
2616 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2617 break;
2618 }
2619 case Primitive::kPrimFloat:
2620 case Primitive::kPrimDouble: {
2621 locations->SetInAt(0, Location::RequiresFpuRegister());
2622 locations->SetInAt(1, Location::RequiresFpuRegister());
2623 locations->SetOut(Location::RequiresRegister());
2624 break;
2625 }
2626 default:
2627 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2628 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002629}
2630
2631void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002632 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002633 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002634 Location left = locations->InAt(0);
2635 Location right = locations->InAt(1);
2636
2637 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002638 switch (compare->InputAt(0)->GetType()) {
2639 case Primitive::kPrimLong: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002640 if (right.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002641 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002642 } else {
2643 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002644 __ cmpl(left.AsRegisterPairHigh<Register>(),
2645 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002646 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002647 __ j(kLess, &less); // Signed compare.
2648 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002649 if (right.IsRegisterPair()) {
2650 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002651 } else {
2652 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002653 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002654 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002655 break;
2656 }
2657 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002658 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002659 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
2660 break;
2661 }
2662 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002663 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002664 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002665 break;
2666 }
2667 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002668 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002669 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002670 __ movl(out, Immediate(0));
2671 __ j(kEqual, &done);
2672 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
2673
2674 __ Bind(&greater);
2675 __ movl(out, Immediate(1));
2676 __ jmp(&done);
2677
2678 __ Bind(&less);
2679 __ movl(out, Immediate(-1));
2680
2681 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002682}
2683
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002684void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002685 LocationSummary* locations =
2686 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002687 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2688 locations->SetInAt(i, Location::Any());
2689 }
2690 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002691}
2692
2693void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002694 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002695 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002696}
2697
Calin Juravle52c48962014-12-16 17:02:57 +00002698void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
2699 /*
2700 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2701 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2702 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2703 */
2704 switch (kind) {
2705 case MemBarrierKind::kAnyAny: {
2706 __ mfence();
2707 break;
2708 }
2709 case MemBarrierKind::kAnyStore:
2710 case MemBarrierKind::kLoadAny:
2711 case MemBarrierKind::kStoreStore: {
2712 // nop
2713 break;
2714 }
2715 default:
2716 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002717 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002718}
2719
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002720
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002721void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002722 Label is_null;
2723 __ testl(value, value);
2724 __ j(kEqual, &is_null);
2725 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2726 __ movl(temp, object);
2727 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002728 __ movb(Address(temp, card, TIMES_1, 0),
2729 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002730 __ Bind(&is_null);
2731}
2732
Calin Juravle52c48962014-12-16 17:02:57 +00002733void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
2734 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002735 LocationSummary* locations =
2736 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002737 locations->SetInAt(0, Location::RequiresRegister());
2738 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle52c48962014-12-16 17:02:57 +00002739
2740 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
2741 // Long values can be loaded atomically into an XMM using movsd.
2742 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
2743 // and then copy the XMM into the output 32bits at a time).
2744 locations->AddTemp(Location::RequiresFpuRegister());
2745 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002746}
2747
Calin Juravle52c48962014-12-16 17:02:57 +00002748void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
2749 const FieldInfo& field_info) {
2750 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002751
Calin Juravle52c48962014-12-16 17:02:57 +00002752 LocationSummary* locations = instruction->GetLocations();
2753 Register base = locations->InAt(0).AsRegister<Register>();
2754 Location out = locations->Out();
2755 bool is_volatile = field_info.IsVolatile();
2756 Primitive::Type field_type = field_info.GetFieldType();
2757 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2758
2759 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002760 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00002761 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002762 break;
2763 }
2764
2765 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002766 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002767 break;
2768 }
2769
2770 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00002771 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002772 break;
2773 }
2774
2775 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002776 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002777 break;
2778 }
2779
2780 case Primitive::kPrimInt:
2781 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002782 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002783 break;
2784 }
2785
2786 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00002787 if (is_volatile) {
2788 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2789 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002790 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002791 __ movd(out.AsRegisterPairLow<Register>(), temp);
2792 __ psrlq(temp, Immediate(32));
2793 __ movd(out.AsRegisterPairHigh<Register>(), temp);
2794 } else {
2795 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002796 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002797 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
2798 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002799 break;
2800 }
2801
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002802 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002803 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002804 break;
2805 }
2806
2807 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002808 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002809 break;
2810 }
2811
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002812 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00002813 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002814 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002815 }
Calin Juravle52c48962014-12-16 17:02:57 +00002816
Calin Juravle77520bc2015-01-12 18:45:46 +00002817 // Longs are handled in the switch.
2818 if (field_type != Primitive::kPrimLong) {
2819 codegen_->MaybeRecordImplicitNullCheck(instruction);
2820 }
2821
Calin Juravle52c48962014-12-16 17:02:57 +00002822 if (is_volatile) {
2823 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2824 }
2825}
2826
2827void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
2828 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2829
2830 LocationSummary* locations =
2831 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2832 locations->SetInAt(0, Location::RequiresRegister());
2833 bool is_volatile = field_info.IsVolatile();
2834 Primitive::Type field_type = field_info.GetFieldType();
2835 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2836 || (field_type == Primitive::kPrimByte);
2837
2838 // The register allocator does not support multiple
2839 // inputs that die at entry with one in a specific register.
2840 if (is_byte_type) {
2841 // Ensure the value is in a byte register.
2842 locations->SetInAt(1, Location::RegisterLocation(EAX));
2843 } else {
2844 locations->SetInAt(1, Location::RequiresRegister());
2845 }
2846 // Temporary registers for the write barrier.
2847 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2848 locations->AddTemp(Location::RequiresRegister());
2849 // Ensure the card is in a byte register.
2850 locations->AddTemp(Location::RegisterLocation(ECX));
2851 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
2852 // 64bits value can be atomically written to an address with movsd and an XMM register.
2853 // We need two XMM registers because there's no easier way to (bit) copy a register pair
2854 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
2855 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
2856 // isolated cases when we need this it isn't worth adding the extra complexity.
2857 locations->AddTemp(Location::RequiresFpuRegister());
2858 locations->AddTemp(Location::RequiresFpuRegister());
2859 }
2860}
2861
2862void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
2863 const FieldInfo& field_info) {
2864 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2865
2866 LocationSummary* locations = instruction->GetLocations();
2867 Register base = locations->InAt(0).AsRegister<Register>();
2868 Location value = locations->InAt(1);
2869 bool is_volatile = field_info.IsVolatile();
2870 Primitive::Type field_type = field_info.GetFieldType();
2871 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2872
2873 if (is_volatile) {
2874 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2875 }
2876
2877 switch (field_type) {
2878 case Primitive::kPrimBoolean:
2879 case Primitive::kPrimByte: {
2880 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
2881 break;
2882 }
2883
2884 case Primitive::kPrimShort:
2885 case Primitive::kPrimChar: {
2886 __ movw(Address(base, offset), value.AsRegister<Register>());
2887 break;
2888 }
2889
2890 case Primitive::kPrimInt:
2891 case Primitive::kPrimNot: {
2892 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00002893 break;
2894 }
2895
2896 case Primitive::kPrimLong: {
2897 if (is_volatile) {
2898 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2899 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
2900 __ movd(temp1, value.AsRegisterPairLow<Register>());
2901 __ movd(temp2, value.AsRegisterPairHigh<Register>());
2902 __ punpckldq(temp1, temp2);
2903 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00002904 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002905 } else {
2906 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00002907 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002908 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
2909 }
2910 break;
2911 }
2912
2913 case Primitive::kPrimFloat: {
2914 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
2915 break;
2916 }
2917
2918 case Primitive::kPrimDouble: {
2919 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
2920 break;
2921 }
2922
2923 case Primitive::kPrimVoid:
2924 LOG(FATAL) << "Unreachable type " << field_type;
2925 UNREACHABLE();
2926 }
2927
Calin Juravle77520bc2015-01-12 18:45:46 +00002928 // Longs are handled in the switch.
2929 if (field_type != Primitive::kPrimLong) {
2930 codegen_->MaybeRecordImplicitNullCheck(instruction);
2931 }
2932
2933 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2934 Register temp = locations->GetTemp(0).AsRegister<Register>();
2935 Register card = locations->GetTemp(1).AsRegister<Register>();
2936 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
2937 }
2938
Calin Juravle52c48962014-12-16 17:02:57 +00002939 if (is_volatile) {
2940 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2941 }
2942}
2943
2944void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2945 HandleFieldGet(instruction, instruction->GetFieldInfo());
2946}
2947
2948void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2949 HandleFieldGet(instruction, instruction->GetFieldInfo());
2950}
2951
2952void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2953 HandleFieldSet(instruction, instruction->GetFieldInfo());
2954}
2955
2956void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2957 HandleFieldSet(instruction, instruction->GetFieldInfo());
2958}
2959
2960void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2961 HandleFieldSet(instruction, instruction->GetFieldInfo());
2962}
2963
2964void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2965 HandleFieldSet(instruction, instruction->GetFieldInfo());
2966}
2967
2968void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2969 HandleFieldGet(instruction, instruction->GetFieldInfo());
2970}
2971
2972void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2973 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002974}
2975
2976void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002977 LocationSummary* locations =
2978 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002979 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
2980 ? Location::RequiresRegister()
2981 : Location::Any();
2982 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002983 if (instruction->HasUses()) {
2984 locations->SetOut(Location::SameAsFirstInput());
2985 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002986}
2987
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002988void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002989 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2990 return;
2991 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002992 LocationSummary* locations = instruction->GetLocations();
2993 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002994
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002995 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
2996 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2997}
2998
2999void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003000 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003001 codegen_->AddSlowPath(slow_path);
3002
3003 LocationSummary* locations = instruction->GetLocations();
3004 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003005
3006 if (obj.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003007 __ cmpl(obj.AsRegister<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003008 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003009 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003010 } else {
3011 DCHECK(obj.IsConstant()) << obj;
3012 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3013 __ jmp(slow_path->GetEntryLabel());
3014 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003015 }
3016 __ j(kEqual, slow_path->GetEntryLabel());
3017}
3018
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003019void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3020 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3021 GenerateImplicitNullCheck(instruction);
3022 } else {
3023 GenerateExplicitNullCheck(instruction);
3024 }
3025}
3026
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003027void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003028 LocationSummary* locations =
3029 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003030 locations->SetInAt(0, Location::RequiresRegister());
3031 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3032 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003033}
3034
3035void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3036 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003037 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003038 Location index = locations->InAt(1);
3039
Calin Juravle77520bc2015-01-12 18:45:46 +00003040 Primitive::Type type = instruction->GetType();
3041 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003042 case Primitive::kPrimBoolean: {
3043 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003044 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003045 if (index.IsConstant()) {
3046 __ movzxb(out, Address(obj,
3047 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3048 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003049 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003050 }
3051 break;
3052 }
3053
3054 case Primitive::kPrimByte: {
3055 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003056 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003057 if (index.IsConstant()) {
3058 __ movsxb(out, Address(obj,
3059 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3060 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003061 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003062 }
3063 break;
3064 }
3065
3066 case Primitive::kPrimShort: {
3067 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003068 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003069 if (index.IsConstant()) {
3070 __ movsxw(out, Address(obj,
3071 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3072 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003073 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003074 }
3075 break;
3076 }
3077
3078 case Primitive::kPrimChar: {
3079 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003080 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003081 if (index.IsConstant()) {
3082 __ movzxw(out, Address(obj,
3083 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3084 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003085 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003086 }
3087 break;
3088 }
3089
3090 case Primitive::kPrimInt:
3091 case Primitive::kPrimNot: {
3092 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003093 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003094 if (index.IsConstant()) {
3095 __ movl(out, Address(obj,
3096 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3097 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003098 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003099 }
3100 break;
3101 }
3102
3103 case Primitive::kPrimLong: {
3104 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003105 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003106 if (index.IsConstant()) {
3107 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003108 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003109 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003110 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003111 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003112 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003113 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003114 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003115 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003116 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003117 }
3118 break;
3119 }
3120
3121 case Primitive::kPrimFloat:
3122 case Primitive::kPrimDouble:
Calin Juravle77520bc2015-01-12 18:45:46 +00003123 LOG(FATAL) << "Unimplemented register type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003124 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003125 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003126 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003127 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003128 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003129
3130 if (type != Primitive::kPrimLong) {
3131 codegen_->MaybeRecordImplicitNullCheck(instruction);
3132 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003133}
3134
3135void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003136 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003137 bool needs_write_barrier =
3138 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3139
3140 DCHECK(kFollowsQuickABI);
3141 bool not_enough_registers = needs_write_barrier
3142 && !instruction->GetValue()->IsConstant()
3143 && !instruction->GetIndex()->IsConstant();
3144 bool needs_runtime_call = instruction->NeedsTypeCheck() || not_enough_registers;
3145
Nicolas Geoffray39468442014-09-02 15:17:15 +01003146 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3147 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003148 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003149
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003150 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003151 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003152 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3153 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3154 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003155 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003156 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3157 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003158 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003159 // In case of a byte operation, the register allocator does not support multiple
3160 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003161 locations->SetInAt(0, Location::RequiresRegister());
3162 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003163 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003164 // Ensure the value is in a byte register.
3165 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003166 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003167 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003168 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003169 // Temporary registers for the write barrier.
3170 if (needs_write_barrier) {
3171 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003172 // Ensure the card is in a byte register.
3173 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003174 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003175 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003176}
3177
3178void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3179 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003180 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003181 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003182 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003183 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003184 bool needs_runtime_call = locations->WillCall();
3185 bool needs_write_barrier =
3186 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003187
3188 switch (value_type) {
3189 case Primitive::kPrimBoolean:
3190 case Primitive::kPrimByte: {
3191 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003192 if (index.IsConstant()) {
3193 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003194 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003195 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003196 } else {
3197 __ movb(Address(obj, offset),
3198 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3199 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003200 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003201 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003202 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003203 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003204 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003205 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003206 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3207 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003208 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003209 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003210 break;
3211 }
3212
3213 case Primitive::kPrimShort:
3214 case Primitive::kPrimChar: {
3215 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003216 if (index.IsConstant()) {
3217 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003218 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003219 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003220 } else {
3221 __ movw(Address(obj, offset),
3222 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3223 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003224 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003225 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003226 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3227 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003228 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003229 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003230 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3231 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003232 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003233 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003234 break;
3235 }
3236
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003237 case Primitive::kPrimInt:
3238 case Primitive::kPrimNot: {
3239 if (!needs_runtime_call) {
3240 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3241 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003242 size_t offset =
3243 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003244 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003245 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003246 } else {
3247 DCHECK(value.IsConstant()) << value;
3248 __ movl(Address(obj, offset),
3249 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3250 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003251 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003252 DCHECK(index.IsRegister()) << index;
3253 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003254 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3255 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003256 } else {
3257 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003258 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003259 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3260 }
3261 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003262 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003263
3264 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003265 Register temp = locations->GetTemp(0).AsRegister<Register>();
3266 Register card = locations->GetTemp(1).AsRegister<Register>();
3267 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003268 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003269 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003270 DCHECK_EQ(value_type, Primitive::kPrimNot);
3271 DCHECK(!codegen_->IsLeafMethod());
3272 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3273 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003274 }
3275 break;
3276 }
3277
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003278 case Primitive::kPrimLong: {
3279 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003280 if (index.IsConstant()) {
3281 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003282 if (value.IsRegisterPair()) {
3283 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003284 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003285 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003286 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003287 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003288 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3289 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003290 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003291 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3292 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003293 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003294 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003295 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003296 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003297 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003298 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003299 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003300 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003301 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003302 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003303 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003304 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003305 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003306 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003307 Immediate(High32Bits(val)));
3308 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003309 }
3310 break;
3311 }
3312
3313 case Primitive::kPrimFloat:
3314 case Primitive::kPrimDouble:
3315 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003316 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003317 case Primitive::kPrimVoid:
3318 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003319 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003320 }
3321}
3322
3323void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3324 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003325 locations->SetInAt(0, Location::RequiresRegister());
3326 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003327 instruction->SetLocations(locations);
3328}
3329
3330void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3331 LocationSummary* locations = instruction->GetLocations();
3332 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003333 Register obj = locations->InAt(0).AsRegister<Register>();
3334 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003335 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003336 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003337}
3338
3339void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003340 LocationSummary* locations =
3341 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003342 locations->SetInAt(0, Location::RequiresRegister());
3343 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003344 if (instruction->HasUses()) {
3345 locations->SetOut(Location::SameAsFirstInput());
3346 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003347}
3348
3349void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3350 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003351 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01003352 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003353 codegen_->AddSlowPath(slow_path);
3354
Roland Levillain271ab9c2014-11-27 15:23:57 +00003355 Register index = locations->InAt(0).AsRegister<Register>();
3356 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003357
3358 __ cmpl(index, length);
3359 __ j(kAboveEqual, slow_path->GetEntryLabel());
3360}
3361
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003362void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3363 temp->SetLocations(nullptr);
3364}
3365
3366void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3367 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003368 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003369}
3370
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003371void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003372 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003373 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003374}
3375
3376void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003377 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3378}
3379
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003380void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3381 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3382}
3383
3384void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003385 HBasicBlock* block = instruction->GetBlock();
3386 if (block->GetLoopInformation() != nullptr) {
3387 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3388 // The back edge will generate the suspend check.
3389 return;
3390 }
3391 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3392 // The goto will generate the suspend check.
3393 return;
3394 }
3395 GenerateSuspendCheck(instruction, nullptr);
3396}
3397
3398void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3399 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003400 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003401 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003402 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003403 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003404 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003405 if (successor == nullptr) {
3406 __ j(kNotEqual, slow_path->GetEntryLabel());
3407 __ Bind(slow_path->GetReturnLabel());
3408 } else {
3409 __ j(kEqual, codegen_->GetLabelOf(successor));
3410 __ jmp(slow_path->GetEntryLabel());
3411 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003412}
3413
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003414X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3415 return codegen_->GetAssembler();
3416}
3417
3418void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
3419 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003420 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003421 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3422 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
3423 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
3424}
3425
3426void ParallelMoveResolverX86::EmitMove(size_t index) {
3427 MoveOperands* move = moves_.Get(index);
3428 Location source = move->GetSource();
3429 Location destination = move->GetDestination();
3430
3431 if (source.IsRegister()) {
3432 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003433 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003434 } else {
3435 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003436 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003437 }
3438 } else if (source.IsStackSlot()) {
3439 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003440 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003441 } else {
3442 DCHECK(destination.IsStackSlot());
3443 MoveMemoryToMemory(destination.GetStackIndex(),
3444 source.GetStackIndex());
3445 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003446 } else if (source.IsConstant()) {
3447 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
3448 Immediate imm(instruction->AsIntConstant()->GetValue());
3449 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003450 __ movl(destination.AsRegister<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003451 } else {
3452 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3453 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003454 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003455 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003456 }
3457}
3458
3459void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003460 Register suggested_scratch = reg == EAX ? EBX : EAX;
3461 ScratchRegisterScope ensure_scratch(
3462 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3463
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003464 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3465 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
3466 __ movl(Address(ESP, mem + stack_offset), reg);
3467 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
3468}
3469
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003470void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
3471 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003472 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3473
3474 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003475 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003476 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3477
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003478 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
3479 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
3480 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
3481 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
3482 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
3483 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
3484}
3485
3486void ParallelMoveResolverX86::EmitSwap(size_t index) {
3487 MoveOperands* move = moves_.Get(index);
3488 Location source = move->GetSource();
3489 Location destination = move->GetDestination();
3490
3491 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003492 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003493 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003494 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003495 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003496 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003497 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3498 Exchange(destination.GetStackIndex(), source.GetStackIndex());
3499 } else {
3500 LOG(FATAL) << "Unimplemented";
3501 }
3502}
3503
3504void ParallelMoveResolverX86::SpillScratch(int reg) {
3505 __ pushl(static_cast<Register>(reg));
3506}
3507
3508void ParallelMoveResolverX86::RestoreScratch(int reg) {
3509 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003510}
3511
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003512void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003513 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3514 ? LocationSummary::kCallOnSlowPath
3515 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003516 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003517 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003518 locations->SetOut(Location::RequiresRegister());
3519}
3520
3521void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003522 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003523 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003524 DCHECK(!cls->CanCallRuntime());
3525 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003526 codegen_->LoadCurrentMethod(out);
3527 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3528 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003529 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003530 codegen_->LoadCurrentMethod(out);
3531 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3532 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003533
3534 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3535 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3536 codegen_->AddSlowPath(slow_path);
3537 __ testl(out, out);
3538 __ j(kEqual, slow_path->GetEntryLabel());
3539 if (cls->MustGenerateClinitCheck()) {
3540 GenerateClassInitializationCheck(slow_path, out);
3541 } else {
3542 __ Bind(slow_path->GetExitLabel());
3543 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003544 }
3545}
3546
3547void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
3548 LocationSummary* locations =
3549 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3550 locations->SetInAt(0, Location::RequiresRegister());
3551 if (check->HasUses()) {
3552 locations->SetOut(Location::SameAsFirstInput());
3553 }
3554}
3555
3556void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003557 // We assume the class to not be null.
3558 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3559 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003560 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003561 GenerateClassInitializationCheck(slow_path,
3562 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003563}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003564
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003565void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
3566 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003567 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3568 Immediate(mirror::Class::kStatusInitialized));
3569 __ j(kLess, slow_path->GetEntryLabel());
3570 __ Bind(slow_path->GetExitLabel());
3571 // No need for memory fence, thanks to the X86 memory model.
3572}
3573
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003574void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
3575 LocationSummary* locations =
3576 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3577 locations->SetOut(Location::RequiresRegister());
3578}
3579
3580void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
3581 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
3582 codegen_->AddSlowPath(slow_path);
3583
Roland Levillain271ab9c2014-11-27 15:23:57 +00003584 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003585 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08003586 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3587 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003588 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3589 __ testl(out, out);
3590 __ j(kEqual, slow_path->GetEntryLabel());
3591 __ Bind(slow_path->GetExitLabel());
3592}
3593
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003594void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
3595 LocationSummary* locations =
3596 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3597 locations->SetOut(Location::RequiresRegister());
3598}
3599
3600void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
3601 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003602 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003603 __ fs()->movl(address, Immediate(0));
3604}
3605
3606void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
3607 LocationSummary* locations =
3608 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3609 InvokeRuntimeCallingConvention calling_convention;
3610 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3611}
3612
3613void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
3614 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
3615 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3616}
3617
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003618void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003619 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3620 ? LocationSummary::kNoCall
3621 : LocationSummary::kCallOnSlowPath;
3622 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3623 locations->SetInAt(0, Location::RequiresRegister());
3624 locations->SetInAt(1, Location::Any());
3625 locations->SetOut(Location::RequiresRegister());
3626}
3627
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003628void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003629 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003630 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003631 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003632 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003633 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3634 Label done, zero;
3635 SlowPathCodeX86* slow_path = nullptr;
3636
3637 // Return 0 if `obj` is null.
3638 // TODO: avoid this check if we know obj is not null.
3639 __ testl(obj, obj);
3640 __ j(kEqual, &zero);
3641 __ movl(out, Address(obj, class_offset));
3642 // Compare the class of `obj` with `cls`.
3643 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003644 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003645 } else {
3646 DCHECK(cls.IsStackSlot()) << cls;
3647 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
3648 }
3649
3650 if (instruction->IsClassFinal()) {
3651 // Classes must be equal for the instanceof to succeed.
3652 __ j(kNotEqual, &zero);
3653 __ movl(out, Immediate(1));
3654 __ jmp(&done);
3655 } else {
3656 // If the classes are not equal, we go into a slow path.
3657 DCHECK(locations->OnlyCallsOnSlowPath());
3658 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003659 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003660 codegen_->AddSlowPath(slow_path);
3661 __ j(kNotEqual, slow_path->GetEntryLabel());
3662 __ movl(out, Immediate(1));
3663 __ jmp(&done);
3664 }
3665 __ Bind(&zero);
3666 __ movl(out, Immediate(0));
3667 if (slow_path != nullptr) {
3668 __ Bind(slow_path->GetExitLabel());
3669 }
3670 __ Bind(&done);
3671}
3672
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003673void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
3674 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3675 instruction, LocationSummary::kCallOnSlowPath);
3676 locations->SetInAt(0, Location::RequiresRegister());
3677 locations->SetInAt(1, Location::Any());
3678 locations->AddTemp(Location::RequiresRegister());
3679}
3680
3681void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
3682 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003683 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003684 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003685 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003686 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3687 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
3688 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3689 codegen_->AddSlowPath(slow_path);
3690
3691 // TODO: avoid this check if we know obj is not null.
3692 __ testl(obj, obj);
3693 __ j(kEqual, slow_path->GetExitLabel());
3694 __ movl(temp, Address(obj, class_offset));
3695
3696 // Compare the class of `obj` with `cls`.
3697 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003698 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003699 } else {
3700 DCHECK(cls.IsStackSlot()) << cls;
3701 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
3702 }
3703
3704 __ j(kNotEqual, slow_path->GetEntryLabel());
3705 __ Bind(slow_path->GetExitLabel());
3706}
3707
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003708void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3709 LocationSummary* locations =
3710 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3711 InvokeRuntimeCallingConvention calling_convention;
3712 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3713}
3714
3715void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3716 __ fs()->call(Address::Absolute(instruction->IsEnter()
3717 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
3718 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
3719 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3720}
3721
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003722void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3723void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3724void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3725
3726void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3727 LocationSummary* locations =
3728 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3729 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3730 || instruction->GetResultType() == Primitive::kPrimLong);
3731 locations->SetInAt(0, Location::RequiresRegister());
3732 locations->SetInAt(1, Location::Any());
3733 locations->SetOut(Location::SameAsFirstInput());
3734}
3735
3736void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
3737 HandleBitwiseOperation(instruction);
3738}
3739
3740void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
3741 HandleBitwiseOperation(instruction);
3742}
3743
3744void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
3745 HandleBitwiseOperation(instruction);
3746}
3747
3748void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3749 LocationSummary* locations = instruction->GetLocations();
3750 Location first = locations->InAt(0);
3751 Location second = locations->InAt(1);
3752 DCHECK(first.Equals(locations->Out()));
3753
3754 if (instruction->GetResultType() == Primitive::kPrimInt) {
3755 if (second.IsRegister()) {
3756 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003757 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003758 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003759 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003760 } else {
3761 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003762 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003763 }
3764 } else if (second.IsConstant()) {
3765 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003766 __ andl(first.AsRegister<Register>(),
3767 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003768 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003769 __ orl(first.AsRegister<Register>(),
3770 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003771 } else {
3772 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00003773 __ xorl(first.AsRegister<Register>(),
3774 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003775 }
3776 } else {
3777 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003778 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003779 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003780 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003781 } else {
3782 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003783 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003784 }
3785 }
3786 } else {
3787 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3788 if (second.IsRegisterPair()) {
3789 if (instruction->IsAnd()) {
3790 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3791 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3792 } else if (instruction->IsOr()) {
3793 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3794 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3795 } else {
3796 DCHECK(instruction->IsXor());
3797 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3798 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3799 }
3800 } else {
3801 if (instruction->IsAnd()) {
3802 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3803 __ andl(first.AsRegisterPairHigh<Register>(),
3804 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3805 } else if (instruction->IsOr()) {
3806 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3807 __ orl(first.AsRegisterPairHigh<Register>(),
3808 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3809 } else {
3810 DCHECK(instruction->IsXor());
3811 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3812 __ xorl(first.AsRegisterPairHigh<Register>(),
3813 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3814 }
3815 }
3816 }
3817}
3818
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003819} // namespace x86
3820} // namespace art