blob: 14c7fe44563fe9fd68d51157794975767530ab75 [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:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000273 TypeCheckSlowPathX86(HInstruction* instruction,
274 Location class_to_check,
275 Location object_class,
276 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000277 : instruction_(instruction),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000278 class_to_check_(class_to_check),
279 object_class_(object_class),
280 dex_pc_(dex_pc) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000281
282 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
283 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000284 DCHECK(instruction_->IsCheckCast()
285 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000286
287 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
288 __ Bind(GetEntryLabel());
289 codegen->SaveLiveRegisters(locations);
290
291 // We're moving two locations to locations that could overlap, so we need a parallel
292 // move resolver.
293 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000294 MoveOperands move1(class_to_check_,
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000295 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
296 nullptr);
297 MoveOperands move2(object_class_,
298 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
299 nullptr);
300 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
301 parallel_move.AddMove(&move1);
302 parallel_move.AddMove(&move2);
303 x86_codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
304
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000305 if (instruction_->IsInstanceOf()) {
306 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInstanceofNonTrivial)));
307 } else {
308 DCHECK(instruction_->IsCheckCast());
309 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pCheckCast)));
310 }
311
312 codegen->RecordPcInfo(instruction_, dex_pc_);
313 if (instruction_->IsInstanceOf()) {
314 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
315 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000316 codegen->RestoreLiveRegisters(locations);
317
318 __ jmp(GetExitLabel());
319 }
320
321 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000322 HInstruction* const instruction_;
323 const Location class_to_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324 const Location object_class_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000325 const uint32_t dex_pc_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000326
327 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
328};
329
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100330#undef __
331#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
332
Dave Allison20dfc792014-06-16 20:44:29 -0700333inline Condition X86Condition(IfCondition cond) {
334 switch (cond) {
335 case kCondEQ: return kEqual;
336 case kCondNE: return kNotEqual;
337 case kCondLT: return kLess;
338 case kCondLE: return kLessEqual;
339 case kCondGT: return kGreater;
340 case kCondGE: return kGreaterEqual;
341 default:
342 LOG(FATAL) << "Unknown if condition";
343 }
344 return kEqual;
345}
346
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100347void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
348 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
349}
350
351void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
352 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
353}
354
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100355size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
356 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
357 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100358}
359
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100360size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
361 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
362 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100363}
364
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100365CodeGeneratorX86::CodeGeneratorX86(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100366 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfXmmRegisters, kNumberOfRegisterPairs),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100367 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100368 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100369 instruction_visitor_(graph, this),
370 move_resolver_(graph->GetArena(), this) {}
371
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100372size_t CodeGeneratorX86::FrameEntrySpillSize() const {
373 return kNumberOfPushedRegistersAtEntry * kX86WordSize;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100374}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100375
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100376Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100377 switch (type) {
378 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100379 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100380 X86ManagedRegister pair =
381 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100382 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
383 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100384 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
385 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100386 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100387 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100388 }
389
390 case Primitive::kPrimByte:
391 case Primitive::kPrimBoolean:
392 case Primitive::kPrimChar:
393 case Primitive::kPrimShort:
394 case Primitive::kPrimInt:
395 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100396 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100397 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100398 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100399 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
400 X86ManagedRegister current =
401 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
402 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100403 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100404 }
405 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100406 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100407 }
408
409 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100410 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100411 return Location::FpuRegisterLocation(
412 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100413 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100414
415 case Primitive::kPrimVoid:
416 LOG(FATAL) << "Unreachable type " << type;
417 }
418
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100419 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100420}
421
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100422void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100423 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100424 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100425
426 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100427 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100428
429 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100430 blocked_core_registers_[EBP] = true;
431 blocked_core_registers_[ESI] = true;
432 blocked_core_registers_[EDI] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100433
434 UpdateBlockedPairRegisters();
435}
436
437void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
438 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
439 X86ManagedRegister current =
440 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
441 if (blocked_core_registers_[current.AsRegisterPairLow()]
442 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
443 blocked_register_pairs_[i] = true;
444 }
445 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100446}
447
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100448InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
449 : HGraphVisitor(graph),
450 assembler_(codegen->GetAssembler()),
451 codegen_(codegen) {}
452
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000453void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000454 // Create a fake register to mimic Quick.
455 static const int kFakeReturnRegister = 8;
456 core_spill_mask_ |= (1 << kFakeReturnRegister);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000457
Dave Allison648d7112014-07-25 16:15:27 -0700458 bool skip_overflow_check = IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100459 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
460 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100461 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100462 }
463
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100464 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100465 __ subl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100466
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100467 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100468 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86();
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100469 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100470
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100471 __ fs()->cmpl(ESP, Address::Absolute(Thread::StackEndOffset<kX86WordSize>()));
472 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100473 }
474
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100475 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000476}
477
478void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100479 __ addl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000480}
481
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100482void CodeGeneratorX86::Bind(HBasicBlock* block) {
483 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000484}
485
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100486void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100487 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000488}
489
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100490Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
491 switch (load->GetType()) {
492 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100493 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100494 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
495 break;
496
497 case Primitive::kPrimInt:
498 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100499 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100500 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100501
502 case Primitive::kPrimBoolean:
503 case Primitive::kPrimByte:
504 case Primitive::kPrimChar:
505 case Primitive::kPrimShort:
506 case Primitive::kPrimVoid:
507 LOG(FATAL) << "Unexpected type " << load->GetType();
508 }
509
510 LOG(FATAL) << "Unreachable";
511 return Location();
512}
513
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100514Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
515 switch (type) {
516 case Primitive::kPrimBoolean:
517 case Primitive::kPrimByte:
518 case Primitive::kPrimChar:
519 case Primitive::kPrimShort:
520 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100521 case Primitive::kPrimFloat:
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100522 case Primitive::kPrimNot: {
523 uint32_t index = gp_index_++;
524 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100525 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100526 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100527 return Location::StackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100528 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100529 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100530
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100531 case Primitive::kPrimLong:
532 case Primitive::kPrimDouble: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100533 uint32_t index = gp_index_;
534 gp_index_ += 2;
535 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100536 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
537 calling_convention.GetRegisterPairAt(index));
538 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100539 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000540 // On X86, the register index and stack index of a quick parameter is the same, since
541 // we are passing floating pointer values in core registers.
542 return Location::QuickParameter(index, index);
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100543 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100544 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100545 }
546 }
547
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100548 case Primitive::kPrimVoid:
549 LOG(FATAL) << "Unexpected parameter type " << type;
550 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100551 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100552 return Location();
553}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100554
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100555void CodeGeneratorX86::Move32(Location destination, Location source) {
556 if (source.Equals(destination)) {
557 return;
558 }
559 if (destination.IsRegister()) {
560 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100561 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100562 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100563 __ movd(destination.As<Register>(), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100564 } else {
565 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100566 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100567 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100568 } else if (destination.IsFpuRegister()) {
569 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100570 __ movd(destination.As<XmmRegister>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100571 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100572 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100573 } else {
574 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100575 __ movss(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100576 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100577 } else {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100578 DCHECK(destination.IsStackSlot());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100579 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100580 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100581 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100582 __ movss(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100583 } else {
584 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100585 __ pushl(Address(ESP, source.GetStackIndex()));
586 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100587 }
588 }
589}
590
591void CodeGeneratorX86::Move64(Location destination, Location source) {
592 if (source.Equals(destination)) {
593 return;
594 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100595 if (destination.IsRegisterPair()) {
596 if (source.IsRegisterPair()) {
597 __ movl(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
598 __ movl(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100599 } else if (source.IsFpuRegister()) {
600 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100601 } else if (source.IsQuickParameter()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000602 uint16_t register_index = source.GetQuickParameterRegisterIndex();
603 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100604 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100605 __ movl(destination.AsRegisterPairLow<Register>(),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000606 calling_convention.GetRegisterAt(register_index));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100607 __ movl(destination.AsRegisterPairHigh<Register>(), Address(ESP,
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000608 calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100609 } else {
610 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100611 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
612 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100613 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
614 }
615 } else if (destination.IsQuickParameter()) {
616 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000617 uint16_t register_index = destination.GetQuickParameterRegisterIndex();
618 uint16_t stack_index = destination.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100619 if (source.IsRegister()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000620 __ movl(calling_convention.GetRegisterAt(register_index), source.AsRegisterPairLow<Register>());
621 __ movl(Address(ESP, calling_convention.GetStackOffsetOf(stack_index + 1)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100622 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100623 } else if (source.IsFpuRegister()) {
624 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100625 } else {
626 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000627 __ movl(calling_convention.GetRegisterAt(register_index),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100628 Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100629 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000630 __ popl(Address(ESP, calling_convention.GetStackOffsetOf(stack_index + 1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100631 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100632 } else if (destination.IsFpuRegister()) {
633 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100634 __ movsd(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100635 } else {
636 LOG(FATAL) << "Unimplemented";
637 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100638 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100639 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100640 if (source.IsRegisterPair()) {
641 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100642 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100643 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100644 } else if (source.IsQuickParameter()) {
645 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000646 uint16_t register_index = source.GetQuickParameterRegisterIndex();
647 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100648 __ movl(Address(ESP, destination.GetStackIndex()),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000649 calling_convention.GetRegisterAt(register_index));
650 DCHECK_EQ(calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100651 static_cast<size_t>(destination.GetHighStackIndex(kX86WordSize)));
652 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100653 __ movsd(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100654 } else {
655 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100656 __ pushl(Address(ESP, source.GetStackIndex()));
657 __ popl(Address(ESP, destination.GetStackIndex()));
658 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
659 __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100660 }
661 }
662}
663
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100664void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Roland Levillain476df552014-10-09 17:51:36 +0100665 if (instruction->IsIntConstant()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100666 Immediate imm(instruction->AsIntConstant()->GetValue());
667 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100668 __ movl(location.As<Register>(), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100669 } else if (location.IsStackSlot()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100670 __ movl(Address(ESP, location.GetStackIndex()), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100671 } else {
672 DCHECK(location.IsConstant());
673 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100674 }
Roland Levillain476df552014-10-09 17:51:36 +0100675 } else if (instruction->IsLongConstant()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100676 int64_t value = instruction->AsLongConstant()->GetValue();
677 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100678 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
679 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100680 } else if (location.IsDoubleStackSlot()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100681 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
682 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100683 } else {
684 DCHECK(location.IsConstant());
685 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100686 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000687 } else if (instruction->IsTemporary()) {
688 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
689 Move32(location, temp_location);
Roland Levillain476df552014-10-09 17:51:36 +0100690 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100691 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100692 switch (instruction->GetType()) {
693 case Primitive::kPrimBoolean:
694 case Primitive::kPrimByte:
695 case Primitive::kPrimChar:
696 case Primitive::kPrimShort:
697 case Primitive::kPrimInt:
698 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100699 case Primitive::kPrimFloat:
700 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100701 break;
702
703 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100704 case Primitive::kPrimDouble:
705 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100706 break;
707
708 default:
709 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
710 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000711 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100712 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100713 switch (instruction->GetType()) {
714 case Primitive::kPrimBoolean:
715 case Primitive::kPrimByte:
716 case Primitive::kPrimChar:
717 case Primitive::kPrimShort:
718 case Primitive::kPrimInt:
719 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100720 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100721 Move32(location, instruction->GetLocations()->Out());
722 break;
723
724 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100725 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100726 Move64(location, instruction->GetLocations()->Out());
727 break;
728
729 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100730 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100731 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000732 }
733}
734
735void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000736 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000737}
738
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000739void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000740 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100741 DCHECK(!successor->IsExitBlock());
742
743 HBasicBlock* block = got->GetBlock();
744 HInstruction* previous = got->GetPrevious();
745
746 HLoopInformation* info = block->GetLoopInformation();
747 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
748 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
749 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
750 return;
751 }
752
753 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
754 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
755 }
756 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000757 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000758 }
759}
760
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000761void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000762 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000763}
764
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000765void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700766 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000767 if (kIsDebugBuild) {
768 __ Comment("Unreachable");
769 __ int3();
770 }
771}
772
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000773void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100774 LocationSummary* locations =
775 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100776 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100777 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100778 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100779 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000780}
781
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000782void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700783 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100784 if (cond->IsIntConstant()) {
785 // Constant condition, statically compared against 1.
786 int32_t cond_value = cond->AsIntConstant()->GetValue();
787 if (cond_value == 1) {
788 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
789 if_instr->IfTrueSuccessor())) {
790 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100791 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100792 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100793 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100794 DCHECK_EQ(cond_value, 0);
795 }
796 } else {
797 bool materialized =
798 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
799 // Moves do not affect the eflags register, so if the condition is
800 // evaluated just before the if, we don't need to evaluate it
801 // again.
802 bool eflags_set = cond->IsCondition()
803 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
804 if (materialized) {
805 if (!eflags_set) {
806 // Materialized condition, compare against 0.
807 Location lhs = if_instr->GetLocations()->InAt(0);
808 if (lhs.IsRegister()) {
809 __ cmpl(lhs.As<Register>(), Immediate(0));
810 } else {
811 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
812 }
813 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
814 } else {
815 __ j(X86Condition(cond->AsCondition()->GetCondition()),
816 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
817 }
818 } else {
819 Location lhs = cond->GetLocations()->InAt(0);
820 Location rhs = cond->GetLocations()->InAt(1);
821 // LHS is guaranteed to be in a register (see
822 // LocationsBuilderX86::VisitCondition).
823 if (rhs.IsRegister()) {
824 __ cmpl(lhs.As<Register>(), rhs.As<Register>());
825 } else if (rhs.IsConstant()) {
826 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
827 Immediate imm(instruction->AsIntConstant()->GetValue());
828 __ cmpl(lhs.As<Register>(), imm);
829 } else {
830 __ cmpl(lhs.As<Register>(), Address(ESP, rhs.GetStackIndex()));
831 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100832 __ j(X86Condition(cond->AsCondition()->GetCondition()),
833 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700834 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100835 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100836 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
837 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700838 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000839 }
840}
841
842void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000843 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000844}
845
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000846void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
847 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000848}
849
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000850void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100851 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000852}
853
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000854void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100855 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700856 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000857}
858
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100859void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100860 LocationSummary* locations =
861 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100862 switch (store->InputAt(1)->GetType()) {
863 case Primitive::kPrimBoolean:
864 case Primitive::kPrimByte:
865 case Primitive::kPrimChar:
866 case Primitive::kPrimShort:
867 case Primitive::kPrimInt:
868 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100869 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100870 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
871 break;
872
873 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100874 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100875 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
876 break;
877
878 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100879 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100880 }
881 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000882}
883
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000884void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700885 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000886}
887
Dave Allison20dfc792014-06-16 20:44:29 -0700888void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100889 LocationSummary* locations =
890 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100891 locations->SetInAt(0, Location::RequiresRegister());
892 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100893 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100894 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100895 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000896}
897
Dave Allison20dfc792014-06-16 20:44:29 -0700898void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
899 if (comp->NeedsMaterialization()) {
900 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100901 Register reg = locations->Out().As<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100902 // Clear register: setcc only sets the low byte.
903 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700904 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100905 __ cmpl(locations->InAt(0).As<Register>(),
906 locations->InAt(1).As<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100907 } else if (locations->InAt(1).IsConstant()) {
908 HConstant* instruction = locations->InAt(1).GetConstant();
909 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100910 __ cmpl(locations->InAt(0).As<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700911 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100912 __ cmpl(locations->InAt(0).As<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700913 Address(ESP, locations->InAt(1).GetStackIndex()));
914 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100915 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100916 }
Dave Allison20dfc792014-06-16 20:44:29 -0700917}
918
919void LocationsBuilderX86::VisitEqual(HEqual* comp) {
920 VisitCondition(comp);
921}
922
923void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
924 VisitCondition(comp);
925}
926
927void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
928 VisitCondition(comp);
929}
930
931void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
932 VisitCondition(comp);
933}
934
935void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
936 VisitCondition(comp);
937}
938
939void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
940 VisitCondition(comp);
941}
942
943void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
944 VisitCondition(comp);
945}
946
947void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
948 VisitCondition(comp);
949}
950
951void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
952 VisitCondition(comp);
953}
954
955void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
956 VisitCondition(comp);
957}
958
959void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
960 VisitCondition(comp);
961}
962
963void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
964 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000965}
966
967void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100968 LocationSummary* locations =
969 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100970 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000971}
972
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000973void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100974 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700975 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000976}
977
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100978void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100979 LocationSummary* locations =
980 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100981 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100982}
983
984void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
985 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700986 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100987}
988
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100989void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
990 LocationSummary* locations =
991 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
992 locations->SetOut(Location::ConstantLocation(constant));
993}
994
995void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
996 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700997 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100998}
999
1000void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1001 LocationSummary* locations =
1002 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1003 locations->SetOut(Location::ConstantLocation(constant));
1004}
1005
1006void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1007 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001008 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001009}
1010
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001011void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001012 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001013}
1014
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001015void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001016 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001017 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001018 __ ret();
1019}
1020
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001021void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001022 LocationSummary* locations =
1023 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001024 switch (ret->InputAt(0)->GetType()) {
1025 case Primitive::kPrimBoolean:
1026 case Primitive::kPrimByte:
1027 case Primitive::kPrimChar:
1028 case Primitive::kPrimShort:
1029 case Primitive::kPrimInt:
1030 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001031 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001032 break;
1033
1034 case Primitive::kPrimLong:
1035 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001036 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001037 break;
1038
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001039 case Primitive::kPrimFloat:
1040 case Primitive::kPrimDouble:
1041 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001042 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001043 break;
1044
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001045 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001046 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001047 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001048}
1049
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001050void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001051 if (kIsDebugBuild) {
1052 switch (ret->InputAt(0)->GetType()) {
1053 case Primitive::kPrimBoolean:
1054 case Primitive::kPrimByte:
1055 case Primitive::kPrimChar:
1056 case Primitive::kPrimShort:
1057 case Primitive::kPrimInt:
1058 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001059 DCHECK_EQ(ret->GetLocations()->InAt(0).As<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001060 break;
1061
1062 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001063 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1064 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001065 break;
1066
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001067 case Primitive::kPrimFloat:
1068 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001069 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001070 break;
1071
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001072 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001073 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001074 }
1075 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001076 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001077 __ ret();
1078}
1079
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001080void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001081 HandleInvoke(invoke);
1082}
1083
1084void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001085 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001086
1087 // TODO: Implement all kinds of calls:
1088 // 1) boot -> boot
1089 // 2) app -> boot
1090 // 3) app -> app
1091 //
1092 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1093
1094 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001095 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001096 // temp = temp->dex_cache_resolved_methods_;
1097 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
1098 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001099 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001100 // (temp + offset_of_quick_compiled_code)()
1101 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1102
1103 DCHECK(!codegen_->IsLeafMethod());
1104 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1105}
1106
1107void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1108 HandleInvoke(invoke);
1109}
1110
1111void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001112 LocationSummary* locations =
1113 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001114 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001115
1116 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001117 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001118 HInstruction* input = invoke->InputAt(i);
1119 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1120 }
1121
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001122 switch (invoke->GetType()) {
1123 case Primitive::kPrimBoolean:
1124 case Primitive::kPrimByte:
1125 case Primitive::kPrimChar:
1126 case Primitive::kPrimShort:
1127 case Primitive::kPrimInt:
1128 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001129 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001130 break;
1131
1132 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001133 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001134 break;
1135
1136 case Primitive::kPrimVoid:
1137 break;
1138
1139 case Primitive::kPrimDouble:
1140 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001141 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001142 break;
1143 }
1144
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001145 invoke->SetLocations(locations);
1146}
1147
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001148void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001149 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001150 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1151 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1152 LocationSummary* locations = invoke->GetLocations();
1153 Location receiver = locations->InAt(0);
1154 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1155 // temp = object->GetClass();
1156 if (receiver.IsStackSlot()) {
1157 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1158 __ movl(temp, Address(temp, class_offset));
1159 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001160 __ movl(temp, Address(receiver.As<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001161 }
1162 // temp = temp->GetMethodAt(method_offset);
1163 __ movl(temp, Address(temp, method_offset));
1164 // call temp->GetEntryPoint();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001165 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1166
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001167 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001168 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001169}
1170
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001171void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1172 HandleInvoke(invoke);
1173 // Add the hidden argument.
1174 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM0));
1175}
1176
1177void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1178 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1179 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
1180 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1181 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1182 LocationSummary* locations = invoke->GetLocations();
1183 Location receiver = locations->InAt(0);
1184 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1185
1186 // Set the hidden argument.
1187 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
1188 __ movd(invoke->GetLocations()->GetTemp(1).As<XmmRegister>(), temp);
1189
1190 // temp = object->GetClass();
1191 if (receiver.IsStackSlot()) {
1192 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1193 __ movl(temp, Address(temp, class_offset));
1194 } else {
1195 __ movl(temp, Address(receiver.As<Register>(), class_offset));
1196 }
1197 // temp = temp->GetImtEntryAt(method_offset);
1198 __ movl(temp, Address(temp, method_offset));
1199 // call temp->GetEntryPoint();
1200 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1201
1202 DCHECK(!codegen_->IsLeafMethod());
1203 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1204}
1205
Roland Levillain88cb1752014-10-20 16:36:47 +01001206void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1207 LocationSummary* locations =
1208 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1209 switch (neg->GetResultType()) {
1210 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001211 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001212 locations->SetInAt(0, Location::RequiresRegister());
1213 locations->SetOut(Location::SameAsFirstInput());
1214 break;
1215
Roland Levillain88cb1752014-10-20 16:36:47 +01001216 case Primitive::kPrimFloat:
1217 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001218 locations->SetInAt(0, Location::RequiresFpuRegister());
1219 // Output overlaps as we need a fresh (zero-initialized)
1220 // register to perform subtraction from zero.
1221 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001222 break;
1223
1224 default:
1225 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1226 }
1227}
1228
1229void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1230 LocationSummary* locations = neg->GetLocations();
1231 Location out = locations->Out();
1232 Location in = locations->InAt(0);
1233 switch (neg->GetResultType()) {
1234 case Primitive::kPrimInt:
1235 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001236 DCHECK(in.Equals(out));
Roland Levillain88cb1752014-10-20 16:36:47 +01001237 __ negl(out.As<Register>());
1238 break;
1239
1240 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001241 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001242 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001243 __ negl(out.AsRegisterPairLow<Register>());
1244 // Negation is similar to subtraction from zero. The least
1245 // significant byte triggers a borrow when it is different from
1246 // zero; to take it into account, add 1 to the most significant
1247 // byte if the carry flag (CF) is set to 1 after the first NEGL
1248 // operation.
1249 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1250 __ negl(out.AsRegisterPairHigh<Register>());
1251 break;
1252
Roland Levillain88cb1752014-10-20 16:36:47 +01001253 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001254 DCHECK(!in.Equals(out));
1255 // out = 0
1256 __ xorps(out.As<XmmRegister>(), out.As<XmmRegister>());
1257 // out = out - in
1258 __ subss(out.As<XmmRegister>(), in.As<XmmRegister>());
1259 break;
1260
Roland Levillain88cb1752014-10-20 16:36:47 +01001261 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001262 DCHECK(!in.Equals(out));
1263 // out = 0
1264 __ xorpd(out.As<XmmRegister>(), out.As<XmmRegister>());
1265 // out = out - in
1266 __ subsd(out.As<XmmRegister>(), in.As<XmmRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001267 break;
1268
1269 default:
1270 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1271 }
1272}
1273
Roland Levillaindff1f282014-11-05 14:15:05 +00001274void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
1275 LocationSummary* locations =
1276 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1277 Primitive::Type result_type = conversion->GetResultType();
1278 Primitive::Type input_type = conversion->GetInputType();
1279 switch (result_type) {
Roland Levillain946e1432014-11-11 17:35:19 +00001280 case Primitive::kPrimInt:
1281 switch (input_type) {
1282 case Primitive::kPrimLong:
1283 // long-to-int conversion.
1284 locations->SetInAt(0, Location::Any());
1285 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1286 break;
1287
1288 case Primitive::kPrimFloat:
1289 case Primitive::kPrimDouble:
1290 LOG(FATAL) << "Type conversion from " << input_type
1291 << " to " << result_type << " not yet implemented";
1292 break;
1293
1294 default:
1295 LOG(FATAL) << "Unexpected type conversion from " << input_type
1296 << " to " << result_type;
1297 }
1298 break;
1299
Roland Levillaindff1f282014-11-05 14:15:05 +00001300 case Primitive::kPrimLong:
1301 switch (input_type) {
1302 case Primitive::kPrimByte:
1303 case Primitive::kPrimShort:
1304 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001305 case Primitive::kPrimChar:
Roland Levillaindff1f282014-11-05 14:15:05 +00001306 // int-to-long conversion.
1307 locations->SetInAt(0, Location::RegisterLocation(EAX));
1308 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1309 break;
1310
1311 case Primitive::kPrimFloat:
1312 case Primitive::kPrimDouble:
1313 LOG(FATAL) << "Type conversion from " << input_type << " to "
1314 << result_type << " not yet implemented";
1315 break;
1316
1317 default:
1318 LOG(FATAL) << "Unexpected type conversion from " << input_type
1319 << " to " << result_type;
1320 }
1321 break;
1322
Roland Levillaindff1f282014-11-05 14:15:05 +00001323 case Primitive::kPrimFloat:
1324 case Primitive::kPrimDouble:
1325 LOG(FATAL) << "Type conversion from " << input_type
1326 << " to " << result_type << " not yet implemented";
1327 break;
1328
1329 default:
1330 LOG(FATAL) << "Unexpected type conversion from " << input_type
1331 << " to " << result_type;
1332 }
1333}
1334
1335void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1336 LocationSummary* locations = conversion->GetLocations();
1337 Location out = locations->Out();
1338 Location in = locations->InAt(0);
1339 Primitive::Type result_type = conversion->GetResultType();
1340 Primitive::Type input_type = conversion->GetInputType();
1341 switch (result_type) {
Roland Levillain946e1432014-11-11 17:35:19 +00001342 case Primitive::kPrimInt:
1343 switch (input_type) {
1344 case Primitive::kPrimLong:
1345 // long-to-int conversion.
1346 if (in.IsRegisterPair()) {
1347 __ movl(out.As<Register>(), in.AsRegisterPairLow<Register>());
1348 } else if (in.IsDoubleStackSlot()) {
1349 __ movl(out.As<Register>(), Address(ESP, in.GetStackIndex()));
1350 } else {
1351 DCHECK(in.IsConstant());
1352 DCHECK(in.GetConstant()->IsLongConstant());
1353 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
1354 __ movl(out.As<Register>(), Immediate(static_cast<int32_t>(value)));
1355 }
1356 break;
1357
1358 case Primitive::kPrimFloat:
1359 case Primitive::kPrimDouble:
1360 LOG(FATAL) << "Type conversion from " << input_type
1361 << " to " << result_type << " not yet implemented";
1362 break;
1363
1364 default:
1365 LOG(FATAL) << "Unexpected type conversion from " << input_type
1366 << " to " << result_type;
1367 }
1368 break;
1369
Roland Levillaindff1f282014-11-05 14:15:05 +00001370 case Primitive::kPrimLong:
1371 switch (input_type) {
1372 case Primitive::kPrimByte:
1373 case Primitive::kPrimShort:
1374 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001375 case Primitive::kPrimChar:
Roland Levillaindff1f282014-11-05 14:15:05 +00001376 // int-to-long conversion.
1377 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1378 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
1379 DCHECK_EQ(in.As<Register>(), EAX);
1380 __ cdq();
1381 break;
1382
1383 case Primitive::kPrimFloat:
1384 case Primitive::kPrimDouble:
1385 LOG(FATAL) << "Type conversion from " << input_type << " to "
1386 << result_type << " not yet implemented";
1387 break;
1388
1389 default:
1390 LOG(FATAL) << "Unexpected type conversion from " << input_type
1391 << " to " << result_type;
1392 }
1393 break;
1394
Roland Levillaindff1f282014-11-05 14:15:05 +00001395 case Primitive::kPrimFloat:
1396 case Primitive::kPrimDouble:
1397 LOG(FATAL) << "Type conversion from " << input_type
1398 << " to " << result_type << " not yet implemented";
1399 break;
1400
1401 default:
1402 LOG(FATAL) << "Unexpected type conversion from " << input_type
1403 << " to " << result_type;
1404 }
1405}
1406
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001407void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001408 LocationSummary* locations =
1409 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001410 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001411 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001412 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001413 locations->SetInAt(0, Location::RequiresRegister());
1414 locations->SetInAt(1, Location::Any());
1415 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001416 break;
1417 }
1418
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001419 case Primitive::kPrimFloat:
1420 case Primitive::kPrimDouble: {
1421 locations->SetInAt(0, Location::RequiresFpuRegister());
1422 locations->SetInAt(1, Location::Any());
1423 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001424 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001425 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001426
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001427 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001428 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1429 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001430 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001431}
1432
1433void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1434 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001435 Location first = locations->InAt(0);
1436 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001437 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001438 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001439 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001440 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001441 __ addl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001442 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001443 __ addl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001444 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001445 __ addl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001446 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001447 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001448 }
1449
1450 case Primitive::kPrimLong: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001451 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001452 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1453 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001454 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001455 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1456 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001457 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001458 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001459 break;
1460 }
1461
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001462 case Primitive::kPrimFloat: {
1463 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001464 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001465 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001466 __ addss(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001467 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001468 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001469 }
1470
1471 case Primitive::kPrimDouble: {
1472 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001473 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001474 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001475 __ addsd(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001476 }
1477 break;
1478 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001479
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001480 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001481 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001482 }
1483}
1484
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001485void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001486 LocationSummary* locations =
1487 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001488 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001489 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001490 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001491 locations->SetInAt(0, Location::RequiresRegister());
1492 locations->SetInAt(1, Location::Any());
1493 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001494 break;
1495 }
Calin Juravle11351682014-10-23 15:38:15 +01001496 case Primitive::kPrimFloat:
1497 case Primitive::kPrimDouble: {
1498 locations->SetInAt(0, Location::RequiresFpuRegister());
1499 locations->SetInAt(1, Location::RequiresFpuRegister());
1500 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001501 break;
Calin Juravle11351682014-10-23 15:38:15 +01001502 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001503
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001504 default:
Calin Juravle11351682014-10-23 15:38:15 +01001505 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001506 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001507}
1508
1509void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1510 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001511 Location first = locations->InAt(0);
1512 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001513 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001514 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001515 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001516 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001517 __ subl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001518 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001519 __ subl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001520 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001521 __ subl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001522 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001523 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001524 }
1525
1526 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001527 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001528 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1529 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001530 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001531 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001532 __ sbbl(first.AsRegisterPairHigh<Register>(),
1533 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001534 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001535 break;
1536 }
1537
Calin Juravle11351682014-10-23 15:38:15 +01001538 case Primitive::kPrimFloat: {
1539 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001540 break;
Calin Juravle11351682014-10-23 15:38:15 +01001541 }
1542
1543 case Primitive::kPrimDouble: {
1544 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1545 break;
1546 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001547
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001548 default:
Calin Juravle11351682014-10-23 15:38:15 +01001549 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001550 }
1551}
1552
Calin Juravle34bacdf2014-10-07 20:23:36 +01001553void LocationsBuilderX86::VisitMul(HMul* mul) {
1554 LocationSummary* locations =
1555 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1556 switch (mul->GetResultType()) {
1557 case Primitive::kPrimInt:
1558 locations->SetInAt(0, Location::RequiresRegister());
1559 locations->SetInAt(1, Location::Any());
1560 locations->SetOut(Location::SameAsFirstInput());
1561 break;
1562 case Primitive::kPrimLong: {
1563 locations->SetInAt(0, Location::RequiresRegister());
1564 // TODO: Currently this handles only stack operands:
1565 // - we don't have enough registers because we currently use Quick ABI.
1566 // - by the time we have a working register allocator we will probably change the ABI
1567 // and fix the above.
1568 // - we don't have a way yet to request operands on stack but the base line compiler
1569 // will leave the operands on the stack with Any().
1570 locations->SetInAt(1, Location::Any());
1571 locations->SetOut(Location::SameAsFirstInput());
1572 // Needed for imul on 32bits with 64bits output.
1573 locations->AddTemp(Location::RegisterLocation(EAX));
1574 locations->AddTemp(Location::RegisterLocation(EDX));
1575 break;
1576 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001577 case Primitive::kPrimFloat:
1578 case Primitive::kPrimDouble: {
1579 locations->SetInAt(0, Location::RequiresFpuRegister());
1580 locations->SetInAt(1, Location::RequiresFpuRegister());
1581 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001582 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001583 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001584
1585 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001586 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001587 }
1588}
1589
1590void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1591 LocationSummary* locations = mul->GetLocations();
1592 Location first = locations->InAt(0);
1593 Location second = locations->InAt(1);
1594 DCHECK(first.Equals(locations->Out()));
1595
1596 switch (mul->GetResultType()) {
1597 case Primitive::kPrimInt: {
1598 if (second.IsRegister()) {
1599 __ imull(first.As<Register>(), second.As<Register>());
1600 } else if (second.IsConstant()) {
1601 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1602 __ imull(first.As<Register>(), imm);
1603 } else {
1604 DCHECK(second.IsStackSlot());
1605 __ imull(first.As<Register>(), Address(ESP, second.GetStackIndex()));
1606 }
1607 break;
1608 }
1609
1610 case Primitive::kPrimLong: {
1611 DCHECK(second.IsDoubleStackSlot());
1612
1613 Register in1_hi = first.AsRegisterPairHigh<Register>();
1614 Register in1_lo = first.AsRegisterPairLow<Register>();
1615 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
1616 Address in2_lo(ESP, second.GetStackIndex());
1617 Register eax = locations->GetTemp(0).As<Register>();
1618 Register edx = locations->GetTemp(1).As<Register>();
1619
1620 DCHECK_EQ(EAX, eax);
1621 DCHECK_EQ(EDX, edx);
1622
1623 // input: in1 - 64 bits, in2 - 64 bits
1624 // output: in1
1625 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
1626 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
1627 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
1628
1629 __ movl(eax, in2_hi);
1630 // eax <- in1.lo * in2.hi
1631 __ imull(eax, in1_lo);
1632 // in1.hi <- in1.hi * in2.lo
1633 __ imull(in1_hi, in2_lo);
1634 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
1635 __ addl(in1_hi, eax);
1636 // move in1_lo to eax to prepare for double precision
1637 __ movl(eax, in1_lo);
1638 // edx:eax <- in1.lo * in2.lo
1639 __ mull(in2_lo);
1640 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
1641 __ addl(in1_hi, edx);
1642 // in1.lo <- (in1.lo * in2.lo)[31:0];
1643 __ movl(in1_lo, eax);
1644
1645 break;
1646 }
1647
Calin Juravleb5bfa962014-10-21 18:02:24 +01001648 case Primitive::kPrimFloat: {
1649 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001650 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001651 }
1652
1653 case Primitive::kPrimDouble: {
1654 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1655 break;
1656 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001657
1658 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001659 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001660 }
1661}
1662
Calin Juravle7c4954d2014-10-28 16:57:40 +00001663void LocationsBuilderX86::VisitDiv(HDiv* div) {
1664 LocationSummary* locations =
1665 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1666 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001667 case Primitive::kPrimInt: {
1668 locations->SetInAt(0, Location::RegisterLocation(EAX));
1669 locations->SetInAt(1, Location::RequiresRegister());
1670 locations->SetOut(Location::SameAsFirstInput());
1671 // Intel uses edx:eax as the dividend.
1672 locations->AddTemp(Location::RegisterLocation(EDX));
1673 break;
1674 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001675 case Primitive::kPrimLong: {
1676 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1677 break;
1678 }
1679 case Primitive::kPrimFloat:
1680 case Primitive::kPrimDouble: {
1681 locations->SetInAt(0, Location::RequiresFpuRegister());
1682 locations->SetInAt(1, Location::RequiresFpuRegister());
1683 locations->SetOut(Location::SameAsFirstInput());
1684 break;
1685 }
1686
1687 default:
1688 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1689 }
1690}
1691
1692void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
1693 LocationSummary* locations = div->GetLocations();
1694 Location first = locations->InAt(0);
1695 Location second = locations->InAt(1);
1696 DCHECK(first.Equals(locations->Out()));
1697
1698 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001699 case Primitive::kPrimInt: {
1700 Register first_reg = first.As<Register>();
1701 Register second_reg = second.As<Register>();
1702 DCHECK_EQ(EAX, first_reg);
1703 DCHECK_EQ(EDX, locations->GetTemp(0).As<Register>());
1704
1705 SlowPathCodeX86* slow_path =
1706 new (GetGraph()->GetArena()) DivMinusOneSlowPathX86(first_reg);
1707 codegen_->AddSlowPath(slow_path);
1708
1709 // 0x80000000/-1 triggers an arithmetic exception!
1710 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
1711 // it's safe to just use negl instead of more complex comparisons.
1712
1713 __ cmpl(second_reg, Immediate(-1));
1714 __ j(kEqual, slow_path->GetEntryLabel());
1715
1716 // edx:eax <- sign-extended of eax
1717 __ cdq();
1718 // eax = quotient, edx = remainder
1719 __ idivl(second_reg);
1720
1721 __ Bind(slow_path->GetExitLabel());
1722 break;
1723 }
1724
Calin Juravle7c4954d2014-10-28 16:57:40 +00001725 case Primitive::kPrimLong: {
1726 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1727 break;
1728 }
1729
1730 case Primitive::kPrimFloat: {
1731 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1732 break;
1733 }
1734
1735 case Primitive::kPrimDouble: {
1736 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1737 break;
1738 }
1739
1740 default:
1741 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1742 }
1743}
1744
Calin Juravled0d48522014-11-04 16:40:20 +00001745void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1746 LocationSummary* locations =
1747 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1748 locations->SetInAt(0, Location::Any());
1749 if (instruction->HasUses()) {
1750 locations->SetOut(Location::SameAsFirstInput());
1751 }
1752}
1753
1754void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1755 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
1756 codegen_->AddSlowPath(slow_path);
1757
1758 LocationSummary* locations = instruction->GetLocations();
1759 Location value = locations->InAt(0);
1760
1761 if (value.IsRegister()) {
1762 __ testl(value.As<Register>(), value.As<Register>());
1763 } else if (value.IsStackSlot()) {
1764 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
1765 } else {
1766 DCHECK(value.IsConstant()) << value;
1767 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
1768 __ jmp(slow_path->GetEntryLabel());
1769 }
1770 return;
1771 }
1772 __ j(kEqual, slow_path->GetEntryLabel());
1773}
1774
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001775void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001776 LocationSummary* locations =
1777 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001778 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001779 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001780 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1781 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001782}
1783
1784void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
1785 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001786 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001787 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001788
Nicolas Geoffray707c8092014-04-04 10:50:14 +01001789 __ fs()->call(
1790 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001791
Nicolas Geoffray39468442014-09-02 15:17:15 +01001792 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001793 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001794}
1795
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001796void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
1797 LocationSummary* locations =
1798 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1799 locations->SetOut(Location::RegisterLocation(EAX));
1800 InvokeRuntimeCallingConvention calling_convention;
1801 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1802 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1803 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1804}
1805
1806void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
1807 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001808 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001809 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
1810
1811 __ fs()->call(
1812 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
1813
1814 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1815 DCHECK(!codegen_->IsLeafMethod());
1816}
1817
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001818void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001819 LocationSummary* locations =
1820 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001821 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1822 if (location.IsStackSlot()) {
1823 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1824 } else if (location.IsDoubleStackSlot()) {
1825 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001826 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001827 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001828}
1829
1830void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001831 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001832}
1833
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001834void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001835 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001836 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001837 locations->SetInAt(0, Location::RequiresRegister());
1838 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001839}
1840
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001841void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
1842 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01001843 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001844 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01001845 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001846 switch (not_->InputAt(0)->GetType()) {
1847 case Primitive::kPrimBoolean:
1848 __ xorl(out.As<Register>(), Immediate(1));
1849 break;
1850
1851 case Primitive::kPrimInt:
1852 __ notl(out.As<Register>());
1853 break;
1854
1855 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001856 __ notl(out.AsRegisterPairLow<Register>());
1857 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001858 break;
1859
1860 default:
1861 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1862 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001863}
1864
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001865void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001866 LocationSummary* locations =
1867 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001868 locations->SetInAt(0, Location::RequiresRegister());
1869 locations->SetInAt(1, Location::Any());
1870 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001871}
1872
1873void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001874 LocationSummary* locations = compare->GetLocations();
1875 switch (compare->InputAt(0)->GetType()) {
1876 case Primitive::kPrimLong: {
1877 Label less, greater, done;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001878 Register output = locations->Out().As<Register>();
1879 Location left = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001880 Location right = locations->InAt(1);
1881 if (right.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001882 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001883 } else {
1884 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001885 __ cmpl(left.AsRegisterPairHigh<Register>(),
1886 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001887 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001888 __ j(kLess, &less); // Signed compare.
1889 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001890 if (right.IsRegisterPair()) {
1891 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001892 } else {
1893 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001894 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001895 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001896 __ movl(output, Immediate(0));
1897 __ j(kEqual, &done);
1898 __ j(kBelow, &less); // Unsigned compare.
1899
1900 __ Bind(&greater);
1901 __ movl(output, Immediate(1));
1902 __ jmp(&done);
1903
1904 __ Bind(&less);
1905 __ movl(output, Immediate(-1));
1906
1907 __ Bind(&done);
1908 break;
1909 }
1910 default:
1911 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
1912 }
1913}
1914
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001915void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001916 LocationSummary* locations =
1917 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001918 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1919 locations->SetInAt(i, Location::Any());
1920 }
1921 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001922}
1923
1924void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001925 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001926 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001927}
1928
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001929void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001930 LocationSummary* locations =
1931 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001932 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001933 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001934 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001935 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
1936 || (field_type == Primitive::kPrimByte);
1937 // The register allocator does not support multiple
1938 // inputs that die at entry with one in a specific register.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001939 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001940 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001941 locations->SetInAt(1, Location::RegisterLocation(EAX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001942 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001943 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001944 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001945 // Temporary registers for the write barrier.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001946 if (is_object_type) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001947 locations->AddTemp(Location::RequiresRegister());
1948 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001949 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001950 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001951}
1952
1953void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1954 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001955 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001956 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001957 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001958
1959 switch (field_type) {
1960 case Primitive::kPrimBoolean:
1961 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001962 ByteRegister value = locations->InAt(1).As<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001963 __ movb(Address(obj, offset), value);
1964 break;
1965 }
1966
1967 case Primitive::kPrimShort:
1968 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001969 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001970 __ movw(Address(obj, offset), value);
1971 break;
1972 }
1973
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001974 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001975 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001976 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001977 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001978
1979 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001980 Register temp = locations->GetTemp(0).As<Register>();
1981 Register card = locations->GetTemp(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001982 codegen_->MarkGCCard(temp, card, obj, value);
1983 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001984 break;
1985 }
1986
1987 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001988 Location value = locations->InAt(1);
1989 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1990 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001991 break;
1992 }
1993
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001994 case Primitive::kPrimFloat: {
1995 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1996 __ movss(Address(obj, offset), value);
1997 break;
1998 }
1999
2000 case Primitive::kPrimDouble: {
2001 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2002 __ movsd(Address(obj, offset), value);
2003 break;
2004 }
2005
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002006 case Primitive::kPrimVoid:
2007 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07002008 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002009 }
2010}
2011
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002012void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
2013 Label is_null;
2014 __ testl(value, value);
2015 __ j(kEqual, &is_null);
2016 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2017 __ movl(temp, object);
2018 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
2019 __ movb(Address(temp, card, TIMES_1, 0),
2020 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
2021 __ Bind(&is_null);
2022}
2023
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002024void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002025 LocationSummary* locations =
2026 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002027 locations->SetInAt(0, Location::RequiresRegister());
2028 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002029}
2030
2031void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2032 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002033 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002034 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2035
2036 switch (instruction->GetType()) {
2037 case Primitive::kPrimBoolean: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002038 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002039 __ movzxb(out, Address(obj, offset));
2040 break;
2041 }
2042
2043 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002044 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002045 __ movsxb(out, Address(obj, offset));
2046 break;
2047 }
2048
2049 case Primitive::kPrimShort: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002050 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002051 __ movsxw(out, Address(obj, offset));
2052 break;
2053 }
2054
2055 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002056 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002057 __ movzxw(out, Address(obj, offset));
2058 break;
2059 }
2060
2061 case Primitive::kPrimInt:
2062 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002063 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002064 __ movl(out, Address(obj, offset));
2065 break;
2066 }
2067
2068 case Primitive::kPrimLong: {
2069 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002070 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
2071 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002072 break;
2073 }
2074
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002075 case Primitive::kPrimFloat: {
2076 XmmRegister out = locations->Out().As<XmmRegister>();
2077 __ movss(out, Address(obj, offset));
2078 break;
2079 }
2080
2081 case Primitive::kPrimDouble: {
2082 XmmRegister out = locations->Out().As<XmmRegister>();
2083 __ movsd(out, Address(obj, offset));
2084 break;
2085 }
2086
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002087 case Primitive::kPrimVoid:
2088 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002089 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002090 }
2091}
2092
2093void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002094 LocationSummary* locations =
2095 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002096 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002097 if (instruction->HasUses()) {
2098 locations->SetOut(Location::SameAsFirstInput());
2099 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002100}
2101
2102void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002103 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002104 codegen_->AddSlowPath(slow_path);
2105
2106 LocationSummary* locations = instruction->GetLocations();
2107 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002108
2109 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002110 __ cmpl(obj.As<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002111 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002112 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002113 } else {
2114 DCHECK(obj.IsConstant()) << obj;
2115 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2116 __ jmp(slow_path->GetEntryLabel());
2117 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002118 }
2119 __ j(kEqual, slow_path->GetEntryLabel());
2120}
2121
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002122void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002123 LocationSummary* locations =
2124 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002125 locations->SetInAt(0, Location::RequiresRegister());
2126 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2127 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002128}
2129
2130void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
2131 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002132 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002133 Location index = locations->InAt(1);
2134
2135 switch (instruction->GetType()) {
2136 case Primitive::kPrimBoolean: {
2137 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002138 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002139 if (index.IsConstant()) {
2140 __ movzxb(out, Address(obj,
2141 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2142 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002143 __ movzxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002144 }
2145 break;
2146 }
2147
2148 case Primitive::kPrimByte: {
2149 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002150 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002151 if (index.IsConstant()) {
2152 __ movsxb(out, Address(obj,
2153 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2154 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002155 __ movsxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002156 }
2157 break;
2158 }
2159
2160 case Primitive::kPrimShort: {
2161 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002162 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002163 if (index.IsConstant()) {
2164 __ movsxw(out, Address(obj,
2165 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2166 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002167 __ movsxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002168 }
2169 break;
2170 }
2171
2172 case Primitive::kPrimChar: {
2173 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002174 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002175 if (index.IsConstant()) {
2176 __ movzxw(out, Address(obj,
2177 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2178 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002179 __ movzxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002180 }
2181 break;
2182 }
2183
2184 case Primitive::kPrimInt:
2185 case Primitive::kPrimNot: {
2186 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002187 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002188 if (index.IsConstant()) {
2189 __ movl(out, Address(obj,
2190 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2191 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002192 __ movl(out, Address(obj, index.As<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002193 }
2194 break;
2195 }
2196
2197 case Primitive::kPrimLong: {
2198 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002199 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002200 if (index.IsConstant()) {
2201 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002202 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
2203 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002204 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002205 __ movl(out.AsRegisterPairLow<Register>(),
2206 Address(obj, index.As<Register>(), TIMES_8, data_offset));
2207 __ movl(out.AsRegisterPairHigh<Register>(),
2208 Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002209 }
2210 break;
2211 }
2212
2213 case Primitive::kPrimFloat:
2214 case Primitive::kPrimDouble:
2215 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002216 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002217 case Primitive::kPrimVoid:
2218 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002219 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002220 }
2221}
2222
2223void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002224 Primitive::Type value_type = instruction->GetComponentType();
2225 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2226 instruction,
2227 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
2228
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002229 if (value_type == Primitive::kPrimNot) {
2230 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002231 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2232 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2233 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002234 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002235 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
2236 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002237 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002238 // In case of a byte operation, the register allocator does not support multiple
2239 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002240 locations->SetInAt(0, Location::RequiresRegister());
2241 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002242 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002243 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002244 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002245 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002246 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002247 }
2248 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002249}
2250
2251void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
2252 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002253 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002254 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002255 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002256 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002257
2258 switch (value_type) {
2259 case Primitive::kPrimBoolean:
2260 case Primitive::kPrimByte: {
2261 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002262 if (index.IsConstant()) {
2263 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002264 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002265 __ movb(Address(obj, offset), value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002266 } else {
2267 __ movb(Address(obj, offset),
2268 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2269 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002270 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002271 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002272 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
2273 value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002274 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002275 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002276 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2277 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002278 }
2279 break;
2280 }
2281
2282 case Primitive::kPrimShort:
2283 case Primitive::kPrimChar: {
2284 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002285 if (index.IsConstant()) {
2286 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002287 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002288 __ movw(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002289 } else {
2290 __ movw(Address(obj, offset),
2291 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2292 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002293 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002294 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002295 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
2296 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002297 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002298 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002299 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2300 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002301 }
2302 break;
2303 }
2304
2305 case Primitive::kPrimInt: {
2306 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002307 if (index.IsConstant()) {
2308 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002309 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002310 __ movl(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002311 } else {
2312 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2313 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002314 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002315 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002316 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
2317 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002318 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002319 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002320 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2321 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002322 }
2323 break;
2324 }
2325
2326 case Primitive::kPrimNot: {
2327 DCHECK(!codegen_->IsLeafMethod());
2328 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01002329 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002330 break;
2331 }
2332
2333 case Primitive::kPrimLong: {
2334 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002335 if (index.IsConstant()) {
2336 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002337 if (value.IsRegisterPair()) {
2338 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
2339 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002340 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002341 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002342 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
2343 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
2344 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
2345 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002346 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002347 if (value.IsRegisterPair()) {
2348 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
2349 value.AsRegisterPairLow<Register>());
2350 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
2351 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002352 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002353 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002354 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002355 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002356 Immediate(Low32Bits(val)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002357 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002358 Immediate(High32Bits(val)));
2359 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002360 }
2361 break;
2362 }
2363
2364 case Primitive::kPrimFloat:
2365 case Primitive::kPrimDouble:
2366 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002367 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002368 case Primitive::kPrimVoid:
2369 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002370 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002371 }
2372}
2373
2374void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
2375 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002376 locations->SetInAt(0, Location::RequiresRegister());
2377 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002378 instruction->SetLocations(locations);
2379}
2380
2381void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
2382 LocationSummary* locations = instruction->GetLocations();
2383 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002384 Register obj = locations->InAt(0).As<Register>();
2385 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002386 __ movl(out, Address(obj, offset));
2387}
2388
2389void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002390 LocationSummary* locations =
2391 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002392 locations->SetInAt(0, Location::RequiresRegister());
2393 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002394 if (instruction->HasUses()) {
2395 locations->SetOut(Location::SameAsFirstInput());
2396 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002397}
2398
2399void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
2400 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002401 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002402 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002403 codegen_->AddSlowPath(slow_path);
2404
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002405 Register index = locations->InAt(0).As<Register>();
2406 Register length = locations->InAt(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002407
2408 __ cmpl(index, length);
2409 __ j(kAboveEqual, slow_path->GetEntryLabel());
2410}
2411
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002412void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
2413 temp->SetLocations(nullptr);
2414}
2415
2416void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
2417 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002418 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002419}
2420
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002421void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002422 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002423 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002424}
2425
2426void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002427 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2428}
2429
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002430void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
2431 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2432}
2433
2434void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002435 HBasicBlock* block = instruction->GetBlock();
2436 if (block->GetLoopInformation() != nullptr) {
2437 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2438 // The back edge will generate the suspend check.
2439 return;
2440 }
2441 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2442 // The goto will generate the suspend check.
2443 return;
2444 }
2445 GenerateSuspendCheck(instruction, nullptr);
2446}
2447
2448void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
2449 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002450 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002451 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002452 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002453 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002454 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002455 if (successor == nullptr) {
2456 __ j(kNotEqual, slow_path->GetEntryLabel());
2457 __ Bind(slow_path->GetReturnLabel());
2458 } else {
2459 __ j(kEqual, codegen_->GetLabelOf(successor));
2460 __ jmp(slow_path->GetEntryLabel());
2461 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002462}
2463
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002464X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
2465 return codegen_->GetAssembler();
2466}
2467
2468void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
2469 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002470 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002471 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2472 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
2473 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
2474}
2475
2476void ParallelMoveResolverX86::EmitMove(size_t index) {
2477 MoveOperands* move = moves_.Get(index);
2478 Location source = move->GetSource();
2479 Location destination = move->GetDestination();
2480
2481 if (source.IsRegister()) {
2482 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002483 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002484 } else {
2485 DCHECK(destination.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002486 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002487 }
2488 } else if (source.IsStackSlot()) {
2489 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002490 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002491 } else {
2492 DCHECK(destination.IsStackSlot());
2493 MoveMemoryToMemory(destination.GetStackIndex(),
2494 source.GetStackIndex());
2495 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002496 } else if (source.IsConstant()) {
2497 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
2498 Immediate imm(instruction->AsIntConstant()->GetValue());
2499 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002500 __ movl(destination.As<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002501 } else {
2502 __ movl(Address(ESP, destination.GetStackIndex()), imm);
2503 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002504 } else {
2505 LOG(FATAL) << "Unimplemented";
2506 }
2507}
2508
2509void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002510 Register suggested_scratch = reg == EAX ? EBX : EAX;
2511 ScratchRegisterScope ensure_scratch(
2512 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2513
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002514 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2515 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
2516 __ movl(Address(ESP, mem + stack_offset), reg);
2517 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
2518}
2519
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002520void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
2521 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002522 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
2523
2524 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002525 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002526 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2527
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002528 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
2529 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
2530 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
2531 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
2532 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
2533 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
2534}
2535
2536void ParallelMoveResolverX86::EmitSwap(size_t index) {
2537 MoveOperands* move = moves_.Get(index);
2538 Location source = move->GetSource();
2539 Location destination = move->GetDestination();
2540
2541 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002542 __ xchgl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002543 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002544 Exchange(source.As<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002545 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002546 Exchange(destination.As<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002547 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
2548 Exchange(destination.GetStackIndex(), source.GetStackIndex());
2549 } else {
2550 LOG(FATAL) << "Unimplemented";
2551 }
2552}
2553
2554void ParallelMoveResolverX86::SpillScratch(int reg) {
2555 __ pushl(static_cast<Register>(reg));
2556}
2557
2558void ParallelMoveResolverX86::RestoreScratch(int reg) {
2559 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002560}
2561
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002562void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002563 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2564 ? LocationSummary::kCallOnSlowPath
2565 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002566 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002567 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002568 locations->SetOut(Location::RequiresRegister());
2569}
2570
2571void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
2572 Register out = cls->GetLocations()->Out().As<Register>();
2573 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002574 DCHECK(!cls->CanCallRuntime());
2575 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002576 codegen_->LoadCurrentMethod(out);
2577 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2578 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002579 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002580 codegen_->LoadCurrentMethod(out);
2581 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2582 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002583
2584 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
2585 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2586 codegen_->AddSlowPath(slow_path);
2587 __ testl(out, out);
2588 __ j(kEqual, slow_path->GetEntryLabel());
2589 if (cls->MustGenerateClinitCheck()) {
2590 GenerateClassInitializationCheck(slow_path, out);
2591 } else {
2592 __ Bind(slow_path->GetExitLabel());
2593 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002594 }
2595}
2596
2597void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
2598 LocationSummary* locations =
2599 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2600 locations->SetInAt(0, Location::RequiresRegister());
2601 if (check->HasUses()) {
2602 locations->SetOut(Location::SameAsFirstInput());
2603 }
2604}
2605
2606void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002607 // We assume the class to not be null.
2608 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
2609 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002610 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002611 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<Register>());
2612}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002613
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002614void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
2615 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002616 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2617 Immediate(mirror::Class::kStatusInitialized));
2618 __ j(kLess, slow_path->GetEntryLabel());
2619 __ Bind(slow_path->GetExitLabel());
2620 // No need for memory fence, thanks to the X86 memory model.
2621}
2622
2623void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2624 LocationSummary* locations =
2625 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2626 locations->SetInAt(0, Location::RequiresRegister());
2627 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2628}
2629
2630void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2631 LocationSummary* locations = instruction->GetLocations();
2632 Register cls = locations->InAt(0).As<Register>();
2633 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2634
2635 switch (instruction->GetType()) {
2636 case Primitive::kPrimBoolean: {
2637 Register out = locations->Out().As<Register>();
2638 __ movzxb(out, Address(cls, offset));
2639 break;
2640 }
2641
2642 case Primitive::kPrimByte: {
2643 Register out = locations->Out().As<Register>();
2644 __ movsxb(out, Address(cls, offset));
2645 break;
2646 }
2647
2648 case Primitive::kPrimShort: {
2649 Register out = locations->Out().As<Register>();
2650 __ movsxw(out, Address(cls, offset));
2651 break;
2652 }
2653
2654 case Primitive::kPrimChar: {
2655 Register out = locations->Out().As<Register>();
2656 __ movzxw(out, Address(cls, offset));
2657 break;
2658 }
2659
2660 case Primitive::kPrimInt:
2661 case Primitive::kPrimNot: {
2662 Register out = locations->Out().As<Register>();
2663 __ movl(out, Address(cls, offset));
2664 break;
2665 }
2666
2667 case Primitive::kPrimLong: {
2668 // TODO: support volatile.
2669 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(cls, offset));
2670 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(cls, kX86WordSize + offset));
2671 break;
2672 }
2673
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002674 case Primitive::kPrimFloat: {
2675 XmmRegister out = locations->Out().As<XmmRegister>();
2676 __ movss(out, Address(cls, offset));
2677 break;
2678 }
2679
2680 case Primitive::kPrimDouble: {
2681 XmmRegister out = locations->Out().As<XmmRegister>();
2682 __ movsd(out, Address(cls, offset));
2683 break;
2684 }
2685
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002686 case Primitive::kPrimVoid:
2687 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2688 UNREACHABLE();
2689 }
2690}
2691
2692void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2693 LocationSummary* locations =
2694 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2695 locations->SetInAt(0, Location::RequiresRegister());
2696 Primitive::Type field_type = instruction->GetFieldType();
2697 bool is_object_type = field_type == Primitive::kPrimNot;
2698 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2699 || (field_type == Primitive::kPrimByte);
2700 // The register allocator does not support multiple
2701 // inputs that die at entry with one in a specific register.
2702 if (is_byte_type) {
2703 // Ensure the value is in a byte register.
2704 locations->SetInAt(1, Location::RegisterLocation(EAX));
2705 } else {
2706 locations->SetInAt(1, Location::RequiresRegister());
2707 }
2708 // Temporary registers for the write barrier.
2709 if (is_object_type) {
2710 locations->AddTemp(Location::RequiresRegister());
2711 // Ensure the card is in a byte register.
2712 locations->AddTemp(Location::RegisterLocation(ECX));
2713 }
2714}
2715
2716void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2717 LocationSummary* locations = instruction->GetLocations();
2718 Register cls = locations->InAt(0).As<Register>();
2719 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2720 Primitive::Type field_type = instruction->GetFieldType();
2721
2722 switch (field_type) {
2723 case Primitive::kPrimBoolean:
2724 case Primitive::kPrimByte: {
2725 ByteRegister value = locations->InAt(1).As<ByteRegister>();
2726 __ movb(Address(cls, offset), value);
2727 break;
2728 }
2729
2730 case Primitive::kPrimShort:
2731 case Primitive::kPrimChar: {
2732 Register value = locations->InAt(1).As<Register>();
2733 __ movw(Address(cls, offset), value);
2734 break;
2735 }
2736
2737 case Primitive::kPrimInt:
2738 case Primitive::kPrimNot: {
2739 Register value = locations->InAt(1).As<Register>();
2740 __ movl(Address(cls, offset), value);
2741
2742 if (field_type == Primitive::kPrimNot) {
2743 Register temp = locations->GetTemp(0).As<Register>();
2744 Register card = locations->GetTemp(1).As<Register>();
2745 codegen_->MarkGCCard(temp, card, cls, value);
2746 }
2747 break;
2748 }
2749
2750 case Primitive::kPrimLong: {
2751 Location value = locations->InAt(1);
2752 __ movl(Address(cls, offset), value.AsRegisterPairLow<Register>());
2753 __ movl(Address(cls, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
2754 break;
2755 }
2756
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002757 case Primitive::kPrimFloat: {
2758 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2759 __ movss(Address(cls, offset), value);
2760 break;
2761 }
2762
2763 case Primitive::kPrimDouble: {
2764 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2765 __ movsd(Address(cls, offset), value);
2766 break;
2767 }
2768
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002769 case Primitive::kPrimVoid:
2770 LOG(FATAL) << "Unreachable type " << field_type;
2771 UNREACHABLE();
2772 }
2773}
2774
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002775void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
2776 LocationSummary* locations =
2777 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2778 locations->SetOut(Location::RequiresRegister());
2779}
2780
2781void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
2782 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
2783 codegen_->AddSlowPath(slow_path);
2784
2785 Register out = load->GetLocations()->Out().As<Register>();
2786 codegen_->LoadCurrentMethod(out);
2787 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
2788 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2789 __ testl(out, out);
2790 __ j(kEqual, slow_path->GetEntryLabel());
2791 __ Bind(slow_path->GetExitLabel());
2792}
2793
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002794void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
2795 LocationSummary* locations =
2796 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2797 locations->SetOut(Location::RequiresRegister());
2798}
2799
2800void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
2801 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
2802 __ fs()->movl(load->GetLocations()->Out().As<Register>(), address);
2803 __ fs()->movl(address, Immediate(0));
2804}
2805
2806void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
2807 LocationSummary* locations =
2808 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2809 InvokeRuntimeCallingConvention calling_convention;
2810 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2811}
2812
2813void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
2814 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
2815 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2816}
2817
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002818void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002819 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
2820 ? LocationSummary::kNoCall
2821 : LocationSummary::kCallOnSlowPath;
2822 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2823 locations->SetInAt(0, Location::RequiresRegister());
2824 locations->SetInAt(1, Location::Any());
2825 locations->SetOut(Location::RequiresRegister());
2826}
2827
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002828void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002829 LocationSummary* locations = instruction->GetLocations();
2830 Register obj = locations->InAt(0).As<Register>();
2831 Location cls = locations->InAt(1);
2832 Register out = locations->Out().As<Register>();
2833 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2834 Label done, zero;
2835 SlowPathCodeX86* slow_path = nullptr;
2836
2837 // Return 0 if `obj` is null.
2838 // TODO: avoid this check if we know obj is not null.
2839 __ testl(obj, obj);
2840 __ j(kEqual, &zero);
2841 __ movl(out, Address(obj, class_offset));
2842 // Compare the class of `obj` with `cls`.
2843 if (cls.IsRegister()) {
2844 __ cmpl(out, cls.As<Register>());
2845 } else {
2846 DCHECK(cls.IsStackSlot()) << cls;
2847 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
2848 }
2849
2850 if (instruction->IsClassFinal()) {
2851 // Classes must be equal for the instanceof to succeed.
2852 __ j(kNotEqual, &zero);
2853 __ movl(out, Immediate(1));
2854 __ jmp(&done);
2855 } else {
2856 // If the classes are not equal, we go into a slow path.
2857 DCHECK(locations->OnlyCallsOnSlowPath());
2858 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002859 instruction, locations->InAt(1), locations->Out(), instruction->GetDexPc());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002860 codegen_->AddSlowPath(slow_path);
2861 __ j(kNotEqual, slow_path->GetEntryLabel());
2862 __ movl(out, Immediate(1));
2863 __ jmp(&done);
2864 }
2865 __ Bind(&zero);
2866 __ movl(out, Immediate(0));
2867 if (slow_path != nullptr) {
2868 __ Bind(slow_path->GetExitLabel());
2869 }
2870 __ Bind(&done);
2871}
2872
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002873void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
2874 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2875 instruction, LocationSummary::kCallOnSlowPath);
2876 locations->SetInAt(0, Location::RequiresRegister());
2877 locations->SetInAt(1, Location::Any());
2878 locations->AddTemp(Location::RequiresRegister());
2879}
2880
2881void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
2882 LocationSummary* locations = instruction->GetLocations();
2883 Register obj = locations->InAt(0).As<Register>();
2884 Location cls = locations->InAt(1);
2885 Register temp = locations->GetTemp(0).As<Register>();
2886 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2887 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
2888 instruction, locations->InAt(1), locations->GetTemp(0), instruction->GetDexPc());
2889 codegen_->AddSlowPath(slow_path);
2890
2891 // TODO: avoid this check if we know obj is not null.
2892 __ testl(obj, obj);
2893 __ j(kEqual, slow_path->GetExitLabel());
2894 __ movl(temp, Address(obj, class_offset));
2895
2896 // Compare the class of `obj` with `cls`.
2897 if (cls.IsRegister()) {
2898 __ cmpl(temp, cls.As<Register>());
2899 } else {
2900 DCHECK(cls.IsStackSlot()) << cls;
2901 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
2902 }
2903
2904 __ j(kNotEqual, slow_path->GetEntryLabel());
2905 __ Bind(slow_path->GetExitLabel());
2906}
2907
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002908} // namespace x86
2909} // namespace art