blob: 985ce8d84885284c139c90ea9cc6a35a059aa1a2 [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) {
Roland Levillain946e1432014-11-11 17:35:19 +00001264 case Primitive::kPrimInt:
1265 switch (input_type) {
1266 case Primitive::kPrimLong:
1267 // long-to-int conversion.
1268 locations->SetInAt(0, Location::Any());
1269 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1270 break;
1271
1272 case Primitive::kPrimFloat:
1273 case Primitive::kPrimDouble:
1274 LOG(FATAL) << "Type conversion from " << input_type
1275 << " to " << result_type << " not yet implemented";
1276 break;
1277
1278 default:
1279 LOG(FATAL) << "Unexpected type conversion from " << input_type
1280 << " to " << result_type;
1281 }
1282 break;
1283
Roland Levillaindff1f282014-11-05 14:15:05 +00001284 case Primitive::kPrimLong:
1285 switch (input_type) {
1286 case Primitive::kPrimByte:
1287 case Primitive::kPrimShort:
1288 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001289 case Primitive::kPrimChar:
Roland Levillaindff1f282014-11-05 14:15:05 +00001290 // int-to-long conversion.
1291 locations->SetInAt(0, Location::RegisterLocation(EAX));
1292 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1293 break;
1294
1295 case Primitive::kPrimFloat:
1296 case Primitive::kPrimDouble:
1297 LOG(FATAL) << "Type conversion from " << input_type << " to "
1298 << result_type << " not yet implemented";
1299 break;
1300
1301 default:
1302 LOG(FATAL) << "Unexpected type conversion from " << input_type
1303 << " to " << result_type;
1304 }
1305 break;
1306
Roland Levillaindff1f282014-11-05 14:15:05 +00001307 case Primitive::kPrimFloat:
1308 case Primitive::kPrimDouble:
1309 LOG(FATAL) << "Type conversion from " << input_type
1310 << " to " << result_type << " not yet implemented";
1311 break;
1312
1313 default:
1314 LOG(FATAL) << "Unexpected type conversion from " << input_type
1315 << " to " << result_type;
1316 }
1317}
1318
1319void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1320 LocationSummary* locations = conversion->GetLocations();
1321 Location out = locations->Out();
1322 Location in = locations->InAt(0);
1323 Primitive::Type result_type = conversion->GetResultType();
1324 Primitive::Type input_type = conversion->GetInputType();
1325 switch (result_type) {
Roland Levillain946e1432014-11-11 17:35:19 +00001326 case Primitive::kPrimInt:
1327 switch (input_type) {
1328 case Primitive::kPrimLong:
1329 // long-to-int conversion.
1330 if (in.IsRegisterPair()) {
1331 __ movl(out.As<Register>(), in.AsRegisterPairLow<Register>());
1332 } else if (in.IsDoubleStackSlot()) {
1333 __ movl(out.As<Register>(), Address(ESP, in.GetStackIndex()));
1334 } else {
1335 DCHECK(in.IsConstant());
1336 DCHECK(in.GetConstant()->IsLongConstant());
1337 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
1338 __ movl(out.As<Register>(), Immediate(static_cast<int32_t>(value)));
1339 }
1340 break;
1341
1342 case Primitive::kPrimFloat:
1343 case Primitive::kPrimDouble:
1344 LOG(FATAL) << "Type conversion from " << input_type
1345 << " to " << result_type << " not yet implemented";
1346 break;
1347
1348 default:
1349 LOG(FATAL) << "Unexpected type conversion from " << input_type
1350 << " to " << result_type;
1351 }
1352 break;
1353
Roland Levillaindff1f282014-11-05 14:15:05 +00001354 case Primitive::kPrimLong:
1355 switch (input_type) {
1356 case Primitive::kPrimByte:
1357 case Primitive::kPrimShort:
1358 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001359 case Primitive::kPrimChar:
Roland Levillaindff1f282014-11-05 14:15:05 +00001360 // int-to-long conversion.
1361 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1362 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
1363 DCHECK_EQ(in.As<Register>(), EAX);
1364 __ cdq();
1365 break;
1366
1367 case Primitive::kPrimFloat:
1368 case Primitive::kPrimDouble:
1369 LOG(FATAL) << "Type conversion from " << input_type << " to "
1370 << result_type << " not yet implemented";
1371 break;
1372
1373 default:
1374 LOG(FATAL) << "Unexpected type conversion from " << input_type
1375 << " to " << result_type;
1376 }
1377 break;
1378
Roland Levillaindff1f282014-11-05 14:15:05 +00001379 case Primitive::kPrimFloat:
1380 case Primitive::kPrimDouble:
1381 LOG(FATAL) << "Type conversion from " << input_type
1382 << " to " << result_type << " not yet implemented";
1383 break;
1384
1385 default:
1386 LOG(FATAL) << "Unexpected type conversion from " << input_type
1387 << " to " << result_type;
1388 }
1389}
1390
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001391void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001392 LocationSummary* locations =
1393 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001394 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001395 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001396 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001397 locations->SetInAt(0, Location::RequiresRegister());
1398 locations->SetInAt(1, Location::Any());
1399 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001400 break;
1401 }
1402
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001403 case Primitive::kPrimFloat:
1404 case Primitive::kPrimDouble: {
1405 locations->SetInAt(0, Location::RequiresFpuRegister());
1406 locations->SetInAt(1, Location::Any());
1407 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001408 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001409 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001410
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001411 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001412 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1413 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001414 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001415}
1416
1417void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1418 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001419 Location first = locations->InAt(0);
1420 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001421 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001422 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001423 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001424 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001425 __ addl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001426 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001427 __ addl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001428 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001429 __ addl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001430 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001431 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001432 }
1433
1434 case Primitive::kPrimLong: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001435 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001436 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1437 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001438 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001439 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1440 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001441 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001442 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001443 break;
1444 }
1445
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001446 case Primitive::kPrimFloat: {
1447 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001448 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001449 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001450 __ addss(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001451 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001452 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001453 }
1454
1455 case Primitive::kPrimDouble: {
1456 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001457 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001458 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001459 __ addsd(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001460 }
1461 break;
1462 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001463
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001464 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001465 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001466 }
1467}
1468
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001469void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001470 LocationSummary* locations =
1471 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001472 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001473 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001474 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001475 locations->SetInAt(0, Location::RequiresRegister());
1476 locations->SetInAt(1, Location::Any());
1477 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001478 break;
1479 }
Calin Juravle11351682014-10-23 15:38:15 +01001480 case Primitive::kPrimFloat:
1481 case Primitive::kPrimDouble: {
1482 locations->SetInAt(0, Location::RequiresFpuRegister());
1483 locations->SetInAt(1, Location::RequiresFpuRegister());
1484 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001485 break;
Calin Juravle11351682014-10-23 15:38:15 +01001486 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001487
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001488 default:
Calin Juravle11351682014-10-23 15:38:15 +01001489 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001490 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001491}
1492
1493void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1494 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001495 Location first = locations->InAt(0);
1496 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001497 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001498 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001499 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001500 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001501 __ subl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001502 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001503 __ subl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001504 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001505 __ subl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001506 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001507 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001508 }
1509
1510 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001511 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001512 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1513 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001514 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001515 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001516 __ sbbl(first.AsRegisterPairHigh<Register>(),
1517 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001518 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001519 break;
1520 }
1521
Calin Juravle11351682014-10-23 15:38:15 +01001522 case Primitive::kPrimFloat: {
1523 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001524 break;
Calin Juravle11351682014-10-23 15:38:15 +01001525 }
1526
1527 case Primitive::kPrimDouble: {
1528 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1529 break;
1530 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001531
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001532 default:
Calin Juravle11351682014-10-23 15:38:15 +01001533 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001534 }
1535}
1536
Calin Juravle34bacdf2014-10-07 20:23:36 +01001537void LocationsBuilderX86::VisitMul(HMul* mul) {
1538 LocationSummary* locations =
1539 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1540 switch (mul->GetResultType()) {
1541 case Primitive::kPrimInt:
1542 locations->SetInAt(0, Location::RequiresRegister());
1543 locations->SetInAt(1, Location::Any());
1544 locations->SetOut(Location::SameAsFirstInput());
1545 break;
1546 case Primitive::kPrimLong: {
1547 locations->SetInAt(0, Location::RequiresRegister());
1548 // TODO: Currently this handles only stack operands:
1549 // - we don't have enough registers because we currently use Quick ABI.
1550 // - by the time we have a working register allocator we will probably change the ABI
1551 // and fix the above.
1552 // - we don't have a way yet to request operands on stack but the base line compiler
1553 // will leave the operands on the stack with Any().
1554 locations->SetInAt(1, Location::Any());
1555 locations->SetOut(Location::SameAsFirstInput());
1556 // Needed for imul on 32bits with 64bits output.
1557 locations->AddTemp(Location::RegisterLocation(EAX));
1558 locations->AddTemp(Location::RegisterLocation(EDX));
1559 break;
1560 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001561 case Primitive::kPrimFloat:
1562 case Primitive::kPrimDouble: {
1563 locations->SetInAt(0, Location::RequiresFpuRegister());
1564 locations->SetInAt(1, Location::RequiresFpuRegister());
1565 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001566 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001567 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001568
1569 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001570 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001571 }
1572}
1573
1574void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1575 LocationSummary* locations = mul->GetLocations();
1576 Location first = locations->InAt(0);
1577 Location second = locations->InAt(1);
1578 DCHECK(first.Equals(locations->Out()));
1579
1580 switch (mul->GetResultType()) {
1581 case Primitive::kPrimInt: {
1582 if (second.IsRegister()) {
1583 __ imull(first.As<Register>(), second.As<Register>());
1584 } else if (second.IsConstant()) {
1585 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1586 __ imull(first.As<Register>(), imm);
1587 } else {
1588 DCHECK(second.IsStackSlot());
1589 __ imull(first.As<Register>(), Address(ESP, second.GetStackIndex()));
1590 }
1591 break;
1592 }
1593
1594 case Primitive::kPrimLong: {
1595 DCHECK(second.IsDoubleStackSlot());
1596
1597 Register in1_hi = first.AsRegisterPairHigh<Register>();
1598 Register in1_lo = first.AsRegisterPairLow<Register>();
1599 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
1600 Address in2_lo(ESP, second.GetStackIndex());
1601 Register eax = locations->GetTemp(0).As<Register>();
1602 Register edx = locations->GetTemp(1).As<Register>();
1603
1604 DCHECK_EQ(EAX, eax);
1605 DCHECK_EQ(EDX, edx);
1606
1607 // input: in1 - 64 bits, in2 - 64 bits
1608 // output: in1
1609 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
1610 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
1611 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
1612
1613 __ movl(eax, in2_hi);
1614 // eax <- in1.lo * in2.hi
1615 __ imull(eax, in1_lo);
1616 // in1.hi <- in1.hi * in2.lo
1617 __ imull(in1_hi, in2_lo);
1618 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
1619 __ addl(in1_hi, eax);
1620 // move in1_lo to eax to prepare for double precision
1621 __ movl(eax, in1_lo);
1622 // edx:eax <- in1.lo * in2.lo
1623 __ mull(in2_lo);
1624 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
1625 __ addl(in1_hi, edx);
1626 // in1.lo <- (in1.lo * in2.lo)[31:0];
1627 __ movl(in1_lo, eax);
1628
1629 break;
1630 }
1631
Calin Juravleb5bfa962014-10-21 18:02:24 +01001632 case Primitive::kPrimFloat: {
1633 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001634 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001635 }
1636
1637 case Primitive::kPrimDouble: {
1638 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1639 break;
1640 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001641
1642 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001643 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001644 }
1645}
1646
Calin Juravle7c4954d2014-10-28 16:57:40 +00001647void LocationsBuilderX86::VisitDiv(HDiv* div) {
1648 LocationSummary* locations =
1649 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1650 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001651 case Primitive::kPrimInt: {
1652 locations->SetInAt(0, Location::RegisterLocation(EAX));
1653 locations->SetInAt(1, Location::RequiresRegister());
1654 locations->SetOut(Location::SameAsFirstInput());
1655 // Intel uses edx:eax as the dividend.
1656 locations->AddTemp(Location::RegisterLocation(EDX));
1657 break;
1658 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001659 case Primitive::kPrimLong: {
1660 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1661 break;
1662 }
1663 case Primitive::kPrimFloat:
1664 case Primitive::kPrimDouble: {
1665 locations->SetInAt(0, Location::RequiresFpuRegister());
1666 locations->SetInAt(1, Location::RequiresFpuRegister());
1667 locations->SetOut(Location::SameAsFirstInput());
1668 break;
1669 }
1670
1671 default:
1672 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1673 }
1674}
1675
1676void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
1677 LocationSummary* locations = div->GetLocations();
1678 Location first = locations->InAt(0);
1679 Location second = locations->InAt(1);
1680 DCHECK(first.Equals(locations->Out()));
1681
1682 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001683 case Primitive::kPrimInt: {
1684 Register first_reg = first.As<Register>();
1685 Register second_reg = second.As<Register>();
1686 DCHECK_EQ(EAX, first_reg);
1687 DCHECK_EQ(EDX, locations->GetTemp(0).As<Register>());
1688
1689 SlowPathCodeX86* slow_path =
1690 new (GetGraph()->GetArena()) DivMinusOneSlowPathX86(first_reg);
1691 codegen_->AddSlowPath(slow_path);
1692
1693 // 0x80000000/-1 triggers an arithmetic exception!
1694 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
1695 // it's safe to just use negl instead of more complex comparisons.
1696
1697 __ cmpl(second_reg, Immediate(-1));
1698 __ j(kEqual, slow_path->GetEntryLabel());
1699
1700 // edx:eax <- sign-extended of eax
1701 __ cdq();
1702 // eax = quotient, edx = remainder
1703 __ idivl(second_reg);
1704
1705 __ Bind(slow_path->GetExitLabel());
1706 break;
1707 }
1708
Calin Juravle7c4954d2014-10-28 16:57:40 +00001709 case Primitive::kPrimLong: {
1710 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1711 break;
1712 }
1713
1714 case Primitive::kPrimFloat: {
1715 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1716 break;
1717 }
1718
1719 case Primitive::kPrimDouble: {
1720 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1721 break;
1722 }
1723
1724 default:
1725 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1726 }
1727}
1728
Calin Juravled0d48522014-11-04 16:40:20 +00001729void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1730 LocationSummary* locations =
1731 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1732 locations->SetInAt(0, Location::Any());
1733 if (instruction->HasUses()) {
1734 locations->SetOut(Location::SameAsFirstInput());
1735 }
1736}
1737
1738void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1739 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
1740 codegen_->AddSlowPath(slow_path);
1741
1742 LocationSummary* locations = instruction->GetLocations();
1743 Location value = locations->InAt(0);
1744
1745 if (value.IsRegister()) {
1746 __ testl(value.As<Register>(), value.As<Register>());
1747 } else if (value.IsStackSlot()) {
1748 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
1749 } else {
1750 DCHECK(value.IsConstant()) << value;
1751 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
1752 __ jmp(slow_path->GetEntryLabel());
1753 }
1754 return;
1755 }
1756 __ j(kEqual, slow_path->GetEntryLabel());
1757}
1758
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001759void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001760 LocationSummary* locations =
1761 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001762 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001763 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001764 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1765 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001766}
1767
1768void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
1769 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001770 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001771 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001772
Nicolas Geoffray707c8092014-04-04 10:50:14 +01001773 __ fs()->call(
1774 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001775
Nicolas Geoffray39468442014-09-02 15:17:15 +01001776 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001777 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001778}
1779
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001780void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
1781 LocationSummary* locations =
1782 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1783 locations->SetOut(Location::RegisterLocation(EAX));
1784 InvokeRuntimeCallingConvention calling_convention;
1785 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1786 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1787 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1788}
1789
1790void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
1791 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001792 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001793 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
1794
1795 __ fs()->call(
1796 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
1797
1798 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1799 DCHECK(!codegen_->IsLeafMethod());
1800}
1801
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001802void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001803 LocationSummary* locations =
1804 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001805 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1806 if (location.IsStackSlot()) {
1807 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1808 } else if (location.IsDoubleStackSlot()) {
1809 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001810 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001811 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001812}
1813
1814void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001815 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001816}
1817
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001818void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001819 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001820 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001821 locations->SetInAt(0, Location::RequiresRegister());
1822 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001823}
1824
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001825void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
1826 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01001827 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001828 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01001829 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001830 switch (not_->InputAt(0)->GetType()) {
1831 case Primitive::kPrimBoolean:
1832 __ xorl(out.As<Register>(), Immediate(1));
1833 break;
1834
1835 case Primitive::kPrimInt:
1836 __ notl(out.As<Register>());
1837 break;
1838
1839 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001840 __ notl(out.AsRegisterPairLow<Register>());
1841 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001842 break;
1843
1844 default:
1845 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1846 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001847}
1848
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001849void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001850 LocationSummary* locations =
1851 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001852 locations->SetInAt(0, Location::RequiresRegister());
1853 locations->SetInAt(1, Location::Any());
1854 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001855}
1856
1857void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001858 LocationSummary* locations = compare->GetLocations();
1859 switch (compare->InputAt(0)->GetType()) {
1860 case Primitive::kPrimLong: {
1861 Label less, greater, done;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001862 Register output = locations->Out().As<Register>();
1863 Location left = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001864 Location right = locations->InAt(1);
1865 if (right.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001866 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001867 } else {
1868 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001869 __ cmpl(left.AsRegisterPairHigh<Register>(),
1870 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001871 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001872 __ j(kLess, &less); // Signed compare.
1873 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001874 if (right.IsRegisterPair()) {
1875 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001876 } else {
1877 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001878 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001879 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001880 __ movl(output, Immediate(0));
1881 __ j(kEqual, &done);
1882 __ j(kBelow, &less); // Unsigned compare.
1883
1884 __ Bind(&greater);
1885 __ movl(output, Immediate(1));
1886 __ jmp(&done);
1887
1888 __ Bind(&less);
1889 __ movl(output, Immediate(-1));
1890
1891 __ Bind(&done);
1892 break;
1893 }
1894 default:
1895 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
1896 }
1897}
1898
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001899void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001900 LocationSummary* locations =
1901 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001902 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1903 locations->SetInAt(i, Location::Any());
1904 }
1905 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001906}
1907
1908void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001909 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001910 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001911}
1912
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001913void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001914 LocationSummary* locations =
1915 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001916 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001917 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001918 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001919 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
1920 || (field_type == Primitive::kPrimByte);
1921 // The register allocator does not support multiple
1922 // inputs that die at entry with one in a specific register.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001923 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001924 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001925 locations->SetInAt(1, Location::RegisterLocation(EAX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001926 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001927 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001928 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001929 // Temporary registers for the write barrier.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001930 if (is_object_type) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001931 locations->AddTemp(Location::RequiresRegister());
1932 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001933 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001934 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001935}
1936
1937void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1938 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001939 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001940 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001941 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001942
1943 switch (field_type) {
1944 case Primitive::kPrimBoolean:
1945 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001946 ByteRegister value = locations->InAt(1).As<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001947 __ movb(Address(obj, offset), value);
1948 break;
1949 }
1950
1951 case Primitive::kPrimShort:
1952 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001953 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001954 __ movw(Address(obj, offset), value);
1955 break;
1956 }
1957
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001958 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001959 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001960 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001961 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001962
1963 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001964 Register temp = locations->GetTemp(0).As<Register>();
1965 Register card = locations->GetTemp(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001966 codegen_->MarkGCCard(temp, card, obj, value);
1967 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001968 break;
1969 }
1970
1971 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001972 Location value = locations->InAt(1);
1973 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1974 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001975 break;
1976 }
1977
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001978 case Primitive::kPrimFloat: {
1979 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1980 __ movss(Address(obj, offset), value);
1981 break;
1982 }
1983
1984 case Primitive::kPrimDouble: {
1985 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1986 __ movsd(Address(obj, offset), value);
1987 break;
1988 }
1989
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001990 case Primitive::kPrimVoid:
1991 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001992 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001993 }
1994}
1995
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001996void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
1997 Label is_null;
1998 __ testl(value, value);
1999 __ j(kEqual, &is_null);
2000 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2001 __ movl(temp, object);
2002 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
2003 __ movb(Address(temp, card, TIMES_1, 0),
2004 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
2005 __ Bind(&is_null);
2006}
2007
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002008void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002009 LocationSummary* locations =
2010 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002011 locations->SetInAt(0, Location::RequiresRegister());
2012 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002013}
2014
2015void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2016 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002017 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002018 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2019
2020 switch (instruction->GetType()) {
2021 case Primitive::kPrimBoolean: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002022 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002023 __ movzxb(out, Address(obj, offset));
2024 break;
2025 }
2026
2027 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002028 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002029 __ movsxb(out, Address(obj, offset));
2030 break;
2031 }
2032
2033 case Primitive::kPrimShort: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002034 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002035 __ movsxw(out, Address(obj, offset));
2036 break;
2037 }
2038
2039 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002040 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002041 __ movzxw(out, Address(obj, offset));
2042 break;
2043 }
2044
2045 case Primitive::kPrimInt:
2046 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002047 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002048 __ movl(out, Address(obj, offset));
2049 break;
2050 }
2051
2052 case Primitive::kPrimLong: {
2053 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002054 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
2055 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002056 break;
2057 }
2058
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002059 case Primitive::kPrimFloat: {
2060 XmmRegister out = locations->Out().As<XmmRegister>();
2061 __ movss(out, Address(obj, offset));
2062 break;
2063 }
2064
2065 case Primitive::kPrimDouble: {
2066 XmmRegister out = locations->Out().As<XmmRegister>();
2067 __ movsd(out, Address(obj, offset));
2068 break;
2069 }
2070
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002071 case Primitive::kPrimVoid:
2072 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002073 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002074 }
2075}
2076
2077void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002078 LocationSummary* locations =
2079 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002080 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002081 if (instruction->HasUses()) {
2082 locations->SetOut(Location::SameAsFirstInput());
2083 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002084}
2085
2086void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002087 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002088 codegen_->AddSlowPath(slow_path);
2089
2090 LocationSummary* locations = instruction->GetLocations();
2091 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002092
2093 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002094 __ cmpl(obj.As<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002095 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002096 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002097 } else {
2098 DCHECK(obj.IsConstant()) << obj;
2099 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2100 __ jmp(slow_path->GetEntryLabel());
2101 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002102 }
2103 __ j(kEqual, slow_path->GetEntryLabel());
2104}
2105
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002106void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002107 LocationSummary* locations =
2108 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002109 locations->SetInAt(0, Location::RequiresRegister());
2110 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2111 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002112}
2113
2114void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
2115 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002116 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002117 Location index = locations->InAt(1);
2118
2119 switch (instruction->GetType()) {
2120 case Primitive::kPrimBoolean: {
2121 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002122 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002123 if (index.IsConstant()) {
2124 __ movzxb(out, Address(obj,
2125 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2126 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002127 __ movzxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002128 }
2129 break;
2130 }
2131
2132 case Primitive::kPrimByte: {
2133 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002134 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002135 if (index.IsConstant()) {
2136 __ movsxb(out, Address(obj,
2137 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2138 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002139 __ movsxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002140 }
2141 break;
2142 }
2143
2144 case Primitive::kPrimShort: {
2145 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002146 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002147 if (index.IsConstant()) {
2148 __ movsxw(out, Address(obj,
2149 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2150 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002151 __ movsxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002152 }
2153 break;
2154 }
2155
2156 case Primitive::kPrimChar: {
2157 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002158 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002159 if (index.IsConstant()) {
2160 __ movzxw(out, Address(obj,
2161 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2162 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002163 __ movzxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002164 }
2165 break;
2166 }
2167
2168 case Primitive::kPrimInt:
2169 case Primitive::kPrimNot: {
2170 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002171 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002172 if (index.IsConstant()) {
2173 __ movl(out, Address(obj,
2174 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2175 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002176 __ movl(out, Address(obj, index.As<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002177 }
2178 break;
2179 }
2180
2181 case Primitive::kPrimLong: {
2182 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002183 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002184 if (index.IsConstant()) {
2185 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002186 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
2187 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002188 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002189 __ movl(out.AsRegisterPairLow<Register>(),
2190 Address(obj, index.As<Register>(), TIMES_8, data_offset));
2191 __ movl(out.AsRegisterPairHigh<Register>(),
2192 Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002193 }
2194 break;
2195 }
2196
2197 case Primitive::kPrimFloat:
2198 case Primitive::kPrimDouble:
2199 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002200 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002201 case Primitive::kPrimVoid:
2202 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002203 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002204 }
2205}
2206
2207void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002208 Primitive::Type value_type = instruction->GetComponentType();
2209 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2210 instruction,
2211 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
2212
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002213 if (value_type == Primitive::kPrimNot) {
2214 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002215 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2216 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2217 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002218 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002219 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
2220 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002221 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002222 // In case of a byte operation, the register allocator does not support multiple
2223 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002224 locations->SetInAt(0, Location::RequiresRegister());
2225 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002226 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002227 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002228 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002229 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002230 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002231 }
2232 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002233}
2234
2235void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
2236 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002237 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002238 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002239 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002240 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002241
2242 switch (value_type) {
2243 case Primitive::kPrimBoolean:
2244 case Primitive::kPrimByte: {
2245 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002246 if (index.IsConstant()) {
2247 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002248 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002249 __ movb(Address(obj, offset), value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002250 } else {
2251 __ movb(Address(obj, offset),
2252 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2253 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002254 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002255 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002256 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
2257 value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002258 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002259 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002260 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2261 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002262 }
2263 break;
2264 }
2265
2266 case Primitive::kPrimShort:
2267 case Primitive::kPrimChar: {
2268 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002269 if (index.IsConstant()) {
2270 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002271 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002272 __ movw(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002273 } else {
2274 __ movw(Address(obj, offset),
2275 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2276 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002277 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002278 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002279 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
2280 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002281 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002282 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002283 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2284 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002285 }
2286 break;
2287 }
2288
2289 case Primitive::kPrimInt: {
2290 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002291 if (index.IsConstant()) {
2292 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002293 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002294 __ movl(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002295 } else {
2296 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2297 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002298 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002299 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002300 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
2301 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002302 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002303 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002304 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2305 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002306 }
2307 break;
2308 }
2309
2310 case Primitive::kPrimNot: {
2311 DCHECK(!codegen_->IsLeafMethod());
2312 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01002313 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002314 break;
2315 }
2316
2317 case Primitive::kPrimLong: {
2318 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002319 if (index.IsConstant()) {
2320 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002321 if (value.IsRegisterPair()) {
2322 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
2323 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002324 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002325 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002326 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
2327 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
2328 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
2329 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002330 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002331 if (value.IsRegisterPair()) {
2332 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
2333 value.AsRegisterPairLow<Register>());
2334 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
2335 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002336 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002337 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002338 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002339 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002340 Immediate(Low32Bits(val)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002341 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002342 Immediate(High32Bits(val)));
2343 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002344 }
2345 break;
2346 }
2347
2348 case Primitive::kPrimFloat:
2349 case Primitive::kPrimDouble:
2350 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002351 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002352 case Primitive::kPrimVoid:
2353 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002354 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002355 }
2356}
2357
2358void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
2359 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002360 locations->SetInAt(0, Location::RequiresRegister());
2361 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002362 instruction->SetLocations(locations);
2363}
2364
2365void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
2366 LocationSummary* locations = instruction->GetLocations();
2367 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002368 Register obj = locations->InAt(0).As<Register>();
2369 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002370 __ movl(out, Address(obj, offset));
2371}
2372
2373void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002374 LocationSummary* locations =
2375 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002376 locations->SetInAt(0, Location::RequiresRegister());
2377 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002378 if (instruction->HasUses()) {
2379 locations->SetOut(Location::SameAsFirstInput());
2380 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002381}
2382
2383void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
2384 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002385 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002386 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002387 codegen_->AddSlowPath(slow_path);
2388
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002389 Register index = locations->InAt(0).As<Register>();
2390 Register length = locations->InAt(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002391
2392 __ cmpl(index, length);
2393 __ j(kAboveEqual, slow_path->GetEntryLabel());
2394}
2395
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002396void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
2397 temp->SetLocations(nullptr);
2398}
2399
2400void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
2401 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002402 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002403}
2404
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002405void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002406 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002407 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002408}
2409
2410void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002411 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2412}
2413
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002414void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
2415 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2416}
2417
2418void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002419 HBasicBlock* block = instruction->GetBlock();
2420 if (block->GetLoopInformation() != nullptr) {
2421 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2422 // The back edge will generate the suspend check.
2423 return;
2424 }
2425 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2426 // The goto will generate the suspend check.
2427 return;
2428 }
2429 GenerateSuspendCheck(instruction, nullptr);
2430}
2431
2432void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
2433 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002434 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002435 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002436 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002437 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002438 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002439 if (successor == nullptr) {
2440 __ j(kNotEqual, slow_path->GetEntryLabel());
2441 __ Bind(slow_path->GetReturnLabel());
2442 } else {
2443 __ j(kEqual, codegen_->GetLabelOf(successor));
2444 __ jmp(slow_path->GetEntryLabel());
2445 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002446}
2447
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002448X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
2449 return codegen_->GetAssembler();
2450}
2451
2452void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
2453 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002454 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002455 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2456 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
2457 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
2458}
2459
2460void ParallelMoveResolverX86::EmitMove(size_t index) {
2461 MoveOperands* move = moves_.Get(index);
2462 Location source = move->GetSource();
2463 Location destination = move->GetDestination();
2464
2465 if (source.IsRegister()) {
2466 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002467 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002468 } else {
2469 DCHECK(destination.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002470 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002471 }
2472 } else if (source.IsStackSlot()) {
2473 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002474 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002475 } else {
2476 DCHECK(destination.IsStackSlot());
2477 MoveMemoryToMemory(destination.GetStackIndex(),
2478 source.GetStackIndex());
2479 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002480 } else if (source.IsConstant()) {
2481 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
2482 Immediate imm(instruction->AsIntConstant()->GetValue());
2483 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002484 __ movl(destination.As<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002485 } else {
2486 __ movl(Address(ESP, destination.GetStackIndex()), imm);
2487 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002488 } else {
2489 LOG(FATAL) << "Unimplemented";
2490 }
2491}
2492
2493void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002494 Register suggested_scratch = reg == EAX ? EBX : EAX;
2495 ScratchRegisterScope ensure_scratch(
2496 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2497
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002498 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2499 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
2500 __ movl(Address(ESP, mem + stack_offset), reg);
2501 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
2502}
2503
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002504void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
2505 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002506 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
2507
2508 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002509 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002510 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2511
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002512 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
2513 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
2514 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
2515 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
2516 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
2517 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
2518}
2519
2520void ParallelMoveResolverX86::EmitSwap(size_t index) {
2521 MoveOperands* move = moves_.Get(index);
2522 Location source = move->GetSource();
2523 Location destination = move->GetDestination();
2524
2525 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002526 __ xchgl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002527 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002528 Exchange(source.As<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002529 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002530 Exchange(destination.As<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002531 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
2532 Exchange(destination.GetStackIndex(), source.GetStackIndex());
2533 } else {
2534 LOG(FATAL) << "Unimplemented";
2535 }
2536}
2537
2538void ParallelMoveResolverX86::SpillScratch(int reg) {
2539 __ pushl(static_cast<Register>(reg));
2540}
2541
2542void ParallelMoveResolverX86::RestoreScratch(int reg) {
2543 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002544}
2545
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002546void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002547 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2548 ? LocationSummary::kCallOnSlowPath
2549 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002550 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002551 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002552 locations->SetOut(Location::RequiresRegister());
2553}
2554
2555void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
2556 Register out = cls->GetLocations()->Out().As<Register>();
2557 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002558 DCHECK(!cls->CanCallRuntime());
2559 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002560 codegen_->LoadCurrentMethod(out);
2561 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2562 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002563 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002564 codegen_->LoadCurrentMethod(out);
2565 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2566 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002567
2568 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
2569 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2570 codegen_->AddSlowPath(slow_path);
2571 __ testl(out, out);
2572 __ j(kEqual, slow_path->GetEntryLabel());
2573 if (cls->MustGenerateClinitCheck()) {
2574 GenerateClassInitializationCheck(slow_path, out);
2575 } else {
2576 __ Bind(slow_path->GetExitLabel());
2577 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002578 }
2579}
2580
2581void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
2582 LocationSummary* locations =
2583 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2584 locations->SetInAt(0, Location::RequiresRegister());
2585 if (check->HasUses()) {
2586 locations->SetOut(Location::SameAsFirstInput());
2587 }
2588}
2589
2590void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002591 // We assume the class to not be null.
2592 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
2593 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002594 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002595 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<Register>());
2596}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002597
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002598void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
2599 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002600 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2601 Immediate(mirror::Class::kStatusInitialized));
2602 __ j(kLess, slow_path->GetEntryLabel());
2603 __ Bind(slow_path->GetExitLabel());
2604 // No need for memory fence, thanks to the X86 memory model.
2605}
2606
2607void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2608 LocationSummary* locations =
2609 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2610 locations->SetInAt(0, Location::RequiresRegister());
2611 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2612}
2613
2614void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2615 LocationSummary* locations = instruction->GetLocations();
2616 Register cls = locations->InAt(0).As<Register>();
2617 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2618
2619 switch (instruction->GetType()) {
2620 case Primitive::kPrimBoolean: {
2621 Register out = locations->Out().As<Register>();
2622 __ movzxb(out, Address(cls, offset));
2623 break;
2624 }
2625
2626 case Primitive::kPrimByte: {
2627 Register out = locations->Out().As<Register>();
2628 __ movsxb(out, Address(cls, offset));
2629 break;
2630 }
2631
2632 case Primitive::kPrimShort: {
2633 Register out = locations->Out().As<Register>();
2634 __ movsxw(out, Address(cls, offset));
2635 break;
2636 }
2637
2638 case Primitive::kPrimChar: {
2639 Register out = locations->Out().As<Register>();
2640 __ movzxw(out, Address(cls, offset));
2641 break;
2642 }
2643
2644 case Primitive::kPrimInt:
2645 case Primitive::kPrimNot: {
2646 Register out = locations->Out().As<Register>();
2647 __ movl(out, Address(cls, offset));
2648 break;
2649 }
2650
2651 case Primitive::kPrimLong: {
2652 // TODO: support volatile.
2653 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(cls, offset));
2654 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(cls, kX86WordSize + offset));
2655 break;
2656 }
2657
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002658 case Primitive::kPrimFloat: {
2659 XmmRegister out = locations->Out().As<XmmRegister>();
2660 __ movss(out, Address(cls, offset));
2661 break;
2662 }
2663
2664 case Primitive::kPrimDouble: {
2665 XmmRegister out = locations->Out().As<XmmRegister>();
2666 __ movsd(out, Address(cls, offset));
2667 break;
2668 }
2669
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002670 case Primitive::kPrimVoid:
2671 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2672 UNREACHABLE();
2673 }
2674}
2675
2676void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2677 LocationSummary* locations =
2678 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2679 locations->SetInAt(0, Location::RequiresRegister());
2680 Primitive::Type field_type = instruction->GetFieldType();
2681 bool is_object_type = field_type == Primitive::kPrimNot;
2682 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2683 || (field_type == Primitive::kPrimByte);
2684 // The register allocator does not support multiple
2685 // inputs that die at entry with one in a specific register.
2686 if (is_byte_type) {
2687 // Ensure the value is in a byte register.
2688 locations->SetInAt(1, Location::RegisterLocation(EAX));
2689 } else {
2690 locations->SetInAt(1, Location::RequiresRegister());
2691 }
2692 // Temporary registers for the write barrier.
2693 if (is_object_type) {
2694 locations->AddTemp(Location::RequiresRegister());
2695 // Ensure the card is in a byte register.
2696 locations->AddTemp(Location::RegisterLocation(ECX));
2697 }
2698}
2699
2700void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2701 LocationSummary* locations = instruction->GetLocations();
2702 Register cls = locations->InAt(0).As<Register>();
2703 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2704 Primitive::Type field_type = instruction->GetFieldType();
2705
2706 switch (field_type) {
2707 case Primitive::kPrimBoolean:
2708 case Primitive::kPrimByte: {
2709 ByteRegister value = locations->InAt(1).As<ByteRegister>();
2710 __ movb(Address(cls, offset), value);
2711 break;
2712 }
2713
2714 case Primitive::kPrimShort:
2715 case Primitive::kPrimChar: {
2716 Register value = locations->InAt(1).As<Register>();
2717 __ movw(Address(cls, offset), value);
2718 break;
2719 }
2720
2721 case Primitive::kPrimInt:
2722 case Primitive::kPrimNot: {
2723 Register value = locations->InAt(1).As<Register>();
2724 __ movl(Address(cls, offset), value);
2725
2726 if (field_type == Primitive::kPrimNot) {
2727 Register temp = locations->GetTemp(0).As<Register>();
2728 Register card = locations->GetTemp(1).As<Register>();
2729 codegen_->MarkGCCard(temp, card, cls, value);
2730 }
2731 break;
2732 }
2733
2734 case Primitive::kPrimLong: {
2735 Location value = locations->InAt(1);
2736 __ movl(Address(cls, offset), value.AsRegisterPairLow<Register>());
2737 __ movl(Address(cls, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
2738 break;
2739 }
2740
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002741 case Primitive::kPrimFloat: {
2742 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2743 __ movss(Address(cls, offset), value);
2744 break;
2745 }
2746
2747 case Primitive::kPrimDouble: {
2748 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2749 __ movsd(Address(cls, offset), value);
2750 break;
2751 }
2752
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002753 case Primitive::kPrimVoid:
2754 LOG(FATAL) << "Unreachable type " << field_type;
2755 UNREACHABLE();
2756 }
2757}
2758
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002759void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
2760 LocationSummary* locations =
2761 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2762 locations->SetOut(Location::RequiresRegister());
2763}
2764
2765void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
2766 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
2767 codegen_->AddSlowPath(slow_path);
2768
2769 Register out = load->GetLocations()->Out().As<Register>();
2770 codegen_->LoadCurrentMethod(out);
2771 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
2772 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2773 __ testl(out, out);
2774 __ j(kEqual, slow_path->GetEntryLabel());
2775 __ Bind(slow_path->GetExitLabel());
2776}
2777
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002778void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
2779 LocationSummary* locations =
2780 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2781 locations->SetOut(Location::RequiresRegister());
2782}
2783
2784void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
2785 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
2786 __ fs()->movl(load->GetLocations()->Out().As<Register>(), address);
2787 __ fs()->movl(address, Immediate(0));
2788}
2789
2790void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
2791 LocationSummary* locations =
2792 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2793 InvokeRuntimeCallingConvention calling_convention;
2794 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2795}
2796
2797void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
2798 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
2799 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2800}
2801
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002802void LocationsBuilderX86::VisitTypeCheck(HTypeCheck* instruction) {
2803 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
2804 ? LocationSummary::kNoCall
2805 : LocationSummary::kCallOnSlowPath;
2806 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2807 locations->SetInAt(0, Location::RequiresRegister());
2808 locations->SetInAt(1, Location::Any());
2809 locations->SetOut(Location::RequiresRegister());
2810}
2811
2812void InstructionCodeGeneratorX86::VisitTypeCheck(HTypeCheck* instruction) {
2813 LocationSummary* locations = instruction->GetLocations();
2814 Register obj = locations->InAt(0).As<Register>();
2815 Location cls = locations->InAt(1);
2816 Register out = locations->Out().As<Register>();
2817 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2818 Label done, zero;
2819 SlowPathCodeX86* slow_path = nullptr;
2820
2821 // Return 0 if `obj` is null.
2822 // TODO: avoid this check if we know obj is not null.
2823 __ testl(obj, obj);
2824 __ j(kEqual, &zero);
2825 __ movl(out, Address(obj, class_offset));
2826 // Compare the class of `obj` with `cls`.
2827 if (cls.IsRegister()) {
2828 __ cmpl(out, cls.As<Register>());
2829 } else {
2830 DCHECK(cls.IsStackSlot()) << cls;
2831 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
2832 }
2833
2834 if (instruction->IsClassFinal()) {
2835 // Classes must be equal for the instanceof to succeed.
2836 __ j(kNotEqual, &zero);
2837 __ movl(out, Immediate(1));
2838 __ jmp(&done);
2839 } else {
2840 // If the classes are not equal, we go into a slow path.
2841 DCHECK(locations->OnlyCallsOnSlowPath());
2842 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
2843 instruction, Location::RegisterLocation(out));
2844 codegen_->AddSlowPath(slow_path);
2845 __ j(kNotEqual, slow_path->GetEntryLabel());
2846 __ movl(out, Immediate(1));
2847 __ jmp(&done);
2848 }
2849 __ Bind(&zero);
2850 __ movl(out, Immediate(0));
2851 if (slow_path != nullptr) {
2852 __ Bind(slow_path->GetExitLabel());
2853 }
2854 __ Bind(&done);
2855}
2856
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002857} // namespace x86
2858} // namespace art