blob: b0f36ce0b4f4826a647231ee0e7ed787a8a32470 [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)()
Mathieu Chartier2d721012014-11-10 11:08:06 -08001123 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(4).Int32Value()));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001124
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();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001187 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(4).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001188
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();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001222 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
1223 kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001224
1225 DCHECK(!codegen_->IsLeafMethod());
1226 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1227}
1228
Roland Levillain88cb1752014-10-20 16:36:47 +01001229void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1230 LocationSummary* locations =
1231 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1232 switch (neg->GetResultType()) {
1233 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001234 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001235 locations->SetInAt(0, Location::RequiresRegister());
1236 locations->SetOut(Location::SameAsFirstInput());
1237 break;
1238
Roland Levillain88cb1752014-10-20 16:36:47 +01001239 case Primitive::kPrimFloat:
1240 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001241 locations->SetInAt(0, Location::RequiresFpuRegister());
1242 // Output overlaps as we need a fresh (zero-initialized)
1243 // register to perform subtraction from zero.
1244 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001245 break;
1246
1247 default:
1248 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1249 }
1250}
1251
1252void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1253 LocationSummary* locations = neg->GetLocations();
1254 Location out = locations->Out();
1255 Location in = locations->InAt(0);
1256 switch (neg->GetResultType()) {
1257 case Primitive::kPrimInt:
1258 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001259 DCHECK(in.Equals(out));
Roland Levillain88cb1752014-10-20 16:36:47 +01001260 __ negl(out.As<Register>());
1261 break;
1262
1263 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001264 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001265 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001266 __ negl(out.AsRegisterPairLow<Register>());
1267 // Negation is similar to subtraction from zero. The least
1268 // significant byte triggers a borrow when it is different from
1269 // zero; to take it into account, add 1 to the most significant
1270 // byte if the carry flag (CF) is set to 1 after the first NEGL
1271 // operation.
1272 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1273 __ negl(out.AsRegisterPairHigh<Register>());
1274 break;
1275
Roland Levillain88cb1752014-10-20 16:36:47 +01001276 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001277 DCHECK(!in.Equals(out));
1278 // out = 0
1279 __ xorps(out.As<XmmRegister>(), out.As<XmmRegister>());
1280 // out = out - in
1281 __ subss(out.As<XmmRegister>(), in.As<XmmRegister>());
1282 break;
1283
Roland Levillain88cb1752014-10-20 16:36:47 +01001284 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001285 DCHECK(!in.Equals(out));
1286 // out = 0
1287 __ xorpd(out.As<XmmRegister>(), out.As<XmmRegister>());
1288 // out = out - in
1289 __ subsd(out.As<XmmRegister>(), in.As<XmmRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001290 break;
1291
1292 default:
1293 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1294 }
1295}
1296
Roland Levillaindff1f282014-11-05 14:15:05 +00001297void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
1298 LocationSummary* locations =
1299 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1300 Primitive::Type result_type = conversion->GetResultType();
1301 Primitive::Type input_type = conversion->GetInputType();
1302 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001303 case Primitive::kPrimByte:
1304 switch (input_type) {
1305 case Primitive::kPrimShort:
1306 case Primitive::kPrimInt:
1307 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001308 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001309 locations->SetInAt(0, Location::Any());
1310 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1311 break;
1312
1313 default:
1314 LOG(FATAL) << "Unexpected type conversion from " << input_type
1315 << " to " << result_type;
1316 }
1317 break;
1318
Roland Levillain01a8d712014-11-14 16:27:39 +00001319 case Primitive::kPrimShort:
1320 switch (input_type) {
1321 case Primitive::kPrimByte:
1322 case Primitive::kPrimInt:
1323 case Primitive::kPrimChar:
1324 // Processing a Dex `int-to-short' instruction.
1325 locations->SetInAt(0, Location::Any());
1326 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1327 break;
1328
1329 default:
1330 LOG(FATAL) << "Unexpected type conversion from " << input_type
1331 << " to " << result_type;
1332 }
1333 break;
1334
Roland Levillain946e1432014-11-11 17:35:19 +00001335 case Primitive::kPrimInt:
1336 switch (input_type) {
1337 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001338 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001339 locations->SetInAt(0, Location::Any());
1340 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1341 break;
1342
1343 case Primitive::kPrimFloat:
1344 case Primitive::kPrimDouble:
1345 LOG(FATAL) << "Type conversion from " << input_type
1346 << " to " << result_type << " not yet implemented";
1347 break;
1348
1349 default:
1350 LOG(FATAL) << "Unexpected type conversion from " << input_type
1351 << " to " << result_type;
1352 }
1353 break;
1354
Roland Levillaindff1f282014-11-05 14:15:05 +00001355 case Primitive::kPrimLong:
1356 switch (input_type) {
1357 case Primitive::kPrimByte:
1358 case Primitive::kPrimShort:
1359 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001360 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001361 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001362 locations->SetInAt(0, Location::RegisterLocation(EAX));
1363 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1364 break;
1365
1366 case Primitive::kPrimFloat:
1367 case Primitive::kPrimDouble:
1368 LOG(FATAL) << "Type conversion from " << input_type << " to "
1369 << result_type << " not yet implemented";
1370 break;
1371
1372 default:
1373 LOG(FATAL) << "Unexpected type conversion from " << input_type
1374 << " to " << result_type;
1375 }
1376 break;
1377
Roland Levillain981e4542014-11-14 11:47:14 +00001378 case Primitive::kPrimChar:
1379 switch (input_type) {
1380 case Primitive::kPrimByte:
1381 case Primitive::kPrimShort:
1382 case Primitive::kPrimInt:
1383 case Primitive::kPrimChar:
1384 // Processing a Dex `int-to-char' instruction.
1385 locations->SetInAt(0, Location::Any());
1386 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1387 break;
1388
1389 default:
1390 LOG(FATAL) << "Unexpected type conversion from " << input_type
1391 << " to " << result_type;
1392 }
1393 break;
1394
Roland Levillaindff1f282014-11-05 14:15:05 +00001395 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001396 switch (input_type) {
1397 case Primitive::kPrimByte:
1398 case Primitive::kPrimShort:
1399 case Primitive::kPrimInt:
1400 case Primitive::kPrimChar:
1401 // Processing a Dex `int-to-float' instruction.
1402 locations->SetInAt(0, Location::RequiresRegister());
1403 locations->SetOut(Location::RequiresFpuRegister());
1404 break;
1405
1406 case Primitive::kPrimLong:
1407 case Primitive::kPrimDouble:
1408 LOG(FATAL) << "Type conversion from " << input_type
1409 << " to " << result_type << " not yet implemented";
1410 break;
1411
1412 default:
1413 LOG(FATAL) << "Unexpected type conversion from " << input_type
1414 << " to " << result_type;
1415 };
1416 break;
1417
Roland Levillaindff1f282014-11-05 14:15:05 +00001418 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001419 switch (input_type) {
1420 case Primitive::kPrimByte:
1421 case Primitive::kPrimShort:
1422 case Primitive::kPrimInt:
1423 case Primitive::kPrimChar:
1424 // Processing a Dex `int-to-double' instruction.
1425 locations->SetInAt(0, Location::RequiresRegister());
1426 locations->SetOut(Location::RequiresFpuRegister());
1427 break;
1428
1429 case Primitive::kPrimLong:
1430 case Primitive::kPrimFloat:
1431 LOG(FATAL) << "Type conversion from " << input_type
1432 << " to " << result_type << " not yet implemented";
1433 break;
1434
1435 default:
1436 LOG(FATAL) << "Unexpected type conversion from " << input_type
1437 << " to " << result_type;
1438 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001439 break;
1440
1441 default:
1442 LOG(FATAL) << "Unexpected type conversion from " << input_type
1443 << " to " << result_type;
1444 }
1445}
1446
1447void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1448 LocationSummary* locations = conversion->GetLocations();
1449 Location out = locations->Out();
1450 Location in = locations->InAt(0);
1451 Primitive::Type result_type = conversion->GetResultType();
1452 Primitive::Type input_type = conversion->GetInputType();
1453 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001454 case Primitive::kPrimByte:
1455 switch (input_type) {
1456 case Primitive::kPrimShort:
1457 case Primitive::kPrimInt:
1458 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001459 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001460 if (in.IsRegister()) {
1461 __ movsxb(out.As<Register>(), in.As<ByteRegister>());
1462 } else if (in.IsStackSlot()) {
1463 __ movsxb(out.As<Register>(), Address(ESP, in.GetStackIndex()));
1464 } else {
1465 DCHECK(in.GetConstant()->IsIntConstant());
1466 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1467 __ movl(out.As<Register>(), Immediate(static_cast<int8_t>(value)));
1468 }
1469 break;
1470
1471 default:
1472 LOG(FATAL) << "Unexpected type conversion from " << input_type
1473 << " to " << result_type;
1474 }
1475 break;
1476
Roland Levillain01a8d712014-11-14 16:27:39 +00001477 case Primitive::kPrimShort:
1478 switch (input_type) {
1479 case Primitive::kPrimByte:
1480 case Primitive::kPrimInt:
1481 case Primitive::kPrimChar:
1482 // Processing a Dex `int-to-short' instruction.
1483 if (in.IsRegister()) {
1484 __ movsxw(out.As<Register>(), in.As<Register>());
1485 } else if (in.IsStackSlot()) {
1486 __ movsxw(out.As<Register>(), Address(ESP, in.GetStackIndex()));
1487 } else {
1488 DCHECK(in.GetConstant()->IsIntConstant());
1489 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1490 __ movl(out.As<Register>(), Immediate(static_cast<int16_t>(value)));
1491 }
1492 break;
1493
1494 default:
1495 LOG(FATAL) << "Unexpected type conversion from " << input_type
1496 << " to " << result_type;
1497 }
1498 break;
1499
Roland Levillain946e1432014-11-11 17:35:19 +00001500 case Primitive::kPrimInt:
1501 switch (input_type) {
1502 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001503 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001504 if (in.IsRegisterPair()) {
1505 __ movl(out.As<Register>(), in.AsRegisterPairLow<Register>());
1506 } else if (in.IsDoubleStackSlot()) {
1507 __ movl(out.As<Register>(), Address(ESP, in.GetStackIndex()));
1508 } else {
1509 DCHECK(in.IsConstant());
1510 DCHECK(in.GetConstant()->IsLongConstant());
1511 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
1512 __ movl(out.As<Register>(), Immediate(static_cast<int32_t>(value)));
1513 }
1514 break;
1515
1516 case Primitive::kPrimFloat:
1517 case Primitive::kPrimDouble:
1518 LOG(FATAL) << "Type conversion from " << input_type
1519 << " to " << result_type << " not yet implemented";
1520 break;
1521
1522 default:
1523 LOG(FATAL) << "Unexpected type conversion from " << input_type
1524 << " to " << result_type;
1525 }
1526 break;
1527
Roland Levillaindff1f282014-11-05 14:15:05 +00001528 case Primitive::kPrimLong:
1529 switch (input_type) {
1530 case Primitive::kPrimByte:
1531 case Primitive::kPrimShort:
1532 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001533 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001534 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001535 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1536 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
1537 DCHECK_EQ(in.As<Register>(), EAX);
1538 __ cdq();
1539 break;
1540
1541 case Primitive::kPrimFloat:
1542 case Primitive::kPrimDouble:
1543 LOG(FATAL) << "Type conversion from " << input_type << " to "
1544 << result_type << " not yet implemented";
1545 break;
1546
1547 default:
1548 LOG(FATAL) << "Unexpected type conversion from " << input_type
1549 << " to " << result_type;
1550 }
1551 break;
1552
Roland Levillain981e4542014-11-14 11:47:14 +00001553 case Primitive::kPrimChar:
1554 switch (input_type) {
1555 case Primitive::kPrimByte:
1556 case Primitive::kPrimShort:
1557 case Primitive::kPrimInt:
1558 case Primitive::kPrimChar:
1559 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1560 if (in.IsRegister()) {
1561 __ movzxw(out.As<Register>(), in.As<Register>());
1562 } else if (in.IsStackSlot()) {
1563 __ movzxw(out.As<Register>(), Address(ESP, in.GetStackIndex()));
1564 } else {
1565 DCHECK(in.GetConstant()->IsIntConstant());
1566 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1567 __ movl(out.As<Register>(), Immediate(static_cast<uint16_t>(value)));
1568 }
1569 break;
1570
1571 default:
1572 LOG(FATAL) << "Unexpected type conversion from " << input_type
1573 << " to " << result_type;
1574 }
1575 break;
1576
Roland Levillaindff1f282014-11-05 14:15:05 +00001577 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001578 switch (input_type) {
1579 // Processing a Dex `int-to-float' instruction.
1580 case Primitive::kPrimByte:
1581 case Primitive::kPrimShort:
1582 case Primitive::kPrimInt:
1583 case Primitive::kPrimChar:
1584 __ cvtsi2ss(out.As<XmmRegister>(), in.As<Register>());
1585 break;
1586
1587 case Primitive::kPrimLong:
1588 case Primitive::kPrimDouble:
1589 LOG(FATAL) << "Type conversion from " << input_type
1590 << " to " << result_type << " not yet implemented";
1591 break;
1592
1593 default:
1594 LOG(FATAL) << "Unexpected type conversion from " << input_type
1595 << " to " << result_type;
1596 };
1597 break;
1598
Roland Levillaindff1f282014-11-05 14:15:05 +00001599 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001600 switch (input_type) {
1601 // Processing a Dex `int-to-double' instruction.
1602 case Primitive::kPrimByte:
1603 case Primitive::kPrimShort:
1604 case Primitive::kPrimInt:
1605 case Primitive::kPrimChar:
1606 __ cvtsi2sd(out.As<XmmRegister>(), in.As<Register>());
1607 break;
1608
1609 case Primitive::kPrimLong:
1610 case Primitive::kPrimFloat:
1611 LOG(FATAL) << "Type conversion from " << input_type
1612 << " to " << result_type << " not yet implemented";
1613 break;
1614
1615 default:
1616 LOG(FATAL) << "Unexpected type conversion from " << input_type
1617 << " to " << result_type;
1618 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001619 break;
1620
1621 default:
1622 LOG(FATAL) << "Unexpected type conversion from " << input_type
1623 << " to " << result_type;
1624 }
1625}
1626
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001627void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001628 LocationSummary* locations =
1629 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001630 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001631 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001632 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001633 locations->SetInAt(0, Location::RequiresRegister());
1634 locations->SetInAt(1, Location::Any());
1635 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001636 break;
1637 }
1638
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001639 case Primitive::kPrimFloat:
1640 case Primitive::kPrimDouble: {
1641 locations->SetInAt(0, Location::RequiresFpuRegister());
1642 locations->SetInAt(1, Location::Any());
1643 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001644 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001645 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001646
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001647 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001648 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1649 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001650 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001651}
1652
1653void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1654 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001655 Location first = locations->InAt(0);
1656 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001657 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001658 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001659 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001660 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001661 __ addl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001662 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001663 __ addl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001664 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001665 __ addl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001666 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001667 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001668 }
1669
1670 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001671 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001672 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1673 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001674 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001675 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1676 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001677 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001678 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001679 break;
1680 }
1681
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001682 case Primitive::kPrimFloat: {
1683 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001684 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001685 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001686 __ addss(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001687 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001688 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001689 }
1690
1691 case Primitive::kPrimDouble: {
1692 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001693 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001694 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001695 __ addsd(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001696 }
1697 break;
1698 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001699
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001700 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001701 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001702 }
1703}
1704
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001705void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001706 LocationSummary* locations =
1707 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001708 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001709 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001710 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001711 locations->SetInAt(0, Location::RequiresRegister());
1712 locations->SetInAt(1, Location::Any());
1713 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001714 break;
1715 }
Calin Juravle11351682014-10-23 15:38:15 +01001716 case Primitive::kPrimFloat:
1717 case Primitive::kPrimDouble: {
1718 locations->SetInAt(0, Location::RequiresFpuRegister());
1719 locations->SetInAt(1, Location::RequiresFpuRegister());
1720 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001721 break;
Calin Juravle11351682014-10-23 15:38:15 +01001722 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001723
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001724 default:
Calin Juravle11351682014-10-23 15:38:15 +01001725 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001726 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001727}
1728
1729void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1730 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001731 Location first = locations->InAt(0);
1732 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001733 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001734 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001735 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001736 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001737 __ subl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001738 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001739 __ subl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001740 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001741 __ subl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001742 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001743 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001744 }
1745
1746 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001747 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01001748 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1749 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001750 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001751 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001752 __ sbbl(first.AsRegisterPairHigh<Register>(),
1753 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001754 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001755 break;
1756 }
1757
Calin Juravle11351682014-10-23 15:38:15 +01001758 case Primitive::kPrimFloat: {
1759 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001760 break;
Calin Juravle11351682014-10-23 15:38:15 +01001761 }
1762
1763 case Primitive::kPrimDouble: {
1764 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1765 break;
1766 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001767
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001768 default:
Calin Juravle11351682014-10-23 15:38:15 +01001769 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001770 }
1771}
1772
Calin Juravle34bacdf2014-10-07 20:23:36 +01001773void LocationsBuilderX86::VisitMul(HMul* mul) {
1774 LocationSummary* locations =
1775 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1776 switch (mul->GetResultType()) {
1777 case Primitive::kPrimInt:
1778 locations->SetInAt(0, Location::RequiresRegister());
1779 locations->SetInAt(1, Location::Any());
1780 locations->SetOut(Location::SameAsFirstInput());
1781 break;
1782 case Primitive::kPrimLong: {
1783 locations->SetInAt(0, Location::RequiresRegister());
1784 // TODO: Currently this handles only stack operands:
1785 // - we don't have enough registers because we currently use Quick ABI.
1786 // - by the time we have a working register allocator we will probably change the ABI
1787 // and fix the above.
1788 // - we don't have a way yet to request operands on stack but the base line compiler
1789 // will leave the operands on the stack with Any().
1790 locations->SetInAt(1, Location::Any());
1791 locations->SetOut(Location::SameAsFirstInput());
1792 // Needed for imul on 32bits with 64bits output.
1793 locations->AddTemp(Location::RegisterLocation(EAX));
1794 locations->AddTemp(Location::RegisterLocation(EDX));
1795 break;
1796 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001797 case Primitive::kPrimFloat:
1798 case Primitive::kPrimDouble: {
1799 locations->SetInAt(0, Location::RequiresFpuRegister());
1800 locations->SetInAt(1, Location::RequiresFpuRegister());
1801 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001802 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001803 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001804
1805 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001806 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001807 }
1808}
1809
1810void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1811 LocationSummary* locations = mul->GetLocations();
1812 Location first = locations->InAt(0);
1813 Location second = locations->InAt(1);
1814 DCHECK(first.Equals(locations->Out()));
1815
1816 switch (mul->GetResultType()) {
1817 case Primitive::kPrimInt: {
1818 if (second.IsRegister()) {
1819 __ imull(first.As<Register>(), second.As<Register>());
1820 } else if (second.IsConstant()) {
1821 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1822 __ imull(first.As<Register>(), imm);
1823 } else {
1824 DCHECK(second.IsStackSlot());
1825 __ imull(first.As<Register>(), Address(ESP, second.GetStackIndex()));
1826 }
1827 break;
1828 }
1829
1830 case Primitive::kPrimLong: {
1831 DCHECK(second.IsDoubleStackSlot());
1832
1833 Register in1_hi = first.AsRegisterPairHigh<Register>();
1834 Register in1_lo = first.AsRegisterPairLow<Register>();
1835 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
1836 Address in2_lo(ESP, second.GetStackIndex());
1837 Register eax = locations->GetTemp(0).As<Register>();
1838 Register edx = locations->GetTemp(1).As<Register>();
1839
1840 DCHECK_EQ(EAX, eax);
1841 DCHECK_EQ(EDX, edx);
1842
1843 // input: in1 - 64 bits, in2 - 64 bits
1844 // output: in1
1845 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
1846 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
1847 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
1848
1849 __ movl(eax, in2_hi);
1850 // eax <- in1.lo * in2.hi
1851 __ imull(eax, in1_lo);
1852 // in1.hi <- in1.hi * in2.lo
1853 __ imull(in1_hi, in2_lo);
1854 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
1855 __ addl(in1_hi, eax);
1856 // move in1_lo to eax to prepare for double precision
1857 __ movl(eax, in1_lo);
1858 // edx:eax <- in1.lo * in2.lo
1859 __ mull(in2_lo);
1860 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
1861 __ addl(in1_hi, edx);
1862 // in1.lo <- (in1.lo * in2.lo)[31:0];
1863 __ movl(in1_lo, eax);
1864
1865 break;
1866 }
1867
Calin Juravleb5bfa962014-10-21 18:02:24 +01001868 case Primitive::kPrimFloat: {
1869 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001870 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001871 }
1872
1873 case Primitive::kPrimDouble: {
1874 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1875 break;
1876 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001877
1878 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001879 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001880 }
1881}
1882
Calin Juravlebacfec32014-11-14 15:54:36 +00001883void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
1884 DCHECK(instruction->IsDiv() || instruction->IsRem());
1885
1886 LocationSummary* locations = instruction->GetLocations();
1887 Location out = locations->Out();
1888 Location first = locations->InAt(0);
1889 Location second = locations->InAt(1);
1890 bool is_div = instruction->IsDiv();
1891
1892 switch (instruction->GetResultType()) {
1893 case Primitive::kPrimInt: {
1894 Register second_reg = second.As<Register>();
1895 DCHECK_EQ(EAX, first.As<Register>());
1896 DCHECK_EQ(is_div ? EAX : EDX, out.As<Register>());
1897
1898 SlowPathCodeX86* slow_path =
1899 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.As<Register>(), is_div);
1900 codegen_->AddSlowPath(slow_path);
1901
1902 // 0x80000000/-1 triggers an arithmetic exception!
1903 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
1904 // it's safe to just use negl instead of more complex comparisons.
1905
1906 __ cmpl(second_reg, Immediate(-1));
1907 __ j(kEqual, slow_path->GetEntryLabel());
1908
1909 // edx:eax <- sign-extended of eax
1910 __ cdq();
1911 // eax = quotient, edx = remainder
1912 __ idivl(second_reg);
1913
1914 __ Bind(slow_path->GetExitLabel());
1915 break;
1916 }
1917
1918 case Primitive::kPrimLong: {
1919 InvokeRuntimeCallingConvention calling_convention;
1920 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
1921 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
1922 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
1923 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
1924 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
1925 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
1926
1927 if (is_div) {
1928 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
1929 } else {
1930 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
1931 }
1932 uint32_t dex_pc = is_div
1933 ? instruction->AsDiv()->GetDexPc()
1934 : instruction->AsRem()->GetDexPc();
1935 codegen_->RecordPcInfo(instruction, dex_pc);
1936
1937 break;
1938 }
1939
1940 default:
1941 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
1942 }
1943}
1944
Calin Juravle7c4954d2014-10-28 16:57:40 +00001945void LocationsBuilderX86::VisitDiv(HDiv* div) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001946 LocationSummary::CallKind call_kind = div->GetResultType() == Primitive::kPrimLong
1947 ? LocationSummary::kCall
1948 : LocationSummary::kNoCall;
1949 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
1950
Calin Juravle7c4954d2014-10-28 16:57:40 +00001951 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001952 case Primitive::kPrimInt: {
1953 locations->SetInAt(0, Location::RegisterLocation(EAX));
1954 locations->SetInAt(1, Location::RequiresRegister());
1955 locations->SetOut(Location::SameAsFirstInput());
1956 // Intel uses edx:eax as the dividend.
1957 locations->AddTemp(Location::RegisterLocation(EDX));
1958 break;
1959 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001960 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001961 InvokeRuntimeCallingConvention calling_convention;
1962 locations->SetInAt(0, Location::RegisterPairLocation(
1963 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
1964 locations->SetInAt(1, Location::RegisterPairLocation(
1965 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
1966 // Runtime helper puts the result in EAX, EDX.
1967 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00001968 break;
1969 }
1970 case Primitive::kPrimFloat:
1971 case Primitive::kPrimDouble: {
1972 locations->SetInAt(0, Location::RequiresFpuRegister());
1973 locations->SetInAt(1, Location::RequiresFpuRegister());
1974 locations->SetOut(Location::SameAsFirstInput());
1975 break;
1976 }
1977
1978 default:
1979 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1980 }
1981}
1982
1983void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
1984 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001985 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00001986 Location first = locations->InAt(0);
1987 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001988
1989 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00001990 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00001991 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00001992 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00001993 break;
1994 }
1995
1996 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001997 DCHECK(first.Equals(out));
Calin Juravle7c4954d2014-10-28 16:57:40 +00001998 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1999 break;
2000 }
2001
2002 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002003 DCHECK(first.Equals(out));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002004 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
2005 break;
2006 }
2007
2008 default:
2009 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2010 }
2011}
2012
Calin Juravlebacfec32014-11-14 15:54:36 +00002013void LocationsBuilderX86::VisitRem(HRem* rem) {
2014 LocationSummary::CallKind call_kind = rem->GetResultType() == Primitive::kPrimLong
2015 ? LocationSummary::kCall
2016 : LocationSummary::kNoCall;
2017 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2018
2019 switch (rem->GetResultType()) {
2020 case Primitive::kPrimInt: {
2021 locations->SetInAt(0, Location::RegisterLocation(EAX));
2022 locations->SetInAt(1, Location::RequiresRegister());
2023 locations->SetOut(Location::RegisterLocation(EDX));
2024 break;
2025 }
2026 case Primitive::kPrimLong: {
2027 InvokeRuntimeCallingConvention calling_convention;
2028 locations->SetInAt(0, Location::RegisterPairLocation(
2029 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2030 locations->SetInAt(1, Location::RegisterPairLocation(
2031 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2032 // Runtime helper puts the result in EAX, EDX.
2033 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2034 break;
2035 }
2036 case Primitive::kPrimFloat:
2037 case Primitive::kPrimDouble: {
2038 LOG(FATAL) << "Unimplemented rem type " << rem->GetResultType();
2039 break;
2040 }
2041
2042 default:
2043 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2044 }
2045}
2046
2047void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2048 Primitive::Type type = rem->GetResultType();
2049 switch (type) {
2050 case Primitive::kPrimInt:
2051 case Primitive::kPrimLong: {
2052 GenerateDivRemIntegral(rem);
2053 break;
2054 }
2055 case Primitive::kPrimFloat:
2056 case Primitive::kPrimDouble: {
2057 LOG(FATAL) << "Unimplemented rem type " << type;
2058 break;
2059 }
2060 default:
2061 LOG(FATAL) << "Unexpected rem type " << type;
2062 }
2063}
2064
Calin Juravled0d48522014-11-04 16:40:20 +00002065void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2066 LocationSummary* locations =
2067 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002068 switch (instruction->GetType()) {
2069 case Primitive::kPrimInt: {
2070 locations->SetInAt(0, Location::Any());
2071 break;
2072 }
2073 case Primitive::kPrimLong: {
2074 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2075 if (!instruction->IsConstant()) {
2076 locations->AddTemp(Location::RequiresRegister());
2077 }
2078 break;
2079 }
2080 default:
2081 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2082 }
Calin Juravled0d48522014-11-04 16:40:20 +00002083 if (instruction->HasUses()) {
2084 locations->SetOut(Location::SameAsFirstInput());
2085 }
2086}
2087
2088void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2089 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2090 codegen_->AddSlowPath(slow_path);
2091
2092 LocationSummary* locations = instruction->GetLocations();
2093 Location value = locations->InAt(0);
2094
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002095 switch (instruction->GetType()) {
2096 case Primitive::kPrimInt: {
2097 if (value.IsRegister()) {
2098 __ testl(value.As<Register>(), value.As<Register>());
2099 __ j(kEqual, slow_path->GetEntryLabel());
2100 } else if (value.IsStackSlot()) {
2101 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2102 __ j(kEqual, slow_path->GetEntryLabel());
2103 } else {
2104 DCHECK(value.IsConstant()) << value;
2105 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2106 __ jmp(slow_path->GetEntryLabel());
2107 }
2108 }
2109 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002110 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002111 case Primitive::kPrimLong: {
2112 if (value.IsRegisterPair()) {
2113 Register temp = locations->GetTemp(0).As<Register>();
2114 __ movl(temp, value.AsRegisterPairLow<Register>());
2115 __ orl(temp, value.AsRegisterPairHigh<Register>());
2116 __ j(kEqual, slow_path->GetEntryLabel());
2117 } else {
2118 DCHECK(value.IsConstant()) << value;
2119 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2120 __ jmp(slow_path->GetEntryLabel());
2121 }
2122 }
2123 break;
2124 }
2125 default:
2126 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002127 }
Calin Juravled0d48522014-11-04 16:40:20 +00002128}
2129
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002130void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002131 LocationSummary* locations =
2132 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002133 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002134 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002135 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2136 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002137}
2138
2139void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2140 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002141 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002142 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002143
Nicolas Geoffray707c8092014-04-04 10:50:14 +01002144 __ fs()->call(
2145 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002146
Nicolas Geoffray39468442014-09-02 15:17:15 +01002147 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002148 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002149}
2150
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002151void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2152 LocationSummary* locations =
2153 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2154 locations->SetOut(Location::RegisterLocation(EAX));
2155 InvokeRuntimeCallingConvention calling_convention;
2156 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2157 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2158 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2159}
2160
2161void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2162 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002163 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002164 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2165
2166 __ fs()->call(
2167 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
2168
2169 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2170 DCHECK(!codegen_->IsLeafMethod());
2171}
2172
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002173void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002174 LocationSummary* locations =
2175 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002176 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2177 if (location.IsStackSlot()) {
2178 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2179 } else if (location.IsDoubleStackSlot()) {
2180 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002181 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002182 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002183}
2184
2185void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002186 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002187}
2188
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002189void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002190 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002191 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002192 locations->SetInAt(0, Location::RequiresRegister());
2193 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002194}
2195
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002196void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2197 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002198 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002199 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002200 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002201 switch (not_->InputAt(0)->GetType()) {
2202 case Primitive::kPrimBoolean:
2203 __ xorl(out.As<Register>(), Immediate(1));
2204 break;
2205
2206 case Primitive::kPrimInt:
2207 __ notl(out.As<Register>());
2208 break;
2209
2210 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002211 __ notl(out.AsRegisterPairLow<Register>());
2212 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002213 break;
2214
2215 default:
2216 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2217 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002218}
2219
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002220void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002221 LocationSummary* locations =
2222 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002223 locations->SetInAt(0, Location::RequiresRegister());
2224 locations->SetInAt(1, Location::Any());
2225 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002226}
2227
2228void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002229 LocationSummary* locations = compare->GetLocations();
2230 switch (compare->InputAt(0)->GetType()) {
2231 case Primitive::kPrimLong: {
2232 Label less, greater, done;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002233 Register output = locations->Out().As<Register>();
2234 Location left = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002235 Location right = locations->InAt(1);
2236 if (right.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002237 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002238 } else {
2239 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002240 __ cmpl(left.AsRegisterPairHigh<Register>(),
2241 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002242 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002243 __ j(kLess, &less); // Signed compare.
2244 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002245 if (right.IsRegisterPair()) {
2246 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002247 } else {
2248 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002249 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002250 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002251 __ movl(output, Immediate(0));
2252 __ j(kEqual, &done);
2253 __ j(kBelow, &less); // Unsigned compare.
2254
2255 __ Bind(&greater);
2256 __ movl(output, Immediate(1));
2257 __ jmp(&done);
2258
2259 __ Bind(&less);
2260 __ movl(output, Immediate(-1));
2261
2262 __ Bind(&done);
2263 break;
2264 }
2265 default:
2266 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
2267 }
2268}
2269
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002270void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002271 LocationSummary* locations =
2272 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002273 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2274 locations->SetInAt(i, Location::Any());
2275 }
2276 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002277}
2278
2279void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002280 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002281 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002282}
2283
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002284void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002285 LocationSummary* locations =
2286 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002287 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002288 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002289 bool needs_write_barrier =
2290 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
2291
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002292 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2293 || (field_type == Primitive::kPrimByte);
2294 // The register allocator does not support multiple
2295 // inputs that die at entry with one in a specific register.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002296 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002297 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002298 locations->SetInAt(1, Location::RegisterLocation(EAX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002299 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002300 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002301 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002302 // Temporary registers for the write barrier.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002303 if (needs_write_barrier) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002304 locations->AddTemp(Location::RequiresRegister());
2305 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002306 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002307 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002308}
2309
2310void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2311 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002312 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002313 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01002314 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002315
2316 switch (field_type) {
2317 case Primitive::kPrimBoolean:
2318 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002319 ByteRegister value = locations->InAt(1).As<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002320 __ movb(Address(obj, offset), value);
2321 break;
2322 }
2323
2324 case Primitive::kPrimShort:
2325 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002326 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002327 __ movw(Address(obj, offset), value);
2328 break;
2329 }
2330
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002331 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002332 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002333 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002334 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002335
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002336 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002337 Register temp = locations->GetTemp(0).As<Register>();
2338 Register card = locations->GetTemp(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002339 codegen_->MarkGCCard(temp, card, obj, value);
2340 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002341 break;
2342 }
2343
2344 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002345 Location value = locations->InAt(1);
2346 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
2347 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002348 break;
2349 }
2350
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002351 case Primitive::kPrimFloat: {
2352 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2353 __ movss(Address(obj, offset), value);
2354 break;
2355 }
2356
2357 case Primitive::kPrimDouble: {
2358 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2359 __ movsd(Address(obj, offset), value);
2360 break;
2361 }
2362
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002363 case Primitive::kPrimVoid:
2364 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002365 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002366 }
2367}
2368
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002369void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
2370 Label is_null;
2371 __ testl(value, value);
2372 __ j(kEqual, &is_null);
2373 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2374 __ movl(temp, object);
2375 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
2376 __ movb(Address(temp, card, TIMES_1, 0),
2377 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
2378 __ Bind(&is_null);
2379}
2380
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002381void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002382 LocationSummary* locations =
2383 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002384 locations->SetInAt(0, Location::RequiresRegister());
2385 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002386}
2387
2388void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2389 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002390 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002391 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2392
2393 switch (instruction->GetType()) {
2394 case Primitive::kPrimBoolean: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002395 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002396 __ movzxb(out, Address(obj, offset));
2397 break;
2398 }
2399
2400 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002401 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002402 __ movsxb(out, Address(obj, offset));
2403 break;
2404 }
2405
2406 case Primitive::kPrimShort: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002407 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002408 __ movsxw(out, Address(obj, offset));
2409 break;
2410 }
2411
2412 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002413 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002414 __ movzxw(out, Address(obj, offset));
2415 break;
2416 }
2417
2418 case Primitive::kPrimInt:
2419 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002420 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002421 __ movl(out, Address(obj, offset));
2422 break;
2423 }
2424
2425 case Primitive::kPrimLong: {
2426 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002427 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
2428 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002429 break;
2430 }
2431
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002432 case Primitive::kPrimFloat: {
2433 XmmRegister out = locations->Out().As<XmmRegister>();
2434 __ movss(out, Address(obj, offset));
2435 break;
2436 }
2437
2438 case Primitive::kPrimDouble: {
2439 XmmRegister out = locations->Out().As<XmmRegister>();
2440 __ movsd(out, Address(obj, offset));
2441 break;
2442 }
2443
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002444 case Primitive::kPrimVoid:
2445 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002446 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002447 }
2448}
2449
2450void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002451 LocationSummary* locations =
2452 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002453 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002454 if (instruction->HasUses()) {
2455 locations->SetOut(Location::SameAsFirstInput());
2456 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002457}
2458
2459void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002460 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002461 codegen_->AddSlowPath(slow_path);
2462
2463 LocationSummary* locations = instruction->GetLocations();
2464 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002465
2466 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002467 __ cmpl(obj.As<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002468 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002469 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002470 } else {
2471 DCHECK(obj.IsConstant()) << obj;
2472 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2473 __ jmp(slow_path->GetEntryLabel());
2474 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002475 }
2476 __ j(kEqual, slow_path->GetEntryLabel());
2477}
2478
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002479void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002480 LocationSummary* locations =
2481 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002482 locations->SetInAt(0, Location::RequiresRegister());
2483 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2484 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002485}
2486
2487void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
2488 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002489 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002490 Location index = locations->InAt(1);
2491
2492 switch (instruction->GetType()) {
2493 case Primitive::kPrimBoolean: {
2494 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002495 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002496 if (index.IsConstant()) {
2497 __ movzxb(out, Address(obj,
2498 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2499 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002500 __ movzxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002501 }
2502 break;
2503 }
2504
2505 case Primitive::kPrimByte: {
2506 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002507 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002508 if (index.IsConstant()) {
2509 __ movsxb(out, Address(obj,
2510 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2511 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002512 __ movsxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002513 }
2514 break;
2515 }
2516
2517 case Primitive::kPrimShort: {
2518 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002519 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002520 if (index.IsConstant()) {
2521 __ movsxw(out, Address(obj,
2522 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2523 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002524 __ movsxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002525 }
2526 break;
2527 }
2528
2529 case Primitive::kPrimChar: {
2530 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002531 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002532 if (index.IsConstant()) {
2533 __ movzxw(out, Address(obj,
2534 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2535 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002536 __ movzxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002537 }
2538 break;
2539 }
2540
2541 case Primitive::kPrimInt:
2542 case Primitive::kPrimNot: {
2543 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002544 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002545 if (index.IsConstant()) {
2546 __ movl(out, Address(obj,
2547 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2548 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002549 __ movl(out, Address(obj, index.As<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002550 }
2551 break;
2552 }
2553
2554 case Primitive::kPrimLong: {
2555 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002556 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002557 if (index.IsConstant()) {
2558 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002559 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
2560 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002561 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002562 __ movl(out.AsRegisterPairLow<Register>(),
2563 Address(obj, index.As<Register>(), TIMES_8, data_offset));
2564 __ movl(out.AsRegisterPairHigh<Register>(),
2565 Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002566 }
2567 break;
2568 }
2569
2570 case Primitive::kPrimFloat:
2571 case Primitive::kPrimDouble:
2572 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002573 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002574 case Primitive::kPrimVoid:
2575 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002576 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002577 }
2578}
2579
2580void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002581 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002582 bool needs_write_barrier =
2583 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2584
2585 DCHECK(kFollowsQuickABI);
2586 bool not_enough_registers = needs_write_barrier
2587 && !instruction->GetValue()->IsConstant()
2588 && !instruction->GetIndex()->IsConstant();
2589 bool needs_runtime_call = instruction->NeedsTypeCheck() || not_enough_registers;
2590
Nicolas Geoffray39468442014-09-02 15:17:15 +01002591 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2592 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002593 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002594
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002595 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002596 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002597 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2598 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2599 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002600 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002601 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
2602 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002603 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002604 // In case of a byte operation, the register allocator does not support multiple
2605 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002606 locations->SetInAt(0, Location::RequiresRegister());
2607 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002608 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002609 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002610 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002611 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002612 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002613 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002614 // Temporary registers for the write barrier.
2615 if (needs_write_barrier) {
2616 locations->AddTemp(Location::RequiresRegister());
2617 // Ensure the card is in a byte register.
2618 locations->AddTemp(Location::RegisterLocation(ECX));
2619 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002620 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002621}
2622
2623void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
2624 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002625 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002626 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002627 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002628 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002629 bool needs_runtime_call = locations->WillCall();
2630 bool needs_write_barrier =
2631 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002632
2633 switch (value_type) {
2634 case Primitive::kPrimBoolean:
2635 case Primitive::kPrimByte: {
2636 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002637 if (index.IsConstant()) {
2638 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002639 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002640 __ movb(Address(obj, offset), value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002641 } else {
2642 __ movb(Address(obj, offset),
2643 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2644 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002645 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002646 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002647 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
2648 value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002649 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002650 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002651 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2652 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002653 }
2654 break;
2655 }
2656
2657 case Primitive::kPrimShort:
2658 case Primitive::kPrimChar: {
2659 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002660 if (index.IsConstant()) {
2661 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002662 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002663 __ movw(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002664 } else {
2665 __ movw(Address(obj, offset),
2666 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2667 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002668 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002669 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002670 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
2671 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002672 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002673 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002674 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2675 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002676 }
2677 break;
2678 }
2679
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002680 case Primitive::kPrimInt:
2681 case Primitive::kPrimNot: {
2682 if (!needs_runtime_call) {
2683 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2684 if (index.IsConstant()) {
2685 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2686 if (value.IsRegister()) {
2687 __ movl(Address(obj, offset), value.As<Register>());
2688 } else {
2689 DCHECK(value.IsConstant()) << value;
2690 __ movl(Address(obj, offset),
2691 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2692 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002693 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002694 DCHECK(index.IsRegister()) << index;
2695 if (value.IsRegister()) {
2696 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
2697 value.As<Register>());
2698 } else {
2699 DCHECK(value.IsConstant()) << value;
2700 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
2701 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2702 }
2703 }
2704
2705 if (needs_write_barrier) {
2706 Register temp = locations->GetTemp(0).As<Register>();
2707 Register card = locations->GetTemp(1).As<Register>();
2708 codegen_->MarkGCCard(temp, card, obj, value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002709 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002710 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002711 DCHECK_EQ(value_type, Primitive::kPrimNot);
2712 DCHECK(!codegen_->IsLeafMethod());
2713 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
2714 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002715 }
2716 break;
2717 }
2718
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002719 case Primitive::kPrimLong: {
2720 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002721 if (index.IsConstant()) {
2722 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002723 if (value.IsRegisterPair()) {
2724 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
2725 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002726 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002727 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002728 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
2729 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
2730 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
2731 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002732 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002733 if (value.IsRegisterPair()) {
2734 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
2735 value.AsRegisterPairLow<Register>());
2736 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
2737 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002738 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002739 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002740 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002741 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002742 Immediate(Low32Bits(val)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002743 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002744 Immediate(High32Bits(val)));
2745 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002746 }
2747 break;
2748 }
2749
2750 case Primitive::kPrimFloat:
2751 case Primitive::kPrimDouble:
2752 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002753 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002754 case Primitive::kPrimVoid:
2755 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002756 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002757 }
2758}
2759
2760void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
2761 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002762 locations->SetInAt(0, Location::RequiresRegister());
2763 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002764 instruction->SetLocations(locations);
2765}
2766
2767void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
2768 LocationSummary* locations = instruction->GetLocations();
2769 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002770 Register obj = locations->InAt(0).As<Register>();
2771 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002772 __ movl(out, Address(obj, offset));
2773}
2774
2775void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002776 LocationSummary* locations =
2777 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002778 locations->SetInAt(0, Location::RequiresRegister());
2779 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002780 if (instruction->HasUses()) {
2781 locations->SetOut(Location::SameAsFirstInput());
2782 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002783}
2784
2785void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
2786 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002787 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002788 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002789 codegen_->AddSlowPath(slow_path);
2790
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002791 Register index = locations->InAt(0).As<Register>();
2792 Register length = locations->InAt(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002793
2794 __ cmpl(index, length);
2795 __ j(kAboveEqual, slow_path->GetEntryLabel());
2796}
2797
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002798void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
2799 temp->SetLocations(nullptr);
2800}
2801
2802void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
2803 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002804 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002805}
2806
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002807void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002808 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002809 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002810}
2811
2812void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002813 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2814}
2815
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002816void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
2817 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2818}
2819
2820void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002821 HBasicBlock* block = instruction->GetBlock();
2822 if (block->GetLoopInformation() != nullptr) {
2823 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2824 // The back edge will generate the suspend check.
2825 return;
2826 }
2827 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2828 // The goto will generate the suspend check.
2829 return;
2830 }
2831 GenerateSuspendCheck(instruction, nullptr);
2832}
2833
2834void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
2835 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002836 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002837 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002838 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002839 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002840 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002841 if (successor == nullptr) {
2842 __ j(kNotEqual, slow_path->GetEntryLabel());
2843 __ Bind(slow_path->GetReturnLabel());
2844 } else {
2845 __ j(kEqual, codegen_->GetLabelOf(successor));
2846 __ jmp(slow_path->GetEntryLabel());
2847 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002848}
2849
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002850X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
2851 return codegen_->GetAssembler();
2852}
2853
2854void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
2855 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002856 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002857 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2858 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
2859 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
2860}
2861
2862void ParallelMoveResolverX86::EmitMove(size_t index) {
2863 MoveOperands* move = moves_.Get(index);
2864 Location source = move->GetSource();
2865 Location destination = move->GetDestination();
2866
2867 if (source.IsRegister()) {
2868 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002869 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002870 } else {
2871 DCHECK(destination.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002872 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002873 }
2874 } else if (source.IsStackSlot()) {
2875 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002876 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002877 } else {
2878 DCHECK(destination.IsStackSlot());
2879 MoveMemoryToMemory(destination.GetStackIndex(),
2880 source.GetStackIndex());
2881 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002882 } else if (source.IsConstant()) {
2883 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
2884 Immediate imm(instruction->AsIntConstant()->GetValue());
2885 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002886 __ movl(destination.As<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002887 } else {
2888 __ movl(Address(ESP, destination.GetStackIndex()), imm);
2889 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002890 } else {
2891 LOG(FATAL) << "Unimplemented";
2892 }
2893}
2894
2895void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002896 Register suggested_scratch = reg == EAX ? EBX : EAX;
2897 ScratchRegisterScope ensure_scratch(
2898 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2899
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002900 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2901 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
2902 __ movl(Address(ESP, mem + stack_offset), reg);
2903 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
2904}
2905
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002906void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
2907 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002908 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
2909
2910 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002911 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002912 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2913
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002914 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
2915 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
2916 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
2917 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
2918 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
2919 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
2920}
2921
2922void ParallelMoveResolverX86::EmitSwap(size_t index) {
2923 MoveOperands* move = moves_.Get(index);
2924 Location source = move->GetSource();
2925 Location destination = move->GetDestination();
2926
2927 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002928 __ xchgl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002929 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002930 Exchange(source.As<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002931 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002932 Exchange(destination.As<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002933 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
2934 Exchange(destination.GetStackIndex(), source.GetStackIndex());
2935 } else {
2936 LOG(FATAL) << "Unimplemented";
2937 }
2938}
2939
2940void ParallelMoveResolverX86::SpillScratch(int reg) {
2941 __ pushl(static_cast<Register>(reg));
2942}
2943
2944void ParallelMoveResolverX86::RestoreScratch(int reg) {
2945 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002946}
2947
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002948void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002949 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2950 ? LocationSummary::kCallOnSlowPath
2951 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002952 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002953 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002954 locations->SetOut(Location::RequiresRegister());
2955}
2956
2957void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
2958 Register out = cls->GetLocations()->Out().As<Register>();
2959 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002960 DCHECK(!cls->CanCallRuntime());
2961 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002962 codegen_->LoadCurrentMethod(out);
2963 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2964 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002965 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002966 codegen_->LoadCurrentMethod(out);
2967 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2968 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002969
2970 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
2971 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2972 codegen_->AddSlowPath(slow_path);
2973 __ testl(out, out);
2974 __ j(kEqual, slow_path->GetEntryLabel());
2975 if (cls->MustGenerateClinitCheck()) {
2976 GenerateClassInitializationCheck(slow_path, out);
2977 } else {
2978 __ Bind(slow_path->GetExitLabel());
2979 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002980 }
2981}
2982
2983void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
2984 LocationSummary* locations =
2985 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2986 locations->SetInAt(0, Location::RequiresRegister());
2987 if (check->HasUses()) {
2988 locations->SetOut(Location::SameAsFirstInput());
2989 }
2990}
2991
2992void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002993 // We assume the class to not be null.
2994 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
2995 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002996 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002997 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<Register>());
2998}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002999
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003000void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
3001 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003002 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3003 Immediate(mirror::Class::kStatusInitialized));
3004 __ j(kLess, slow_path->GetEntryLabel());
3005 __ Bind(slow_path->GetExitLabel());
3006 // No need for memory fence, thanks to the X86 memory model.
3007}
3008
3009void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3010 LocationSummary* locations =
3011 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3012 locations->SetInAt(0, Location::RequiresRegister());
3013 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3014}
3015
3016void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3017 LocationSummary* locations = instruction->GetLocations();
3018 Register cls = locations->InAt(0).As<Register>();
3019 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
3020
3021 switch (instruction->GetType()) {
3022 case Primitive::kPrimBoolean: {
3023 Register out = locations->Out().As<Register>();
3024 __ movzxb(out, Address(cls, offset));
3025 break;
3026 }
3027
3028 case Primitive::kPrimByte: {
3029 Register out = locations->Out().As<Register>();
3030 __ movsxb(out, Address(cls, offset));
3031 break;
3032 }
3033
3034 case Primitive::kPrimShort: {
3035 Register out = locations->Out().As<Register>();
3036 __ movsxw(out, Address(cls, offset));
3037 break;
3038 }
3039
3040 case Primitive::kPrimChar: {
3041 Register out = locations->Out().As<Register>();
3042 __ movzxw(out, Address(cls, offset));
3043 break;
3044 }
3045
3046 case Primitive::kPrimInt:
3047 case Primitive::kPrimNot: {
3048 Register out = locations->Out().As<Register>();
3049 __ movl(out, Address(cls, offset));
3050 break;
3051 }
3052
3053 case Primitive::kPrimLong: {
3054 // TODO: support volatile.
3055 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(cls, offset));
3056 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(cls, kX86WordSize + offset));
3057 break;
3058 }
3059
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003060 case Primitive::kPrimFloat: {
3061 XmmRegister out = locations->Out().As<XmmRegister>();
3062 __ movss(out, Address(cls, offset));
3063 break;
3064 }
3065
3066 case Primitive::kPrimDouble: {
3067 XmmRegister out = locations->Out().As<XmmRegister>();
3068 __ movsd(out, Address(cls, offset));
3069 break;
3070 }
3071
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003072 case Primitive::kPrimVoid:
3073 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3074 UNREACHABLE();
3075 }
3076}
3077
3078void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3079 LocationSummary* locations =
3080 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3081 locations->SetInAt(0, Location::RequiresRegister());
3082 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003083 bool needs_write_barrier =
3084 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003085 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3086 || (field_type == Primitive::kPrimByte);
3087 // The register allocator does not support multiple
3088 // inputs that die at entry with one in a specific register.
3089 if (is_byte_type) {
3090 // Ensure the value is in a byte register.
3091 locations->SetInAt(1, Location::RegisterLocation(EAX));
3092 } else {
3093 locations->SetInAt(1, Location::RequiresRegister());
3094 }
3095 // Temporary registers for the write barrier.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003096 if (needs_write_barrier) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003097 locations->AddTemp(Location::RequiresRegister());
3098 // Ensure the card is in a byte register.
3099 locations->AddTemp(Location::RegisterLocation(ECX));
3100 }
3101}
3102
3103void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3104 LocationSummary* locations = instruction->GetLocations();
3105 Register cls = locations->InAt(0).As<Register>();
3106 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
3107 Primitive::Type field_type = instruction->GetFieldType();
3108
3109 switch (field_type) {
3110 case Primitive::kPrimBoolean:
3111 case Primitive::kPrimByte: {
3112 ByteRegister value = locations->InAt(1).As<ByteRegister>();
3113 __ movb(Address(cls, offset), value);
3114 break;
3115 }
3116
3117 case Primitive::kPrimShort:
3118 case Primitive::kPrimChar: {
3119 Register value = locations->InAt(1).As<Register>();
3120 __ movw(Address(cls, offset), value);
3121 break;
3122 }
3123
3124 case Primitive::kPrimInt:
3125 case Primitive::kPrimNot: {
3126 Register value = locations->InAt(1).As<Register>();
3127 __ movl(Address(cls, offset), value);
3128
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003129 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003130 Register temp = locations->GetTemp(0).As<Register>();
3131 Register card = locations->GetTemp(1).As<Register>();
3132 codegen_->MarkGCCard(temp, card, cls, value);
3133 }
3134 break;
3135 }
3136
3137 case Primitive::kPrimLong: {
3138 Location value = locations->InAt(1);
3139 __ movl(Address(cls, offset), value.AsRegisterPairLow<Register>());
3140 __ movl(Address(cls, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3141 break;
3142 }
3143
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003144 case Primitive::kPrimFloat: {
3145 XmmRegister value = locations->InAt(1).As<XmmRegister>();
3146 __ movss(Address(cls, offset), value);
3147 break;
3148 }
3149
3150 case Primitive::kPrimDouble: {
3151 XmmRegister value = locations->InAt(1).As<XmmRegister>();
3152 __ movsd(Address(cls, offset), value);
3153 break;
3154 }
3155
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003156 case Primitive::kPrimVoid:
3157 LOG(FATAL) << "Unreachable type " << field_type;
3158 UNREACHABLE();
3159 }
3160}
3161
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003162void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
3163 LocationSummary* locations =
3164 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3165 locations->SetOut(Location::RequiresRegister());
3166}
3167
3168void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
3169 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
3170 codegen_->AddSlowPath(slow_path);
3171
3172 Register out = load->GetLocations()->Out().As<Register>();
3173 codegen_->LoadCurrentMethod(out);
3174 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
3175 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3176 __ testl(out, out);
3177 __ j(kEqual, slow_path->GetEntryLabel());
3178 __ Bind(slow_path->GetExitLabel());
3179}
3180
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003181void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
3182 LocationSummary* locations =
3183 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3184 locations->SetOut(Location::RequiresRegister());
3185}
3186
3187void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
3188 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
3189 __ fs()->movl(load->GetLocations()->Out().As<Register>(), address);
3190 __ fs()->movl(address, Immediate(0));
3191}
3192
3193void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
3194 LocationSummary* locations =
3195 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3196 InvokeRuntimeCallingConvention calling_convention;
3197 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3198}
3199
3200void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
3201 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
3202 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3203}
3204
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003205void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003206 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3207 ? LocationSummary::kNoCall
3208 : LocationSummary::kCallOnSlowPath;
3209 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3210 locations->SetInAt(0, Location::RequiresRegister());
3211 locations->SetInAt(1, Location::Any());
3212 locations->SetOut(Location::RequiresRegister());
3213}
3214
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003215void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003216 LocationSummary* locations = instruction->GetLocations();
3217 Register obj = locations->InAt(0).As<Register>();
3218 Location cls = locations->InAt(1);
3219 Register out = locations->Out().As<Register>();
3220 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3221 Label done, zero;
3222 SlowPathCodeX86* slow_path = nullptr;
3223
3224 // Return 0 if `obj` is null.
3225 // TODO: avoid this check if we know obj is not null.
3226 __ testl(obj, obj);
3227 __ j(kEqual, &zero);
3228 __ movl(out, Address(obj, class_offset));
3229 // Compare the class of `obj` with `cls`.
3230 if (cls.IsRegister()) {
3231 __ cmpl(out, cls.As<Register>());
3232 } else {
3233 DCHECK(cls.IsStackSlot()) << cls;
3234 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
3235 }
3236
3237 if (instruction->IsClassFinal()) {
3238 // Classes must be equal for the instanceof to succeed.
3239 __ j(kNotEqual, &zero);
3240 __ movl(out, Immediate(1));
3241 __ jmp(&done);
3242 } else {
3243 // If the classes are not equal, we go into a slow path.
3244 DCHECK(locations->OnlyCallsOnSlowPath());
3245 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003246 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003247 codegen_->AddSlowPath(slow_path);
3248 __ j(kNotEqual, slow_path->GetEntryLabel());
3249 __ movl(out, Immediate(1));
3250 __ jmp(&done);
3251 }
3252 __ Bind(&zero);
3253 __ movl(out, Immediate(0));
3254 if (slow_path != nullptr) {
3255 __ Bind(slow_path->GetExitLabel());
3256 }
3257 __ Bind(&done);
3258}
3259
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003260void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
3261 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3262 instruction, LocationSummary::kCallOnSlowPath);
3263 locations->SetInAt(0, Location::RequiresRegister());
3264 locations->SetInAt(1, Location::Any());
3265 locations->AddTemp(Location::RequiresRegister());
3266}
3267
3268void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
3269 LocationSummary* locations = instruction->GetLocations();
3270 Register obj = locations->InAt(0).As<Register>();
3271 Location cls = locations->InAt(1);
3272 Register temp = locations->GetTemp(0).As<Register>();
3273 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3274 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
3275 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3276 codegen_->AddSlowPath(slow_path);
3277
3278 // TODO: avoid this check if we know obj is not null.
3279 __ testl(obj, obj);
3280 __ j(kEqual, slow_path->GetExitLabel());
3281 __ movl(temp, Address(obj, class_offset));
3282
3283 // Compare the class of `obj` with `cls`.
3284 if (cls.IsRegister()) {
3285 __ cmpl(temp, cls.As<Register>());
3286 } else {
3287 DCHECK(cls.IsStackSlot()) << cls;
3288 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
3289 }
3290
3291 __ j(kNotEqual, slow_path->GetEntryLabel());
3292 __ Bind(slow_path->GetExitLabel());
3293}
3294
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003295void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3296 LocationSummary* locations =
3297 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3298 InvokeRuntimeCallingConvention calling_convention;
3299 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3300}
3301
3302void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3303 __ fs()->call(Address::Absolute(instruction->IsEnter()
3304 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
3305 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
3306 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3307}
3308
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003309void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3310void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3311void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3312
3313void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3314 LocationSummary* locations =
3315 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3316 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3317 || instruction->GetResultType() == Primitive::kPrimLong);
3318 locations->SetInAt(0, Location::RequiresRegister());
3319 locations->SetInAt(1, Location::Any());
3320 locations->SetOut(Location::SameAsFirstInput());
3321}
3322
3323void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
3324 HandleBitwiseOperation(instruction);
3325}
3326
3327void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
3328 HandleBitwiseOperation(instruction);
3329}
3330
3331void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
3332 HandleBitwiseOperation(instruction);
3333}
3334
3335void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3336 LocationSummary* locations = instruction->GetLocations();
3337 Location first = locations->InAt(0);
3338 Location second = locations->InAt(1);
3339 DCHECK(first.Equals(locations->Out()));
3340
3341 if (instruction->GetResultType() == Primitive::kPrimInt) {
3342 if (second.IsRegister()) {
3343 if (instruction->IsAnd()) {
3344 __ andl(first.As<Register>(), second.As<Register>());
3345 } else if (instruction->IsOr()) {
3346 __ orl(first.As<Register>(), second.As<Register>());
3347 } else {
3348 DCHECK(instruction->IsXor());
3349 __ xorl(first.As<Register>(), second.As<Register>());
3350 }
3351 } else if (second.IsConstant()) {
3352 if (instruction->IsAnd()) {
3353 __ andl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3354 } else if (instruction->IsOr()) {
3355 __ orl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3356 } else {
3357 DCHECK(instruction->IsXor());
3358 __ xorl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3359 }
3360 } else {
3361 if (instruction->IsAnd()) {
3362 __ andl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
3363 } else if (instruction->IsOr()) {
3364 __ orl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
3365 } else {
3366 DCHECK(instruction->IsXor());
3367 __ xorl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
3368 }
3369 }
3370 } else {
3371 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3372 if (second.IsRegisterPair()) {
3373 if (instruction->IsAnd()) {
3374 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3375 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3376 } else if (instruction->IsOr()) {
3377 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3378 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3379 } else {
3380 DCHECK(instruction->IsXor());
3381 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3382 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3383 }
3384 } else {
3385 if (instruction->IsAnd()) {
3386 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3387 __ andl(first.AsRegisterPairHigh<Register>(),
3388 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3389 } else if (instruction->IsOr()) {
3390 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3391 __ orl(first.AsRegisterPairHigh<Register>(),
3392 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3393 } else {
3394 DCHECK(instruction->IsXor());
3395 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3396 __ xorl(first.AsRegisterPairHigh<Register>(),
3397 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3398 }
3399 }
3400 }
3401}
3402
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003403} // namespace x86
3404} // namespace art