blob: 548d6995d07a09b26cac4a4224cb66901edb8a1e [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
Roland Levillain88cb1752014-10-20 16:36:47 +01001155void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1156 LocationSummary* locations =
1157 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1158 switch (neg->GetResultType()) {
1159 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001160 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001161 locations->SetInAt(0, Location::RequiresRegister());
1162 locations->SetOut(Location::SameAsFirstInput());
1163 break;
1164
Roland Levillain88cb1752014-10-20 16:36:47 +01001165 case Primitive::kPrimFloat:
1166 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001167 locations->SetInAt(0, Location::RequiresFpuRegister());
1168 // Output overlaps as we need a fresh (zero-initialized)
1169 // register to perform subtraction from zero.
1170 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001171 break;
1172
1173 default:
1174 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1175 }
1176}
1177
1178void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1179 LocationSummary* locations = neg->GetLocations();
1180 Location out = locations->Out();
1181 Location in = locations->InAt(0);
1182 switch (neg->GetResultType()) {
1183 case Primitive::kPrimInt:
1184 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001185 DCHECK(in.Equals(out));
Roland Levillain88cb1752014-10-20 16:36:47 +01001186 __ negl(out.As<Register>());
1187 break;
1188
1189 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001190 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001191 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001192 __ negl(out.AsRegisterPairLow<Register>());
1193 // Negation is similar to subtraction from zero. The least
1194 // significant byte triggers a borrow when it is different from
1195 // zero; to take it into account, add 1 to the most significant
1196 // byte if the carry flag (CF) is set to 1 after the first NEGL
1197 // operation.
1198 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1199 __ negl(out.AsRegisterPairHigh<Register>());
1200 break;
1201
Roland Levillain88cb1752014-10-20 16:36:47 +01001202 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001203 DCHECK(!in.Equals(out));
1204 // out = 0
1205 __ xorps(out.As<XmmRegister>(), out.As<XmmRegister>());
1206 // out = out - in
1207 __ subss(out.As<XmmRegister>(), in.As<XmmRegister>());
1208 break;
1209
Roland Levillain88cb1752014-10-20 16:36:47 +01001210 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001211 DCHECK(!in.Equals(out));
1212 // out = 0
1213 __ xorpd(out.As<XmmRegister>(), out.As<XmmRegister>());
1214 // out = out - in
1215 __ subsd(out.As<XmmRegister>(), in.As<XmmRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001216 break;
1217
1218 default:
1219 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1220 }
1221}
1222
Roland Levillaindff1f282014-11-05 14:15:05 +00001223void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
1224 LocationSummary* locations =
1225 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1226 Primitive::Type result_type = conversion->GetResultType();
1227 Primitive::Type input_type = conversion->GetInputType();
1228 switch (result_type) {
1229 case Primitive::kPrimLong:
1230 switch (input_type) {
1231 case Primitive::kPrimByte:
1232 case Primitive::kPrimShort:
1233 case Primitive::kPrimInt:
1234 // int-to-long conversion.
1235 locations->SetInAt(0, Location::RegisterLocation(EAX));
1236 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1237 break;
1238
1239 case Primitive::kPrimFloat:
1240 case Primitive::kPrimDouble:
1241 LOG(FATAL) << "Type conversion from " << input_type << " to "
1242 << result_type << " not yet implemented";
1243 break;
1244
1245 default:
1246 LOG(FATAL) << "Unexpected type conversion from " << input_type
1247 << " to " << result_type;
1248 }
1249 break;
1250
1251 case Primitive::kPrimInt:
1252 case Primitive::kPrimFloat:
1253 case Primitive::kPrimDouble:
1254 LOG(FATAL) << "Type conversion from " << input_type
1255 << " to " << result_type << " not yet implemented";
1256 break;
1257
1258 default:
1259 LOG(FATAL) << "Unexpected type conversion from " << input_type
1260 << " to " << result_type;
1261 }
1262}
1263
1264void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1265 LocationSummary* locations = conversion->GetLocations();
1266 Location out = locations->Out();
1267 Location in = locations->InAt(0);
1268 Primitive::Type result_type = conversion->GetResultType();
1269 Primitive::Type input_type = conversion->GetInputType();
1270 switch (result_type) {
1271 case Primitive::kPrimLong:
1272 switch (input_type) {
1273 case Primitive::kPrimByte:
1274 case Primitive::kPrimShort:
1275 case Primitive::kPrimInt:
1276 // int-to-long conversion.
1277 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1278 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
1279 DCHECK_EQ(in.As<Register>(), EAX);
1280 __ cdq();
1281 break;
1282
1283 case Primitive::kPrimFloat:
1284 case Primitive::kPrimDouble:
1285 LOG(FATAL) << "Type conversion from " << input_type << " to "
1286 << result_type << " not yet implemented";
1287 break;
1288
1289 default:
1290 LOG(FATAL) << "Unexpected type conversion from " << input_type
1291 << " to " << result_type;
1292 }
1293 break;
1294
1295 case Primitive::kPrimInt:
1296 case Primitive::kPrimFloat:
1297 case Primitive::kPrimDouble:
1298 LOG(FATAL) << "Type conversion from " << input_type
1299 << " to " << result_type << " not yet implemented";
1300 break;
1301
1302 default:
1303 LOG(FATAL) << "Unexpected type conversion from " << input_type
1304 << " to " << result_type;
1305 }
1306}
1307
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001308void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001309 LocationSummary* locations =
1310 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001311 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001312 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001313 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001314 locations->SetInAt(0, Location::RequiresRegister());
1315 locations->SetInAt(1, Location::Any());
1316 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001317 break;
1318 }
1319
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001320 case Primitive::kPrimFloat:
1321 case Primitive::kPrimDouble: {
1322 locations->SetInAt(0, Location::RequiresFpuRegister());
1323 locations->SetInAt(1, Location::Any());
1324 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001325 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001326 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001327
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001328 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001329 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1330 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001331 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001332}
1333
1334void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1335 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001336 Location first = locations->InAt(0);
1337 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001338 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001339 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001340 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001341 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001342 __ addl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001343 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001344 __ addl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001345 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001346 __ addl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001347 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001348 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001349 }
1350
1351 case Primitive::kPrimLong: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001352 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001353 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1354 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001355 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001356 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1357 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001358 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001359 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001360 break;
1361 }
1362
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001363 case Primitive::kPrimFloat: {
1364 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001365 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001366 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001367 __ addss(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001368 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001369 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001370 }
1371
1372 case Primitive::kPrimDouble: {
1373 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001374 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001375 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001376 __ addsd(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001377 }
1378 break;
1379 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001380
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001381 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001382 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001383 }
1384}
1385
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001386void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001387 LocationSummary* locations =
1388 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001389 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001390 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001391 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001392 locations->SetInAt(0, Location::RequiresRegister());
1393 locations->SetInAt(1, Location::Any());
1394 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001395 break;
1396 }
Calin Juravle11351682014-10-23 15:38:15 +01001397 case Primitive::kPrimFloat:
1398 case Primitive::kPrimDouble: {
1399 locations->SetInAt(0, Location::RequiresFpuRegister());
1400 locations->SetInAt(1, Location::RequiresFpuRegister());
1401 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001402 break;
Calin Juravle11351682014-10-23 15:38:15 +01001403 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001404
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001405 default:
Calin Juravle11351682014-10-23 15:38:15 +01001406 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001407 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001408}
1409
1410void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1411 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001412 Location first = locations->InAt(0);
1413 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001414 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001415 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001416 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001417 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001418 __ subl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001419 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001420 __ subl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001421 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001422 __ subl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001423 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001424 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001425 }
1426
1427 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001428 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001429 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1430 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001431 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001432 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001433 __ sbbl(first.AsRegisterPairHigh<Register>(),
1434 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001435 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001436 break;
1437 }
1438
Calin Juravle11351682014-10-23 15:38:15 +01001439 case Primitive::kPrimFloat: {
1440 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001441 break;
Calin Juravle11351682014-10-23 15:38:15 +01001442 }
1443
1444 case Primitive::kPrimDouble: {
1445 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1446 break;
1447 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001448
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001449 default:
Calin Juravle11351682014-10-23 15:38:15 +01001450 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001451 }
1452}
1453
Calin Juravle34bacdf2014-10-07 20:23:36 +01001454void LocationsBuilderX86::VisitMul(HMul* mul) {
1455 LocationSummary* locations =
1456 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1457 switch (mul->GetResultType()) {
1458 case Primitive::kPrimInt:
1459 locations->SetInAt(0, Location::RequiresRegister());
1460 locations->SetInAt(1, Location::Any());
1461 locations->SetOut(Location::SameAsFirstInput());
1462 break;
1463 case Primitive::kPrimLong: {
1464 locations->SetInAt(0, Location::RequiresRegister());
1465 // TODO: Currently this handles only stack operands:
1466 // - we don't have enough registers because we currently use Quick ABI.
1467 // - by the time we have a working register allocator we will probably change the ABI
1468 // and fix the above.
1469 // - we don't have a way yet to request operands on stack but the base line compiler
1470 // will leave the operands on the stack with Any().
1471 locations->SetInAt(1, Location::Any());
1472 locations->SetOut(Location::SameAsFirstInput());
1473 // Needed for imul on 32bits with 64bits output.
1474 locations->AddTemp(Location::RegisterLocation(EAX));
1475 locations->AddTemp(Location::RegisterLocation(EDX));
1476 break;
1477 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001478 case Primitive::kPrimFloat:
1479 case Primitive::kPrimDouble: {
1480 locations->SetInAt(0, Location::RequiresFpuRegister());
1481 locations->SetInAt(1, Location::RequiresFpuRegister());
1482 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001483 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001484 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001485
1486 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001487 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001488 }
1489}
1490
1491void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1492 LocationSummary* locations = mul->GetLocations();
1493 Location first = locations->InAt(0);
1494 Location second = locations->InAt(1);
1495 DCHECK(first.Equals(locations->Out()));
1496
1497 switch (mul->GetResultType()) {
1498 case Primitive::kPrimInt: {
1499 if (second.IsRegister()) {
1500 __ imull(first.As<Register>(), second.As<Register>());
1501 } else if (second.IsConstant()) {
1502 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1503 __ imull(first.As<Register>(), imm);
1504 } else {
1505 DCHECK(second.IsStackSlot());
1506 __ imull(first.As<Register>(), Address(ESP, second.GetStackIndex()));
1507 }
1508 break;
1509 }
1510
1511 case Primitive::kPrimLong: {
1512 DCHECK(second.IsDoubleStackSlot());
1513
1514 Register in1_hi = first.AsRegisterPairHigh<Register>();
1515 Register in1_lo = first.AsRegisterPairLow<Register>();
1516 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
1517 Address in2_lo(ESP, second.GetStackIndex());
1518 Register eax = locations->GetTemp(0).As<Register>();
1519 Register edx = locations->GetTemp(1).As<Register>();
1520
1521 DCHECK_EQ(EAX, eax);
1522 DCHECK_EQ(EDX, edx);
1523
1524 // input: in1 - 64 bits, in2 - 64 bits
1525 // output: in1
1526 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
1527 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
1528 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
1529
1530 __ movl(eax, in2_hi);
1531 // eax <- in1.lo * in2.hi
1532 __ imull(eax, in1_lo);
1533 // in1.hi <- in1.hi * in2.lo
1534 __ imull(in1_hi, in2_lo);
1535 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
1536 __ addl(in1_hi, eax);
1537 // move in1_lo to eax to prepare for double precision
1538 __ movl(eax, in1_lo);
1539 // edx:eax <- in1.lo * in2.lo
1540 __ mull(in2_lo);
1541 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
1542 __ addl(in1_hi, edx);
1543 // in1.lo <- (in1.lo * in2.lo)[31:0];
1544 __ movl(in1_lo, eax);
1545
1546 break;
1547 }
1548
Calin Juravleb5bfa962014-10-21 18:02:24 +01001549 case Primitive::kPrimFloat: {
1550 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001551 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001552 }
1553
1554 case Primitive::kPrimDouble: {
1555 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1556 break;
1557 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001558
1559 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001560 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001561 }
1562}
1563
Calin Juravle7c4954d2014-10-28 16:57:40 +00001564void LocationsBuilderX86::VisitDiv(HDiv* div) {
1565 LocationSummary* locations =
1566 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1567 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001568 case Primitive::kPrimInt: {
1569 locations->SetInAt(0, Location::RegisterLocation(EAX));
1570 locations->SetInAt(1, Location::RequiresRegister());
1571 locations->SetOut(Location::SameAsFirstInput());
1572 // Intel uses edx:eax as the dividend.
1573 locations->AddTemp(Location::RegisterLocation(EDX));
1574 break;
1575 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001576 case Primitive::kPrimLong: {
1577 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1578 break;
1579 }
1580 case Primitive::kPrimFloat:
1581 case Primitive::kPrimDouble: {
1582 locations->SetInAt(0, Location::RequiresFpuRegister());
1583 locations->SetInAt(1, Location::RequiresFpuRegister());
1584 locations->SetOut(Location::SameAsFirstInput());
1585 break;
1586 }
1587
1588 default:
1589 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1590 }
1591}
1592
1593void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
1594 LocationSummary* locations = div->GetLocations();
1595 Location first = locations->InAt(0);
1596 Location second = locations->InAt(1);
1597 DCHECK(first.Equals(locations->Out()));
1598
1599 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001600 case Primitive::kPrimInt: {
1601 Register first_reg = first.As<Register>();
1602 Register second_reg = second.As<Register>();
1603 DCHECK_EQ(EAX, first_reg);
1604 DCHECK_EQ(EDX, locations->GetTemp(0).As<Register>());
1605
1606 SlowPathCodeX86* slow_path =
1607 new (GetGraph()->GetArena()) DivMinusOneSlowPathX86(first_reg);
1608 codegen_->AddSlowPath(slow_path);
1609
1610 // 0x80000000/-1 triggers an arithmetic exception!
1611 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
1612 // it's safe to just use negl instead of more complex comparisons.
1613
1614 __ cmpl(second_reg, Immediate(-1));
1615 __ j(kEqual, slow_path->GetEntryLabel());
1616
1617 // edx:eax <- sign-extended of eax
1618 __ cdq();
1619 // eax = quotient, edx = remainder
1620 __ idivl(second_reg);
1621
1622 __ Bind(slow_path->GetExitLabel());
1623 break;
1624 }
1625
Calin Juravle7c4954d2014-10-28 16:57:40 +00001626 case Primitive::kPrimLong: {
1627 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1628 break;
1629 }
1630
1631 case Primitive::kPrimFloat: {
1632 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1633 break;
1634 }
1635
1636 case Primitive::kPrimDouble: {
1637 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1638 break;
1639 }
1640
1641 default:
1642 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1643 }
1644}
1645
Calin Juravled0d48522014-11-04 16:40:20 +00001646void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1647 LocationSummary* locations =
1648 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1649 locations->SetInAt(0, Location::Any());
1650 if (instruction->HasUses()) {
1651 locations->SetOut(Location::SameAsFirstInput());
1652 }
1653}
1654
1655void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1656 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
1657 codegen_->AddSlowPath(slow_path);
1658
1659 LocationSummary* locations = instruction->GetLocations();
1660 Location value = locations->InAt(0);
1661
1662 if (value.IsRegister()) {
1663 __ testl(value.As<Register>(), value.As<Register>());
1664 } else if (value.IsStackSlot()) {
1665 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
1666 } else {
1667 DCHECK(value.IsConstant()) << value;
1668 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
1669 __ jmp(slow_path->GetEntryLabel());
1670 }
1671 return;
1672 }
1673 __ j(kEqual, slow_path->GetEntryLabel());
1674}
1675
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001676void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001677 LocationSummary* locations =
1678 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001679 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001680 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001681 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1682 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001683}
1684
1685void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
1686 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001687 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001688 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001689
Nicolas Geoffray707c8092014-04-04 10:50:14 +01001690 __ fs()->call(
1691 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001692
Nicolas Geoffray39468442014-09-02 15:17:15 +01001693 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001694 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001695}
1696
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001697void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
1698 LocationSummary* locations =
1699 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1700 locations->SetOut(Location::RegisterLocation(EAX));
1701 InvokeRuntimeCallingConvention calling_convention;
1702 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1703 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1704 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1705}
1706
1707void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
1708 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001709 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001710 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
1711
1712 __ fs()->call(
1713 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
1714
1715 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1716 DCHECK(!codegen_->IsLeafMethod());
1717}
1718
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001719void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001720 LocationSummary* locations =
1721 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001722 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1723 if (location.IsStackSlot()) {
1724 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1725 } else if (location.IsDoubleStackSlot()) {
1726 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001727 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001728 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001729}
1730
1731void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001732 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001733}
1734
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001735void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001736 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001737 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001738 locations->SetInAt(0, Location::RequiresRegister());
1739 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001740}
1741
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001742void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
1743 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01001744 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001745 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01001746 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001747 switch (not_->InputAt(0)->GetType()) {
1748 case Primitive::kPrimBoolean:
1749 __ xorl(out.As<Register>(), Immediate(1));
1750 break;
1751
1752 case Primitive::kPrimInt:
1753 __ notl(out.As<Register>());
1754 break;
1755
1756 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001757 __ notl(out.AsRegisterPairLow<Register>());
1758 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001759 break;
1760
1761 default:
1762 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1763 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001764}
1765
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001766void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001767 LocationSummary* locations =
1768 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001769 locations->SetInAt(0, Location::RequiresRegister());
1770 locations->SetInAt(1, Location::Any());
1771 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001772}
1773
1774void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001775 LocationSummary* locations = compare->GetLocations();
1776 switch (compare->InputAt(0)->GetType()) {
1777 case Primitive::kPrimLong: {
1778 Label less, greater, done;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001779 Register output = locations->Out().As<Register>();
1780 Location left = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001781 Location right = locations->InAt(1);
1782 if (right.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001783 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001784 } else {
1785 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001786 __ cmpl(left.AsRegisterPairHigh<Register>(),
1787 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001788 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001789 __ j(kLess, &less); // Signed compare.
1790 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001791 if (right.IsRegisterPair()) {
1792 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001793 } else {
1794 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001795 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001796 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001797 __ movl(output, Immediate(0));
1798 __ j(kEqual, &done);
1799 __ j(kBelow, &less); // Unsigned compare.
1800
1801 __ Bind(&greater);
1802 __ movl(output, Immediate(1));
1803 __ jmp(&done);
1804
1805 __ Bind(&less);
1806 __ movl(output, Immediate(-1));
1807
1808 __ Bind(&done);
1809 break;
1810 }
1811 default:
1812 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
1813 }
1814}
1815
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001816void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001817 LocationSummary* locations =
1818 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001819 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1820 locations->SetInAt(i, Location::Any());
1821 }
1822 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001823}
1824
1825void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001826 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001827 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001828}
1829
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001830void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001831 LocationSummary* locations =
1832 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001833 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001834 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001835 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001836 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
1837 || (field_type == Primitive::kPrimByte);
1838 // The register allocator does not support multiple
1839 // inputs that die at entry with one in a specific register.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001840 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001841 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001842 locations->SetInAt(1, Location::RegisterLocation(EAX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001843 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001844 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001845 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001846 // Temporary registers for the write barrier.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001847 if (is_object_type) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001848 locations->AddTemp(Location::RequiresRegister());
1849 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001850 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001851 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001852}
1853
1854void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1855 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001856 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001857 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001858 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001859
1860 switch (field_type) {
1861 case Primitive::kPrimBoolean:
1862 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001863 ByteRegister value = locations->InAt(1).As<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001864 __ movb(Address(obj, offset), value);
1865 break;
1866 }
1867
1868 case Primitive::kPrimShort:
1869 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001870 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001871 __ movw(Address(obj, offset), value);
1872 break;
1873 }
1874
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001875 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001876 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001877 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001878 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001879
1880 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001881 Register temp = locations->GetTemp(0).As<Register>();
1882 Register card = locations->GetTemp(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001883 codegen_->MarkGCCard(temp, card, obj, value);
1884 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001885 break;
1886 }
1887
1888 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001889 Location value = locations->InAt(1);
1890 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1891 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001892 break;
1893 }
1894
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001895 case Primitive::kPrimFloat: {
1896 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1897 __ movss(Address(obj, offset), value);
1898 break;
1899 }
1900
1901 case Primitive::kPrimDouble: {
1902 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1903 __ movsd(Address(obj, offset), value);
1904 break;
1905 }
1906
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001907 case Primitive::kPrimVoid:
1908 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001909 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001910 }
1911}
1912
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001913void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
1914 Label is_null;
1915 __ testl(value, value);
1916 __ j(kEqual, &is_null);
1917 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
1918 __ movl(temp, object);
1919 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
1920 __ movb(Address(temp, card, TIMES_1, 0),
1921 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
1922 __ Bind(&is_null);
1923}
1924
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001925void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001926 LocationSummary* locations =
1927 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001928 locations->SetInAt(0, Location::RequiresRegister());
1929 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001930}
1931
1932void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1933 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001934 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001935 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
1936
1937 switch (instruction->GetType()) {
1938 case Primitive::kPrimBoolean: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001939 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001940 __ movzxb(out, Address(obj, offset));
1941 break;
1942 }
1943
1944 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001945 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001946 __ movsxb(out, Address(obj, offset));
1947 break;
1948 }
1949
1950 case Primitive::kPrimShort: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001951 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001952 __ movsxw(out, Address(obj, offset));
1953 break;
1954 }
1955
1956 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001957 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001958 __ movzxw(out, Address(obj, offset));
1959 break;
1960 }
1961
1962 case Primitive::kPrimInt:
1963 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001964 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001965 __ movl(out, Address(obj, offset));
1966 break;
1967 }
1968
1969 case Primitive::kPrimLong: {
1970 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001971 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
1972 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001973 break;
1974 }
1975
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001976 case Primitive::kPrimFloat: {
1977 XmmRegister out = locations->Out().As<XmmRegister>();
1978 __ movss(out, Address(obj, offset));
1979 break;
1980 }
1981
1982 case Primitive::kPrimDouble: {
1983 XmmRegister out = locations->Out().As<XmmRegister>();
1984 __ movsd(out, Address(obj, offset));
1985 break;
1986 }
1987
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001988 case Primitive::kPrimVoid:
1989 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07001990 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001991 }
1992}
1993
1994void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001995 LocationSummary* locations =
1996 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001997 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001998 if (instruction->HasUses()) {
1999 locations->SetOut(Location::SameAsFirstInput());
2000 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002001}
2002
2003void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002004 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002005 codegen_->AddSlowPath(slow_path);
2006
2007 LocationSummary* locations = instruction->GetLocations();
2008 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002009
2010 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002011 __ cmpl(obj.As<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002012 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002013 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002014 } else {
2015 DCHECK(obj.IsConstant()) << obj;
2016 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2017 __ jmp(slow_path->GetEntryLabel());
2018 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002019 }
2020 __ j(kEqual, slow_path->GetEntryLabel());
2021}
2022
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002023void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002024 LocationSummary* locations =
2025 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002026 locations->SetInAt(0, Location::RequiresRegister());
2027 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2028 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002029}
2030
2031void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
2032 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002033 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002034 Location index = locations->InAt(1);
2035
2036 switch (instruction->GetType()) {
2037 case Primitive::kPrimBoolean: {
2038 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002039 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002040 if (index.IsConstant()) {
2041 __ movzxb(out, Address(obj,
2042 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2043 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002044 __ movzxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002045 }
2046 break;
2047 }
2048
2049 case Primitive::kPrimByte: {
2050 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002051 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002052 if (index.IsConstant()) {
2053 __ movsxb(out, Address(obj,
2054 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2055 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002056 __ movsxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002057 }
2058 break;
2059 }
2060
2061 case Primitive::kPrimShort: {
2062 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002063 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002064 if (index.IsConstant()) {
2065 __ movsxw(out, Address(obj,
2066 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2067 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002068 __ movsxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002069 }
2070 break;
2071 }
2072
2073 case Primitive::kPrimChar: {
2074 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002075 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002076 if (index.IsConstant()) {
2077 __ movzxw(out, Address(obj,
2078 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2079 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002080 __ movzxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002081 }
2082 break;
2083 }
2084
2085 case Primitive::kPrimInt:
2086 case Primitive::kPrimNot: {
2087 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002088 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002089 if (index.IsConstant()) {
2090 __ movl(out, Address(obj,
2091 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2092 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002093 __ movl(out, Address(obj, index.As<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002094 }
2095 break;
2096 }
2097
2098 case Primitive::kPrimLong: {
2099 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002100 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002101 if (index.IsConstant()) {
2102 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002103 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
2104 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002105 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002106 __ movl(out.AsRegisterPairLow<Register>(),
2107 Address(obj, index.As<Register>(), TIMES_8, data_offset));
2108 __ movl(out.AsRegisterPairHigh<Register>(),
2109 Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002110 }
2111 break;
2112 }
2113
2114 case Primitive::kPrimFloat:
2115 case Primitive::kPrimDouble:
2116 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002117 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002118 case Primitive::kPrimVoid:
2119 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002120 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002121 }
2122}
2123
2124void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002125 Primitive::Type value_type = instruction->GetComponentType();
2126 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2127 instruction,
2128 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
2129
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002130 if (value_type == Primitive::kPrimNot) {
2131 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002132 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2133 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2134 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002135 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002136 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
2137 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002138 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002139 // In case of a byte operation, the register allocator does not support multiple
2140 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002141 locations->SetInAt(0, Location::RequiresRegister());
2142 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002143 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002144 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002145 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002146 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002147 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002148 }
2149 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002150}
2151
2152void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
2153 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002154 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002155 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002156 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002157 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002158
2159 switch (value_type) {
2160 case Primitive::kPrimBoolean:
2161 case Primitive::kPrimByte: {
2162 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002163 if (index.IsConstant()) {
2164 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002165 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002166 __ movb(Address(obj, offset), value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002167 } else {
2168 __ movb(Address(obj, offset),
2169 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2170 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002171 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002172 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002173 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
2174 value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002175 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002176 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002177 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2178 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002179 }
2180 break;
2181 }
2182
2183 case Primitive::kPrimShort:
2184 case Primitive::kPrimChar: {
2185 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002186 if (index.IsConstant()) {
2187 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002188 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002189 __ movw(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002190 } else {
2191 __ movw(Address(obj, offset),
2192 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2193 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002194 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002195 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002196 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
2197 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002198 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002199 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002200 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2201 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002202 }
2203 break;
2204 }
2205
2206 case Primitive::kPrimInt: {
2207 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002208 if (index.IsConstant()) {
2209 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002210 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002211 __ movl(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002212 } else {
2213 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2214 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002215 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002216 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002217 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
2218 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002219 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002220 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002221 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2222 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002223 }
2224 break;
2225 }
2226
2227 case Primitive::kPrimNot: {
2228 DCHECK(!codegen_->IsLeafMethod());
2229 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01002230 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002231 break;
2232 }
2233
2234 case Primitive::kPrimLong: {
2235 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002236 if (index.IsConstant()) {
2237 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002238 if (value.IsRegisterPair()) {
2239 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
2240 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002241 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002242 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002243 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
2244 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
2245 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
2246 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002247 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002248 if (value.IsRegisterPair()) {
2249 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
2250 value.AsRegisterPairLow<Register>());
2251 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
2252 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002253 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002254 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002255 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002256 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002257 Immediate(Low32Bits(val)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002258 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002259 Immediate(High32Bits(val)));
2260 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002261 }
2262 break;
2263 }
2264
2265 case Primitive::kPrimFloat:
2266 case Primitive::kPrimDouble:
2267 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002268 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002269 case Primitive::kPrimVoid:
2270 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002271 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002272 }
2273}
2274
2275void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
2276 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002277 locations->SetInAt(0, Location::RequiresRegister());
2278 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002279 instruction->SetLocations(locations);
2280}
2281
2282void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
2283 LocationSummary* locations = instruction->GetLocations();
2284 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002285 Register obj = locations->InAt(0).As<Register>();
2286 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002287 __ movl(out, Address(obj, offset));
2288}
2289
2290void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002291 LocationSummary* locations =
2292 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002293 locations->SetInAt(0, Location::RequiresRegister());
2294 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002295 if (instruction->HasUses()) {
2296 locations->SetOut(Location::SameAsFirstInput());
2297 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002298}
2299
2300void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
2301 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002302 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002303 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002304 codegen_->AddSlowPath(slow_path);
2305
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002306 Register index = locations->InAt(0).As<Register>();
2307 Register length = locations->InAt(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002308
2309 __ cmpl(index, length);
2310 __ j(kAboveEqual, slow_path->GetEntryLabel());
2311}
2312
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002313void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
2314 temp->SetLocations(nullptr);
2315}
2316
2317void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
2318 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002319 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002320}
2321
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002322void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002323 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002324 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002325}
2326
2327void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002328 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2329}
2330
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002331void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
2332 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2333}
2334
2335void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002336 HBasicBlock* block = instruction->GetBlock();
2337 if (block->GetLoopInformation() != nullptr) {
2338 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2339 // The back edge will generate the suspend check.
2340 return;
2341 }
2342 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2343 // The goto will generate the suspend check.
2344 return;
2345 }
2346 GenerateSuspendCheck(instruction, nullptr);
2347}
2348
2349void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
2350 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002351 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002352 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002353 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002354 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002355 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002356 if (successor == nullptr) {
2357 __ j(kNotEqual, slow_path->GetEntryLabel());
2358 __ Bind(slow_path->GetReturnLabel());
2359 } else {
2360 __ j(kEqual, codegen_->GetLabelOf(successor));
2361 __ jmp(slow_path->GetEntryLabel());
2362 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002363}
2364
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002365X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
2366 return codegen_->GetAssembler();
2367}
2368
2369void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
2370 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002371 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002372 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2373 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
2374 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
2375}
2376
2377void ParallelMoveResolverX86::EmitMove(size_t index) {
2378 MoveOperands* move = moves_.Get(index);
2379 Location source = move->GetSource();
2380 Location destination = move->GetDestination();
2381
2382 if (source.IsRegister()) {
2383 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002384 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002385 } else {
2386 DCHECK(destination.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002387 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002388 }
2389 } else if (source.IsStackSlot()) {
2390 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002391 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002392 } else {
2393 DCHECK(destination.IsStackSlot());
2394 MoveMemoryToMemory(destination.GetStackIndex(),
2395 source.GetStackIndex());
2396 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002397 } else if (source.IsConstant()) {
2398 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
2399 Immediate imm(instruction->AsIntConstant()->GetValue());
2400 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002401 __ movl(destination.As<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002402 } else {
2403 __ movl(Address(ESP, destination.GetStackIndex()), imm);
2404 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002405 } else {
2406 LOG(FATAL) << "Unimplemented";
2407 }
2408}
2409
2410void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002411 Register suggested_scratch = reg == EAX ? EBX : EAX;
2412 ScratchRegisterScope ensure_scratch(
2413 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2414
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002415 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2416 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
2417 __ movl(Address(ESP, mem + stack_offset), reg);
2418 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
2419}
2420
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002421void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
2422 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002423 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
2424
2425 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002426 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002427 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2428
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002429 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
2430 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
2431 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
2432 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
2433 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
2434 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
2435}
2436
2437void ParallelMoveResolverX86::EmitSwap(size_t index) {
2438 MoveOperands* move = moves_.Get(index);
2439 Location source = move->GetSource();
2440 Location destination = move->GetDestination();
2441
2442 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002443 __ xchgl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002444 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002445 Exchange(source.As<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002446 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002447 Exchange(destination.As<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002448 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
2449 Exchange(destination.GetStackIndex(), source.GetStackIndex());
2450 } else {
2451 LOG(FATAL) << "Unimplemented";
2452 }
2453}
2454
2455void ParallelMoveResolverX86::SpillScratch(int reg) {
2456 __ pushl(static_cast<Register>(reg));
2457}
2458
2459void ParallelMoveResolverX86::RestoreScratch(int reg) {
2460 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002461}
2462
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002463void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002464 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2465 ? LocationSummary::kCallOnSlowPath
2466 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002467 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002468 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002469 locations->SetOut(Location::RequiresRegister());
2470}
2471
2472void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
2473 Register out = cls->GetLocations()->Out().As<Register>();
2474 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002475 DCHECK(!cls->CanCallRuntime());
2476 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002477 codegen_->LoadCurrentMethod(out);
2478 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2479 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002480 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002481 codegen_->LoadCurrentMethod(out);
2482 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2483 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002484
2485 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
2486 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2487 codegen_->AddSlowPath(slow_path);
2488 __ testl(out, out);
2489 __ j(kEqual, slow_path->GetEntryLabel());
2490 if (cls->MustGenerateClinitCheck()) {
2491 GenerateClassInitializationCheck(slow_path, out);
2492 } else {
2493 __ Bind(slow_path->GetExitLabel());
2494 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002495 }
2496}
2497
2498void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
2499 LocationSummary* locations =
2500 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2501 locations->SetInAt(0, Location::RequiresRegister());
2502 if (check->HasUses()) {
2503 locations->SetOut(Location::SameAsFirstInput());
2504 }
2505}
2506
2507void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002508 // We assume the class to not be null.
2509 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
2510 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002511 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002512 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<Register>());
2513}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002514
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002515void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
2516 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002517 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2518 Immediate(mirror::Class::kStatusInitialized));
2519 __ j(kLess, slow_path->GetEntryLabel());
2520 __ Bind(slow_path->GetExitLabel());
2521 // No need for memory fence, thanks to the X86 memory model.
2522}
2523
2524void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2525 LocationSummary* locations =
2526 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2527 locations->SetInAt(0, Location::RequiresRegister());
2528 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2529}
2530
2531void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2532 LocationSummary* locations = instruction->GetLocations();
2533 Register cls = locations->InAt(0).As<Register>();
2534 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2535
2536 switch (instruction->GetType()) {
2537 case Primitive::kPrimBoolean: {
2538 Register out = locations->Out().As<Register>();
2539 __ movzxb(out, Address(cls, offset));
2540 break;
2541 }
2542
2543 case Primitive::kPrimByte: {
2544 Register out = locations->Out().As<Register>();
2545 __ movsxb(out, Address(cls, offset));
2546 break;
2547 }
2548
2549 case Primitive::kPrimShort: {
2550 Register out = locations->Out().As<Register>();
2551 __ movsxw(out, Address(cls, offset));
2552 break;
2553 }
2554
2555 case Primitive::kPrimChar: {
2556 Register out = locations->Out().As<Register>();
2557 __ movzxw(out, Address(cls, offset));
2558 break;
2559 }
2560
2561 case Primitive::kPrimInt:
2562 case Primitive::kPrimNot: {
2563 Register out = locations->Out().As<Register>();
2564 __ movl(out, Address(cls, offset));
2565 break;
2566 }
2567
2568 case Primitive::kPrimLong: {
2569 // TODO: support volatile.
2570 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(cls, offset));
2571 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(cls, kX86WordSize + offset));
2572 break;
2573 }
2574
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002575 case Primitive::kPrimFloat: {
2576 XmmRegister out = locations->Out().As<XmmRegister>();
2577 __ movss(out, Address(cls, offset));
2578 break;
2579 }
2580
2581 case Primitive::kPrimDouble: {
2582 XmmRegister out = locations->Out().As<XmmRegister>();
2583 __ movsd(out, Address(cls, offset));
2584 break;
2585 }
2586
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002587 case Primitive::kPrimVoid:
2588 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2589 UNREACHABLE();
2590 }
2591}
2592
2593void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2594 LocationSummary* locations =
2595 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2596 locations->SetInAt(0, Location::RequiresRegister());
2597 Primitive::Type field_type = instruction->GetFieldType();
2598 bool is_object_type = field_type == Primitive::kPrimNot;
2599 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2600 || (field_type == Primitive::kPrimByte);
2601 // The register allocator does not support multiple
2602 // inputs that die at entry with one in a specific register.
2603 if (is_byte_type) {
2604 // Ensure the value is in a byte register.
2605 locations->SetInAt(1, Location::RegisterLocation(EAX));
2606 } else {
2607 locations->SetInAt(1, Location::RequiresRegister());
2608 }
2609 // Temporary registers for the write barrier.
2610 if (is_object_type) {
2611 locations->AddTemp(Location::RequiresRegister());
2612 // Ensure the card is in a byte register.
2613 locations->AddTemp(Location::RegisterLocation(ECX));
2614 }
2615}
2616
2617void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2618 LocationSummary* locations = instruction->GetLocations();
2619 Register cls = locations->InAt(0).As<Register>();
2620 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2621 Primitive::Type field_type = instruction->GetFieldType();
2622
2623 switch (field_type) {
2624 case Primitive::kPrimBoolean:
2625 case Primitive::kPrimByte: {
2626 ByteRegister value = locations->InAt(1).As<ByteRegister>();
2627 __ movb(Address(cls, offset), value);
2628 break;
2629 }
2630
2631 case Primitive::kPrimShort:
2632 case Primitive::kPrimChar: {
2633 Register value = locations->InAt(1).As<Register>();
2634 __ movw(Address(cls, offset), value);
2635 break;
2636 }
2637
2638 case Primitive::kPrimInt:
2639 case Primitive::kPrimNot: {
2640 Register value = locations->InAt(1).As<Register>();
2641 __ movl(Address(cls, offset), value);
2642
2643 if (field_type == Primitive::kPrimNot) {
2644 Register temp = locations->GetTemp(0).As<Register>();
2645 Register card = locations->GetTemp(1).As<Register>();
2646 codegen_->MarkGCCard(temp, card, cls, value);
2647 }
2648 break;
2649 }
2650
2651 case Primitive::kPrimLong: {
2652 Location value = locations->InAt(1);
2653 __ movl(Address(cls, offset), value.AsRegisterPairLow<Register>());
2654 __ movl(Address(cls, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
2655 break;
2656 }
2657
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002658 case Primitive::kPrimFloat: {
2659 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2660 __ movss(Address(cls, offset), value);
2661 break;
2662 }
2663
2664 case Primitive::kPrimDouble: {
2665 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2666 __ movsd(Address(cls, offset), value);
2667 break;
2668 }
2669
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002670 case Primitive::kPrimVoid:
2671 LOG(FATAL) << "Unreachable type " << field_type;
2672 UNREACHABLE();
2673 }
2674}
2675
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002676void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
2677 LocationSummary* locations =
2678 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2679 locations->SetOut(Location::RequiresRegister());
2680}
2681
2682void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
2683 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
2684 codegen_->AddSlowPath(slow_path);
2685
2686 Register out = load->GetLocations()->Out().As<Register>();
2687 codegen_->LoadCurrentMethod(out);
2688 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
2689 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2690 __ testl(out, out);
2691 __ j(kEqual, slow_path->GetEntryLabel());
2692 __ Bind(slow_path->GetExitLabel());
2693}
2694
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002695void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
2696 LocationSummary* locations =
2697 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2698 locations->SetOut(Location::RequiresRegister());
2699}
2700
2701void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
2702 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
2703 __ fs()->movl(load->GetLocations()->Out().As<Register>(), address);
2704 __ fs()->movl(address, Immediate(0));
2705}
2706
2707void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
2708 LocationSummary* locations =
2709 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2710 InvokeRuntimeCallingConvention calling_convention;
2711 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2712}
2713
2714void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
2715 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
2716 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2717}
2718
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002719void LocationsBuilderX86::VisitTypeCheck(HTypeCheck* instruction) {
2720 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
2721 ? LocationSummary::kNoCall
2722 : LocationSummary::kCallOnSlowPath;
2723 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2724 locations->SetInAt(0, Location::RequiresRegister());
2725 locations->SetInAt(1, Location::Any());
2726 locations->SetOut(Location::RequiresRegister());
2727}
2728
2729void InstructionCodeGeneratorX86::VisitTypeCheck(HTypeCheck* instruction) {
2730 LocationSummary* locations = instruction->GetLocations();
2731 Register obj = locations->InAt(0).As<Register>();
2732 Location cls = locations->InAt(1);
2733 Register out = locations->Out().As<Register>();
2734 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2735 Label done, zero;
2736 SlowPathCodeX86* slow_path = nullptr;
2737
2738 // Return 0 if `obj` is null.
2739 // TODO: avoid this check if we know obj is not null.
2740 __ testl(obj, obj);
2741 __ j(kEqual, &zero);
2742 __ movl(out, Address(obj, class_offset));
2743 // Compare the class of `obj` with `cls`.
2744 if (cls.IsRegister()) {
2745 __ cmpl(out, cls.As<Register>());
2746 } else {
2747 DCHECK(cls.IsStackSlot()) << cls;
2748 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
2749 }
2750
2751 if (instruction->IsClassFinal()) {
2752 // Classes must be equal for the instanceof to succeed.
2753 __ j(kNotEqual, &zero);
2754 __ movl(out, Immediate(1));
2755 __ jmp(&done);
2756 } else {
2757 // If the classes are not equal, we go into a slow path.
2758 DCHECK(locations->OnlyCallsOnSlowPath());
2759 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
2760 instruction, Location::RegisterLocation(out));
2761 codegen_->AddSlowPath(slow_path);
2762 __ j(kNotEqual, slow_path->GetEntryLabel());
2763 __ movl(out, Immediate(1));
2764 __ jmp(&done);
2765 }
2766 __ Bind(&zero);
2767 __ movl(out, Immediate(0));
2768 if (slow_path != nullptr) {
2769 __ Bind(slow_path->GetExitLabel());
2770 }
2771 __ Bind(&done);
2772}
2773
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002774} // namespace x86
2775} // namespace art