blob: 3689452234eb24cb91ca24bb43456412cb81540a [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
19#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010020#include "gc/accounting/card_table.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070021#include "mirror/array-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010022#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010023#include "mirror/class.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010024#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000025#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010026#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000027#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010028#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010031
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032namespace x86 {
33
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010034static constexpr bool kExplicitStackOverflowCheck = false;
35
36static constexpr int kNumberOfPushedRegistersAtEntry = 1;
37static constexpr int kCurrentMethodStackOffset = 0;
38
Calin Juravled6fb6cf2014-11-11 19:07:44 +000039static constexpr Register kRuntimeParameterCoreRegisters[] = { EAX, ECX, EDX, EBX };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010040static constexpr size_t kRuntimeParameterCoreRegistersLength =
41 arraysize(kRuntimeParameterCoreRegisters);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010042static constexpr XmmRegister kRuntimeParameterFpuRegisters[] = { };
43static constexpr size_t kRuntimeParameterFpuRegistersLength = 0;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010044
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000045// Marker for places that can be updated once we don't follow the quick ABI.
46static constexpr bool kFollowsQuickABI = true;
47
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010048class InvokeRuntimeCallingConvention : public CallingConvention<Register, XmmRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010049 public:
50 InvokeRuntimeCallingConvention()
51 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010052 kRuntimeParameterCoreRegistersLength,
53 kRuntimeParameterFpuRegisters,
54 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010055
56 private:
57 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
58};
59
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
61
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010062class SlowPathCodeX86 : public SlowPathCode {
63 public:
64 SlowPathCodeX86() : entry_label_(), exit_label_() {}
65
66 Label* GetEntryLabel() { return &entry_label_; }
67 Label* GetExitLabel() { return &exit_label_; }
68
69 private:
70 Label entry_label_;
71 Label exit_label_;
72
73 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86);
74};
75
76class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010077 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010078 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079
80 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
81 __ Bind(GetEntryLabel());
82 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010083 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010084 }
85
86 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010087 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010088 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
89};
90
Calin Juravled0d48522014-11-04 16:40:20 +000091class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
92 public:
93 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
94
95 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
96 __ Bind(GetEntryLabel());
97 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
98 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
99 }
100
101 private:
102 HDivZeroCheck* const instruction_;
103 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
104};
105
Calin Juravlebacfec32014-11-14 15:54:36 +0000106class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +0000107 public:
Calin Juravlebacfec32014-11-14 15:54:36 +0000108 explicit DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000109
110 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
111 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000112 if (is_div_) {
113 __ negl(reg_);
114 } else {
115 __ movl(reg_, Immediate(0));
116 }
Calin Juravled0d48522014-11-04 16:40:20 +0000117 __ jmp(GetExitLabel());
118 }
119
120 private:
121 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000122 bool is_div_;
123 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000124};
125
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100126class StackOverflowCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100127 public:
128 StackOverflowCheckSlowPathX86() {}
129
130 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
131 __ Bind(GetEntryLabel());
132 __ addl(ESP,
133 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
134 __ fs()->jmp(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowStackOverflow)));
135 }
136
137 private:
138 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86);
139};
140
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100141class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100142 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100143 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
144 Location index_location,
145 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100146 : instruction_(instruction), index_location_(index_location), 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;
216 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(0));
217 __ movl(calling_convention.GetRegisterAt(1), Immediate(instruction_->GetStringIndex()));
218 __ 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()) {
314 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInstanceofNonTrivial)));
315 } else {
316 DCHECK(instruction_->IsCheckCast());
317 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
318 }
319
320 codegen->RecordPcInfo(instruction_, dex_pc_);
321 if (instruction_->IsInstanceOf()) {
322 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
323 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324 codegen->RestoreLiveRegisters(locations);
325
326 __ jmp(GetExitLabel());
327 }
328
329 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000330 HInstruction* const instruction_;
331 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000332 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000333 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000334
335 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
336};
337
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100338#undef __
339#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
340
Dave Allison20dfc792014-06-16 20:44:29 -0700341inline Condition X86Condition(IfCondition cond) {
342 switch (cond) {
343 case kCondEQ: return kEqual;
344 case kCondNE: return kNotEqual;
345 case kCondLT: return kLess;
346 case kCondLE: return kLessEqual;
347 case kCondGT: return kGreater;
348 case kCondGE: return kGreaterEqual;
349 default:
350 LOG(FATAL) << "Unknown if condition";
351 }
352 return kEqual;
353}
354
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100355void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
356 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
357}
358
359void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
360 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
361}
362
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100363size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
364 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
365 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100366}
367
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100368size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
369 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
370 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100371}
372
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100373CodeGeneratorX86::CodeGeneratorX86(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100374 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfXmmRegisters, kNumberOfRegisterPairs),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100375 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100376 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100377 instruction_visitor_(graph, this),
378 move_resolver_(graph->GetArena(), this) {}
379
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100380size_t CodeGeneratorX86::FrameEntrySpillSize() const {
381 return kNumberOfPushedRegistersAtEntry * kX86WordSize;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100382}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100383
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100384Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100385 switch (type) {
386 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100387 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100388 X86ManagedRegister pair =
389 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100390 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
391 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100392 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
393 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100394 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100395 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100396 }
397
398 case Primitive::kPrimByte:
399 case Primitive::kPrimBoolean:
400 case Primitive::kPrimChar:
401 case Primitive::kPrimShort:
402 case Primitive::kPrimInt:
403 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100404 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100405 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100406 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100407 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
408 X86ManagedRegister current =
409 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
410 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100411 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100412 }
413 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100414 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100415 }
416
417 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100418 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100419 return Location::FpuRegisterLocation(
420 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100421 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100422
423 case Primitive::kPrimVoid:
424 LOG(FATAL) << "Unreachable type " << type;
425 }
426
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100427 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100428}
429
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100430void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100431 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100432 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100433
434 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100435 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100436
437 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000438 DCHECK(kFollowsQuickABI);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100439 blocked_core_registers_[EBP] = true;
440 blocked_core_registers_[ESI] = true;
441 blocked_core_registers_[EDI] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100442
443 UpdateBlockedPairRegisters();
444}
445
446void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
447 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
448 X86ManagedRegister current =
449 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
450 if (blocked_core_registers_[current.AsRegisterPairLow()]
451 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
452 blocked_register_pairs_[i] = true;
453 }
454 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100455}
456
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100457InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
458 : HGraphVisitor(graph),
459 assembler_(codegen->GetAssembler()),
460 codegen_(codegen) {}
461
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000462void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000463 // Create a fake register to mimic Quick.
464 static const int kFakeReturnRegister = 8;
465 core_spill_mask_ |= (1 << kFakeReturnRegister);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000466
Dave Allison648d7112014-07-25 16:15:27 -0700467 bool skip_overflow_check = IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100468 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
469 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100470 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100471 }
472
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100473 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100474 __ subl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100475
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100476 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100477 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86();
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100478 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100479
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100480 __ fs()->cmpl(ESP, Address::Absolute(Thread::StackEndOffset<kX86WordSize>()));
481 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100482 }
483
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100484 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000485}
486
487void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100488 __ addl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000489}
490
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100491void CodeGeneratorX86::Bind(HBasicBlock* block) {
492 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000493}
494
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100495void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100496 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000497}
498
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100499Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
500 switch (load->GetType()) {
501 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100502 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100503 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
504 break;
505
506 case Primitive::kPrimInt:
507 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100508 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100509 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100510
511 case Primitive::kPrimBoolean:
512 case Primitive::kPrimByte:
513 case Primitive::kPrimChar:
514 case Primitive::kPrimShort:
515 case Primitive::kPrimVoid:
516 LOG(FATAL) << "Unexpected type " << load->GetType();
517 }
518
519 LOG(FATAL) << "Unreachable";
520 return Location();
521}
522
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100523Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
524 switch (type) {
525 case Primitive::kPrimBoolean:
526 case Primitive::kPrimByte:
527 case Primitive::kPrimChar:
528 case Primitive::kPrimShort:
529 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100530 case Primitive::kPrimFloat:
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100531 case Primitive::kPrimNot: {
532 uint32_t index = gp_index_++;
533 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100534 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100535 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100536 return Location::StackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100537 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100538 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100539
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100540 case Primitive::kPrimLong:
541 case Primitive::kPrimDouble: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100542 uint32_t index = gp_index_;
543 gp_index_ += 2;
544 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100545 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
546 calling_convention.GetRegisterPairAt(index));
547 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100548 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000549 // On X86, the register index and stack index of a quick parameter is the same, since
550 // we are passing floating pointer values in core registers.
551 return Location::QuickParameter(index, index);
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100552 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100553 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100554 }
555 }
556
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100557 case Primitive::kPrimVoid:
558 LOG(FATAL) << "Unexpected parameter type " << type;
559 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100560 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100561 return Location();
562}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100563
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100564void CodeGeneratorX86::Move32(Location destination, Location source) {
565 if (source.Equals(destination)) {
566 return;
567 }
568 if (destination.IsRegister()) {
569 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100570 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100571 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100572 __ movd(destination.As<Register>(), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100573 } else {
574 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100575 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100576 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100577 } else if (destination.IsFpuRegister()) {
578 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100579 __ movd(destination.As<XmmRegister>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100580 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100581 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100582 } else {
583 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100584 __ movss(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100585 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100586 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000587 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100588 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100589 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100590 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100591 __ movss(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100592 } else {
593 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100594 __ pushl(Address(ESP, source.GetStackIndex()));
595 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100596 }
597 }
598}
599
600void CodeGeneratorX86::Move64(Location destination, Location source) {
601 if (source.Equals(destination)) {
602 return;
603 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100604 if (destination.IsRegisterPair()) {
605 if (source.IsRegisterPair()) {
606 __ movl(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
607 __ movl(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100608 } else if (source.IsFpuRegister()) {
609 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100610 } else if (source.IsQuickParameter()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000611 uint16_t register_index = source.GetQuickParameterRegisterIndex();
612 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100613 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100614 __ movl(destination.AsRegisterPairLow<Register>(),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000615 calling_convention.GetRegisterAt(register_index));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100616 __ movl(destination.AsRegisterPairHigh<Register>(), Address(ESP,
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000617 calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100618 } else {
619 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100620 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
621 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100622 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
623 }
624 } else if (destination.IsQuickParameter()) {
625 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000626 uint16_t register_index = destination.GetQuickParameterRegisterIndex();
627 uint16_t stack_index = destination.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100628 if (source.IsRegister()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000629 __ movl(calling_convention.GetRegisterAt(register_index), source.AsRegisterPairLow<Register>());
630 __ movl(Address(ESP, calling_convention.GetStackOffsetOf(stack_index + 1)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100631 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100632 } else if (source.IsFpuRegister()) {
633 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100634 } else {
635 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000636 __ movl(calling_convention.GetRegisterAt(register_index),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100637 Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100638 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000639 __ popl(Address(ESP, calling_convention.GetStackOffsetOf(stack_index + 1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100640 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100641 } else if (destination.IsFpuRegister()) {
642 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100643 __ movsd(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100644 } else {
645 LOG(FATAL) << "Unimplemented";
646 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100647 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000648 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100649 if (source.IsRegisterPair()) {
650 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100651 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100652 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100653 } else if (source.IsQuickParameter()) {
654 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000655 uint16_t register_index = source.GetQuickParameterRegisterIndex();
656 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100657 __ movl(Address(ESP, destination.GetStackIndex()),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000658 calling_convention.GetRegisterAt(register_index));
659 DCHECK_EQ(calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100660 static_cast<size_t>(destination.GetHighStackIndex(kX86WordSize)));
661 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100662 __ movsd(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100663 } else {
664 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100665 __ pushl(Address(ESP, source.GetStackIndex()));
666 __ popl(Address(ESP, destination.GetStackIndex()));
667 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
668 __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100669 }
670 }
671}
672
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100673void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000674 LocationSummary* locations = instruction->GetLocations();
675 if (locations != nullptr && locations->Out().Equals(location)) {
676 return;
677 }
678
679 if (locations != nullptr && locations->Out().IsConstant()) {
680 HConstant* const_to_move = locations->Out().GetConstant();
681 if (const_to_move->IsIntConstant()) {
682 Immediate imm(const_to_move->AsIntConstant()->GetValue());
683 if (location.IsRegister()) {
684 __ movl(location.As<Register>(), imm);
685 } else if (location.IsStackSlot()) {
686 __ movl(Address(ESP, location.GetStackIndex()), imm);
687 } else {
688 DCHECK(location.IsConstant());
689 DCHECK_EQ(location.GetConstant(), const_to_move);
690 }
691 } else if (const_to_move->IsLongConstant()) {
692 int64_t value = const_to_move->AsLongConstant()->GetValue();
693 if (location.IsRegisterPair()) {
694 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
695 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
696 } else if (location.IsDoubleStackSlot()) {
697 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
698 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
699 } else {
700 DCHECK(location.IsConstant());
701 DCHECK_EQ(location.GetConstant(), instruction);
702 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100703 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000704 } else if (instruction->IsTemporary()) {
705 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000706 if (temp_location.IsStackSlot()) {
707 Move32(location, temp_location);
708 } else {
709 DCHECK(temp_location.IsDoubleStackSlot());
710 Move64(location, temp_location);
711 }
Roland Levillain476df552014-10-09 17:51:36 +0100712 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100713 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100714 switch (instruction->GetType()) {
715 case Primitive::kPrimBoolean:
716 case Primitive::kPrimByte:
717 case Primitive::kPrimChar:
718 case Primitive::kPrimShort:
719 case Primitive::kPrimInt:
720 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100721 case Primitive::kPrimFloat:
722 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100723 break;
724
725 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100726 case Primitive::kPrimDouble:
727 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100728 break;
729
730 default:
731 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
732 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000733 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100734 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100735 switch (instruction->GetType()) {
736 case Primitive::kPrimBoolean:
737 case Primitive::kPrimByte:
738 case Primitive::kPrimChar:
739 case Primitive::kPrimShort:
740 case Primitive::kPrimInt:
741 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100742 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000743 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100744 break;
745
746 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100747 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000748 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100749 break;
750
751 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100752 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100753 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000754 }
755}
756
757void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000758 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000759}
760
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000761void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000762 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100763 DCHECK(!successor->IsExitBlock());
764
765 HBasicBlock* block = got->GetBlock();
766 HInstruction* previous = got->GetPrevious();
767
768 HLoopInformation* info = block->GetLoopInformation();
769 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
770 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
771 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
772 return;
773 }
774
775 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
776 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
777 }
778 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000779 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000780 }
781}
782
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000783void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000784 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000785}
786
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000787void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700788 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000789 if (kIsDebugBuild) {
790 __ Comment("Unreachable");
791 __ int3();
792 }
793}
794
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000795void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100796 LocationSummary* locations =
797 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100798 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100799 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100800 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100801 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000802}
803
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000804void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700805 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100806 if (cond->IsIntConstant()) {
807 // Constant condition, statically compared against 1.
808 int32_t cond_value = cond->AsIntConstant()->GetValue();
809 if (cond_value == 1) {
810 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
811 if_instr->IfTrueSuccessor())) {
812 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100813 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100814 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100815 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100816 DCHECK_EQ(cond_value, 0);
817 }
818 } else {
819 bool materialized =
820 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
821 // Moves do not affect the eflags register, so if the condition is
822 // evaluated just before the if, we don't need to evaluate it
823 // again.
824 bool eflags_set = cond->IsCondition()
825 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
826 if (materialized) {
827 if (!eflags_set) {
828 // Materialized condition, compare against 0.
829 Location lhs = if_instr->GetLocations()->InAt(0);
830 if (lhs.IsRegister()) {
831 __ cmpl(lhs.As<Register>(), Immediate(0));
832 } else {
833 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
834 }
835 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
836 } else {
837 __ j(X86Condition(cond->AsCondition()->GetCondition()),
838 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
839 }
840 } else {
841 Location lhs = cond->GetLocations()->InAt(0);
842 Location rhs = cond->GetLocations()->InAt(1);
843 // LHS is guaranteed to be in a register (see
844 // LocationsBuilderX86::VisitCondition).
845 if (rhs.IsRegister()) {
846 __ cmpl(lhs.As<Register>(), rhs.As<Register>());
847 } else if (rhs.IsConstant()) {
848 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
849 Immediate imm(instruction->AsIntConstant()->GetValue());
850 __ cmpl(lhs.As<Register>(), imm);
851 } else {
852 __ cmpl(lhs.As<Register>(), Address(ESP, rhs.GetStackIndex()));
853 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100854 __ j(X86Condition(cond->AsCondition()->GetCondition()),
855 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700856 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100857 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100858 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
859 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700860 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000861 }
862}
863
864void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000865 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000866}
867
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000868void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
869 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000870}
871
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000872void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100873 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000874}
875
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000876void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100877 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700878 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000879}
880
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100881void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100882 LocationSummary* locations =
883 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100884 switch (store->InputAt(1)->GetType()) {
885 case Primitive::kPrimBoolean:
886 case Primitive::kPrimByte:
887 case Primitive::kPrimChar:
888 case Primitive::kPrimShort:
889 case Primitive::kPrimInt:
890 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100891 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100892 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
893 break;
894
895 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100896 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100897 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
898 break;
899
900 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100901 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100902 }
903 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000904}
905
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000906void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700907 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000908}
909
Dave Allison20dfc792014-06-16 20:44:29 -0700910void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100911 LocationSummary* locations =
912 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100913 locations->SetInAt(0, Location::RequiresRegister());
914 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100915 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100916 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100917 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000918}
919
Dave Allison20dfc792014-06-16 20:44:29 -0700920void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
921 if (comp->NeedsMaterialization()) {
922 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100923 Register reg = locations->Out().As<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100924 // Clear register: setcc only sets the low byte.
925 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700926 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100927 __ cmpl(locations->InAt(0).As<Register>(),
928 locations->InAt(1).As<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100929 } else if (locations->InAt(1).IsConstant()) {
930 HConstant* instruction = locations->InAt(1).GetConstant();
931 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100932 __ cmpl(locations->InAt(0).As<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700933 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100934 __ cmpl(locations->InAt(0).As<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700935 Address(ESP, locations->InAt(1).GetStackIndex()));
936 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100937 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100938 }
Dave Allison20dfc792014-06-16 20:44:29 -0700939}
940
941void LocationsBuilderX86::VisitEqual(HEqual* comp) {
942 VisitCondition(comp);
943}
944
945void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
946 VisitCondition(comp);
947}
948
949void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
950 VisitCondition(comp);
951}
952
953void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
954 VisitCondition(comp);
955}
956
957void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
958 VisitCondition(comp);
959}
960
961void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
962 VisitCondition(comp);
963}
964
965void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
966 VisitCondition(comp);
967}
968
969void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
970 VisitCondition(comp);
971}
972
973void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
974 VisitCondition(comp);
975}
976
977void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
978 VisitCondition(comp);
979}
980
981void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
982 VisitCondition(comp);
983}
984
985void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
986 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000987}
988
989void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100990 LocationSummary* locations =
991 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100992 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000993}
994
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000995void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100996 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700997 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000998}
999
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001000void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001001 LocationSummary* locations =
1002 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001003 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001004}
1005
1006void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1007 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001008 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001009}
1010
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001011void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1012 LocationSummary* locations =
1013 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1014 locations->SetOut(Location::ConstantLocation(constant));
1015}
1016
1017void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1018 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001019 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001020}
1021
1022void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1023 LocationSummary* locations =
1024 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1025 locations->SetOut(Location::ConstantLocation(constant));
1026}
1027
1028void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1029 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001030 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001031}
1032
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001033void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001034 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001035}
1036
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001037void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001038 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001039 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001040 __ ret();
1041}
1042
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001043void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001044 LocationSummary* locations =
1045 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001046 switch (ret->InputAt(0)->GetType()) {
1047 case Primitive::kPrimBoolean:
1048 case Primitive::kPrimByte:
1049 case Primitive::kPrimChar:
1050 case Primitive::kPrimShort:
1051 case Primitive::kPrimInt:
1052 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001053 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001054 break;
1055
1056 case Primitive::kPrimLong:
1057 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001058 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001059 break;
1060
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001061 case Primitive::kPrimFloat:
1062 case Primitive::kPrimDouble:
1063 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001064 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001065 break;
1066
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001067 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001068 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001069 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001070}
1071
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001072void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001073 if (kIsDebugBuild) {
1074 switch (ret->InputAt(0)->GetType()) {
1075 case Primitive::kPrimBoolean:
1076 case Primitive::kPrimByte:
1077 case Primitive::kPrimChar:
1078 case Primitive::kPrimShort:
1079 case Primitive::kPrimInt:
1080 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001081 DCHECK_EQ(ret->GetLocations()->InAt(0).As<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001082 break;
1083
1084 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001085 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1086 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001087 break;
1088
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001089 case Primitive::kPrimFloat:
1090 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001091 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001092 break;
1093
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001094 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001095 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001096 }
1097 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001098 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001099 __ ret();
1100}
1101
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001102void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001103 HandleInvoke(invoke);
1104}
1105
1106void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001107 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001108
1109 // TODO: Implement all kinds of calls:
1110 // 1) boot -> boot
1111 // 2) app -> boot
1112 // 3) app -> app
1113 //
1114 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1115
1116 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001117 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001118 // temp = temp->dex_cache_resolved_methods_;
1119 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
1120 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001121 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001122 // (temp + offset_of_quick_compiled_code)()
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001123 __ call(Address(
1124 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001125
1126 DCHECK(!codegen_->IsLeafMethod());
1127 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1128}
1129
1130void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1131 HandleInvoke(invoke);
1132}
1133
1134void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001135 LocationSummary* locations =
1136 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001137 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001138
1139 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001140 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001141 HInstruction* input = invoke->InputAt(i);
1142 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1143 }
1144
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001145 switch (invoke->GetType()) {
1146 case Primitive::kPrimBoolean:
1147 case Primitive::kPrimByte:
1148 case Primitive::kPrimChar:
1149 case Primitive::kPrimShort:
1150 case Primitive::kPrimInt:
1151 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001152 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001153 break;
1154
1155 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001156 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001157 break;
1158
1159 case Primitive::kPrimVoid:
1160 break;
1161
1162 case Primitive::kPrimDouble:
1163 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001164 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001165 break;
1166 }
1167
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001168 invoke->SetLocations(locations);
1169}
1170
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001171void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001172 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001173 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1174 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1175 LocationSummary* locations = invoke->GetLocations();
1176 Location receiver = locations->InAt(0);
1177 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1178 // temp = object->GetClass();
1179 if (receiver.IsStackSlot()) {
1180 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1181 __ movl(temp, Address(temp, class_offset));
1182 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001183 __ movl(temp, Address(receiver.As<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001184 }
1185 // temp = temp->GetMethodAt(method_offset);
1186 __ movl(temp, Address(temp, method_offset));
1187 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001188 __ call(Address(
1189 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001190
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001191 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001192 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001193}
1194
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001195void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1196 HandleInvoke(invoke);
1197 // Add the hidden argument.
1198 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM0));
1199}
1200
1201void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1202 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1203 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
1204 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1205 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1206 LocationSummary* locations = invoke->GetLocations();
1207 Location receiver = locations->InAt(0);
1208 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1209
1210 // Set the hidden argument.
1211 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
1212 __ movd(invoke->GetLocations()->GetTemp(1).As<XmmRegister>(), temp);
1213
1214 // temp = object->GetClass();
1215 if (receiver.IsStackSlot()) {
1216 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1217 __ movl(temp, Address(temp, class_offset));
1218 } else {
1219 __ movl(temp, Address(receiver.As<Register>(), class_offset));
1220 }
1221 // temp = temp->GetImtEntryAt(method_offset);
1222 __ movl(temp, Address(temp, method_offset));
1223 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001224 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001225 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001226
1227 DCHECK(!codegen_->IsLeafMethod());
1228 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1229}
1230
Roland Levillain88cb1752014-10-20 16:36:47 +01001231void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1232 LocationSummary* locations =
1233 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1234 switch (neg->GetResultType()) {
1235 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001236 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001237 locations->SetInAt(0, Location::RequiresRegister());
1238 locations->SetOut(Location::SameAsFirstInput());
1239 break;
1240
Roland Levillain88cb1752014-10-20 16:36:47 +01001241 case Primitive::kPrimFloat:
1242 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001243 locations->SetInAt(0, Location::RequiresFpuRegister());
1244 // Output overlaps as we need a fresh (zero-initialized)
1245 // register to perform subtraction from zero.
1246 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001247 break;
1248
1249 default:
1250 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1251 }
1252}
1253
1254void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1255 LocationSummary* locations = neg->GetLocations();
1256 Location out = locations->Out();
1257 Location in = locations->InAt(0);
1258 switch (neg->GetResultType()) {
1259 case Primitive::kPrimInt:
1260 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001261 DCHECK(in.Equals(out));
Roland Levillain88cb1752014-10-20 16:36:47 +01001262 __ negl(out.As<Register>());
1263 break;
1264
1265 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001266 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001267 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001268 __ negl(out.AsRegisterPairLow<Register>());
1269 // Negation is similar to subtraction from zero. The least
1270 // significant byte triggers a borrow when it is different from
1271 // zero; to take it into account, add 1 to the most significant
1272 // byte if the carry flag (CF) is set to 1 after the first NEGL
1273 // operation.
1274 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1275 __ negl(out.AsRegisterPairHigh<Register>());
1276 break;
1277
Roland Levillain88cb1752014-10-20 16:36:47 +01001278 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001279 DCHECK(!in.Equals(out));
1280 // out = 0
1281 __ xorps(out.As<XmmRegister>(), out.As<XmmRegister>());
1282 // out = out - in
1283 __ subss(out.As<XmmRegister>(), in.As<XmmRegister>());
1284 break;
1285
Roland Levillain88cb1752014-10-20 16:36:47 +01001286 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001287 DCHECK(!in.Equals(out));
1288 // out = 0
1289 __ xorpd(out.As<XmmRegister>(), out.As<XmmRegister>());
1290 // out = out - in
1291 __ subsd(out.As<XmmRegister>(), in.As<XmmRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001292 break;
1293
1294 default:
1295 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1296 }
1297}
1298
Roland Levillaindff1f282014-11-05 14:15:05 +00001299void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
1300 LocationSummary* locations =
1301 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1302 Primitive::Type result_type = conversion->GetResultType();
1303 Primitive::Type input_type = conversion->GetInputType();
1304 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001305 case Primitive::kPrimByte:
1306 switch (input_type) {
1307 case Primitive::kPrimShort:
1308 case Primitive::kPrimInt:
1309 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001310 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001311 locations->SetInAt(0, Location::Any());
1312 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1313 break;
1314
1315 default:
1316 LOG(FATAL) << "Unexpected type conversion from " << input_type
1317 << " to " << result_type;
1318 }
1319 break;
1320
Roland Levillain01a8d712014-11-14 16:27:39 +00001321 case Primitive::kPrimShort:
1322 switch (input_type) {
1323 case Primitive::kPrimByte:
1324 case Primitive::kPrimInt:
1325 case Primitive::kPrimChar:
1326 // Processing a Dex `int-to-short' instruction.
1327 locations->SetInAt(0, Location::Any());
1328 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1329 break;
1330
1331 default:
1332 LOG(FATAL) << "Unexpected type conversion from " << input_type
1333 << " to " << result_type;
1334 }
1335 break;
1336
Roland Levillain946e1432014-11-11 17:35:19 +00001337 case Primitive::kPrimInt:
1338 switch (input_type) {
1339 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001340 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001341 locations->SetInAt(0, Location::Any());
1342 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1343 break;
1344
1345 case Primitive::kPrimFloat:
1346 case Primitive::kPrimDouble:
1347 LOG(FATAL) << "Type conversion from " << input_type
1348 << " to " << result_type << " not yet implemented";
1349 break;
1350
1351 default:
1352 LOG(FATAL) << "Unexpected type conversion from " << input_type
1353 << " to " << result_type;
1354 }
1355 break;
1356
Roland Levillaindff1f282014-11-05 14:15:05 +00001357 case Primitive::kPrimLong:
1358 switch (input_type) {
1359 case Primitive::kPrimByte:
1360 case Primitive::kPrimShort:
1361 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001362 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001363 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001364 locations->SetInAt(0, Location::RegisterLocation(EAX));
1365 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1366 break;
1367
1368 case Primitive::kPrimFloat:
1369 case Primitive::kPrimDouble:
1370 LOG(FATAL) << "Type conversion from " << input_type << " to "
1371 << result_type << " not yet implemented";
1372 break;
1373
1374 default:
1375 LOG(FATAL) << "Unexpected type conversion from " << input_type
1376 << " to " << result_type;
1377 }
1378 break;
1379
Roland Levillain981e4542014-11-14 11:47:14 +00001380 case Primitive::kPrimChar:
1381 switch (input_type) {
1382 case Primitive::kPrimByte:
1383 case Primitive::kPrimShort:
1384 case Primitive::kPrimInt:
1385 case Primitive::kPrimChar:
1386 // Processing a Dex `int-to-char' instruction.
1387 locations->SetInAt(0, Location::Any());
1388 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1389 break;
1390
1391 default:
1392 LOG(FATAL) << "Unexpected type conversion from " << input_type
1393 << " to " << result_type;
1394 }
1395 break;
1396
Roland Levillaindff1f282014-11-05 14:15:05 +00001397 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001398 switch (input_type) {
1399 case Primitive::kPrimByte:
1400 case Primitive::kPrimShort:
1401 case Primitive::kPrimInt:
1402 case Primitive::kPrimChar:
1403 // Processing a Dex `int-to-float' instruction.
1404 locations->SetInAt(0, Location::RequiresRegister());
1405 locations->SetOut(Location::RequiresFpuRegister());
1406 break;
1407
1408 case Primitive::kPrimLong:
1409 case Primitive::kPrimDouble:
1410 LOG(FATAL) << "Type conversion from " << input_type
1411 << " to " << result_type << " not yet implemented";
1412 break;
1413
1414 default:
1415 LOG(FATAL) << "Unexpected type conversion from " << input_type
1416 << " to " << result_type;
1417 };
1418 break;
1419
Roland Levillaindff1f282014-11-05 14:15:05 +00001420 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001421 switch (input_type) {
1422 case Primitive::kPrimByte:
1423 case Primitive::kPrimShort:
1424 case Primitive::kPrimInt:
1425 case Primitive::kPrimChar:
1426 // Processing a Dex `int-to-double' instruction.
1427 locations->SetInAt(0, Location::RequiresRegister());
1428 locations->SetOut(Location::RequiresFpuRegister());
1429 break;
1430
1431 case Primitive::kPrimLong:
1432 case Primitive::kPrimFloat:
1433 LOG(FATAL) << "Type conversion from " << input_type
1434 << " to " << result_type << " not yet implemented";
1435 break;
1436
1437 default:
1438 LOG(FATAL) << "Unexpected type conversion from " << input_type
1439 << " to " << result_type;
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}
1448
1449void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1450 LocationSummary* locations = conversion->GetLocations();
1451 Location out = locations->Out();
1452 Location in = locations->InAt(0);
1453 Primitive::Type result_type = conversion->GetResultType();
1454 Primitive::Type input_type = conversion->GetInputType();
1455 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001456 case Primitive::kPrimByte:
1457 switch (input_type) {
1458 case Primitive::kPrimShort:
1459 case Primitive::kPrimInt:
1460 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001461 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001462 if (in.IsRegister()) {
1463 __ movsxb(out.As<Register>(), in.As<ByteRegister>());
1464 } else if (in.IsStackSlot()) {
1465 __ movsxb(out.As<Register>(), Address(ESP, in.GetStackIndex()));
1466 } else {
1467 DCHECK(in.GetConstant()->IsIntConstant());
1468 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1469 __ movl(out.As<Register>(), Immediate(static_cast<int8_t>(value)));
1470 }
1471 break;
1472
1473 default:
1474 LOG(FATAL) << "Unexpected type conversion from " << input_type
1475 << " to " << result_type;
1476 }
1477 break;
1478
Roland Levillain01a8d712014-11-14 16:27:39 +00001479 case Primitive::kPrimShort:
1480 switch (input_type) {
1481 case Primitive::kPrimByte:
1482 case Primitive::kPrimInt:
1483 case Primitive::kPrimChar:
1484 // Processing a Dex `int-to-short' instruction.
1485 if (in.IsRegister()) {
1486 __ movsxw(out.As<Register>(), in.As<Register>());
1487 } else if (in.IsStackSlot()) {
1488 __ movsxw(out.As<Register>(), Address(ESP, in.GetStackIndex()));
1489 } else {
1490 DCHECK(in.GetConstant()->IsIntConstant());
1491 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1492 __ movl(out.As<Register>(), Immediate(static_cast<int16_t>(value)));
1493 }
1494 break;
1495
1496 default:
1497 LOG(FATAL) << "Unexpected type conversion from " << input_type
1498 << " to " << result_type;
1499 }
1500 break;
1501
Roland Levillain946e1432014-11-11 17:35:19 +00001502 case Primitive::kPrimInt:
1503 switch (input_type) {
1504 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001505 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001506 if (in.IsRegisterPair()) {
1507 __ movl(out.As<Register>(), in.AsRegisterPairLow<Register>());
1508 } else if (in.IsDoubleStackSlot()) {
1509 __ movl(out.As<Register>(), Address(ESP, in.GetStackIndex()));
1510 } else {
1511 DCHECK(in.IsConstant());
1512 DCHECK(in.GetConstant()->IsLongConstant());
1513 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
1514 __ movl(out.As<Register>(), Immediate(static_cast<int32_t>(value)));
1515 }
1516 break;
1517
1518 case Primitive::kPrimFloat:
1519 case Primitive::kPrimDouble:
1520 LOG(FATAL) << "Type conversion from " << input_type
1521 << " to " << result_type << " not yet implemented";
1522 break;
1523
1524 default:
1525 LOG(FATAL) << "Unexpected type conversion from " << input_type
1526 << " to " << result_type;
1527 }
1528 break;
1529
Roland Levillaindff1f282014-11-05 14:15:05 +00001530 case Primitive::kPrimLong:
1531 switch (input_type) {
1532 case Primitive::kPrimByte:
1533 case Primitive::kPrimShort:
1534 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001535 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001536 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001537 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1538 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
1539 DCHECK_EQ(in.As<Register>(), EAX);
1540 __ cdq();
1541 break;
1542
1543 case Primitive::kPrimFloat:
1544 case Primitive::kPrimDouble:
1545 LOG(FATAL) << "Type conversion from " << input_type << " to "
1546 << result_type << " not yet implemented";
1547 break;
1548
1549 default:
1550 LOG(FATAL) << "Unexpected type conversion from " << input_type
1551 << " to " << result_type;
1552 }
1553 break;
1554
Roland Levillain981e4542014-11-14 11:47:14 +00001555 case Primitive::kPrimChar:
1556 switch (input_type) {
1557 case Primitive::kPrimByte:
1558 case Primitive::kPrimShort:
1559 case Primitive::kPrimInt:
1560 case Primitive::kPrimChar:
1561 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1562 if (in.IsRegister()) {
1563 __ movzxw(out.As<Register>(), in.As<Register>());
1564 } else if (in.IsStackSlot()) {
1565 __ movzxw(out.As<Register>(), Address(ESP, in.GetStackIndex()));
1566 } else {
1567 DCHECK(in.GetConstant()->IsIntConstant());
1568 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1569 __ movl(out.As<Register>(), Immediate(static_cast<uint16_t>(value)));
1570 }
1571 break;
1572
1573 default:
1574 LOG(FATAL) << "Unexpected type conversion from " << input_type
1575 << " to " << result_type;
1576 }
1577 break;
1578
Roland Levillaindff1f282014-11-05 14:15:05 +00001579 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001580 switch (input_type) {
1581 // Processing a Dex `int-to-float' instruction.
1582 case Primitive::kPrimByte:
1583 case Primitive::kPrimShort:
1584 case Primitive::kPrimInt:
1585 case Primitive::kPrimChar:
1586 __ cvtsi2ss(out.As<XmmRegister>(), in.As<Register>());
1587 break;
1588
1589 case Primitive::kPrimLong:
1590 case Primitive::kPrimDouble:
1591 LOG(FATAL) << "Type conversion from " << input_type
1592 << " to " << result_type << " not yet implemented";
1593 break;
1594
1595 default:
1596 LOG(FATAL) << "Unexpected type conversion from " << input_type
1597 << " to " << result_type;
1598 };
1599 break;
1600
Roland Levillaindff1f282014-11-05 14:15:05 +00001601 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001602 switch (input_type) {
1603 // Processing a Dex `int-to-double' instruction.
1604 case Primitive::kPrimByte:
1605 case Primitive::kPrimShort:
1606 case Primitive::kPrimInt:
1607 case Primitive::kPrimChar:
1608 __ cvtsi2sd(out.As<XmmRegister>(), in.As<Register>());
1609 break;
1610
1611 case Primitive::kPrimLong:
1612 case Primitive::kPrimFloat:
1613 LOG(FATAL) << "Type conversion from " << input_type
1614 << " to " << result_type << " not yet implemented";
1615 break;
1616
1617 default:
1618 LOG(FATAL) << "Unexpected type conversion from " << input_type
1619 << " to " << result_type;
1620 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001621 break;
1622
1623 default:
1624 LOG(FATAL) << "Unexpected type conversion from " << input_type
1625 << " to " << result_type;
1626 }
1627}
1628
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001629void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001630 LocationSummary* locations =
1631 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001632 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001633 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001634 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001635 locations->SetInAt(0, Location::RequiresRegister());
1636 locations->SetInAt(1, Location::Any());
1637 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001638 break;
1639 }
1640
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001641 case Primitive::kPrimFloat:
1642 case Primitive::kPrimDouble: {
1643 locations->SetInAt(0, Location::RequiresFpuRegister());
1644 locations->SetInAt(1, Location::Any());
1645 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001646 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001647 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001648
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001649 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001650 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1651 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001652 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001653}
1654
1655void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1656 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001657 Location first = locations->InAt(0);
1658 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001659 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001660 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001661 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001662 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001663 __ addl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001664 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001665 __ addl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001666 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001667 __ addl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001668 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001669 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001670 }
1671
1672 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001673 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001674 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1675 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001676 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001677 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1678 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001679 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001680 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001681 break;
1682 }
1683
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001684 case Primitive::kPrimFloat: {
1685 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001686 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001687 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001688 __ addss(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001689 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001690 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001691 }
1692
1693 case Primitive::kPrimDouble: {
1694 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001695 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001696 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001697 __ addsd(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001698 }
1699 break;
1700 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001701
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001702 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001703 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001704 }
1705}
1706
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001707void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001708 LocationSummary* locations =
1709 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001710 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001711 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001712 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001713 locations->SetInAt(0, Location::RequiresRegister());
1714 locations->SetInAt(1, Location::Any());
1715 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001716 break;
1717 }
Calin Juravle11351682014-10-23 15:38:15 +01001718 case Primitive::kPrimFloat:
1719 case Primitive::kPrimDouble: {
1720 locations->SetInAt(0, Location::RequiresFpuRegister());
1721 locations->SetInAt(1, Location::RequiresFpuRegister());
1722 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001723 break;
Calin Juravle11351682014-10-23 15:38:15 +01001724 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001725
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001726 default:
Calin Juravle11351682014-10-23 15:38:15 +01001727 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001728 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001729}
1730
1731void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1732 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001733 Location first = locations->InAt(0);
1734 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001735 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001736 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001737 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001738 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001739 __ subl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001740 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001741 __ subl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001742 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001743 __ subl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001744 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001745 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001746 }
1747
1748 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001749 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01001750 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1751 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001752 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001753 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001754 __ sbbl(first.AsRegisterPairHigh<Register>(),
1755 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001756 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001757 break;
1758 }
1759
Calin Juravle11351682014-10-23 15:38:15 +01001760 case Primitive::kPrimFloat: {
1761 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001762 break;
Calin Juravle11351682014-10-23 15:38:15 +01001763 }
1764
1765 case Primitive::kPrimDouble: {
1766 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1767 break;
1768 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001769
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001770 default:
Calin Juravle11351682014-10-23 15:38:15 +01001771 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001772 }
1773}
1774
Calin Juravle34bacdf2014-10-07 20:23:36 +01001775void LocationsBuilderX86::VisitMul(HMul* mul) {
1776 LocationSummary* locations =
1777 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1778 switch (mul->GetResultType()) {
1779 case Primitive::kPrimInt:
1780 locations->SetInAt(0, Location::RequiresRegister());
1781 locations->SetInAt(1, Location::Any());
1782 locations->SetOut(Location::SameAsFirstInput());
1783 break;
1784 case Primitive::kPrimLong: {
1785 locations->SetInAt(0, Location::RequiresRegister());
1786 // TODO: Currently this handles only stack operands:
1787 // - we don't have enough registers because we currently use Quick ABI.
1788 // - by the time we have a working register allocator we will probably change the ABI
1789 // and fix the above.
1790 // - we don't have a way yet to request operands on stack but the base line compiler
1791 // will leave the operands on the stack with Any().
1792 locations->SetInAt(1, Location::Any());
1793 locations->SetOut(Location::SameAsFirstInput());
1794 // Needed for imul on 32bits with 64bits output.
1795 locations->AddTemp(Location::RegisterLocation(EAX));
1796 locations->AddTemp(Location::RegisterLocation(EDX));
1797 break;
1798 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001799 case Primitive::kPrimFloat:
1800 case Primitive::kPrimDouble: {
1801 locations->SetInAt(0, Location::RequiresFpuRegister());
1802 locations->SetInAt(1, Location::RequiresFpuRegister());
1803 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001804 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001805 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001806
1807 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001808 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001809 }
1810}
1811
1812void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1813 LocationSummary* locations = mul->GetLocations();
1814 Location first = locations->InAt(0);
1815 Location second = locations->InAt(1);
1816 DCHECK(first.Equals(locations->Out()));
1817
1818 switch (mul->GetResultType()) {
1819 case Primitive::kPrimInt: {
1820 if (second.IsRegister()) {
1821 __ imull(first.As<Register>(), second.As<Register>());
1822 } else if (second.IsConstant()) {
1823 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1824 __ imull(first.As<Register>(), imm);
1825 } else {
1826 DCHECK(second.IsStackSlot());
1827 __ imull(first.As<Register>(), Address(ESP, second.GetStackIndex()));
1828 }
1829 break;
1830 }
1831
1832 case Primitive::kPrimLong: {
1833 DCHECK(second.IsDoubleStackSlot());
1834
1835 Register in1_hi = first.AsRegisterPairHigh<Register>();
1836 Register in1_lo = first.AsRegisterPairLow<Register>();
1837 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
1838 Address in2_lo(ESP, second.GetStackIndex());
1839 Register eax = locations->GetTemp(0).As<Register>();
1840 Register edx = locations->GetTemp(1).As<Register>();
1841
1842 DCHECK_EQ(EAX, eax);
1843 DCHECK_EQ(EDX, edx);
1844
1845 // input: in1 - 64 bits, in2 - 64 bits
1846 // output: in1
1847 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
1848 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
1849 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
1850
1851 __ movl(eax, in2_hi);
1852 // eax <- in1.lo * in2.hi
1853 __ imull(eax, in1_lo);
1854 // in1.hi <- in1.hi * in2.lo
1855 __ imull(in1_hi, in2_lo);
1856 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
1857 __ addl(in1_hi, eax);
1858 // move in1_lo to eax to prepare for double precision
1859 __ movl(eax, in1_lo);
1860 // edx:eax <- in1.lo * in2.lo
1861 __ mull(in2_lo);
1862 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
1863 __ addl(in1_hi, edx);
1864 // in1.lo <- (in1.lo * in2.lo)[31:0];
1865 __ movl(in1_lo, eax);
1866
1867 break;
1868 }
1869
Calin Juravleb5bfa962014-10-21 18:02:24 +01001870 case Primitive::kPrimFloat: {
1871 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001872 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001873 }
1874
1875 case Primitive::kPrimDouble: {
1876 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1877 break;
1878 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001879
1880 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001881 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001882 }
1883}
1884
Calin Juravlebacfec32014-11-14 15:54:36 +00001885void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
1886 DCHECK(instruction->IsDiv() || instruction->IsRem());
1887
1888 LocationSummary* locations = instruction->GetLocations();
1889 Location out = locations->Out();
1890 Location first = locations->InAt(0);
1891 Location second = locations->InAt(1);
1892 bool is_div = instruction->IsDiv();
1893
1894 switch (instruction->GetResultType()) {
1895 case Primitive::kPrimInt: {
1896 Register second_reg = second.As<Register>();
1897 DCHECK_EQ(EAX, first.As<Register>());
1898 DCHECK_EQ(is_div ? EAX : EDX, out.As<Register>());
1899
1900 SlowPathCodeX86* slow_path =
1901 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.As<Register>(), is_div);
1902 codegen_->AddSlowPath(slow_path);
1903
1904 // 0x80000000/-1 triggers an arithmetic exception!
1905 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
1906 // it's safe to just use negl instead of more complex comparisons.
1907
1908 __ cmpl(second_reg, Immediate(-1));
1909 __ j(kEqual, slow_path->GetEntryLabel());
1910
1911 // edx:eax <- sign-extended of eax
1912 __ cdq();
1913 // eax = quotient, edx = remainder
1914 __ idivl(second_reg);
1915
1916 __ Bind(slow_path->GetExitLabel());
1917 break;
1918 }
1919
1920 case Primitive::kPrimLong: {
1921 InvokeRuntimeCallingConvention calling_convention;
1922 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
1923 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
1924 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
1925 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
1926 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
1927 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
1928
1929 if (is_div) {
1930 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
1931 } else {
1932 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
1933 }
1934 uint32_t dex_pc = is_div
1935 ? instruction->AsDiv()->GetDexPc()
1936 : instruction->AsRem()->GetDexPc();
1937 codegen_->RecordPcInfo(instruction, dex_pc);
1938
1939 break;
1940 }
1941
1942 default:
1943 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
1944 }
1945}
1946
Calin Juravle7c4954d2014-10-28 16:57:40 +00001947void LocationsBuilderX86::VisitDiv(HDiv* div) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001948 LocationSummary::CallKind call_kind = div->GetResultType() == Primitive::kPrimLong
1949 ? LocationSummary::kCall
1950 : LocationSummary::kNoCall;
1951 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
1952
Calin Juravle7c4954d2014-10-28 16:57:40 +00001953 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001954 case Primitive::kPrimInt: {
1955 locations->SetInAt(0, Location::RegisterLocation(EAX));
1956 locations->SetInAt(1, Location::RequiresRegister());
1957 locations->SetOut(Location::SameAsFirstInput());
1958 // Intel uses edx:eax as the dividend.
1959 locations->AddTemp(Location::RegisterLocation(EDX));
1960 break;
1961 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001962 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001963 InvokeRuntimeCallingConvention calling_convention;
1964 locations->SetInAt(0, Location::RegisterPairLocation(
1965 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
1966 locations->SetInAt(1, Location::RegisterPairLocation(
1967 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
1968 // Runtime helper puts the result in EAX, EDX.
1969 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00001970 break;
1971 }
1972 case Primitive::kPrimFloat:
1973 case Primitive::kPrimDouble: {
1974 locations->SetInAt(0, Location::RequiresFpuRegister());
1975 locations->SetInAt(1, Location::RequiresFpuRegister());
1976 locations->SetOut(Location::SameAsFirstInput());
1977 break;
1978 }
1979
1980 default:
1981 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1982 }
1983}
1984
1985void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
1986 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001987 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00001988 Location first = locations->InAt(0);
1989 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001990
1991 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00001992 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00001993 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001994 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001995 break;
1996 }
1997
1998 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001999 DCHECK(first.Equals(out));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002000 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
2001 break;
2002 }
2003
2004 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002005 DCHECK(first.Equals(out));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002006 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
2007 break;
2008 }
2009
2010 default:
2011 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2012 }
2013}
2014
Calin Juravlebacfec32014-11-14 15:54:36 +00002015void LocationsBuilderX86::VisitRem(HRem* rem) {
2016 LocationSummary::CallKind call_kind = rem->GetResultType() == Primitive::kPrimLong
2017 ? LocationSummary::kCall
2018 : LocationSummary::kNoCall;
2019 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2020
2021 switch (rem->GetResultType()) {
2022 case Primitive::kPrimInt: {
2023 locations->SetInAt(0, Location::RegisterLocation(EAX));
2024 locations->SetInAt(1, Location::RequiresRegister());
2025 locations->SetOut(Location::RegisterLocation(EDX));
2026 break;
2027 }
2028 case Primitive::kPrimLong: {
2029 InvokeRuntimeCallingConvention calling_convention;
2030 locations->SetInAt(0, Location::RegisterPairLocation(
2031 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2032 locations->SetInAt(1, Location::RegisterPairLocation(
2033 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2034 // Runtime helper puts the result in EAX, EDX.
2035 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2036 break;
2037 }
2038 case Primitive::kPrimFloat:
2039 case Primitive::kPrimDouble: {
2040 LOG(FATAL) << "Unimplemented rem type " << rem->GetResultType();
2041 break;
2042 }
2043
2044 default:
2045 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2046 }
2047}
2048
2049void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2050 Primitive::Type type = rem->GetResultType();
2051 switch (type) {
2052 case Primitive::kPrimInt:
2053 case Primitive::kPrimLong: {
2054 GenerateDivRemIntegral(rem);
2055 break;
2056 }
2057 case Primitive::kPrimFloat:
2058 case Primitive::kPrimDouble: {
2059 LOG(FATAL) << "Unimplemented rem type " << type;
2060 break;
2061 }
2062 default:
2063 LOG(FATAL) << "Unexpected rem type " << type;
2064 }
2065}
2066
Calin Juravled0d48522014-11-04 16:40:20 +00002067void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2068 LocationSummary* locations =
2069 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002070 switch (instruction->GetType()) {
2071 case Primitive::kPrimInt: {
2072 locations->SetInAt(0, Location::Any());
2073 break;
2074 }
2075 case Primitive::kPrimLong: {
2076 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2077 if (!instruction->IsConstant()) {
2078 locations->AddTemp(Location::RequiresRegister());
2079 }
2080 break;
2081 }
2082 default:
2083 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2084 }
Calin Juravled0d48522014-11-04 16:40:20 +00002085 if (instruction->HasUses()) {
2086 locations->SetOut(Location::SameAsFirstInput());
2087 }
2088}
2089
2090void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2091 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2092 codegen_->AddSlowPath(slow_path);
2093
2094 LocationSummary* locations = instruction->GetLocations();
2095 Location value = locations->InAt(0);
2096
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002097 switch (instruction->GetType()) {
2098 case Primitive::kPrimInt: {
2099 if (value.IsRegister()) {
2100 __ testl(value.As<Register>(), value.As<Register>());
2101 __ j(kEqual, slow_path->GetEntryLabel());
2102 } else if (value.IsStackSlot()) {
2103 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2104 __ j(kEqual, slow_path->GetEntryLabel());
2105 } else {
2106 DCHECK(value.IsConstant()) << value;
2107 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2108 __ jmp(slow_path->GetEntryLabel());
2109 }
2110 }
2111 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002112 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002113 case Primitive::kPrimLong: {
2114 if (value.IsRegisterPair()) {
2115 Register temp = locations->GetTemp(0).As<Register>();
2116 __ movl(temp, value.AsRegisterPairLow<Register>());
2117 __ orl(temp, value.AsRegisterPairHigh<Register>());
2118 __ j(kEqual, slow_path->GetEntryLabel());
2119 } else {
2120 DCHECK(value.IsConstant()) << value;
2121 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2122 __ jmp(slow_path->GetEntryLabel());
2123 }
2124 }
2125 break;
2126 }
2127 default:
2128 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002129 }
Calin Juravled0d48522014-11-04 16:40:20 +00002130}
2131
Calin Juravle9aec02f2014-11-18 23:06:35 +00002132void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2133 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2134
2135 LocationSummary* locations =
2136 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2137
2138 switch (op->GetResultType()) {
2139 case Primitive::kPrimInt: {
2140 locations->SetInAt(0, Location::RequiresRegister());
2141 // The shift count needs to be in CL.
2142 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2143 locations->SetOut(Location::SameAsFirstInput());
2144 break;
2145 }
2146 case Primitive::kPrimLong: {
2147 locations->SetInAt(0, Location::RequiresRegister());
2148 // The shift count needs to be in CL.
2149 locations->SetInAt(1, Location::RegisterLocation(ECX));
2150 locations->SetOut(Location::SameAsFirstInput());
2151 break;
2152 }
2153 default:
2154 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2155 }
2156}
2157
2158void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2159 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2160
2161 LocationSummary* locations = op->GetLocations();
2162 Location first = locations->InAt(0);
2163 Location second = locations->InAt(1);
2164 DCHECK(first.Equals(locations->Out()));
2165
2166 switch (op->GetResultType()) {
2167 case Primitive::kPrimInt: {
2168 Register first_reg = first.As<Register>();
2169 if (second.IsRegister()) {
2170 Register second_reg = second.As<Register>();
2171 DCHECK_EQ(ECX, second_reg);
2172 if (op->IsShl()) {
2173 __ shll(first_reg, second_reg);
2174 } else if (op->IsShr()) {
2175 __ sarl(first_reg, second_reg);
2176 } else {
2177 __ shrl(first_reg, second_reg);
2178 }
2179 } else {
2180 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
2181 if (op->IsShl()) {
2182 __ shll(first_reg, imm);
2183 } else if (op->IsShr()) {
2184 __ sarl(first_reg, imm);
2185 } else {
2186 __ shrl(first_reg, imm);
2187 }
2188 }
2189 break;
2190 }
2191 case Primitive::kPrimLong: {
2192 Register second_reg = second.As<Register>();
2193 DCHECK_EQ(ECX, second_reg);
2194 if (op->IsShl()) {
2195 GenerateShlLong(first, second_reg);
2196 } else if (op->IsShr()) {
2197 GenerateShrLong(first, second_reg);
2198 } else {
2199 GenerateUShrLong(first, second_reg);
2200 }
2201 break;
2202 }
2203 default:
2204 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2205 }
2206}
2207
2208void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2209 Label done;
2210 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2211 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2212 __ testl(shifter, Immediate(32));
2213 __ j(kEqual, &done);
2214 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2215 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2216 __ Bind(&done);
2217}
2218
2219void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2220 Label done;
2221 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2222 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2223 __ testl(shifter, Immediate(32));
2224 __ j(kEqual, &done);
2225 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2226 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2227 __ Bind(&done);
2228}
2229
2230void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2231 Label done;
2232 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2233 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2234 __ testl(shifter, Immediate(32));
2235 __ j(kEqual, &done);
2236 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2237 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2238 __ Bind(&done);
2239}
2240
2241void LocationsBuilderX86::VisitShl(HShl* shl) {
2242 HandleShift(shl);
2243}
2244
2245void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2246 HandleShift(shl);
2247}
2248
2249void LocationsBuilderX86::VisitShr(HShr* shr) {
2250 HandleShift(shr);
2251}
2252
2253void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2254 HandleShift(shr);
2255}
2256
2257void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2258 HandleShift(ushr);
2259}
2260
2261void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2262 HandleShift(ushr);
2263}
2264
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002265void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002266 LocationSummary* locations =
2267 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002268 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002269 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002270 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2271 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002272}
2273
2274void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2275 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002276 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002277 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002278
Nicolas Geoffray707c8092014-04-04 10:50:14 +01002279 __ fs()->call(
2280 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002281
Nicolas Geoffray39468442014-09-02 15:17:15 +01002282 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002283 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002284}
2285
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002286void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2287 LocationSummary* locations =
2288 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2289 locations->SetOut(Location::RegisterLocation(EAX));
2290 InvokeRuntimeCallingConvention calling_convention;
2291 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2292 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2293 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2294}
2295
2296void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2297 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002298 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002299 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2300
2301 __ fs()->call(
2302 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
2303
2304 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2305 DCHECK(!codegen_->IsLeafMethod());
2306}
2307
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002308void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002309 LocationSummary* locations =
2310 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002311 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2312 if (location.IsStackSlot()) {
2313 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2314 } else if (location.IsDoubleStackSlot()) {
2315 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002316 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002317 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002318}
2319
2320void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002321 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002322}
2323
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002324void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002325 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002326 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002327 locations->SetInAt(0, Location::RequiresRegister());
2328 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002329}
2330
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002331void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2332 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002333 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002334 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002335 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002336 switch (not_->InputAt(0)->GetType()) {
2337 case Primitive::kPrimBoolean:
2338 __ xorl(out.As<Register>(), Immediate(1));
2339 break;
2340
2341 case Primitive::kPrimInt:
2342 __ notl(out.As<Register>());
2343 break;
2344
2345 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002346 __ notl(out.AsRegisterPairLow<Register>());
2347 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002348 break;
2349
2350 default:
2351 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2352 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002353}
2354
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002355void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002356 LocationSummary* locations =
2357 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravle91debbc2014-11-26 19:01:09 +00002358 locations->SetInAt(0, Location::RequiresRegister());
2359 locations->SetInAt(1, Location::Any());
2360 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002361}
2362
2363void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002364 LocationSummary* locations = compare->GetLocations();
2365 switch (compare->InputAt(0)->GetType()) {
2366 case Primitive::kPrimLong: {
Calin Juravle91debbc2014-11-26 19:01:09 +00002367 Label less, greater, done;
2368 Register output = locations->Out().As<Register>();
2369 Location left = locations->InAt(0);
2370 Location right = locations->InAt(1);
2371 if (right.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002372 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002373 } else {
2374 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002375 __ cmpl(left.AsRegisterPairHigh<Register>(),
2376 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002377 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002378 __ j(kLess, &less); // Signed compare.
2379 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002380 if (right.IsRegisterPair()) {
2381 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002382 } else {
2383 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002384 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002385 }
Calin Juravle91debbc2014-11-26 19:01:09 +00002386 __ movl(output, Immediate(0));
2387 __ j(kEqual, &done);
2388 __ j(kBelow, &less); // Unsigned compare.
2389
2390 __ Bind(&greater);
2391 __ movl(output, Immediate(1));
2392 __ jmp(&done);
2393
2394 __ Bind(&less);
2395 __ movl(output, Immediate(-1));
2396
2397 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002398 break;
2399 }
2400 default:
Calin Juravle91debbc2014-11-26 19:01:09 +00002401 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002402 }
2403}
2404
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002405void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002406 LocationSummary* locations =
2407 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002408 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2409 locations->SetInAt(i, Location::Any());
2410 }
2411 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002412}
2413
2414void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002415 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002416 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002417}
2418
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002419void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002420 LocationSummary* locations =
2421 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002422 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002423 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002424 bool needs_write_barrier =
2425 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
2426
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002427 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2428 || (field_type == Primitive::kPrimByte);
2429 // The register allocator does not support multiple
2430 // inputs that die at entry with one in a specific register.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002431 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002432 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002433 locations->SetInAt(1, Location::RegisterLocation(EAX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002434 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002435 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002436 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002437 // Temporary registers for the write barrier.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002438 if (needs_write_barrier) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002439 locations->AddTemp(Location::RequiresRegister());
2440 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002441 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002442 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002443}
2444
2445void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2446 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002447 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002448 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01002449 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002450
2451 switch (field_type) {
2452 case Primitive::kPrimBoolean:
2453 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002454 ByteRegister value = locations->InAt(1).As<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002455 __ movb(Address(obj, offset), value);
2456 break;
2457 }
2458
2459 case Primitive::kPrimShort:
2460 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002461 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002462 __ movw(Address(obj, offset), value);
2463 break;
2464 }
2465
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002466 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002467 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002468 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002469 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002470
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002471 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002472 Register temp = locations->GetTemp(0).As<Register>();
2473 Register card = locations->GetTemp(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002474 codegen_->MarkGCCard(temp, card, obj, value);
2475 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002476 break;
2477 }
2478
2479 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002480 Location value = locations->InAt(1);
2481 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
2482 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002483 break;
2484 }
2485
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002486 case Primitive::kPrimFloat: {
2487 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2488 __ movss(Address(obj, offset), value);
2489 break;
2490 }
2491
2492 case Primitive::kPrimDouble: {
2493 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2494 __ movsd(Address(obj, offset), value);
2495 break;
2496 }
2497
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002498 case Primitive::kPrimVoid:
2499 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002500 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002501 }
2502}
2503
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002504void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
2505 Label is_null;
2506 __ testl(value, value);
2507 __ j(kEqual, &is_null);
2508 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2509 __ movl(temp, object);
2510 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
2511 __ movb(Address(temp, card, TIMES_1, 0),
2512 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
2513 __ Bind(&is_null);
2514}
2515
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002516void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002517 LocationSummary* locations =
2518 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002519 locations->SetInAt(0, Location::RequiresRegister());
2520 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002521}
2522
2523void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2524 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002525 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002526 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2527
2528 switch (instruction->GetType()) {
2529 case Primitive::kPrimBoolean: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002530 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002531 __ movzxb(out, Address(obj, offset));
2532 break;
2533 }
2534
2535 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002536 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002537 __ movsxb(out, Address(obj, offset));
2538 break;
2539 }
2540
2541 case Primitive::kPrimShort: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002542 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002543 __ movsxw(out, Address(obj, offset));
2544 break;
2545 }
2546
2547 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002548 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002549 __ movzxw(out, Address(obj, offset));
2550 break;
2551 }
2552
2553 case Primitive::kPrimInt:
2554 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002555 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002556 __ movl(out, Address(obj, offset));
2557 break;
2558 }
2559
2560 case Primitive::kPrimLong: {
2561 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002562 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
2563 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002564 break;
2565 }
2566
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002567 case Primitive::kPrimFloat: {
2568 XmmRegister out = locations->Out().As<XmmRegister>();
2569 __ movss(out, Address(obj, offset));
2570 break;
2571 }
2572
2573 case Primitive::kPrimDouble: {
2574 XmmRegister out = locations->Out().As<XmmRegister>();
2575 __ movsd(out, Address(obj, offset));
2576 break;
2577 }
2578
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002579 case Primitive::kPrimVoid:
2580 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002581 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002582 }
2583}
2584
2585void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002586 LocationSummary* locations =
2587 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002588 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002589 if (instruction->HasUses()) {
2590 locations->SetOut(Location::SameAsFirstInput());
2591 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002592}
2593
2594void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002595 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002596 codegen_->AddSlowPath(slow_path);
2597
2598 LocationSummary* locations = instruction->GetLocations();
2599 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002600
2601 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002602 __ cmpl(obj.As<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002603 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002604 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002605 } else {
2606 DCHECK(obj.IsConstant()) << obj;
2607 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2608 __ jmp(slow_path->GetEntryLabel());
2609 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002610 }
2611 __ j(kEqual, slow_path->GetEntryLabel());
2612}
2613
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002614void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002615 LocationSummary* locations =
2616 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002617 locations->SetInAt(0, Location::RequiresRegister());
2618 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2619 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002620}
2621
2622void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
2623 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002624 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002625 Location index = locations->InAt(1);
2626
2627 switch (instruction->GetType()) {
2628 case Primitive::kPrimBoolean: {
2629 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002630 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002631 if (index.IsConstant()) {
2632 __ movzxb(out, Address(obj,
2633 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2634 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002635 __ movzxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002636 }
2637 break;
2638 }
2639
2640 case Primitive::kPrimByte: {
2641 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002642 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002643 if (index.IsConstant()) {
2644 __ movsxb(out, Address(obj,
2645 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2646 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002647 __ movsxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002648 }
2649 break;
2650 }
2651
2652 case Primitive::kPrimShort: {
2653 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002654 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002655 if (index.IsConstant()) {
2656 __ movsxw(out, Address(obj,
2657 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2658 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002659 __ movsxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002660 }
2661 break;
2662 }
2663
2664 case Primitive::kPrimChar: {
2665 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002666 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002667 if (index.IsConstant()) {
2668 __ movzxw(out, Address(obj,
2669 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2670 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002671 __ movzxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002672 }
2673 break;
2674 }
2675
2676 case Primitive::kPrimInt:
2677 case Primitive::kPrimNot: {
2678 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002679 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002680 if (index.IsConstant()) {
2681 __ movl(out, Address(obj,
2682 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2683 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002684 __ movl(out, Address(obj, index.As<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002685 }
2686 break;
2687 }
2688
2689 case Primitive::kPrimLong: {
2690 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002691 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002692 if (index.IsConstant()) {
2693 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002694 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
2695 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002696 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002697 __ movl(out.AsRegisterPairLow<Register>(),
2698 Address(obj, index.As<Register>(), TIMES_8, data_offset));
2699 __ movl(out.AsRegisterPairHigh<Register>(),
2700 Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002701 }
2702 break;
2703 }
2704
2705 case Primitive::kPrimFloat:
2706 case Primitive::kPrimDouble:
2707 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002708 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002709 case Primitive::kPrimVoid:
2710 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002711 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002712 }
2713}
2714
2715void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002716 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002717 bool needs_write_barrier =
2718 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2719
2720 DCHECK(kFollowsQuickABI);
2721 bool not_enough_registers = needs_write_barrier
2722 && !instruction->GetValue()->IsConstant()
2723 && !instruction->GetIndex()->IsConstant();
2724 bool needs_runtime_call = instruction->NeedsTypeCheck() || not_enough_registers;
2725
Nicolas Geoffray39468442014-09-02 15:17:15 +01002726 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2727 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002728 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002729
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002730 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002731 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002732 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2733 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2734 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002735 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002736 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
2737 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002738 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002739 // In case of a byte operation, the register allocator does not support multiple
2740 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002741 locations->SetInAt(0, Location::RequiresRegister());
2742 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002743 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002744 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002745 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002746 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002747 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002748 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002749 // Temporary registers for the write barrier.
2750 if (needs_write_barrier) {
2751 locations->AddTemp(Location::RequiresRegister());
2752 // Ensure the card is in a byte register.
2753 locations->AddTemp(Location::RegisterLocation(ECX));
2754 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002755 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002756}
2757
2758void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
2759 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002760 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002761 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002762 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002763 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002764 bool needs_runtime_call = locations->WillCall();
2765 bool needs_write_barrier =
2766 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002767
2768 switch (value_type) {
2769 case Primitive::kPrimBoolean:
2770 case Primitive::kPrimByte: {
2771 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002772 if (index.IsConstant()) {
2773 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002774 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002775 __ movb(Address(obj, offset), value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002776 } else {
2777 __ movb(Address(obj, offset),
2778 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2779 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002780 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002781 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002782 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
2783 value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002784 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002785 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002786 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2787 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002788 }
2789 break;
2790 }
2791
2792 case Primitive::kPrimShort:
2793 case Primitive::kPrimChar: {
2794 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002795 if (index.IsConstant()) {
2796 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002797 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002798 __ movw(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002799 } else {
2800 __ movw(Address(obj, offset),
2801 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2802 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002803 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002804 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002805 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
2806 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002807 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002808 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002809 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2810 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002811 }
2812 break;
2813 }
2814
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002815 case Primitive::kPrimInt:
2816 case Primitive::kPrimNot: {
2817 if (!needs_runtime_call) {
2818 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2819 if (index.IsConstant()) {
2820 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2821 if (value.IsRegister()) {
2822 __ movl(Address(obj, offset), value.As<Register>());
2823 } else {
2824 DCHECK(value.IsConstant()) << value;
2825 __ movl(Address(obj, offset),
2826 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2827 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002828 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002829 DCHECK(index.IsRegister()) << index;
2830 if (value.IsRegister()) {
2831 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
2832 value.As<Register>());
2833 } else {
2834 DCHECK(value.IsConstant()) << value;
2835 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
2836 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2837 }
2838 }
2839
2840 if (needs_write_barrier) {
2841 Register temp = locations->GetTemp(0).As<Register>();
2842 Register card = locations->GetTemp(1).As<Register>();
2843 codegen_->MarkGCCard(temp, card, obj, value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002844 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002845 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002846 DCHECK_EQ(value_type, Primitive::kPrimNot);
2847 DCHECK(!codegen_->IsLeafMethod());
2848 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
2849 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002850 }
2851 break;
2852 }
2853
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002854 case Primitive::kPrimLong: {
2855 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002856 if (index.IsConstant()) {
2857 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002858 if (value.IsRegisterPair()) {
2859 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
2860 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002861 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002862 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002863 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
2864 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
2865 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
2866 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002867 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002868 if (value.IsRegisterPair()) {
2869 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
2870 value.AsRegisterPairLow<Register>());
2871 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
2872 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002873 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002874 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002875 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002876 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002877 Immediate(Low32Bits(val)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002878 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002879 Immediate(High32Bits(val)));
2880 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002881 }
2882 break;
2883 }
2884
2885 case Primitive::kPrimFloat:
2886 case Primitive::kPrimDouble:
2887 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002888 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002889 case Primitive::kPrimVoid:
2890 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002891 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002892 }
2893}
2894
2895void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
2896 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002897 locations->SetInAt(0, Location::RequiresRegister());
2898 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002899 instruction->SetLocations(locations);
2900}
2901
2902void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
2903 LocationSummary* locations = instruction->GetLocations();
2904 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002905 Register obj = locations->InAt(0).As<Register>();
2906 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002907 __ movl(out, Address(obj, offset));
2908}
2909
2910void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002911 LocationSummary* locations =
2912 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002913 locations->SetInAt(0, Location::RequiresRegister());
2914 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002915 if (instruction->HasUses()) {
2916 locations->SetOut(Location::SameAsFirstInput());
2917 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002918}
2919
2920void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
2921 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002922 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002923 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002924 codegen_->AddSlowPath(slow_path);
2925
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002926 Register index = locations->InAt(0).As<Register>();
2927 Register length = locations->InAt(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002928
2929 __ cmpl(index, length);
2930 __ j(kAboveEqual, slow_path->GetEntryLabel());
2931}
2932
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002933void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
2934 temp->SetLocations(nullptr);
2935}
2936
2937void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
2938 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002939 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002940}
2941
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002942void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002943 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002944 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002945}
2946
2947void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002948 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2949}
2950
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002951void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
2952 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2953}
2954
2955void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002956 HBasicBlock* block = instruction->GetBlock();
2957 if (block->GetLoopInformation() != nullptr) {
2958 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2959 // The back edge will generate the suspend check.
2960 return;
2961 }
2962 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2963 // The goto will generate the suspend check.
2964 return;
2965 }
2966 GenerateSuspendCheck(instruction, nullptr);
2967}
2968
2969void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
2970 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002971 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002972 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002973 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002974 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002975 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002976 if (successor == nullptr) {
2977 __ j(kNotEqual, slow_path->GetEntryLabel());
2978 __ Bind(slow_path->GetReturnLabel());
2979 } else {
2980 __ j(kEqual, codegen_->GetLabelOf(successor));
2981 __ jmp(slow_path->GetEntryLabel());
2982 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002983}
2984
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002985X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
2986 return codegen_->GetAssembler();
2987}
2988
2989void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
2990 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002991 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002992 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2993 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
2994 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
2995}
2996
2997void ParallelMoveResolverX86::EmitMove(size_t index) {
2998 MoveOperands* move = moves_.Get(index);
2999 Location source = move->GetSource();
3000 Location destination = move->GetDestination();
3001
3002 if (source.IsRegister()) {
3003 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003004 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003005 } else {
3006 DCHECK(destination.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003007 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003008 }
3009 } else if (source.IsStackSlot()) {
3010 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003011 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003012 } else {
3013 DCHECK(destination.IsStackSlot());
3014 MoveMemoryToMemory(destination.GetStackIndex(),
3015 source.GetStackIndex());
3016 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003017 } else if (source.IsConstant()) {
3018 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
3019 Immediate imm(instruction->AsIntConstant()->GetValue());
3020 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003021 __ movl(destination.As<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003022 } else {
3023 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3024 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003025 } else {
3026 LOG(FATAL) << "Unimplemented";
3027 }
3028}
3029
3030void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003031 Register suggested_scratch = reg == EAX ? EBX : EAX;
3032 ScratchRegisterScope ensure_scratch(
3033 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3034
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003035 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3036 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
3037 __ movl(Address(ESP, mem + stack_offset), reg);
3038 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
3039}
3040
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003041void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
3042 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003043 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3044
3045 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003046 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003047 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3048
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003049 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
3050 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
3051 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
3052 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
3053 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
3054 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
3055}
3056
3057void ParallelMoveResolverX86::EmitSwap(size_t index) {
3058 MoveOperands* move = moves_.Get(index);
3059 Location source = move->GetSource();
3060 Location destination = move->GetDestination();
3061
3062 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003063 __ xchgl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003064 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003065 Exchange(source.As<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003066 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003067 Exchange(destination.As<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003068 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3069 Exchange(destination.GetStackIndex(), source.GetStackIndex());
3070 } else {
3071 LOG(FATAL) << "Unimplemented";
3072 }
3073}
3074
3075void ParallelMoveResolverX86::SpillScratch(int reg) {
3076 __ pushl(static_cast<Register>(reg));
3077}
3078
3079void ParallelMoveResolverX86::RestoreScratch(int reg) {
3080 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003081}
3082
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003083void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003084 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3085 ? LocationSummary::kCallOnSlowPath
3086 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003087 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003088 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003089 locations->SetOut(Location::RequiresRegister());
3090}
3091
3092void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
3093 Register out = cls->GetLocations()->Out().As<Register>();
3094 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003095 DCHECK(!cls->CanCallRuntime());
3096 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003097 codegen_->LoadCurrentMethod(out);
3098 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3099 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003100 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003101 codegen_->LoadCurrentMethod(out);
3102 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3103 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003104
3105 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3106 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3107 codegen_->AddSlowPath(slow_path);
3108 __ testl(out, out);
3109 __ j(kEqual, slow_path->GetEntryLabel());
3110 if (cls->MustGenerateClinitCheck()) {
3111 GenerateClassInitializationCheck(slow_path, out);
3112 } else {
3113 __ Bind(slow_path->GetExitLabel());
3114 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003115 }
3116}
3117
3118void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
3119 LocationSummary* locations =
3120 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3121 locations->SetInAt(0, Location::RequiresRegister());
3122 if (check->HasUses()) {
3123 locations->SetOut(Location::SameAsFirstInput());
3124 }
3125}
3126
3127void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003128 // We assume the class to not be null.
3129 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3130 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003131 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003132 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<Register>());
3133}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003134
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003135void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
3136 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003137 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3138 Immediate(mirror::Class::kStatusInitialized));
3139 __ j(kLess, slow_path->GetEntryLabel());
3140 __ Bind(slow_path->GetExitLabel());
3141 // No need for memory fence, thanks to the X86 memory model.
3142}
3143
3144void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3145 LocationSummary* locations =
3146 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3147 locations->SetInAt(0, Location::RequiresRegister());
3148 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3149}
3150
3151void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3152 LocationSummary* locations = instruction->GetLocations();
3153 Register cls = locations->InAt(0).As<Register>();
3154 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
3155
3156 switch (instruction->GetType()) {
3157 case Primitive::kPrimBoolean: {
3158 Register out = locations->Out().As<Register>();
3159 __ movzxb(out, Address(cls, offset));
3160 break;
3161 }
3162
3163 case Primitive::kPrimByte: {
3164 Register out = locations->Out().As<Register>();
3165 __ movsxb(out, Address(cls, offset));
3166 break;
3167 }
3168
3169 case Primitive::kPrimShort: {
3170 Register out = locations->Out().As<Register>();
3171 __ movsxw(out, Address(cls, offset));
3172 break;
3173 }
3174
3175 case Primitive::kPrimChar: {
3176 Register out = locations->Out().As<Register>();
3177 __ movzxw(out, Address(cls, offset));
3178 break;
3179 }
3180
3181 case Primitive::kPrimInt:
3182 case Primitive::kPrimNot: {
3183 Register out = locations->Out().As<Register>();
3184 __ movl(out, Address(cls, offset));
3185 break;
3186 }
3187
3188 case Primitive::kPrimLong: {
3189 // TODO: support volatile.
3190 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(cls, offset));
3191 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(cls, kX86WordSize + offset));
3192 break;
3193 }
3194
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003195 case Primitive::kPrimFloat: {
3196 XmmRegister out = locations->Out().As<XmmRegister>();
3197 __ movss(out, Address(cls, offset));
3198 break;
3199 }
3200
3201 case Primitive::kPrimDouble: {
3202 XmmRegister out = locations->Out().As<XmmRegister>();
3203 __ movsd(out, Address(cls, offset));
3204 break;
3205 }
3206
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003207 case Primitive::kPrimVoid:
3208 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3209 UNREACHABLE();
3210 }
3211}
3212
3213void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3214 LocationSummary* locations =
3215 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3216 locations->SetInAt(0, Location::RequiresRegister());
3217 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003218 bool needs_write_barrier =
3219 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003220 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3221 || (field_type == Primitive::kPrimByte);
3222 // The register allocator does not support multiple
3223 // inputs that die at entry with one in a specific register.
3224 if (is_byte_type) {
3225 // Ensure the value is in a byte register.
3226 locations->SetInAt(1, Location::RegisterLocation(EAX));
3227 } else {
3228 locations->SetInAt(1, Location::RequiresRegister());
3229 }
3230 // Temporary registers for the write barrier.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003231 if (needs_write_barrier) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003232 locations->AddTemp(Location::RequiresRegister());
3233 // Ensure the card is in a byte register.
3234 locations->AddTemp(Location::RegisterLocation(ECX));
3235 }
3236}
3237
3238void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3239 LocationSummary* locations = instruction->GetLocations();
3240 Register cls = locations->InAt(0).As<Register>();
3241 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
3242 Primitive::Type field_type = instruction->GetFieldType();
3243
3244 switch (field_type) {
3245 case Primitive::kPrimBoolean:
3246 case Primitive::kPrimByte: {
3247 ByteRegister value = locations->InAt(1).As<ByteRegister>();
3248 __ movb(Address(cls, offset), value);
3249 break;
3250 }
3251
3252 case Primitive::kPrimShort:
3253 case Primitive::kPrimChar: {
3254 Register value = locations->InAt(1).As<Register>();
3255 __ movw(Address(cls, offset), value);
3256 break;
3257 }
3258
3259 case Primitive::kPrimInt:
3260 case Primitive::kPrimNot: {
3261 Register value = locations->InAt(1).As<Register>();
3262 __ movl(Address(cls, offset), value);
3263
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003264 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003265 Register temp = locations->GetTemp(0).As<Register>();
3266 Register card = locations->GetTemp(1).As<Register>();
3267 codegen_->MarkGCCard(temp, card, cls, value);
3268 }
3269 break;
3270 }
3271
3272 case Primitive::kPrimLong: {
3273 Location value = locations->InAt(1);
3274 __ movl(Address(cls, offset), value.AsRegisterPairLow<Register>());
3275 __ movl(Address(cls, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3276 break;
3277 }
3278
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003279 case Primitive::kPrimFloat: {
3280 XmmRegister value = locations->InAt(1).As<XmmRegister>();
3281 __ movss(Address(cls, offset), value);
3282 break;
3283 }
3284
3285 case Primitive::kPrimDouble: {
3286 XmmRegister value = locations->InAt(1).As<XmmRegister>();
3287 __ movsd(Address(cls, offset), value);
3288 break;
3289 }
3290
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003291 case Primitive::kPrimVoid:
3292 LOG(FATAL) << "Unreachable type " << field_type;
3293 UNREACHABLE();
3294 }
3295}
3296
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003297void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
3298 LocationSummary* locations =
3299 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3300 locations->SetOut(Location::RequiresRegister());
3301}
3302
3303void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
3304 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
3305 codegen_->AddSlowPath(slow_path);
3306
3307 Register out = load->GetLocations()->Out().As<Register>();
3308 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08003309 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3310 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003311 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3312 __ testl(out, out);
3313 __ j(kEqual, slow_path->GetEntryLabel());
3314 __ Bind(slow_path->GetExitLabel());
3315}
3316
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003317void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
3318 LocationSummary* locations =
3319 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3320 locations->SetOut(Location::RequiresRegister());
3321}
3322
3323void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
3324 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
3325 __ fs()->movl(load->GetLocations()->Out().As<Register>(), address);
3326 __ fs()->movl(address, Immediate(0));
3327}
3328
3329void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
3330 LocationSummary* locations =
3331 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3332 InvokeRuntimeCallingConvention calling_convention;
3333 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3334}
3335
3336void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
3337 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
3338 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3339}
3340
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003341void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003342 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3343 ? LocationSummary::kNoCall
3344 : LocationSummary::kCallOnSlowPath;
3345 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3346 locations->SetInAt(0, Location::RequiresRegister());
3347 locations->SetInAt(1, Location::Any());
3348 locations->SetOut(Location::RequiresRegister());
3349}
3350
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003351void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003352 LocationSummary* locations = instruction->GetLocations();
3353 Register obj = locations->InAt(0).As<Register>();
3354 Location cls = locations->InAt(1);
3355 Register out = locations->Out().As<Register>();
3356 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3357 Label done, zero;
3358 SlowPathCodeX86* slow_path = nullptr;
3359
3360 // Return 0 if `obj` is null.
3361 // TODO: avoid this check if we know obj is not null.
3362 __ testl(obj, obj);
3363 __ j(kEqual, &zero);
3364 __ movl(out, Address(obj, class_offset));
3365 // Compare the class of `obj` with `cls`.
3366 if (cls.IsRegister()) {
3367 __ cmpl(out, cls.As<Register>());
3368 } else {
3369 DCHECK(cls.IsStackSlot()) << cls;
3370 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
3371 }
3372
3373 if (instruction->IsClassFinal()) {
3374 // Classes must be equal for the instanceof to succeed.
3375 __ j(kNotEqual, &zero);
3376 __ movl(out, Immediate(1));
3377 __ jmp(&done);
3378 } else {
3379 // If the classes are not equal, we go into a slow path.
3380 DCHECK(locations->OnlyCallsOnSlowPath());
3381 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003382 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003383 codegen_->AddSlowPath(slow_path);
3384 __ j(kNotEqual, slow_path->GetEntryLabel());
3385 __ movl(out, Immediate(1));
3386 __ jmp(&done);
3387 }
3388 __ Bind(&zero);
3389 __ movl(out, Immediate(0));
3390 if (slow_path != nullptr) {
3391 __ Bind(slow_path->GetExitLabel());
3392 }
3393 __ Bind(&done);
3394}
3395
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003396void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
3397 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3398 instruction, LocationSummary::kCallOnSlowPath);
3399 locations->SetInAt(0, Location::RequiresRegister());
3400 locations->SetInAt(1, Location::Any());
3401 locations->AddTemp(Location::RequiresRegister());
3402}
3403
3404void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
3405 LocationSummary* locations = instruction->GetLocations();
3406 Register obj = locations->InAt(0).As<Register>();
3407 Location cls = locations->InAt(1);
3408 Register temp = locations->GetTemp(0).As<Register>();
3409 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3410 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
3411 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3412 codegen_->AddSlowPath(slow_path);
3413
3414 // TODO: avoid this check if we know obj is not null.
3415 __ testl(obj, obj);
3416 __ j(kEqual, slow_path->GetExitLabel());
3417 __ movl(temp, Address(obj, class_offset));
3418
3419 // Compare the class of `obj` with `cls`.
3420 if (cls.IsRegister()) {
3421 __ cmpl(temp, cls.As<Register>());
3422 } else {
3423 DCHECK(cls.IsStackSlot()) << cls;
3424 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
3425 }
3426
3427 __ j(kNotEqual, slow_path->GetEntryLabel());
3428 __ Bind(slow_path->GetExitLabel());
3429}
3430
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003431void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3432 LocationSummary* locations =
3433 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3434 InvokeRuntimeCallingConvention calling_convention;
3435 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3436}
3437
3438void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3439 __ fs()->call(Address::Absolute(instruction->IsEnter()
3440 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
3441 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
3442 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3443}
3444
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003445void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3446void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3447void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3448
3449void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3450 LocationSummary* locations =
3451 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3452 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3453 || instruction->GetResultType() == Primitive::kPrimLong);
3454 locations->SetInAt(0, Location::RequiresRegister());
3455 locations->SetInAt(1, Location::Any());
3456 locations->SetOut(Location::SameAsFirstInput());
3457}
3458
3459void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
3460 HandleBitwiseOperation(instruction);
3461}
3462
3463void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
3464 HandleBitwiseOperation(instruction);
3465}
3466
3467void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
3468 HandleBitwiseOperation(instruction);
3469}
3470
3471void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3472 LocationSummary* locations = instruction->GetLocations();
3473 Location first = locations->InAt(0);
3474 Location second = locations->InAt(1);
3475 DCHECK(first.Equals(locations->Out()));
3476
3477 if (instruction->GetResultType() == Primitive::kPrimInt) {
3478 if (second.IsRegister()) {
3479 if (instruction->IsAnd()) {
3480 __ andl(first.As<Register>(), second.As<Register>());
3481 } else if (instruction->IsOr()) {
3482 __ orl(first.As<Register>(), second.As<Register>());
3483 } else {
3484 DCHECK(instruction->IsXor());
3485 __ xorl(first.As<Register>(), second.As<Register>());
3486 }
3487 } else if (second.IsConstant()) {
3488 if (instruction->IsAnd()) {
3489 __ andl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3490 } else if (instruction->IsOr()) {
3491 __ orl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3492 } else {
3493 DCHECK(instruction->IsXor());
3494 __ xorl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3495 }
3496 } else {
3497 if (instruction->IsAnd()) {
3498 __ andl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
3499 } else if (instruction->IsOr()) {
3500 __ orl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
3501 } else {
3502 DCHECK(instruction->IsXor());
3503 __ xorl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
3504 }
3505 }
3506 } else {
3507 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3508 if (second.IsRegisterPair()) {
3509 if (instruction->IsAnd()) {
3510 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3511 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3512 } else if (instruction->IsOr()) {
3513 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3514 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3515 } else {
3516 DCHECK(instruction->IsXor());
3517 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3518 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3519 }
3520 } else {
3521 if (instruction->IsAnd()) {
3522 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3523 __ andl(first.AsRegisterPairHigh<Register>(),
3524 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3525 } else if (instruction->IsOr()) {
3526 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3527 __ orl(first.AsRegisterPairHigh<Register>(),
3528 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3529 } else {
3530 DCHECK(instruction->IsXor());
3531 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3532 __ xorl(first.AsRegisterPairHigh<Register>(),
3533 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3534 }
3535 }
3536 }
3537}
3538
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003539} // namespace x86
3540} // namespace art