blob: c0fdcaa8aa4e614ce93f0d5d7d17f0638f4bca47 [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);
Vladimir Marko949c91f2015-01-27 10:48:44 +000039static constexpr XmmRegister kRuntimeParameterFpuRegisters[] = { };
40static constexpr size_t kRuntimeParameterFpuRegistersLength = 0;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010041
Mark Mendell24f2dfa2015-01-14 19:51:45 -050042static constexpr int kC2ConditionMask = 0x400;
43
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000044// Marker for places that can be updated once we don't follow the quick ABI.
45static constexpr bool kFollowsQuickABI = true;
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000046static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000047
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010048class InvokeRuntimeCallingConvention : public CallingConvention<Register, XmmRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010049 public:
50 InvokeRuntimeCallingConvention()
51 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010052 kRuntimeParameterCoreRegistersLength,
53 kRuntimeParameterFpuRegisters,
54 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010055
56 private:
57 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
58};
59
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
61
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010062class SlowPathCodeX86 : public SlowPathCode {
63 public:
64 SlowPathCodeX86() : entry_label_(), exit_label_() {}
65
66 Label* GetEntryLabel() { return &entry_label_; }
67 Label* GetExitLabel() { return &exit_label_; }
68
69 private:
70 Label entry_label_;
71 Label exit_label_;
72
73 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86);
74};
75
76class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010077 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010078 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079
80 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
81 __ Bind(GetEntryLabel());
82 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010083 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010084 }
85
86 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010087 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010088 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
89};
90
Calin Juravled0d48522014-11-04 16:40:20 +000091class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
92 public:
93 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
94
95 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
96 __ Bind(GetEntryLabel());
97 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
98 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
99 }
100
101 private:
102 HDivZeroCheck* const instruction_;
103 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
104};
105
Calin Juravlebacfec32014-11-14 15:54:36 +0000106class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +0000107 public:
Calin Juravlebacfec32014-11-14 15:54:36 +0000108 explicit DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000109
110 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
111 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000112 if (is_div_) {
113 __ negl(reg_);
114 } else {
115 __ movl(reg_, Immediate(0));
116 }
Calin Juravled0d48522014-11-04 16:40:20 +0000117 __ jmp(GetExitLabel());
118 }
119
120 private:
121 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000122 bool is_div_;
123 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000124};
125
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100126class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100127 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100128 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
129 Location index_location,
130 Location length_location)
Roland Levillain199f3362014-11-27 17:15:16 +0000131 : instruction_(instruction),
132 index_location_(index_location),
133 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100134
135 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100136 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100137 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000138 // We're moving two locations to locations that could overlap, so we need a parallel
139 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100140 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000141 x86_codegen->EmitParallelMoves(
142 index_location_,
143 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
144 length_location_,
145 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100146 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100147 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100148 }
149
150 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100151 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100152 const Location index_location_;
153 const Location length_location_;
154
155 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
156};
157
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100158class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000159 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100160 explicit SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
161 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000162
163 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100164 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000165 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100166 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000167 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
168 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100169 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100170 if (successor_ == nullptr) {
171 __ jmp(GetReturnLabel());
172 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100173 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100174 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000175 }
176
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100177 Label* GetReturnLabel() {
178 DCHECK(successor_ == nullptr);
179 return &return_label_;
180 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000181
182 private:
183 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100184 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000185 Label return_label_;
186
187 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
188};
189
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000190class LoadStringSlowPathX86 : public SlowPathCodeX86 {
191 public:
192 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
193
194 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
195 LocationSummary* locations = instruction_->GetLocations();
196 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
197
198 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
199 __ Bind(GetEntryLabel());
200 codegen->SaveLiveRegisters(locations);
201
202 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800203 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
204 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000205 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
206 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
207 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
208 codegen->RestoreLiveRegisters(locations);
209
210 __ jmp(GetExitLabel());
211 }
212
213 private:
214 HLoadString* const instruction_;
215
216 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
217};
218
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000219class LoadClassSlowPathX86 : public SlowPathCodeX86 {
220 public:
221 LoadClassSlowPathX86(HLoadClass* cls,
222 HInstruction* at,
223 uint32_t dex_pc,
224 bool do_clinit)
225 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
226 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
227 }
228
229 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
230 LocationSummary* locations = at_->GetLocations();
231 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
232 __ Bind(GetEntryLabel());
233 codegen->SaveLiveRegisters(locations);
234
235 InvokeRuntimeCallingConvention calling_convention;
236 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
237 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
238 __ fs()->call(Address::Absolute(do_clinit_
239 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
240 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
241 codegen->RecordPcInfo(at_, dex_pc_);
242
243 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000244 Location out = locations->Out();
245 if (out.IsValid()) {
246 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
247 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000248 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000249
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000250 codegen->RestoreLiveRegisters(locations);
251 __ jmp(GetExitLabel());
252 }
253
254 private:
255 // The class this slow path will load.
256 HLoadClass* const cls_;
257
258 // The instruction where this slow path is happening.
259 // (Might be the load class or an initialization check).
260 HInstruction* const at_;
261
262 // The dex PC of `at_`.
263 const uint32_t dex_pc_;
264
265 // Whether to initialize the class.
266 const bool do_clinit_;
267
268 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
269};
270
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000271class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
272 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000273 TypeCheckSlowPathX86(HInstruction* instruction,
274 Location class_to_check,
275 Location object_class,
276 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000277 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000278 class_to_check_(class_to_check),
279 object_class_(object_class),
280 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000281
282 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
283 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000284 DCHECK(instruction_->IsCheckCast()
285 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000286
287 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
288 __ Bind(GetEntryLabel());
289 codegen->SaveLiveRegisters(locations);
290
291 // We're moving two locations to locations that could overlap, so we need a parallel
292 // move resolver.
293 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000294 x86_codegen->EmitParallelMoves(
295 class_to_check_,
296 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
297 object_class_,
298 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000299
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000300 if (instruction_->IsInstanceOf()) {
Roland Levillain199f3362014-11-27 17:15:16 +0000301 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize,
302 pInstanceofNonTrivial)));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000303 } else {
304 DCHECK(instruction_->IsCheckCast());
305 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
306 }
307
308 codegen->RecordPcInfo(instruction_, dex_pc_);
309 if (instruction_->IsInstanceOf()) {
310 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
311 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000312 codegen->RestoreLiveRegisters(locations);
313
314 __ jmp(GetExitLabel());
315 }
316
317 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000318 HInstruction* const instruction_;
319 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000320 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000321 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000322
323 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
324};
325
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100326#undef __
327#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
328
Dave Allison20dfc792014-06-16 20:44:29 -0700329inline Condition X86Condition(IfCondition cond) {
330 switch (cond) {
331 case kCondEQ: return kEqual;
332 case kCondNE: return kNotEqual;
333 case kCondLT: return kLess;
334 case kCondLE: return kLessEqual;
335 case kCondGT: return kGreater;
336 case kCondGE: return kGreaterEqual;
337 default:
338 LOG(FATAL) << "Unknown if condition";
339 }
340 return kEqual;
341}
342
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100343void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
344 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
345}
346
347void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
348 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
349}
350
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100351size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
352 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
353 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100354}
355
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100356size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
357 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
358 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100359}
360
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000361CodeGeneratorX86::CodeGeneratorX86(HGraph* graph, const CompilerOptions& compiler_options)
362 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfXmmRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000363 kNumberOfRegisterPairs, (1 << kFakeReturnRegister), 0, compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100364 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100365 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100366 instruction_visitor_(graph, this),
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000367 move_resolver_(graph->GetArena(), this) {
368 // Use a fake return address register to mimic Quick.
369 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100370}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100371
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100372Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100373 switch (type) {
374 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100375 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100376 X86ManagedRegister pair =
377 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100378 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
379 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100380 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
381 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100382 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100383 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100384 }
385
386 case Primitive::kPrimByte:
387 case Primitive::kPrimBoolean:
388 case Primitive::kPrimChar:
389 case Primitive::kPrimShort:
390 case Primitive::kPrimInt:
391 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100392 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100393 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100394 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100395 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
396 X86ManagedRegister current =
397 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
398 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100399 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100400 }
401 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100402 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100403 }
404
405 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100406 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100407 return Location::FpuRegisterLocation(
408 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100409 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100410
411 case Primitive::kPrimVoid:
412 LOG(FATAL) << "Unreachable type " << type;
413 }
414
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100415 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100416}
417
Nicolas Geoffray98893962015-01-21 12:32:32 +0000418void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline ATTRIBUTE_UNUSED) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100419 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100420 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100421
422 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100423 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100424
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000425 // TODO: We currently don't use Quick's callee saved registers.
426 DCHECK(kFollowsQuickABI);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100427 blocked_core_registers_[EBP] = true;
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000428 blocked_core_registers_[ESI] = true;
429 blocked_core_registers_[EDI] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100430
431 UpdateBlockedPairRegisters();
432}
433
434void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
435 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
436 X86ManagedRegister current =
437 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
438 if (blocked_core_registers_[current.AsRegisterPairLow()]
439 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
440 blocked_register_pairs_[i] = true;
441 }
442 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100443}
444
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100445InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
446 : HGraphVisitor(graph),
447 assembler_(codegen->GetAssembler()),
448 codegen_(codegen) {}
449
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000450void CodeGeneratorX86::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +0000451 bool skip_overflow_check =
452 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000453 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000454
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000455 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100456 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100457 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100458 }
459
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000460 __ subl(ESP, Immediate(GetFrameSize() - FrameEntrySpillSize()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100461 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000462}
463
464void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000465 __ addl(ESP, Immediate(GetFrameSize() - FrameEntrySpillSize()));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000466}
467
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100468void CodeGeneratorX86::Bind(HBasicBlock* block) {
469 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000470}
471
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100472void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100473 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000474}
475
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100476Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
477 switch (load->GetType()) {
478 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100479 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100480 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
481 break;
482
483 case Primitive::kPrimInt:
484 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100485 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100486 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100487
488 case Primitive::kPrimBoolean:
489 case Primitive::kPrimByte:
490 case Primitive::kPrimChar:
491 case Primitive::kPrimShort:
492 case Primitive::kPrimVoid:
493 LOG(FATAL) << "Unexpected type " << load->GetType();
494 }
495
496 LOG(FATAL) << "Unreachable";
497 return Location();
498}
499
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100500Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
501 switch (type) {
502 case Primitive::kPrimBoolean:
503 case Primitive::kPrimByte:
504 case Primitive::kPrimChar:
505 case Primitive::kPrimShort:
506 case Primitive::kPrimInt:
Vladimir Marko949c91f2015-01-27 10:48:44 +0000507 case Primitive::kPrimFloat:
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100508 case Primitive::kPrimNot: {
509 uint32_t index = gp_index_++;
510 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100511 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100512 } else {
Vladimir Marko949c91f2015-01-27 10:48:44 +0000513 return Location::StackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100514 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100515 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100516
Vladimir Marko949c91f2015-01-27 10:48:44 +0000517 case Primitive::kPrimLong:
518 case Primitive::kPrimDouble: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100519 uint32_t index = gp_index_;
520 gp_index_ += 2;
521 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100522 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
523 calling_convention.GetRegisterPairAt(index));
524 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100525 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
Vladimir Marko949c91f2015-01-27 10:48:44 +0000526 // On X86, the register index and stack index of a quick parameter is the same, since
527 // we are passing floating pointer values in core registers.
528 return Location::QuickParameter(index, index);
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100529 } else {
Vladimir Marko949c91f2015-01-27 10:48:44 +0000530 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100531 }
532 }
533
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100534 case Primitive::kPrimVoid:
535 LOG(FATAL) << "Unexpected parameter type " << type;
536 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100537 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100538 return Location();
539}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100540
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100541void CodeGeneratorX86::Move32(Location destination, Location source) {
542 if (source.Equals(destination)) {
543 return;
544 }
545 if (destination.IsRegister()) {
546 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000547 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100548 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000549 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100550 } else {
551 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000552 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100553 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100554 } else if (destination.IsFpuRegister()) {
555 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000556 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100557 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000558 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100559 } else {
560 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000561 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100562 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100563 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000564 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100565 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000566 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100567 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000568 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100569 } else {
570 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100571 __ pushl(Address(ESP, source.GetStackIndex()));
572 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100573 }
574 }
575}
576
577void CodeGeneratorX86::Move64(Location destination, Location source) {
578 if (source.Equals(destination)) {
579 return;
580 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100581 if (destination.IsRegisterPair()) {
582 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000583 EmitParallelMoves(
584 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
585 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
586 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
587 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100588 } else if (source.IsFpuRegister()) {
589 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100590 } else if (source.IsQuickParameter()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000591 uint16_t register_index = source.GetQuickParameterRegisterIndex();
592 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100593 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000594 EmitParallelMoves(
595 Location::RegisterLocation(calling_convention.GetRegisterAt(register_index)),
596 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
597 Location::StackSlot(
598 calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize()),
599 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100600 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000601 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100602 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100603 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
604 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100605 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
606 }
607 } else if (destination.IsQuickParameter()) {
608 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000609 uint16_t register_index = destination.GetQuickParameterRegisterIndex();
610 uint16_t stack_index = destination.GetQuickParameterStackIndex();
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000611 if (source.IsRegisterPair()) {
612 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100613 } else if (source.IsFpuRegister()) {
614 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100615 } else {
616 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000617 EmitParallelMoves(
618 Location::StackSlot(source.GetStackIndex()),
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000619 Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index)),
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000620 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
621 Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index + 1)));
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000622 __ movl(calling_convention.GetRegisterAt(register_index), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100623 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100624 } else if (destination.IsFpuRegister()) {
625 if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000626 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100627 } else {
628 LOG(FATAL) << "Unimplemented";
629 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100630 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000631 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100632 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000633 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100634 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100635 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100636 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100637 } else if (source.IsQuickParameter()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000638 // No conflict possible, so just do the move.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100639 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000640 uint16_t register_index = source.GetQuickParameterRegisterIndex();
641 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000642 // Just move the low part. The only time a source is a quick parameter is
643 // when moving the parameter to its stack locations. And the (Java) caller
644 // of this method has already done that.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100645 __ movl(Address(ESP, destination.GetStackIndex()),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000646 calling_convention.GetRegisterAt(register_index));
647 DCHECK_EQ(calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100648 static_cast<size_t>(destination.GetHighStackIndex(kX86WordSize)));
649 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000650 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100651 } else {
652 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000653 EmitParallelMoves(
654 Location::StackSlot(source.GetStackIndex()),
655 Location::StackSlot(destination.GetStackIndex()),
656 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
657 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100658 }
659 }
660}
661
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100662void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000663 LocationSummary* locations = instruction->GetLocations();
664 if (locations != nullptr && locations->Out().Equals(location)) {
665 return;
666 }
667
668 if (locations != nullptr && locations->Out().IsConstant()) {
669 HConstant* const_to_move = locations->Out().GetConstant();
670 if (const_to_move->IsIntConstant()) {
671 Immediate imm(const_to_move->AsIntConstant()->GetValue());
672 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000673 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000674 } else if (location.IsStackSlot()) {
675 __ movl(Address(ESP, location.GetStackIndex()), imm);
676 } else {
677 DCHECK(location.IsConstant());
678 DCHECK_EQ(location.GetConstant(), const_to_move);
679 }
680 } else if (const_to_move->IsLongConstant()) {
681 int64_t value = const_to_move->AsLongConstant()->GetValue();
682 if (location.IsRegisterPair()) {
683 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
684 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
685 } else if (location.IsDoubleStackSlot()) {
686 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000687 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
688 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000689 } else {
690 DCHECK(location.IsConstant());
691 DCHECK_EQ(location.GetConstant(), instruction);
692 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100693 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000694 } else if (instruction->IsTemporary()) {
695 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000696 if (temp_location.IsStackSlot()) {
697 Move32(location, temp_location);
698 } else {
699 DCHECK(temp_location.IsDoubleStackSlot());
700 Move64(location, temp_location);
701 }
Roland Levillain476df552014-10-09 17:51:36 +0100702 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100703 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100704 switch (instruction->GetType()) {
705 case Primitive::kPrimBoolean:
706 case Primitive::kPrimByte:
707 case Primitive::kPrimChar:
708 case Primitive::kPrimShort:
709 case Primitive::kPrimInt:
710 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100711 case Primitive::kPrimFloat:
712 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100713 break;
714
715 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100716 case Primitive::kPrimDouble:
717 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100718 break;
719
720 default:
721 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
722 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000723 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100724 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100725 switch (instruction->GetType()) {
726 case Primitive::kPrimBoolean:
727 case Primitive::kPrimByte:
728 case Primitive::kPrimChar:
729 case Primitive::kPrimShort:
730 case Primitive::kPrimInt:
731 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100732 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000733 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100734 break;
735
736 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100737 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000738 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100739 break;
740
741 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100742 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100743 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000744 }
745}
746
747void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000748 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000749}
750
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000751void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000752 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100753 DCHECK(!successor->IsExitBlock());
754
755 HBasicBlock* block = got->GetBlock();
756 HInstruction* previous = got->GetPrevious();
757
758 HLoopInformation* info = block->GetLoopInformation();
759 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
760 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
761 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
762 return;
763 }
764
765 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
766 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
767 }
768 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000769 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000770 }
771}
772
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000773void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000774 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000775}
776
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000777void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700778 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000779 if (kIsDebugBuild) {
780 __ Comment("Unreachable");
781 __ int3();
782 }
783}
784
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000785void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100786 LocationSummary* locations =
787 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100788 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100789 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100790 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100791 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000792}
793
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000794void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700795 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100796 if (cond->IsIntConstant()) {
797 // Constant condition, statically compared against 1.
798 int32_t cond_value = cond->AsIntConstant()->GetValue();
799 if (cond_value == 1) {
800 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
801 if_instr->IfTrueSuccessor())) {
802 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100803 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100804 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100805 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100806 DCHECK_EQ(cond_value, 0);
807 }
808 } else {
809 bool materialized =
810 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
811 // Moves do not affect the eflags register, so if the condition is
812 // evaluated just before the if, we don't need to evaluate it
813 // again.
814 bool eflags_set = cond->IsCondition()
815 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
816 if (materialized) {
817 if (!eflags_set) {
818 // Materialized condition, compare against 0.
819 Location lhs = if_instr->GetLocations()->InAt(0);
820 if (lhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000821 __ cmpl(lhs.AsRegister<Register>(), Immediate(0));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100822 } else {
823 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
824 }
825 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
826 } else {
827 __ j(X86Condition(cond->AsCondition()->GetCondition()),
828 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
829 }
830 } else {
831 Location lhs = cond->GetLocations()->InAt(0);
832 Location rhs = cond->GetLocations()->InAt(1);
833 // LHS is guaranteed to be in a register (see
834 // LocationsBuilderX86::VisitCondition).
835 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000836 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100837 } else if (rhs.IsConstant()) {
838 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
839 Immediate imm(instruction->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000840 __ cmpl(lhs.AsRegister<Register>(), imm);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100841 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000842 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100843 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100844 __ j(X86Condition(cond->AsCondition()->GetCondition()),
845 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700846 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100847 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100848 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
849 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700850 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000851 }
852}
853
854void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000855 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000856}
857
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000858void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
859 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000860}
861
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000862void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100863 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000864}
865
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000866void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100867 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700868 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000869}
870
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100871void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100872 LocationSummary* locations =
873 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100874 switch (store->InputAt(1)->GetType()) {
875 case Primitive::kPrimBoolean:
876 case Primitive::kPrimByte:
877 case Primitive::kPrimChar:
878 case Primitive::kPrimShort:
879 case Primitive::kPrimInt:
880 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100881 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100882 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
883 break;
884
885 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100886 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100887 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
888 break;
889
890 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100891 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100892 }
893 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000894}
895
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000896void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700897 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000898}
899
Dave Allison20dfc792014-06-16 20:44:29 -0700900void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100901 LocationSummary* locations =
902 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100903 locations->SetInAt(0, Location::RequiresRegister());
904 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100905 if (comp->NeedsMaterialization()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000906 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100907 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000908}
909
Dave Allison20dfc792014-06-16 20:44:29 -0700910void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
911 if (comp->NeedsMaterialization()) {
912 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000913 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100914 // Clear register: setcc only sets the low byte.
915 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700916 if (locations->InAt(1).IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000917 __ cmpl(locations->InAt(0).AsRegister<Register>(),
918 locations->InAt(1).AsRegister<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100919 } else if (locations->InAt(1).IsConstant()) {
920 HConstant* instruction = locations->InAt(1).GetConstant();
921 Immediate imm(instruction->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000922 __ cmpl(locations->InAt(0).AsRegister<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700923 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000924 __ cmpl(locations->InAt(0).AsRegister<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700925 Address(ESP, locations->InAt(1).GetStackIndex()));
926 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000927 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100928 }
Dave Allison20dfc792014-06-16 20:44:29 -0700929}
930
931void LocationsBuilderX86::VisitEqual(HEqual* comp) {
932 VisitCondition(comp);
933}
934
935void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
936 VisitCondition(comp);
937}
938
939void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
940 VisitCondition(comp);
941}
942
943void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
944 VisitCondition(comp);
945}
946
947void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
948 VisitCondition(comp);
949}
950
951void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
952 VisitCondition(comp);
953}
954
955void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
956 VisitCondition(comp);
957}
958
959void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
960 VisitCondition(comp);
961}
962
963void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
964 VisitCondition(comp);
965}
966
967void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
968 VisitCondition(comp);
969}
970
971void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
972 VisitCondition(comp);
973}
974
975void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
976 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000977}
978
979void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100980 LocationSummary* locations =
981 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100982 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000983}
984
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000985void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100986 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700987 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000988}
989
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100990void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100991 LocationSummary* locations =
992 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100993 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100994}
995
996void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
997 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700998 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100999}
1000
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001001void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1002 LocationSummary* locations =
1003 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1004 locations->SetOut(Location::ConstantLocation(constant));
1005}
1006
1007void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1008 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001009 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001010}
1011
1012void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1013 LocationSummary* locations =
1014 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1015 locations->SetOut(Location::ConstantLocation(constant));
1016}
1017
1018void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1019 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001020 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001021}
1022
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001023void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001024 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001025}
1026
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001027void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001028 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001029 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001030 __ ret();
1031}
1032
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001033void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001034 LocationSummary* locations =
1035 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001036 switch (ret->InputAt(0)->GetType()) {
1037 case Primitive::kPrimBoolean:
1038 case Primitive::kPrimByte:
1039 case Primitive::kPrimChar:
1040 case Primitive::kPrimShort:
1041 case Primitive::kPrimInt:
1042 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001043 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001044 break;
1045
1046 case Primitive::kPrimLong:
1047 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001048 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001049 break;
1050
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001051 case Primitive::kPrimFloat:
1052 case Primitive::kPrimDouble:
1053 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001054 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001055 break;
1056
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001057 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001058 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001059 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001060}
1061
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001062void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001063 if (kIsDebugBuild) {
1064 switch (ret->InputAt(0)->GetType()) {
1065 case Primitive::kPrimBoolean:
1066 case Primitive::kPrimByte:
1067 case Primitive::kPrimChar:
1068 case Primitive::kPrimShort:
1069 case Primitive::kPrimInt:
1070 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001071 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001072 break;
1073
1074 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001075 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1076 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001077 break;
1078
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001079 case Primitive::kPrimFloat:
1080 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001081 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001082 break;
1083
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001084 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001085 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001086 }
1087 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001088 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001089 __ ret();
1090}
1091
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001092void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001093 HandleInvoke(invoke);
1094}
1095
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001096void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001097 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001098
1099 // TODO: Implement all kinds of calls:
1100 // 1) boot -> boot
1101 // 2) app -> boot
1102 // 3) app -> app
1103 //
1104 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1105
1106 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001107 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffray4e44c822014-12-17 12:25:12 +00001108 // temp = temp->dex_cache_resolved_methods_;
1109 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
1110 // temp = temp[index_in_cache]
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001111 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001112 // (temp + offset_of_quick_compiled_code)()
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001113 __ call(Address(
1114 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001115
1116 DCHECK(!codegen_->IsLeafMethod());
1117 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1118}
1119
1120void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1121 HandleInvoke(invoke);
1122}
1123
1124void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001125 LocationSummary* locations =
1126 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001127 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001128
1129 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001130 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001131 HInstruction* input = invoke->InputAt(i);
1132 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1133 }
1134
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001135 switch (invoke->GetType()) {
1136 case Primitive::kPrimBoolean:
1137 case Primitive::kPrimByte:
1138 case Primitive::kPrimChar:
1139 case Primitive::kPrimShort:
1140 case Primitive::kPrimInt:
1141 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001142 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001143 break;
1144
1145 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001146 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001147 break;
1148
1149 case Primitive::kPrimVoid:
1150 break;
1151
1152 case Primitive::kPrimDouble:
1153 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001154 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001155 break;
1156 }
1157
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001158 invoke->SetLocations(locations);
1159}
1160
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001161void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001162 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001163 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1164 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1165 LocationSummary* locations = invoke->GetLocations();
1166 Location receiver = locations->InAt(0);
1167 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1168 // temp = object->GetClass();
1169 if (receiver.IsStackSlot()) {
1170 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1171 __ movl(temp, Address(temp, class_offset));
1172 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001173 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001174 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001175 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001176 // temp = temp->GetMethodAt(method_offset);
1177 __ movl(temp, Address(temp, method_offset));
1178 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001179 __ call(Address(
1180 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001181
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001182 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001183 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001184}
1185
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001186void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1187 HandleInvoke(invoke);
1188 // Add the hidden argument.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001189 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001190}
1191
1192void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1193 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001194 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001195 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1196 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1197 LocationSummary* locations = invoke->GetLocations();
1198 Location receiver = locations->InAt(0);
1199 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1200
1201 // Set the hidden argument.
1202 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001203 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001204
1205 // temp = object->GetClass();
1206 if (receiver.IsStackSlot()) {
1207 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1208 __ movl(temp, Address(temp, class_offset));
1209 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001210 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001211 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001212 codegen_->MaybeRecordImplicitNullCheck(invoke);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001213 // temp = temp->GetImtEntryAt(method_offset);
1214 __ movl(temp, Address(temp, method_offset));
1215 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001216 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001217 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001218
1219 DCHECK(!codegen_->IsLeafMethod());
1220 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1221}
1222
Roland Levillain88cb1752014-10-20 16:36:47 +01001223void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1224 LocationSummary* locations =
1225 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1226 switch (neg->GetResultType()) {
1227 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001228 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001229 locations->SetInAt(0, Location::RequiresRegister());
1230 locations->SetOut(Location::SameAsFirstInput());
1231 break;
1232
Roland Levillain88cb1752014-10-20 16:36:47 +01001233 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001234 locations->SetInAt(0, Location::RequiresFpuRegister());
1235 locations->SetOut(Location::SameAsFirstInput());
1236 locations->AddTemp(Location::RequiresRegister());
1237 locations->AddTemp(Location::RequiresFpuRegister());
1238 break;
1239
Roland Levillain88cb1752014-10-20 16:36:47 +01001240 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001241 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001242 locations->SetOut(Location::SameAsFirstInput());
1243 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001244 break;
1245
1246 default:
1247 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1248 }
1249}
1250
1251void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1252 LocationSummary* locations = neg->GetLocations();
1253 Location out = locations->Out();
1254 Location in = locations->InAt(0);
1255 switch (neg->GetResultType()) {
1256 case Primitive::kPrimInt:
1257 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001258 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001259 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001260 break;
1261
1262 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001263 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001264 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001265 __ negl(out.AsRegisterPairLow<Register>());
1266 // Negation is similar to subtraction from zero. The least
1267 // significant byte triggers a borrow when it is different from
1268 // zero; to take it into account, add 1 to the most significant
1269 // byte if the carry flag (CF) is set to 1 after the first NEGL
1270 // operation.
1271 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1272 __ negl(out.AsRegisterPairHigh<Register>());
1273 break;
1274
Roland Levillain5368c212014-11-27 15:03:41 +00001275 case Primitive::kPrimFloat: {
1276 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001277 Register constant = locations->GetTemp(0).AsRegister<Register>();
1278 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001279 // Implement float negation with an exclusive or with value
1280 // 0x80000000 (mask for bit 31, representing the sign of a
1281 // single-precision floating-point number).
1282 __ movl(constant, Immediate(INT32_C(0x80000000)));
1283 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001284 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001285 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001286 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001287
Roland Levillain5368c212014-11-27 15:03:41 +00001288 case Primitive::kPrimDouble: {
1289 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001290 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001291 // Implement double negation with an exclusive or with value
1292 // 0x8000000000000000 (mask for bit 63, representing the sign of
1293 // a double-precision floating-point number).
1294 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001295 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001296 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001297 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001298
1299 default:
1300 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1301 }
1302}
1303
Roland Levillaindff1f282014-11-05 14:15:05 +00001304void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001305 Primitive::Type result_type = conversion->GetResultType();
1306 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001307 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001308
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001309 // The float-to-long and double-to-long type conversions rely on a
1310 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001311 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001312 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1313 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001314 ? LocationSummary::kCall
1315 : LocationSummary::kNoCall;
1316 LocationSummary* locations =
1317 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1318
Roland Levillaindff1f282014-11-05 14:15:05 +00001319 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001320 case Primitive::kPrimByte:
1321 switch (input_type) {
1322 case Primitive::kPrimShort:
1323 case Primitive::kPrimInt:
1324 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001325 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001326 locations->SetInAt(0, Location::Any());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001327 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1328 break;
1329
1330 default:
1331 LOG(FATAL) << "Unexpected type conversion from " << input_type
1332 << " to " << result_type;
1333 }
1334 break;
1335
Roland Levillain01a8d712014-11-14 16:27:39 +00001336 case Primitive::kPrimShort:
1337 switch (input_type) {
1338 case Primitive::kPrimByte:
1339 case Primitive::kPrimInt:
1340 case Primitive::kPrimChar:
1341 // Processing a Dex `int-to-short' instruction.
1342 locations->SetInAt(0, Location::Any());
1343 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1344 break;
1345
1346 default:
1347 LOG(FATAL) << "Unexpected type conversion from " << input_type
1348 << " to " << result_type;
1349 }
1350 break;
1351
Roland Levillain946e1432014-11-11 17:35:19 +00001352 case Primitive::kPrimInt:
1353 switch (input_type) {
1354 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001355 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001356 locations->SetInAt(0, Location::Any());
1357 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1358 break;
1359
1360 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001361 // Processing a Dex `float-to-int' instruction.
1362 locations->SetInAt(0, Location::RequiresFpuRegister());
1363 locations->SetOut(Location::RequiresRegister());
1364 locations->AddTemp(Location::RequiresFpuRegister());
1365 break;
1366
Roland Levillain946e1432014-11-11 17:35:19 +00001367 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001368 // Processing a Dex `double-to-int' instruction.
1369 locations->SetInAt(0, Location::RequiresFpuRegister());
1370 locations->SetOut(Location::RequiresRegister());
1371 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001372 break;
1373
1374 default:
1375 LOG(FATAL) << "Unexpected type conversion from " << input_type
1376 << " to " << result_type;
1377 }
1378 break;
1379
Roland Levillaindff1f282014-11-05 14:15:05 +00001380 case Primitive::kPrimLong:
1381 switch (input_type) {
1382 case Primitive::kPrimByte:
1383 case Primitive::kPrimShort:
1384 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001385 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001386 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001387 locations->SetInAt(0, Location::RegisterLocation(EAX));
1388 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1389 break;
1390
Vladimir Marko949c91f2015-01-27 10:48:44 +00001391 case Primitive::kPrimFloat: {
1392 // Processing a Dex `float-to-long' instruction.
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001393 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko949c91f2015-01-27 10:48:44 +00001394 // Note that on x86 floating-point parameters are passed
1395 // through core registers (here, EAX).
1396 locations->SetInAt(0, Location::RegisterLocation(
1397 calling_convention.GetRegisterAt(0)));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001398 // The runtime helper puts the result in EAX, EDX.
1399 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001400 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001401 }
Vladimir Marko949c91f2015-01-27 10:48:44 +00001402
1403 case Primitive::kPrimDouble: {
1404 // Processing a Dex `double-to-long' instruction.
1405 InvokeRuntimeCallingConvention calling_convention;
1406 // Note that on x86 floating-point parameters are passed
1407 // through core registers (here, EAX and ECX).
1408 locations->SetInAt(0, Location::RegisterPairLocation(
1409 calling_convention.GetRegisterAt(0),
1410 calling_convention.GetRegisterAt(1)));
1411 // The runtime helper puts the result in EAX, EDX.
1412 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1413 break;
1414 }
1415 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001416
1417 default:
1418 LOG(FATAL) << "Unexpected type conversion from " << input_type
1419 << " to " << result_type;
1420 }
1421 break;
1422
Roland Levillain981e4542014-11-14 11:47:14 +00001423 case Primitive::kPrimChar:
1424 switch (input_type) {
1425 case Primitive::kPrimByte:
1426 case Primitive::kPrimShort:
1427 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001428 // Processing a Dex `int-to-char' instruction.
1429 locations->SetInAt(0, Location::Any());
1430 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1431 break;
1432
1433 default:
1434 LOG(FATAL) << "Unexpected type conversion from " << input_type
1435 << " to " << result_type;
1436 }
1437 break;
1438
Roland Levillaindff1f282014-11-05 14:15:05 +00001439 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001440 switch (input_type) {
1441 case Primitive::kPrimByte:
1442 case Primitive::kPrimShort:
1443 case Primitive::kPrimInt:
1444 case Primitive::kPrimChar:
1445 // Processing a Dex `int-to-float' instruction.
1446 locations->SetInAt(0, Location::RequiresRegister());
1447 locations->SetOut(Location::RequiresFpuRegister());
1448 break;
1449
1450 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001451 // Processing a Dex `long-to-float' instruction.
1452 locations->SetInAt(0, Location::RequiresRegister());
1453 locations->SetOut(Location::RequiresFpuRegister());
1454 locations->AddTemp(Location::RequiresFpuRegister());
1455 locations->AddTemp(Location::RequiresFpuRegister());
1456 break;
1457
Roland Levillaincff13742014-11-17 14:32:17 +00001458 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001459 // Processing a Dex `double-to-float' instruction.
1460 locations->SetInAt(0, Location::RequiresFpuRegister());
1461 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001462 break;
1463
1464 default:
1465 LOG(FATAL) << "Unexpected type conversion from " << input_type
1466 << " to " << result_type;
1467 };
1468 break;
1469
Roland Levillaindff1f282014-11-05 14:15:05 +00001470 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001471 switch (input_type) {
1472 case Primitive::kPrimByte:
1473 case Primitive::kPrimShort:
1474 case Primitive::kPrimInt:
1475 case Primitive::kPrimChar:
1476 // Processing a Dex `int-to-double' instruction.
1477 locations->SetInAt(0, Location::RequiresRegister());
1478 locations->SetOut(Location::RequiresFpuRegister());
1479 break;
1480
1481 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001482 // Processing a Dex `long-to-double' instruction.
1483 locations->SetInAt(0, Location::RequiresRegister());
1484 locations->SetOut(Location::RequiresFpuRegister());
1485 locations->AddTemp(Location::RequiresFpuRegister());
1486 locations->AddTemp(Location::RequiresFpuRegister());
1487 break;
1488
Roland Levillaincff13742014-11-17 14:32:17 +00001489 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001490 // Processing a Dex `float-to-double' instruction.
1491 locations->SetInAt(0, Location::RequiresFpuRegister());
1492 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001493 break;
1494
1495 default:
1496 LOG(FATAL) << "Unexpected type conversion from " << input_type
1497 << " to " << result_type;
1498 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001499 break;
1500
1501 default:
1502 LOG(FATAL) << "Unexpected type conversion from " << input_type
1503 << " to " << result_type;
1504 }
1505}
1506
1507void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1508 LocationSummary* locations = conversion->GetLocations();
1509 Location out = locations->Out();
1510 Location in = locations->InAt(0);
1511 Primitive::Type result_type = conversion->GetResultType();
1512 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001513 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001514 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001515 case Primitive::kPrimByte:
1516 switch (input_type) {
1517 case Primitive::kPrimShort:
1518 case Primitive::kPrimInt:
1519 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001520 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001521 if (in.IsRegister()) {
1522 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
1523 } else if (in.IsStackSlot()) {
1524 __ movsxb(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
1525 } else {
1526 DCHECK(in.GetConstant()->IsIntConstant());
1527 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1528 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1529 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001530 break;
1531
1532 default:
1533 LOG(FATAL) << "Unexpected type conversion from " << input_type
1534 << " to " << result_type;
1535 }
1536 break;
1537
Roland Levillain01a8d712014-11-14 16:27:39 +00001538 case Primitive::kPrimShort:
1539 switch (input_type) {
1540 case Primitive::kPrimByte:
1541 case Primitive::kPrimInt:
1542 case Primitive::kPrimChar:
1543 // Processing a Dex `int-to-short' instruction.
1544 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001545 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001546 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001547 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001548 } else {
1549 DCHECK(in.GetConstant()->IsIntConstant());
1550 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001551 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001552 }
1553 break;
1554
1555 default:
1556 LOG(FATAL) << "Unexpected type conversion from " << input_type
1557 << " to " << result_type;
1558 }
1559 break;
1560
Roland Levillain946e1432014-11-11 17:35:19 +00001561 case Primitive::kPrimInt:
1562 switch (input_type) {
1563 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001564 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001565 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001566 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001567 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001568 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001569 } else {
1570 DCHECK(in.IsConstant());
1571 DCHECK(in.GetConstant()->IsLongConstant());
1572 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001573 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001574 }
1575 break;
1576
Roland Levillain3f8f9362014-12-02 17:45:01 +00001577 case Primitive::kPrimFloat: {
1578 // Processing a Dex `float-to-int' instruction.
1579 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1580 Register output = out.AsRegister<Register>();
1581 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1582 Label done, nan;
1583
1584 __ movl(output, Immediate(kPrimIntMax));
1585 // temp = int-to-float(output)
1586 __ cvtsi2ss(temp, output);
1587 // if input >= temp goto done
1588 __ comiss(input, temp);
1589 __ j(kAboveEqual, &done);
1590 // if input == NaN goto nan
1591 __ j(kUnordered, &nan);
1592 // output = float-to-int-truncate(input)
1593 __ cvttss2si(output, input);
1594 __ jmp(&done);
1595 __ Bind(&nan);
1596 // output = 0
1597 __ xorl(output, output);
1598 __ Bind(&done);
1599 break;
1600 }
1601
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001602 case Primitive::kPrimDouble: {
1603 // Processing a Dex `double-to-int' instruction.
1604 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1605 Register output = out.AsRegister<Register>();
1606 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1607 Label done, nan;
1608
1609 __ movl(output, Immediate(kPrimIntMax));
1610 // temp = int-to-double(output)
1611 __ cvtsi2sd(temp, output);
1612 // if input >= temp goto done
1613 __ comisd(input, temp);
1614 __ j(kAboveEqual, &done);
1615 // if input == NaN goto nan
1616 __ j(kUnordered, &nan);
1617 // output = double-to-int-truncate(input)
1618 __ cvttsd2si(output, input);
1619 __ jmp(&done);
1620 __ Bind(&nan);
1621 // output = 0
1622 __ xorl(output, output);
1623 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001624 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001625 }
Roland Levillain946e1432014-11-11 17:35:19 +00001626
1627 default:
1628 LOG(FATAL) << "Unexpected type conversion from " << input_type
1629 << " to " << result_type;
1630 }
1631 break;
1632
Roland Levillaindff1f282014-11-05 14:15:05 +00001633 case Primitive::kPrimLong:
1634 switch (input_type) {
1635 case Primitive::kPrimByte:
1636 case Primitive::kPrimShort:
1637 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001638 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001639 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001640 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1641 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001642 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001643 __ cdq();
1644 break;
1645
1646 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001647 // Processing a Dex `float-to-long' instruction.
1648 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001649 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1650 break;
1651
Roland Levillaindff1f282014-11-05 14:15:05 +00001652 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001653 // Processing a Dex `double-to-long' instruction.
1654 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1655 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001656 break;
1657
1658 default:
1659 LOG(FATAL) << "Unexpected type conversion from " << input_type
1660 << " to " << result_type;
1661 }
1662 break;
1663
Roland Levillain981e4542014-11-14 11:47:14 +00001664 case Primitive::kPrimChar:
1665 switch (input_type) {
1666 case Primitive::kPrimByte:
1667 case Primitive::kPrimShort:
1668 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001669 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1670 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001671 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001672 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001673 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001674 } else {
1675 DCHECK(in.GetConstant()->IsIntConstant());
1676 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001677 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001678 }
1679 break;
1680
1681 default:
1682 LOG(FATAL) << "Unexpected type conversion from " << input_type
1683 << " to " << result_type;
1684 }
1685 break;
1686
Roland Levillaindff1f282014-11-05 14:15:05 +00001687 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001688 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001689 case Primitive::kPrimByte:
1690 case Primitive::kPrimShort:
1691 case Primitive::kPrimInt:
1692 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001693 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001694 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001695 break;
1696
Roland Levillain6d0e4832014-11-27 18:31:21 +00001697 case Primitive::kPrimLong: {
1698 // Processing a Dex `long-to-float' instruction.
1699 Register low = in.AsRegisterPairLow<Register>();
1700 Register high = in.AsRegisterPairHigh<Register>();
1701 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1702 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1703 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1704
1705 // Operations use doubles for precision reasons (each 32-bit
1706 // half of a long fits in the 53-bit mantissa of a double,
1707 // but not in the 24-bit mantissa of a float). This is
1708 // especially important for the low bits. The result is
1709 // eventually converted to float.
1710
1711 // low = low - 2^31 (to prevent bit 31 of `low` to be
1712 // interpreted as a sign bit)
1713 __ subl(low, Immediate(0x80000000));
1714 // temp = int-to-double(high)
1715 __ cvtsi2sd(temp, high);
1716 // temp = temp * 2^32
1717 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
1718 __ mulsd(temp, constant);
1719 // result = int-to-double(low)
1720 __ cvtsi2sd(result, low);
1721 // result = result + 2^31 (restore the original value of `low`)
1722 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
1723 __ addsd(result, constant);
1724 // result = result + temp
1725 __ addsd(result, temp);
1726 // result = double-to-float(result)
1727 __ cvtsd2ss(result, result);
1728 break;
1729 }
1730
Roland Levillaincff13742014-11-17 14:32:17 +00001731 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001732 // Processing a Dex `double-to-float' instruction.
1733 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001734 break;
1735
1736 default:
1737 LOG(FATAL) << "Unexpected type conversion from " << input_type
1738 << " to " << result_type;
1739 };
1740 break;
1741
Roland Levillaindff1f282014-11-05 14:15:05 +00001742 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001743 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001744 case Primitive::kPrimByte:
1745 case Primitive::kPrimShort:
1746 case Primitive::kPrimInt:
1747 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001748 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001749 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001750 break;
1751
Roland Levillain647b9ed2014-11-27 12:06:00 +00001752 case Primitive::kPrimLong: {
1753 // Processing a Dex `long-to-double' instruction.
1754 Register low = in.AsRegisterPairLow<Register>();
1755 Register high = in.AsRegisterPairHigh<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001756 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1757 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1758 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001759
Roland Levillain647b9ed2014-11-27 12:06:00 +00001760 // low = low - 2^31 (to prevent bit 31 of `low` to be
1761 // interpreted as a sign bit)
1762 __ subl(low, Immediate(0x80000000));
1763 // temp = int-to-double(high)
1764 __ cvtsi2sd(temp, high);
1765 // temp = temp * 2^32
Roland Levillain6d0e4832014-11-27 18:31:21 +00001766 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001767 __ mulsd(temp, constant);
1768 // result = int-to-double(low)
1769 __ cvtsi2sd(result, low);
1770 // result = result + 2^31 (restore the original value of `low`)
Roland Levillain6d0e4832014-11-27 18:31:21 +00001771 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001772 __ addsd(result, constant);
1773 // result = result + temp
1774 __ addsd(result, temp);
1775 break;
1776 }
1777
Roland Levillaincff13742014-11-17 14:32:17 +00001778 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001779 // Processing a Dex `float-to-double' instruction.
1780 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001781 break;
1782
1783 default:
1784 LOG(FATAL) << "Unexpected type conversion from " << input_type
1785 << " to " << result_type;
1786 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001787 break;
1788
1789 default:
1790 LOG(FATAL) << "Unexpected type conversion from " << input_type
1791 << " to " << result_type;
1792 }
1793}
1794
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001795void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001796 LocationSummary* locations =
1797 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001798 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001799 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001800 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001801 locations->SetInAt(0, Location::RequiresRegister());
1802 locations->SetInAt(1, Location::Any());
1803 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001804 break;
1805 }
1806
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001807 case Primitive::kPrimFloat:
1808 case Primitive::kPrimDouble: {
1809 locations->SetInAt(0, Location::RequiresFpuRegister());
1810 locations->SetInAt(1, Location::Any());
1811 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001812 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001813 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001814
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001815 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001816 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1817 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001818 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001819}
1820
1821void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1822 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001823 Location first = locations->InAt(0);
1824 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001825 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001826 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001827 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001828 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001829 __ addl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001830 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001831 __ addl(first.AsRegister<Register>(),
1832 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001833 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001834 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001835 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001836 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001837 }
1838
1839 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001840 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001841 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1842 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001843 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001844 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1845 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001846 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001847 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001848 break;
1849 }
1850
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001851 case Primitive::kPrimFloat: {
1852 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001853 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001854 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001855 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001856 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001857 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001858 }
1859
1860 case Primitive::kPrimDouble: {
1861 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001862 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001863 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001864 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001865 }
1866 break;
1867 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001868
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001869 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001870 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001871 }
1872}
1873
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001874void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001875 LocationSummary* locations =
1876 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001877 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001878 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001879 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001880 locations->SetInAt(0, Location::RequiresRegister());
1881 locations->SetInAt(1, Location::Any());
1882 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001883 break;
1884 }
Calin Juravle11351682014-10-23 15:38:15 +01001885 case Primitive::kPrimFloat:
1886 case Primitive::kPrimDouble: {
1887 locations->SetInAt(0, Location::RequiresFpuRegister());
1888 locations->SetInAt(1, Location::RequiresFpuRegister());
1889 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001890 break;
Calin Juravle11351682014-10-23 15:38:15 +01001891 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001892
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001893 default:
Calin Juravle11351682014-10-23 15:38:15 +01001894 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001895 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001896}
1897
1898void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1899 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001900 Location first = locations->InAt(0);
1901 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001902 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001903 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001904 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001905 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001906 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001907 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001908 __ subl(first.AsRegister<Register>(),
1909 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001910 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001911 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001912 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001913 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001914 }
1915
1916 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001917 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01001918 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1919 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001920 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001921 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001922 __ sbbl(first.AsRegisterPairHigh<Register>(),
1923 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001924 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001925 break;
1926 }
1927
Calin Juravle11351682014-10-23 15:38:15 +01001928 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001929 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001930 break;
Calin Juravle11351682014-10-23 15:38:15 +01001931 }
1932
1933 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001934 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001935 break;
1936 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001937
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001938 default:
Calin Juravle11351682014-10-23 15:38:15 +01001939 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001940 }
1941}
1942
Calin Juravle34bacdf2014-10-07 20:23:36 +01001943void LocationsBuilderX86::VisitMul(HMul* mul) {
1944 LocationSummary* locations =
1945 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1946 switch (mul->GetResultType()) {
1947 case Primitive::kPrimInt:
1948 locations->SetInAt(0, Location::RequiresRegister());
1949 locations->SetInAt(1, Location::Any());
1950 locations->SetOut(Location::SameAsFirstInput());
1951 break;
1952 case Primitive::kPrimLong: {
1953 locations->SetInAt(0, Location::RequiresRegister());
1954 // TODO: Currently this handles only stack operands:
1955 // - we don't have enough registers because we currently use Quick ABI.
1956 // - by the time we have a working register allocator we will probably change the ABI
1957 // and fix the above.
1958 // - we don't have a way yet to request operands on stack but the base line compiler
1959 // will leave the operands on the stack with Any().
1960 locations->SetInAt(1, Location::Any());
1961 locations->SetOut(Location::SameAsFirstInput());
1962 // Needed for imul on 32bits with 64bits output.
1963 locations->AddTemp(Location::RegisterLocation(EAX));
1964 locations->AddTemp(Location::RegisterLocation(EDX));
1965 break;
1966 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001967 case Primitive::kPrimFloat:
1968 case Primitive::kPrimDouble: {
1969 locations->SetInAt(0, Location::RequiresFpuRegister());
1970 locations->SetInAt(1, Location::RequiresFpuRegister());
1971 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001972 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001973 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001974
1975 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001976 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001977 }
1978}
1979
1980void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1981 LocationSummary* locations = mul->GetLocations();
1982 Location first = locations->InAt(0);
1983 Location second = locations->InAt(1);
1984 DCHECK(first.Equals(locations->Out()));
1985
1986 switch (mul->GetResultType()) {
1987 case Primitive::kPrimInt: {
1988 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001989 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001990 } else if (second.IsConstant()) {
1991 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001992 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01001993 } else {
1994 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001995 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01001996 }
1997 break;
1998 }
1999
2000 case Primitive::kPrimLong: {
2001 DCHECK(second.IsDoubleStackSlot());
2002
2003 Register in1_hi = first.AsRegisterPairHigh<Register>();
2004 Register in1_lo = first.AsRegisterPairLow<Register>();
2005 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2006 Address in2_lo(ESP, second.GetStackIndex());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002007 Register eax = locations->GetTemp(0).AsRegister<Register>();
2008 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002009
2010 DCHECK_EQ(EAX, eax);
2011 DCHECK_EQ(EDX, edx);
2012
2013 // input: in1 - 64 bits, in2 - 64 bits
2014 // output: in1
2015 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2016 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2017 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
2018
2019 __ movl(eax, in2_hi);
2020 // eax <- in1.lo * in2.hi
2021 __ imull(eax, in1_lo);
2022 // in1.hi <- in1.hi * in2.lo
2023 __ imull(in1_hi, in2_lo);
2024 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2025 __ addl(in1_hi, eax);
2026 // move in1_lo to eax to prepare for double precision
2027 __ movl(eax, in1_lo);
2028 // edx:eax <- in1.lo * in2.lo
2029 __ mull(in2_lo);
2030 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2031 __ addl(in1_hi, edx);
2032 // in1.lo <- (in1.lo * in2.lo)[31:0];
2033 __ movl(in1_lo, eax);
2034
2035 break;
2036 }
2037
Calin Juravleb5bfa962014-10-21 18:02:24 +01002038 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002039 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002040 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002041 }
2042
2043 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002044 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002045 break;
2046 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002047
2048 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002049 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002050 }
2051}
2052
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002053void InstructionCodeGeneratorX86::PushOntoFPStack(Location source, uint32_t temp_offset,
2054 uint32_t stack_adjustment, bool is_float) {
2055 if (source.IsStackSlot()) {
2056 DCHECK(is_float);
2057 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2058 } else if (source.IsDoubleStackSlot()) {
2059 DCHECK(!is_float);
2060 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2061 } else {
2062 // Write the value to the temporary location on the stack and load to FP stack.
2063 if (is_float) {
2064 Location stack_temp = Location::StackSlot(temp_offset);
2065 codegen_->Move32(stack_temp, source);
2066 __ flds(Address(ESP, temp_offset));
2067 } else {
2068 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2069 codegen_->Move64(stack_temp, source);
2070 __ fldl(Address(ESP, temp_offset));
2071 }
2072 }
2073}
2074
2075void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2076 Primitive::Type type = rem->GetResultType();
2077 bool is_float = type == Primitive::kPrimFloat;
2078 size_t elem_size = Primitive::ComponentSize(type);
2079 LocationSummary* locations = rem->GetLocations();
2080 Location first = locations->InAt(0);
2081 Location second = locations->InAt(1);
2082 Location out = locations->Out();
2083
2084 // Create stack space for 2 elements.
2085 // TODO: enhance register allocator to ask for stack temporaries.
2086 __ subl(ESP, Immediate(2 * elem_size));
2087
2088 // Load the values to the FP stack in reverse order, using temporaries if needed.
2089 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
2090 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
2091
2092 // Loop doing FPREM until we stabilize.
2093 Label retry;
2094 __ Bind(&retry);
2095 __ fprem();
2096
2097 // Move FP status to AX.
2098 __ fstsw();
2099
2100 // And see if the argument reduction is complete. This is signaled by the
2101 // C2 FPU flag bit set to 0.
2102 __ andl(EAX, Immediate(kC2ConditionMask));
2103 __ j(kNotEqual, &retry);
2104
2105 // We have settled on the final value. Retrieve it into an XMM register.
2106 // Store FP top of stack to real stack.
2107 if (is_float) {
2108 __ fsts(Address(ESP, 0));
2109 } else {
2110 __ fstl(Address(ESP, 0));
2111 }
2112
2113 // Pop the 2 items from the FP stack.
2114 __ fucompp();
2115
2116 // Load the value from the stack into an XMM register.
2117 DCHECK(out.IsFpuRegister()) << out;
2118 if (is_float) {
2119 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2120 } else {
2121 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2122 }
2123
2124 // And remove the temporary stack space we allocated.
2125 __ addl(ESP, Immediate(2 * elem_size));
2126}
2127
Calin Juravlebacfec32014-11-14 15:54:36 +00002128void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2129 DCHECK(instruction->IsDiv() || instruction->IsRem());
2130
2131 LocationSummary* locations = instruction->GetLocations();
2132 Location out = locations->Out();
2133 Location first = locations->InAt(0);
2134 Location second = locations->InAt(1);
2135 bool is_div = instruction->IsDiv();
2136
2137 switch (instruction->GetResultType()) {
2138 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002139 Register second_reg = second.AsRegister<Register>();
2140 DCHECK_EQ(EAX, first.AsRegister<Register>());
2141 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002142
2143 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002144 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
2145 is_div);
Calin Juravlebacfec32014-11-14 15:54:36 +00002146 codegen_->AddSlowPath(slow_path);
2147
2148 // 0x80000000/-1 triggers an arithmetic exception!
2149 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2150 // it's safe to just use negl instead of more complex comparisons.
2151
2152 __ cmpl(second_reg, Immediate(-1));
2153 __ j(kEqual, slow_path->GetEntryLabel());
2154
2155 // edx:eax <- sign-extended of eax
2156 __ cdq();
2157 // eax = quotient, edx = remainder
2158 __ idivl(second_reg);
2159
2160 __ Bind(slow_path->GetExitLabel());
2161 break;
2162 }
2163
2164 case Primitive::kPrimLong: {
2165 InvokeRuntimeCallingConvention calling_convention;
2166 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2167 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2168 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2169 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2170 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2171 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2172
2173 if (is_div) {
2174 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2175 } else {
2176 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2177 }
2178 uint32_t dex_pc = is_div
2179 ? instruction->AsDiv()->GetDexPc()
2180 : instruction->AsRem()->GetDexPc();
2181 codegen_->RecordPcInfo(instruction, dex_pc);
2182
2183 break;
2184 }
2185
2186 default:
2187 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2188 }
2189}
2190
Calin Juravle7c4954d2014-10-28 16:57:40 +00002191void LocationsBuilderX86::VisitDiv(HDiv* div) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002192 LocationSummary::CallKind call_kind = div->GetResultType() == Primitive::kPrimLong
2193 ? LocationSummary::kCall
2194 : LocationSummary::kNoCall;
2195 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2196
Calin Juravle7c4954d2014-10-28 16:57:40 +00002197 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002198 case Primitive::kPrimInt: {
2199 locations->SetInAt(0, Location::RegisterLocation(EAX));
2200 locations->SetInAt(1, Location::RequiresRegister());
2201 locations->SetOut(Location::SameAsFirstInput());
2202 // Intel uses edx:eax as the dividend.
2203 locations->AddTemp(Location::RegisterLocation(EDX));
2204 break;
2205 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002206 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002207 InvokeRuntimeCallingConvention calling_convention;
2208 locations->SetInAt(0, Location::RegisterPairLocation(
2209 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2210 locations->SetInAt(1, Location::RegisterPairLocation(
2211 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2212 // Runtime helper puts the result in EAX, EDX.
2213 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002214 break;
2215 }
2216 case Primitive::kPrimFloat:
2217 case Primitive::kPrimDouble: {
2218 locations->SetInAt(0, Location::RequiresFpuRegister());
2219 locations->SetInAt(1, Location::RequiresFpuRegister());
2220 locations->SetOut(Location::SameAsFirstInput());
2221 break;
2222 }
2223
2224 default:
2225 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2226 }
2227}
2228
2229void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2230 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002231 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002232 Location first = locations->InAt(0);
2233 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002234
2235 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002236 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002237 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002238 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002239 break;
2240 }
2241
2242 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002243 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002244 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002245 break;
2246 }
2247
2248 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002249 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002250 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002251 break;
2252 }
2253
2254 default:
2255 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2256 }
2257}
2258
Calin Juravlebacfec32014-11-14 15:54:36 +00002259void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002260 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002261 LocationSummary* locations =
2262 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravlebacfec32014-11-14 15:54:36 +00002263
Calin Juravled2ec87d2014-12-08 14:24:46 +00002264 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002265 case Primitive::kPrimInt: {
2266 locations->SetInAt(0, Location::RegisterLocation(EAX));
2267 locations->SetInAt(1, Location::RequiresRegister());
2268 locations->SetOut(Location::RegisterLocation(EDX));
2269 break;
2270 }
2271 case Primitive::kPrimLong: {
2272 InvokeRuntimeCallingConvention calling_convention;
2273 locations->SetInAt(0, Location::RegisterPairLocation(
2274 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2275 locations->SetInAt(1, Location::RegisterPairLocation(
2276 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2277 // Runtime helper puts the result in EAX, EDX.
2278 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2279 break;
2280 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002281 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002282 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002283 locations->SetInAt(0, Location::Any());
2284 locations->SetInAt(1, Location::Any());
2285 locations->SetOut(Location::RequiresFpuRegister());
2286 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00002287 break;
2288 }
2289
2290 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002291 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002292 }
2293}
2294
2295void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2296 Primitive::Type type = rem->GetResultType();
2297 switch (type) {
2298 case Primitive::kPrimInt:
2299 case Primitive::kPrimLong: {
2300 GenerateDivRemIntegral(rem);
2301 break;
2302 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002303 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00002304 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002305 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00002306 break;
2307 }
2308 default:
2309 LOG(FATAL) << "Unexpected rem type " << type;
2310 }
2311}
2312
Calin Juravled0d48522014-11-04 16:40:20 +00002313void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2314 LocationSummary* locations =
2315 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002316 switch (instruction->GetType()) {
2317 case Primitive::kPrimInt: {
2318 locations->SetInAt(0, Location::Any());
2319 break;
2320 }
2321 case Primitive::kPrimLong: {
2322 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2323 if (!instruction->IsConstant()) {
2324 locations->AddTemp(Location::RequiresRegister());
2325 }
2326 break;
2327 }
2328 default:
2329 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2330 }
Calin Juravled0d48522014-11-04 16:40:20 +00002331 if (instruction->HasUses()) {
2332 locations->SetOut(Location::SameAsFirstInput());
2333 }
2334}
2335
2336void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2337 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2338 codegen_->AddSlowPath(slow_path);
2339
2340 LocationSummary* locations = instruction->GetLocations();
2341 Location value = locations->InAt(0);
2342
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002343 switch (instruction->GetType()) {
2344 case Primitive::kPrimInt: {
2345 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002346 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002347 __ j(kEqual, slow_path->GetEntryLabel());
2348 } else if (value.IsStackSlot()) {
2349 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2350 __ j(kEqual, slow_path->GetEntryLabel());
2351 } else {
2352 DCHECK(value.IsConstant()) << value;
2353 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2354 __ jmp(slow_path->GetEntryLabel());
2355 }
2356 }
2357 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002358 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002359 case Primitive::kPrimLong: {
2360 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002361 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002362 __ movl(temp, value.AsRegisterPairLow<Register>());
2363 __ orl(temp, value.AsRegisterPairHigh<Register>());
2364 __ j(kEqual, slow_path->GetEntryLabel());
2365 } else {
2366 DCHECK(value.IsConstant()) << value;
2367 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2368 __ jmp(slow_path->GetEntryLabel());
2369 }
2370 }
2371 break;
2372 }
2373 default:
2374 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002375 }
Calin Juravled0d48522014-11-04 16:40:20 +00002376}
2377
Calin Juravle9aec02f2014-11-18 23:06:35 +00002378void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2379 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2380
2381 LocationSummary* locations =
2382 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2383
2384 switch (op->GetResultType()) {
2385 case Primitive::kPrimInt: {
2386 locations->SetInAt(0, Location::RequiresRegister());
2387 // The shift count needs to be in CL.
2388 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2389 locations->SetOut(Location::SameAsFirstInput());
2390 break;
2391 }
2392 case Primitive::kPrimLong: {
2393 locations->SetInAt(0, Location::RequiresRegister());
2394 // The shift count needs to be in CL.
2395 locations->SetInAt(1, Location::RegisterLocation(ECX));
2396 locations->SetOut(Location::SameAsFirstInput());
2397 break;
2398 }
2399 default:
2400 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2401 }
2402}
2403
2404void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2405 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2406
2407 LocationSummary* locations = op->GetLocations();
2408 Location first = locations->InAt(0);
2409 Location second = locations->InAt(1);
2410 DCHECK(first.Equals(locations->Out()));
2411
2412 switch (op->GetResultType()) {
2413 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002414 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002415 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002416 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002417 DCHECK_EQ(ECX, second_reg);
2418 if (op->IsShl()) {
2419 __ shll(first_reg, second_reg);
2420 } else if (op->IsShr()) {
2421 __ sarl(first_reg, second_reg);
2422 } else {
2423 __ shrl(first_reg, second_reg);
2424 }
2425 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002426 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002427 if (op->IsShl()) {
2428 __ shll(first_reg, imm);
2429 } else if (op->IsShr()) {
2430 __ sarl(first_reg, imm);
2431 } else {
2432 __ shrl(first_reg, imm);
2433 }
2434 }
2435 break;
2436 }
2437 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002438 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002439 DCHECK_EQ(ECX, second_reg);
2440 if (op->IsShl()) {
2441 GenerateShlLong(first, second_reg);
2442 } else if (op->IsShr()) {
2443 GenerateShrLong(first, second_reg);
2444 } else {
2445 GenerateUShrLong(first, second_reg);
2446 }
2447 break;
2448 }
2449 default:
2450 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2451 }
2452}
2453
2454void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2455 Label done;
2456 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2457 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2458 __ testl(shifter, Immediate(32));
2459 __ j(kEqual, &done);
2460 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2461 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2462 __ Bind(&done);
2463}
2464
2465void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2466 Label done;
2467 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2468 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2469 __ testl(shifter, Immediate(32));
2470 __ j(kEqual, &done);
2471 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2472 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2473 __ Bind(&done);
2474}
2475
2476void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2477 Label done;
2478 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2479 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2480 __ testl(shifter, Immediate(32));
2481 __ j(kEqual, &done);
2482 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2483 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2484 __ Bind(&done);
2485}
2486
2487void LocationsBuilderX86::VisitShl(HShl* shl) {
2488 HandleShift(shl);
2489}
2490
2491void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2492 HandleShift(shl);
2493}
2494
2495void LocationsBuilderX86::VisitShr(HShr* shr) {
2496 HandleShift(shr);
2497}
2498
2499void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2500 HandleShift(shr);
2501}
2502
2503void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2504 HandleShift(ushr);
2505}
2506
2507void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2508 HandleShift(ushr);
2509}
2510
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002511void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002512 LocationSummary* locations =
2513 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002514 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002515 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002516 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2517 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002518}
2519
2520void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2521 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002522 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002523 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002524
Nicolas Geoffray707c8092014-04-04 10:50:14 +01002525 __ fs()->call(
2526 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002527
Nicolas Geoffray39468442014-09-02 15:17:15 +01002528 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002529 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002530}
2531
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002532void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2533 LocationSummary* locations =
2534 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2535 locations->SetOut(Location::RegisterLocation(EAX));
2536 InvokeRuntimeCallingConvention calling_convention;
2537 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002538 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2539 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002540}
2541
2542void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2543 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002544 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002545 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2546
2547 __ fs()->call(
2548 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
2549
2550 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2551 DCHECK(!codegen_->IsLeafMethod());
2552}
2553
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002554void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002555 LocationSummary* locations =
2556 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002557 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2558 if (location.IsStackSlot()) {
2559 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2560 } else if (location.IsDoubleStackSlot()) {
2561 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002562 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002563 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002564}
2565
2566void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002567 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002568}
2569
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002570void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002571 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002572 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002573 locations->SetInAt(0, Location::RequiresRegister());
2574 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002575}
2576
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002577void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2578 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002579 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002580 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002581 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002582 switch (not_->InputAt(0)->GetType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002583 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002584 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002585 break;
2586
2587 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002588 __ notl(out.AsRegisterPairLow<Register>());
2589 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002590 break;
2591
2592 default:
2593 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2594 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002595}
2596
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002597void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002598 LocationSummary* locations =
2599 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002600 switch (compare->InputAt(0)->GetType()) {
2601 case Primitive::kPrimLong: {
2602 locations->SetInAt(0, Location::RequiresRegister());
2603 // TODO: we set any here but we don't handle constants
2604 locations->SetInAt(1, Location::Any());
2605 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2606 break;
2607 }
2608 case Primitive::kPrimFloat:
2609 case Primitive::kPrimDouble: {
2610 locations->SetInAt(0, Location::RequiresFpuRegister());
2611 locations->SetInAt(1, Location::RequiresFpuRegister());
2612 locations->SetOut(Location::RequiresRegister());
2613 break;
2614 }
2615 default:
2616 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2617 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002618}
2619
2620void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002621 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002622 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002623 Location left = locations->InAt(0);
2624 Location right = locations->InAt(1);
2625
2626 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002627 switch (compare->InputAt(0)->GetType()) {
2628 case Primitive::kPrimLong: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002629 if (right.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002630 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002631 } else {
2632 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002633 __ cmpl(left.AsRegisterPairHigh<Register>(),
2634 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002635 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002636 __ j(kLess, &less); // Signed compare.
2637 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002638 if (right.IsRegisterPair()) {
2639 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002640 } else {
2641 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002642 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002643 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002644 break;
2645 }
2646 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002647 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002648 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
2649 break;
2650 }
2651 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002652 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002653 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002654 break;
2655 }
2656 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002657 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002658 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002659 __ movl(out, Immediate(0));
2660 __ j(kEqual, &done);
2661 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
2662
2663 __ Bind(&greater);
2664 __ movl(out, Immediate(1));
2665 __ jmp(&done);
2666
2667 __ Bind(&less);
2668 __ movl(out, Immediate(-1));
2669
2670 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002671}
2672
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002673void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002674 LocationSummary* locations =
2675 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002676 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2677 locations->SetInAt(i, Location::Any());
2678 }
2679 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002680}
2681
2682void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002683 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002684 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002685}
2686
Calin Juravle52c48962014-12-16 17:02:57 +00002687void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
2688 /*
2689 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2690 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2691 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2692 */
2693 switch (kind) {
2694 case MemBarrierKind::kAnyAny: {
2695 __ mfence();
2696 break;
2697 }
2698 case MemBarrierKind::kAnyStore:
2699 case MemBarrierKind::kLoadAny:
2700 case MemBarrierKind::kStoreStore: {
2701 // nop
2702 break;
2703 }
2704 default:
2705 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002706 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002707}
2708
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002709
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002710void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002711 Label is_null;
2712 __ testl(value, value);
2713 __ j(kEqual, &is_null);
2714 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2715 __ movl(temp, object);
2716 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002717 __ movb(Address(temp, card, TIMES_1, 0),
2718 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002719 __ Bind(&is_null);
2720}
2721
Calin Juravle52c48962014-12-16 17:02:57 +00002722void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
2723 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002724 LocationSummary* locations =
2725 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002726 locations->SetInAt(0, Location::RequiresRegister());
2727 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle52c48962014-12-16 17:02:57 +00002728
2729 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
2730 // Long values can be loaded atomically into an XMM using movsd.
2731 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
2732 // and then copy the XMM into the output 32bits at a time).
2733 locations->AddTemp(Location::RequiresFpuRegister());
2734 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002735}
2736
Calin Juravle52c48962014-12-16 17:02:57 +00002737void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
2738 const FieldInfo& field_info) {
2739 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002740
Calin Juravle52c48962014-12-16 17:02:57 +00002741 LocationSummary* locations = instruction->GetLocations();
2742 Register base = locations->InAt(0).AsRegister<Register>();
2743 Location out = locations->Out();
2744 bool is_volatile = field_info.IsVolatile();
2745 Primitive::Type field_type = field_info.GetFieldType();
2746 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2747
2748 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002749 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00002750 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002751 break;
2752 }
2753
2754 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002755 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002756 break;
2757 }
2758
2759 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00002760 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002761 break;
2762 }
2763
2764 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002765 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002766 break;
2767 }
2768
2769 case Primitive::kPrimInt:
2770 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002771 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002772 break;
2773 }
2774
2775 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00002776 if (is_volatile) {
2777 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2778 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002779 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002780 __ movd(out.AsRegisterPairLow<Register>(), temp);
2781 __ psrlq(temp, Immediate(32));
2782 __ movd(out.AsRegisterPairHigh<Register>(), temp);
2783 } else {
2784 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002785 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002786 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
2787 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002788 break;
2789 }
2790
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002791 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002792 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002793 break;
2794 }
2795
2796 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002797 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002798 break;
2799 }
2800
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002801 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00002802 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002803 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002804 }
Calin Juravle52c48962014-12-16 17:02:57 +00002805
Calin Juravle77520bc2015-01-12 18:45:46 +00002806 // Longs are handled in the switch.
2807 if (field_type != Primitive::kPrimLong) {
2808 codegen_->MaybeRecordImplicitNullCheck(instruction);
2809 }
2810
Calin Juravle52c48962014-12-16 17:02:57 +00002811 if (is_volatile) {
2812 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2813 }
2814}
2815
2816void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
2817 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2818
2819 LocationSummary* locations =
2820 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2821 locations->SetInAt(0, Location::RequiresRegister());
2822 bool is_volatile = field_info.IsVolatile();
2823 Primitive::Type field_type = field_info.GetFieldType();
2824 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2825 || (field_type == Primitive::kPrimByte);
2826
2827 // The register allocator does not support multiple
2828 // inputs that die at entry with one in a specific register.
2829 if (is_byte_type) {
2830 // Ensure the value is in a byte register.
2831 locations->SetInAt(1, Location::RegisterLocation(EAX));
2832 } else {
2833 locations->SetInAt(1, Location::RequiresRegister());
2834 }
2835 // Temporary registers for the write barrier.
2836 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2837 locations->AddTemp(Location::RequiresRegister());
2838 // Ensure the card is in a byte register.
2839 locations->AddTemp(Location::RegisterLocation(ECX));
2840 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
2841 // 64bits value can be atomically written to an address with movsd and an XMM register.
2842 // We need two XMM registers because there's no easier way to (bit) copy a register pair
2843 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
2844 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
2845 // isolated cases when we need this it isn't worth adding the extra complexity.
2846 locations->AddTemp(Location::RequiresFpuRegister());
2847 locations->AddTemp(Location::RequiresFpuRegister());
2848 }
2849}
2850
2851void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
2852 const FieldInfo& field_info) {
2853 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2854
2855 LocationSummary* locations = instruction->GetLocations();
2856 Register base = locations->InAt(0).AsRegister<Register>();
2857 Location value = locations->InAt(1);
2858 bool is_volatile = field_info.IsVolatile();
2859 Primitive::Type field_type = field_info.GetFieldType();
2860 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2861
2862 if (is_volatile) {
2863 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2864 }
2865
2866 switch (field_type) {
2867 case Primitive::kPrimBoolean:
2868 case Primitive::kPrimByte: {
2869 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
2870 break;
2871 }
2872
2873 case Primitive::kPrimShort:
2874 case Primitive::kPrimChar: {
2875 __ movw(Address(base, offset), value.AsRegister<Register>());
2876 break;
2877 }
2878
2879 case Primitive::kPrimInt:
2880 case Primitive::kPrimNot: {
2881 __ movl(Address(base, offset), value.AsRegister<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00002882 break;
2883 }
2884
2885 case Primitive::kPrimLong: {
2886 if (is_volatile) {
2887 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2888 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
2889 __ movd(temp1, value.AsRegisterPairLow<Register>());
2890 __ movd(temp2, value.AsRegisterPairHigh<Register>());
2891 __ punpckldq(temp1, temp2);
2892 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00002893 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002894 } else {
2895 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00002896 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00002897 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
2898 }
2899 break;
2900 }
2901
2902 case Primitive::kPrimFloat: {
2903 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
2904 break;
2905 }
2906
2907 case Primitive::kPrimDouble: {
2908 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
2909 break;
2910 }
2911
2912 case Primitive::kPrimVoid:
2913 LOG(FATAL) << "Unreachable type " << field_type;
2914 UNREACHABLE();
2915 }
2916
Calin Juravle77520bc2015-01-12 18:45:46 +00002917 // Longs are handled in the switch.
2918 if (field_type != Primitive::kPrimLong) {
2919 codegen_->MaybeRecordImplicitNullCheck(instruction);
2920 }
2921
2922 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2923 Register temp = locations->GetTemp(0).AsRegister<Register>();
2924 Register card = locations->GetTemp(1).AsRegister<Register>();
2925 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
2926 }
2927
Calin Juravle52c48962014-12-16 17:02:57 +00002928 if (is_volatile) {
2929 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2930 }
2931}
2932
2933void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2934 HandleFieldGet(instruction, instruction->GetFieldInfo());
2935}
2936
2937void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2938 HandleFieldGet(instruction, instruction->GetFieldInfo());
2939}
2940
2941void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2942 HandleFieldSet(instruction, instruction->GetFieldInfo());
2943}
2944
2945void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2946 HandleFieldSet(instruction, instruction->GetFieldInfo());
2947}
2948
2949void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2950 HandleFieldSet(instruction, instruction->GetFieldInfo());
2951}
2952
2953void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2954 HandleFieldSet(instruction, instruction->GetFieldInfo());
2955}
2956
2957void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2958 HandleFieldGet(instruction, instruction->GetFieldInfo());
2959}
2960
2961void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2962 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002963}
2964
2965void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002966 LocationSummary* locations =
2967 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002968 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
2969 ? Location::RequiresRegister()
2970 : Location::Any();
2971 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002972 if (instruction->HasUses()) {
2973 locations->SetOut(Location::SameAsFirstInput());
2974 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002975}
2976
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002977void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00002978 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2979 return;
2980 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002981 LocationSummary* locations = instruction->GetLocations();
2982 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00002983
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002984 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
2985 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2986}
2987
2988void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002989 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002990 codegen_->AddSlowPath(slow_path);
2991
2992 LocationSummary* locations = instruction->GetLocations();
2993 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002994
2995 if (obj.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002996 __ cmpl(obj.AsRegister<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002997 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002998 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002999 } else {
3000 DCHECK(obj.IsConstant()) << obj;
3001 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
3002 __ jmp(slow_path->GetEntryLabel());
3003 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003004 }
3005 __ j(kEqual, slow_path->GetEntryLabel());
3006}
3007
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003008void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
3009 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
3010 GenerateImplicitNullCheck(instruction);
3011 } else {
3012 GenerateExplicitNullCheck(instruction);
3013 }
3014}
3015
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003016void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003017 LocationSummary* locations =
3018 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003019 locations->SetInAt(0, Location::RequiresRegister());
3020 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3021 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003022}
3023
3024void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
3025 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003026 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003027 Location index = locations->InAt(1);
3028
Calin Juravle77520bc2015-01-12 18:45:46 +00003029 Primitive::Type type = instruction->GetType();
3030 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003031 case Primitive::kPrimBoolean: {
3032 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003033 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003034 if (index.IsConstant()) {
3035 __ movzxb(out, Address(obj,
3036 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3037 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003038 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003039 }
3040 break;
3041 }
3042
3043 case Primitive::kPrimByte: {
3044 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003045 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003046 if (index.IsConstant()) {
3047 __ movsxb(out, Address(obj,
3048 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3049 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003050 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003051 }
3052 break;
3053 }
3054
3055 case Primitive::kPrimShort: {
3056 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003057 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003058 if (index.IsConstant()) {
3059 __ movsxw(out, Address(obj,
3060 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3061 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003062 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003063 }
3064 break;
3065 }
3066
3067 case Primitive::kPrimChar: {
3068 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003069 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003070 if (index.IsConstant()) {
3071 __ movzxw(out, Address(obj,
3072 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3073 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003074 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003075 }
3076 break;
3077 }
3078
3079 case Primitive::kPrimInt:
3080 case Primitive::kPrimNot: {
3081 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003082 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003083 if (index.IsConstant()) {
3084 __ movl(out, Address(obj,
3085 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3086 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003087 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003088 }
3089 break;
3090 }
3091
3092 case Primitive::kPrimLong: {
3093 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003094 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003095 if (index.IsConstant()) {
3096 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003097 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003098 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003099 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003100 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003101 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003102 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003103 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003104 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003105 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003106 }
3107 break;
3108 }
3109
3110 case Primitive::kPrimFloat:
3111 case Primitive::kPrimDouble:
Calin Juravle77520bc2015-01-12 18:45:46 +00003112 LOG(FATAL) << "Unimplemented register type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003113 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003114 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00003115 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003116 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003117 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003118
3119 if (type != Primitive::kPrimLong) {
3120 codegen_->MaybeRecordImplicitNullCheck(instruction);
3121 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003122}
3123
3124void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003125 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003126 bool needs_write_barrier =
3127 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3128
3129 DCHECK(kFollowsQuickABI);
3130 bool not_enough_registers = needs_write_barrier
3131 && !instruction->GetValue()->IsConstant()
3132 && !instruction->GetIndex()->IsConstant();
3133 bool needs_runtime_call = instruction->NeedsTypeCheck() || not_enough_registers;
3134
Nicolas Geoffray39468442014-09-02 15:17:15 +01003135 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3136 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003137 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003138
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003139 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003140 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003141 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3142 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3143 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003144 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003145 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3146 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003147 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003148 // In case of a byte operation, the register allocator does not support multiple
3149 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003150 locations->SetInAt(0, Location::RequiresRegister());
3151 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003152 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003153 // Ensure the value is in a byte register.
3154 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003155 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003156 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003157 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003158 // Temporary registers for the write barrier.
3159 if (needs_write_barrier) {
3160 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003161 // Ensure the card is in a byte register.
3162 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003163 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003164 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003165}
3166
3167void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3168 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003169 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003170 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003171 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003172 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003173 bool needs_runtime_call = locations->WillCall();
3174 bool needs_write_barrier =
3175 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003176
3177 switch (value_type) {
3178 case Primitive::kPrimBoolean:
3179 case Primitive::kPrimByte: {
3180 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003181 if (index.IsConstant()) {
3182 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003183 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003184 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003185 } else {
3186 __ movb(Address(obj, offset),
3187 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3188 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003189 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003190 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003191 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003192 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003193 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003194 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003195 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3196 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003197 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003198 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003199 break;
3200 }
3201
3202 case Primitive::kPrimShort:
3203 case Primitive::kPrimChar: {
3204 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003205 if (index.IsConstant()) {
3206 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003207 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003208 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003209 } else {
3210 __ movw(Address(obj, offset),
3211 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3212 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003213 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003214 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003215 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3216 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003217 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003218 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003219 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3220 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003221 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003222 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003223 break;
3224 }
3225
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003226 case Primitive::kPrimInt:
3227 case Primitive::kPrimNot: {
3228 if (!needs_runtime_call) {
3229 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3230 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003231 size_t offset =
3232 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003233 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003234 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003235 } else {
3236 DCHECK(value.IsConstant()) << value;
3237 __ movl(Address(obj, offset),
3238 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3239 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003240 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003241 DCHECK(index.IsRegister()) << index;
3242 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003243 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3244 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003245 } else {
3246 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003247 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003248 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3249 }
3250 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003251 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003252
3253 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003254 Register temp = locations->GetTemp(0).AsRegister<Register>();
3255 Register card = locations->GetTemp(1).AsRegister<Register>();
3256 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003257 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003258 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003259 DCHECK_EQ(value_type, Primitive::kPrimNot);
3260 DCHECK(!codegen_->IsLeafMethod());
3261 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3262 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003263 }
3264 break;
3265 }
3266
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003267 case Primitive::kPrimLong: {
3268 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003269 if (index.IsConstant()) {
3270 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003271 if (value.IsRegisterPair()) {
3272 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003273 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003274 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003275 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003276 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003277 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3278 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003279 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003280 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3281 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003282 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003283 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003284 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003285 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00003286 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003287 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003288 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003289 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003290 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003291 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003292 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003293 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00003294 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003295 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003296 Immediate(High32Bits(val)));
3297 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003298 }
3299 break;
3300 }
3301
3302 case Primitive::kPrimFloat:
3303 case Primitive::kPrimDouble:
3304 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003305 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003306 case Primitive::kPrimVoid:
3307 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003308 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003309 }
3310}
3311
3312void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3313 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003314 locations->SetInAt(0, Location::RequiresRegister());
3315 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003316 instruction->SetLocations(locations);
3317}
3318
3319void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3320 LocationSummary* locations = instruction->GetLocations();
3321 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003322 Register obj = locations->InAt(0).AsRegister<Register>();
3323 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003324 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003325 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003326}
3327
3328void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003329 LocationSummary* locations =
3330 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003331 locations->SetInAt(0, Location::RequiresRegister());
3332 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003333 if (instruction->HasUses()) {
3334 locations->SetOut(Location::SameAsFirstInput());
3335 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003336}
3337
3338void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3339 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003340 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01003341 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003342 codegen_->AddSlowPath(slow_path);
3343
Roland Levillain271ab9c2014-11-27 15:23:57 +00003344 Register index = locations->InAt(0).AsRegister<Register>();
3345 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003346
3347 __ cmpl(index, length);
3348 __ j(kAboveEqual, slow_path->GetEntryLabel());
3349}
3350
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003351void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3352 temp->SetLocations(nullptr);
3353}
3354
3355void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3356 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003357 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003358}
3359
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003360void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003361 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003362 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003363}
3364
3365void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003366 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3367}
3368
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003369void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3370 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3371}
3372
3373void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003374 HBasicBlock* block = instruction->GetBlock();
3375 if (block->GetLoopInformation() != nullptr) {
3376 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3377 // The back edge will generate the suspend check.
3378 return;
3379 }
3380 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3381 // The goto will generate the suspend check.
3382 return;
3383 }
3384 GenerateSuspendCheck(instruction, nullptr);
3385}
3386
3387void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3388 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003389 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003390 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003391 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003392 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003393 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003394 if (successor == nullptr) {
3395 __ j(kNotEqual, slow_path->GetEntryLabel());
3396 __ Bind(slow_path->GetReturnLabel());
3397 } else {
3398 __ j(kEqual, codegen_->GetLabelOf(successor));
3399 __ jmp(slow_path->GetEntryLabel());
3400 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003401}
3402
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003403X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3404 return codegen_->GetAssembler();
3405}
3406
3407void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
3408 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003409 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003410 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3411 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
3412 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
3413}
3414
3415void ParallelMoveResolverX86::EmitMove(size_t index) {
3416 MoveOperands* move = moves_.Get(index);
3417 Location source = move->GetSource();
3418 Location destination = move->GetDestination();
3419
3420 if (source.IsRegister()) {
3421 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003422 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003423 } else {
3424 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003425 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003426 }
3427 } else if (source.IsStackSlot()) {
3428 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003429 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003430 } else {
3431 DCHECK(destination.IsStackSlot());
3432 MoveMemoryToMemory(destination.GetStackIndex(),
3433 source.GetStackIndex());
3434 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003435 } else if (source.IsConstant()) {
3436 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
3437 Immediate imm(instruction->AsIntConstant()->GetValue());
3438 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003439 __ movl(destination.AsRegister<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003440 } else {
3441 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3442 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003443 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003444 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003445 }
3446}
3447
3448void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003449 Register suggested_scratch = reg == EAX ? EBX : EAX;
3450 ScratchRegisterScope ensure_scratch(
3451 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3452
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003453 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3454 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
3455 __ movl(Address(ESP, mem + stack_offset), reg);
3456 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
3457}
3458
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003459void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
3460 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003461 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3462
3463 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003464 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003465 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3466
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003467 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
3468 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
3469 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
3470 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
3471 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
3472 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
3473}
3474
3475void ParallelMoveResolverX86::EmitSwap(size_t index) {
3476 MoveOperands* move = moves_.Get(index);
3477 Location source = move->GetSource();
3478 Location destination = move->GetDestination();
3479
3480 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003481 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003482 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003483 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003484 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003485 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003486 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3487 Exchange(destination.GetStackIndex(), source.GetStackIndex());
3488 } else {
3489 LOG(FATAL) << "Unimplemented";
3490 }
3491}
3492
3493void ParallelMoveResolverX86::SpillScratch(int reg) {
3494 __ pushl(static_cast<Register>(reg));
3495}
3496
3497void ParallelMoveResolverX86::RestoreScratch(int reg) {
3498 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003499}
3500
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003501void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003502 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3503 ? LocationSummary::kCallOnSlowPath
3504 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003505 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003506 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003507 locations->SetOut(Location::RequiresRegister());
3508}
3509
3510void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003511 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003512 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003513 DCHECK(!cls->CanCallRuntime());
3514 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003515 codegen_->LoadCurrentMethod(out);
3516 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3517 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003518 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003519 codegen_->LoadCurrentMethod(out);
3520 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3521 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003522
3523 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3524 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3525 codegen_->AddSlowPath(slow_path);
3526 __ testl(out, out);
3527 __ j(kEqual, slow_path->GetEntryLabel());
3528 if (cls->MustGenerateClinitCheck()) {
3529 GenerateClassInitializationCheck(slow_path, out);
3530 } else {
3531 __ Bind(slow_path->GetExitLabel());
3532 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003533 }
3534}
3535
3536void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
3537 LocationSummary* locations =
3538 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3539 locations->SetInAt(0, Location::RequiresRegister());
3540 if (check->HasUses()) {
3541 locations->SetOut(Location::SameAsFirstInput());
3542 }
3543}
3544
3545void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003546 // We assume the class to not be null.
3547 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3548 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003549 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003550 GenerateClassInitializationCheck(slow_path,
3551 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003552}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003553
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003554void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
3555 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003556 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3557 Immediate(mirror::Class::kStatusInitialized));
3558 __ j(kLess, slow_path->GetEntryLabel());
3559 __ Bind(slow_path->GetExitLabel());
3560 // No need for memory fence, thanks to the X86 memory model.
3561}
3562
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003563void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
3564 LocationSummary* locations =
3565 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3566 locations->SetOut(Location::RequiresRegister());
3567}
3568
3569void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
3570 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
3571 codegen_->AddSlowPath(slow_path);
3572
Roland Levillain271ab9c2014-11-27 15:23:57 +00003573 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003574 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08003575 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3576 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003577 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3578 __ testl(out, out);
3579 __ j(kEqual, slow_path->GetEntryLabel());
3580 __ Bind(slow_path->GetExitLabel());
3581}
3582
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003583void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
3584 LocationSummary* locations =
3585 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3586 locations->SetOut(Location::RequiresRegister());
3587}
3588
3589void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
3590 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003591 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003592 __ fs()->movl(address, Immediate(0));
3593}
3594
3595void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
3596 LocationSummary* locations =
3597 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3598 InvokeRuntimeCallingConvention calling_convention;
3599 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3600}
3601
3602void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
3603 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
3604 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3605}
3606
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003607void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003608 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3609 ? LocationSummary::kNoCall
3610 : LocationSummary::kCallOnSlowPath;
3611 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3612 locations->SetInAt(0, Location::RequiresRegister());
3613 locations->SetInAt(1, Location::Any());
3614 locations->SetOut(Location::RequiresRegister());
3615}
3616
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003617void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003618 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003619 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003620 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003621 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003622 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3623 Label done, zero;
3624 SlowPathCodeX86* slow_path = nullptr;
3625
3626 // Return 0 if `obj` is null.
3627 // TODO: avoid this check if we know obj is not null.
3628 __ testl(obj, obj);
3629 __ j(kEqual, &zero);
3630 __ movl(out, Address(obj, class_offset));
3631 // Compare the class of `obj` with `cls`.
3632 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003633 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003634 } else {
3635 DCHECK(cls.IsStackSlot()) << cls;
3636 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
3637 }
3638
3639 if (instruction->IsClassFinal()) {
3640 // Classes must be equal for the instanceof to succeed.
3641 __ j(kNotEqual, &zero);
3642 __ movl(out, Immediate(1));
3643 __ jmp(&done);
3644 } else {
3645 // If the classes are not equal, we go into a slow path.
3646 DCHECK(locations->OnlyCallsOnSlowPath());
3647 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003648 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003649 codegen_->AddSlowPath(slow_path);
3650 __ j(kNotEqual, slow_path->GetEntryLabel());
3651 __ movl(out, Immediate(1));
3652 __ jmp(&done);
3653 }
3654 __ Bind(&zero);
3655 __ movl(out, Immediate(0));
3656 if (slow_path != nullptr) {
3657 __ Bind(slow_path->GetExitLabel());
3658 }
3659 __ Bind(&done);
3660}
3661
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003662void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
3663 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3664 instruction, LocationSummary::kCallOnSlowPath);
3665 locations->SetInAt(0, Location::RequiresRegister());
3666 locations->SetInAt(1, Location::Any());
3667 locations->AddTemp(Location::RequiresRegister());
3668}
3669
3670void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
3671 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003672 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003673 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003674 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003675 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3676 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
3677 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3678 codegen_->AddSlowPath(slow_path);
3679
3680 // TODO: avoid this check if we know obj is not null.
3681 __ testl(obj, obj);
3682 __ j(kEqual, slow_path->GetExitLabel());
3683 __ movl(temp, Address(obj, class_offset));
3684
3685 // Compare the class of `obj` with `cls`.
3686 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003687 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003688 } else {
3689 DCHECK(cls.IsStackSlot()) << cls;
3690 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
3691 }
3692
3693 __ j(kNotEqual, slow_path->GetEntryLabel());
3694 __ Bind(slow_path->GetExitLabel());
3695}
3696
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003697void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3698 LocationSummary* locations =
3699 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3700 InvokeRuntimeCallingConvention calling_convention;
3701 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3702}
3703
3704void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3705 __ fs()->call(Address::Absolute(instruction->IsEnter()
3706 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
3707 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
3708 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3709}
3710
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003711void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3712void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3713void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3714
3715void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3716 LocationSummary* locations =
3717 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3718 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3719 || instruction->GetResultType() == Primitive::kPrimLong);
3720 locations->SetInAt(0, Location::RequiresRegister());
3721 locations->SetInAt(1, Location::Any());
3722 locations->SetOut(Location::SameAsFirstInput());
3723}
3724
3725void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
3726 HandleBitwiseOperation(instruction);
3727}
3728
3729void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
3730 HandleBitwiseOperation(instruction);
3731}
3732
3733void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
3734 HandleBitwiseOperation(instruction);
3735}
3736
3737void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3738 LocationSummary* locations = instruction->GetLocations();
3739 Location first = locations->InAt(0);
3740 Location second = locations->InAt(1);
3741 DCHECK(first.Equals(locations->Out()));
3742
3743 if (instruction->GetResultType() == Primitive::kPrimInt) {
3744 if (second.IsRegister()) {
3745 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003746 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003747 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003748 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003749 } else {
3750 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003751 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003752 }
3753 } else if (second.IsConstant()) {
3754 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003755 __ andl(first.AsRegister<Register>(),
3756 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003757 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003758 __ orl(first.AsRegister<Register>(),
3759 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003760 } else {
3761 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00003762 __ xorl(first.AsRegister<Register>(),
3763 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003764 }
3765 } else {
3766 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003767 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003768 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003769 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003770 } else {
3771 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003772 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003773 }
3774 }
3775 } else {
3776 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3777 if (second.IsRegisterPair()) {
3778 if (instruction->IsAnd()) {
3779 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3780 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3781 } else if (instruction->IsOr()) {
3782 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3783 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3784 } else {
3785 DCHECK(instruction->IsXor());
3786 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3787 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3788 }
3789 } else {
3790 if (instruction->IsAnd()) {
3791 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3792 __ andl(first.AsRegisterPairHigh<Register>(),
3793 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3794 } else if (instruction->IsOr()) {
3795 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3796 __ orl(first.AsRegisterPairHigh<Register>(),
3797 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3798 } else {
3799 DCHECK(instruction->IsXor());
3800 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3801 __ xorl(first.AsRegisterPairHigh<Register>(),
3802 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3803 }
3804 }
3805 }
3806}
3807
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003808} // namespace x86
3809} // namespace art