blob: 1c4b40038d769b717ae5eed594bef09d8037cf6c [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"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010021#include "mirror/array.h"
22#include "mirror/art_method.h"
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +010023#include "mirror/class.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010024#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000025#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010026#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000027#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010028#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010031
32x86::X86ManagedRegister Location::AsX86() const {
33 return reg().AsX86();
34}
35
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036namespace x86 {
37
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010038static constexpr bool kExplicitStackOverflowCheck = false;
39
40static constexpr int kNumberOfPushedRegistersAtEntry = 1;
41static constexpr int kCurrentMethodStackOffset = 0;
42
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010043static Location X86CpuLocation(Register reg) {
44 return Location::RegisterLocation(X86ManagedRegister::FromCpuRegister(reg));
45}
46
47static constexpr Register kRuntimeParameterCoreRegisters[] = { EAX, ECX, EDX };
48static constexpr size_t kRuntimeParameterCoreRegistersLength =
49 arraysize(kRuntimeParameterCoreRegisters);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010050static constexpr XmmRegister kRuntimeParameterFpuRegisters[] = { };
51static constexpr size_t kRuntimeParameterFpuRegistersLength = 0;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010052
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010053class InvokeRuntimeCallingConvention : public CallingConvention<Register, XmmRegister> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010054 public:
55 InvokeRuntimeCallingConvention()
56 : CallingConvention(kRuntimeParameterCoreRegisters,
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +010057 kRuntimeParameterCoreRegistersLength,
58 kRuntimeParameterFpuRegisters,
59 kRuntimeParameterFpuRegistersLength) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010060
61 private:
62 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
63};
64
Nicolas Geoffraye5038322014-07-04 09:41:32 +010065#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
66
67class NullCheckSlowPathX86 : public SlowPathCode {
68 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010069 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010070
71 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
72 __ Bind(GetEntryLabel());
73 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010074 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010075 }
76
77 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010078 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
80};
81
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010082class StackOverflowCheckSlowPathX86 : public SlowPathCode {
83 public:
84 StackOverflowCheckSlowPathX86() {}
85
86 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
87 __ Bind(GetEntryLabel());
88 __ addl(ESP,
89 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
90 __ fs()->jmp(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowStackOverflow)));
91 }
92
93 private:
94 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86);
95};
96
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010097class BoundsCheckSlowPathX86 : public SlowPathCode {
98 public:
Roland Levillain5799fc02014-09-25 12:15:20 +010099 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
100 Location index_location,
101 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +0100102 : instruction_(instruction), index_location_(index_location), length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100103
104 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
105 CodeGeneratorX86* x86_codegen = reinterpret_cast<CodeGeneratorX86*>(codegen);
106 __ Bind(GetEntryLabel());
107 InvokeRuntimeCallingConvention calling_convention;
108 x86_codegen->Move32(X86CpuLocation(calling_convention.GetRegisterAt(0)), index_location_);
109 x86_codegen->Move32(X86CpuLocation(calling_convention.GetRegisterAt(1)), length_location_);
110 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100111 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100112 }
113
114 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100115 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100116 const Location index_location_;
117 const Location length_location_;
118
119 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
120};
121
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000122class SuspendCheckSlowPathX86 : public SlowPathCode {
123 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100124 explicit SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
125 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000126
127 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
128 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100129 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000130 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
131 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100132 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100133 if (successor_ == nullptr) {
134 __ jmp(GetReturnLabel());
135 } else {
136 __ jmp(codegen->GetLabelOf(successor_));
137 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000138 }
139
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100140 Label* GetReturnLabel() {
141 DCHECK(successor_ == nullptr);
142 return &return_label_;
143 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000144
145 private:
146 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100147 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000148 Label return_label_;
149
150 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
151};
152
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100153#undef __
154#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
155
Dave Allison20dfc792014-06-16 20:44:29 -0700156inline Condition X86Condition(IfCondition cond) {
157 switch (cond) {
158 case kCondEQ: return kEqual;
159 case kCondNE: return kNotEqual;
160 case kCondLT: return kLess;
161 case kCondLE: return kLessEqual;
162 case kCondGT: return kGreater;
163 case kCondGE: return kGreaterEqual;
164 default:
165 LOG(FATAL) << "Unknown if condition";
166 }
167 return kEqual;
168}
169
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100170void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
171 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
172}
173
174void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
175 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
176}
177
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100178void CodeGeneratorX86::SaveCoreRegister(Location stack_location, uint32_t reg_id) {
179 __ movl(Address(ESP, stack_location.GetStackIndex()), static_cast<Register>(reg_id));
180}
181
182void CodeGeneratorX86::RestoreCoreRegister(Location stack_location, uint32_t reg_id) {
183 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_location.GetStackIndex()));
184}
185
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100186CodeGeneratorX86::CodeGeneratorX86(HGraph* graph)
187 : CodeGenerator(graph, kNumberOfRegIds),
188 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100189 instruction_visitor_(graph, this),
190 move_resolver_(graph->GetArena(), this) {}
191
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100192size_t CodeGeneratorX86::FrameEntrySpillSize() const {
193 return kNumberOfPushedRegistersAtEntry * kX86WordSize;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100194}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100195
196static bool* GetBlockedRegisterPairs(bool* blocked_registers) {
197 return blocked_registers + kNumberOfAllocIds;
198}
199
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100200static bool* GetBlockedXmmRegisters(bool* blocked_registers) {
201 return blocked_registers + kNumberOfCpuRegisters;
202}
203
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100204ManagedRegister CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type,
205 bool* blocked_registers) const {
206 switch (type) {
207 case Primitive::kPrimLong: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100208 bool* blocked_register_pairs = GetBlockedRegisterPairs(blocked_registers);
209 size_t reg = AllocateFreeRegisterInternal(blocked_register_pairs, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100210 X86ManagedRegister pair =
211 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
212 blocked_registers[pair.AsRegisterPairLow()] = true;
213 blocked_registers[pair.AsRegisterPairHigh()] = true;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100214 // Block all other register pairs that share a register with `pair`.
215 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
216 X86ManagedRegister current =
217 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
218 if (current.AsRegisterPairLow() == pair.AsRegisterPairLow()
219 || current.AsRegisterPairLow() == pair.AsRegisterPairHigh()
220 || current.AsRegisterPairHigh() == pair.AsRegisterPairLow()
221 || current.AsRegisterPairHigh() == pair.AsRegisterPairHigh()) {
222 blocked_register_pairs[i] = true;
223 }
224 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100225 return pair;
226 }
227
228 case Primitive::kPrimByte:
229 case Primitive::kPrimBoolean:
230 case Primitive::kPrimChar:
231 case Primitive::kPrimShort:
232 case Primitive::kPrimInt:
233 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100234 Register reg = static_cast<Register>(
235 AllocateFreeRegisterInternal(blocked_registers, kNumberOfCpuRegisters));
236 // Block all register pairs that contain `reg`.
237 bool* blocked_register_pairs = GetBlockedRegisterPairs(blocked_registers);
238 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
239 X86ManagedRegister current =
240 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
241 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
242 blocked_register_pairs[i] = true;
243 }
244 }
245 return X86ManagedRegister::FromCpuRegister(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100246 }
247
248 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100249 case Primitive::kPrimDouble: {
250 XmmRegister reg = static_cast<XmmRegister>(AllocateFreeRegisterInternal(
251 GetBlockedXmmRegisters(blocked_registers), kNumberOfXmmRegisters));
252 return X86ManagedRegister::FromXmmRegister(reg);
253 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100254
255 case Primitive::kPrimVoid:
256 LOG(FATAL) << "Unreachable type " << type;
257 }
258
259 return ManagedRegister::NoRegister();
260}
261
262void CodeGeneratorX86::SetupBlockedRegisters(bool* blocked_registers) const {
263 bool* blocked_register_pairs = GetBlockedRegisterPairs(blocked_registers);
264
265 // Don't allocate the dalvik style register pair passing.
266 blocked_register_pairs[ECX_EDX] = true;
267
268 // Stack register is always reserved.
269 blocked_registers[ESP] = true;
270
271 // TODO: We currently don't use Quick's callee saved registers.
272 blocked_registers[EBP] = true;
273 blocked_registers[ESI] = true;
274 blocked_registers[EDI] = true;
275 blocked_register_pairs[EAX_EDI] = true;
276 blocked_register_pairs[EDX_EDI] = true;
277 blocked_register_pairs[ECX_EDI] = true;
278 blocked_register_pairs[EBX_EDI] = true;
279}
280
281size_t CodeGeneratorX86::GetNumberOfRegisters() const {
282 return kNumberOfRegIds;
283}
284
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100285InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
286 : HGraphVisitor(graph),
287 assembler_(codegen->GetAssembler()),
288 codegen_(codegen) {}
289
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000290void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000291 // Create a fake register to mimic Quick.
292 static const int kFakeReturnRegister = 8;
293 core_spill_mask_ |= (1 << kFakeReturnRegister);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000294
Dave Allison648d7112014-07-25 16:15:27 -0700295 bool skip_overflow_check = IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100296 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
297 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100298 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100299 }
300
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100301 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100302 __ subl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100303
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100304 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
305 SlowPathCode* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86();
306 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100307
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100308 __ fs()->cmpl(ESP, Address::Absolute(Thread::StackEndOffset<kX86WordSize>()));
309 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100310 }
311
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100312 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000313}
314
315void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100316 __ addl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000317}
318
319void CodeGeneratorX86::Bind(Label* label) {
320 __ Bind(label);
321}
322
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000323void InstructionCodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100324 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000325}
326
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100327Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
328 switch (load->GetType()) {
329 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100330 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100331 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
332 break;
333
334 case Primitive::kPrimInt:
335 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100336 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100337 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100338
339 case Primitive::kPrimBoolean:
340 case Primitive::kPrimByte:
341 case Primitive::kPrimChar:
342 case Primitive::kPrimShort:
343 case Primitive::kPrimVoid:
344 LOG(FATAL) << "Unexpected type " << load->GetType();
345 }
346
347 LOG(FATAL) << "Unreachable";
348 return Location();
349}
350
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100351Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
352 switch (type) {
353 case Primitive::kPrimBoolean:
354 case Primitive::kPrimByte:
355 case Primitive::kPrimChar:
356 case Primitive::kPrimShort:
357 case Primitive::kPrimInt:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100358 case Primitive::kPrimFloat:
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100359 case Primitive::kPrimNot: {
360 uint32_t index = gp_index_++;
361 if (index < calling_convention.GetNumberOfRegisters()) {
362 return X86CpuLocation(calling_convention.GetRegisterAt(index));
363 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100364 return Location::StackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100365 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100366 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100367
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100368 case Primitive::kPrimLong:
369 case Primitive::kPrimDouble: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100370 uint32_t index = gp_index_;
371 gp_index_ += 2;
372 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
373 return Location::RegisterLocation(X86ManagedRegister::FromRegisterPair(
374 calling_convention.GetRegisterPairAt(index)));
375 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
376 return Location::QuickParameter(index);
377 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100378 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100379 }
380 }
381
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100382 case Primitive::kPrimVoid:
383 LOG(FATAL) << "Unexpected parameter type " << type;
384 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100385 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100386 return Location();
387}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100388
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100389void CodeGeneratorX86::Move32(Location destination, Location source) {
390 if (source.Equals(destination)) {
391 return;
392 }
393 if (destination.IsRegister()) {
394 if (source.IsRegister()) {
395 __ movl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100396 } else if (source.IsFpuRegister()) {
397 __ movd(destination.AsX86().AsCpuRegister(), source.AsX86().AsXmmRegister());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100398 } else {
399 DCHECK(source.IsStackSlot());
400 __ movl(destination.AsX86().AsCpuRegister(), Address(ESP, source.GetStackIndex()));
401 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100402 } else if (destination.IsFpuRegister()) {
403 if (source.IsRegister()) {
404 __ movd(destination.AsX86().AsXmmRegister(), source.AsX86().AsCpuRegister());
405 } else if (source.IsFpuRegister()) {
406 __ movaps(destination.AsX86().AsXmmRegister(), source.AsX86().AsXmmRegister());
407 } else {
408 DCHECK(source.IsStackSlot());
409 __ movss(destination.AsX86().AsXmmRegister(), Address(ESP, source.GetStackIndex()));
410 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100411 } else {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100412 DCHECK(destination.IsStackSlot());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100413 if (source.IsRegister()) {
414 __ movl(Address(ESP, destination.GetStackIndex()), source.AsX86().AsCpuRegister());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100415 } else if (source.IsFpuRegister()) {
416 __ movss(Address(ESP, destination.GetStackIndex()), source.AsX86().AsXmmRegister());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100417 } else {
418 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100419 __ pushl(Address(ESP, source.GetStackIndex()));
420 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100421 }
422 }
423}
424
425void CodeGeneratorX86::Move64(Location destination, Location source) {
426 if (source.Equals(destination)) {
427 return;
428 }
429 if (destination.IsRegister()) {
430 if (source.IsRegister()) {
431 __ movl(destination.AsX86().AsRegisterPairLow(), source.AsX86().AsRegisterPairLow());
432 __ movl(destination.AsX86().AsRegisterPairHigh(), source.AsX86().AsRegisterPairHigh());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100433 } else if (source.IsFpuRegister()) {
434 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100435 } else if (source.IsQuickParameter()) {
436 uint32_t argument_index = source.GetQuickParameterIndex();
437 InvokeDexCallingConvention calling_convention;
438 __ movl(destination.AsX86().AsRegisterPairLow(),
439 calling_convention.GetRegisterAt(argument_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100440 __ movl(destination.AsX86().AsRegisterPairHigh(), Address(ESP,
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100441 calling_convention.GetStackOffsetOf(argument_index + 1) + GetFrameSize()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100442 } else {
443 DCHECK(source.IsDoubleStackSlot());
444 __ movl(destination.AsX86().AsRegisterPairLow(), Address(ESP, source.GetStackIndex()));
445 __ movl(destination.AsX86().AsRegisterPairHigh(),
446 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
447 }
448 } else if (destination.IsQuickParameter()) {
449 InvokeDexCallingConvention calling_convention;
450 uint32_t argument_index = destination.GetQuickParameterIndex();
451 if (source.IsRegister()) {
452 __ movl(calling_convention.GetRegisterAt(argument_index), source.AsX86().AsRegisterPairLow());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100453 __ movl(Address(ESP, calling_convention.GetStackOffsetOf(argument_index + 1)),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100454 source.AsX86().AsRegisterPairHigh());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100455 } else if (source.IsFpuRegister()) {
456 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100457 } else {
458 DCHECK(source.IsDoubleStackSlot());
459 __ movl(calling_convention.GetRegisterAt(argument_index),
460 Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100461 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100462 __ popl(Address(ESP, calling_convention.GetStackOffsetOf(argument_index + 1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100463 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100464 } else if (destination.IsFpuRegister()) {
465 if (source.IsDoubleStackSlot()) {
466 __ movsd(destination.AsX86().AsXmmRegister(), Address(ESP, source.GetStackIndex()));
467 } else {
468 LOG(FATAL) << "Unimplemented";
469 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100470 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100471 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100472 if (source.IsRegister()) {
473 __ movl(Address(ESP, destination.GetStackIndex()), source.AsX86().AsRegisterPairLow());
474 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
475 source.AsX86().AsRegisterPairHigh());
476 } else if (source.IsQuickParameter()) {
477 InvokeDexCallingConvention calling_convention;
478 uint32_t argument_index = source.GetQuickParameterIndex();
479 __ movl(Address(ESP, destination.GetStackIndex()),
480 calling_convention.GetRegisterAt(argument_index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100481 DCHECK_EQ(calling_convention.GetStackOffsetOf(argument_index + 1) + GetFrameSize(),
482 static_cast<size_t>(destination.GetHighStackIndex(kX86WordSize)));
483 } else if (source.IsFpuRegister()) {
484 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsX86().AsXmmRegister());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100485 } else {
486 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100487 __ pushl(Address(ESP, source.GetStackIndex()));
488 __ popl(Address(ESP, destination.GetStackIndex()));
489 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
490 __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100491 }
492 }
493}
494
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100495void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
496 if (instruction->AsIntConstant() != nullptr) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100497 Immediate imm(instruction->AsIntConstant()->GetValue());
498 if (location.IsRegister()) {
499 __ movl(location.AsX86().AsCpuRegister(), imm);
500 } else {
501 __ movl(Address(ESP, location.GetStackIndex()), imm);
502 }
503 } else if (instruction->AsLongConstant() != nullptr) {
504 int64_t value = instruction->AsLongConstant()->GetValue();
505 if (location.IsRegister()) {
506 __ movl(location.AsX86().AsRegisterPairLow(), Immediate(Low32Bits(value)));
507 __ movl(location.AsX86().AsRegisterPairHigh(), Immediate(High32Bits(value)));
508 } else {
509 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
510 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
511 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100512 } else if (instruction->AsLoadLocal() != nullptr) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100513 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100514 switch (instruction->GetType()) {
515 case Primitive::kPrimBoolean:
516 case Primitive::kPrimByte:
517 case Primitive::kPrimChar:
518 case Primitive::kPrimShort:
519 case Primitive::kPrimInt:
520 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100521 case Primitive::kPrimFloat:
522 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100523 break;
524
525 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100526 case Primitive::kPrimDouble:
527 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100528 break;
529
530 default:
531 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
532 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000533 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100534 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100535 switch (instruction->GetType()) {
536 case Primitive::kPrimBoolean:
537 case Primitive::kPrimByte:
538 case Primitive::kPrimChar:
539 case Primitive::kPrimShort:
540 case Primitive::kPrimInt:
541 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100542 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100543 Move32(location, instruction->GetLocations()->Out());
544 break;
545
546 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100547 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100548 Move64(location, instruction->GetLocations()->Out());
549 break;
550
551 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100552 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100553 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000554 }
555}
556
557void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000558 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000559}
560
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000561void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000562 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100563 DCHECK(!successor->IsExitBlock());
564
565 HBasicBlock* block = got->GetBlock();
566 HInstruction* previous = got->GetPrevious();
567
568 HLoopInformation* info = block->GetLoopInformation();
569 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
570 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
571 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
572 return;
573 }
574
575 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
576 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
577 }
578 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000579 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000580 }
581}
582
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000583void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000584 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000585}
586
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000587void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000588 if (kIsDebugBuild) {
589 __ Comment("Unreachable");
590 __ int3();
591 }
592}
593
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000594void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100595 LocationSummary* locations =
596 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100597 HInstruction* cond = if_instr->InputAt(0);
598 DCHECK(cond->IsCondition());
599 HCondition* condition = cond->AsCondition();
600 if (condition->NeedsMaterialization()) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +0100601 locations->SetInAt(0, Location::Any(), Location::kDiesAtEntry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100602 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000603}
604
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000605void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700606 HInstruction* cond = if_instr->InputAt(0);
607 DCHECK(cond->IsCondition());
608 HCondition* condition = cond->AsCondition();
609 if (condition->NeedsMaterialization()) {
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100610 // Moves do not affect the eflags register, so if the condition is evaluated
611 // just before the if, we don't need to evaluate it again.
612 if (!condition->IsBeforeWhenDisregardMoves(if_instr)) {
613 // Materialized condition, compare against 0
614 Location lhs = if_instr->GetLocations()->InAt(0);
615 if (lhs.IsRegister()) {
616 __ cmpl(lhs.AsX86().AsCpuRegister(), Immediate(0));
617 } else {
618 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
619 }
Dave Allison20dfc792014-06-16 20:44:29 -0700620 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100621 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100622 } else {
Dave Allison20dfc792014-06-16 20:44:29 -0700623 Location lhs = condition->GetLocations()->InAt(0);
624 Location rhs = condition->GetLocations()->InAt(1);
625 // LHS is guaranteed to be in a register (see LocationsBuilderX86::VisitCondition).
626 if (rhs.IsRegister()) {
627 __ cmpl(lhs.AsX86().AsCpuRegister(), rhs.AsX86().AsCpuRegister());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100628 } else if (rhs.IsConstant()) {
629 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
630 Immediate imm(instruction->AsIntConstant()->GetValue());
631 __ cmpl(lhs.AsX86().AsCpuRegister(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700632 } else {
633 __ cmpl(lhs.AsX86().AsCpuRegister(), Address(ESP, rhs.GetStackIndex()));
634 }
635 __ j(X86Condition(condition->GetCondition()),
636 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100637 }
Dave Allison20dfc792014-06-16 20:44:29 -0700638 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfFalseSuccessor())) {
639 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000640 }
641}
642
643void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000644 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000645}
646
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000647void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
648 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000649}
650
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000651void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100652 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000653}
654
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000655void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100656 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000657}
658
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100659void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100660 LocationSummary* locations =
661 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100662 switch (store->InputAt(1)->GetType()) {
663 case Primitive::kPrimBoolean:
664 case Primitive::kPrimByte:
665 case Primitive::kPrimChar:
666 case Primitive::kPrimShort:
667 case Primitive::kPrimInt:
668 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100669 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100670 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
671 break;
672
673 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100674 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100675 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
676 break;
677
678 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100679 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100680 }
681 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000682}
683
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000684void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000685}
686
Dave Allison20dfc792014-06-16 20:44:29 -0700687void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100688 LocationSummary* locations =
689 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +0100690 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
691 locations->SetInAt(1, Location::Any(), Location::kDiesAtEntry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100692 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100693 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100694 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000695}
696
Dave Allison20dfc792014-06-16 20:44:29 -0700697void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
698 if (comp->NeedsMaterialization()) {
699 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100700 Register reg = locations->Out().AsX86().AsCpuRegister();
701 // Clear register: setcc only sets the low byte.
702 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700703 if (locations->InAt(1).IsRegister()) {
704 __ cmpl(locations->InAt(0).AsX86().AsCpuRegister(),
705 locations->InAt(1).AsX86().AsCpuRegister());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100706 } else if (locations->InAt(1).IsConstant()) {
707 HConstant* instruction = locations->InAt(1).GetConstant();
708 Immediate imm(instruction->AsIntConstant()->GetValue());
709 __ cmpl(locations->InAt(0).AsX86().AsCpuRegister(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700710 } else {
711 __ cmpl(locations->InAt(0).AsX86().AsCpuRegister(),
712 Address(ESP, locations->InAt(1).GetStackIndex()));
713 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100714 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100715 }
Dave Allison20dfc792014-06-16 20:44:29 -0700716}
717
718void LocationsBuilderX86::VisitEqual(HEqual* comp) {
719 VisitCondition(comp);
720}
721
722void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
723 VisitCondition(comp);
724}
725
726void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
727 VisitCondition(comp);
728}
729
730void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
731 VisitCondition(comp);
732}
733
734void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
735 VisitCondition(comp);
736}
737
738void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
739 VisitCondition(comp);
740}
741
742void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
743 VisitCondition(comp);
744}
745
746void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
747 VisitCondition(comp);
748}
749
750void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
751 VisitCondition(comp);
752}
753
754void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
755 VisitCondition(comp);
756}
757
758void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
759 VisitCondition(comp);
760}
761
762void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
763 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000764}
765
766void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100767 LocationSummary* locations =
768 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100769 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000770}
771
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000772void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000773}
774
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100775void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100776 LocationSummary* locations =
777 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100778 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100779}
780
781void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
782 // Will be generated at use site.
783}
784
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000785void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000786 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000787}
788
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000789void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
790 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000791 __ ret();
792}
793
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000794void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100795 LocationSummary* locations =
796 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100797 switch (ret->InputAt(0)->GetType()) {
798 case Primitive::kPrimBoolean:
799 case Primitive::kPrimByte:
800 case Primitive::kPrimChar:
801 case Primitive::kPrimShort:
802 case Primitive::kPrimInt:
803 case Primitive::kPrimNot:
804 locations->SetInAt(0, X86CpuLocation(EAX));
805 break;
806
807 case Primitive::kPrimLong:
808 locations->SetInAt(
809 0, Location::RegisterLocation(X86ManagedRegister::FromRegisterPair(EAX_EDX)));
810 break;
811
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100812 case Primitive::kPrimFloat:
813 case Primitive::kPrimDouble:
814 locations->SetInAt(
815 0, Location::FpuRegisterLocation(X86ManagedRegister::FromXmmRegister(XMM0)));
816 break;
817
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100818 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100819 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100820 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000821}
822
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000823void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100824 if (kIsDebugBuild) {
825 switch (ret->InputAt(0)->GetType()) {
826 case Primitive::kPrimBoolean:
827 case Primitive::kPrimByte:
828 case Primitive::kPrimChar:
829 case Primitive::kPrimShort:
830 case Primitive::kPrimInt:
831 case Primitive::kPrimNot:
832 DCHECK_EQ(ret->GetLocations()->InAt(0).AsX86().AsCpuRegister(), EAX);
833 break;
834
835 case Primitive::kPrimLong:
836 DCHECK_EQ(ret->GetLocations()->InAt(0).AsX86().AsRegisterPair(), EAX_EDX);
837 break;
838
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100839 case Primitive::kPrimFloat:
840 case Primitive::kPrimDouble:
841 DCHECK_EQ(ret->GetLocations()->InAt(0).AsX86().AsXmmRegister(), XMM0);
842 break;
843
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100844 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100845 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100846 }
847 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000848 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000849 __ ret();
850}
851
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000852void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100853 HandleInvoke(invoke);
854}
855
856void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
857 Register temp = invoke->GetLocations()->GetTemp(0).AsX86().AsCpuRegister();
858 uint32_t heap_reference_size = sizeof(mirror::HeapReference<mirror::Object>);
859 size_t index_in_cache = mirror::Array::DataOffset(heap_reference_size).Int32Value() +
860 invoke->GetIndexInDexCache() * kX86WordSize;
861
862 // TODO: Implement all kinds of calls:
863 // 1) boot -> boot
864 // 2) app -> boot
865 // 3) app -> app
866 //
867 // Currently we implement the app -> app logic, which looks up in the resolve cache.
868
869 // temp = method;
870 LoadCurrentMethod(temp);
871 // temp = temp->dex_cache_resolved_methods_;
872 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
873 // temp = temp[index_in_cache]
874 __ movl(temp, Address(temp, index_in_cache));
875 // (temp + offset_of_quick_compiled_code)()
876 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
877
878 DCHECK(!codegen_->IsLeafMethod());
879 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
880}
881
882void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
883 HandleInvoke(invoke);
884}
885
886void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100887 LocationSummary* locations =
888 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100889 locations->AddTemp(X86CpuLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100890
891 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100892 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100893 HInstruction* input = invoke->InputAt(i);
894 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
895 }
896
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100897 switch (invoke->GetType()) {
898 case Primitive::kPrimBoolean:
899 case Primitive::kPrimByte:
900 case Primitive::kPrimChar:
901 case Primitive::kPrimShort:
902 case Primitive::kPrimInt:
903 case Primitive::kPrimNot:
904 locations->SetOut(X86CpuLocation(EAX));
905 break;
906
907 case Primitive::kPrimLong:
908 locations->SetOut(Location::RegisterLocation(X86ManagedRegister::FromRegisterPair(EAX_EDX)));
909 break;
910
911 case Primitive::kPrimVoid:
912 break;
913
914 case Primitive::kPrimDouble:
915 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100916 locations->SetOut(Location::FpuRegisterLocation(X86ManagedRegister::FromXmmRegister(XMM0)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100917 break;
918 }
919
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000920 invoke->SetLocations(locations);
921}
922
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100923void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100924 Register temp = invoke->GetLocations()->GetTemp(0).AsX86().AsCpuRegister();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100925 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
926 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
927 LocationSummary* locations = invoke->GetLocations();
928 Location receiver = locations->InAt(0);
929 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
930 // temp = object->GetClass();
931 if (receiver.IsStackSlot()) {
932 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
933 __ movl(temp, Address(temp, class_offset));
934 } else {
935 __ movl(temp, Address(receiver.AsX86().AsCpuRegister(), class_offset));
936 }
937 // temp = temp->GetMethodAt(method_offset);
938 __ movl(temp, Address(temp, method_offset));
939 // call temp->GetEntryPoint();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000940 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
941
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100942 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100943 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000944}
945
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000946void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100947 LocationSummary* locations =
948 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000949 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100950 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100951 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100952 locations->SetInAt(0, Location::RequiresRegister());
953 locations->SetInAt(1, Location::Any());
954 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100955 break;
956 }
957
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100958 case Primitive::kPrimFloat:
959 case Primitive::kPrimDouble: {
960 locations->SetInAt(0, Location::RequiresFpuRegister());
961 locations->SetInAt(1, Location::Any());
962 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100963 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100964 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100965
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000966 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100967 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
968 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000969 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000970}
971
972void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
973 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100974 Location first = locations->InAt(0);
975 Location second = locations->InAt(1);
976
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000977 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100978 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100979 DCHECK_EQ(first.AsX86().AsCpuRegister(), locations->Out().AsX86().AsCpuRegister());
980 if (second.IsRegister()) {
981 __ addl(first.AsX86().AsCpuRegister(), second.AsX86().AsCpuRegister());
982 } else if (second.IsConstant()) {
983 HConstant* instruction = second.GetConstant();
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100984 Immediate imm(instruction->AsIntConstant()->GetValue());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100985 __ addl(first.AsX86().AsCpuRegister(), imm);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100986 } else {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100987 __ addl(first.AsX86().AsCpuRegister(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100988 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000989 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100990 }
991
992 case Primitive::kPrimLong: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100993 DCHECK_EQ(first.AsX86().AsRegisterPair(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100994 locations->Out().AsX86().AsRegisterPair());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100995 if (second.IsRegister()) {
996 __ addl(first.AsX86().AsRegisterPairLow(), second.AsX86().AsRegisterPairLow());
997 __ adcl(first.AsX86().AsRegisterPairHigh(), second.AsX86().AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100998 } else {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100999 __ addl(first.AsX86().AsRegisterPairLow(), Address(ESP, second.GetStackIndex()));
1000 __ adcl(first.AsX86().AsRegisterPairHigh(),
1001 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001002 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001003 break;
1004 }
1005
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001006 case Primitive::kPrimFloat: {
1007 if (second.IsFpuRegister()) {
1008 __ addss(first.AsX86().AsXmmRegister(), second.AsX86().AsXmmRegister());
1009 } else {
1010 __ addss(first.AsX86().AsXmmRegister(), Address(ESP, second.GetStackIndex()));
1011 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001012 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001013 }
1014
1015 case Primitive::kPrimDouble: {
1016 if (second.IsFpuRegister()) {
1017 __ addsd(first.AsX86().AsXmmRegister(), second.AsX86().AsXmmRegister());
1018 } else {
1019 __ addsd(first.AsX86().AsXmmRegister(), Address(ESP, second.GetStackIndex()));
1020 }
1021 break;
1022 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001023
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001024 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001025 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001026 }
1027}
1028
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001029void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001030 LocationSummary* locations =
1031 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001032 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001033 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001034 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001035 locations->SetInAt(0, Location::RequiresRegister());
1036 locations->SetInAt(1, Location::Any());
1037 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001038 break;
1039 }
1040
1041 case Primitive::kPrimBoolean:
1042 case Primitive::kPrimByte:
1043 case Primitive::kPrimChar:
1044 case Primitive::kPrimShort:
1045 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
1046 break;
1047
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001048 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001049 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001050 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001051}
1052
1053void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
1054 LocationSummary* locations = sub->GetLocations();
1055 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001056 case Primitive::kPrimInt: {
1057 DCHECK_EQ(locations->InAt(0).AsX86().AsCpuRegister(),
1058 locations->Out().AsX86().AsCpuRegister());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001059 if (locations->InAt(1).IsRegister()) {
1060 __ subl(locations->InAt(0).AsX86().AsCpuRegister(),
1061 locations->InAt(1).AsX86().AsCpuRegister());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001062 } else if (locations->InAt(1).IsConstant()) {
1063 HConstant* instruction = locations->InAt(1).GetConstant();
1064 Immediate imm(instruction->AsIntConstant()->GetValue());
1065 __ subl(locations->InAt(0).AsX86().AsCpuRegister(), imm);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001066 } else {
1067 __ subl(locations->InAt(0).AsX86().AsCpuRegister(),
1068 Address(ESP, locations->InAt(1).GetStackIndex()));
1069 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001070 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001071 }
1072
1073 case Primitive::kPrimLong: {
1074 DCHECK_EQ(locations->InAt(0).AsX86().AsRegisterPair(),
1075 locations->Out().AsX86().AsRegisterPair());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001076 if (locations->InAt(1).IsRegister()) {
1077 __ subl(locations->InAt(0).AsX86().AsRegisterPairLow(),
1078 locations->InAt(1).AsX86().AsRegisterPairLow());
1079 __ sbbl(locations->InAt(0).AsX86().AsRegisterPairHigh(),
1080 locations->InAt(1).AsX86().AsRegisterPairHigh());
1081 } else {
1082 __ subl(locations->InAt(0).AsX86().AsRegisterPairLow(),
1083 Address(ESP, locations->InAt(1).GetStackIndex()));
1084 __ sbbl(locations->InAt(0).AsX86().AsRegisterPairHigh(),
1085 Address(ESP, locations->InAt(1).GetHighStackIndex(kX86WordSize)));
1086 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001087 break;
1088 }
1089
1090 case Primitive::kPrimBoolean:
1091 case Primitive::kPrimByte:
1092 case Primitive::kPrimChar:
1093 case Primitive::kPrimShort:
1094 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
1095 break;
1096
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001097 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001098 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001099 }
1100}
1101
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001102void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001103 LocationSummary* locations =
1104 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001105 locations->SetOut(X86CpuLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001106 InvokeRuntimeCallingConvention calling_convention;
1107 locations->AddTemp(X86CpuLocation(calling_convention.GetRegisterAt(0)));
1108 locations->AddTemp(X86CpuLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001109}
1110
1111void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
1112 InvokeRuntimeCallingConvention calling_convention;
1113 LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001114 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001115
Nicolas Geoffray707c8092014-04-04 10:50:14 +01001116 __ fs()->call(
1117 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001118
Nicolas Geoffray39468442014-09-02 15:17:15 +01001119 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001120 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001121}
1122
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001123void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001124 LocationSummary* locations =
1125 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001126 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1127 if (location.IsStackSlot()) {
1128 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1129 } else if (location.IsDoubleStackSlot()) {
1130 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001131 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001132 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001133}
1134
1135void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001136}
1137
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001138void LocationsBuilderX86::VisitNot(HNot* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001139 LocationSummary* locations =
1140 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001141 locations->SetInAt(0, Location::RequiresRegister());
1142 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001143}
1144
1145void InstructionCodeGeneratorX86::VisitNot(HNot* instruction) {
1146 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001147 Location out = locations->Out();
1148 DCHECK_EQ(locations->InAt(0).AsX86().AsCpuRegister(), out.AsX86().AsCpuRegister());
1149 __ xorl(out.AsX86().AsCpuRegister(), Immediate(1));
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001150}
1151
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001152void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001153 LocationSummary* locations =
1154 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001155 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
1156 locations->SetInAt(1, Location::Any(), Location::kDiesAtEntry);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001157 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001158}
1159
1160void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
1161 Label greater, done;
1162 LocationSummary* locations = compare->GetLocations();
1163 switch (compare->InputAt(0)->GetType()) {
1164 case Primitive::kPrimLong: {
1165 Label less, greater, done;
1166 Register output = locations->Out().AsX86().AsCpuRegister();
1167 X86ManagedRegister left = locations->InAt(0).AsX86();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001168 Location right = locations->InAt(1);
1169 if (right.IsRegister()) {
1170 __ cmpl(left.AsRegisterPairHigh(), right.AsX86().AsRegisterPairHigh());
1171 } else {
1172 DCHECK(right.IsDoubleStackSlot());
1173 __ cmpl(left.AsRegisterPairHigh(), Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1174 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001175 __ j(kLess, &less); // Signed compare.
1176 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001177 if (right.IsRegister()) {
1178 __ cmpl(left.AsRegisterPairLow(), right.AsX86().AsRegisterPairLow());
1179 } else {
1180 DCHECK(right.IsDoubleStackSlot());
1181 __ cmpl(left.AsRegisterPairLow(), Address(ESP, right.GetStackIndex()));
1182 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001183 __ movl(output, Immediate(0));
1184 __ j(kEqual, &done);
1185 __ j(kBelow, &less); // Unsigned compare.
1186
1187 __ Bind(&greater);
1188 __ movl(output, Immediate(1));
1189 __ jmp(&done);
1190
1191 __ Bind(&less);
1192 __ movl(output, Immediate(-1));
1193
1194 __ Bind(&done);
1195 break;
1196 }
1197 default:
1198 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
1199 }
1200}
1201
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001202void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001203 LocationSummary* locations =
1204 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001205 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1206 locations->SetInAt(i, Location::Any());
1207 }
1208 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001209}
1210
1211void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001212 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001213}
1214
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001215void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001216 LocationSummary* locations =
1217 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001218 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001219 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001220 bool is_object_type = field_type == Primitive::kPrimNot;
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001221 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
1222 || (field_type == Primitive::kPrimByte);
1223 // The register allocator does not support multiple
1224 // inputs that die at entry with one in a specific register.
1225 bool dies_at_entry = !is_object_type && !is_byte_type;
1226 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001227 // Ensure the value is in a byte register.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001228 locations->SetInAt(1, X86CpuLocation(EAX), dies_at_entry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001229 } else {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001230 locations->SetInAt(1, Location::RequiresRegister(), dies_at_entry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001231 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001232 // Temporary registers for the write barrier.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001233 if (is_object_type) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001234 locations->AddTemp(Location::RequiresRegister());
1235 // Ensure the card is in a byte register.
1236 locations->AddTemp(X86CpuLocation(ECX));
1237 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001238}
1239
1240void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1241 LocationSummary* locations = instruction->GetLocations();
1242 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1243 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001244 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001245
1246 switch (field_type) {
1247 case Primitive::kPrimBoolean:
1248 case Primitive::kPrimByte: {
1249 ByteRegister value = locations->InAt(1).AsX86().AsByteRegister();
1250 __ movb(Address(obj, offset), value);
1251 break;
1252 }
1253
1254 case Primitive::kPrimShort:
1255 case Primitive::kPrimChar: {
1256 Register value = locations->InAt(1).AsX86().AsCpuRegister();
1257 __ movw(Address(obj, offset), value);
1258 break;
1259 }
1260
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001261 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001262 case Primitive::kPrimNot: {
1263 Register value = locations->InAt(1).AsX86().AsCpuRegister();
1264 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001265
1266 if (field_type == Primitive::kPrimNot) {
1267 Register temp = locations->GetTemp(0).AsX86().AsCpuRegister();
1268 Register card = locations->GetTemp(1).AsX86().AsCpuRegister();
1269 codegen_->MarkGCCard(temp, card, obj, value);
1270 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001271 break;
1272 }
1273
1274 case Primitive::kPrimLong: {
1275 X86ManagedRegister value = locations->InAt(1).AsX86();
1276 __ movl(Address(obj, offset), value.AsRegisterPairLow());
1277 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh());
1278 break;
1279 }
1280
1281 case Primitive::kPrimFloat:
1282 case Primitive::kPrimDouble:
1283 LOG(FATAL) << "Unimplemented register type " << field_type;
1284
1285 case Primitive::kPrimVoid:
1286 LOG(FATAL) << "Unreachable type " << field_type;
1287 }
1288}
1289
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001290void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
1291 Label is_null;
1292 __ testl(value, value);
1293 __ j(kEqual, &is_null);
1294 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
1295 __ movl(temp, object);
1296 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
1297 __ movb(Address(temp, card, TIMES_1, 0),
1298 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
1299 __ Bind(&is_null);
1300}
1301
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001302void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001303 LocationSummary* locations =
1304 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001305 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001306 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001307}
1308
1309void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1310 LocationSummary* locations = instruction->GetLocations();
1311 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1312 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
1313
1314 switch (instruction->GetType()) {
1315 case Primitive::kPrimBoolean: {
1316 Register out = locations->Out().AsX86().AsCpuRegister();
1317 __ movzxb(out, Address(obj, offset));
1318 break;
1319 }
1320
1321 case Primitive::kPrimByte: {
1322 Register out = locations->Out().AsX86().AsCpuRegister();
1323 __ movsxb(out, Address(obj, offset));
1324 break;
1325 }
1326
1327 case Primitive::kPrimShort: {
1328 Register out = locations->Out().AsX86().AsCpuRegister();
1329 __ movsxw(out, Address(obj, offset));
1330 break;
1331 }
1332
1333 case Primitive::kPrimChar: {
1334 Register out = locations->Out().AsX86().AsCpuRegister();
1335 __ movzxw(out, Address(obj, offset));
1336 break;
1337 }
1338
1339 case Primitive::kPrimInt:
1340 case Primitive::kPrimNot: {
1341 Register out = locations->Out().AsX86().AsCpuRegister();
1342 __ movl(out, Address(obj, offset));
1343 break;
1344 }
1345
1346 case Primitive::kPrimLong: {
1347 // TODO: support volatile.
1348 X86ManagedRegister out = locations->Out().AsX86();
1349 __ movl(out.AsRegisterPairLow(), Address(obj, offset));
1350 __ movl(out.AsRegisterPairHigh(), Address(obj, kX86WordSize + offset));
1351 break;
1352 }
1353
1354 case Primitive::kPrimFloat:
1355 case Primitive::kPrimDouble:
1356 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1357
1358 case Primitive::kPrimVoid:
1359 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1360 }
1361}
1362
1363void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001364 LocationSummary* locations =
1365 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001366 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001367 if (instruction->HasUses()) {
1368 locations->SetOut(Location::SameAsFirstInput());
1369 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001370}
1371
1372void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001373 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001374 codegen_->AddSlowPath(slow_path);
1375
1376 LocationSummary* locations = instruction->GetLocations();
1377 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001378
1379 if (obj.IsRegister()) {
1380 __ cmpl(obj.AsX86().AsCpuRegister(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001381 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001382 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001383 } else {
1384 DCHECK(obj.IsConstant()) << obj;
1385 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1386 __ jmp(slow_path->GetEntryLabel());
1387 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001388 }
1389 __ j(kEqual, slow_path->GetEntryLabel());
1390}
1391
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001392void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001393 LocationSummary* locations =
1394 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001395 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
1396 locations->SetInAt(
1397 1, Location::RegisterOrConstant(instruction->InputAt(1)), Location::kDiesAtEntry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001398 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001399}
1400
1401void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
1402 LocationSummary* locations = instruction->GetLocations();
1403 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1404 Location index = locations->InAt(1);
1405
1406 switch (instruction->GetType()) {
1407 case Primitive::kPrimBoolean: {
1408 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1409 Register out = locations->Out().AsX86().AsCpuRegister();
1410 if (index.IsConstant()) {
1411 __ movzxb(out, Address(obj,
1412 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1413 } else {
1414 __ movzxb(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_1, data_offset));
1415 }
1416 break;
1417 }
1418
1419 case Primitive::kPrimByte: {
1420 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1421 Register out = locations->Out().AsX86().AsCpuRegister();
1422 if (index.IsConstant()) {
1423 __ movsxb(out, Address(obj,
1424 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1425 } else {
1426 __ movsxb(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_1, data_offset));
1427 }
1428 break;
1429 }
1430
1431 case Primitive::kPrimShort: {
1432 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1433 Register out = locations->Out().AsX86().AsCpuRegister();
1434 if (index.IsConstant()) {
1435 __ movsxw(out, Address(obj,
1436 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1437 } else {
1438 __ movsxw(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_2, data_offset));
1439 }
1440 break;
1441 }
1442
1443 case Primitive::kPrimChar: {
1444 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1445 Register out = locations->Out().AsX86().AsCpuRegister();
1446 if (index.IsConstant()) {
1447 __ movzxw(out, Address(obj,
1448 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1449 } else {
1450 __ movzxw(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_2, data_offset));
1451 }
1452 break;
1453 }
1454
1455 case Primitive::kPrimInt:
1456 case Primitive::kPrimNot: {
1457 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1458 Register out = locations->Out().AsX86().AsCpuRegister();
1459 if (index.IsConstant()) {
1460 __ movl(out, Address(obj,
1461 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1462 } else {
1463 __ movl(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_4, data_offset));
1464 }
1465 break;
1466 }
1467
1468 case Primitive::kPrimLong: {
1469 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1470 X86ManagedRegister out = locations->Out().AsX86();
1471 if (index.IsConstant()) {
1472 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1473 __ movl(out.AsRegisterPairLow(), Address(obj, offset));
1474 __ movl(out.AsRegisterPairHigh(), Address(obj, offset + kX86WordSize));
1475 } else {
1476 __ movl(out.AsRegisterPairLow(),
1477 Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset));
1478 __ movl(out.AsRegisterPairHigh(),
1479 Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset + kX86WordSize));
1480 }
1481 break;
1482 }
1483
1484 case Primitive::kPrimFloat:
1485 case Primitive::kPrimDouble:
1486 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1487
1488 case Primitive::kPrimVoid:
1489 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1490 }
1491}
1492
1493void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001494 Primitive::Type value_type = instruction->GetComponentType();
1495 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1496 instruction,
1497 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
1498
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001499 if (value_type == Primitive::kPrimNot) {
1500 InvokeRuntimeCallingConvention calling_convention;
1501 locations->SetInAt(0, X86CpuLocation(calling_convention.GetRegisterAt(0)));
1502 locations->SetInAt(1, X86CpuLocation(calling_convention.GetRegisterAt(1)));
1503 locations->SetInAt(2, X86CpuLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001504 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001505 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
1506 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001507 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001508 // In case of a byte operation, the register allocator does not support multiple
1509 // inputs that die at entry with one in a specific register.
1510 bool dies_at_entry = value_type != Primitive::kPrimLong && !is_byte_type;
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001511 locations->SetInAt(0, Location::RequiresRegister(), dies_at_entry);
1512 locations->SetInAt(
1513 1, Location::RegisterOrConstant(instruction->InputAt(1)), dies_at_entry);
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001514 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001515 // Ensure the value is in a byte register.
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001516 locations->SetInAt(2, Location::ByteRegisterOrConstant(
1517 X86ManagedRegister::FromCpuRegister(EAX), instruction->InputAt(2)), dies_at_entry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001518 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001519 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)), dies_at_entry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001520 }
1521 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001522}
1523
1524void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
1525 LocationSummary* locations = instruction->GetLocations();
1526 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1527 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001528 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001529 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001530
1531 switch (value_type) {
1532 case Primitive::kPrimBoolean:
1533 case Primitive::kPrimByte: {
1534 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001535 if (index.IsConstant()) {
1536 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001537 if (value.IsRegister()) {
1538 __ movb(Address(obj, offset), value.AsX86().AsByteRegister());
1539 } else {
1540 __ movb(Address(obj, offset),
1541 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1542 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001543 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001544 if (value.IsRegister()) {
1545 __ movb(Address(obj, index.AsX86().AsCpuRegister(), TIMES_1, data_offset),
1546 value.AsX86().AsByteRegister());
1547 } else {
1548 __ movb(Address(obj, index.AsX86().AsCpuRegister(), TIMES_1, data_offset),
1549 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1550 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001551 }
1552 break;
1553 }
1554
1555 case Primitive::kPrimShort:
1556 case Primitive::kPrimChar: {
1557 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001558 if (index.IsConstant()) {
1559 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001560 if (value.IsRegister()) {
1561 __ movw(Address(obj, offset), value.AsX86().AsCpuRegister());
1562 } else {
1563 __ movw(Address(obj, offset),
1564 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1565 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001566 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001567 if (value.IsRegister()) {
1568 __ movw(Address(obj, index.AsX86().AsCpuRegister(), TIMES_2, data_offset),
1569 value.AsX86().AsCpuRegister());
1570 } else {
1571 __ movw(Address(obj, index.AsX86().AsCpuRegister(), TIMES_2, data_offset),
1572 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1573 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001574 }
1575 break;
1576 }
1577
1578 case Primitive::kPrimInt: {
1579 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001580 if (index.IsConstant()) {
1581 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001582 if (value.IsRegister()) {
1583 __ movl(Address(obj, offset), value.AsX86().AsCpuRegister());
1584 } else {
1585 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1586 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001587 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001588 if (value.IsRegister()) {
1589 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_4, data_offset),
1590 value.AsX86().AsCpuRegister());
1591 } else {
1592 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_4, data_offset),
1593 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1594 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001595 }
1596 break;
1597 }
1598
1599 case Primitive::kPrimNot: {
1600 DCHECK(!codegen_->IsLeafMethod());
1601 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001602 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001603 break;
1604 }
1605
1606 case Primitive::kPrimLong: {
1607 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001608 if (index.IsConstant()) {
1609 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001610 if (value.IsRegister()) {
1611 __ movl(Address(obj, offset), value.AsX86().AsRegisterPairLow());
1612 __ movl(Address(obj, offset + kX86WordSize), value.AsX86().AsRegisterPairHigh());
1613 } else {
1614 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
1615 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
1616 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
1617 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001618 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001619 if (value.IsRegister()) {
1620 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset),
1621 value.AsX86().AsRegisterPairLow());
1622 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset + kX86WordSize),
1623 value.AsX86().AsRegisterPairHigh());
1624 } else {
1625 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
1626 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset),
1627 Immediate(Low32Bits(val)));
1628 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset + kX86WordSize),
1629 Immediate(High32Bits(val)));
1630 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001631 }
1632 break;
1633 }
1634
1635 case Primitive::kPrimFloat:
1636 case Primitive::kPrimDouble:
1637 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1638
1639 case Primitive::kPrimVoid:
1640 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1641 }
1642}
1643
1644void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
1645 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001646 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001647 locations->SetOut(Location::RequiresRegister());
1648 instruction->SetLocations(locations);
1649}
1650
1651void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
1652 LocationSummary* locations = instruction->GetLocations();
1653 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1654 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1655 Register out = locations->Out().AsX86().AsCpuRegister();
1656 __ movl(out, Address(obj, offset));
1657}
1658
1659void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001660 LocationSummary* locations =
1661 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001662 locations->SetInAt(0, Location::RequiresRegister());
1663 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001664 if (instruction->HasUses()) {
1665 locations->SetOut(Location::SameAsFirstInput());
1666 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001667}
1668
1669void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
1670 LocationSummary* locations = instruction->GetLocations();
1671 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01001672 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001673 codegen_->AddSlowPath(slow_path);
1674
1675 Register index = locations->InAt(0).AsX86().AsCpuRegister();
1676 Register length = locations->InAt(1).AsX86().AsCpuRegister();
1677
1678 __ cmpl(index, length);
1679 __ j(kAboveEqual, slow_path->GetEntryLabel());
1680}
1681
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001682void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
1683 temp->SetLocations(nullptr);
1684}
1685
1686void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
1687 // Nothing to do, this is driven by the code generator.
1688}
1689
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001690void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001691 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001692}
1693
1694void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001695 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
1696}
1697
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001698void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
1699 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
1700}
1701
1702void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001703 HBasicBlock* block = instruction->GetBlock();
1704 if (block->GetLoopInformation() != nullptr) {
1705 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
1706 // The back edge will generate the suspend check.
1707 return;
1708 }
1709 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
1710 // The goto will generate the suspend check.
1711 return;
1712 }
1713 GenerateSuspendCheck(instruction, nullptr);
1714}
1715
1716void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
1717 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001718 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001719 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001720 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001721 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001722 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001723 if (successor == nullptr) {
1724 __ j(kNotEqual, slow_path->GetEntryLabel());
1725 __ Bind(slow_path->GetReturnLabel());
1726 } else {
1727 __ j(kEqual, codegen_->GetLabelOf(successor));
1728 __ jmp(slow_path->GetEntryLabel());
1729 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001730}
1731
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001732X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
1733 return codegen_->GetAssembler();
1734}
1735
1736void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
1737 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001738 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001739 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1740 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
1741 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
1742}
1743
1744void ParallelMoveResolverX86::EmitMove(size_t index) {
1745 MoveOperands* move = moves_.Get(index);
1746 Location source = move->GetSource();
1747 Location destination = move->GetDestination();
1748
1749 if (source.IsRegister()) {
1750 if (destination.IsRegister()) {
1751 __ movl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister());
1752 } else {
1753 DCHECK(destination.IsStackSlot());
1754 __ movl(Address(ESP, destination.GetStackIndex()), source.AsX86().AsCpuRegister());
1755 }
1756 } else if (source.IsStackSlot()) {
1757 if (destination.IsRegister()) {
1758 __ movl(destination.AsX86().AsCpuRegister(), Address(ESP, source.GetStackIndex()));
1759 } else {
1760 DCHECK(destination.IsStackSlot());
1761 MoveMemoryToMemory(destination.GetStackIndex(),
1762 source.GetStackIndex());
1763 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001764 } else if (source.IsConstant()) {
1765 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
1766 Immediate imm(instruction->AsIntConstant()->GetValue());
1767 if (destination.IsRegister()) {
1768 __ movl(destination.AsX86().AsCpuRegister(), imm);
1769 } else {
1770 __ movl(Address(ESP, destination.GetStackIndex()), imm);
1771 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001772 } else {
1773 LOG(FATAL) << "Unimplemented";
1774 }
1775}
1776
1777void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001778 Register suggested_scratch = reg == EAX ? EBX : EAX;
1779 ScratchRegisterScope ensure_scratch(
1780 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
1781
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001782 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1783 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
1784 __ movl(Address(ESP, mem + stack_offset), reg);
1785 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
1786}
1787
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001788void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
1789 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001790 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
1791
1792 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001793 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001794 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
1795
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001796 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
1797 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
1798 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
1799 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
1800 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
1801 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
1802}
1803
1804void ParallelMoveResolverX86::EmitSwap(size_t index) {
1805 MoveOperands* move = moves_.Get(index);
1806 Location source = move->GetSource();
1807 Location destination = move->GetDestination();
1808
1809 if (source.IsRegister() && destination.IsRegister()) {
1810 __ xchgl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister());
1811 } else if (source.IsRegister() && destination.IsStackSlot()) {
1812 Exchange(source.AsX86().AsCpuRegister(), destination.GetStackIndex());
1813 } else if (source.IsStackSlot() && destination.IsRegister()) {
1814 Exchange(destination.AsX86().AsCpuRegister(), source.GetStackIndex());
1815 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
1816 Exchange(destination.GetStackIndex(), source.GetStackIndex());
1817 } else {
1818 LOG(FATAL) << "Unimplemented";
1819 }
1820}
1821
1822void ParallelMoveResolverX86::SpillScratch(int reg) {
1823 __ pushl(static_cast<Register>(reg));
1824}
1825
1826void ParallelMoveResolverX86::RestoreScratch(int reg) {
1827 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001828}
1829
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001830} // namespace x86
1831} // namespace art