blob: 66f1d5e58da648434c12ba40b22b02745a83c67a [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() {
Roland Levillain199f3362014-11-27 17:15:16 +0000452 bool skip_overflow_check =
453 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000454 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000455
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000456 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100457 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100458 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100459 }
460
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000461 __ subl(ESP, Immediate(GetFrameSize() - FrameEntrySpillSize()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100462 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000463}
464
465void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000466 __ addl(ESP, Immediate(GetFrameSize() - FrameEntrySpillSize()));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000467}
468
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100469void CodeGeneratorX86::Bind(HBasicBlock* block) {
470 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000471}
472
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100473void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100474 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000475}
476
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100477Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
478 switch (load->GetType()) {
479 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100480 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100481 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
482 break;
483
484 case Primitive::kPrimInt:
485 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100486 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100487 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100488
489 case Primitive::kPrimBoolean:
490 case Primitive::kPrimByte:
491 case Primitive::kPrimChar:
492 case Primitive::kPrimShort:
493 case Primitive::kPrimVoid:
494 LOG(FATAL) << "Unexpected type " << load->GetType();
495 }
496
497 LOG(FATAL) << "Unreachable";
498 return Location();
499}
500
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100501Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
502 switch (type) {
503 case Primitive::kPrimBoolean:
504 case Primitive::kPrimByte:
505 case Primitive::kPrimChar:
506 case Primitive::kPrimShort:
507 case Primitive::kPrimInt:
508 case Primitive::kPrimNot: {
509 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000510 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100511 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100512 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100513 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000514 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100515 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100516 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100517
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000518 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100519 uint32_t index = gp_index_;
520 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000521 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100522 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100523 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
524 calling_convention.GetRegisterPairAt(index));
525 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100526 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000527 // stack_index_ is the right offset for the memory.
528 return Location::QuickParameter(index, stack_index_ - 2);
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100529 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000530 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
531 }
532 }
533
534 case Primitive::kPrimFloat: {
535 uint32_t index = fp_index_++;
536 stack_index_++;
537 if (index < calling_convention.GetNumberOfFpuRegisters()) {
538 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
539 } else {
540 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
541 }
542 }
543
544 case Primitive::kPrimDouble: {
545 uint32_t index = fp_index_++;
546 stack_index_ += 2;
547 if (index < calling_convention.GetNumberOfFpuRegisters()) {
548 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
549 } else {
550 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100551 }
552 }
553
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100554 case Primitive::kPrimVoid:
555 LOG(FATAL) << "Unexpected parameter type " << type;
556 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100557 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100558 return Location();
559}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100560
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100561void CodeGeneratorX86::Move32(Location destination, Location source) {
562 if (source.Equals(destination)) {
563 return;
564 }
565 if (destination.IsRegister()) {
566 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000567 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100568 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000569 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100570 } else {
571 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000572 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100573 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100574 } else if (destination.IsFpuRegister()) {
575 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000576 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100577 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000578 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100579 } else {
580 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000581 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100582 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100583 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000584 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100585 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000586 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100587 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000588 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100589 } else {
590 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100591 __ pushl(Address(ESP, source.GetStackIndex()));
592 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100593 }
594 }
595}
596
597void CodeGeneratorX86::Move64(Location destination, Location source) {
598 if (source.Equals(destination)) {
599 return;
600 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100601 if (destination.IsRegisterPair()) {
602 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000603 EmitParallelMoves(
604 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
605 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
606 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
607 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100608 } else if (source.IsFpuRegister()) {
609 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100610 } else if (source.IsQuickParameter()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000611 uint16_t register_index = source.GetQuickParameterRegisterIndex();
612 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100613 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000614 EmitParallelMoves(
615 Location::RegisterLocation(calling_convention.GetRegisterAt(register_index)),
616 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
617 Location::StackSlot(
618 calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize()),
619 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100620 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000621 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100622 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100623 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
624 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100625 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
626 }
627 } else if (destination.IsQuickParameter()) {
628 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000629 uint16_t register_index = destination.GetQuickParameterRegisterIndex();
630 uint16_t stack_index = destination.GetQuickParameterStackIndex();
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000631 if (source.IsRegisterPair()) {
632 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100633 } else if (source.IsFpuRegister()) {
634 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100635 } else {
636 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000637 EmitParallelMoves(
638 Location::StackSlot(source.GetStackIndex()),
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000639 Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index)),
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000640 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
641 Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index + 1)));
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000642 __ movl(calling_convention.GetRegisterAt(register_index), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100643 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100644 } else if (destination.IsFpuRegister()) {
645 if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000646 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100647 } else {
648 LOG(FATAL) << "Unimplemented";
649 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100650 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000651 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100652 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000653 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100654 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100655 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100656 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100657 } else if (source.IsQuickParameter()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000658 // No conflict possible, so just do the move.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100659 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000660 uint16_t register_index = source.GetQuickParameterRegisterIndex();
661 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000662 // Just move the low part. The only time a source is a quick parameter is
663 // when moving the parameter to its stack locations. And the (Java) caller
664 // of this method has already done that.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100665 __ movl(Address(ESP, destination.GetStackIndex()),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000666 calling_convention.GetRegisterAt(register_index));
667 DCHECK_EQ(calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100668 static_cast<size_t>(destination.GetHighStackIndex(kX86WordSize)));
669 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000670 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100671 } else {
672 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000673 EmitParallelMoves(
674 Location::StackSlot(source.GetStackIndex()),
675 Location::StackSlot(destination.GetStackIndex()),
676 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
677 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100678 }
679 }
680}
681
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100682void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000683 LocationSummary* locations = instruction->GetLocations();
684 if (locations != nullptr && locations->Out().Equals(location)) {
685 return;
686 }
687
688 if (locations != nullptr && locations->Out().IsConstant()) {
689 HConstant* const_to_move = locations->Out().GetConstant();
690 if (const_to_move->IsIntConstant()) {
691 Immediate imm(const_to_move->AsIntConstant()->GetValue());
692 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000693 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000694 } else if (location.IsStackSlot()) {
695 __ movl(Address(ESP, location.GetStackIndex()), imm);
696 } else {
697 DCHECK(location.IsConstant());
698 DCHECK_EQ(location.GetConstant(), const_to_move);
699 }
700 } else if (const_to_move->IsLongConstant()) {
701 int64_t value = const_to_move->AsLongConstant()->GetValue();
702 if (location.IsRegisterPair()) {
703 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
704 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
705 } else if (location.IsDoubleStackSlot()) {
706 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000707 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
708 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000709 } else {
710 DCHECK(location.IsConstant());
711 DCHECK_EQ(location.GetConstant(), instruction);
712 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100713 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000714 } else if (instruction->IsTemporary()) {
715 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000716 if (temp_location.IsStackSlot()) {
717 Move32(location, temp_location);
718 } else {
719 DCHECK(temp_location.IsDoubleStackSlot());
720 Move64(location, temp_location);
721 }
Roland Levillain476df552014-10-09 17:51:36 +0100722 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100723 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100724 switch (instruction->GetType()) {
725 case Primitive::kPrimBoolean:
726 case Primitive::kPrimByte:
727 case Primitive::kPrimChar:
728 case Primitive::kPrimShort:
729 case Primitive::kPrimInt:
730 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100731 case Primitive::kPrimFloat:
732 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100733 break;
734
735 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100736 case Primitive::kPrimDouble:
737 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100738 break;
739
740 default:
741 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
742 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000743 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100744 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100745 switch (instruction->GetType()) {
746 case Primitive::kPrimBoolean:
747 case Primitive::kPrimByte:
748 case Primitive::kPrimChar:
749 case Primitive::kPrimShort:
750 case Primitive::kPrimInt:
751 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100752 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000753 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100754 break;
755
756 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100757 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000758 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100759 break;
760
761 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100762 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100763 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000764 }
765}
766
767void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000768 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000769}
770
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000771void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000772 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100773 DCHECK(!successor->IsExitBlock());
774
775 HBasicBlock* block = got->GetBlock();
776 HInstruction* previous = got->GetPrevious();
777
778 HLoopInformation* info = block->GetLoopInformation();
779 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
780 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
781 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
782 return;
783 }
784
785 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
786 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
787 }
788 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000789 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000790 }
791}
792
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000793void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000794 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000795}
796
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000797void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700798 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000799 if (kIsDebugBuild) {
800 __ Comment("Unreachable");
801 __ int3();
802 }
803}
804
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000805void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100806 LocationSummary* locations =
807 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100808 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100809 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100810 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100811 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000812}
813
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000814void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700815 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100816 if (cond->IsIntConstant()) {
817 // Constant condition, statically compared against 1.
818 int32_t cond_value = cond->AsIntConstant()->GetValue();
819 if (cond_value == 1) {
820 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
821 if_instr->IfTrueSuccessor())) {
822 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100823 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100824 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100825 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100826 DCHECK_EQ(cond_value, 0);
827 }
828 } else {
829 bool materialized =
830 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
831 // Moves do not affect the eflags register, so if the condition is
832 // evaluated just before the if, we don't need to evaluate it
833 // again.
834 bool eflags_set = cond->IsCondition()
835 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
836 if (materialized) {
837 if (!eflags_set) {
838 // Materialized condition, compare against 0.
839 Location lhs = if_instr->GetLocations()->InAt(0);
840 if (lhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000841 __ cmpl(lhs.AsRegister<Register>(), Immediate(0));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100842 } else {
843 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
844 }
845 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
846 } else {
847 __ j(X86Condition(cond->AsCondition()->GetCondition()),
848 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
849 }
850 } else {
851 Location lhs = cond->GetLocations()->InAt(0);
852 Location rhs = cond->GetLocations()->InAt(1);
853 // LHS is guaranteed to be in a register (see
854 // LocationsBuilderX86::VisitCondition).
855 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000856 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100857 } else if (rhs.IsConstant()) {
858 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
859 Immediate imm(instruction->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000860 __ cmpl(lhs.AsRegister<Register>(), imm);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100861 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000862 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100863 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100864 __ j(X86Condition(cond->AsCondition()->GetCondition()),
865 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700866 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100867 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100868 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
869 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700870 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000871 }
872}
873
874void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000875 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000876}
877
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000878void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
879 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000880}
881
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000882void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100883 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000884}
885
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000886void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100887 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700888 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000889}
890
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100891void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100892 LocationSummary* locations =
893 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100894 switch (store->InputAt(1)->GetType()) {
895 case Primitive::kPrimBoolean:
896 case Primitive::kPrimByte:
897 case Primitive::kPrimChar:
898 case Primitive::kPrimShort:
899 case Primitive::kPrimInt:
900 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100901 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100902 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
903 break;
904
905 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100906 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100907 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
908 break;
909
910 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100911 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100912 }
913 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000914}
915
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000916void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700917 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000918}
919
Dave Allison20dfc792014-06-16 20:44:29 -0700920void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100921 LocationSummary* locations =
922 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100923 locations->SetInAt(0, Location::RequiresRegister());
924 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100925 if (comp->NeedsMaterialization()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000926 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100927 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000928}
929
Dave Allison20dfc792014-06-16 20:44:29 -0700930void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
931 if (comp->NeedsMaterialization()) {
932 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000933 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100934 // Clear register: setcc only sets the low byte.
935 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700936 if (locations->InAt(1).IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000937 __ cmpl(locations->InAt(0).AsRegister<Register>(),
938 locations->InAt(1).AsRegister<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100939 } else if (locations->InAt(1).IsConstant()) {
940 HConstant* instruction = locations->InAt(1).GetConstant();
941 Immediate imm(instruction->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000942 __ cmpl(locations->InAt(0).AsRegister<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700943 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000944 __ cmpl(locations->InAt(0).AsRegister<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700945 Address(ESP, locations->InAt(1).GetStackIndex()));
946 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000947 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100948 }
Dave Allison20dfc792014-06-16 20:44:29 -0700949}
950
951void LocationsBuilderX86::VisitEqual(HEqual* comp) {
952 VisitCondition(comp);
953}
954
955void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
956 VisitCondition(comp);
957}
958
959void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
960 VisitCondition(comp);
961}
962
963void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
964 VisitCondition(comp);
965}
966
967void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
968 VisitCondition(comp);
969}
970
971void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
972 VisitCondition(comp);
973}
974
975void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
976 VisitCondition(comp);
977}
978
979void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
980 VisitCondition(comp);
981}
982
983void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
984 VisitCondition(comp);
985}
986
987void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
988 VisitCondition(comp);
989}
990
991void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
992 VisitCondition(comp);
993}
994
995void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
996 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000997}
998
999void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001000 LocationSummary* locations =
1001 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001002 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001003}
1004
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001005void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001006 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001007 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001008}
1009
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001010void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001011 LocationSummary* locations =
1012 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001013 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001014}
1015
1016void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1017 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001018 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001019}
1020
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001021void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1022 LocationSummary* locations =
1023 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1024 locations->SetOut(Location::ConstantLocation(constant));
1025}
1026
1027void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1028 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001029 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001030}
1031
1032void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1033 LocationSummary* locations =
1034 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1035 locations->SetOut(Location::ConstantLocation(constant));
1036}
1037
1038void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1039 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001040 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001041}
1042
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001043void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001044 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001045}
1046
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001047void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001048 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001049 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001050 __ ret();
1051}
1052
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001053void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001054 LocationSummary* locations =
1055 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001056 switch (ret->InputAt(0)->GetType()) {
1057 case Primitive::kPrimBoolean:
1058 case Primitive::kPrimByte:
1059 case Primitive::kPrimChar:
1060 case Primitive::kPrimShort:
1061 case Primitive::kPrimInt:
1062 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001063 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001064 break;
1065
1066 case Primitive::kPrimLong:
1067 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001068 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001069 break;
1070
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001071 case Primitive::kPrimFloat:
1072 case Primitive::kPrimDouble:
1073 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001074 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001075 break;
1076
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001077 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001078 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001079 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001080}
1081
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001082void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001083 if (kIsDebugBuild) {
1084 switch (ret->InputAt(0)->GetType()) {
1085 case Primitive::kPrimBoolean:
1086 case Primitive::kPrimByte:
1087 case Primitive::kPrimChar:
1088 case Primitive::kPrimShort:
1089 case Primitive::kPrimInt:
1090 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001091 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001092 break;
1093
1094 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001095 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1096 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001097 break;
1098
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001099 case Primitive::kPrimFloat:
1100 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001101 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001102 break;
1103
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001104 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001105 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001106 }
1107 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001108 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001109 __ ret();
1110}
1111
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001112void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001113 HandleInvoke(invoke);
1114}
1115
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001116void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001117 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001118
1119 // TODO: Implement all kinds of calls:
1120 // 1) boot -> boot
1121 // 2) app -> boot
1122 // 3) app -> app
1123 //
1124 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1125
1126 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001127 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffray4e44c822014-12-17 12:25:12 +00001128 // temp = temp->dex_cache_resolved_methods_;
1129 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
1130 // temp = temp[index_in_cache]
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001131 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001132 // (temp + offset_of_quick_compiled_code)()
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001133 __ call(Address(
1134 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001135
1136 DCHECK(!codegen_->IsLeafMethod());
1137 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1138}
1139
1140void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1141 HandleInvoke(invoke);
1142}
1143
1144void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001145 LocationSummary* locations =
1146 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001147 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001148
1149 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001150 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001151 HInstruction* input = invoke->InputAt(i);
1152 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1153 }
1154
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001155 switch (invoke->GetType()) {
1156 case Primitive::kPrimBoolean:
1157 case Primitive::kPrimByte:
1158 case Primitive::kPrimChar:
1159 case Primitive::kPrimShort:
1160 case Primitive::kPrimInt:
1161 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001162 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001163 break;
1164
1165 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001166 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001167 break;
1168
1169 case Primitive::kPrimVoid:
1170 break;
1171
1172 case Primitive::kPrimDouble:
1173 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001174 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001175 break;
1176 }
1177
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001178 invoke->SetLocations(locations);
1179}
1180
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001181void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001182 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001183 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1184 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1185 LocationSummary* locations = invoke->GetLocations();
1186 Location receiver = locations->InAt(0);
1187 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1188 // temp = object->GetClass();
1189 if (receiver.IsStackSlot()) {
1190 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1191 __ movl(temp, Address(temp, class_offset));
1192 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001193 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001194 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001195 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001196 // temp = temp->GetMethodAt(method_offset);
1197 __ movl(temp, Address(temp, method_offset));
1198 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001199 __ call(Address(
1200 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001201
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001202 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001203 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001204}
1205
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001206void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1207 HandleInvoke(invoke);
1208 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001209 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001210}
1211
1212void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1213 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001214 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001215 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1216 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1217 LocationSummary* locations = invoke->GetLocations();
1218 Location receiver = locations->InAt(0);
1219 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1220
1221 // Set the hidden argument.
1222 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001223 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001224
1225 // temp = object->GetClass();
1226 if (receiver.IsStackSlot()) {
1227 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1228 __ movl(temp, Address(temp, class_offset));
1229 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001230 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001231 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001232 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001233 // temp = temp->GetImtEntryAt(method_offset);
1234 __ movl(temp, Address(temp, method_offset));
1235 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001236 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001237 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001238
1239 DCHECK(!codegen_->IsLeafMethod());
1240 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1241}
1242
Roland Levillain88cb1752014-10-20 16:36:47 +01001243void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1244 LocationSummary* locations =
1245 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1246 switch (neg->GetResultType()) {
1247 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001248 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001249 locations->SetInAt(0, Location::RequiresRegister());
1250 locations->SetOut(Location::SameAsFirstInput());
1251 break;
1252
Roland Levillain88cb1752014-10-20 16:36:47 +01001253 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001254 locations->SetInAt(0, Location::RequiresFpuRegister());
1255 locations->SetOut(Location::SameAsFirstInput());
1256 locations->AddTemp(Location::RequiresRegister());
1257 locations->AddTemp(Location::RequiresFpuRegister());
1258 break;
1259
Roland Levillain88cb1752014-10-20 16:36:47 +01001260 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001261 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001262 locations->SetOut(Location::SameAsFirstInput());
1263 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001264 break;
1265
1266 default:
1267 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1268 }
1269}
1270
1271void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1272 LocationSummary* locations = neg->GetLocations();
1273 Location out = locations->Out();
1274 Location in = locations->InAt(0);
1275 switch (neg->GetResultType()) {
1276 case Primitive::kPrimInt:
1277 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001278 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001279 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001280 break;
1281
1282 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001283 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001284 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001285 __ negl(out.AsRegisterPairLow<Register>());
1286 // Negation is similar to subtraction from zero. The least
1287 // significant byte triggers a borrow when it is different from
1288 // zero; to take it into account, add 1 to the most significant
1289 // byte if the carry flag (CF) is set to 1 after the first NEGL
1290 // operation.
1291 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1292 __ negl(out.AsRegisterPairHigh<Register>());
1293 break;
1294
Roland Levillain5368c212014-11-27 15:03:41 +00001295 case Primitive::kPrimFloat: {
1296 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001297 Register constant = locations->GetTemp(0).AsRegister<Register>();
1298 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001299 // Implement float negation with an exclusive or with value
1300 // 0x80000000 (mask for bit 31, representing the sign of a
1301 // single-precision floating-point number).
1302 __ movl(constant, Immediate(INT32_C(0x80000000)));
1303 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001304 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001305 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001306 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001307
Roland Levillain5368c212014-11-27 15:03:41 +00001308 case Primitive::kPrimDouble: {
1309 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001310 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001311 // Implement double negation with an exclusive or with value
1312 // 0x8000000000000000 (mask for bit 63, representing the sign of
1313 // a double-precision floating-point number).
1314 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001315 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001316 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001317 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001318
1319 default:
1320 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1321 }
1322}
1323
Roland Levillaindff1f282014-11-05 14:15:05 +00001324void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001325 Primitive::Type result_type = conversion->GetResultType();
1326 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001327 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001328
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001329 // The float-to-long and double-to-long type conversions rely on a
1330 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001331 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001332 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1333 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001334 ? LocationSummary::kCall
1335 : LocationSummary::kNoCall;
1336 LocationSummary* locations =
1337 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1338
Roland Levillaindff1f282014-11-05 14:15:05 +00001339 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001340 case Primitive::kPrimByte:
1341 switch (input_type) {
1342 case Primitive::kPrimShort:
1343 case Primitive::kPrimInt:
1344 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001345 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001346 locations->SetInAt(0, Location::Any());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001347 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1348 break;
1349
1350 default:
1351 LOG(FATAL) << "Unexpected type conversion from " << input_type
1352 << " to " << result_type;
1353 }
1354 break;
1355
Roland Levillain01a8d712014-11-14 16:27:39 +00001356 case Primitive::kPrimShort:
1357 switch (input_type) {
1358 case Primitive::kPrimByte:
1359 case Primitive::kPrimInt:
1360 case Primitive::kPrimChar:
1361 // Processing a Dex `int-to-short' instruction.
1362 locations->SetInAt(0, Location::Any());
1363 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1364 break;
1365
1366 default:
1367 LOG(FATAL) << "Unexpected type conversion from " << input_type
1368 << " to " << result_type;
1369 }
1370 break;
1371
Roland Levillain946e1432014-11-11 17:35:19 +00001372 case Primitive::kPrimInt:
1373 switch (input_type) {
1374 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001375 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001376 locations->SetInAt(0, Location::Any());
1377 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1378 break;
1379
1380 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001381 // Processing a Dex `float-to-int' instruction.
1382 locations->SetInAt(0, Location::RequiresFpuRegister());
1383 locations->SetOut(Location::RequiresRegister());
1384 locations->AddTemp(Location::RequiresFpuRegister());
1385 break;
1386
Roland Levillain946e1432014-11-11 17:35:19 +00001387 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001388 // Processing a Dex `double-to-int' instruction.
1389 locations->SetInAt(0, Location::RequiresFpuRegister());
1390 locations->SetOut(Location::RequiresRegister());
1391 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001392 break;
1393
1394 default:
1395 LOG(FATAL) << "Unexpected type conversion from " << input_type
1396 << " to " << result_type;
1397 }
1398 break;
1399
Roland Levillaindff1f282014-11-05 14:15:05 +00001400 case Primitive::kPrimLong:
1401 switch (input_type) {
1402 case Primitive::kPrimByte:
1403 case Primitive::kPrimShort:
1404 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001405 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001406 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001407 locations->SetInAt(0, Location::RegisterLocation(EAX));
1408 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1409 break;
1410
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001411 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001412 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001413 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001414 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001415 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1416 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1417
Vladimir Marko949c91f2015-01-27 10:48:44 +00001418 // The runtime helper puts the result in EAX, EDX.
1419 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001420 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001421 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001422
1423 default:
1424 LOG(FATAL) << "Unexpected type conversion from " << input_type
1425 << " to " << result_type;
1426 }
1427 break;
1428
Roland Levillain981e4542014-11-14 11:47:14 +00001429 case Primitive::kPrimChar:
1430 switch (input_type) {
1431 case Primitive::kPrimByte:
1432 case Primitive::kPrimShort:
1433 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001434 // Processing a Dex `int-to-char' instruction.
1435 locations->SetInAt(0, Location::Any());
1436 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1437 break;
1438
1439 default:
1440 LOG(FATAL) << "Unexpected type conversion from " << input_type
1441 << " to " << result_type;
1442 }
1443 break;
1444
Roland Levillaindff1f282014-11-05 14:15:05 +00001445 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001446 switch (input_type) {
1447 case Primitive::kPrimByte:
1448 case Primitive::kPrimShort:
1449 case Primitive::kPrimInt:
1450 case Primitive::kPrimChar:
1451 // Processing a Dex `int-to-float' instruction.
1452 locations->SetInAt(0, Location::RequiresRegister());
1453 locations->SetOut(Location::RequiresFpuRegister());
1454 break;
1455
1456 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001457 // Processing a Dex `long-to-float' instruction.
1458 locations->SetInAt(0, Location::RequiresRegister());
1459 locations->SetOut(Location::RequiresFpuRegister());
1460 locations->AddTemp(Location::RequiresFpuRegister());
1461 locations->AddTemp(Location::RequiresFpuRegister());
1462 break;
1463
Roland Levillaincff13742014-11-17 14:32:17 +00001464 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001465 // Processing a Dex `double-to-float' instruction.
1466 locations->SetInAt(0, Location::RequiresFpuRegister());
1467 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001468 break;
1469
1470 default:
1471 LOG(FATAL) << "Unexpected type conversion from " << input_type
1472 << " to " << result_type;
1473 };
1474 break;
1475
Roland Levillaindff1f282014-11-05 14:15:05 +00001476 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001477 switch (input_type) {
1478 case Primitive::kPrimByte:
1479 case Primitive::kPrimShort:
1480 case Primitive::kPrimInt:
1481 case Primitive::kPrimChar:
1482 // Processing a Dex `int-to-double' instruction.
1483 locations->SetInAt(0, Location::RequiresRegister());
1484 locations->SetOut(Location::RequiresFpuRegister());
1485 break;
1486
1487 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001488 // Processing a Dex `long-to-double' instruction.
1489 locations->SetInAt(0, Location::RequiresRegister());
1490 locations->SetOut(Location::RequiresFpuRegister());
1491 locations->AddTemp(Location::RequiresFpuRegister());
1492 locations->AddTemp(Location::RequiresFpuRegister());
1493 break;
1494
Roland Levillaincff13742014-11-17 14:32:17 +00001495 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001496 // Processing a Dex `float-to-double' instruction.
1497 locations->SetInAt(0, Location::RequiresFpuRegister());
1498 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001499 break;
1500
1501 default:
1502 LOG(FATAL) << "Unexpected type conversion from " << input_type
1503 << " to " << result_type;
1504 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001505 break;
1506
1507 default:
1508 LOG(FATAL) << "Unexpected type conversion from " << input_type
1509 << " to " << result_type;
1510 }
1511}
1512
1513void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1514 LocationSummary* locations = conversion->GetLocations();
1515 Location out = locations->Out();
1516 Location in = locations->InAt(0);
1517 Primitive::Type result_type = conversion->GetResultType();
1518 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001519 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001520 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001521 case Primitive::kPrimByte:
1522 switch (input_type) {
1523 case Primitive::kPrimShort:
1524 case Primitive::kPrimInt:
1525 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001526 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001527 if (in.IsRegister()) {
1528 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
1529 } else if (in.IsStackSlot()) {
1530 __ movsxb(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
1531 } else {
1532 DCHECK(in.GetConstant()->IsIntConstant());
1533 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1534 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1535 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001536 break;
1537
1538 default:
1539 LOG(FATAL) << "Unexpected type conversion from " << input_type
1540 << " to " << result_type;
1541 }
1542 break;
1543
Roland Levillain01a8d712014-11-14 16:27:39 +00001544 case Primitive::kPrimShort:
1545 switch (input_type) {
1546 case Primitive::kPrimByte:
1547 case Primitive::kPrimInt:
1548 case Primitive::kPrimChar:
1549 // Processing a Dex `int-to-short' instruction.
1550 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001551 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001552 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001553 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001554 } else {
1555 DCHECK(in.GetConstant()->IsIntConstant());
1556 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001557 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001558 }
1559 break;
1560
1561 default:
1562 LOG(FATAL) << "Unexpected type conversion from " << input_type
1563 << " to " << result_type;
1564 }
1565 break;
1566
Roland Levillain946e1432014-11-11 17:35:19 +00001567 case Primitive::kPrimInt:
1568 switch (input_type) {
1569 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001570 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001571 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001572 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001573 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001574 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001575 } else {
1576 DCHECK(in.IsConstant());
1577 DCHECK(in.GetConstant()->IsLongConstant());
1578 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001579 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001580 }
1581 break;
1582
Roland Levillain3f8f9362014-12-02 17:45:01 +00001583 case Primitive::kPrimFloat: {
1584 // Processing a Dex `float-to-int' instruction.
1585 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1586 Register output = out.AsRegister<Register>();
1587 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1588 Label done, nan;
1589
1590 __ movl(output, Immediate(kPrimIntMax));
1591 // temp = int-to-float(output)
1592 __ cvtsi2ss(temp, output);
1593 // if input >= temp goto done
1594 __ comiss(input, temp);
1595 __ j(kAboveEqual, &done);
1596 // if input == NaN goto nan
1597 __ j(kUnordered, &nan);
1598 // output = float-to-int-truncate(input)
1599 __ cvttss2si(output, input);
1600 __ jmp(&done);
1601 __ Bind(&nan);
1602 // output = 0
1603 __ xorl(output, output);
1604 __ Bind(&done);
1605 break;
1606 }
1607
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001608 case Primitive::kPrimDouble: {
1609 // Processing a Dex `double-to-int' instruction.
1610 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1611 Register output = out.AsRegister<Register>();
1612 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1613 Label done, nan;
1614
1615 __ movl(output, Immediate(kPrimIntMax));
1616 // temp = int-to-double(output)
1617 __ cvtsi2sd(temp, output);
1618 // if input >= temp goto done
1619 __ comisd(input, temp);
1620 __ j(kAboveEqual, &done);
1621 // if input == NaN goto nan
1622 __ j(kUnordered, &nan);
1623 // output = double-to-int-truncate(input)
1624 __ cvttsd2si(output, input);
1625 __ jmp(&done);
1626 __ Bind(&nan);
1627 // output = 0
1628 __ xorl(output, output);
1629 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001630 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001631 }
Roland Levillain946e1432014-11-11 17:35:19 +00001632
1633 default:
1634 LOG(FATAL) << "Unexpected type conversion from " << input_type
1635 << " to " << result_type;
1636 }
1637 break;
1638
Roland Levillaindff1f282014-11-05 14:15:05 +00001639 case Primitive::kPrimLong:
1640 switch (input_type) {
1641 case Primitive::kPrimByte:
1642 case Primitive::kPrimShort:
1643 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001644 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001645 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001646 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1647 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001648 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001649 __ cdq();
1650 break;
1651
1652 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001653 // Processing a Dex `float-to-long' instruction.
1654 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001655 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1656 break;
1657
Roland Levillaindff1f282014-11-05 14:15:05 +00001658 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001659 // Processing a Dex `double-to-long' instruction.
1660 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1661 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001662 break;
1663
1664 default:
1665 LOG(FATAL) << "Unexpected type conversion from " << input_type
1666 << " to " << result_type;
1667 }
1668 break;
1669
Roland Levillain981e4542014-11-14 11:47:14 +00001670 case Primitive::kPrimChar:
1671 switch (input_type) {
1672 case Primitive::kPrimByte:
1673 case Primitive::kPrimShort:
1674 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001675 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1676 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001677 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001678 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001679 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001680 } else {
1681 DCHECK(in.GetConstant()->IsIntConstant());
1682 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001683 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001684 }
1685 break;
1686
1687 default:
1688 LOG(FATAL) << "Unexpected type conversion from " << input_type
1689 << " to " << result_type;
1690 }
1691 break;
1692
Roland Levillaindff1f282014-11-05 14:15:05 +00001693 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001694 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001695 case Primitive::kPrimByte:
1696 case Primitive::kPrimShort:
1697 case Primitive::kPrimInt:
1698 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001699 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001700 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001701 break;
1702
Roland Levillain6d0e4832014-11-27 18:31:21 +00001703 case Primitive::kPrimLong: {
1704 // Processing a Dex `long-to-float' instruction.
1705 Register low = in.AsRegisterPairLow<Register>();
1706 Register high = in.AsRegisterPairHigh<Register>();
1707 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1708 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1709 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1710
1711 // Operations use doubles for precision reasons (each 32-bit
1712 // half of a long fits in the 53-bit mantissa of a double,
1713 // but not in the 24-bit mantissa of a float). This is
1714 // especially important for the low bits. The result is
1715 // eventually converted to float.
1716
1717 // low = low - 2^31 (to prevent bit 31 of `low` to be
1718 // interpreted as a sign bit)
1719 __ subl(low, Immediate(0x80000000));
1720 // temp = int-to-double(high)
1721 __ cvtsi2sd(temp, high);
1722 // temp = temp * 2^32
1723 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
1724 __ mulsd(temp, constant);
1725 // result = int-to-double(low)
1726 __ cvtsi2sd(result, low);
1727 // result = result + 2^31 (restore the original value of `low`)
1728 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
1729 __ addsd(result, constant);
1730 // result = result + temp
1731 __ addsd(result, temp);
1732 // result = double-to-float(result)
1733 __ cvtsd2ss(result, result);
1734 break;
1735 }
1736
Roland Levillaincff13742014-11-17 14:32:17 +00001737 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001738 // Processing a Dex `double-to-float' instruction.
1739 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001740 break;
1741
1742 default:
1743 LOG(FATAL) << "Unexpected type conversion from " << input_type
1744 << " to " << result_type;
1745 };
1746 break;
1747
Roland Levillaindff1f282014-11-05 14:15:05 +00001748 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001749 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001750 case Primitive::kPrimByte:
1751 case Primitive::kPrimShort:
1752 case Primitive::kPrimInt:
1753 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001754 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001755 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001756 break;
1757
Roland Levillain647b9ed2014-11-27 12:06:00 +00001758 case Primitive::kPrimLong: {
1759 // Processing a Dex `long-to-double' instruction.
1760 Register low = in.AsRegisterPairLow<Register>();
1761 Register high = in.AsRegisterPairHigh<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001762 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1763 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1764 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001765
Roland Levillain647b9ed2014-11-27 12:06:00 +00001766 // low = low - 2^31 (to prevent bit 31 of `low` to be
1767 // interpreted as a sign bit)
1768 __ subl(low, Immediate(0x80000000));
1769 // temp = int-to-double(high)
1770 __ cvtsi2sd(temp, high);
1771 // temp = temp * 2^32
Roland Levillain6d0e4832014-11-27 18:31:21 +00001772 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001773 __ mulsd(temp, constant);
1774 // result = int-to-double(low)
1775 __ cvtsi2sd(result, low);
1776 // result = result + 2^31 (restore the original value of `low`)
Roland Levillain6d0e4832014-11-27 18:31:21 +00001777 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001778 __ addsd(result, constant);
1779 // result = result + temp
1780 __ addsd(result, temp);
1781 break;
1782 }
1783
Roland Levillaincff13742014-11-17 14:32:17 +00001784 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001785 // Processing a Dex `float-to-double' instruction.
1786 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001787 break;
1788
1789 default:
1790 LOG(FATAL) << "Unexpected type conversion from " << input_type
1791 << " to " << result_type;
1792 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001793 break;
1794
1795 default:
1796 LOG(FATAL) << "Unexpected type conversion from " << input_type
1797 << " to " << result_type;
1798 }
1799}
1800
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001801void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001802 LocationSummary* locations =
1803 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001804 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001805 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001806 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001807 locations->SetInAt(0, Location::RequiresRegister());
1808 locations->SetInAt(1, Location::Any());
1809 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001810 break;
1811 }
1812
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001813 case Primitive::kPrimFloat:
1814 case Primitive::kPrimDouble: {
1815 locations->SetInAt(0, Location::RequiresFpuRegister());
1816 locations->SetInAt(1, Location::Any());
1817 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001818 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001819 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001820
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001821 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001822 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1823 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001824 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001825}
1826
1827void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1828 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001829 Location first = locations->InAt(0);
1830 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001831 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001832 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001833 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001834 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001835 __ addl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001836 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001837 __ addl(first.AsRegister<Register>(),
1838 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001839 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001840 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001841 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001842 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001843 }
1844
1845 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001846 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001847 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1848 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001849 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001850 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1851 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001852 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001853 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001854 break;
1855 }
1856
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001857 case Primitive::kPrimFloat: {
1858 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001859 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001860 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001861 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001862 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001863 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001864 }
1865
1866 case Primitive::kPrimDouble: {
1867 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001868 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001869 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001870 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001871 }
1872 break;
1873 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001874
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001875 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001876 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001877 }
1878}
1879
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001880void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001881 LocationSummary* locations =
1882 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001883 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001884 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001885 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001886 locations->SetInAt(0, Location::RequiresRegister());
1887 locations->SetInAt(1, Location::Any());
1888 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001889 break;
1890 }
Calin Juravle11351682014-10-23 15:38:15 +01001891 case Primitive::kPrimFloat:
1892 case Primitive::kPrimDouble: {
1893 locations->SetInAt(0, Location::RequiresFpuRegister());
1894 locations->SetInAt(1, Location::RequiresFpuRegister());
1895 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001896 break;
Calin Juravle11351682014-10-23 15:38:15 +01001897 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001898
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001899 default:
Calin Juravle11351682014-10-23 15:38:15 +01001900 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001901 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001902}
1903
1904void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1905 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001906 Location first = locations->InAt(0);
1907 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001908 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001909 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001910 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001911 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001912 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001913 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001914 __ subl(first.AsRegister<Register>(),
1915 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001916 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001917 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001918 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001919 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001920 }
1921
1922 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001923 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01001924 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1925 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001926 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001927 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001928 __ sbbl(first.AsRegisterPairHigh<Register>(),
1929 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001930 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001931 break;
1932 }
1933
Calin Juravle11351682014-10-23 15:38:15 +01001934 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001935 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001936 break;
Calin Juravle11351682014-10-23 15:38:15 +01001937 }
1938
1939 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001940 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001941 break;
1942 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001943
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001944 default:
Calin Juravle11351682014-10-23 15:38:15 +01001945 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001946 }
1947}
1948
Calin Juravle34bacdf2014-10-07 20:23:36 +01001949void LocationsBuilderX86::VisitMul(HMul* mul) {
1950 LocationSummary* locations =
1951 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1952 switch (mul->GetResultType()) {
1953 case Primitive::kPrimInt:
1954 locations->SetInAt(0, Location::RequiresRegister());
1955 locations->SetInAt(1, Location::Any());
1956 locations->SetOut(Location::SameAsFirstInput());
1957 break;
1958 case Primitive::kPrimLong: {
1959 locations->SetInAt(0, Location::RequiresRegister());
1960 // TODO: Currently this handles only stack operands:
1961 // - we don't have enough registers because we currently use Quick ABI.
1962 // - by the time we have a working register allocator we will probably change the ABI
1963 // and fix the above.
1964 // - we don't have a way yet to request operands on stack but the base line compiler
1965 // will leave the operands on the stack with Any().
1966 locations->SetInAt(1, Location::Any());
1967 locations->SetOut(Location::SameAsFirstInput());
1968 // Needed for imul on 32bits with 64bits output.
1969 locations->AddTemp(Location::RegisterLocation(EAX));
1970 locations->AddTemp(Location::RegisterLocation(EDX));
1971 break;
1972 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001973 case Primitive::kPrimFloat:
1974 case Primitive::kPrimDouble: {
1975 locations->SetInAt(0, Location::RequiresFpuRegister());
1976 locations->SetInAt(1, Location::RequiresFpuRegister());
1977 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001978 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001979 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001980
1981 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001982 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001983 }
1984}
1985
1986void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1987 LocationSummary* locations = mul->GetLocations();
1988 Location first = locations->InAt(0);
1989 Location second = locations->InAt(1);
1990 DCHECK(first.Equals(locations->Out()));
1991
1992 switch (mul->GetResultType()) {
1993 case Primitive::kPrimInt: {
1994 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001995 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001996 } else if (second.IsConstant()) {
1997 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001998 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001999 } else {
2000 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002001 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002002 }
2003 break;
2004 }
2005
2006 case Primitive::kPrimLong: {
2007 DCHECK(second.IsDoubleStackSlot());
2008
2009 Register in1_hi = first.AsRegisterPairHigh<Register>();
2010 Register in1_lo = first.AsRegisterPairLow<Register>();
2011 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2012 Address in2_lo(ESP, second.GetStackIndex());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002013 Register eax = locations->GetTemp(0).AsRegister<Register>();
2014 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002015
2016 DCHECK_EQ(EAX, eax);
2017 DCHECK_EQ(EDX, edx);
2018
2019 // input: in1 - 64 bits, in2 - 64 bits
2020 // output: in1
2021 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2022 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2023 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
2024
2025 __ movl(eax, in2_hi);
2026 // eax <- in1.lo * in2.hi
2027 __ imull(eax, in1_lo);
2028 // in1.hi <- in1.hi * in2.lo
2029 __ imull(in1_hi, in2_lo);
2030 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2031 __ addl(in1_hi, eax);
2032 // move in1_lo to eax to prepare for double precision
2033 __ movl(eax, in1_lo);
2034 // edx:eax <- in1.lo * in2.lo
2035 __ mull(in2_lo);
2036 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2037 __ addl(in1_hi, edx);
2038 // in1.lo <- (in1.lo * in2.lo)[31:0];
2039 __ movl(in1_lo, eax);
2040
2041 break;
2042 }
2043
Calin Juravleb5bfa962014-10-21 18:02:24 +01002044 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002045 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002046 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002047 }
2048
2049 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002050 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002051 break;
2052 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002053
2054 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002055 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002056 }
2057}
2058
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002059void InstructionCodeGeneratorX86::PushOntoFPStack(Location source, uint32_t temp_offset,
2060 uint32_t stack_adjustment, bool is_float) {
2061 if (source.IsStackSlot()) {
2062 DCHECK(is_float);
2063 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2064 } else if (source.IsDoubleStackSlot()) {
2065 DCHECK(!is_float);
2066 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2067 } else {
2068 // Write the value to the temporary location on the stack and load to FP stack.
2069 if (is_float) {
2070 Location stack_temp = Location::StackSlot(temp_offset);
2071 codegen_->Move32(stack_temp, source);
2072 __ flds(Address(ESP, temp_offset));
2073 } else {
2074 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2075 codegen_->Move64(stack_temp, source);
2076 __ fldl(Address(ESP, temp_offset));
2077 }
2078 }
2079}
2080
2081void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2082 Primitive::Type type = rem->GetResultType();
2083 bool is_float = type == Primitive::kPrimFloat;
2084 size_t elem_size = Primitive::ComponentSize(type);
2085 LocationSummary* locations = rem->GetLocations();
2086 Location first = locations->InAt(0);
2087 Location second = locations->InAt(1);
2088 Location out = locations->Out();
2089
2090 // Create stack space for 2 elements.
2091 // TODO: enhance register allocator to ask for stack temporaries.
2092 __ subl(ESP, Immediate(2 * elem_size));
2093
2094 // Load the values to the FP stack in reverse order, using temporaries if needed.
2095 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2096 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2097
2098 // Loop doing FPREM until we stabilize.
2099 Label retry;
2100 __ Bind(&retry);
2101 __ fprem();
2102
2103 // Move FP status to AX.
2104 __ fstsw();
2105
2106 // And see if the argument reduction is complete. This is signaled by the
2107 // C2 FPU flag bit set to 0.
2108 __ andl(EAX, Immediate(kC2ConditionMask));
2109 __ j(kNotEqual, &retry);
2110
2111 // We have settled on the final value. Retrieve it into an XMM register.
2112 // Store FP top of stack to real stack.
2113 if (is_float) {
2114 __ fsts(Address(ESP, 0));
2115 } else {
2116 __ fstl(Address(ESP, 0));
2117 }
2118
2119 // Pop the 2 items from the FP stack.
2120 __ fucompp();
2121
2122 // Load the value from the stack into an XMM register.
2123 DCHECK(out.IsFpuRegister()) << out;
2124 if (is_float) {
2125 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2126 } else {
2127 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2128 }
2129
2130 // And remove the temporary stack space we allocated.
2131 __ addl(ESP, Immediate(2 * elem_size));
2132}
2133
Calin Juravlebacfec32014-11-14 15:54:36 +00002134void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2135 DCHECK(instruction->IsDiv() || instruction->IsRem());
2136
2137 LocationSummary* locations = instruction->GetLocations();
2138 Location out = locations->Out();
2139 Location first = locations->InAt(0);
2140 Location second = locations->InAt(1);
2141 bool is_div = instruction->IsDiv();
2142
2143 switch (instruction->GetResultType()) {
2144 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002145 Register second_reg = second.AsRegister<Register>();
2146 DCHECK_EQ(EAX, first.AsRegister<Register>());
2147 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002148
2149 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002150 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
2151 is_div);
Calin Juravlebacfec32014-11-14 15:54:36 +00002152 codegen_->AddSlowPath(slow_path);
2153
2154 // 0x80000000/-1 triggers an arithmetic exception!
2155 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2156 // it's safe to just use negl instead of more complex comparisons.
2157
2158 __ cmpl(second_reg, Immediate(-1));
2159 __ j(kEqual, slow_path->GetEntryLabel());
2160
2161 // edx:eax <- sign-extended of eax
2162 __ cdq();
2163 // eax = quotient, edx = remainder
2164 __ idivl(second_reg);
2165
2166 __ Bind(slow_path->GetExitLabel());
2167 break;
2168 }
2169
2170 case Primitive::kPrimLong: {
2171 InvokeRuntimeCallingConvention calling_convention;
2172 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2173 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2174 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2175 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2176 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2177 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2178
2179 if (is_div) {
2180 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2181 } else {
2182 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2183 }
2184 uint32_t dex_pc = is_div
2185 ? instruction->AsDiv()->GetDexPc()
2186 : instruction->AsRem()->GetDexPc();
2187 codegen_->RecordPcInfo(instruction, dex_pc);
2188
2189 break;
2190 }
2191
2192 default:
2193 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2194 }
2195}
2196
Calin Juravle7c4954d2014-10-28 16:57:40 +00002197void LocationsBuilderX86::VisitDiv(HDiv* div) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002198 LocationSummary::CallKind call_kind = div->GetResultType() == Primitive::kPrimLong
2199 ? LocationSummary::kCall
2200 : LocationSummary::kNoCall;
2201 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2202
Calin Juravle7c4954d2014-10-28 16:57:40 +00002203 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002204 case Primitive::kPrimInt: {
2205 locations->SetInAt(0, Location::RegisterLocation(EAX));
2206 locations->SetInAt(1, Location::RequiresRegister());
2207 locations->SetOut(Location::SameAsFirstInput());
2208 // Intel uses edx:eax as the dividend.
2209 locations->AddTemp(Location::RegisterLocation(EDX));
2210 break;
2211 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002212 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002213 InvokeRuntimeCallingConvention calling_convention;
2214 locations->SetInAt(0, Location::RegisterPairLocation(
2215 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2216 locations->SetInAt(1, Location::RegisterPairLocation(
2217 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2218 // Runtime helper puts the result in EAX, EDX.
2219 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002220 break;
2221 }
2222 case Primitive::kPrimFloat:
2223 case Primitive::kPrimDouble: {
2224 locations->SetInAt(0, Location::RequiresFpuRegister());
2225 locations->SetInAt(1, Location::RequiresFpuRegister());
2226 locations->SetOut(Location::SameAsFirstInput());
2227 break;
2228 }
2229
2230 default:
2231 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2232 }
2233}
2234
2235void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2236 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002237 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002238 Location first = locations->InAt(0);
2239 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002240
2241 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002242 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002243 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002244 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002245 break;
2246 }
2247
2248 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002249 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002250 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002251 break;
2252 }
2253
2254 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002255 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002256 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002257 break;
2258 }
2259
2260 default:
2261 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2262 }
2263}
2264
Calin Juravlebacfec32014-11-14 15:54:36 +00002265void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002266 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002267 LocationSummary* locations =
2268 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravlebacfec32014-11-14 15:54:36 +00002269
Calin Juravled2ec87d2014-12-08 14:24:46 +00002270 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002271 case Primitive::kPrimInt: {
2272 locations->SetInAt(0, Location::RegisterLocation(EAX));
2273 locations->SetInAt(1, Location::RequiresRegister());
2274 locations->SetOut(Location::RegisterLocation(EDX));
2275 break;
2276 }
2277 case Primitive::kPrimLong: {
2278 InvokeRuntimeCallingConvention calling_convention;
2279 locations->SetInAt(0, Location::RegisterPairLocation(
2280 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2281 locations->SetInAt(1, Location::RegisterPairLocation(
2282 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2283 // Runtime helper puts the result in EAX, EDX.
2284 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2285 break;
2286 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002287 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002288 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002289 locations->SetInAt(0, Location::Any());
2290 locations->SetInAt(1, Location::Any());
2291 locations->SetOut(Location::RequiresFpuRegister());
2292 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002293 break;
2294 }
2295
2296 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002297 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002298 }
2299}
2300
2301void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2302 Primitive::Type type = rem->GetResultType();
2303 switch (type) {
2304 case Primitive::kPrimInt:
2305 case Primitive::kPrimLong: {
2306 GenerateDivRemIntegral(rem);
2307 break;
2308 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002309 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002310 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002311 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002312 break;
2313 }
2314 default:
2315 LOG(FATAL) << "Unexpected rem type " << type;
2316 }
2317}
2318
Calin Juravled0d48522014-11-04 16:40:20 +00002319void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2320 LocationSummary* locations =
2321 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002322 switch (instruction->GetType()) {
2323 case Primitive::kPrimInt: {
2324 locations->SetInAt(0, Location::Any());
2325 break;
2326 }
2327 case Primitive::kPrimLong: {
2328 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2329 if (!instruction->IsConstant()) {
2330 locations->AddTemp(Location::RequiresRegister());
2331 }
2332 break;
2333 }
2334 default:
2335 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2336 }
Calin Juravled0d48522014-11-04 16:40:20 +00002337 if (instruction->HasUses()) {
2338 locations->SetOut(Location::SameAsFirstInput());
2339 }
2340}
2341
2342void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2343 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2344 codegen_->AddSlowPath(slow_path);
2345
2346 LocationSummary* locations = instruction->GetLocations();
2347 Location value = locations->InAt(0);
2348
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002349 switch (instruction->GetType()) {
2350 case Primitive::kPrimInt: {
2351 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002352 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002353 __ j(kEqual, slow_path->GetEntryLabel());
2354 } else if (value.IsStackSlot()) {
2355 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2356 __ j(kEqual, slow_path->GetEntryLabel());
2357 } else {
2358 DCHECK(value.IsConstant()) << value;
2359 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2360 __ jmp(slow_path->GetEntryLabel());
2361 }
2362 }
2363 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002364 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002365 case Primitive::kPrimLong: {
2366 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002367 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002368 __ movl(temp, value.AsRegisterPairLow<Register>());
2369 __ orl(temp, value.AsRegisterPairHigh<Register>());
2370 __ j(kEqual, slow_path->GetEntryLabel());
2371 } else {
2372 DCHECK(value.IsConstant()) << value;
2373 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2374 __ jmp(slow_path->GetEntryLabel());
2375 }
2376 }
2377 break;
2378 }
2379 default:
2380 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002381 }
Calin Juravled0d48522014-11-04 16:40:20 +00002382}
2383
Calin Juravle9aec02f2014-11-18 23:06:35 +00002384void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2385 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2386
2387 LocationSummary* locations =
2388 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2389
2390 switch (op->GetResultType()) {
2391 case Primitive::kPrimInt: {
2392 locations->SetInAt(0, Location::RequiresRegister());
2393 // The shift count needs to be in CL.
2394 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2395 locations->SetOut(Location::SameAsFirstInput());
2396 break;
2397 }
2398 case Primitive::kPrimLong: {
2399 locations->SetInAt(0, Location::RequiresRegister());
2400 // The shift count needs to be in CL.
2401 locations->SetInAt(1, Location::RegisterLocation(ECX));
2402 locations->SetOut(Location::SameAsFirstInput());
2403 break;
2404 }
2405 default:
2406 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2407 }
2408}
2409
2410void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2411 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2412
2413 LocationSummary* locations = op->GetLocations();
2414 Location first = locations->InAt(0);
2415 Location second = locations->InAt(1);
2416 DCHECK(first.Equals(locations->Out()));
2417
2418 switch (op->GetResultType()) {
2419 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002420 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002421 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002422 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002423 DCHECK_EQ(ECX, second_reg);
2424 if (op->IsShl()) {
2425 __ shll(first_reg, second_reg);
2426 } else if (op->IsShr()) {
2427 __ sarl(first_reg, second_reg);
2428 } else {
2429 __ shrl(first_reg, second_reg);
2430 }
2431 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002432 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002433 if (op->IsShl()) {
2434 __ shll(first_reg, imm);
2435 } else if (op->IsShr()) {
2436 __ sarl(first_reg, imm);
2437 } else {
2438 __ shrl(first_reg, imm);
2439 }
2440 }
2441 break;
2442 }
2443 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002444 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002445 DCHECK_EQ(ECX, second_reg);
2446 if (op->IsShl()) {
2447 GenerateShlLong(first, second_reg);
2448 } else if (op->IsShr()) {
2449 GenerateShrLong(first, second_reg);
2450 } else {
2451 GenerateUShrLong(first, second_reg);
2452 }
2453 break;
2454 }
2455 default:
2456 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2457 }
2458}
2459
2460void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2461 Label done;
2462 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2463 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2464 __ testl(shifter, Immediate(32));
2465 __ j(kEqual, &done);
2466 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2467 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2468 __ Bind(&done);
2469}
2470
2471void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2472 Label done;
2473 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2474 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2475 __ testl(shifter, Immediate(32));
2476 __ j(kEqual, &done);
2477 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2478 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2479 __ Bind(&done);
2480}
2481
2482void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2483 Label done;
2484 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2485 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2486 __ testl(shifter, Immediate(32));
2487 __ j(kEqual, &done);
2488 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2489 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2490 __ Bind(&done);
2491}
2492
2493void LocationsBuilderX86::VisitShl(HShl* shl) {
2494 HandleShift(shl);
2495}
2496
2497void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2498 HandleShift(shl);
2499}
2500
2501void LocationsBuilderX86::VisitShr(HShr* shr) {
2502 HandleShift(shr);
2503}
2504
2505void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2506 HandleShift(shr);
2507}
2508
2509void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2510 HandleShift(ushr);
2511}
2512
2513void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2514 HandleShift(ushr);
2515}
2516
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002517void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002518 LocationSummary* locations =
2519 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002520 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002521 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002522 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2523 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002524}
2525
2526void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2527 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002528 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002529 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002530
Nicolas Geoffray707c8092014-04-04 10:50:14 +01002531 __ fs()->call(
2532 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002533
Nicolas Geoffray39468442014-09-02 15:17:15 +01002534 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002535 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002536}
2537
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002538void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2539 LocationSummary* locations =
2540 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2541 locations->SetOut(Location::RegisterLocation(EAX));
2542 InvokeRuntimeCallingConvention calling_convention;
2543 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002544 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2545 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002546}
2547
2548void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2549 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002550 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002551 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2552
2553 __ fs()->call(
2554 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
2555
2556 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2557 DCHECK(!codegen_->IsLeafMethod());
2558}
2559
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002560void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002561 LocationSummary* locations =
2562 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002563 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2564 if (location.IsStackSlot()) {
2565 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2566 } else if (location.IsDoubleStackSlot()) {
2567 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002568 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002569 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002570}
2571
2572void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002573 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002574}
2575
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002576void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002577 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002578 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002579 locations->SetInAt(0, Location::RequiresRegister());
2580 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002581}
2582
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002583void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2584 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002585 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002586 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002587 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002588 switch (not_->InputAt(0)->GetType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002589 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002590 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002591 break;
2592
2593 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002594 __ notl(out.AsRegisterPairLow<Register>());
2595 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002596 break;
2597
2598 default:
2599 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2600 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002601}
2602
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002603void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002604 LocationSummary* locations =
2605 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002606 switch (compare->InputAt(0)->GetType()) {
2607 case Primitive::kPrimLong: {
2608 locations->SetInAt(0, Location::RequiresRegister());
2609 // TODO: we set any here but we don't handle constants
2610 locations->SetInAt(1, Location::Any());
2611 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2612 break;
2613 }
2614 case Primitive::kPrimFloat:
2615 case Primitive::kPrimDouble: {
2616 locations->SetInAt(0, Location::RequiresFpuRegister());
2617 locations->SetInAt(1, Location::RequiresFpuRegister());
2618 locations->SetOut(Location::RequiresRegister());
2619 break;
2620 }
2621 default:
2622 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2623 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002624}
2625
2626void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002627 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002628 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002629 Location left = locations->InAt(0);
2630 Location right = locations->InAt(1);
2631
2632 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002633 switch (compare->InputAt(0)->GetType()) {
2634 case Primitive::kPrimLong: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002635 if (right.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002636 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002637 } else {
2638 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002639 __ cmpl(left.AsRegisterPairHigh<Register>(),
2640 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002641 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002642 __ j(kLess, &less); // Signed compare.
2643 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002644 if (right.IsRegisterPair()) {
2645 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002646 } else {
2647 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002648 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002649 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002650 break;
2651 }
2652 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002653 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002654 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
2655 break;
2656 }
2657 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002658 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002659 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002660 break;
2661 }
2662 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002663 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002664 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002665 __ movl(out, Immediate(0));
2666 __ j(kEqual, &done);
2667 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
2668
2669 __ Bind(&greater);
2670 __ movl(out, Immediate(1));
2671 __ jmp(&done);
2672
2673 __ Bind(&less);
2674 __ movl(out, Immediate(-1));
2675
2676 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002677}
2678
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002679void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002680 LocationSummary* locations =
2681 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002682 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2683 locations->SetInAt(i, Location::Any());
2684 }
2685 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002686}
2687
2688void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002689 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002690 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002691}
2692
Calin Juravle52c48962014-12-16 17:02:57 +00002693void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
2694 /*
2695 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2696 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2697 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2698 */
2699 switch (kind) {
2700 case MemBarrierKind::kAnyAny: {
2701 __ mfence();
2702 break;
2703 }
2704 case MemBarrierKind::kAnyStore:
2705 case MemBarrierKind::kLoadAny:
2706 case MemBarrierKind::kStoreStore: {
2707 // nop
2708 break;
2709 }
2710 default:
2711 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002712 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002713}
2714
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002715
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002716void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002717 Label is_null;
2718 __ testl(value, value);
2719 __ j(kEqual, &is_null);
2720 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2721 __ movl(temp, object);
2722 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002723 __ movb(Address(temp, card, TIMES_1, 0),
2724 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002725 __ Bind(&is_null);
2726}
2727
Calin Juravle52c48962014-12-16 17:02:57 +00002728void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
2729 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002730 LocationSummary* locations =
2731 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002732 locations->SetInAt(0, Location::RequiresRegister());
2733 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle52c48962014-12-16 17:02:57 +00002734
2735 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
2736 // Long values can be loaded atomically into an XMM using movsd.
2737 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
2738 // and then copy the XMM into the output 32bits at a time).
2739 locations->AddTemp(Location::RequiresFpuRegister());
2740 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002741}
2742
Calin Juravle52c48962014-12-16 17:02:57 +00002743void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
2744 const FieldInfo& field_info) {
2745 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002746
Calin Juravle52c48962014-12-16 17:02:57 +00002747 LocationSummary* locations = instruction->GetLocations();
2748 Register base = locations->InAt(0).AsRegister<Register>();
2749 Location out = locations->Out();
2750 bool is_volatile = field_info.IsVolatile();
2751 Primitive::Type field_type = field_info.GetFieldType();
2752 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2753
2754 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002755 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00002756 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002757 break;
2758 }
2759
2760 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002761 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002762 break;
2763 }
2764
2765 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00002766 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002767 break;
2768 }
2769
2770 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002771 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002772 break;
2773 }
2774
2775 case Primitive::kPrimInt:
2776 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002777 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002778 break;
2779 }
2780
2781 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00002782 if (is_volatile) {
2783 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2784 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002785 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002786 __ movd(out.AsRegisterPairLow<Register>(), temp);
2787 __ psrlq(temp, Immediate(32));
2788 __ movd(out.AsRegisterPairHigh<Register>(), temp);
2789 } else {
2790 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002791 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002792 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
2793 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002794 break;
2795 }
2796
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002797 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002798 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002799 break;
2800 }
2801
2802 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002803 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002804 break;
2805 }
2806
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002807 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00002808 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002809 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002810 }
Calin Juravle52c48962014-12-16 17:02:57 +00002811
Calin Juravle77520bc2015-01-12 18:45:46 +00002812 // Longs are handled in the switch.
2813 if (field_type != Primitive::kPrimLong) {
2814 codegen_->MaybeRecordImplicitNullCheck(instruction);
2815 }
2816
Calin Juravle52c48962014-12-16 17:02:57 +00002817 if (is_volatile) {
2818 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2819 }
2820}
2821
2822void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
2823 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2824
2825 LocationSummary* locations =
2826 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2827 locations->SetInAt(0, Location::RequiresRegister());
2828 bool is_volatile = field_info.IsVolatile();
2829 Primitive::Type field_type = field_info.GetFieldType();
2830 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2831 || (field_type == Primitive::kPrimByte);
2832
2833 // The register allocator does not support multiple
2834 // inputs that die at entry with one in a specific register.
2835 if (is_byte_type) {
2836 // Ensure the value is in a byte register.
2837 locations->SetInAt(1, Location::RegisterLocation(EAX));
2838 } else {
2839 locations->SetInAt(1, Location::RequiresRegister());
2840 }
2841 // Temporary registers for the write barrier.
2842 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2843 locations->AddTemp(Location::RequiresRegister());
2844 // Ensure the card is in a byte register.
2845 locations->AddTemp(Location::RegisterLocation(ECX));
2846 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
2847 // 64bits value can be atomically written to an address with movsd and an XMM register.
2848 // We need two XMM registers because there's no easier way to (bit) copy a register pair
2849 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
2850 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
2851 // isolated cases when we need this it isn't worth adding the extra complexity.
2852 locations->AddTemp(Location::RequiresFpuRegister());
2853 locations->AddTemp(Location::RequiresFpuRegister());
2854 }
2855}
2856
2857void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
2858 const FieldInfo& field_info) {
2859 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2860
2861 LocationSummary* locations = instruction->GetLocations();
2862 Register base = locations->InAt(0).AsRegister<Register>();
2863 Location value = locations->InAt(1);
2864 bool is_volatile = field_info.IsVolatile();
2865 Primitive::Type field_type = field_info.GetFieldType();
2866 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2867
2868 if (is_volatile) {
2869 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2870 }
2871
2872 switch (field_type) {
2873 case Primitive::kPrimBoolean:
2874 case Primitive::kPrimByte: {
2875 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
2876 break;
2877 }
2878
2879 case Primitive::kPrimShort:
2880 case Primitive::kPrimChar: {
2881 __ movw(Address(base, offset), value.AsRegister<Register>());
2882 break;
2883 }
2884
2885 case Primitive::kPrimInt:
2886 case Primitive::kPrimNot: {
2887 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00002888 break;
2889 }
2890
2891 case Primitive::kPrimLong: {
2892 if (is_volatile) {
2893 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2894 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
2895 __ movd(temp1, value.AsRegisterPairLow<Register>());
2896 __ movd(temp2, value.AsRegisterPairHigh<Register>());
2897 __ punpckldq(temp1, temp2);
2898 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00002899 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002900 } else {
2901 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00002902 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002903 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
2904 }
2905 break;
2906 }
2907
2908 case Primitive::kPrimFloat: {
2909 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
2910 break;
2911 }
2912
2913 case Primitive::kPrimDouble: {
2914 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
2915 break;
2916 }
2917
2918 case Primitive::kPrimVoid:
2919 LOG(FATAL) << "Unreachable type " << field_type;
2920 UNREACHABLE();
2921 }
2922
Calin Juravle77520bc2015-01-12 18:45:46 +00002923 // Longs are handled in the switch.
2924 if (field_type != Primitive::kPrimLong) {
2925 codegen_->MaybeRecordImplicitNullCheck(instruction);
2926 }
2927
2928 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2929 Register temp = locations->GetTemp(0).AsRegister<Register>();
2930 Register card = locations->GetTemp(1).AsRegister<Register>();
2931 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
2932 }
2933
Calin Juravle52c48962014-12-16 17:02:57 +00002934 if (is_volatile) {
2935 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2936 }
2937}
2938
2939void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2940 HandleFieldGet(instruction, instruction->GetFieldInfo());
2941}
2942
2943void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2944 HandleFieldGet(instruction, instruction->GetFieldInfo());
2945}
2946
2947void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2948 HandleFieldSet(instruction, instruction->GetFieldInfo());
2949}
2950
2951void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2952 HandleFieldSet(instruction, instruction->GetFieldInfo());
2953}
2954
2955void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2956 HandleFieldSet(instruction, instruction->GetFieldInfo());
2957}
2958
2959void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2960 HandleFieldSet(instruction, instruction->GetFieldInfo());
2961}
2962
2963void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2964 HandleFieldGet(instruction, instruction->GetFieldInfo());
2965}
2966
2967void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2968 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002969}
2970
2971void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002972 LocationSummary* locations =
2973 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002974 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
2975 ? Location::RequiresRegister()
2976 : Location::Any();
2977 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002978 if (instruction->HasUses()) {
2979 locations->SetOut(Location::SameAsFirstInput());
2980 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002981}
2982
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002983void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002984 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2985 return;
2986 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002987 LocationSummary* locations = instruction->GetLocations();
2988 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002989
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002990 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
2991 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2992}
2993
2994void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002995 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002996 codegen_->AddSlowPath(slow_path);
2997
2998 LocationSummary* locations = instruction->GetLocations();
2999 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003000
3001 if (obj.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003002 __ cmpl(obj.AsRegister<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003003 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003004 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003005 } else {
3006 DCHECK(obj.IsConstant()) << obj;
3007 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3008 __ jmp(slow_path->GetEntryLabel());
3009 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003010 }
3011 __ j(kEqual, slow_path->GetEntryLabel());
3012}
3013
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003014void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3015 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3016 GenerateImplicitNullCheck(instruction);
3017 } else {
3018 GenerateExplicitNullCheck(instruction);
3019 }
3020}
3021
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003022void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003023 LocationSummary* locations =
3024 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003025 locations->SetInAt(0, Location::RequiresRegister());
3026 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3027 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003028}
3029
3030void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3031 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003032 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003033 Location index = locations->InAt(1);
3034
Calin Juravle77520bc2015-01-12 18:45:46 +00003035 Primitive::Type type = instruction->GetType();
3036 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003037 case Primitive::kPrimBoolean: {
3038 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003039 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003040 if (index.IsConstant()) {
3041 __ movzxb(out, Address(obj,
3042 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3043 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003044 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003045 }
3046 break;
3047 }
3048
3049 case Primitive::kPrimByte: {
3050 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003051 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003052 if (index.IsConstant()) {
3053 __ movsxb(out, Address(obj,
3054 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3055 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003056 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003057 }
3058 break;
3059 }
3060
3061 case Primitive::kPrimShort: {
3062 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003063 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003064 if (index.IsConstant()) {
3065 __ movsxw(out, Address(obj,
3066 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3067 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003068 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003069 }
3070 break;
3071 }
3072
3073 case Primitive::kPrimChar: {
3074 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003075 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003076 if (index.IsConstant()) {
3077 __ movzxw(out, Address(obj,
3078 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3079 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003080 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003081 }
3082 break;
3083 }
3084
3085 case Primitive::kPrimInt:
3086 case Primitive::kPrimNot: {
3087 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003088 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003089 if (index.IsConstant()) {
3090 __ movl(out, Address(obj,
3091 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3092 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003093 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003094 }
3095 break;
3096 }
3097
3098 case Primitive::kPrimLong: {
3099 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003100 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003101 if (index.IsConstant()) {
3102 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003103 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003104 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003105 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003106 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003107 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003108 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003109 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003110 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003111 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003112 }
3113 break;
3114 }
3115
3116 case Primitive::kPrimFloat:
3117 case Primitive::kPrimDouble:
Calin Juravle77520bc2015-01-12 18:45:46 +00003118 LOG(FATAL) << "Unimplemented register type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003119 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003120 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003121 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003122 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003123 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003124
3125 if (type != Primitive::kPrimLong) {
3126 codegen_->MaybeRecordImplicitNullCheck(instruction);
3127 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003128}
3129
3130void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003131 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003132 bool needs_write_barrier =
3133 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3134
3135 DCHECK(kFollowsQuickABI);
3136 bool not_enough_registers = needs_write_barrier
3137 && !instruction->GetValue()->IsConstant()
3138 && !instruction->GetIndex()->IsConstant();
3139 bool needs_runtime_call = instruction->NeedsTypeCheck() || not_enough_registers;
3140
Nicolas Geoffray39468442014-09-02 15:17:15 +01003141 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3142 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003143 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003144
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003145 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003146 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003147 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3148 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3149 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003150 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003151 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3152 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003153 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003154 // In case of a byte operation, the register allocator does not support multiple
3155 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003156 locations->SetInAt(0, Location::RequiresRegister());
3157 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003158 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003159 // Ensure the value is in a byte register.
3160 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003161 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003162 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003163 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003164 // Temporary registers for the write barrier.
3165 if (needs_write_barrier) {
3166 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003167 // Ensure the card is in a byte register.
3168 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003169 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003170 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003171}
3172
3173void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3174 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003175 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003176 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003177 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003178 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003179 bool needs_runtime_call = locations->WillCall();
3180 bool needs_write_barrier =
3181 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003182
3183 switch (value_type) {
3184 case Primitive::kPrimBoolean:
3185 case Primitive::kPrimByte: {
3186 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003187 if (index.IsConstant()) {
3188 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003189 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003190 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003191 } else {
3192 __ movb(Address(obj, offset),
3193 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3194 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003195 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003196 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003197 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003198 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003199 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003200 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003201 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3202 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003203 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003204 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003205 break;
3206 }
3207
3208 case Primitive::kPrimShort:
3209 case Primitive::kPrimChar: {
3210 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003211 if (index.IsConstant()) {
3212 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003213 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003214 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003215 } else {
3216 __ movw(Address(obj, offset),
3217 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3218 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003219 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003220 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003221 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3222 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003223 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003224 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003225 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3226 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003227 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003228 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003229 break;
3230 }
3231
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003232 case Primitive::kPrimInt:
3233 case Primitive::kPrimNot: {
3234 if (!needs_runtime_call) {
3235 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3236 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003237 size_t offset =
3238 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003239 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003240 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003241 } else {
3242 DCHECK(value.IsConstant()) << value;
3243 __ movl(Address(obj, offset),
3244 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3245 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003246 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003247 DCHECK(index.IsRegister()) << index;
3248 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003249 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3250 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003251 } else {
3252 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003253 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003254 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3255 }
3256 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003257 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003258
3259 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003260 Register temp = locations->GetTemp(0).AsRegister<Register>();
3261 Register card = locations->GetTemp(1).AsRegister<Register>();
3262 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003263 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003264 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003265 DCHECK_EQ(value_type, Primitive::kPrimNot);
3266 DCHECK(!codegen_->IsLeafMethod());
3267 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3268 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003269 }
3270 break;
3271 }
3272
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003273 case Primitive::kPrimLong: {
3274 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003275 if (index.IsConstant()) {
3276 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003277 if (value.IsRegisterPair()) {
3278 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003279 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003280 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003281 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003282 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003283 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3284 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003285 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003286 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3287 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003288 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003289 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003290 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003291 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003292 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003293 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003294 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003295 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003296 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003297 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003298 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003299 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003300 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003301 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003302 Immediate(High32Bits(val)));
3303 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003304 }
3305 break;
3306 }
3307
3308 case Primitive::kPrimFloat:
3309 case Primitive::kPrimDouble:
3310 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003311 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003312 case Primitive::kPrimVoid:
3313 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003314 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003315 }
3316}
3317
3318void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3319 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003320 locations->SetInAt(0, Location::RequiresRegister());
3321 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003322 instruction->SetLocations(locations);
3323}
3324
3325void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3326 LocationSummary* locations = instruction->GetLocations();
3327 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003328 Register obj = locations->InAt(0).AsRegister<Register>();
3329 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003330 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003331 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003332}
3333
3334void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003335 LocationSummary* locations =
3336 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003337 locations->SetInAt(0, Location::RequiresRegister());
3338 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003339 if (instruction->HasUses()) {
3340 locations->SetOut(Location::SameAsFirstInput());
3341 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003342}
3343
3344void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3345 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003346 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01003347 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003348 codegen_->AddSlowPath(slow_path);
3349
Roland Levillain271ab9c2014-11-27 15:23:57 +00003350 Register index = locations->InAt(0).AsRegister<Register>();
3351 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003352
3353 __ cmpl(index, length);
3354 __ j(kAboveEqual, slow_path->GetEntryLabel());
3355}
3356
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003357void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3358 temp->SetLocations(nullptr);
3359}
3360
3361void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3362 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003363 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003364}
3365
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003366void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003367 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003368 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003369}
3370
3371void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003372 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3373}
3374
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003375void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3376 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3377}
3378
3379void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003380 HBasicBlock* block = instruction->GetBlock();
3381 if (block->GetLoopInformation() != nullptr) {
3382 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3383 // The back edge will generate the suspend check.
3384 return;
3385 }
3386 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3387 // The goto will generate the suspend check.
3388 return;
3389 }
3390 GenerateSuspendCheck(instruction, nullptr);
3391}
3392
3393void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3394 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003395 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003396 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003397 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003398 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003399 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003400 if (successor == nullptr) {
3401 __ j(kNotEqual, slow_path->GetEntryLabel());
3402 __ Bind(slow_path->GetReturnLabel());
3403 } else {
3404 __ j(kEqual, codegen_->GetLabelOf(successor));
3405 __ jmp(slow_path->GetEntryLabel());
3406 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003407}
3408
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003409X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3410 return codegen_->GetAssembler();
3411}
3412
3413void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
3414 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003415 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003416 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3417 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
3418 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
3419}
3420
3421void ParallelMoveResolverX86::EmitMove(size_t index) {
3422 MoveOperands* move = moves_.Get(index);
3423 Location source = move->GetSource();
3424 Location destination = move->GetDestination();
3425
3426 if (source.IsRegister()) {
3427 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003428 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003429 } else {
3430 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003431 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003432 }
3433 } else if (source.IsStackSlot()) {
3434 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003435 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003436 } else {
3437 DCHECK(destination.IsStackSlot());
3438 MoveMemoryToMemory(destination.GetStackIndex(),
3439 source.GetStackIndex());
3440 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003441 } else if (source.IsConstant()) {
3442 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
3443 Immediate imm(instruction->AsIntConstant()->GetValue());
3444 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003445 __ movl(destination.AsRegister<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003446 } else {
3447 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3448 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003449 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003450 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003451 }
3452}
3453
3454void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003455 Register suggested_scratch = reg == EAX ? EBX : EAX;
3456 ScratchRegisterScope ensure_scratch(
3457 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3458
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003459 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3460 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
3461 __ movl(Address(ESP, mem + stack_offset), reg);
3462 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
3463}
3464
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003465void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
3466 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003467 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3468
3469 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003470 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003471 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3472
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003473 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
3474 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
3475 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
3476 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
3477 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
3478 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
3479}
3480
3481void ParallelMoveResolverX86::EmitSwap(size_t index) {
3482 MoveOperands* move = moves_.Get(index);
3483 Location source = move->GetSource();
3484 Location destination = move->GetDestination();
3485
3486 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003487 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003488 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003489 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003490 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003491 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003492 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3493 Exchange(destination.GetStackIndex(), source.GetStackIndex());
3494 } else {
3495 LOG(FATAL) << "Unimplemented";
3496 }
3497}
3498
3499void ParallelMoveResolverX86::SpillScratch(int reg) {
3500 __ pushl(static_cast<Register>(reg));
3501}
3502
3503void ParallelMoveResolverX86::RestoreScratch(int reg) {
3504 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003505}
3506
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003507void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003508 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3509 ? LocationSummary::kCallOnSlowPath
3510 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003511 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003512 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003513 locations->SetOut(Location::RequiresRegister());
3514}
3515
3516void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003517 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003518 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003519 DCHECK(!cls->CanCallRuntime());
3520 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003521 codegen_->LoadCurrentMethod(out);
3522 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3523 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003524 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003525 codegen_->LoadCurrentMethod(out);
3526 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3527 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003528
3529 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3530 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3531 codegen_->AddSlowPath(slow_path);
3532 __ testl(out, out);
3533 __ j(kEqual, slow_path->GetEntryLabel());
3534 if (cls->MustGenerateClinitCheck()) {
3535 GenerateClassInitializationCheck(slow_path, out);
3536 } else {
3537 __ Bind(slow_path->GetExitLabel());
3538 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003539 }
3540}
3541
3542void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
3543 LocationSummary* locations =
3544 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3545 locations->SetInAt(0, Location::RequiresRegister());
3546 if (check->HasUses()) {
3547 locations->SetOut(Location::SameAsFirstInput());
3548 }
3549}
3550
3551void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003552 // We assume the class to not be null.
3553 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3554 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003555 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003556 GenerateClassInitializationCheck(slow_path,
3557 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003558}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003559
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003560void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
3561 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003562 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3563 Immediate(mirror::Class::kStatusInitialized));
3564 __ j(kLess, slow_path->GetEntryLabel());
3565 __ Bind(slow_path->GetExitLabel());
3566 // No need for memory fence, thanks to the X86 memory model.
3567}
3568
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003569void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
3570 LocationSummary* locations =
3571 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3572 locations->SetOut(Location::RequiresRegister());
3573}
3574
3575void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
3576 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
3577 codegen_->AddSlowPath(slow_path);
3578
Roland Levillain271ab9c2014-11-27 15:23:57 +00003579 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003580 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08003581 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3582 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003583 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3584 __ testl(out, out);
3585 __ j(kEqual, slow_path->GetEntryLabel());
3586 __ Bind(slow_path->GetExitLabel());
3587}
3588
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003589void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
3590 LocationSummary* locations =
3591 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3592 locations->SetOut(Location::RequiresRegister());
3593}
3594
3595void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
3596 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003597 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003598 __ fs()->movl(address, Immediate(0));
3599}
3600
3601void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
3602 LocationSummary* locations =
3603 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3604 InvokeRuntimeCallingConvention calling_convention;
3605 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3606}
3607
3608void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
3609 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
3610 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3611}
3612
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003613void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003614 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3615 ? LocationSummary::kNoCall
3616 : LocationSummary::kCallOnSlowPath;
3617 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3618 locations->SetInAt(0, Location::RequiresRegister());
3619 locations->SetInAt(1, Location::Any());
3620 locations->SetOut(Location::RequiresRegister());
3621}
3622
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003623void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003624 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003625 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003626 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003627 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003628 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3629 Label done, zero;
3630 SlowPathCodeX86* slow_path = nullptr;
3631
3632 // Return 0 if `obj` is null.
3633 // TODO: avoid this check if we know obj is not null.
3634 __ testl(obj, obj);
3635 __ j(kEqual, &zero);
3636 __ movl(out, Address(obj, class_offset));
3637 // Compare the class of `obj` with `cls`.
3638 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003639 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003640 } else {
3641 DCHECK(cls.IsStackSlot()) << cls;
3642 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
3643 }
3644
3645 if (instruction->IsClassFinal()) {
3646 // Classes must be equal for the instanceof to succeed.
3647 __ j(kNotEqual, &zero);
3648 __ movl(out, Immediate(1));
3649 __ jmp(&done);
3650 } else {
3651 // If the classes are not equal, we go into a slow path.
3652 DCHECK(locations->OnlyCallsOnSlowPath());
3653 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003654 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003655 codegen_->AddSlowPath(slow_path);
3656 __ j(kNotEqual, slow_path->GetEntryLabel());
3657 __ movl(out, Immediate(1));
3658 __ jmp(&done);
3659 }
3660 __ Bind(&zero);
3661 __ movl(out, Immediate(0));
3662 if (slow_path != nullptr) {
3663 __ Bind(slow_path->GetExitLabel());
3664 }
3665 __ Bind(&done);
3666}
3667
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003668void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
3669 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3670 instruction, LocationSummary::kCallOnSlowPath);
3671 locations->SetInAt(0, Location::RequiresRegister());
3672 locations->SetInAt(1, Location::Any());
3673 locations->AddTemp(Location::RequiresRegister());
3674}
3675
3676void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
3677 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003678 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003679 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003680 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003681 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3682 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
3683 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3684 codegen_->AddSlowPath(slow_path);
3685
3686 // TODO: avoid this check if we know obj is not null.
3687 __ testl(obj, obj);
3688 __ j(kEqual, slow_path->GetExitLabel());
3689 __ movl(temp, Address(obj, class_offset));
3690
3691 // Compare the class of `obj` with `cls`.
3692 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003693 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003694 } else {
3695 DCHECK(cls.IsStackSlot()) << cls;
3696 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
3697 }
3698
3699 __ j(kNotEqual, slow_path->GetEntryLabel());
3700 __ Bind(slow_path->GetExitLabel());
3701}
3702
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003703void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3704 LocationSummary* locations =
3705 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3706 InvokeRuntimeCallingConvention calling_convention;
3707 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3708}
3709
3710void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3711 __ fs()->call(Address::Absolute(instruction->IsEnter()
3712 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
3713 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
3714 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3715}
3716
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003717void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3718void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3719void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3720
3721void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3722 LocationSummary* locations =
3723 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3724 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3725 || instruction->GetResultType() == Primitive::kPrimLong);
3726 locations->SetInAt(0, Location::RequiresRegister());
3727 locations->SetInAt(1, Location::Any());
3728 locations->SetOut(Location::SameAsFirstInput());
3729}
3730
3731void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
3732 HandleBitwiseOperation(instruction);
3733}
3734
3735void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
3736 HandleBitwiseOperation(instruction);
3737}
3738
3739void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
3740 HandleBitwiseOperation(instruction);
3741}
3742
3743void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3744 LocationSummary* locations = instruction->GetLocations();
3745 Location first = locations->InAt(0);
3746 Location second = locations->InAt(1);
3747 DCHECK(first.Equals(locations->Out()));
3748
3749 if (instruction->GetResultType() == Primitive::kPrimInt) {
3750 if (second.IsRegister()) {
3751 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003752 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003753 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003754 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003755 } else {
3756 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003757 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003758 }
3759 } else if (second.IsConstant()) {
3760 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003761 __ andl(first.AsRegister<Register>(),
3762 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003763 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003764 __ orl(first.AsRegister<Register>(),
3765 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003766 } else {
3767 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00003768 __ xorl(first.AsRegister<Register>(),
3769 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003770 }
3771 } else {
3772 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003773 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003774 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003775 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003776 } else {
3777 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003778 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003779 }
3780 }
3781 } else {
3782 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3783 if (second.IsRegisterPair()) {
3784 if (instruction->IsAnd()) {
3785 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3786 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3787 } else if (instruction->IsOr()) {
3788 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3789 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3790 } else {
3791 DCHECK(instruction->IsXor());
3792 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3793 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3794 }
3795 } else {
3796 if (instruction->IsAnd()) {
3797 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3798 __ andl(first.AsRegisterPairHigh<Register>(),
3799 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3800 } else if (instruction->IsOr()) {
3801 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3802 __ orl(first.AsRegisterPairHigh<Register>(),
3803 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3804 } else {
3805 DCHECK(instruction->IsXor());
3806 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3807 __ xorl(first.AsRegisterPairHigh<Register>(),
3808 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3809 }
3810 }
3811 }
3812}
3813
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003814} // namespace x86
3815} // namespace art