blob: 3f7a69a88fefbf223e47ff6f7b49dbd0edda2272 [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()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000606 EmitParallelMoves(
607 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
608 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
609 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
610 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100611 } else if (source.IsFpuRegister()) {
612 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100613 } else if (source.IsQuickParameter()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000614 uint16_t register_index = source.GetQuickParameterRegisterIndex();
615 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100616 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000617 EmitParallelMoves(
618 Location::RegisterLocation(calling_convention.GetRegisterAt(register_index)),
619 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
620 Location::StackSlot(
621 calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize()),
622 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100623 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000624 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100625 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100626 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
627 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100628 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
629 }
630 } else if (destination.IsQuickParameter()) {
631 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000632 uint16_t register_index = destination.GetQuickParameterRegisterIndex();
633 uint16_t stack_index = destination.GetQuickParameterStackIndex();
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000634 if (source.IsRegisterPair()) {
635 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100636 } else if (source.IsFpuRegister()) {
637 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100638 } else {
639 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000640 EmitParallelMoves(
641 Location::StackSlot(source.GetStackIndex()),
642 Location::RegisterLocation(calling_convention.GetRegisterAt(register_index)),
643 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
644 Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index + 1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100645 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100646 } else if (destination.IsFpuRegister()) {
647 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100648 __ movsd(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100649 } else {
650 LOG(FATAL) << "Unimplemented";
651 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100652 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000653 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100654 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000655 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100656 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100657 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100658 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100659 } else if (source.IsQuickParameter()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000660 // No conflict possible, so just do the move.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100661 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000662 uint16_t register_index = source.GetQuickParameterRegisterIndex();
663 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000664 // Just move the low part. The only time a source is a quick parameter is
665 // when moving the parameter to its stack locations. And the (Java) caller
666 // of this method has already done that.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100667 __ movl(Address(ESP, destination.GetStackIndex()),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000668 calling_convention.GetRegisterAt(register_index));
669 DCHECK_EQ(calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100670 static_cast<size_t>(destination.GetHighStackIndex(kX86WordSize)));
671 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100672 __ movsd(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100673 } else {
674 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000675 EmitParallelMoves(
676 Location::StackSlot(source.GetStackIndex()),
677 Location::StackSlot(destination.GetStackIndex()),
678 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
679 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100680 }
681 }
682}
683
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100684void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000685 LocationSummary* locations = instruction->GetLocations();
686 if (locations != nullptr && locations->Out().Equals(location)) {
687 return;
688 }
689
690 if (locations != nullptr && locations->Out().IsConstant()) {
691 HConstant* const_to_move = locations->Out().GetConstant();
692 if (const_to_move->IsIntConstant()) {
693 Immediate imm(const_to_move->AsIntConstant()->GetValue());
694 if (location.IsRegister()) {
695 __ movl(location.As<Register>(), imm);
696 } else if (location.IsStackSlot()) {
697 __ movl(Address(ESP, location.GetStackIndex()), imm);
698 } else {
699 DCHECK(location.IsConstant());
700 DCHECK_EQ(location.GetConstant(), const_to_move);
701 }
702 } else if (const_to_move->IsLongConstant()) {
703 int64_t value = const_to_move->AsLongConstant()->GetValue();
704 if (location.IsRegisterPair()) {
705 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
706 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
707 } else if (location.IsDoubleStackSlot()) {
708 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
709 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
710 } else {
711 DCHECK(location.IsConstant());
712 DCHECK_EQ(location.GetConstant(), instruction);
713 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100714 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000715 } else if (instruction->IsTemporary()) {
716 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000717 if (temp_location.IsStackSlot()) {
718 Move32(location, temp_location);
719 } else {
720 DCHECK(temp_location.IsDoubleStackSlot());
721 Move64(location, temp_location);
722 }
Roland Levillain476df552014-10-09 17:51:36 +0100723 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100724 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100725 switch (instruction->GetType()) {
726 case Primitive::kPrimBoolean:
727 case Primitive::kPrimByte:
728 case Primitive::kPrimChar:
729 case Primitive::kPrimShort:
730 case Primitive::kPrimInt:
731 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100732 case Primitive::kPrimFloat:
733 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100734 break;
735
736 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100737 case Primitive::kPrimDouble:
738 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100739 break;
740
741 default:
742 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
743 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000744 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100745 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100746 switch (instruction->GetType()) {
747 case Primitive::kPrimBoolean:
748 case Primitive::kPrimByte:
749 case Primitive::kPrimChar:
750 case Primitive::kPrimShort:
751 case Primitive::kPrimInt:
752 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100753 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000754 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100755 break;
756
757 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100758 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000759 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100760 break;
761
762 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100763 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100764 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000765 }
766}
767
768void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000769 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000770}
771
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000772void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000773 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100774 DCHECK(!successor->IsExitBlock());
775
776 HBasicBlock* block = got->GetBlock();
777 HInstruction* previous = got->GetPrevious();
778
779 HLoopInformation* info = block->GetLoopInformation();
780 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
781 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
782 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
783 return;
784 }
785
786 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
787 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
788 }
789 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000790 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000791 }
792}
793
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000794void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000795 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000796}
797
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000798void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700799 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000800 if (kIsDebugBuild) {
801 __ Comment("Unreachable");
802 __ int3();
803 }
804}
805
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000806void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100807 LocationSummary* locations =
808 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100809 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100810 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100811 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100812 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000813}
814
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000815void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700816 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100817 if (cond->IsIntConstant()) {
818 // Constant condition, statically compared against 1.
819 int32_t cond_value = cond->AsIntConstant()->GetValue();
820 if (cond_value == 1) {
821 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
822 if_instr->IfTrueSuccessor())) {
823 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100824 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100825 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100826 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100827 DCHECK_EQ(cond_value, 0);
828 }
829 } else {
830 bool materialized =
831 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
832 // Moves do not affect the eflags register, so if the condition is
833 // evaluated just before the if, we don't need to evaluate it
834 // again.
835 bool eflags_set = cond->IsCondition()
836 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
837 if (materialized) {
838 if (!eflags_set) {
839 // Materialized condition, compare against 0.
840 Location lhs = if_instr->GetLocations()->InAt(0);
841 if (lhs.IsRegister()) {
842 __ cmpl(lhs.As<Register>(), Immediate(0));
843 } else {
844 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
845 }
846 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
847 } else {
848 __ j(X86Condition(cond->AsCondition()->GetCondition()),
849 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
850 }
851 } else {
852 Location lhs = cond->GetLocations()->InAt(0);
853 Location rhs = cond->GetLocations()->InAt(1);
854 // LHS is guaranteed to be in a register (see
855 // LocationsBuilderX86::VisitCondition).
856 if (rhs.IsRegister()) {
857 __ cmpl(lhs.As<Register>(), rhs.As<Register>());
858 } else if (rhs.IsConstant()) {
859 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
860 Immediate imm(instruction->AsIntConstant()->GetValue());
861 __ cmpl(lhs.As<Register>(), imm);
862 } else {
863 __ cmpl(lhs.As<Register>(), Address(ESP, rhs.GetStackIndex()));
864 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100865 __ j(X86Condition(cond->AsCondition()->GetCondition()),
866 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700867 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100868 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100869 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
870 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700871 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000872 }
873}
874
875void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000876 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000877}
878
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000879void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
880 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000881}
882
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000883void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100884 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000885}
886
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000887void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100888 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700889 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000890}
891
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100892void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100893 LocationSummary* locations =
894 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100895 switch (store->InputAt(1)->GetType()) {
896 case Primitive::kPrimBoolean:
897 case Primitive::kPrimByte:
898 case Primitive::kPrimChar:
899 case Primitive::kPrimShort:
900 case Primitive::kPrimInt:
901 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100902 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100903 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
904 break;
905
906 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100907 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100908 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
909 break;
910
911 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100912 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100913 }
914 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000915}
916
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000917void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700918 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000919}
920
Dave Allison20dfc792014-06-16 20:44:29 -0700921void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100922 LocationSummary* locations =
923 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100924 locations->SetInAt(0, Location::RequiresRegister());
925 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100926 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100927 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100928 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000929}
930
Dave Allison20dfc792014-06-16 20:44:29 -0700931void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
932 if (comp->NeedsMaterialization()) {
933 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100934 Register reg = locations->Out().As<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100935 // Clear register: setcc only sets the low byte.
936 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700937 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100938 __ cmpl(locations->InAt(0).As<Register>(),
939 locations->InAt(1).As<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100940 } else if (locations->InAt(1).IsConstant()) {
941 HConstant* instruction = locations->InAt(1).GetConstant();
942 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100943 __ cmpl(locations->InAt(0).As<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700944 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100945 __ cmpl(locations->InAt(0).As<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700946 Address(ESP, locations->InAt(1).GetStackIndex()));
947 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100948 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100949 }
Dave Allison20dfc792014-06-16 20:44:29 -0700950}
951
952void LocationsBuilderX86::VisitEqual(HEqual* comp) {
953 VisitCondition(comp);
954}
955
956void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
957 VisitCondition(comp);
958}
959
960void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
961 VisitCondition(comp);
962}
963
964void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
965 VisitCondition(comp);
966}
967
968void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
969 VisitCondition(comp);
970}
971
972void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
973 VisitCondition(comp);
974}
975
976void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
977 VisitCondition(comp);
978}
979
980void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
981 VisitCondition(comp);
982}
983
984void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
985 VisitCondition(comp);
986}
987
988void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
989 VisitCondition(comp);
990}
991
992void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
993 VisitCondition(comp);
994}
995
996void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
997 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000998}
999
1000void LocationsBuilderX86::VisitIntConstant(HIntConstant* 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 Geoffray3ff386a2014-03-04 14:46:47 +00001004}
1005
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001006void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001007 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001008 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001009}
1010
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001011void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001012 LocationSummary* locations =
1013 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001014 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001015}
1016
1017void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1018 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001019 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001020}
1021
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001022void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1023 LocationSummary* locations =
1024 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1025 locations->SetOut(Location::ConstantLocation(constant));
1026}
1027
1028void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* 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
1033void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1034 LocationSummary* locations =
1035 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1036 locations->SetOut(Location::ConstantLocation(constant));
1037}
1038
1039void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1040 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001041 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001042}
1043
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001044void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001045 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001046}
1047
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001048void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001049 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001050 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001051 __ ret();
1052}
1053
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001054void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001055 LocationSummary* locations =
1056 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001057 switch (ret->InputAt(0)->GetType()) {
1058 case Primitive::kPrimBoolean:
1059 case Primitive::kPrimByte:
1060 case Primitive::kPrimChar:
1061 case Primitive::kPrimShort:
1062 case Primitive::kPrimInt:
1063 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001064 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001065 break;
1066
1067 case Primitive::kPrimLong:
1068 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001069 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001070 break;
1071
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001072 case Primitive::kPrimFloat:
1073 case Primitive::kPrimDouble:
1074 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001075 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001076 break;
1077
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001078 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001079 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001080 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001081}
1082
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001083void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001084 if (kIsDebugBuild) {
1085 switch (ret->InputAt(0)->GetType()) {
1086 case Primitive::kPrimBoolean:
1087 case Primitive::kPrimByte:
1088 case Primitive::kPrimChar:
1089 case Primitive::kPrimShort:
1090 case Primitive::kPrimInt:
1091 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001092 DCHECK_EQ(ret->GetLocations()->InAt(0).As<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001093 break;
1094
1095 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001096 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1097 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001098 break;
1099
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001100 case Primitive::kPrimFloat:
1101 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001102 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001103 break;
1104
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001105 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001106 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001107 }
1108 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001109 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001110 __ ret();
1111}
1112
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001113void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001114 HandleInvoke(invoke);
1115}
1116
1117void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001118 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001119
1120 // TODO: Implement all kinds of calls:
1121 // 1) boot -> boot
1122 // 2) app -> boot
1123 // 3) app -> app
1124 //
1125 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1126
1127 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001128 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001129 // temp = temp->dex_cache_resolved_methods_;
1130 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
1131 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001132 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001133 // (temp + offset_of_quick_compiled_code)()
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001134 __ call(Address(
1135 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001136
1137 DCHECK(!codegen_->IsLeafMethod());
1138 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1139}
1140
1141void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1142 HandleInvoke(invoke);
1143}
1144
1145void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001146 LocationSummary* locations =
1147 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001148 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001149
1150 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001151 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001152 HInstruction* input = invoke->InputAt(i);
1153 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1154 }
1155
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001156 switch (invoke->GetType()) {
1157 case Primitive::kPrimBoolean:
1158 case Primitive::kPrimByte:
1159 case Primitive::kPrimChar:
1160 case Primitive::kPrimShort:
1161 case Primitive::kPrimInt:
1162 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001163 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001164 break;
1165
1166 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001167 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001168 break;
1169
1170 case Primitive::kPrimVoid:
1171 break;
1172
1173 case Primitive::kPrimDouble:
1174 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001175 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001176 break;
1177 }
1178
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001179 invoke->SetLocations(locations);
1180}
1181
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001182void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001183 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001184 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1185 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1186 LocationSummary* locations = invoke->GetLocations();
1187 Location receiver = locations->InAt(0);
1188 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1189 // temp = object->GetClass();
1190 if (receiver.IsStackSlot()) {
1191 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1192 __ movl(temp, Address(temp, class_offset));
1193 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001194 __ movl(temp, Address(receiver.As<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001195 }
1196 // temp = temp->GetMethodAt(method_offset);
1197 __ movl(temp, Address(temp, method_offset));
1198 // call temp->GetEntryPoint();
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001199 __ call(Address(
1200 temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001201
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001202 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001203 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001204}
1205
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001206void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1207 HandleInvoke(invoke);
1208 // Add the hidden argument.
1209 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM0));
1210}
1211
1212void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1213 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1214 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
1215 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1216 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1217 LocationSummary* locations = invoke->GetLocations();
1218 Location receiver = locations->InAt(0);
1219 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1220
1221 // Set the hidden argument.
1222 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
1223 __ movd(invoke->GetLocations()->GetTemp(1).As<XmmRegister>(), temp);
1224
1225 // temp = object->GetClass();
1226 if (receiver.IsStackSlot()) {
1227 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1228 __ movl(temp, Address(temp, class_offset));
1229 } else {
1230 __ movl(temp, Address(receiver.As<Register>(), class_offset));
1231 }
1232 // temp = temp->GetImtEntryAt(method_offset);
1233 __ movl(temp, Address(temp, method_offset));
1234 // call temp->GetEntryPoint();
Mathieu Chartier2d721012014-11-10 11:08:06 -08001235 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001236 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001237
1238 DCHECK(!codegen_->IsLeafMethod());
1239 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1240}
1241
Roland Levillain88cb1752014-10-20 16:36:47 +01001242void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1243 LocationSummary* locations =
1244 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1245 switch (neg->GetResultType()) {
1246 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001247 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001248 locations->SetInAt(0, Location::RequiresRegister());
1249 locations->SetOut(Location::SameAsFirstInput());
1250 break;
1251
Roland Levillain88cb1752014-10-20 16:36:47 +01001252 case Primitive::kPrimFloat:
1253 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001254 locations->SetInAt(0, Location::RequiresFpuRegister());
1255 // Output overlaps as we need a fresh (zero-initialized)
1256 // register to perform subtraction from zero.
1257 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001258 break;
1259
1260 default:
1261 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1262 }
1263}
1264
1265void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1266 LocationSummary* locations = neg->GetLocations();
1267 Location out = locations->Out();
1268 Location in = locations->InAt(0);
1269 switch (neg->GetResultType()) {
1270 case Primitive::kPrimInt:
1271 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001272 DCHECK(in.Equals(out));
Roland Levillain88cb1752014-10-20 16:36:47 +01001273 __ negl(out.As<Register>());
1274 break;
1275
1276 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001277 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001278 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001279 __ negl(out.AsRegisterPairLow<Register>());
1280 // Negation is similar to subtraction from zero. The least
1281 // significant byte triggers a borrow when it is different from
1282 // zero; to take it into account, add 1 to the most significant
1283 // byte if the carry flag (CF) is set to 1 after the first NEGL
1284 // operation.
1285 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1286 __ negl(out.AsRegisterPairHigh<Register>());
1287 break;
1288
Roland Levillain88cb1752014-10-20 16:36:47 +01001289 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001290 DCHECK(!in.Equals(out));
1291 // out = 0
1292 __ xorps(out.As<XmmRegister>(), out.As<XmmRegister>());
1293 // out = out - in
1294 __ subss(out.As<XmmRegister>(), in.As<XmmRegister>());
1295 break;
1296
Roland Levillain88cb1752014-10-20 16:36:47 +01001297 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001298 DCHECK(!in.Equals(out));
1299 // out = 0
1300 __ xorpd(out.As<XmmRegister>(), out.As<XmmRegister>());
1301 // out = out - in
1302 __ subsd(out.As<XmmRegister>(), in.As<XmmRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001303 break;
1304
1305 default:
1306 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1307 }
1308}
1309
Roland Levillaindff1f282014-11-05 14:15:05 +00001310void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
1311 LocationSummary* locations =
1312 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1313 Primitive::Type result_type = conversion->GetResultType();
1314 Primitive::Type input_type = conversion->GetInputType();
1315 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001316 case Primitive::kPrimByte:
1317 switch (input_type) {
1318 case Primitive::kPrimShort:
1319 case Primitive::kPrimInt:
1320 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001321 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001322 locations->SetInAt(0, Location::Any());
1323 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1324 break;
1325
1326 default:
1327 LOG(FATAL) << "Unexpected type conversion from " << input_type
1328 << " to " << result_type;
1329 }
1330 break;
1331
Roland Levillain01a8d712014-11-14 16:27:39 +00001332 case Primitive::kPrimShort:
1333 switch (input_type) {
1334 case Primitive::kPrimByte:
1335 case Primitive::kPrimInt:
1336 case Primitive::kPrimChar:
1337 // Processing a Dex `int-to-short' instruction.
1338 locations->SetInAt(0, Location::Any());
1339 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1340 break;
1341
1342 default:
1343 LOG(FATAL) << "Unexpected type conversion from " << input_type
1344 << " to " << result_type;
1345 }
1346 break;
1347
Roland Levillain946e1432014-11-11 17:35:19 +00001348 case Primitive::kPrimInt:
1349 switch (input_type) {
1350 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001351 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001352 locations->SetInAt(0, Location::Any());
1353 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1354 break;
1355
1356 case Primitive::kPrimFloat:
1357 case Primitive::kPrimDouble:
1358 LOG(FATAL) << "Type conversion from " << input_type
1359 << " to " << result_type << " not yet implemented";
1360 break;
1361
1362 default:
1363 LOG(FATAL) << "Unexpected type conversion from " << input_type
1364 << " to " << result_type;
1365 }
1366 break;
1367
Roland Levillaindff1f282014-11-05 14:15:05 +00001368 case Primitive::kPrimLong:
1369 switch (input_type) {
1370 case Primitive::kPrimByte:
1371 case Primitive::kPrimShort:
1372 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001373 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001374 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001375 locations->SetInAt(0, Location::RegisterLocation(EAX));
1376 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1377 break;
1378
1379 case Primitive::kPrimFloat:
1380 case Primitive::kPrimDouble:
1381 LOG(FATAL) << "Type conversion from " << input_type << " to "
1382 << result_type << " not yet implemented";
1383 break;
1384
1385 default:
1386 LOG(FATAL) << "Unexpected type conversion from " << input_type
1387 << " to " << result_type;
1388 }
1389 break;
1390
Roland Levillain981e4542014-11-14 11:47:14 +00001391 case Primitive::kPrimChar:
1392 switch (input_type) {
1393 case Primitive::kPrimByte:
1394 case Primitive::kPrimShort:
1395 case Primitive::kPrimInt:
1396 case Primitive::kPrimChar:
1397 // Processing a Dex `int-to-char' instruction.
1398 locations->SetInAt(0, Location::Any());
1399 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1400 break;
1401
1402 default:
1403 LOG(FATAL) << "Unexpected type conversion from " << input_type
1404 << " to " << result_type;
1405 }
1406 break;
1407
Roland Levillaindff1f282014-11-05 14:15:05 +00001408 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001409 switch (input_type) {
1410 case Primitive::kPrimByte:
1411 case Primitive::kPrimShort:
1412 case Primitive::kPrimInt:
1413 case Primitive::kPrimChar:
1414 // Processing a Dex `int-to-float' instruction.
1415 locations->SetInAt(0, Location::RequiresRegister());
1416 locations->SetOut(Location::RequiresFpuRegister());
1417 break;
1418
1419 case Primitive::kPrimLong:
1420 case Primitive::kPrimDouble:
1421 LOG(FATAL) << "Type conversion from " << input_type
1422 << " to " << result_type << " not yet implemented";
1423 break;
1424
1425 default:
1426 LOG(FATAL) << "Unexpected type conversion from " << input_type
1427 << " to " << result_type;
1428 };
1429 break;
1430
Roland Levillaindff1f282014-11-05 14:15:05 +00001431 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001432 switch (input_type) {
1433 case Primitive::kPrimByte:
1434 case Primitive::kPrimShort:
1435 case Primitive::kPrimInt:
1436 case Primitive::kPrimChar:
1437 // Processing a Dex `int-to-double' instruction.
1438 locations->SetInAt(0, Location::RequiresRegister());
1439 locations->SetOut(Location::RequiresFpuRegister());
1440 break;
1441
1442 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001443 // Processing a Dex `long-to-double' instruction.
1444 locations->SetInAt(0, Location::RequiresRegister());
1445 locations->SetOut(Location::RequiresFpuRegister());
1446 locations->AddTemp(Location::RequiresFpuRegister());
1447 locations->AddTemp(Location::RequiresFpuRegister());
1448 break;
1449
Roland Levillaincff13742014-11-17 14:32:17 +00001450 case Primitive::kPrimFloat:
1451 LOG(FATAL) << "Type conversion from " << input_type
1452 << " to " << result_type << " not yet implemented";
1453 break;
1454
1455 default:
1456 LOG(FATAL) << "Unexpected type conversion from " << input_type
1457 << " to " << result_type;
1458 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001459 break;
1460
1461 default:
1462 LOG(FATAL) << "Unexpected type conversion from " << input_type
1463 << " to " << result_type;
1464 }
1465}
1466
1467void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1468 LocationSummary* locations = conversion->GetLocations();
1469 Location out = locations->Out();
1470 Location in = locations->InAt(0);
1471 Primitive::Type result_type = conversion->GetResultType();
1472 Primitive::Type input_type = conversion->GetInputType();
1473 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001474 case Primitive::kPrimByte:
1475 switch (input_type) {
1476 case Primitive::kPrimShort:
1477 case Primitive::kPrimInt:
1478 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001479 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001480 if (in.IsRegister()) {
1481 __ movsxb(out.As<Register>(), in.As<ByteRegister>());
1482 } else if (in.IsStackSlot()) {
1483 __ movsxb(out.As<Register>(), Address(ESP, in.GetStackIndex()));
1484 } else {
1485 DCHECK(in.GetConstant()->IsIntConstant());
1486 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1487 __ movl(out.As<Register>(), Immediate(static_cast<int8_t>(value)));
1488 }
1489 break;
1490
1491 default:
1492 LOG(FATAL) << "Unexpected type conversion from " << input_type
1493 << " to " << result_type;
1494 }
1495 break;
1496
Roland Levillain01a8d712014-11-14 16:27:39 +00001497 case Primitive::kPrimShort:
1498 switch (input_type) {
1499 case Primitive::kPrimByte:
1500 case Primitive::kPrimInt:
1501 case Primitive::kPrimChar:
1502 // Processing a Dex `int-to-short' instruction.
1503 if (in.IsRegister()) {
1504 __ movsxw(out.As<Register>(), in.As<Register>());
1505 } else if (in.IsStackSlot()) {
1506 __ movsxw(out.As<Register>(), Address(ESP, in.GetStackIndex()));
1507 } else {
1508 DCHECK(in.GetConstant()->IsIntConstant());
1509 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1510 __ movl(out.As<Register>(), Immediate(static_cast<int16_t>(value)));
1511 }
1512 break;
1513
1514 default:
1515 LOG(FATAL) << "Unexpected type conversion from " << input_type
1516 << " to " << result_type;
1517 }
1518 break;
1519
Roland Levillain946e1432014-11-11 17:35:19 +00001520 case Primitive::kPrimInt:
1521 switch (input_type) {
1522 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001523 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001524 if (in.IsRegisterPair()) {
1525 __ movl(out.As<Register>(), in.AsRegisterPairLow<Register>());
1526 } else if (in.IsDoubleStackSlot()) {
1527 __ movl(out.As<Register>(), Address(ESP, in.GetStackIndex()));
1528 } else {
1529 DCHECK(in.IsConstant());
1530 DCHECK(in.GetConstant()->IsLongConstant());
1531 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
1532 __ movl(out.As<Register>(), Immediate(static_cast<int32_t>(value)));
1533 }
1534 break;
1535
1536 case Primitive::kPrimFloat:
1537 case Primitive::kPrimDouble:
1538 LOG(FATAL) << "Type conversion from " << input_type
1539 << " to " << result_type << " not yet implemented";
1540 break;
1541
1542 default:
1543 LOG(FATAL) << "Unexpected type conversion from " << input_type
1544 << " to " << result_type;
1545 }
1546 break;
1547
Roland Levillaindff1f282014-11-05 14:15:05 +00001548 case Primitive::kPrimLong:
1549 switch (input_type) {
1550 case Primitive::kPrimByte:
1551 case Primitive::kPrimShort:
1552 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001553 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001554 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001555 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1556 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
1557 DCHECK_EQ(in.As<Register>(), EAX);
1558 __ cdq();
1559 break;
1560
1561 case Primitive::kPrimFloat:
1562 case Primitive::kPrimDouble:
1563 LOG(FATAL) << "Type conversion from " << input_type << " to "
1564 << result_type << " not yet implemented";
1565 break;
1566
1567 default:
1568 LOG(FATAL) << "Unexpected type conversion from " << input_type
1569 << " to " << result_type;
1570 }
1571 break;
1572
Roland Levillain981e4542014-11-14 11:47:14 +00001573 case Primitive::kPrimChar:
1574 switch (input_type) {
1575 case Primitive::kPrimByte:
1576 case Primitive::kPrimShort:
1577 case Primitive::kPrimInt:
1578 case Primitive::kPrimChar:
1579 // Processing a Dex `Process a Dex `int-to-char'' instruction.
1580 if (in.IsRegister()) {
1581 __ movzxw(out.As<Register>(), in.As<Register>());
1582 } else if (in.IsStackSlot()) {
1583 __ movzxw(out.As<Register>(), Address(ESP, in.GetStackIndex()));
1584 } else {
1585 DCHECK(in.GetConstant()->IsIntConstant());
1586 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1587 __ movl(out.As<Register>(), Immediate(static_cast<uint16_t>(value)));
1588 }
1589 break;
1590
1591 default:
1592 LOG(FATAL) << "Unexpected type conversion from " << input_type
1593 << " to " << result_type;
1594 }
1595 break;
1596
Roland Levillaindff1f282014-11-05 14:15:05 +00001597 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001598 switch (input_type) {
1599 // Processing a Dex `int-to-float' instruction.
1600 case Primitive::kPrimByte:
1601 case Primitive::kPrimShort:
1602 case Primitive::kPrimInt:
1603 case Primitive::kPrimChar:
1604 __ cvtsi2ss(out.As<XmmRegister>(), in.As<Register>());
1605 break;
1606
1607 case Primitive::kPrimLong:
1608 case Primitive::kPrimDouble:
1609 LOG(FATAL) << "Type conversion from " << input_type
1610 << " to " << result_type << " not yet implemented";
1611 break;
1612
1613 default:
1614 LOG(FATAL) << "Unexpected type conversion from " << input_type
1615 << " to " << result_type;
1616 };
1617 break;
1618
Roland Levillaindff1f282014-11-05 14:15:05 +00001619 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001620 switch (input_type) {
1621 // Processing a Dex `int-to-double' instruction.
1622 case Primitive::kPrimByte:
1623 case Primitive::kPrimShort:
1624 case Primitive::kPrimInt:
1625 case Primitive::kPrimChar:
1626 __ cvtsi2sd(out.As<XmmRegister>(), in.As<Register>());
1627 break;
1628
Roland Levillain647b9ed2014-11-27 12:06:00 +00001629 case Primitive::kPrimLong: {
1630 // Processing a Dex `long-to-double' instruction.
1631 Register low = in.AsRegisterPairLow<Register>();
1632 Register high = in.AsRegisterPairHigh<Register>();
1633 XmmRegister result = out.As<XmmRegister>();
1634 XmmRegister temp = locations->GetTemp(0).As<XmmRegister>();
1635 XmmRegister constant = locations->GetTemp(1).As<XmmRegister>();
1636
1637 // Binary encoding of 2^32 for type double.
1638 const int64_t c1 = INT64_C(0x41F0000000000000);
1639 // Binary encoding of 2^31 for type double.
1640 const int64_t c2 = INT64_C(0x41E0000000000000);
1641
1642 // low = low - 2^31 (to prevent bit 31 of `low` to be
1643 // interpreted as a sign bit)
1644 __ subl(low, Immediate(0x80000000));
1645 // temp = int-to-double(high)
1646 __ cvtsi2sd(temp, high);
1647 // temp = temp * 2^32
1648 __ LoadLongConstant(constant, c1);
1649 __ mulsd(temp, constant);
1650 // result = int-to-double(low)
1651 __ cvtsi2sd(result, low);
1652 // result = result + 2^31 (restore the original value of `low`)
1653 __ LoadLongConstant(constant, c2);
1654 __ addsd(result, constant);
1655 // result = result + temp
1656 __ addsd(result, temp);
1657 break;
1658 }
1659
Roland Levillaincff13742014-11-17 14:32:17 +00001660 case Primitive::kPrimFloat:
1661 LOG(FATAL) << "Type conversion from " << input_type
1662 << " to " << result_type << " not yet implemented";
1663 break;
1664
1665 default:
1666 LOG(FATAL) << "Unexpected type conversion from " << input_type
1667 << " to " << result_type;
1668 };
Roland Levillaindff1f282014-11-05 14:15:05 +00001669 break;
1670
1671 default:
1672 LOG(FATAL) << "Unexpected type conversion from " << input_type
1673 << " to " << result_type;
1674 }
1675}
1676
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001677void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001678 LocationSummary* locations =
1679 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001680 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001681 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001682 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001683 locations->SetInAt(0, Location::RequiresRegister());
1684 locations->SetInAt(1, Location::Any());
1685 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001686 break;
1687 }
1688
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001689 case Primitive::kPrimFloat:
1690 case Primitive::kPrimDouble: {
1691 locations->SetInAt(0, Location::RequiresFpuRegister());
1692 locations->SetInAt(1, Location::Any());
1693 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001694 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001695 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001696
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001697 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001698 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1699 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001700 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001701}
1702
1703void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1704 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001705 Location first = locations->InAt(0);
1706 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001707 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001708 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001709 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001710 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001711 __ addl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001712 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001713 __ addl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001714 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001715 __ addl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001716 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001717 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001718 }
1719
1720 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001721 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001722 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1723 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001724 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001725 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1726 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001727 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001728 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001729 break;
1730 }
1731
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001732 case Primitive::kPrimFloat: {
1733 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001734 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001735 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001736 __ addss(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001737 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001738 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001739 }
1740
1741 case Primitive::kPrimDouble: {
1742 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001743 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001744 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001745 __ addsd(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001746 }
1747 break;
1748 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001749
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001750 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001751 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001752 }
1753}
1754
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001755void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001756 LocationSummary* locations =
1757 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001758 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001759 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001760 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001761 locations->SetInAt(0, Location::RequiresRegister());
1762 locations->SetInAt(1, Location::Any());
1763 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001764 break;
1765 }
Calin Juravle11351682014-10-23 15:38:15 +01001766 case Primitive::kPrimFloat:
1767 case Primitive::kPrimDouble: {
1768 locations->SetInAt(0, Location::RequiresFpuRegister());
1769 locations->SetInAt(1, Location::RequiresFpuRegister());
1770 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001771 break;
Calin Juravle11351682014-10-23 15:38:15 +01001772 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001773
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001774 default:
Calin Juravle11351682014-10-23 15:38:15 +01001775 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001776 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001777}
1778
1779void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1780 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001781 Location first = locations->InAt(0);
1782 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001783 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001784 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001785 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001786 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001787 __ subl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001788 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001789 __ subl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001790 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001791 __ subl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001792 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001793 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001794 }
1795
1796 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001797 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01001798 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1799 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001800 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001801 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001802 __ sbbl(first.AsRegisterPairHigh<Register>(),
1803 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001804 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001805 break;
1806 }
1807
Calin Juravle11351682014-10-23 15:38:15 +01001808 case Primitive::kPrimFloat: {
1809 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001810 break;
Calin Juravle11351682014-10-23 15:38:15 +01001811 }
1812
1813 case Primitive::kPrimDouble: {
1814 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1815 break;
1816 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001817
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001818 default:
Calin Juravle11351682014-10-23 15:38:15 +01001819 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001820 }
1821}
1822
Calin Juravle34bacdf2014-10-07 20:23:36 +01001823void LocationsBuilderX86::VisitMul(HMul* mul) {
1824 LocationSummary* locations =
1825 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1826 switch (mul->GetResultType()) {
1827 case Primitive::kPrimInt:
1828 locations->SetInAt(0, Location::RequiresRegister());
1829 locations->SetInAt(1, Location::Any());
1830 locations->SetOut(Location::SameAsFirstInput());
1831 break;
1832 case Primitive::kPrimLong: {
1833 locations->SetInAt(0, Location::RequiresRegister());
1834 // TODO: Currently this handles only stack operands:
1835 // - we don't have enough registers because we currently use Quick ABI.
1836 // - by the time we have a working register allocator we will probably change the ABI
1837 // and fix the above.
1838 // - we don't have a way yet to request operands on stack but the base line compiler
1839 // will leave the operands on the stack with Any().
1840 locations->SetInAt(1, Location::Any());
1841 locations->SetOut(Location::SameAsFirstInput());
1842 // Needed for imul on 32bits with 64bits output.
1843 locations->AddTemp(Location::RegisterLocation(EAX));
1844 locations->AddTemp(Location::RegisterLocation(EDX));
1845 break;
1846 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001847 case Primitive::kPrimFloat:
1848 case Primitive::kPrimDouble: {
1849 locations->SetInAt(0, Location::RequiresFpuRegister());
1850 locations->SetInAt(1, Location::RequiresFpuRegister());
1851 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001852 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001853 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001854
1855 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001856 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001857 }
1858}
1859
1860void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1861 LocationSummary* locations = mul->GetLocations();
1862 Location first = locations->InAt(0);
1863 Location second = locations->InAt(1);
1864 DCHECK(first.Equals(locations->Out()));
1865
1866 switch (mul->GetResultType()) {
1867 case Primitive::kPrimInt: {
1868 if (second.IsRegister()) {
1869 __ imull(first.As<Register>(), second.As<Register>());
1870 } else if (second.IsConstant()) {
1871 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1872 __ imull(first.As<Register>(), imm);
1873 } else {
1874 DCHECK(second.IsStackSlot());
1875 __ imull(first.As<Register>(), Address(ESP, second.GetStackIndex()));
1876 }
1877 break;
1878 }
1879
1880 case Primitive::kPrimLong: {
1881 DCHECK(second.IsDoubleStackSlot());
1882
1883 Register in1_hi = first.AsRegisterPairHigh<Register>();
1884 Register in1_lo = first.AsRegisterPairLow<Register>();
1885 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
1886 Address in2_lo(ESP, second.GetStackIndex());
1887 Register eax = locations->GetTemp(0).As<Register>();
1888 Register edx = locations->GetTemp(1).As<Register>();
1889
1890 DCHECK_EQ(EAX, eax);
1891 DCHECK_EQ(EDX, edx);
1892
1893 // input: in1 - 64 bits, in2 - 64 bits
1894 // output: in1
1895 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
1896 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
1897 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
1898
1899 __ movl(eax, in2_hi);
1900 // eax <- in1.lo * in2.hi
1901 __ imull(eax, in1_lo);
1902 // in1.hi <- in1.hi * in2.lo
1903 __ imull(in1_hi, in2_lo);
1904 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
1905 __ addl(in1_hi, eax);
1906 // move in1_lo to eax to prepare for double precision
1907 __ movl(eax, in1_lo);
1908 // edx:eax <- in1.lo * in2.lo
1909 __ mull(in2_lo);
1910 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
1911 __ addl(in1_hi, edx);
1912 // in1.lo <- (in1.lo * in2.lo)[31:0];
1913 __ movl(in1_lo, eax);
1914
1915 break;
1916 }
1917
Calin Juravleb5bfa962014-10-21 18:02:24 +01001918 case Primitive::kPrimFloat: {
1919 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001920 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001921 }
1922
1923 case Primitive::kPrimDouble: {
1924 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1925 break;
1926 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001927
1928 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001929 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001930 }
1931}
1932
Calin Juravlebacfec32014-11-14 15:54:36 +00001933void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
1934 DCHECK(instruction->IsDiv() || instruction->IsRem());
1935
1936 LocationSummary* locations = instruction->GetLocations();
1937 Location out = locations->Out();
1938 Location first = locations->InAt(0);
1939 Location second = locations->InAt(1);
1940 bool is_div = instruction->IsDiv();
1941
1942 switch (instruction->GetResultType()) {
1943 case Primitive::kPrimInt: {
1944 Register second_reg = second.As<Register>();
1945 DCHECK_EQ(EAX, first.As<Register>());
1946 DCHECK_EQ(is_div ? EAX : EDX, out.As<Register>());
1947
1948 SlowPathCodeX86* slow_path =
1949 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.As<Register>(), is_div);
1950 codegen_->AddSlowPath(slow_path);
1951
1952 // 0x80000000/-1 triggers an arithmetic exception!
1953 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
1954 // it's safe to just use negl instead of more complex comparisons.
1955
1956 __ cmpl(second_reg, Immediate(-1));
1957 __ j(kEqual, slow_path->GetEntryLabel());
1958
1959 // edx:eax <- sign-extended of eax
1960 __ cdq();
1961 // eax = quotient, edx = remainder
1962 __ idivl(second_reg);
1963
1964 __ Bind(slow_path->GetExitLabel());
1965 break;
1966 }
1967
1968 case Primitive::kPrimLong: {
1969 InvokeRuntimeCallingConvention calling_convention;
1970 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
1971 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
1972 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
1973 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
1974 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
1975 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
1976
1977 if (is_div) {
1978 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLdiv)));
1979 } else {
1980 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLmod)));
1981 }
1982 uint32_t dex_pc = is_div
1983 ? instruction->AsDiv()->GetDexPc()
1984 : instruction->AsRem()->GetDexPc();
1985 codegen_->RecordPcInfo(instruction, dex_pc);
1986
1987 break;
1988 }
1989
1990 default:
1991 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
1992 }
1993}
1994
Calin Juravle7c4954d2014-10-28 16:57:40 +00001995void LocationsBuilderX86::VisitDiv(HDiv* div) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001996 LocationSummary::CallKind call_kind = div->GetResultType() == Primitive::kPrimLong
1997 ? LocationSummary::kCall
1998 : LocationSummary::kNoCall;
1999 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2000
Calin Juravle7c4954d2014-10-28 16:57:40 +00002001 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002002 case Primitive::kPrimInt: {
2003 locations->SetInAt(0, Location::RegisterLocation(EAX));
2004 locations->SetInAt(1, Location::RequiresRegister());
2005 locations->SetOut(Location::SameAsFirstInput());
2006 // Intel uses edx:eax as the dividend.
2007 locations->AddTemp(Location::RegisterLocation(EDX));
2008 break;
2009 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002010 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002011 InvokeRuntimeCallingConvention calling_convention;
2012 locations->SetInAt(0, Location::RegisterPairLocation(
2013 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2014 locations->SetInAt(1, Location::RegisterPairLocation(
2015 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2016 // Runtime helper puts the result in EAX, EDX.
2017 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002018 break;
2019 }
2020 case Primitive::kPrimFloat:
2021 case Primitive::kPrimDouble: {
2022 locations->SetInAt(0, Location::RequiresFpuRegister());
2023 locations->SetInAt(1, Location::RequiresFpuRegister());
2024 locations->SetOut(Location::SameAsFirstInput());
2025 break;
2026 }
2027
2028 default:
2029 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2030 }
2031}
2032
2033void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
2034 LocationSummary* locations = div->GetLocations();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002035 Location out = locations->Out();
Calin Juravle7c4954d2014-10-28 16:57:40 +00002036 Location first = locations->InAt(0);
2037 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002038
2039 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00002040 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00002041 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00002042 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00002043 break;
2044 }
2045
2046 case Primitive::kPrimFloat: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002047 DCHECK(first.Equals(out));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002048 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
2049 break;
2050 }
2051
2052 case Primitive::kPrimDouble: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002053 DCHECK(first.Equals(out));
Calin Juravle7c4954d2014-10-28 16:57:40 +00002054 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
2055 break;
2056 }
2057
2058 default:
2059 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2060 }
2061}
2062
Calin Juravlebacfec32014-11-14 15:54:36 +00002063void LocationsBuilderX86::VisitRem(HRem* rem) {
2064 LocationSummary::CallKind call_kind = rem->GetResultType() == Primitive::kPrimLong
2065 ? LocationSummary::kCall
2066 : LocationSummary::kNoCall;
2067 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
2068
2069 switch (rem->GetResultType()) {
2070 case Primitive::kPrimInt: {
2071 locations->SetInAt(0, Location::RegisterLocation(EAX));
2072 locations->SetInAt(1, Location::RequiresRegister());
2073 locations->SetOut(Location::RegisterLocation(EDX));
2074 break;
2075 }
2076 case Primitive::kPrimLong: {
2077 InvokeRuntimeCallingConvention calling_convention;
2078 locations->SetInAt(0, Location::RegisterPairLocation(
2079 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2080 locations->SetInAt(1, Location::RegisterPairLocation(
2081 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2082 // Runtime helper puts the result in EAX, EDX.
2083 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2084 break;
2085 }
2086 case Primitive::kPrimFloat:
2087 case Primitive::kPrimDouble: {
2088 LOG(FATAL) << "Unimplemented rem type " << rem->GetResultType();
2089 break;
2090 }
2091
2092 default:
2093 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
2094 }
2095}
2096
2097void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
2098 Primitive::Type type = rem->GetResultType();
2099 switch (type) {
2100 case Primitive::kPrimInt:
2101 case Primitive::kPrimLong: {
2102 GenerateDivRemIntegral(rem);
2103 break;
2104 }
2105 case Primitive::kPrimFloat:
2106 case Primitive::kPrimDouble: {
2107 LOG(FATAL) << "Unimplemented rem type " << type;
2108 break;
2109 }
2110 default:
2111 LOG(FATAL) << "Unexpected rem type " << type;
2112 }
2113}
2114
Calin Juravled0d48522014-11-04 16:40:20 +00002115void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2116 LocationSummary* locations =
2117 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002118 switch (instruction->GetType()) {
2119 case Primitive::kPrimInt: {
2120 locations->SetInAt(0, Location::Any());
2121 break;
2122 }
2123 case Primitive::kPrimLong: {
2124 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2125 if (!instruction->IsConstant()) {
2126 locations->AddTemp(Location::RequiresRegister());
2127 }
2128 break;
2129 }
2130 default:
2131 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
2132 }
Calin Juravled0d48522014-11-04 16:40:20 +00002133 if (instruction->HasUses()) {
2134 locations->SetOut(Location::SameAsFirstInput());
2135 }
2136}
2137
2138void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2139 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
2140 codegen_->AddSlowPath(slow_path);
2141
2142 LocationSummary* locations = instruction->GetLocations();
2143 Location value = locations->InAt(0);
2144
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002145 switch (instruction->GetType()) {
2146 case Primitive::kPrimInt: {
2147 if (value.IsRegister()) {
2148 __ testl(value.As<Register>(), value.As<Register>());
2149 __ j(kEqual, slow_path->GetEntryLabel());
2150 } else if (value.IsStackSlot()) {
2151 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
2152 __ j(kEqual, slow_path->GetEntryLabel());
2153 } else {
2154 DCHECK(value.IsConstant()) << value;
2155 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2156 __ jmp(slow_path->GetEntryLabel());
2157 }
2158 }
2159 break;
Calin Juravled0d48522014-11-04 16:40:20 +00002160 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002161 case Primitive::kPrimLong: {
2162 if (value.IsRegisterPair()) {
2163 Register temp = locations->GetTemp(0).As<Register>();
2164 __ movl(temp, value.AsRegisterPairLow<Register>());
2165 __ orl(temp, value.AsRegisterPairHigh<Register>());
2166 __ j(kEqual, slow_path->GetEntryLabel());
2167 } else {
2168 DCHECK(value.IsConstant()) << value;
2169 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2170 __ jmp(slow_path->GetEntryLabel());
2171 }
2172 }
2173 break;
2174 }
2175 default:
2176 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00002177 }
Calin Juravled0d48522014-11-04 16:40:20 +00002178}
2179
Calin Juravle9aec02f2014-11-18 23:06:35 +00002180void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
2181 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2182
2183 LocationSummary* locations =
2184 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
2185
2186 switch (op->GetResultType()) {
2187 case Primitive::kPrimInt: {
2188 locations->SetInAt(0, Location::RequiresRegister());
2189 // The shift count needs to be in CL.
2190 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
2191 locations->SetOut(Location::SameAsFirstInput());
2192 break;
2193 }
2194 case Primitive::kPrimLong: {
2195 locations->SetInAt(0, Location::RequiresRegister());
2196 // The shift count needs to be in CL.
2197 locations->SetInAt(1, Location::RegisterLocation(ECX));
2198 locations->SetOut(Location::SameAsFirstInput());
2199 break;
2200 }
2201 default:
2202 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2203 }
2204}
2205
2206void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
2207 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
2208
2209 LocationSummary* locations = op->GetLocations();
2210 Location first = locations->InAt(0);
2211 Location second = locations->InAt(1);
2212 DCHECK(first.Equals(locations->Out()));
2213
2214 switch (op->GetResultType()) {
2215 case Primitive::kPrimInt: {
2216 Register first_reg = first.As<Register>();
2217 if (second.IsRegister()) {
2218 Register second_reg = second.As<Register>();
2219 DCHECK_EQ(ECX, second_reg);
2220 if (op->IsShl()) {
2221 __ shll(first_reg, second_reg);
2222 } else if (op->IsShr()) {
2223 __ sarl(first_reg, second_reg);
2224 } else {
2225 __ shrl(first_reg, second_reg);
2226 }
2227 } else {
2228 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
2229 if (op->IsShl()) {
2230 __ shll(first_reg, imm);
2231 } else if (op->IsShr()) {
2232 __ sarl(first_reg, imm);
2233 } else {
2234 __ shrl(first_reg, imm);
2235 }
2236 }
2237 break;
2238 }
2239 case Primitive::kPrimLong: {
2240 Register second_reg = second.As<Register>();
2241 DCHECK_EQ(ECX, second_reg);
2242 if (op->IsShl()) {
2243 GenerateShlLong(first, second_reg);
2244 } else if (op->IsShr()) {
2245 GenerateShrLong(first, second_reg);
2246 } else {
2247 GenerateUShrLong(first, second_reg);
2248 }
2249 break;
2250 }
2251 default:
2252 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
2253 }
2254}
2255
2256void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
2257 Label done;
2258 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
2259 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
2260 __ testl(shifter, Immediate(32));
2261 __ j(kEqual, &done);
2262 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
2263 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
2264 __ Bind(&done);
2265}
2266
2267void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
2268 Label done;
2269 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2270 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
2271 __ testl(shifter, Immediate(32));
2272 __ j(kEqual, &done);
2273 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2274 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
2275 __ Bind(&done);
2276}
2277
2278void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
2279 Label done;
2280 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
2281 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
2282 __ testl(shifter, Immediate(32));
2283 __ j(kEqual, &done);
2284 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
2285 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
2286 __ Bind(&done);
2287}
2288
2289void LocationsBuilderX86::VisitShl(HShl* shl) {
2290 HandleShift(shl);
2291}
2292
2293void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
2294 HandleShift(shl);
2295}
2296
2297void LocationsBuilderX86::VisitShr(HShr* shr) {
2298 HandleShift(shr);
2299}
2300
2301void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
2302 HandleShift(shr);
2303}
2304
2305void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
2306 HandleShift(ushr);
2307}
2308
2309void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
2310 HandleShift(ushr);
2311}
2312
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002313void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002314 LocationSummary* locations =
2315 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002316 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002317 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002318 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2319 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002320}
2321
2322void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
2323 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002324 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002325 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002326
Nicolas Geoffray707c8092014-04-04 10:50:14 +01002327 __ fs()->call(
2328 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002329
Nicolas Geoffray39468442014-09-02 15:17:15 +01002330 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002331 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002332}
2333
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002334void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
2335 LocationSummary* locations =
2336 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2337 locations->SetOut(Location::RegisterLocation(EAX));
2338 InvokeRuntimeCallingConvention calling_convention;
2339 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2340 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2341 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2342}
2343
2344void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
2345 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002346 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002347 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
2348
2349 __ fs()->call(
2350 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
2351
2352 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2353 DCHECK(!codegen_->IsLeafMethod());
2354}
2355
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002356void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002357 LocationSummary* locations =
2358 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002359 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2360 if (location.IsStackSlot()) {
2361 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2362 } else if (location.IsDoubleStackSlot()) {
2363 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002364 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002365 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002366}
2367
2368void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002369 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002370}
2371
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002372void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002373 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002374 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002375 locations->SetInAt(0, Location::RequiresRegister());
2376 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002377}
2378
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002379void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
2380 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01002381 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002382 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01002383 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002384 switch (not_->InputAt(0)->GetType()) {
2385 case Primitive::kPrimBoolean:
2386 __ xorl(out.As<Register>(), Immediate(1));
2387 break;
2388
2389 case Primitive::kPrimInt:
2390 __ notl(out.As<Register>());
2391 break;
2392
2393 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01002394 __ notl(out.AsRegisterPairLow<Register>());
2395 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002396 break;
2397
2398 default:
2399 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
2400 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002401}
2402
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002403void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002404 LocationSummary* locations =
2405 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00002406 switch (compare->InputAt(0)->GetType()) {
2407 case Primitive::kPrimLong: {
2408 locations->SetInAt(0, Location::RequiresRegister());
2409 // TODO: we set any here but we don't handle constants
2410 locations->SetInAt(1, Location::Any());
2411 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2412 break;
2413 }
2414 case Primitive::kPrimFloat:
2415 case Primitive::kPrimDouble: {
2416 locations->SetInAt(0, Location::RequiresFpuRegister());
2417 locations->SetInAt(1, Location::RequiresFpuRegister());
2418 locations->SetOut(Location::RequiresRegister());
2419 break;
2420 }
2421 default:
2422 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
2423 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002424}
2425
2426void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002427 LocationSummary* locations = compare->GetLocations();
Calin Juravleddb7df22014-11-25 20:56:51 +00002428 Register out = locations->Out().As<Register>();
2429 Location left = locations->InAt(0);
2430 Location right = locations->InAt(1);
2431
2432 Label less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002433 switch (compare->InputAt(0)->GetType()) {
2434 case Primitive::kPrimLong: {
Calin Juravleddb7df22014-11-25 20:56:51 +00002435 if (right.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002436 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002437 } else {
2438 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002439 __ cmpl(left.AsRegisterPairHigh<Register>(),
2440 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002441 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002442 __ j(kLess, &less); // Signed compare.
2443 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002444 if (right.IsRegisterPair()) {
2445 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002446 } else {
2447 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002448 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002449 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002450 break;
2451 }
2452 case Primitive::kPrimFloat: {
2453 __ ucomiss(left.As<XmmRegister>(), right.As<XmmRegister>());
2454 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
2455 break;
2456 }
2457 case Primitive::kPrimDouble: {
2458 __ ucomisd(left.As<XmmRegister>(), right.As<XmmRegister>());
2459 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002460 break;
2461 }
2462 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00002463 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002464 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002465 __ movl(out, Immediate(0));
2466 __ j(kEqual, &done);
2467 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
2468
2469 __ Bind(&greater);
2470 __ movl(out, Immediate(1));
2471 __ jmp(&done);
2472
2473 __ Bind(&less);
2474 __ movl(out, Immediate(-1));
2475
2476 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002477}
2478
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002479void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002480 LocationSummary* locations =
2481 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01002482 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2483 locations->SetInAt(i, Location::Any());
2484 }
2485 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002486}
2487
2488void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002489 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002490 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002491}
2492
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002493void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002494 LocationSummary* locations =
2495 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002496 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002497 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002498 bool needs_write_barrier =
2499 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
2500
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002501 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2502 || (field_type == Primitive::kPrimByte);
2503 // The register allocator does not support multiple
2504 // inputs that die at entry with one in a specific register.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002505 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002506 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002507 locations->SetInAt(1, Location::RegisterLocation(EAX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002508 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002509 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002510 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002511 // Temporary registers for the write barrier.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002512 if (needs_write_barrier) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002513 locations->AddTemp(Location::RequiresRegister());
2514 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002515 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01002516 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002517}
2518
2519void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2520 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002521 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002522 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01002523 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002524
2525 switch (field_type) {
2526 case Primitive::kPrimBoolean:
2527 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002528 ByteRegister value = locations->InAt(1).As<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002529 __ movb(Address(obj, offset), value);
2530 break;
2531 }
2532
2533 case Primitive::kPrimShort:
2534 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002535 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002536 __ movw(Address(obj, offset), value);
2537 break;
2538 }
2539
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002540 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002541 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002542 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002543 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002544
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002545 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002546 Register temp = locations->GetTemp(0).As<Register>();
2547 Register card = locations->GetTemp(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002548 codegen_->MarkGCCard(temp, card, obj, value);
2549 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002550 break;
2551 }
2552
2553 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002554 Location value = locations->InAt(1);
2555 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
2556 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002557 break;
2558 }
2559
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002560 case Primitive::kPrimFloat: {
2561 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2562 __ movss(Address(obj, offset), value);
2563 break;
2564 }
2565
2566 case Primitive::kPrimDouble: {
2567 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2568 __ movsd(Address(obj, offset), value);
2569 break;
2570 }
2571
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002572 case Primitive::kPrimVoid:
2573 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002574 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002575 }
2576}
2577
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002578void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
2579 Label is_null;
2580 __ testl(value, value);
2581 __ j(kEqual, &is_null);
2582 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2583 __ movl(temp, object);
2584 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
2585 __ movb(Address(temp, card, TIMES_1, 0),
2586 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
2587 __ Bind(&is_null);
2588}
2589
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002590void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002591 LocationSummary* locations =
2592 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002593 locations->SetInAt(0, Location::RequiresRegister());
2594 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002595}
2596
2597void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2598 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002599 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002600 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2601
2602 switch (instruction->GetType()) {
2603 case Primitive::kPrimBoolean: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002604 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002605 __ movzxb(out, Address(obj, offset));
2606 break;
2607 }
2608
2609 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002610 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002611 __ movsxb(out, Address(obj, offset));
2612 break;
2613 }
2614
2615 case Primitive::kPrimShort: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002616 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002617 __ movsxw(out, Address(obj, offset));
2618 break;
2619 }
2620
2621 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002622 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002623 __ movzxw(out, Address(obj, offset));
2624 break;
2625 }
2626
2627 case Primitive::kPrimInt:
2628 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002629 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002630 __ movl(out, Address(obj, offset));
2631 break;
2632 }
2633
2634 case Primitive::kPrimLong: {
2635 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002636 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
2637 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002638 break;
2639 }
2640
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002641 case Primitive::kPrimFloat: {
2642 XmmRegister out = locations->Out().As<XmmRegister>();
2643 __ movss(out, Address(obj, offset));
2644 break;
2645 }
2646
2647 case Primitive::kPrimDouble: {
2648 XmmRegister out = locations->Out().As<XmmRegister>();
2649 __ movsd(out, Address(obj, offset));
2650 break;
2651 }
2652
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002653 case Primitive::kPrimVoid:
2654 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002655 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002656 }
2657}
2658
2659void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002660 LocationSummary* locations =
2661 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002662 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002663 if (instruction->HasUses()) {
2664 locations->SetOut(Location::SameAsFirstInput());
2665 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002666}
2667
2668void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002669 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002670 codegen_->AddSlowPath(slow_path);
2671
2672 LocationSummary* locations = instruction->GetLocations();
2673 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002674
2675 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002676 __ cmpl(obj.As<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002677 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002678 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002679 } else {
2680 DCHECK(obj.IsConstant()) << obj;
2681 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2682 __ jmp(slow_path->GetEntryLabel());
2683 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002684 }
2685 __ j(kEqual, slow_path->GetEntryLabel());
2686}
2687
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002688void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002689 LocationSummary* locations =
2690 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002691 locations->SetInAt(0, Location::RequiresRegister());
2692 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2693 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002694}
2695
2696void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
2697 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002698 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002699 Location index = locations->InAt(1);
2700
2701 switch (instruction->GetType()) {
2702 case Primitive::kPrimBoolean: {
2703 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002704 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002705 if (index.IsConstant()) {
2706 __ movzxb(out, Address(obj,
2707 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2708 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002709 __ movzxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002710 }
2711 break;
2712 }
2713
2714 case Primitive::kPrimByte: {
2715 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002716 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002717 if (index.IsConstant()) {
2718 __ movsxb(out, Address(obj,
2719 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2720 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002721 __ movsxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002722 }
2723 break;
2724 }
2725
2726 case Primitive::kPrimShort: {
2727 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002728 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002729 if (index.IsConstant()) {
2730 __ movsxw(out, Address(obj,
2731 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2732 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002733 __ movsxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002734 }
2735 break;
2736 }
2737
2738 case Primitive::kPrimChar: {
2739 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002740 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002741 if (index.IsConstant()) {
2742 __ movzxw(out, Address(obj,
2743 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2744 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002745 __ movzxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002746 }
2747 break;
2748 }
2749
2750 case Primitive::kPrimInt:
2751 case Primitive::kPrimNot: {
2752 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002753 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002754 if (index.IsConstant()) {
2755 __ movl(out, Address(obj,
2756 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2757 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002758 __ movl(out, Address(obj, index.As<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002759 }
2760 break;
2761 }
2762
2763 case Primitive::kPrimLong: {
2764 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002765 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002766 if (index.IsConstant()) {
2767 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002768 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
2769 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002770 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002771 __ movl(out.AsRegisterPairLow<Register>(),
2772 Address(obj, index.As<Register>(), TIMES_8, data_offset));
2773 __ movl(out.AsRegisterPairHigh<Register>(),
2774 Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002775 }
2776 break;
2777 }
2778
2779 case Primitive::kPrimFloat:
2780 case Primitive::kPrimDouble:
2781 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002782 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002783 case Primitive::kPrimVoid:
2784 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002785 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002786 }
2787}
2788
2789void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002790 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002791 bool needs_write_barrier =
2792 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2793
2794 DCHECK(kFollowsQuickABI);
2795 bool not_enough_registers = needs_write_barrier
2796 && !instruction->GetValue()->IsConstant()
2797 && !instruction->GetIndex()->IsConstant();
2798 bool needs_runtime_call = instruction->NeedsTypeCheck() || not_enough_registers;
2799
Nicolas Geoffray39468442014-09-02 15:17:15 +01002800 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2801 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002802 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002803
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002804 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002805 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002806 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2807 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2808 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002809 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002810 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
2811 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002812 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002813 // In case of a byte operation, the register allocator does not support multiple
2814 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002815 locations->SetInAt(0, Location::RequiresRegister());
2816 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002817 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002818 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002819 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002820 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002821 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002822 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002823 // Temporary registers for the write barrier.
2824 if (needs_write_barrier) {
2825 locations->AddTemp(Location::RequiresRegister());
2826 // Ensure the card is in a byte register.
2827 locations->AddTemp(Location::RegisterLocation(ECX));
2828 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002829 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002830}
2831
2832void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
2833 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002834 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002835 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002836 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002837 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002838 bool needs_runtime_call = locations->WillCall();
2839 bool needs_write_barrier =
2840 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002841
2842 switch (value_type) {
2843 case Primitive::kPrimBoolean:
2844 case Primitive::kPrimByte: {
2845 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002846 if (index.IsConstant()) {
2847 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002848 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002849 __ movb(Address(obj, offset), value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002850 } else {
2851 __ movb(Address(obj, offset),
2852 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2853 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002854 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002855 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002856 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
2857 value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002858 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002859 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002860 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2861 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002862 }
2863 break;
2864 }
2865
2866 case Primitive::kPrimShort:
2867 case Primitive::kPrimChar: {
2868 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002869 if (index.IsConstant()) {
2870 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002871 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002872 __ movw(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002873 } else {
2874 __ movw(Address(obj, offset),
2875 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2876 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002877 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002878 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002879 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
2880 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002881 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002882 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002883 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2884 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002885 }
2886 break;
2887 }
2888
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002889 case Primitive::kPrimInt:
2890 case Primitive::kPrimNot: {
2891 if (!needs_runtime_call) {
2892 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2893 if (index.IsConstant()) {
2894 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2895 if (value.IsRegister()) {
2896 __ movl(Address(obj, offset), value.As<Register>());
2897 } else {
2898 DCHECK(value.IsConstant()) << value;
2899 __ movl(Address(obj, offset),
2900 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2901 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002902 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002903 DCHECK(index.IsRegister()) << index;
2904 if (value.IsRegister()) {
2905 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
2906 value.As<Register>());
2907 } else {
2908 DCHECK(value.IsConstant()) << value;
2909 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
2910 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2911 }
2912 }
2913
2914 if (needs_write_barrier) {
2915 Register temp = locations->GetTemp(0).As<Register>();
2916 Register card = locations->GetTemp(1).As<Register>();
2917 codegen_->MarkGCCard(temp, card, obj, value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002918 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002919 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002920 DCHECK_EQ(value_type, Primitive::kPrimNot);
2921 DCHECK(!codegen_->IsLeafMethod());
2922 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
2923 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002924 }
2925 break;
2926 }
2927
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002928 case Primitive::kPrimLong: {
2929 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002930 if (index.IsConstant()) {
2931 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002932 if (value.IsRegisterPair()) {
2933 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
2934 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002935 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002936 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002937 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
2938 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
2939 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
2940 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002941 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002942 if (value.IsRegisterPair()) {
2943 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
2944 value.AsRegisterPairLow<Register>());
2945 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
2946 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002947 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002948 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002949 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002950 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002951 Immediate(Low32Bits(val)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002952 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002953 Immediate(High32Bits(val)));
2954 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002955 }
2956 break;
2957 }
2958
2959 case Primitive::kPrimFloat:
2960 case Primitive::kPrimDouble:
2961 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002962 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002963 case Primitive::kPrimVoid:
2964 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002965 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002966 }
2967}
2968
2969void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
2970 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002971 locations->SetInAt(0, Location::RequiresRegister());
2972 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002973 instruction->SetLocations(locations);
2974}
2975
2976void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
2977 LocationSummary* locations = instruction->GetLocations();
2978 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002979 Register obj = locations->InAt(0).As<Register>();
2980 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002981 __ movl(out, Address(obj, offset));
2982}
2983
2984void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002985 LocationSummary* locations =
2986 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002987 locations->SetInAt(0, Location::RequiresRegister());
2988 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002989 if (instruction->HasUses()) {
2990 locations->SetOut(Location::SameAsFirstInput());
2991 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002992}
2993
2994void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
2995 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002996 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002997 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002998 codegen_->AddSlowPath(slow_path);
2999
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003000 Register index = locations->InAt(0).As<Register>();
3001 Register length = locations->InAt(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003002
3003 __ cmpl(index, length);
3004 __ j(kAboveEqual, slow_path->GetEntryLabel());
3005}
3006
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003007void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
3008 temp->SetLocations(nullptr);
3009}
3010
3011void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
3012 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003013 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003014}
3015
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003016void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003017 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003018 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003019}
3020
3021void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003022 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3023}
3024
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003025void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
3026 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3027}
3028
3029void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003030 HBasicBlock* block = instruction->GetBlock();
3031 if (block->GetLoopInformation() != nullptr) {
3032 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3033 // The back edge will generate the suspend check.
3034 return;
3035 }
3036 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3037 // The goto will generate the suspend check.
3038 return;
3039 }
3040 GenerateSuspendCheck(instruction, nullptr);
3041}
3042
3043void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
3044 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003045 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003046 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003047 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003048 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003049 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01003050 if (successor == nullptr) {
3051 __ j(kNotEqual, slow_path->GetEntryLabel());
3052 __ Bind(slow_path->GetReturnLabel());
3053 } else {
3054 __ j(kEqual, codegen_->GetLabelOf(successor));
3055 __ jmp(slow_path->GetEntryLabel());
3056 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003057}
3058
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003059X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
3060 return codegen_->GetAssembler();
3061}
3062
3063void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
3064 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003065 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003066 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3067 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
3068 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
3069}
3070
3071void ParallelMoveResolverX86::EmitMove(size_t index) {
3072 MoveOperands* move = moves_.Get(index);
3073 Location source = move->GetSource();
3074 Location destination = move->GetDestination();
3075
3076 if (source.IsRegister()) {
3077 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003078 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003079 } else {
3080 DCHECK(destination.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003081 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003082 }
3083 } else if (source.IsStackSlot()) {
3084 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003085 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003086 } else {
3087 DCHECK(destination.IsStackSlot());
3088 MoveMemoryToMemory(destination.GetStackIndex(),
3089 source.GetStackIndex());
3090 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003091 } else if (source.IsConstant()) {
3092 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
3093 Immediate imm(instruction->AsIntConstant()->GetValue());
3094 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003095 __ movl(destination.As<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003096 } else {
3097 __ movl(Address(ESP, destination.GetStackIndex()), imm);
3098 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003099 } else {
3100 LOG(FATAL) << "Unimplemented";
3101 }
3102}
3103
3104void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003105 Register suggested_scratch = reg == EAX ? EBX : EAX;
3106 ScratchRegisterScope ensure_scratch(
3107 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3108
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003109 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
3110 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
3111 __ movl(Address(ESP, mem + stack_offset), reg);
3112 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
3113}
3114
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003115void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
3116 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003117 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
3118
3119 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003120 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003121 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
3122
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003123 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
3124 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
3125 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
3126 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
3127 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
3128 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
3129}
3130
3131void ParallelMoveResolverX86::EmitSwap(size_t index) {
3132 MoveOperands* move = moves_.Get(index);
3133 Location source = move->GetSource();
3134 Location destination = move->GetDestination();
3135
3136 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003137 __ xchgl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003138 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003139 Exchange(source.As<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003140 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003141 Exchange(destination.As<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003142 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
3143 Exchange(destination.GetStackIndex(), source.GetStackIndex());
3144 } else {
3145 LOG(FATAL) << "Unimplemented";
3146 }
3147}
3148
3149void ParallelMoveResolverX86::SpillScratch(int reg) {
3150 __ pushl(static_cast<Register>(reg));
3151}
3152
3153void ParallelMoveResolverX86::RestoreScratch(int reg) {
3154 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003155}
3156
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003157void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003158 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
3159 ? LocationSummary::kCallOnSlowPath
3160 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003161 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003162 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003163 locations->SetOut(Location::RequiresRegister());
3164}
3165
3166void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
3167 Register out = cls->GetLocations()->Out().As<Register>();
3168 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003169 DCHECK(!cls->CanCallRuntime());
3170 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003171 codegen_->LoadCurrentMethod(out);
3172 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3173 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003174 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003175 codegen_->LoadCurrentMethod(out);
3176 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
3177 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003178
3179 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3180 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3181 codegen_->AddSlowPath(slow_path);
3182 __ testl(out, out);
3183 __ j(kEqual, slow_path->GetEntryLabel());
3184 if (cls->MustGenerateClinitCheck()) {
3185 GenerateClassInitializationCheck(slow_path, out);
3186 } else {
3187 __ Bind(slow_path->GetExitLabel());
3188 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003189 }
3190}
3191
3192void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
3193 LocationSummary* locations =
3194 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3195 locations->SetInAt(0, Location::RequiresRegister());
3196 if (check->HasUses()) {
3197 locations->SetOut(Location::SameAsFirstInput());
3198 }
3199}
3200
3201void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003202 // We assume the class to not be null.
3203 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
3204 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003205 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003206 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<Register>());
3207}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003208
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003209void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
3210 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003211 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
3212 Immediate(mirror::Class::kStatusInitialized));
3213 __ j(kLess, slow_path->GetEntryLabel());
3214 __ Bind(slow_path->GetExitLabel());
3215 // No need for memory fence, thanks to the X86 memory model.
3216}
3217
3218void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3219 LocationSummary* locations =
3220 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3221 locations->SetInAt(0, Location::RequiresRegister());
3222 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3223}
3224
3225void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3226 LocationSummary* locations = instruction->GetLocations();
3227 Register cls = locations->InAt(0).As<Register>();
3228 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
3229
3230 switch (instruction->GetType()) {
3231 case Primitive::kPrimBoolean: {
3232 Register out = locations->Out().As<Register>();
3233 __ movzxb(out, Address(cls, offset));
3234 break;
3235 }
3236
3237 case Primitive::kPrimByte: {
3238 Register out = locations->Out().As<Register>();
3239 __ movsxb(out, Address(cls, offset));
3240 break;
3241 }
3242
3243 case Primitive::kPrimShort: {
3244 Register out = locations->Out().As<Register>();
3245 __ movsxw(out, Address(cls, offset));
3246 break;
3247 }
3248
3249 case Primitive::kPrimChar: {
3250 Register out = locations->Out().As<Register>();
3251 __ movzxw(out, Address(cls, offset));
3252 break;
3253 }
3254
3255 case Primitive::kPrimInt:
3256 case Primitive::kPrimNot: {
3257 Register out = locations->Out().As<Register>();
3258 __ movl(out, Address(cls, offset));
3259 break;
3260 }
3261
3262 case Primitive::kPrimLong: {
3263 // TODO: support volatile.
3264 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(cls, offset));
3265 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(cls, kX86WordSize + offset));
3266 break;
3267 }
3268
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003269 case Primitive::kPrimFloat: {
3270 XmmRegister out = locations->Out().As<XmmRegister>();
3271 __ movss(out, Address(cls, offset));
3272 break;
3273 }
3274
3275 case Primitive::kPrimDouble: {
3276 XmmRegister out = locations->Out().As<XmmRegister>();
3277 __ movsd(out, Address(cls, offset));
3278 break;
3279 }
3280
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003281 case Primitive::kPrimVoid:
3282 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3283 UNREACHABLE();
3284 }
3285}
3286
3287void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3288 LocationSummary* locations =
3289 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3290 locations->SetInAt(0, Location::RequiresRegister());
3291 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003292 bool needs_write_barrier =
3293 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003294 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3295 || (field_type == Primitive::kPrimByte);
3296 // The register allocator does not support multiple
3297 // inputs that die at entry with one in a specific register.
3298 if (is_byte_type) {
3299 // Ensure the value is in a byte register.
3300 locations->SetInAt(1, Location::RegisterLocation(EAX));
3301 } else {
3302 locations->SetInAt(1, Location::RequiresRegister());
3303 }
3304 // Temporary registers for the write barrier.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003305 if (needs_write_barrier) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003306 locations->AddTemp(Location::RequiresRegister());
3307 // Ensure the card is in a byte register.
3308 locations->AddTemp(Location::RegisterLocation(ECX));
3309 }
3310}
3311
3312void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3313 LocationSummary* locations = instruction->GetLocations();
3314 Register cls = locations->InAt(0).As<Register>();
3315 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
3316 Primitive::Type field_type = instruction->GetFieldType();
3317
3318 switch (field_type) {
3319 case Primitive::kPrimBoolean:
3320 case Primitive::kPrimByte: {
3321 ByteRegister value = locations->InAt(1).As<ByteRegister>();
3322 __ movb(Address(cls, offset), value);
3323 break;
3324 }
3325
3326 case Primitive::kPrimShort:
3327 case Primitive::kPrimChar: {
3328 Register value = locations->InAt(1).As<Register>();
3329 __ movw(Address(cls, offset), value);
3330 break;
3331 }
3332
3333 case Primitive::kPrimInt:
3334 case Primitive::kPrimNot: {
3335 Register value = locations->InAt(1).As<Register>();
3336 __ movl(Address(cls, offset), value);
3337
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003338 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003339 Register temp = locations->GetTemp(0).As<Register>();
3340 Register card = locations->GetTemp(1).As<Register>();
3341 codegen_->MarkGCCard(temp, card, cls, value);
3342 }
3343 break;
3344 }
3345
3346 case Primitive::kPrimLong: {
3347 Location value = locations->InAt(1);
3348 __ movl(Address(cls, offset), value.AsRegisterPairLow<Register>());
3349 __ movl(Address(cls, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
3350 break;
3351 }
3352
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003353 case Primitive::kPrimFloat: {
3354 XmmRegister value = locations->InAt(1).As<XmmRegister>();
3355 __ movss(Address(cls, offset), value);
3356 break;
3357 }
3358
3359 case Primitive::kPrimDouble: {
3360 XmmRegister value = locations->InAt(1).As<XmmRegister>();
3361 __ movsd(Address(cls, offset), value);
3362 break;
3363 }
3364
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003365 case Primitive::kPrimVoid:
3366 LOG(FATAL) << "Unreachable type " << field_type;
3367 UNREACHABLE();
3368 }
3369}
3370
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003371void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
3372 LocationSummary* locations =
3373 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3374 locations->SetOut(Location::RequiresRegister());
3375}
3376
3377void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
3378 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
3379 codegen_->AddSlowPath(slow_path);
3380
3381 Register out = load->GetLocations()->Out().As<Register>();
3382 codegen_->LoadCurrentMethod(out);
Mathieu Chartiereace4582014-11-24 18:29:54 -08003383 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
3384 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003385 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
3386 __ testl(out, out);
3387 __ j(kEqual, slow_path->GetEntryLabel());
3388 __ Bind(slow_path->GetExitLabel());
3389}
3390
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003391void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
3392 LocationSummary* locations =
3393 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3394 locations->SetOut(Location::RequiresRegister());
3395}
3396
3397void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
3398 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
3399 __ fs()->movl(load->GetLocations()->Out().As<Register>(), address);
3400 __ fs()->movl(address, Immediate(0));
3401}
3402
3403void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
3404 LocationSummary* locations =
3405 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3406 InvokeRuntimeCallingConvention calling_convention;
3407 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3408}
3409
3410void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
3411 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
3412 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3413}
3414
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003415void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003416 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
3417 ? LocationSummary::kNoCall
3418 : LocationSummary::kCallOnSlowPath;
3419 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3420 locations->SetInAt(0, Location::RequiresRegister());
3421 locations->SetInAt(1, Location::Any());
3422 locations->SetOut(Location::RequiresRegister());
3423}
3424
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003425void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003426 LocationSummary* locations = instruction->GetLocations();
3427 Register obj = locations->InAt(0).As<Register>();
3428 Location cls = locations->InAt(1);
3429 Register out = locations->Out().As<Register>();
3430 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3431 Label done, zero;
3432 SlowPathCodeX86* slow_path = nullptr;
3433
3434 // Return 0 if `obj` is null.
3435 // TODO: avoid this check if we know obj is not null.
3436 __ testl(obj, obj);
3437 __ j(kEqual, &zero);
3438 __ movl(out, Address(obj, class_offset));
3439 // Compare the class of `obj` with `cls`.
3440 if (cls.IsRegister()) {
3441 __ cmpl(out, cls.As<Register>());
3442 } else {
3443 DCHECK(cls.IsStackSlot()) << cls;
3444 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
3445 }
3446
3447 if (instruction->IsClassFinal()) {
3448 // Classes must be equal for the instanceof to succeed.
3449 __ j(kNotEqual, &zero);
3450 __ movl(out, Immediate(1));
3451 __ jmp(&done);
3452 } else {
3453 // If the classes are not equal, we go into a slow path.
3454 DCHECK(locations->OnlyCallsOnSlowPath());
3455 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003456 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003457 codegen_->AddSlowPath(slow_path);
3458 __ j(kNotEqual, slow_path->GetEntryLabel());
3459 __ movl(out, Immediate(1));
3460 __ jmp(&done);
3461 }
3462 __ Bind(&zero);
3463 __ movl(out, Immediate(0));
3464 if (slow_path != nullptr) {
3465 __ Bind(slow_path->GetExitLabel());
3466 }
3467 __ Bind(&done);
3468}
3469
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003470void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
3471 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
3472 instruction, LocationSummary::kCallOnSlowPath);
3473 locations->SetInAt(0, Location::RequiresRegister());
3474 locations->SetInAt(1, Location::Any());
3475 locations->AddTemp(Location::RequiresRegister());
3476}
3477
3478void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
3479 LocationSummary* locations = instruction->GetLocations();
3480 Register obj = locations->InAt(0).As<Register>();
3481 Location cls = locations->InAt(1);
3482 Register temp = locations->GetTemp(0).As<Register>();
3483 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3484 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
3485 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
3486 codegen_->AddSlowPath(slow_path);
3487
3488 // TODO: avoid this check if we know obj is not null.
3489 __ testl(obj, obj);
3490 __ j(kEqual, slow_path->GetExitLabel());
3491 __ movl(temp, Address(obj, class_offset));
3492
3493 // Compare the class of `obj` with `cls`.
3494 if (cls.IsRegister()) {
3495 __ cmpl(temp, cls.As<Register>());
3496 } else {
3497 DCHECK(cls.IsStackSlot()) << cls;
3498 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
3499 }
3500
3501 __ j(kNotEqual, slow_path->GetEntryLabel());
3502 __ Bind(slow_path->GetExitLabel());
3503}
3504
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003505void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3506 LocationSummary* locations =
3507 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3508 InvokeRuntimeCallingConvention calling_convention;
3509 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3510}
3511
3512void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
3513 __ fs()->call(Address::Absolute(instruction->IsEnter()
3514 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pLockObject)
3515 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pUnlockObject)));
3516 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3517}
3518
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003519void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
3520void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
3521void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
3522
3523void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3524 LocationSummary* locations =
3525 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3526 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
3527 || instruction->GetResultType() == Primitive::kPrimLong);
3528 locations->SetInAt(0, Location::RequiresRegister());
3529 locations->SetInAt(1, Location::Any());
3530 locations->SetOut(Location::SameAsFirstInput());
3531}
3532
3533void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
3534 HandleBitwiseOperation(instruction);
3535}
3536
3537void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
3538 HandleBitwiseOperation(instruction);
3539}
3540
3541void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
3542 HandleBitwiseOperation(instruction);
3543}
3544
3545void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
3546 LocationSummary* locations = instruction->GetLocations();
3547 Location first = locations->InAt(0);
3548 Location second = locations->InAt(1);
3549 DCHECK(first.Equals(locations->Out()));
3550
3551 if (instruction->GetResultType() == Primitive::kPrimInt) {
3552 if (second.IsRegister()) {
3553 if (instruction->IsAnd()) {
3554 __ andl(first.As<Register>(), second.As<Register>());
3555 } else if (instruction->IsOr()) {
3556 __ orl(first.As<Register>(), second.As<Register>());
3557 } else {
3558 DCHECK(instruction->IsXor());
3559 __ xorl(first.As<Register>(), second.As<Register>());
3560 }
3561 } else if (second.IsConstant()) {
3562 if (instruction->IsAnd()) {
3563 __ andl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3564 } else if (instruction->IsOr()) {
3565 __ orl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3566 } else {
3567 DCHECK(instruction->IsXor());
3568 __ xorl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
3569 }
3570 } else {
3571 if (instruction->IsAnd()) {
3572 __ andl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
3573 } else if (instruction->IsOr()) {
3574 __ orl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
3575 } else {
3576 DCHECK(instruction->IsXor());
3577 __ xorl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
3578 }
3579 }
3580 } else {
3581 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3582 if (second.IsRegisterPair()) {
3583 if (instruction->IsAnd()) {
3584 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3585 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3586 } else if (instruction->IsOr()) {
3587 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3588 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3589 } else {
3590 DCHECK(instruction->IsXor());
3591 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3592 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
3593 }
3594 } else {
3595 if (instruction->IsAnd()) {
3596 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3597 __ andl(first.AsRegisterPairHigh<Register>(),
3598 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3599 } else if (instruction->IsOr()) {
3600 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3601 __ orl(first.AsRegisterPairHigh<Register>(),
3602 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3603 } else {
3604 DCHECK(instruction->IsXor());
3605 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3606 __ xorl(first.AsRegisterPairHigh<Register>(),
3607 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
3608 }
3609 }
3610 }
3611}
3612
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003613} // namespace x86
3614} // namespace art