blob: b6549b21c3073078cead16ae0cfcb81f6be23ca8 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
19#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010020#include "gc/accounting/card_table.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070021#include "mirror/array-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010022#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010023#include "mirror/class.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010024#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000025#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010026#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000027#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010028#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010031
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032namespace x86 {
33
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010034static constexpr bool kExplicitStackOverflowCheck = false;
35
36static constexpr int kNumberOfPushedRegistersAtEntry = 1;
37static constexpr int kCurrentMethodStackOffset = 0;
38
Calin Juravled6fb6cf2014-11-11 19:07:44 +000039static constexpr Register kRuntimeParameterCoreRegisters[] = { EAX, ECX, EDX, EBX };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010040static constexpr size_t kRuntimeParameterCoreRegistersLength =
41 arraysize(kRuntimeParameterCoreRegisters);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010042static constexpr XmmRegister kRuntimeParameterFpuRegisters[] = { };
43static constexpr size_t kRuntimeParameterFpuRegistersLength = 0;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010044
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010045class InvokeRuntimeCallingConvention : public CallingConvention<Register, XmmRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010046 public:
47 InvokeRuntimeCallingConvention()
48 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010049 kRuntimeParameterCoreRegistersLength,
50 kRuntimeParameterFpuRegisters,
51 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010052
53 private:
54 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
55};
56
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
58
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010059class SlowPathCodeX86 : public SlowPathCode {
60 public:
61 SlowPathCodeX86() : entry_label_(), exit_label_() {}
62
63 Label* GetEntryLabel() { return &entry_label_; }
64 Label* GetExitLabel() { return &exit_label_; }
65
66 private:
67 Label entry_label_;
68 Label exit_label_;
69
70 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86);
71};
72
73class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010074 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010075 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076
77 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
78 __ Bind(GetEntryLabel());
79 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010080 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010081 }
82
83 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010084 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010085 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
86};
87
Calin Juravled0d48522014-11-04 16:40:20 +000088class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
89 public:
90 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
91
92 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
93 __ Bind(GetEntryLabel());
94 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
95 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
96 }
97
98 private:
99 HDivZeroCheck* const instruction_;
100 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
101};
102
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());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000143 // We're moving two locations to locations that could overlap, so we need a parallel
144 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100145 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000146 x86_codegen->EmitParallelMoves(
147 index_location_,
148 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
149 length_location_,
150 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100151 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100152 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100153 }
154
155 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100156 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100157 const Location index_location_;
158 const Location length_location_;
159
160 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
161};
162
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100163class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000164 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100165 explicit SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
166 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000167
168 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100169 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000170 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100171 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000172 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
173 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100174 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100175 if (successor_ == nullptr) {
176 __ jmp(GetReturnLabel());
177 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100178 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100179 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000180 }
181
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100182 Label* GetReturnLabel() {
183 DCHECK(successor_ == nullptr);
184 return &return_label_;
185 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000186
187 private:
188 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100189 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000190 Label return_label_;
191
192 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
193};
194
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000195class LoadStringSlowPathX86 : public SlowPathCodeX86 {
196 public:
197 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
198
199 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
200 LocationSummary* locations = instruction_->GetLocations();
201 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
202
203 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
204 __ Bind(GetEntryLabel());
205 codegen->SaveLiveRegisters(locations);
206
207 InvokeRuntimeCallingConvention calling_convention;
208 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(0));
209 __ movl(calling_convention.GetRegisterAt(1), Immediate(instruction_->GetStringIndex()));
210 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
211 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
212 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
213 codegen->RestoreLiveRegisters(locations);
214
215 __ jmp(GetExitLabel());
216 }
217
218 private:
219 HLoadString* const instruction_;
220
221 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
222};
223
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000224class LoadClassSlowPathX86 : public SlowPathCodeX86 {
225 public:
226 LoadClassSlowPathX86(HLoadClass* cls,
227 HInstruction* at,
228 uint32_t dex_pc,
229 bool do_clinit)
230 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
231 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
232 }
233
234 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
235 LocationSummary* locations = at_->GetLocations();
236 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
237 __ Bind(GetEntryLabel());
238 codegen->SaveLiveRegisters(locations);
239
240 InvokeRuntimeCallingConvention calling_convention;
241 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
242 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
243 __ fs()->call(Address::Absolute(do_clinit_
244 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
245 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
246 codegen->RecordPcInfo(at_, dex_pc_);
247
248 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000249 Location out = locations->Out();
250 if (out.IsValid()) {
251 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
252 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000253 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000254
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000255 codegen->RestoreLiveRegisters(locations);
256 __ jmp(GetExitLabel());
257 }
258
259 private:
260 // The class this slow path will load.
261 HLoadClass* const cls_;
262
263 // The instruction where this slow path is happening.
264 // (Might be the load class or an initialization check).
265 HInstruction* const at_;
266
267 // The dex PC of `at_`.
268 const uint32_t dex_pc_;
269
270 // Whether to initialize the class.
271 const bool do_clinit_;
272
273 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
274};
275
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000276class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
277 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000278 TypeCheckSlowPathX86(HInstruction* instruction,
279 Location class_to_check,
280 Location object_class,
281 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000282 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000283 class_to_check_(class_to_check),
284 object_class_(object_class),
285 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000286
287 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
288 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000289 DCHECK(instruction_->IsCheckCast()
290 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000291
292 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
293 __ Bind(GetEntryLabel());
294 codegen->SaveLiveRegisters(locations);
295
296 // We're moving two locations to locations that could overlap, so we need a parallel
297 // move resolver.
298 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000299 x86_codegen->EmitParallelMoves(
300 class_to_check_,
301 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
302 object_class_,
303 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000304
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 {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000578 DCHECK(destination.IsStackSlot()) << destination;
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 {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000639 DCHECK(destination.IsDoubleStackSlot()) << destination;
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) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000665 LocationSummary* locations = instruction->GetLocations();
666 if (locations != nullptr && locations->Out().Equals(location)) {
667 return;
668 }
669
670 if (locations != nullptr && locations->Out().IsConstant()) {
671 HConstant* const_to_move = locations->Out().GetConstant();
672 if (const_to_move->IsIntConstant()) {
673 Immediate imm(const_to_move->AsIntConstant()->GetValue());
674 if (location.IsRegister()) {
675 __ movl(location.As<Register>(), imm);
676 } else if (location.IsStackSlot()) {
677 __ movl(Address(ESP, location.GetStackIndex()), imm);
678 } else {
679 DCHECK(location.IsConstant());
680 DCHECK_EQ(location.GetConstant(), const_to_move);
681 }
682 } else if (const_to_move->IsLongConstant()) {
683 int64_t value = const_to_move->AsLongConstant()->GetValue();
684 if (location.IsRegisterPair()) {
685 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
686 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
687 } else if (location.IsDoubleStackSlot()) {
688 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
689 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
690 } else {
691 DCHECK(location.IsConstant());
692 DCHECK_EQ(location.GetConstant(), instruction);
693 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100694 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000695 } else if (instruction->IsTemporary()) {
696 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000697 if (temp_location.IsStackSlot()) {
698 Move32(location, temp_location);
699 } else {
700 DCHECK(temp_location.IsDoubleStackSlot());
701 Move64(location, temp_location);
702 }
Roland Levillain476df552014-10-09 17:51:36 +0100703 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100704 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100705 switch (instruction->GetType()) {
706 case Primitive::kPrimBoolean:
707 case Primitive::kPrimByte:
708 case Primitive::kPrimChar:
709 case Primitive::kPrimShort:
710 case Primitive::kPrimInt:
711 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100712 case Primitive::kPrimFloat:
713 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100714 break;
715
716 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100717 case Primitive::kPrimDouble:
718 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100719 break;
720
721 default:
722 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
723 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000724 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100725 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100726 switch (instruction->GetType()) {
727 case Primitive::kPrimBoolean:
728 case Primitive::kPrimByte:
729 case Primitive::kPrimChar:
730 case Primitive::kPrimShort:
731 case Primitive::kPrimInt:
732 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100733 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000734 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100735 break;
736
737 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100738 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000739 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100740 break;
741
742 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100743 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100744 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000745 }
746}
747
748void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000749 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000750}
751
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000752void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000753 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100754 DCHECK(!successor->IsExitBlock());
755
756 HBasicBlock* block = got->GetBlock();
757 HInstruction* previous = got->GetPrevious();
758
759 HLoopInformation* info = block->GetLoopInformation();
760 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
761 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
762 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
763 return;
764 }
765
766 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
767 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
768 }
769 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000770 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000771 }
772}
773
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000774void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000775 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000776}
777
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000778void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700779 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000780 if (kIsDebugBuild) {
781 __ Comment("Unreachable");
782 __ int3();
783 }
784}
785
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000786void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100787 LocationSummary* locations =
788 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100789 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100790 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100791 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100792 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000793}
794
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000795void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700796 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100797 if (cond->IsIntConstant()) {
798 // Constant condition, statically compared against 1.
799 int32_t cond_value = cond->AsIntConstant()->GetValue();
800 if (cond_value == 1) {
801 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
802 if_instr->IfTrueSuccessor())) {
803 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100804 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100805 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100806 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100807 DCHECK_EQ(cond_value, 0);
808 }
809 } else {
810 bool materialized =
811 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
812 // Moves do not affect the eflags register, so if the condition is
813 // evaluated just before the if, we don't need to evaluate it
814 // again.
815 bool eflags_set = cond->IsCondition()
816 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
817 if (materialized) {
818 if (!eflags_set) {
819 // Materialized condition, compare against 0.
820 Location lhs = if_instr->GetLocations()->InAt(0);
821 if (lhs.IsRegister()) {
822 __ cmpl(lhs.As<Register>(), Immediate(0));
823 } else {
824 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
825 }
826 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
827 } else {
828 __ j(X86Condition(cond->AsCondition()->GetCondition()),
829 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
830 }
831 } else {
832 Location lhs = cond->GetLocations()->InAt(0);
833 Location rhs = cond->GetLocations()->InAt(1);
834 // LHS is guaranteed to be in a register (see
835 // LocationsBuilderX86::VisitCondition).
836 if (rhs.IsRegister()) {
837 __ cmpl(lhs.As<Register>(), rhs.As<Register>());
838 } else if (rhs.IsConstant()) {
839 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
840 Immediate imm(instruction->AsIntConstant()->GetValue());
841 __ cmpl(lhs.As<Register>(), imm);
842 } else {
843 __ cmpl(lhs.As<Register>(), Address(ESP, rhs.GetStackIndex()));
844 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100845 __ j(X86Condition(cond->AsCondition()->GetCondition()),
846 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700847 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100848 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100849 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
850 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700851 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000852 }
853}
854
855void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000856 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000857}
858
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000859void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
860 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000861}
862
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000863void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100864 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000865}
866
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000867void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100868 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700869 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000870}
871
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100872void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100873 LocationSummary* locations =
874 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100875 switch (store->InputAt(1)->GetType()) {
876 case Primitive::kPrimBoolean:
877 case Primitive::kPrimByte:
878 case Primitive::kPrimChar:
879 case Primitive::kPrimShort:
880 case Primitive::kPrimInt:
881 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100882 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100883 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
884 break;
885
886 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100887 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100888 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
889 break;
890
891 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100892 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100893 }
894 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000895}
896
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000897void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700898 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000899}
900
Dave Allison20dfc792014-06-16 20:44:29 -0700901void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100902 LocationSummary* locations =
903 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100904 locations->SetInAt(0, Location::RequiresRegister());
905 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100906 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100907 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100908 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000909}
910
Dave Allison20dfc792014-06-16 20:44:29 -0700911void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
912 if (comp->NeedsMaterialization()) {
913 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100914 Register reg = locations->Out().As<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100915 // Clear register: setcc only sets the low byte.
916 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700917 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100918 __ cmpl(locations->InAt(0).As<Register>(),
919 locations->InAt(1).As<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100920 } else if (locations->InAt(1).IsConstant()) {
921 HConstant* instruction = locations->InAt(1).GetConstant();
922 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100923 __ cmpl(locations->InAt(0).As<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700924 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100925 __ cmpl(locations->InAt(0).As<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700926 Address(ESP, locations->InAt(1).GetStackIndex()));
927 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100928 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100929 }
Dave Allison20dfc792014-06-16 20:44:29 -0700930}
931
932void LocationsBuilderX86::VisitEqual(HEqual* comp) {
933 VisitCondition(comp);
934}
935
936void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
937 VisitCondition(comp);
938}
939
940void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
941 VisitCondition(comp);
942}
943
944void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
945 VisitCondition(comp);
946}
947
948void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
949 VisitCondition(comp);
950}
951
952void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
953 VisitCondition(comp);
954}
955
956void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
957 VisitCondition(comp);
958}
959
960void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
961 VisitCondition(comp);
962}
963
964void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
965 VisitCondition(comp);
966}
967
968void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
969 VisitCondition(comp);
970}
971
972void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
973 VisitCondition(comp);
974}
975
976void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
977 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000978}
979
980void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100981 LocationSummary* locations =
982 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100983 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000984}
985
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000986void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100987 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700988 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000989}
990
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100991void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100992 LocationSummary* locations =
993 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100994 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100995}
996
997void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
998 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700999 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001000}
1001
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001002void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1003 LocationSummary* locations =
1004 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1005 locations->SetOut(Location::ConstantLocation(constant));
1006}
1007
1008void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1009 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001010 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001011}
1012
1013void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1014 LocationSummary* locations =
1015 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1016 locations->SetOut(Location::ConstantLocation(constant));
1017}
1018
1019void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1020 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001021 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001022}
1023
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001024void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001025 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001026}
1027
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001028void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001029 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001030 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001031 __ ret();
1032}
1033
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001034void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001035 LocationSummary* locations =
1036 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001037 switch (ret->InputAt(0)->GetType()) {
1038 case Primitive::kPrimBoolean:
1039 case Primitive::kPrimByte:
1040 case Primitive::kPrimChar:
1041 case Primitive::kPrimShort:
1042 case Primitive::kPrimInt:
1043 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001044 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001045 break;
1046
1047 case Primitive::kPrimLong:
1048 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001049 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001050 break;
1051
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001052 case Primitive::kPrimFloat:
1053 case Primitive::kPrimDouble:
1054 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001055 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001056 break;
1057
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001058 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001059 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001060 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001061}
1062
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001063void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001064 if (kIsDebugBuild) {
1065 switch (ret->InputAt(0)->GetType()) {
1066 case Primitive::kPrimBoolean:
1067 case Primitive::kPrimByte:
1068 case Primitive::kPrimChar:
1069 case Primitive::kPrimShort:
1070 case Primitive::kPrimInt:
1071 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001072 DCHECK_EQ(ret->GetLocations()->InAt(0).As<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001073 break;
1074
1075 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001076 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1077 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001078 break;
1079
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001080 case Primitive::kPrimFloat:
1081 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001082 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001083 break;
1084
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001085 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001086 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001087 }
1088 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001089 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001090 __ ret();
1091}
1092
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001093void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001094 HandleInvoke(invoke);
1095}
1096
1097void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001098 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001099
1100 // TODO: Implement all kinds of calls:
1101 // 1) boot -> boot
1102 // 2) app -> boot
1103 // 3) app -> app
1104 //
1105 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1106
1107 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001108 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001109 // temp = temp->dex_cache_resolved_methods_;
1110 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
1111 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001112 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001113 // (temp + offset_of_quick_compiled_code)()
1114 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1115
1116 DCHECK(!codegen_->IsLeafMethod());
1117 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1118}
1119
1120void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1121 HandleInvoke(invoke);
1122}
1123
1124void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001125 LocationSummary* locations =
1126 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001127 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001128
1129 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001130 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001131 HInstruction* input = invoke->InputAt(i);
1132 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1133 }
1134
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001135 switch (invoke->GetType()) {
1136 case Primitive::kPrimBoolean:
1137 case Primitive::kPrimByte:
1138 case Primitive::kPrimChar:
1139 case Primitive::kPrimShort:
1140 case Primitive::kPrimInt:
1141 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001142 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001143 break;
1144
1145 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001146 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001147 break;
1148
1149 case Primitive::kPrimVoid:
1150 break;
1151
1152 case Primitive::kPrimDouble:
1153 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001154 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001155 break;
1156 }
1157
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001158 invoke->SetLocations(locations);
1159}
1160
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001161void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001162 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001163 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1164 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1165 LocationSummary* locations = invoke->GetLocations();
1166 Location receiver = locations->InAt(0);
1167 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1168 // temp = object->GetClass();
1169 if (receiver.IsStackSlot()) {
1170 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1171 __ movl(temp, Address(temp, class_offset));
1172 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001173 __ movl(temp, Address(receiver.As<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001174 }
1175 // temp = temp->GetMethodAt(method_offset);
1176 __ movl(temp, Address(temp, method_offset));
1177 // call temp->GetEntryPoint();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001178 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1179
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001180 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001181 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001182}
1183
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001184void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1185 HandleInvoke(invoke);
1186 // Add the hidden argument.
1187 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM0));
1188}
1189
1190void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1191 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1192 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
1193 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1194 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1195 LocationSummary* locations = invoke->GetLocations();
1196 Location receiver = locations->InAt(0);
1197 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1198
1199 // Set the hidden argument.
1200 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
1201 __ movd(invoke->GetLocations()->GetTemp(1).As<XmmRegister>(), temp);
1202
1203 // temp = object->GetClass();
1204 if (receiver.IsStackSlot()) {
1205 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1206 __ movl(temp, Address(temp, class_offset));
1207 } else {
1208 __ movl(temp, Address(receiver.As<Register>(), class_offset));
1209 }
1210 // temp = temp->GetImtEntryAt(method_offset);
1211 __ movl(temp, Address(temp, method_offset));
1212 // call temp->GetEntryPoint();
1213 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1214
1215 DCHECK(!codegen_->IsLeafMethod());
1216 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1217}
1218
Roland Levillain88cb1752014-10-20 16:36:47 +01001219void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1220 LocationSummary* locations =
1221 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1222 switch (neg->GetResultType()) {
1223 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001224 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001225 locations->SetInAt(0, Location::RequiresRegister());
1226 locations->SetOut(Location::SameAsFirstInput());
1227 break;
1228
Roland Levillain88cb1752014-10-20 16:36:47 +01001229 case Primitive::kPrimFloat:
1230 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001231 locations->SetInAt(0, Location::RequiresFpuRegister());
1232 // Output overlaps as we need a fresh (zero-initialized)
1233 // register to perform subtraction from zero.
1234 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001235 break;
1236
1237 default:
1238 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1239 }
1240}
1241
1242void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1243 LocationSummary* locations = neg->GetLocations();
1244 Location out = locations->Out();
1245 Location in = locations->InAt(0);
1246 switch (neg->GetResultType()) {
1247 case Primitive::kPrimInt:
1248 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001249 DCHECK(in.Equals(out));
Roland Levillain88cb1752014-10-20 16:36:47 +01001250 __ negl(out.As<Register>());
1251 break;
1252
1253 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001254 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001255 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001256 __ negl(out.AsRegisterPairLow<Register>());
1257 // Negation is similar to subtraction from zero. The least
1258 // significant byte triggers a borrow when it is different from
1259 // zero; to take it into account, add 1 to the most significant
1260 // byte if the carry flag (CF) is set to 1 after the first NEGL
1261 // operation.
1262 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1263 __ negl(out.AsRegisterPairHigh<Register>());
1264 break;
1265
Roland Levillain88cb1752014-10-20 16:36:47 +01001266 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001267 DCHECK(!in.Equals(out));
1268 // out = 0
1269 __ xorps(out.As<XmmRegister>(), out.As<XmmRegister>());
1270 // out = out - in
1271 __ subss(out.As<XmmRegister>(), in.As<XmmRegister>());
1272 break;
1273
Roland Levillain88cb1752014-10-20 16:36:47 +01001274 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001275 DCHECK(!in.Equals(out));
1276 // out = 0
1277 __ xorpd(out.As<XmmRegister>(), out.As<XmmRegister>());
1278 // out = out - in
1279 __ subsd(out.As<XmmRegister>(), in.As<XmmRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001280 break;
1281
1282 default:
1283 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1284 }
1285}
1286
Roland Levillaindff1f282014-11-05 14:15:05 +00001287void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
1288 LocationSummary* locations =
1289 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1290 Primitive::Type result_type = conversion->GetResultType();
1291 Primitive::Type input_type = conversion->GetInputType();
1292 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001293 case Primitive::kPrimByte:
1294 switch (input_type) {
1295 case Primitive::kPrimShort:
1296 case Primitive::kPrimInt:
1297 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001298 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001299 locations->SetInAt(0, Location::Any());
1300 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1301 break;
1302
1303 default:
1304 LOG(FATAL) << "Unexpected type conversion from " << input_type
1305 << " to " << result_type;
1306 }
1307 break;
1308
Roland Levillain01a8d712014-11-14 16:27:39 +00001309 case Primitive::kPrimShort:
1310 switch (input_type) {
1311 case Primitive::kPrimByte:
1312 case Primitive::kPrimInt:
1313 case Primitive::kPrimChar:
1314 // Processing a Dex `int-to-short' instruction.
1315 locations->SetInAt(0, Location::Any());
1316 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1317 break;
1318
1319 default:
1320 LOG(FATAL) << "Unexpected type conversion from " << input_type
1321 << " to " << result_type;
1322 }
1323 break;
1324
Roland Levillain946e1432014-11-11 17:35:19 +00001325 case Primitive::kPrimInt:
1326 switch (input_type) {
1327 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001328 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001329 locations->SetInAt(0, Location::Any());
1330 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1331 break;
1332
1333 case Primitive::kPrimFloat:
1334 case Primitive::kPrimDouble:
1335 LOG(FATAL) << "Type conversion from " << input_type
1336 << " to " << result_type << " not yet implemented";
1337 break;
1338
1339 default:
1340 LOG(FATAL) << "Unexpected type conversion from " << input_type
1341 << " to " << result_type;
1342 }
1343 break;
1344
Roland Levillaindff1f282014-11-05 14:15:05 +00001345 case Primitive::kPrimLong:
1346 switch (input_type) {
1347 case Primitive::kPrimByte:
1348 case Primitive::kPrimShort:
1349 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001350 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001351 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001352 locations->SetInAt(0, Location::RegisterLocation(EAX));
1353 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1354 break;
1355
1356 case Primitive::kPrimFloat:
1357 case Primitive::kPrimDouble:
1358 LOG(FATAL) << "Type conversion from " << input_type << " to "
1359 << result_type << " not yet implemented";
1360 break;
1361
1362 default:
1363 LOG(FATAL) << "Unexpected type conversion from " << input_type
1364 << " to " << result_type;
1365 }
1366 break;
1367
Roland Levillain981e4542014-11-14 11:47:14 +00001368 case Primitive::kPrimChar:
1369 switch (input_type) {
1370 case Primitive::kPrimByte:
1371 case Primitive::kPrimShort:
1372 case Primitive::kPrimInt:
1373 case Primitive::kPrimChar:
1374 // Processing a Dex `int-to-char' instruction.
1375 locations->SetInAt(0, Location::Any());
1376 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1377 break;
1378
1379 default:
1380 LOG(FATAL) << "Unexpected type conversion from " << input_type
1381 << " to " << result_type;
1382 }
1383 break;
1384
Roland Levillaindff1f282014-11-05 14:15:05 +00001385 case Primitive::kPrimFloat:
1386 case Primitive::kPrimDouble:
1387 LOG(FATAL) << "Type conversion from " << input_type
1388 << " to " << result_type << " not yet implemented";
1389 break;
1390
1391 default:
1392 LOG(FATAL) << "Unexpected type conversion from " << input_type
1393 << " to " << result_type;
1394 }
1395}
1396
1397void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1398 LocationSummary* locations = conversion->GetLocations();
1399 Location out = locations->Out();
1400 Location in = locations->InAt(0);
1401 Primitive::Type result_type = conversion->GetResultType();
1402 Primitive::Type input_type = conversion->GetInputType();
1403 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001404 case Primitive::kPrimByte:
1405 switch (input_type) {
1406 case Primitive::kPrimShort:
1407 case Primitive::kPrimInt:
1408 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001409 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001410 if (in.IsRegister()) {
1411 __ movsxb(out.As<Register>(), in.As<ByteRegister>());
1412 } else if (in.IsStackSlot()) {
1413 __ movsxb(out.As<Register>(), Address(ESP, in.GetStackIndex()));
1414 } else {
1415 DCHECK(in.GetConstant()->IsIntConstant());
1416 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1417 __ movl(out.As<Register>(), Immediate(static_cast<int8_t>(value)));
1418 }
1419 break;
1420
1421 default:
1422 LOG(FATAL) << "Unexpected type conversion from " << input_type
1423 << " to " << result_type;
1424 }
1425 break;
1426
Roland Levillain01a8d712014-11-14 16:27:39 +00001427 case Primitive::kPrimShort:
1428 switch (input_type) {
1429 case Primitive::kPrimByte:
1430 case Primitive::kPrimInt:
1431 case Primitive::kPrimChar:
1432 // Processing a Dex `int-to-short' instruction.
1433 if (in.IsRegister()) {
1434 __ movsxw(out.As<Register>(), in.As<Register>());
1435 } else if (in.IsStackSlot()) {
1436 __ movsxw(out.As<Register>(), Address(ESP, in.GetStackIndex()));
1437 } else {
1438 DCHECK(in.GetConstant()->IsIntConstant());
1439 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1440 __ movl(out.As<Register>(), Immediate(static_cast<int16_t>(value)));
1441 }
1442 break;
1443
1444 default:
1445 LOG(FATAL) << "Unexpected type conversion from " << input_type
1446 << " to " << result_type;
1447 }
1448 break;
1449
Roland Levillain946e1432014-11-11 17:35:19 +00001450 case Primitive::kPrimInt:
1451 switch (input_type) {
1452 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001453 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001454 if (in.IsRegisterPair()) {
1455 __ movl(out.As<Register>(), in.AsRegisterPairLow<Register>());
1456 } else if (in.IsDoubleStackSlot()) {
1457 __ movl(out.As<Register>(), Address(ESP, in.GetStackIndex()));
1458 } else {
1459 DCHECK(in.IsConstant());
1460 DCHECK(in.GetConstant()->IsLongConstant());
1461 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
1462 __ movl(out.As<Register>(), Immediate(static_cast<int32_t>(value)));
1463 }
1464 break;
1465
1466 case Primitive::kPrimFloat:
1467 case Primitive::kPrimDouble:
1468 LOG(FATAL) << "Type conversion from " << input_type
1469 << " to " << result_type << " not yet implemented";
1470 break;
1471
1472 default:
1473 LOG(FATAL) << "Unexpected type conversion from " << input_type
1474 << " to " << result_type;
1475 }
1476 break;
1477
Roland Levillaindff1f282014-11-05 14:15:05 +00001478 case Primitive::kPrimLong:
1479 switch (input_type) {
1480 case Primitive::kPrimByte:
1481 case Primitive::kPrimShort:
1482 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001483 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001484 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001485 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1486 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
1487 DCHECK_EQ(in.As<Register>(), EAX);
1488 __ cdq();
1489 break;
1490
1491 case Primitive::kPrimFloat:
1492 case Primitive::kPrimDouble:
1493 LOG(FATAL) << "Type conversion from " << input_type << " to "
1494 << result_type << " not yet implemented";
1495 break;
1496
1497 default:
1498 LOG(FATAL) << "Unexpected type conversion from " << input_type
1499 << " to " << result_type;
1500 }
1501 break;
1502
Roland Levillain981e4542014-11-14 11:47:14 +00001503 case Primitive::kPrimChar:
1504 switch (input_type) {
1505 case Primitive::kPrimByte:
1506 case Primitive::kPrimShort:
1507 case Primitive::kPrimInt:
1508 case Primitive::kPrimChar:
1509 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1510 if (in.IsRegister()) {
1511 __ movzxw(out.As<Register>(), in.As<Register>());
1512 } else if (in.IsStackSlot()) {
1513 __ movzxw(out.As<Register>(), Address(ESP, in.GetStackIndex()));
1514 } else {
1515 DCHECK(in.GetConstant()->IsIntConstant());
1516 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1517 __ movl(out.As<Register>(), Immediate(static_cast<uint16_t>(value)));
1518 }
1519 break;
1520
1521 default:
1522 LOG(FATAL) << "Unexpected type conversion from " << input_type
1523 << " to " << result_type;
1524 }
1525 break;
1526
Roland Levillaindff1f282014-11-05 14:15:05 +00001527 case Primitive::kPrimFloat:
1528 case Primitive::kPrimDouble:
1529 LOG(FATAL) << "Type conversion from " << input_type
1530 << " to " << result_type << " not yet implemented";
1531 break;
1532
1533 default:
1534 LOG(FATAL) << "Unexpected type conversion from " << input_type
1535 << " to " << result_type;
1536 }
1537}
1538
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001539void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001540 LocationSummary* locations =
1541 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001542 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001543 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001544 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001545 locations->SetInAt(0, Location::RequiresRegister());
1546 locations->SetInAt(1, Location::Any());
1547 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001548 break;
1549 }
1550
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001551 case Primitive::kPrimFloat:
1552 case Primitive::kPrimDouble: {
1553 locations->SetInAt(0, Location::RequiresFpuRegister());
1554 locations->SetInAt(1, Location::Any());
1555 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001556 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001557 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001558
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001559 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001560 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1561 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001562 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001563}
1564
1565void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1566 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001567 Location first = locations->InAt(0);
1568 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001569 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001570 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001571 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001572 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001573 __ addl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001574 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001575 __ addl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001576 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001577 __ addl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001578 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001579 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001580 }
1581
1582 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001583 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001584 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1585 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001586 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001587 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1588 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001589 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001590 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001591 break;
1592 }
1593
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001594 case Primitive::kPrimFloat: {
1595 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001596 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001597 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001598 __ addss(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001599 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001600 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001601 }
1602
1603 case Primitive::kPrimDouble: {
1604 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001605 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001606 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001607 __ addsd(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001608 }
1609 break;
1610 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001611
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001612 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001613 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001614 }
1615}
1616
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001617void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001618 LocationSummary* locations =
1619 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001620 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001621 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001622 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001623 locations->SetInAt(0, Location::RequiresRegister());
1624 locations->SetInAt(1, Location::Any());
1625 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001626 break;
1627 }
Calin Juravle11351682014-10-23 15:38:15 +01001628 case Primitive::kPrimFloat:
1629 case Primitive::kPrimDouble: {
1630 locations->SetInAt(0, Location::RequiresFpuRegister());
1631 locations->SetInAt(1, Location::RequiresFpuRegister());
1632 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001633 break;
Calin Juravle11351682014-10-23 15:38:15 +01001634 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001635
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001636 default:
Calin Juravle11351682014-10-23 15:38:15 +01001637 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001638 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001639}
1640
1641void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1642 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001643 Location first = locations->InAt(0);
1644 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001645 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001646 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001647 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001648 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001649 __ subl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001650 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001651 __ subl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001652 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001653 __ subl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001654 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001655 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001656 }
1657
1658 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001659 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01001660 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1661 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001662 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001663 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001664 __ sbbl(first.AsRegisterPairHigh<Register>(),
1665 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001666 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001667 break;
1668 }
1669
Calin Juravle11351682014-10-23 15:38:15 +01001670 case Primitive::kPrimFloat: {
1671 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001672 break;
Calin Juravle11351682014-10-23 15:38:15 +01001673 }
1674
1675 case Primitive::kPrimDouble: {
1676 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1677 break;
1678 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001679
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001680 default:
Calin Juravle11351682014-10-23 15:38:15 +01001681 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001682 }
1683}
1684
Calin Juravle34bacdf2014-10-07 20:23:36 +01001685void LocationsBuilderX86::VisitMul(HMul* mul) {
1686 LocationSummary* locations =
1687 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1688 switch (mul->GetResultType()) {
1689 case Primitive::kPrimInt:
1690 locations->SetInAt(0, Location::RequiresRegister());
1691 locations->SetInAt(1, Location::Any());
1692 locations->SetOut(Location::SameAsFirstInput());
1693 break;
1694 case Primitive::kPrimLong: {
1695 locations->SetInAt(0, Location::RequiresRegister());
1696 // TODO: Currently this handles only stack operands:
1697 // - we don't have enough registers because we currently use Quick ABI.
1698 // - by the time we have a working register allocator we will probably change the ABI
1699 // and fix the above.
1700 // - we don't have a way yet to request operands on stack but the base line compiler
1701 // will leave the operands on the stack with Any().
1702 locations->SetInAt(1, Location::Any());
1703 locations->SetOut(Location::SameAsFirstInput());
1704 // Needed for imul on 32bits with 64bits output.
1705 locations->AddTemp(Location::RegisterLocation(EAX));
1706 locations->AddTemp(Location::RegisterLocation(EDX));
1707 break;
1708 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001709 case Primitive::kPrimFloat:
1710 case Primitive::kPrimDouble: {
1711 locations->SetInAt(0, Location::RequiresFpuRegister());
1712 locations->SetInAt(1, Location::RequiresFpuRegister());
1713 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001714 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001715 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001716
1717 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001718 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001719 }
1720}
1721
1722void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1723 LocationSummary* locations = mul->GetLocations();
1724 Location first = locations->InAt(0);
1725 Location second = locations->InAt(1);
1726 DCHECK(first.Equals(locations->Out()));
1727
1728 switch (mul->GetResultType()) {
1729 case Primitive::kPrimInt: {
1730 if (second.IsRegister()) {
1731 __ imull(first.As<Register>(), second.As<Register>());
1732 } else if (second.IsConstant()) {
1733 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1734 __ imull(first.As<Register>(), imm);
1735 } else {
1736 DCHECK(second.IsStackSlot());
1737 __ imull(first.As<Register>(), Address(ESP, second.GetStackIndex()));
1738 }
1739 break;
1740 }
1741
1742 case Primitive::kPrimLong: {
1743 DCHECK(second.IsDoubleStackSlot());
1744
1745 Register in1_hi = first.AsRegisterPairHigh<Register>();
1746 Register in1_lo = first.AsRegisterPairLow<Register>();
1747 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
1748 Address in2_lo(ESP, second.GetStackIndex());
1749 Register eax = locations->GetTemp(0).As<Register>();
1750 Register edx = locations->GetTemp(1).As<Register>();
1751
1752 DCHECK_EQ(EAX, eax);
1753 DCHECK_EQ(EDX, edx);
1754
1755 // input: in1 - 64 bits, in2 - 64 bits
1756 // output: in1
1757 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
1758 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
1759 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
1760
1761 __ movl(eax, in2_hi);
1762 // eax <- in1.lo * in2.hi
1763 __ imull(eax, in1_lo);
1764 // in1.hi <- in1.hi * in2.lo
1765 __ imull(in1_hi, in2_lo);
1766 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
1767 __ addl(in1_hi, eax);
1768 // move in1_lo to eax to prepare for double precision
1769 __ movl(eax, in1_lo);
1770 // edx:eax <- in1.lo * in2.lo
1771 __ mull(in2_lo);
1772 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
1773 __ addl(in1_hi, edx);
1774 // in1.lo <- (in1.lo * in2.lo)[31:0];
1775 __ movl(in1_lo, eax);
1776
1777 break;
1778 }
1779
Calin Juravleb5bfa962014-10-21 18:02:24 +01001780 case Primitive::kPrimFloat: {
1781 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001782 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001783 }
1784
1785 case Primitive::kPrimDouble: {
1786 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1787 break;
1788 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001789
1790 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001791 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001792 }
1793}
1794
Calin Juravle7c4954d2014-10-28 16:57:40 +00001795void LocationsBuilderX86::VisitDiv(HDiv* div) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001796 LocationSummary::CallKind call_kind = div->GetResultType() == Primitive::kPrimLong
1797 ? LocationSummary::kCall
1798 : LocationSummary::kNoCall;
1799 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
1800
Calin Juravle7c4954d2014-10-28 16:57:40 +00001801 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001802 case Primitive::kPrimInt: {
1803 locations->SetInAt(0, Location::RegisterLocation(EAX));
1804 locations->SetInAt(1, Location::RequiresRegister());
1805 locations->SetOut(Location::SameAsFirstInput());
1806 // Intel uses edx:eax as the dividend.
1807 locations->AddTemp(Location::RegisterLocation(EDX));
1808 break;
1809 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001810 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001811 InvokeRuntimeCallingConvention calling_convention;
1812 locations->SetInAt(0, Location::RegisterPairLocation(
1813 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
1814 locations->SetInAt(1, Location::RegisterPairLocation(
1815 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
1816 // Runtime helper puts the result in EAX, EDX.
1817 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00001818 break;
1819 }
1820 case Primitive::kPrimFloat:
1821 case Primitive::kPrimDouble: {
1822 locations->SetInAt(0, Location::RequiresFpuRegister());
1823 locations->SetInAt(1, Location::RequiresFpuRegister());
1824 locations->SetOut(Location::SameAsFirstInput());
1825 break;
1826 }
1827
1828 default:
1829 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1830 }
1831}
1832
1833void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
1834 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001835 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00001836 Location first = locations->InAt(0);
1837 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001838
1839 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001840 case Primitive::kPrimInt: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001841 DCHECK(first.Equals(out));
Calin Juravled0d48522014-11-04 16:40:20 +00001842 Register first_reg = first.As<Register>();
1843 Register second_reg = second.As<Register>();
1844 DCHECK_EQ(EAX, first_reg);
1845 DCHECK_EQ(EDX, locations->GetTemp(0).As<Register>());
1846
1847 SlowPathCodeX86* slow_path =
1848 new (GetGraph()->GetArena()) DivMinusOneSlowPathX86(first_reg);
1849 codegen_->AddSlowPath(slow_path);
1850
1851 // 0x80000000/-1 triggers an arithmetic exception!
1852 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
1853 // it's safe to just use negl instead of more complex comparisons.
1854
1855 __ cmpl(second_reg, Immediate(-1));
1856 __ j(kEqual, slow_path->GetEntryLabel());
1857
1858 // edx:eax <- sign-extended of eax
1859 __ cdq();
1860 // eax = quotient, edx = remainder
1861 __ idivl(second_reg);
1862
1863 __ Bind(slow_path->GetExitLabel());
1864 break;
1865 }
1866
Calin Juravle7c4954d2014-10-28 16:57:40 +00001867 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001868 InvokeRuntimeCallingConvention calling_convention;
1869 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
1870 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
1871 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
1872 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
1873 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
1874 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
1875
1876 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
1877 codegen_->RecordPcInfo(div, div->GetDexPc());
1878
Calin Juravle7c4954d2014-10-28 16:57:40 +00001879 break;
1880 }
1881
1882 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001883 DCHECK(first.Equals(out));
Calin Juravle7c4954d2014-10-28 16:57:40 +00001884 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1885 break;
1886 }
1887
1888 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001889 DCHECK(first.Equals(out));
Calin Juravle7c4954d2014-10-28 16:57:40 +00001890 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1891 break;
1892 }
1893
1894 default:
1895 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1896 }
1897}
1898
Calin Juravled0d48522014-11-04 16:40:20 +00001899void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1900 LocationSummary* locations =
1901 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001902 switch (instruction->GetType()) {
1903 case Primitive::kPrimInt: {
1904 locations->SetInAt(0, Location::Any());
1905 break;
1906 }
1907 case Primitive::kPrimLong: {
1908 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
1909 if (!instruction->IsConstant()) {
1910 locations->AddTemp(Location::RequiresRegister());
1911 }
1912 break;
1913 }
1914 default:
1915 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
1916 }
Calin Juravled0d48522014-11-04 16:40:20 +00001917 if (instruction->HasUses()) {
1918 locations->SetOut(Location::SameAsFirstInput());
1919 }
1920}
1921
1922void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1923 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
1924 codegen_->AddSlowPath(slow_path);
1925
1926 LocationSummary* locations = instruction->GetLocations();
1927 Location value = locations->InAt(0);
1928
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001929 switch (instruction->GetType()) {
1930 case Primitive::kPrimInt: {
1931 if (value.IsRegister()) {
1932 __ testl(value.As<Register>(), value.As<Register>());
1933 __ j(kEqual, slow_path->GetEntryLabel());
1934 } else if (value.IsStackSlot()) {
1935 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
1936 __ j(kEqual, slow_path->GetEntryLabel());
1937 } else {
1938 DCHECK(value.IsConstant()) << value;
1939 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
1940 __ jmp(slow_path->GetEntryLabel());
1941 }
1942 }
1943 break;
Calin Juravled0d48522014-11-04 16:40:20 +00001944 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001945 case Primitive::kPrimLong: {
1946 if (value.IsRegisterPair()) {
1947 Register temp = locations->GetTemp(0).As<Register>();
1948 __ movl(temp, value.AsRegisterPairLow<Register>());
1949 __ orl(temp, value.AsRegisterPairHigh<Register>());
1950 __ j(kEqual, slow_path->GetEntryLabel());
1951 } else {
1952 DCHECK(value.IsConstant()) << value;
1953 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
1954 __ jmp(slow_path->GetEntryLabel());
1955 }
1956 }
1957 break;
1958 }
1959 default:
1960 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00001961 }
Calin Juravled0d48522014-11-04 16:40:20 +00001962}
1963
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001964void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001965 LocationSummary* locations =
1966 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001967 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001968 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001969 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1970 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001971}
1972
1973void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
1974 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001975 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001976 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001977
Nicolas Geoffray707c8092014-04-04 10:50:14 +01001978 __ fs()->call(
1979 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001980
Nicolas Geoffray39468442014-09-02 15:17:15 +01001981 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001982 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001983}
1984
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001985void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
1986 LocationSummary* locations =
1987 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1988 locations->SetOut(Location::RegisterLocation(EAX));
1989 InvokeRuntimeCallingConvention calling_convention;
1990 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1991 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1992 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1993}
1994
1995void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
1996 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001997 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001998 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
1999
2000 __ fs()->call(
2001 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
2002
2003 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2004 DCHECK(!codegen_->IsLeafMethod());
2005}
2006
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002007void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002008 LocationSummary* locations =
2009 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002010 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2011 if (location.IsStackSlot()) {
2012 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2013 } else if (location.IsDoubleStackSlot()) {
2014 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002015 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002016 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002017}
2018
2019void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002020 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002021}
2022
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002023void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002024 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002025 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002026 locations->SetInAt(0, Location::RequiresRegister());
2027 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002028}
2029
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002030void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2031 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002032 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002033 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002034 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002035 switch (not_->InputAt(0)->GetType()) {
2036 case Primitive::kPrimBoolean:
2037 __ xorl(out.As<Register>(), Immediate(1));
2038 break;
2039
2040 case Primitive::kPrimInt:
2041 __ notl(out.As<Register>());
2042 break;
2043
2044 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002045 __ notl(out.AsRegisterPairLow<Register>());
2046 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002047 break;
2048
2049 default:
2050 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2051 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002052}
2053
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002054void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002055 LocationSummary* locations =
2056 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002057 locations->SetInAt(0, Location::RequiresRegister());
2058 locations->SetInAt(1, Location::Any());
2059 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002060}
2061
2062void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002063 LocationSummary* locations = compare->GetLocations();
2064 switch (compare->InputAt(0)->GetType()) {
2065 case Primitive::kPrimLong: {
2066 Label less, greater, done;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002067 Register output = locations->Out().As<Register>();
2068 Location left = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002069 Location right = locations->InAt(1);
2070 if (right.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002071 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002072 } else {
2073 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002074 __ cmpl(left.AsRegisterPairHigh<Register>(),
2075 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002076 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002077 __ j(kLess, &less); // Signed compare.
2078 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002079 if (right.IsRegisterPair()) {
2080 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002081 } else {
2082 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002083 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002084 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002085 __ movl(output, Immediate(0));
2086 __ j(kEqual, &done);
2087 __ j(kBelow, &less); // Unsigned compare.
2088
2089 __ Bind(&greater);
2090 __ movl(output, Immediate(1));
2091 __ jmp(&done);
2092
2093 __ Bind(&less);
2094 __ movl(output, Immediate(-1));
2095
2096 __ Bind(&done);
2097 break;
2098 }
2099 default:
2100 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
2101 }
2102}
2103
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002104void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002105 LocationSummary* locations =
2106 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002107 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2108 locations->SetInAt(i, Location::Any());
2109 }
2110 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002111}
2112
2113void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002114 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002115 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002116}
2117
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002118void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002119 LocationSummary* locations =
2120 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002121 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002122 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002123 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002124 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2125 || (field_type == Primitive::kPrimByte);
2126 // The register allocator does not support multiple
2127 // inputs that die at entry with one in a specific register.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002128 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002129 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002130 locations->SetInAt(1, Location::RegisterLocation(EAX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002131 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002132 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002133 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002134 // Temporary registers for the write barrier.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002135 if (is_object_type) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002136 locations->AddTemp(Location::RequiresRegister());
2137 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002138 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002139 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002140}
2141
2142void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2143 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002144 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002145 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01002146 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002147
2148 switch (field_type) {
2149 case Primitive::kPrimBoolean:
2150 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002151 ByteRegister value = locations->InAt(1).As<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002152 __ movb(Address(obj, offset), value);
2153 break;
2154 }
2155
2156 case Primitive::kPrimShort:
2157 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002158 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002159 __ movw(Address(obj, offset), value);
2160 break;
2161 }
2162
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002163 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002164 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002165 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002166 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002167
2168 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002169 Register temp = locations->GetTemp(0).As<Register>();
2170 Register card = locations->GetTemp(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002171 codegen_->MarkGCCard(temp, card, obj, value);
2172 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002173 break;
2174 }
2175
2176 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002177 Location value = locations->InAt(1);
2178 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
2179 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002180 break;
2181 }
2182
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002183 case Primitive::kPrimFloat: {
2184 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2185 __ movss(Address(obj, offset), value);
2186 break;
2187 }
2188
2189 case Primitive::kPrimDouble: {
2190 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2191 __ movsd(Address(obj, offset), value);
2192 break;
2193 }
2194
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002195 case Primitive::kPrimVoid:
2196 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002197 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002198 }
2199}
2200
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002201void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
2202 Label is_null;
2203 __ testl(value, value);
2204 __ j(kEqual, &is_null);
2205 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2206 __ movl(temp, object);
2207 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
2208 __ movb(Address(temp, card, TIMES_1, 0),
2209 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
2210 __ Bind(&is_null);
2211}
2212
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002213void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002214 LocationSummary* locations =
2215 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002216 locations->SetInAt(0, Location::RequiresRegister());
2217 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002218}
2219
2220void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2221 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002222 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002223 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2224
2225 switch (instruction->GetType()) {
2226 case Primitive::kPrimBoolean: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002227 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002228 __ movzxb(out, Address(obj, offset));
2229 break;
2230 }
2231
2232 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002233 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002234 __ movsxb(out, Address(obj, offset));
2235 break;
2236 }
2237
2238 case Primitive::kPrimShort: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002239 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002240 __ movsxw(out, Address(obj, offset));
2241 break;
2242 }
2243
2244 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002245 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002246 __ movzxw(out, Address(obj, offset));
2247 break;
2248 }
2249
2250 case Primitive::kPrimInt:
2251 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002252 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002253 __ movl(out, Address(obj, offset));
2254 break;
2255 }
2256
2257 case Primitive::kPrimLong: {
2258 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002259 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
2260 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002261 break;
2262 }
2263
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002264 case Primitive::kPrimFloat: {
2265 XmmRegister out = locations->Out().As<XmmRegister>();
2266 __ movss(out, Address(obj, offset));
2267 break;
2268 }
2269
2270 case Primitive::kPrimDouble: {
2271 XmmRegister out = locations->Out().As<XmmRegister>();
2272 __ movsd(out, Address(obj, offset));
2273 break;
2274 }
2275
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002276 case Primitive::kPrimVoid:
2277 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002278 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002279 }
2280}
2281
2282void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002283 LocationSummary* locations =
2284 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002285 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002286 if (instruction->HasUses()) {
2287 locations->SetOut(Location::SameAsFirstInput());
2288 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002289}
2290
2291void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002292 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002293 codegen_->AddSlowPath(slow_path);
2294
2295 LocationSummary* locations = instruction->GetLocations();
2296 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002297
2298 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002299 __ cmpl(obj.As<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002300 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002301 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002302 } else {
2303 DCHECK(obj.IsConstant()) << obj;
2304 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2305 __ jmp(slow_path->GetEntryLabel());
2306 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002307 }
2308 __ j(kEqual, slow_path->GetEntryLabel());
2309}
2310
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002311void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002312 LocationSummary* locations =
2313 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002314 locations->SetInAt(0, Location::RequiresRegister());
2315 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2316 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002317}
2318
2319void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
2320 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002321 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002322 Location index = locations->InAt(1);
2323
2324 switch (instruction->GetType()) {
2325 case Primitive::kPrimBoolean: {
2326 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002327 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002328 if (index.IsConstant()) {
2329 __ movzxb(out, Address(obj,
2330 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2331 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002332 __ movzxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002333 }
2334 break;
2335 }
2336
2337 case Primitive::kPrimByte: {
2338 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002339 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002340 if (index.IsConstant()) {
2341 __ movsxb(out, Address(obj,
2342 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2343 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002344 __ movsxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002345 }
2346 break;
2347 }
2348
2349 case Primitive::kPrimShort: {
2350 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002351 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002352 if (index.IsConstant()) {
2353 __ movsxw(out, Address(obj,
2354 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2355 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002356 __ movsxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002357 }
2358 break;
2359 }
2360
2361 case Primitive::kPrimChar: {
2362 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002363 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002364 if (index.IsConstant()) {
2365 __ movzxw(out, Address(obj,
2366 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2367 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002368 __ movzxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002369 }
2370 break;
2371 }
2372
2373 case Primitive::kPrimInt:
2374 case Primitive::kPrimNot: {
2375 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002376 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002377 if (index.IsConstant()) {
2378 __ movl(out, Address(obj,
2379 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2380 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002381 __ movl(out, Address(obj, index.As<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002382 }
2383 break;
2384 }
2385
2386 case Primitive::kPrimLong: {
2387 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002388 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002389 if (index.IsConstant()) {
2390 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002391 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
2392 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002393 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002394 __ movl(out.AsRegisterPairLow<Register>(),
2395 Address(obj, index.As<Register>(), TIMES_8, data_offset));
2396 __ movl(out.AsRegisterPairHigh<Register>(),
2397 Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002398 }
2399 break;
2400 }
2401
2402 case Primitive::kPrimFloat:
2403 case Primitive::kPrimDouble:
2404 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002405 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002406 case Primitive::kPrimVoid:
2407 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002408 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002409 }
2410}
2411
2412void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002413 Primitive::Type value_type = instruction->GetComponentType();
2414 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2415 instruction,
2416 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
2417
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002418 if (value_type == Primitive::kPrimNot) {
2419 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002420 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2421 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2422 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002423 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002424 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
2425 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002426 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002427 // In case of a byte operation, the register allocator does not support multiple
2428 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002429 locations->SetInAt(0, Location::RequiresRegister());
2430 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002431 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002432 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002433 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002434 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002435 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002436 }
2437 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002438}
2439
2440void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
2441 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002442 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002443 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002444 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002445 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002446
2447 switch (value_type) {
2448 case Primitive::kPrimBoolean:
2449 case Primitive::kPrimByte: {
2450 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002451 if (index.IsConstant()) {
2452 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002453 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002454 __ movb(Address(obj, offset), value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002455 } else {
2456 __ movb(Address(obj, offset),
2457 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2458 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002459 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002460 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002461 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
2462 value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002463 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002464 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002465 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2466 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002467 }
2468 break;
2469 }
2470
2471 case Primitive::kPrimShort:
2472 case Primitive::kPrimChar: {
2473 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002474 if (index.IsConstant()) {
2475 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002476 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002477 __ movw(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002478 } else {
2479 __ movw(Address(obj, offset),
2480 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2481 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002482 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002483 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002484 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
2485 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002486 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002487 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002488 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2489 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002490 }
2491 break;
2492 }
2493
2494 case Primitive::kPrimInt: {
2495 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002496 if (index.IsConstant()) {
2497 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002498 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002499 __ movl(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002500 } else {
2501 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2502 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002503 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002504 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002505 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
2506 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002507 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002508 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002509 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2510 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002511 }
2512 break;
2513 }
2514
2515 case Primitive::kPrimNot: {
2516 DCHECK(!codegen_->IsLeafMethod());
2517 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01002518 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002519 break;
2520 }
2521
2522 case Primitive::kPrimLong: {
2523 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002524 if (index.IsConstant()) {
2525 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002526 if (value.IsRegisterPair()) {
2527 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
2528 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002529 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002530 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002531 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
2532 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
2533 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
2534 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002535 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002536 if (value.IsRegisterPair()) {
2537 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
2538 value.AsRegisterPairLow<Register>());
2539 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
2540 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002541 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002542 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002543 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002544 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002545 Immediate(Low32Bits(val)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002546 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002547 Immediate(High32Bits(val)));
2548 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002549 }
2550 break;
2551 }
2552
2553 case Primitive::kPrimFloat:
2554 case Primitive::kPrimDouble:
2555 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002556 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002557 case Primitive::kPrimVoid:
2558 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002559 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002560 }
2561}
2562
2563void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
2564 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002565 locations->SetInAt(0, Location::RequiresRegister());
2566 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002567 instruction->SetLocations(locations);
2568}
2569
2570void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
2571 LocationSummary* locations = instruction->GetLocations();
2572 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002573 Register obj = locations->InAt(0).As<Register>();
2574 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002575 __ movl(out, Address(obj, offset));
2576}
2577
2578void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002579 LocationSummary* locations =
2580 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002581 locations->SetInAt(0, Location::RequiresRegister());
2582 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002583 if (instruction->HasUses()) {
2584 locations->SetOut(Location::SameAsFirstInput());
2585 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002586}
2587
2588void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
2589 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002590 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002591 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002592 codegen_->AddSlowPath(slow_path);
2593
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002594 Register index = locations->InAt(0).As<Register>();
2595 Register length = locations->InAt(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002596
2597 __ cmpl(index, length);
2598 __ j(kAboveEqual, slow_path->GetEntryLabel());
2599}
2600
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002601void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
2602 temp->SetLocations(nullptr);
2603}
2604
2605void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
2606 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002607 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002608}
2609
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002610void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002611 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002612 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002613}
2614
2615void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002616 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2617}
2618
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002619void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
2620 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2621}
2622
2623void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002624 HBasicBlock* block = instruction->GetBlock();
2625 if (block->GetLoopInformation() != nullptr) {
2626 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2627 // The back edge will generate the suspend check.
2628 return;
2629 }
2630 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2631 // The goto will generate the suspend check.
2632 return;
2633 }
2634 GenerateSuspendCheck(instruction, nullptr);
2635}
2636
2637void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
2638 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002639 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002640 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002641 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002642 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002643 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002644 if (successor == nullptr) {
2645 __ j(kNotEqual, slow_path->GetEntryLabel());
2646 __ Bind(slow_path->GetReturnLabel());
2647 } else {
2648 __ j(kEqual, codegen_->GetLabelOf(successor));
2649 __ jmp(slow_path->GetEntryLabel());
2650 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002651}
2652
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002653X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
2654 return codegen_->GetAssembler();
2655}
2656
2657void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
2658 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002659 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002660 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2661 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
2662 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
2663}
2664
2665void ParallelMoveResolverX86::EmitMove(size_t index) {
2666 MoveOperands* move = moves_.Get(index);
2667 Location source = move->GetSource();
2668 Location destination = move->GetDestination();
2669
2670 if (source.IsRegister()) {
2671 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002672 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002673 } else {
2674 DCHECK(destination.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002675 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002676 }
2677 } else if (source.IsStackSlot()) {
2678 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002679 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002680 } else {
2681 DCHECK(destination.IsStackSlot());
2682 MoveMemoryToMemory(destination.GetStackIndex(),
2683 source.GetStackIndex());
2684 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002685 } else if (source.IsConstant()) {
2686 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
2687 Immediate imm(instruction->AsIntConstant()->GetValue());
2688 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002689 __ movl(destination.As<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002690 } else {
2691 __ movl(Address(ESP, destination.GetStackIndex()), imm);
2692 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002693 } else {
2694 LOG(FATAL) << "Unimplemented";
2695 }
2696}
2697
2698void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002699 Register suggested_scratch = reg == EAX ? EBX : EAX;
2700 ScratchRegisterScope ensure_scratch(
2701 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2702
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002703 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2704 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
2705 __ movl(Address(ESP, mem + stack_offset), reg);
2706 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
2707}
2708
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002709void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
2710 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002711 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
2712
2713 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002714 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002715 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2716
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002717 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
2718 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
2719 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
2720 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
2721 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
2722 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
2723}
2724
2725void ParallelMoveResolverX86::EmitSwap(size_t index) {
2726 MoveOperands* move = moves_.Get(index);
2727 Location source = move->GetSource();
2728 Location destination = move->GetDestination();
2729
2730 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002731 __ xchgl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002732 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002733 Exchange(source.As<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002734 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002735 Exchange(destination.As<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002736 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
2737 Exchange(destination.GetStackIndex(), source.GetStackIndex());
2738 } else {
2739 LOG(FATAL) << "Unimplemented";
2740 }
2741}
2742
2743void ParallelMoveResolverX86::SpillScratch(int reg) {
2744 __ pushl(static_cast<Register>(reg));
2745}
2746
2747void ParallelMoveResolverX86::RestoreScratch(int reg) {
2748 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002749}
2750
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002751void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002752 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2753 ? LocationSummary::kCallOnSlowPath
2754 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002755 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002756 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002757 locations->SetOut(Location::RequiresRegister());
2758}
2759
2760void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
2761 Register out = cls->GetLocations()->Out().As<Register>();
2762 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002763 DCHECK(!cls->CanCallRuntime());
2764 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002765 codegen_->LoadCurrentMethod(out);
2766 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2767 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002768 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002769 codegen_->LoadCurrentMethod(out);
2770 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2771 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002772
2773 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
2774 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2775 codegen_->AddSlowPath(slow_path);
2776 __ testl(out, out);
2777 __ j(kEqual, slow_path->GetEntryLabel());
2778 if (cls->MustGenerateClinitCheck()) {
2779 GenerateClassInitializationCheck(slow_path, out);
2780 } else {
2781 __ Bind(slow_path->GetExitLabel());
2782 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002783 }
2784}
2785
2786void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
2787 LocationSummary* locations =
2788 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2789 locations->SetInAt(0, Location::RequiresRegister());
2790 if (check->HasUses()) {
2791 locations->SetOut(Location::SameAsFirstInput());
2792 }
2793}
2794
2795void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002796 // We assume the class to not be null.
2797 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
2798 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002799 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002800 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<Register>());
2801}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002802
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002803void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
2804 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002805 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2806 Immediate(mirror::Class::kStatusInitialized));
2807 __ j(kLess, slow_path->GetEntryLabel());
2808 __ Bind(slow_path->GetExitLabel());
2809 // No need for memory fence, thanks to the X86 memory model.
2810}
2811
2812void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2813 LocationSummary* locations =
2814 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2815 locations->SetInAt(0, Location::RequiresRegister());
2816 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2817}
2818
2819void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2820 LocationSummary* locations = instruction->GetLocations();
2821 Register cls = locations->InAt(0).As<Register>();
2822 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2823
2824 switch (instruction->GetType()) {
2825 case Primitive::kPrimBoolean: {
2826 Register out = locations->Out().As<Register>();
2827 __ movzxb(out, Address(cls, offset));
2828 break;
2829 }
2830
2831 case Primitive::kPrimByte: {
2832 Register out = locations->Out().As<Register>();
2833 __ movsxb(out, Address(cls, offset));
2834 break;
2835 }
2836
2837 case Primitive::kPrimShort: {
2838 Register out = locations->Out().As<Register>();
2839 __ movsxw(out, Address(cls, offset));
2840 break;
2841 }
2842
2843 case Primitive::kPrimChar: {
2844 Register out = locations->Out().As<Register>();
2845 __ movzxw(out, Address(cls, offset));
2846 break;
2847 }
2848
2849 case Primitive::kPrimInt:
2850 case Primitive::kPrimNot: {
2851 Register out = locations->Out().As<Register>();
2852 __ movl(out, Address(cls, offset));
2853 break;
2854 }
2855
2856 case Primitive::kPrimLong: {
2857 // TODO: support volatile.
2858 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(cls, offset));
2859 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(cls, kX86WordSize + offset));
2860 break;
2861 }
2862
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002863 case Primitive::kPrimFloat: {
2864 XmmRegister out = locations->Out().As<XmmRegister>();
2865 __ movss(out, Address(cls, offset));
2866 break;
2867 }
2868
2869 case Primitive::kPrimDouble: {
2870 XmmRegister out = locations->Out().As<XmmRegister>();
2871 __ movsd(out, Address(cls, offset));
2872 break;
2873 }
2874
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002875 case Primitive::kPrimVoid:
2876 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2877 UNREACHABLE();
2878 }
2879}
2880
2881void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2882 LocationSummary* locations =
2883 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2884 locations->SetInAt(0, Location::RequiresRegister());
2885 Primitive::Type field_type = instruction->GetFieldType();
2886 bool is_object_type = field_type == Primitive::kPrimNot;
2887 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2888 || (field_type == Primitive::kPrimByte);
2889 // The register allocator does not support multiple
2890 // inputs that die at entry with one in a specific register.
2891 if (is_byte_type) {
2892 // Ensure the value is in a byte register.
2893 locations->SetInAt(1, Location::RegisterLocation(EAX));
2894 } else {
2895 locations->SetInAt(1, Location::RequiresRegister());
2896 }
2897 // Temporary registers for the write barrier.
2898 if (is_object_type) {
2899 locations->AddTemp(Location::RequiresRegister());
2900 // Ensure the card is in a byte register.
2901 locations->AddTemp(Location::RegisterLocation(ECX));
2902 }
2903}
2904
2905void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2906 LocationSummary* locations = instruction->GetLocations();
2907 Register cls = locations->InAt(0).As<Register>();
2908 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2909 Primitive::Type field_type = instruction->GetFieldType();
2910
2911 switch (field_type) {
2912 case Primitive::kPrimBoolean:
2913 case Primitive::kPrimByte: {
2914 ByteRegister value = locations->InAt(1).As<ByteRegister>();
2915 __ movb(Address(cls, offset), value);
2916 break;
2917 }
2918
2919 case Primitive::kPrimShort:
2920 case Primitive::kPrimChar: {
2921 Register value = locations->InAt(1).As<Register>();
2922 __ movw(Address(cls, offset), value);
2923 break;
2924 }
2925
2926 case Primitive::kPrimInt:
2927 case Primitive::kPrimNot: {
2928 Register value = locations->InAt(1).As<Register>();
2929 __ movl(Address(cls, offset), value);
2930
2931 if (field_type == Primitive::kPrimNot) {
2932 Register temp = locations->GetTemp(0).As<Register>();
2933 Register card = locations->GetTemp(1).As<Register>();
2934 codegen_->MarkGCCard(temp, card, cls, value);
2935 }
2936 break;
2937 }
2938
2939 case Primitive::kPrimLong: {
2940 Location value = locations->InAt(1);
2941 __ movl(Address(cls, offset), value.AsRegisterPairLow<Register>());
2942 __ movl(Address(cls, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
2943 break;
2944 }
2945
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002946 case Primitive::kPrimFloat: {
2947 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2948 __ movss(Address(cls, offset), value);
2949 break;
2950 }
2951
2952 case Primitive::kPrimDouble: {
2953 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2954 __ movsd(Address(cls, offset), value);
2955 break;
2956 }
2957
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002958 case Primitive::kPrimVoid:
2959 LOG(FATAL) << "Unreachable type " << field_type;
2960 UNREACHABLE();
2961 }
2962}
2963
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002964void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
2965 LocationSummary* locations =
2966 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2967 locations->SetOut(Location::RequiresRegister());
2968}
2969
2970void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
2971 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
2972 codegen_->AddSlowPath(slow_path);
2973
2974 Register out = load->GetLocations()->Out().As<Register>();
2975 codegen_->LoadCurrentMethod(out);
2976 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
2977 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2978 __ testl(out, out);
2979 __ j(kEqual, slow_path->GetEntryLabel());
2980 __ Bind(slow_path->GetExitLabel());
2981}
2982
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002983void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
2984 LocationSummary* locations =
2985 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2986 locations->SetOut(Location::RequiresRegister());
2987}
2988
2989void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
2990 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
2991 __ fs()->movl(load->GetLocations()->Out().As<Register>(), address);
2992 __ fs()->movl(address, Immediate(0));
2993}
2994
2995void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
2996 LocationSummary* locations =
2997 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2998 InvokeRuntimeCallingConvention calling_convention;
2999 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3000}
3001
3002void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
3003 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
3004 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3005}
3006
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003007void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003008 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3009 ? LocationSummary::kNoCall
3010 : LocationSummary::kCallOnSlowPath;
3011 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3012 locations->SetInAt(0, Location::RequiresRegister());
3013 locations->SetInAt(1, Location::Any());
3014 locations->SetOut(Location::RequiresRegister());
3015}
3016
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003017void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003018 LocationSummary* locations = instruction->GetLocations();
3019 Register obj = locations->InAt(0).As<Register>();
3020 Location cls = locations->InAt(1);
3021 Register out = locations->Out().As<Register>();
3022 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3023 Label done, zero;
3024 SlowPathCodeX86* slow_path = nullptr;
3025
3026 // Return 0 if `obj` is null.
3027 // TODO: avoid this check if we know obj is not null.
3028 __ testl(obj, obj);
3029 __ j(kEqual, &zero);
3030 __ movl(out, Address(obj, class_offset));
3031 // Compare the class of `obj` with `cls`.
3032 if (cls.IsRegister()) {
3033 __ cmpl(out, cls.As<Register>());
3034 } else {
3035 DCHECK(cls.IsStackSlot()) << cls;
3036 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
3037 }
3038
3039 if (instruction->IsClassFinal()) {
3040 // Classes must be equal for the instanceof to succeed.
3041 __ j(kNotEqual, &zero);
3042 __ movl(out, Immediate(1));
3043 __ jmp(&done);
3044 } else {
3045 // If the classes are not equal, we go into a slow path.
3046 DCHECK(locations->OnlyCallsOnSlowPath());
3047 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003048 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003049 codegen_->AddSlowPath(slow_path);
3050 __ j(kNotEqual, slow_path->GetEntryLabel());
3051 __ movl(out, Immediate(1));
3052 __ jmp(&done);
3053 }
3054 __ Bind(&zero);
3055 __ movl(out, Immediate(0));
3056 if (slow_path != nullptr) {
3057 __ Bind(slow_path->GetExitLabel());
3058 }
3059 __ Bind(&done);
3060}
3061
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003062void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
3063 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3064 instruction, LocationSummary::kCallOnSlowPath);
3065 locations->SetInAt(0, Location::RequiresRegister());
3066 locations->SetInAt(1, Location::Any());
3067 locations->AddTemp(Location::RequiresRegister());
3068}
3069
3070void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
3071 LocationSummary* locations = instruction->GetLocations();
3072 Register obj = locations->InAt(0).As<Register>();
3073 Location cls = locations->InAt(1);
3074 Register temp = locations->GetTemp(0).As<Register>();
3075 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3076 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
3077 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3078 codegen_->AddSlowPath(slow_path);
3079
3080 // TODO: avoid this check if we know obj is not null.
3081 __ testl(obj, obj);
3082 __ j(kEqual, slow_path->GetExitLabel());
3083 __ movl(temp, Address(obj, class_offset));
3084
3085 // Compare the class of `obj` with `cls`.
3086 if (cls.IsRegister()) {
3087 __ cmpl(temp, cls.As<Register>());
3088 } else {
3089 DCHECK(cls.IsStackSlot()) << cls;
3090 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
3091 }
3092
3093 __ j(kNotEqual, slow_path->GetEntryLabel());
3094 __ Bind(slow_path->GetExitLabel());
3095}
3096
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003097void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3098 LocationSummary* locations =
3099 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3100 InvokeRuntimeCallingConvention calling_convention;
3101 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3102}
3103
3104void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3105 __ fs()->call(Address::Absolute(instruction->IsEnter()
3106 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
3107 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
3108 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3109}
3110
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003111void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3112void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3113void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3114
3115void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3116 LocationSummary* locations =
3117 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3118 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3119 || instruction->GetResultType() == Primitive::kPrimLong);
3120 locations->SetInAt(0, Location::RequiresRegister());
3121 locations->SetInAt(1, Location::Any());
3122 locations->SetOut(Location::SameAsFirstInput());
3123}
3124
3125void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
3126 HandleBitwiseOperation(instruction);
3127}
3128
3129void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
3130 HandleBitwiseOperation(instruction);
3131}
3132
3133void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
3134 HandleBitwiseOperation(instruction);
3135}
3136
3137void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3138 LocationSummary* locations = instruction->GetLocations();
3139 Location first = locations->InAt(0);
3140 Location second = locations->InAt(1);
3141 DCHECK(first.Equals(locations->Out()));
3142
3143 if (instruction->GetResultType() == Primitive::kPrimInt) {
3144 if (second.IsRegister()) {
3145 if (instruction->IsAnd()) {
3146 __ andl(first.As<Register>(), second.As<Register>());
3147 } else if (instruction->IsOr()) {
3148 __ orl(first.As<Register>(), second.As<Register>());
3149 } else {
3150 DCHECK(instruction->IsXor());
3151 __ xorl(first.As<Register>(), second.As<Register>());
3152 }
3153 } else if (second.IsConstant()) {
3154 if (instruction->IsAnd()) {
3155 __ andl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3156 } else if (instruction->IsOr()) {
3157 __ orl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3158 } else {
3159 DCHECK(instruction->IsXor());
3160 __ xorl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3161 }
3162 } else {
3163 if (instruction->IsAnd()) {
3164 __ andl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
3165 } else if (instruction->IsOr()) {
3166 __ orl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
3167 } else {
3168 DCHECK(instruction->IsXor());
3169 __ xorl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
3170 }
3171 }
3172 } else {
3173 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3174 if (second.IsRegisterPair()) {
3175 if (instruction->IsAnd()) {
3176 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3177 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3178 } else if (instruction->IsOr()) {
3179 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3180 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3181 } else {
3182 DCHECK(instruction->IsXor());
3183 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3184 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3185 }
3186 } else {
3187 if (instruction->IsAnd()) {
3188 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3189 __ andl(first.AsRegisterPairHigh<Register>(),
3190 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3191 } else if (instruction->IsOr()) {
3192 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3193 __ orl(first.AsRegisterPairHigh<Register>(),
3194 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3195 } else {
3196 DCHECK(instruction->IsXor());
3197 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3198 __ xorl(first.AsRegisterPairHigh<Register>(),
3199 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3200 }
3201 }
3202 }
3203}
3204
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003205} // namespace x86
3206} // namespace art