blob: 36fe063109122e6cebf53b85c2e08055e4845c55 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
19#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010020#include "gc/accounting/card_table.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070021#include "mirror/array-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010022#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010023#include "mirror/class.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010024#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000025#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010026#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000027#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010028#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010031
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032namespace x86 {
33
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010034static constexpr bool kExplicitStackOverflowCheck = false;
35
36static constexpr int kNumberOfPushedRegistersAtEntry = 1;
37static constexpr int kCurrentMethodStackOffset = 0;
38
Calin Juravled6fb6cf2014-11-11 19:07:44 +000039static constexpr Register kRuntimeParameterCoreRegisters[] = { EAX, ECX, EDX, EBX };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010040static constexpr size_t kRuntimeParameterCoreRegistersLength =
41 arraysize(kRuntimeParameterCoreRegisters);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010042static constexpr XmmRegister kRuntimeParameterFpuRegisters[] = { };
43static constexpr size_t kRuntimeParameterFpuRegistersLength = 0;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010044
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000045// Marker for places that can be updated once we don't follow the quick ABI.
46static constexpr bool kFollowsQuickABI = true;
47
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010048class InvokeRuntimeCallingConvention : public CallingConvention<Register, XmmRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010049 public:
50 InvokeRuntimeCallingConvention()
51 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010052 kRuntimeParameterCoreRegistersLength,
53 kRuntimeParameterFpuRegisters,
54 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010055
56 private:
57 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
58};
59
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
61
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010062class SlowPathCodeX86 : public SlowPathCode {
63 public:
64 SlowPathCodeX86() : entry_label_(), exit_label_() {}
65
66 Label* GetEntryLabel() { return &entry_label_; }
67 Label* GetExitLabel() { return &exit_label_; }
68
69 private:
70 Label entry_label_;
71 Label exit_label_;
72
73 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86);
74};
75
76class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010077 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010078 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079
80 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
81 __ Bind(GetEntryLabel());
82 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010083 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010084 }
85
86 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010087 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010088 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
89};
90
Calin Juravled0d48522014-11-04 16:40:20 +000091class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
92 public:
93 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
94
95 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
96 __ Bind(GetEntryLabel());
97 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
98 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
99 }
100
101 private:
102 HDivZeroCheck* const instruction_;
103 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
104};
105
Calin Juravlebacfec32014-11-14 15:54:36 +0000106class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
Calin Juravled0d48522014-11-04 16:40:20 +0000107 public:
Calin Juravlebacfec32014-11-14 15:54:36 +0000108 explicit DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000109
110 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
111 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000112 if (is_div_) {
113 __ negl(reg_);
114 } else {
115 __ movl(reg_, Immediate(0));
116 }
Calin Juravled0d48522014-11-04 16:40:20 +0000117 __ jmp(GetExitLabel());
118 }
119
120 private:
121 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000122 bool is_div_;
123 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000124};
125
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100126class StackOverflowCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100127 public:
128 StackOverflowCheckSlowPathX86() {}
129
130 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
131 __ Bind(GetEntryLabel());
132 __ addl(ESP,
133 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
134 __ fs()->jmp(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowStackOverflow)));
135 }
136
137 private:
138 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86);
139};
140
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100141class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100142 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100143 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
144 Location index_location,
145 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100146 : instruction_(instruction), index_location_(index_location), length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100147
148 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100149 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100150 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000151 // We're moving two locations to locations that could overlap, so we need a parallel
152 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100153 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000154 x86_codegen->EmitParallelMoves(
155 index_location_,
156 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
157 length_location_,
158 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100159 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100160 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100161 }
162
163 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100164 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100165 const Location index_location_;
166 const Location length_location_;
167
168 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
169};
170
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100171class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000172 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100173 explicit SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
174 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000175
176 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100177 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000178 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100179 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000180 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
181 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100182 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100183 if (successor_ == nullptr) {
184 __ jmp(GetReturnLabel());
185 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100186 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100187 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000188 }
189
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100190 Label* GetReturnLabel() {
191 DCHECK(successor_ == nullptr);
192 return &return_label_;
193 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000194
195 private:
196 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100197 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000198 Label return_label_;
199
200 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
201};
202
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000203class LoadStringSlowPathX86 : public SlowPathCodeX86 {
204 public:
205 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
206
207 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
208 LocationSummary* locations = instruction_->GetLocations();
209 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
210
211 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
212 __ Bind(GetEntryLabel());
213 codegen->SaveLiveRegisters(locations);
214
215 InvokeRuntimeCallingConvention calling_convention;
216 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(0));
217 __ movl(calling_convention.GetRegisterAt(1), Immediate(instruction_->GetStringIndex()));
218 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
219 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
220 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
221 codegen->RestoreLiveRegisters(locations);
222
223 __ jmp(GetExitLabel());
224 }
225
226 private:
227 HLoadString* const instruction_;
228
229 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
230};
231
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000232class LoadClassSlowPathX86 : public SlowPathCodeX86 {
233 public:
234 LoadClassSlowPathX86(HLoadClass* cls,
235 HInstruction* at,
236 uint32_t dex_pc,
237 bool do_clinit)
238 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
239 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
240 }
241
242 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
243 LocationSummary* locations = at_->GetLocations();
244 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
245 __ Bind(GetEntryLabel());
246 codegen->SaveLiveRegisters(locations);
247
248 InvokeRuntimeCallingConvention calling_convention;
249 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
250 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
251 __ fs()->call(Address::Absolute(do_clinit_
252 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
253 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
254 codegen->RecordPcInfo(at_, dex_pc_);
255
256 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000257 Location out = locations->Out();
258 if (out.IsValid()) {
259 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
260 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000261 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000262
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000263 codegen->RestoreLiveRegisters(locations);
264 __ jmp(GetExitLabel());
265 }
266
267 private:
268 // The class this slow path will load.
269 HLoadClass* const cls_;
270
271 // The instruction where this slow path is happening.
272 // (Might be the load class or an initialization check).
273 HInstruction* const at_;
274
275 // The dex PC of `at_`.
276 const uint32_t dex_pc_;
277
278 // Whether to initialize the class.
279 const bool do_clinit_;
280
281 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
282};
283
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000284class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
285 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000286 TypeCheckSlowPathX86(HInstruction* instruction,
287 Location class_to_check,
288 Location object_class,
289 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000290 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000291 class_to_check_(class_to_check),
292 object_class_(object_class),
293 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000294
295 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
296 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000297 DCHECK(instruction_->IsCheckCast()
298 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000299
300 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
301 __ Bind(GetEntryLabel());
302 codegen->SaveLiveRegisters(locations);
303
304 // We're moving two locations to locations that could overlap, so we need a parallel
305 // move resolver.
306 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000307 x86_codegen->EmitParallelMoves(
308 class_to_check_,
309 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
310 object_class_,
311 Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000312
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000313 if (instruction_->IsInstanceOf()) {
314 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInstanceofNonTrivial)));
315 } else {
316 DCHECK(instruction_->IsCheckCast());
317 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
318 }
319
320 codegen->RecordPcInfo(instruction_, dex_pc_);
321 if (instruction_->IsInstanceOf()) {
322 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
323 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324 codegen->RestoreLiveRegisters(locations);
325
326 __ jmp(GetExitLabel());
327 }
328
329 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000330 HInstruction* const instruction_;
331 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000332 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000333 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000334
335 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
336};
337
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100338#undef __
339#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
340
Dave Allison20dfc792014-06-16 20:44:29 -0700341inline Condition X86Condition(IfCondition cond) {
342 switch (cond) {
343 case kCondEQ: return kEqual;
344 case kCondNE: return kNotEqual;
345 case kCondLT: return kLess;
346 case kCondLE: return kLessEqual;
347 case kCondGT: return kGreater;
348 case kCondGE: return kGreaterEqual;
349 default:
350 LOG(FATAL) << "Unknown if condition";
351 }
352 return kEqual;
353}
354
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100355void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
356 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
357}
358
359void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
360 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
361}
362
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100363size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
364 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
365 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100366}
367
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100368size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
369 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
370 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100371}
372
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100373CodeGeneratorX86::CodeGeneratorX86(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100374 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfXmmRegisters, kNumberOfRegisterPairs),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100375 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100376 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100377 instruction_visitor_(graph, this),
378 move_resolver_(graph->GetArena(), this) {}
379
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100380size_t CodeGeneratorX86::FrameEntrySpillSize() const {
381 return kNumberOfPushedRegistersAtEntry * kX86WordSize;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100382}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100383
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100384Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100385 switch (type) {
386 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100387 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100388 X86ManagedRegister pair =
389 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100390 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
391 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100392 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
393 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100394 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100395 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100396 }
397
398 case Primitive::kPrimByte:
399 case Primitive::kPrimBoolean:
400 case Primitive::kPrimChar:
401 case Primitive::kPrimShort:
402 case Primitive::kPrimInt:
403 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100404 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100405 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100406 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100407 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
408 X86ManagedRegister current =
409 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
410 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100411 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100412 }
413 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100414 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100415 }
416
417 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100418 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100419 return Location::FpuRegisterLocation(
420 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100421 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100422
423 case Primitive::kPrimVoid:
424 LOG(FATAL) << "Unreachable type " << type;
425 }
426
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100427 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100428}
429
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100430void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100431 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100432 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100433
434 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100435 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100436
437 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +0000438 DCHECK(kFollowsQuickABI);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100439 blocked_core_registers_[EBP] = true;
440 blocked_core_registers_[ESI] = true;
441 blocked_core_registers_[EDI] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100442
443 UpdateBlockedPairRegisters();
444}
445
446void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
447 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
448 X86ManagedRegister current =
449 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
450 if (blocked_core_registers_[current.AsRegisterPairLow()]
451 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
452 blocked_register_pairs_[i] = true;
453 }
454 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100455}
456
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100457InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
458 : HGraphVisitor(graph),
459 assembler_(codegen->GetAssembler()),
460 codegen_(codegen) {}
461
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000462void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000463 // Create a fake register to mimic Quick.
464 static const int kFakeReturnRegister = 8;
465 core_spill_mask_ |= (1 << kFakeReturnRegister);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000466
Dave Allison648d7112014-07-25 16:15:27 -0700467 bool skip_overflow_check = IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100468 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
469 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100470 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100471 }
472
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100473 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100474 __ subl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100475
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100476 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100477 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86();
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100478 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100479
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100480 __ fs()->cmpl(ESP, Address::Absolute(Thread::StackEndOffset<kX86WordSize>()));
481 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100482 }
483
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100484 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000485}
486
487void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100488 __ addl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000489}
490
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100491void CodeGeneratorX86::Bind(HBasicBlock* block) {
492 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000493}
494
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100495void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100496 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000497}
498
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100499Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
500 switch (load->GetType()) {
501 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100502 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100503 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
504 break;
505
506 case Primitive::kPrimInt:
507 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100508 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100509 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100510
511 case Primitive::kPrimBoolean:
512 case Primitive::kPrimByte:
513 case Primitive::kPrimChar:
514 case Primitive::kPrimShort:
515 case Primitive::kPrimVoid:
516 LOG(FATAL) << "Unexpected type " << load->GetType();
517 }
518
519 LOG(FATAL) << "Unreachable";
520 return Location();
521}
522
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100523Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
524 switch (type) {
525 case Primitive::kPrimBoolean:
526 case Primitive::kPrimByte:
527 case Primitive::kPrimChar:
528 case Primitive::kPrimShort:
529 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100530 case Primitive::kPrimFloat:
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100531 case Primitive::kPrimNot: {
532 uint32_t index = gp_index_++;
533 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100534 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100535 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100536 return Location::StackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100537 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100538 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100539
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100540 case Primitive::kPrimLong:
541 case Primitive::kPrimDouble: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100542 uint32_t index = gp_index_;
543 gp_index_ += 2;
544 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100545 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
546 calling_convention.GetRegisterPairAt(index));
547 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100548 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000549 // On X86, the register index and stack index of a quick parameter is the same, since
550 // we are passing floating pointer values in core registers.
551 return Location::QuickParameter(index, index);
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100552 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100553 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100554 }
555 }
556
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100557 case Primitive::kPrimVoid:
558 LOG(FATAL) << "Unexpected parameter type " << type;
559 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100560 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100561 return Location();
562}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100563
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100564void CodeGeneratorX86::Move32(Location destination, Location source) {
565 if (source.Equals(destination)) {
566 return;
567 }
568 if (destination.IsRegister()) {
569 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100570 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100571 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100572 __ movd(destination.As<Register>(), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100573 } else {
574 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100575 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100576 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100577 } else if (destination.IsFpuRegister()) {
578 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100579 __ movd(destination.As<XmmRegister>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100580 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100581 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100582 } else {
583 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100584 __ movss(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100585 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100586 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000587 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100588 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100589 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100590 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100591 __ movss(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100592 } else {
593 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100594 __ pushl(Address(ESP, source.GetStackIndex()));
595 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100596 }
597 }
598}
599
600void CodeGeneratorX86::Move64(Location destination, Location source) {
601 if (source.Equals(destination)) {
602 return;
603 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100604 if (destination.IsRegisterPair()) {
605 if (source.IsRegisterPair()) {
606 __ movl(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
607 __ movl(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100608 } else if (source.IsFpuRegister()) {
609 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100610 } else if (source.IsQuickParameter()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000611 uint16_t register_index = source.GetQuickParameterRegisterIndex();
612 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100613 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100614 __ movl(destination.AsRegisterPairLow<Register>(),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000615 calling_convention.GetRegisterAt(register_index));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100616 __ movl(destination.AsRegisterPairHigh<Register>(), Address(ESP,
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000617 calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100618 } else {
619 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100620 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
621 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100622 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
623 }
624 } else if (destination.IsQuickParameter()) {
625 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000626 uint16_t register_index = destination.GetQuickParameterRegisterIndex();
627 uint16_t stack_index = destination.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100628 if (source.IsRegister()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000629 __ movl(calling_convention.GetRegisterAt(register_index), source.AsRegisterPairLow<Register>());
630 __ movl(Address(ESP, calling_convention.GetStackOffsetOf(stack_index + 1)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100631 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100632 } else if (source.IsFpuRegister()) {
633 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100634 } else {
635 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000636 __ movl(calling_convention.GetRegisterAt(register_index),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100637 Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100638 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000639 __ popl(Address(ESP, calling_convention.GetStackOffsetOf(stack_index + 1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100640 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100641 } else if (destination.IsFpuRegister()) {
642 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100643 __ movsd(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100644 } else {
645 LOG(FATAL) << "Unimplemented";
646 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100647 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000648 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100649 if (source.IsRegisterPair()) {
650 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100651 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100652 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100653 } else if (source.IsQuickParameter()) {
654 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000655 uint16_t register_index = source.GetQuickParameterRegisterIndex();
656 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100657 __ movl(Address(ESP, destination.GetStackIndex()),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000658 calling_convention.GetRegisterAt(register_index));
659 DCHECK_EQ(calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100660 static_cast<size_t>(destination.GetHighStackIndex(kX86WordSize)));
661 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100662 __ movsd(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100663 } else {
664 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100665 __ pushl(Address(ESP, source.GetStackIndex()));
666 __ popl(Address(ESP, destination.GetStackIndex()));
667 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
668 __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100669 }
670 }
671}
672
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100673void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000674 LocationSummary* locations = instruction->GetLocations();
675 if (locations != nullptr && locations->Out().Equals(location)) {
676 return;
677 }
678
679 if (locations != nullptr && locations->Out().IsConstant()) {
680 HConstant* const_to_move = locations->Out().GetConstant();
681 if (const_to_move->IsIntConstant()) {
682 Immediate imm(const_to_move->AsIntConstant()->GetValue());
683 if (location.IsRegister()) {
684 __ movl(location.As<Register>(), imm);
685 } else if (location.IsStackSlot()) {
686 __ movl(Address(ESP, location.GetStackIndex()), imm);
687 } else {
688 DCHECK(location.IsConstant());
689 DCHECK_EQ(location.GetConstant(), const_to_move);
690 }
691 } else if (const_to_move->IsLongConstant()) {
692 int64_t value = const_to_move->AsLongConstant()->GetValue();
693 if (location.IsRegisterPair()) {
694 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
695 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
696 } else if (location.IsDoubleStackSlot()) {
697 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
698 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
699 } else {
700 DCHECK(location.IsConstant());
701 DCHECK_EQ(location.GetConstant(), instruction);
702 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100703 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000704 } else if (instruction->IsTemporary()) {
705 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000706 if (temp_location.IsStackSlot()) {
707 Move32(location, temp_location);
708 } else {
709 DCHECK(temp_location.IsDoubleStackSlot());
710 Move64(location, temp_location);
711 }
Roland Levillain476df552014-10-09 17:51:36 +0100712 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100713 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100714 switch (instruction->GetType()) {
715 case Primitive::kPrimBoolean:
716 case Primitive::kPrimByte:
717 case Primitive::kPrimChar:
718 case Primitive::kPrimShort:
719 case Primitive::kPrimInt:
720 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100721 case Primitive::kPrimFloat:
722 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100723 break;
724
725 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100726 case Primitive::kPrimDouble:
727 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100728 break;
729
730 default:
731 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
732 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000733 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100734 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100735 switch (instruction->GetType()) {
736 case Primitive::kPrimBoolean:
737 case Primitive::kPrimByte:
738 case Primitive::kPrimChar:
739 case Primitive::kPrimShort:
740 case Primitive::kPrimInt:
741 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100742 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000743 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100744 break;
745
746 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100747 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000748 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100749 break;
750
751 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100752 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100753 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000754 }
755}
756
757void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000758 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000759}
760
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000761void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000762 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100763 DCHECK(!successor->IsExitBlock());
764
765 HBasicBlock* block = got->GetBlock();
766 HInstruction* previous = got->GetPrevious();
767
768 HLoopInformation* info = block->GetLoopInformation();
769 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
770 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
771 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
772 return;
773 }
774
775 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
776 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
777 }
778 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000779 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000780 }
781}
782
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000783void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000784 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000785}
786
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000787void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700788 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000789 if (kIsDebugBuild) {
790 __ Comment("Unreachable");
791 __ int3();
792 }
793}
794
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000795void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100796 LocationSummary* locations =
797 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100798 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100799 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100800 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100801 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000802}
803
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000804void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700805 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100806 if (cond->IsIntConstant()) {
807 // Constant condition, statically compared against 1.
808 int32_t cond_value = cond->AsIntConstant()->GetValue();
809 if (cond_value == 1) {
810 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
811 if_instr->IfTrueSuccessor())) {
812 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100813 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100814 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100815 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100816 DCHECK_EQ(cond_value, 0);
817 }
818 } else {
819 bool materialized =
820 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
821 // Moves do not affect the eflags register, so if the condition is
822 // evaluated just before the if, we don't need to evaluate it
823 // again.
824 bool eflags_set = cond->IsCondition()
825 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
826 if (materialized) {
827 if (!eflags_set) {
828 // Materialized condition, compare against 0.
829 Location lhs = if_instr->GetLocations()->InAt(0);
830 if (lhs.IsRegister()) {
831 __ cmpl(lhs.As<Register>(), Immediate(0));
832 } else {
833 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
834 }
835 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
836 } else {
837 __ j(X86Condition(cond->AsCondition()->GetCondition()),
838 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
839 }
840 } else {
841 Location lhs = cond->GetLocations()->InAt(0);
842 Location rhs = cond->GetLocations()->InAt(1);
843 // LHS is guaranteed to be in a register (see
844 // LocationsBuilderX86::VisitCondition).
845 if (rhs.IsRegister()) {
846 __ cmpl(lhs.As<Register>(), rhs.As<Register>());
847 } else if (rhs.IsConstant()) {
848 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
849 Immediate imm(instruction->AsIntConstant()->GetValue());
850 __ cmpl(lhs.As<Register>(), imm);
851 } else {
852 __ cmpl(lhs.As<Register>(), Address(ESP, rhs.GetStackIndex()));
853 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100854 __ j(X86Condition(cond->AsCondition()->GetCondition()),
855 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700856 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100857 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100858 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
859 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700860 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000861 }
862}
863
864void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000865 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000866}
867
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000868void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
869 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000870}
871
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000872void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100873 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000874}
875
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000876void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100877 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700878 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000879}
880
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100881void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100882 LocationSummary* locations =
883 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100884 switch (store->InputAt(1)->GetType()) {
885 case Primitive::kPrimBoolean:
886 case Primitive::kPrimByte:
887 case Primitive::kPrimChar:
888 case Primitive::kPrimShort:
889 case Primitive::kPrimInt:
890 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100891 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100892 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
893 break;
894
895 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100896 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100897 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
898 break;
899
900 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100901 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100902 }
903 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000904}
905
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000906void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700907 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000908}
909
Dave Allison20dfc792014-06-16 20:44:29 -0700910void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100911 LocationSummary* locations =
912 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100913 locations->SetInAt(0, Location::RequiresRegister());
914 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100915 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100916 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100917 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000918}
919
Dave Allison20dfc792014-06-16 20:44:29 -0700920void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
921 if (comp->NeedsMaterialization()) {
922 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100923 Register reg = locations->Out().As<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100924 // Clear register: setcc only sets the low byte.
925 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700926 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100927 __ cmpl(locations->InAt(0).As<Register>(),
928 locations->InAt(1).As<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100929 } else if (locations->InAt(1).IsConstant()) {
930 HConstant* instruction = locations->InAt(1).GetConstant();
931 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100932 __ cmpl(locations->InAt(0).As<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700933 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100934 __ cmpl(locations->InAt(0).As<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700935 Address(ESP, locations->InAt(1).GetStackIndex()));
936 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100937 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100938 }
Dave Allison20dfc792014-06-16 20:44:29 -0700939}
940
941void LocationsBuilderX86::VisitEqual(HEqual* comp) {
942 VisitCondition(comp);
943}
944
945void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
946 VisitCondition(comp);
947}
948
949void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
950 VisitCondition(comp);
951}
952
953void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
954 VisitCondition(comp);
955}
956
957void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
958 VisitCondition(comp);
959}
960
961void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
962 VisitCondition(comp);
963}
964
965void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
966 VisitCondition(comp);
967}
968
969void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
970 VisitCondition(comp);
971}
972
973void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
974 VisitCondition(comp);
975}
976
977void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
978 VisitCondition(comp);
979}
980
981void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
982 VisitCondition(comp);
983}
984
985void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
986 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000987}
988
989void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100990 LocationSummary* locations =
991 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100992 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000993}
994
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000995void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100996 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700997 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000998}
999
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001000void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001001 LocationSummary* locations =
1002 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001003 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001004}
1005
1006void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1007 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001008 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001009}
1010
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001011void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1012 LocationSummary* locations =
1013 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1014 locations->SetOut(Location::ConstantLocation(constant));
1015}
1016
1017void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1018 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001019 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001020}
1021
1022void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1023 LocationSummary* locations =
1024 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1025 locations->SetOut(Location::ConstantLocation(constant));
1026}
1027
1028void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1029 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001030 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001031}
1032
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001033void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001034 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001035}
1036
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001037void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001038 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001039 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001040 __ ret();
1041}
1042
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001043void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001044 LocationSummary* locations =
1045 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001046 switch (ret->InputAt(0)->GetType()) {
1047 case Primitive::kPrimBoolean:
1048 case Primitive::kPrimByte:
1049 case Primitive::kPrimChar:
1050 case Primitive::kPrimShort:
1051 case Primitive::kPrimInt:
1052 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001053 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001054 break;
1055
1056 case Primitive::kPrimLong:
1057 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001058 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001059 break;
1060
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001061 case Primitive::kPrimFloat:
1062 case Primitive::kPrimDouble:
1063 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001064 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001065 break;
1066
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001067 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001068 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001069 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001070}
1071
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001072void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001073 if (kIsDebugBuild) {
1074 switch (ret->InputAt(0)->GetType()) {
1075 case Primitive::kPrimBoolean:
1076 case Primitive::kPrimByte:
1077 case Primitive::kPrimChar:
1078 case Primitive::kPrimShort:
1079 case Primitive::kPrimInt:
1080 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001081 DCHECK_EQ(ret->GetLocations()->InAt(0).As<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001082 break;
1083
1084 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001085 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1086 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001087 break;
1088
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001089 case Primitive::kPrimFloat:
1090 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001091 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001092 break;
1093
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001094 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001095 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001096 }
1097 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001098 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001099 __ ret();
1100}
1101
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001102void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001103 HandleInvoke(invoke);
1104}
1105
1106void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001107 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001108
1109 // TODO: Implement all kinds of calls:
1110 // 1) boot -> boot
1111 // 2) app -> boot
1112 // 3) app -> app
1113 //
1114 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1115
1116 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001117 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001118 // temp = temp->dex_cache_resolved_methods_;
1119 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
1120 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001121 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001122 // (temp + offset_of_quick_compiled_code)()
1123 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1124
1125 DCHECK(!codegen_->IsLeafMethod());
1126 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1127}
1128
1129void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1130 HandleInvoke(invoke);
1131}
1132
1133void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001134 LocationSummary* locations =
1135 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001136 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001137
1138 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001139 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001140 HInstruction* input = invoke->InputAt(i);
1141 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1142 }
1143
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001144 switch (invoke->GetType()) {
1145 case Primitive::kPrimBoolean:
1146 case Primitive::kPrimByte:
1147 case Primitive::kPrimChar:
1148 case Primitive::kPrimShort:
1149 case Primitive::kPrimInt:
1150 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001151 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001152 break;
1153
1154 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001155 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001156 break;
1157
1158 case Primitive::kPrimVoid:
1159 break;
1160
1161 case Primitive::kPrimDouble:
1162 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001163 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001164 break;
1165 }
1166
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001167 invoke->SetLocations(locations);
1168}
1169
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001170void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001171 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001172 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1173 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1174 LocationSummary* locations = invoke->GetLocations();
1175 Location receiver = locations->InAt(0);
1176 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1177 // temp = object->GetClass();
1178 if (receiver.IsStackSlot()) {
1179 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1180 __ movl(temp, Address(temp, class_offset));
1181 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001182 __ movl(temp, Address(receiver.As<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001183 }
1184 // temp = temp->GetMethodAt(method_offset);
1185 __ movl(temp, Address(temp, method_offset));
1186 // call temp->GetEntryPoint();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001187 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1188
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001189 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001190 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001191}
1192
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001193void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1194 HandleInvoke(invoke);
1195 // Add the hidden argument.
1196 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM0));
1197}
1198
1199void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1200 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1201 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
1202 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1203 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1204 LocationSummary* locations = invoke->GetLocations();
1205 Location receiver = locations->InAt(0);
1206 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1207
1208 // Set the hidden argument.
1209 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
1210 __ movd(invoke->GetLocations()->GetTemp(1).As<XmmRegister>(), temp);
1211
1212 // temp = object->GetClass();
1213 if (receiver.IsStackSlot()) {
1214 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1215 __ movl(temp, Address(temp, class_offset));
1216 } else {
1217 __ movl(temp, Address(receiver.As<Register>(), class_offset));
1218 }
1219 // temp = temp->GetImtEntryAt(method_offset);
1220 __ movl(temp, Address(temp, method_offset));
1221 // call temp->GetEntryPoint();
1222 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1223
1224 DCHECK(!codegen_->IsLeafMethod());
1225 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1226}
1227
Roland Levillain88cb1752014-10-20 16:36:47 +01001228void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1229 LocationSummary* locations =
1230 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1231 switch (neg->GetResultType()) {
1232 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001233 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001234 locations->SetInAt(0, Location::RequiresRegister());
1235 locations->SetOut(Location::SameAsFirstInput());
1236 break;
1237
Roland Levillain88cb1752014-10-20 16:36:47 +01001238 case Primitive::kPrimFloat:
1239 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001240 locations->SetInAt(0, Location::RequiresFpuRegister());
1241 // Output overlaps as we need a fresh (zero-initialized)
1242 // register to perform subtraction from zero.
1243 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001244 break;
1245
1246 default:
1247 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1248 }
1249}
1250
1251void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1252 LocationSummary* locations = neg->GetLocations();
1253 Location out = locations->Out();
1254 Location in = locations->InAt(0);
1255 switch (neg->GetResultType()) {
1256 case Primitive::kPrimInt:
1257 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001258 DCHECK(in.Equals(out));
Roland Levillain88cb1752014-10-20 16:36:47 +01001259 __ negl(out.As<Register>());
1260 break;
1261
1262 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001263 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001264 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001265 __ negl(out.AsRegisterPairLow<Register>());
1266 // Negation is similar to subtraction from zero. The least
1267 // significant byte triggers a borrow when it is different from
1268 // zero; to take it into account, add 1 to the most significant
1269 // byte if the carry flag (CF) is set to 1 after the first NEGL
1270 // operation.
1271 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1272 __ negl(out.AsRegisterPairHigh<Register>());
1273 break;
1274
Roland Levillain88cb1752014-10-20 16:36:47 +01001275 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001276 DCHECK(!in.Equals(out));
1277 // out = 0
1278 __ xorps(out.As<XmmRegister>(), out.As<XmmRegister>());
1279 // out = out - in
1280 __ subss(out.As<XmmRegister>(), in.As<XmmRegister>());
1281 break;
1282
Roland Levillain88cb1752014-10-20 16:36:47 +01001283 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001284 DCHECK(!in.Equals(out));
1285 // out = 0
1286 __ xorpd(out.As<XmmRegister>(), out.As<XmmRegister>());
1287 // out = out - in
1288 __ subsd(out.As<XmmRegister>(), in.As<XmmRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001289 break;
1290
1291 default:
1292 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1293 }
1294}
1295
Roland Levillaindff1f282014-11-05 14:15:05 +00001296void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
1297 LocationSummary* locations =
1298 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1299 Primitive::Type result_type = conversion->GetResultType();
1300 Primitive::Type input_type = conversion->GetInputType();
1301 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001302 case Primitive::kPrimByte:
1303 switch (input_type) {
1304 case Primitive::kPrimShort:
1305 case Primitive::kPrimInt:
1306 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001307 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001308 locations->SetInAt(0, Location::Any());
1309 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1310 break;
1311
1312 default:
1313 LOG(FATAL) << "Unexpected type conversion from " << input_type
1314 << " to " << result_type;
1315 }
1316 break;
1317
Roland Levillain01a8d712014-11-14 16:27:39 +00001318 case Primitive::kPrimShort:
1319 switch (input_type) {
1320 case Primitive::kPrimByte:
1321 case Primitive::kPrimInt:
1322 case Primitive::kPrimChar:
1323 // Processing a Dex `int-to-short' instruction.
1324 locations->SetInAt(0, Location::Any());
1325 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1326 break;
1327
1328 default:
1329 LOG(FATAL) << "Unexpected type conversion from " << input_type
1330 << " to " << result_type;
1331 }
1332 break;
1333
Roland Levillain946e1432014-11-11 17:35:19 +00001334 case Primitive::kPrimInt:
1335 switch (input_type) {
1336 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001337 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001338 locations->SetInAt(0, Location::Any());
1339 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1340 break;
1341
1342 case Primitive::kPrimFloat:
1343 case Primitive::kPrimDouble:
1344 LOG(FATAL) << "Type conversion from " << input_type
1345 << " to " << result_type << " not yet implemented";
1346 break;
1347
1348 default:
1349 LOG(FATAL) << "Unexpected type conversion from " << input_type
1350 << " to " << result_type;
1351 }
1352 break;
1353
Roland Levillaindff1f282014-11-05 14:15:05 +00001354 case Primitive::kPrimLong:
1355 switch (input_type) {
1356 case Primitive::kPrimByte:
1357 case Primitive::kPrimShort:
1358 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001359 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001360 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001361 locations->SetInAt(0, Location::RegisterLocation(EAX));
1362 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1363 break;
1364
1365 case Primitive::kPrimFloat:
1366 case Primitive::kPrimDouble:
1367 LOG(FATAL) << "Type conversion from " << input_type << " to "
1368 << result_type << " not yet implemented";
1369 break;
1370
1371 default:
1372 LOG(FATAL) << "Unexpected type conversion from " << input_type
1373 << " to " << result_type;
1374 }
1375 break;
1376
Roland Levillain981e4542014-11-14 11:47:14 +00001377 case Primitive::kPrimChar:
1378 switch (input_type) {
1379 case Primitive::kPrimByte:
1380 case Primitive::kPrimShort:
1381 case Primitive::kPrimInt:
1382 case Primitive::kPrimChar:
1383 // Processing a Dex `int-to-char' instruction.
1384 locations->SetInAt(0, Location::Any());
1385 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1386 break;
1387
1388 default:
1389 LOG(FATAL) << "Unexpected type conversion from " << input_type
1390 << " to " << result_type;
1391 }
1392 break;
1393
Roland Levillaindff1f282014-11-05 14:15:05 +00001394 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001395 switch (input_type) {
1396 case Primitive::kPrimByte:
1397 case Primitive::kPrimShort:
1398 case Primitive::kPrimInt:
1399 case Primitive::kPrimChar:
1400 // Processing a Dex `int-to-float' instruction.
1401 locations->SetInAt(0, Location::RequiresRegister());
1402 locations->SetOut(Location::RequiresFpuRegister());
1403 break;
1404
1405 case Primitive::kPrimLong:
1406 case Primitive::kPrimDouble:
1407 LOG(FATAL) << "Type conversion from " << input_type
1408 << " to " << result_type << " not yet implemented";
1409 break;
1410
1411 default:
1412 LOG(FATAL) << "Unexpected type conversion from " << input_type
1413 << " to " << result_type;
1414 };
1415 break;
1416
Roland Levillaindff1f282014-11-05 14:15:05 +00001417 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001418 switch (input_type) {
1419 case Primitive::kPrimByte:
1420 case Primitive::kPrimShort:
1421 case Primitive::kPrimInt:
1422 case Primitive::kPrimChar:
1423 // Processing a Dex `int-to-double' instruction.
1424 locations->SetInAt(0, Location::RequiresRegister());
1425 locations->SetOut(Location::RequiresFpuRegister());
1426 break;
1427
1428 case Primitive::kPrimLong:
1429 case Primitive::kPrimFloat:
1430 LOG(FATAL) << "Type conversion from " << input_type
1431 << " to " << result_type << " not yet implemented";
1432 break;
1433
1434 default:
1435 LOG(FATAL) << "Unexpected type conversion from " << input_type
1436 << " to " << result_type;
1437 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001438 break;
1439
1440 default:
1441 LOG(FATAL) << "Unexpected type conversion from " << input_type
1442 << " to " << result_type;
1443 }
1444}
1445
1446void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1447 LocationSummary* locations = conversion->GetLocations();
1448 Location out = locations->Out();
1449 Location in = locations->InAt(0);
1450 Primitive::Type result_type = conversion->GetResultType();
1451 Primitive::Type input_type = conversion->GetInputType();
1452 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001453 case Primitive::kPrimByte:
1454 switch (input_type) {
1455 case Primitive::kPrimShort:
1456 case Primitive::kPrimInt:
1457 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001458 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001459 if (in.IsRegister()) {
1460 __ movsxb(out.As<Register>(), in.As<ByteRegister>());
1461 } else if (in.IsStackSlot()) {
1462 __ movsxb(out.As<Register>(), Address(ESP, in.GetStackIndex()));
1463 } else {
1464 DCHECK(in.GetConstant()->IsIntConstant());
1465 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1466 __ movl(out.As<Register>(), Immediate(static_cast<int8_t>(value)));
1467 }
1468 break;
1469
1470 default:
1471 LOG(FATAL) << "Unexpected type conversion from " << input_type
1472 << " to " << result_type;
1473 }
1474 break;
1475
Roland Levillain01a8d712014-11-14 16:27:39 +00001476 case Primitive::kPrimShort:
1477 switch (input_type) {
1478 case Primitive::kPrimByte:
1479 case Primitive::kPrimInt:
1480 case Primitive::kPrimChar:
1481 // Processing a Dex `int-to-short' instruction.
1482 if (in.IsRegister()) {
1483 __ movsxw(out.As<Register>(), in.As<Register>());
1484 } else if (in.IsStackSlot()) {
1485 __ movsxw(out.As<Register>(), Address(ESP, in.GetStackIndex()));
1486 } else {
1487 DCHECK(in.GetConstant()->IsIntConstant());
1488 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1489 __ movl(out.As<Register>(), Immediate(static_cast<int16_t>(value)));
1490 }
1491 break;
1492
1493 default:
1494 LOG(FATAL) << "Unexpected type conversion from " << input_type
1495 << " to " << result_type;
1496 }
1497 break;
1498
Roland Levillain946e1432014-11-11 17:35:19 +00001499 case Primitive::kPrimInt:
1500 switch (input_type) {
1501 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001502 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001503 if (in.IsRegisterPair()) {
1504 __ movl(out.As<Register>(), in.AsRegisterPairLow<Register>());
1505 } else if (in.IsDoubleStackSlot()) {
1506 __ movl(out.As<Register>(), Address(ESP, in.GetStackIndex()));
1507 } else {
1508 DCHECK(in.IsConstant());
1509 DCHECK(in.GetConstant()->IsLongConstant());
1510 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
1511 __ movl(out.As<Register>(), Immediate(static_cast<int32_t>(value)));
1512 }
1513 break;
1514
1515 case Primitive::kPrimFloat:
1516 case Primitive::kPrimDouble:
1517 LOG(FATAL) << "Type conversion from " << input_type
1518 << " to " << result_type << " not yet implemented";
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::kPrimLong:
1528 switch (input_type) {
1529 case Primitive::kPrimByte:
1530 case Primitive::kPrimShort:
1531 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001532 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001533 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001534 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1535 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
1536 DCHECK_EQ(in.As<Register>(), EAX);
1537 __ cdq();
1538 break;
1539
1540 case Primitive::kPrimFloat:
1541 case Primitive::kPrimDouble:
1542 LOG(FATAL) << "Type conversion from " << input_type << " to "
1543 << result_type << " not yet implemented";
1544 break;
1545
1546 default:
1547 LOG(FATAL) << "Unexpected type conversion from " << input_type
1548 << " to " << result_type;
1549 }
1550 break;
1551
Roland Levillain981e4542014-11-14 11:47:14 +00001552 case Primitive::kPrimChar:
1553 switch (input_type) {
1554 case Primitive::kPrimByte:
1555 case Primitive::kPrimShort:
1556 case Primitive::kPrimInt:
1557 case Primitive::kPrimChar:
1558 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1559 if (in.IsRegister()) {
1560 __ movzxw(out.As<Register>(), in.As<Register>());
1561 } else if (in.IsStackSlot()) {
1562 __ movzxw(out.As<Register>(), Address(ESP, in.GetStackIndex()));
1563 } else {
1564 DCHECK(in.GetConstant()->IsIntConstant());
1565 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1566 __ movl(out.As<Register>(), Immediate(static_cast<uint16_t>(value)));
1567 }
1568 break;
1569
1570 default:
1571 LOG(FATAL) << "Unexpected type conversion from " << input_type
1572 << " to " << result_type;
1573 }
1574 break;
1575
Roland Levillaindff1f282014-11-05 14:15:05 +00001576 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001577 switch (input_type) {
1578 // Processing a Dex `int-to-float' instruction.
1579 case Primitive::kPrimByte:
1580 case Primitive::kPrimShort:
1581 case Primitive::kPrimInt:
1582 case Primitive::kPrimChar:
1583 __ cvtsi2ss(out.As<XmmRegister>(), in.As<Register>());
1584 break;
1585
1586 case Primitive::kPrimLong:
1587 case Primitive::kPrimDouble:
1588 LOG(FATAL) << "Type conversion from " << input_type
1589 << " to " << result_type << " not yet implemented";
1590 break;
1591
1592 default:
1593 LOG(FATAL) << "Unexpected type conversion from " << input_type
1594 << " to " << result_type;
1595 };
1596 break;
1597
Roland Levillaindff1f282014-11-05 14:15:05 +00001598 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001599 switch (input_type) {
1600 // Processing a Dex `int-to-double' instruction.
1601 case Primitive::kPrimByte:
1602 case Primitive::kPrimShort:
1603 case Primitive::kPrimInt:
1604 case Primitive::kPrimChar:
1605 __ cvtsi2sd(out.As<XmmRegister>(), in.As<Register>());
1606 break;
1607
1608 case Primitive::kPrimLong:
1609 case Primitive::kPrimFloat:
1610 LOG(FATAL) << "Type conversion from " << input_type
1611 << " to " << result_type << " not yet implemented";
1612 break;
1613
1614 default:
1615 LOG(FATAL) << "Unexpected type conversion from " << input_type
1616 << " to " << result_type;
1617 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001618 break;
1619
1620 default:
1621 LOG(FATAL) << "Unexpected type conversion from " << input_type
1622 << " to " << result_type;
1623 }
1624}
1625
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001626void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001627 LocationSummary* locations =
1628 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001629 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001630 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001631 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001632 locations->SetInAt(0, Location::RequiresRegister());
1633 locations->SetInAt(1, Location::Any());
1634 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001635 break;
1636 }
1637
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001638 case Primitive::kPrimFloat:
1639 case Primitive::kPrimDouble: {
1640 locations->SetInAt(0, Location::RequiresFpuRegister());
1641 locations->SetInAt(1, Location::Any());
1642 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001643 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001644 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001645
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001646 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001647 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1648 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001649 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001650}
1651
1652void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1653 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001654 Location first = locations->InAt(0);
1655 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001656 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001657 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001658 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001659 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001660 __ addl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001661 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001662 __ addl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001663 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001664 __ addl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001665 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001666 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001667 }
1668
1669 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001670 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001671 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1672 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001673 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001674 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1675 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001676 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001677 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001678 break;
1679 }
1680
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001681 case Primitive::kPrimFloat: {
1682 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001683 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001684 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001685 __ addss(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001686 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001687 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001688 }
1689
1690 case Primitive::kPrimDouble: {
1691 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001692 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001693 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001694 __ addsd(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001695 }
1696 break;
1697 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001698
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001699 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001700 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001701 }
1702}
1703
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001704void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001705 LocationSummary* locations =
1706 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001707 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001708 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001709 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001710 locations->SetInAt(0, Location::RequiresRegister());
1711 locations->SetInAt(1, Location::Any());
1712 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001713 break;
1714 }
Calin Juravle11351682014-10-23 15:38:15 +01001715 case Primitive::kPrimFloat:
1716 case Primitive::kPrimDouble: {
1717 locations->SetInAt(0, Location::RequiresFpuRegister());
1718 locations->SetInAt(1, Location::RequiresFpuRegister());
1719 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001720 break;
Calin Juravle11351682014-10-23 15:38:15 +01001721 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001722
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001723 default:
Calin Juravle11351682014-10-23 15:38:15 +01001724 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001725 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001726}
1727
1728void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1729 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001730 Location first = locations->InAt(0);
1731 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001732 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001733 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001734 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001735 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001736 __ subl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001737 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001738 __ subl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001739 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001740 __ subl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001741 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001742 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001743 }
1744
1745 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001746 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01001747 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1748 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001749 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001750 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001751 __ sbbl(first.AsRegisterPairHigh<Register>(),
1752 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001753 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001754 break;
1755 }
1756
Calin Juravle11351682014-10-23 15:38:15 +01001757 case Primitive::kPrimFloat: {
1758 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001759 break;
Calin Juravle11351682014-10-23 15:38:15 +01001760 }
1761
1762 case Primitive::kPrimDouble: {
1763 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1764 break;
1765 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001766
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001767 default:
Calin Juravle11351682014-10-23 15:38:15 +01001768 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001769 }
1770}
1771
Calin Juravle34bacdf2014-10-07 20:23:36 +01001772void LocationsBuilderX86::VisitMul(HMul* mul) {
1773 LocationSummary* locations =
1774 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1775 switch (mul->GetResultType()) {
1776 case Primitive::kPrimInt:
1777 locations->SetInAt(0, Location::RequiresRegister());
1778 locations->SetInAt(1, Location::Any());
1779 locations->SetOut(Location::SameAsFirstInput());
1780 break;
1781 case Primitive::kPrimLong: {
1782 locations->SetInAt(0, Location::RequiresRegister());
1783 // TODO: Currently this handles only stack operands:
1784 // - we don't have enough registers because we currently use Quick ABI.
1785 // - by the time we have a working register allocator we will probably change the ABI
1786 // and fix the above.
1787 // - we don't have a way yet to request operands on stack but the base line compiler
1788 // will leave the operands on the stack with Any().
1789 locations->SetInAt(1, Location::Any());
1790 locations->SetOut(Location::SameAsFirstInput());
1791 // Needed for imul on 32bits with 64bits output.
1792 locations->AddTemp(Location::RegisterLocation(EAX));
1793 locations->AddTemp(Location::RegisterLocation(EDX));
1794 break;
1795 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001796 case Primitive::kPrimFloat:
1797 case Primitive::kPrimDouble: {
1798 locations->SetInAt(0, Location::RequiresFpuRegister());
1799 locations->SetInAt(1, Location::RequiresFpuRegister());
1800 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001801 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001802 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001803
1804 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001805 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001806 }
1807}
1808
1809void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1810 LocationSummary* locations = mul->GetLocations();
1811 Location first = locations->InAt(0);
1812 Location second = locations->InAt(1);
1813 DCHECK(first.Equals(locations->Out()));
1814
1815 switch (mul->GetResultType()) {
1816 case Primitive::kPrimInt: {
1817 if (second.IsRegister()) {
1818 __ imull(first.As<Register>(), second.As<Register>());
1819 } else if (second.IsConstant()) {
1820 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1821 __ imull(first.As<Register>(), imm);
1822 } else {
1823 DCHECK(second.IsStackSlot());
1824 __ imull(first.As<Register>(), Address(ESP, second.GetStackIndex()));
1825 }
1826 break;
1827 }
1828
1829 case Primitive::kPrimLong: {
1830 DCHECK(second.IsDoubleStackSlot());
1831
1832 Register in1_hi = first.AsRegisterPairHigh<Register>();
1833 Register in1_lo = first.AsRegisterPairLow<Register>();
1834 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
1835 Address in2_lo(ESP, second.GetStackIndex());
1836 Register eax = locations->GetTemp(0).As<Register>();
1837 Register edx = locations->GetTemp(1).As<Register>();
1838
1839 DCHECK_EQ(EAX, eax);
1840 DCHECK_EQ(EDX, edx);
1841
1842 // input: in1 - 64 bits, in2 - 64 bits
1843 // output: in1
1844 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
1845 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
1846 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
1847
1848 __ movl(eax, in2_hi);
1849 // eax <- in1.lo * in2.hi
1850 __ imull(eax, in1_lo);
1851 // in1.hi <- in1.hi * in2.lo
1852 __ imull(in1_hi, in2_lo);
1853 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
1854 __ addl(in1_hi, eax);
1855 // move in1_lo to eax to prepare for double precision
1856 __ movl(eax, in1_lo);
1857 // edx:eax <- in1.lo * in2.lo
1858 __ mull(in2_lo);
1859 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
1860 __ addl(in1_hi, edx);
1861 // in1.lo <- (in1.lo * in2.lo)[31:0];
1862 __ movl(in1_lo, eax);
1863
1864 break;
1865 }
1866
Calin Juravleb5bfa962014-10-21 18:02:24 +01001867 case Primitive::kPrimFloat: {
1868 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001869 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001870 }
1871
1872 case Primitive::kPrimDouble: {
1873 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1874 break;
1875 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001876
1877 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001878 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001879 }
1880}
1881
Calin Juravlebacfec32014-11-14 15:54:36 +00001882void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
1883 DCHECK(instruction->IsDiv() || instruction->IsRem());
1884
1885 LocationSummary* locations = instruction->GetLocations();
1886 Location out = locations->Out();
1887 Location first = locations->InAt(0);
1888 Location second = locations->InAt(1);
1889 bool is_div = instruction->IsDiv();
1890
1891 switch (instruction->GetResultType()) {
1892 case Primitive::kPrimInt: {
1893 Register second_reg = second.As<Register>();
1894 DCHECK_EQ(EAX, first.As<Register>());
1895 DCHECK_EQ(is_div ? EAX : EDX, out.As<Register>());
1896
1897 SlowPathCodeX86* slow_path =
1898 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.As<Register>(), is_div);
1899 codegen_->AddSlowPath(slow_path);
1900
1901 // 0x80000000/-1 triggers an arithmetic exception!
1902 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
1903 // it's safe to just use negl instead of more complex comparisons.
1904
1905 __ cmpl(second_reg, Immediate(-1));
1906 __ j(kEqual, slow_path->GetEntryLabel());
1907
1908 // edx:eax <- sign-extended of eax
1909 __ cdq();
1910 // eax = quotient, edx = remainder
1911 __ idivl(second_reg);
1912
1913 __ Bind(slow_path->GetExitLabel());
1914 break;
1915 }
1916
1917 case Primitive::kPrimLong: {
1918 InvokeRuntimeCallingConvention calling_convention;
1919 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
1920 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
1921 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
1922 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
1923 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
1924 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
1925
1926 if (is_div) {
1927 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
1928 } else {
1929 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
1930 }
1931 uint32_t dex_pc = is_div
1932 ? instruction->AsDiv()->GetDexPc()
1933 : instruction->AsRem()->GetDexPc();
1934 codegen_->RecordPcInfo(instruction, dex_pc);
1935
1936 break;
1937 }
1938
1939 default:
1940 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
1941 }
1942}
1943
Calin Juravle7c4954d2014-10-28 16:57:40 +00001944void LocationsBuilderX86::VisitDiv(HDiv* div) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001945 LocationSummary::CallKind call_kind = div->GetResultType() == Primitive::kPrimLong
1946 ? LocationSummary::kCall
1947 : LocationSummary::kNoCall;
1948 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
1949
Calin Juravle7c4954d2014-10-28 16:57:40 +00001950 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001951 case Primitive::kPrimInt: {
1952 locations->SetInAt(0, Location::RegisterLocation(EAX));
1953 locations->SetInAt(1, Location::RequiresRegister());
1954 locations->SetOut(Location::SameAsFirstInput());
1955 // Intel uses edx:eax as the dividend.
1956 locations->AddTemp(Location::RegisterLocation(EDX));
1957 break;
1958 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001959 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001960 InvokeRuntimeCallingConvention calling_convention;
1961 locations->SetInAt(0, Location::RegisterPairLocation(
1962 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
1963 locations->SetInAt(1, Location::RegisterPairLocation(
1964 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
1965 // Runtime helper puts the result in EAX, EDX.
1966 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00001967 break;
1968 }
1969 case Primitive::kPrimFloat:
1970 case Primitive::kPrimDouble: {
1971 locations->SetInAt(0, Location::RequiresFpuRegister());
1972 locations->SetInAt(1, Location::RequiresFpuRegister());
1973 locations->SetOut(Location::SameAsFirstInput());
1974 break;
1975 }
1976
1977 default:
1978 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1979 }
1980}
1981
1982void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
1983 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001984 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00001985 Location first = locations->InAt(0);
1986 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001987
1988 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00001989 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00001990 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001991 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001992 break;
1993 }
1994
1995 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001996 DCHECK(first.Equals(out));
Calin Juravle7c4954d2014-10-28 16:57:40 +00001997 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1998 break;
1999 }
2000
2001 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002002 DCHECK(first.Equals(out));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002003 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
2004 break;
2005 }
2006
2007 default:
2008 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2009 }
2010}
2011
Calin Juravlebacfec32014-11-14 15:54:36 +00002012void LocationsBuilderX86::VisitRem(HRem* rem) {
2013 LocationSummary::CallKind call_kind = rem->GetResultType() == Primitive::kPrimLong
2014 ? LocationSummary::kCall
2015 : LocationSummary::kNoCall;
2016 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2017
2018 switch (rem->GetResultType()) {
2019 case Primitive::kPrimInt: {
2020 locations->SetInAt(0, Location::RegisterLocation(EAX));
2021 locations->SetInAt(1, Location::RequiresRegister());
2022 locations->SetOut(Location::RegisterLocation(EDX));
2023 break;
2024 }
2025 case Primitive::kPrimLong: {
2026 InvokeRuntimeCallingConvention calling_convention;
2027 locations->SetInAt(0, Location::RegisterPairLocation(
2028 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2029 locations->SetInAt(1, Location::RegisterPairLocation(
2030 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2031 // Runtime helper puts the result in EAX, EDX.
2032 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2033 break;
2034 }
2035 case Primitive::kPrimFloat:
2036 case Primitive::kPrimDouble: {
2037 LOG(FATAL) << "Unimplemented rem type " << rem->GetResultType();
2038 break;
2039 }
2040
2041 default:
2042 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2043 }
2044}
2045
2046void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2047 Primitive::Type type = rem->GetResultType();
2048 switch (type) {
2049 case Primitive::kPrimInt:
2050 case Primitive::kPrimLong: {
2051 GenerateDivRemIntegral(rem);
2052 break;
2053 }
2054 case Primitive::kPrimFloat:
2055 case Primitive::kPrimDouble: {
2056 LOG(FATAL) << "Unimplemented rem type " << type;
2057 break;
2058 }
2059 default:
2060 LOG(FATAL) << "Unexpected rem type " << type;
2061 }
2062}
2063
Calin Juravled0d48522014-11-04 16:40:20 +00002064void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2065 LocationSummary* locations =
2066 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002067 switch (instruction->GetType()) {
2068 case Primitive::kPrimInt: {
2069 locations->SetInAt(0, Location::Any());
2070 break;
2071 }
2072 case Primitive::kPrimLong: {
2073 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2074 if (!instruction->IsConstant()) {
2075 locations->AddTemp(Location::RequiresRegister());
2076 }
2077 break;
2078 }
2079 default:
2080 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2081 }
Calin Juravled0d48522014-11-04 16:40:20 +00002082 if (instruction->HasUses()) {
2083 locations->SetOut(Location::SameAsFirstInput());
2084 }
2085}
2086
2087void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2088 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2089 codegen_->AddSlowPath(slow_path);
2090
2091 LocationSummary* locations = instruction->GetLocations();
2092 Location value = locations->InAt(0);
2093
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002094 switch (instruction->GetType()) {
2095 case Primitive::kPrimInt: {
2096 if (value.IsRegister()) {
2097 __ testl(value.As<Register>(), value.As<Register>());
2098 __ j(kEqual, slow_path->GetEntryLabel());
2099 } else if (value.IsStackSlot()) {
2100 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2101 __ j(kEqual, slow_path->GetEntryLabel());
2102 } else {
2103 DCHECK(value.IsConstant()) << value;
2104 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2105 __ jmp(slow_path->GetEntryLabel());
2106 }
2107 }
2108 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002109 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002110 case Primitive::kPrimLong: {
2111 if (value.IsRegisterPair()) {
2112 Register temp = locations->GetTemp(0).As<Register>();
2113 __ movl(temp, value.AsRegisterPairLow<Register>());
2114 __ orl(temp, value.AsRegisterPairHigh<Register>());
2115 __ j(kEqual, slow_path->GetEntryLabel());
2116 } else {
2117 DCHECK(value.IsConstant()) << value;
2118 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2119 __ jmp(slow_path->GetEntryLabel());
2120 }
2121 }
2122 break;
2123 }
2124 default:
2125 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002126 }
Calin Juravled0d48522014-11-04 16:40:20 +00002127}
2128
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002129void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002130 LocationSummary* locations =
2131 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002132 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002133 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002134 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2135 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002136}
2137
2138void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2139 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002140 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002141 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002142
Nicolas Geoffray707c8092014-04-04 10:50:14 +01002143 __ fs()->call(
2144 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002145
Nicolas Geoffray39468442014-09-02 15:17:15 +01002146 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002147 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002148}
2149
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002150void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2151 LocationSummary* locations =
2152 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2153 locations->SetOut(Location::RegisterLocation(EAX));
2154 InvokeRuntimeCallingConvention calling_convention;
2155 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2156 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2157 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2158}
2159
2160void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2161 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002162 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002163 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2164
2165 __ fs()->call(
2166 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
2167
2168 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2169 DCHECK(!codegen_->IsLeafMethod());
2170}
2171
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002172void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002173 LocationSummary* locations =
2174 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002175 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2176 if (location.IsStackSlot()) {
2177 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2178 } else if (location.IsDoubleStackSlot()) {
2179 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002180 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002181 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002182}
2183
2184void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002185 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002186}
2187
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002188void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002189 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002190 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002191 locations->SetInAt(0, Location::RequiresRegister());
2192 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002193}
2194
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002195void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2196 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002197 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002198 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002199 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002200 switch (not_->InputAt(0)->GetType()) {
2201 case Primitive::kPrimBoolean:
2202 __ xorl(out.As<Register>(), Immediate(1));
2203 break;
2204
2205 case Primitive::kPrimInt:
2206 __ notl(out.As<Register>());
2207 break;
2208
2209 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002210 __ notl(out.AsRegisterPairLow<Register>());
2211 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002212 break;
2213
2214 default:
2215 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2216 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002217}
2218
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002219void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002220 LocationSummary* locations =
2221 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002222 locations->SetInAt(0, Location::RequiresRegister());
2223 locations->SetInAt(1, Location::Any());
2224 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002225}
2226
2227void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002228 LocationSummary* locations = compare->GetLocations();
2229 switch (compare->InputAt(0)->GetType()) {
2230 case Primitive::kPrimLong: {
2231 Label less, greater, done;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002232 Register output = locations->Out().As<Register>();
2233 Location left = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002234 Location right = locations->InAt(1);
2235 if (right.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002236 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002237 } else {
2238 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002239 __ cmpl(left.AsRegisterPairHigh<Register>(),
2240 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002241 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002242 __ j(kLess, &less); // Signed compare.
2243 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002244 if (right.IsRegisterPair()) {
2245 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002246 } else {
2247 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002248 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002249 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002250 __ movl(output, Immediate(0));
2251 __ j(kEqual, &done);
2252 __ j(kBelow, &less); // Unsigned compare.
2253
2254 __ Bind(&greater);
2255 __ movl(output, Immediate(1));
2256 __ jmp(&done);
2257
2258 __ Bind(&less);
2259 __ movl(output, Immediate(-1));
2260
2261 __ Bind(&done);
2262 break;
2263 }
2264 default:
2265 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
2266 }
2267}
2268
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002269void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002270 LocationSummary* locations =
2271 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002272 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2273 locations->SetInAt(i, Location::Any());
2274 }
2275 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002276}
2277
2278void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002279 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002280 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002281}
2282
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002283void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002284 LocationSummary* locations =
2285 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002286 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002287 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002288 bool needs_write_barrier =
2289 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
2290
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002291 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2292 || (field_type == Primitive::kPrimByte);
2293 // The register allocator does not support multiple
2294 // inputs that die at entry with one in a specific register.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002295 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002296 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002297 locations->SetInAt(1, Location::RegisterLocation(EAX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002298 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002299 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002300 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002301 // Temporary registers for the write barrier.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002302 if (needs_write_barrier) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002303 locations->AddTemp(Location::RequiresRegister());
2304 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002305 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002306 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002307}
2308
2309void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2310 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002311 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002312 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01002313 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002314
2315 switch (field_type) {
2316 case Primitive::kPrimBoolean:
2317 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002318 ByteRegister value = locations->InAt(1).As<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002319 __ movb(Address(obj, offset), value);
2320 break;
2321 }
2322
2323 case Primitive::kPrimShort:
2324 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002325 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002326 __ movw(Address(obj, offset), value);
2327 break;
2328 }
2329
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002330 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002331 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002332 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002333 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002334
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002335 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002336 Register temp = locations->GetTemp(0).As<Register>();
2337 Register card = locations->GetTemp(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002338 codegen_->MarkGCCard(temp, card, obj, value);
2339 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002340 break;
2341 }
2342
2343 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002344 Location value = locations->InAt(1);
2345 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
2346 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002347 break;
2348 }
2349
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002350 case Primitive::kPrimFloat: {
2351 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2352 __ movss(Address(obj, offset), value);
2353 break;
2354 }
2355
2356 case Primitive::kPrimDouble: {
2357 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2358 __ movsd(Address(obj, offset), value);
2359 break;
2360 }
2361
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002362 case Primitive::kPrimVoid:
2363 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002364 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002365 }
2366}
2367
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002368void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
2369 Label is_null;
2370 __ testl(value, value);
2371 __ j(kEqual, &is_null);
2372 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2373 __ movl(temp, object);
2374 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
2375 __ movb(Address(temp, card, TIMES_1, 0),
2376 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
2377 __ Bind(&is_null);
2378}
2379
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002380void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002381 LocationSummary* locations =
2382 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002383 locations->SetInAt(0, Location::RequiresRegister());
2384 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002385}
2386
2387void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2388 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002389 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002390 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2391
2392 switch (instruction->GetType()) {
2393 case Primitive::kPrimBoolean: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002394 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002395 __ movzxb(out, Address(obj, offset));
2396 break;
2397 }
2398
2399 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002400 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002401 __ movsxb(out, Address(obj, offset));
2402 break;
2403 }
2404
2405 case Primitive::kPrimShort: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002406 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002407 __ movsxw(out, Address(obj, offset));
2408 break;
2409 }
2410
2411 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002412 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002413 __ movzxw(out, Address(obj, offset));
2414 break;
2415 }
2416
2417 case Primitive::kPrimInt:
2418 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002419 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002420 __ movl(out, Address(obj, offset));
2421 break;
2422 }
2423
2424 case Primitive::kPrimLong: {
2425 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002426 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
2427 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002428 break;
2429 }
2430
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002431 case Primitive::kPrimFloat: {
2432 XmmRegister out = locations->Out().As<XmmRegister>();
2433 __ movss(out, Address(obj, offset));
2434 break;
2435 }
2436
2437 case Primitive::kPrimDouble: {
2438 XmmRegister out = locations->Out().As<XmmRegister>();
2439 __ movsd(out, Address(obj, offset));
2440 break;
2441 }
2442
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002443 case Primitive::kPrimVoid:
2444 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002445 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002446 }
2447}
2448
2449void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002450 LocationSummary* locations =
2451 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002452 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002453 if (instruction->HasUses()) {
2454 locations->SetOut(Location::SameAsFirstInput());
2455 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002456}
2457
2458void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002459 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002460 codegen_->AddSlowPath(slow_path);
2461
2462 LocationSummary* locations = instruction->GetLocations();
2463 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002464
2465 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002466 __ cmpl(obj.As<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002467 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002468 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002469 } else {
2470 DCHECK(obj.IsConstant()) << obj;
2471 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2472 __ jmp(slow_path->GetEntryLabel());
2473 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002474 }
2475 __ j(kEqual, slow_path->GetEntryLabel());
2476}
2477
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002478void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002479 LocationSummary* locations =
2480 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002481 locations->SetInAt(0, Location::RequiresRegister());
2482 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2483 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002484}
2485
2486void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
2487 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002488 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002489 Location index = locations->InAt(1);
2490
2491 switch (instruction->GetType()) {
2492 case Primitive::kPrimBoolean: {
2493 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002494 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002495 if (index.IsConstant()) {
2496 __ movzxb(out, Address(obj,
2497 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2498 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002499 __ movzxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002500 }
2501 break;
2502 }
2503
2504 case Primitive::kPrimByte: {
2505 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002506 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002507 if (index.IsConstant()) {
2508 __ movsxb(out, Address(obj,
2509 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2510 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002511 __ movsxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002512 }
2513 break;
2514 }
2515
2516 case Primitive::kPrimShort: {
2517 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002518 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002519 if (index.IsConstant()) {
2520 __ movsxw(out, Address(obj,
2521 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2522 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002523 __ movsxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002524 }
2525 break;
2526 }
2527
2528 case Primitive::kPrimChar: {
2529 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002530 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002531 if (index.IsConstant()) {
2532 __ movzxw(out, Address(obj,
2533 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2534 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002535 __ movzxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002536 }
2537 break;
2538 }
2539
2540 case Primitive::kPrimInt:
2541 case Primitive::kPrimNot: {
2542 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002543 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002544 if (index.IsConstant()) {
2545 __ movl(out, Address(obj,
2546 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2547 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002548 __ movl(out, Address(obj, index.As<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002549 }
2550 break;
2551 }
2552
2553 case Primitive::kPrimLong: {
2554 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002555 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002556 if (index.IsConstant()) {
2557 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002558 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
2559 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002560 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002561 __ movl(out.AsRegisterPairLow<Register>(),
2562 Address(obj, index.As<Register>(), TIMES_8, data_offset));
2563 __ movl(out.AsRegisterPairHigh<Register>(),
2564 Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002565 }
2566 break;
2567 }
2568
2569 case Primitive::kPrimFloat:
2570 case Primitive::kPrimDouble:
2571 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002572 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002573 case Primitive::kPrimVoid:
2574 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002575 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002576 }
2577}
2578
2579void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002580 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002581 bool needs_write_barrier =
2582 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2583
2584 DCHECK(kFollowsQuickABI);
2585 bool not_enough_registers = needs_write_barrier
2586 && !instruction->GetValue()->IsConstant()
2587 && !instruction->GetIndex()->IsConstant();
2588 bool needs_runtime_call = instruction->NeedsTypeCheck() || not_enough_registers;
2589
Nicolas Geoffray39468442014-09-02 15:17:15 +01002590 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2591 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002592 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002593
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002594 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002595 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002596 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2597 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2598 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002599 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002600 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
2601 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002602 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002603 // In case of a byte operation, the register allocator does not support multiple
2604 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002605 locations->SetInAt(0, Location::RequiresRegister());
2606 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002607 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002608 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002609 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002610 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002611 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002612 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002613 // Temporary registers for the write barrier.
2614 if (needs_write_barrier) {
2615 locations->AddTemp(Location::RequiresRegister());
2616 // Ensure the card is in a byte register.
2617 locations->AddTemp(Location::RegisterLocation(ECX));
2618 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002619 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002620}
2621
2622void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
2623 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002624 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002625 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002626 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002627 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002628 bool needs_runtime_call = locations->WillCall();
2629 bool needs_write_barrier =
2630 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002631
2632 switch (value_type) {
2633 case Primitive::kPrimBoolean:
2634 case Primitive::kPrimByte: {
2635 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002636 if (index.IsConstant()) {
2637 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002638 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002639 __ movb(Address(obj, offset), value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002640 } else {
2641 __ movb(Address(obj, offset),
2642 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2643 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002644 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002645 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002646 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
2647 value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002648 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002649 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002650 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2651 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002652 }
2653 break;
2654 }
2655
2656 case Primitive::kPrimShort:
2657 case Primitive::kPrimChar: {
2658 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002659 if (index.IsConstant()) {
2660 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002661 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002662 __ movw(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002663 } else {
2664 __ movw(Address(obj, offset),
2665 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2666 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002667 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002668 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002669 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
2670 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002671 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002672 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002673 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2674 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002675 }
2676 break;
2677 }
2678
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002679 case Primitive::kPrimInt:
2680 case Primitive::kPrimNot: {
2681 if (!needs_runtime_call) {
2682 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2683 if (index.IsConstant()) {
2684 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2685 if (value.IsRegister()) {
2686 __ movl(Address(obj, offset), value.As<Register>());
2687 } else {
2688 DCHECK(value.IsConstant()) << value;
2689 __ movl(Address(obj, offset),
2690 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2691 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002692 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002693 DCHECK(index.IsRegister()) << index;
2694 if (value.IsRegister()) {
2695 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
2696 value.As<Register>());
2697 } else {
2698 DCHECK(value.IsConstant()) << value;
2699 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
2700 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2701 }
2702 }
2703
2704 if (needs_write_barrier) {
2705 Register temp = locations->GetTemp(0).As<Register>();
2706 Register card = locations->GetTemp(1).As<Register>();
2707 codegen_->MarkGCCard(temp, card, obj, value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002708 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002709 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002710 DCHECK_EQ(value_type, Primitive::kPrimNot);
2711 DCHECK(!codegen_->IsLeafMethod());
2712 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
2713 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002714 }
2715 break;
2716 }
2717
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002718 case Primitive::kPrimLong: {
2719 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002720 if (index.IsConstant()) {
2721 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002722 if (value.IsRegisterPair()) {
2723 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
2724 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002725 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002726 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002727 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
2728 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
2729 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
2730 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002731 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002732 if (value.IsRegisterPair()) {
2733 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
2734 value.AsRegisterPairLow<Register>());
2735 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
2736 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002737 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002738 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002739 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002740 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002741 Immediate(Low32Bits(val)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002742 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002743 Immediate(High32Bits(val)));
2744 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002745 }
2746 break;
2747 }
2748
2749 case Primitive::kPrimFloat:
2750 case Primitive::kPrimDouble:
2751 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002752 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002753 case Primitive::kPrimVoid:
2754 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002755 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002756 }
2757}
2758
2759void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
2760 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002761 locations->SetInAt(0, Location::RequiresRegister());
2762 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002763 instruction->SetLocations(locations);
2764}
2765
2766void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
2767 LocationSummary* locations = instruction->GetLocations();
2768 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002769 Register obj = locations->InAt(0).As<Register>();
2770 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002771 __ movl(out, Address(obj, offset));
2772}
2773
2774void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002775 LocationSummary* locations =
2776 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002777 locations->SetInAt(0, Location::RequiresRegister());
2778 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002779 if (instruction->HasUses()) {
2780 locations->SetOut(Location::SameAsFirstInput());
2781 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002782}
2783
2784void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
2785 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002786 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002787 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002788 codegen_->AddSlowPath(slow_path);
2789
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002790 Register index = locations->InAt(0).As<Register>();
2791 Register length = locations->InAt(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002792
2793 __ cmpl(index, length);
2794 __ j(kAboveEqual, slow_path->GetEntryLabel());
2795}
2796
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002797void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
2798 temp->SetLocations(nullptr);
2799}
2800
2801void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
2802 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002803 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002804}
2805
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002806void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002807 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002808 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002809}
2810
2811void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002812 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2813}
2814
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002815void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
2816 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2817}
2818
2819void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002820 HBasicBlock* block = instruction->GetBlock();
2821 if (block->GetLoopInformation() != nullptr) {
2822 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2823 // The back edge will generate the suspend check.
2824 return;
2825 }
2826 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2827 // The goto will generate the suspend check.
2828 return;
2829 }
2830 GenerateSuspendCheck(instruction, nullptr);
2831}
2832
2833void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
2834 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002835 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002836 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002837 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002838 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002839 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002840 if (successor == nullptr) {
2841 __ j(kNotEqual, slow_path->GetEntryLabel());
2842 __ Bind(slow_path->GetReturnLabel());
2843 } else {
2844 __ j(kEqual, codegen_->GetLabelOf(successor));
2845 __ jmp(slow_path->GetEntryLabel());
2846 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002847}
2848
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002849X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
2850 return codegen_->GetAssembler();
2851}
2852
2853void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
2854 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002855 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002856 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2857 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
2858 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
2859}
2860
2861void ParallelMoveResolverX86::EmitMove(size_t index) {
2862 MoveOperands* move = moves_.Get(index);
2863 Location source = move->GetSource();
2864 Location destination = move->GetDestination();
2865
2866 if (source.IsRegister()) {
2867 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002868 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002869 } else {
2870 DCHECK(destination.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002871 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002872 }
2873 } else if (source.IsStackSlot()) {
2874 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002875 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002876 } else {
2877 DCHECK(destination.IsStackSlot());
2878 MoveMemoryToMemory(destination.GetStackIndex(),
2879 source.GetStackIndex());
2880 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002881 } else if (source.IsConstant()) {
2882 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
2883 Immediate imm(instruction->AsIntConstant()->GetValue());
2884 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002885 __ movl(destination.As<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002886 } else {
2887 __ movl(Address(ESP, destination.GetStackIndex()), imm);
2888 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002889 } else {
2890 LOG(FATAL) << "Unimplemented";
2891 }
2892}
2893
2894void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002895 Register suggested_scratch = reg == EAX ? EBX : EAX;
2896 ScratchRegisterScope ensure_scratch(
2897 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2898
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002899 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2900 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
2901 __ movl(Address(ESP, mem + stack_offset), reg);
2902 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
2903}
2904
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002905void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
2906 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002907 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
2908
2909 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002910 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002911 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2912
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002913 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
2914 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
2915 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
2916 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
2917 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
2918 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
2919}
2920
2921void ParallelMoveResolverX86::EmitSwap(size_t index) {
2922 MoveOperands* move = moves_.Get(index);
2923 Location source = move->GetSource();
2924 Location destination = move->GetDestination();
2925
2926 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002927 __ xchgl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002928 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002929 Exchange(source.As<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002930 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002931 Exchange(destination.As<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002932 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
2933 Exchange(destination.GetStackIndex(), source.GetStackIndex());
2934 } else {
2935 LOG(FATAL) << "Unimplemented";
2936 }
2937}
2938
2939void ParallelMoveResolverX86::SpillScratch(int reg) {
2940 __ pushl(static_cast<Register>(reg));
2941}
2942
2943void ParallelMoveResolverX86::RestoreScratch(int reg) {
2944 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002945}
2946
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002947void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002948 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2949 ? LocationSummary::kCallOnSlowPath
2950 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002951 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002952 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002953 locations->SetOut(Location::RequiresRegister());
2954}
2955
2956void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
2957 Register out = cls->GetLocations()->Out().As<Register>();
2958 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002959 DCHECK(!cls->CanCallRuntime());
2960 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002961 codegen_->LoadCurrentMethod(out);
2962 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2963 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002964 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002965 codegen_->LoadCurrentMethod(out);
2966 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2967 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002968
2969 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
2970 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2971 codegen_->AddSlowPath(slow_path);
2972 __ testl(out, out);
2973 __ j(kEqual, slow_path->GetEntryLabel());
2974 if (cls->MustGenerateClinitCheck()) {
2975 GenerateClassInitializationCheck(slow_path, out);
2976 } else {
2977 __ Bind(slow_path->GetExitLabel());
2978 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002979 }
2980}
2981
2982void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
2983 LocationSummary* locations =
2984 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2985 locations->SetInAt(0, Location::RequiresRegister());
2986 if (check->HasUses()) {
2987 locations->SetOut(Location::SameAsFirstInput());
2988 }
2989}
2990
2991void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002992 // We assume the class to not be null.
2993 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
2994 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002995 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002996 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<Register>());
2997}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002998
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002999void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
3000 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003001 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3002 Immediate(mirror::Class::kStatusInitialized));
3003 __ j(kLess, slow_path->GetEntryLabel());
3004 __ Bind(slow_path->GetExitLabel());
3005 // No need for memory fence, thanks to the X86 memory model.
3006}
3007
3008void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3009 LocationSummary* locations =
3010 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3011 locations->SetInAt(0, Location::RequiresRegister());
3012 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3013}
3014
3015void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3016 LocationSummary* locations = instruction->GetLocations();
3017 Register cls = locations->InAt(0).As<Register>();
3018 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
3019
3020 switch (instruction->GetType()) {
3021 case Primitive::kPrimBoolean: {
3022 Register out = locations->Out().As<Register>();
3023 __ movzxb(out, Address(cls, offset));
3024 break;
3025 }
3026
3027 case Primitive::kPrimByte: {
3028 Register out = locations->Out().As<Register>();
3029 __ movsxb(out, Address(cls, offset));
3030 break;
3031 }
3032
3033 case Primitive::kPrimShort: {
3034 Register out = locations->Out().As<Register>();
3035 __ movsxw(out, Address(cls, offset));
3036 break;
3037 }
3038
3039 case Primitive::kPrimChar: {
3040 Register out = locations->Out().As<Register>();
3041 __ movzxw(out, Address(cls, offset));
3042 break;
3043 }
3044
3045 case Primitive::kPrimInt:
3046 case Primitive::kPrimNot: {
3047 Register out = locations->Out().As<Register>();
3048 __ movl(out, Address(cls, offset));
3049 break;
3050 }
3051
3052 case Primitive::kPrimLong: {
3053 // TODO: support volatile.
3054 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(cls, offset));
3055 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(cls, kX86WordSize + offset));
3056 break;
3057 }
3058
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003059 case Primitive::kPrimFloat: {
3060 XmmRegister out = locations->Out().As<XmmRegister>();
3061 __ movss(out, Address(cls, offset));
3062 break;
3063 }
3064
3065 case Primitive::kPrimDouble: {
3066 XmmRegister out = locations->Out().As<XmmRegister>();
3067 __ movsd(out, Address(cls, offset));
3068 break;
3069 }
3070
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003071 case Primitive::kPrimVoid:
3072 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3073 UNREACHABLE();
3074 }
3075}
3076
3077void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3078 LocationSummary* locations =
3079 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3080 locations->SetInAt(0, Location::RequiresRegister());
3081 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003082 bool needs_write_barrier =
3083 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003084 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3085 || (field_type == Primitive::kPrimByte);
3086 // The register allocator does not support multiple
3087 // inputs that die at entry with one in a specific register.
3088 if (is_byte_type) {
3089 // Ensure the value is in a byte register.
3090 locations->SetInAt(1, Location::RegisterLocation(EAX));
3091 } else {
3092 locations->SetInAt(1, Location::RequiresRegister());
3093 }
3094 // Temporary registers for the write barrier.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003095 if (needs_write_barrier) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003096 locations->AddTemp(Location::RequiresRegister());
3097 // Ensure the card is in a byte register.
3098 locations->AddTemp(Location::RegisterLocation(ECX));
3099 }
3100}
3101
3102void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3103 LocationSummary* locations = instruction->GetLocations();
3104 Register cls = locations->InAt(0).As<Register>();
3105 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
3106 Primitive::Type field_type = instruction->GetFieldType();
3107
3108 switch (field_type) {
3109 case Primitive::kPrimBoolean:
3110 case Primitive::kPrimByte: {
3111 ByteRegister value = locations->InAt(1).As<ByteRegister>();
3112 __ movb(Address(cls, offset), value);
3113 break;
3114 }
3115
3116 case Primitive::kPrimShort:
3117 case Primitive::kPrimChar: {
3118 Register value = locations->InAt(1).As<Register>();
3119 __ movw(Address(cls, offset), value);
3120 break;
3121 }
3122
3123 case Primitive::kPrimInt:
3124 case Primitive::kPrimNot: {
3125 Register value = locations->InAt(1).As<Register>();
3126 __ movl(Address(cls, offset), value);
3127
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003128 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003129 Register temp = locations->GetTemp(0).As<Register>();
3130 Register card = locations->GetTemp(1).As<Register>();
3131 codegen_->MarkGCCard(temp, card, cls, value);
3132 }
3133 break;
3134 }
3135
3136 case Primitive::kPrimLong: {
3137 Location value = locations->InAt(1);
3138 __ movl(Address(cls, offset), value.AsRegisterPairLow<Register>());
3139 __ movl(Address(cls, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3140 break;
3141 }
3142
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003143 case Primitive::kPrimFloat: {
3144 XmmRegister value = locations->InAt(1).As<XmmRegister>();
3145 __ movss(Address(cls, offset), value);
3146 break;
3147 }
3148
3149 case Primitive::kPrimDouble: {
3150 XmmRegister value = locations->InAt(1).As<XmmRegister>();
3151 __ movsd(Address(cls, offset), value);
3152 break;
3153 }
3154
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003155 case Primitive::kPrimVoid:
3156 LOG(FATAL) << "Unreachable type " << field_type;
3157 UNREACHABLE();
3158 }
3159}
3160
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003161void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
3162 LocationSummary* locations =
3163 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3164 locations->SetOut(Location::RequiresRegister());
3165}
3166
3167void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
3168 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
3169 codegen_->AddSlowPath(slow_path);
3170
3171 Register out = load->GetLocations()->Out().As<Register>();
3172 codegen_->LoadCurrentMethod(out);
3173 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
3174 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3175 __ testl(out, out);
3176 __ j(kEqual, slow_path->GetEntryLabel());
3177 __ Bind(slow_path->GetExitLabel());
3178}
3179
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003180void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
3181 LocationSummary* locations =
3182 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3183 locations->SetOut(Location::RequiresRegister());
3184}
3185
3186void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
3187 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
3188 __ fs()->movl(load->GetLocations()->Out().As<Register>(), address);
3189 __ fs()->movl(address, Immediate(0));
3190}
3191
3192void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
3193 LocationSummary* locations =
3194 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3195 InvokeRuntimeCallingConvention calling_convention;
3196 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3197}
3198
3199void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
3200 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
3201 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3202}
3203
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003204void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003205 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3206 ? LocationSummary::kNoCall
3207 : LocationSummary::kCallOnSlowPath;
3208 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3209 locations->SetInAt(0, Location::RequiresRegister());
3210 locations->SetInAt(1, Location::Any());
3211 locations->SetOut(Location::RequiresRegister());
3212}
3213
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003214void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003215 LocationSummary* locations = instruction->GetLocations();
3216 Register obj = locations->InAt(0).As<Register>();
3217 Location cls = locations->InAt(1);
3218 Register out = locations->Out().As<Register>();
3219 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3220 Label done, zero;
3221 SlowPathCodeX86* slow_path = nullptr;
3222
3223 // Return 0 if `obj` is null.
3224 // TODO: avoid this check if we know obj is not null.
3225 __ testl(obj, obj);
3226 __ j(kEqual, &zero);
3227 __ movl(out, Address(obj, class_offset));
3228 // Compare the class of `obj` with `cls`.
3229 if (cls.IsRegister()) {
3230 __ cmpl(out, cls.As<Register>());
3231 } else {
3232 DCHECK(cls.IsStackSlot()) << cls;
3233 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
3234 }
3235
3236 if (instruction->IsClassFinal()) {
3237 // Classes must be equal for the instanceof to succeed.
3238 __ j(kNotEqual, &zero);
3239 __ movl(out, Immediate(1));
3240 __ jmp(&done);
3241 } else {
3242 // If the classes are not equal, we go into a slow path.
3243 DCHECK(locations->OnlyCallsOnSlowPath());
3244 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003245 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003246 codegen_->AddSlowPath(slow_path);
3247 __ j(kNotEqual, slow_path->GetEntryLabel());
3248 __ movl(out, Immediate(1));
3249 __ jmp(&done);
3250 }
3251 __ Bind(&zero);
3252 __ movl(out, Immediate(0));
3253 if (slow_path != nullptr) {
3254 __ Bind(slow_path->GetExitLabel());
3255 }
3256 __ Bind(&done);
3257}
3258
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003259void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
3260 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3261 instruction, LocationSummary::kCallOnSlowPath);
3262 locations->SetInAt(0, Location::RequiresRegister());
3263 locations->SetInAt(1, Location::Any());
3264 locations->AddTemp(Location::RequiresRegister());
3265}
3266
3267void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
3268 LocationSummary* locations = instruction->GetLocations();
3269 Register obj = locations->InAt(0).As<Register>();
3270 Location cls = locations->InAt(1);
3271 Register temp = locations->GetTemp(0).As<Register>();
3272 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3273 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
3274 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3275 codegen_->AddSlowPath(slow_path);
3276
3277 // TODO: avoid this check if we know obj is not null.
3278 __ testl(obj, obj);
3279 __ j(kEqual, slow_path->GetExitLabel());
3280 __ movl(temp, Address(obj, class_offset));
3281
3282 // Compare the class of `obj` with `cls`.
3283 if (cls.IsRegister()) {
3284 __ cmpl(temp, cls.As<Register>());
3285 } else {
3286 DCHECK(cls.IsStackSlot()) << cls;
3287 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
3288 }
3289
3290 __ j(kNotEqual, slow_path->GetEntryLabel());
3291 __ Bind(slow_path->GetExitLabel());
3292}
3293
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003294void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3295 LocationSummary* locations =
3296 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3297 InvokeRuntimeCallingConvention calling_convention;
3298 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3299}
3300
3301void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3302 __ fs()->call(Address::Absolute(instruction->IsEnter()
3303 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
3304 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
3305 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3306}
3307
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003308void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3309void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3310void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3311
3312void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3313 LocationSummary* locations =
3314 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3315 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3316 || instruction->GetResultType() == Primitive::kPrimLong);
3317 locations->SetInAt(0, Location::RequiresRegister());
3318 locations->SetInAt(1, Location::Any());
3319 locations->SetOut(Location::SameAsFirstInput());
3320}
3321
3322void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
3323 HandleBitwiseOperation(instruction);
3324}
3325
3326void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
3327 HandleBitwiseOperation(instruction);
3328}
3329
3330void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
3331 HandleBitwiseOperation(instruction);
3332}
3333
3334void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3335 LocationSummary* locations = instruction->GetLocations();
3336 Location first = locations->InAt(0);
3337 Location second = locations->InAt(1);
3338 DCHECK(first.Equals(locations->Out()));
3339
3340 if (instruction->GetResultType() == Primitive::kPrimInt) {
3341 if (second.IsRegister()) {
3342 if (instruction->IsAnd()) {
3343 __ andl(first.As<Register>(), second.As<Register>());
3344 } else if (instruction->IsOr()) {
3345 __ orl(first.As<Register>(), second.As<Register>());
3346 } else {
3347 DCHECK(instruction->IsXor());
3348 __ xorl(first.As<Register>(), second.As<Register>());
3349 }
3350 } else if (second.IsConstant()) {
3351 if (instruction->IsAnd()) {
3352 __ andl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3353 } else if (instruction->IsOr()) {
3354 __ orl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3355 } else {
3356 DCHECK(instruction->IsXor());
3357 __ xorl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3358 }
3359 } else {
3360 if (instruction->IsAnd()) {
3361 __ andl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
3362 } else if (instruction->IsOr()) {
3363 __ orl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
3364 } else {
3365 DCHECK(instruction->IsXor());
3366 __ xorl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
3367 }
3368 }
3369 } else {
3370 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3371 if (second.IsRegisterPair()) {
3372 if (instruction->IsAnd()) {
3373 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3374 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3375 } else if (instruction->IsOr()) {
3376 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3377 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3378 } else {
3379 DCHECK(instruction->IsXor());
3380 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3381 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3382 }
3383 } else {
3384 if (instruction->IsAnd()) {
3385 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3386 __ andl(first.AsRegisterPairHigh<Register>(),
3387 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3388 } else if (instruction->IsOr()) {
3389 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3390 __ orl(first.AsRegisterPairHigh<Register>(),
3391 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3392 } else {
3393 DCHECK(instruction->IsXor());
3394 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3395 __ xorl(first.AsRegisterPairHigh<Register>(),
3396 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3397 }
3398 }
3399 }
3400}
3401
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003402} // namespace x86
3403} // namespace art