blob: 9b36a9705099407de19b9f5e63c70e0d9ef29810 [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()) {
554 // Materialized condition, compare against 0
555 Location lhs = if_instr->GetLocations()->InAt(0);
556 if (lhs.IsRegister()) {
557 __ cmpl(lhs.AsX86().AsCpuRegister(), Immediate(0));
558 } else {
559 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
560 }
561 __ j(kEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100562 } else {
Dave Allison20dfc792014-06-16 20:44:29 -0700563 Location lhs = condition->GetLocations()->InAt(0);
564 Location rhs = condition->GetLocations()->InAt(1);
565 // LHS is guaranteed to be in a register (see LocationsBuilderX86::VisitCondition).
566 if (rhs.IsRegister()) {
567 __ cmpl(lhs.AsX86().AsCpuRegister(), rhs.AsX86().AsCpuRegister());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100568 } else if (rhs.IsConstant()) {
569 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
570 Immediate imm(instruction->AsIntConstant()->GetValue());
571 __ cmpl(lhs.AsX86().AsCpuRegister(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700572 } else {
573 __ cmpl(lhs.AsX86().AsCpuRegister(), Address(ESP, rhs.GetStackIndex()));
574 }
575 __ j(X86Condition(condition->GetCondition()),
576 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100577 }
Dave Allison20dfc792014-06-16 20:44:29 -0700578 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfFalseSuccessor())) {
579 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000580 }
581}
582
583void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000584 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000585}
586
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000587void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
588 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000589}
590
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000591void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100592 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000593}
594
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000595void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100596 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000597}
598
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100599void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100600 LocationSummary* locations =
601 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100602 switch (store->InputAt(1)->GetType()) {
603 case Primitive::kPrimBoolean:
604 case Primitive::kPrimByte:
605 case Primitive::kPrimChar:
606 case Primitive::kPrimShort:
607 case Primitive::kPrimInt:
608 case Primitive::kPrimNot:
609 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
610 break;
611
612 case Primitive::kPrimLong:
613 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
614 break;
615
616 default:
617 LOG(FATAL) << "Unimplemented local type " << store->InputAt(1)->GetType();
618 }
619 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000620}
621
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000622void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000623}
624
Dave Allison20dfc792014-06-16 20:44:29 -0700625void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100626 LocationSummary* locations =
627 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100628 locations->SetInAt(0, Location::RequiresRegister());
629 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100630 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100631 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100632 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000633}
634
Dave Allison20dfc792014-06-16 20:44:29 -0700635void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
636 if (comp->NeedsMaterialization()) {
637 LocationSummary* locations = comp->GetLocations();
638 if (locations->InAt(1).IsRegister()) {
639 __ cmpl(locations->InAt(0).AsX86().AsCpuRegister(),
640 locations->InAt(1).AsX86().AsCpuRegister());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100641 } else if (locations->InAt(1).IsConstant()) {
642 HConstant* instruction = locations->InAt(1).GetConstant();
643 Immediate imm(instruction->AsIntConstant()->GetValue());
644 __ cmpl(locations->InAt(0).AsX86().AsCpuRegister(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700645 } else {
646 __ cmpl(locations->InAt(0).AsX86().AsCpuRegister(),
647 Address(ESP, locations->InAt(1).GetStackIndex()));
648 }
649 __ setb(X86Condition(comp->GetCondition()), locations->Out().AsX86().AsCpuRegister());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100650 }
Dave Allison20dfc792014-06-16 20:44:29 -0700651}
652
653void LocationsBuilderX86::VisitEqual(HEqual* comp) {
654 VisitCondition(comp);
655}
656
657void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
658 VisitCondition(comp);
659}
660
661void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
662 VisitCondition(comp);
663}
664
665void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
666 VisitCondition(comp);
667}
668
669void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
670 VisitCondition(comp);
671}
672
673void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
674 VisitCondition(comp);
675}
676
677void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
678 VisitCondition(comp);
679}
680
681void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
682 VisitCondition(comp);
683}
684
685void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
686 VisitCondition(comp);
687}
688
689void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
690 VisitCondition(comp);
691}
692
693void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
694 VisitCondition(comp);
695}
696
697void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
698 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000699}
700
701void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100702 LocationSummary* locations =
703 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100704 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000705}
706
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000707void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000708}
709
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100710void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100711 LocationSummary* locations =
712 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100713 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100714}
715
716void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
717 // Will be generated at use site.
718}
719
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000720void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000721 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000722}
723
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000724void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
725 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000726 __ ret();
727}
728
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000729void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100730 LocationSummary* locations =
731 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100732 switch (ret->InputAt(0)->GetType()) {
733 case Primitive::kPrimBoolean:
734 case Primitive::kPrimByte:
735 case Primitive::kPrimChar:
736 case Primitive::kPrimShort:
737 case Primitive::kPrimInt:
738 case Primitive::kPrimNot:
739 locations->SetInAt(0, X86CpuLocation(EAX));
740 break;
741
742 case Primitive::kPrimLong:
743 locations->SetInAt(
744 0, Location::RegisterLocation(X86ManagedRegister::FromRegisterPair(EAX_EDX)));
745 break;
746
747 default:
748 LOG(FATAL) << "Unimplemented return type " << ret->InputAt(0)->GetType();
749 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000750}
751
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000752void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100753 if (kIsDebugBuild) {
754 switch (ret->InputAt(0)->GetType()) {
755 case Primitive::kPrimBoolean:
756 case Primitive::kPrimByte:
757 case Primitive::kPrimChar:
758 case Primitive::kPrimShort:
759 case Primitive::kPrimInt:
760 case Primitive::kPrimNot:
761 DCHECK_EQ(ret->GetLocations()->InAt(0).AsX86().AsCpuRegister(), EAX);
762 break;
763
764 case Primitive::kPrimLong:
765 DCHECK_EQ(ret->GetLocations()->InAt(0).AsX86().AsRegisterPair(), EAX_EDX);
766 break;
767
768 default:
769 LOG(FATAL) << "Unimplemented return type " << ret->InputAt(0)->GetType();
770 }
771 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000772 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000773 __ ret();
774}
775
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000776void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100777 HandleInvoke(invoke);
778}
779
780void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
781 Register temp = invoke->GetLocations()->GetTemp(0).AsX86().AsCpuRegister();
782 uint32_t heap_reference_size = sizeof(mirror::HeapReference<mirror::Object>);
783 size_t index_in_cache = mirror::Array::DataOffset(heap_reference_size).Int32Value() +
784 invoke->GetIndexInDexCache() * kX86WordSize;
785
786 // TODO: Implement all kinds of calls:
787 // 1) boot -> boot
788 // 2) app -> boot
789 // 3) app -> app
790 //
791 // Currently we implement the app -> app logic, which looks up in the resolve cache.
792
793 // temp = method;
794 LoadCurrentMethod(temp);
795 // temp = temp->dex_cache_resolved_methods_;
796 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
797 // temp = temp[index_in_cache]
798 __ movl(temp, Address(temp, index_in_cache));
799 // (temp + offset_of_quick_compiled_code)()
800 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
801
802 DCHECK(!codegen_->IsLeafMethod());
803 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
804}
805
806void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
807 HandleInvoke(invoke);
808}
809
810void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100811 LocationSummary* locations =
812 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100813 locations->AddTemp(X86CpuLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100814
815 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100816 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100817 HInstruction* input = invoke->InputAt(i);
818 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
819 }
820
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100821 switch (invoke->GetType()) {
822 case Primitive::kPrimBoolean:
823 case Primitive::kPrimByte:
824 case Primitive::kPrimChar:
825 case Primitive::kPrimShort:
826 case Primitive::kPrimInt:
827 case Primitive::kPrimNot:
828 locations->SetOut(X86CpuLocation(EAX));
829 break;
830
831 case Primitive::kPrimLong:
832 locations->SetOut(Location::RegisterLocation(X86ManagedRegister::FromRegisterPair(EAX_EDX)));
833 break;
834
835 case Primitive::kPrimVoid:
836 break;
837
838 case Primitive::kPrimDouble:
839 case Primitive::kPrimFloat:
840 LOG(FATAL) << "Unimplemented return type " << invoke->GetType();
841 break;
842 }
843
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000844 invoke->SetLocations(locations);
845}
846
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100847void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100848 Register temp = invoke->GetLocations()->GetTemp(0).AsX86().AsCpuRegister();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100849 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
850 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
851 LocationSummary* locations = invoke->GetLocations();
852 Location receiver = locations->InAt(0);
853 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
854 // temp = object->GetClass();
855 if (receiver.IsStackSlot()) {
856 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
857 __ movl(temp, Address(temp, class_offset));
858 } else {
859 __ movl(temp, Address(receiver.AsX86().AsCpuRegister(), class_offset));
860 }
861 // temp = temp->GetMethodAt(method_offset);
862 __ movl(temp, Address(temp, method_offset));
863 // call temp->GetEntryPoint();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000864 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
865
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100866 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100867 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000868}
869
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000870void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100871 LocationSummary* locations =
872 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000873 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100874 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100875 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100876 locations->SetInAt(0, Location::RequiresRegister());
877 locations->SetInAt(1, Location::Any());
878 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100879 break;
880 }
881
882 case Primitive::kPrimBoolean:
883 case Primitive::kPrimByte:
884 case Primitive::kPrimChar:
885 case Primitive::kPrimShort:
886 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
887 break;
888
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000889 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100890 LOG(FATAL) << "Unimplemented add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000891 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000892}
893
894void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
895 LocationSummary* locations = add->GetLocations();
896 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100897 case Primitive::kPrimInt: {
898 DCHECK_EQ(locations->InAt(0).AsX86().AsCpuRegister(),
899 locations->Out().AsX86().AsCpuRegister());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100900 if (locations->InAt(1).IsRegister()) {
901 __ addl(locations->InAt(0).AsX86().AsCpuRegister(),
902 locations->InAt(1).AsX86().AsCpuRegister());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100903 } else if (locations->InAt(1).IsConstant()) {
904 HConstant* instruction = locations->InAt(1).GetConstant();
905 Immediate imm(instruction->AsIntConstant()->GetValue());
906 __ addl(locations->InAt(0).AsX86().AsCpuRegister(), imm);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100907 } else {
908 __ addl(locations->InAt(0).AsX86().AsCpuRegister(),
909 Address(ESP, locations->InAt(1).GetStackIndex()));
910 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000911 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100912 }
913
914 case Primitive::kPrimLong: {
915 DCHECK_EQ(locations->InAt(0).AsX86().AsRegisterPair(),
916 locations->Out().AsX86().AsRegisterPair());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100917 if (locations->InAt(1).IsRegister()) {
918 __ addl(locations->InAt(0).AsX86().AsRegisterPairLow(),
919 locations->InAt(1).AsX86().AsRegisterPairLow());
920 __ adcl(locations->InAt(0).AsX86().AsRegisterPairHigh(),
921 locations->InAt(1).AsX86().AsRegisterPairHigh());
922 } else {
923 __ addl(locations->InAt(0).AsX86().AsRegisterPairLow(),
924 Address(ESP, locations->InAt(1).GetStackIndex()));
925 __ adcl(locations->InAt(0).AsX86().AsRegisterPairHigh(),
926 Address(ESP, locations->InAt(1).GetHighStackIndex(kX86WordSize)));
927 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100928 break;
929 }
930
931 case Primitive::kPrimBoolean:
932 case Primitive::kPrimByte:
933 case Primitive::kPrimChar:
934 case Primitive::kPrimShort:
935 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
936 break;
937
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000938 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100939 LOG(FATAL) << "Unimplemented add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000940 }
941}
942
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100943void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100944 LocationSummary* locations =
945 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100946 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100947 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100948 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100949 locations->SetInAt(0, Location::RequiresRegister());
950 locations->SetInAt(1, Location::Any());
951 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100952 break;
953 }
954
955 case Primitive::kPrimBoolean:
956 case Primitive::kPrimByte:
957 case Primitive::kPrimChar:
958 case Primitive::kPrimShort:
959 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
960 break;
961
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100962 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100963 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100964 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100965}
966
967void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
968 LocationSummary* locations = sub->GetLocations();
969 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100970 case Primitive::kPrimInt: {
971 DCHECK_EQ(locations->InAt(0).AsX86().AsCpuRegister(),
972 locations->Out().AsX86().AsCpuRegister());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100973 if (locations->InAt(1).IsRegister()) {
974 __ subl(locations->InAt(0).AsX86().AsCpuRegister(),
975 locations->InAt(1).AsX86().AsCpuRegister());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100976 } else if (locations->InAt(1).IsConstant()) {
977 HConstant* instruction = locations->InAt(1).GetConstant();
978 Immediate imm(instruction->AsIntConstant()->GetValue());
979 __ subl(locations->InAt(0).AsX86().AsCpuRegister(), imm);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100980 } else {
981 __ subl(locations->InAt(0).AsX86().AsCpuRegister(),
982 Address(ESP, locations->InAt(1).GetStackIndex()));
983 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100984 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100985 }
986
987 case Primitive::kPrimLong: {
988 DCHECK_EQ(locations->InAt(0).AsX86().AsRegisterPair(),
989 locations->Out().AsX86().AsRegisterPair());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100990 if (locations->InAt(1).IsRegister()) {
991 __ subl(locations->InAt(0).AsX86().AsRegisterPairLow(),
992 locations->InAt(1).AsX86().AsRegisterPairLow());
993 __ sbbl(locations->InAt(0).AsX86().AsRegisterPairHigh(),
994 locations->InAt(1).AsX86().AsRegisterPairHigh());
995 } else {
996 __ subl(locations->InAt(0).AsX86().AsRegisterPairLow(),
997 Address(ESP, locations->InAt(1).GetStackIndex()));
998 __ sbbl(locations->InAt(0).AsX86().AsRegisterPairHigh(),
999 Address(ESP, locations->InAt(1).GetHighStackIndex(kX86WordSize)));
1000 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001001 break;
1002 }
1003
1004 case Primitive::kPrimBoolean:
1005 case Primitive::kPrimByte:
1006 case Primitive::kPrimChar:
1007 case Primitive::kPrimShort:
1008 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
1009 break;
1010
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001011 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001012 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001013 }
1014}
1015
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001016void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001017 LocationSummary* locations =
1018 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001019 locations->SetOut(X86CpuLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001020 InvokeRuntimeCallingConvention calling_convention;
1021 locations->AddTemp(X86CpuLocation(calling_convention.GetRegisterAt(0)));
1022 locations->AddTemp(X86CpuLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001023}
1024
1025void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
1026 InvokeRuntimeCallingConvention calling_convention;
1027 LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001028 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001029
Nicolas Geoffray707c8092014-04-04 10:50:14 +01001030 __ fs()->call(
1031 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001032
Nicolas Geoffray39468442014-09-02 15:17:15 +01001033 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001034 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001035}
1036
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001037void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001038 LocationSummary* locations =
1039 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001040 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1041 if (location.IsStackSlot()) {
1042 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1043 } else if (location.IsDoubleStackSlot()) {
1044 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001045 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001046 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001047}
1048
1049void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001050}
1051
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001052void LocationsBuilderX86::VisitNot(HNot* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001053 LocationSummary* locations =
1054 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001055 locations->SetInAt(0, Location::RequiresRegister());
1056 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001057}
1058
1059void InstructionCodeGeneratorX86::VisitNot(HNot* instruction) {
1060 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001061 Location out = locations->Out();
1062 DCHECK_EQ(locations->InAt(0).AsX86().AsCpuRegister(), out.AsX86().AsCpuRegister());
1063 __ xorl(out.AsX86().AsCpuRegister(), Immediate(1));
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001064}
1065
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001066void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001067 LocationSummary* locations =
1068 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001069 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001070 locations->SetInAt(1, Location::Any());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001071 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001072}
1073
1074void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
1075 Label greater, done;
1076 LocationSummary* locations = compare->GetLocations();
1077 switch (compare->InputAt(0)->GetType()) {
1078 case Primitive::kPrimLong: {
1079 Label less, greater, done;
1080 Register output = locations->Out().AsX86().AsCpuRegister();
1081 X86ManagedRegister left = locations->InAt(0).AsX86();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001082 Location right = locations->InAt(1);
1083 if (right.IsRegister()) {
1084 __ cmpl(left.AsRegisterPairHigh(), right.AsX86().AsRegisterPairHigh());
1085 } else {
1086 DCHECK(right.IsDoubleStackSlot());
1087 __ cmpl(left.AsRegisterPairHigh(), Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1088 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001089 __ j(kLess, &less); // Signed compare.
1090 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001091 if (right.IsRegister()) {
1092 __ cmpl(left.AsRegisterPairLow(), right.AsX86().AsRegisterPairLow());
1093 } else {
1094 DCHECK(right.IsDoubleStackSlot());
1095 __ cmpl(left.AsRegisterPairLow(), Address(ESP, right.GetStackIndex()));
1096 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001097 __ movl(output, Immediate(0));
1098 __ j(kEqual, &done);
1099 __ j(kBelow, &less); // Unsigned compare.
1100
1101 __ Bind(&greater);
1102 __ movl(output, Immediate(1));
1103 __ jmp(&done);
1104
1105 __ Bind(&less);
1106 __ movl(output, Immediate(-1));
1107
1108 __ Bind(&done);
1109 break;
1110 }
1111 default:
1112 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
1113 }
1114}
1115
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001116void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001117 LocationSummary* locations =
1118 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001119 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1120 locations->SetInAt(i, Location::Any());
1121 }
1122 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001123}
1124
1125void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001126 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001127}
1128
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001129void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001130 LocationSummary* locations =
1131 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001132 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001133 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001134 if (field_type == Primitive::kPrimBoolean || field_type == Primitive::kPrimByte) {
1135 // Ensure the value is in a byte register.
1136 locations->SetInAt(1, X86CpuLocation(EAX));
1137 } else {
1138 locations->SetInAt(1, Location::RequiresRegister());
1139 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001140 // Temporary registers for the write barrier.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001141 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001142 locations->AddTemp(Location::RequiresRegister());
1143 // Ensure the card is in a byte register.
1144 locations->AddTemp(X86CpuLocation(ECX));
1145 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001146}
1147
1148void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1149 LocationSummary* locations = instruction->GetLocations();
1150 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1151 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001152 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001153
1154 switch (field_type) {
1155 case Primitive::kPrimBoolean:
1156 case Primitive::kPrimByte: {
1157 ByteRegister value = locations->InAt(1).AsX86().AsByteRegister();
1158 __ movb(Address(obj, offset), value);
1159 break;
1160 }
1161
1162 case Primitive::kPrimShort:
1163 case Primitive::kPrimChar: {
1164 Register value = locations->InAt(1).AsX86().AsCpuRegister();
1165 __ movw(Address(obj, offset), value);
1166 break;
1167 }
1168
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001169 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001170 case Primitive::kPrimNot: {
1171 Register value = locations->InAt(1).AsX86().AsCpuRegister();
1172 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001173
1174 if (field_type == Primitive::kPrimNot) {
1175 Register temp = locations->GetTemp(0).AsX86().AsCpuRegister();
1176 Register card = locations->GetTemp(1).AsX86().AsCpuRegister();
1177 codegen_->MarkGCCard(temp, card, obj, value);
1178 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001179 break;
1180 }
1181
1182 case Primitive::kPrimLong: {
1183 X86ManagedRegister value = locations->InAt(1).AsX86();
1184 __ movl(Address(obj, offset), value.AsRegisterPairLow());
1185 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh());
1186 break;
1187 }
1188
1189 case Primitive::kPrimFloat:
1190 case Primitive::kPrimDouble:
1191 LOG(FATAL) << "Unimplemented register type " << field_type;
1192
1193 case Primitive::kPrimVoid:
1194 LOG(FATAL) << "Unreachable type " << field_type;
1195 }
1196}
1197
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001198void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
1199 Label is_null;
1200 __ testl(value, value);
1201 __ j(kEqual, &is_null);
1202 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
1203 __ movl(temp, object);
1204 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
1205 __ movb(Address(temp, card, TIMES_1, 0),
1206 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
1207 __ Bind(&is_null);
1208}
1209
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001210void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001211 LocationSummary* locations =
1212 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001213 locations->SetInAt(0, Location::RequiresRegister());
1214 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001215}
1216
1217void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1218 LocationSummary* locations = instruction->GetLocations();
1219 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1220 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
1221
1222 switch (instruction->GetType()) {
1223 case Primitive::kPrimBoolean: {
1224 Register out = locations->Out().AsX86().AsCpuRegister();
1225 __ movzxb(out, Address(obj, offset));
1226 break;
1227 }
1228
1229 case Primitive::kPrimByte: {
1230 Register out = locations->Out().AsX86().AsCpuRegister();
1231 __ movsxb(out, Address(obj, offset));
1232 break;
1233 }
1234
1235 case Primitive::kPrimShort: {
1236 Register out = locations->Out().AsX86().AsCpuRegister();
1237 __ movsxw(out, Address(obj, offset));
1238 break;
1239 }
1240
1241 case Primitive::kPrimChar: {
1242 Register out = locations->Out().AsX86().AsCpuRegister();
1243 __ movzxw(out, Address(obj, offset));
1244 break;
1245 }
1246
1247 case Primitive::kPrimInt:
1248 case Primitive::kPrimNot: {
1249 Register out = locations->Out().AsX86().AsCpuRegister();
1250 __ movl(out, Address(obj, offset));
1251 break;
1252 }
1253
1254 case Primitive::kPrimLong: {
1255 // TODO: support volatile.
1256 X86ManagedRegister out = locations->Out().AsX86();
1257 __ movl(out.AsRegisterPairLow(), Address(obj, offset));
1258 __ movl(out.AsRegisterPairHigh(), Address(obj, kX86WordSize + offset));
1259 break;
1260 }
1261
1262 case Primitive::kPrimFloat:
1263 case Primitive::kPrimDouble:
1264 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1265
1266 case Primitive::kPrimVoid:
1267 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1268 }
1269}
1270
1271void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001272 LocationSummary* locations =
1273 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001274 locations->SetInAt(0, Location::Any());
1275 // TODO: Have a normalization phase that makes this instruction never used.
1276 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001277}
1278
1279void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001280 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001281 codegen_->AddSlowPath(slow_path);
1282
1283 LocationSummary* locations = instruction->GetLocations();
1284 Location obj = locations->InAt(0);
1285 DCHECK(obj.Equals(locations->Out()));
1286
1287 if (obj.IsRegister()) {
1288 __ cmpl(obj.AsX86().AsCpuRegister(), Immediate(0));
1289 } else {
1290 DCHECK(locations->InAt(0).IsStackSlot());
1291 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
1292 }
1293 __ j(kEqual, slow_path->GetEntryLabel());
1294}
1295
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001296void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001297 LocationSummary* locations =
1298 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001299 locations->SetInAt(0, Location::RequiresRegister());
1300 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1301 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001302}
1303
1304void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
1305 LocationSummary* locations = instruction->GetLocations();
1306 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1307 Location index = locations->InAt(1);
1308
1309 switch (instruction->GetType()) {
1310 case Primitive::kPrimBoolean: {
1311 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1312 Register out = locations->Out().AsX86().AsCpuRegister();
1313 if (index.IsConstant()) {
1314 __ movzxb(out, Address(obj,
1315 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1316 } else {
1317 __ movzxb(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_1, data_offset));
1318 }
1319 break;
1320 }
1321
1322 case Primitive::kPrimByte: {
1323 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1324 Register out = locations->Out().AsX86().AsCpuRegister();
1325 if (index.IsConstant()) {
1326 __ movsxb(out, Address(obj,
1327 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1328 } else {
1329 __ movsxb(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_1, data_offset));
1330 }
1331 break;
1332 }
1333
1334 case Primitive::kPrimShort: {
1335 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1336 Register out = locations->Out().AsX86().AsCpuRegister();
1337 if (index.IsConstant()) {
1338 __ movsxw(out, Address(obj,
1339 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1340 } else {
1341 __ movsxw(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_2, data_offset));
1342 }
1343 break;
1344 }
1345
1346 case Primitive::kPrimChar: {
1347 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1348 Register out = locations->Out().AsX86().AsCpuRegister();
1349 if (index.IsConstant()) {
1350 __ movzxw(out, Address(obj,
1351 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1352 } else {
1353 __ movzxw(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_2, data_offset));
1354 }
1355 break;
1356 }
1357
1358 case Primitive::kPrimInt:
1359 case Primitive::kPrimNot: {
1360 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1361 Register out = locations->Out().AsX86().AsCpuRegister();
1362 if (index.IsConstant()) {
1363 __ movl(out, Address(obj,
1364 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1365 } else {
1366 __ movl(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_4, data_offset));
1367 }
1368 break;
1369 }
1370
1371 case Primitive::kPrimLong: {
1372 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1373 X86ManagedRegister out = locations->Out().AsX86();
1374 if (index.IsConstant()) {
1375 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1376 __ movl(out.AsRegisterPairLow(), Address(obj, offset));
1377 __ movl(out.AsRegisterPairHigh(), Address(obj, offset + kX86WordSize));
1378 } else {
1379 __ movl(out.AsRegisterPairLow(),
1380 Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset));
1381 __ movl(out.AsRegisterPairHigh(),
1382 Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset + kX86WordSize));
1383 }
1384 break;
1385 }
1386
1387 case Primitive::kPrimFloat:
1388 case Primitive::kPrimDouble:
1389 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1390
1391 case Primitive::kPrimVoid:
1392 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1393 }
1394}
1395
1396void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001397 Primitive::Type value_type = instruction->GetComponentType();
1398 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1399 instruction,
1400 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
1401
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001402 if (value_type == Primitive::kPrimNot) {
1403 InvokeRuntimeCallingConvention calling_convention;
1404 locations->SetInAt(0, X86CpuLocation(calling_convention.GetRegisterAt(0)));
1405 locations->SetInAt(1, X86CpuLocation(calling_convention.GetRegisterAt(1)));
1406 locations->SetInAt(2, X86CpuLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001407 } else {
1408 locations->SetInAt(0, Location::RequiresRegister());
1409 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1410 if (value_type == Primitive::kPrimBoolean || value_type == Primitive::kPrimByte) {
1411 // Ensure the value is in a byte register.
1412 locations->SetInAt(2, X86CpuLocation(EAX));
1413 } else {
1414 locations->SetInAt(2, Location::RequiresRegister());
1415 }
1416 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001417}
1418
1419void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
1420 LocationSummary* locations = instruction->GetLocations();
1421 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1422 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001423 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001424
1425 switch (value_type) {
1426 case Primitive::kPrimBoolean:
1427 case Primitive::kPrimByte: {
1428 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1429 ByteRegister value = locations->InAt(2).AsX86().AsByteRegister();
1430 if (index.IsConstant()) {
1431 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1432 __ movb(Address(obj, offset), value);
1433 } else {
1434 __ movb(Address(obj, index.AsX86().AsCpuRegister(), TIMES_1, data_offset), value);
1435 }
1436 break;
1437 }
1438
1439 case Primitive::kPrimShort:
1440 case Primitive::kPrimChar: {
1441 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1442 Register value = locations->InAt(2).AsX86().AsCpuRegister();
1443 if (index.IsConstant()) {
1444 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1445 __ movw(Address(obj, offset), value);
1446 } else {
1447 __ movw(Address(obj, index.AsX86().AsCpuRegister(), TIMES_2, data_offset), value);
1448 }
1449 break;
1450 }
1451
1452 case Primitive::kPrimInt: {
1453 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1454 Register value = locations->InAt(2).AsX86().AsCpuRegister();
1455 if (index.IsConstant()) {
1456 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1457 __ movl(Address(obj, offset), value);
1458 } else {
1459 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_4, data_offset), value);
1460 }
1461 break;
1462 }
1463
1464 case Primitive::kPrimNot: {
1465 DCHECK(!codegen_->IsLeafMethod());
1466 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001467 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001468 break;
1469 }
1470
1471 case Primitive::kPrimLong: {
1472 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1473 X86ManagedRegister value = locations->InAt(2).AsX86();
1474 if (index.IsConstant()) {
1475 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1476 __ movl(Address(obj, offset), value.AsRegisterPairLow());
1477 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh());
1478 } else {
1479 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset),
1480 value.AsRegisterPairLow());
1481 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset + kX86WordSize),
1482 value.AsRegisterPairHigh());
1483 }
1484 break;
1485 }
1486
1487 case Primitive::kPrimFloat:
1488 case Primitive::kPrimDouble:
1489 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1490
1491 case Primitive::kPrimVoid:
1492 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1493 }
1494}
1495
1496void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
1497 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1498 locations->SetInAt(0, Location::RequiresRegister());
1499 locations->SetOut(Location::RequiresRegister());
1500 instruction->SetLocations(locations);
1501}
1502
1503void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
1504 LocationSummary* locations = instruction->GetLocations();
1505 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1506 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1507 Register out = locations->Out().AsX86().AsCpuRegister();
1508 __ movl(out, Address(obj, offset));
1509}
1510
1511void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001512 LocationSummary* locations =
1513 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001514 locations->SetInAt(0, Location::RequiresRegister());
1515 locations->SetInAt(1, Location::RequiresRegister());
1516 // TODO: Have a normalization phase that makes this instruction never used.
1517 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001518}
1519
1520void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
1521 LocationSummary* locations = instruction->GetLocations();
1522 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01001523 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001524 codegen_->AddSlowPath(slow_path);
1525
1526 Register index = locations->InAt(0).AsX86().AsCpuRegister();
1527 Register length = locations->InAt(1).AsX86().AsCpuRegister();
1528
1529 __ cmpl(index, length);
1530 __ j(kAboveEqual, slow_path->GetEntryLabel());
1531}
1532
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001533void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
1534 temp->SetLocations(nullptr);
1535}
1536
1537void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
1538 // Nothing to do, this is driven by the code generator.
1539}
1540
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001541void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001542 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001543}
1544
1545void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001546 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
1547}
1548
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001549void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
1550 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
1551}
1552
1553void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
1554 SuspendCheckSlowPathX86* slow_path =
1555 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction);
1556 codegen_->AddSlowPath(slow_path);
1557 __ fs()->cmpl(Address::Absolute(
1558 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
1559 __ j(kNotEqual, slow_path->GetEntryLabel());
1560 __ Bind(slow_path->GetReturnLabel());
1561}
1562
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001563X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
1564 return codegen_->GetAssembler();
1565}
1566
1567void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
1568 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001569 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001570 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1571 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
1572 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
1573}
1574
1575void ParallelMoveResolverX86::EmitMove(size_t index) {
1576 MoveOperands* move = moves_.Get(index);
1577 Location source = move->GetSource();
1578 Location destination = move->GetDestination();
1579
1580 if (source.IsRegister()) {
1581 if (destination.IsRegister()) {
1582 __ movl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister());
1583 } else {
1584 DCHECK(destination.IsStackSlot());
1585 __ movl(Address(ESP, destination.GetStackIndex()), source.AsX86().AsCpuRegister());
1586 }
1587 } else if (source.IsStackSlot()) {
1588 if (destination.IsRegister()) {
1589 __ movl(destination.AsX86().AsCpuRegister(), Address(ESP, source.GetStackIndex()));
1590 } else {
1591 DCHECK(destination.IsStackSlot());
1592 MoveMemoryToMemory(destination.GetStackIndex(),
1593 source.GetStackIndex());
1594 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001595 } else if (source.IsConstant()) {
1596 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
1597 Immediate imm(instruction->AsIntConstant()->GetValue());
1598 if (destination.IsRegister()) {
1599 __ movl(destination.AsX86().AsCpuRegister(), imm);
1600 } else {
1601 __ movl(Address(ESP, destination.GetStackIndex()), imm);
1602 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001603 } else {
1604 LOG(FATAL) << "Unimplemented";
1605 }
1606}
1607
1608void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001609 Register suggested_scratch = reg == EAX ? EBX : EAX;
1610 ScratchRegisterScope ensure_scratch(
1611 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
1612
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001613 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1614 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
1615 __ movl(Address(ESP, mem + stack_offset), reg);
1616 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
1617}
1618
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001619void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
1620 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001621 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
1622
1623 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001624 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001625 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
1626
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001627 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
1628 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
1629 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
1630 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
1631 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
1632 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
1633}
1634
1635void ParallelMoveResolverX86::EmitSwap(size_t index) {
1636 MoveOperands* move = moves_.Get(index);
1637 Location source = move->GetSource();
1638 Location destination = move->GetDestination();
1639
1640 if (source.IsRegister() && destination.IsRegister()) {
1641 __ xchgl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister());
1642 } else if (source.IsRegister() && destination.IsStackSlot()) {
1643 Exchange(source.AsX86().AsCpuRegister(), destination.GetStackIndex());
1644 } else if (source.IsStackSlot() && destination.IsRegister()) {
1645 Exchange(destination.AsX86().AsCpuRegister(), source.GetStackIndex());
1646 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
1647 Exchange(destination.GetStackIndex(), source.GetStackIndex());
1648 } else {
1649 LOG(FATAL) << "Unimplemented";
1650 }
1651}
1652
1653void ParallelMoveResolverX86::SpillScratch(int reg) {
1654 __ pushl(static_cast<Register>(reg));
1655}
1656
1657void ParallelMoveResolverX86::RestoreScratch(int reg) {
1658 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001659}
1660
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001661} // namespace x86
1662} // namespace art