blob: 5b09fc190bb31856332001f8a446be250ffea289 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
19#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010020#include "gc/accounting/card_table.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070021#include "mirror/array-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010022#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010023#include "mirror/class.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010024#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000025#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010026#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000027#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010028#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010031
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032namespace x86 {
33
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010034static constexpr int kNumberOfPushedRegistersAtEntry = 1;
35static constexpr int kCurrentMethodStackOffset = 0;
36
Calin Juravled6fb6cf2014-11-11 19:07:44 +000037static constexpr Register kRuntimeParameterCoreRegisters[] = { EAX, ECX, EDX, EBX };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010038static constexpr size_t kRuntimeParameterCoreRegistersLength =
39 arraysize(kRuntimeParameterCoreRegisters);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010040static constexpr XmmRegister kRuntimeParameterFpuRegisters[] = { };
41static constexpr size_t kRuntimeParameterFpuRegistersLength = 0;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010042
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000043// Marker for places that can be updated once we don't follow the quick ABI.
44static constexpr bool kFollowsQuickABI = true;
45
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010046class InvokeRuntimeCallingConvention : public CallingConvention<Register, XmmRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010047 public:
48 InvokeRuntimeCallingConvention()
49 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010050 kRuntimeParameterCoreRegistersLength,
51 kRuntimeParameterFpuRegisters,
52 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010053
54 private:
55 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
56};
57
Nicolas Geoffraye5038322014-07-04 09:41:32 +010058#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
59
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010060class SlowPathCodeX86 : public SlowPathCode {
61 public:
62 SlowPathCodeX86() : entry_label_(), exit_label_() {}
63
64 Label* GetEntryLabel() { return &entry_label_; }
65 Label* GetExitLabel() { return &exit_label_; }
66
67 private:
68 Label entry_label_;
69 Label exit_label_;
70
71 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86);
72};
73
74class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010075 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010076 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010077
78 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
79 __ Bind(GetEntryLabel());
80 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010081 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010082 }
83
84 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010085 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010086 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
87};
88
Calin Juravled0d48522014-11-04 16:40:20 +000089class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
90 public:
91 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
92
93 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
94 __ Bind(GetEntryLabel());
95 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
96 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
97 }
98
99 private:
100 HDivZeroCheck* const instruction_;
101 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
102};
103
Calin Juravlebacfec32014-11-14 15:54:36 +0000104class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +0000105 public:
Calin Juravlebacfec32014-11-14 15:54:36 +0000106 explicit DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000107
108 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
109 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000110 if (is_div_) {
111 __ negl(reg_);
112 } else {
113 __ movl(reg_, Immediate(0));
114 }
Calin Juravled0d48522014-11-04 16:40:20 +0000115 __ jmp(GetExitLabel());
116 }
117
118 private:
119 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000120 bool is_div_;
121 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000122};
123
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100124class StackOverflowCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100125 public:
126 StackOverflowCheckSlowPathX86() {}
127
128 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
129 __ Bind(GetEntryLabel());
130 __ addl(ESP,
131 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
132 __ fs()->jmp(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowStackOverflow)));
133 }
134
135 private:
136 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86);
137};
138
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100139class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100140 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100141 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
142 Location index_location,
143 Location length_location)
Roland Levillain199f3362014-11-27 17:15:16 +0000144 : instruction_(instruction),
145 index_location_(index_location),
146 length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100147
148 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100149 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100150 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000151 // We're moving two locations to locations that could overlap, so we need a parallel
152 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100153 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000154 x86_codegen->EmitParallelMoves(
155 index_location_,
156 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
157 length_location_,
158 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100159 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100160 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100161 }
162
163 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100164 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100165 const Location index_location_;
166 const Location length_location_;
167
168 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
169};
170
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100171class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000172 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100173 explicit SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
174 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000175
176 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100177 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000178 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100179 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000180 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
181 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100182 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100183 if (successor_ == nullptr) {
184 __ jmp(GetReturnLabel());
185 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100186 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100187 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000188 }
189
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100190 Label* GetReturnLabel() {
191 DCHECK(successor_ == nullptr);
192 return &return_label_;
193 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000194
195 private:
196 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100197 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000198 Label return_label_;
199
200 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
201};
202
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000203class LoadStringSlowPathX86 : public SlowPathCodeX86 {
204 public:
205 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
206
207 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
208 LocationSummary* locations = instruction_->GetLocations();
209 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
210
211 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
212 __ Bind(GetEntryLabel());
213 codegen->SaveLiveRegisters(locations);
214
215 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800216 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
217 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000218 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
219 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
220 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
221 codegen->RestoreLiveRegisters(locations);
222
223 __ jmp(GetExitLabel());
224 }
225
226 private:
227 HLoadString* const instruction_;
228
229 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
230};
231
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000232class LoadClassSlowPathX86 : public SlowPathCodeX86 {
233 public:
234 LoadClassSlowPathX86(HLoadClass* cls,
235 HInstruction* at,
236 uint32_t dex_pc,
237 bool do_clinit)
238 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
239 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
240 }
241
242 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
243 LocationSummary* locations = at_->GetLocations();
244 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
245 __ Bind(GetEntryLabel());
246 codegen->SaveLiveRegisters(locations);
247
248 InvokeRuntimeCallingConvention calling_convention;
249 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
250 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
251 __ fs()->call(Address::Absolute(do_clinit_
252 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
253 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
254 codegen->RecordPcInfo(at_, dex_pc_);
255
256 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000257 Location out = locations->Out();
258 if (out.IsValid()) {
259 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
260 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000261 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000262
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000263 codegen->RestoreLiveRegisters(locations);
264 __ jmp(GetExitLabel());
265 }
266
267 private:
268 // The class this slow path will load.
269 HLoadClass* const cls_;
270
271 // The instruction where this slow path is happening.
272 // (Might be the load class or an initialization check).
273 HInstruction* const at_;
274
275 // The dex PC of `at_`.
276 const uint32_t dex_pc_;
277
278 // Whether to initialize the class.
279 const bool do_clinit_;
280
281 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
282};
283
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000284class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
285 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000286 TypeCheckSlowPathX86(HInstruction* instruction,
287 Location class_to_check,
288 Location object_class,
289 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000290 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000291 class_to_check_(class_to_check),
292 object_class_(object_class),
293 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000294
295 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
296 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000297 DCHECK(instruction_->IsCheckCast()
298 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000299
300 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
301 __ Bind(GetEntryLabel());
302 codegen->SaveLiveRegisters(locations);
303
304 // We're moving two locations to locations that could overlap, so we need a parallel
305 // move resolver.
306 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000307 x86_codegen->EmitParallelMoves(
308 class_to_check_,
309 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
310 object_class_,
311 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000312
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000313 if (instruction_->IsInstanceOf()) {
Roland Levillain199f3362014-11-27 17:15:16 +0000314 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize,
315 pInstanceofNonTrivial)));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000316 } else {
317 DCHECK(instruction_->IsCheckCast());
318 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
319 }
320
321 codegen->RecordPcInfo(instruction_, dex_pc_);
322 if (instruction_->IsInstanceOf()) {
323 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
324 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000325 codegen->RestoreLiveRegisters(locations);
326
327 __ jmp(GetExitLabel());
328 }
329
330 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000331 HInstruction* const instruction_;
332 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000333 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000334 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000335
336 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
337};
338
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100339#undef __
340#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
341
Dave Allison20dfc792014-06-16 20:44:29 -0700342inline Condition X86Condition(IfCondition cond) {
343 switch (cond) {
344 case kCondEQ: return kEqual;
345 case kCondNE: return kNotEqual;
346 case kCondLT: return kLess;
347 case kCondLE: return kLessEqual;
348 case kCondGT: return kGreater;
349 case kCondGE: return kGreaterEqual;
350 default:
351 LOG(FATAL) << "Unknown if condition";
352 }
353 return kEqual;
354}
355
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100356void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
357 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
358}
359
360void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
361 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
362}
363
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100364size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
365 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
366 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100367}
368
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100369size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
370 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
371 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100372}
373
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000374CodeGeneratorX86::CodeGeneratorX86(HGraph* graph, const CompilerOptions& compiler_options)
375 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfXmmRegisters,
376 kNumberOfRegisterPairs, compiler_options),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100377 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100378 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100379 instruction_visitor_(graph, this),
380 move_resolver_(graph->GetArena(), this) {}
381
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100382size_t CodeGeneratorX86::FrameEntrySpillSize() const {
383 return kNumberOfPushedRegistersAtEntry * kX86WordSize;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100384}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100385
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100386Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100387 switch (type) {
388 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100389 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100390 X86ManagedRegister pair =
391 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100392 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
393 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100394 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
395 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100396 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100397 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100398 }
399
400 case Primitive::kPrimByte:
401 case Primitive::kPrimBoolean:
402 case Primitive::kPrimChar:
403 case Primitive::kPrimShort:
404 case Primitive::kPrimInt:
405 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100406 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100407 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100408 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100409 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
410 X86ManagedRegister current =
411 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
412 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100413 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100414 }
415 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100416 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100417 }
418
419 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100420 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100421 return Location::FpuRegisterLocation(
422 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100423 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100424
425 case Primitive::kPrimVoid:
426 LOG(FATAL) << "Unreachable type " << type;
427 }
428
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100429 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100430}
431
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100432void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100433 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100434 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100435
436 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100437 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100438
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000439 // TODO: We currently don't use Quick's callee saved registers.
440 DCHECK(kFollowsQuickABI);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100441 blocked_core_registers_[EBP] = true;
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000442 blocked_core_registers_[ESI] = true;
443 blocked_core_registers_[EDI] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100444
445 UpdateBlockedPairRegisters();
446}
447
448void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
449 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
450 X86ManagedRegister current =
451 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
452 if (blocked_core_registers_[current.AsRegisterPairLow()]
453 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
454 blocked_register_pairs_[i] = true;
455 }
456 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100457}
458
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100459InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
460 : HGraphVisitor(graph),
461 assembler_(codegen->GetAssembler()),
462 codegen_(codegen) {}
463
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000464void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000465 // Create a fake register to mimic Quick.
466 static const int kFakeReturnRegister = 8;
467 core_spill_mask_ |= (1 << kFakeReturnRegister);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000468
Roland Levillain199f3362014-11-27 17:15:16 +0000469 bool skip_overflow_check =
470 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Calin Juravle93edf732015-01-20 20:14:07 +0000471 bool implicitStackOverflowChecks = GetCompilerOptions().GetImplicitStackOverflowChecks();
472
473 if (!skip_overflow_check && implicitStackOverflowChecks) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100474 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100475 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100476 }
477
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100478 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100479 __ subl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100480
Calin Juravle93edf732015-01-20 20:14:07 +0000481 if (!skip_overflow_check && !implicitStackOverflowChecks) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100482 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86();
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100483 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100484
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100485 __ fs()->cmpl(ESP, Address::Absolute(Thread::StackEndOffset<kX86WordSize>()));
486 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100487 }
488
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100489 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000490}
491
492void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100493 __ addl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000494}
495
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100496void CodeGeneratorX86::Bind(HBasicBlock* block) {
497 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000498}
499
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100500void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100501 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000502}
503
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100504Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
505 switch (load->GetType()) {
506 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100507 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100508 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
509 break;
510
511 case Primitive::kPrimInt:
512 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100513 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100514 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100515
516 case Primitive::kPrimBoolean:
517 case Primitive::kPrimByte:
518 case Primitive::kPrimChar:
519 case Primitive::kPrimShort:
520 case Primitive::kPrimVoid:
521 LOG(FATAL) << "Unexpected type " << load->GetType();
522 }
523
524 LOG(FATAL) << "Unreachable";
525 return Location();
526}
527
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100528Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
529 switch (type) {
530 case Primitive::kPrimBoolean:
531 case Primitive::kPrimByte:
532 case Primitive::kPrimChar:
533 case Primitive::kPrimShort:
534 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100535 case Primitive::kPrimFloat:
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100536 case Primitive::kPrimNot: {
537 uint32_t index = gp_index_++;
538 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100539 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100540 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100541 return Location::StackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100542 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100543 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100544
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100545 case Primitive::kPrimLong:
546 case Primitive::kPrimDouble: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100547 uint32_t index = gp_index_;
548 gp_index_ += 2;
549 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100550 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
551 calling_convention.GetRegisterPairAt(index));
552 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100553 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000554 // On X86, the register index and stack index of a quick parameter is the same, since
555 // we are passing floating pointer values in core registers.
556 return Location::QuickParameter(index, index);
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100557 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100558 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100559 }
560 }
561
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100562 case Primitive::kPrimVoid:
563 LOG(FATAL) << "Unexpected parameter type " << type;
564 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100565 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100566 return Location();
567}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100568
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100569void CodeGeneratorX86::Move32(Location destination, Location source) {
570 if (source.Equals(destination)) {
571 return;
572 }
573 if (destination.IsRegister()) {
574 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000575 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100576 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000577 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100578 } else {
579 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000580 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100581 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100582 } else if (destination.IsFpuRegister()) {
583 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000584 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100585 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000586 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100587 } else {
588 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000589 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100590 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100591 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000592 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100593 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000594 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100595 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000596 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100597 } else {
598 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100599 __ pushl(Address(ESP, source.GetStackIndex()));
600 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100601 }
602 }
603}
604
605void CodeGeneratorX86::Move64(Location destination, Location source) {
606 if (source.Equals(destination)) {
607 return;
608 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100609 if (destination.IsRegisterPair()) {
610 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000611 EmitParallelMoves(
612 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
613 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
614 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
615 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100616 } else if (source.IsFpuRegister()) {
617 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100618 } else if (source.IsQuickParameter()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000619 uint16_t register_index = source.GetQuickParameterRegisterIndex();
620 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100621 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000622 EmitParallelMoves(
623 Location::RegisterLocation(calling_convention.GetRegisterAt(register_index)),
624 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
625 Location::StackSlot(
626 calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize()),
627 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100628 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000629 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100630 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100631 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
632 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100633 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
634 }
635 } else if (destination.IsQuickParameter()) {
636 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000637 uint16_t register_index = destination.GetQuickParameterRegisterIndex();
638 uint16_t stack_index = destination.GetQuickParameterStackIndex();
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000639 if (source.IsRegisterPair()) {
640 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100641 } else if (source.IsFpuRegister()) {
642 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100643 } else {
644 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000645 EmitParallelMoves(
646 Location::StackSlot(source.GetStackIndex()),
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000647 Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index)),
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000648 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
649 Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index + 1)));
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000650 __ movl(calling_convention.GetRegisterAt(register_index), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100651 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100652 } else if (destination.IsFpuRegister()) {
653 if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000654 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100655 } else {
656 LOG(FATAL) << "Unimplemented";
657 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100658 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000659 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100660 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000661 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100662 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100663 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100664 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100665 } else if (source.IsQuickParameter()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000666 // No conflict possible, so just do the move.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100667 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000668 uint16_t register_index = source.GetQuickParameterRegisterIndex();
669 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000670 // Just move the low part. The only time a source is a quick parameter is
671 // when moving the parameter to its stack locations. And the (Java) caller
672 // of this method has already done that.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100673 __ movl(Address(ESP, destination.GetStackIndex()),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000674 calling_convention.GetRegisterAt(register_index));
675 DCHECK_EQ(calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100676 static_cast<size_t>(destination.GetHighStackIndex(kX86WordSize)));
677 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000678 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100679 } else {
680 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000681 EmitParallelMoves(
682 Location::StackSlot(source.GetStackIndex()),
683 Location::StackSlot(destination.GetStackIndex()),
684 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
685 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100686 }
687 }
688}
689
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100690void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000691 LocationSummary* locations = instruction->GetLocations();
692 if (locations != nullptr && locations->Out().Equals(location)) {
693 return;
694 }
695
696 if (locations != nullptr && locations->Out().IsConstant()) {
697 HConstant* const_to_move = locations->Out().GetConstant();
698 if (const_to_move->IsIntConstant()) {
699 Immediate imm(const_to_move->AsIntConstant()->GetValue());
700 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000701 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000702 } else if (location.IsStackSlot()) {
703 __ movl(Address(ESP, location.GetStackIndex()), imm);
704 } else {
705 DCHECK(location.IsConstant());
706 DCHECK_EQ(location.GetConstant(), const_to_move);
707 }
708 } else if (const_to_move->IsLongConstant()) {
709 int64_t value = const_to_move->AsLongConstant()->GetValue();
710 if (location.IsRegisterPair()) {
711 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
712 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
713 } else if (location.IsDoubleStackSlot()) {
714 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000715 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
716 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000717 } else {
718 DCHECK(location.IsConstant());
719 DCHECK_EQ(location.GetConstant(), instruction);
720 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100721 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000722 } else if (instruction->IsTemporary()) {
723 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000724 if (temp_location.IsStackSlot()) {
725 Move32(location, temp_location);
726 } else {
727 DCHECK(temp_location.IsDoubleStackSlot());
728 Move64(location, temp_location);
729 }
Roland Levillain476df552014-10-09 17:51:36 +0100730 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100731 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100732 switch (instruction->GetType()) {
733 case Primitive::kPrimBoolean:
734 case Primitive::kPrimByte:
735 case Primitive::kPrimChar:
736 case Primitive::kPrimShort:
737 case Primitive::kPrimInt:
738 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100739 case Primitive::kPrimFloat:
740 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100741 break;
742
743 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100744 case Primitive::kPrimDouble:
745 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100746 break;
747
748 default:
749 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
750 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000751 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100752 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100753 switch (instruction->GetType()) {
754 case Primitive::kPrimBoolean:
755 case Primitive::kPrimByte:
756 case Primitive::kPrimChar:
757 case Primitive::kPrimShort:
758 case Primitive::kPrimInt:
759 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100760 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000761 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100762 break;
763
764 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100765 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000766 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100767 break;
768
769 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100770 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100771 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000772 }
773}
774
775void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000776 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000777}
778
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000779void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000780 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100781 DCHECK(!successor->IsExitBlock());
782
783 HBasicBlock* block = got->GetBlock();
784 HInstruction* previous = got->GetPrevious();
785
786 HLoopInformation* info = block->GetLoopInformation();
787 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
788 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
789 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
790 return;
791 }
792
793 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
794 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
795 }
796 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000797 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000798 }
799}
800
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000801void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000802 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000803}
804
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000805void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700806 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000807 if (kIsDebugBuild) {
808 __ Comment("Unreachable");
809 __ int3();
810 }
811}
812
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000813void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100814 LocationSummary* locations =
815 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100816 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100817 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100818 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100819 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000820}
821
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000822void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700823 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100824 if (cond->IsIntConstant()) {
825 // Constant condition, statically compared against 1.
826 int32_t cond_value = cond->AsIntConstant()->GetValue();
827 if (cond_value == 1) {
828 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
829 if_instr->IfTrueSuccessor())) {
830 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100831 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100832 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100833 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100834 DCHECK_EQ(cond_value, 0);
835 }
836 } else {
837 bool materialized =
838 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
839 // Moves do not affect the eflags register, so if the condition is
840 // evaluated just before the if, we don't need to evaluate it
841 // again.
842 bool eflags_set = cond->IsCondition()
843 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
844 if (materialized) {
845 if (!eflags_set) {
846 // Materialized condition, compare against 0.
847 Location lhs = if_instr->GetLocations()->InAt(0);
848 if (lhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000849 __ cmpl(lhs.AsRegister<Register>(), Immediate(0));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100850 } else {
851 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
852 }
853 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
854 } else {
855 __ j(X86Condition(cond->AsCondition()->GetCondition()),
856 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
857 }
858 } else {
859 Location lhs = cond->GetLocations()->InAt(0);
860 Location rhs = cond->GetLocations()->InAt(1);
861 // LHS is guaranteed to be in a register (see
862 // LocationsBuilderX86::VisitCondition).
863 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000864 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100865 } else if (rhs.IsConstant()) {
866 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
867 Immediate imm(instruction->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000868 __ cmpl(lhs.AsRegister<Register>(), imm);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100869 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000870 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100871 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100872 __ j(X86Condition(cond->AsCondition()->GetCondition()),
873 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700874 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100875 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100876 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
877 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700878 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000879 }
880}
881
882void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000883 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000884}
885
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000886void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
887 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000888}
889
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000890void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100891 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000892}
893
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000894void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100895 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700896 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000897}
898
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100899void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100900 LocationSummary* locations =
901 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100902 switch (store->InputAt(1)->GetType()) {
903 case Primitive::kPrimBoolean:
904 case Primitive::kPrimByte:
905 case Primitive::kPrimChar:
906 case Primitive::kPrimShort:
907 case Primitive::kPrimInt:
908 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100909 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100910 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
911 break;
912
913 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100914 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100915 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
916 break;
917
918 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100919 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100920 }
921 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000922}
923
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000924void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700925 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000926}
927
Dave Allison20dfc792014-06-16 20:44:29 -0700928void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100929 LocationSummary* locations =
930 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100931 locations->SetInAt(0, Location::RequiresRegister());
932 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100933 if (comp->NeedsMaterialization()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000934 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100935 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000936}
937
Dave Allison20dfc792014-06-16 20:44:29 -0700938void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
939 if (comp->NeedsMaterialization()) {
940 LocationSummary* locations = comp->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +0000941 Register reg = locations->Out().AsRegister<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100942 // Clear register: setcc only sets the low byte.
943 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700944 if (locations->InAt(1).IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000945 __ cmpl(locations->InAt(0).AsRegister<Register>(),
946 locations->InAt(1).AsRegister<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100947 } else if (locations->InAt(1).IsConstant()) {
948 HConstant* instruction = locations->InAt(1).GetConstant();
949 Immediate imm(instruction->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000950 __ cmpl(locations->InAt(0).AsRegister<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700951 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000952 __ cmpl(locations->InAt(0).AsRegister<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700953 Address(ESP, locations->InAt(1).GetStackIndex()));
954 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000955 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100956 }
Dave Allison20dfc792014-06-16 20:44:29 -0700957}
958
959void LocationsBuilderX86::VisitEqual(HEqual* comp) {
960 VisitCondition(comp);
961}
962
963void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
964 VisitCondition(comp);
965}
966
967void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
968 VisitCondition(comp);
969}
970
971void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
972 VisitCondition(comp);
973}
974
975void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
976 VisitCondition(comp);
977}
978
979void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
980 VisitCondition(comp);
981}
982
983void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
984 VisitCondition(comp);
985}
986
987void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
988 VisitCondition(comp);
989}
990
991void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
992 VisitCondition(comp);
993}
994
995void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
996 VisitCondition(comp);
997}
998
999void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1000 VisitCondition(comp);
1001}
1002
1003void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1004 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001005}
1006
1007void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001008 LocationSummary* locations =
1009 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001010 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001011}
1012
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001013void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001014 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001015 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001016}
1017
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001018void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001019 LocationSummary* locations =
1020 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001021 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001022}
1023
1024void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1025 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001026 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001027}
1028
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001029void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1030 LocationSummary* locations =
1031 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1032 locations->SetOut(Location::ConstantLocation(constant));
1033}
1034
1035void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1036 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001037 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001038}
1039
1040void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1041 LocationSummary* locations =
1042 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1043 locations->SetOut(Location::ConstantLocation(constant));
1044}
1045
1046void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1047 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001048 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001049}
1050
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001051void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001052 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001053}
1054
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001055void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001056 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001057 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001058 __ ret();
1059}
1060
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001061void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001062 LocationSummary* locations =
1063 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001064 switch (ret->InputAt(0)->GetType()) {
1065 case Primitive::kPrimBoolean:
1066 case Primitive::kPrimByte:
1067 case Primitive::kPrimChar:
1068 case Primitive::kPrimShort:
1069 case Primitive::kPrimInt:
1070 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001071 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001072 break;
1073
1074 case Primitive::kPrimLong:
1075 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001076 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001077 break;
1078
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001079 case Primitive::kPrimFloat:
1080 case Primitive::kPrimDouble:
1081 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001082 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001083 break;
1084
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001085 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001086 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001087 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001088}
1089
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001090void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001091 if (kIsDebugBuild) {
1092 switch (ret->InputAt(0)->GetType()) {
1093 case Primitive::kPrimBoolean:
1094 case Primitive::kPrimByte:
1095 case Primitive::kPrimChar:
1096 case Primitive::kPrimShort:
1097 case Primitive::kPrimInt:
1098 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001099 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001100 break;
1101
1102 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001103 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1104 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001105 break;
1106
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001107 case Primitive::kPrimFloat:
1108 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001109 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001110 break;
1111
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001112 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001113 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001114 }
1115 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001116 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001117 __ ret();
1118}
1119
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001120void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001121 HandleInvoke(invoke);
1122}
1123
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001124void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001125 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001126
1127 // TODO: Implement all kinds of calls:
1128 // 1) boot -> boot
1129 // 2) app -> boot
1130 // 3) app -> app
1131 //
1132 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1133
1134 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001135 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffray4e44c822014-12-17 12:25:12 +00001136 // temp = temp->dex_cache_resolved_methods_;
1137 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
1138 // temp = temp[index_in_cache]
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001139 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetDexMethodIndex())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001140 // (temp + offset_of_quick_compiled_code)()
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001141 __ call(Address(
1142 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001143
1144 DCHECK(!codegen_->IsLeafMethod());
1145 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1146}
1147
1148void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1149 HandleInvoke(invoke);
1150}
1151
1152void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001153 LocationSummary* locations =
1154 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001155 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001156
1157 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001158 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001159 HInstruction* input = invoke->InputAt(i);
1160 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1161 }
1162
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001163 switch (invoke->GetType()) {
1164 case Primitive::kPrimBoolean:
1165 case Primitive::kPrimByte:
1166 case Primitive::kPrimChar:
1167 case Primitive::kPrimShort:
1168 case Primitive::kPrimInt:
1169 case Primitive::kPrimNot:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001170 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001171 break;
1172
1173 case Primitive::kPrimLong:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001174 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001175 break;
1176
1177 case Primitive::kPrimVoid:
1178 break;
1179
1180 case Primitive::kPrimDouble:
1181 case Primitive::kPrimFloat:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001182 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001183 break;
1184 }
1185
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001186 invoke->SetLocations(locations);
1187}
1188
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001189void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001190 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001191 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1192 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1193 LocationSummary* locations = invoke->GetLocations();
1194 Location receiver = locations->InAt(0);
1195 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1196 // temp = object->GetClass();
1197 if (receiver.IsStackSlot()) {
1198 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1199 __ movl(temp, Address(temp, class_offset));
1200 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001201 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001202 }
1203 // temp = temp->GetMethodAt(method_offset);
1204 __ movl(temp, Address(temp, method_offset));
1205 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001206 __ call(Address(
1207 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001208
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001209 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001210 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001211}
1212
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001213void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1214 HandleInvoke(invoke);
1215 // Add the hidden argument.
1216 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM0));
1217}
1218
1219void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1220 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001221 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001222 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1223 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1224 LocationSummary* locations = invoke->GetLocations();
1225 Location receiver = locations->InAt(0);
1226 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1227
1228 // Set the hidden argument.
1229 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001230 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001231
1232 // temp = object->GetClass();
1233 if (receiver.IsStackSlot()) {
1234 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1235 __ movl(temp, Address(temp, class_offset));
1236 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001237 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001238 }
1239 // temp = temp->GetImtEntryAt(method_offset);
1240 __ movl(temp, Address(temp, method_offset));
1241 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001242 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001243 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001244
1245 DCHECK(!codegen_->IsLeafMethod());
1246 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1247}
1248
Roland Levillain88cb1752014-10-20 16:36:47 +01001249void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1250 LocationSummary* locations =
1251 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1252 switch (neg->GetResultType()) {
1253 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001254 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001255 locations->SetInAt(0, Location::RequiresRegister());
1256 locations->SetOut(Location::SameAsFirstInput());
1257 break;
1258
Roland Levillain88cb1752014-10-20 16:36:47 +01001259 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001260 locations->SetInAt(0, Location::RequiresFpuRegister());
1261 locations->SetOut(Location::SameAsFirstInput());
1262 locations->AddTemp(Location::RequiresRegister());
1263 locations->AddTemp(Location::RequiresFpuRegister());
1264 break;
1265
Roland Levillain88cb1752014-10-20 16:36:47 +01001266 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001267 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001268 locations->SetOut(Location::SameAsFirstInput());
1269 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001270 break;
1271
1272 default:
1273 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1274 }
1275}
1276
1277void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1278 LocationSummary* locations = neg->GetLocations();
1279 Location out = locations->Out();
1280 Location in = locations->InAt(0);
1281 switch (neg->GetResultType()) {
1282 case Primitive::kPrimInt:
1283 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001284 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001285 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001286 break;
1287
1288 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001289 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001290 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001291 __ negl(out.AsRegisterPairLow<Register>());
1292 // Negation is similar to subtraction from zero. The least
1293 // significant byte triggers a borrow when it is different from
1294 // zero; to take it into account, add 1 to the most significant
1295 // byte if the carry flag (CF) is set to 1 after the first NEGL
1296 // operation.
1297 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1298 __ negl(out.AsRegisterPairHigh<Register>());
1299 break;
1300
Roland Levillain5368c212014-11-27 15:03:41 +00001301 case Primitive::kPrimFloat: {
1302 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001303 Register constant = locations->GetTemp(0).AsRegister<Register>();
1304 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001305 // Implement float negation with an exclusive or with value
1306 // 0x80000000 (mask for bit 31, representing the sign of a
1307 // single-precision floating-point number).
1308 __ movl(constant, Immediate(INT32_C(0x80000000)));
1309 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001310 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001311 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001312 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001313
Roland Levillain5368c212014-11-27 15:03:41 +00001314 case Primitive::kPrimDouble: {
1315 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001316 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001317 // Implement double negation with an exclusive or with value
1318 // 0x8000000000000000 (mask for bit 63, representing the sign of
1319 // a double-precision floating-point number).
1320 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001321 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001322 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001323 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001324
1325 default:
1326 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1327 }
1328}
1329
Roland Levillaindff1f282014-11-05 14:15:05 +00001330void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001331 Primitive::Type result_type = conversion->GetResultType();
1332 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001333 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001334
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001335 // The float-to-long and double-to-long type conversions rely on a
1336 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001337 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001338 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1339 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001340 ? LocationSummary::kCall
1341 : LocationSummary::kNoCall;
1342 LocationSummary* locations =
1343 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1344
Roland Levillaindff1f282014-11-05 14:15:05 +00001345 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001346 case Primitive::kPrimByte:
1347 switch (input_type) {
1348 case Primitive::kPrimShort:
1349 case Primitive::kPrimInt:
1350 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001351 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001352 locations->SetInAt(0, Location::Any());
Roland Levillain51d3fc42014-11-13 14:11:42 +00001353 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1354 break;
1355
1356 default:
1357 LOG(FATAL) << "Unexpected type conversion from " << input_type
1358 << " to " << result_type;
1359 }
1360 break;
1361
Roland Levillain01a8d712014-11-14 16:27:39 +00001362 case Primitive::kPrimShort:
1363 switch (input_type) {
1364 case Primitive::kPrimByte:
1365 case Primitive::kPrimInt:
1366 case Primitive::kPrimChar:
1367 // Processing a Dex `int-to-short' instruction.
1368 locations->SetInAt(0, Location::Any());
1369 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1370 break;
1371
1372 default:
1373 LOG(FATAL) << "Unexpected type conversion from " << input_type
1374 << " to " << result_type;
1375 }
1376 break;
1377
Roland Levillain946e1432014-11-11 17:35:19 +00001378 case Primitive::kPrimInt:
1379 switch (input_type) {
1380 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001381 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001382 locations->SetInAt(0, Location::Any());
1383 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1384 break;
1385
1386 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001387 // Processing a Dex `float-to-int' instruction.
1388 locations->SetInAt(0, Location::RequiresFpuRegister());
1389 locations->SetOut(Location::RequiresRegister());
1390 locations->AddTemp(Location::RequiresFpuRegister());
1391 break;
1392
Roland Levillain946e1432014-11-11 17:35:19 +00001393 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001394 // Processing a Dex `double-to-int' instruction.
1395 locations->SetInAt(0, Location::RequiresFpuRegister());
1396 locations->SetOut(Location::RequiresRegister());
1397 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001398 break;
1399
1400 default:
1401 LOG(FATAL) << "Unexpected type conversion from " << input_type
1402 << " to " << result_type;
1403 }
1404 break;
1405
Roland Levillaindff1f282014-11-05 14:15:05 +00001406 case Primitive::kPrimLong:
1407 switch (input_type) {
1408 case Primitive::kPrimByte:
1409 case Primitive::kPrimShort:
1410 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001411 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001412 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001413 locations->SetInAt(0, Location::RegisterLocation(EAX));
1414 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1415 break;
1416
Roland Levillain624279f2014-12-04 11:54:28 +00001417 case Primitive::kPrimFloat: {
1418 // Processing a Dex `float-to-long' instruction.
1419 InvokeRuntimeCallingConvention calling_convention;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001420 // Note that on x86 floating-point parameters are passed
1421 // through core registers (here, EAX).
1422 locations->SetInAt(0, Location::RegisterLocation(
1423 calling_convention.GetRegisterAt(0)));
Roland Levillain624279f2014-12-04 11:54:28 +00001424 // The runtime helper puts the result in EAX, EDX.
1425 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1426 break;
1427 }
1428
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001429 case Primitive::kPrimDouble: {
1430 // Processing a Dex `double-to-long' instruction.
1431 InvokeRuntimeCallingConvention calling_convention;
1432 // Note that on x86 floating-point parameters are passed
1433 // through core registers (here, EAX and ECX).
1434 locations->SetInAt(0, Location::RegisterPairLocation(
1435 calling_convention.GetRegisterAt(0),
1436 calling_convention.GetRegisterAt(1)));
1437 // The runtime helper puts the result in EAX, EDX.
1438 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1439 break;
1440 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001441 break;
1442
1443 default:
1444 LOG(FATAL) << "Unexpected type conversion from " << input_type
1445 << " to " << result_type;
1446 }
1447 break;
1448
Roland Levillain981e4542014-11-14 11:47:14 +00001449 case Primitive::kPrimChar:
1450 switch (input_type) {
1451 case Primitive::kPrimByte:
1452 case Primitive::kPrimShort:
1453 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001454 // Processing a Dex `int-to-char' instruction.
1455 locations->SetInAt(0, Location::Any());
1456 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1457 break;
1458
1459 default:
1460 LOG(FATAL) << "Unexpected type conversion from " << input_type
1461 << " to " << result_type;
1462 }
1463 break;
1464
Roland Levillaindff1f282014-11-05 14:15:05 +00001465 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001466 switch (input_type) {
1467 case Primitive::kPrimByte:
1468 case Primitive::kPrimShort:
1469 case Primitive::kPrimInt:
1470 case Primitive::kPrimChar:
1471 // Processing a Dex `int-to-float' instruction.
1472 locations->SetInAt(0, Location::RequiresRegister());
1473 locations->SetOut(Location::RequiresFpuRegister());
1474 break;
1475
1476 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001477 // Processing a Dex `long-to-float' instruction.
1478 locations->SetInAt(0, Location::RequiresRegister());
1479 locations->SetOut(Location::RequiresFpuRegister());
1480 locations->AddTemp(Location::RequiresFpuRegister());
1481 locations->AddTemp(Location::RequiresFpuRegister());
1482 break;
1483
Roland Levillaincff13742014-11-17 14:32:17 +00001484 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001485 // Processing a Dex `double-to-float' instruction.
1486 locations->SetInAt(0, Location::RequiresFpuRegister());
1487 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001488 break;
1489
1490 default:
1491 LOG(FATAL) << "Unexpected type conversion from " << input_type
1492 << " to " << result_type;
1493 };
1494 break;
1495
Roland Levillaindff1f282014-11-05 14:15:05 +00001496 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001497 switch (input_type) {
1498 case Primitive::kPrimByte:
1499 case Primitive::kPrimShort:
1500 case Primitive::kPrimInt:
1501 case Primitive::kPrimChar:
1502 // Processing a Dex `int-to-double' instruction.
1503 locations->SetInAt(0, Location::RequiresRegister());
1504 locations->SetOut(Location::RequiresFpuRegister());
1505 break;
1506
1507 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001508 // Processing a Dex `long-to-double' instruction.
1509 locations->SetInAt(0, Location::RequiresRegister());
1510 locations->SetOut(Location::RequiresFpuRegister());
1511 locations->AddTemp(Location::RequiresFpuRegister());
1512 locations->AddTemp(Location::RequiresFpuRegister());
1513 break;
1514
Roland Levillaincff13742014-11-17 14:32:17 +00001515 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001516 // Processing a Dex `float-to-double' instruction.
1517 locations->SetInAt(0, Location::RequiresFpuRegister());
1518 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001519 break;
1520
1521 default:
1522 LOG(FATAL) << "Unexpected type conversion from " << input_type
1523 << " to " << result_type;
1524 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001525 break;
1526
1527 default:
1528 LOG(FATAL) << "Unexpected type conversion from " << input_type
1529 << " to " << result_type;
1530 }
1531}
1532
1533void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1534 LocationSummary* locations = conversion->GetLocations();
1535 Location out = locations->Out();
1536 Location in = locations->InAt(0);
1537 Primitive::Type result_type = conversion->GetResultType();
1538 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001539 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001540 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001541 case Primitive::kPrimByte:
1542 switch (input_type) {
1543 case Primitive::kPrimShort:
1544 case Primitive::kPrimInt:
1545 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001546 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001547 if (in.IsRegister()) {
1548 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
1549 } else if (in.IsStackSlot()) {
1550 __ movsxb(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
1551 } else {
1552 DCHECK(in.GetConstant()->IsIntConstant());
1553 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1554 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
1555 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00001556 break;
1557
1558 default:
1559 LOG(FATAL) << "Unexpected type conversion from " << input_type
1560 << " to " << result_type;
1561 }
1562 break;
1563
Roland Levillain01a8d712014-11-14 16:27:39 +00001564 case Primitive::kPrimShort:
1565 switch (input_type) {
1566 case Primitive::kPrimByte:
1567 case Primitive::kPrimInt:
1568 case Primitive::kPrimChar:
1569 // Processing a Dex `int-to-short' instruction.
1570 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001571 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00001572 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001573 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00001574 } else {
1575 DCHECK(in.GetConstant()->IsIntConstant());
1576 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001577 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00001578 }
1579 break;
1580
1581 default:
1582 LOG(FATAL) << "Unexpected type conversion from " << input_type
1583 << " to " << result_type;
1584 }
1585 break;
1586
Roland Levillain946e1432014-11-11 17:35:19 +00001587 case Primitive::kPrimInt:
1588 switch (input_type) {
1589 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001590 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001591 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001592 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00001593 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001594 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00001595 } else {
1596 DCHECK(in.IsConstant());
1597 DCHECK(in.GetConstant()->IsLongConstant());
1598 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001599 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00001600 }
1601 break;
1602
Roland Levillain3f8f9362014-12-02 17:45:01 +00001603 case Primitive::kPrimFloat: {
1604 // Processing a Dex `float-to-int' instruction.
1605 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1606 Register output = out.AsRegister<Register>();
1607 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1608 Label done, nan;
1609
1610 __ movl(output, Immediate(kPrimIntMax));
1611 // temp = int-to-float(output)
1612 __ cvtsi2ss(temp, output);
1613 // if input >= temp goto done
1614 __ comiss(input, temp);
1615 __ j(kAboveEqual, &done);
1616 // if input == NaN goto nan
1617 __ j(kUnordered, &nan);
1618 // output = float-to-int-truncate(input)
1619 __ cvttss2si(output, input);
1620 __ jmp(&done);
1621 __ Bind(&nan);
1622 // output = 0
1623 __ xorl(output, output);
1624 __ Bind(&done);
1625 break;
1626 }
1627
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001628 case Primitive::kPrimDouble: {
1629 // Processing a Dex `double-to-int' instruction.
1630 XmmRegister input = in.AsFpuRegister<XmmRegister>();
1631 Register output = out.AsRegister<Register>();
1632 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1633 Label done, nan;
1634
1635 __ movl(output, Immediate(kPrimIntMax));
1636 // temp = int-to-double(output)
1637 __ cvtsi2sd(temp, output);
1638 // if input >= temp goto done
1639 __ comisd(input, temp);
1640 __ j(kAboveEqual, &done);
1641 // if input == NaN goto nan
1642 __ j(kUnordered, &nan);
1643 // output = double-to-int-truncate(input)
1644 __ cvttsd2si(output, input);
1645 __ jmp(&done);
1646 __ Bind(&nan);
1647 // output = 0
1648 __ xorl(output, output);
1649 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00001650 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001651 }
Roland Levillain946e1432014-11-11 17:35:19 +00001652
1653 default:
1654 LOG(FATAL) << "Unexpected type conversion from " << input_type
1655 << " to " << result_type;
1656 }
1657 break;
1658
Roland Levillaindff1f282014-11-05 14:15:05 +00001659 case Primitive::kPrimLong:
1660 switch (input_type) {
1661 case Primitive::kPrimByte:
1662 case Primitive::kPrimShort:
1663 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001664 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001665 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001666 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1667 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001668 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00001669 __ cdq();
1670 break;
1671
1672 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00001673 // Processing a Dex `float-to-long' instruction.
1674 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pF2l)));
Roland Levillain624279f2014-12-04 11:54:28 +00001675 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
1676 break;
1677
Roland Levillaindff1f282014-11-05 14:15:05 +00001678 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001679 // Processing a Dex `double-to-long' instruction.
1680 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pD2l)));
1681 codegen_->RecordPcInfo(conversion, conversion->GetDexPc());
Roland Levillaindff1f282014-11-05 14:15:05 +00001682 break;
1683
1684 default:
1685 LOG(FATAL) << "Unexpected type conversion from " << input_type
1686 << " to " << result_type;
1687 }
1688 break;
1689
Roland Levillain981e4542014-11-14 11:47:14 +00001690 case Primitive::kPrimChar:
1691 switch (input_type) {
1692 case Primitive::kPrimByte:
1693 case Primitive::kPrimShort:
1694 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001695 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1696 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001697 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00001698 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001699 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00001700 } else {
1701 DCHECK(in.GetConstant()->IsIntConstant());
1702 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001703 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00001704 }
1705 break;
1706
1707 default:
1708 LOG(FATAL) << "Unexpected type conversion from " << input_type
1709 << " to " << result_type;
1710 }
1711 break;
1712
Roland Levillaindff1f282014-11-05 14:15:05 +00001713 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001714 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001715 case Primitive::kPrimByte:
1716 case Primitive::kPrimShort:
1717 case Primitive::kPrimInt:
1718 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001719 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001720 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001721 break;
1722
Roland Levillain6d0e4832014-11-27 18:31:21 +00001723 case Primitive::kPrimLong: {
1724 // Processing a Dex `long-to-float' instruction.
1725 Register low = in.AsRegisterPairLow<Register>();
1726 Register high = in.AsRegisterPairHigh<Register>();
1727 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1728 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1729 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1730
1731 // Operations use doubles for precision reasons (each 32-bit
1732 // half of a long fits in the 53-bit mantissa of a double,
1733 // but not in the 24-bit mantissa of a float). This is
1734 // especially important for the low bits. The result is
1735 // eventually converted to float.
1736
1737 // low = low - 2^31 (to prevent bit 31 of `low` to be
1738 // interpreted as a sign bit)
1739 __ subl(low, Immediate(0x80000000));
1740 // temp = int-to-double(high)
1741 __ cvtsi2sd(temp, high);
1742 // temp = temp * 2^32
1743 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
1744 __ mulsd(temp, constant);
1745 // result = int-to-double(low)
1746 __ cvtsi2sd(result, low);
1747 // result = result + 2^31 (restore the original value of `low`)
1748 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
1749 __ addsd(result, constant);
1750 // result = result + temp
1751 __ addsd(result, temp);
1752 // result = double-to-float(result)
1753 __ cvtsd2ss(result, result);
1754 break;
1755 }
1756
Roland Levillaincff13742014-11-17 14:32:17 +00001757 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001758 // Processing a Dex `double-to-float' instruction.
1759 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001760 break;
1761
1762 default:
1763 LOG(FATAL) << "Unexpected type conversion from " << input_type
1764 << " to " << result_type;
1765 };
1766 break;
1767
Roland Levillaindff1f282014-11-05 14:15:05 +00001768 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001769 switch (input_type) {
Roland Levillaincff13742014-11-17 14:32:17 +00001770 case Primitive::kPrimByte:
1771 case Primitive::kPrimShort:
1772 case Primitive::kPrimInt:
1773 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001774 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001775 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00001776 break;
1777
Roland Levillain647b9ed2014-11-27 12:06:00 +00001778 case Primitive::kPrimLong: {
1779 // Processing a Dex `long-to-double' instruction.
1780 Register low = in.AsRegisterPairLow<Register>();
1781 Register high = in.AsRegisterPairHigh<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001782 XmmRegister result = out.AsFpuRegister<XmmRegister>();
1783 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1784 XmmRegister constant = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00001785
Roland Levillain647b9ed2014-11-27 12:06:00 +00001786 // low = low - 2^31 (to prevent bit 31 of `low` to be
1787 // interpreted as a sign bit)
1788 __ subl(low, Immediate(0x80000000));
1789 // temp = int-to-double(high)
1790 __ cvtsi2sd(temp, high);
1791 // temp = temp * 2^32
Roland Levillain6d0e4832014-11-27 18:31:21 +00001792 __ LoadLongConstant(constant, k2Pow32EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001793 __ mulsd(temp, constant);
1794 // result = int-to-double(low)
1795 __ cvtsi2sd(result, low);
1796 // result = result + 2^31 (restore the original value of `low`)
Roland Levillain6d0e4832014-11-27 18:31:21 +00001797 __ LoadLongConstant(constant, k2Pow31EncodingForDouble);
Roland Levillain647b9ed2014-11-27 12:06:00 +00001798 __ addsd(result, constant);
1799 // result = result + temp
1800 __ addsd(result, temp);
1801 break;
1802 }
1803
Roland Levillaincff13742014-11-17 14:32:17 +00001804 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001805 // Processing a Dex `float-to-double' instruction.
1806 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00001807 break;
1808
1809 default:
1810 LOG(FATAL) << "Unexpected type conversion from " << input_type
1811 << " to " << result_type;
1812 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001813 break;
1814
1815 default:
1816 LOG(FATAL) << "Unexpected type conversion from " << input_type
1817 << " to " << result_type;
1818 }
1819}
1820
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001821void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001822 LocationSummary* locations =
1823 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001824 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001825 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001826 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001827 locations->SetInAt(0, Location::RequiresRegister());
1828 locations->SetInAt(1, Location::Any());
1829 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001830 break;
1831 }
1832
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001833 case Primitive::kPrimFloat:
1834 case Primitive::kPrimDouble: {
1835 locations->SetInAt(0, Location::RequiresFpuRegister());
1836 locations->SetInAt(1, Location::Any());
1837 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001838 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001839 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001840
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001841 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001842 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1843 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001844 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001845}
1846
1847void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1848 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001849 Location first = locations->InAt(0);
1850 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001851 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001852 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001853 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001854 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001855 __ addl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001856 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001857 __ addl(first.AsRegister<Register>(),
1858 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001859 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001860 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001861 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001862 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001863 }
1864
1865 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001866 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001867 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1868 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001869 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001870 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1871 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001872 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001873 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001874 break;
1875 }
1876
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001877 case Primitive::kPrimFloat: {
1878 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001879 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001880 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001881 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001882 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001883 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001884 }
1885
1886 case Primitive::kPrimDouble: {
1887 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001888 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001889 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001890 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001891 }
1892 break;
1893 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001894
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001895 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001896 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001897 }
1898}
1899
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001900void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001901 LocationSummary* locations =
1902 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001903 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001904 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001905 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001906 locations->SetInAt(0, Location::RequiresRegister());
1907 locations->SetInAt(1, Location::Any());
1908 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001909 break;
1910 }
Calin Juravle11351682014-10-23 15:38:15 +01001911 case Primitive::kPrimFloat:
1912 case Primitive::kPrimDouble: {
1913 locations->SetInAt(0, Location::RequiresFpuRegister());
1914 locations->SetInAt(1, Location::RequiresFpuRegister());
1915 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001916 break;
Calin Juravle11351682014-10-23 15:38:15 +01001917 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001918
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001919 default:
Calin Juravle11351682014-10-23 15:38:15 +01001920 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001921 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001922}
1923
1924void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1925 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001926 Location first = locations->InAt(0);
1927 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001928 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001929 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001930 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001931 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001932 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001933 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00001934 __ subl(first.AsRegister<Register>(),
1935 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001936 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001937 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001938 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001939 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001940 }
1941
1942 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001943 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01001944 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1945 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001946 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001947 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001948 __ sbbl(first.AsRegisterPairHigh<Register>(),
1949 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001950 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001951 break;
1952 }
1953
Calin Juravle11351682014-10-23 15:38:15 +01001954 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001955 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001956 break;
Calin Juravle11351682014-10-23 15:38:15 +01001957 }
1958
1959 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001960 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01001961 break;
1962 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001963
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001964 default:
Calin Juravle11351682014-10-23 15:38:15 +01001965 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001966 }
1967}
1968
Calin Juravle34bacdf2014-10-07 20:23:36 +01001969void LocationsBuilderX86::VisitMul(HMul* mul) {
1970 LocationSummary* locations =
1971 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1972 switch (mul->GetResultType()) {
1973 case Primitive::kPrimInt:
1974 locations->SetInAt(0, Location::RequiresRegister());
1975 locations->SetInAt(1, Location::Any());
1976 locations->SetOut(Location::SameAsFirstInput());
1977 break;
1978 case Primitive::kPrimLong: {
1979 locations->SetInAt(0, Location::RequiresRegister());
1980 // TODO: Currently this handles only stack operands:
1981 // - we don't have enough registers because we currently use Quick ABI.
1982 // - by the time we have a working register allocator we will probably change the ABI
1983 // and fix the above.
1984 // - we don't have a way yet to request operands on stack but the base line compiler
1985 // will leave the operands on the stack with Any().
1986 locations->SetInAt(1, Location::Any());
1987 locations->SetOut(Location::SameAsFirstInput());
1988 // Needed for imul on 32bits with 64bits output.
1989 locations->AddTemp(Location::RegisterLocation(EAX));
1990 locations->AddTemp(Location::RegisterLocation(EDX));
1991 break;
1992 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001993 case Primitive::kPrimFloat:
1994 case Primitive::kPrimDouble: {
1995 locations->SetInAt(0, Location::RequiresFpuRegister());
1996 locations->SetInAt(1, Location::RequiresFpuRegister());
1997 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001998 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001999 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002000
2001 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002002 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002003 }
2004}
2005
2006void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2007 LocationSummary* locations = mul->GetLocations();
2008 Location first = locations->InAt(0);
2009 Location second = locations->InAt(1);
2010 DCHECK(first.Equals(locations->Out()));
2011
2012 switch (mul->GetResultType()) {
2013 case Primitive::kPrimInt: {
2014 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002015 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002016 } else if (second.IsConstant()) {
2017 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002018 __ imull(first.AsRegister<Register>(), imm);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002019 } else {
2020 DCHECK(second.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002021 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002022 }
2023 break;
2024 }
2025
2026 case Primitive::kPrimLong: {
2027 DCHECK(second.IsDoubleStackSlot());
2028
2029 Register in1_hi = first.AsRegisterPairHigh<Register>();
2030 Register in1_lo = first.AsRegisterPairLow<Register>();
2031 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2032 Address in2_lo(ESP, second.GetStackIndex());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002033 Register eax = locations->GetTemp(0).AsRegister<Register>();
2034 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002035
2036 DCHECK_EQ(EAX, eax);
2037 DCHECK_EQ(EDX, edx);
2038
2039 // input: in1 - 64 bits, in2 - 64 bits
2040 // output: in1
2041 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2042 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2043 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
2044
2045 __ movl(eax, in2_hi);
2046 // eax <- in1.lo * in2.hi
2047 __ imull(eax, in1_lo);
2048 // in1.hi <- in1.hi * in2.lo
2049 __ imull(in1_hi, in2_lo);
2050 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2051 __ addl(in1_hi, eax);
2052 // move in1_lo to eax to prepare for double precision
2053 __ movl(eax, in1_lo);
2054 // edx:eax <- in1.lo * in2.lo
2055 __ mull(in2_lo);
2056 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2057 __ addl(in1_hi, edx);
2058 // in1.lo <- (in1.lo * in2.lo)[31:0];
2059 __ movl(in1_lo, eax);
2060
2061 break;
2062 }
2063
Calin Juravleb5bfa962014-10-21 18:02:24 +01002064 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002065 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002066 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002067 }
2068
2069 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002070 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002071 break;
2072 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002073
2074 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002075 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002076 }
2077}
2078
Calin Juravlebacfec32014-11-14 15:54:36 +00002079void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2080 DCHECK(instruction->IsDiv() || instruction->IsRem());
2081
2082 LocationSummary* locations = instruction->GetLocations();
2083 Location out = locations->Out();
2084 Location first = locations->InAt(0);
2085 Location second = locations->InAt(1);
2086 bool is_div = instruction->IsDiv();
2087
2088 switch (instruction->GetResultType()) {
2089 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002090 Register second_reg = second.AsRegister<Register>();
2091 DCHECK_EQ(EAX, first.AsRegister<Register>());
2092 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002093
2094 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002095 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
2096 is_div);
Calin Juravlebacfec32014-11-14 15:54:36 +00002097 codegen_->AddSlowPath(slow_path);
2098
2099 // 0x80000000/-1 triggers an arithmetic exception!
2100 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2101 // it's safe to just use negl instead of more complex comparisons.
2102
2103 __ cmpl(second_reg, Immediate(-1));
2104 __ j(kEqual, slow_path->GetEntryLabel());
2105
2106 // edx:eax <- sign-extended of eax
2107 __ cdq();
2108 // eax = quotient, edx = remainder
2109 __ idivl(second_reg);
2110
2111 __ Bind(slow_path->GetExitLabel());
2112 break;
2113 }
2114
2115 case Primitive::kPrimLong: {
2116 InvokeRuntimeCallingConvention calling_convention;
2117 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2118 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2119 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2120 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2121 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2122 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2123
2124 if (is_div) {
2125 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
2126 } else {
2127 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
2128 }
2129 uint32_t dex_pc = is_div
2130 ? instruction->AsDiv()->GetDexPc()
2131 : instruction->AsRem()->GetDexPc();
2132 codegen_->RecordPcInfo(instruction, dex_pc);
2133
2134 break;
2135 }
2136
2137 default:
2138 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2139 }
2140}
2141
Calin Juravle7c4954d2014-10-28 16:57:40 +00002142void LocationsBuilderX86::VisitDiv(HDiv* div) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002143 LocationSummary::CallKind call_kind = div->GetResultType() == Primitive::kPrimLong
2144 ? LocationSummary::kCall
2145 : LocationSummary::kNoCall;
2146 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2147
Calin Juravle7c4954d2014-10-28 16:57:40 +00002148 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002149 case Primitive::kPrimInt: {
2150 locations->SetInAt(0, Location::RegisterLocation(EAX));
2151 locations->SetInAt(1, Location::RequiresRegister());
2152 locations->SetOut(Location::SameAsFirstInput());
2153 // Intel uses edx:eax as the dividend.
2154 locations->AddTemp(Location::RegisterLocation(EDX));
2155 break;
2156 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002157 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002158 InvokeRuntimeCallingConvention calling_convention;
2159 locations->SetInAt(0, Location::RegisterPairLocation(
2160 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2161 locations->SetInAt(1, Location::RegisterPairLocation(
2162 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2163 // Runtime helper puts the result in EAX, EDX.
2164 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002165 break;
2166 }
2167 case Primitive::kPrimFloat:
2168 case Primitive::kPrimDouble: {
2169 locations->SetInAt(0, Location::RequiresFpuRegister());
2170 locations->SetInAt(1, Location::RequiresFpuRegister());
2171 locations->SetOut(Location::SameAsFirstInput());
2172 break;
2173 }
2174
2175 default:
2176 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2177 }
2178}
2179
2180void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2181 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002182 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002183 Location first = locations->InAt(0);
2184 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002185
2186 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002187 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002188 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002189 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002190 break;
2191 }
2192
2193 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002194 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002195 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002196 break;
2197 }
2198
2199 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002200 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002201 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002202 break;
2203 }
2204
2205 default:
2206 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2207 }
2208}
2209
Calin Juravlebacfec32014-11-14 15:54:36 +00002210void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002211 Primitive::Type type = rem->GetResultType();
2212 LocationSummary::CallKind call_kind = type == Primitive::kPrimInt
2213 ? LocationSummary::kNoCall
2214 : LocationSummary::kCall;
Calin Juravlebacfec32014-11-14 15:54:36 +00002215 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2216
Calin Juravled2ec87d2014-12-08 14:24:46 +00002217 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002218 case Primitive::kPrimInt: {
2219 locations->SetInAt(0, Location::RegisterLocation(EAX));
2220 locations->SetInAt(1, Location::RequiresRegister());
2221 locations->SetOut(Location::RegisterLocation(EDX));
2222 break;
2223 }
2224 case Primitive::kPrimLong: {
2225 InvokeRuntimeCallingConvention calling_convention;
2226 locations->SetInAt(0, Location::RegisterPairLocation(
2227 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2228 locations->SetInAt(1, Location::RegisterPairLocation(
2229 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2230 // Runtime helper puts the result in EAX, EDX.
2231 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2232 break;
2233 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00002234 case Primitive::kPrimFloat: {
2235 InvokeRuntimeCallingConvention calling_convention;
2236 // x86 floating-point parameters are passed through core registers (EAX, ECX).
2237 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2238 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2239 // The runtime helper puts the result in XMM0.
2240 locations->SetOut(Location::FpuRegisterLocation(XMM0));
2241 break;
2242 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002243 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002244 InvokeRuntimeCallingConvention calling_convention;
2245 // x86 floating-point parameters are passed through core registers (EAX_ECX, EDX_EBX).
2246 locations->SetInAt(0, Location::RegisterPairLocation(
2247 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2248 locations->SetInAt(1, Location::RegisterPairLocation(
2249 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2250 // The runtime helper puts the result in XMM0.
2251 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Calin Juravlebacfec32014-11-14 15:54:36 +00002252 break;
2253 }
2254
2255 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00002256 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00002257 }
2258}
2259
2260void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2261 Primitive::Type type = rem->GetResultType();
2262 switch (type) {
2263 case Primitive::kPrimInt:
2264 case Primitive::kPrimLong: {
2265 GenerateDivRemIntegral(rem);
2266 break;
2267 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00002268 case Primitive::kPrimFloat: {
2269 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pFmodf)));
2270 codegen_->RecordPcInfo(rem, rem->GetDexPc());
2271 break;
2272 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002273 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00002274 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pFmod)));
2275 codegen_->RecordPcInfo(rem, rem->GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00002276 break;
2277 }
2278 default:
2279 LOG(FATAL) << "Unexpected rem type " << type;
2280 }
2281}
2282
Calin Juravled0d48522014-11-04 16:40:20 +00002283void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2284 LocationSummary* locations =
2285 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002286 switch (instruction->GetType()) {
2287 case Primitive::kPrimInt: {
2288 locations->SetInAt(0, Location::Any());
2289 break;
2290 }
2291 case Primitive::kPrimLong: {
2292 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2293 if (!instruction->IsConstant()) {
2294 locations->AddTemp(Location::RequiresRegister());
2295 }
2296 break;
2297 }
2298 default:
2299 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2300 }
Calin Juravled0d48522014-11-04 16:40:20 +00002301 if (instruction->HasUses()) {
2302 locations->SetOut(Location::SameAsFirstInput());
2303 }
2304}
2305
2306void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2307 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2308 codegen_->AddSlowPath(slow_path);
2309
2310 LocationSummary* locations = instruction->GetLocations();
2311 Location value = locations->InAt(0);
2312
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002313 switch (instruction->GetType()) {
2314 case Primitive::kPrimInt: {
2315 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002316 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002317 __ j(kEqual, slow_path->GetEntryLabel());
2318 } else if (value.IsStackSlot()) {
2319 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2320 __ j(kEqual, slow_path->GetEntryLabel());
2321 } else {
2322 DCHECK(value.IsConstant()) << value;
2323 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2324 __ jmp(slow_path->GetEntryLabel());
2325 }
2326 }
2327 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002328 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002329 case Primitive::kPrimLong: {
2330 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002331 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002332 __ movl(temp, value.AsRegisterPairLow<Register>());
2333 __ orl(temp, value.AsRegisterPairHigh<Register>());
2334 __ j(kEqual, slow_path->GetEntryLabel());
2335 } else {
2336 DCHECK(value.IsConstant()) << value;
2337 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2338 __ jmp(slow_path->GetEntryLabel());
2339 }
2340 }
2341 break;
2342 }
2343 default:
2344 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002345 }
Calin Juravled0d48522014-11-04 16:40:20 +00002346}
2347
Calin Juravle9aec02f2014-11-18 23:06:35 +00002348void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2349 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2350
2351 LocationSummary* locations =
2352 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2353
2354 switch (op->GetResultType()) {
2355 case Primitive::kPrimInt: {
2356 locations->SetInAt(0, Location::RequiresRegister());
2357 // The shift count needs to be in CL.
2358 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2359 locations->SetOut(Location::SameAsFirstInput());
2360 break;
2361 }
2362 case Primitive::kPrimLong: {
2363 locations->SetInAt(0, Location::RequiresRegister());
2364 // The shift count needs to be in CL.
2365 locations->SetInAt(1, Location::RegisterLocation(ECX));
2366 locations->SetOut(Location::SameAsFirstInput());
2367 break;
2368 }
2369 default:
2370 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2371 }
2372}
2373
2374void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2375 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2376
2377 LocationSummary* locations = op->GetLocations();
2378 Location first = locations->InAt(0);
2379 Location second = locations->InAt(1);
2380 DCHECK(first.Equals(locations->Out()));
2381
2382 switch (op->GetResultType()) {
2383 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002384 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002385 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002386 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002387 DCHECK_EQ(ECX, second_reg);
2388 if (op->IsShl()) {
2389 __ shll(first_reg, second_reg);
2390 } else if (op->IsShr()) {
2391 __ sarl(first_reg, second_reg);
2392 } else {
2393 __ shrl(first_reg, second_reg);
2394 }
2395 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00002396 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00002397 if (op->IsShl()) {
2398 __ shll(first_reg, imm);
2399 } else if (op->IsShr()) {
2400 __ sarl(first_reg, imm);
2401 } else {
2402 __ shrl(first_reg, imm);
2403 }
2404 }
2405 break;
2406 }
2407 case Primitive::kPrimLong: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002408 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00002409 DCHECK_EQ(ECX, second_reg);
2410 if (op->IsShl()) {
2411 GenerateShlLong(first, second_reg);
2412 } else if (op->IsShr()) {
2413 GenerateShrLong(first, second_reg);
2414 } else {
2415 GenerateUShrLong(first, second_reg);
2416 }
2417 break;
2418 }
2419 default:
2420 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2421 }
2422}
2423
2424void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2425 Label done;
2426 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2427 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2428 __ testl(shifter, Immediate(32));
2429 __ j(kEqual, &done);
2430 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2431 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2432 __ Bind(&done);
2433}
2434
2435void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2436 Label done;
2437 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2438 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2439 __ testl(shifter, Immediate(32));
2440 __ j(kEqual, &done);
2441 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2442 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2443 __ Bind(&done);
2444}
2445
2446void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2447 Label done;
2448 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2449 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2450 __ testl(shifter, Immediate(32));
2451 __ j(kEqual, &done);
2452 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2453 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2454 __ Bind(&done);
2455}
2456
2457void LocationsBuilderX86::VisitShl(HShl* shl) {
2458 HandleShift(shl);
2459}
2460
2461void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2462 HandleShift(shl);
2463}
2464
2465void LocationsBuilderX86::VisitShr(HShr* shr) {
2466 HandleShift(shr);
2467}
2468
2469void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2470 HandleShift(shr);
2471}
2472
2473void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2474 HandleShift(ushr);
2475}
2476
2477void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2478 HandleShift(ushr);
2479}
2480
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002481void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002482 LocationSummary* locations =
2483 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002484 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002485 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002486 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2487 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002488}
2489
2490void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2491 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002492 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002493 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002494
Nicolas Geoffray707c8092014-04-04 10:50:14 +01002495 __ fs()->call(
2496 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002497
Nicolas Geoffray39468442014-09-02 15:17:15 +01002498 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002499 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002500}
2501
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002502void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2503 LocationSummary* locations =
2504 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2505 locations->SetOut(Location::RegisterLocation(EAX));
2506 InvokeRuntimeCallingConvention calling_convention;
2507 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002508 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2509 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002510}
2511
2512void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2513 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08002514 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(2));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002515 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2516
2517 __ fs()->call(
2518 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
2519
2520 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2521 DCHECK(!codegen_->IsLeafMethod());
2522}
2523
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002524void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002525 LocationSummary* locations =
2526 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002527 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2528 if (location.IsStackSlot()) {
2529 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2530 } else if (location.IsDoubleStackSlot()) {
2531 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002532 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002533 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002534}
2535
2536void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002537 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002538}
2539
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002540void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002541 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002542 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002543 locations->SetInAt(0, Location::RequiresRegister());
2544 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002545}
2546
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002547void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2548 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002549 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002550 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002551 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002552 switch (not_->InputAt(0)->GetType()) {
2553 case Primitive::kPrimBoolean:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002554 __ xorl(out.AsRegister<Register>(), Immediate(1));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002555 break;
2556
2557 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002558 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002559 break;
2560
2561 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002562 __ notl(out.AsRegisterPairLow<Register>());
2563 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002564 break;
2565
2566 default:
2567 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2568 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002569}
2570
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002571void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002572 LocationSummary* locations =
2573 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002574 switch (compare->InputAt(0)->GetType()) {
2575 case Primitive::kPrimLong: {
2576 locations->SetInAt(0, Location::RequiresRegister());
2577 // TODO: we set any here but we don't handle constants
2578 locations->SetInAt(1, Location::Any());
2579 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2580 break;
2581 }
2582 case Primitive::kPrimFloat:
2583 case Primitive::kPrimDouble: {
2584 locations->SetInAt(0, Location::RequiresFpuRegister());
2585 locations->SetInAt(1, Location::RequiresFpuRegister());
2586 locations->SetOut(Location::RequiresRegister());
2587 break;
2588 }
2589 default:
2590 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2591 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002592}
2593
2594void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002595 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002596 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00002597 Location left = locations->InAt(0);
2598 Location right = locations->InAt(1);
2599
2600 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002601 switch (compare->InputAt(0)->GetType()) {
2602 case Primitive::kPrimLong: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002603 if (right.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002604 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002605 } else {
2606 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002607 __ cmpl(left.AsRegisterPairHigh<Register>(),
2608 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002609 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002610 __ j(kLess, &less); // Signed compare.
2611 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002612 if (right.IsRegisterPair()) {
2613 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002614 } else {
2615 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002616 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002617 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002618 break;
2619 }
2620 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002621 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002622 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
2623 break;
2624 }
2625 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002626 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00002627 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002628 break;
2629 }
2630 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002631 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002632 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002633 __ movl(out, Immediate(0));
2634 __ j(kEqual, &done);
2635 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
2636
2637 __ Bind(&greater);
2638 __ movl(out, Immediate(1));
2639 __ jmp(&done);
2640
2641 __ Bind(&less);
2642 __ movl(out, Immediate(-1));
2643
2644 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002645}
2646
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002647void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002648 LocationSummary* locations =
2649 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002650 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2651 locations->SetInAt(i, Location::Any());
2652 }
2653 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002654}
2655
2656void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002657 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002658 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002659}
2660
Calin Juravle52c48962014-12-16 17:02:57 +00002661void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
2662 /*
2663 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
2664 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
2665 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
2666 */
2667 switch (kind) {
2668 case MemBarrierKind::kAnyAny: {
2669 __ mfence();
2670 break;
2671 }
2672 case MemBarrierKind::kAnyStore:
2673 case MemBarrierKind::kLoadAny:
2674 case MemBarrierKind::kStoreStore: {
2675 // nop
2676 break;
2677 }
2678 default:
2679 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002680 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002681}
2682
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002683
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002684void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002685 Label is_null;
2686 __ testl(value, value);
2687 __ j(kEqual, &is_null);
2688 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2689 __ movl(temp, object);
2690 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002691 __ movb(Address(temp, card, TIMES_1, 0),
2692 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002693 __ Bind(&is_null);
2694}
2695
Calin Juravle52c48962014-12-16 17:02:57 +00002696void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
2697 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002698 LocationSummary* locations =
2699 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002700 locations->SetInAt(0, Location::RequiresRegister());
2701 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle52c48962014-12-16 17:02:57 +00002702
2703 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
2704 // Long values can be loaded atomically into an XMM using movsd.
2705 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
2706 // and then copy the XMM into the output 32bits at a time).
2707 locations->AddTemp(Location::RequiresFpuRegister());
2708 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002709}
2710
Calin Juravle52c48962014-12-16 17:02:57 +00002711void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
2712 const FieldInfo& field_info) {
2713 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002714
Calin Juravle52c48962014-12-16 17:02:57 +00002715 LocationSummary* locations = instruction->GetLocations();
2716 Register base = locations->InAt(0).AsRegister<Register>();
2717 Location out = locations->Out();
2718 bool is_volatile = field_info.IsVolatile();
2719 Primitive::Type field_type = field_info.GetFieldType();
2720 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2721
2722 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002723 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00002724 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002725 break;
2726 }
2727
2728 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00002729 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002730 break;
2731 }
2732
2733 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00002734 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002735 break;
2736 }
2737
2738 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00002739 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002740 break;
2741 }
2742
2743 case Primitive::kPrimInt:
2744 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00002745 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002746 break;
2747 }
2748
2749 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00002750 if (is_volatile) {
2751 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2752 __ movsd(temp, Address(base, offset));
2753 __ movd(out.AsRegisterPairLow<Register>(), temp);
2754 __ psrlq(temp, Immediate(32));
2755 __ movd(out.AsRegisterPairHigh<Register>(), temp);
2756 } else {
2757 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
2758 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
2759 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002760 break;
2761 }
2762
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002763 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00002764 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002765 break;
2766 }
2767
2768 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00002769 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002770 break;
2771 }
2772
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002773 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00002774 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002775 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002776 }
Calin Juravle52c48962014-12-16 17:02:57 +00002777
2778 if (is_volatile) {
2779 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2780 }
2781}
2782
2783void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
2784 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2785
2786 LocationSummary* locations =
2787 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2788 locations->SetInAt(0, Location::RequiresRegister());
2789 bool is_volatile = field_info.IsVolatile();
2790 Primitive::Type field_type = field_info.GetFieldType();
2791 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2792 || (field_type == Primitive::kPrimByte);
2793
2794 // The register allocator does not support multiple
2795 // inputs that die at entry with one in a specific register.
2796 if (is_byte_type) {
2797 // Ensure the value is in a byte register.
2798 locations->SetInAt(1, Location::RegisterLocation(EAX));
2799 } else {
2800 locations->SetInAt(1, Location::RequiresRegister());
2801 }
2802 // Temporary registers for the write barrier.
2803 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2804 locations->AddTemp(Location::RequiresRegister());
2805 // Ensure the card is in a byte register.
2806 locations->AddTemp(Location::RegisterLocation(ECX));
2807 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
2808 // 64bits value can be atomically written to an address with movsd and an XMM register.
2809 // We need two XMM registers because there's no easier way to (bit) copy a register pair
2810 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
2811 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
2812 // isolated cases when we need this it isn't worth adding the extra complexity.
2813 locations->AddTemp(Location::RequiresFpuRegister());
2814 locations->AddTemp(Location::RequiresFpuRegister());
2815 }
2816}
2817
2818void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
2819 const FieldInfo& field_info) {
2820 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2821
2822 LocationSummary* locations = instruction->GetLocations();
2823 Register base = locations->InAt(0).AsRegister<Register>();
2824 Location value = locations->InAt(1);
2825 bool is_volatile = field_info.IsVolatile();
2826 Primitive::Type field_type = field_info.GetFieldType();
2827 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
2828
2829 if (is_volatile) {
2830 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2831 }
2832
2833 switch (field_type) {
2834 case Primitive::kPrimBoolean:
2835 case Primitive::kPrimByte: {
2836 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
2837 break;
2838 }
2839
2840 case Primitive::kPrimShort:
2841 case Primitive::kPrimChar: {
2842 __ movw(Address(base, offset), value.AsRegister<Register>());
2843 break;
2844 }
2845
2846 case Primitive::kPrimInt:
2847 case Primitive::kPrimNot: {
2848 __ movl(Address(base, offset), value.AsRegister<Register>());
2849
2850 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
2851 Register temp = locations->GetTemp(0).AsRegister<Register>();
2852 Register card = locations->GetTemp(1).AsRegister<Register>();
2853 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>());
2854 }
2855 break;
2856 }
2857
2858 case Primitive::kPrimLong: {
2859 if (is_volatile) {
2860 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2861 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
2862 __ movd(temp1, value.AsRegisterPairLow<Register>());
2863 __ movd(temp2, value.AsRegisterPairHigh<Register>());
2864 __ punpckldq(temp1, temp2);
2865 __ movsd(Address(base, offset), temp1);
2866 } else {
2867 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
2868 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
2869 }
2870 break;
2871 }
2872
2873 case Primitive::kPrimFloat: {
2874 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
2875 break;
2876 }
2877
2878 case Primitive::kPrimDouble: {
2879 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
2880 break;
2881 }
2882
2883 case Primitive::kPrimVoid:
2884 LOG(FATAL) << "Unreachable type " << field_type;
2885 UNREACHABLE();
2886 }
2887
2888 if (is_volatile) {
2889 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2890 }
2891}
2892
2893void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2894 HandleFieldGet(instruction, instruction->GetFieldInfo());
2895}
2896
2897void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2898 HandleFieldGet(instruction, instruction->GetFieldInfo());
2899}
2900
2901void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2902 HandleFieldSet(instruction, instruction->GetFieldInfo());
2903}
2904
2905void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2906 HandleFieldSet(instruction, instruction->GetFieldInfo());
2907}
2908
2909void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2910 HandleFieldSet(instruction, instruction->GetFieldInfo());
2911}
2912
2913void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2914 HandleFieldSet(instruction, instruction->GetFieldInfo());
2915}
2916
2917void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2918 HandleFieldGet(instruction, instruction->GetFieldInfo());
2919}
2920
2921void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2922 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002923}
2924
2925void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002926 LocationSummary* locations =
2927 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002928 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
2929 ? Location::RequiresRegister()
2930 : Location::Any();
2931 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002932 if (instruction->HasUses()) {
2933 locations->SetOut(Location::SameAsFirstInput());
2934 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002935}
2936
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002937void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
2938 LocationSummary* locations = instruction->GetLocations();
2939 Location obj = locations->InAt(0);
2940 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
2941 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2942}
2943
2944void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002945 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002946 codegen_->AddSlowPath(slow_path);
2947
2948 LocationSummary* locations = instruction->GetLocations();
2949 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002950
2951 if (obj.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002952 __ cmpl(obj.AsRegister<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002953 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002954 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002955 } else {
2956 DCHECK(obj.IsConstant()) << obj;
2957 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2958 __ jmp(slow_path->GetEntryLabel());
2959 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002960 }
2961 __ j(kEqual, slow_path->GetEntryLabel());
2962}
2963
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002964void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
2965 if (codegen_->GetCompilerOptions().GetImplicitNullChecks()) {
2966 GenerateImplicitNullCheck(instruction);
2967 } else {
2968 GenerateExplicitNullCheck(instruction);
2969 }
2970}
2971
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002972void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002973 LocationSummary* locations =
2974 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002975 locations->SetInAt(0, Location::RequiresRegister());
2976 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2977 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002978}
2979
2980void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
2981 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002982 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002983 Location index = locations->InAt(1);
2984
2985 switch (instruction->GetType()) {
2986 case Primitive::kPrimBoolean: {
2987 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002988 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002989 if (index.IsConstant()) {
2990 __ movzxb(out, Address(obj,
2991 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2992 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002993 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002994 }
2995 break;
2996 }
2997
2998 case Primitive::kPrimByte: {
2999 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003000 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003001 if (index.IsConstant()) {
3002 __ movsxb(out, Address(obj,
3003 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
3004 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003005 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003006 }
3007 break;
3008 }
3009
3010 case Primitive::kPrimShort: {
3011 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003012 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003013 if (index.IsConstant()) {
3014 __ movsxw(out, Address(obj,
3015 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3016 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003017 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003018 }
3019 break;
3020 }
3021
3022 case Primitive::kPrimChar: {
3023 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003024 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003025 if (index.IsConstant()) {
3026 __ movzxw(out, Address(obj,
3027 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
3028 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003029 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003030 }
3031 break;
3032 }
3033
3034 case Primitive::kPrimInt:
3035 case Primitive::kPrimNot: {
3036 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003037 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003038 if (index.IsConstant()) {
3039 __ movl(out, Address(obj,
3040 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
3041 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003042 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003043 }
3044 break;
3045 }
3046
3047 case Primitive::kPrimLong: {
3048 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003049 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003050 if (index.IsConstant()) {
3051 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003052 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
3053 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003054 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003055 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003056 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003057 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00003058 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003059 }
3060 break;
3061 }
3062
3063 case Primitive::kPrimFloat:
3064 case Primitive::kPrimDouble:
3065 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003066 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003067 case Primitive::kPrimVoid:
3068 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003069 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003070 }
3071}
3072
3073void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003074 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003075 bool needs_write_barrier =
3076 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3077
3078 DCHECK(kFollowsQuickABI);
3079 bool not_enough_registers = needs_write_barrier
3080 && !instruction->GetValue()->IsConstant()
3081 && !instruction->GetIndex()->IsConstant();
3082 bool needs_runtime_call = instruction->NeedsTypeCheck() || not_enough_registers;
3083
Nicolas Geoffray39468442014-09-02 15:17:15 +01003084 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3085 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003086 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003087
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003088 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003089 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003090 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3091 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3092 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003093 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003094 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
3095 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003096 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003097 // In case of a byte operation, the register allocator does not support multiple
3098 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003099 locations->SetInAt(0, Location::RequiresRegister());
3100 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01003101 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003102 // Ensure the value is in a byte register.
3103 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003104 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003105 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003106 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003107 // Temporary registers for the write barrier.
3108 if (needs_write_barrier) {
3109 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003110 // Ensure the card is in a byte register.
3111 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003112 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003113 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003114}
3115
3116void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
3117 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003118 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003119 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003120 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003121 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003122 bool needs_runtime_call = locations->WillCall();
3123 bool needs_write_barrier =
3124 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003125
3126 switch (value_type) {
3127 case Primitive::kPrimBoolean:
3128 case Primitive::kPrimByte: {
3129 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003130 if (index.IsConstant()) {
3131 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003132 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003133 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003134 } else {
3135 __ movb(Address(obj, offset),
3136 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3137 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003138 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003139 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003140 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003141 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003142 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003143 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003144 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3145 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003146 }
3147 break;
3148 }
3149
3150 case Primitive::kPrimShort:
3151 case Primitive::kPrimChar: {
3152 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003153 if (index.IsConstant()) {
3154 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003155 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003156 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003157 } else {
3158 __ movw(Address(obj, offset),
3159 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3160 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003161 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003162 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003163 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
3164 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003165 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003166 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003167 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3168 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003169 }
3170 break;
3171 }
3172
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003173 case Primitive::kPrimInt:
3174 case Primitive::kPrimNot: {
3175 if (!needs_runtime_call) {
3176 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3177 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003178 size_t offset =
3179 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003180 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003181 __ movl(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003182 } else {
3183 DCHECK(value.IsConstant()) << value;
3184 __ movl(Address(obj, offset),
3185 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3186 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003187 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003188 DCHECK(index.IsRegister()) << index;
3189 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003190 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
3191 value.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003192 } else {
3193 DCHECK(value.IsConstant()) << value;
Roland Levillain271ab9c2014-11-27 15:23:57 +00003194 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003195 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
3196 }
3197 }
3198
3199 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003200 Register temp = locations->GetTemp(0).AsRegister<Register>();
3201 Register card = locations->GetTemp(1).AsRegister<Register>();
3202 codegen_->MarkGCCard(temp, card, obj, value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003203 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003204 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003205 DCHECK_EQ(value_type, Primitive::kPrimNot);
3206 DCHECK(!codegen_->IsLeafMethod());
3207 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
3208 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003209 }
3210 break;
3211 }
3212
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003213 case Primitive::kPrimLong: {
3214 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003215 if (index.IsConstant()) {
3216 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003217 if (value.IsRegisterPair()) {
3218 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
3219 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003220 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003221 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003222 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
3223 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
3224 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
3225 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003226 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003227 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003228 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003229 value.AsRegisterPairLow<Register>());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003230 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003231 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003232 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003233 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003234 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003235 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003236 Immediate(Low32Bits(val)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003237 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003238 Immediate(High32Bits(val)));
3239 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003240 }
3241 break;
3242 }
3243
3244 case Primitive::kPrimFloat:
3245 case Primitive::kPrimDouble:
3246 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003247 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003248 case Primitive::kPrimVoid:
3249 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07003250 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003251 }
3252}
3253
3254void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
3255 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003256 locations->SetInAt(0, Location::RequiresRegister());
3257 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003258 instruction->SetLocations(locations);
3259}
3260
3261void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
3262 LocationSummary* locations = instruction->GetLocations();
3263 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003264 Register obj = locations->InAt(0).AsRegister<Register>();
3265 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003266 __ movl(out, Address(obj, offset));
3267}
3268
3269void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003270 LocationSummary* locations =
3271 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003272 locations->SetInAt(0, Location::RequiresRegister());
3273 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01003274 if (instruction->HasUses()) {
3275 locations->SetOut(Location::SameAsFirstInput());
3276 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003277}
3278
3279void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
3280 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01003281 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01003282 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003283 codegen_->AddSlowPath(slow_path);
3284
Roland Levillain271ab9c2014-11-27 15:23:57 +00003285 Register index = locations->InAt(0).AsRegister<Register>();
3286 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003287
3288 __ cmpl(index, length);
3289 __ j(kAboveEqual, slow_path->GetEntryLabel());
3290}
3291
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003292void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3293 temp->SetLocations(nullptr);
3294}
3295
3296void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3297 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003298 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003299}
3300
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003301void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003302 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003303 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003304}
3305
3306void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003307 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3308}
3309
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003310void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3311 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3312}
3313
3314void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003315 HBasicBlock* block = instruction->GetBlock();
3316 if (block->GetLoopInformation() != nullptr) {
3317 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3318 // The back edge will generate the suspend check.
3319 return;
3320 }
3321 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3322 // The goto will generate the suspend check.
3323 return;
3324 }
3325 GenerateSuspendCheck(instruction, nullptr);
3326}
3327
3328void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3329 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003330 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003331 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003332 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003333 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003334 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003335 if (successor == nullptr) {
3336 __ j(kNotEqual, slow_path->GetEntryLabel());
3337 __ Bind(slow_path->GetReturnLabel());
3338 } else {
3339 __ j(kEqual, codegen_->GetLabelOf(successor));
3340 __ jmp(slow_path->GetEntryLabel());
3341 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003342}
3343
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003344X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3345 return codegen_->GetAssembler();
3346}
3347
3348void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
3349 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003350 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003351 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3352 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
3353 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
3354}
3355
3356void ParallelMoveResolverX86::EmitMove(size_t index) {
3357 MoveOperands* move = moves_.Get(index);
3358 Location source = move->GetSource();
3359 Location destination = move->GetDestination();
3360
3361 if (source.IsRegister()) {
3362 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003363 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003364 } else {
3365 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003366 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003367 }
3368 } else if (source.IsStackSlot()) {
3369 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003370 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003371 } else {
3372 DCHECK(destination.IsStackSlot());
3373 MoveMemoryToMemory(destination.GetStackIndex(),
3374 source.GetStackIndex());
3375 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003376 } else if (source.IsConstant()) {
3377 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
3378 Immediate imm(instruction->AsIntConstant()->GetValue());
3379 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003380 __ movl(destination.AsRegister<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003381 } else {
3382 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3383 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003384 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003385 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003386 }
3387}
3388
3389void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003390 Register suggested_scratch = reg == EAX ? EBX : EAX;
3391 ScratchRegisterScope ensure_scratch(
3392 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3393
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003394 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3395 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
3396 __ movl(Address(ESP, mem + stack_offset), reg);
3397 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
3398}
3399
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003400void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
3401 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003402 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3403
3404 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003405 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003406 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3407
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003408 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
3409 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
3410 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
3411 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
3412 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
3413 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
3414}
3415
3416void ParallelMoveResolverX86::EmitSwap(size_t index) {
3417 MoveOperands* move = moves_.Get(index);
3418 Location source = move->GetSource();
3419 Location destination = move->GetDestination();
3420
3421 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003422 __ xchgl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003423 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003424 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003425 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003426 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003427 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3428 Exchange(destination.GetStackIndex(), source.GetStackIndex());
3429 } else {
3430 LOG(FATAL) << "Unimplemented";
3431 }
3432}
3433
3434void ParallelMoveResolverX86::SpillScratch(int reg) {
3435 __ pushl(static_cast<Register>(reg));
3436}
3437
3438void ParallelMoveResolverX86::RestoreScratch(int reg) {
3439 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003440}
3441
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003442void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003443 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3444 ? LocationSummary::kCallOnSlowPath
3445 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003446 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003447 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003448 locations->SetOut(Location::RequiresRegister());
3449}
3450
3451void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003452 Register out = cls->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003453 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003454 DCHECK(!cls->CanCallRuntime());
3455 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003456 codegen_->LoadCurrentMethod(out);
3457 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3458 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003459 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003460 codegen_->LoadCurrentMethod(out);
3461 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3462 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003463
3464 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3465 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3466 codegen_->AddSlowPath(slow_path);
3467 __ testl(out, out);
3468 __ j(kEqual, slow_path->GetEntryLabel());
3469 if (cls->MustGenerateClinitCheck()) {
3470 GenerateClassInitializationCheck(slow_path, out);
3471 } else {
3472 __ Bind(slow_path->GetExitLabel());
3473 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003474 }
3475}
3476
3477void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
3478 LocationSummary* locations =
3479 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3480 locations->SetInAt(0, Location::RequiresRegister());
3481 if (check->HasUses()) {
3482 locations->SetOut(Location::SameAsFirstInput());
3483 }
3484}
3485
3486void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003487 // We assume the class to not be null.
3488 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3489 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003490 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00003491 GenerateClassInitializationCheck(slow_path,
3492 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003493}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003494
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003495void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
3496 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003497 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3498 Immediate(mirror::Class::kStatusInitialized));
3499 __ j(kLess, slow_path->GetEntryLabel());
3500 __ Bind(slow_path->GetExitLabel());
3501 // No need for memory fence, thanks to the X86 memory model.
3502}
3503
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003504void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
3505 LocationSummary* locations =
3506 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3507 locations->SetOut(Location::RequiresRegister());
3508}
3509
3510void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
3511 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
3512 codegen_->AddSlowPath(slow_path);
3513
Roland Levillain271ab9c2014-11-27 15:23:57 +00003514 Register out = load->GetLocations()->Out().AsRegister<Register>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003515 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08003516 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3517 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003518 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3519 __ testl(out, out);
3520 __ j(kEqual, slow_path->GetEntryLabel());
3521 __ Bind(slow_path->GetExitLabel());
3522}
3523
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003524void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
3525 LocationSummary* locations =
3526 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3527 locations->SetOut(Location::RequiresRegister());
3528}
3529
3530void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
3531 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003532 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), address);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003533 __ fs()->movl(address, Immediate(0));
3534}
3535
3536void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
3537 LocationSummary* locations =
3538 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3539 InvokeRuntimeCallingConvention calling_convention;
3540 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3541}
3542
3543void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
3544 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
3545 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3546}
3547
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003548void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003549 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3550 ? LocationSummary::kNoCall
3551 : LocationSummary::kCallOnSlowPath;
3552 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3553 locations->SetInAt(0, Location::RequiresRegister());
3554 locations->SetInAt(1, Location::Any());
3555 locations->SetOut(Location::RequiresRegister());
3556}
3557
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003558void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003559 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003560 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003561 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003562 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003563 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3564 Label done, zero;
3565 SlowPathCodeX86* slow_path = nullptr;
3566
3567 // Return 0 if `obj` is null.
3568 // TODO: avoid this check if we know obj is not null.
3569 __ testl(obj, obj);
3570 __ j(kEqual, &zero);
3571 __ movl(out, Address(obj, class_offset));
3572 // Compare the class of `obj` with `cls`.
3573 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003574 __ cmpl(out, cls.AsRegister<Register>());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003575 } else {
3576 DCHECK(cls.IsStackSlot()) << cls;
3577 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
3578 }
3579
3580 if (instruction->IsClassFinal()) {
3581 // Classes must be equal for the instanceof to succeed.
3582 __ j(kNotEqual, &zero);
3583 __ movl(out, Immediate(1));
3584 __ jmp(&done);
3585 } else {
3586 // If the classes are not equal, we go into a slow path.
3587 DCHECK(locations->OnlyCallsOnSlowPath());
3588 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003589 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003590 codegen_->AddSlowPath(slow_path);
3591 __ j(kNotEqual, slow_path->GetEntryLabel());
3592 __ movl(out, Immediate(1));
3593 __ jmp(&done);
3594 }
3595 __ Bind(&zero);
3596 __ movl(out, Immediate(0));
3597 if (slow_path != nullptr) {
3598 __ Bind(slow_path->GetExitLabel());
3599 }
3600 __ Bind(&done);
3601}
3602
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003603void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
3604 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3605 instruction, LocationSummary::kCallOnSlowPath);
3606 locations->SetInAt(0, Location::RequiresRegister());
3607 locations->SetInAt(1, Location::Any());
3608 locations->AddTemp(Location::RequiresRegister());
3609}
3610
3611void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
3612 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003613 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003614 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003615 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003616 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3617 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
3618 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3619 codegen_->AddSlowPath(slow_path);
3620
3621 // TODO: avoid this check if we know obj is not null.
3622 __ testl(obj, obj);
3623 __ j(kEqual, slow_path->GetExitLabel());
3624 __ movl(temp, Address(obj, class_offset));
3625
3626 // Compare the class of `obj` with `cls`.
3627 if (cls.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003628 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003629 } else {
3630 DCHECK(cls.IsStackSlot()) << cls;
3631 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
3632 }
3633
3634 __ j(kNotEqual, slow_path->GetEntryLabel());
3635 __ Bind(slow_path->GetExitLabel());
3636}
3637
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003638void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3639 LocationSummary* locations =
3640 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3641 InvokeRuntimeCallingConvention calling_convention;
3642 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3643}
3644
3645void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3646 __ fs()->call(Address::Absolute(instruction->IsEnter()
3647 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
3648 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
3649 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3650}
3651
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003652void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3653void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3654void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3655
3656void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3657 LocationSummary* locations =
3658 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3659 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3660 || instruction->GetResultType() == Primitive::kPrimLong);
3661 locations->SetInAt(0, Location::RequiresRegister());
3662 locations->SetInAt(1, Location::Any());
3663 locations->SetOut(Location::SameAsFirstInput());
3664}
3665
3666void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
3667 HandleBitwiseOperation(instruction);
3668}
3669
3670void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
3671 HandleBitwiseOperation(instruction);
3672}
3673
3674void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
3675 HandleBitwiseOperation(instruction);
3676}
3677
3678void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3679 LocationSummary* locations = instruction->GetLocations();
3680 Location first = locations->InAt(0);
3681 Location second = locations->InAt(1);
3682 DCHECK(first.Equals(locations->Out()));
3683
3684 if (instruction->GetResultType() == Primitive::kPrimInt) {
3685 if (second.IsRegister()) {
3686 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003687 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003688 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003689 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003690 } else {
3691 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003692 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003693 }
3694 } else if (second.IsConstant()) {
3695 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003696 __ andl(first.AsRegister<Register>(),
3697 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003698 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003699 __ orl(first.AsRegister<Register>(),
3700 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003701 } else {
3702 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00003703 __ xorl(first.AsRegister<Register>(),
3704 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003705 }
3706 } else {
3707 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003708 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003709 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003710 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003711 } else {
3712 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003713 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003714 }
3715 }
3716 } else {
3717 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3718 if (second.IsRegisterPair()) {
3719 if (instruction->IsAnd()) {
3720 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3721 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3722 } else if (instruction->IsOr()) {
3723 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3724 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3725 } else {
3726 DCHECK(instruction->IsXor());
3727 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3728 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3729 }
3730 } else {
3731 if (instruction->IsAnd()) {
3732 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3733 __ andl(first.AsRegisterPairHigh<Register>(),
3734 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3735 } else if (instruction->IsOr()) {
3736 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3737 __ orl(first.AsRegisterPairHigh<Register>(),
3738 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3739 } else {
3740 DCHECK(instruction->IsXor());
3741 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3742 __ xorl(first.AsRegisterPairHigh<Register>(),
3743 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3744 }
3745 }
3746 }
3747}
3748
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003749} // namespace x86
3750} // namespace art