blob: 3dd9b37158a7bfc4174e5fa2028485bc7a7ae1d1 [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"
23#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000024#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010025#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000026#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010027#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000028
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010030
31x86::X86ManagedRegister Location::AsX86() const {
32 return reg().AsX86();
33}
34
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace x86 {
36
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010037static constexpr bool kExplicitStackOverflowCheck = false;
38
39static constexpr int kNumberOfPushedRegistersAtEntry = 1;
40static constexpr int kCurrentMethodStackOffset = 0;
41
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010042static Location X86CpuLocation(Register reg) {
43 return Location::RegisterLocation(X86ManagedRegister::FromCpuRegister(reg));
44}
45
46static constexpr Register kRuntimeParameterCoreRegisters[] = { EAX, ECX, EDX };
47static constexpr size_t kRuntimeParameterCoreRegistersLength =
48 arraysize(kRuntimeParameterCoreRegisters);
49
50class InvokeRuntimeCallingConvention : public CallingConvention<Register> {
51 public:
52 InvokeRuntimeCallingConvention()
53 : CallingConvention(kRuntimeParameterCoreRegisters,
54 kRuntimeParameterCoreRegistersLength) {}
55
56 private:
57 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
58};
59
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
61
62class NullCheckSlowPathX86 : public SlowPathCode {
63 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010064 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010065
66 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
67 __ Bind(GetEntryLabel());
68 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010069 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010070 }
71
72 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010073 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010074 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
75};
76
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010077class StackOverflowCheckSlowPathX86 : public SlowPathCode {
78 public:
79 StackOverflowCheckSlowPathX86() {}
80
81 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
82 __ Bind(GetEntryLabel());
83 __ addl(ESP,
84 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
85 __ fs()->jmp(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowStackOverflow)));
86 }
87
88 private:
89 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86);
90};
91
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010092class BoundsCheckSlowPathX86 : public SlowPathCode {
93 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010094 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010095 Location index_location,
96 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +010097 : instruction_(instruction), index_location_(index_location), length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010098
99 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
100 CodeGeneratorX86* x86_codegen = reinterpret_cast<CodeGeneratorX86*>(codegen);
101 __ Bind(GetEntryLabel());
102 InvokeRuntimeCallingConvention calling_convention;
103 x86_codegen->Move32(X86CpuLocation(calling_convention.GetRegisterAt(0)), index_location_);
104 x86_codegen->Move32(X86CpuLocation(calling_convention.GetRegisterAt(1)), length_location_);
105 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100106 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100107 }
108
109 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100110 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100111 const Location index_location_;
112 const Location length_location_;
113
114 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
115};
116
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000117class SuspendCheckSlowPathX86 : public SlowPathCode {
118 public:
119 explicit SuspendCheckSlowPathX86(HSuspendCheck* instruction)
120 : instruction_(instruction) {}
121
122 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
123 __ Bind(GetEntryLabel());
124 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
125 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
126 __ jmp(GetReturnLabel());
127 }
128
129 Label* GetReturnLabel() { return &return_label_; }
130
131 private:
132 HSuspendCheck* const instruction_;
133 Label return_label_;
134
135 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
136};
137
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100138#undef __
139#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
140
Dave Allison20dfc792014-06-16 20:44:29 -0700141inline Condition X86Condition(IfCondition cond) {
142 switch (cond) {
143 case kCondEQ: return kEqual;
144 case kCondNE: return kNotEqual;
145 case kCondLT: return kLess;
146 case kCondLE: return kLessEqual;
147 case kCondGT: return kGreater;
148 case kCondGE: return kGreaterEqual;
149 default:
150 LOG(FATAL) << "Unknown if condition";
151 }
152 return kEqual;
153}
154
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100155void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
156 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
157}
158
159void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
160 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
161}
162
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100163CodeGeneratorX86::CodeGeneratorX86(HGraph* graph)
164 : CodeGenerator(graph, kNumberOfRegIds),
165 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100166 instruction_visitor_(graph, this),
167 move_resolver_(graph->GetArena(), this) {}
168
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100169size_t CodeGeneratorX86::FrameEntrySpillSize() const {
170 return kNumberOfPushedRegistersAtEntry * kX86WordSize;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100171}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100172
173static bool* GetBlockedRegisterPairs(bool* blocked_registers) {
174 return blocked_registers + kNumberOfAllocIds;
175}
176
177ManagedRegister CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type,
178 bool* blocked_registers) const {
179 switch (type) {
180 case Primitive::kPrimLong: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100181 bool* blocked_register_pairs = GetBlockedRegisterPairs(blocked_registers);
182 size_t reg = AllocateFreeRegisterInternal(blocked_register_pairs, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100183 X86ManagedRegister pair =
184 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
185 blocked_registers[pair.AsRegisterPairLow()] = true;
186 blocked_registers[pair.AsRegisterPairHigh()] = true;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100187 // Block all other register pairs that share a register with `pair`.
188 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
189 X86ManagedRegister current =
190 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
191 if (current.AsRegisterPairLow() == pair.AsRegisterPairLow()
192 || current.AsRegisterPairLow() == pair.AsRegisterPairHigh()
193 || current.AsRegisterPairHigh() == pair.AsRegisterPairLow()
194 || current.AsRegisterPairHigh() == pair.AsRegisterPairHigh()) {
195 blocked_register_pairs[i] = true;
196 }
197 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100198 return pair;
199 }
200
201 case Primitive::kPrimByte:
202 case Primitive::kPrimBoolean:
203 case Primitive::kPrimChar:
204 case Primitive::kPrimShort:
205 case Primitive::kPrimInt:
206 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100207 Register reg = static_cast<Register>(
208 AllocateFreeRegisterInternal(blocked_registers, kNumberOfCpuRegisters));
209 // Block all register pairs that contain `reg`.
210 bool* blocked_register_pairs = GetBlockedRegisterPairs(blocked_registers);
211 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
212 X86ManagedRegister current =
213 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
214 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
215 blocked_register_pairs[i] = true;
216 }
217 }
218 return X86ManagedRegister::FromCpuRegister(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100219 }
220
221 case Primitive::kPrimFloat:
222 case Primitive::kPrimDouble:
223 LOG(FATAL) << "Unimplemented register type " << type;
224
225 case Primitive::kPrimVoid:
226 LOG(FATAL) << "Unreachable type " << type;
227 }
228
229 return ManagedRegister::NoRegister();
230}
231
232void CodeGeneratorX86::SetupBlockedRegisters(bool* blocked_registers) const {
233 bool* blocked_register_pairs = GetBlockedRegisterPairs(blocked_registers);
234
235 // Don't allocate the dalvik style register pair passing.
236 blocked_register_pairs[ECX_EDX] = true;
237
238 // Stack register is always reserved.
239 blocked_registers[ESP] = true;
240
241 // TODO: We currently don't use Quick's callee saved registers.
242 blocked_registers[EBP] = true;
243 blocked_registers[ESI] = true;
244 blocked_registers[EDI] = true;
245 blocked_register_pairs[EAX_EDI] = true;
246 blocked_register_pairs[EDX_EDI] = true;
247 blocked_register_pairs[ECX_EDI] = true;
248 blocked_register_pairs[EBX_EDI] = true;
249}
250
251size_t CodeGeneratorX86::GetNumberOfRegisters() const {
252 return kNumberOfRegIds;
253}
254
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100255InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
256 : HGraphVisitor(graph),
257 assembler_(codegen->GetAssembler()),
258 codegen_(codegen) {}
259
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000260void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000261 // Create a fake register to mimic Quick.
262 static const int kFakeReturnRegister = 8;
263 core_spill_mask_ |= (1 << kFakeReturnRegister);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000264
Dave Allison648d7112014-07-25 16:15:27 -0700265 bool skip_overflow_check = IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100266 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
267 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100268 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100269 }
270
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100271 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100272 __ subl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100273
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100274 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
275 SlowPathCode* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86();
276 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100277
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100278 __ fs()->cmpl(ESP, Address::Absolute(Thread::StackEndOffset<kX86WordSize>()));
279 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100280 }
281
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100282 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000283}
284
285void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100286 __ addl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000287}
288
289void CodeGeneratorX86::Bind(Label* label) {
290 __ Bind(label);
291}
292
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000293void InstructionCodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100294 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000295}
296
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100297Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
298 switch (load->GetType()) {
299 case Primitive::kPrimLong:
300 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
301 break;
302
303 case Primitive::kPrimInt:
304 case Primitive::kPrimNot:
305 return Location::StackSlot(GetStackSlot(load->GetLocal()));
306
307 case Primitive::kPrimFloat:
308 case Primitive::kPrimDouble:
309 LOG(FATAL) << "Unimplemented type " << load->GetType();
310
311 case Primitive::kPrimBoolean:
312 case Primitive::kPrimByte:
313 case Primitive::kPrimChar:
314 case Primitive::kPrimShort:
315 case Primitive::kPrimVoid:
316 LOG(FATAL) << "Unexpected type " << load->GetType();
317 }
318
319 LOG(FATAL) << "Unreachable";
320 return Location();
321}
322
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100323Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
324 switch (type) {
325 case Primitive::kPrimBoolean:
326 case Primitive::kPrimByte:
327 case Primitive::kPrimChar:
328 case Primitive::kPrimShort:
329 case Primitive::kPrimInt:
330 case Primitive::kPrimNot: {
331 uint32_t index = gp_index_++;
332 if (index < calling_convention.GetNumberOfRegisters()) {
333 return X86CpuLocation(calling_convention.GetRegisterAt(index));
334 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100335 return Location::StackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100336 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100337 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100338
339 case Primitive::kPrimLong: {
340 uint32_t index = gp_index_;
341 gp_index_ += 2;
342 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
343 return Location::RegisterLocation(X86ManagedRegister::FromRegisterPair(
344 calling_convention.GetRegisterPairAt(index)));
345 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
346 return Location::QuickParameter(index);
347 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100348 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100349 }
350 }
351
352 case Primitive::kPrimDouble:
353 case Primitive::kPrimFloat:
354 LOG(FATAL) << "Unimplemented parameter type " << type;
355 break;
356
357 case Primitive::kPrimVoid:
358 LOG(FATAL) << "Unexpected parameter type " << type;
359 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100360 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100361 return Location();
362}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100363
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100364void CodeGeneratorX86::Move32(Location destination, Location source) {
365 if (source.Equals(destination)) {
366 return;
367 }
368 if (destination.IsRegister()) {
369 if (source.IsRegister()) {
370 __ movl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister());
371 } else {
372 DCHECK(source.IsStackSlot());
373 __ movl(destination.AsX86().AsCpuRegister(), Address(ESP, source.GetStackIndex()));
374 }
375 } else {
376 if (source.IsRegister()) {
377 __ movl(Address(ESP, destination.GetStackIndex()), source.AsX86().AsCpuRegister());
378 } else {
379 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100380 __ pushl(Address(ESP, source.GetStackIndex()));
381 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100382 }
383 }
384}
385
386void CodeGeneratorX86::Move64(Location destination, Location source) {
387 if (source.Equals(destination)) {
388 return;
389 }
390 if (destination.IsRegister()) {
391 if (source.IsRegister()) {
392 __ movl(destination.AsX86().AsRegisterPairLow(), source.AsX86().AsRegisterPairLow());
393 __ movl(destination.AsX86().AsRegisterPairHigh(), source.AsX86().AsRegisterPairHigh());
394 } else if (source.IsQuickParameter()) {
395 uint32_t argument_index = source.GetQuickParameterIndex();
396 InvokeDexCallingConvention calling_convention;
397 __ movl(destination.AsX86().AsRegisterPairLow(),
398 calling_convention.GetRegisterAt(argument_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100399 __ movl(destination.AsX86().AsRegisterPairHigh(), Address(ESP,
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100400 calling_convention.GetStackOffsetOf(argument_index + 1) + GetFrameSize()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100401 } else {
402 DCHECK(source.IsDoubleStackSlot());
403 __ movl(destination.AsX86().AsRegisterPairLow(), Address(ESP, source.GetStackIndex()));
404 __ movl(destination.AsX86().AsRegisterPairHigh(),
405 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
406 }
407 } else if (destination.IsQuickParameter()) {
408 InvokeDexCallingConvention calling_convention;
409 uint32_t argument_index = destination.GetQuickParameterIndex();
410 if (source.IsRegister()) {
411 __ movl(calling_convention.GetRegisterAt(argument_index), source.AsX86().AsRegisterPairLow());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100412 __ movl(Address(ESP, calling_convention.GetStackOffsetOf(argument_index + 1)),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100413 source.AsX86().AsRegisterPairHigh());
414 } else {
415 DCHECK(source.IsDoubleStackSlot());
416 __ movl(calling_convention.GetRegisterAt(argument_index),
417 Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100418 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100419 __ popl(Address(ESP, calling_convention.GetStackOffsetOf(argument_index + 1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100420 }
421 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100422 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100423 if (source.IsRegister()) {
424 __ movl(Address(ESP, destination.GetStackIndex()), source.AsX86().AsRegisterPairLow());
425 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
426 source.AsX86().AsRegisterPairHigh());
427 } else if (source.IsQuickParameter()) {
428 InvokeDexCallingConvention calling_convention;
429 uint32_t argument_index = source.GetQuickParameterIndex();
430 __ movl(Address(ESP, destination.GetStackIndex()),
431 calling_convention.GetRegisterAt(argument_index));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100432 __ pushl(Address(ESP,
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100433 calling_convention.GetStackOffsetOf(argument_index + 1) + GetFrameSize()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100434 __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100435 } else {
436 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100437 __ pushl(Address(ESP, source.GetStackIndex()));
438 __ popl(Address(ESP, destination.GetStackIndex()));
439 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
440 __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100441 }
442 }
443}
444
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100445void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
446 if (instruction->AsIntConstant() != nullptr) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100447 Immediate imm(instruction->AsIntConstant()->GetValue());
448 if (location.IsRegister()) {
449 __ movl(location.AsX86().AsCpuRegister(), imm);
450 } else {
451 __ movl(Address(ESP, location.GetStackIndex()), imm);
452 }
453 } else if (instruction->AsLongConstant() != nullptr) {
454 int64_t value = instruction->AsLongConstant()->GetValue();
455 if (location.IsRegister()) {
456 __ movl(location.AsX86().AsRegisterPairLow(), Immediate(Low32Bits(value)));
457 __ movl(location.AsX86().AsRegisterPairHigh(), Immediate(High32Bits(value)));
458 } else {
459 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
460 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
461 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100462 } else if (instruction->AsLoadLocal() != nullptr) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100463 switch (instruction->GetType()) {
464 case Primitive::kPrimBoolean:
465 case Primitive::kPrimByte:
466 case Primitive::kPrimChar:
467 case Primitive::kPrimShort:
468 case Primitive::kPrimInt:
469 case Primitive::kPrimNot:
470 Move32(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
471 break;
472
473 case Primitive::kPrimLong:
474 Move64(location, Location::DoubleStackSlot(
475 GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
476 break;
477
478 default:
479 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
480 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000481 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100482 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100483 switch (instruction->GetType()) {
484 case Primitive::kPrimBoolean:
485 case Primitive::kPrimByte:
486 case Primitive::kPrimChar:
487 case Primitive::kPrimShort:
488 case Primitive::kPrimInt:
489 case Primitive::kPrimNot:
490 Move32(location, instruction->GetLocations()->Out());
491 break;
492
493 case Primitive::kPrimLong:
494 Move64(location, instruction->GetLocations()->Out());
495 break;
496
497 default:
498 LOG(FATAL) << "Unimplemented type " << instruction->GetType();
499 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000500 }
501}
502
503void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000504 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000505}
506
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000507void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000508 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000509 if (GetGraph()->GetExitBlock() == successor) {
510 codegen_->GenerateFrameExit();
511 } else if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
512 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000513 }
514}
515
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000516void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000517 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000518}
519
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000520void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000521 if (kIsDebugBuild) {
522 __ Comment("Unreachable");
523 __ int3();
524 }
525}
526
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000527void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100528 LocationSummary* locations =
529 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100530 HInstruction* cond = if_instr->InputAt(0);
531 DCHECK(cond->IsCondition());
532 HCondition* condition = cond->AsCondition();
533 if (condition->NeedsMaterialization()) {
534 locations->SetInAt(0, Location::Any());
535 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000536}
537
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000538void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700539 HInstruction* cond = if_instr->InputAt(0);
540 DCHECK(cond->IsCondition());
541 HCondition* condition = cond->AsCondition();
542 if (condition->NeedsMaterialization()) {
543 // Materialized condition, compare against 0
544 Location lhs = if_instr->GetLocations()->InAt(0);
545 if (lhs.IsRegister()) {
546 __ cmpl(lhs.AsX86().AsCpuRegister(), Immediate(0));
547 } else {
548 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
549 }
550 __ j(kEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100551 } else {
Dave Allison20dfc792014-06-16 20:44:29 -0700552 Location lhs = condition->GetLocations()->InAt(0);
553 Location rhs = condition->GetLocations()->InAt(1);
554 // LHS is guaranteed to be in a register (see LocationsBuilderX86::VisitCondition).
555 if (rhs.IsRegister()) {
556 __ cmpl(lhs.AsX86().AsCpuRegister(), rhs.AsX86().AsCpuRegister());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100557 } else if (rhs.IsConstant()) {
558 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
559 Immediate imm(instruction->AsIntConstant()->GetValue());
560 __ cmpl(lhs.AsX86().AsCpuRegister(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700561 } else {
562 __ cmpl(lhs.AsX86().AsCpuRegister(), Address(ESP, rhs.GetStackIndex()));
563 }
564 __ j(X86Condition(condition->GetCondition()),
565 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100566 }
Dave Allison20dfc792014-06-16 20:44:29 -0700567 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfFalseSuccessor())) {
568 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000569 }
570}
571
572void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000573 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000574}
575
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000576void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
577 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000578}
579
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000580void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100581 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000582}
583
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000584void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100585 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000586}
587
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100588void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100589 LocationSummary* locations =
590 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100591 switch (store->InputAt(1)->GetType()) {
592 case Primitive::kPrimBoolean:
593 case Primitive::kPrimByte:
594 case Primitive::kPrimChar:
595 case Primitive::kPrimShort:
596 case Primitive::kPrimInt:
597 case Primitive::kPrimNot:
598 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
599 break;
600
601 case Primitive::kPrimLong:
602 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
603 break;
604
605 default:
606 LOG(FATAL) << "Unimplemented local type " << store->InputAt(1)->GetType();
607 }
608 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000609}
610
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000611void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000612}
613
Dave Allison20dfc792014-06-16 20:44:29 -0700614void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100615 LocationSummary* locations =
616 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100617 locations->SetInAt(0, Location::RequiresRegister());
618 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100619 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100620 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100621 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000622}
623
Dave Allison20dfc792014-06-16 20:44:29 -0700624void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
625 if (comp->NeedsMaterialization()) {
626 LocationSummary* locations = comp->GetLocations();
627 if (locations->InAt(1).IsRegister()) {
628 __ cmpl(locations->InAt(0).AsX86().AsCpuRegister(),
629 locations->InAt(1).AsX86().AsCpuRegister());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100630 } else if (locations->InAt(1).IsConstant()) {
631 HConstant* instruction = locations->InAt(1).GetConstant();
632 Immediate imm(instruction->AsIntConstant()->GetValue());
633 __ cmpl(locations->InAt(0).AsX86().AsCpuRegister(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700634 } else {
635 __ cmpl(locations->InAt(0).AsX86().AsCpuRegister(),
636 Address(ESP, locations->InAt(1).GetStackIndex()));
637 }
638 __ setb(X86Condition(comp->GetCondition()), locations->Out().AsX86().AsCpuRegister());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100639 }
Dave Allison20dfc792014-06-16 20:44:29 -0700640}
641
642void LocationsBuilderX86::VisitEqual(HEqual* comp) {
643 VisitCondition(comp);
644}
645
646void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
647 VisitCondition(comp);
648}
649
650void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
651 VisitCondition(comp);
652}
653
654void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
655 VisitCondition(comp);
656}
657
658void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
659 VisitCondition(comp);
660}
661
662void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
663 VisitCondition(comp);
664}
665
666void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
667 VisitCondition(comp);
668}
669
670void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
671 VisitCondition(comp);
672}
673
674void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
675 VisitCondition(comp);
676}
677
678void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
679 VisitCondition(comp);
680}
681
682void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
683 VisitCondition(comp);
684}
685
686void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
687 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000688}
689
690void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100691 LocationSummary* locations =
692 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100693 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000694}
695
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000696void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000697}
698
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100699void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100700 LocationSummary* locations =
701 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100702 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100703}
704
705void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
706 // Will be generated at use site.
707}
708
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000709void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000710 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000711}
712
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000713void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
714 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000715 __ ret();
716}
717
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000718void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100719 LocationSummary* locations =
720 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100721 switch (ret->InputAt(0)->GetType()) {
722 case Primitive::kPrimBoolean:
723 case Primitive::kPrimByte:
724 case Primitive::kPrimChar:
725 case Primitive::kPrimShort:
726 case Primitive::kPrimInt:
727 case Primitive::kPrimNot:
728 locations->SetInAt(0, X86CpuLocation(EAX));
729 break;
730
731 case Primitive::kPrimLong:
732 locations->SetInAt(
733 0, Location::RegisterLocation(X86ManagedRegister::FromRegisterPair(EAX_EDX)));
734 break;
735
736 default:
737 LOG(FATAL) << "Unimplemented return type " << ret->InputAt(0)->GetType();
738 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000739}
740
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000741void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100742 if (kIsDebugBuild) {
743 switch (ret->InputAt(0)->GetType()) {
744 case Primitive::kPrimBoolean:
745 case Primitive::kPrimByte:
746 case Primitive::kPrimChar:
747 case Primitive::kPrimShort:
748 case Primitive::kPrimInt:
749 case Primitive::kPrimNot:
750 DCHECK_EQ(ret->GetLocations()->InAt(0).AsX86().AsCpuRegister(), EAX);
751 break;
752
753 case Primitive::kPrimLong:
754 DCHECK_EQ(ret->GetLocations()->InAt(0).AsX86().AsRegisterPair(), EAX_EDX);
755 break;
756
757 default:
758 LOG(FATAL) << "Unimplemented return type " << ret->InputAt(0)->GetType();
759 }
760 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000761 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000762 __ ret();
763}
764
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000765void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100766 LocationSummary* locations =
767 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100768 locations->AddTemp(X86CpuLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100769
770 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100771 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100772 HInstruction* input = invoke->InputAt(i);
773 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
774 }
775
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100776 switch (invoke->GetType()) {
777 case Primitive::kPrimBoolean:
778 case Primitive::kPrimByte:
779 case Primitive::kPrimChar:
780 case Primitive::kPrimShort:
781 case Primitive::kPrimInt:
782 case Primitive::kPrimNot:
783 locations->SetOut(X86CpuLocation(EAX));
784 break;
785
786 case Primitive::kPrimLong:
787 locations->SetOut(Location::RegisterLocation(X86ManagedRegister::FromRegisterPair(EAX_EDX)));
788 break;
789
790 case Primitive::kPrimVoid:
791 break;
792
793 case Primitive::kPrimDouble:
794 case Primitive::kPrimFloat:
795 LOG(FATAL) << "Unimplemented return type " << invoke->GetType();
796 break;
797 }
798
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000799 invoke->SetLocations(locations);
800}
801
802void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100803 Register temp = invoke->GetLocations()->GetTemp(0).AsX86().AsCpuRegister();
Nicolas Geoffrayf61b5372014-06-25 14:35:34 +0100804 uint32_t heap_reference_size = sizeof(mirror::HeapReference<mirror::Object>);
805 size_t index_in_cache = mirror::Array::DataOffset(heap_reference_size).Int32Value() +
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100806 invoke->GetIndexInDexCache() * kX86WordSize;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000807
808 // TODO: Implement all kinds of calls:
809 // 1) boot -> boot
810 // 2) app -> boot
811 // 3) app -> app
812 //
813 // Currently we implement the app -> app logic, which looks up in the resolve cache.
814
815 // temp = method;
816 LoadCurrentMethod(temp);
817 // temp = temp->dex_cache_resolved_methods_;
818 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
819 // temp = temp[index_in_cache]
820 __ movl(temp, Address(temp, index_in_cache));
821 // (temp + offset_of_quick_compiled_code)()
822 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
823
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100824 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100825 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000826}
827
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000828void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100829 LocationSummary* locations =
830 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000831 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100832 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100833 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100834 locations->SetInAt(0, Location::RequiresRegister());
835 locations->SetInAt(1, Location::Any());
836 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100837 break;
838 }
839
840 case Primitive::kPrimBoolean:
841 case Primitive::kPrimByte:
842 case Primitive::kPrimChar:
843 case Primitive::kPrimShort:
844 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
845 break;
846
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000847 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100848 LOG(FATAL) << "Unimplemented add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000849 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000850}
851
852void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
853 LocationSummary* locations = add->GetLocations();
854 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100855 case Primitive::kPrimInt: {
856 DCHECK_EQ(locations->InAt(0).AsX86().AsCpuRegister(),
857 locations->Out().AsX86().AsCpuRegister());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100858 if (locations->InAt(1).IsRegister()) {
859 __ addl(locations->InAt(0).AsX86().AsCpuRegister(),
860 locations->InAt(1).AsX86().AsCpuRegister());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100861 } else if (locations->InAt(1).IsConstant()) {
862 HConstant* instruction = locations->InAt(1).GetConstant();
863 Immediate imm(instruction->AsIntConstant()->GetValue());
864 __ addl(locations->InAt(0).AsX86().AsCpuRegister(), imm);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100865 } else {
866 __ addl(locations->InAt(0).AsX86().AsCpuRegister(),
867 Address(ESP, locations->InAt(1).GetStackIndex()));
868 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000869 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100870 }
871
872 case Primitive::kPrimLong: {
873 DCHECK_EQ(locations->InAt(0).AsX86().AsRegisterPair(),
874 locations->Out().AsX86().AsRegisterPair());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100875 if (locations->InAt(1).IsRegister()) {
876 __ addl(locations->InAt(0).AsX86().AsRegisterPairLow(),
877 locations->InAt(1).AsX86().AsRegisterPairLow());
878 __ adcl(locations->InAt(0).AsX86().AsRegisterPairHigh(),
879 locations->InAt(1).AsX86().AsRegisterPairHigh());
880 } else {
881 __ addl(locations->InAt(0).AsX86().AsRegisterPairLow(),
882 Address(ESP, locations->InAt(1).GetStackIndex()));
883 __ adcl(locations->InAt(0).AsX86().AsRegisterPairHigh(),
884 Address(ESP, locations->InAt(1).GetHighStackIndex(kX86WordSize)));
885 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100886 break;
887 }
888
889 case Primitive::kPrimBoolean:
890 case Primitive::kPrimByte:
891 case Primitive::kPrimChar:
892 case Primitive::kPrimShort:
893 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
894 break;
895
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000896 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100897 LOG(FATAL) << "Unimplemented add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000898 }
899}
900
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100901void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100902 LocationSummary* locations =
903 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100904 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100905 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100906 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100907 locations->SetInAt(0, Location::RequiresRegister());
908 locations->SetInAt(1, Location::Any());
909 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100910 break;
911 }
912
913 case Primitive::kPrimBoolean:
914 case Primitive::kPrimByte:
915 case Primitive::kPrimChar:
916 case Primitive::kPrimShort:
917 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
918 break;
919
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100920 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100921 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100922 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100923}
924
925void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
926 LocationSummary* locations = sub->GetLocations();
927 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100928 case Primitive::kPrimInt: {
929 DCHECK_EQ(locations->InAt(0).AsX86().AsCpuRegister(),
930 locations->Out().AsX86().AsCpuRegister());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100931 if (locations->InAt(1).IsRegister()) {
932 __ subl(locations->InAt(0).AsX86().AsCpuRegister(),
933 locations->InAt(1).AsX86().AsCpuRegister());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100934 } else if (locations->InAt(1).IsConstant()) {
935 HConstant* instruction = locations->InAt(1).GetConstant();
936 Immediate imm(instruction->AsIntConstant()->GetValue());
937 __ subl(locations->InAt(0).AsX86().AsCpuRegister(), imm);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100938 } else {
939 __ subl(locations->InAt(0).AsX86().AsCpuRegister(),
940 Address(ESP, locations->InAt(1).GetStackIndex()));
941 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100942 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100943 }
944
945 case Primitive::kPrimLong: {
946 DCHECK_EQ(locations->InAt(0).AsX86().AsRegisterPair(),
947 locations->Out().AsX86().AsRegisterPair());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100948 if (locations->InAt(1).IsRegister()) {
949 __ subl(locations->InAt(0).AsX86().AsRegisterPairLow(),
950 locations->InAt(1).AsX86().AsRegisterPairLow());
951 __ sbbl(locations->InAt(0).AsX86().AsRegisterPairHigh(),
952 locations->InAt(1).AsX86().AsRegisterPairHigh());
953 } else {
954 __ subl(locations->InAt(0).AsX86().AsRegisterPairLow(),
955 Address(ESP, locations->InAt(1).GetStackIndex()));
956 __ sbbl(locations->InAt(0).AsX86().AsRegisterPairHigh(),
957 Address(ESP, locations->InAt(1).GetHighStackIndex(kX86WordSize)));
958 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100959 break;
960 }
961
962 case Primitive::kPrimBoolean:
963 case Primitive::kPrimByte:
964 case Primitive::kPrimChar:
965 case Primitive::kPrimShort:
966 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
967 break;
968
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100969 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100970 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100971 }
972}
973
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100974void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100975 LocationSummary* locations =
976 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100977 locations->SetOut(X86CpuLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100978 InvokeRuntimeCallingConvention calling_convention;
979 locations->AddTemp(X86CpuLocation(calling_convention.GetRegisterAt(0)));
980 locations->AddTemp(X86CpuLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100981}
982
983void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
984 InvokeRuntimeCallingConvention calling_convention;
985 LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100986 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100987
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100988 __ fs()->call(
989 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100990
Nicolas Geoffray39468442014-09-02 15:17:15 +0100991 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100992 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100993}
994
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100995void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100996 LocationSummary* locations =
997 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100998 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
999 if (location.IsStackSlot()) {
1000 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1001 } else if (location.IsDoubleStackSlot()) {
1002 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001003 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001004 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001005}
1006
1007void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001008}
1009
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001010void LocationsBuilderX86::VisitNot(HNot* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001011 LocationSummary* locations =
1012 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001013 locations->SetInAt(0, Location::RequiresRegister());
1014 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001015}
1016
1017void InstructionCodeGeneratorX86::VisitNot(HNot* instruction) {
1018 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001019 Location out = locations->Out();
1020 DCHECK_EQ(locations->InAt(0).AsX86().AsCpuRegister(), out.AsX86().AsCpuRegister());
1021 __ xorl(out.AsX86().AsCpuRegister(), Immediate(1));
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001022}
1023
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001024void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001025 LocationSummary* locations =
1026 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001027 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001028 locations->SetInAt(1, Location::Any());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001029 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001030}
1031
1032void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
1033 Label greater, done;
1034 LocationSummary* locations = compare->GetLocations();
1035 switch (compare->InputAt(0)->GetType()) {
1036 case Primitive::kPrimLong: {
1037 Label less, greater, done;
1038 Register output = locations->Out().AsX86().AsCpuRegister();
1039 X86ManagedRegister left = locations->InAt(0).AsX86();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001040 Location right = locations->InAt(1);
1041 if (right.IsRegister()) {
1042 __ cmpl(left.AsRegisterPairHigh(), right.AsX86().AsRegisterPairHigh());
1043 } else {
1044 DCHECK(right.IsDoubleStackSlot());
1045 __ cmpl(left.AsRegisterPairHigh(), Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1046 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001047 __ j(kLess, &less); // Signed compare.
1048 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001049 if (right.IsRegister()) {
1050 __ cmpl(left.AsRegisterPairLow(), right.AsX86().AsRegisterPairLow());
1051 } else {
1052 DCHECK(right.IsDoubleStackSlot());
1053 __ cmpl(left.AsRegisterPairLow(), Address(ESP, right.GetStackIndex()));
1054 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001055 __ movl(output, Immediate(0));
1056 __ j(kEqual, &done);
1057 __ j(kBelow, &less); // Unsigned compare.
1058
1059 __ Bind(&greater);
1060 __ movl(output, Immediate(1));
1061 __ jmp(&done);
1062
1063 __ Bind(&less);
1064 __ movl(output, Immediate(-1));
1065
1066 __ Bind(&done);
1067 break;
1068 }
1069 default:
1070 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
1071 }
1072}
1073
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001074void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001075 LocationSummary* locations =
1076 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001077 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1078 locations->SetInAt(i, Location::Any());
1079 }
1080 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001081}
1082
1083void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001084 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001085}
1086
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001087void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001088 LocationSummary* locations =
1089 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001090 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001091 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001092 if (field_type == Primitive::kPrimBoolean || field_type == Primitive::kPrimByte) {
1093 // Ensure the value is in a byte register.
1094 locations->SetInAt(1, X86CpuLocation(EAX));
1095 } else {
1096 locations->SetInAt(1, Location::RequiresRegister());
1097 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001098 // Temporary registers for the write barrier.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001099 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001100 locations->AddTemp(Location::RequiresRegister());
1101 // Ensure the card is in a byte register.
1102 locations->AddTemp(X86CpuLocation(ECX));
1103 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001104}
1105
1106void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1107 LocationSummary* locations = instruction->GetLocations();
1108 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1109 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001110 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001111
1112 switch (field_type) {
1113 case Primitive::kPrimBoolean:
1114 case Primitive::kPrimByte: {
1115 ByteRegister value = locations->InAt(1).AsX86().AsByteRegister();
1116 __ movb(Address(obj, offset), value);
1117 break;
1118 }
1119
1120 case Primitive::kPrimShort:
1121 case Primitive::kPrimChar: {
1122 Register value = locations->InAt(1).AsX86().AsCpuRegister();
1123 __ movw(Address(obj, offset), value);
1124 break;
1125 }
1126
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001127 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001128 case Primitive::kPrimNot: {
1129 Register value = locations->InAt(1).AsX86().AsCpuRegister();
1130 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001131
1132 if (field_type == Primitive::kPrimNot) {
1133 Register temp = locations->GetTemp(0).AsX86().AsCpuRegister();
1134 Register card = locations->GetTemp(1).AsX86().AsCpuRegister();
1135 codegen_->MarkGCCard(temp, card, obj, value);
1136 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001137 break;
1138 }
1139
1140 case Primitive::kPrimLong: {
1141 X86ManagedRegister value = locations->InAt(1).AsX86();
1142 __ movl(Address(obj, offset), value.AsRegisterPairLow());
1143 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh());
1144 break;
1145 }
1146
1147 case Primitive::kPrimFloat:
1148 case Primitive::kPrimDouble:
1149 LOG(FATAL) << "Unimplemented register type " << field_type;
1150
1151 case Primitive::kPrimVoid:
1152 LOG(FATAL) << "Unreachable type " << field_type;
1153 }
1154}
1155
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001156void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
1157 Label is_null;
1158 __ testl(value, value);
1159 __ j(kEqual, &is_null);
1160 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
1161 __ movl(temp, object);
1162 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
1163 __ movb(Address(temp, card, TIMES_1, 0),
1164 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
1165 __ Bind(&is_null);
1166}
1167
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001168void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001169 LocationSummary* locations =
1170 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001171 locations->SetInAt(0, Location::RequiresRegister());
1172 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001173}
1174
1175void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1176 LocationSummary* locations = instruction->GetLocations();
1177 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1178 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
1179
1180 switch (instruction->GetType()) {
1181 case Primitive::kPrimBoolean: {
1182 Register out = locations->Out().AsX86().AsCpuRegister();
1183 __ movzxb(out, Address(obj, offset));
1184 break;
1185 }
1186
1187 case Primitive::kPrimByte: {
1188 Register out = locations->Out().AsX86().AsCpuRegister();
1189 __ movsxb(out, Address(obj, offset));
1190 break;
1191 }
1192
1193 case Primitive::kPrimShort: {
1194 Register out = locations->Out().AsX86().AsCpuRegister();
1195 __ movsxw(out, Address(obj, offset));
1196 break;
1197 }
1198
1199 case Primitive::kPrimChar: {
1200 Register out = locations->Out().AsX86().AsCpuRegister();
1201 __ movzxw(out, Address(obj, offset));
1202 break;
1203 }
1204
1205 case Primitive::kPrimInt:
1206 case Primitive::kPrimNot: {
1207 Register out = locations->Out().AsX86().AsCpuRegister();
1208 __ movl(out, Address(obj, offset));
1209 break;
1210 }
1211
1212 case Primitive::kPrimLong: {
1213 // TODO: support volatile.
1214 X86ManagedRegister out = locations->Out().AsX86();
1215 __ movl(out.AsRegisterPairLow(), Address(obj, offset));
1216 __ movl(out.AsRegisterPairHigh(), Address(obj, kX86WordSize + offset));
1217 break;
1218 }
1219
1220 case Primitive::kPrimFloat:
1221 case Primitive::kPrimDouble:
1222 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1223
1224 case Primitive::kPrimVoid:
1225 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1226 }
1227}
1228
1229void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001230 LocationSummary* locations =
1231 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001232 locations->SetInAt(0, Location::Any());
1233 // TODO: Have a normalization phase that makes this instruction never used.
1234 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001235}
1236
1237void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001238 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001239 codegen_->AddSlowPath(slow_path);
1240
1241 LocationSummary* locations = instruction->GetLocations();
1242 Location obj = locations->InAt(0);
1243 DCHECK(obj.Equals(locations->Out()));
1244
1245 if (obj.IsRegister()) {
1246 __ cmpl(obj.AsX86().AsCpuRegister(), Immediate(0));
1247 } else {
1248 DCHECK(locations->InAt(0).IsStackSlot());
1249 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
1250 }
1251 __ j(kEqual, slow_path->GetEntryLabel());
1252}
1253
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001254void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001255 LocationSummary* locations =
1256 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001257 locations->SetInAt(0, Location::RequiresRegister());
1258 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1259 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001260}
1261
1262void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
1263 LocationSummary* locations = instruction->GetLocations();
1264 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1265 Location index = locations->InAt(1);
1266
1267 switch (instruction->GetType()) {
1268 case Primitive::kPrimBoolean: {
1269 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1270 Register out = locations->Out().AsX86().AsCpuRegister();
1271 if (index.IsConstant()) {
1272 __ movzxb(out, Address(obj,
1273 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1274 } else {
1275 __ movzxb(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_1, data_offset));
1276 }
1277 break;
1278 }
1279
1280 case Primitive::kPrimByte: {
1281 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1282 Register out = locations->Out().AsX86().AsCpuRegister();
1283 if (index.IsConstant()) {
1284 __ movsxb(out, Address(obj,
1285 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1286 } else {
1287 __ movsxb(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_1, data_offset));
1288 }
1289 break;
1290 }
1291
1292 case Primitive::kPrimShort: {
1293 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1294 Register out = locations->Out().AsX86().AsCpuRegister();
1295 if (index.IsConstant()) {
1296 __ movsxw(out, Address(obj,
1297 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1298 } else {
1299 __ movsxw(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_2, data_offset));
1300 }
1301 break;
1302 }
1303
1304 case Primitive::kPrimChar: {
1305 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1306 Register out = locations->Out().AsX86().AsCpuRegister();
1307 if (index.IsConstant()) {
1308 __ movzxw(out, Address(obj,
1309 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1310 } else {
1311 __ movzxw(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_2, data_offset));
1312 }
1313 break;
1314 }
1315
1316 case Primitive::kPrimInt:
1317 case Primitive::kPrimNot: {
1318 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1319 Register out = locations->Out().AsX86().AsCpuRegister();
1320 if (index.IsConstant()) {
1321 __ movl(out, Address(obj,
1322 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1323 } else {
1324 __ movl(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_4, data_offset));
1325 }
1326 break;
1327 }
1328
1329 case Primitive::kPrimLong: {
1330 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1331 X86ManagedRegister out = locations->Out().AsX86();
1332 if (index.IsConstant()) {
1333 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1334 __ movl(out.AsRegisterPairLow(), Address(obj, offset));
1335 __ movl(out.AsRegisterPairHigh(), Address(obj, offset + kX86WordSize));
1336 } else {
1337 __ movl(out.AsRegisterPairLow(),
1338 Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset));
1339 __ movl(out.AsRegisterPairHigh(),
1340 Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset + kX86WordSize));
1341 }
1342 break;
1343 }
1344
1345 case Primitive::kPrimFloat:
1346 case Primitive::kPrimDouble:
1347 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1348
1349 case Primitive::kPrimVoid:
1350 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1351 }
1352}
1353
1354void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001355 Primitive::Type value_type = instruction->GetComponentType();
1356 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1357 instruction,
1358 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
1359
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001360 if (value_type == Primitive::kPrimNot) {
1361 InvokeRuntimeCallingConvention calling_convention;
1362 locations->SetInAt(0, X86CpuLocation(calling_convention.GetRegisterAt(0)));
1363 locations->SetInAt(1, X86CpuLocation(calling_convention.GetRegisterAt(1)));
1364 locations->SetInAt(2, X86CpuLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001365 } else {
1366 locations->SetInAt(0, Location::RequiresRegister());
1367 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1368 if (value_type == Primitive::kPrimBoolean || value_type == Primitive::kPrimByte) {
1369 // Ensure the value is in a byte register.
1370 locations->SetInAt(2, X86CpuLocation(EAX));
1371 } else {
1372 locations->SetInAt(2, Location::RequiresRegister());
1373 }
1374 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001375}
1376
1377void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
1378 LocationSummary* locations = instruction->GetLocations();
1379 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1380 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001381 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001382
1383 switch (value_type) {
1384 case Primitive::kPrimBoolean:
1385 case Primitive::kPrimByte: {
1386 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1387 ByteRegister value = locations->InAt(2).AsX86().AsByteRegister();
1388 if (index.IsConstant()) {
1389 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1390 __ movb(Address(obj, offset), value);
1391 } else {
1392 __ movb(Address(obj, index.AsX86().AsCpuRegister(), TIMES_1, data_offset), value);
1393 }
1394 break;
1395 }
1396
1397 case Primitive::kPrimShort:
1398 case Primitive::kPrimChar: {
1399 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1400 Register value = locations->InAt(2).AsX86().AsCpuRegister();
1401 if (index.IsConstant()) {
1402 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1403 __ movw(Address(obj, offset), value);
1404 } else {
1405 __ movw(Address(obj, index.AsX86().AsCpuRegister(), TIMES_2, data_offset), value);
1406 }
1407 break;
1408 }
1409
1410 case Primitive::kPrimInt: {
1411 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1412 Register value = locations->InAt(2).AsX86().AsCpuRegister();
1413 if (index.IsConstant()) {
1414 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1415 __ movl(Address(obj, offset), value);
1416 } else {
1417 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_4, data_offset), value);
1418 }
1419 break;
1420 }
1421
1422 case Primitive::kPrimNot: {
1423 DCHECK(!codegen_->IsLeafMethod());
1424 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001425 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001426 break;
1427 }
1428
1429 case Primitive::kPrimLong: {
1430 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1431 X86ManagedRegister value = locations->InAt(2).AsX86();
1432 if (index.IsConstant()) {
1433 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1434 __ movl(Address(obj, offset), value.AsRegisterPairLow());
1435 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh());
1436 } else {
1437 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset),
1438 value.AsRegisterPairLow());
1439 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset + kX86WordSize),
1440 value.AsRegisterPairHigh());
1441 }
1442 break;
1443 }
1444
1445 case Primitive::kPrimFloat:
1446 case Primitive::kPrimDouble:
1447 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1448
1449 case Primitive::kPrimVoid:
1450 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1451 }
1452}
1453
1454void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
1455 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1456 locations->SetInAt(0, Location::RequiresRegister());
1457 locations->SetOut(Location::RequiresRegister());
1458 instruction->SetLocations(locations);
1459}
1460
1461void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
1462 LocationSummary* locations = instruction->GetLocations();
1463 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1464 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1465 Register out = locations->Out().AsX86().AsCpuRegister();
1466 __ movl(out, Address(obj, offset));
1467}
1468
1469void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001470 LocationSummary* locations =
1471 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001472 locations->SetInAt(0, Location::RequiresRegister());
1473 locations->SetInAt(1, Location::RequiresRegister());
1474 // TODO: Have a normalization phase that makes this instruction never used.
1475 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001476}
1477
1478void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
1479 LocationSummary* locations = instruction->GetLocations();
1480 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01001481 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001482 codegen_->AddSlowPath(slow_path);
1483
1484 Register index = locations->InAt(0).AsX86().AsCpuRegister();
1485 Register length = locations->InAt(1).AsX86().AsCpuRegister();
1486
1487 __ cmpl(index, length);
1488 __ j(kAboveEqual, slow_path->GetEntryLabel());
1489}
1490
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001491void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
1492 temp->SetLocations(nullptr);
1493}
1494
1495void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
1496 // Nothing to do, this is driven by the code generator.
1497}
1498
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001499void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001500 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001501}
1502
1503void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001504 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
1505}
1506
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001507void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
1508 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
1509}
1510
1511void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
1512 SuspendCheckSlowPathX86* slow_path =
1513 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction);
1514 codegen_->AddSlowPath(slow_path);
1515 __ fs()->cmpl(Address::Absolute(
1516 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
1517 __ j(kNotEqual, slow_path->GetEntryLabel());
1518 __ Bind(slow_path->GetReturnLabel());
1519}
1520
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001521X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
1522 return codegen_->GetAssembler();
1523}
1524
1525void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
1526 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001527 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001528 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1529 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
1530 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
1531}
1532
1533void ParallelMoveResolverX86::EmitMove(size_t index) {
1534 MoveOperands* move = moves_.Get(index);
1535 Location source = move->GetSource();
1536 Location destination = move->GetDestination();
1537
1538 if (source.IsRegister()) {
1539 if (destination.IsRegister()) {
1540 __ movl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister());
1541 } else {
1542 DCHECK(destination.IsStackSlot());
1543 __ movl(Address(ESP, destination.GetStackIndex()), source.AsX86().AsCpuRegister());
1544 }
1545 } else if (source.IsStackSlot()) {
1546 if (destination.IsRegister()) {
1547 __ movl(destination.AsX86().AsCpuRegister(), Address(ESP, source.GetStackIndex()));
1548 } else {
1549 DCHECK(destination.IsStackSlot());
1550 MoveMemoryToMemory(destination.GetStackIndex(),
1551 source.GetStackIndex());
1552 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001553 } else if (source.IsConstant()) {
1554 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
1555 Immediate imm(instruction->AsIntConstant()->GetValue());
1556 if (destination.IsRegister()) {
1557 __ movl(destination.AsX86().AsCpuRegister(), imm);
1558 } else {
1559 __ movl(Address(ESP, destination.GetStackIndex()), imm);
1560 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001561 } else {
1562 LOG(FATAL) << "Unimplemented";
1563 }
1564}
1565
1566void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001567 Register suggested_scratch = reg == EAX ? EBX : EAX;
1568 ScratchRegisterScope ensure_scratch(
1569 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
1570
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001571 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1572 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
1573 __ movl(Address(ESP, mem + stack_offset), reg);
1574 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
1575}
1576
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001577void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
1578 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001579 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
1580
1581 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001582 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001583 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
1584
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001585 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
1586 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
1587 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
1588 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
1589 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
1590 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
1591}
1592
1593void ParallelMoveResolverX86::EmitSwap(size_t index) {
1594 MoveOperands* move = moves_.Get(index);
1595 Location source = move->GetSource();
1596 Location destination = move->GetDestination();
1597
1598 if (source.IsRegister() && destination.IsRegister()) {
1599 __ xchgl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister());
1600 } else if (source.IsRegister() && destination.IsStackSlot()) {
1601 Exchange(source.AsX86().AsCpuRegister(), destination.GetStackIndex());
1602 } else if (source.IsStackSlot() && destination.IsRegister()) {
1603 Exchange(destination.AsX86().AsCpuRegister(), source.GetStackIndex());
1604 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
1605 Exchange(destination.GetStackIndex(), source.GetStackIndex());
1606 } else {
1607 LOG(FATAL) << "Unimplemented";
1608 }
1609}
1610
1611void ParallelMoveResolverX86::SpillScratch(int reg) {
1612 __ pushl(static_cast<Register>(reg));
1613}
1614
1615void ParallelMoveResolverX86::RestoreScratch(int reg) {
1616 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001617}
1618
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001619} // namespace x86
1620} // namespace art