blob: f20ca016878fc74e41865d213168b19d40f07e45 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
19#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010020#include "gc/accounting/card_table.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070021#include "mirror/array-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010022#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010023#include "mirror/class.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010024#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000025#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010026#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000027#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010028#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010031
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032namespace x86 {
33
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010034static constexpr bool kExplicitStackOverflowCheck = false;
35
36static constexpr int kNumberOfPushedRegistersAtEntry = 1;
37static constexpr int kCurrentMethodStackOffset = 0;
38
Calin Juravled6fb6cf2014-11-11 19:07:44 +000039static constexpr Register kRuntimeParameterCoreRegisters[] = { EAX, ECX, EDX, EBX };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010040static constexpr size_t kRuntimeParameterCoreRegistersLength =
41 arraysize(kRuntimeParameterCoreRegisters);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010042static constexpr XmmRegister kRuntimeParameterFpuRegisters[] = { };
43static constexpr size_t kRuntimeParameterFpuRegistersLength = 0;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010044
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010045class InvokeRuntimeCallingConvention : public CallingConvention<Register, XmmRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010046 public:
47 InvokeRuntimeCallingConvention()
48 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010049 kRuntimeParameterCoreRegistersLength,
50 kRuntimeParameterFpuRegisters,
51 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010052
53 private:
54 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
55};
56
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
58
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010059class SlowPathCodeX86 : public SlowPathCode {
60 public:
61 SlowPathCodeX86() : entry_label_(), exit_label_() {}
62
63 Label* GetEntryLabel() { return &entry_label_; }
64 Label* GetExitLabel() { return &exit_label_; }
65
66 private:
67 Label entry_label_;
68 Label exit_label_;
69
70 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86);
71};
72
73class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010074 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010075 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076
77 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
78 __ Bind(GetEntryLabel());
79 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010080 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010081 }
82
83 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010084 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010085 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
86};
87
Calin Juravled0d48522014-11-04 16:40:20 +000088class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
89 public:
90 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
91
92 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
93 __ Bind(GetEntryLabel());
94 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
95 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
96 }
97
98 private:
99 HDivZeroCheck* const instruction_;
100 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
101};
102
Calin Juravlebacfec32014-11-14 15:54:36 +0000103class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +0000104 public:
Calin Juravlebacfec32014-11-14 15:54:36 +0000105 explicit DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000106
107 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
108 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000109 if (is_div_) {
110 __ negl(reg_);
111 } else {
112 __ movl(reg_, Immediate(0));
113 }
Calin Juravled0d48522014-11-04 16:40:20 +0000114 __ jmp(GetExitLabel());
115 }
116
117 private:
118 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000119 bool is_div_;
120 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000121};
122
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100123class StackOverflowCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100124 public:
125 StackOverflowCheckSlowPathX86() {}
126
127 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
128 __ Bind(GetEntryLabel());
129 __ addl(ESP,
130 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
131 __ fs()->jmp(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowStackOverflow)));
132 }
133
134 private:
135 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86);
136};
137
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100138class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100139 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100140 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
141 Location index_location,
142 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100143 : instruction_(instruction), index_location_(index_location), length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100144
145 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100146 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100147 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000148 // We're moving two locations to locations that could overlap, so we need a parallel
149 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100150 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000151 x86_codegen->EmitParallelMoves(
152 index_location_,
153 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
154 length_location_,
155 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100156 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100157 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100158 }
159
160 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100161 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100162 const Location index_location_;
163 const Location length_location_;
164
165 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
166};
167
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100168class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000169 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100170 explicit SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
171 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000172
173 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100174 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000175 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100176 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000177 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
178 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100179 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100180 if (successor_ == nullptr) {
181 __ jmp(GetReturnLabel());
182 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100183 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100184 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000185 }
186
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100187 Label* GetReturnLabel() {
188 DCHECK(successor_ == nullptr);
189 return &return_label_;
190 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000191
192 private:
193 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100194 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000195 Label return_label_;
196
197 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
198};
199
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000200class LoadStringSlowPathX86 : public SlowPathCodeX86 {
201 public:
202 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
203
204 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
205 LocationSummary* locations = instruction_->GetLocations();
206 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
207
208 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
209 __ Bind(GetEntryLabel());
210 codegen->SaveLiveRegisters(locations);
211
212 InvokeRuntimeCallingConvention calling_convention;
213 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(0));
214 __ movl(calling_convention.GetRegisterAt(1), Immediate(instruction_->GetStringIndex()));
215 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
216 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
217 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
218 codegen->RestoreLiveRegisters(locations);
219
220 __ jmp(GetExitLabel());
221 }
222
223 private:
224 HLoadString* const instruction_;
225
226 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
227};
228
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000229class LoadClassSlowPathX86 : public SlowPathCodeX86 {
230 public:
231 LoadClassSlowPathX86(HLoadClass* cls,
232 HInstruction* at,
233 uint32_t dex_pc,
234 bool do_clinit)
235 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
236 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
237 }
238
239 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
240 LocationSummary* locations = at_->GetLocations();
241 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
242 __ Bind(GetEntryLabel());
243 codegen->SaveLiveRegisters(locations);
244
245 InvokeRuntimeCallingConvention calling_convention;
246 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
247 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
248 __ fs()->call(Address::Absolute(do_clinit_
249 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
250 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
251 codegen->RecordPcInfo(at_, dex_pc_);
252
253 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000254 Location out = locations->Out();
255 if (out.IsValid()) {
256 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
257 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000258 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000259
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000260 codegen->RestoreLiveRegisters(locations);
261 __ jmp(GetExitLabel());
262 }
263
264 private:
265 // The class this slow path will load.
266 HLoadClass* const cls_;
267
268 // The instruction where this slow path is happening.
269 // (Might be the load class or an initialization check).
270 HInstruction* const at_;
271
272 // The dex PC of `at_`.
273 const uint32_t dex_pc_;
274
275 // Whether to initialize the class.
276 const bool do_clinit_;
277
278 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
279};
280
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000281class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
282 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000283 TypeCheckSlowPathX86(HInstruction* instruction,
284 Location class_to_check,
285 Location object_class,
286 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000287 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000288 class_to_check_(class_to_check),
289 object_class_(object_class),
290 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000291
292 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
293 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000294 DCHECK(instruction_->IsCheckCast()
295 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000296
297 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
298 __ Bind(GetEntryLabel());
299 codegen->SaveLiveRegisters(locations);
300
301 // We're moving two locations to locations that could overlap, so we need a parallel
302 // move resolver.
303 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000304 x86_codegen->EmitParallelMoves(
305 class_to_check_,
306 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
307 object_class_,
308 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000309
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000310 if (instruction_->IsInstanceOf()) {
311 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInstanceofNonTrivial)));
312 } else {
313 DCHECK(instruction_->IsCheckCast());
314 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
315 }
316
317 codegen->RecordPcInfo(instruction_, dex_pc_);
318 if (instruction_->IsInstanceOf()) {
319 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
320 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000321 codegen->RestoreLiveRegisters(locations);
322
323 __ jmp(GetExitLabel());
324 }
325
326 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000327 HInstruction* const instruction_;
328 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000329 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000330 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000331
332 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
333};
334
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100335#undef __
336#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
337
Dave Allison20dfc792014-06-16 20:44:29 -0700338inline Condition X86Condition(IfCondition cond) {
339 switch (cond) {
340 case kCondEQ: return kEqual;
341 case kCondNE: return kNotEqual;
342 case kCondLT: return kLess;
343 case kCondLE: return kLessEqual;
344 case kCondGT: return kGreater;
345 case kCondGE: return kGreaterEqual;
346 default:
347 LOG(FATAL) << "Unknown if condition";
348 }
349 return kEqual;
350}
351
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100352void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
353 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
354}
355
356void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
357 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
358}
359
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100360size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
361 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
362 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100363}
364
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100365size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
366 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
367 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100368}
369
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100370CodeGeneratorX86::CodeGeneratorX86(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100371 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfXmmRegisters, kNumberOfRegisterPairs),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100372 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100373 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100374 instruction_visitor_(graph, this),
375 move_resolver_(graph->GetArena(), this) {}
376
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100377size_t CodeGeneratorX86::FrameEntrySpillSize() const {
378 return kNumberOfPushedRegistersAtEntry * kX86WordSize;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100379}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100380
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100381Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100382 switch (type) {
383 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100384 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100385 X86ManagedRegister pair =
386 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100387 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
388 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100389 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
390 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100391 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100392 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100393 }
394
395 case Primitive::kPrimByte:
396 case Primitive::kPrimBoolean:
397 case Primitive::kPrimChar:
398 case Primitive::kPrimShort:
399 case Primitive::kPrimInt:
400 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100401 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100402 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100403 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100404 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
405 X86ManagedRegister current =
406 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
407 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100408 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100409 }
410 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100411 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100412 }
413
414 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100415 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100416 return Location::FpuRegisterLocation(
417 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100418 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100419
420 case Primitive::kPrimVoid:
421 LOG(FATAL) << "Unreachable type " << type;
422 }
423
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100424 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100425}
426
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100427void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100428 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100429 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100430
431 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100432 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100433
434 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100435 blocked_core_registers_[EBP] = true;
436 blocked_core_registers_[ESI] = true;
437 blocked_core_registers_[EDI] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100438
439 UpdateBlockedPairRegisters();
440}
441
442void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
443 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
444 X86ManagedRegister current =
445 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
446 if (blocked_core_registers_[current.AsRegisterPairLow()]
447 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
448 blocked_register_pairs_[i] = true;
449 }
450 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100451}
452
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100453InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
454 : HGraphVisitor(graph),
455 assembler_(codegen->GetAssembler()),
456 codegen_(codegen) {}
457
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000458void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000459 // Create a fake register to mimic Quick.
460 static const int kFakeReturnRegister = 8;
461 core_spill_mask_ |= (1 << kFakeReturnRegister);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000462
Dave Allison648d7112014-07-25 16:15:27 -0700463 bool skip_overflow_check = IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100464 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
465 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100466 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100467 }
468
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100469 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100470 __ subl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100471
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100472 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100473 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86();
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100474 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100475
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100476 __ fs()->cmpl(ESP, Address::Absolute(Thread::StackEndOffset<kX86WordSize>()));
477 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100478 }
479
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100480 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000481}
482
483void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100484 __ addl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000485}
486
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100487void CodeGeneratorX86::Bind(HBasicBlock* block) {
488 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000489}
490
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100491void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100492 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000493}
494
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100495Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
496 switch (load->GetType()) {
497 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100498 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100499 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
500 break;
501
502 case Primitive::kPrimInt:
503 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100504 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100505 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100506
507 case Primitive::kPrimBoolean:
508 case Primitive::kPrimByte:
509 case Primitive::kPrimChar:
510 case Primitive::kPrimShort:
511 case Primitive::kPrimVoid:
512 LOG(FATAL) << "Unexpected type " << load->GetType();
513 }
514
515 LOG(FATAL) << "Unreachable";
516 return Location();
517}
518
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100519Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
520 switch (type) {
521 case Primitive::kPrimBoolean:
522 case Primitive::kPrimByte:
523 case Primitive::kPrimChar:
524 case Primitive::kPrimShort:
525 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100526 case Primitive::kPrimFloat:
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100527 case Primitive::kPrimNot: {
528 uint32_t index = gp_index_++;
529 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100530 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100531 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100532 return Location::StackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100533 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100534 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100535
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100536 case Primitive::kPrimLong:
537 case Primitive::kPrimDouble: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100538 uint32_t index = gp_index_;
539 gp_index_ += 2;
540 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100541 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
542 calling_convention.GetRegisterPairAt(index));
543 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100544 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000545 // On X86, the register index and stack index of a quick parameter is the same, since
546 // we are passing floating pointer values in core registers.
547 return Location::QuickParameter(index, index);
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100548 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100549 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100550 }
551 }
552
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100553 case Primitive::kPrimVoid:
554 LOG(FATAL) << "Unexpected parameter type " << type;
555 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100556 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100557 return Location();
558}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100559
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100560void CodeGeneratorX86::Move32(Location destination, Location source) {
561 if (source.Equals(destination)) {
562 return;
563 }
564 if (destination.IsRegister()) {
565 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100566 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100567 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100568 __ movd(destination.As<Register>(), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100569 } else {
570 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100571 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100572 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100573 } else if (destination.IsFpuRegister()) {
574 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100575 __ movd(destination.As<XmmRegister>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100576 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100577 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100578 } else {
579 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100580 __ movss(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100581 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100582 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000583 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100584 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100585 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100586 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100587 __ movss(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100588 } else {
589 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100590 __ pushl(Address(ESP, source.GetStackIndex()));
591 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100592 }
593 }
594}
595
596void CodeGeneratorX86::Move64(Location destination, Location source) {
597 if (source.Equals(destination)) {
598 return;
599 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100600 if (destination.IsRegisterPair()) {
601 if (source.IsRegisterPair()) {
602 __ movl(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
603 __ movl(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100604 } else if (source.IsFpuRegister()) {
605 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100606 } else if (source.IsQuickParameter()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000607 uint16_t register_index = source.GetQuickParameterRegisterIndex();
608 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100609 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100610 __ movl(destination.AsRegisterPairLow<Register>(),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000611 calling_convention.GetRegisterAt(register_index));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100612 __ movl(destination.AsRegisterPairHigh<Register>(), Address(ESP,
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000613 calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100614 } else {
615 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100616 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
617 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100618 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
619 }
620 } else if (destination.IsQuickParameter()) {
621 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000622 uint16_t register_index = destination.GetQuickParameterRegisterIndex();
623 uint16_t stack_index = destination.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100624 if (source.IsRegister()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000625 __ movl(calling_convention.GetRegisterAt(register_index), source.AsRegisterPairLow<Register>());
626 __ movl(Address(ESP, calling_convention.GetStackOffsetOf(stack_index + 1)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100627 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100628 } else if (source.IsFpuRegister()) {
629 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100630 } else {
631 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000632 __ movl(calling_convention.GetRegisterAt(register_index),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100633 Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100634 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000635 __ popl(Address(ESP, calling_convention.GetStackOffsetOf(stack_index + 1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100636 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100637 } else if (destination.IsFpuRegister()) {
638 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100639 __ movsd(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100640 } else {
641 LOG(FATAL) << "Unimplemented";
642 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100643 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000644 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100645 if (source.IsRegisterPair()) {
646 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100647 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100648 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100649 } else if (source.IsQuickParameter()) {
650 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000651 uint16_t register_index = source.GetQuickParameterRegisterIndex();
652 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100653 __ movl(Address(ESP, destination.GetStackIndex()),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000654 calling_convention.GetRegisterAt(register_index));
655 DCHECK_EQ(calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100656 static_cast<size_t>(destination.GetHighStackIndex(kX86WordSize)));
657 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100658 __ movsd(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100659 } else {
660 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100661 __ pushl(Address(ESP, source.GetStackIndex()));
662 __ popl(Address(ESP, destination.GetStackIndex()));
663 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
664 __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100665 }
666 }
667}
668
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100669void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000670 LocationSummary* locations = instruction->GetLocations();
671 if (locations != nullptr && locations->Out().Equals(location)) {
672 return;
673 }
674
675 if (locations != nullptr && locations->Out().IsConstant()) {
676 HConstant* const_to_move = locations->Out().GetConstant();
677 if (const_to_move->IsIntConstant()) {
678 Immediate imm(const_to_move->AsIntConstant()->GetValue());
679 if (location.IsRegister()) {
680 __ movl(location.As<Register>(), imm);
681 } else if (location.IsStackSlot()) {
682 __ movl(Address(ESP, location.GetStackIndex()), imm);
683 } else {
684 DCHECK(location.IsConstant());
685 DCHECK_EQ(location.GetConstant(), const_to_move);
686 }
687 } else if (const_to_move->IsLongConstant()) {
688 int64_t value = const_to_move->AsLongConstant()->GetValue();
689 if (location.IsRegisterPair()) {
690 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
691 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
692 } else if (location.IsDoubleStackSlot()) {
693 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
694 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
695 } else {
696 DCHECK(location.IsConstant());
697 DCHECK_EQ(location.GetConstant(), instruction);
698 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100699 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000700 } else if (instruction->IsTemporary()) {
701 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000702 if (temp_location.IsStackSlot()) {
703 Move32(location, temp_location);
704 } else {
705 DCHECK(temp_location.IsDoubleStackSlot());
706 Move64(location, temp_location);
707 }
Roland Levillain476df552014-10-09 17:51:36 +0100708 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100709 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100710 switch (instruction->GetType()) {
711 case Primitive::kPrimBoolean:
712 case Primitive::kPrimByte:
713 case Primitive::kPrimChar:
714 case Primitive::kPrimShort:
715 case Primitive::kPrimInt:
716 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100717 case Primitive::kPrimFloat:
718 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100719 break;
720
721 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100722 case Primitive::kPrimDouble:
723 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100724 break;
725
726 default:
727 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
728 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000729 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100730 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100731 switch (instruction->GetType()) {
732 case Primitive::kPrimBoolean:
733 case Primitive::kPrimByte:
734 case Primitive::kPrimChar:
735 case Primitive::kPrimShort:
736 case Primitive::kPrimInt:
737 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100738 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000739 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100740 break;
741
742 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100743 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000744 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100745 break;
746
747 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100748 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100749 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000750 }
751}
752
753void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000754 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000755}
756
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000757void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000758 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100759 DCHECK(!successor->IsExitBlock());
760
761 HBasicBlock* block = got->GetBlock();
762 HInstruction* previous = got->GetPrevious();
763
764 HLoopInformation* info = block->GetLoopInformation();
765 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
766 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
767 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
768 return;
769 }
770
771 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
772 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
773 }
774 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000775 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000776 }
777}
778
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000779void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000780 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000781}
782
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000783void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700784 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000785 if (kIsDebugBuild) {
786 __ Comment("Unreachable");
787 __ int3();
788 }
789}
790
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000791void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100792 LocationSummary* locations =
793 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100794 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100795 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100796 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100797 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000798}
799
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000800void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700801 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100802 if (cond->IsIntConstant()) {
803 // Constant condition, statically compared against 1.
804 int32_t cond_value = cond->AsIntConstant()->GetValue();
805 if (cond_value == 1) {
806 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
807 if_instr->IfTrueSuccessor())) {
808 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100809 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100810 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100811 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100812 DCHECK_EQ(cond_value, 0);
813 }
814 } else {
815 bool materialized =
816 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
817 // Moves do not affect the eflags register, so if the condition is
818 // evaluated just before the if, we don't need to evaluate it
819 // again.
820 bool eflags_set = cond->IsCondition()
821 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
822 if (materialized) {
823 if (!eflags_set) {
824 // Materialized condition, compare against 0.
825 Location lhs = if_instr->GetLocations()->InAt(0);
826 if (lhs.IsRegister()) {
827 __ cmpl(lhs.As<Register>(), Immediate(0));
828 } else {
829 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
830 }
831 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
832 } else {
833 __ j(X86Condition(cond->AsCondition()->GetCondition()),
834 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
835 }
836 } else {
837 Location lhs = cond->GetLocations()->InAt(0);
838 Location rhs = cond->GetLocations()->InAt(1);
839 // LHS is guaranteed to be in a register (see
840 // LocationsBuilderX86::VisitCondition).
841 if (rhs.IsRegister()) {
842 __ cmpl(lhs.As<Register>(), rhs.As<Register>());
843 } else if (rhs.IsConstant()) {
844 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
845 Immediate imm(instruction->AsIntConstant()->GetValue());
846 __ cmpl(lhs.As<Register>(), imm);
847 } else {
848 __ cmpl(lhs.As<Register>(), Address(ESP, rhs.GetStackIndex()));
849 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100850 __ j(X86Condition(cond->AsCondition()->GetCondition()),
851 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700852 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100853 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100854 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
855 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700856 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000857 }
858}
859
860void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000861 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000862}
863
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000864void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
865 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000866}
867
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000868void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100869 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000870}
871
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000872void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100873 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700874 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000875}
876
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100877void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100878 LocationSummary* locations =
879 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100880 switch (store->InputAt(1)->GetType()) {
881 case Primitive::kPrimBoolean:
882 case Primitive::kPrimByte:
883 case Primitive::kPrimChar:
884 case Primitive::kPrimShort:
885 case Primitive::kPrimInt:
886 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100887 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100888 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
889 break;
890
891 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100892 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100893 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
894 break;
895
896 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100897 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100898 }
899 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000900}
901
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000902void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700903 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000904}
905
Dave Allison20dfc792014-06-16 20:44:29 -0700906void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100907 LocationSummary* locations =
908 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100909 locations->SetInAt(0, Location::RequiresRegister());
910 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100911 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100912 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100913 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000914}
915
Dave Allison20dfc792014-06-16 20:44:29 -0700916void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
917 if (comp->NeedsMaterialization()) {
918 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100919 Register reg = locations->Out().As<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100920 // Clear register: setcc only sets the low byte.
921 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700922 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100923 __ cmpl(locations->InAt(0).As<Register>(),
924 locations->InAt(1).As<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100925 } else if (locations->InAt(1).IsConstant()) {
926 HConstant* instruction = locations->InAt(1).GetConstant();
927 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100928 __ cmpl(locations->InAt(0).As<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700929 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100930 __ cmpl(locations->InAt(0).As<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700931 Address(ESP, locations->InAt(1).GetStackIndex()));
932 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100933 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100934 }
Dave Allison20dfc792014-06-16 20:44:29 -0700935}
936
937void LocationsBuilderX86::VisitEqual(HEqual* comp) {
938 VisitCondition(comp);
939}
940
941void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
942 VisitCondition(comp);
943}
944
945void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
946 VisitCondition(comp);
947}
948
949void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
950 VisitCondition(comp);
951}
952
953void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
954 VisitCondition(comp);
955}
956
957void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
958 VisitCondition(comp);
959}
960
961void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
962 VisitCondition(comp);
963}
964
965void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
966 VisitCondition(comp);
967}
968
969void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
970 VisitCondition(comp);
971}
972
973void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
974 VisitCondition(comp);
975}
976
977void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
978 VisitCondition(comp);
979}
980
981void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
982 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000983}
984
985void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100986 LocationSummary* locations =
987 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100988 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000989}
990
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000991void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100992 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700993 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000994}
995
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100996void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100997 LocationSummary* locations =
998 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100999 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001000}
1001
1002void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1003 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001004 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001005}
1006
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001007void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1008 LocationSummary* locations =
1009 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1010 locations->SetOut(Location::ConstantLocation(constant));
1011}
1012
1013void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1014 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001015 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001016}
1017
1018void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1019 LocationSummary* locations =
1020 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1021 locations->SetOut(Location::ConstantLocation(constant));
1022}
1023
1024void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1025 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001026 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001027}
1028
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001029void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001030 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001031}
1032
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001033void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001034 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001035 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001036 __ ret();
1037}
1038
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001039void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001040 LocationSummary* locations =
1041 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001042 switch (ret->InputAt(0)->GetType()) {
1043 case Primitive::kPrimBoolean:
1044 case Primitive::kPrimByte:
1045 case Primitive::kPrimChar:
1046 case Primitive::kPrimShort:
1047 case Primitive::kPrimInt:
1048 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001049 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001050 break;
1051
1052 case Primitive::kPrimLong:
1053 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001054 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001055 break;
1056
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001057 case Primitive::kPrimFloat:
1058 case Primitive::kPrimDouble:
1059 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001060 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001061 break;
1062
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001063 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001064 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001065 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001066}
1067
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001068void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001069 if (kIsDebugBuild) {
1070 switch (ret->InputAt(0)->GetType()) {
1071 case Primitive::kPrimBoolean:
1072 case Primitive::kPrimByte:
1073 case Primitive::kPrimChar:
1074 case Primitive::kPrimShort:
1075 case Primitive::kPrimInt:
1076 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001077 DCHECK_EQ(ret->GetLocations()->InAt(0).As<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001078 break;
1079
1080 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001081 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1082 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001083 break;
1084
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001085 case Primitive::kPrimFloat:
1086 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001087 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001088 break;
1089
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001090 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001091 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001092 }
1093 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001094 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001095 __ ret();
1096}
1097
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001098void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001099 HandleInvoke(invoke);
1100}
1101
1102void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001103 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001104
1105 // TODO: Implement all kinds of calls:
1106 // 1) boot -> boot
1107 // 2) app -> boot
1108 // 3) app -> app
1109 //
1110 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1111
1112 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001113 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001114 // temp = temp->dex_cache_resolved_methods_;
1115 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
1116 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001117 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001118 // (temp + offset_of_quick_compiled_code)()
1119 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1120
1121 DCHECK(!codegen_->IsLeafMethod());
1122 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1123}
1124
1125void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1126 HandleInvoke(invoke);
1127}
1128
1129void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001130 LocationSummary* locations =
1131 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001132 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001133
1134 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001135 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001136 HInstruction* input = invoke->InputAt(i);
1137 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1138 }
1139
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001140 switch (invoke->GetType()) {
1141 case Primitive::kPrimBoolean:
1142 case Primitive::kPrimByte:
1143 case Primitive::kPrimChar:
1144 case Primitive::kPrimShort:
1145 case Primitive::kPrimInt:
1146 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001147 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001148 break;
1149
1150 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001151 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001152 break;
1153
1154 case Primitive::kPrimVoid:
1155 break;
1156
1157 case Primitive::kPrimDouble:
1158 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001159 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001160 break;
1161 }
1162
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001163 invoke->SetLocations(locations);
1164}
1165
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001166void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001167 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001168 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1169 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1170 LocationSummary* locations = invoke->GetLocations();
1171 Location receiver = locations->InAt(0);
1172 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1173 // temp = object->GetClass();
1174 if (receiver.IsStackSlot()) {
1175 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1176 __ movl(temp, Address(temp, class_offset));
1177 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001178 __ movl(temp, Address(receiver.As<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001179 }
1180 // temp = temp->GetMethodAt(method_offset);
1181 __ movl(temp, Address(temp, method_offset));
1182 // call temp->GetEntryPoint();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001183 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1184
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001185 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001186 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001187}
1188
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001189void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1190 HandleInvoke(invoke);
1191 // Add the hidden argument.
1192 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM0));
1193}
1194
1195void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1196 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1197 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
1198 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1199 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1200 LocationSummary* locations = invoke->GetLocations();
1201 Location receiver = locations->InAt(0);
1202 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1203
1204 // Set the hidden argument.
1205 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
1206 __ movd(invoke->GetLocations()->GetTemp(1).As<XmmRegister>(), temp);
1207
1208 // temp = object->GetClass();
1209 if (receiver.IsStackSlot()) {
1210 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1211 __ movl(temp, Address(temp, class_offset));
1212 } else {
1213 __ movl(temp, Address(receiver.As<Register>(), class_offset));
1214 }
1215 // temp = temp->GetImtEntryAt(method_offset);
1216 __ movl(temp, Address(temp, method_offset));
1217 // call temp->GetEntryPoint();
1218 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1219
1220 DCHECK(!codegen_->IsLeafMethod());
1221 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1222}
1223
Roland Levillain88cb1752014-10-20 16:36:47 +01001224void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1225 LocationSummary* locations =
1226 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1227 switch (neg->GetResultType()) {
1228 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001229 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001230 locations->SetInAt(0, Location::RequiresRegister());
1231 locations->SetOut(Location::SameAsFirstInput());
1232 break;
1233
Roland Levillain88cb1752014-10-20 16:36:47 +01001234 case Primitive::kPrimFloat:
1235 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001236 locations->SetInAt(0, Location::RequiresFpuRegister());
1237 // Output overlaps as we need a fresh (zero-initialized)
1238 // register to perform subtraction from zero.
1239 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001240 break;
1241
1242 default:
1243 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1244 }
1245}
1246
1247void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1248 LocationSummary* locations = neg->GetLocations();
1249 Location out = locations->Out();
1250 Location in = locations->InAt(0);
1251 switch (neg->GetResultType()) {
1252 case Primitive::kPrimInt:
1253 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001254 DCHECK(in.Equals(out));
Roland Levillain88cb1752014-10-20 16:36:47 +01001255 __ negl(out.As<Register>());
1256 break;
1257
1258 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001259 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001260 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001261 __ negl(out.AsRegisterPairLow<Register>());
1262 // Negation is similar to subtraction from zero. The least
1263 // significant byte triggers a borrow when it is different from
1264 // zero; to take it into account, add 1 to the most significant
1265 // byte if the carry flag (CF) is set to 1 after the first NEGL
1266 // operation.
1267 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1268 __ negl(out.AsRegisterPairHigh<Register>());
1269 break;
1270
Roland Levillain88cb1752014-10-20 16:36:47 +01001271 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001272 DCHECK(!in.Equals(out));
1273 // out = 0
1274 __ xorps(out.As<XmmRegister>(), out.As<XmmRegister>());
1275 // out = out - in
1276 __ subss(out.As<XmmRegister>(), in.As<XmmRegister>());
1277 break;
1278
Roland Levillain88cb1752014-10-20 16:36:47 +01001279 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001280 DCHECK(!in.Equals(out));
1281 // out = 0
1282 __ xorpd(out.As<XmmRegister>(), out.As<XmmRegister>());
1283 // out = out - in
1284 __ subsd(out.As<XmmRegister>(), in.As<XmmRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001285 break;
1286
1287 default:
1288 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1289 }
1290}
1291
Roland Levillaindff1f282014-11-05 14:15:05 +00001292void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
1293 LocationSummary* locations =
1294 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1295 Primitive::Type result_type = conversion->GetResultType();
1296 Primitive::Type input_type = conversion->GetInputType();
1297 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001298 case Primitive::kPrimByte:
1299 switch (input_type) {
1300 case Primitive::kPrimShort:
1301 case Primitive::kPrimInt:
1302 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001303 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001304 locations->SetInAt(0, Location::Any());
1305 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1306 break;
1307
1308 default:
1309 LOG(FATAL) << "Unexpected type conversion from " << input_type
1310 << " to " << result_type;
1311 }
1312 break;
1313
Roland Levillain946e1432014-11-11 17:35:19 +00001314 case Primitive::kPrimInt:
1315 switch (input_type) {
1316 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001317 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001318 locations->SetInAt(0, Location::Any());
1319 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1320 break;
1321
1322 case Primitive::kPrimFloat:
1323 case Primitive::kPrimDouble:
1324 LOG(FATAL) << "Type conversion from " << input_type
1325 << " to " << result_type << " not yet implemented";
1326 break;
1327
1328 default:
1329 LOG(FATAL) << "Unexpected type conversion from " << input_type
1330 << " to " << result_type;
1331 }
1332 break;
1333
Roland Levillaindff1f282014-11-05 14:15:05 +00001334 case Primitive::kPrimLong:
1335 switch (input_type) {
1336 case Primitive::kPrimByte:
1337 case Primitive::kPrimShort:
1338 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001339 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001340 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001341 locations->SetInAt(0, Location::RegisterLocation(EAX));
1342 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1343 break;
1344
1345 case Primitive::kPrimFloat:
1346 case Primitive::kPrimDouble:
1347 LOG(FATAL) << "Type conversion from " << input_type << " to "
1348 << result_type << " not yet implemented";
1349 break;
1350
1351 default:
1352 LOG(FATAL) << "Unexpected type conversion from " << input_type
1353 << " to " << result_type;
1354 }
1355 break;
1356
Roland Levillain981e4542014-11-14 11:47:14 +00001357 case Primitive::kPrimChar:
1358 switch (input_type) {
1359 case Primitive::kPrimByte:
1360 case Primitive::kPrimShort:
1361 case Primitive::kPrimInt:
1362 case Primitive::kPrimChar:
1363 // Processing a Dex `int-to-char' instruction.
1364 locations->SetInAt(0, Location::Any());
1365 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1366 break;
1367
1368 default:
1369 LOG(FATAL) << "Unexpected type conversion from " << input_type
1370 << " to " << result_type;
1371 }
1372 break;
1373
Roland Levillaindff1f282014-11-05 14:15:05 +00001374 case Primitive::kPrimFloat:
1375 case Primitive::kPrimDouble:
1376 LOG(FATAL) << "Type conversion from " << input_type
1377 << " to " << result_type << " not yet implemented";
1378 break;
1379
1380 default:
1381 LOG(FATAL) << "Unexpected type conversion from " << input_type
1382 << " to " << result_type;
1383 }
1384}
1385
1386void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1387 LocationSummary* locations = conversion->GetLocations();
1388 Location out = locations->Out();
1389 Location in = locations->InAt(0);
1390 Primitive::Type result_type = conversion->GetResultType();
1391 Primitive::Type input_type = conversion->GetInputType();
1392 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001393 case Primitive::kPrimByte:
1394 switch (input_type) {
1395 case Primitive::kPrimShort:
1396 case Primitive::kPrimInt:
1397 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001398 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001399 if (in.IsRegister()) {
1400 __ movsxb(out.As<Register>(), in.As<ByteRegister>());
1401 } else if (in.IsStackSlot()) {
1402 __ movsxb(out.As<Register>(), Address(ESP, in.GetStackIndex()));
1403 } else {
1404 DCHECK(in.GetConstant()->IsIntConstant());
1405 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1406 __ movl(out.As<Register>(), Immediate(static_cast<int8_t>(value)));
1407 }
1408 break;
1409
1410 default:
1411 LOG(FATAL) << "Unexpected type conversion from " << input_type
1412 << " to " << result_type;
1413 }
1414 break;
1415
Roland Levillain946e1432014-11-11 17:35:19 +00001416 case Primitive::kPrimInt:
1417 switch (input_type) {
1418 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001419 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001420 if (in.IsRegisterPair()) {
1421 __ movl(out.As<Register>(), in.AsRegisterPairLow<Register>());
1422 } else if (in.IsDoubleStackSlot()) {
1423 __ movl(out.As<Register>(), Address(ESP, in.GetStackIndex()));
1424 } else {
1425 DCHECK(in.IsConstant());
1426 DCHECK(in.GetConstant()->IsLongConstant());
1427 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
1428 __ movl(out.As<Register>(), Immediate(static_cast<int32_t>(value)));
1429 }
1430 break;
1431
1432 case Primitive::kPrimFloat:
1433 case Primitive::kPrimDouble:
1434 LOG(FATAL) << "Type conversion from " << input_type
1435 << " to " << result_type << " not yet implemented";
1436 break;
1437
1438 default:
1439 LOG(FATAL) << "Unexpected type conversion from " << input_type
1440 << " to " << result_type;
1441 }
1442 break;
1443
Roland Levillaindff1f282014-11-05 14:15:05 +00001444 case Primitive::kPrimLong:
1445 switch (input_type) {
1446 case Primitive::kPrimByte:
1447 case Primitive::kPrimShort:
1448 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001449 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001450 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001451 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1452 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
1453 DCHECK_EQ(in.As<Register>(), EAX);
1454 __ cdq();
1455 break;
1456
1457 case Primitive::kPrimFloat:
1458 case Primitive::kPrimDouble:
1459 LOG(FATAL) << "Type conversion from " << input_type << " to "
1460 << result_type << " not yet implemented";
1461 break;
1462
1463 default:
1464 LOG(FATAL) << "Unexpected type conversion from " << input_type
1465 << " to " << result_type;
1466 }
1467 break;
1468
Roland Levillain981e4542014-11-14 11:47:14 +00001469 case Primitive::kPrimChar:
1470 switch (input_type) {
1471 case Primitive::kPrimByte:
1472 case Primitive::kPrimShort:
1473 case Primitive::kPrimInt:
1474 case Primitive::kPrimChar:
1475 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1476 if (in.IsRegister()) {
1477 __ movzxw(out.As<Register>(), in.As<Register>());
1478 } else if (in.IsStackSlot()) {
1479 __ movzxw(out.As<Register>(), Address(ESP, in.GetStackIndex()));
1480 } else {
1481 DCHECK(in.GetConstant()->IsIntConstant());
1482 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1483 __ movl(out.As<Register>(), Immediate(static_cast<uint16_t>(value)));
1484 }
1485 break;
1486
1487 default:
1488 LOG(FATAL) << "Unexpected type conversion from " << input_type
1489 << " to " << result_type;
1490 }
1491 break;
1492
Roland Levillaindff1f282014-11-05 14:15:05 +00001493 case Primitive::kPrimFloat:
1494 case Primitive::kPrimDouble:
1495 LOG(FATAL) << "Type conversion from " << input_type
1496 << " to " << result_type << " not yet implemented";
1497 break;
1498
1499 default:
1500 LOG(FATAL) << "Unexpected type conversion from " << input_type
1501 << " to " << result_type;
1502 }
1503}
1504
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001505void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001506 LocationSummary* locations =
1507 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001508 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001509 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001510 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001511 locations->SetInAt(0, Location::RequiresRegister());
1512 locations->SetInAt(1, Location::Any());
1513 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001514 break;
1515 }
1516
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001517 case Primitive::kPrimFloat:
1518 case Primitive::kPrimDouble: {
1519 locations->SetInAt(0, Location::RequiresFpuRegister());
1520 locations->SetInAt(1, Location::Any());
1521 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001522 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001523 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001524
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001525 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001526 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1527 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001528 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001529}
1530
1531void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1532 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001533 Location first = locations->InAt(0);
1534 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001535 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001536 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001537 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001538 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001539 __ addl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001540 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001541 __ addl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001542 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001543 __ addl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001544 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001545 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001546 }
1547
1548 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001549 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001550 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1551 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001552 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001553 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1554 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001555 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001556 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001557 break;
1558 }
1559
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001560 case Primitive::kPrimFloat: {
1561 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001562 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001563 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001564 __ addss(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001565 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001566 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001567 }
1568
1569 case Primitive::kPrimDouble: {
1570 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001571 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001572 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001573 __ addsd(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001574 }
1575 break;
1576 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001577
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001578 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001579 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001580 }
1581}
1582
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001583void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001584 LocationSummary* locations =
1585 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001586 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001587 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001588 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001589 locations->SetInAt(0, Location::RequiresRegister());
1590 locations->SetInAt(1, Location::Any());
1591 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001592 break;
1593 }
Calin Juravle11351682014-10-23 15:38:15 +01001594 case Primitive::kPrimFloat:
1595 case Primitive::kPrimDouble: {
1596 locations->SetInAt(0, Location::RequiresFpuRegister());
1597 locations->SetInAt(1, Location::RequiresFpuRegister());
1598 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001599 break;
Calin Juravle11351682014-10-23 15:38:15 +01001600 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001601
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001602 default:
Calin Juravle11351682014-10-23 15:38:15 +01001603 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001604 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001605}
1606
1607void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1608 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001609 Location first = locations->InAt(0);
1610 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001611 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001612 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001613 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001614 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001615 __ subl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001616 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001617 __ subl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001618 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001619 __ subl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001620 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001621 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001622 }
1623
1624 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001625 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01001626 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1627 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001628 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001629 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001630 __ sbbl(first.AsRegisterPairHigh<Register>(),
1631 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001632 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001633 break;
1634 }
1635
Calin Juravle11351682014-10-23 15:38:15 +01001636 case Primitive::kPrimFloat: {
1637 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001638 break;
Calin Juravle11351682014-10-23 15:38:15 +01001639 }
1640
1641 case Primitive::kPrimDouble: {
1642 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1643 break;
1644 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001645
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001646 default:
Calin Juravle11351682014-10-23 15:38:15 +01001647 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001648 }
1649}
1650
Calin Juravle34bacdf2014-10-07 20:23:36 +01001651void LocationsBuilderX86::VisitMul(HMul* mul) {
1652 LocationSummary* locations =
1653 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1654 switch (mul->GetResultType()) {
1655 case Primitive::kPrimInt:
1656 locations->SetInAt(0, Location::RequiresRegister());
1657 locations->SetInAt(1, Location::Any());
1658 locations->SetOut(Location::SameAsFirstInput());
1659 break;
1660 case Primitive::kPrimLong: {
1661 locations->SetInAt(0, Location::RequiresRegister());
1662 // TODO: Currently this handles only stack operands:
1663 // - we don't have enough registers because we currently use Quick ABI.
1664 // - by the time we have a working register allocator we will probably change the ABI
1665 // and fix the above.
1666 // - we don't have a way yet to request operands on stack but the base line compiler
1667 // will leave the operands on the stack with Any().
1668 locations->SetInAt(1, Location::Any());
1669 locations->SetOut(Location::SameAsFirstInput());
1670 // Needed for imul on 32bits with 64bits output.
1671 locations->AddTemp(Location::RegisterLocation(EAX));
1672 locations->AddTemp(Location::RegisterLocation(EDX));
1673 break;
1674 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001675 case Primitive::kPrimFloat:
1676 case Primitive::kPrimDouble: {
1677 locations->SetInAt(0, Location::RequiresFpuRegister());
1678 locations->SetInAt(1, Location::RequiresFpuRegister());
1679 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001680 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001681 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001682
1683 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001684 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001685 }
1686}
1687
1688void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1689 LocationSummary* locations = mul->GetLocations();
1690 Location first = locations->InAt(0);
1691 Location second = locations->InAt(1);
1692 DCHECK(first.Equals(locations->Out()));
1693
1694 switch (mul->GetResultType()) {
1695 case Primitive::kPrimInt: {
1696 if (second.IsRegister()) {
1697 __ imull(first.As<Register>(), second.As<Register>());
1698 } else if (second.IsConstant()) {
1699 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1700 __ imull(first.As<Register>(), imm);
1701 } else {
1702 DCHECK(second.IsStackSlot());
1703 __ imull(first.As<Register>(), Address(ESP, second.GetStackIndex()));
1704 }
1705 break;
1706 }
1707
1708 case Primitive::kPrimLong: {
1709 DCHECK(second.IsDoubleStackSlot());
1710
1711 Register in1_hi = first.AsRegisterPairHigh<Register>();
1712 Register in1_lo = first.AsRegisterPairLow<Register>();
1713 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
1714 Address in2_lo(ESP, second.GetStackIndex());
1715 Register eax = locations->GetTemp(0).As<Register>();
1716 Register edx = locations->GetTemp(1).As<Register>();
1717
1718 DCHECK_EQ(EAX, eax);
1719 DCHECK_EQ(EDX, edx);
1720
1721 // input: in1 - 64 bits, in2 - 64 bits
1722 // output: in1
1723 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
1724 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
1725 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
1726
1727 __ movl(eax, in2_hi);
1728 // eax <- in1.lo * in2.hi
1729 __ imull(eax, in1_lo);
1730 // in1.hi <- in1.hi * in2.lo
1731 __ imull(in1_hi, in2_lo);
1732 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
1733 __ addl(in1_hi, eax);
1734 // move in1_lo to eax to prepare for double precision
1735 __ movl(eax, in1_lo);
1736 // edx:eax <- in1.lo * in2.lo
1737 __ mull(in2_lo);
1738 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
1739 __ addl(in1_hi, edx);
1740 // in1.lo <- (in1.lo * in2.lo)[31:0];
1741 __ movl(in1_lo, eax);
1742
1743 break;
1744 }
1745
Calin Juravleb5bfa962014-10-21 18:02:24 +01001746 case Primitive::kPrimFloat: {
1747 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001748 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001749 }
1750
1751 case Primitive::kPrimDouble: {
1752 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1753 break;
1754 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001755
1756 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001757 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001758 }
1759}
1760
Calin Juravlebacfec32014-11-14 15:54:36 +00001761void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
1762 DCHECK(instruction->IsDiv() || instruction->IsRem());
1763
1764 LocationSummary* locations = instruction->GetLocations();
1765 Location out = locations->Out();
1766 Location first = locations->InAt(0);
1767 Location second = locations->InAt(1);
1768 bool is_div = instruction->IsDiv();
1769
1770 switch (instruction->GetResultType()) {
1771 case Primitive::kPrimInt: {
1772 Register second_reg = second.As<Register>();
1773 DCHECK_EQ(EAX, first.As<Register>());
1774 DCHECK_EQ(is_div ? EAX : EDX, out.As<Register>());
1775
1776 SlowPathCodeX86* slow_path =
1777 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.As<Register>(), is_div);
1778 codegen_->AddSlowPath(slow_path);
1779
1780 // 0x80000000/-1 triggers an arithmetic exception!
1781 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
1782 // it's safe to just use negl instead of more complex comparisons.
1783
1784 __ cmpl(second_reg, Immediate(-1));
1785 __ j(kEqual, slow_path->GetEntryLabel());
1786
1787 // edx:eax <- sign-extended of eax
1788 __ cdq();
1789 // eax = quotient, edx = remainder
1790 __ idivl(second_reg);
1791
1792 __ Bind(slow_path->GetExitLabel());
1793 break;
1794 }
1795
1796 case Primitive::kPrimLong: {
1797 InvokeRuntimeCallingConvention calling_convention;
1798 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
1799 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
1800 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
1801 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
1802 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
1803 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
1804
1805 if (is_div) {
1806 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
1807 } else {
1808 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
1809 }
1810 uint32_t dex_pc = is_div
1811 ? instruction->AsDiv()->GetDexPc()
1812 : instruction->AsRem()->GetDexPc();
1813 codegen_->RecordPcInfo(instruction, dex_pc);
1814
1815 break;
1816 }
1817
1818 default:
1819 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
1820 }
1821}
1822
Calin Juravle7c4954d2014-10-28 16:57:40 +00001823void LocationsBuilderX86::VisitDiv(HDiv* div) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001824 LocationSummary::CallKind call_kind = div->GetResultType() == Primitive::kPrimLong
1825 ? LocationSummary::kCall
1826 : LocationSummary::kNoCall;
1827 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
1828
Calin Juravle7c4954d2014-10-28 16:57:40 +00001829 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001830 case Primitive::kPrimInt: {
1831 locations->SetInAt(0, Location::RegisterLocation(EAX));
1832 locations->SetInAt(1, Location::RequiresRegister());
1833 locations->SetOut(Location::SameAsFirstInput());
1834 // Intel uses edx:eax as the dividend.
1835 locations->AddTemp(Location::RegisterLocation(EDX));
1836 break;
1837 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001838 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001839 InvokeRuntimeCallingConvention calling_convention;
1840 locations->SetInAt(0, Location::RegisterPairLocation(
1841 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
1842 locations->SetInAt(1, Location::RegisterPairLocation(
1843 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
1844 // Runtime helper puts the result in EAX, EDX.
1845 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00001846 break;
1847 }
1848 case Primitive::kPrimFloat:
1849 case Primitive::kPrimDouble: {
1850 locations->SetInAt(0, Location::RequiresFpuRegister());
1851 locations->SetInAt(1, Location::RequiresFpuRegister());
1852 locations->SetOut(Location::SameAsFirstInput());
1853 break;
1854 }
1855
1856 default:
1857 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1858 }
1859}
1860
1861void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
1862 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001863 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00001864 Location first = locations->InAt(0);
1865 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001866
1867 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00001868 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00001869 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001870 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001871 break;
1872 }
1873
1874 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001875 DCHECK(first.Equals(out));
Calin Juravle7c4954d2014-10-28 16:57:40 +00001876 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1877 break;
1878 }
1879
1880 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001881 DCHECK(first.Equals(out));
Calin Juravle7c4954d2014-10-28 16:57:40 +00001882 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1883 break;
1884 }
1885
1886 default:
1887 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1888 }
1889}
1890
Calin Juravlebacfec32014-11-14 15:54:36 +00001891void LocationsBuilderX86::VisitRem(HRem* rem) {
1892 LocationSummary::CallKind call_kind = rem->GetResultType() == Primitive::kPrimLong
1893 ? LocationSummary::kCall
1894 : LocationSummary::kNoCall;
1895 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
1896
1897 switch (rem->GetResultType()) {
1898 case Primitive::kPrimInt: {
1899 locations->SetInAt(0, Location::RegisterLocation(EAX));
1900 locations->SetInAt(1, Location::RequiresRegister());
1901 locations->SetOut(Location::RegisterLocation(EDX));
1902 break;
1903 }
1904 case Primitive::kPrimLong: {
1905 InvokeRuntimeCallingConvention calling_convention;
1906 locations->SetInAt(0, Location::RegisterPairLocation(
1907 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
1908 locations->SetInAt(1, Location::RegisterPairLocation(
1909 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
1910 // Runtime helper puts the result in EAX, EDX.
1911 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1912 break;
1913 }
1914 case Primitive::kPrimFloat:
1915 case Primitive::kPrimDouble: {
1916 LOG(FATAL) << "Unimplemented rem type " << rem->GetResultType();
1917 break;
1918 }
1919
1920 default:
1921 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
1922 }
1923}
1924
1925void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
1926 Primitive::Type type = rem->GetResultType();
1927 switch (type) {
1928 case Primitive::kPrimInt:
1929 case Primitive::kPrimLong: {
1930 GenerateDivRemIntegral(rem);
1931 break;
1932 }
1933 case Primitive::kPrimFloat:
1934 case Primitive::kPrimDouble: {
1935 LOG(FATAL) << "Unimplemented rem type " << type;
1936 break;
1937 }
1938 default:
1939 LOG(FATAL) << "Unexpected rem type " << type;
1940 }
1941}
1942
Calin Juravled0d48522014-11-04 16:40:20 +00001943void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1944 LocationSummary* locations =
1945 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001946 switch (instruction->GetType()) {
1947 case Primitive::kPrimInt: {
1948 locations->SetInAt(0, Location::Any());
1949 break;
1950 }
1951 case Primitive::kPrimLong: {
1952 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
1953 if (!instruction->IsConstant()) {
1954 locations->AddTemp(Location::RequiresRegister());
1955 }
1956 break;
1957 }
1958 default:
1959 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
1960 }
Calin Juravled0d48522014-11-04 16:40:20 +00001961 if (instruction->HasUses()) {
1962 locations->SetOut(Location::SameAsFirstInput());
1963 }
1964}
1965
1966void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1967 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
1968 codegen_->AddSlowPath(slow_path);
1969
1970 LocationSummary* locations = instruction->GetLocations();
1971 Location value = locations->InAt(0);
1972
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001973 switch (instruction->GetType()) {
1974 case Primitive::kPrimInt: {
1975 if (value.IsRegister()) {
1976 __ testl(value.As<Register>(), value.As<Register>());
1977 __ j(kEqual, slow_path->GetEntryLabel());
1978 } else if (value.IsStackSlot()) {
1979 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
1980 __ j(kEqual, slow_path->GetEntryLabel());
1981 } else {
1982 DCHECK(value.IsConstant()) << value;
1983 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
1984 __ jmp(slow_path->GetEntryLabel());
1985 }
1986 }
1987 break;
Calin Juravled0d48522014-11-04 16:40:20 +00001988 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001989 case Primitive::kPrimLong: {
1990 if (value.IsRegisterPair()) {
1991 Register temp = locations->GetTemp(0).As<Register>();
1992 __ movl(temp, value.AsRegisterPairLow<Register>());
1993 __ orl(temp, value.AsRegisterPairHigh<Register>());
1994 __ j(kEqual, slow_path->GetEntryLabel());
1995 } else {
1996 DCHECK(value.IsConstant()) << value;
1997 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
1998 __ jmp(slow_path->GetEntryLabel());
1999 }
2000 }
2001 break;
2002 }
2003 default:
2004 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002005 }
Calin Juravled0d48522014-11-04 16:40:20 +00002006}
2007
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002008void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002009 LocationSummary* locations =
2010 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002011 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002012 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002013 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2014 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002015}
2016
2017void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2018 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002019 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002020 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002021
Nicolas Geoffray707c8092014-04-04 10:50:14 +01002022 __ fs()->call(
2023 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002024
Nicolas Geoffray39468442014-09-02 15:17:15 +01002025 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002026 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002027}
2028
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002029void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2030 LocationSummary* locations =
2031 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2032 locations->SetOut(Location::RegisterLocation(EAX));
2033 InvokeRuntimeCallingConvention calling_convention;
2034 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2035 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2036 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2037}
2038
2039void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2040 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002041 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002042 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2043
2044 __ fs()->call(
2045 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
2046
2047 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2048 DCHECK(!codegen_->IsLeafMethod());
2049}
2050
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002051void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002052 LocationSummary* locations =
2053 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002054 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2055 if (location.IsStackSlot()) {
2056 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2057 } else if (location.IsDoubleStackSlot()) {
2058 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002059 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002060 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002061}
2062
2063void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002064 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002065}
2066
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002067void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002068 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002069 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002070 locations->SetInAt(0, Location::RequiresRegister());
2071 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002072}
2073
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002074void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2075 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002076 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002077 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002078 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002079 switch (not_->InputAt(0)->GetType()) {
2080 case Primitive::kPrimBoolean:
2081 __ xorl(out.As<Register>(), Immediate(1));
2082 break;
2083
2084 case Primitive::kPrimInt:
2085 __ notl(out.As<Register>());
2086 break;
2087
2088 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002089 __ notl(out.AsRegisterPairLow<Register>());
2090 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002091 break;
2092
2093 default:
2094 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2095 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002096}
2097
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002098void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002099 LocationSummary* locations =
2100 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002101 locations->SetInAt(0, Location::RequiresRegister());
2102 locations->SetInAt(1, Location::Any());
2103 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002104}
2105
2106void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002107 LocationSummary* locations = compare->GetLocations();
2108 switch (compare->InputAt(0)->GetType()) {
2109 case Primitive::kPrimLong: {
2110 Label less, greater, done;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002111 Register output = locations->Out().As<Register>();
2112 Location left = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002113 Location right = locations->InAt(1);
2114 if (right.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002115 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002116 } else {
2117 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002118 __ cmpl(left.AsRegisterPairHigh<Register>(),
2119 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002120 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002121 __ j(kLess, &less); // Signed compare.
2122 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002123 if (right.IsRegisterPair()) {
2124 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002125 } else {
2126 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002127 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002128 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002129 __ movl(output, Immediate(0));
2130 __ j(kEqual, &done);
2131 __ j(kBelow, &less); // Unsigned compare.
2132
2133 __ Bind(&greater);
2134 __ movl(output, Immediate(1));
2135 __ jmp(&done);
2136
2137 __ Bind(&less);
2138 __ movl(output, Immediate(-1));
2139
2140 __ Bind(&done);
2141 break;
2142 }
2143 default:
2144 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
2145 }
2146}
2147
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002148void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002149 LocationSummary* locations =
2150 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002151 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2152 locations->SetInAt(i, Location::Any());
2153 }
2154 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002155}
2156
2157void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002158 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002159 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002160}
2161
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002162void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002163 LocationSummary* locations =
2164 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002165 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002166 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002167 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002168 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2169 || (field_type == Primitive::kPrimByte);
2170 // The register allocator does not support multiple
2171 // inputs that die at entry with one in a specific register.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002172 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002173 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002174 locations->SetInAt(1, Location::RegisterLocation(EAX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002175 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002176 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002177 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002178 // Temporary registers for the write barrier.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002179 if (is_object_type) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002180 locations->AddTemp(Location::RequiresRegister());
2181 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002182 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002183 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002184}
2185
2186void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2187 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002188 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002189 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01002190 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002191
2192 switch (field_type) {
2193 case Primitive::kPrimBoolean:
2194 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002195 ByteRegister value = locations->InAt(1).As<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002196 __ movb(Address(obj, offset), value);
2197 break;
2198 }
2199
2200 case Primitive::kPrimShort:
2201 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002202 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002203 __ movw(Address(obj, offset), value);
2204 break;
2205 }
2206
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002207 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002208 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002209 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002210 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002211
2212 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002213 Register temp = locations->GetTemp(0).As<Register>();
2214 Register card = locations->GetTemp(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002215 codegen_->MarkGCCard(temp, card, obj, value);
2216 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002217 break;
2218 }
2219
2220 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002221 Location value = locations->InAt(1);
2222 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
2223 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002224 break;
2225 }
2226
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002227 case Primitive::kPrimFloat: {
2228 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2229 __ movss(Address(obj, offset), value);
2230 break;
2231 }
2232
2233 case Primitive::kPrimDouble: {
2234 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2235 __ movsd(Address(obj, offset), value);
2236 break;
2237 }
2238
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002239 case Primitive::kPrimVoid:
2240 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002241 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002242 }
2243}
2244
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002245void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
2246 Label is_null;
2247 __ testl(value, value);
2248 __ j(kEqual, &is_null);
2249 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2250 __ movl(temp, object);
2251 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
2252 __ movb(Address(temp, card, TIMES_1, 0),
2253 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
2254 __ Bind(&is_null);
2255}
2256
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002257void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002258 LocationSummary* locations =
2259 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002260 locations->SetInAt(0, Location::RequiresRegister());
2261 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002262}
2263
2264void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2265 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002266 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002267 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2268
2269 switch (instruction->GetType()) {
2270 case Primitive::kPrimBoolean: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002271 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002272 __ movzxb(out, Address(obj, offset));
2273 break;
2274 }
2275
2276 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002277 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002278 __ movsxb(out, Address(obj, offset));
2279 break;
2280 }
2281
2282 case Primitive::kPrimShort: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002283 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002284 __ movsxw(out, Address(obj, offset));
2285 break;
2286 }
2287
2288 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002289 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002290 __ movzxw(out, Address(obj, offset));
2291 break;
2292 }
2293
2294 case Primitive::kPrimInt:
2295 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002296 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002297 __ movl(out, Address(obj, offset));
2298 break;
2299 }
2300
2301 case Primitive::kPrimLong: {
2302 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002303 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
2304 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002305 break;
2306 }
2307
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002308 case Primitive::kPrimFloat: {
2309 XmmRegister out = locations->Out().As<XmmRegister>();
2310 __ movss(out, Address(obj, offset));
2311 break;
2312 }
2313
2314 case Primitive::kPrimDouble: {
2315 XmmRegister out = locations->Out().As<XmmRegister>();
2316 __ movsd(out, Address(obj, offset));
2317 break;
2318 }
2319
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002320 case Primitive::kPrimVoid:
2321 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002322 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002323 }
2324}
2325
2326void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002327 LocationSummary* locations =
2328 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002329 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002330 if (instruction->HasUses()) {
2331 locations->SetOut(Location::SameAsFirstInput());
2332 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002333}
2334
2335void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002336 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002337 codegen_->AddSlowPath(slow_path);
2338
2339 LocationSummary* locations = instruction->GetLocations();
2340 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002341
2342 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002343 __ cmpl(obj.As<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002344 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002345 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002346 } else {
2347 DCHECK(obj.IsConstant()) << obj;
2348 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2349 __ jmp(slow_path->GetEntryLabel());
2350 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002351 }
2352 __ j(kEqual, slow_path->GetEntryLabel());
2353}
2354
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002355void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002356 LocationSummary* locations =
2357 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002358 locations->SetInAt(0, Location::RequiresRegister());
2359 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2360 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002361}
2362
2363void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
2364 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002365 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002366 Location index = locations->InAt(1);
2367
2368 switch (instruction->GetType()) {
2369 case Primitive::kPrimBoolean: {
2370 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002371 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002372 if (index.IsConstant()) {
2373 __ movzxb(out, Address(obj,
2374 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2375 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002376 __ movzxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002377 }
2378 break;
2379 }
2380
2381 case Primitive::kPrimByte: {
2382 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002383 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002384 if (index.IsConstant()) {
2385 __ movsxb(out, Address(obj,
2386 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2387 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002388 __ movsxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002389 }
2390 break;
2391 }
2392
2393 case Primitive::kPrimShort: {
2394 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002395 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002396 if (index.IsConstant()) {
2397 __ movsxw(out, Address(obj,
2398 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2399 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002400 __ movsxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002401 }
2402 break;
2403 }
2404
2405 case Primitive::kPrimChar: {
2406 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002407 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002408 if (index.IsConstant()) {
2409 __ movzxw(out, Address(obj,
2410 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2411 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002412 __ movzxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002413 }
2414 break;
2415 }
2416
2417 case Primitive::kPrimInt:
2418 case Primitive::kPrimNot: {
2419 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002420 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002421 if (index.IsConstant()) {
2422 __ movl(out, Address(obj,
2423 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2424 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002425 __ movl(out, Address(obj, index.As<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002426 }
2427 break;
2428 }
2429
2430 case Primitive::kPrimLong: {
2431 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002432 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002433 if (index.IsConstant()) {
2434 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002435 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
2436 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002437 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002438 __ movl(out.AsRegisterPairLow<Register>(),
2439 Address(obj, index.As<Register>(), TIMES_8, data_offset));
2440 __ movl(out.AsRegisterPairHigh<Register>(),
2441 Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002442 }
2443 break;
2444 }
2445
2446 case Primitive::kPrimFloat:
2447 case Primitive::kPrimDouble:
2448 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002449 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002450 case Primitive::kPrimVoid:
2451 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002452 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002453 }
2454}
2455
2456void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002457 Primitive::Type value_type = instruction->GetComponentType();
2458 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2459 instruction,
2460 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
2461
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002462 if (value_type == Primitive::kPrimNot) {
2463 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002464 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2465 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2466 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002467 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002468 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
2469 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002470 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002471 // In case of a byte operation, the register allocator does not support multiple
2472 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002473 locations->SetInAt(0, Location::RequiresRegister());
2474 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002475 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002476 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002477 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002478 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002479 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002480 }
2481 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002482}
2483
2484void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
2485 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002486 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002487 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002488 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002489 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002490
2491 switch (value_type) {
2492 case Primitive::kPrimBoolean:
2493 case Primitive::kPrimByte: {
2494 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002495 if (index.IsConstant()) {
2496 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002497 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002498 __ movb(Address(obj, offset), value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002499 } else {
2500 __ movb(Address(obj, offset),
2501 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2502 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002503 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002504 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002505 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
2506 value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002507 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002508 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002509 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2510 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002511 }
2512 break;
2513 }
2514
2515 case Primitive::kPrimShort:
2516 case Primitive::kPrimChar: {
2517 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002518 if (index.IsConstant()) {
2519 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002520 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002521 __ movw(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002522 } else {
2523 __ movw(Address(obj, offset),
2524 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2525 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002526 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002527 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002528 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
2529 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002530 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002531 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002532 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2533 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002534 }
2535 break;
2536 }
2537
2538 case Primitive::kPrimInt: {
2539 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002540 if (index.IsConstant()) {
2541 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002542 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002543 __ movl(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002544 } else {
2545 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2546 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002547 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002548 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002549 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
2550 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002551 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002552 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002553 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2554 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002555 }
2556 break;
2557 }
2558
2559 case Primitive::kPrimNot: {
2560 DCHECK(!codegen_->IsLeafMethod());
2561 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01002562 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002563 break;
2564 }
2565
2566 case Primitive::kPrimLong: {
2567 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002568 if (index.IsConstant()) {
2569 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002570 if (value.IsRegisterPair()) {
2571 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
2572 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002573 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002574 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002575 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
2576 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
2577 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
2578 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002579 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002580 if (value.IsRegisterPair()) {
2581 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
2582 value.AsRegisterPairLow<Register>());
2583 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
2584 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002585 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002586 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002587 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002588 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002589 Immediate(Low32Bits(val)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002590 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002591 Immediate(High32Bits(val)));
2592 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002593 }
2594 break;
2595 }
2596
2597 case Primitive::kPrimFloat:
2598 case Primitive::kPrimDouble:
2599 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002600 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002601 case Primitive::kPrimVoid:
2602 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002603 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002604 }
2605}
2606
2607void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
2608 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002609 locations->SetInAt(0, Location::RequiresRegister());
2610 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002611 instruction->SetLocations(locations);
2612}
2613
2614void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
2615 LocationSummary* locations = instruction->GetLocations();
2616 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002617 Register obj = locations->InAt(0).As<Register>();
2618 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002619 __ movl(out, Address(obj, offset));
2620}
2621
2622void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002623 LocationSummary* locations =
2624 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002625 locations->SetInAt(0, Location::RequiresRegister());
2626 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002627 if (instruction->HasUses()) {
2628 locations->SetOut(Location::SameAsFirstInput());
2629 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002630}
2631
2632void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
2633 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002634 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002635 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002636 codegen_->AddSlowPath(slow_path);
2637
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002638 Register index = locations->InAt(0).As<Register>();
2639 Register length = locations->InAt(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002640
2641 __ cmpl(index, length);
2642 __ j(kAboveEqual, slow_path->GetEntryLabel());
2643}
2644
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002645void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
2646 temp->SetLocations(nullptr);
2647}
2648
2649void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
2650 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002651 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002652}
2653
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002654void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002655 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002656 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002657}
2658
2659void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002660 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2661}
2662
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002663void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
2664 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2665}
2666
2667void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002668 HBasicBlock* block = instruction->GetBlock();
2669 if (block->GetLoopInformation() != nullptr) {
2670 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2671 // The back edge will generate the suspend check.
2672 return;
2673 }
2674 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2675 // The goto will generate the suspend check.
2676 return;
2677 }
2678 GenerateSuspendCheck(instruction, nullptr);
2679}
2680
2681void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
2682 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002683 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002684 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002685 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002686 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002687 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002688 if (successor == nullptr) {
2689 __ j(kNotEqual, slow_path->GetEntryLabel());
2690 __ Bind(slow_path->GetReturnLabel());
2691 } else {
2692 __ j(kEqual, codegen_->GetLabelOf(successor));
2693 __ jmp(slow_path->GetEntryLabel());
2694 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002695}
2696
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002697X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
2698 return codegen_->GetAssembler();
2699}
2700
2701void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
2702 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002703 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002704 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2705 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
2706 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
2707}
2708
2709void ParallelMoveResolverX86::EmitMove(size_t index) {
2710 MoveOperands* move = moves_.Get(index);
2711 Location source = move->GetSource();
2712 Location destination = move->GetDestination();
2713
2714 if (source.IsRegister()) {
2715 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002716 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002717 } else {
2718 DCHECK(destination.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002719 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002720 }
2721 } else if (source.IsStackSlot()) {
2722 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002723 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002724 } else {
2725 DCHECK(destination.IsStackSlot());
2726 MoveMemoryToMemory(destination.GetStackIndex(),
2727 source.GetStackIndex());
2728 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002729 } else if (source.IsConstant()) {
2730 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
2731 Immediate imm(instruction->AsIntConstant()->GetValue());
2732 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002733 __ movl(destination.As<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002734 } else {
2735 __ movl(Address(ESP, destination.GetStackIndex()), imm);
2736 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002737 } else {
2738 LOG(FATAL) << "Unimplemented";
2739 }
2740}
2741
2742void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002743 Register suggested_scratch = reg == EAX ? EBX : EAX;
2744 ScratchRegisterScope ensure_scratch(
2745 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2746
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002747 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2748 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
2749 __ movl(Address(ESP, mem + stack_offset), reg);
2750 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
2751}
2752
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002753void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
2754 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002755 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
2756
2757 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002758 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002759 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2760
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002761 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
2762 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
2763 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
2764 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
2765 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
2766 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
2767}
2768
2769void ParallelMoveResolverX86::EmitSwap(size_t index) {
2770 MoveOperands* move = moves_.Get(index);
2771 Location source = move->GetSource();
2772 Location destination = move->GetDestination();
2773
2774 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002775 __ xchgl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002776 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002777 Exchange(source.As<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002778 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002779 Exchange(destination.As<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002780 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
2781 Exchange(destination.GetStackIndex(), source.GetStackIndex());
2782 } else {
2783 LOG(FATAL) << "Unimplemented";
2784 }
2785}
2786
2787void ParallelMoveResolverX86::SpillScratch(int reg) {
2788 __ pushl(static_cast<Register>(reg));
2789}
2790
2791void ParallelMoveResolverX86::RestoreScratch(int reg) {
2792 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002793}
2794
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002795void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002796 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2797 ? LocationSummary::kCallOnSlowPath
2798 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002799 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002800 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002801 locations->SetOut(Location::RequiresRegister());
2802}
2803
2804void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
2805 Register out = cls->GetLocations()->Out().As<Register>();
2806 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002807 DCHECK(!cls->CanCallRuntime());
2808 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002809 codegen_->LoadCurrentMethod(out);
2810 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2811 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002812 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002813 codegen_->LoadCurrentMethod(out);
2814 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2815 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002816
2817 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
2818 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2819 codegen_->AddSlowPath(slow_path);
2820 __ testl(out, out);
2821 __ j(kEqual, slow_path->GetEntryLabel());
2822 if (cls->MustGenerateClinitCheck()) {
2823 GenerateClassInitializationCheck(slow_path, out);
2824 } else {
2825 __ Bind(slow_path->GetExitLabel());
2826 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002827 }
2828}
2829
2830void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
2831 LocationSummary* locations =
2832 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2833 locations->SetInAt(0, Location::RequiresRegister());
2834 if (check->HasUses()) {
2835 locations->SetOut(Location::SameAsFirstInput());
2836 }
2837}
2838
2839void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002840 // We assume the class to not be null.
2841 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
2842 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002843 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002844 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<Register>());
2845}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002846
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002847void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
2848 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002849 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2850 Immediate(mirror::Class::kStatusInitialized));
2851 __ j(kLess, slow_path->GetEntryLabel());
2852 __ Bind(slow_path->GetExitLabel());
2853 // No need for memory fence, thanks to the X86 memory model.
2854}
2855
2856void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2857 LocationSummary* locations =
2858 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2859 locations->SetInAt(0, Location::RequiresRegister());
2860 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2861}
2862
2863void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2864 LocationSummary* locations = instruction->GetLocations();
2865 Register cls = locations->InAt(0).As<Register>();
2866 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2867
2868 switch (instruction->GetType()) {
2869 case Primitive::kPrimBoolean: {
2870 Register out = locations->Out().As<Register>();
2871 __ movzxb(out, Address(cls, offset));
2872 break;
2873 }
2874
2875 case Primitive::kPrimByte: {
2876 Register out = locations->Out().As<Register>();
2877 __ movsxb(out, Address(cls, offset));
2878 break;
2879 }
2880
2881 case Primitive::kPrimShort: {
2882 Register out = locations->Out().As<Register>();
2883 __ movsxw(out, Address(cls, offset));
2884 break;
2885 }
2886
2887 case Primitive::kPrimChar: {
2888 Register out = locations->Out().As<Register>();
2889 __ movzxw(out, Address(cls, offset));
2890 break;
2891 }
2892
2893 case Primitive::kPrimInt:
2894 case Primitive::kPrimNot: {
2895 Register out = locations->Out().As<Register>();
2896 __ movl(out, Address(cls, offset));
2897 break;
2898 }
2899
2900 case Primitive::kPrimLong: {
2901 // TODO: support volatile.
2902 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(cls, offset));
2903 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(cls, kX86WordSize + offset));
2904 break;
2905 }
2906
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002907 case Primitive::kPrimFloat: {
2908 XmmRegister out = locations->Out().As<XmmRegister>();
2909 __ movss(out, Address(cls, offset));
2910 break;
2911 }
2912
2913 case Primitive::kPrimDouble: {
2914 XmmRegister out = locations->Out().As<XmmRegister>();
2915 __ movsd(out, Address(cls, offset));
2916 break;
2917 }
2918
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002919 case Primitive::kPrimVoid:
2920 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2921 UNREACHABLE();
2922 }
2923}
2924
2925void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2926 LocationSummary* locations =
2927 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2928 locations->SetInAt(0, Location::RequiresRegister());
2929 Primitive::Type field_type = instruction->GetFieldType();
2930 bool is_object_type = field_type == Primitive::kPrimNot;
2931 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2932 || (field_type == Primitive::kPrimByte);
2933 // The register allocator does not support multiple
2934 // inputs that die at entry with one in a specific register.
2935 if (is_byte_type) {
2936 // Ensure the value is in a byte register.
2937 locations->SetInAt(1, Location::RegisterLocation(EAX));
2938 } else {
2939 locations->SetInAt(1, Location::RequiresRegister());
2940 }
2941 // Temporary registers for the write barrier.
2942 if (is_object_type) {
2943 locations->AddTemp(Location::RequiresRegister());
2944 // Ensure the card is in a byte register.
2945 locations->AddTemp(Location::RegisterLocation(ECX));
2946 }
2947}
2948
2949void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2950 LocationSummary* locations = instruction->GetLocations();
2951 Register cls = locations->InAt(0).As<Register>();
2952 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2953 Primitive::Type field_type = instruction->GetFieldType();
2954
2955 switch (field_type) {
2956 case Primitive::kPrimBoolean:
2957 case Primitive::kPrimByte: {
2958 ByteRegister value = locations->InAt(1).As<ByteRegister>();
2959 __ movb(Address(cls, offset), value);
2960 break;
2961 }
2962
2963 case Primitive::kPrimShort:
2964 case Primitive::kPrimChar: {
2965 Register value = locations->InAt(1).As<Register>();
2966 __ movw(Address(cls, offset), value);
2967 break;
2968 }
2969
2970 case Primitive::kPrimInt:
2971 case Primitive::kPrimNot: {
2972 Register value = locations->InAt(1).As<Register>();
2973 __ movl(Address(cls, offset), value);
2974
2975 if (field_type == Primitive::kPrimNot) {
2976 Register temp = locations->GetTemp(0).As<Register>();
2977 Register card = locations->GetTemp(1).As<Register>();
2978 codegen_->MarkGCCard(temp, card, cls, value);
2979 }
2980 break;
2981 }
2982
2983 case Primitive::kPrimLong: {
2984 Location value = locations->InAt(1);
2985 __ movl(Address(cls, offset), value.AsRegisterPairLow<Register>());
2986 __ movl(Address(cls, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
2987 break;
2988 }
2989
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002990 case Primitive::kPrimFloat: {
2991 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2992 __ movss(Address(cls, offset), value);
2993 break;
2994 }
2995
2996 case Primitive::kPrimDouble: {
2997 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2998 __ movsd(Address(cls, offset), value);
2999 break;
3000 }
3001
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003002 case Primitive::kPrimVoid:
3003 LOG(FATAL) << "Unreachable type " << field_type;
3004 UNREACHABLE();
3005 }
3006}
3007
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003008void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
3009 LocationSummary* locations =
3010 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3011 locations->SetOut(Location::RequiresRegister());
3012}
3013
3014void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
3015 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
3016 codegen_->AddSlowPath(slow_path);
3017
3018 Register out = load->GetLocations()->Out().As<Register>();
3019 codegen_->LoadCurrentMethod(out);
3020 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
3021 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3022 __ testl(out, out);
3023 __ j(kEqual, slow_path->GetEntryLabel());
3024 __ Bind(slow_path->GetExitLabel());
3025}
3026
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003027void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
3028 LocationSummary* locations =
3029 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3030 locations->SetOut(Location::RequiresRegister());
3031}
3032
3033void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
3034 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
3035 __ fs()->movl(load->GetLocations()->Out().As<Register>(), address);
3036 __ fs()->movl(address, Immediate(0));
3037}
3038
3039void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
3040 LocationSummary* locations =
3041 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3042 InvokeRuntimeCallingConvention calling_convention;
3043 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3044}
3045
3046void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
3047 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
3048 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3049}
3050
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003051void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003052 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3053 ? LocationSummary::kNoCall
3054 : LocationSummary::kCallOnSlowPath;
3055 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3056 locations->SetInAt(0, Location::RequiresRegister());
3057 locations->SetInAt(1, Location::Any());
3058 locations->SetOut(Location::RequiresRegister());
3059}
3060
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003061void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003062 LocationSummary* locations = instruction->GetLocations();
3063 Register obj = locations->InAt(0).As<Register>();
3064 Location cls = locations->InAt(1);
3065 Register out = locations->Out().As<Register>();
3066 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3067 Label done, zero;
3068 SlowPathCodeX86* slow_path = nullptr;
3069
3070 // Return 0 if `obj` is null.
3071 // TODO: avoid this check if we know obj is not null.
3072 __ testl(obj, obj);
3073 __ j(kEqual, &zero);
3074 __ movl(out, Address(obj, class_offset));
3075 // Compare the class of `obj` with `cls`.
3076 if (cls.IsRegister()) {
3077 __ cmpl(out, cls.As<Register>());
3078 } else {
3079 DCHECK(cls.IsStackSlot()) << cls;
3080 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
3081 }
3082
3083 if (instruction->IsClassFinal()) {
3084 // Classes must be equal for the instanceof to succeed.
3085 __ j(kNotEqual, &zero);
3086 __ movl(out, Immediate(1));
3087 __ jmp(&done);
3088 } else {
3089 // If the classes are not equal, we go into a slow path.
3090 DCHECK(locations->OnlyCallsOnSlowPath());
3091 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003092 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003093 codegen_->AddSlowPath(slow_path);
3094 __ j(kNotEqual, slow_path->GetEntryLabel());
3095 __ movl(out, Immediate(1));
3096 __ jmp(&done);
3097 }
3098 __ Bind(&zero);
3099 __ movl(out, Immediate(0));
3100 if (slow_path != nullptr) {
3101 __ Bind(slow_path->GetExitLabel());
3102 }
3103 __ Bind(&done);
3104}
3105
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003106void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
3107 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3108 instruction, LocationSummary::kCallOnSlowPath);
3109 locations->SetInAt(0, Location::RequiresRegister());
3110 locations->SetInAt(1, Location::Any());
3111 locations->AddTemp(Location::RequiresRegister());
3112}
3113
3114void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
3115 LocationSummary* locations = instruction->GetLocations();
3116 Register obj = locations->InAt(0).As<Register>();
3117 Location cls = locations->InAt(1);
3118 Register temp = locations->GetTemp(0).As<Register>();
3119 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3120 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
3121 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3122 codegen_->AddSlowPath(slow_path);
3123
3124 // TODO: avoid this check if we know obj is not null.
3125 __ testl(obj, obj);
3126 __ j(kEqual, slow_path->GetExitLabel());
3127 __ movl(temp, Address(obj, class_offset));
3128
3129 // Compare the class of `obj` with `cls`.
3130 if (cls.IsRegister()) {
3131 __ cmpl(temp, cls.As<Register>());
3132 } else {
3133 DCHECK(cls.IsStackSlot()) << cls;
3134 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
3135 }
3136
3137 __ j(kNotEqual, slow_path->GetEntryLabel());
3138 __ Bind(slow_path->GetExitLabel());
3139}
3140
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003141void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3142 LocationSummary* locations =
3143 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3144 InvokeRuntimeCallingConvention calling_convention;
3145 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3146}
3147
3148void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3149 __ fs()->call(Address::Absolute(instruction->IsEnter()
3150 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
3151 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
3152 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3153}
3154
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003155void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3156void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3157void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3158
3159void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3160 LocationSummary* locations =
3161 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3162 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3163 || instruction->GetResultType() == Primitive::kPrimLong);
3164 locations->SetInAt(0, Location::RequiresRegister());
3165 locations->SetInAt(1, Location::Any());
3166 locations->SetOut(Location::SameAsFirstInput());
3167}
3168
3169void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
3170 HandleBitwiseOperation(instruction);
3171}
3172
3173void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
3174 HandleBitwiseOperation(instruction);
3175}
3176
3177void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
3178 HandleBitwiseOperation(instruction);
3179}
3180
3181void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3182 LocationSummary* locations = instruction->GetLocations();
3183 Location first = locations->InAt(0);
3184 Location second = locations->InAt(1);
3185 DCHECK(first.Equals(locations->Out()));
3186
3187 if (instruction->GetResultType() == Primitive::kPrimInt) {
3188 if (second.IsRegister()) {
3189 if (instruction->IsAnd()) {
3190 __ andl(first.As<Register>(), second.As<Register>());
3191 } else if (instruction->IsOr()) {
3192 __ orl(first.As<Register>(), second.As<Register>());
3193 } else {
3194 DCHECK(instruction->IsXor());
3195 __ xorl(first.As<Register>(), second.As<Register>());
3196 }
3197 } else if (second.IsConstant()) {
3198 if (instruction->IsAnd()) {
3199 __ andl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3200 } else if (instruction->IsOr()) {
3201 __ orl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3202 } else {
3203 DCHECK(instruction->IsXor());
3204 __ xorl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3205 }
3206 } else {
3207 if (instruction->IsAnd()) {
3208 __ andl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
3209 } else if (instruction->IsOr()) {
3210 __ orl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
3211 } else {
3212 DCHECK(instruction->IsXor());
3213 __ xorl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
3214 }
3215 }
3216 } else {
3217 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3218 if (second.IsRegisterPair()) {
3219 if (instruction->IsAnd()) {
3220 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3221 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3222 } else if (instruction->IsOr()) {
3223 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3224 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3225 } else {
3226 DCHECK(instruction->IsXor());
3227 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3228 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3229 }
3230 } else {
3231 if (instruction->IsAnd()) {
3232 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3233 __ andl(first.AsRegisterPairHigh<Register>(),
3234 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3235 } else if (instruction->IsOr()) {
3236 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3237 __ orl(first.AsRegisterPairHigh<Register>(),
3238 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3239 } else {
3240 DCHECK(instruction->IsXor());
3241 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3242 __ xorl(first.AsRegisterPairHigh<Register>(),
3243 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3244 }
3245 }
3246 }
3247}
3248
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003249} // namespace x86
3250} // namespace art