blob: 82591b0ebf8124e01aa7fdf6ac0c497e084bd8b1 [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:
1269 // int-to-long conversion.
1270 locations->SetInAt(0, Location::RegisterLocation(EAX));
1271 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1272 break;
1273
1274 case Primitive::kPrimFloat:
1275 case Primitive::kPrimDouble:
1276 LOG(FATAL) << "Type conversion from " << input_type << " to "
1277 << result_type << " not yet implemented";
1278 break;
1279
1280 default:
1281 LOG(FATAL) << "Unexpected type conversion from " << input_type
1282 << " to " << result_type;
1283 }
1284 break;
1285
1286 case Primitive::kPrimInt:
1287 case Primitive::kPrimFloat:
1288 case Primitive::kPrimDouble:
1289 LOG(FATAL) << "Type conversion from " << input_type
1290 << " to " << result_type << " not yet implemented";
1291 break;
1292
1293 default:
1294 LOG(FATAL) << "Unexpected type conversion from " << input_type
1295 << " to " << result_type;
1296 }
1297}
1298
1299void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1300 LocationSummary* locations = conversion->GetLocations();
1301 Location out = locations->Out();
1302 Location in = locations->InAt(0);
1303 Primitive::Type result_type = conversion->GetResultType();
1304 Primitive::Type input_type = conversion->GetInputType();
1305 switch (result_type) {
1306 case Primitive::kPrimLong:
1307 switch (input_type) {
1308 case Primitive::kPrimByte:
1309 case Primitive::kPrimShort:
1310 case Primitive::kPrimInt:
1311 // int-to-long conversion.
1312 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1313 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
1314 DCHECK_EQ(in.As<Register>(), EAX);
1315 __ cdq();
1316 break;
1317
1318 case Primitive::kPrimFloat:
1319 case Primitive::kPrimDouble:
1320 LOG(FATAL) << "Type conversion from " << input_type << " to "
1321 << result_type << " not yet implemented";
1322 break;
1323
1324 default:
1325 LOG(FATAL) << "Unexpected type conversion from " << input_type
1326 << " to " << result_type;
1327 }
1328 break;
1329
1330 case Primitive::kPrimInt:
1331 case Primitive::kPrimFloat:
1332 case Primitive::kPrimDouble:
1333 LOG(FATAL) << "Type conversion from " << input_type
1334 << " to " << result_type << " not yet implemented";
1335 break;
1336
1337 default:
1338 LOG(FATAL) << "Unexpected type conversion from " << input_type
1339 << " to " << result_type;
1340 }
1341}
1342
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001343void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001344 LocationSummary* locations =
1345 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001346 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001347 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001348 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001349 locations->SetInAt(0, Location::RequiresRegister());
1350 locations->SetInAt(1, Location::Any());
1351 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001352 break;
1353 }
1354
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001355 case Primitive::kPrimFloat:
1356 case Primitive::kPrimDouble: {
1357 locations->SetInAt(0, Location::RequiresFpuRegister());
1358 locations->SetInAt(1, Location::Any());
1359 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001360 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001361 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001362
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001363 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001364 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1365 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001366 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001367}
1368
1369void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1370 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001371 Location first = locations->InAt(0);
1372 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001373 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001374 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001375 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001376 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001377 __ addl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001378 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001379 __ addl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001380 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001381 __ addl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001382 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001383 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001384 }
1385
1386 case Primitive::kPrimLong: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001387 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001388 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1389 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001390 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001391 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1392 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001393 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001394 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001395 break;
1396 }
1397
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001398 case Primitive::kPrimFloat: {
1399 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001400 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001401 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001402 __ addss(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001403 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001404 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001405 }
1406
1407 case Primitive::kPrimDouble: {
1408 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001409 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001410 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001411 __ addsd(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001412 }
1413 break;
1414 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001415
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001416 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001417 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001418 }
1419}
1420
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001421void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001422 LocationSummary* locations =
1423 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001424 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001425 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001426 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001427 locations->SetInAt(0, Location::RequiresRegister());
1428 locations->SetInAt(1, Location::Any());
1429 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001430 break;
1431 }
Calin Juravle11351682014-10-23 15:38:15 +01001432 case Primitive::kPrimFloat:
1433 case Primitive::kPrimDouble: {
1434 locations->SetInAt(0, Location::RequiresFpuRegister());
1435 locations->SetInAt(1, Location::RequiresFpuRegister());
1436 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001437 break;
Calin Juravle11351682014-10-23 15:38:15 +01001438 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001439
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001440 default:
Calin Juravle11351682014-10-23 15:38:15 +01001441 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001442 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001443}
1444
1445void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1446 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001447 Location first = locations->InAt(0);
1448 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001449 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001450 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001451 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001452 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001453 __ subl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001454 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001455 __ subl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001456 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001457 __ subl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001458 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001459 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001460 }
1461
1462 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001463 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001464 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1465 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001466 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001467 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001468 __ sbbl(first.AsRegisterPairHigh<Register>(),
1469 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001470 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001471 break;
1472 }
1473
Calin Juravle11351682014-10-23 15:38:15 +01001474 case Primitive::kPrimFloat: {
1475 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001476 break;
Calin Juravle11351682014-10-23 15:38:15 +01001477 }
1478
1479 case Primitive::kPrimDouble: {
1480 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1481 break;
1482 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001483
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001484 default:
Calin Juravle11351682014-10-23 15:38:15 +01001485 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001486 }
1487}
1488
Calin Juravle34bacdf2014-10-07 20:23:36 +01001489void LocationsBuilderX86::VisitMul(HMul* mul) {
1490 LocationSummary* locations =
1491 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1492 switch (mul->GetResultType()) {
1493 case Primitive::kPrimInt:
1494 locations->SetInAt(0, Location::RequiresRegister());
1495 locations->SetInAt(1, Location::Any());
1496 locations->SetOut(Location::SameAsFirstInput());
1497 break;
1498 case Primitive::kPrimLong: {
1499 locations->SetInAt(0, Location::RequiresRegister());
1500 // TODO: Currently this handles only stack operands:
1501 // - we don't have enough registers because we currently use Quick ABI.
1502 // - by the time we have a working register allocator we will probably change the ABI
1503 // and fix the above.
1504 // - we don't have a way yet to request operands on stack but the base line compiler
1505 // will leave the operands on the stack with Any().
1506 locations->SetInAt(1, Location::Any());
1507 locations->SetOut(Location::SameAsFirstInput());
1508 // Needed for imul on 32bits with 64bits output.
1509 locations->AddTemp(Location::RegisterLocation(EAX));
1510 locations->AddTemp(Location::RegisterLocation(EDX));
1511 break;
1512 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001513 case Primitive::kPrimFloat:
1514 case Primitive::kPrimDouble: {
1515 locations->SetInAt(0, Location::RequiresFpuRegister());
1516 locations->SetInAt(1, Location::RequiresFpuRegister());
1517 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001518 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001519 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001520
1521 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001522 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001523 }
1524}
1525
1526void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1527 LocationSummary* locations = mul->GetLocations();
1528 Location first = locations->InAt(0);
1529 Location second = locations->InAt(1);
1530 DCHECK(first.Equals(locations->Out()));
1531
1532 switch (mul->GetResultType()) {
1533 case Primitive::kPrimInt: {
1534 if (second.IsRegister()) {
1535 __ imull(first.As<Register>(), second.As<Register>());
1536 } else if (second.IsConstant()) {
1537 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1538 __ imull(first.As<Register>(), imm);
1539 } else {
1540 DCHECK(second.IsStackSlot());
1541 __ imull(first.As<Register>(), Address(ESP, second.GetStackIndex()));
1542 }
1543 break;
1544 }
1545
1546 case Primitive::kPrimLong: {
1547 DCHECK(second.IsDoubleStackSlot());
1548
1549 Register in1_hi = first.AsRegisterPairHigh<Register>();
1550 Register in1_lo = first.AsRegisterPairLow<Register>();
1551 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
1552 Address in2_lo(ESP, second.GetStackIndex());
1553 Register eax = locations->GetTemp(0).As<Register>();
1554 Register edx = locations->GetTemp(1).As<Register>();
1555
1556 DCHECK_EQ(EAX, eax);
1557 DCHECK_EQ(EDX, edx);
1558
1559 // input: in1 - 64 bits, in2 - 64 bits
1560 // output: in1
1561 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
1562 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
1563 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
1564
1565 __ movl(eax, in2_hi);
1566 // eax <- in1.lo * in2.hi
1567 __ imull(eax, in1_lo);
1568 // in1.hi <- in1.hi * in2.lo
1569 __ imull(in1_hi, in2_lo);
1570 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
1571 __ addl(in1_hi, eax);
1572 // move in1_lo to eax to prepare for double precision
1573 __ movl(eax, in1_lo);
1574 // edx:eax <- in1.lo * in2.lo
1575 __ mull(in2_lo);
1576 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
1577 __ addl(in1_hi, edx);
1578 // in1.lo <- (in1.lo * in2.lo)[31:0];
1579 __ movl(in1_lo, eax);
1580
1581 break;
1582 }
1583
Calin Juravleb5bfa962014-10-21 18:02:24 +01001584 case Primitive::kPrimFloat: {
1585 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001586 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001587 }
1588
1589 case Primitive::kPrimDouble: {
1590 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1591 break;
1592 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001593
1594 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001595 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001596 }
1597}
1598
Calin Juravle7c4954d2014-10-28 16:57:40 +00001599void LocationsBuilderX86::VisitDiv(HDiv* div) {
1600 LocationSummary* locations =
1601 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1602 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001603 case Primitive::kPrimInt: {
1604 locations->SetInAt(0, Location::RegisterLocation(EAX));
1605 locations->SetInAt(1, Location::RequiresRegister());
1606 locations->SetOut(Location::SameAsFirstInput());
1607 // Intel uses edx:eax as the dividend.
1608 locations->AddTemp(Location::RegisterLocation(EDX));
1609 break;
1610 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001611 case Primitive::kPrimLong: {
1612 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1613 break;
1614 }
1615 case Primitive::kPrimFloat:
1616 case Primitive::kPrimDouble: {
1617 locations->SetInAt(0, Location::RequiresFpuRegister());
1618 locations->SetInAt(1, Location::RequiresFpuRegister());
1619 locations->SetOut(Location::SameAsFirstInput());
1620 break;
1621 }
1622
1623 default:
1624 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1625 }
1626}
1627
1628void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
1629 LocationSummary* locations = div->GetLocations();
1630 Location first = locations->InAt(0);
1631 Location second = locations->InAt(1);
1632 DCHECK(first.Equals(locations->Out()));
1633
1634 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001635 case Primitive::kPrimInt: {
1636 Register first_reg = first.As<Register>();
1637 Register second_reg = second.As<Register>();
1638 DCHECK_EQ(EAX, first_reg);
1639 DCHECK_EQ(EDX, locations->GetTemp(0).As<Register>());
1640
1641 SlowPathCodeX86* slow_path =
1642 new (GetGraph()->GetArena()) DivMinusOneSlowPathX86(first_reg);
1643 codegen_->AddSlowPath(slow_path);
1644
1645 // 0x80000000/-1 triggers an arithmetic exception!
1646 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
1647 // it's safe to just use negl instead of more complex comparisons.
1648
1649 __ cmpl(second_reg, Immediate(-1));
1650 __ j(kEqual, slow_path->GetEntryLabel());
1651
1652 // edx:eax <- sign-extended of eax
1653 __ cdq();
1654 // eax = quotient, edx = remainder
1655 __ idivl(second_reg);
1656
1657 __ Bind(slow_path->GetExitLabel());
1658 break;
1659 }
1660
Calin Juravle7c4954d2014-10-28 16:57:40 +00001661 case Primitive::kPrimLong: {
1662 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1663 break;
1664 }
1665
1666 case Primitive::kPrimFloat: {
1667 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1668 break;
1669 }
1670
1671 case Primitive::kPrimDouble: {
1672 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1673 break;
1674 }
1675
1676 default:
1677 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1678 }
1679}
1680
Calin Juravled0d48522014-11-04 16:40:20 +00001681void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1682 LocationSummary* locations =
1683 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1684 locations->SetInAt(0, Location::Any());
1685 if (instruction->HasUses()) {
1686 locations->SetOut(Location::SameAsFirstInput());
1687 }
1688}
1689
1690void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1691 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
1692 codegen_->AddSlowPath(slow_path);
1693
1694 LocationSummary* locations = instruction->GetLocations();
1695 Location value = locations->InAt(0);
1696
1697 if (value.IsRegister()) {
1698 __ testl(value.As<Register>(), value.As<Register>());
1699 } else if (value.IsStackSlot()) {
1700 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
1701 } else {
1702 DCHECK(value.IsConstant()) << value;
1703 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
1704 __ jmp(slow_path->GetEntryLabel());
1705 }
1706 return;
1707 }
1708 __ j(kEqual, slow_path->GetEntryLabel());
1709}
1710
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001711void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001712 LocationSummary* locations =
1713 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001714 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001715 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001716 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1717 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001718}
1719
1720void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
1721 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001722 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001723 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001724
Nicolas Geoffray707c8092014-04-04 10:50:14 +01001725 __ fs()->call(
1726 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001727
Nicolas Geoffray39468442014-09-02 15:17:15 +01001728 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001729 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001730}
1731
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001732void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
1733 LocationSummary* locations =
1734 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1735 locations->SetOut(Location::RegisterLocation(EAX));
1736 InvokeRuntimeCallingConvention calling_convention;
1737 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1738 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1739 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1740}
1741
1742void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
1743 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001744 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001745 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
1746
1747 __ fs()->call(
1748 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
1749
1750 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1751 DCHECK(!codegen_->IsLeafMethod());
1752}
1753
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001754void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001755 LocationSummary* locations =
1756 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001757 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1758 if (location.IsStackSlot()) {
1759 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1760 } else if (location.IsDoubleStackSlot()) {
1761 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001762 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001763 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001764}
1765
1766void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001767 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001768}
1769
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001770void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001771 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001772 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001773 locations->SetInAt(0, Location::RequiresRegister());
1774 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001775}
1776
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001777void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
1778 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01001779 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001780 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01001781 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001782 switch (not_->InputAt(0)->GetType()) {
1783 case Primitive::kPrimBoolean:
1784 __ xorl(out.As<Register>(), Immediate(1));
1785 break;
1786
1787 case Primitive::kPrimInt:
1788 __ notl(out.As<Register>());
1789 break;
1790
1791 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001792 __ notl(out.AsRegisterPairLow<Register>());
1793 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001794 break;
1795
1796 default:
1797 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1798 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001799}
1800
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001801void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001802 LocationSummary* locations =
1803 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001804 locations->SetInAt(0, Location::RequiresRegister());
1805 locations->SetInAt(1, Location::Any());
1806 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001807}
1808
1809void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001810 LocationSummary* locations = compare->GetLocations();
1811 switch (compare->InputAt(0)->GetType()) {
1812 case Primitive::kPrimLong: {
1813 Label less, greater, done;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001814 Register output = locations->Out().As<Register>();
1815 Location left = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001816 Location right = locations->InAt(1);
1817 if (right.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001818 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001819 } else {
1820 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001821 __ cmpl(left.AsRegisterPairHigh<Register>(),
1822 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001823 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001824 __ j(kLess, &less); // Signed compare.
1825 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001826 if (right.IsRegisterPair()) {
1827 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001828 } else {
1829 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001830 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001831 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001832 __ movl(output, Immediate(0));
1833 __ j(kEqual, &done);
1834 __ j(kBelow, &less); // Unsigned compare.
1835
1836 __ Bind(&greater);
1837 __ movl(output, Immediate(1));
1838 __ jmp(&done);
1839
1840 __ Bind(&less);
1841 __ movl(output, Immediate(-1));
1842
1843 __ Bind(&done);
1844 break;
1845 }
1846 default:
1847 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
1848 }
1849}
1850
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001851void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001852 LocationSummary* locations =
1853 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001854 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1855 locations->SetInAt(i, Location::Any());
1856 }
1857 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001858}
1859
1860void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001861 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001862 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001863}
1864
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001865void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001866 LocationSummary* locations =
1867 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001868 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001869 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001870 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001871 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
1872 || (field_type == Primitive::kPrimByte);
1873 // The register allocator does not support multiple
1874 // inputs that die at entry with one in a specific register.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001875 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001876 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001877 locations->SetInAt(1, Location::RegisterLocation(EAX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001878 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001879 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001880 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001881 // Temporary registers for the write barrier.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001882 if (is_object_type) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001883 locations->AddTemp(Location::RequiresRegister());
1884 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001885 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001886 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001887}
1888
1889void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1890 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001891 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001892 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001893 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001894
1895 switch (field_type) {
1896 case Primitive::kPrimBoolean:
1897 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001898 ByteRegister value = locations->InAt(1).As<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001899 __ movb(Address(obj, offset), value);
1900 break;
1901 }
1902
1903 case Primitive::kPrimShort:
1904 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001905 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001906 __ movw(Address(obj, offset), value);
1907 break;
1908 }
1909
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001910 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001911 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001912 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001913 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001914
1915 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001916 Register temp = locations->GetTemp(0).As<Register>();
1917 Register card = locations->GetTemp(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001918 codegen_->MarkGCCard(temp, card, obj, value);
1919 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001920 break;
1921 }
1922
1923 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001924 Location value = locations->InAt(1);
1925 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1926 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001927 break;
1928 }
1929
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001930 case Primitive::kPrimFloat: {
1931 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1932 __ movss(Address(obj, offset), value);
1933 break;
1934 }
1935
1936 case Primitive::kPrimDouble: {
1937 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1938 __ movsd(Address(obj, offset), value);
1939 break;
1940 }
1941
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001942 case Primitive::kPrimVoid:
1943 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001944 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001945 }
1946}
1947
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001948void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
1949 Label is_null;
1950 __ testl(value, value);
1951 __ j(kEqual, &is_null);
1952 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
1953 __ movl(temp, object);
1954 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
1955 __ movb(Address(temp, card, TIMES_1, 0),
1956 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
1957 __ Bind(&is_null);
1958}
1959
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001960void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001961 LocationSummary* locations =
1962 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001963 locations->SetInAt(0, Location::RequiresRegister());
1964 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001965}
1966
1967void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1968 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001969 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001970 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
1971
1972 switch (instruction->GetType()) {
1973 case Primitive::kPrimBoolean: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001974 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001975 __ movzxb(out, Address(obj, offset));
1976 break;
1977 }
1978
1979 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001980 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001981 __ movsxb(out, Address(obj, offset));
1982 break;
1983 }
1984
1985 case Primitive::kPrimShort: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001986 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001987 __ movsxw(out, Address(obj, offset));
1988 break;
1989 }
1990
1991 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001992 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001993 __ movzxw(out, Address(obj, offset));
1994 break;
1995 }
1996
1997 case Primitive::kPrimInt:
1998 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001999 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002000 __ movl(out, Address(obj, offset));
2001 break;
2002 }
2003
2004 case Primitive::kPrimLong: {
2005 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002006 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
2007 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002008 break;
2009 }
2010
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002011 case Primitive::kPrimFloat: {
2012 XmmRegister out = locations->Out().As<XmmRegister>();
2013 __ movss(out, Address(obj, offset));
2014 break;
2015 }
2016
2017 case Primitive::kPrimDouble: {
2018 XmmRegister out = locations->Out().As<XmmRegister>();
2019 __ movsd(out, Address(obj, offset));
2020 break;
2021 }
2022
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002023 case Primitive::kPrimVoid:
2024 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002025 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002026 }
2027}
2028
2029void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002030 LocationSummary* locations =
2031 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002032 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002033 if (instruction->HasUses()) {
2034 locations->SetOut(Location::SameAsFirstInput());
2035 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002036}
2037
2038void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002039 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002040 codegen_->AddSlowPath(slow_path);
2041
2042 LocationSummary* locations = instruction->GetLocations();
2043 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002044
2045 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002046 __ cmpl(obj.As<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002047 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002048 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002049 } else {
2050 DCHECK(obj.IsConstant()) << obj;
2051 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2052 __ jmp(slow_path->GetEntryLabel());
2053 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002054 }
2055 __ j(kEqual, slow_path->GetEntryLabel());
2056}
2057
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002058void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002059 LocationSummary* locations =
2060 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002061 locations->SetInAt(0, Location::RequiresRegister());
2062 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2063 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002064}
2065
2066void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
2067 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002068 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002069 Location index = locations->InAt(1);
2070
2071 switch (instruction->GetType()) {
2072 case Primitive::kPrimBoolean: {
2073 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002074 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002075 if (index.IsConstant()) {
2076 __ movzxb(out, Address(obj,
2077 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2078 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002079 __ movzxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002080 }
2081 break;
2082 }
2083
2084 case Primitive::kPrimByte: {
2085 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002086 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002087 if (index.IsConstant()) {
2088 __ movsxb(out, Address(obj,
2089 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2090 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002091 __ movsxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002092 }
2093 break;
2094 }
2095
2096 case Primitive::kPrimShort: {
2097 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002098 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002099 if (index.IsConstant()) {
2100 __ movsxw(out, Address(obj,
2101 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2102 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002103 __ movsxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002104 }
2105 break;
2106 }
2107
2108 case Primitive::kPrimChar: {
2109 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002110 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002111 if (index.IsConstant()) {
2112 __ movzxw(out, Address(obj,
2113 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2114 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002115 __ movzxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002116 }
2117 break;
2118 }
2119
2120 case Primitive::kPrimInt:
2121 case Primitive::kPrimNot: {
2122 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002123 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002124 if (index.IsConstant()) {
2125 __ movl(out, Address(obj,
2126 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2127 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002128 __ movl(out, Address(obj, index.As<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002129 }
2130 break;
2131 }
2132
2133 case Primitive::kPrimLong: {
2134 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002135 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002136 if (index.IsConstant()) {
2137 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002138 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
2139 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002140 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002141 __ movl(out.AsRegisterPairLow<Register>(),
2142 Address(obj, index.As<Register>(), TIMES_8, data_offset));
2143 __ movl(out.AsRegisterPairHigh<Register>(),
2144 Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002145 }
2146 break;
2147 }
2148
2149 case Primitive::kPrimFloat:
2150 case Primitive::kPrimDouble:
2151 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002152 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002153 case Primitive::kPrimVoid:
2154 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002155 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002156 }
2157}
2158
2159void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002160 Primitive::Type value_type = instruction->GetComponentType();
2161 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2162 instruction,
2163 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
2164
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002165 if (value_type == Primitive::kPrimNot) {
2166 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002167 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2168 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2169 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002170 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002171 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
2172 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002173 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002174 // In case of a byte operation, the register allocator does not support multiple
2175 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002176 locations->SetInAt(0, Location::RequiresRegister());
2177 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002178 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002179 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002180 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002181 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002182 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002183 }
2184 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002185}
2186
2187void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
2188 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002189 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002190 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002191 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002192 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002193
2194 switch (value_type) {
2195 case Primitive::kPrimBoolean:
2196 case Primitive::kPrimByte: {
2197 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002198 if (index.IsConstant()) {
2199 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002200 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002201 __ movb(Address(obj, offset), value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002202 } else {
2203 __ movb(Address(obj, offset),
2204 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2205 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002206 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002207 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002208 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
2209 value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002210 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002211 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002212 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2213 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002214 }
2215 break;
2216 }
2217
2218 case Primitive::kPrimShort:
2219 case Primitive::kPrimChar: {
2220 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002221 if (index.IsConstant()) {
2222 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002223 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002224 __ movw(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002225 } else {
2226 __ movw(Address(obj, offset),
2227 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2228 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002229 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002230 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002231 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
2232 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002233 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002234 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002235 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2236 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002237 }
2238 break;
2239 }
2240
2241 case Primitive::kPrimInt: {
2242 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002243 if (index.IsConstant()) {
2244 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002245 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002246 __ movl(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002247 } else {
2248 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2249 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002250 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002251 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002252 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
2253 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002254 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002255 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002256 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2257 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002258 }
2259 break;
2260 }
2261
2262 case Primitive::kPrimNot: {
2263 DCHECK(!codegen_->IsLeafMethod());
2264 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01002265 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002266 break;
2267 }
2268
2269 case Primitive::kPrimLong: {
2270 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002271 if (index.IsConstant()) {
2272 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002273 if (value.IsRegisterPair()) {
2274 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
2275 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002276 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002277 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002278 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
2279 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
2280 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
2281 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002282 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002283 if (value.IsRegisterPair()) {
2284 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
2285 value.AsRegisterPairLow<Register>());
2286 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
2287 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002288 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002289 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002290 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002291 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002292 Immediate(Low32Bits(val)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002293 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002294 Immediate(High32Bits(val)));
2295 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002296 }
2297 break;
2298 }
2299
2300 case Primitive::kPrimFloat:
2301 case Primitive::kPrimDouble:
2302 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002303 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002304 case Primitive::kPrimVoid:
2305 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002306 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002307 }
2308}
2309
2310void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
2311 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002312 locations->SetInAt(0, Location::RequiresRegister());
2313 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002314 instruction->SetLocations(locations);
2315}
2316
2317void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
2318 LocationSummary* locations = instruction->GetLocations();
2319 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002320 Register obj = locations->InAt(0).As<Register>();
2321 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002322 __ movl(out, Address(obj, offset));
2323}
2324
2325void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002326 LocationSummary* locations =
2327 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002328 locations->SetInAt(0, Location::RequiresRegister());
2329 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002330 if (instruction->HasUses()) {
2331 locations->SetOut(Location::SameAsFirstInput());
2332 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002333}
2334
2335void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
2336 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002337 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002338 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002339 codegen_->AddSlowPath(slow_path);
2340
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002341 Register index = locations->InAt(0).As<Register>();
2342 Register length = locations->InAt(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002343
2344 __ cmpl(index, length);
2345 __ j(kAboveEqual, slow_path->GetEntryLabel());
2346}
2347
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002348void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
2349 temp->SetLocations(nullptr);
2350}
2351
2352void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
2353 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002354 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002355}
2356
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002357void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002358 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002359 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002360}
2361
2362void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002363 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2364}
2365
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002366void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
2367 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2368}
2369
2370void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002371 HBasicBlock* block = instruction->GetBlock();
2372 if (block->GetLoopInformation() != nullptr) {
2373 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2374 // The back edge will generate the suspend check.
2375 return;
2376 }
2377 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2378 // The goto will generate the suspend check.
2379 return;
2380 }
2381 GenerateSuspendCheck(instruction, nullptr);
2382}
2383
2384void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
2385 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002386 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002387 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002388 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002389 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002390 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002391 if (successor == nullptr) {
2392 __ j(kNotEqual, slow_path->GetEntryLabel());
2393 __ Bind(slow_path->GetReturnLabel());
2394 } else {
2395 __ j(kEqual, codegen_->GetLabelOf(successor));
2396 __ jmp(slow_path->GetEntryLabel());
2397 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002398}
2399
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002400X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
2401 return codegen_->GetAssembler();
2402}
2403
2404void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
2405 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002406 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002407 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2408 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
2409 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
2410}
2411
2412void ParallelMoveResolverX86::EmitMove(size_t index) {
2413 MoveOperands* move = moves_.Get(index);
2414 Location source = move->GetSource();
2415 Location destination = move->GetDestination();
2416
2417 if (source.IsRegister()) {
2418 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002419 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002420 } else {
2421 DCHECK(destination.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002422 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002423 }
2424 } else if (source.IsStackSlot()) {
2425 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002426 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002427 } else {
2428 DCHECK(destination.IsStackSlot());
2429 MoveMemoryToMemory(destination.GetStackIndex(),
2430 source.GetStackIndex());
2431 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002432 } else if (source.IsConstant()) {
2433 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
2434 Immediate imm(instruction->AsIntConstant()->GetValue());
2435 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002436 __ movl(destination.As<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002437 } else {
2438 __ movl(Address(ESP, destination.GetStackIndex()), imm);
2439 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002440 } else {
2441 LOG(FATAL) << "Unimplemented";
2442 }
2443}
2444
2445void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002446 Register suggested_scratch = reg == EAX ? EBX : EAX;
2447 ScratchRegisterScope ensure_scratch(
2448 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2449
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002450 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2451 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
2452 __ movl(Address(ESP, mem + stack_offset), reg);
2453 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
2454}
2455
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002456void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
2457 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002458 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
2459
2460 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002461 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002462 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2463
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002464 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
2465 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
2466 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
2467 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
2468 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
2469 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
2470}
2471
2472void ParallelMoveResolverX86::EmitSwap(size_t index) {
2473 MoveOperands* move = moves_.Get(index);
2474 Location source = move->GetSource();
2475 Location destination = move->GetDestination();
2476
2477 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002478 __ xchgl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002479 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002480 Exchange(source.As<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002481 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002482 Exchange(destination.As<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002483 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
2484 Exchange(destination.GetStackIndex(), source.GetStackIndex());
2485 } else {
2486 LOG(FATAL) << "Unimplemented";
2487 }
2488}
2489
2490void ParallelMoveResolverX86::SpillScratch(int reg) {
2491 __ pushl(static_cast<Register>(reg));
2492}
2493
2494void ParallelMoveResolverX86::RestoreScratch(int reg) {
2495 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002496}
2497
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002498void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002499 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2500 ? LocationSummary::kCallOnSlowPath
2501 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002502 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002503 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002504 locations->SetOut(Location::RequiresRegister());
2505}
2506
2507void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
2508 Register out = cls->GetLocations()->Out().As<Register>();
2509 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002510 DCHECK(!cls->CanCallRuntime());
2511 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002512 codegen_->LoadCurrentMethod(out);
2513 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2514 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002515 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002516 codegen_->LoadCurrentMethod(out);
2517 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2518 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002519
2520 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
2521 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2522 codegen_->AddSlowPath(slow_path);
2523 __ testl(out, out);
2524 __ j(kEqual, slow_path->GetEntryLabel());
2525 if (cls->MustGenerateClinitCheck()) {
2526 GenerateClassInitializationCheck(slow_path, out);
2527 } else {
2528 __ Bind(slow_path->GetExitLabel());
2529 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002530 }
2531}
2532
2533void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
2534 LocationSummary* locations =
2535 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2536 locations->SetInAt(0, Location::RequiresRegister());
2537 if (check->HasUses()) {
2538 locations->SetOut(Location::SameAsFirstInput());
2539 }
2540}
2541
2542void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002543 // We assume the class to not be null.
2544 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
2545 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002546 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002547 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<Register>());
2548}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002549
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002550void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
2551 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002552 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2553 Immediate(mirror::Class::kStatusInitialized));
2554 __ j(kLess, slow_path->GetEntryLabel());
2555 __ Bind(slow_path->GetExitLabel());
2556 // No need for memory fence, thanks to the X86 memory model.
2557}
2558
2559void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2560 LocationSummary* locations =
2561 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2562 locations->SetInAt(0, Location::RequiresRegister());
2563 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2564}
2565
2566void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2567 LocationSummary* locations = instruction->GetLocations();
2568 Register cls = locations->InAt(0).As<Register>();
2569 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2570
2571 switch (instruction->GetType()) {
2572 case Primitive::kPrimBoolean: {
2573 Register out = locations->Out().As<Register>();
2574 __ movzxb(out, Address(cls, offset));
2575 break;
2576 }
2577
2578 case Primitive::kPrimByte: {
2579 Register out = locations->Out().As<Register>();
2580 __ movsxb(out, Address(cls, offset));
2581 break;
2582 }
2583
2584 case Primitive::kPrimShort: {
2585 Register out = locations->Out().As<Register>();
2586 __ movsxw(out, Address(cls, offset));
2587 break;
2588 }
2589
2590 case Primitive::kPrimChar: {
2591 Register out = locations->Out().As<Register>();
2592 __ movzxw(out, Address(cls, offset));
2593 break;
2594 }
2595
2596 case Primitive::kPrimInt:
2597 case Primitive::kPrimNot: {
2598 Register out = locations->Out().As<Register>();
2599 __ movl(out, Address(cls, offset));
2600 break;
2601 }
2602
2603 case Primitive::kPrimLong: {
2604 // TODO: support volatile.
2605 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(cls, offset));
2606 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(cls, kX86WordSize + offset));
2607 break;
2608 }
2609
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002610 case Primitive::kPrimFloat: {
2611 XmmRegister out = locations->Out().As<XmmRegister>();
2612 __ movss(out, Address(cls, offset));
2613 break;
2614 }
2615
2616 case Primitive::kPrimDouble: {
2617 XmmRegister out = locations->Out().As<XmmRegister>();
2618 __ movsd(out, Address(cls, offset));
2619 break;
2620 }
2621
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002622 case Primitive::kPrimVoid:
2623 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2624 UNREACHABLE();
2625 }
2626}
2627
2628void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2629 LocationSummary* locations =
2630 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2631 locations->SetInAt(0, Location::RequiresRegister());
2632 Primitive::Type field_type = instruction->GetFieldType();
2633 bool is_object_type = field_type == Primitive::kPrimNot;
2634 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2635 || (field_type == Primitive::kPrimByte);
2636 // The register allocator does not support multiple
2637 // inputs that die at entry with one in a specific register.
2638 if (is_byte_type) {
2639 // Ensure the value is in a byte register.
2640 locations->SetInAt(1, Location::RegisterLocation(EAX));
2641 } else {
2642 locations->SetInAt(1, Location::RequiresRegister());
2643 }
2644 // Temporary registers for the write barrier.
2645 if (is_object_type) {
2646 locations->AddTemp(Location::RequiresRegister());
2647 // Ensure the card is in a byte register.
2648 locations->AddTemp(Location::RegisterLocation(ECX));
2649 }
2650}
2651
2652void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2653 LocationSummary* locations = instruction->GetLocations();
2654 Register cls = locations->InAt(0).As<Register>();
2655 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2656 Primitive::Type field_type = instruction->GetFieldType();
2657
2658 switch (field_type) {
2659 case Primitive::kPrimBoolean:
2660 case Primitive::kPrimByte: {
2661 ByteRegister value = locations->InAt(1).As<ByteRegister>();
2662 __ movb(Address(cls, offset), value);
2663 break;
2664 }
2665
2666 case Primitive::kPrimShort:
2667 case Primitive::kPrimChar: {
2668 Register value = locations->InAt(1).As<Register>();
2669 __ movw(Address(cls, offset), value);
2670 break;
2671 }
2672
2673 case Primitive::kPrimInt:
2674 case Primitive::kPrimNot: {
2675 Register value = locations->InAt(1).As<Register>();
2676 __ movl(Address(cls, offset), value);
2677
2678 if (field_type == Primitive::kPrimNot) {
2679 Register temp = locations->GetTemp(0).As<Register>();
2680 Register card = locations->GetTemp(1).As<Register>();
2681 codegen_->MarkGCCard(temp, card, cls, value);
2682 }
2683 break;
2684 }
2685
2686 case Primitive::kPrimLong: {
2687 Location value = locations->InAt(1);
2688 __ movl(Address(cls, offset), value.AsRegisterPairLow<Register>());
2689 __ movl(Address(cls, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
2690 break;
2691 }
2692
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002693 case Primitive::kPrimFloat: {
2694 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2695 __ movss(Address(cls, offset), value);
2696 break;
2697 }
2698
2699 case Primitive::kPrimDouble: {
2700 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2701 __ movsd(Address(cls, offset), value);
2702 break;
2703 }
2704
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002705 case Primitive::kPrimVoid:
2706 LOG(FATAL) << "Unreachable type " << field_type;
2707 UNREACHABLE();
2708 }
2709}
2710
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002711void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
2712 LocationSummary* locations =
2713 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2714 locations->SetOut(Location::RequiresRegister());
2715}
2716
2717void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
2718 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
2719 codegen_->AddSlowPath(slow_path);
2720
2721 Register out = load->GetLocations()->Out().As<Register>();
2722 codegen_->LoadCurrentMethod(out);
2723 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
2724 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2725 __ testl(out, out);
2726 __ j(kEqual, slow_path->GetEntryLabel());
2727 __ Bind(slow_path->GetExitLabel());
2728}
2729
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002730void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
2731 LocationSummary* locations =
2732 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2733 locations->SetOut(Location::RequiresRegister());
2734}
2735
2736void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
2737 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
2738 __ fs()->movl(load->GetLocations()->Out().As<Register>(), address);
2739 __ fs()->movl(address, Immediate(0));
2740}
2741
2742void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
2743 LocationSummary* locations =
2744 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2745 InvokeRuntimeCallingConvention calling_convention;
2746 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2747}
2748
2749void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
2750 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
2751 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2752}
2753
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002754void LocationsBuilderX86::VisitTypeCheck(HTypeCheck* instruction) {
2755 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
2756 ? LocationSummary::kNoCall
2757 : LocationSummary::kCallOnSlowPath;
2758 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2759 locations->SetInAt(0, Location::RequiresRegister());
2760 locations->SetInAt(1, Location::Any());
2761 locations->SetOut(Location::RequiresRegister());
2762}
2763
2764void InstructionCodeGeneratorX86::VisitTypeCheck(HTypeCheck* instruction) {
2765 LocationSummary* locations = instruction->GetLocations();
2766 Register obj = locations->InAt(0).As<Register>();
2767 Location cls = locations->InAt(1);
2768 Register out = locations->Out().As<Register>();
2769 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2770 Label done, zero;
2771 SlowPathCodeX86* slow_path = nullptr;
2772
2773 // Return 0 if `obj` is null.
2774 // TODO: avoid this check if we know obj is not null.
2775 __ testl(obj, obj);
2776 __ j(kEqual, &zero);
2777 __ movl(out, Address(obj, class_offset));
2778 // Compare the class of `obj` with `cls`.
2779 if (cls.IsRegister()) {
2780 __ cmpl(out, cls.As<Register>());
2781 } else {
2782 DCHECK(cls.IsStackSlot()) << cls;
2783 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
2784 }
2785
2786 if (instruction->IsClassFinal()) {
2787 // Classes must be equal for the instanceof to succeed.
2788 __ j(kNotEqual, &zero);
2789 __ movl(out, Immediate(1));
2790 __ jmp(&done);
2791 } else {
2792 // If the classes are not equal, we go into a slow path.
2793 DCHECK(locations->OnlyCallsOnSlowPath());
2794 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
2795 instruction, Location::RegisterLocation(out));
2796 codegen_->AddSlowPath(slow_path);
2797 __ j(kNotEqual, slow_path->GetEntryLabel());
2798 __ movl(out, Immediate(1));
2799 __ jmp(&done);
2800 }
2801 __ Bind(&zero);
2802 __ movl(out, Immediate(0));
2803 if (slow_path != nullptr) {
2804 __ Bind(slow_path->GetExitLabel());
2805 }
2806 __ Bind(&done);
2807}
2808
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002809} // namespace x86
2810} // namespace art