blob: 0db4311f03b7f24e5d27fa121399625404e47ceb [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);
50
51class InvokeRuntimeCallingConvention : public CallingConvention<Register> {
52 public:
53 InvokeRuntimeCallingConvention()
54 : CallingConvention(kRuntimeParameterCoreRegisters,
55 kRuntimeParameterCoreRegistersLength) {}
56
57 private:
58 DISALLOW_COPY_AND_ASSIGN(InvokeRuntimeCallingConvention);
59};
60
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061#define __ reinterpret_cast<X86Assembler*>(codegen->GetAssembler())->
62
63class NullCheckSlowPathX86 : public SlowPathCode {
64 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010065 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010066
67 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
68 __ Bind(GetEntryLabel());
69 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowNullPointer)));
Nicolas Geoffray39468442014-09-02 15:17:15 +010070 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffraye5038322014-07-04 09:41:32 +010071 }
72
73 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010074 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010075 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
76};
77
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010078class StackOverflowCheckSlowPathX86 : public SlowPathCode {
79 public:
80 StackOverflowCheckSlowPathX86() {}
81
82 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
83 __ Bind(GetEntryLabel());
84 __ addl(ESP,
85 Immediate(codegen->GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
86 __ fs()->jmp(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowStackOverflow)));
87 }
88
89 private:
90 DISALLOW_COPY_AND_ASSIGN(StackOverflowCheckSlowPathX86);
91};
92
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010093class BoundsCheckSlowPathX86 : public SlowPathCode {
94 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010095 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction,
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010096 Location index_location,
97 Location length_location)
Nicolas Geoffray39468442014-09-02 15:17:15 +010098 : instruction_(instruction), index_location_(index_location), length_location_(length_location) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010099
100 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
101 CodeGeneratorX86* x86_codegen = reinterpret_cast<CodeGeneratorX86*>(codegen);
102 __ Bind(GetEntryLabel());
103 InvokeRuntimeCallingConvention calling_convention;
104 x86_codegen->Move32(X86CpuLocation(calling_convention.GetRegisterAt(0)), index_location_);
105 x86_codegen->Move32(X86CpuLocation(calling_convention.GetRegisterAt(1)), length_location_);
106 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pThrowArrayBounds)));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100107 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100108 }
109
110 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100111 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100112 const Location index_location_;
113 const Location length_location_;
114
115 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
116};
117
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000118class SuspendCheckSlowPathX86 : public SlowPathCode {
119 public:
120 explicit SuspendCheckSlowPathX86(HSuspendCheck* instruction)
121 : instruction_(instruction) {}
122
123 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
124 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100125 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000126 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
127 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100128 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000129 __ jmp(GetReturnLabel());
130 }
131
132 Label* GetReturnLabel() { return &return_label_; }
133
134 private:
135 HSuspendCheck* const instruction_;
136 Label return_label_;
137
138 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
139};
140
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100141#undef __
142#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
143
Dave Allison20dfc792014-06-16 20:44:29 -0700144inline Condition X86Condition(IfCondition cond) {
145 switch (cond) {
146 case kCondEQ: return kEqual;
147 case kCondNE: return kNotEqual;
148 case kCondLT: return kLess;
149 case kCondLE: return kLessEqual;
150 case kCondGT: return kGreater;
151 case kCondGE: return kGreaterEqual;
152 default:
153 LOG(FATAL) << "Unknown if condition";
154 }
155 return kEqual;
156}
157
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100158void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
159 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
160}
161
162void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
163 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
164}
165
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100166void CodeGeneratorX86::SaveCoreRegister(Location stack_location, uint32_t reg_id) {
167 __ movl(Address(ESP, stack_location.GetStackIndex()), static_cast<Register>(reg_id));
168}
169
170void CodeGeneratorX86::RestoreCoreRegister(Location stack_location, uint32_t reg_id) {
171 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_location.GetStackIndex()));
172}
173
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100174CodeGeneratorX86::CodeGeneratorX86(HGraph* graph)
175 : CodeGenerator(graph, kNumberOfRegIds),
176 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100177 instruction_visitor_(graph, this),
178 move_resolver_(graph->GetArena(), this) {}
179
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100180size_t CodeGeneratorX86::FrameEntrySpillSize() const {
181 return kNumberOfPushedRegistersAtEntry * kX86WordSize;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100182}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100183
184static bool* GetBlockedRegisterPairs(bool* blocked_registers) {
185 return blocked_registers + kNumberOfAllocIds;
186}
187
188ManagedRegister CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type,
189 bool* blocked_registers) const {
190 switch (type) {
191 case Primitive::kPrimLong: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100192 bool* blocked_register_pairs = GetBlockedRegisterPairs(blocked_registers);
193 size_t reg = AllocateFreeRegisterInternal(blocked_register_pairs, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100194 X86ManagedRegister pair =
195 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
196 blocked_registers[pair.AsRegisterPairLow()] = true;
197 blocked_registers[pair.AsRegisterPairHigh()] = true;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100198 // Block all other register pairs that share a register with `pair`.
199 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
200 X86ManagedRegister current =
201 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
202 if (current.AsRegisterPairLow() == pair.AsRegisterPairLow()
203 || current.AsRegisterPairLow() == pair.AsRegisterPairHigh()
204 || current.AsRegisterPairHigh() == pair.AsRegisterPairLow()
205 || current.AsRegisterPairHigh() == pair.AsRegisterPairHigh()) {
206 blocked_register_pairs[i] = true;
207 }
208 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100209 return pair;
210 }
211
212 case Primitive::kPrimByte:
213 case Primitive::kPrimBoolean:
214 case Primitive::kPrimChar:
215 case Primitive::kPrimShort:
216 case Primitive::kPrimInt:
217 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100218 Register reg = static_cast<Register>(
219 AllocateFreeRegisterInternal(blocked_registers, kNumberOfCpuRegisters));
220 // Block all register pairs that contain `reg`.
221 bool* blocked_register_pairs = GetBlockedRegisterPairs(blocked_registers);
222 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
223 X86ManagedRegister current =
224 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
225 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
226 blocked_register_pairs[i] = true;
227 }
228 }
229 return X86ManagedRegister::FromCpuRegister(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100230 }
231
232 case Primitive::kPrimFloat:
233 case Primitive::kPrimDouble:
234 LOG(FATAL) << "Unimplemented register type " << type;
235
236 case Primitive::kPrimVoid:
237 LOG(FATAL) << "Unreachable type " << type;
238 }
239
240 return ManagedRegister::NoRegister();
241}
242
243void CodeGeneratorX86::SetupBlockedRegisters(bool* blocked_registers) const {
244 bool* blocked_register_pairs = GetBlockedRegisterPairs(blocked_registers);
245
246 // Don't allocate the dalvik style register pair passing.
247 blocked_register_pairs[ECX_EDX] = true;
248
249 // Stack register is always reserved.
250 blocked_registers[ESP] = true;
251
252 // TODO: We currently don't use Quick's callee saved registers.
253 blocked_registers[EBP] = true;
254 blocked_registers[ESI] = true;
255 blocked_registers[EDI] = true;
256 blocked_register_pairs[EAX_EDI] = true;
257 blocked_register_pairs[EDX_EDI] = true;
258 blocked_register_pairs[ECX_EDI] = true;
259 blocked_register_pairs[EBX_EDI] = true;
260}
261
262size_t CodeGeneratorX86::GetNumberOfRegisters() const {
263 return kNumberOfRegIds;
264}
265
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100266InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
267 : HGraphVisitor(graph),
268 assembler_(codegen->GetAssembler()),
269 codegen_(codegen) {}
270
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000271void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000272 // Create a fake register to mimic Quick.
273 static const int kFakeReturnRegister = 8;
274 core_spill_mask_ |= (1 << kFakeReturnRegister);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000275
Dave Allison648d7112014-07-25 16:15:27 -0700276 bool skip_overflow_check = IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100277 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
278 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100279 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100280 }
281
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100282 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100283 __ subl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100284
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100285 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
286 SlowPathCode* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86();
287 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100288
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100289 __ fs()->cmpl(ESP, Address::Absolute(Thread::StackEndOffset<kX86WordSize>()));
290 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100291 }
292
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100293 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000294}
295
296void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100297 __ addl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000298}
299
300void CodeGeneratorX86::Bind(Label* label) {
301 __ Bind(label);
302}
303
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000304void InstructionCodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100305 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000306}
307
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100308Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
309 switch (load->GetType()) {
310 case Primitive::kPrimLong:
311 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
312 break;
313
314 case Primitive::kPrimInt:
315 case Primitive::kPrimNot:
316 return Location::StackSlot(GetStackSlot(load->GetLocal()));
317
318 case Primitive::kPrimFloat:
319 case Primitive::kPrimDouble:
320 LOG(FATAL) << "Unimplemented type " << load->GetType();
321
322 case Primitive::kPrimBoolean:
323 case Primitive::kPrimByte:
324 case Primitive::kPrimChar:
325 case Primitive::kPrimShort:
326 case Primitive::kPrimVoid:
327 LOG(FATAL) << "Unexpected type " << load->GetType();
328 }
329
330 LOG(FATAL) << "Unreachable";
331 return Location();
332}
333
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100334Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
335 switch (type) {
336 case Primitive::kPrimBoolean:
337 case Primitive::kPrimByte:
338 case Primitive::kPrimChar:
339 case Primitive::kPrimShort:
340 case Primitive::kPrimInt:
341 case Primitive::kPrimNot: {
342 uint32_t index = gp_index_++;
343 if (index < calling_convention.GetNumberOfRegisters()) {
344 return X86CpuLocation(calling_convention.GetRegisterAt(index));
345 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100346 return Location::StackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100347 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100348 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100349
350 case Primitive::kPrimLong: {
351 uint32_t index = gp_index_;
352 gp_index_ += 2;
353 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
354 return Location::RegisterLocation(X86ManagedRegister::FromRegisterPair(
355 calling_convention.GetRegisterPairAt(index)));
356 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
357 return Location::QuickParameter(index);
358 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100359 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100360 }
361 }
362
363 case Primitive::kPrimDouble:
364 case Primitive::kPrimFloat:
365 LOG(FATAL) << "Unimplemented parameter type " << type;
366 break;
367
368 case Primitive::kPrimVoid:
369 LOG(FATAL) << "Unexpected parameter type " << type;
370 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100371 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100372 return Location();
373}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100374
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100375void CodeGeneratorX86::Move32(Location destination, Location source) {
376 if (source.Equals(destination)) {
377 return;
378 }
379 if (destination.IsRegister()) {
380 if (source.IsRegister()) {
381 __ movl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister());
382 } else {
383 DCHECK(source.IsStackSlot());
384 __ movl(destination.AsX86().AsCpuRegister(), Address(ESP, source.GetStackIndex()));
385 }
386 } else {
387 if (source.IsRegister()) {
388 __ movl(Address(ESP, destination.GetStackIndex()), source.AsX86().AsCpuRegister());
389 } else {
390 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100391 __ pushl(Address(ESP, source.GetStackIndex()));
392 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100393 }
394 }
395}
396
397void CodeGeneratorX86::Move64(Location destination, Location source) {
398 if (source.Equals(destination)) {
399 return;
400 }
401 if (destination.IsRegister()) {
402 if (source.IsRegister()) {
403 __ movl(destination.AsX86().AsRegisterPairLow(), source.AsX86().AsRegisterPairLow());
404 __ movl(destination.AsX86().AsRegisterPairHigh(), source.AsX86().AsRegisterPairHigh());
405 } else if (source.IsQuickParameter()) {
406 uint32_t argument_index = source.GetQuickParameterIndex();
407 InvokeDexCallingConvention calling_convention;
408 __ movl(destination.AsX86().AsRegisterPairLow(),
409 calling_convention.GetRegisterAt(argument_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100410 __ movl(destination.AsX86().AsRegisterPairHigh(), Address(ESP,
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100411 calling_convention.GetStackOffsetOf(argument_index + 1) + GetFrameSize()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100412 } else {
413 DCHECK(source.IsDoubleStackSlot());
414 __ movl(destination.AsX86().AsRegisterPairLow(), Address(ESP, source.GetStackIndex()));
415 __ movl(destination.AsX86().AsRegisterPairHigh(),
416 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
417 }
418 } else if (destination.IsQuickParameter()) {
419 InvokeDexCallingConvention calling_convention;
420 uint32_t argument_index = destination.GetQuickParameterIndex();
421 if (source.IsRegister()) {
422 __ movl(calling_convention.GetRegisterAt(argument_index), source.AsX86().AsRegisterPairLow());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100423 __ movl(Address(ESP, calling_convention.GetStackOffsetOf(argument_index + 1)),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100424 source.AsX86().AsRegisterPairHigh());
425 } else {
426 DCHECK(source.IsDoubleStackSlot());
427 __ movl(calling_convention.GetRegisterAt(argument_index),
428 Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100429 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100430 __ popl(Address(ESP, calling_convention.GetStackOffsetOf(argument_index + 1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100431 }
432 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100433 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100434 if (source.IsRegister()) {
435 __ movl(Address(ESP, destination.GetStackIndex()), source.AsX86().AsRegisterPairLow());
436 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
437 source.AsX86().AsRegisterPairHigh());
438 } else if (source.IsQuickParameter()) {
439 InvokeDexCallingConvention calling_convention;
440 uint32_t argument_index = source.GetQuickParameterIndex();
441 __ movl(Address(ESP, destination.GetStackIndex()),
442 calling_convention.GetRegisterAt(argument_index));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100443 __ pushl(Address(ESP,
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100444 calling_convention.GetStackOffsetOf(argument_index + 1) + GetFrameSize()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100445 __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100446 } else {
447 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100448 __ pushl(Address(ESP, source.GetStackIndex()));
449 __ popl(Address(ESP, destination.GetStackIndex()));
450 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
451 __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100452 }
453 }
454}
455
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100456void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
457 if (instruction->AsIntConstant() != nullptr) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100458 Immediate imm(instruction->AsIntConstant()->GetValue());
459 if (location.IsRegister()) {
460 __ movl(location.AsX86().AsCpuRegister(), imm);
461 } else {
462 __ movl(Address(ESP, location.GetStackIndex()), imm);
463 }
464 } else if (instruction->AsLongConstant() != nullptr) {
465 int64_t value = instruction->AsLongConstant()->GetValue();
466 if (location.IsRegister()) {
467 __ movl(location.AsX86().AsRegisterPairLow(), Immediate(Low32Bits(value)));
468 __ movl(location.AsX86().AsRegisterPairHigh(), Immediate(High32Bits(value)));
469 } else {
470 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
471 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
472 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100473 } else if (instruction->AsLoadLocal() != nullptr) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100474 switch (instruction->GetType()) {
475 case Primitive::kPrimBoolean:
476 case Primitive::kPrimByte:
477 case Primitive::kPrimChar:
478 case Primitive::kPrimShort:
479 case Primitive::kPrimInt:
480 case Primitive::kPrimNot:
481 Move32(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
482 break;
483
484 case Primitive::kPrimLong:
485 Move64(location, Location::DoubleStackSlot(
486 GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
487 break;
488
489 default:
490 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
491 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000492 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100493 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100494 switch (instruction->GetType()) {
495 case Primitive::kPrimBoolean:
496 case Primitive::kPrimByte:
497 case Primitive::kPrimChar:
498 case Primitive::kPrimShort:
499 case Primitive::kPrimInt:
500 case Primitive::kPrimNot:
501 Move32(location, instruction->GetLocations()->Out());
502 break;
503
504 case Primitive::kPrimLong:
505 Move64(location, instruction->GetLocations()->Out());
506 break;
507
508 default:
509 LOG(FATAL) << "Unimplemented type " << instruction->GetType();
510 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000511 }
512}
513
514void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000515 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000516}
517
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000518void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000519 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000520 if (GetGraph()->GetExitBlock() == successor) {
521 codegen_->GenerateFrameExit();
522 } else if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
523 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000524 }
525}
526
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000527void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000528 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000529}
530
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000531void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000532 if (kIsDebugBuild) {
533 __ Comment("Unreachable");
534 __ int3();
535 }
536}
537
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000538void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100539 LocationSummary* locations =
540 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100541 HInstruction* cond = if_instr->InputAt(0);
542 DCHECK(cond->IsCondition());
543 HCondition* condition = cond->AsCondition();
544 if (condition->NeedsMaterialization()) {
545 locations->SetInAt(0, Location::Any());
546 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000547}
548
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000549void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700550 HInstruction* cond = if_instr->InputAt(0);
551 DCHECK(cond->IsCondition());
552 HCondition* condition = cond->AsCondition();
553 if (condition->NeedsMaterialization()) {
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100554 // Moves do not affect the eflags register, so if the condition is evaluated
555 // just before the if, we don't need to evaluate it again.
556 if (!condition->IsBeforeWhenDisregardMoves(if_instr)) {
557 // Materialized condition, compare against 0
558 Location lhs = if_instr->GetLocations()->InAt(0);
559 if (lhs.IsRegister()) {
560 __ cmpl(lhs.AsX86().AsCpuRegister(), Immediate(0));
561 } else {
562 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
563 }
Dave Allison20dfc792014-06-16 20:44:29 -0700564 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100565 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100566 } else {
Dave Allison20dfc792014-06-16 20:44:29 -0700567 Location lhs = condition->GetLocations()->InAt(0);
568 Location rhs = condition->GetLocations()->InAt(1);
569 // LHS is guaranteed to be in a register (see LocationsBuilderX86::VisitCondition).
570 if (rhs.IsRegister()) {
571 __ cmpl(lhs.AsX86().AsCpuRegister(), rhs.AsX86().AsCpuRegister());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100572 } else if (rhs.IsConstant()) {
573 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
574 Immediate imm(instruction->AsIntConstant()->GetValue());
575 __ cmpl(lhs.AsX86().AsCpuRegister(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700576 } else {
577 __ cmpl(lhs.AsX86().AsCpuRegister(), Address(ESP, rhs.GetStackIndex()));
578 }
579 __ j(X86Condition(condition->GetCondition()),
580 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100581 }
Dave Allison20dfc792014-06-16 20:44:29 -0700582 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfFalseSuccessor())) {
583 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000584 }
585}
586
587void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000588 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000589}
590
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000591void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
592 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000593}
594
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000595void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100596 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000597}
598
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000599void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100600 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000601}
602
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100603void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100604 LocationSummary* locations =
605 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100606 switch (store->InputAt(1)->GetType()) {
607 case Primitive::kPrimBoolean:
608 case Primitive::kPrimByte:
609 case Primitive::kPrimChar:
610 case Primitive::kPrimShort:
611 case Primitive::kPrimInt:
612 case Primitive::kPrimNot:
613 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
614 break;
615
616 case Primitive::kPrimLong:
617 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
618 break;
619
620 default:
621 LOG(FATAL) << "Unimplemented local type " << store->InputAt(1)->GetType();
622 }
623 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000624}
625
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000626void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000627}
628
Dave Allison20dfc792014-06-16 20:44:29 -0700629void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100630 LocationSummary* locations =
631 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100632 locations->SetInAt(0, Location::RequiresRegister());
633 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100634 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100635 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100636 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000637}
638
Dave Allison20dfc792014-06-16 20:44:29 -0700639void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
640 if (comp->NeedsMaterialization()) {
641 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100642 Register reg = locations->Out().AsX86().AsCpuRegister();
643 // Clear register: setcc only sets the low byte.
644 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700645 if (locations->InAt(1).IsRegister()) {
646 __ cmpl(locations->InAt(0).AsX86().AsCpuRegister(),
647 locations->InAt(1).AsX86().AsCpuRegister());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100648 } else if (locations->InAt(1).IsConstant()) {
649 HConstant* instruction = locations->InAt(1).GetConstant();
650 Immediate imm(instruction->AsIntConstant()->GetValue());
651 __ cmpl(locations->InAt(0).AsX86().AsCpuRegister(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700652 } else {
653 __ cmpl(locations->InAt(0).AsX86().AsCpuRegister(),
654 Address(ESP, locations->InAt(1).GetStackIndex()));
655 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100656 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100657 }
Dave Allison20dfc792014-06-16 20:44:29 -0700658}
659
660void LocationsBuilderX86::VisitEqual(HEqual* comp) {
661 VisitCondition(comp);
662}
663
664void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
665 VisitCondition(comp);
666}
667
668void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
669 VisitCondition(comp);
670}
671
672void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
673 VisitCondition(comp);
674}
675
676void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
677 VisitCondition(comp);
678}
679
680void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
681 VisitCondition(comp);
682}
683
684void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
685 VisitCondition(comp);
686}
687
688void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
689 VisitCondition(comp);
690}
691
692void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
693 VisitCondition(comp);
694}
695
696void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
697 VisitCondition(comp);
698}
699
700void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
701 VisitCondition(comp);
702}
703
704void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
705 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000706}
707
708void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100709 LocationSummary* locations =
710 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100711 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000712}
713
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000714void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000715}
716
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100717void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100718 LocationSummary* locations =
719 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100720 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100721}
722
723void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
724 // Will be generated at use site.
725}
726
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000727void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000728 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000729}
730
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000731void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
732 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000733 __ ret();
734}
735
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000736void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100737 LocationSummary* locations =
738 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100739 switch (ret->InputAt(0)->GetType()) {
740 case Primitive::kPrimBoolean:
741 case Primitive::kPrimByte:
742 case Primitive::kPrimChar:
743 case Primitive::kPrimShort:
744 case Primitive::kPrimInt:
745 case Primitive::kPrimNot:
746 locations->SetInAt(0, X86CpuLocation(EAX));
747 break;
748
749 case Primitive::kPrimLong:
750 locations->SetInAt(
751 0, Location::RegisterLocation(X86ManagedRegister::FromRegisterPair(EAX_EDX)));
752 break;
753
754 default:
755 LOG(FATAL) << "Unimplemented return type " << ret->InputAt(0)->GetType();
756 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000757}
758
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000759void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100760 if (kIsDebugBuild) {
761 switch (ret->InputAt(0)->GetType()) {
762 case Primitive::kPrimBoolean:
763 case Primitive::kPrimByte:
764 case Primitive::kPrimChar:
765 case Primitive::kPrimShort:
766 case Primitive::kPrimInt:
767 case Primitive::kPrimNot:
768 DCHECK_EQ(ret->GetLocations()->InAt(0).AsX86().AsCpuRegister(), EAX);
769 break;
770
771 case Primitive::kPrimLong:
772 DCHECK_EQ(ret->GetLocations()->InAt(0).AsX86().AsRegisterPair(), EAX_EDX);
773 break;
774
775 default:
776 LOG(FATAL) << "Unimplemented return type " << ret->InputAt(0)->GetType();
777 }
778 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000779 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000780 __ ret();
781}
782
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000783void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100784 HandleInvoke(invoke);
785}
786
787void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
788 Register temp = invoke->GetLocations()->GetTemp(0).AsX86().AsCpuRegister();
789 uint32_t heap_reference_size = sizeof(mirror::HeapReference<mirror::Object>);
790 size_t index_in_cache = mirror::Array::DataOffset(heap_reference_size).Int32Value() +
791 invoke->GetIndexInDexCache() * kX86WordSize;
792
793 // TODO: Implement all kinds of calls:
794 // 1) boot -> boot
795 // 2) app -> boot
796 // 3) app -> app
797 //
798 // Currently we implement the app -> app logic, which looks up in the resolve cache.
799
800 // temp = method;
801 LoadCurrentMethod(temp);
802 // temp = temp->dex_cache_resolved_methods_;
803 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
804 // temp = temp[index_in_cache]
805 __ movl(temp, Address(temp, index_in_cache));
806 // (temp + offset_of_quick_compiled_code)()
807 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
808
809 DCHECK(!codegen_->IsLeafMethod());
810 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
811}
812
813void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
814 HandleInvoke(invoke);
815}
816
817void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100818 LocationSummary* locations =
819 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100820 locations->AddTemp(X86CpuLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100821
822 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100823 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100824 HInstruction* input = invoke->InputAt(i);
825 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
826 }
827
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100828 switch (invoke->GetType()) {
829 case Primitive::kPrimBoolean:
830 case Primitive::kPrimByte:
831 case Primitive::kPrimChar:
832 case Primitive::kPrimShort:
833 case Primitive::kPrimInt:
834 case Primitive::kPrimNot:
835 locations->SetOut(X86CpuLocation(EAX));
836 break;
837
838 case Primitive::kPrimLong:
839 locations->SetOut(Location::RegisterLocation(X86ManagedRegister::FromRegisterPair(EAX_EDX)));
840 break;
841
842 case Primitive::kPrimVoid:
843 break;
844
845 case Primitive::kPrimDouble:
846 case Primitive::kPrimFloat:
847 LOG(FATAL) << "Unimplemented return type " << invoke->GetType();
848 break;
849 }
850
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000851 invoke->SetLocations(locations);
852}
853
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100854void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100855 Register temp = invoke->GetLocations()->GetTemp(0).AsX86().AsCpuRegister();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100856 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
857 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
858 LocationSummary* locations = invoke->GetLocations();
859 Location receiver = locations->InAt(0);
860 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
861 // temp = object->GetClass();
862 if (receiver.IsStackSlot()) {
863 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
864 __ movl(temp, Address(temp, class_offset));
865 } else {
866 __ movl(temp, Address(receiver.AsX86().AsCpuRegister(), class_offset));
867 }
868 // temp = temp->GetMethodAt(method_offset);
869 __ movl(temp, Address(temp, method_offset));
870 // call temp->GetEntryPoint();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000871 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
872
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100873 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100874 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000875}
876
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000877void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100878 LocationSummary* locations =
879 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000880 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100881 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100882 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100883 locations->SetInAt(0, Location::RequiresRegister());
884 locations->SetInAt(1, Location::Any());
885 locations->SetOut(Location::SameAsFirstInput());
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 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000899}
900
901void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
902 LocationSummary* locations = add->GetLocations();
903 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100904 case Primitive::kPrimInt: {
905 DCHECK_EQ(locations->InAt(0).AsX86().AsCpuRegister(),
906 locations->Out().AsX86().AsCpuRegister());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100907 if (locations->InAt(1).IsRegister()) {
908 __ addl(locations->InAt(0).AsX86().AsCpuRegister(),
909 locations->InAt(1).AsX86().AsCpuRegister());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100910 } else if (locations->InAt(1).IsConstant()) {
911 HConstant* instruction = locations->InAt(1).GetConstant();
912 Immediate imm(instruction->AsIntConstant()->GetValue());
913 __ addl(locations->InAt(0).AsX86().AsCpuRegister(), imm);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100914 } else {
915 __ addl(locations->InAt(0).AsX86().AsCpuRegister(),
916 Address(ESP, locations->InAt(1).GetStackIndex()));
917 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000918 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100919 }
920
921 case Primitive::kPrimLong: {
922 DCHECK_EQ(locations->InAt(0).AsX86().AsRegisterPair(),
923 locations->Out().AsX86().AsRegisterPair());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100924 if (locations->InAt(1).IsRegister()) {
925 __ addl(locations->InAt(0).AsX86().AsRegisterPairLow(),
926 locations->InAt(1).AsX86().AsRegisterPairLow());
927 __ adcl(locations->InAt(0).AsX86().AsRegisterPairHigh(),
928 locations->InAt(1).AsX86().AsRegisterPairHigh());
929 } else {
930 __ addl(locations->InAt(0).AsX86().AsRegisterPairLow(),
931 Address(ESP, locations->InAt(1).GetStackIndex()));
932 __ adcl(locations->InAt(0).AsX86().AsRegisterPairHigh(),
933 Address(ESP, locations->InAt(1).GetHighStackIndex(kX86WordSize)));
934 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100935 break;
936 }
937
938 case Primitive::kPrimBoolean:
939 case Primitive::kPrimByte:
940 case Primitive::kPrimChar:
941 case Primitive::kPrimShort:
942 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
943 break;
944
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000945 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100946 LOG(FATAL) << "Unimplemented add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000947 }
948}
949
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100950void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100951 LocationSummary* locations =
952 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100953 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100954 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100955 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100956 locations->SetInAt(0, Location::RequiresRegister());
957 locations->SetInAt(1, Location::Any());
958 locations->SetOut(Location::SameAsFirstInput());
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 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100972}
973
974void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
975 LocationSummary* locations = sub->GetLocations();
976 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100977 case Primitive::kPrimInt: {
978 DCHECK_EQ(locations->InAt(0).AsX86().AsCpuRegister(),
979 locations->Out().AsX86().AsCpuRegister());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100980 if (locations->InAt(1).IsRegister()) {
981 __ subl(locations->InAt(0).AsX86().AsCpuRegister(),
982 locations->InAt(1).AsX86().AsCpuRegister());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100983 } else if (locations->InAt(1).IsConstant()) {
984 HConstant* instruction = locations->InAt(1).GetConstant();
985 Immediate imm(instruction->AsIntConstant()->GetValue());
986 __ subl(locations->InAt(0).AsX86().AsCpuRegister(), imm);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100987 } else {
988 __ subl(locations->InAt(0).AsX86().AsCpuRegister(),
989 Address(ESP, locations->InAt(1).GetStackIndex()));
990 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100991 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100992 }
993
994 case Primitive::kPrimLong: {
995 DCHECK_EQ(locations->InAt(0).AsX86().AsRegisterPair(),
996 locations->Out().AsX86().AsRegisterPair());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100997 if (locations->InAt(1).IsRegister()) {
998 __ subl(locations->InAt(0).AsX86().AsRegisterPairLow(),
999 locations->InAt(1).AsX86().AsRegisterPairLow());
1000 __ sbbl(locations->InAt(0).AsX86().AsRegisterPairHigh(),
1001 locations->InAt(1).AsX86().AsRegisterPairHigh());
1002 } else {
1003 __ subl(locations->InAt(0).AsX86().AsRegisterPairLow(),
1004 Address(ESP, locations->InAt(1).GetStackIndex()));
1005 __ sbbl(locations->InAt(0).AsX86().AsRegisterPairHigh(),
1006 Address(ESP, locations->InAt(1).GetHighStackIndex(kX86WordSize)));
1007 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001008 break;
1009 }
1010
1011 case Primitive::kPrimBoolean:
1012 case Primitive::kPrimByte:
1013 case Primitive::kPrimChar:
1014 case Primitive::kPrimShort:
1015 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
1016 break;
1017
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001018 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001019 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001020 }
1021}
1022
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001023void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001024 LocationSummary* locations =
1025 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001026 locations->SetOut(X86CpuLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001027 InvokeRuntimeCallingConvention calling_convention;
1028 locations->AddTemp(X86CpuLocation(calling_convention.GetRegisterAt(0)));
1029 locations->AddTemp(X86CpuLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001030}
1031
1032void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
1033 InvokeRuntimeCallingConvention calling_convention;
1034 LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001035 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001036
Nicolas Geoffray707c8092014-04-04 10:50:14 +01001037 __ fs()->call(
1038 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001039
Nicolas Geoffray39468442014-09-02 15:17:15 +01001040 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001041 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001042}
1043
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001044void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001045 LocationSummary* locations =
1046 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001047 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1048 if (location.IsStackSlot()) {
1049 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1050 } else if (location.IsDoubleStackSlot()) {
1051 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001052 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001053 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001054}
1055
1056void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001057}
1058
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001059void LocationsBuilderX86::VisitNot(HNot* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001060 LocationSummary* locations =
1061 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001062 locations->SetInAt(0, Location::RequiresRegister());
1063 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001064}
1065
1066void InstructionCodeGeneratorX86::VisitNot(HNot* instruction) {
1067 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001068 Location out = locations->Out();
1069 DCHECK_EQ(locations->InAt(0).AsX86().AsCpuRegister(), out.AsX86().AsCpuRegister());
1070 __ xorl(out.AsX86().AsCpuRegister(), Immediate(1));
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001071}
1072
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001073void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001074 LocationSummary* locations =
1075 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001076 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001077 locations->SetInAt(1, Location::Any());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001078 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001079}
1080
1081void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
1082 Label greater, done;
1083 LocationSummary* locations = compare->GetLocations();
1084 switch (compare->InputAt(0)->GetType()) {
1085 case Primitive::kPrimLong: {
1086 Label less, greater, done;
1087 Register output = locations->Out().AsX86().AsCpuRegister();
1088 X86ManagedRegister left = locations->InAt(0).AsX86();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001089 Location right = locations->InAt(1);
1090 if (right.IsRegister()) {
1091 __ cmpl(left.AsRegisterPairHigh(), right.AsX86().AsRegisterPairHigh());
1092 } else {
1093 DCHECK(right.IsDoubleStackSlot());
1094 __ cmpl(left.AsRegisterPairHigh(), Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1095 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001096 __ j(kLess, &less); // Signed compare.
1097 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001098 if (right.IsRegister()) {
1099 __ cmpl(left.AsRegisterPairLow(), right.AsX86().AsRegisterPairLow());
1100 } else {
1101 DCHECK(right.IsDoubleStackSlot());
1102 __ cmpl(left.AsRegisterPairLow(), Address(ESP, right.GetStackIndex()));
1103 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001104 __ movl(output, Immediate(0));
1105 __ j(kEqual, &done);
1106 __ j(kBelow, &less); // Unsigned compare.
1107
1108 __ Bind(&greater);
1109 __ movl(output, Immediate(1));
1110 __ jmp(&done);
1111
1112 __ Bind(&less);
1113 __ movl(output, Immediate(-1));
1114
1115 __ Bind(&done);
1116 break;
1117 }
1118 default:
1119 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
1120 }
1121}
1122
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001123void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001124 LocationSummary* locations =
1125 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001126 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1127 locations->SetInAt(i, Location::Any());
1128 }
1129 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001130}
1131
1132void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001133 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001134}
1135
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001136void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001137 LocationSummary* locations =
1138 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001139 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001140 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001141 if (field_type == Primitive::kPrimBoolean || field_type == Primitive::kPrimByte) {
1142 // Ensure the value is in a byte register.
1143 locations->SetInAt(1, X86CpuLocation(EAX));
1144 } else {
1145 locations->SetInAt(1, Location::RequiresRegister());
1146 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001147 // Temporary registers for the write barrier.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001148 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001149 locations->AddTemp(Location::RequiresRegister());
1150 // Ensure the card is in a byte register.
1151 locations->AddTemp(X86CpuLocation(ECX));
1152 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001153}
1154
1155void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1156 LocationSummary* locations = instruction->GetLocations();
1157 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1158 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001159 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001160
1161 switch (field_type) {
1162 case Primitive::kPrimBoolean:
1163 case Primitive::kPrimByte: {
1164 ByteRegister value = locations->InAt(1).AsX86().AsByteRegister();
1165 __ movb(Address(obj, offset), value);
1166 break;
1167 }
1168
1169 case Primitive::kPrimShort:
1170 case Primitive::kPrimChar: {
1171 Register value = locations->InAt(1).AsX86().AsCpuRegister();
1172 __ movw(Address(obj, offset), value);
1173 break;
1174 }
1175
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001176 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001177 case Primitive::kPrimNot: {
1178 Register value = locations->InAt(1).AsX86().AsCpuRegister();
1179 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001180
1181 if (field_type == Primitive::kPrimNot) {
1182 Register temp = locations->GetTemp(0).AsX86().AsCpuRegister();
1183 Register card = locations->GetTemp(1).AsX86().AsCpuRegister();
1184 codegen_->MarkGCCard(temp, card, obj, value);
1185 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001186 break;
1187 }
1188
1189 case Primitive::kPrimLong: {
1190 X86ManagedRegister value = locations->InAt(1).AsX86();
1191 __ movl(Address(obj, offset), value.AsRegisterPairLow());
1192 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh());
1193 break;
1194 }
1195
1196 case Primitive::kPrimFloat:
1197 case Primitive::kPrimDouble:
1198 LOG(FATAL) << "Unimplemented register type " << field_type;
1199
1200 case Primitive::kPrimVoid:
1201 LOG(FATAL) << "Unreachable type " << field_type;
1202 }
1203}
1204
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001205void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
1206 Label is_null;
1207 __ testl(value, value);
1208 __ j(kEqual, &is_null);
1209 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
1210 __ movl(temp, object);
1211 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
1212 __ movb(Address(temp, card, TIMES_1, 0),
1213 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
1214 __ Bind(&is_null);
1215}
1216
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001217void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001218 LocationSummary* locations =
1219 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001220 locations->SetInAt(0, Location::RequiresRegister());
1221 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001222}
1223
1224void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1225 LocationSummary* locations = instruction->GetLocations();
1226 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1227 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
1228
1229 switch (instruction->GetType()) {
1230 case Primitive::kPrimBoolean: {
1231 Register out = locations->Out().AsX86().AsCpuRegister();
1232 __ movzxb(out, Address(obj, offset));
1233 break;
1234 }
1235
1236 case Primitive::kPrimByte: {
1237 Register out = locations->Out().AsX86().AsCpuRegister();
1238 __ movsxb(out, Address(obj, offset));
1239 break;
1240 }
1241
1242 case Primitive::kPrimShort: {
1243 Register out = locations->Out().AsX86().AsCpuRegister();
1244 __ movsxw(out, Address(obj, offset));
1245 break;
1246 }
1247
1248 case Primitive::kPrimChar: {
1249 Register out = locations->Out().AsX86().AsCpuRegister();
1250 __ movzxw(out, Address(obj, offset));
1251 break;
1252 }
1253
1254 case Primitive::kPrimInt:
1255 case Primitive::kPrimNot: {
1256 Register out = locations->Out().AsX86().AsCpuRegister();
1257 __ movl(out, Address(obj, offset));
1258 break;
1259 }
1260
1261 case Primitive::kPrimLong: {
1262 // TODO: support volatile.
1263 X86ManagedRegister out = locations->Out().AsX86();
1264 __ movl(out.AsRegisterPairLow(), Address(obj, offset));
1265 __ movl(out.AsRegisterPairHigh(), Address(obj, kX86WordSize + offset));
1266 break;
1267 }
1268
1269 case Primitive::kPrimFloat:
1270 case Primitive::kPrimDouble:
1271 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1272
1273 case Primitive::kPrimVoid:
1274 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1275 }
1276}
1277
1278void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001279 LocationSummary* locations =
1280 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001281 locations->SetInAt(0, Location::Any());
1282 // TODO: Have a normalization phase that makes this instruction never used.
1283 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001284}
1285
1286void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001287 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001288 codegen_->AddSlowPath(slow_path);
1289
1290 LocationSummary* locations = instruction->GetLocations();
1291 Location obj = locations->InAt(0);
1292 DCHECK(obj.Equals(locations->Out()));
1293
1294 if (obj.IsRegister()) {
1295 __ cmpl(obj.AsX86().AsCpuRegister(), Immediate(0));
1296 } else {
1297 DCHECK(locations->InAt(0).IsStackSlot());
1298 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
1299 }
1300 __ j(kEqual, slow_path->GetEntryLabel());
1301}
1302
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001303void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001304 LocationSummary* locations =
1305 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001306 locations->SetInAt(0, Location::RequiresRegister());
1307 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1308 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001309}
1310
1311void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
1312 LocationSummary* locations = instruction->GetLocations();
1313 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1314 Location index = locations->InAt(1);
1315
1316 switch (instruction->GetType()) {
1317 case Primitive::kPrimBoolean: {
1318 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1319 Register out = locations->Out().AsX86().AsCpuRegister();
1320 if (index.IsConstant()) {
1321 __ movzxb(out, Address(obj,
1322 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1323 } else {
1324 __ movzxb(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_1, data_offset));
1325 }
1326 break;
1327 }
1328
1329 case Primitive::kPrimByte: {
1330 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1331 Register out = locations->Out().AsX86().AsCpuRegister();
1332 if (index.IsConstant()) {
1333 __ movsxb(out, Address(obj,
1334 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1335 } else {
1336 __ movsxb(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_1, data_offset));
1337 }
1338 break;
1339 }
1340
1341 case Primitive::kPrimShort: {
1342 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1343 Register out = locations->Out().AsX86().AsCpuRegister();
1344 if (index.IsConstant()) {
1345 __ movsxw(out, Address(obj,
1346 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1347 } else {
1348 __ movsxw(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_2, data_offset));
1349 }
1350 break;
1351 }
1352
1353 case Primitive::kPrimChar: {
1354 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1355 Register out = locations->Out().AsX86().AsCpuRegister();
1356 if (index.IsConstant()) {
1357 __ movzxw(out, Address(obj,
1358 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1359 } else {
1360 __ movzxw(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_2, data_offset));
1361 }
1362 break;
1363 }
1364
1365 case Primitive::kPrimInt:
1366 case Primitive::kPrimNot: {
1367 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1368 Register out = locations->Out().AsX86().AsCpuRegister();
1369 if (index.IsConstant()) {
1370 __ movl(out, Address(obj,
1371 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1372 } else {
1373 __ movl(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_4, data_offset));
1374 }
1375 break;
1376 }
1377
1378 case Primitive::kPrimLong: {
1379 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1380 X86ManagedRegister out = locations->Out().AsX86();
1381 if (index.IsConstant()) {
1382 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1383 __ movl(out.AsRegisterPairLow(), Address(obj, offset));
1384 __ movl(out.AsRegisterPairHigh(), Address(obj, offset + kX86WordSize));
1385 } else {
1386 __ movl(out.AsRegisterPairLow(),
1387 Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset));
1388 __ movl(out.AsRegisterPairHigh(),
1389 Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset + kX86WordSize));
1390 }
1391 break;
1392 }
1393
1394 case Primitive::kPrimFloat:
1395 case Primitive::kPrimDouble:
1396 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1397
1398 case Primitive::kPrimVoid:
1399 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1400 }
1401}
1402
1403void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001404 Primitive::Type value_type = instruction->GetComponentType();
1405 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1406 instruction,
1407 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
1408
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001409 if (value_type == Primitive::kPrimNot) {
1410 InvokeRuntimeCallingConvention calling_convention;
1411 locations->SetInAt(0, X86CpuLocation(calling_convention.GetRegisterAt(0)));
1412 locations->SetInAt(1, X86CpuLocation(calling_convention.GetRegisterAt(1)));
1413 locations->SetInAt(2, X86CpuLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001414 } else {
1415 locations->SetInAt(0, Location::RequiresRegister());
1416 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1417 if (value_type == Primitive::kPrimBoolean || value_type == Primitive::kPrimByte) {
1418 // Ensure the value is in a byte register.
1419 locations->SetInAt(2, X86CpuLocation(EAX));
1420 } else {
1421 locations->SetInAt(2, Location::RequiresRegister());
1422 }
1423 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001424}
1425
1426void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
1427 LocationSummary* locations = instruction->GetLocations();
1428 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1429 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001430 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001431
1432 switch (value_type) {
1433 case Primitive::kPrimBoolean:
1434 case Primitive::kPrimByte: {
1435 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1436 ByteRegister value = locations->InAt(2).AsX86().AsByteRegister();
1437 if (index.IsConstant()) {
1438 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1439 __ movb(Address(obj, offset), value);
1440 } else {
1441 __ movb(Address(obj, index.AsX86().AsCpuRegister(), TIMES_1, data_offset), value);
1442 }
1443 break;
1444 }
1445
1446 case Primitive::kPrimShort:
1447 case Primitive::kPrimChar: {
1448 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1449 Register value = locations->InAt(2).AsX86().AsCpuRegister();
1450 if (index.IsConstant()) {
1451 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1452 __ movw(Address(obj, offset), value);
1453 } else {
1454 __ movw(Address(obj, index.AsX86().AsCpuRegister(), TIMES_2, data_offset), value);
1455 }
1456 break;
1457 }
1458
1459 case Primitive::kPrimInt: {
1460 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1461 Register value = locations->InAt(2).AsX86().AsCpuRegister();
1462 if (index.IsConstant()) {
1463 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1464 __ movl(Address(obj, offset), value);
1465 } else {
1466 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_4, data_offset), value);
1467 }
1468 break;
1469 }
1470
1471 case Primitive::kPrimNot: {
1472 DCHECK(!codegen_->IsLeafMethod());
1473 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001474 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001475 break;
1476 }
1477
1478 case Primitive::kPrimLong: {
1479 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1480 X86ManagedRegister value = locations->InAt(2).AsX86();
1481 if (index.IsConstant()) {
1482 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1483 __ movl(Address(obj, offset), value.AsRegisterPairLow());
1484 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh());
1485 } else {
1486 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset),
1487 value.AsRegisterPairLow());
1488 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset + kX86WordSize),
1489 value.AsRegisterPairHigh());
1490 }
1491 break;
1492 }
1493
1494 case Primitive::kPrimFloat:
1495 case Primitive::kPrimDouble:
1496 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1497
1498 case Primitive::kPrimVoid:
1499 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1500 }
1501}
1502
1503void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
1504 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1505 locations->SetInAt(0, Location::RequiresRegister());
1506 locations->SetOut(Location::RequiresRegister());
1507 instruction->SetLocations(locations);
1508}
1509
1510void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
1511 LocationSummary* locations = instruction->GetLocations();
1512 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1513 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1514 Register out = locations->Out().AsX86().AsCpuRegister();
1515 __ movl(out, Address(obj, offset));
1516}
1517
1518void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001519 LocationSummary* locations =
1520 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001521 locations->SetInAt(0, Location::RequiresRegister());
1522 locations->SetInAt(1, Location::RequiresRegister());
1523 // TODO: Have a normalization phase that makes this instruction never used.
1524 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001525}
1526
1527void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
1528 LocationSummary* locations = instruction->GetLocations();
1529 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01001530 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001531 codegen_->AddSlowPath(slow_path);
1532
1533 Register index = locations->InAt(0).AsX86().AsCpuRegister();
1534 Register length = locations->InAt(1).AsX86().AsCpuRegister();
1535
1536 __ cmpl(index, length);
1537 __ j(kAboveEqual, slow_path->GetEntryLabel());
1538}
1539
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001540void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
1541 temp->SetLocations(nullptr);
1542}
1543
1544void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
1545 // Nothing to do, this is driven by the code generator.
1546}
1547
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001548void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001549 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001550}
1551
1552void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001553 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
1554}
1555
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001556void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
1557 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
1558}
1559
1560void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
1561 SuspendCheckSlowPathX86* slow_path =
1562 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction);
1563 codegen_->AddSlowPath(slow_path);
1564 __ fs()->cmpl(Address::Absolute(
1565 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
1566 __ j(kNotEqual, slow_path->GetEntryLabel());
1567 __ Bind(slow_path->GetReturnLabel());
1568}
1569
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001570X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
1571 return codegen_->GetAssembler();
1572}
1573
1574void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
1575 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001576 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001577 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1578 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
1579 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
1580}
1581
1582void ParallelMoveResolverX86::EmitMove(size_t index) {
1583 MoveOperands* move = moves_.Get(index);
1584 Location source = move->GetSource();
1585 Location destination = move->GetDestination();
1586
1587 if (source.IsRegister()) {
1588 if (destination.IsRegister()) {
1589 __ movl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister());
1590 } else {
1591 DCHECK(destination.IsStackSlot());
1592 __ movl(Address(ESP, destination.GetStackIndex()), source.AsX86().AsCpuRegister());
1593 }
1594 } else if (source.IsStackSlot()) {
1595 if (destination.IsRegister()) {
1596 __ movl(destination.AsX86().AsCpuRegister(), Address(ESP, source.GetStackIndex()));
1597 } else {
1598 DCHECK(destination.IsStackSlot());
1599 MoveMemoryToMemory(destination.GetStackIndex(),
1600 source.GetStackIndex());
1601 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001602 } else if (source.IsConstant()) {
1603 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
1604 Immediate imm(instruction->AsIntConstant()->GetValue());
1605 if (destination.IsRegister()) {
1606 __ movl(destination.AsX86().AsCpuRegister(), imm);
1607 } else {
1608 __ movl(Address(ESP, destination.GetStackIndex()), imm);
1609 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001610 } else {
1611 LOG(FATAL) << "Unimplemented";
1612 }
1613}
1614
1615void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001616 Register suggested_scratch = reg == EAX ? EBX : EAX;
1617 ScratchRegisterScope ensure_scratch(
1618 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
1619
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001620 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1621 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
1622 __ movl(Address(ESP, mem + stack_offset), reg);
1623 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
1624}
1625
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001626void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
1627 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001628 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
1629
1630 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001631 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001632 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
1633
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001634 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
1635 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
1636 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
1637 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
1638 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
1639 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
1640}
1641
1642void ParallelMoveResolverX86::EmitSwap(size_t index) {
1643 MoveOperands* move = moves_.Get(index);
1644 Location source = move->GetSource();
1645 Location destination = move->GetDestination();
1646
1647 if (source.IsRegister() && destination.IsRegister()) {
1648 __ xchgl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister());
1649 } else if (source.IsRegister() && destination.IsStackSlot()) {
1650 Exchange(source.AsX86().AsCpuRegister(), destination.GetStackIndex());
1651 } else if (source.IsStackSlot() && destination.IsRegister()) {
1652 Exchange(destination.AsX86().AsCpuRegister(), source.GetStackIndex());
1653 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
1654 Exchange(destination.GetStackIndex(), source.GetStackIndex());
1655 } else {
1656 LOG(FATAL) << "Unimplemented";
1657 }
1658}
1659
1660void ParallelMoveResolverX86::SpillScratch(int reg) {
1661 __ pushl(static_cast<Register>(reg));
1662}
1663
1664void ParallelMoveResolverX86::RestoreScratch(int reg) {
1665 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001666}
1667
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001668} // namespace x86
1669} // namespace art