blob: d66180be32c0124efb0c80a6da84cb6f5f340903 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
19#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010020#include "gc/accounting/card_table.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070021#include "mirror/array-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010022#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010023#include "mirror/class.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010024#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000025#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010026#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000027#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010028#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010031
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032namespace x86 {
33
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010034static constexpr bool kExplicitStackOverflowCheck = false;
35
36static constexpr int kNumberOfPushedRegistersAtEntry = 1;
37static constexpr int kCurrentMethodStackOffset = 0;
38
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010039static constexpr Register kRuntimeParameterCoreRegisters[] = { EAX, ECX, EDX };
40static constexpr size_t kRuntimeParameterCoreRegistersLength =
41 arraysize(kRuntimeParameterCoreRegisters);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010042static constexpr XmmRegister kRuntimeParameterFpuRegisters[] = { };
43static constexpr size_t kRuntimeParameterFpuRegistersLength = 0;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010044
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010045class InvokeRuntimeCallingConvention : public CallingConvention<Register, XmmRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010046 public:
47 InvokeRuntimeCallingConvention()
48 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010049 kRuntimeParameterCoreRegistersLength,
50 kRuntimeParameterFpuRegisters,
51 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010052
53 private:
54 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
55};
56
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
58
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010059class SlowPathCodeX86 : public SlowPathCode {
60 public:
61 SlowPathCodeX86() : entry_label_(), exit_label_() {}
62
63 Label* GetEntryLabel() { return &entry_label_; }
64 Label* GetExitLabel() { return &exit_label_; }
65
66 private:
67 Label entry_label_;
68 Label exit_label_;
69
70 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86);
71};
72
73class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010074 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010075 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076
77 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
78 __ Bind(GetEntryLabel());
79 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010080 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010081 }
82
83 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010084 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010085 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
86};
87
Calin Juravled0d48522014-11-04 16:40:20 +000088class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
89 public:
90 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
91
92 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
93 __ Bind(GetEntryLabel());
94 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
95 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
96 }
97
98 private:
99 HDivZeroCheck* const instruction_;
100 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
101};
102
103class DivMinusOneSlowPathX86 : public SlowPathCodeX86 {
104 public:
105 explicit DivMinusOneSlowPathX86(Register reg) : reg_(reg) {}
106
107 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
108 __ Bind(GetEntryLabel());
109 __ negl(reg_);
110 __ jmp(GetExitLabel());
111 }
112
113 private:
114 Register reg_;
115 DISALLOW_COPY_AND_ASSIGN(DivMinusOneSlowPathX86);
116};
117
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100118class StackOverflowCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100119 public:
120 StackOverflowCheckSlowPathX86() {}
121
122 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
123 __ Bind(GetEntryLabel());
124 __ addl(ESP,
125 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
126 __ fs()->jmp(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowStackOverflow)));
127 }
128
129 private:
130 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86);
131};
132
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100133class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100134 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100135 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
136 Location index_location,
137 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100138 : instruction_(instruction), index_location_(index_location), length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100139
140 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100141 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100142 __ Bind(GetEntryLabel());
143 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100144 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), index_location_);
145 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(1)), length_location_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100146 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100147 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100148 }
149
150 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100151 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100152 const Location index_location_;
153 const Location length_location_;
154
155 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
156};
157
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100158class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000159 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100160 explicit SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
161 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000162
163 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100164 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000165 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100166 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000167 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
168 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100169 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100170 if (successor_ == nullptr) {
171 __ jmp(GetReturnLabel());
172 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100173 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100174 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000175 }
176
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100177 Label* GetReturnLabel() {
178 DCHECK(successor_ == nullptr);
179 return &return_label_;
180 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000181
182 private:
183 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100184 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000185 Label return_label_;
186
187 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
188};
189
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000190class LoadStringSlowPathX86 : public SlowPathCodeX86 {
191 public:
192 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
193
194 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
195 LocationSummary* locations = instruction_->GetLocations();
196 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
197
198 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
199 __ Bind(GetEntryLabel());
200 codegen->SaveLiveRegisters(locations);
201
202 InvokeRuntimeCallingConvention calling_convention;
203 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(0));
204 __ movl(calling_convention.GetRegisterAt(1), Immediate(instruction_->GetStringIndex()));
205 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
206 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
207 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
208 codegen->RestoreLiveRegisters(locations);
209
210 __ jmp(GetExitLabel());
211 }
212
213 private:
214 HLoadString* const instruction_;
215
216 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
217};
218
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000219class LoadClassSlowPathX86 : public SlowPathCodeX86 {
220 public:
221 LoadClassSlowPathX86(HLoadClass* cls,
222 HInstruction* at,
223 uint32_t dex_pc,
224 bool do_clinit)
225 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
226 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
227 }
228
229 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
230 LocationSummary* locations = at_->GetLocations();
231 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
232 __ Bind(GetEntryLabel());
233 codegen->SaveLiveRegisters(locations);
234
235 InvokeRuntimeCallingConvention calling_convention;
236 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
237 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
238 __ fs()->call(Address::Absolute(do_clinit_
239 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
240 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
241 codegen->RecordPcInfo(at_, dex_pc_);
242
243 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000244 Location out = locations->Out();
245 if (out.IsValid()) {
246 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
247 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000248 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000249
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000250 codegen->RestoreLiveRegisters(locations);
251 __ jmp(GetExitLabel());
252 }
253
254 private:
255 // The class this slow path will load.
256 HLoadClass* const cls_;
257
258 // The instruction where this slow path is happening.
259 // (Might be the load class or an initialization check).
260 HInstruction* const at_;
261
262 // The dex PC of `at_`.
263 const uint32_t dex_pc_;
264
265 // Whether to initialize the class.
266 const bool do_clinit_;
267
268 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
269};
270
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000271class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
272 public:
273 TypeCheckSlowPathX86(HTypeCheck* instruction, Location object_class)
274 : instruction_(instruction),
275 object_class_(object_class) {}
276
277 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
278 LocationSummary* locations = instruction_->GetLocations();
279 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
280
281 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
282 __ Bind(GetEntryLabel());
283 codegen->SaveLiveRegisters(locations);
284
285 // We're moving two locations to locations that could overlap, so we need a parallel
286 // move resolver.
287 InvokeRuntimeCallingConvention calling_convention;
288 MoveOperands move1(locations->InAt(1),
289 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
290 nullptr);
291 MoveOperands move2(object_class_,
292 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
293 nullptr);
294 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
295 parallel_move.AddMove(&move1);
296 parallel_move.AddMove(&move2);
297 x86_codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
298
299 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInstanceofNonTrivial)));
300 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
301 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
302 codegen->RestoreLiveRegisters(locations);
303
304 __ jmp(GetExitLabel());
305 }
306
307 private:
308 HTypeCheck* const instruction_;
309 const Location object_class_;
310
311 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
312};
313
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100314#undef __
315#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
316
Dave Allison20dfc792014-06-16 20:44:29 -0700317inline Condition X86Condition(IfCondition cond) {
318 switch (cond) {
319 case kCondEQ: return kEqual;
320 case kCondNE: return kNotEqual;
321 case kCondLT: return kLess;
322 case kCondLE: return kLessEqual;
323 case kCondGT: return kGreater;
324 case kCondGE: return kGreaterEqual;
325 default:
326 LOG(FATAL) << "Unknown if condition";
327 }
328 return kEqual;
329}
330
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100331void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
332 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
333}
334
335void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
336 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
337}
338
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100339size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
340 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
341 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100342}
343
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100344size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
345 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
346 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100347}
348
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100349CodeGeneratorX86::CodeGeneratorX86(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100350 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfXmmRegisters, kNumberOfRegisterPairs),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100351 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100352 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100353 instruction_visitor_(graph, this),
354 move_resolver_(graph->GetArena(), this) {}
355
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100356size_t CodeGeneratorX86::FrameEntrySpillSize() const {
357 return kNumberOfPushedRegistersAtEntry * kX86WordSize;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100358}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100359
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100360Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100361 switch (type) {
362 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100363 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100364 X86ManagedRegister pair =
365 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100366 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
367 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100368 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
369 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100370 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100371 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100372 }
373
374 case Primitive::kPrimByte:
375 case Primitive::kPrimBoolean:
376 case Primitive::kPrimChar:
377 case Primitive::kPrimShort:
378 case Primitive::kPrimInt:
379 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100380 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100381 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100382 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100383 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
384 X86ManagedRegister current =
385 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
386 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100387 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100388 }
389 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100390 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100391 }
392
393 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100394 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100395 return Location::FpuRegisterLocation(
396 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100397 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100398
399 case Primitive::kPrimVoid:
400 LOG(FATAL) << "Unreachable type " << type;
401 }
402
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100403 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100404}
405
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100406void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100407 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100408 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100409
410 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100411 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100412
413 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100414 blocked_core_registers_[EBP] = true;
415 blocked_core_registers_[ESI] = true;
416 blocked_core_registers_[EDI] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100417
418 UpdateBlockedPairRegisters();
419}
420
421void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
422 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
423 X86ManagedRegister current =
424 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
425 if (blocked_core_registers_[current.AsRegisterPairLow()]
426 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
427 blocked_register_pairs_[i] = true;
428 }
429 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100430}
431
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100432InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
433 : HGraphVisitor(graph),
434 assembler_(codegen->GetAssembler()),
435 codegen_(codegen) {}
436
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000437void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000438 // Create a fake register to mimic Quick.
439 static const int kFakeReturnRegister = 8;
440 core_spill_mask_ |= (1 << kFakeReturnRegister);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000441
Dave Allison648d7112014-07-25 16:15:27 -0700442 bool skip_overflow_check = IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100443 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
444 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100445 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100446 }
447
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100448 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100449 __ subl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100450
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100451 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100452 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86();
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100453 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100454
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100455 __ fs()->cmpl(ESP, Address::Absolute(Thread::StackEndOffset<kX86WordSize>()));
456 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100457 }
458
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100459 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000460}
461
462void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100463 __ addl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000464}
465
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100466void CodeGeneratorX86::Bind(HBasicBlock* block) {
467 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000468}
469
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100470void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100471 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000472}
473
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100474Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
475 switch (load->GetType()) {
476 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100477 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100478 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
479 break;
480
481 case Primitive::kPrimInt:
482 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100483 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100484 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100485
486 case Primitive::kPrimBoolean:
487 case Primitive::kPrimByte:
488 case Primitive::kPrimChar:
489 case Primitive::kPrimShort:
490 case Primitive::kPrimVoid:
491 LOG(FATAL) << "Unexpected type " << load->GetType();
492 }
493
494 LOG(FATAL) << "Unreachable";
495 return Location();
496}
497
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100498Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
499 switch (type) {
500 case Primitive::kPrimBoolean:
501 case Primitive::kPrimByte:
502 case Primitive::kPrimChar:
503 case Primitive::kPrimShort:
504 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100505 case Primitive::kPrimFloat:
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100506 case Primitive::kPrimNot: {
507 uint32_t index = gp_index_++;
508 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100509 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100510 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100511 return Location::StackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100512 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100513 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100514
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100515 case Primitive::kPrimLong:
516 case Primitive::kPrimDouble: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100517 uint32_t index = gp_index_;
518 gp_index_ += 2;
519 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100520 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
521 calling_convention.GetRegisterPairAt(index));
522 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100523 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000524 // On X86, the register index and stack index of a quick parameter is the same, since
525 // we are passing floating pointer values in core registers.
526 return Location::QuickParameter(index, index);
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100527 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100528 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100529 }
530 }
531
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100532 case Primitive::kPrimVoid:
533 LOG(FATAL) << "Unexpected parameter type " << type;
534 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100535 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100536 return Location();
537}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100538
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100539void CodeGeneratorX86::Move32(Location destination, Location source) {
540 if (source.Equals(destination)) {
541 return;
542 }
543 if (destination.IsRegister()) {
544 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100545 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100546 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100547 __ movd(destination.As<Register>(), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100548 } else {
549 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100550 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100551 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100552 } else if (destination.IsFpuRegister()) {
553 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100554 __ movd(destination.As<XmmRegister>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100555 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100556 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100557 } else {
558 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100559 __ movss(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100560 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100561 } else {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100562 DCHECK(destination.IsStackSlot());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100563 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100564 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100565 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100566 __ movss(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100567 } else {
568 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100569 __ pushl(Address(ESP, source.GetStackIndex()));
570 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100571 }
572 }
573}
574
575void CodeGeneratorX86::Move64(Location destination, Location source) {
576 if (source.Equals(destination)) {
577 return;
578 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100579 if (destination.IsRegisterPair()) {
580 if (source.IsRegisterPair()) {
581 __ movl(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
582 __ movl(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100583 } else if (source.IsFpuRegister()) {
584 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100585 } else if (source.IsQuickParameter()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000586 uint16_t register_index = source.GetQuickParameterRegisterIndex();
587 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100588 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100589 __ movl(destination.AsRegisterPairLow<Register>(),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000590 calling_convention.GetRegisterAt(register_index));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100591 __ movl(destination.AsRegisterPairHigh<Register>(), Address(ESP,
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000592 calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100593 } else {
594 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100595 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
596 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100597 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
598 }
599 } else if (destination.IsQuickParameter()) {
600 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000601 uint16_t register_index = destination.GetQuickParameterRegisterIndex();
602 uint16_t stack_index = destination.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100603 if (source.IsRegister()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000604 __ movl(calling_convention.GetRegisterAt(register_index), source.AsRegisterPairLow<Register>());
605 __ movl(Address(ESP, calling_convention.GetStackOffsetOf(stack_index + 1)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100606 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100607 } else if (source.IsFpuRegister()) {
608 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100609 } else {
610 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000611 __ movl(calling_convention.GetRegisterAt(register_index),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100612 Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100613 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000614 __ popl(Address(ESP, calling_convention.GetStackOffsetOf(stack_index + 1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100615 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100616 } else if (destination.IsFpuRegister()) {
617 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100618 __ movsd(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100619 } else {
620 LOG(FATAL) << "Unimplemented";
621 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100622 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100623 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100624 if (source.IsRegisterPair()) {
625 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100626 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100627 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100628 } else if (source.IsQuickParameter()) {
629 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000630 uint16_t register_index = source.GetQuickParameterRegisterIndex();
631 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100632 __ movl(Address(ESP, destination.GetStackIndex()),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000633 calling_convention.GetRegisterAt(register_index));
634 DCHECK_EQ(calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100635 static_cast<size_t>(destination.GetHighStackIndex(kX86WordSize)));
636 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100637 __ movsd(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100638 } else {
639 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100640 __ pushl(Address(ESP, source.GetStackIndex()));
641 __ popl(Address(ESP, destination.GetStackIndex()));
642 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
643 __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100644 }
645 }
646}
647
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100648void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Roland Levillain476df552014-10-09 17:51:36 +0100649 if (instruction->IsIntConstant()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100650 Immediate imm(instruction->AsIntConstant()->GetValue());
651 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100652 __ movl(location.As<Register>(), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100653 } else if (location.IsStackSlot()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100654 __ movl(Address(ESP, location.GetStackIndex()), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100655 } else {
656 DCHECK(location.IsConstant());
657 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100658 }
Roland Levillain476df552014-10-09 17:51:36 +0100659 } else if (instruction->IsLongConstant()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100660 int64_t value = instruction->AsLongConstant()->GetValue();
661 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100662 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
663 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100664 } else if (location.IsDoubleStackSlot()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100665 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
666 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100667 } else {
668 DCHECK(location.IsConstant());
669 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100670 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000671 } else if (instruction->IsTemporary()) {
672 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
673 Move32(location, temp_location);
Roland Levillain476df552014-10-09 17:51:36 +0100674 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100675 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100676 switch (instruction->GetType()) {
677 case Primitive::kPrimBoolean:
678 case Primitive::kPrimByte:
679 case Primitive::kPrimChar:
680 case Primitive::kPrimShort:
681 case Primitive::kPrimInt:
682 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100683 case Primitive::kPrimFloat:
684 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100685 break;
686
687 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100688 case Primitive::kPrimDouble:
689 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100690 break;
691
692 default:
693 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
694 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000695 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100696 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100697 switch (instruction->GetType()) {
698 case Primitive::kPrimBoolean:
699 case Primitive::kPrimByte:
700 case Primitive::kPrimChar:
701 case Primitive::kPrimShort:
702 case Primitive::kPrimInt:
703 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100704 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100705 Move32(location, instruction->GetLocations()->Out());
706 break;
707
708 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100709 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100710 Move64(location, instruction->GetLocations()->Out());
711 break;
712
713 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100714 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100715 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000716 }
717}
718
719void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000720 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000721}
722
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000723void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000724 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100725 DCHECK(!successor->IsExitBlock());
726
727 HBasicBlock* block = got->GetBlock();
728 HInstruction* previous = got->GetPrevious();
729
730 HLoopInformation* info = block->GetLoopInformation();
731 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
732 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
733 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
734 return;
735 }
736
737 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
738 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
739 }
740 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000741 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000742 }
743}
744
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000745void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000746 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000747}
748
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000749void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700750 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000751 if (kIsDebugBuild) {
752 __ Comment("Unreachable");
753 __ int3();
754 }
755}
756
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000757void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100758 LocationSummary* locations =
759 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100760 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100761 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100762 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100763 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000764}
765
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000766void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700767 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100768 if (cond->IsIntConstant()) {
769 // Constant condition, statically compared against 1.
770 int32_t cond_value = cond->AsIntConstant()->GetValue();
771 if (cond_value == 1) {
772 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
773 if_instr->IfTrueSuccessor())) {
774 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100775 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100776 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100777 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100778 DCHECK_EQ(cond_value, 0);
779 }
780 } else {
781 bool materialized =
782 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
783 // Moves do not affect the eflags register, so if the condition is
784 // evaluated just before the if, we don't need to evaluate it
785 // again.
786 bool eflags_set = cond->IsCondition()
787 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
788 if (materialized) {
789 if (!eflags_set) {
790 // Materialized condition, compare against 0.
791 Location lhs = if_instr->GetLocations()->InAt(0);
792 if (lhs.IsRegister()) {
793 __ cmpl(lhs.As<Register>(), Immediate(0));
794 } else {
795 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
796 }
797 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
798 } else {
799 __ j(X86Condition(cond->AsCondition()->GetCondition()),
800 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
801 }
802 } else {
803 Location lhs = cond->GetLocations()->InAt(0);
804 Location rhs = cond->GetLocations()->InAt(1);
805 // LHS is guaranteed to be in a register (see
806 // LocationsBuilderX86::VisitCondition).
807 if (rhs.IsRegister()) {
808 __ cmpl(lhs.As<Register>(), rhs.As<Register>());
809 } else if (rhs.IsConstant()) {
810 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
811 Immediate imm(instruction->AsIntConstant()->GetValue());
812 __ cmpl(lhs.As<Register>(), imm);
813 } else {
814 __ cmpl(lhs.As<Register>(), Address(ESP, rhs.GetStackIndex()));
815 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100816 __ j(X86Condition(cond->AsCondition()->GetCondition()),
817 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700818 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100819 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100820 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
821 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700822 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000823 }
824}
825
826void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000827 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000828}
829
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000830void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
831 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000832}
833
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000834void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100835 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000836}
837
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000838void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100839 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700840 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000841}
842
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100843void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100844 LocationSummary* locations =
845 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100846 switch (store->InputAt(1)->GetType()) {
847 case Primitive::kPrimBoolean:
848 case Primitive::kPrimByte:
849 case Primitive::kPrimChar:
850 case Primitive::kPrimShort:
851 case Primitive::kPrimInt:
852 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100853 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100854 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
855 break;
856
857 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100858 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100859 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
860 break;
861
862 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100863 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100864 }
865 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000866}
867
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000868void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700869 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000870}
871
Dave Allison20dfc792014-06-16 20:44:29 -0700872void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100873 LocationSummary* locations =
874 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100875 locations->SetInAt(0, Location::RequiresRegister());
876 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100877 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100878 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100879 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000880}
881
Dave Allison20dfc792014-06-16 20:44:29 -0700882void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
883 if (comp->NeedsMaterialization()) {
884 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100885 Register reg = locations->Out().As<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100886 // Clear register: setcc only sets the low byte.
887 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700888 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100889 __ cmpl(locations->InAt(0).As<Register>(),
890 locations->InAt(1).As<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100891 } else if (locations->InAt(1).IsConstant()) {
892 HConstant* instruction = locations->InAt(1).GetConstant();
893 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100894 __ cmpl(locations->InAt(0).As<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700895 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100896 __ cmpl(locations->InAt(0).As<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700897 Address(ESP, locations->InAt(1).GetStackIndex()));
898 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100899 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100900 }
Dave Allison20dfc792014-06-16 20:44:29 -0700901}
902
903void LocationsBuilderX86::VisitEqual(HEqual* comp) {
904 VisitCondition(comp);
905}
906
907void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
908 VisitCondition(comp);
909}
910
911void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
912 VisitCondition(comp);
913}
914
915void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
916 VisitCondition(comp);
917}
918
919void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
920 VisitCondition(comp);
921}
922
923void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
924 VisitCondition(comp);
925}
926
927void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
928 VisitCondition(comp);
929}
930
931void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
932 VisitCondition(comp);
933}
934
935void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
936 VisitCondition(comp);
937}
938
939void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
940 VisitCondition(comp);
941}
942
943void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
944 VisitCondition(comp);
945}
946
947void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
948 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000949}
950
951void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100952 LocationSummary* locations =
953 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100954 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000955}
956
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000957void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100958 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700959 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000960}
961
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100962void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100963 LocationSummary* locations =
964 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100965 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100966}
967
968void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
969 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700970 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100971}
972
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100973void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
974 LocationSummary* locations =
975 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
976 locations->SetOut(Location::ConstantLocation(constant));
977}
978
979void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
980 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700981 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100982}
983
984void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
985 LocationSummary* locations =
986 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
987 locations->SetOut(Location::ConstantLocation(constant));
988}
989
990void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
991 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700992 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100993}
994
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000995void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000996 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000997}
998
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000999void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001000 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001001 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001002 __ ret();
1003}
1004
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001005void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001006 LocationSummary* locations =
1007 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001008 switch (ret->InputAt(0)->GetType()) {
1009 case Primitive::kPrimBoolean:
1010 case Primitive::kPrimByte:
1011 case Primitive::kPrimChar:
1012 case Primitive::kPrimShort:
1013 case Primitive::kPrimInt:
1014 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001015 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001016 break;
1017
1018 case Primitive::kPrimLong:
1019 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001020 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001021 break;
1022
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001023 case Primitive::kPrimFloat:
1024 case Primitive::kPrimDouble:
1025 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001026 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001027 break;
1028
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001029 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001030 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001031 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001032}
1033
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001034void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001035 if (kIsDebugBuild) {
1036 switch (ret->InputAt(0)->GetType()) {
1037 case Primitive::kPrimBoolean:
1038 case Primitive::kPrimByte:
1039 case Primitive::kPrimChar:
1040 case Primitive::kPrimShort:
1041 case Primitive::kPrimInt:
1042 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001043 DCHECK_EQ(ret->GetLocations()->InAt(0).As<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001044 break;
1045
1046 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001047 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1048 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001049 break;
1050
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001051 case Primitive::kPrimFloat:
1052 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001053 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001054 break;
1055
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001056 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001057 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001058 }
1059 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001060 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001061 __ ret();
1062}
1063
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001064void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001065 HandleInvoke(invoke);
1066}
1067
1068void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001069 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001070
1071 // TODO: Implement all kinds of calls:
1072 // 1) boot -> boot
1073 // 2) app -> boot
1074 // 3) app -> app
1075 //
1076 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1077
1078 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001079 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001080 // temp = temp->dex_cache_resolved_methods_;
1081 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
1082 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001083 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001084 // (temp + offset_of_quick_compiled_code)()
1085 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1086
1087 DCHECK(!codegen_->IsLeafMethod());
1088 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1089}
1090
1091void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1092 HandleInvoke(invoke);
1093}
1094
1095void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001096 LocationSummary* locations =
1097 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001098 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001099
1100 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001101 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001102 HInstruction* input = invoke->InputAt(i);
1103 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1104 }
1105
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001106 switch (invoke->GetType()) {
1107 case Primitive::kPrimBoolean:
1108 case Primitive::kPrimByte:
1109 case Primitive::kPrimChar:
1110 case Primitive::kPrimShort:
1111 case Primitive::kPrimInt:
1112 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001113 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001114 break;
1115
1116 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001117 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001118 break;
1119
1120 case Primitive::kPrimVoid:
1121 break;
1122
1123 case Primitive::kPrimDouble:
1124 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001125 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001126 break;
1127 }
1128
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001129 invoke->SetLocations(locations);
1130}
1131
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001132void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001133 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001134 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1135 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1136 LocationSummary* locations = invoke->GetLocations();
1137 Location receiver = locations->InAt(0);
1138 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1139 // temp = object->GetClass();
1140 if (receiver.IsStackSlot()) {
1141 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1142 __ movl(temp, Address(temp, class_offset));
1143 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001144 __ movl(temp, Address(receiver.As<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001145 }
1146 // temp = temp->GetMethodAt(method_offset);
1147 __ movl(temp, Address(temp, method_offset));
1148 // call temp->GetEntryPoint();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001149 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1150
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001151 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001152 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001153}
1154
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001155void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1156 HandleInvoke(invoke);
1157 // Add the hidden argument.
1158 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM0));
1159}
1160
1161void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1162 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1163 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
1164 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1165 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1166 LocationSummary* locations = invoke->GetLocations();
1167 Location receiver = locations->InAt(0);
1168 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1169
1170 // Set the hidden argument.
1171 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
1172 __ movd(invoke->GetLocations()->GetTemp(1).As<XmmRegister>(), temp);
1173
1174 // temp = object->GetClass();
1175 if (receiver.IsStackSlot()) {
1176 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1177 __ movl(temp, Address(temp, class_offset));
1178 } else {
1179 __ movl(temp, Address(receiver.As<Register>(), class_offset));
1180 }
1181 // temp = temp->GetImtEntryAt(method_offset);
1182 __ movl(temp, Address(temp, method_offset));
1183 // call temp->GetEntryPoint();
1184 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1185
1186 DCHECK(!codegen_->IsLeafMethod());
1187 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1188}
1189
Roland Levillain88cb1752014-10-20 16:36:47 +01001190void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1191 LocationSummary* locations =
1192 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1193 switch (neg->GetResultType()) {
1194 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001195 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001196 locations->SetInAt(0, Location::RequiresRegister());
1197 locations->SetOut(Location::SameAsFirstInput());
1198 break;
1199
Roland Levillain88cb1752014-10-20 16:36:47 +01001200 case Primitive::kPrimFloat:
1201 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001202 locations->SetInAt(0, Location::RequiresFpuRegister());
1203 // Output overlaps as we need a fresh (zero-initialized)
1204 // register to perform subtraction from zero.
1205 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001206 break;
1207
1208 default:
1209 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1210 }
1211}
1212
1213void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1214 LocationSummary* locations = neg->GetLocations();
1215 Location out = locations->Out();
1216 Location in = locations->InAt(0);
1217 switch (neg->GetResultType()) {
1218 case Primitive::kPrimInt:
1219 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001220 DCHECK(in.Equals(out));
Roland Levillain88cb1752014-10-20 16:36:47 +01001221 __ negl(out.As<Register>());
1222 break;
1223
1224 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001225 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001226 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001227 __ negl(out.AsRegisterPairLow<Register>());
1228 // Negation is similar to subtraction from zero. The least
1229 // significant byte triggers a borrow when it is different from
1230 // zero; to take it into account, add 1 to the most significant
1231 // byte if the carry flag (CF) is set to 1 after the first NEGL
1232 // operation.
1233 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1234 __ negl(out.AsRegisterPairHigh<Register>());
1235 break;
1236
Roland Levillain88cb1752014-10-20 16:36:47 +01001237 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001238 DCHECK(!in.Equals(out));
1239 // out = 0
1240 __ xorps(out.As<XmmRegister>(), out.As<XmmRegister>());
1241 // out = out - in
1242 __ subss(out.As<XmmRegister>(), in.As<XmmRegister>());
1243 break;
1244
Roland Levillain88cb1752014-10-20 16:36:47 +01001245 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001246 DCHECK(!in.Equals(out));
1247 // out = 0
1248 __ xorpd(out.As<XmmRegister>(), out.As<XmmRegister>());
1249 // out = out - in
1250 __ subsd(out.As<XmmRegister>(), in.As<XmmRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001251 break;
1252
1253 default:
1254 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1255 }
1256}
1257
Roland Levillaindff1f282014-11-05 14:15:05 +00001258void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
1259 LocationSummary* locations =
1260 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1261 Primitive::Type result_type = conversion->GetResultType();
1262 Primitive::Type input_type = conversion->GetInputType();
1263 switch (result_type) {
1264 case Primitive::kPrimLong:
1265 switch (input_type) {
1266 case Primitive::kPrimByte:
1267 case Primitive::kPrimShort:
1268 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001269 case Primitive::kPrimChar:
Roland Levillaindff1f282014-11-05 14:15:05 +00001270 // int-to-long conversion.
1271 locations->SetInAt(0, Location::RegisterLocation(EAX));
1272 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1273 break;
1274
1275 case Primitive::kPrimFloat:
1276 case Primitive::kPrimDouble:
1277 LOG(FATAL) << "Type conversion from " << input_type << " to "
1278 << result_type << " not yet implemented";
1279 break;
1280
1281 default:
1282 LOG(FATAL) << "Unexpected type conversion from " << input_type
1283 << " to " << result_type;
1284 }
1285 break;
1286
Roland Levillain3adfd1b2014-11-11 14:48:08 +00001287 case Primitive::kPrimInt:
Roland Levillaindff1f282014-11-05 14:15:05 +00001288 case Primitive::kPrimFloat:
1289 case Primitive::kPrimDouble:
1290 LOG(FATAL) << "Type conversion from " << input_type
1291 << " to " << result_type << " not yet implemented";
1292 break;
1293
1294 default:
1295 LOG(FATAL) << "Unexpected type conversion from " << input_type
1296 << " to " << result_type;
1297 }
1298}
1299
1300void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1301 LocationSummary* locations = conversion->GetLocations();
1302 Location out = locations->Out();
1303 Location in = locations->InAt(0);
1304 Primitive::Type result_type = conversion->GetResultType();
1305 Primitive::Type input_type = conversion->GetInputType();
1306 switch (result_type) {
1307 case Primitive::kPrimLong:
1308 switch (input_type) {
1309 case Primitive::kPrimByte:
1310 case Primitive::kPrimShort:
1311 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001312 case Primitive::kPrimChar:
Roland Levillaindff1f282014-11-05 14:15:05 +00001313 // int-to-long conversion.
1314 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1315 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
1316 DCHECK_EQ(in.As<Register>(), EAX);
1317 __ cdq();
1318 break;
1319
1320 case Primitive::kPrimFloat:
1321 case Primitive::kPrimDouble:
1322 LOG(FATAL) << "Type conversion from " << input_type << " to "
1323 << result_type << " not yet implemented";
1324 break;
1325
1326 default:
1327 LOG(FATAL) << "Unexpected type conversion from " << input_type
1328 << " to " << result_type;
1329 }
1330 break;
1331
Roland Levillain3adfd1b2014-11-11 14:48:08 +00001332 case Primitive::kPrimInt:
Roland Levillaindff1f282014-11-05 14:15:05 +00001333 case Primitive::kPrimFloat:
1334 case Primitive::kPrimDouble:
1335 LOG(FATAL) << "Type conversion from " << input_type
1336 << " to " << result_type << " not yet implemented";
1337 break;
1338
1339 default:
1340 LOG(FATAL) << "Unexpected type conversion from " << input_type
1341 << " to " << result_type;
1342 }
1343}
1344
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001345void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001346 LocationSummary* locations =
1347 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001348 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001349 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001350 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001351 locations->SetInAt(0, Location::RequiresRegister());
1352 locations->SetInAt(1, Location::Any());
1353 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001354 break;
1355 }
1356
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001357 case Primitive::kPrimFloat:
1358 case Primitive::kPrimDouble: {
1359 locations->SetInAt(0, Location::RequiresFpuRegister());
1360 locations->SetInAt(1, Location::Any());
1361 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001362 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001363 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001364
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001365 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001366 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1367 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001368 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001369}
1370
1371void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1372 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001373 Location first = locations->InAt(0);
1374 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001375 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001376 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001377 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001378 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001379 __ addl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001380 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001381 __ addl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001382 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001383 __ addl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001384 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001385 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001386 }
1387
1388 case Primitive::kPrimLong: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001389 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001390 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1391 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001392 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001393 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1394 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001395 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001396 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001397 break;
1398 }
1399
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001400 case Primitive::kPrimFloat: {
1401 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001402 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001403 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001404 __ addss(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001405 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001406 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001407 }
1408
1409 case Primitive::kPrimDouble: {
1410 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001411 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001412 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001413 __ addsd(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001414 }
1415 break;
1416 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001417
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001418 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001419 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001420 }
1421}
1422
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001423void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001424 LocationSummary* locations =
1425 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001426 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001427 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001428 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001429 locations->SetInAt(0, Location::RequiresRegister());
1430 locations->SetInAt(1, Location::Any());
1431 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001432 break;
1433 }
Calin Juravle11351682014-10-23 15:38:15 +01001434 case Primitive::kPrimFloat:
1435 case Primitive::kPrimDouble: {
1436 locations->SetInAt(0, Location::RequiresFpuRegister());
1437 locations->SetInAt(1, Location::RequiresFpuRegister());
1438 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001439 break;
Calin Juravle11351682014-10-23 15:38:15 +01001440 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001441
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001442 default:
Calin Juravle11351682014-10-23 15:38:15 +01001443 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001444 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001445}
1446
1447void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1448 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001449 Location first = locations->InAt(0);
1450 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001451 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001452 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001453 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001454 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001455 __ subl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001456 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001457 __ subl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001458 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001459 __ subl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001460 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001461 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001462 }
1463
1464 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001465 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001466 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1467 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001468 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001469 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001470 __ sbbl(first.AsRegisterPairHigh<Register>(),
1471 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001472 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001473 break;
1474 }
1475
Calin Juravle11351682014-10-23 15:38:15 +01001476 case Primitive::kPrimFloat: {
1477 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001478 break;
Calin Juravle11351682014-10-23 15:38:15 +01001479 }
1480
1481 case Primitive::kPrimDouble: {
1482 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1483 break;
1484 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001485
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001486 default:
Calin Juravle11351682014-10-23 15:38:15 +01001487 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001488 }
1489}
1490
Calin Juravle34bacdf2014-10-07 20:23:36 +01001491void LocationsBuilderX86::VisitMul(HMul* mul) {
1492 LocationSummary* locations =
1493 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1494 switch (mul->GetResultType()) {
1495 case Primitive::kPrimInt:
1496 locations->SetInAt(0, Location::RequiresRegister());
1497 locations->SetInAt(1, Location::Any());
1498 locations->SetOut(Location::SameAsFirstInput());
1499 break;
1500 case Primitive::kPrimLong: {
1501 locations->SetInAt(0, Location::RequiresRegister());
1502 // TODO: Currently this handles only stack operands:
1503 // - we don't have enough registers because we currently use Quick ABI.
1504 // - by the time we have a working register allocator we will probably change the ABI
1505 // and fix the above.
1506 // - we don't have a way yet to request operands on stack but the base line compiler
1507 // will leave the operands on the stack with Any().
1508 locations->SetInAt(1, Location::Any());
1509 locations->SetOut(Location::SameAsFirstInput());
1510 // Needed for imul on 32bits with 64bits output.
1511 locations->AddTemp(Location::RegisterLocation(EAX));
1512 locations->AddTemp(Location::RegisterLocation(EDX));
1513 break;
1514 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001515 case Primitive::kPrimFloat:
1516 case Primitive::kPrimDouble: {
1517 locations->SetInAt(0, Location::RequiresFpuRegister());
1518 locations->SetInAt(1, Location::RequiresFpuRegister());
1519 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001520 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001521 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001522
1523 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001524 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001525 }
1526}
1527
1528void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1529 LocationSummary* locations = mul->GetLocations();
1530 Location first = locations->InAt(0);
1531 Location second = locations->InAt(1);
1532 DCHECK(first.Equals(locations->Out()));
1533
1534 switch (mul->GetResultType()) {
1535 case Primitive::kPrimInt: {
1536 if (second.IsRegister()) {
1537 __ imull(first.As<Register>(), second.As<Register>());
1538 } else if (second.IsConstant()) {
1539 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1540 __ imull(first.As<Register>(), imm);
1541 } else {
1542 DCHECK(second.IsStackSlot());
1543 __ imull(first.As<Register>(), Address(ESP, second.GetStackIndex()));
1544 }
1545 break;
1546 }
1547
1548 case Primitive::kPrimLong: {
1549 DCHECK(second.IsDoubleStackSlot());
1550
1551 Register in1_hi = first.AsRegisterPairHigh<Register>();
1552 Register in1_lo = first.AsRegisterPairLow<Register>();
1553 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
1554 Address in2_lo(ESP, second.GetStackIndex());
1555 Register eax = locations->GetTemp(0).As<Register>();
1556 Register edx = locations->GetTemp(1).As<Register>();
1557
1558 DCHECK_EQ(EAX, eax);
1559 DCHECK_EQ(EDX, edx);
1560
1561 // input: in1 - 64 bits, in2 - 64 bits
1562 // output: in1
1563 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
1564 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
1565 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
1566
1567 __ movl(eax, in2_hi);
1568 // eax <- in1.lo * in2.hi
1569 __ imull(eax, in1_lo);
1570 // in1.hi <- in1.hi * in2.lo
1571 __ imull(in1_hi, in2_lo);
1572 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
1573 __ addl(in1_hi, eax);
1574 // move in1_lo to eax to prepare for double precision
1575 __ movl(eax, in1_lo);
1576 // edx:eax <- in1.lo * in2.lo
1577 __ mull(in2_lo);
1578 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
1579 __ addl(in1_hi, edx);
1580 // in1.lo <- (in1.lo * in2.lo)[31:0];
1581 __ movl(in1_lo, eax);
1582
1583 break;
1584 }
1585
Calin Juravleb5bfa962014-10-21 18:02:24 +01001586 case Primitive::kPrimFloat: {
1587 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001588 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001589 }
1590
1591 case Primitive::kPrimDouble: {
1592 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1593 break;
1594 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001595
1596 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001597 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001598 }
1599}
1600
Calin Juravle7c4954d2014-10-28 16:57:40 +00001601void LocationsBuilderX86::VisitDiv(HDiv* div) {
1602 LocationSummary* locations =
1603 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1604 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001605 case Primitive::kPrimInt: {
1606 locations->SetInAt(0, Location::RegisterLocation(EAX));
1607 locations->SetInAt(1, Location::RequiresRegister());
1608 locations->SetOut(Location::SameAsFirstInput());
1609 // Intel uses edx:eax as the dividend.
1610 locations->AddTemp(Location::RegisterLocation(EDX));
1611 break;
1612 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001613 case Primitive::kPrimLong: {
1614 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1615 break;
1616 }
1617 case Primitive::kPrimFloat:
1618 case Primitive::kPrimDouble: {
1619 locations->SetInAt(0, Location::RequiresFpuRegister());
1620 locations->SetInAt(1, Location::RequiresFpuRegister());
1621 locations->SetOut(Location::SameAsFirstInput());
1622 break;
1623 }
1624
1625 default:
1626 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1627 }
1628}
1629
1630void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
1631 LocationSummary* locations = div->GetLocations();
1632 Location first = locations->InAt(0);
1633 Location second = locations->InAt(1);
1634 DCHECK(first.Equals(locations->Out()));
1635
1636 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001637 case Primitive::kPrimInt: {
1638 Register first_reg = first.As<Register>();
1639 Register second_reg = second.As<Register>();
1640 DCHECK_EQ(EAX, first_reg);
1641 DCHECK_EQ(EDX, locations->GetTemp(0).As<Register>());
1642
1643 SlowPathCodeX86* slow_path =
1644 new (GetGraph()->GetArena()) DivMinusOneSlowPathX86(first_reg);
1645 codegen_->AddSlowPath(slow_path);
1646
1647 // 0x80000000/-1 triggers an arithmetic exception!
1648 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
1649 // it's safe to just use negl instead of more complex comparisons.
1650
1651 __ cmpl(second_reg, Immediate(-1));
1652 __ j(kEqual, slow_path->GetEntryLabel());
1653
1654 // edx:eax <- sign-extended of eax
1655 __ cdq();
1656 // eax = quotient, edx = remainder
1657 __ idivl(second_reg);
1658
1659 __ Bind(slow_path->GetExitLabel());
1660 break;
1661 }
1662
Calin Juravle7c4954d2014-10-28 16:57:40 +00001663 case Primitive::kPrimLong: {
1664 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1665 break;
1666 }
1667
1668 case Primitive::kPrimFloat: {
1669 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1670 break;
1671 }
1672
1673 case Primitive::kPrimDouble: {
1674 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1675 break;
1676 }
1677
1678 default:
1679 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1680 }
1681}
1682
Calin Juravled0d48522014-11-04 16:40:20 +00001683void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1684 LocationSummary* locations =
1685 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1686 locations->SetInAt(0, Location::Any());
1687 if (instruction->HasUses()) {
1688 locations->SetOut(Location::SameAsFirstInput());
1689 }
1690}
1691
1692void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1693 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
1694 codegen_->AddSlowPath(slow_path);
1695
1696 LocationSummary* locations = instruction->GetLocations();
1697 Location value = locations->InAt(0);
1698
1699 if (value.IsRegister()) {
1700 __ testl(value.As<Register>(), value.As<Register>());
1701 } else if (value.IsStackSlot()) {
1702 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
1703 } else {
1704 DCHECK(value.IsConstant()) << value;
1705 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
1706 __ jmp(slow_path->GetEntryLabel());
1707 }
1708 return;
1709 }
1710 __ j(kEqual, slow_path->GetEntryLabel());
1711}
1712
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001713void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001714 LocationSummary* locations =
1715 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001716 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001717 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001718 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1719 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001720}
1721
1722void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
1723 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001724 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001725 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001726
Nicolas Geoffray707c8092014-04-04 10:50:14 +01001727 __ fs()->call(
1728 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001729
Nicolas Geoffray39468442014-09-02 15:17:15 +01001730 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001731 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001732}
1733
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001734void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
1735 LocationSummary* locations =
1736 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1737 locations->SetOut(Location::RegisterLocation(EAX));
1738 InvokeRuntimeCallingConvention calling_convention;
1739 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1740 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1741 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1742}
1743
1744void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
1745 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001746 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001747 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
1748
1749 __ fs()->call(
1750 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
1751
1752 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1753 DCHECK(!codegen_->IsLeafMethod());
1754}
1755
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001756void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001757 LocationSummary* locations =
1758 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001759 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1760 if (location.IsStackSlot()) {
1761 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1762 } else if (location.IsDoubleStackSlot()) {
1763 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001764 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001765 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001766}
1767
1768void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001769 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001770}
1771
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001772void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001773 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001774 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001775 locations->SetInAt(0, Location::RequiresRegister());
1776 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001777}
1778
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001779void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
1780 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01001781 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001782 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01001783 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001784 switch (not_->InputAt(0)->GetType()) {
1785 case Primitive::kPrimBoolean:
1786 __ xorl(out.As<Register>(), Immediate(1));
1787 break;
1788
1789 case Primitive::kPrimInt:
1790 __ notl(out.As<Register>());
1791 break;
1792
1793 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001794 __ notl(out.AsRegisterPairLow<Register>());
1795 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001796 break;
1797
1798 default:
1799 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1800 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001801}
1802
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001803void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001804 LocationSummary* locations =
1805 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001806 locations->SetInAt(0, Location::RequiresRegister());
1807 locations->SetInAt(1, Location::Any());
1808 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001809}
1810
1811void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001812 LocationSummary* locations = compare->GetLocations();
1813 switch (compare->InputAt(0)->GetType()) {
1814 case Primitive::kPrimLong: {
1815 Label less, greater, done;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001816 Register output = locations->Out().As<Register>();
1817 Location left = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001818 Location right = locations->InAt(1);
1819 if (right.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001820 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001821 } else {
1822 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001823 __ cmpl(left.AsRegisterPairHigh<Register>(),
1824 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001825 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001826 __ j(kLess, &less); // Signed compare.
1827 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001828 if (right.IsRegisterPair()) {
1829 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001830 } else {
1831 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001832 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001833 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001834 __ movl(output, Immediate(0));
1835 __ j(kEqual, &done);
1836 __ j(kBelow, &less); // Unsigned compare.
1837
1838 __ Bind(&greater);
1839 __ movl(output, Immediate(1));
1840 __ jmp(&done);
1841
1842 __ Bind(&less);
1843 __ movl(output, Immediate(-1));
1844
1845 __ Bind(&done);
1846 break;
1847 }
1848 default:
1849 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
1850 }
1851}
1852
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001853void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001854 LocationSummary* locations =
1855 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001856 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1857 locations->SetInAt(i, Location::Any());
1858 }
1859 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001860}
1861
1862void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001863 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001864 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001865}
1866
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001867void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001868 LocationSummary* locations =
1869 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001870 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001871 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001872 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001873 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
1874 || (field_type == Primitive::kPrimByte);
1875 // The register allocator does not support multiple
1876 // inputs that die at entry with one in a specific register.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001877 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001878 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001879 locations->SetInAt(1, Location::RegisterLocation(EAX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001880 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001881 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001882 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001883 // Temporary registers for the write barrier.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001884 if (is_object_type) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001885 locations->AddTemp(Location::RequiresRegister());
1886 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001887 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001888 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001889}
1890
1891void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1892 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001893 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001894 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001895 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001896
1897 switch (field_type) {
1898 case Primitive::kPrimBoolean:
1899 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001900 ByteRegister value = locations->InAt(1).As<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001901 __ movb(Address(obj, offset), value);
1902 break;
1903 }
1904
1905 case Primitive::kPrimShort:
1906 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001907 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001908 __ movw(Address(obj, offset), value);
1909 break;
1910 }
1911
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001912 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001913 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001914 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001915 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001916
1917 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001918 Register temp = locations->GetTemp(0).As<Register>();
1919 Register card = locations->GetTemp(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001920 codegen_->MarkGCCard(temp, card, obj, value);
1921 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001922 break;
1923 }
1924
1925 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001926 Location value = locations->InAt(1);
1927 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1928 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001929 break;
1930 }
1931
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001932 case Primitive::kPrimFloat: {
1933 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1934 __ movss(Address(obj, offset), value);
1935 break;
1936 }
1937
1938 case Primitive::kPrimDouble: {
1939 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1940 __ movsd(Address(obj, offset), value);
1941 break;
1942 }
1943
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001944 case Primitive::kPrimVoid:
1945 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001946 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001947 }
1948}
1949
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001950void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
1951 Label is_null;
1952 __ testl(value, value);
1953 __ j(kEqual, &is_null);
1954 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
1955 __ movl(temp, object);
1956 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
1957 __ movb(Address(temp, card, TIMES_1, 0),
1958 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
1959 __ Bind(&is_null);
1960}
1961
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001962void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001963 LocationSummary* locations =
1964 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001965 locations->SetInAt(0, Location::RequiresRegister());
1966 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001967}
1968
1969void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1970 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001971 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001972 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
1973
1974 switch (instruction->GetType()) {
1975 case Primitive::kPrimBoolean: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001976 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001977 __ movzxb(out, Address(obj, offset));
1978 break;
1979 }
1980
1981 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001982 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001983 __ movsxb(out, Address(obj, offset));
1984 break;
1985 }
1986
1987 case Primitive::kPrimShort: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001988 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001989 __ movsxw(out, Address(obj, offset));
1990 break;
1991 }
1992
1993 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001994 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001995 __ movzxw(out, Address(obj, offset));
1996 break;
1997 }
1998
1999 case Primitive::kPrimInt:
2000 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002001 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002002 __ movl(out, Address(obj, offset));
2003 break;
2004 }
2005
2006 case Primitive::kPrimLong: {
2007 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002008 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
2009 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002010 break;
2011 }
2012
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002013 case Primitive::kPrimFloat: {
2014 XmmRegister out = locations->Out().As<XmmRegister>();
2015 __ movss(out, Address(obj, offset));
2016 break;
2017 }
2018
2019 case Primitive::kPrimDouble: {
2020 XmmRegister out = locations->Out().As<XmmRegister>();
2021 __ movsd(out, Address(obj, offset));
2022 break;
2023 }
2024
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002025 case Primitive::kPrimVoid:
2026 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002027 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002028 }
2029}
2030
2031void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002032 LocationSummary* locations =
2033 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002034 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002035 if (instruction->HasUses()) {
2036 locations->SetOut(Location::SameAsFirstInput());
2037 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002038}
2039
2040void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002041 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002042 codegen_->AddSlowPath(slow_path);
2043
2044 LocationSummary* locations = instruction->GetLocations();
2045 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002046
2047 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002048 __ cmpl(obj.As<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002049 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002050 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002051 } else {
2052 DCHECK(obj.IsConstant()) << obj;
2053 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2054 __ jmp(slow_path->GetEntryLabel());
2055 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002056 }
2057 __ j(kEqual, slow_path->GetEntryLabel());
2058}
2059
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002060void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002061 LocationSummary* locations =
2062 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002063 locations->SetInAt(0, Location::RequiresRegister());
2064 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2065 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002066}
2067
2068void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
2069 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002070 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002071 Location index = locations->InAt(1);
2072
2073 switch (instruction->GetType()) {
2074 case Primitive::kPrimBoolean: {
2075 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002076 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002077 if (index.IsConstant()) {
2078 __ movzxb(out, Address(obj,
2079 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2080 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002081 __ movzxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002082 }
2083 break;
2084 }
2085
2086 case Primitive::kPrimByte: {
2087 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002088 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002089 if (index.IsConstant()) {
2090 __ movsxb(out, Address(obj,
2091 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2092 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002093 __ movsxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002094 }
2095 break;
2096 }
2097
2098 case Primitive::kPrimShort: {
2099 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002100 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002101 if (index.IsConstant()) {
2102 __ movsxw(out, Address(obj,
2103 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2104 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002105 __ movsxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002106 }
2107 break;
2108 }
2109
2110 case Primitive::kPrimChar: {
2111 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002112 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002113 if (index.IsConstant()) {
2114 __ movzxw(out, Address(obj,
2115 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2116 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002117 __ movzxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002118 }
2119 break;
2120 }
2121
2122 case Primitive::kPrimInt:
2123 case Primitive::kPrimNot: {
2124 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002125 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002126 if (index.IsConstant()) {
2127 __ movl(out, Address(obj,
2128 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2129 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002130 __ movl(out, Address(obj, index.As<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002131 }
2132 break;
2133 }
2134
2135 case Primitive::kPrimLong: {
2136 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002137 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002138 if (index.IsConstant()) {
2139 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002140 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
2141 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002142 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002143 __ movl(out.AsRegisterPairLow<Register>(),
2144 Address(obj, index.As<Register>(), TIMES_8, data_offset));
2145 __ movl(out.AsRegisterPairHigh<Register>(),
2146 Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002147 }
2148 break;
2149 }
2150
2151 case Primitive::kPrimFloat:
2152 case Primitive::kPrimDouble:
2153 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002154 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002155 case Primitive::kPrimVoid:
2156 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002157 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002158 }
2159}
2160
2161void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002162 Primitive::Type value_type = instruction->GetComponentType();
2163 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2164 instruction,
2165 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
2166
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002167 if (value_type == Primitive::kPrimNot) {
2168 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002169 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2170 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2171 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002172 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002173 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
2174 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002175 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002176 // In case of a byte operation, the register allocator does not support multiple
2177 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002178 locations->SetInAt(0, Location::RequiresRegister());
2179 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002180 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002181 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002182 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002183 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002184 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002185 }
2186 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002187}
2188
2189void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
2190 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002191 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002192 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002193 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002194 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002195
2196 switch (value_type) {
2197 case Primitive::kPrimBoolean:
2198 case Primitive::kPrimByte: {
2199 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002200 if (index.IsConstant()) {
2201 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002202 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002203 __ movb(Address(obj, offset), value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002204 } else {
2205 __ movb(Address(obj, offset),
2206 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2207 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002208 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002209 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002210 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
2211 value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002212 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002213 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002214 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2215 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002216 }
2217 break;
2218 }
2219
2220 case Primitive::kPrimShort:
2221 case Primitive::kPrimChar: {
2222 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002223 if (index.IsConstant()) {
2224 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002225 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002226 __ movw(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002227 } else {
2228 __ movw(Address(obj, offset),
2229 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2230 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002231 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002232 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002233 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
2234 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002235 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002236 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002237 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2238 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002239 }
2240 break;
2241 }
2242
2243 case Primitive::kPrimInt: {
2244 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002245 if (index.IsConstant()) {
2246 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002247 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002248 __ movl(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002249 } else {
2250 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2251 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002252 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002253 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002254 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
2255 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002256 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002257 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002258 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2259 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002260 }
2261 break;
2262 }
2263
2264 case Primitive::kPrimNot: {
2265 DCHECK(!codegen_->IsLeafMethod());
2266 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01002267 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002268 break;
2269 }
2270
2271 case Primitive::kPrimLong: {
2272 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002273 if (index.IsConstant()) {
2274 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002275 if (value.IsRegisterPair()) {
2276 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
2277 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002278 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002279 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002280 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
2281 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
2282 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
2283 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002284 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002285 if (value.IsRegisterPair()) {
2286 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
2287 value.AsRegisterPairLow<Register>());
2288 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
2289 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002290 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002291 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002292 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002293 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002294 Immediate(Low32Bits(val)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002295 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002296 Immediate(High32Bits(val)));
2297 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002298 }
2299 break;
2300 }
2301
2302 case Primitive::kPrimFloat:
2303 case Primitive::kPrimDouble:
2304 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002305 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002306 case Primitive::kPrimVoid:
2307 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002308 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002309 }
2310}
2311
2312void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
2313 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002314 locations->SetInAt(0, Location::RequiresRegister());
2315 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002316 instruction->SetLocations(locations);
2317}
2318
2319void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
2320 LocationSummary* locations = instruction->GetLocations();
2321 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002322 Register obj = locations->InAt(0).As<Register>();
2323 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002324 __ movl(out, Address(obj, offset));
2325}
2326
2327void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002328 LocationSummary* locations =
2329 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002330 locations->SetInAt(0, Location::RequiresRegister());
2331 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002332 if (instruction->HasUses()) {
2333 locations->SetOut(Location::SameAsFirstInput());
2334 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002335}
2336
2337void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
2338 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002339 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002340 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002341 codegen_->AddSlowPath(slow_path);
2342
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002343 Register index = locations->InAt(0).As<Register>();
2344 Register length = locations->InAt(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002345
2346 __ cmpl(index, length);
2347 __ j(kAboveEqual, slow_path->GetEntryLabel());
2348}
2349
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002350void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
2351 temp->SetLocations(nullptr);
2352}
2353
2354void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
2355 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002356 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002357}
2358
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002359void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002360 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002361 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002362}
2363
2364void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002365 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2366}
2367
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002368void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
2369 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2370}
2371
2372void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002373 HBasicBlock* block = instruction->GetBlock();
2374 if (block->GetLoopInformation() != nullptr) {
2375 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2376 // The back edge will generate the suspend check.
2377 return;
2378 }
2379 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2380 // The goto will generate the suspend check.
2381 return;
2382 }
2383 GenerateSuspendCheck(instruction, nullptr);
2384}
2385
2386void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
2387 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002388 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002389 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002390 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002391 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002392 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002393 if (successor == nullptr) {
2394 __ j(kNotEqual, slow_path->GetEntryLabel());
2395 __ Bind(slow_path->GetReturnLabel());
2396 } else {
2397 __ j(kEqual, codegen_->GetLabelOf(successor));
2398 __ jmp(slow_path->GetEntryLabel());
2399 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002400}
2401
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002402X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
2403 return codegen_->GetAssembler();
2404}
2405
2406void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
2407 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002408 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002409 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2410 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
2411 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
2412}
2413
2414void ParallelMoveResolverX86::EmitMove(size_t index) {
2415 MoveOperands* move = moves_.Get(index);
2416 Location source = move->GetSource();
2417 Location destination = move->GetDestination();
2418
2419 if (source.IsRegister()) {
2420 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002421 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002422 } else {
2423 DCHECK(destination.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002424 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002425 }
2426 } else if (source.IsStackSlot()) {
2427 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002428 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002429 } else {
2430 DCHECK(destination.IsStackSlot());
2431 MoveMemoryToMemory(destination.GetStackIndex(),
2432 source.GetStackIndex());
2433 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002434 } else if (source.IsConstant()) {
2435 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
2436 Immediate imm(instruction->AsIntConstant()->GetValue());
2437 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002438 __ movl(destination.As<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002439 } else {
2440 __ movl(Address(ESP, destination.GetStackIndex()), imm);
2441 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002442 } else {
2443 LOG(FATAL) << "Unimplemented";
2444 }
2445}
2446
2447void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002448 Register suggested_scratch = reg == EAX ? EBX : EAX;
2449 ScratchRegisterScope ensure_scratch(
2450 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2451
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002452 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2453 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
2454 __ movl(Address(ESP, mem + stack_offset), reg);
2455 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
2456}
2457
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002458void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
2459 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002460 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
2461
2462 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002463 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002464 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2465
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002466 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
2467 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
2468 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
2469 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
2470 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
2471 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
2472}
2473
2474void ParallelMoveResolverX86::EmitSwap(size_t index) {
2475 MoveOperands* move = moves_.Get(index);
2476 Location source = move->GetSource();
2477 Location destination = move->GetDestination();
2478
2479 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002480 __ xchgl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002481 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002482 Exchange(source.As<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002483 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002484 Exchange(destination.As<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002485 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
2486 Exchange(destination.GetStackIndex(), source.GetStackIndex());
2487 } else {
2488 LOG(FATAL) << "Unimplemented";
2489 }
2490}
2491
2492void ParallelMoveResolverX86::SpillScratch(int reg) {
2493 __ pushl(static_cast<Register>(reg));
2494}
2495
2496void ParallelMoveResolverX86::RestoreScratch(int reg) {
2497 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002498}
2499
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002500void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002501 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2502 ? LocationSummary::kCallOnSlowPath
2503 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002504 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002505 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002506 locations->SetOut(Location::RequiresRegister());
2507}
2508
2509void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
2510 Register out = cls->GetLocations()->Out().As<Register>();
2511 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002512 DCHECK(!cls->CanCallRuntime());
2513 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002514 codegen_->LoadCurrentMethod(out);
2515 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2516 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002517 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002518 codegen_->LoadCurrentMethod(out);
2519 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2520 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002521
2522 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
2523 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2524 codegen_->AddSlowPath(slow_path);
2525 __ testl(out, out);
2526 __ j(kEqual, slow_path->GetEntryLabel());
2527 if (cls->MustGenerateClinitCheck()) {
2528 GenerateClassInitializationCheck(slow_path, out);
2529 } else {
2530 __ Bind(slow_path->GetExitLabel());
2531 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002532 }
2533}
2534
2535void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
2536 LocationSummary* locations =
2537 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2538 locations->SetInAt(0, Location::RequiresRegister());
2539 if (check->HasUses()) {
2540 locations->SetOut(Location::SameAsFirstInput());
2541 }
2542}
2543
2544void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002545 // We assume the class to not be null.
2546 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
2547 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002548 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002549 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<Register>());
2550}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002551
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002552void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
2553 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002554 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2555 Immediate(mirror::Class::kStatusInitialized));
2556 __ j(kLess, slow_path->GetEntryLabel());
2557 __ Bind(slow_path->GetExitLabel());
2558 // No need for memory fence, thanks to the X86 memory model.
2559}
2560
2561void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2562 LocationSummary* locations =
2563 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2564 locations->SetInAt(0, Location::RequiresRegister());
2565 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2566}
2567
2568void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2569 LocationSummary* locations = instruction->GetLocations();
2570 Register cls = locations->InAt(0).As<Register>();
2571 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2572
2573 switch (instruction->GetType()) {
2574 case Primitive::kPrimBoolean: {
2575 Register out = locations->Out().As<Register>();
2576 __ movzxb(out, Address(cls, offset));
2577 break;
2578 }
2579
2580 case Primitive::kPrimByte: {
2581 Register out = locations->Out().As<Register>();
2582 __ movsxb(out, Address(cls, offset));
2583 break;
2584 }
2585
2586 case Primitive::kPrimShort: {
2587 Register out = locations->Out().As<Register>();
2588 __ movsxw(out, Address(cls, offset));
2589 break;
2590 }
2591
2592 case Primitive::kPrimChar: {
2593 Register out = locations->Out().As<Register>();
2594 __ movzxw(out, Address(cls, offset));
2595 break;
2596 }
2597
2598 case Primitive::kPrimInt:
2599 case Primitive::kPrimNot: {
2600 Register out = locations->Out().As<Register>();
2601 __ movl(out, Address(cls, offset));
2602 break;
2603 }
2604
2605 case Primitive::kPrimLong: {
2606 // TODO: support volatile.
2607 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(cls, offset));
2608 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(cls, kX86WordSize + offset));
2609 break;
2610 }
2611
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002612 case Primitive::kPrimFloat: {
2613 XmmRegister out = locations->Out().As<XmmRegister>();
2614 __ movss(out, Address(cls, offset));
2615 break;
2616 }
2617
2618 case Primitive::kPrimDouble: {
2619 XmmRegister out = locations->Out().As<XmmRegister>();
2620 __ movsd(out, Address(cls, offset));
2621 break;
2622 }
2623
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002624 case Primitive::kPrimVoid:
2625 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2626 UNREACHABLE();
2627 }
2628}
2629
2630void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2631 LocationSummary* locations =
2632 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2633 locations->SetInAt(0, Location::RequiresRegister());
2634 Primitive::Type field_type = instruction->GetFieldType();
2635 bool is_object_type = field_type == Primitive::kPrimNot;
2636 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2637 || (field_type == Primitive::kPrimByte);
2638 // The register allocator does not support multiple
2639 // inputs that die at entry with one in a specific register.
2640 if (is_byte_type) {
2641 // Ensure the value is in a byte register.
2642 locations->SetInAt(1, Location::RegisterLocation(EAX));
2643 } else {
2644 locations->SetInAt(1, Location::RequiresRegister());
2645 }
2646 // Temporary registers for the write barrier.
2647 if (is_object_type) {
2648 locations->AddTemp(Location::RequiresRegister());
2649 // Ensure the card is in a byte register.
2650 locations->AddTemp(Location::RegisterLocation(ECX));
2651 }
2652}
2653
2654void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2655 LocationSummary* locations = instruction->GetLocations();
2656 Register cls = locations->InAt(0).As<Register>();
2657 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2658 Primitive::Type field_type = instruction->GetFieldType();
2659
2660 switch (field_type) {
2661 case Primitive::kPrimBoolean:
2662 case Primitive::kPrimByte: {
2663 ByteRegister value = locations->InAt(1).As<ByteRegister>();
2664 __ movb(Address(cls, offset), value);
2665 break;
2666 }
2667
2668 case Primitive::kPrimShort:
2669 case Primitive::kPrimChar: {
2670 Register value = locations->InAt(1).As<Register>();
2671 __ movw(Address(cls, offset), value);
2672 break;
2673 }
2674
2675 case Primitive::kPrimInt:
2676 case Primitive::kPrimNot: {
2677 Register value = locations->InAt(1).As<Register>();
2678 __ movl(Address(cls, offset), value);
2679
2680 if (field_type == Primitive::kPrimNot) {
2681 Register temp = locations->GetTemp(0).As<Register>();
2682 Register card = locations->GetTemp(1).As<Register>();
2683 codegen_->MarkGCCard(temp, card, cls, value);
2684 }
2685 break;
2686 }
2687
2688 case Primitive::kPrimLong: {
2689 Location value = locations->InAt(1);
2690 __ movl(Address(cls, offset), value.AsRegisterPairLow<Register>());
2691 __ movl(Address(cls, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
2692 break;
2693 }
2694
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002695 case Primitive::kPrimFloat: {
2696 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2697 __ movss(Address(cls, offset), value);
2698 break;
2699 }
2700
2701 case Primitive::kPrimDouble: {
2702 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2703 __ movsd(Address(cls, offset), value);
2704 break;
2705 }
2706
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002707 case Primitive::kPrimVoid:
2708 LOG(FATAL) << "Unreachable type " << field_type;
2709 UNREACHABLE();
2710 }
2711}
2712
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002713void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
2714 LocationSummary* locations =
2715 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2716 locations->SetOut(Location::RequiresRegister());
2717}
2718
2719void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
2720 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
2721 codegen_->AddSlowPath(slow_path);
2722
2723 Register out = load->GetLocations()->Out().As<Register>();
2724 codegen_->LoadCurrentMethod(out);
2725 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
2726 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2727 __ testl(out, out);
2728 __ j(kEqual, slow_path->GetEntryLabel());
2729 __ Bind(slow_path->GetExitLabel());
2730}
2731
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002732void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
2733 LocationSummary* locations =
2734 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2735 locations->SetOut(Location::RequiresRegister());
2736}
2737
2738void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
2739 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
2740 __ fs()->movl(load->GetLocations()->Out().As<Register>(), address);
2741 __ fs()->movl(address, Immediate(0));
2742}
2743
2744void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
2745 LocationSummary* locations =
2746 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2747 InvokeRuntimeCallingConvention calling_convention;
2748 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2749}
2750
2751void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
2752 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
2753 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2754}
2755
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002756void LocationsBuilderX86::VisitTypeCheck(HTypeCheck* instruction) {
2757 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
2758 ? LocationSummary::kNoCall
2759 : LocationSummary::kCallOnSlowPath;
2760 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2761 locations->SetInAt(0, Location::RequiresRegister());
2762 locations->SetInAt(1, Location::Any());
2763 locations->SetOut(Location::RequiresRegister());
2764}
2765
2766void InstructionCodeGeneratorX86::VisitTypeCheck(HTypeCheck* instruction) {
2767 LocationSummary* locations = instruction->GetLocations();
2768 Register obj = locations->InAt(0).As<Register>();
2769 Location cls = locations->InAt(1);
2770 Register out = locations->Out().As<Register>();
2771 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2772 Label done, zero;
2773 SlowPathCodeX86* slow_path = nullptr;
2774
2775 // Return 0 if `obj` is null.
2776 // TODO: avoid this check if we know obj is not null.
2777 __ testl(obj, obj);
2778 __ j(kEqual, &zero);
2779 __ movl(out, Address(obj, class_offset));
2780 // Compare the class of `obj` with `cls`.
2781 if (cls.IsRegister()) {
2782 __ cmpl(out, cls.As<Register>());
2783 } else {
2784 DCHECK(cls.IsStackSlot()) << cls;
2785 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
2786 }
2787
2788 if (instruction->IsClassFinal()) {
2789 // Classes must be equal for the instanceof to succeed.
2790 __ j(kNotEqual, &zero);
2791 __ movl(out, Immediate(1));
2792 __ jmp(&done);
2793 } else {
2794 // If the classes are not equal, we go into a slow path.
2795 DCHECK(locations->OnlyCallsOnSlowPath());
2796 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
2797 instruction, Location::RegisterLocation(out));
2798 codegen_->AddSlowPath(slow_path);
2799 __ j(kNotEqual, slow_path->GetEntryLabel());
2800 __ movl(out, Immediate(1));
2801 __ jmp(&done);
2802 }
2803 __ Bind(&zero);
2804 __ movl(out, Immediate(0));
2805 if (slow_path != nullptr) {
2806 __ Bind(slow_path->GetExitLabel());
2807 }
2808 __ Bind(&done);
2809}
2810
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002811} // namespace x86
2812} // namespace art