blob: d7ce38753fbf6736b3b694bd810ad823c989628e [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
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010039static constexpr Register kRuntimeParameterCoreRegisters[] = { EAX, ECX, EDX };
40static constexpr size_t kRuntimeParameterCoreRegistersLength =
41 arraysize(kRuntimeParameterCoreRegisters);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010042static constexpr XmmRegister kRuntimeParameterFpuRegisters[] = { };
43static constexpr size_t kRuntimeParameterFpuRegistersLength = 0;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010044
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010045class InvokeRuntimeCallingConvention : public CallingConvention<Register, XmmRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010046 public:
47 InvokeRuntimeCallingConvention()
48 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010049 kRuntimeParameterCoreRegistersLength,
50 kRuntimeParameterFpuRegisters,
51 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010052
53 private:
54 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
55};
56
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
58
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010059class SlowPathCodeX86 : public SlowPathCode {
60 public:
61 SlowPathCodeX86() : entry_label_(), exit_label_() {}
62
63 Label* GetEntryLabel() { return &entry_label_; }
64 Label* GetExitLabel() { return &exit_label_; }
65
66 private:
67 Label entry_label_;
68 Label exit_label_;
69
70 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86);
71};
72
73class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010074 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010075 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076
77 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
78 __ Bind(GetEntryLabel());
79 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010080 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010081 }
82
83 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010084 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010085 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
86};
87
Calin Juravled0d48522014-11-04 16:40:20 +000088class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
89 public:
90 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
91
92 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
93 __ Bind(GetEntryLabel());
94 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
95 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
96 }
97
98 private:
99 HDivZeroCheck* const instruction_;
100 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
101};
102
103class DivMinusOneSlowPathX86 : public SlowPathCodeX86 {
104 public:
105 explicit DivMinusOneSlowPathX86(Register reg) : reg_(reg) {}
106
107 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
108 __ Bind(GetEntryLabel());
109 __ negl(reg_);
110 __ jmp(GetExitLabel());
111 }
112
113 private:
114 Register reg_;
115 DISALLOW_COPY_AND_ASSIGN(DivMinusOneSlowPathX86);
116};
117
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100118class StackOverflowCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100119 public:
120 StackOverflowCheckSlowPathX86() {}
121
122 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
123 __ Bind(GetEntryLabel());
124 __ addl(ESP,
125 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
126 __ fs()->jmp(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowStackOverflow)));
127 }
128
129 private:
130 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86);
131};
132
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100133class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100134 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100135 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
136 Location index_location,
137 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100138 : instruction_(instruction), index_location_(index_location), length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100139
140 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100141 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100142 __ Bind(GetEntryLabel());
143 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100144 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), index_location_);
145 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(1)), length_location_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100146 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100147 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100148 }
149
150 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100151 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100152 const Location index_location_;
153 const Location length_location_;
154
155 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
156};
157
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100158class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000159 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100160 explicit SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
161 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000162
163 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100164 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000165 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100166 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000167 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
168 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100169 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100170 if (successor_ == nullptr) {
171 __ jmp(GetReturnLabel());
172 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100173 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100174 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000175 }
176
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100177 Label* GetReturnLabel() {
178 DCHECK(successor_ == nullptr);
179 return &return_label_;
180 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000181
182 private:
183 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100184 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000185 Label return_label_;
186
187 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
188};
189
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000190class LoadStringSlowPathX86 : public SlowPathCodeX86 {
191 public:
192 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
193
194 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
195 LocationSummary* locations = instruction_->GetLocations();
196 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
197
198 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
199 __ Bind(GetEntryLabel());
200 codegen->SaveLiveRegisters(locations);
201
202 InvokeRuntimeCallingConvention calling_convention;
203 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(0));
204 __ movl(calling_convention.GetRegisterAt(1), Immediate(instruction_->GetStringIndex()));
205 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
206 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
207 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
208 codegen->RestoreLiveRegisters(locations);
209
210 __ jmp(GetExitLabel());
211 }
212
213 private:
214 HLoadString* const instruction_;
215
216 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
217};
218
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000219class LoadClassSlowPathX86 : public SlowPathCodeX86 {
220 public:
221 LoadClassSlowPathX86(HLoadClass* cls,
222 HInstruction* at,
223 uint32_t dex_pc,
224 bool do_clinit)
225 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
226 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
227 }
228
229 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
230 LocationSummary* locations = at_->GetLocations();
231 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
232 __ Bind(GetEntryLabel());
233 codegen->SaveLiveRegisters(locations);
234
235 InvokeRuntimeCallingConvention calling_convention;
236 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
237 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
238 __ fs()->call(Address::Absolute(do_clinit_
239 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
240 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
241 codegen->RecordPcInfo(at_, dex_pc_);
242
243 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000244 Location out = locations->Out();
245 if (out.IsValid()) {
246 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
247 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000248 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000249
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000250 codegen->RestoreLiveRegisters(locations);
251 __ jmp(GetExitLabel());
252 }
253
254 private:
255 // The class this slow path will load.
256 HLoadClass* const cls_;
257
258 // The instruction where this slow path is happening.
259 // (Might be the load class or an initialization check).
260 HInstruction* const at_;
261
262 // The dex PC of `at_`.
263 const uint32_t dex_pc_;
264
265 // Whether to initialize the class.
266 const bool do_clinit_;
267
268 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
269};
270
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000271class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
272 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000273 TypeCheckSlowPathX86(HInstruction* instruction,
274 Location class_to_check,
275 Location object_class,
276 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000277 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000278 class_to_check_(class_to_check),
279 object_class_(object_class),
280 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000281
282 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
283 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000284 DCHECK(instruction_->IsCheckCast()
285 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000286
287 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
288 __ Bind(GetEntryLabel());
289 codegen->SaveLiveRegisters(locations);
290
291 // We're moving two locations to locations that could overlap, so we need a parallel
292 // move resolver.
293 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000294 MoveOperands move1(class_to_check_,
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000295 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
296 nullptr);
297 MoveOperands move2(object_class_,
298 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
299 nullptr);
300 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
301 parallel_move.AddMove(&move1);
302 parallel_move.AddMove(&move2);
303 x86_codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
304
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000305 if (instruction_->IsInstanceOf()) {
306 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInstanceofNonTrivial)));
307 } else {
308 DCHECK(instruction_->IsCheckCast());
309 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
310 }
311
312 codegen->RecordPcInfo(instruction_, dex_pc_);
313 if (instruction_->IsInstanceOf()) {
314 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
315 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000316 codegen->RestoreLiveRegisters(locations);
317
318 __ jmp(GetExitLabel());
319 }
320
321 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000322 HInstruction* const instruction_;
323 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000325 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000326
327 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
328};
329
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100330#undef __
331#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
332
Dave Allison20dfc792014-06-16 20:44:29 -0700333inline Condition X86Condition(IfCondition cond) {
334 switch (cond) {
335 case kCondEQ: return kEqual;
336 case kCondNE: return kNotEqual;
337 case kCondLT: return kLess;
338 case kCondLE: return kLessEqual;
339 case kCondGT: return kGreater;
340 case kCondGE: return kGreaterEqual;
341 default:
342 LOG(FATAL) << "Unknown if condition";
343 }
344 return kEqual;
345}
346
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100347void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
348 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
349}
350
351void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
352 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
353}
354
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100355size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
356 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
357 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100358}
359
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100360size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
361 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
362 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100363}
364
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100365CodeGeneratorX86::CodeGeneratorX86(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100366 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfXmmRegisters, kNumberOfRegisterPairs),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100367 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100368 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100369 instruction_visitor_(graph, this),
370 move_resolver_(graph->GetArena(), this) {}
371
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100372size_t CodeGeneratorX86::FrameEntrySpillSize() const {
373 return kNumberOfPushedRegistersAtEntry * kX86WordSize;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100374}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100375
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100376Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100377 switch (type) {
378 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100379 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100380 X86ManagedRegister pair =
381 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100382 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
383 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100384 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
385 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100386 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100387 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100388 }
389
390 case Primitive::kPrimByte:
391 case Primitive::kPrimBoolean:
392 case Primitive::kPrimChar:
393 case Primitive::kPrimShort:
394 case Primitive::kPrimInt:
395 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100396 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100397 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100398 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100399 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
400 X86ManagedRegister current =
401 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
402 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100403 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100404 }
405 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100406 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100407 }
408
409 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100410 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100411 return Location::FpuRegisterLocation(
412 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100413 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100414
415 case Primitive::kPrimVoid:
416 LOG(FATAL) << "Unreachable type " << type;
417 }
418
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100419 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100420}
421
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100422void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100423 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100424 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100425
426 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100427 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100428
429 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100430 blocked_core_registers_[EBP] = true;
431 blocked_core_registers_[ESI] = true;
432 blocked_core_registers_[EDI] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100433
434 UpdateBlockedPairRegisters();
435}
436
437void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
438 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
439 X86ManagedRegister current =
440 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
441 if (blocked_core_registers_[current.AsRegisterPairLow()]
442 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
443 blocked_register_pairs_[i] = true;
444 }
445 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100446}
447
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100448InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
449 : HGraphVisitor(graph),
450 assembler_(codegen->GetAssembler()),
451 codegen_(codegen) {}
452
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000453void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000454 // Create a fake register to mimic Quick.
455 static const int kFakeReturnRegister = 8;
456 core_spill_mask_ |= (1 << kFakeReturnRegister);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000457
Dave Allison648d7112014-07-25 16:15:27 -0700458 bool skip_overflow_check = IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100459 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
460 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100461 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100462 }
463
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100464 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100465 __ subl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100466
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100467 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100468 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86();
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100469 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100470
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100471 __ fs()->cmpl(ESP, Address::Absolute(Thread::StackEndOffset<kX86WordSize>()));
472 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100473 }
474
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100475 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000476}
477
478void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100479 __ addl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000480}
481
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100482void CodeGeneratorX86::Bind(HBasicBlock* block) {
483 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000484}
485
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100486void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100487 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000488}
489
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100490Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
491 switch (load->GetType()) {
492 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100493 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100494 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
495 break;
496
497 case Primitive::kPrimInt:
498 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100499 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100500 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100501
502 case Primitive::kPrimBoolean:
503 case Primitive::kPrimByte:
504 case Primitive::kPrimChar:
505 case Primitive::kPrimShort:
506 case Primitive::kPrimVoid:
507 LOG(FATAL) << "Unexpected type " << load->GetType();
508 }
509
510 LOG(FATAL) << "Unreachable";
511 return Location();
512}
513
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100514Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
515 switch (type) {
516 case Primitive::kPrimBoolean:
517 case Primitive::kPrimByte:
518 case Primitive::kPrimChar:
519 case Primitive::kPrimShort:
520 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100521 case Primitive::kPrimFloat:
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100522 case Primitive::kPrimNot: {
523 uint32_t index = gp_index_++;
524 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100525 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100526 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100527 return Location::StackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100528 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100529 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100530
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100531 case Primitive::kPrimLong:
532 case Primitive::kPrimDouble: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100533 uint32_t index = gp_index_;
534 gp_index_ += 2;
535 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100536 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
537 calling_convention.GetRegisterPairAt(index));
538 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100539 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000540 // On X86, the register index and stack index of a quick parameter is the same, since
541 // we are passing floating pointer values in core registers.
542 return Location::QuickParameter(index, index);
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100543 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100544 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100545 }
546 }
547
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100548 case Primitive::kPrimVoid:
549 LOG(FATAL) << "Unexpected parameter type " << type;
550 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100551 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100552 return Location();
553}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100554
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100555void CodeGeneratorX86::Move32(Location destination, Location source) {
556 if (source.Equals(destination)) {
557 return;
558 }
559 if (destination.IsRegister()) {
560 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100561 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100562 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100563 __ movd(destination.As<Register>(), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100564 } else {
565 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100566 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100567 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100568 } else if (destination.IsFpuRegister()) {
569 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100570 __ movd(destination.As<XmmRegister>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100571 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100572 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100573 } else {
574 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100575 __ movss(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100576 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100577 } else {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100578 DCHECK(destination.IsStackSlot());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100579 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100580 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100581 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100582 __ movss(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100583 } else {
584 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100585 __ pushl(Address(ESP, source.GetStackIndex()));
586 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100587 }
588 }
589}
590
591void CodeGeneratorX86::Move64(Location destination, Location source) {
592 if (source.Equals(destination)) {
593 return;
594 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100595 if (destination.IsRegisterPair()) {
596 if (source.IsRegisterPair()) {
597 __ movl(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
598 __ movl(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100599 } else if (source.IsFpuRegister()) {
600 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100601 } else if (source.IsQuickParameter()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000602 uint16_t register_index = source.GetQuickParameterRegisterIndex();
603 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100604 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100605 __ movl(destination.AsRegisterPairLow<Register>(),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000606 calling_convention.GetRegisterAt(register_index));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100607 __ movl(destination.AsRegisterPairHigh<Register>(), Address(ESP,
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000608 calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100609 } else {
610 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100611 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
612 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100613 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
614 }
615 } else if (destination.IsQuickParameter()) {
616 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000617 uint16_t register_index = destination.GetQuickParameterRegisterIndex();
618 uint16_t stack_index = destination.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100619 if (source.IsRegister()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000620 __ movl(calling_convention.GetRegisterAt(register_index), source.AsRegisterPairLow<Register>());
621 __ movl(Address(ESP, calling_convention.GetStackOffsetOf(stack_index + 1)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100622 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100623 } else if (source.IsFpuRegister()) {
624 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100625 } else {
626 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000627 __ movl(calling_convention.GetRegisterAt(register_index),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100628 Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100629 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000630 __ popl(Address(ESP, calling_convention.GetStackOffsetOf(stack_index + 1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100631 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100632 } else if (destination.IsFpuRegister()) {
633 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100634 __ movsd(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100635 } else {
636 LOG(FATAL) << "Unimplemented";
637 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100638 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100639 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100640 if (source.IsRegisterPair()) {
641 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100642 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100643 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100644 } else if (source.IsQuickParameter()) {
645 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000646 uint16_t register_index = source.GetQuickParameterRegisterIndex();
647 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100648 __ movl(Address(ESP, destination.GetStackIndex()),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000649 calling_convention.GetRegisterAt(register_index));
650 DCHECK_EQ(calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100651 static_cast<size_t>(destination.GetHighStackIndex(kX86WordSize)));
652 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100653 __ movsd(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100654 } else {
655 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100656 __ pushl(Address(ESP, source.GetStackIndex()));
657 __ popl(Address(ESP, destination.GetStackIndex()));
658 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
659 __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100660 }
661 }
662}
663
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100664void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Roland Levillain476df552014-10-09 17:51:36 +0100665 if (instruction->IsIntConstant()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100666 Immediate imm(instruction->AsIntConstant()->GetValue());
667 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100668 __ movl(location.As<Register>(), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100669 } else if (location.IsStackSlot()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100670 __ movl(Address(ESP, location.GetStackIndex()), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100671 } else {
672 DCHECK(location.IsConstant());
673 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100674 }
Roland Levillain476df552014-10-09 17:51:36 +0100675 } else if (instruction->IsLongConstant()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100676 int64_t value = instruction->AsLongConstant()->GetValue();
677 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100678 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
679 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100680 } else if (location.IsDoubleStackSlot()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100681 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
682 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100683 } else {
684 DCHECK(location.IsConstant());
685 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100686 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000687 } else if (instruction->IsTemporary()) {
688 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000689 if (temp_location.IsStackSlot()) {
690 Move32(location, temp_location);
691 } else {
692 DCHECK(temp_location.IsDoubleStackSlot());
693 Move64(location, temp_location);
694 }
Roland Levillain476df552014-10-09 17:51:36 +0100695 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100696 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100697 switch (instruction->GetType()) {
698 case Primitive::kPrimBoolean:
699 case Primitive::kPrimByte:
700 case Primitive::kPrimChar:
701 case Primitive::kPrimShort:
702 case Primitive::kPrimInt:
703 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100704 case Primitive::kPrimFloat:
705 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100706 break;
707
708 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100709 case Primitive::kPrimDouble:
710 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100711 break;
712
713 default:
714 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
715 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000716 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100717 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100718 switch (instruction->GetType()) {
719 case Primitive::kPrimBoolean:
720 case Primitive::kPrimByte:
721 case Primitive::kPrimChar:
722 case Primitive::kPrimShort:
723 case Primitive::kPrimInt:
724 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100725 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100726 Move32(location, instruction->GetLocations()->Out());
727 break;
728
729 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100730 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100731 Move64(location, instruction->GetLocations()->Out());
732 break;
733
734 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100735 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100736 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000737 }
738}
739
740void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000741 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000742}
743
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000744void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000745 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100746 DCHECK(!successor->IsExitBlock());
747
748 HBasicBlock* block = got->GetBlock();
749 HInstruction* previous = got->GetPrevious();
750
751 HLoopInformation* info = block->GetLoopInformation();
752 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
753 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
754 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
755 return;
756 }
757
758 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
759 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
760 }
761 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000762 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000763 }
764}
765
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000766void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000767 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000768}
769
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000770void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700771 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000772 if (kIsDebugBuild) {
773 __ Comment("Unreachable");
774 __ int3();
775 }
776}
777
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000778void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100779 LocationSummary* locations =
780 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100781 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100782 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100783 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100784 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000785}
786
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000787void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700788 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100789 if (cond->IsIntConstant()) {
790 // Constant condition, statically compared against 1.
791 int32_t cond_value = cond->AsIntConstant()->GetValue();
792 if (cond_value == 1) {
793 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
794 if_instr->IfTrueSuccessor())) {
795 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100796 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100797 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100798 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100799 DCHECK_EQ(cond_value, 0);
800 }
801 } else {
802 bool materialized =
803 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
804 // Moves do not affect the eflags register, so if the condition is
805 // evaluated just before the if, we don't need to evaluate it
806 // again.
807 bool eflags_set = cond->IsCondition()
808 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
809 if (materialized) {
810 if (!eflags_set) {
811 // Materialized condition, compare against 0.
812 Location lhs = if_instr->GetLocations()->InAt(0);
813 if (lhs.IsRegister()) {
814 __ cmpl(lhs.As<Register>(), Immediate(0));
815 } else {
816 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
817 }
818 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
819 } else {
820 __ j(X86Condition(cond->AsCondition()->GetCondition()),
821 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
822 }
823 } else {
824 Location lhs = cond->GetLocations()->InAt(0);
825 Location rhs = cond->GetLocations()->InAt(1);
826 // LHS is guaranteed to be in a register (see
827 // LocationsBuilderX86::VisitCondition).
828 if (rhs.IsRegister()) {
829 __ cmpl(lhs.As<Register>(), rhs.As<Register>());
830 } else if (rhs.IsConstant()) {
831 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
832 Immediate imm(instruction->AsIntConstant()->GetValue());
833 __ cmpl(lhs.As<Register>(), imm);
834 } else {
835 __ cmpl(lhs.As<Register>(), Address(ESP, rhs.GetStackIndex()));
836 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100837 __ j(X86Condition(cond->AsCondition()->GetCondition()),
838 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700839 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100840 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100841 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
842 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700843 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000844 }
845}
846
847void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000848 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000849}
850
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000851void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
852 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000853}
854
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000855void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100856 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000857}
858
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000859void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100860 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700861 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000862}
863
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100864void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100865 LocationSummary* locations =
866 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100867 switch (store->InputAt(1)->GetType()) {
868 case Primitive::kPrimBoolean:
869 case Primitive::kPrimByte:
870 case Primitive::kPrimChar:
871 case Primitive::kPrimShort:
872 case Primitive::kPrimInt:
873 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100874 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100875 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
876 break;
877
878 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100879 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100880 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
881 break;
882
883 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100884 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100885 }
886 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000887}
888
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000889void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700890 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000891}
892
Dave Allison20dfc792014-06-16 20:44:29 -0700893void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100894 LocationSummary* locations =
895 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100896 locations->SetInAt(0, Location::RequiresRegister());
897 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100898 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100899 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100900 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000901}
902
Dave Allison20dfc792014-06-16 20:44:29 -0700903void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
904 if (comp->NeedsMaterialization()) {
905 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100906 Register reg = locations->Out().As<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100907 // Clear register: setcc only sets the low byte.
908 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700909 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100910 __ cmpl(locations->InAt(0).As<Register>(),
911 locations->InAt(1).As<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100912 } else if (locations->InAt(1).IsConstant()) {
913 HConstant* instruction = locations->InAt(1).GetConstant();
914 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100915 __ cmpl(locations->InAt(0).As<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700916 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100917 __ cmpl(locations->InAt(0).As<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700918 Address(ESP, locations->InAt(1).GetStackIndex()));
919 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100920 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100921 }
Dave Allison20dfc792014-06-16 20:44:29 -0700922}
923
924void LocationsBuilderX86::VisitEqual(HEqual* comp) {
925 VisitCondition(comp);
926}
927
928void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
929 VisitCondition(comp);
930}
931
932void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
933 VisitCondition(comp);
934}
935
936void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
937 VisitCondition(comp);
938}
939
940void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
941 VisitCondition(comp);
942}
943
944void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
945 VisitCondition(comp);
946}
947
948void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
949 VisitCondition(comp);
950}
951
952void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
953 VisitCondition(comp);
954}
955
956void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
957 VisitCondition(comp);
958}
959
960void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
961 VisitCondition(comp);
962}
963
964void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
965 VisitCondition(comp);
966}
967
968void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
969 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000970}
971
972void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100973 LocationSummary* locations =
974 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100975 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000976}
977
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000978void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100979 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700980 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000981}
982
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100983void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100984 LocationSummary* locations =
985 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100986 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100987}
988
989void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
990 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700991 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100992}
993
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100994void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
995 LocationSummary* locations =
996 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
997 locations->SetOut(Location::ConstantLocation(constant));
998}
999
1000void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1001 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001002 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001003}
1004
1005void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1006 LocationSummary* locations =
1007 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1008 locations->SetOut(Location::ConstantLocation(constant));
1009}
1010
1011void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1012 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001013 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001014}
1015
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001016void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001017 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001018}
1019
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001020void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001021 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001022 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001023 __ ret();
1024}
1025
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001026void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001027 LocationSummary* locations =
1028 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001029 switch (ret->InputAt(0)->GetType()) {
1030 case Primitive::kPrimBoolean:
1031 case Primitive::kPrimByte:
1032 case Primitive::kPrimChar:
1033 case Primitive::kPrimShort:
1034 case Primitive::kPrimInt:
1035 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001036 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001037 break;
1038
1039 case Primitive::kPrimLong:
1040 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001041 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001042 break;
1043
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001044 case Primitive::kPrimFloat:
1045 case Primitive::kPrimDouble:
1046 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001047 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001048 break;
1049
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001050 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001051 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001052 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001053}
1054
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001055void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001056 if (kIsDebugBuild) {
1057 switch (ret->InputAt(0)->GetType()) {
1058 case Primitive::kPrimBoolean:
1059 case Primitive::kPrimByte:
1060 case Primitive::kPrimChar:
1061 case Primitive::kPrimShort:
1062 case Primitive::kPrimInt:
1063 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001064 DCHECK_EQ(ret->GetLocations()->InAt(0).As<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001065 break;
1066
1067 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001068 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1069 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001070 break;
1071
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001072 case Primitive::kPrimFloat:
1073 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001074 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001075 break;
1076
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001077 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001078 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001079 }
1080 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001081 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001082 __ ret();
1083}
1084
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001085void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001086 HandleInvoke(invoke);
1087}
1088
1089void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001090 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001091
1092 // TODO: Implement all kinds of calls:
1093 // 1) boot -> boot
1094 // 2) app -> boot
1095 // 3) app -> app
1096 //
1097 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1098
1099 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001100 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001101 // temp = temp->dex_cache_resolved_methods_;
1102 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
1103 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001104 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001105 // (temp + offset_of_quick_compiled_code)()
1106 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1107
1108 DCHECK(!codegen_->IsLeafMethod());
1109 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1110}
1111
1112void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1113 HandleInvoke(invoke);
1114}
1115
1116void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001117 LocationSummary* locations =
1118 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001119 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001120
1121 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001122 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001123 HInstruction* input = invoke->InputAt(i);
1124 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1125 }
1126
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001127 switch (invoke->GetType()) {
1128 case Primitive::kPrimBoolean:
1129 case Primitive::kPrimByte:
1130 case Primitive::kPrimChar:
1131 case Primitive::kPrimShort:
1132 case Primitive::kPrimInt:
1133 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001134 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001135 break;
1136
1137 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001138 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001139 break;
1140
1141 case Primitive::kPrimVoid:
1142 break;
1143
1144 case Primitive::kPrimDouble:
1145 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001146 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001147 break;
1148 }
1149
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001150 invoke->SetLocations(locations);
1151}
1152
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001153void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001154 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001155 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1156 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1157 LocationSummary* locations = invoke->GetLocations();
1158 Location receiver = locations->InAt(0);
1159 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1160 // temp = object->GetClass();
1161 if (receiver.IsStackSlot()) {
1162 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1163 __ movl(temp, Address(temp, class_offset));
1164 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001165 __ movl(temp, Address(receiver.As<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001166 }
1167 // temp = temp->GetMethodAt(method_offset);
1168 __ movl(temp, Address(temp, method_offset));
1169 // call temp->GetEntryPoint();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001170 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1171
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001172 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001173 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001174}
1175
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001176void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1177 HandleInvoke(invoke);
1178 // Add the hidden argument.
1179 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM0));
1180}
1181
1182void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1183 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1184 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
1185 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1186 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1187 LocationSummary* locations = invoke->GetLocations();
1188 Location receiver = locations->InAt(0);
1189 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1190
1191 // Set the hidden argument.
1192 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
1193 __ movd(invoke->GetLocations()->GetTemp(1).As<XmmRegister>(), temp);
1194
1195 // temp = object->GetClass();
1196 if (receiver.IsStackSlot()) {
1197 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1198 __ movl(temp, Address(temp, class_offset));
1199 } else {
1200 __ movl(temp, Address(receiver.As<Register>(), class_offset));
1201 }
1202 // temp = temp->GetImtEntryAt(method_offset);
1203 __ movl(temp, Address(temp, method_offset));
1204 // call temp->GetEntryPoint();
1205 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1206
1207 DCHECK(!codegen_->IsLeafMethod());
1208 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1209}
1210
Roland Levillain88cb1752014-10-20 16:36:47 +01001211void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1212 LocationSummary* locations =
1213 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1214 switch (neg->GetResultType()) {
1215 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001216 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001217 locations->SetInAt(0, Location::RequiresRegister());
1218 locations->SetOut(Location::SameAsFirstInput());
1219 break;
1220
Roland Levillain88cb1752014-10-20 16:36:47 +01001221 case Primitive::kPrimFloat:
1222 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001223 locations->SetInAt(0, Location::RequiresFpuRegister());
1224 // Output overlaps as we need a fresh (zero-initialized)
1225 // register to perform subtraction from zero.
1226 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001227 break;
1228
1229 default:
1230 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1231 }
1232}
1233
1234void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1235 LocationSummary* locations = neg->GetLocations();
1236 Location out = locations->Out();
1237 Location in = locations->InAt(0);
1238 switch (neg->GetResultType()) {
1239 case Primitive::kPrimInt:
1240 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001241 DCHECK(in.Equals(out));
Roland Levillain88cb1752014-10-20 16:36:47 +01001242 __ negl(out.As<Register>());
1243 break;
1244
1245 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001246 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001247 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001248 __ negl(out.AsRegisterPairLow<Register>());
1249 // Negation is similar to subtraction from zero. The least
1250 // significant byte triggers a borrow when it is different from
1251 // zero; to take it into account, add 1 to the most significant
1252 // byte if the carry flag (CF) is set to 1 after the first NEGL
1253 // operation.
1254 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1255 __ negl(out.AsRegisterPairHigh<Register>());
1256 break;
1257
Roland Levillain88cb1752014-10-20 16:36:47 +01001258 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001259 DCHECK(!in.Equals(out));
1260 // out = 0
1261 __ xorps(out.As<XmmRegister>(), out.As<XmmRegister>());
1262 // out = out - in
1263 __ subss(out.As<XmmRegister>(), in.As<XmmRegister>());
1264 break;
1265
Roland Levillain88cb1752014-10-20 16:36:47 +01001266 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001267 DCHECK(!in.Equals(out));
1268 // out = 0
1269 __ xorpd(out.As<XmmRegister>(), out.As<XmmRegister>());
1270 // out = out - in
1271 __ subsd(out.As<XmmRegister>(), in.As<XmmRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001272 break;
1273
1274 default:
1275 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1276 }
1277}
1278
Roland Levillaindff1f282014-11-05 14:15:05 +00001279void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
1280 LocationSummary* locations =
1281 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1282 Primitive::Type result_type = conversion->GetResultType();
1283 Primitive::Type input_type = conversion->GetInputType();
1284 switch (result_type) {
Roland Levillain946e1432014-11-11 17:35:19 +00001285 case Primitive::kPrimInt:
1286 switch (input_type) {
1287 case Primitive::kPrimLong:
1288 // long-to-int conversion.
1289 locations->SetInAt(0, Location::Any());
1290 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1291 break;
1292
1293 case Primitive::kPrimFloat:
1294 case Primitive::kPrimDouble:
1295 LOG(FATAL) << "Type conversion from " << input_type
1296 << " to " << result_type << " not yet implemented";
1297 break;
1298
1299 default:
1300 LOG(FATAL) << "Unexpected type conversion from " << input_type
1301 << " to " << result_type;
1302 }
1303 break;
1304
Roland Levillaindff1f282014-11-05 14:15:05 +00001305 case Primitive::kPrimLong:
1306 switch (input_type) {
1307 case Primitive::kPrimByte:
1308 case Primitive::kPrimShort:
1309 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001310 case Primitive::kPrimChar:
Roland Levillaindff1f282014-11-05 14:15:05 +00001311 // int-to-long conversion.
1312 locations->SetInAt(0, Location::RegisterLocation(EAX));
1313 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1314 break;
1315
1316 case Primitive::kPrimFloat:
1317 case Primitive::kPrimDouble:
1318 LOG(FATAL) << "Type conversion from " << input_type << " to "
1319 << result_type << " not yet implemented";
1320 break;
1321
1322 default:
1323 LOG(FATAL) << "Unexpected type conversion from " << input_type
1324 << " to " << result_type;
1325 }
1326 break;
1327
Roland Levillaindff1f282014-11-05 14:15:05 +00001328 case Primitive::kPrimFloat:
1329 case Primitive::kPrimDouble:
1330 LOG(FATAL) << "Type conversion from " << input_type
1331 << " to " << result_type << " not yet implemented";
1332 break;
1333
1334 default:
1335 LOG(FATAL) << "Unexpected type conversion from " << input_type
1336 << " to " << result_type;
1337 }
1338}
1339
1340void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1341 LocationSummary* locations = conversion->GetLocations();
1342 Location out = locations->Out();
1343 Location in = locations->InAt(0);
1344 Primitive::Type result_type = conversion->GetResultType();
1345 Primitive::Type input_type = conversion->GetInputType();
1346 switch (result_type) {
Roland Levillain946e1432014-11-11 17:35:19 +00001347 case Primitive::kPrimInt:
1348 switch (input_type) {
1349 case Primitive::kPrimLong:
1350 // long-to-int conversion.
1351 if (in.IsRegisterPair()) {
1352 __ movl(out.As<Register>(), in.AsRegisterPairLow<Register>());
1353 } else if (in.IsDoubleStackSlot()) {
1354 __ movl(out.As<Register>(), Address(ESP, in.GetStackIndex()));
1355 } else {
1356 DCHECK(in.IsConstant());
1357 DCHECK(in.GetConstant()->IsLongConstant());
1358 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
1359 __ movl(out.As<Register>(), Immediate(static_cast<int32_t>(value)));
1360 }
1361 break;
1362
1363 case Primitive::kPrimFloat:
1364 case Primitive::kPrimDouble:
1365 LOG(FATAL) << "Type conversion from " << input_type
1366 << " to " << result_type << " not yet implemented";
1367 break;
1368
1369 default:
1370 LOG(FATAL) << "Unexpected type conversion from " << input_type
1371 << " to " << result_type;
1372 }
1373 break;
1374
Roland Levillaindff1f282014-11-05 14:15:05 +00001375 case Primitive::kPrimLong:
1376 switch (input_type) {
1377 case Primitive::kPrimByte:
1378 case Primitive::kPrimShort:
1379 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001380 case Primitive::kPrimChar:
Roland Levillaindff1f282014-11-05 14:15:05 +00001381 // int-to-long conversion.
1382 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1383 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
1384 DCHECK_EQ(in.As<Register>(), EAX);
1385 __ cdq();
1386 break;
1387
1388 case Primitive::kPrimFloat:
1389 case Primitive::kPrimDouble:
1390 LOG(FATAL) << "Type conversion from " << input_type << " to "
1391 << result_type << " not yet implemented";
1392 break;
1393
1394 default:
1395 LOG(FATAL) << "Unexpected type conversion from " << input_type
1396 << " to " << result_type;
1397 }
1398 break;
1399
Roland Levillaindff1f282014-11-05 14:15:05 +00001400 case Primitive::kPrimFloat:
1401 case Primitive::kPrimDouble:
1402 LOG(FATAL) << "Type conversion from " << input_type
1403 << " to " << result_type << " not yet implemented";
1404 break;
1405
1406 default:
1407 LOG(FATAL) << "Unexpected type conversion from " << input_type
1408 << " to " << result_type;
1409 }
1410}
1411
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001412void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001413 LocationSummary* locations =
1414 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001415 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001416 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001417 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001418 locations->SetInAt(0, Location::RequiresRegister());
1419 locations->SetInAt(1, Location::Any());
1420 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001421 break;
1422 }
1423
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001424 case Primitive::kPrimFloat:
1425 case Primitive::kPrimDouble: {
1426 locations->SetInAt(0, Location::RequiresFpuRegister());
1427 locations->SetInAt(1, Location::Any());
1428 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001429 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001430 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001431
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001432 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001433 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1434 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001435 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001436}
1437
1438void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1439 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001440 Location first = locations->InAt(0);
1441 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001442 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001443 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001444 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001445 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001446 __ addl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001447 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001448 __ addl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001449 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001450 __ addl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001451 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001452 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001453 }
1454
1455 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001456 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001457 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1458 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001459 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001460 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1461 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001462 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001463 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001464 break;
1465 }
1466
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001467 case Primitive::kPrimFloat: {
1468 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001469 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001470 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001471 __ addss(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001472 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001473 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001474 }
1475
1476 case Primitive::kPrimDouble: {
1477 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001478 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001479 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001480 __ addsd(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001481 }
1482 break;
1483 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001484
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001485 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001486 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001487 }
1488}
1489
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001490void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001491 LocationSummary* locations =
1492 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001493 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001494 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001495 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001496 locations->SetInAt(0, Location::RequiresRegister());
1497 locations->SetInAt(1, Location::Any());
1498 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001499 break;
1500 }
Calin Juravle11351682014-10-23 15:38:15 +01001501 case Primitive::kPrimFloat:
1502 case Primitive::kPrimDouble: {
1503 locations->SetInAt(0, Location::RequiresFpuRegister());
1504 locations->SetInAt(1, Location::RequiresFpuRegister());
1505 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001506 break;
Calin Juravle11351682014-10-23 15:38:15 +01001507 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001508
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001509 default:
Calin Juravle11351682014-10-23 15:38:15 +01001510 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001511 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001512}
1513
1514void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1515 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001516 Location first = locations->InAt(0);
1517 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001518 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001519 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001520 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001521 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001522 __ subl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001523 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001524 __ subl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001525 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001526 __ subl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001527 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001528 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001529 }
1530
1531 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001532 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01001533 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1534 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001535 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001536 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001537 __ sbbl(first.AsRegisterPairHigh<Register>(),
1538 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001539 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001540 break;
1541 }
1542
Calin Juravle11351682014-10-23 15:38:15 +01001543 case Primitive::kPrimFloat: {
1544 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001545 break;
Calin Juravle11351682014-10-23 15:38:15 +01001546 }
1547
1548 case Primitive::kPrimDouble: {
1549 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1550 break;
1551 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001552
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001553 default:
Calin Juravle11351682014-10-23 15:38:15 +01001554 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001555 }
1556}
1557
Calin Juravle34bacdf2014-10-07 20:23:36 +01001558void LocationsBuilderX86::VisitMul(HMul* mul) {
1559 LocationSummary* locations =
1560 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1561 switch (mul->GetResultType()) {
1562 case Primitive::kPrimInt:
1563 locations->SetInAt(0, Location::RequiresRegister());
1564 locations->SetInAt(1, Location::Any());
1565 locations->SetOut(Location::SameAsFirstInput());
1566 break;
1567 case Primitive::kPrimLong: {
1568 locations->SetInAt(0, Location::RequiresRegister());
1569 // TODO: Currently this handles only stack operands:
1570 // - we don't have enough registers because we currently use Quick ABI.
1571 // - by the time we have a working register allocator we will probably change the ABI
1572 // and fix the above.
1573 // - we don't have a way yet to request operands on stack but the base line compiler
1574 // will leave the operands on the stack with Any().
1575 locations->SetInAt(1, Location::Any());
1576 locations->SetOut(Location::SameAsFirstInput());
1577 // Needed for imul on 32bits with 64bits output.
1578 locations->AddTemp(Location::RegisterLocation(EAX));
1579 locations->AddTemp(Location::RegisterLocation(EDX));
1580 break;
1581 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001582 case Primitive::kPrimFloat:
1583 case Primitive::kPrimDouble: {
1584 locations->SetInAt(0, Location::RequiresFpuRegister());
1585 locations->SetInAt(1, Location::RequiresFpuRegister());
1586 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001587 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001588 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001589
1590 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001591 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001592 }
1593}
1594
1595void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1596 LocationSummary* locations = mul->GetLocations();
1597 Location first = locations->InAt(0);
1598 Location second = locations->InAt(1);
1599 DCHECK(first.Equals(locations->Out()));
1600
1601 switch (mul->GetResultType()) {
1602 case Primitive::kPrimInt: {
1603 if (second.IsRegister()) {
1604 __ imull(first.As<Register>(), second.As<Register>());
1605 } else if (second.IsConstant()) {
1606 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1607 __ imull(first.As<Register>(), imm);
1608 } else {
1609 DCHECK(second.IsStackSlot());
1610 __ imull(first.As<Register>(), Address(ESP, second.GetStackIndex()));
1611 }
1612 break;
1613 }
1614
1615 case Primitive::kPrimLong: {
1616 DCHECK(second.IsDoubleStackSlot());
1617
1618 Register in1_hi = first.AsRegisterPairHigh<Register>();
1619 Register in1_lo = first.AsRegisterPairLow<Register>();
1620 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
1621 Address in2_lo(ESP, second.GetStackIndex());
1622 Register eax = locations->GetTemp(0).As<Register>();
1623 Register edx = locations->GetTemp(1).As<Register>();
1624
1625 DCHECK_EQ(EAX, eax);
1626 DCHECK_EQ(EDX, edx);
1627
1628 // input: in1 - 64 bits, in2 - 64 bits
1629 // output: in1
1630 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
1631 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
1632 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
1633
1634 __ movl(eax, in2_hi);
1635 // eax <- in1.lo * in2.hi
1636 __ imull(eax, in1_lo);
1637 // in1.hi <- in1.hi * in2.lo
1638 __ imull(in1_hi, in2_lo);
1639 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
1640 __ addl(in1_hi, eax);
1641 // move in1_lo to eax to prepare for double precision
1642 __ movl(eax, in1_lo);
1643 // edx:eax <- in1.lo * in2.lo
1644 __ mull(in2_lo);
1645 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
1646 __ addl(in1_hi, edx);
1647 // in1.lo <- (in1.lo * in2.lo)[31:0];
1648 __ movl(in1_lo, eax);
1649
1650 break;
1651 }
1652
Calin Juravleb5bfa962014-10-21 18:02:24 +01001653 case Primitive::kPrimFloat: {
1654 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001655 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001656 }
1657
1658 case Primitive::kPrimDouble: {
1659 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1660 break;
1661 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001662
1663 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001664 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001665 }
1666}
1667
Calin Juravle7c4954d2014-10-28 16:57:40 +00001668void LocationsBuilderX86::VisitDiv(HDiv* div) {
1669 LocationSummary* locations =
1670 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1671 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001672 case Primitive::kPrimInt: {
1673 locations->SetInAt(0, Location::RegisterLocation(EAX));
1674 locations->SetInAt(1, Location::RequiresRegister());
1675 locations->SetOut(Location::SameAsFirstInput());
1676 // Intel uses edx:eax as the dividend.
1677 locations->AddTemp(Location::RegisterLocation(EDX));
1678 break;
1679 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001680 case Primitive::kPrimLong: {
1681 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1682 break;
1683 }
1684 case Primitive::kPrimFloat:
1685 case Primitive::kPrimDouble: {
1686 locations->SetInAt(0, Location::RequiresFpuRegister());
1687 locations->SetInAt(1, Location::RequiresFpuRegister());
1688 locations->SetOut(Location::SameAsFirstInput());
1689 break;
1690 }
1691
1692 default:
1693 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1694 }
1695}
1696
1697void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
1698 LocationSummary* locations = div->GetLocations();
1699 Location first = locations->InAt(0);
1700 Location second = locations->InAt(1);
1701 DCHECK(first.Equals(locations->Out()));
1702
1703 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001704 case Primitive::kPrimInt: {
1705 Register first_reg = first.As<Register>();
1706 Register second_reg = second.As<Register>();
1707 DCHECK_EQ(EAX, first_reg);
1708 DCHECK_EQ(EDX, locations->GetTemp(0).As<Register>());
1709
1710 SlowPathCodeX86* slow_path =
1711 new (GetGraph()->GetArena()) DivMinusOneSlowPathX86(first_reg);
1712 codegen_->AddSlowPath(slow_path);
1713
1714 // 0x80000000/-1 triggers an arithmetic exception!
1715 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
1716 // it's safe to just use negl instead of more complex comparisons.
1717
1718 __ cmpl(second_reg, Immediate(-1));
1719 __ j(kEqual, slow_path->GetEntryLabel());
1720
1721 // edx:eax <- sign-extended of eax
1722 __ cdq();
1723 // eax = quotient, edx = remainder
1724 __ idivl(second_reg);
1725
1726 __ Bind(slow_path->GetExitLabel());
1727 break;
1728 }
1729
Calin Juravle7c4954d2014-10-28 16:57:40 +00001730 case Primitive::kPrimLong: {
1731 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1732 break;
1733 }
1734
1735 case Primitive::kPrimFloat: {
1736 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1737 break;
1738 }
1739
1740 case Primitive::kPrimDouble: {
1741 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1742 break;
1743 }
1744
1745 default:
1746 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1747 }
1748}
1749
Calin Juravled0d48522014-11-04 16:40:20 +00001750void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1751 LocationSummary* locations =
1752 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1753 locations->SetInAt(0, Location::Any());
1754 if (instruction->HasUses()) {
1755 locations->SetOut(Location::SameAsFirstInput());
1756 }
1757}
1758
1759void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1760 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
1761 codegen_->AddSlowPath(slow_path);
1762
1763 LocationSummary* locations = instruction->GetLocations();
1764 Location value = locations->InAt(0);
1765
1766 if (value.IsRegister()) {
1767 __ testl(value.As<Register>(), value.As<Register>());
1768 } else if (value.IsStackSlot()) {
1769 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
1770 } else {
1771 DCHECK(value.IsConstant()) << value;
1772 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
1773 __ jmp(slow_path->GetEntryLabel());
1774 }
1775 return;
1776 }
1777 __ j(kEqual, slow_path->GetEntryLabel());
1778}
1779
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001780void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001781 LocationSummary* locations =
1782 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001783 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001784 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001785 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1786 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001787}
1788
1789void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
1790 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001791 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001792 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001793
Nicolas Geoffray707c8092014-04-04 10:50:14 +01001794 __ fs()->call(
1795 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001796
Nicolas Geoffray39468442014-09-02 15:17:15 +01001797 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001798 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001799}
1800
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001801void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
1802 LocationSummary* locations =
1803 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1804 locations->SetOut(Location::RegisterLocation(EAX));
1805 InvokeRuntimeCallingConvention calling_convention;
1806 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1807 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1808 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1809}
1810
1811void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
1812 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001813 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001814 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
1815
1816 __ fs()->call(
1817 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
1818
1819 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1820 DCHECK(!codegen_->IsLeafMethod());
1821}
1822
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001823void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001824 LocationSummary* locations =
1825 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001826 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1827 if (location.IsStackSlot()) {
1828 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1829 } else if (location.IsDoubleStackSlot()) {
1830 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001831 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001832 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001833}
1834
1835void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001836 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001837}
1838
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001839void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001840 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001841 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001842 locations->SetInAt(0, Location::RequiresRegister());
1843 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001844}
1845
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001846void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
1847 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01001848 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001849 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01001850 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001851 switch (not_->InputAt(0)->GetType()) {
1852 case Primitive::kPrimBoolean:
1853 __ xorl(out.As<Register>(), Immediate(1));
1854 break;
1855
1856 case Primitive::kPrimInt:
1857 __ notl(out.As<Register>());
1858 break;
1859
1860 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001861 __ notl(out.AsRegisterPairLow<Register>());
1862 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001863 break;
1864
1865 default:
1866 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1867 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001868}
1869
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001870void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001871 LocationSummary* locations =
1872 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001873 locations->SetInAt(0, Location::RequiresRegister());
1874 locations->SetInAt(1, Location::Any());
1875 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001876}
1877
1878void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001879 LocationSummary* locations = compare->GetLocations();
1880 switch (compare->InputAt(0)->GetType()) {
1881 case Primitive::kPrimLong: {
1882 Label less, greater, done;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001883 Register output = locations->Out().As<Register>();
1884 Location left = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001885 Location right = locations->InAt(1);
1886 if (right.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001887 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001888 } else {
1889 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001890 __ cmpl(left.AsRegisterPairHigh<Register>(),
1891 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001892 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001893 __ j(kLess, &less); // Signed compare.
1894 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001895 if (right.IsRegisterPair()) {
1896 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001897 } else {
1898 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001899 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001900 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001901 __ movl(output, Immediate(0));
1902 __ j(kEqual, &done);
1903 __ j(kBelow, &less); // Unsigned compare.
1904
1905 __ Bind(&greater);
1906 __ movl(output, Immediate(1));
1907 __ jmp(&done);
1908
1909 __ Bind(&less);
1910 __ movl(output, Immediate(-1));
1911
1912 __ Bind(&done);
1913 break;
1914 }
1915 default:
1916 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
1917 }
1918}
1919
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001920void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001921 LocationSummary* locations =
1922 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001923 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1924 locations->SetInAt(i, Location::Any());
1925 }
1926 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001927}
1928
1929void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001930 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001931 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001932}
1933
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001934void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001935 LocationSummary* locations =
1936 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001937 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001938 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001939 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001940 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
1941 || (field_type == Primitive::kPrimByte);
1942 // The register allocator does not support multiple
1943 // inputs that die at entry with one in a specific register.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001944 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001945 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001946 locations->SetInAt(1, Location::RegisterLocation(EAX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001947 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001948 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001949 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001950 // Temporary registers for the write barrier.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001951 if (is_object_type) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001952 locations->AddTemp(Location::RequiresRegister());
1953 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001954 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001955 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001956}
1957
1958void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1959 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001960 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001961 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001962 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001963
1964 switch (field_type) {
1965 case Primitive::kPrimBoolean:
1966 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001967 ByteRegister value = locations->InAt(1).As<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001968 __ movb(Address(obj, offset), value);
1969 break;
1970 }
1971
1972 case Primitive::kPrimShort:
1973 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001974 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001975 __ movw(Address(obj, offset), value);
1976 break;
1977 }
1978
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001979 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001980 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001981 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001982 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001983
1984 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001985 Register temp = locations->GetTemp(0).As<Register>();
1986 Register card = locations->GetTemp(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001987 codegen_->MarkGCCard(temp, card, obj, value);
1988 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001989 break;
1990 }
1991
1992 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001993 Location value = locations->InAt(1);
1994 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1995 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001996 break;
1997 }
1998
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001999 case Primitive::kPrimFloat: {
2000 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2001 __ movss(Address(obj, offset), value);
2002 break;
2003 }
2004
2005 case Primitive::kPrimDouble: {
2006 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2007 __ movsd(Address(obj, offset), value);
2008 break;
2009 }
2010
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002011 case Primitive::kPrimVoid:
2012 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002013 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002014 }
2015}
2016
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002017void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
2018 Label is_null;
2019 __ testl(value, value);
2020 __ j(kEqual, &is_null);
2021 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2022 __ movl(temp, object);
2023 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
2024 __ movb(Address(temp, card, TIMES_1, 0),
2025 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
2026 __ Bind(&is_null);
2027}
2028
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002029void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002030 LocationSummary* locations =
2031 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002032 locations->SetInAt(0, Location::RequiresRegister());
2033 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002034}
2035
2036void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2037 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002038 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002039 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2040
2041 switch (instruction->GetType()) {
2042 case Primitive::kPrimBoolean: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002043 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002044 __ movzxb(out, Address(obj, offset));
2045 break;
2046 }
2047
2048 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002049 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002050 __ movsxb(out, Address(obj, offset));
2051 break;
2052 }
2053
2054 case Primitive::kPrimShort: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002055 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002056 __ movsxw(out, Address(obj, offset));
2057 break;
2058 }
2059
2060 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002061 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002062 __ movzxw(out, Address(obj, offset));
2063 break;
2064 }
2065
2066 case Primitive::kPrimInt:
2067 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002068 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002069 __ movl(out, Address(obj, offset));
2070 break;
2071 }
2072
2073 case Primitive::kPrimLong: {
2074 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002075 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
2076 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002077 break;
2078 }
2079
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002080 case Primitive::kPrimFloat: {
2081 XmmRegister out = locations->Out().As<XmmRegister>();
2082 __ movss(out, Address(obj, offset));
2083 break;
2084 }
2085
2086 case Primitive::kPrimDouble: {
2087 XmmRegister out = locations->Out().As<XmmRegister>();
2088 __ movsd(out, Address(obj, offset));
2089 break;
2090 }
2091
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002092 case Primitive::kPrimVoid:
2093 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002094 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002095 }
2096}
2097
2098void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002099 LocationSummary* locations =
2100 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002101 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002102 if (instruction->HasUses()) {
2103 locations->SetOut(Location::SameAsFirstInput());
2104 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002105}
2106
2107void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002108 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002109 codegen_->AddSlowPath(slow_path);
2110
2111 LocationSummary* locations = instruction->GetLocations();
2112 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002113
2114 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002115 __ cmpl(obj.As<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002116 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002117 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002118 } else {
2119 DCHECK(obj.IsConstant()) << obj;
2120 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2121 __ jmp(slow_path->GetEntryLabel());
2122 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002123 }
2124 __ j(kEqual, slow_path->GetEntryLabel());
2125}
2126
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002127void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002128 LocationSummary* locations =
2129 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002130 locations->SetInAt(0, Location::RequiresRegister());
2131 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2132 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002133}
2134
2135void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
2136 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002137 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002138 Location index = locations->InAt(1);
2139
2140 switch (instruction->GetType()) {
2141 case Primitive::kPrimBoolean: {
2142 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002143 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002144 if (index.IsConstant()) {
2145 __ movzxb(out, Address(obj,
2146 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2147 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002148 __ movzxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002149 }
2150 break;
2151 }
2152
2153 case Primitive::kPrimByte: {
2154 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002155 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002156 if (index.IsConstant()) {
2157 __ movsxb(out, Address(obj,
2158 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2159 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002160 __ movsxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002161 }
2162 break;
2163 }
2164
2165 case Primitive::kPrimShort: {
2166 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002167 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002168 if (index.IsConstant()) {
2169 __ movsxw(out, Address(obj,
2170 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2171 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002172 __ movsxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002173 }
2174 break;
2175 }
2176
2177 case Primitive::kPrimChar: {
2178 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002179 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002180 if (index.IsConstant()) {
2181 __ movzxw(out, Address(obj,
2182 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2183 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002184 __ movzxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002185 }
2186 break;
2187 }
2188
2189 case Primitive::kPrimInt:
2190 case Primitive::kPrimNot: {
2191 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002192 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002193 if (index.IsConstant()) {
2194 __ movl(out, Address(obj,
2195 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2196 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002197 __ movl(out, Address(obj, index.As<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002198 }
2199 break;
2200 }
2201
2202 case Primitive::kPrimLong: {
2203 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002204 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002205 if (index.IsConstant()) {
2206 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002207 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
2208 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002209 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002210 __ movl(out.AsRegisterPairLow<Register>(),
2211 Address(obj, index.As<Register>(), TIMES_8, data_offset));
2212 __ movl(out.AsRegisterPairHigh<Register>(),
2213 Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002214 }
2215 break;
2216 }
2217
2218 case Primitive::kPrimFloat:
2219 case Primitive::kPrimDouble:
2220 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002221 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002222 case Primitive::kPrimVoid:
2223 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002224 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002225 }
2226}
2227
2228void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002229 Primitive::Type value_type = instruction->GetComponentType();
2230 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2231 instruction,
2232 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
2233
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002234 if (value_type == Primitive::kPrimNot) {
2235 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002236 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2237 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2238 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002239 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002240 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
2241 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002242 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002243 // In case of a byte operation, the register allocator does not support multiple
2244 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002245 locations->SetInAt(0, Location::RequiresRegister());
2246 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002247 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002248 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002249 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002250 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002251 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002252 }
2253 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002254}
2255
2256void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
2257 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002258 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002259 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002260 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002261 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002262
2263 switch (value_type) {
2264 case Primitive::kPrimBoolean:
2265 case Primitive::kPrimByte: {
2266 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002267 if (index.IsConstant()) {
2268 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002269 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002270 __ movb(Address(obj, offset), value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002271 } else {
2272 __ movb(Address(obj, offset),
2273 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2274 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002275 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002276 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002277 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
2278 value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002279 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002280 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002281 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2282 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002283 }
2284 break;
2285 }
2286
2287 case Primitive::kPrimShort:
2288 case Primitive::kPrimChar: {
2289 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002290 if (index.IsConstant()) {
2291 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002292 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002293 __ movw(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002294 } else {
2295 __ movw(Address(obj, offset),
2296 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2297 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002298 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002299 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002300 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
2301 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002302 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002303 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002304 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2305 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002306 }
2307 break;
2308 }
2309
2310 case Primitive::kPrimInt: {
2311 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002312 if (index.IsConstant()) {
2313 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002314 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002315 __ movl(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002316 } else {
2317 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2318 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002319 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002320 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002321 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
2322 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002323 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002324 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002325 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2326 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002327 }
2328 break;
2329 }
2330
2331 case Primitive::kPrimNot: {
2332 DCHECK(!codegen_->IsLeafMethod());
2333 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01002334 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002335 break;
2336 }
2337
2338 case Primitive::kPrimLong: {
2339 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002340 if (index.IsConstant()) {
2341 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002342 if (value.IsRegisterPair()) {
2343 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
2344 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002345 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002346 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002347 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
2348 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
2349 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
2350 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002351 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002352 if (value.IsRegisterPair()) {
2353 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
2354 value.AsRegisterPairLow<Register>());
2355 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
2356 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002357 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002358 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002359 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002360 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002361 Immediate(Low32Bits(val)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002362 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002363 Immediate(High32Bits(val)));
2364 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002365 }
2366 break;
2367 }
2368
2369 case Primitive::kPrimFloat:
2370 case Primitive::kPrimDouble:
2371 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002372 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002373 case Primitive::kPrimVoid:
2374 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002375 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002376 }
2377}
2378
2379void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
2380 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002381 locations->SetInAt(0, Location::RequiresRegister());
2382 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002383 instruction->SetLocations(locations);
2384}
2385
2386void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
2387 LocationSummary* locations = instruction->GetLocations();
2388 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002389 Register obj = locations->InAt(0).As<Register>();
2390 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002391 __ movl(out, Address(obj, offset));
2392}
2393
2394void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002395 LocationSummary* locations =
2396 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002397 locations->SetInAt(0, Location::RequiresRegister());
2398 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002399 if (instruction->HasUses()) {
2400 locations->SetOut(Location::SameAsFirstInput());
2401 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002402}
2403
2404void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
2405 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002406 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002407 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002408 codegen_->AddSlowPath(slow_path);
2409
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002410 Register index = locations->InAt(0).As<Register>();
2411 Register length = locations->InAt(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002412
2413 __ cmpl(index, length);
2414 __ j(kAboveEqual, slow_path->GetEntryLabel());
2415}
2416
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002417void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
2418 temp->SetLocations(nullptr);
2419}
2420
2421void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
2422 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002423 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002424}
2425
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002426void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002427 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002428 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002429}
2430
2431void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002432 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2433}
2434
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002435void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
2436 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2437}
2438
2439void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002440 HBasicBlock* block = instruction->GetBlock();
2441 if (block->GetLoopInformation() != nullptr) {
2442 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2443 // The back edge will generate the suspend check.
2444 return;
2445 }
2446 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2447 // The goto will generate the suspend check.
2448 return;
2449 }
2450 GenerateSuspendCheck(instruction, nullptr);
2451}
2452
2453void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
2454 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002455 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002456 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002457 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002458 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002459 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002460 if (successor == nullptr) {
2461 __ j(kNotEqual, slow_path->GetEntryLabel());
2462 __ Bind(slow_path->GetReturnLabel());
2463 } else {
2464 __ j(kEqual, codegen_->GetLabelOf(successor));
2465 __ jmp(slow_path->GetEntryLabel());
2466 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002467}
2468
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002469X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
2470 return codegen_->GetAssembler();
2471}
2472
2473void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
2474 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002475 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002476 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2477 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
2478 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
2479}
2480
2481void ParallelMoveResolverX86::EmitMove(size_t index) {
2482 MoveOperands* move = moves_.Get(index);
2483 Location source = move->GetSource();
2484 Location destination = move->GetDestination();
2485
2486 if (source.IsRegister()) {
2487 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002488 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002489 } else {
2490 DCHECK(destination.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002491 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002492 }
2493 } else if (source.IsStackSlot()) {
2494 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002495 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002496 } else {
2497 DCHECK(destination.IsStackSlot());
2498 MoveMemoryToMemory(destination.GetStackIndex(),
2499 source.GetStackIndex());
2500 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002501 } else if (source.IsConstant()) {
2502 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
2503 Immediate imm(instruction->AsIntConstant()->GetValue());
2504 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002505 __ movl(destination.As<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002506 } else {
2507 __ movl(Address(ESP, destination.GetStackIndex()), imm);
2508 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002509 } else {
2510 LOG(FATAL) << "Unimplemented";
2511 }
2512}
2513
2514void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002515 Register suggested_scratch = reg == EAX ? EBX : EAX;
2516 ScratchRegisterScope ensure_scratch(
2517 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2518
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002519 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2520 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
2521 __ movl(Address(ESP, mem + stack_offset), reg);
2522 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
2523}
2524
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002525void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
2526 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002527 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
2528
2529 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002530 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002531 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2532
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002533 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
2534 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
2535 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
2536 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
2537 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
2538 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
2539}
2540
2541void ParallelMoveResolverX86::EmitSwap(size_t index) {
2542 MoveOperands* move = moves_.Get(index);
2543 Location source = move->GetSource();
2544 Location destination = move->GetDestination();
2545
2546 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002547 __ xchgl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002548 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002549 Exchange(source.As<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002550 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002551 Exchange(destination.As<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002552 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
2553 Exchange(destination.GetStackIndex(), source.GetStackIndex());
2554 } else {
2555 LOG(FATAL) << "Unimplemented";
2556 }
2557}
2558
2559void ParallelMoveResolverX86::SpillScratch(int reg) {
2560 __ pushl(static_cast<Register>(reg));
2561}
2562
2563void ParallelMoveResolverX86::RestoreScratch(int reg) {
2564 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002565}
2566
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002567void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002568 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2569 ? LocationSummary::kCallOnSlowPath
2570 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002571 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002572 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002573 locations->SetOut(Location::RequiresRegister());
2574}
2575
2576void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
2577 Register out = cls->GetLocations()->Out().As<Register>();
2578 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002579 DCHECK(!cls->CanCallRuntime());
2580 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002581 codegen_->LoadCurrentMethod(out);
2582 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2583 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002584 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002585 codegen_->LoadCurrentMethod(out);
2586 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2587 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002588
2589 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
2590 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2591 codegen_->AddSlowPath(slow_path);
2592 __ testl(out, out);
2593 __ j(kEqual, slow_path->GetEntryLabel());
2594 if (cls->MustGenerateClinitCheck()) {
2595 GenerateClassInitializationCheck(slow_path, out);
2596 } else {
2597 __ Bind(slow_path->GetExitLabel());
2598 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002599 }
2600}
2601
2602void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
2603 LocationSummary* locations =
2604 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2605 locations->SetInAt(0, Location::RequiresRegister());
2606 if (check->HasUses()) {
2607 locations->SetOut(Location::SameAsFirstInput());
2608 }
2609}
2610
2611void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002612 // We assume the class to not be null.
2613 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
2614 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002615 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002616 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<Register>());
2617}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002618
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002619void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
2620 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002621 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2622 Immediate(mirror::Class::kStatusInitialized));
2623 __ j(kLess, slow_path->GetEntryLabel());
2624 __ Bind(slow_path->GetExitLabel());
2625 // No need for memory fence, thanks to the X86 memory model.
2626}
2627
2628void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2629 LocationSummary* locations =
2630 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2631 locations->SetInAt(0, Location::RequiresRegister());
2632 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2633}
2634
2635void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2636 LocationSummary* locations = instruction->GetLocations();
2637 Register cls = locations->InAt(0).As<Register>();
2638 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2639
2640 switch (instruction->GetType()) {
2641 case Primitive::kPrimBoolean: {
2642 Register out = locations->Out().As<Register>();
2643 __ movzxb(out, Address(cls, offset));
2644 break;
2645 }
2646
2647 case Primitive::kPrimByte: {
2648 Register out = locations->Out().As<Register>();
2649 __ movsxb(out, Address(cls, offset));
2650 break;
2651 }
2652
2653 case Primitive::kPrimShort: {
2654 Register out = locations->Out().As<Register>();
2655 __ movsxw(out, Address(cls, offset));
2656 break;
2657 }
2658
2659 case Primitive::kPrimChar: {
2660 Register out = locations->Out().As<Register>();
2661 __ movzxw(out, Address(cls, offset));
2662 break;
2663 }
2664
2665 case Primitive::kPrimInt:
2666 case Primitive::kPrimNot: {
2667 Register out = locations->Out().As<Register>();
2668 __ movl(out, Address(cls, offset));
2669 break;
2670 }
2671
2672 case Primitive::kPrimLong: {
2673 // TODO: support volatile.
2674 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(cls, offset));
2675 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(cls, kX86WordSize + offset));
2676 break;
2677 }
2678
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002679 case Primitive::kPrimFloat: {
2680 XmmRegister out = locations->Out().As<XmmRegister>();
2681 __ movss(out, Address(cls, offset));
2682 break;
2683 }
2684
2685 case Primitive::kPrimDouble: {
2686 XmmRegister out = locations->Out().As<XmmRegister>();
2687 __ movsd(out, Address(cls, offset));
2688 break;
2689 }
2690
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002691 case Primitive::kPrimVoid:
2692 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2693 UNREACHABLE();
2694 }
2695}
2696
2697void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2698 LocationSummary* locations =
2699 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2700 locations->SetInAt(0, Location::RequiresRegister());
2701 Primitive::Type field_type = instruction->GetFieldType();
2702 bool is_object_type = field_type == Primitive::kPrimNot;
2703 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2704 || (field_type == Primitive::kPrimByte);
2705 // The register allocator does not support multiple
2706 // inputs that die at entry with one in a specific register.
2707 if (is_byte_type) {
2708 // Ensure the value is in a byte register.
2709 locations->SetInAt(1, Location::RegisterLocation(EAX));
2710 } else {
2711 locations->SetInAt(1, Location::RequiresRegister());
2712 }
2713 // Temporary registers for the write barrier.
2714 if (is_object_type) {
2715 locations->AddTemp(Location::RequiresRegister());
2716 // Ensure the card is in a byte register.
2717 locations->AddTemp(Location::RegisterLocation(ECX));
2718 }
2719}
2720
2721void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2722 LocationSummary* locations = instruction->GetLocations();
2723 Register cls = locations->InAt(0).As<Register>();
2724 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2725 Primitive::Type field_type = instruction->GetFieldType();
2726
2727 switch (field_type) {
2728 case Primitive::kPrimBoolean:
2729 case Primitive::kPrimByte: {
2730 ByteRegister value = locations->InAt(1).As<ByteRegister>();
2731 __ movb(Address(cls, offset), value);
2732 break;
2733 }
2734
2735 case Primitive::kPrimShort:
2736 case Primitive::kPrimChar: {
2737 Register value = locations->InAt(1).As<Register>();
2738 __ movw(Address(cls, offset), value);
2739 break;
2740 }
2741
2742 case Primitive::kPrimInt:
2743 case Primitive::kPrimNot: {
2744 Register value = locations->InAt(1).As<Register>();
2745 __ movl(Address(cls, offset), value);
2746
2747 if (field_type == Primitive::kPrimNot) {
2748 Register temp = locations->GetTemp(0).As<Register>();
2749 Register card = locations->GetTemp(1).As<Register>();
2750 codegen_->MarkGCCard(temp, card, cls, value);
2751 }
2752 break;
2753 }
2754
2755 case Primitive::kPrimLong: {
2756 Location value = locations->InAt(1);
2757 __ movl(Address(cls, offset), value.AsRegisterPairLow<Register>());
2758 __ movl(Address(cls, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
2759 break;
2760 }
2761
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002762 case Primitive::kPrimFloat: {
2763 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2764 __ movss(Address(cls, offset), value);
2765 break;
2766 }
2767
2768 case Primitive::kPrimDouble: {
2769 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2770 __ movsd(Address(cls, offset), value);
2771 break;
2772 }
2773
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002774 case Primitive::kPrimVoid:
2775 LOG(FATAL) << "Unreachable type " << field_type;
2776 UNREACHABLE();
2777 }
2778}
2779
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002780void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
2781 LocationSummary* locations =
2782 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2783 locations->SetOut(Location::RequiresRegister());
2784}
2785
2786void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
2787 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
2788 codegen_->AddSlowPath(slow_path);
2789
2790 Register out = load->GetLocations()->Out().As<Register>();
2791 codegen_->LoadCurrentMethod(out);
2792 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
2793 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2794 __ testl(out, out);
2795 __ j(kEqual, slow_path->GetEntryLabel());
2796 __ Bind(slow_path->GetExitLabel());
2797}
2798
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002799void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
2800 LocationSummary* locations =
2801 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2802 locations->SetOut(Location::RequiresRegister());
2803}
2804
2805void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
2806 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
2807 __ fs()->movl(load->GetLocations()->Out().As<Register>(), address);
2808 __ fs()->movl(address, Immediate(0));
2809}
2810
2811void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
2812 LocationSummary* locations =
2813 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2814 InvokeRuntimeCallingConvention calling_convention;
2815 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2816}
2817
2818void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
2819 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
2820 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2821}
2822
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002823void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002824 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
2825 ? LocationSummary::kNoCall
2826 : LocationSummary::kCallOnSlowPath;
2827 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2828 locations->SetInAt(0, Location::RequiresRegister());
2829 locations->SetInAt(1, Location::Any());
2830 locations->SetOut(Location::RequiresRegister());
2831}
2832
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002833void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002834 LocationSummary* locations = instruction->GetLocations();
2835 Register obj = locations->InAt(0).As<Register>();
2836 Location cls = locations->InAt(1);
2837 Register out = locations->Out().As<Register>();
2838 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2839 Label done, zero;
2840 SlowPathCodeX86* slow_path = nullptr;
2841
2842 // Return 0 if `obj` is null.
2843 // TODO: avoid this check if we know obj is not null.
2844 __ testl(obj, obj);
2845 __ j(kEqual, &zero);
2846 __ movl(out, Address(obj, class_offset));
2847 // Compare the class of `obj` with `cls`.
2848 if (cls.IsRegister()) {
2849 __ cmpl(out, cls.As<Register>());
2850 } else {
2851 DCHECK(cls.IsStackSlot()) << cls;
2852 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
2853 }
2854
2855 if (instruction->IsClassFinal()) {
2856 // Classes must be equal for the instanceof to succeed.
2857 __ j(kNotEqual, &zero);
2858 __ movl(out, Immediate(1));
2859 __ jmp(&done);
2860 } else {
2861 // If the classes are not equal, we go into a slow path.
2862 DCHECK(locations->OnlyCallsOnSlowPath());
2863 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002864 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002865 codegen_->AddSlowPath(slow_path);
2866 __ j(kNotEqual, slow_path->GetEntryLabel());
2867 __ movl(out, Immediate(1));
2868 __ jmp(&done);
2869 }
2870 __ Bind(&zero);
2871 __ movl(out, Immediate(0));
2872 if (slow_path != nullptr) {
2873 __ Bind(slow_path->GetExitLabel());
2874 }
2875 __ Bind(&done);
2876}
2877
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002878void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
2879 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2880 instruction, LocationSummary::kCallOnSlowPath);
2881 locations->SetInAt(0, Location::RequiresRegister());
2882 locations->SetInAt(1, Location::Any());
2883 locations->AddTemp(Location::RequiresRegister());
2884}
2885
2886void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
2887 LocationSummary* locations = instruction->GetLocations();
2888 Register obj = locations->InAt(0).As<Register>();
2889 Location cls = locations->InAt(1);
2890 Register temp = locations->GetTemp(0).As<Register>();
2891 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2892 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
2893 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
2894 codegen_->AddSlowPath(slow_path);
2895
2896 // TODO: avoid this check if we know obj is not null.
2897 __ testl(obj, obj);
2898 __ j(kEqual, slow_path->GetExitLabel());
2899 __ movl(temp, Address(obj, class_offset));
2900
2901 // Compare the class of `obj` with `cls`.
2902 if (cls.IsRegister()) {
2903 __ cmpl(temp, cls.As<Register>());
2904 } else {
2905 DCHECK(cls.IsStackSlot()) << cls;
2906 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
2907 }
2908
2909 __ j(kNotEqual, slow_path->GetEntryLabel());
2910 __ Bind(slow_path->GetExitLabel());
2911}
2912
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002913void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
2914 LocationSummary* locations =
2915 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2916 InvokeRuntimeCallingConvention calling_convention;
2917 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2918}
2919
2920void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
2921 __ fs()->call(Address::Absolute(instruction->IsEnter()
2922 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
2923 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
2924 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2925}
2926
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002927void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
2928void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
2929void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
2930
2931void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
2932 LocationSummary* locations =
2933 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2934 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
2935 || instruction->GetResultType() == Primitive::kPrimLong);
2936 locations->SetInAt(0, Location::RequiresRegister());
2937 locations->SetInAt(1, Location::Any());
2938 locations->SetOut(Location::SameAsFirstInput());
2939}
2940
2941void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
2942 HandleBitwiseOperation(instruction);
2943}
2944
2945void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
2946 HandleBitwiseOperation(instruction);
2947}
2948
2949void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
2950 HandleBitwiseOperation(instruction);
2951}
2952
2953void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
2954 LocationSummary* locations = instruction->GetLocations();
2955 Location first = locations->InAt(0);
2956 Location second = locations->InAt(1);
2957 DCHECK(first.Equals(locations->Out()));
2958
2959 if (instruction->GetResultType() == Primitive::kPrimInt) {
2960 if (second.IsRegister()) {
2961 if (instruction->IsAnd()) {
2962 __ andl(first.As<Register>(), second.As<Register>());
2963 } else if (instruction->IsOr()) {
2964 __ orl(first.As<Register>(), second.As<Register>());
2965 } else {
2966 DCHECK(instruction->IsXor());
2967 __ xorl(first.As<Register>(), second.As<Register>());
2968 }
2969 } else if (second.IsConstant()) {
2970 if (instruction->IsAnd()) {
2971 __ andl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2972 } else if (instruction->IsOr()) {
2973 __ orl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2974 } else {
2975 DCHECK(instruction->IsXor());
2976 __ xorl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2977 }
2978 } else {
2979 if (instruction->IsAnd()) {
2980 __ andl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
2981 } else if (instruction->IsOr()) {
2982 __ orl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
2983 } else {
2984 DCHECK(instruction->IsXor());
2985 __ xorl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
2986 }
2987 }
2988 } else {
2989 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
2990 if (second.IsRegisterPair()) {
2991 if (instruction->IsAnd()) {
2992 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2993 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
2994 } else if (instruction->IsOr()) {
2995 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2996 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
2997 } else {
2998 DCHECK(instruction->IsXor());
2999 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3000 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3001 }
3002 } else {
3003 if (instruction->IsAnd()) {
3004 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3005 __ andl(first.AsRegisterPairHigh<Register>(),
3006 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3007 } else if (instruction->IsOr()) {
3008 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3009 __ orl(first.AsRegisterPairHigh<Register>(),
3010 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3011 } else {
3012 DCHECK(instruction->IsXor());
3013 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3014 __ xorl(first.AsRegisterPairHigh<Register>(),
3015 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3016 }
3017 }
3018 }
3019}
3020
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003021} // namespace x86
3022} // namespace art