blob: 47062744db0281f269cbf636431214aaca55e29a [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"
Roland Levillain647b96f2014-11-11 12:26:26 +000025#include "utils.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000026#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010027#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000028#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010029#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010032
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033namespace x86 {
34
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010035static constexpr bool kExplicitStackOverflowCheck = false;
36
37static constexpr int kNumberOfPushedRegistersAtEntry = 1;
38static constexpr int kCurrentMethodStackOffset = 0;
39
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010040static constexpr Register kRuntimeParameterCoreRegisters[] = { EAX, ECX, EDX };
41static constexpr size_t kRuntimeParameterCoreRegistersLength =
42 arraysize(kRuntimeParameterCoreRegisters);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010043static constexpr XmmRegister kRuntimeParameterFpuRegisters[] = { };
44static constexpr size_t kRuntimeParameterFpuRegistersLength = 0;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010045
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010046class InvokeRuntimeCallingConvention : public CallingConvention<Register, XmmRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010047 public:
48 InvokeRuntimeCallingConvention()
49 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010050 kRuntimeParameterCoreRegistersLength,
51 kRuntimeParameterFpuRegisters,
52 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010053
54 private:
55 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
56};
57
Nicolas Geoffraye5038322014-07-04 09:41:32 +010058#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
59
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010060class SlowPathCodeX86 : public SlowPathCode {
61 public:
62 SlowPathCodeX86() : entry_label_(), exit_label_() {}
63
64 Label* GetEntryLabel() { return &entry_label_; }
65 Label* GetExitLabel() { return &exit_label_; }
66
67 private:
68 Label entry_label_;
69 Label exit_label_;
70
71 DISALLOW_COPY_AND_ASSIGN(SlowPathCodeX86);
72};
73
74class NullCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010075 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010076 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010077
78 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
79 __ Bind(GetEntryLabel());
80 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010081 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010082 }
83
84 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010085 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010086 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
87};
88
Calin Juravled0d48522014-11-04 16:40:20 +000089class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
90 public:
91 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
92
93 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
94 __ Bind(GetEntryLabel());
95 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowDivZero)));
96 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
97 }
98
99 private:
100 HDivZeroCheck* const instruction_;
101 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
102};
103
104class DivMinusOneSlowPathX86 : public SlowPathCodeX86 {
105 public:
106 explicit DivMinusOneSlowPathX86(Register reg) : reg_(reg) {}
107
108 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
109 __ Bind(GetEntryLabel());
110 __ negl(reg_);
111 __ jmp(GetExitLabel());
112 }
113
114 private:
115 Register reg_;
116 DISALLOW_COPY_AND_ASSIGN(DivMinusOneSlowPathX86);
117};
118
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100119class StackOverflowCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100120 public:
121 StackOverflowCheckSlowPathX86() {}
122
123 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
124 __ Bind(GetEntryLabel());
125 __ addl(ESP,
126 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
127 __ fs()->jmp(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowStackOverflow)));
128 }
129
130 private:
131 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86);
132};
133
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100134class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100135 public:
Roland Levillain5799fc02014-09-25 12:15:20 +0100136 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
137 Location index_location,
138 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100139 : instruction_(instruction), index_location_(index_location), length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100140
141 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100142 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100143 __ Bind(GetEntryLabel());
144 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100145 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), index_location_);
146 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(1)), length_location_);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100147 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100148 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100149 }
150
151 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100152 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100153 const Location index_location_;
154 const Location length_location_;
155
156 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
157};
158
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100159class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000160 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100161 explicit SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
162 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000163
164 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100165 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000166 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100167 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000168 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
169 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100170 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100171 if (successor_ == nullptr) {
172 __ jmp(GetReturnLabel());
173 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100174 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100175 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000176 }
177
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100178 Label* GetReturnLabel() {
179 DCHECK(successor_ == nullptr);
180 return &return_label_;
181 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000182
183 private:
184 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100185 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000186 Label return_label_;
187
188 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
189};
190
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000191class LoadStringSlowPathX86 : public SlowPathCodeX86 {
192 public:
193 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
194
195 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
196 LocationSummary* locations = instruction_->GetLocations();
197 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
198
199 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
200 __ Bind(GetEntryLabel());
201 codegen->SaveLiveRegisters(locations);
202
203 InvokeRuntimeCallingConvention calling_convention;
204 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(0));
205 __ movl(calling_convention.GetRegisterAt(1), Immediate(instruction_->GetStringIndex()));
206 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pResolveString)));
207 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
208 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
209 codegen->RestoreLiveRegisters(locations);
210
211 __ jmp(GetExitLabel());
212 }
213
214 private:
215 HLoadString* const instruction_;
216
217 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
218};
219
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000220class LoadClassSlowPathX86 : public SlowPathCodeX86 {
221 public:
222 LoadClassSlowPathX86(HLoadClass* cls,
223 HInstruction* at,
224 uint32_t dex_pc,
225 bool do_clinit)
226 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
227 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
228 }
229
230 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
231 LocationSummary* locations = at_->GetLocations();
232 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
233 __ Bind(GetEntryLabel());
234 codegen->SaveLiveRegisters(locations);
235
236 InvokeRuntimeCallingConvention calling_convention;
237 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
238 x86_codegen->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
239 __ fs()->call(Address::Absolute(do_clinit_
240 ? QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeStaticStorage)
241 : QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInitializeType)));
242 codegen->RecordPcInfo(at_, dex_pc_);
243
244 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000245 Location out = locations->Out();
246 if (out.IsValid()) {
247 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
248 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000249 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000250
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000251 codegen->RestoreLiveRegisters(locations);
252 __ jmp(GetExitLabel());
253 }
254
255 private:
256 // The class this slow path will load.
257 HLoadClass* const cls_;
258
259 // The instruction where this slow path is happening.
260 // (Might be the load class or an initialization check).
261 HInstruction* const at_;
262
263 // The dex PC of `at_`.
264 const uint32_t dex_pc_;
265
266 // Whether to initialize the class.
267 const bool do_clinit_;
268
269 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
270};
271
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000272class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
273 public:
274 TypeCheckSlowPathX86(HTypeCheck* instruction, Location object_class)
275 : instruction_(instruction),
276 object_class_(object_class) {}
277
278 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
279 LocationSummary* locations = instruction_->GetLocations();
280 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
281
282 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
283 __ Bind(GetEntryLabel());
284 codegen->SaveLiveRegisters(locations);
285
286 // We're moving two locations to locations that could overlap, so we need a parallel
287 // move resolver.
288 InvokeRuntimeCallingConvention calling_convention;
289 MoveOperands move1(locations->InAt(1),
290 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
291 nullptr);
292 MoveOperands move2(object_class_,
293 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
294 nullptr);
295 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
296 parallel_move.AddMove(&move1);
297 parallel_move.AddMove(&move2);
298 x86_codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
299
300 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pInstanceofNonTrivial)));
301 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
302 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
303 codegen->RestoreLiveRegisters(locations);
304
305 __ jmp(GetExitLabel());
306 }
307
308 private:
309 HTypeCheck* const instruction_;
310 const Location object_class_;
311
312 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
313};
314
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100315#undef __
316#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
317
Dave Allison20dfc792014-06-16 20:44:29 -0700318inline Condition X86Condition(IfCondition cond) {
319 switch (cond) {
320 case kCondEQ: return kEqual;
321 case kCondNE: return kNotEqual;
322 case kCondLT: return kLess;
323 case kCondLE: return kLessEqual;
324 case kCondGT: return kGreater;
325 case kCondGE: return kGreaterEqual;
326 default:
327 LOG(FATAL) << "Unknown if condition";
328 }
329 return kEqual;
330}
331
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100332void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
333 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
334}
335
336void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
337 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
338}
339
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100340size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
341 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
342 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100343}
344
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100345size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
346 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
347 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100348}
349
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100350CodeGeneratorX86::CodeGeneratorX86(HGraph* graph)
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100351 : CodeGenerator(graph, kNumberOfCpuRegisters, kNumberOfXmmRegisters, kNumberOfRegisterPairs),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100352 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100353 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100354 instruction_visitor_(graph, this),
355 move_resolver_(graph->GetArena(), this) {}
356
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100357size_t CodeGeneratorX86::FrameEntrySpillSize() const {
358 return kNumberOfPushedRegistersAtEntry * kX86WordSize;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100359}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100360
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100361Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100362 switch (type) {
363 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100364 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100365 X86ManagedRegister pair =
366 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100367 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
368 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100369 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
370 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100371 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100372 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100373 }
374
375 case Primitive::kPrimByte:
376 case Primitive::kPrimBoolean:
377 case Primitive::kPrimChar:
378 case Primitive::kPrimShort:
379 case Primitive::kPrimInt:
380 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100381 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100382 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100383 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100384 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
385 X86ManagedRegister current =
386 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
387 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100388 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100389 }
390 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100391 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100392 }
393
394 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100395 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100396 return Location::FpuRegisterLocation(
397 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100398 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100399
400 case Primitive::kPrimVoid:
401 LOG(FATAL) << "Unreachable type " << type;
402 }
403
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100404 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100405}
406
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100407void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100408 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100409 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100410
411 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100412 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100413
414 // TODO: We currently don't use Quick's callee saved registers.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100415 blocked_core_registers_[EBP] = true;
416 blocked_core_registers_[ESI] = true;
417 blocked_core_registers_[EDI] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100418
419 UpdateBlockedPairRegisters();
420}
421
422void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
423 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
424 X86ManagedRegister current =
425 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
426 if (blocked_core_registers_[current.AsRegisterPairLow()]
427 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
428 blocked_register_pairs_[i] = true;
429 }
430 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100431}
432
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100433InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
434 : HGraphVisitor(graph),
435 assembler_(codegen->GetAssembler()),
436 codegen_(codegen) {}
437
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000438void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000439 // Create a fake register to mimic Quick.
440 static const int kFakeReturnRegister = 8;
441 core_spill_mask_ |= (1 << kFakeReturnRegister);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000442
Dave Allison648d7112014-07-25 16:15:27 -0700443 bool skip_overflow_check = IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100444 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
445 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100446 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100447 }
448
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100449 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100450 __ subl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100451
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100452 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100453 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86();
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100454 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100455
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100456 __ fs()->cmpl(ESP, Address::Absolute(Thread::StackEndOffset<kX86WordSize>()));
457 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100458 }
459
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100460 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000461}
462
463void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100464 __ addl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000465}
466
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100467void CodeGeneratorX86::Bind(HBasicBlock* block) {
468 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000469}
470
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100471void CodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100472 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000473}
474
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100475Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
476 switch (load->GetType()) {
477 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100478 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100479 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
480 break;
481
482 case Primitive::kPrimInt:
483 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100484 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100485 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100486
487 case Primitive::kPrimBoolean:
488 case Primitive::kPrimByte:
489 case Primitive::kPrimChar:
490 case Primitive::kPrimShort:
491 case Primitive::kPrimVoid:
492 LOG(FATAL) << "Unexpected type " << load->GetType();
493 }
494
495 LOG(FATAL) << "Unreachable";
496 return Location();
497}
498
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100499Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
500 switch (type) {
501 case Primitive::kPrimBoolean:
502 case Primitive::kPrimByte:
503 case Primitive::kPrimChar:
504 case Primitive::kPrimShort:
505 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100506 case Primitive::kPrimFloat:
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100507 case Primitive::kPrimNot: {
508 uint32_t index = gp_index_++;
509 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100510 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100511 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100512 return Location::StackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100513 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100514 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100515
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100516 case Primitive::kPrimLong:
517 case Primitive::kPrimDouble: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100518 uint32_t index = gp_index_;
519 gp_index_ += 2;
520 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100521 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
522 calling_convention.GetRegisterPairAt(index));
523 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100524 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000525 // On X86, the register index and stack index of a quick parameter is the same, since
526 // we are passing floating pointer values in core registers.
527 return Location::QuickParameter(index, index);
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100528 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100529 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100530 }
531 }
532
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100533 case Primitive::kPrimVoid:
534 LOG(FATAL) << "Unexpected parameter type " << type;
535 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100536 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100537 return Location();
538}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100539
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100540void CodeGeneratorX86::Move32(Location destination, Location source) {
541 if (source.Equals(destination)) {
542 return;
543 }
544 if (destination.IsRegister()) {
545 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100546 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100547 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100548 __ movd(destination.As<Register>(), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100549 } else {
550 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100551 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100552 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100553 } else if (destination.IsFpuRegister()) {
554 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100555 __ movd(destination.As<XmmRegister>(), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100556 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100557 __ movaps(destination.As<XmmRegister>(), source.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100558 } else {
559 DCHECK(source.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100560 __ movss(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100561 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100562 } else {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100563 DCHECK(destination.IsStackSlot());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100564 if (source.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100565 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100566 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100567 __ movss(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100568 } else {
569 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100570 __ pushl(Address(ESP, source.GetStackIndex()));
571 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100572 }
573 }
574}
575
576void CodeGeneratorX86::Move64(Location destination, Location source) {
577 if (source.Equals(destination)) {
578 return;
579 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100580 if (destination.IsRegisterPair()) {
581 if (source.IsRegisterPair()) {
582 __ movl(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
583 __ movl(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100584 } else if (source.IsFpuRegister()) {
585 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100586 } else if (source.IsQuickParameter()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000587 uint16_t register_index = source.GetQuickParameterRegisterIndex();
588 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100589 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100590 __ movl(destination.AsRegisterPairLow<Register>(),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000591 calling_convention.GetRegisterAt(register_index));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100592 __ movl(destination.AsRegisterPairHigh<Register>(), Address(ESP,
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000593 calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100594 } else {
595 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100596 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
597 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100598 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
599 }
600 } else if (destination.IsQuickParameter()) {
601 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000602 uint16_t register_index = destination.GetQuickParameterRegisterIndex();
603 uint16_t stack_index = destination.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100604 if (source.IsRegister()) {
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000605 __ movl(calling_convention.GetRegisterAt(register_index), source.AsRegisterPairLow<Register>());
606 __ movl(Address(ESP, calling_convention.GetStackOffsetOf(stack_index + 1)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100607 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100608 } else if (source.IsFpuRegister()) {
609 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100610 } else {
611 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000612 __ movl(calling_convention.GetRegisterAt(register_index),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100613 Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100614 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000615 __ popl(Address(ESP, calling_convention.GetStackOffsetOf(stack_index + 1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100616 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100617 } else if (destination.IsFpuRegister()) {
618 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100619 __ movsd(destination.As<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100620 } else {
621 LOG(FATAL) << "Unimplemented";
622 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100623 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100624 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100625 if (source.IsRegisterPair()) {
626 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100627 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100628 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100629 } else if (source.IsQuickParameter()) {
630 InvokeDexCallingConvention calling_convention;
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000631 uint16_t register_index = source.GetQuickParameterRegisterIndex();
632 uint16_t stack_index = source.GetQuickParameterStackIndex();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100633 __ movl(Address(ESP, destination.GetStackIndex()),
Nicolas Geoffray0a6c4592014-10-30 16:37:57 +0000634 calling_convention.GetRegisterAt(register_index));
635 DCHECK_EQ(calling_convention.GetStackOffsetOf(stack_index + 1) + GetFrameSize(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100636 static_cast<size_t>(destination.GetHighStackIndex(kX86WordSize)));
637 } else if (source.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100638 __ movsd(Address(ESP, destination.GetStackIndex()), source.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100639 } else {
640 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100641 __ pushl(Address(ESP, source.GetStackIndex()));
642 __ popl(Address(ESP, destination.GetStackIndex()));
643 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
644 __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100645 }
646 }
647}
648
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100649void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Roland Levillain476df552014-10-09 17:51:36 +0100650 if (instruction->IsIntConstant()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100651 Immediate imm(instruction->AsIntConstant()->GetValue());
652 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100653 __ movl(location.As<Register>(), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100654 } else if (location.IsStackSlot()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100655 __ movl(Address(ESP, location.GetStackIndex()), imm);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100656 } else {
657 DCHECK(location.IsConstant());
658 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100659 }
Roland Levillain476df552014-10-09 17:51:36 +0100660 } else if (instruction->IsLongConstant()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100661 int64_t value = instruction->AsLongConstant()->GetValue();
662 if (location.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100663 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
664 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100665 } else if (location.IsDoubleStackSlot()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100666 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
667 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100668 } else {
669 DCHECK(location.IsConstant());
670 DCHECK_EQ(location.GetConstant(), instruction);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100671 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000672 } else if (instruction->IsTemporary()) {
673 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
674 Move32(location, temp_location);
Roland Levillain476df552014-10-09 17:51:36 +0100675 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100676 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100677 switch (instruction->GetType()) {
678 case Primitive::kPrimBoolean:
679 case Primitive::kPrimByte:
680 case Primitive::kPrimChar:
681 case Primitive::kPrimShort:
682 case Primitive::kPrimInt:
683 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100684 case Primitive::kPrimFloat:
685 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100686 break;
687
688 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100689 case Primitive::kPrimDouble:
690 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100691 break;
692
693 default:
694 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
695 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000696 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100697 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100698 switch (instruction->GetType()) {
699 case Primitive::kPrimBoolean:
700 case Primitive::kPrimByte:
701 case Primitive::kPrimChar:
702 case Primitive::kPrimShort:
703 case Primitive::kPrimInt:
704 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100705 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100706 Move32(location, instruction->GetLocations()->Out());
707 break;
708
709 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100710 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100711 Move64(location, instruction->GetLocations()->Out());
712 break;
713
714 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100715 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100716 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000717 }
718}
719
720void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000721 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000722}
723
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000724void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000725 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100726 DCHECK(!successor->IsExitBlock());
727
728 HBasicBlock* block = got->GetBlock();
729 HInstruction* previous = got->GetPrevious();
730
731 HLoopInformation* info = block->GetLoopInformation();
732 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
733 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
734 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
735 return;
736 }
737
738 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
739 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
740 }
741 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000742 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000743 }
744}
745
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000746void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000747 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000748}
749
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000750void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700751 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000752 if (kIsDebugBuild) {
753 __ Comment("Unreachable");
754 __ int3();
755 }
756}
757
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000758void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100759 LocationSummary* locations =
760 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100761 HInstruction* cond = if_instr->InputAt(0);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100762 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100763 locations->SetInAt(0, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100764 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000765}
766
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000767void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700768 HInstruction* cond = if_instr->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100769 if (cond->IsIntConstant()) {
770 // Constant condition, statically compared against 1.
771 int32_t cond_value = cond->AsIntConstant()->GetValue();
772 if (cond_value == 1) {
773 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
774 if_instr->IfTrueSuccessor())) {
775 __ jmp(codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100776 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100777 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100778 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100779 DCHECK_EQ(cond_value, 0);
780 }
781 } else {
782 bool materialized =
783 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
784 // Moves do not affect the eflags register, so if the condition is
785 // evaluated just before the if, we don't need to evaluate it
786 // again.
787 bool eflags_set = cond->IsCondition()
788 && cond->AsCondition()->IsBeforeWhenDisregardMoves(if_instr);
789 if (materialized) {
790 if (!eflags_set) {
791 // Materialized condition, compare against 0.
792 Location lhs = if_instr->GetLocations()->InAt(0);
793 if (lhs.IsRegister()) {
794 __ cmpl(lhs.As<Register>(), Immediate(0));
795 } else {
796 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
797 }
798 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
799 } else {
800 __ j(X86Condition(cond->AsCondition()->GetCondition()),
801 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
802 }
803 } else {
804 Location lhs = cond->GetLocations()->InAt(0);
805 Location rhs = cond->GetLocations()->InAt(1);
806 // LHS is guaranteed to be in a register (see
807 // LocationsBuilderX86::VisitCondition).
808 if (rhs.IsRegister()) {
809 __ cmpl(lhs.As<Register>(), rhs.As<Register>());
810 } else if (rhs.IsConstant()) {
811 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
812 Immediate imm(instruction->AsIntConstant()->GetValue());
813 __ cmpl(lhs.As<Register>(), imm);
814 } else {
815 __ cmpl(lhs.As<Register>(), Address(ESP, rhs.GetStackIndex()));
816 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100817 __ j(X86Condition(cond->AsCondition()->GetCondition()),
818 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Dave Allison20dfc792014-06-16 20:44:29 -0700819 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100820 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100821 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(),
822 if_instr->IfFalseSuccessor())) {
Dave Allison20dfc792014-06-16 20:44:29 -0700823 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000824 }
825}
826
827void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000828 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000829}
830
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000831void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
832 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000833}
834
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000835void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100836 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000837}
838
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000839void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100840 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700841 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000842}
843
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100844void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100845 LocationSummary* locations =
846 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100847 switch (store->InputAt(1)->GetType()) {
848 case Primitive::kPrimBoolean:
849 case Primitive::kPrimByte:
850 case Primitive::kPrimChar:
851 case Primitive::kPrimShort:
852 case Primitive::kPrimInt:
853 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100854 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100855 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
856 break;
857
858 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100859 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100860 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
861 break;
862
863 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100864 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100865 }
866 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000867}
868
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000869void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700870 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000871}
872
Dave Allison20dfc792014-06-16 20:44:29 -0700873void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100874 LocationSummary* locations =
875 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100876 locations->SetInAt(0, Location::RequiresRegister());
877 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100878 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100879 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100880 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000881}
882
Dave Allison20dfc792014-06-16 20:44:29 -0700883void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
884 if (comp->NeedsMaterialization()) {
885 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100886 Register reg = locations->Out().As<Register>();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100887 // Clear register: setcc only sets the low byte.
888 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700889 if (locations->InAt(1).IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100890 __ cmpl(locations->InAt(0).As<Register>(),
891 locations->InAt(1).As<Register>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100892 } else if (locations->InAt(1).IsConstant()) {
893 HConstant* instruction = locations->InAt(1).GetConstant();
894 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100895 __ cmpl(locations->InAt(0).As<Register>(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700896 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100897 __ cmpl(locations->InAt(0).As<Register>(),
Dave Allison20dfc792014-06-16 20:44:29 -0700898 Address(ESP, locations->InAt(1).GetStackIndex()));
899 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100900 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100901 }
Dave Allison20dfc792014-06-16 20:44:29 -0700902}
903
904void LocationsBuilderX86::VisitEqual(HEqual* comp) {
905 VisitCondition(comp);
906}
907
908void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
909 VisitCondition(comp);
910}
911
912void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
913 VisitCondition(comp);
914}
915
916void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
917 VisitCondition(comp);
918}
919
920void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
921 VisitCondition(comp);
922}
923
924void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
925 VisitCondition(comp);
926}
927
928void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
929 VisitCondition(comp);
930}
931
932void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
933 VisitCondition(comp);
934}
935
936void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
937 VisitCondition(comp);
938}
939
940void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
941 VisitCondition(comp);
942}
943
944void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
945 VisitCondition(comp);
946}
947
948void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
949 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000950}
951
952void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100953 LocationSummary* locations =
954 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100955 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000956}
957
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000958void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +0100959 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700960 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000961}
962
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100963void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100964 LocationSummary* locations =
965 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100966 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100967}
968
969void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
970 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700971 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100972}
973
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100974void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
975 LocationSummary* locations =
976 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
977 locations->SetOut(Location::ConstantLocation(constant));
978}
979
980void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
981 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700982 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100983}
984
985void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
986 LocationSummary* locations =
987 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
988 locations->SetOut(Location::ConstantLocation(constant));
989}
990
991void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
992 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700993 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100994}
995
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000996void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000997 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000998}
999
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001000void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001001 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001002 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001003 __ ret();
1004}
1005
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001006void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001007 LocationSummary* locations =
1008 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001009 switch (ret->InputAt(0)->GetType()) {
1010 case Primitive::kPrimBoolean:
1011 case Primitive::kPrimByte:
1012 case Primitive::kPrimChar:
1013 case Primitive::kPrimShort:
1014 case Primitive::kPrimInt:
1015 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001016 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001017 break;
1018
1019 case Primitive::kPrimLong:
1020 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001021 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001022 break;
1023
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001024 case Primitive::kPrimFloat:
1025 case Primitive::kPrimDouble:
1026 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001027 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001028 break;
1029
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001030 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001031 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001032 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001033}
1034
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001035void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001036 if (kIsDebugBuild) {
1037 switch (ret->InputAt(0)->GetType()) {
1038 case Primitive::kPrimBoolean:
1039 case Primitive::kPrimByte:
1040 case Primitive::kPrimChar:
1041 case Primitive::kPrimShort:
1042 case Primitive::kPrimInt:
1043 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001044 DCHECK_EQ(ret->GetLocations()->InAt(0).As<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001045 break;
1046
1047 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001048 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1049 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001050 break;
1051
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001052 case Primitive::kPrimFloat:
1053 case Primitive::kPrimDouble:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001054 DCHECK_EQ(ret->GetLocations()->InAt(0).As<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001055 break;
1056
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001057 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001058 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001059 }
1060 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001061 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001062 __ ret();
1063}
1064
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001065void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001066 HandleInvoke(invoke);
1067}
1068
1069void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001070 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001071
1072 // TODO: Implement all kinds of calls:
1073 // 1) boot -> boot
1074 // 2) app -> boot
1075 // 3) app -> app
1076 //
1077 // Currently we implement the app -> app logic, which looks up in the resolve cache.
1078
1079 // temp = method;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001080 codegen_->LoadCurrentMethod(temp);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001081 // temp = temp->dex_cache_resolved_methods_;
1082 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
1083 // temp = temp[index_in_cache]
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001084 __ movl(temp, Address(temp, CodeGenerator::GetCacheOffset(invoke->GetIndexInDexCache())));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001085 // (temp + offset_of_quick_compiled_code)()
1086 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1087
1088 DCHECK(!codegen_->IsLeafMethod());
1089 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1090}
1091
1092void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1093 HandleInvoke(invoke);
1094}
1095
1096void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001097 LocationSummary* locations =
1098 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001099 locations->AddTemp(Location::RegisterLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001100
1101 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001102 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001103 HInstruction* input = invoke->InputAt(i);
1104 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
1105 }
1106
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001107 switch (invoke->GetType()) {
1108 case Primitive::kPrimBoolean:
1109 case Primitive::kPrimByte:
1110 case Primitive::kPrimChar:
1111 case Primitive::kPrimShort:
1112 case Primitive::kPrimInt:
1113 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001114 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001115 break;
1116
1117 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001118 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001119 break;
1120
1121 case Primitive::kPrimVoid:
1122 break;
1123
1124 case Primitive::kPrimDouble:
1125 case Primitive::kPrimFloat:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001126 locations->SetOut(Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001127 break;
1128 }
1129
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001130 invoke->SetLocations(locations);
1131}
1132
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001133void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001134 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001135 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
1136 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
1137 LocationSummary* locations = invoke->GetLocations();
1138 Location receiver = locations->InAt(0);
1139 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1140 // temp = object->GetClass();
1141 if (receiver.IsStackSlot()) {
1142 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1143 __ movl(temp, Address(temp, class_offset));
1144 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001145 __ movl(temp, Address(receiver.As<Register>(), class_offset));
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001146 }
1147 // temp = temp->GetMethodAt(method_offset);
1148 __ movl(temp, Address(temp, method_offset));
1149 // call temp->GetEntryPoint();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001150 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1151
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001152 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001153 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001154}
1155
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001156void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1157 HandleInvoke(invoke);
1158 // Add the hidden argument.
1159 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM0));
1160}
1161
1162void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1163 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
1164 Register temp = invoke->GetLocations()->GetTemp(0).As<Register>();
1165 uint32_t method_offset = mirror::Class::EmbeddedImTableOffset().Uint32Value() +
1166 (invoke->GetImtIndex() % mirror::Class::kImtSize) * sizeof(mirror::Class::ImTableEntry);
1167 LocationSummary* locations = invoke->GetLocations();
1168 Location receiver = locations->InAt(0);
1169 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1170
1171 // Set the hidden argument.
1172 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
1173 __ movd(invoke->GetLocations()->GetTemp(1).As<XmmRegister>(), temp);
1174
1175 // temp = object->GetClass();
1176 if (receiver.IsStackSlot()) {
1177 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1178 __ movl(temp, Address(temp, class_offset));
1179 } else {
1180 __ movl(temp, Address(receiver.As<Register>(), class_offset));
1181 }
1182 // temp = temp->GetImtEntryAt(method_offset);
1183 __ movl(temp, Address(temp, method_offset));
1184 // call temp->GetEntryPoint();
1185 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
1186
1187 DCHECK(!codegen_->IsLeafMethod());
1188 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1189}
1190
Roland Levillain88cb1752014-10-20 16:36:47 +01001191void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1192 LocationSummary* locations =
1193 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1194 switch (neg->GetResultType()) {
1195 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001196 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001197 locations->SetInAt(0, Location::RequiresRegister());
1198 locations->SetOut(Location::SameAsFirstInput());
1199 break;
1200
Roland Levillain88cb1752014-10-20 16:36:47 +01001201 case Primitive::kPrimFloat:
1202 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001203 locations->SetInAt(0, Location::RequiresFpuRegister());
1204 // Output overlaps as we need a fresh (zero-initialized)
1205 // register to perform subtraction from zero.
1206 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001207 break;
1208
1209 default:
1210 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1211 }
1212}
1213
1214void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1215 LocationSummary* locations = neg->GetLocations();
1216 Location out = locations->Out();
1217 Location in = locations->InAt(0);
1218 switch (neg->GetResultType()) {
1219 case Primitive::kPrimInt:
1220 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001221 DCHECK(in.Equals(out));
Roland Levillain88cb1752014-10-20 16:36:47 +01001222 __ negl(out.As<Register>());
1223 break;
1224
1225 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001226 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001227 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001228 __ negl(out.AsRegisterPairLow<Register>());
1229 // Negation is similar to subtraction from zero. The least
1230 // significant byte triggers a borrow when it is different from
1231 // zero; to take it into account, add 1 to the most significant
1232 // byte if the carry flag (CF) is set to 1 after the first NEGL
1233 // operation.
1234 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1235 __ negl(out.AsRegisterPairHigh<Register>());
1236 break;
1237
Roland Levillain88cb1752014-10-20 16:36:47 +01001238 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001239 DCHECK(!in.Equals(out));
1240 // out = 0
1241 __ xorps(out.As<XmmRegister>(), out.As<XmmRegister>());
1242 // out = out - in
1243 __ subss(out.As<XmmRegister>(), in.As<XmmRegister>());
1244 break;
1245
Roland Levillain88cb1752014-10-20 16:36:47 +01001246 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001247 DCHECK(!in.Equals(out));
1248 // out = 0
1249 __ xorpd(out.As<XmmRegister>(), out.As<XmmRegister>());
1250 // out = out - in
1251 __ subsd(out.As<XmmRegister>(), in.As<XmmRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001252 break;
1253
1254 default:
1255 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1256 }
1257}
1258
Roland Levillaindff1f282014-11-05 14:15:05 +00001259void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
1260 LocationSummary* locations =
1261 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
1262 Primitive::Type result_type = conversion->GetResultType();
1263 Primitive::Type input_type = conversion->GetInputType();
1264 switch (result_type) {
Roland Levillain647b96f2014-11-11 12:26:26 +00001265 case Primitive::kPrimInt:
1266 switch (input_type) {
1267 case Primitive::kPrimLong:
1268 // long-to-int conversion.
1269 locations->SetInAt(0, Location::Any());
1270 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1271 break;
1272
1273 case Primitive::kPrimFloat:
1274 case Primitive::kPrimDouble:
1275 LOG(FATAL) << "Type conversion from " << input_type
1276 << " to " << result_type << " not yet implemented";
1277 break;
1278
1279 default:
1280 LOG(FATAL) << "Unexpected type conversion from " << input_type
1281 << " to " << result_type;
1282 }
1283 break;
1284
Roland Levillaindff1f282014-11-05 14:15:05 +00001285 case Primitive::kPrimLong:
1286 switch (input_type) {
1287 case Primitive::kPrimByte:
1288 case Primitive::kPrimShort:
1289 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001290 case Primitive::kPrimChar:
Roland Levillaindff1f282014-11-05 14:15:05 +00001291 // int-to-long conversion.
1292 locations->SetInAt(0, Location::RegisterLocation(EAX));
1293 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1294 break;
1295
1296 case Primitive::kPrimFloat:
1297 case Primitive::kPrimDouble:
1298 LOG(FATAL) << "Type conversion from " << input_type << " to "
1299 << result_type << " not yet implemented";
1300 break;
1301
1302 default:
1303 LOG(FATAL) << "Unexpected type conversion from " << input_type
1304 << " to " << result_type;
1305 }
1306 break;
1307
Roland Levillaindff1f282014-11-05 14:15:05 +00001308 case Primitive::kPrimFloat:
1309 case Primitive::kPrimDouble:
1310 LOG(FATAL) << "Type conversion from " << input_type
1311 << " to " << result_type << " not yet implemented";
1312 break;
1313
1314 default:
1315 LOG(FATAL) << "Unexpected type conversion from " << input_type
1316 << " to " << result_type;
1317 }
1318}
1319
1320void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1321 LocationSummary* locations = conversion->GetLocations();
1322 Location out = locations->Out();
1323 Location in = locations->InAt(0);
1324 Primitive::Type result_type = conversion->GetResultType();
1325 Primitive::Type input_type = conversion->GetInputType();
1326 switch (result_type) {
Roland Levillain647b96f2014-11-11 12:26:26 +00001327 case Primitive::kPrimInt:
1328 switch (input_type) {
1329 case Primitive::kPrimLong:
1330 // long-to-int conversion.
1331 if (in.IsRegisterPair()) {
1332 __ movl(out.As<Register>(), in.AsRegisterPairLow<Register>());
1333 } else if (in.IsDoubleStackSlot()) {
1334 __ movl(out.As<Register>(), Address(ESP, in.GetStackIndex()));
1335 } else {
1336 DCHECK(in.IsConstant());
1337 DCHECK(in.GetConstant()->IsLongConstant());
1338 __ movl(out.As<Register>(),
1339 Immediate(Low32Bits(in.GetConstant()->AsLongConstant()->GetValue())));
1340 }
1341 break;
1342
1343 case Primitive::kPrimFloat:
1344 case Primitive::kPrimDouble:
1345 LOG(FATAL) << "Type conversion from " << input_type
1346 << " to " << result_type << " not yet implemented";
1347 break;
1348
1349 default:
1350 LOG(FATAL) << "Unexpected type conversion from " << input_type
1351 << " to " << result_type;
1352 }
1353 break;
1354
Roland Levillaindff1f282014-11-05 14:15:05 +00001355 case Primitive::kPrimLong:
1356 switch (input_type) {
1357 case Primitive::kPrimByte:
1358 case Primitive::kPrimShort:
1359 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001360 case Primitive::kPrimChar:
Roland Levillaindff1f282014-11-05 14:15:05 +00001361 // int-to-long conversion.
1362 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
1363 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
1364 DCHECK_EQ(in.As<Register>(), EAX);
1365 __ cdq();
1366 break;
1367
1368 case Primitive::kPrimFloat:
1369 case Primitive::kPrimDouble:
1370 LOG(FATAL) << "Type conversion from " << input_type << " to "
1371 << result_type << " not yet implemented";
1372 break;
1373
1374 default:
1375 LOG(FATAL) << "Unexpected type conversion from " << input_type
1376 << " to " << result_type;
1377 }
1378 break;
1379
Roland Levillaindff1f282014-11-05 14:15:05 +00001380 case Primitive::kPrimFloat:
1381 case Primitive::kPrimDouble:
1382 LOG(FATAL) << "Type conversion from " << input_type
1383 << " to " << result_type << " not yet implemented";
1384 break;
1385
1386 default:
1387 LOG(FATAL) << "Unexpected type conversion from " << input_type
1388 << " to " << result_type;
1389 }
1390}
1391
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001392void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001393 LocationSummary* locations =
1394 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001395 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001396 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001397 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001398 locations->SetInAt(0, Location::RequiresRegister());
1399 locations->SetInAt(1, Location::Any());
1400 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001401 break;
1402 }
1403
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001404 case Primitive::kPrimFloat:
1405 case Primitive::kPrimDouble: {
1406 locations->SetInAt(0, Location::RequiresFpuRegister());
1407 locations->SetInAt(1, Location::Any());
1408 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001409 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001410 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001411
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001412 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001413 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
1414 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001415 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001416}
1417
1418void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
1419 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001420 Location first = locations->InAt(0);
1421 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001422 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001423 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001424 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001425 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001426 __ addl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001427 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001428 __ addl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001429 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001430 __ addl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001431 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001432 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001433 }
1434
1435 case Primitive::kPrimLong: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001436 if (second.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001437 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1438 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001439 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001440 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
1441 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001442 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001443 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001444 break;
1445 }
1446
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001447 case Primitive::kPrimFloat: {
1448 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001449 __ addss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001450 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001451 __ addss(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001452 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001453 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001454 }
1455
1456 case Primitive::kPrimDouble: {
1457 if (second.IsFpuRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001458 __ addsd(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001459 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001460 __ addsd(first.As<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001461 }
1462 break;
1463 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001464
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001465 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001466 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001467 }
1468}
1469
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001470void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001471 LocationSummary* locations =
1472 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001473 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001474 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001475 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001476 locations->SetInAt(0, Location::RequiresRegister());
1477 locations->SetInAt(1, Location::Any());
1478 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001479 break;
1480 }
Calin Juravle11351682014-10-23 15:38:15 +01001481 case Primitive::kPrimFloat:
1482 case Primitive::kPrimDouble: {
1483 locations->SetInAt(0, Location::RequiresFpuRegister());
1484 locations->SetInAt(1, Location::RequiresFpuRegister());
1485 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001486 break;
Calin Juravle11351682014-10-23 15:38:15 +01001487 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001488
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001489 default:
Calin Juravle11351682014-10-23 15:38:15 +01001490 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001491 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001492}
1493
1494void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1495 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001496 Location first = locations->InAt(0);
1497 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01001498 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001499 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001500 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001501 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001502 __ subl(first.As<Register>(), second.As<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001503 } else if (second.IsConstant()) {
Calin Juravle11351682014-10-23 15:38:15 +01001504 __ subl(first.As<Register>(), Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001505 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001506 __ subl(first.As<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001507 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001508 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001509 }
1510
1511 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001512 if (second.IsRegister()) {
Calin Juravle11351682014-10-23 15:38:15 +01001513 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
1514 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001515 } else {
Calin Juravle11351682014-10-23 15:38:15 +01001516 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001517 __ sbbl(first.AsRegisterPairHigh<Register>(),
1518 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001519 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001520 break;
1521 }
1522
Calin Juravle11351682014-10-23 15:38:15 +01001523 case Primitive::kPrimFloat: {
1524 __ subss(first.As<XmmRegister>(), second.As<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001525 break;
Calin Juravle11351682014-10-23 15:38:15 +01001526 }
1527
1528 case Primitive::kPrimDouble: {
1529 __ subsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1530 break;
1531 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001532
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001533 default:
Calin Juravle11351682014-10-23 15:38:15 +01001534 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001535 }
1536}
1537
Calin Juravle34bacdf2014-10-07 20:23:36 +01001538void LocationsBuilderX86::VisitMul(HMul* mul) {
1539 LocationSummary* locations =
1540 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
1541 switch (mul->GetResultType()) {
1542 case Primitive::kPrimInt:
1543 locations->SetInAt(0, Location::RequiresRegister());
1544 locations->SetInAt(1, Location::Any());
1545 locations->SetOut(Location::SameAsFirstInput());
1546 break;
1547 case Primitive::kPrimLong: {
1548 locations->SetInAt(0, Location::RequiresRegister());
1549 // TODO: Currently this handles only stack operands:
1550 // - we don't have enough registers because we currently use Quick ABI.
1551 // - by the time we have a working register allocator we will probably change the ABI
1552 // and fix the above.
1553 // - we don't have a way yet to request operands on stack but the base line compiler
1554 // will leave the operands on the stack with Any().
1555 locations->SetInAt(1, Location::Any());
1556 locations->SetOut(Location::SameAsFirstInput());
1557 // Needed for imul on 32bits with 64bits output.
1558 locations->AddTemp(Location::RegisterLocation(EAX));
1559 locations->AddTemp(Location::RegisterLocation(EDX));
1560 break;
1561 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01001562 case Primitive::kPrimFloat:
1563 case Primitive::kPrimDouble: {
1564 locations->SetInAt(0, Location::RequiresFpuRegister());
1565 locations->SetInAt(1, Location::RequiresFpuRegister());
1566 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001567 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001568 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001569
1570 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001571 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001572 }
1573}
1574
1575void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
1576 LocationSummary* locations = mul->GetLocations();
1577 Location first = locations->InAt(0);
1578 Location second = locations->InAt(1);
1579 DCHECK(first.Equals(locations->Out()));
1580
1581 switch (mul->GetResultType()) {
1582 case Primitive::kPrimInt: {
1583 if (second.IsRegister()) {
1584 __ imull(first.As<Register>(), second.As<Register>());
1585 } else if (second.IsConstant()) {
1586 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
1587 __ imull(first.As<Register>(), imm);
1588 } else {
1589 DCHECK(second.IsStackSlot());
1590 __ imull(first.As<Register>(), Address(ESP, second.GetStackIndex()));
1591 }
1592 break;
1593 }
1594
1595 case Primitive::kPrimLong: {
1596 DCHECK(second.IsDoubleStackSlot());
1597
1598 Register in1_hi = first.AsRegisterPairHigh<Register>();
1599 Register in1_lo = first.AsRegisterPairLow<Register>();
1600 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
1601 Address in2_lo(ESP, second.GetStackIndex());
1602 Register eax = locations->GetTemp(0).As<Register>();
1603 Register edx = locations->GetTemp(1).As<Register>();
1604
1605 DCHECK_EQ(EAX, eax);
1606 DCHECK_EQ(EDX, edx);
1607
1608 // input: in1 - 64 bits, in2 - 64 bits
1609 // output: in1
1610 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
1611 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
1612 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
1613
1614 __ movl(eax, in2_hi);
1615 // eax <- in1.lo * in2.hi
1616 __ imull(eax, in1_lo);
1617 // in1.hi <- in1.hi * in2.lo
1618 __ imull(in1_hi, in2_lo);
1619 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
1620 __ addl(in1_hi, eax);
1621 // move in1_lo to eax to prepare for double precision
1622 __ movl(eax, in1_lo);
1623 // edx:eax <- in1.lo * in2.lo
1624 __ mull(in2_lo);
1625 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
1626 __ addl(in1_hi, edx);
1627 // in1.lo <- (in1.lo * in2.lo)[31:0];
1628 __ movl(in1_lo, eax);
1629
1630 break;
1631 }
1632
Calin Juravleb5bfa962014-10-21 18:02:24 +01001633 case Primitive::kPrimFloat: {
1634 __ mulss(first.As<XmmRegister>(), second.As<XmmRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01001635 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01001636 }
1637
1638 case Primitive::kPrimDouble: {
1639 __ mulsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1640 break;
1641 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001642
1643 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01001644 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01001645 }
1646}
1647
Calin Juravle7c4954d2014-10-28 16:57:40 +00001648void LocationsBuilderX86::VisitDiv(HDiv* div) {
1649 LocationSummary* locations =
1650 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1651 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001652 case Primitive::kPrimInt: {
1653 locations->SetInAt(0, Location::RegisterLocation(EAX));
1654 locations->SetInAt(1, Location::RequiresRegister());
1655 locations->SetOut(Location::SameAsFirstInput());
1656 // Intel uses edx:eax as the dividend.
1657 locations->AddTemp(Location::RegisterLocation(EDX));
1658 break;
1659 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001660 case Primitive::kPrimLong: {
1661 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1662 break;
1663 }
1664 case Primitive::kPrimFloat:
1665 case Primitive::kPrimDouble: {
1666 locations->SetInAt(0, Location::RequiresFpuRegister());
1667 locations->SetInAt(1, Location::RequiresFpuRegister());
1668 locations->SetOut(Location::SameAsFirstInput());
1669 break;
1670 }
1671
1672 default:
1673 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1674 }
1675}
1676
1677void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
1678 LocationSummary* locations = div->GetLocations();
1679 Location first = locations->InAt(0);
1680 Location second = locations->InAt(1);
1681 DCHECK(first.Equals(locations->Out()));
1682
1683 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00001684 case Primitive::kPrimInt: {
1685 Register first_reg = first.As<Register>();
1686 Register second_reg = second.As<Register>();
1687 DCHECK_EQ(EAX, first_reg);
1688 DCHECK_EQ(EDX, locations->GetTemp(0).As<Register>());
1689
1690 SlowPathCodeX86* slow_path =
1691 new (GetGraph()->GetArena()) DivMinusOneSlowPathX86(first_reg);
1692 codegen_->AddSlowPath(slow_path);
1693
1694 // 0x80000000/-1 triggers an arithmetic exception!
1695 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
1696 // it's safe to just use negl instead of more complex comparisons.
1697
1698 __ cmpl(second_reg, Immediate(-1));
1699 __ j(kEqual, slow_path->GetEntryLabel());
1700
1701 // edx:eax <- sign-extended of eax
1702 __ cdq();
1703 // eax = quotient, edx = remainder
1704 __ idivl(second_reg);
1705
1706 __ Bind(slow_path->GetExitLabel());
1707 break;
1708 }
1709
Calin Juravle7c4954d2014-10-28 16:57:40 +00001710 case Primitive::kPrimLong: {
1711 LOG(FATAL) << "Not implemented div type" << div->GetResultType();
1712 break;
1713 }
1714
1715 case Primitive::kPrimFloat: {
1716 __ divss(first.As<XmmRegister>(), second.As<XmmRegister>());
1717 break;
1718 }
1719
1720 case Primitive::kPrimDouble: {
1721 __ divsd(first.As<XmmRegister>(), second.As<XmmRegister>());
1722 break;
1723 }
1724
1725 default:
1726 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1727 }
1728}
1729
Calin Juravled0d48522014-11-04 16:40:20 +00001730void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1731 LocationSummary* locations =
1732 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1733 locations->SetInAt(0, Location::Any());
1734 if (instruction->HasUses()) {
1735 locations->SetOut(Location::SameAsFirstInput());
1736 }
1737}
1738
1739void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1740 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
1741 codegen_->AddSlowPath(slow_path);
1742
1743 LocationSummary* locations = instruction->GetLocations();
1744 Location value = locations->InAt(0);
1745
1746 if (value.IsRegister()) {
1747 __ testl(value.As<Register>(), value.As<Register>());
1748 } else if (value.IsStackSlot()) {
1749 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
1750 } else {
1751 DCHECK(value.IsConstant()) << value;
1752 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
1753 __ jmp(slow_path->GetEntryLabel());
1754 }
1755 return;
1756 }
1757 __ j(kEqual, slow_path->GetEntryLabel());
1758}
1759
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001760void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001761 LocationSummary* locations =
1762 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001763 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001764 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001765 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1766 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001767}
1768
1769void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
1770 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001771 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001772 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001773
Nicolas Geoffray707c8092014-04-04 10:50:14 +01001774 __ fs()->call(
1775 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001776
Nicolas Geoffray39468442014-09-02 15:17:15 +01001777 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001778 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001779}
1780
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001781void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
1782 LocationSummary* locations =
1783 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
1784 locations->SetOut(Location::RegisterLocation(EAX));
1785 InvokeRuntimeCallingConvention calling_convention;
1786 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1787 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1788 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1789}
1790
1791void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
1792 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001793 codegen_->LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001794 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
1795
1796 __ fs()->call(
1797 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocArrayWithAccessCheck)));
1798
1799 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
1800 DCHECK(!codegen_->IsLeafMethod());
1801}
1802
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001803void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001804 LocationSummary* locations =
1805 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001806 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1807 if (location.IsStackSlot()) {
1808 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1809 } else if (location.IsDoubleStackSlot()) {
1810 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001811 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001812 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001813}
1814
1815void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001816 UNUSED(instruction);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001817}
1818
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001819void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001820 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001821 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001822 locations->SetInAt(0, Location::RequiresRegister());
1823 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001824}
1825
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001826void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
1827 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01001828 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001829 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01001830 DCHECK(in.Equals(out));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001831 switch (not_->InputAt(0)->GetType()) {
1832 case Primitive::kPrimBoolean:
1833 __ xorl(out.As<Register>(), Immediate(1));
1834 break;
1835
1836 case Primitive::kPrimInt:
1837 __ notl(out.As<Register>());
1838 break;
1839
1840 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01001841 __ notl(out.AsRegisterPairLow<Register>());
1842 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001843 break;
1844
1845 default:
1846 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
1847 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001848}
1849
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001850void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001851 LocationSummary* locations =
1852 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001853 locations->SetInAt(0, Location::RequiresRegister());
1854 locations->SetInAt(1, Location::Any());
1855 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001856}
1857
1858void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001859 LocationSummary* locations = compare->GetLocations();
1860 switch (compare->InputAt(0)->GetType()) {
1861 case Primitive::kPrimLong: {
1862 Label less, greater, done;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001863 Register output = locations->Out().As<Register>();
1864 Location left = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001865 Location right = locations->InAt(1);
1866 if (right.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001867 __ cmpl(left.AsRegisterPairHigh<Register>(), right.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001868 } else {
1869 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001870 __ cmpl(left.AsRegisterPairHigh<Register>(),
1871 Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001872 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001873 __ j(kLess, &less); // Signed compare.
1874 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001875 if (right.IsRegisterPair()) {
1876 __ cmpl(left.AsRegisterPairLow<Register>(), right.AsRegisterPairLow<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001877 } else {
1878 DCHECK(right.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001879 __ cmpl(left.AsRegisterPairLow<Register>(), Address(ESP, right.GetStackIndex()));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001880 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001881 __ movl(output, Immediate(0));
1882 __ j(kEqual, &done);
1883 __ j(kBelow, &less); // Unsigned compare.
1884
1885 __ Bind(&greater);
1886 __ movl(output, Immediate(1));
1887 __ jmp(&done);
1888
1889 __ Bind(&less);
1890 __ movl(output, Immediate(-1));
1891
1892 __ Bind(&done);
1893 break;
1894 }
1895 default:
1896 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
1897 }
1898}
1899
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001900void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001901 LocationSummary* locations =
1902 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001903 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1904 locations->SetInAt(i, Location::Any());
1905 }
1906 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001907}
1908
1909void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001910 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001911 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001912}
1913
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001914void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001915 LocationSummary* locations =
1916 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001917 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001918 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001919 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001920 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
1921 || (field_type == Primitive::kPrimByte);
1922 // The register allocator does not support multiple
1923 // inputs that die at entry with one in a specific register.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001924 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001925 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001926 locations->SetInAt(1, Location::RegisterLocation(EAX));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001927 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01001928 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001929 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001930 // Temporary registers for the write barrier.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001931 if (is_object_type) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001932 locations->AddTemp(Location::RequiresRegister());
1933 // Ensure the card is in a byte register.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001934 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001935 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001936}
1937
1938void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1939 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001940 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001941 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001942 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001943
1944 switch (field_type) {
1945 case Primitive::kPrimBoolean:
1946 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001947 ByteRegister value = locations->InAt(1).As<ByteRegister>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001948 __ movb(Address(obj, offset), value);
1949 break;
1950 }
1951
1952 case Primitive::kPrimShort:
1953 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001954 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001955 __ movw(Address(obj, offset), value);
1956 break;
1957 }
1958
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001959 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001960 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001961 Register value = locations->InAt(1).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001962 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001963
1964 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001965 Register temp = locations->GetTemp(0).As<Register>();
1966 Register card = locations->GetTemp(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001967 codegen_->MarkGCCard(temp, card, obj, value);
1968 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001969 break;
1970 }
1971
1972 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001973 Location value = locations->InAt(1);
1974 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
1975 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001976 break;
1977 }
1978
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00001979 case Primitive::kPrimFloat: {
1980 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1981 __ movss(Address(obj, offset), value);
1982 break;
1983 }
1984
1985 case Primitive::kPrimDouble: {
1986 XmmRegister value = locations->InAt(1).As<XmmRegister>();
1987 __ movsd(Address(obj, offset), value);
1988 break;
1989 }
1990
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001991 case Primitive::kPrimVoid:
1992 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07001993 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001994 }
1995}
1996
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001997void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
1998 Label is_null;
1999 __ testl(value, value);
2000 __ j(kEqual, &is_null);
2001 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
2002 __ movl(temp, object);
2003 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
2004 __ movb(Address(temp, card, TIMES_1, 0),
2005 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
2006 __ Bind(&is_null);
2007}
2008
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002009void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002010 LocationSummary* locations =
2011 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002012 locations->SetInAt(0, Location::RequiresRegister());
2013 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002014}
2015
2016void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2017 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002018 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002019 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2020
2021 switch (instruction->GetType()) {
2022 case Primitive::kPrimBoolean: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002023 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002024 __ movzxb(out, Address(obj, offset));
2025 break;
2026 }
2027
2028 case Primitive::kPrimByte: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002029 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002030 __ movsxb(out, Address(obj, offset));
2031 break;
2032 }
2033
2034 case Primitive::kPrimShort: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002035 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002036 __ movsxw(out, Address(obj, offset));
2037 break;
2038 }
2039
2040 case Primitive::kPrimChar: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002041 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002042 __ movzxw(out, Address(obj, offset));
2043 break;
2044 }
2045
2046 case Primitive::kPrimInt:
2047 case Primitive::kPrimNot: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002048 Register out = locations->Out().As<Register>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002049 __ movl(out, Address(obj, offset));
2050 break;
2051 }
2052
2053 case Primitive::kPrimLong: {
2054 // TODO: support volatile.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002055 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(obj, offset));
2056 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(obj, kX86WordSize + offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002057 break;
2058 }
2059
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002060 case Primitive::kPrimFloat: {
2061 XmmRegister out = locations->Out().As<XmmRegister>();
2062 __ movss(out, Address(obj, offset));
2063 break;
2064 }
2065
2066 case Primitive::kPrimDouble: {
2067 XmmRegister out = locations->Out().As<XmmRegister>();
2068 __ movsd(out, Address(obj, offset));
2069 break;
2070 }
2071
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002072 case Primitive::kPrimVoid:
2073 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002074 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002075 }
2076}
2077
2078void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002079 LocationSummary* locations =
2080 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002081 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002082 if (instruction->HasUses()) {
2083 locations->SetOut(Location::SameAsFirstInput());
2084 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002085}
2086
2087void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002088 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002089 codegen_->AddSlowPath(slow_path);
2090
2091 LocationSummary* locations = instruction->GetLocations();
2092 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002093
2094 if (obj.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002095 __ cmpl(obj.As<Register>(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002096 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002097 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002098 } else {
2099 DCHECK(obj.IsConstant()) << obj;
2100 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
2101 __ jmp(slow_path->GetEntryLabel());
2102 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002103 }
2104 __ j(kEqual, slow_path->GetEntryLabel());
2105}
2106
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002107void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002108 LocationSummary* locations =
2109 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002110 locations->SetInAt(0, Location::RequiresRegister());
2111 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2112 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002113}
2114
2115void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
2116 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002117 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002118 Location index = locations->InAt(1);
2119
2120 switch (instruction->GetType()) {
2121 case Primitive::kPrimBoolean: {
2122 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002123 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002124 if (index.IsConstant()) {
2125 __ movzxb(out, Address(obj,
2126 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2127 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002128 __ movzxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002129 }
2130 break;
2131 }
2132
2133 case Primitive::kPrimByte: {
2134 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002135 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002136 if (index.IsConstant()) {
2137 __ movsxb(out, Address(obj,
2138 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
2139 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002140 __ movsxb(out, Address(obj, index.As<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002141 }
2142 break;
2143 }
2144
2145 case Primitive::kPrimShort: {
2146 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002147 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002148 if (index.IsConstant()) {
2149 __ movsxw(out, Address(obj,
2150 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2151 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002152 __ movsxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002153 }
2154 break;
2155 }
2156
2157 case Primitive::kPrimChar: {
2158 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002159 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002160 if (index.IsConstant()) {
2161 __ movzxw(out, Address(obj,
2162 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
2163 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002164 __ movzxw(out, Address(obj, index.As<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002165 }
2166 break;
2167 }
2168
2169 case Primitive::kPrimInt:
2170 case Primitive::kPrimNot: {
2171 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002172 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002173 if (index.IsConstant()) {
2174 __ movl(out, Address(obj,
2175 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
2176 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002177 __ movl(out, Address(obj, index.As<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002178 }
2179 break;
2180 }
2181
2182 case Primitive::kPrimLong: {
2183 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002184 Location out = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002185 if (index.IsConstant()) {
2186 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002187 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
2188 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002189 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002190 __ movl(out.AsRegisterPairLow<Register>(),
2191 Address(obj, index.As<Register>(), TIMES_8, data_offset));
2192 __ movl(out.AsRegisterPairHigh<Register>(),
2193 Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002194 }
2195 break;
2196 }
2197
2198 case Primitive::kPrimFloat:
2199 case Primitive::kPrimDouble:
2200 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002201 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002202 case Primitive::kPrimVoid:
2203 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002204 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002205 }
2206}
2207
2208void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002209 Primitive::Type value_type = instruction->GetComponentType();
2210 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2211 instruction,
2212 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
2213
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002214 if (value_type == Primitive::kPrimNot) {
2215 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002216 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2217 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2218 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002219 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002220 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
2221 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01002222 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002223 // In case of a byte operation, the register allocator does not support multiple
2224 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002225 locations->SetInAt(0, Location::RequiresRegister());
2226 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01002227 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002228 // Ensure the value is in a byte register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002229 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002230 } else {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002231 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002232 }
2233 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002234}
2235
2236void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
2237 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002238 Register obj = locations->InAt(0).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002239 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002240 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01002241 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002242
2243 switch (value_type) {
2244 case Primitive::kPrimBoolean:
2245 case Primitive::kPrimByte: {
2246 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002247 if (index.IsConstant()) {
2248 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002249 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002250 __ movb(Address(obj, offset), value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002251 } else {
2252 __ movb(Address(obj, offset),
2253 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2254 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002255 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002256 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002257 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
2258 value.As<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002259 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002260 __ movb(Address(obj, index.As<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002261 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2262 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002263 }
2264 break;
2265 }
2266
2267 case Primitive::kPrimShort:
2268 case Primitive::kPrimChar: {
2269 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002270 if (index.IsConstant()) {
2271 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002272 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002273 __ movw(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002274 } else {
2275 __ movw(Address(obj, offset),
2276 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2277 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002278 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002279 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002280 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
2281 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002282 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002283 __ movw(Address(obj, index.As<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002284 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2285 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002286 }
2287 break;
2288 }
2289
2290 case Primitive::kPrimInt: {
2291 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002292 if (index.IsConstant()) {
2293 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002294 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002295 __ movl(Address(obj, offset), value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002296 } else {
2297 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2298 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002299 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002300 if (value.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002301 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
2302 value.As<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002303 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002304 __ movl(Address(obj, index.As<Register>(), TIMES_4, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002305 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
2306 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002307 }
2308 break;
2309 }
2310
2311 case Primitive::kPrimNot: {
2312 DCHECK(!codegen_->IsLeafMethod());
2313 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01002314 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002315 break;
2316 }
2317
2318 case Primitive::kPrimLong: {
2319 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002320 if (index.IsConstant()) {
2321 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002322 if (value.IsRegisterPair()) {
2323 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
2324 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002325 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002326 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002327 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
2328 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
2329 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
2330 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002331 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002332 if (value.IsRegisterPair()) {
2333 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
2334 value.AsRegisterPairLow<Register>());
2335 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
2336 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002337 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002338 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002339 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002340 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002341 Immediate(Low32Bits(val)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002342 __ movl(Address(obj, index.As<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002343 Immediate(High32Bits(val)));
2344 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002345 }
2346 break;
2347 }
2348
2349 case Primitive::kPrimFloat:
2350 case Primitive::kPrimDouble:
2351 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002352 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002353 case Primitive::kPrimVoid:
2354 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07002355 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002356 }
2357}
2358
2359void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
2360 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002361 locations->SetInAt(0, Location::RequiresRegister());
2362 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002363 instruction->SetLocations(locations);
2364}
2365
2366void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
2367 LocationSummary* locations = instruction->GetLocations();
2368 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002369 Register obj = locations->InAt(0).As<Register>();
2370 Register out = locations->Out().As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002371 __ movl(out, Address(obj, offset));
2372}
2373
2374void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002375 LocationSummary* locations =
2376 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002377 locations->SetInAt(0, Location::RequiresRegister());
2378 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01002379 if (instruction->HasUses()) {
2380 locations->SetOut(Location::SameAsFirstInput());
2381 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002382}
2383
2384void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
2385 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002386 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01002387 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002388 codegen_->AddSlowPath(slow_path);
2389
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002390 Register index = locations->InAt(0).As<Register>();
2391 Register length = locations->InAt(1).As<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002392
2393 __ cmpl(index, length);
2394 __ j(kAboveEqual, slow_path->GetEntryLabel());
2395}
2396
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002397void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
2398 temp->SetLocations(nullptr);
2399}
2400
2401void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
2402 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002403 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002404}
2405
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002406void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002407 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002408 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002409}
2410
2411void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002412 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2413}
2414
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002415void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
2416 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
2417}
2418
2419void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002420 HBasicBlock* block = instruction->GetBlock();
2421 if (block->GetLoopInformation() != nullptr) {
2422 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
2423 // The back edge will generate the suspend check.
2424 return;
2425 }
2426 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
2427 // The goto will generate the suspend check.
2428 return;
2429 }
2430 GenerateSuspendCheck(instruction, nullptr);
2431}
2432
2433void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
2434 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002435 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002436 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002437 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002438 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002439 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002440 if (successor == nullptr) {
2441 __ j(kNotEqual, slow_path->GetEntryLabel());
2442 __ Bind(slow_path->GetReturnLabel());
2443 } else {
2444 __ j(kEqual, codegen_->GetLabelOf(successor));
2445 __ jmp(slow_path->GetEntryLabel());
2446 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002447}
2448
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002449X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
2450 return codegen_->GetAssembler();
2451}
2452
2453void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
2454 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002455 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002456 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2457 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
2458 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
2459}
2460
2461void ParallelMoveResolverX86::EmitMove(size_t index) {
2462 MoveOperands* move = moves_.Get(index);
2463 Location source = move->GetSource();
2464 Location destination = move->GetDestination();
2465
2466 if (source.IsRegister()) {
2467 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002468 __ movl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002469 } else {
2470 DCHECK(destination.IsStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002471 __ movl(Address(ESP, destination.GetStackIndex()), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002472 }
2473 } else if (source.IsStackSlot()) {
2474 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002475 __ movl(destination.As<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002476 } else {
2477 DCHECK(destination.IsStackSlot());
2478 MoveMemoryToMemory(destination.GetStackIndex(),
2479 source.GetStackIndex());
2480 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002481 } else if (source.IsConstant()) {
2482 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
2483 Immediate imm(instruction->AsIntConstant()->GetValue());
2484 if (destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002485 __ movl(destination.As<Register>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002486 } else {
2487 __ movl(Address(ESP, destination.GetStackIndex()), imm);
2488 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002489 } else {
2490 LOG(FATAL) << "Unimplemented";
2491 }
2492}
2493
2494void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002495 Register suggested_scratch = reg == EAX ? EBX : EAX;
2496 ScratchRegisterScope ensure_scratch(
2497 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2498
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002499 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
2500 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
2501 __ movl(Address(ESP, mem + stack_offset), reg);
2502 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
2503}
2504
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002505void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
2506 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002507 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
2508
2509 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002510 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002511 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
2512
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002513 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
2514 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
2515 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
2516 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
2517 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
2518 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
2519}
2520
2521void ParallelMoveResolverX86::EmitSwap(size_t index) {
2522 MoveOperands* move = moves_.Get(index);
2523 Location source = move->GetSource();
2524 Location destination = move->GetDestination();
2525
2526 if (source.IsRegister() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002527 __ xchgl(destination.As<Register>(), source.As<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002528 } else if (source.IsRegister() && destination.IsStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002529 Exchange(source.As<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002530 } else if (source.IsStackSlot() && destination.IsRegister()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002531 Exchange(destination.As<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01002532 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
2533 Exchange(destination.GetStackIndex(), source.GetStackIndex());
2534 } else {
2535 LOG(FATAL) << "Unimplemented";
2536 }
2537}
2538
2539void ParallelMoveResolverX86::SpillScratch(int reg) {
2540 __ pushl(static_cast<Register>(reg));
2541}
2542
2543void ParallelMoveResolverX86::RestoreScratch(int reg) {
2544 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002545}
2546
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002547void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002548 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
2549 ? LocationSummary::kCallOnSlowPath
2550 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002551 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002552 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002553 locations->SetOut(Location::RequiresRegister());
2554}
2555
2556void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
2557 Register out = cls->GetLocations()->Out().As<Register>();
2558 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002559 DCHECK(!cls->CanCallRuntime());
2560 DCHECK(!cls->MustGenerateClinitCheck());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002561 codegen_->LoadCurrentMethod(out);
2562 __ movl(out, Address(out, mirror::ArtMethod::DeclaringClassOffset().Int32Value()));
2563 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002564 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002565 codegen_->LoadCurrentMethod(out);
2566 __ movl(out, Address(out, mirror::ArtMethod::DexCacheResolvedTypesOffset().Int32Value()));
2567 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002568
2569 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
2570 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
2571 codegen_->AddSlowPath(slow_path);
2572 __ testl(out, out);
2573 __ j(kEqual, slow_path->GetEntryLabel());
2574 if (cls->MustGenerateClinitCheck()) {
2575 GenerateClassInitializationCheck(slow_path, out);
2576 } else {
2577 __ Bind(slow_path->GetExitLabel());
2578 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002579 }
2580}
2581
2582void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
2583 LocationSummary* locations =
2584 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2585 locations->SetInAt(0, Location::RequiresRegister());
2586 if (check->HasUses()) {
2587 locations->SetOut(Location::SameAsFirstInput());
2588 }
2589}
2590
2591void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002592 // We assume the class to not be null.
2593 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
2594 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002595 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002596 GenerateClassInitializationCheck(slow_path, check->GetLocations()->InAt(0).As<Register>());
2597}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002598
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002599void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
2600 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002601 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
2602 Immediate(mirror::Class::kStatusInitialized));
2603 __ j(kLess, slow_path->GetEntryLabel());
2604 __ Bind(slow_path->GetExitLabel());
2605 // No need for memory fence, thanks to the X86 memory model.
2606}
2607
2608void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2609 LocationSummary* locations =
2610 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2611 locations->SetInAt(0, Location::RequiresRegister());
2612 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2613}
2614
2615void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
2616 LocationSummary* locations = instruction->GetLocations();
2617 Register cls = locations->InAt(0).As<Register>();
2618 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2619
2620 switch (instruction->GetType()) {
2621 case Primitive::kPrimBoolean: {
2622 Register out = locations->Out().As<Register>();
2623 __ movzxb(out, Address(cls, offset));
2624 break;
2625 }
2626
2627 case Primitive::kPrimByte: {
2628 Register out = locations->Out().As<Register>();
2629 __ movsxb(out, Address(cls, offset));
2630 break;
2631 }
2632
2633 case Primitive::kPrimShort: {
2634 Register out = locations->Out().As<Register>();
2635 __ movsxw(out, Address(cls, offset));
2636 break;
2637 }
2638
2639 case Primitive::kPrimChar: {
2640 Register out = locations->Out().As<Register>();
2641 __ movzxw(out, Address(cls, offset));
2642 break;
2643 }
2644
2645 case Primitive::kPrimInt:
2646 case Primitive::kPrimNot: {
2647 Register out = locations->Out().As<Register>();
2648 __ movl(out, Address(cls, offset));
2649 break;
2650 }
2651
2652 case Primitive::kPrimLong: {
2653 // TODO: support volatile.
2654 __ movl(locations->Out().AsRegisterPairLow<Register>(), Address(cls, offset));
2655 __ movl(locations->Out().AsRegisterPairHigh<Register>(), Address(cls, kX86WordSize + offset));
2656 break;
2657 }
2658
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002659 case Primitive::kPrimFloat: {
2660 XmmRegister out = locations->Out().As<XmmRegister>();
2661 __ movss(out, Address(cls, offset));
2662 break;
2663 }
2664
2665 case Primitive::kPrimDouble: {
2666 XmmRegister out = locations->Out().As<XmmRegister>();
2667 __ movsd(out, Address(cls, offset));
2668 break;
2669 }
2670
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002671 case Primitive::kPrimVoid:
2672 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2673 UNREACHABLE();
2674 }
2675}
2676
2677void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2678 LocationSummary* locations =
2679 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2680 locations->SetInAt(0, Location::RequiresRegister());
2681 Primitive::Type field_type = instruction->GetFieldType();
2682 bool is_object_type = field_type == Primitive::kPrimNot;
2683 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
2684 || (field_type == Primitive::kPrimByte);
2685 // The register allocator does not support multiple
2686 // inputs that die at entry with one in a specific register.
2687 if (is_byte_type) {
2688 // Ensure the value is in a byte register.
2689 locations->SetInAt(1, Location::RegisterLocation(EAX));
2690 } else {
2691 locations->SetInAt(1, Location::RequiresRegister());
2692 }
2693 // Temporary registers for the write barrier.
2694 if (is_object_type) {
2695 locations->AddTemp(Location::RequiresRegister());
2696 // Ensure the card is in a byte register.
2697 locations->AddTemp(Location::RegisterLocation(ECX));
2698 }
2699}
2700
2701void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
2702 LocationSummary* locations = instruction->GetLocations();
2703 Register cls = locations->InAt(0).As<Register>();
2704 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
2705 Primitive::Type field_type = instruction->GetFieldType();
2706
2707 switch (field_type) {
2708 case Primitive::kPrimBoolean:
2709 case Primitive::kPrimByte: {
2710 ByteRegister value = locations->InAt(1).As<ByteRegister>();
2711 __ movb(Address(cls, offset), value);
2712 break;
2713 }
2714
2715 case Primitive::kPrimShort:
2716 case Primitive::kPrimChar: {
2717 Register value = locations->InAt(1).As<Register>();
2718 __ movw(Address(cls, offset), value);
2719 break;
2720 }
2721
2722 case Primitive::kPrimInt:
2723 case Primitive::kPrimNot: {
2724 Register value = locations->InAt(1).As<Register>();
2725 __ movl(Address(cls, offset), value);
2726
2727 if (field_type == Primitive::kPrimNot) {
2728 Register temp = locations->GetTemp(0).As<Register>();
2729 Register card = locations->GetTemp(1).As<Register>();
2730 codegen_->MarkGCCard(temp, card, cls, value);
2731 }
2732 break;
2733 }
2734
2735 case Primitive::kPrimLong: {
2736 Location value = locations->InAt(1);
2737 __ movl(Address(cls, offset), value.AsRegisterPairLow<Register>());
2738 __ movl(Address(cls, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
2739 break;
2740 }
2741
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00002742 case Primitive::kPrimFloat: {
2743 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2744 __ movss(Address(cls, offset), value);
2745 break;
2746 }
2747
2748 case Primitive::kPrimDouble: {
2749 XmmRegister value = locations->InAt(1).As<XmmRegister>();
2750 __ movsd(Address(cls, offset), value);
2751 break;
2752 }
2753
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002754 case Primitive::kPrimVoid:
2755 LOG(FATAL) << "Unreachable type " << field_type;
2756 UNREACHABLE();
2757 }
2758}
2759
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002760void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
2761 LocationSummary* locations =
2762 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2763 locations->SetOut(Location::RequiresRegister());
2764}
2765
2766void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
2767 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
2768 codegen_->AddSlowPath(slow_path);
2769
2770 Register out = load->GetLocations()->Out().As<Register>();
2771 codegen_->LoadCurrentMethod(out);
2772 __ movl(out, Address(out, mirror::ArtMethod::DexCacheStringsOffset().Int32Value()));
2773 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
2774 __ testl(out, out);
2775 __ j(kEqual, slow_path->GetEntryLabel());
2776 __ Bind(slow_path->GetExitLabel());
2777}
2778
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002779void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
2780 LocationSummary* locations =
2781 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2782 locations->SetOut(Location::RequiresRegister());
2783}
2784
2785void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
2786 Address address = Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
2787 __ fs()->movl(load->GetLocations()->Out().As<Register>(), address);
2788 __ fs()->movl(address, Immediate(0));
2789}
2790
2791void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
2792 LocationSummary* locations =
2793 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2794 InvokeRuntimeCallingConvention calling_convention;
2795 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2796}
2797
2798void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
2799 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pDeliverException)));
2800 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2801}
2802
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002803void LocationsBuilderX86::VisitTypeCheck(HTypeCheck* instruction) {
2804 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
2805 ? LocationSummary::kNoCall
2806 : LocationSummary::kCallOnSlowPath;
2807 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2808 locations->SetInAt(0, Location::RequiresRegister());
2809 locations->SetInAt(1, Location::Any());
2810 locations->SetOut(Location::RequiresRegister());
2811}
2812
2813void InstructionCodeGeneratorX86::VisitTypeCheck(HTypeCheck* instruction) {
2814 LocationSummary* locations = instruction->GetLocations();
2815 Register obj = locations->InAt(0).As<Register>();
2816 Location cls = locations->InAt(1);
2817 Register out = locations->Out().As<Register>();
2818 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2819 Label done, zero;
2820 SlowPathCodeX86* slow_path = nullptr;
2821
2822 // Return 0 if `obj` is null.
2823 // TODO: avoid this check if we know obj is not null.
2824 __ testl(obj, obj);
2825 __ j(kEqual, &zero);
2826 __ movl(out, Address(obj, class_offset));
2827 // Compare the class of `obj` with `cls`.
2828 if (cls.IsRegister()) {
2829 __ cmpl(out, cls.As<Register>());
2830 } else {
2831 DCHECK(cls.IsStackSlot()) << cls;
2832 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
2833 }
2834
2835 if (instruction->IsClassFinal()) {
2836 // Classes must be equal for the instanceof to succeed.
2837 __ j(kNotEqual, &zero);
2838 __ movl(out, Immediate(1));
2839 __ jmp(&done);
2840 } else {
2841 // If the classes are not equal, we go into a slow path.
2842 DCHECK(locations->OnlyCallsOnSlowPath());
2843 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
2844 instruction, Location::RegisterLocation(out));
2845 codegen_->AddSlowPath(slow_path);
2846 __ j(kNotEqual, slow_path->GetEntryLabel());
2847 __ movl(out, Immediate(1));
2848 __ jmp(&done);
2849 }
2850 __ Bind(&zero);
2851 __ movl(out, Immediate(0));
2852 if (slow_path != nullptr) {
2853 __ Bind(slow_path->GetExitLabel());
2854 }
2855 __ Bind(&done);
2856}
2857
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002858} // namespace x86
2859} // namespace art