blob: 3383cb2117bf484857c75ddfcadac48b930bf455 [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());
125 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
126 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
127 __ jmp(GetReturnLabel());
128 }
129
130 Label* GetReturnLabel() { return &return_label_; }
131
132 private:
133 HSuspendCheck* const instruction_;
134 Label return_label_;
135
136 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
137};
138
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100139#undef __
140#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
141
Dave Allison20dfc792014-06-16 20:44:29 -0700142inline Condition X86Condition(IfCondition cond) {
143 switch (cond) {
144 case kCondEQ: return kEqual;
145 case kCondNE: return kNotEqual;
146 case kCondLT: return kLess;
147 case kCondLE: return kLessEqual;
148 case kCondGT: return kGreater;
149 case kCondGE: return kGreaterEqual;
150 default:
151 LOG(FATAL) << "Unknown if condition";
152 }
153 return kEqual;
154}
155
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100156void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
157 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
158}
159
160void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
161 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
162}
163
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100164CodeGeneratorX86::CodeGeneratorX86(HGraph* graph)
165 : CodeGenerator(graph, kNumberOfRegIds),
166 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100167 instruction_visitor_(graph, this),
168 move_resolver_(graph->GetArena(), this) {}
169
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100170size_t CodeGeneratorX86::FrameEntrySpillSize() const {
171 return kNumberOfPushedRegistersAtEntry * kX86WordSize;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100172}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100173
174static bool* GetBlockedRegisterPairs(bool* blocked_registers) {
175 return blocked_registers + kNumberOfAllocIds;
176}
177
178ManagedRegister CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type,
179 bool* blocked_registers) const {
180 switch (type) {
181 case Primitive::kPrimLong: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100182 bool* blocked_register_pairs = GetBlockedRegisterPairs(blocked_registers);
183 size_t reg = AllocateFreeRegisterInternal(blocked_register_pairs, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100184 X86ManagedRegister pair =
185 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
186 blocked_registers[pair.AsRegisterPairLow()] = true;
187 blocked_registers[pair.AsRegisterPairHigh()] = true;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100188 // Block all other register pairs that share a register with `pair`.
189 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
190 X86ManagedRegister current =
191 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
192 if (current.AsRegisterPairLow() == pair.AsRegisterPairLow()
193 || current.AsRegisterPairLow() == pair.AsRegisterPairHigh()
194 || current.AsRegisterPairHigh() == pair.AsRegisterPairLow()
195 || current.AsRegisterPairHigh() == pair.AsRegisterPairHigh()) {
196 blocked_register_pairs[i] = true;
197 }
198 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100199 return pair;
200 }
201
202 case Primitive::kPrimByte:
203 case Primitive::kPrimBoolean:
204 case Primitive::kPrimChar:
205 case Primitive::kPrimShort:
206 case Primitive::kPrimInt:
207 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100208 Register reg = static_cast<Register>(
209 AllocateFreeRegisterInternal(blocked_registers, kNumberOfCpuRegisters));
210 // Block all register pairs that contain `reg`.
211 bool* blocked_register_pairs = GetBlockedRegisterPairs(blocked_registers);
212 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
213 X86ManagedRegister current =
214 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
215 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
216 blocked_register_pairs[i] = true;
217 }
218 }
219 return X86ManagedRegister::FromCpuRegister(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100220 }
221
222 case Primitive::kPrimFloat:
223 case Primitive::kPrimDouble:
224 LOG(FATAL) << "Unimplemented register type " << type;
225
226 case Primitive::kPrimVoid:
227 LOG(FATAL) << "Unreachable type " << type;
228 }
229
230 return ManagedRegister::NoRegister();
231}
232
233void CodeGeneratorX86::SetupBlockedRegisters(bool* blocked_registers) const {
234 bool* blocked_register_pairs = GetBlockedRegisterPairs(blocked_registers);
235
236 // Don't allocate the dalvik style register pair passing.
237 blocked_register_pairs[ECX_EDX] = true;
238
239 // Stack register is always reserved.
240 blocked_registers[ESP] = true;
241
242 // TODO: We currently don't use Quick's callee saved registers.
243 blocked_registers[EBP] = true;
244 blocked_registers[ESI] = true;
245 blocked_registers[EDI] = true;
246 blocked_register_pairs[EAX_EDI] = true;
247 blocked_register_pairs[EDX_EDI] = true;
248 blocked_register_pairs[ECX_EDI] = true;
249 blocked_register_pairs[EBX_EDI] = true;
250}
251
252size_t CodeGeneratorX86::GetNumberOfRegisters() const {
253 return kNumberOfRegIds;
254}
255
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100256InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
257 : HGraphVisitor(graph),
258 assembler_(codegen->GetAssembler()),
259 codegen_(codegen) {}
260
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000261void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000262 // Create a fake register to mimic Quick.
263 static const int kFakeReturnRegister = 8;
264 core_spill_mask_ |= (1 << kFakeReturnRegister);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000265
Dave Allison648d7112014-07-25 16:15:27 -0700266 bool skip_overflow_check = IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100267 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
268 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100269 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100270 }
271
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100272 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100273 __ subl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100274
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100275 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
276 SlowPathCode* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86();
277 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100278
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100279 __ fs()->cmpl(ESP, Address::Absolute(Thread::StackEndOffset<kX86WordSize>()));
280 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100281 }
282
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100283 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000284}
285
286void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100287 __ addl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000288}
289
290void CodeGeneratorX86::Bind(Label* label) {
291 __ Bind(label);
292}
293
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000294void InstructionCodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100295 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000296}
297
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100298Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
299 switch (load->GetType()) {
300 case Primitive::kPrimLong:
301 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
302 break;
303
304 case Primitive::kPrimInt:
305 case Primitive::kPrimNot:
306 return Location::StackSlot(GetStackSlot(load->GetLocal()));
307
308 case Primitive::kPrimFloat:
309 case Primitive::kPrimDouble:
310 LOG(FATAL) << "Unimplemented type " << load->GetType();
311
312 case Primitive::kPrimBoolean:
313 case Primitive::kPrimByte:
314 case Primitive::kPrimChar:
315 case Primitive::kPrimShort:
316 case Primitive::kPrimVoid:
317 LOG(FATAL) << "Unexpected type " << load->GetType();
318 }
319
320 LOG(FATAL) << "Unreachable";
321 return Location();
322}
323
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100324Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
325 switch (type) {
326 case Primitive::kPrimBoolean:
327 case Primitive::kPrimByte:
328 case Primitive::kPrimChar:
329 case Primitive::kPrimShort:
330 case Primitive::kPrimInt:
331 case Primitive::kPrimNot: {
332 uint32_t index = gp_index_++;
333 if (index < calling_convention.GetNumberOfRegisters()) {
334 return X86CpuLocation(calling_convention.GetRegisterAt(index));
335 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100336 return Location::StackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100337 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100338 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100339
340 case Primitive::kPrimLong: {
341 uint32_t index = gp_index_;
342 gp_index_ += 2;
343 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
344 return Location::RegisterLocation(X86ManagedRegister::FromRegisterPair(
345 calling_convention.GetRegisterPairAt(index)));
346 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
347 return Location::QuickParameter(index);
348 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100349 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100350 }
351 }
352
353 case Primitive::kPrimDouble:
354 case Primitive::kPrimFloat:
355 LOG(FATAL) << "Unimplemented parameter type " << type;
356 break;
357
358 case Primitive::kPrimVoid:
359 LOG(FATAL) << "Unexpected parameter type " << type;
360 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100361 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100362 return Location();
363}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100364
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100365void CodeGeneratorX86::Move32(Location destination, Location source) {
366 if (source.Equals(destination)) {
367 return;
368 }
369 if (destination.IsRegister()) {
370 if (source.IsRegister()) {
371 __ movl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister());
372 } else {
373 DCHECK(source.IsStackSlot());
374 __ movl(destination.AsX86().AsCpuRegister(), Address(ESP, source.GetStackIndex()));
375 }
376 } else {
377 if (source.IsRegister()) {
378 __ movl(Address(ESP, destination.GetStackIndex()), source.AsX86().AsCpuRegister());
379 } else {
380 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100381 __ pushl(Address(ESP, source.GetStackIndex()));
382 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100383 }
384 }
385}
386
387void CodeGeneratorX86::Move64(Location destination, Location source) {
388 if (source.Equals(destination)) {
389 return;
390 }
391 if (destination.IsRegister()) {
392 if (source.IsRegister()) {
393 __ movl(destination.AsX86().AsRegisterPairLow(), source.AsX86().AsRegisterPairLow());
394 __ movl(destination.AsX86().AsRegisterPairHigh(), source.AsX86().AsRegisterPairHigh());
395 } else if (source.IsQuickParameter()) {
396 uint32_t argument_index = source.GetQuickParameterIndex();
397 InvokeDexCallingConvention calling_convention;
398 __ movl(destination.AsX86().AsRegisterPairLow(),
399 calling_convention.GetRegisterAt(argument_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100400 __ movl(destination.AsX86().AsRegisterPairHigh(), Address(ESP,
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100401 calling_convention.GetStackOffsetOf(argument_index + 1) + GetFrameSize()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100402 } else {
403 DCHECK(source.IsDoubleStackSlot());
404 __ movl(destination.AsX86().AsRegisterPairLow(), Address(ESP, source.GetStackIndex()));
405 __ movl(destination.AsX86().AsRegisterPairHigh(),
406 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
407 }
408 } else if (destination.IsQuickParameter()) {
409 InvokeDexCallingConvention calling_convention;
410 uint32_t argument_index = destination.GetQuickParameterIndex();
411 if (source.IsRegister()) {
412 __ movl(calling_convention.GetRegisterAt(argument_index), source.AsX86().AsRegisterPairLow());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100413 __ movl(Address(ESP, calling_convention.GetStackOffsetOf(argument_index + 1)),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100414 source.AsX86().AsRegisterPairHigh());
415 } else {
416 DCHECK(source.IsDoubleStackSlot());
417 __ movl(calling_convention.GetRegisterAt(argument_index),
418 Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100419 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100420 __ popl(Address(ESP, calling_convention.GetStackOffsetOf(argument_index + 1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100421 }
422 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100423 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100424 if (source.IsRegister()) {
425 __ movl(Address(ESP, destination.GetStackIndex()), source.AsX86().AsRegisterPairLow());
426 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
427 source.AsX86().AsRegisterPairHigh());
428 } else if (source.IsQuickParameter()) {
429 InvokeDexCallingConvention calling_convention;
430 uint32_t argument_index = source.GetQuickParameterIndex();
431 __ movl(Address(ESP, destination.GetStackIndex()),
432 calling_convention.GetRegisterAt(argument_index));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100433 __ pushl(Address(ESP,
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100434 calling_convention.GetStackOffsetOf(argument_index + 1) + GetFrameSize()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100435 __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100436 } else {
437 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100438 __ pushl(Address(ESP, source.GetStackIndex()));
439 __ popl(Address(ESP, destination.GetStackIndex()));
440 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
441 __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100442 }
443 }
444}
445
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100446void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
447 if (instruction->AsIntConstant() != nullptr) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100448 Immediate imm(instruction->AsIntConstant()->GetValue());
449 if (location.IsRegister()) {
450 __ movl(location.AsX86().AsCpuRegister(), imm);
451 } else {
452 __ movl(Address(ESP, location.GetStackIndex()), imm);
453 }
454 } else if (instruction->AsLongConstant() != nullptr) {
455 int64_t value = instruction->AsLongConstant()->GetValue();
456 if (location.IsRegister()) {
457 __ movl(location.AsX86().AsRegisterPairLow(), Immediate(Low32Bits(value)));
458 __ movl(location.AsX86().AsRegisterPairHigh(), Immediate(High32Bits(value)));
459 } else {
460 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
461 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
462 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100463 } else if (instruction->AsLoadLocal() != nullptr) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100464 switch (instruction->GetType()) {
465 case Primitive::kPrimBoolean:
466 case Primitive::kPrimByte:
467 case Primitive::kPrimChar:
468 case Primitive::kPrimShort:
469 case Primitive::kPrimInt:
470 case Primitive::kPrimNot:
471 Move32(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
472 break;
473
474 case Primitive::kPrimLong:
475 Move64(location, Location::DoubleStackSlot(
476 GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
477 break;
478
479 default:
480 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
481 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000482 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100483 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100484 switch (instruction->GetType()) {
485 case Primitive::kPrimBoolean:
486 case Primitive::kPrimByte:
487 case Primitive::kPrimChar:
488 case Primitive::kPrimShort:
489 case Primitive::kPrimInt:
490 case Primitive::kPrimNot:
491 Move32(location, instruction->GetLocations()->Out());
492 break;
493
494 case Primitive::kPrimLong:
495 Move64(location, instruction->GetLocations()->Out());
496 break;
497
498 default:
499 LOG(FATAL) << "Unimplemented type " << instruction->GetType();
500 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000501 }
502}
503
504void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000505 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000506}
507
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000508void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000509 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000510 if (GetGraph()->GetExitBlock() == successor) {
511 codegen_->GenerateFrameExit();
512 } else if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
513 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000514 }
515}
516
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000517void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000518 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000519}
520
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000521void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000522 if (kIsDebugBuild) {
523 __ Comment("Unreachable");
524 __ int3();
525 }
526}
527
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000528void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100529 LocationSummary* locations =
530 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100531 HInstruction* cond = if_instr->InputAt(0);
532 DCHECK(cond->IsCondition());
533 HCondition* condition = cond->AsCondition();
534 if (condition->NeedsMaterialization()) {
535 locations->SetInAt(0, Location::Any());
536 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000537}
538
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000539void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700540 HInstruction* cond = if_instr->InputAt(0);
541 DCHECK(cond->IsCondition());
542 HCondition* condition = cond->AsCondition();
543 if (condition->NeedsMaterialization()) {
544 // Materialized condition, compare against 0
545 Location lhs = if_instr->GetLocations()->InAt(0);
546 if (lhs.IsRegister()) {
547 __ cmpl(lhs.AsX86().AsCpuRegister(), Immediate(0));
548 } else {
549 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
550 }
551 __ j(kEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100552 } else {
Dave Allison20dfc792014-06-16 20:44:29 -0700553 Location lhs = condition->GetLocations()->InAt(0);
554 Location rhs = condition->GetLocations()->InAt(1);
555 // LHS is guaranteed to be in a register (see LocationsBuilderX86::VisitCondition).
556 if (rhs.IsRegister()) {
557 __ cmpl(lhs.AsX86().AsCpuRegister(), rhs.AsX86().AsCpuRegister());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100558 } else if (rhs.IsConstant()) {
559 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
560 Immediate imm(instruction->AsIntConstant()->GetValue());
561 __ cmpl(lhs.AsX86().AsCpuRegister(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700562 } else {
563 __ cmpl(lhs.AsX86().AsCpuRegister(), Address(ESP, rhs.GetStackIndex()));
564 }
565 __ j(X86Condition(condition->GetCondition()),
566 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100567 }
Dave Allison20dfc792014-06-16 20:44:29 -0700568 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfFalseSuccessor())) {
569 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000570 }
571}
572
573void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000574 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000575}
576
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000577void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
578 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000579}
580
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000581void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100582 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000583}
584
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000585void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100586 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000587}
588
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100589void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100590 LocationSummary* locations =
591 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100592 switch (store->InputAt(1)->GetType()) {
593 case Primitive::kPrimBoolean:
594 case Primitive::kPrimByte:
595 case Primitive::kPrimChar:
596 case Primitive::kPrimShort:
597 case Primitive::kPrimInt:
598 case Primitive::kPrimNot:
599 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
600 break;
601
602 case Primitive::kPrimLong:
603 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
604 break;
605
606 default:
607 LOG(FATAL) << "Unimplemented local type " << store->InputAt(1)->GetType();
608 }
609 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000610}
611
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000612void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000613}
614
Dave Allison20dfc792014-06-16 20:44:29 -0700615void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100616 LocationSummary* locations =
617 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100618 locations->SetInAt(0, Location::RequiresRegister());
619 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100620 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100621 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100622 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000623}
624
Dave Allison20dfc792014-06-16 20:44:29 -0700625void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
626 if (comp->NeedsMaterialization()) {
627 LocationSummary* locations = comp->GetLocations();
628 if (locations->InAt(1).IsRegister()) {
629 __ cmpl(locations->InAt(0).AsX86().AsCpuRegister(),
630 locations->InAt(1).AsX86().AsCpuRegister());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100631 } else if (locations->InAt(1).IsConstant()) {
632 HConstant* instruction = locations->InAt(1).GetConstant();
633 Immediate imm(instruction->AsIntConstant()->GetValue());
634 __ cmpl(locations->InAt(0).AsX86().AsCpuRegister(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700635 } else {
636 __ cmpl(locations->InAt(0).AsX86().AsCpuRegister(),
637 Address(ESP, locations->InAt(1).GetStackIndex()));
638 }
639 __ setb(X86Condition(comp->GetCondition()), locations->Out().AsX86().AsCpuRegister());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100640 }
Dave Allison20dfc792014-06-16 20:44:29 -0700641}
642
643void LocationsBuilderX86::VisitEqual(HEqual* comp) {
644 VisitCondition(comp);
645}
646
647void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
648 VisitCondition(comp);
649}
650
651void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
652 VisitCondition(comp);
653}
654
655void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
656 VisitCondition(comp);
657}
658
659void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
660 VisitCondition(comp);
661}
662
663void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
664 VisitCondition(comp);
665}
666
667void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
668 VisitCondition(comp);
669}
670
671void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
672 VisitCondition(comp);
673}
674
675void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
676 VisitCondition(comp);
677}
678
679void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
680 VisitCondition(comp);
681}
682
683void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
684 VisitCondition(comp);
685}
686
687void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
688 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000689}
690
691void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100692 LocationSummary* locations =
693 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100694 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000695}
696
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000697void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000698}
699
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100700void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100701 LocationSummary* locations =
702 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100703 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100704}
705
706void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
707 // Will be generated at use site.
708}
709
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000710void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000711 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000712}
713
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000714void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
715 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000716 __ ret();
717}
718
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000719void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100720 LocationSummary* locations =
721 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100722 switch (ret->InputAt(0)->GetType()) {
723 case Primitive::kPrimBoolean:
724 case Primitive::kPrimByte:
725 case Primitive::kPrimChar:
726 case Primitive::kPrimShort:
727 case Primitive::kPrimInt:
728 case Primitive::kPrimNot:
729 locations->SetInAt(0, X86CpuLocation(EAX));
730 break;
731
732 case Primitive::kPrimLong:
733 locations->SetInAt(
734 0, Location::RegisterLocation(X86ManagedRegister::FromRegisterPair(EAX_EDX)));
735 break;
736
737 default:
738 LOG(FATAL) << "Unimplemented return type " << ret->InputAt(0)->GetType();
739 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000740}
741
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000742void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100743 if (kIsDebugBuild) {
744 switch (ret->InputAt(0)->GetType()) {
745 case Primitive::kPrimBoolean:
746 case Primitive::kPrimByte:
747 case Primitive::kPrimChar:
748 case Primitive::kPrimShort:
749 case Primitive::kPrimInt:
750 case Primitive::kPrimNot:
751 DCHECK_EQ(ret->GetLocations()->InAt(0).AsX86().AsCpuRegister(), EAX);
752 break;
753
754 case Primitive::kPrimLong:
755 DCHECK_EQ(ret->GetLocations()->InAt(0).AsX86().AsRegisterPair(), EAX_EDX);
756 break;
757
758 default:
759 LOG(FATAL) << "Unimplemented return type " << ret->InputAt(0)->GetType();
760 }
761 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000762 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000763 __ ret();
764}
765
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000766void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100767 HandleInvoke(invoke);
768}
769
770void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
771 Register temp = invoke->GetLocations()->GetTemp(0).AsX86().AsCpuRegister();
772 uint32_t heap_reference_size = sizeof(mirror::HeapReference<mirror::Object>);
773 size_t index_in_cache = mirror::Array::DataOffset(heap_reference_size).Int32Value() +
774 invoke->GetIndexInDexCache() * kX86WordSize;
775
776 // TODO: Implement all kinds of calls:
777 // 1) boot -> boot
778 // 2) app -> boot
779 // 3) app -> app
780 //
781 // Currently we implement the app -> app logic, which looks up in the resolve cache.
782
783 // temp = method;
784 LoadCurrentMethod(temp);
785 // temp = temp->dex_cache_resolved_methods_;
786 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
787 // temp = temp[index_in_cache]
788 __ movl(temp, Address(temp, index_in_cache));
789 // (temp + offset_of_quick_compiled_code)()
790 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
791
792 DCHECK(!codegen_->IsLeafMethod());
793 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
794}
795
796void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
797 HandleInvoke(invoke);
798}
799
800void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100801 LocationSummary* locations =
802 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100803 locations->AddTemp(X86CpuLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100804
805 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100806 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100807 HInstruction* input = invoke->InputAt(i);
808 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
809 }
810
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100811 switch (invoke->GetType()) {
812 case Primitive::kPrimBoolean:
813 case Primitive::kPrimByte:
814 case Primitive::kPrimChar:
815 case Primitive::kPrimShort:
816 case Primitive::kPrimInt:
817 case Primitive::kPrimNot:
818 locations->SetOut(X86CpuLocation(EAX));
819 break;
820
821 case Primitive::kPrimLong:
822 locations->SetOut(Location::RegisterLocation(X86ManagedRegister::FromRegisterPair(EAX_EDX)));
823 break;
824
825 case Primitive::kPrimVoid:
826 break;
827
828 case Primitive::kPrimDouble:
829 case Primitive::kPrimFloat:
830 LOG(FATAL) << "Unimplemented return type " << invoke->GetType();
831 break;
832 }
833
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000834 invoke->SetLocations(locations);
835}
836
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100837void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100838 Register temp = invoke->GetLocations()->GetTemp(0).AsX86().AsCpuRegister();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100839 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
840 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
841 LocationSummary* locations = invoke->GetLocations();
842 Location receiver = locations->InAt(0);
843 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
844 // temp = object->GetClass();
845 if (receiver.IsStackSlot()) {
846 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
847 __ movl(temp, Address(temp, class_offset));
848 } else {
849 __ movl(temp, Address(receiver.AsX86().AsCpuRegister(), class_offset));
850 }
851 // temp = temp->GetMethodAt(method_offset);
852 __ movl(temp, Address(temp, method_offset));
853 // call temp->GetEntryPoint();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000854 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
855
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100856 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100857 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000858}
859
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000860void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100861 LocationSummary* locations =
862 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000863 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100864 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100865 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100866 locations->SetInAt(0, Location::RequiresRegister());
867 locations->SetInAt(1, Location::Any());
868 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100869 break;
870 }
871
872 case Primitive::kPrimBoolean:
873 case Primitive::kPrimByte:
874 case Primitive::kPrimChar:
875 case Primitive::kPrimShort:
876 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
877 break;
878
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000879 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100880 LOG(FATAL) << "Unimplemented add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000881 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000882}
883
884void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
885 LocationSummary* locations = add->GetLocations();
886 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100887 case Primitive::kPrimInt: {
888 DCHECK_EQ(locations->InAt(0).AsX86().AsCpuRegister(),
889 locations->Out().AsX86().AsCpuRegister());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100890 if (locations->InAt(1).IsRegister()) {
891 __ addl(locations->InAt(0).AsX86().AsCpuRegister(),
892 locations->InAt(1).AsX86().AsCpuRegister());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100893 } else if (locations->InAt(1).IsConstant()) {
894 HConstant* instruction = locations->InAt(1).GetConstant();
895 Immediate imm(instruction->AsIntConstant()->GetValue());
896 __ addl(locations->InAt(0).AsX86().AsCpuRegister(), imm);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100897 } else {
898 __ addl(locations->InAt(0).AsX86().AsCpuRegister(),
899 Address(ESP, locations->InAt(1).GetStackIndex()));
900 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000901 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100902 }
903
904 case Primitive::kPrimLong: {
905 DCHECK_EQ(locations->InAt(0).AsX86().AsRegisterPair(),
906 locations->Out().AsX86().AsRegisterPair());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100907 if (locations->InAt(1).IsRegister()) {
908 __ addl(locations->InAt(0).AsX86().AsRegisterPairLow(),
909 locations->InAt(1).AsX86().AsRegisterPairLow());
910 __ adcl(locations->InAt(0).AsX86().AsRegisterPairHigh(),
911 locations->InAt(1).AsX86().AsRegisterPairHigh());
912 } else {
913 __ addl(locations->InAt(0).AsX86().AsRegisterPairLow(),
914 Address(ESP, locations->InAt(1).GetStackIndex()));
915 __ adcl(locations->InAt(0).AsX86().AsRegisterPairHigh(),
916 Address(ESP, locations->InAt(1).GetHighStackIndex(kX86WordSize)));
917 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100918 break;
919 }
920
921 case Primitive::kPrimBoolean:
922 case Primitive::kPrimByte:
923 case Primitive::kPrimChar:
924 case Primitive::kPrimShort:
925 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
926 break;
927
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000928 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100929 LOG(FATAL) << "Unimplemented add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000930 }
931}
932
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100933void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100934 LocationSummary* locations =
935 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100936 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100937 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100938 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100939 locations->SetInAt(0, Location::RequiresRegister());
940 locations->SetInAt(1, Location::Any());
941 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100942 break;
943 }
944
945 case Primitive::kPrimBoolean:
946 case Primitive::kPrimByte:
947 case Primitive::kPrimChar:
948 case Primitive::kPrimShort:
949 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
950 break;
951
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100952 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100953 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100954 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100955}
956
957void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
958 LocationSummary* locations = sub->GetLocations();
959 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100960 case Primitive::kPrimInt: {
961 DCHECK_EQ(locations->InAt(0).AsX86().AsCpuRegister(),
962 locations->Out().AsX86().AsCpuRegister());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100963 if (locations->InAt(1).IsRegister()) {
964 __ subl(locations->InAt(0).AsX86().AsCpuRegister(),
965 locations->InAt(1).AsX86().AsCpuRegister());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100966 } else if (locations->InAt(1).IsConstant()) {
967 HConstant* instruction = locations->InAt(1).GetConstant();
968 Immediate imm(instruction->AsIntConstant()->GetValue());
969 __ subl(locations->InAt(0).AsX86().AsCpuRegister(), imm);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100970 } else {
971 __ subl(locations->InAt(0).AsX86().AsCpuRegister(),
972 Address(ESP, locations->InAt(1).GetStackIndex()));
973 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100974 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100975 }
976
977 case Primitive::kPrimLong: {
978 DCHECK_EQ(locations->InAt(0).AsX86().AsRegisterPair(),
979 locations->Out().AsX86().AsRegisterPair());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100980 if (locations->InAt(1).IsRegister()) {
981 __ subl(locations->InAt(0).AsX86().AsRegisterPairLow(),
982 locations->InAt(1).AsX86().AsRegisterPairLow());
983 __ sbbl(locations->InAt(0).AsX86().AsRegisterPairHigh(),
984 locations->InAt(1).AsX86().AsRegisterPairHigh());
985 } else {
986 __ subl(locations->InAt(0).AsX86().AsRegisterPairLow(),
987 Address(ESP, locations->InAt(1).GetStackIndex()));
988 __ sbbl(locations->InAt(0).AsX86().AsRegisterPairHigh(),
989 Address(ESP, locations->InAt(1).GetHighStackIndex(kX86WordSize)));
990 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100991 break;
992 }
993
994 case Primitive::kPrimBoolean:
995 case Primitive::kPrimByte:
996 case Primitive::kPrimChar:
997 case Primitive::kPrimShort:
998 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
999 break;
1000
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001001 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001002 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001003 }
1004}
1005
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001006void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001007 LocationSummary* locations =
1008 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001009 locations->SetOut(X86CpuLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001010 InvokeRuntimeCallingConvention calling_convention;
1011 locations->AddTemp(X86CpuLocation(calling_convention.GetRegisterAt(0)));
1012 locations->AddTemp(X86CpuLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001013}
1014
1015void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
1016 InvokeRuntimeCallingConvention calling_convention;
1017 LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001018 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001019
Nicolas Geoffray707c8092014-04-04 10:50:14 +01001020 __ fs()->call(
1021 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001022
Nicolas Geoffray39468442014-09-02 15:17:15 +01001023 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001024 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001025}
1026
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001027void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001028 LocationSummary* locations =
1029 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001030 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1031 if (location.IsStackSlot()) {
1032 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1033 } else if (location.IsDoubleStackSlot()) {
1034 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001035 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001036 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001037}
1038
1039void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001040}
1041
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001042void LocationsBuilderX86::VisitNot(HNot* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001043 LocationSummary* locations =
1044 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001045 locations->SetInAt(0, Location::RequiresRegister());
1046 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001047}
1048
1049void InstructionCodeGeneratorX86::VisitNot(HNot* instruction) {
1050 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001051 Location out = locations->Out();
1052 DCHECK_EQ(locations->InAt(0).AsX86().AsCpuRegister(), out.AsX86().AsCpuRegister());
1053 __ xorl(out.AsX86().AsCpuRegister(), Immediate(1));
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001054}
1055
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001056void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001057 LocationSummary* locations =
1058 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001059 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001060 locations->SetInAt(1, Location::Any());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001061 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001062}
1063
1064void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
1065 Label greater, done;
1066 LocationSummary* locations = compare->GetLocations();
1067 switch (compare->InputAt(0)->GetType()) {
1068 case Primitive::kPrimLong: {
1069 Label less, greater, done;
1070 Register output = locations->Out().AsX86().AsCpuRegister();
1071 X86ManagedRegister left = locations->InAt(0).AsX86();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001072 Location right = locations->InAt(1);
1073 if (right.IsRegister()) {
1074 __ cmpl(left.AsRegisterPairHigh(), right.AsX86().AsRegisterPairHigh());
1075 } else {
1076 DCHECK(right.IsDoubleStackSlot());
1077 __ cmpl(left.AsRegisterPairHigh(), Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1078 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001079 __ j(kLess, &less); // Signed compare.
1080 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001081 if (right.IsRegister()) {
1082 __ cmpl(left.AsRegisterPairLow(), right.AsX86().AsRegisterPairLow());
1083 } else {
1084 DCHECK(right.IsDoubleStackSlot());
1085 __ cmpl(left.AsRegisterPairLow(), Address(ESP, right.GetStackIndex()));
1086 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001087 __ movl(output, Immediate(0));
1088 __ j(kEqual, &done);
1089 __ j(kBelow, &less); // Unsigned compare.
1090
1091 __ Bind(&greater);
1092 __ movl(output, Immediate(1));
1093 __ jmp(&done);
1094
1095 __ Bind(&less);
1096 __ movl(output, Immediate(-1));
1097
1098 __ Bind(&done);
1099 break;
1100 }
1101 default:
1102 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
1103 }
1104}
1105
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001106void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001107 LocationSummary* locations =
1108 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001109 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1110 locations->SetInAt(i, Location::Any());
1111 }
1112 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001113}
1114
1115void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001116 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001117}
1118
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001119void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001120 LocationSummary* locations =
1121 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001122 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001123 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001124 if (field_type == Primitive::kPrimBoolean || field_type == Primitive::kPrimByte) {
1125 // Ensure the value is in a byte register.
1126 locations->SetInAt(1, X86CpuLocation(EAX));
1127 } else {
1128 locations->SetInAt(1, Location::RequiresRegister());
1129 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001130 // Temporary registers for the write barrier.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001131 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001132 locations->AddTemp(Location::RequiresRegister());
1133 // Ensure the card is in a byte register.
1134 locations->AddTemp(X86CpuLocation(ECX));
1135 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001136}
1137
1138void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1139 LocationSummary* locations = instruction->GetLocations();
1140 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1141 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001142 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001143
1144 switch (field_type) {
1145 case Primitive::kPrimBoolean:
1146 case Primitive::kPrimByte: {
1147 ByteRegister value = locations->InAt(1).AsX86().AsByteRegister();
1148 __ movb(Address(obj, offset), value);
1149 break;
1150 }
1151
1152 case Primitive::kPrimShort:
1153 case Primitive::kPrimChar: {
1154 Register value = locations->InAt(1).AsX86().AsCpuRegister();
1155 __ movw(Address(obj, offset), value);
1156 break;
1157 }
1158
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001159 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001160 case Primitive::kPrimNot: {
1161 Register value = locations->InAt(1).AsX86().AsCpuRegister();
1162 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001163
1164 if (field_type == Primitive::kPrimNot) {
1165 Register temp = locations->GetTemp(0).AsX86().AsCpuRegister();
1166 Register card = locations->GetTemp(1).AsX86().AsCpuRegister();
1167 codegen_->MarkGCCard(temp, card, obj, value);
1168 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001169 break;
1170 }
1171
1172 case Primitive::kPrimLong: {
1173 X86ManagedRegister value = locations->InAt(1).AsX86();
1174 __ movl(Address(obj, offset), value.AsRegisterPairLow());
1175 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh());
1176 break;
1177 }
1178
1179 case Primitive::kPrimFloat:
1180 case Primitive::kPrimDouble:
1181 LOG(FATAL) << "Unimplemented register type " << field_type;
1182
1183 case Primitive::kPrimVoid:
1184 LOG(FATAL) << "Unreachable type " << field_type;
1185 }
1186}
1187
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001188void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
1189 Label is_null;
1190 __ testl(value, value);
1191 __ j(kEqual, &is_null);
1192 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
1193 __ movl(temp, object);
1194 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
1195 __ movb(Address(temp, card, TIMES_1, 0),
1196 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
1197 __ Bind(&is_null);
1198}
1199
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001200void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001201 LocationSummary* locations =
1202 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001203 locations->SetInAt(0, Location::RequiresRegister());
1204 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001205}
1206
1207void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1208 LocationSummary* locations = instruction->GetLocations();
1209 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1210 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
1211
1212 switch (instruction->GetType()) {
1213 case Primitive::kPrimBoolean: {
1214 Register out = locations->Out().AsX86().AsCpuRegister();
1215 __ movzxb(out, Address(obj, offset));
1216 break;
1217 }
1218
1219 case Primitive::kPrimByte: {
1220 Register out = locations->Out().AsX86().AsCpuRegister();
1221 __ movsxb(out, Address(obj, offset));
1222 break;
1223 }
1224
1225 case Primitive::kPrimShort: {
1226 Register out = locations->Out().AsX86().AsCpuRegister();
1227 __ movsxw(out, Address(obj, offset));
1228 break;
1229 }
1230
1231 case Primitive::kPrimChar: {
1232 Register out = locations->Out().AsX86().AsCpuRegister();
1233 __ movzxw(out, Address(obj, offset));
1234 break;
1235 }
1236
1237 case Primitive::kPrimInt:
1238 case Primitive::kPrimNot: {
1239 Register out = locations->Out().AsX86().AsCpuRegister();
1240 __ movl(out, Address(obj, offset));
1241 break;
1242 }
1243
1244 case Primitive::kPrimLong: {
1245 // TODO: support volatile.
1246 X86ManagedRegister out = locations->Out().AsX86();
1247 __ movl(out.AsRegisterPairLow(), Address(obj, offset));
1248 __ movl(out.AsRegisterPairHigh(), Address(obj, kX86WordSize + offset));
1249 break;
1250 }
1251
1252 case Primitive::kPrimFloat:
1253 case Primitive::kPrimDouble:
1254 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1255
1256 case Primitive::kPrimVoid:
1257 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1258 }
1259}
1260
1261void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001262 LocationSummary* locations =
1263 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001264 locations->SetInAt(0, Location::Any());
1265 // TODO: Have a normalization phase that makes this instruction never used.
1266 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001267}
1268
1269void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001270 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001271 codegen_->AddSlowPath(slow_path);
1272
1273 LocationSummary* locations = instruction->GetLocations();
1274 Location obj = locations->InAt(0);
1275 DCHECK(obj.Equals(locations->Out()));
1276
1277 if (obj.IsRegister()) {
1278 __ cmpl(obj.AsX86().AsCpuRegister(), Immediate(0));
1279 } else {
1280 DCHECK(locations->InAt(0).IsStackSlot());
1281 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
1282 }
1283 __ j(kEqual, slow_path->GetEntryLabel());
1284}
1285
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001286void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001287 LocationSummary* locations =
1288 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001289 locations->SetInAt(0, Location::RequiresRegister());
1290 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1291 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001292}
1293
1294void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
1295 LocationSummary* locations = instruction->GetLocations();
1296 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1297 Location index = locations->InAt(1);
1298
1299 switch (instruction->GetType()) {
1300 case Primitive::kPrimBoolean: {
1301 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1302 Register out = locations->Out().AsX86().AsCpuRegister();
1303 if (index.IsConstant()) {
1304 __ movzxb(out, Address(obj,
1305 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1306 } else {
1307 __ movzxb(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_1, data_offset));
1308 }
1309 break;
1310 }
1311
1312 case Primitive::kPrimByte: {
1313 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1314 Register out = locations->Out().AsX86().AsCpuRegister();
1315 if (index.IsConstant()) {
1316 __ movsxb(out, Address(obj,
1317 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1318 } else {
1319 __ movsxb(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_1, data_offset));
1320 }
1321 break;
1322 }
1323
1324 case Primitive::kPrimShort: {
1325 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1326 Register out = locations->Out().AsX86().AsCpuRegister();
1327 if (index.IsConstant()) {
1328 __ movsxw(out, Address(obj,
1329 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1330 } else {
1331 __ movsxw(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_2, data_offset));
1332 }
1333 break;
1334 }
1335
1336 case Primitive::kPrimChar: {
1337 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1338 Register out = locations->Out().AsX86().AsCpuRegister();
1339 if (index.IsConstant()) {
1340 __ movzxw(out, Address(obj,
1341 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1342 } else {
1343 __ movzxw(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_2, data_offset));
1344 }
1345 break;
1346 }
1347
1348 case Primitive::kPrimInt:
1349 case Primitive::kPrimNot: {
1350 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1351 Register out = locations->Out().AsX86().AsCpuRegister();
1352 if (index.IsConstant()) {
1353 __ movl(out, Address(obj,
1354 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1355 } else {
1356 __ movl(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_4, data_offset));
1357 }
1358 break;
1359 }
1360
1361 case Primitive::kPrimLong: {
1362 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1363 X86ManagedRegister out = locations->Out().AsX86();
1364 if (index.IsConstant()) {
1365 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1366 __ movl(out.AsRegisterPairLow(), Address(obj, offset));
1367 __ movl(out.AsRegisterPairHigh(), Address(obj, offset + kX86WordSize));
1368 } else {
1369 __ movl(out.AsRegisterPairLow(),
1370 Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset));
1371 __ movl(out.AsRegisterPairHigh(),
1372 Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset + kX86WordSize));
1373 }
1374 break;
1375 }
1376
1377 case Primitive::kPrimFloat:
1378 case Primitive::kPrimDouble:
1379 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1380
1381 case Primitive::kPrimVoid:
1382 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1383 }
1384}
1385
1386void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001387 Primitive::Type value_type = instruction->GetComponentType();
1388 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1389 instruction,
1390 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
1391
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001392 if (value_type == Primitive::kPrimNot) {
1393 InvokeRuntimeCallingConvention calling_convention;
1394 locations->SetInAt(0, X86CpuLocation(calling_convention.GetRegisterAt(0)));
1395 locations->SetInAt(1, X86CpuLocation(calling_convention.GetRegisterAt(1)));
1396 locations->SetInAt(2, X86CpuLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001397 } else {
1398 locations->SetInAt(0, Location::RequiresRegister());
1399 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1400 if (value_type == Primitive::kPrimBoolean || value_type == Primitive::kPrimByte) {
1401 // Ensure the value is in a byte register.
1402 locations->SetInAt(2, X86CpuLocation(EAX));
1403 } else {
1404 locations->SetInAt(2, Location::RequiresRegister());
1405 }
1406 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001407}
1408
1409void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
1410 LocationSummary* locations = instruction->GetLocations();
1411 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1412 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001413 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001414
1415 switch (value_type) {
1416 case Primitive::kPrimBoolean:
1417 case Primitive::kPrimByte: {
1418 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1419 ByteRegister value = locations->InAt(2).AsX86().AsByteRegister();
1420 if (index.IsConstant()) {
1421 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1422 __ movb(Address(obj, offset), value);
1423 } else {
1424 __ movb(Address(obj, index.AsX86().AsCpuRegister(), TIMES_1, data_offset), value);
1425 }
1426 break;
1427 }
1428
1429 case Primitive::kPrimShort:
1430 case Primitive::kPrimChar: {
1431 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1432 Register value = locations->InAt(2).AsX86().AsCpuRegister();
1433 if (index.IsConstant()) {
1434 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1435 __ movw(Address(obj, offset), value);
1436 } else {
1437 __ movw(Address(obj, index.AsX86().AsCpuRegister(), TIMES_2, data_offset), value);
1438 }
1439 break;
1440 }
1441
1442 case Primitive::kPrimInt: {
1443 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1444 Register value = locations->InAt(2).AsX86().AsCpuRegister();
1445 if (index.IsConstant()) {
1446 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1447 __ movl(Address(obj, offset), value);
1448 } else {
1449 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_4, data_offset), value);
1450 }
1451 break;
1452 }
1453
1454 case Primitive::kPrimNot: {
1455 DCHECK(!codegen_->IsLeafMethod());
1456 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001457 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001458 break;
1459 }
1460
1461 case Primitive::kPrimLong: {
1462 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1463 X86ManagedRegister value = locations->InAt(2).AsX86();
1464 if (index.IsConstant()) {
1465 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1466 __ movl(Address(obj, offset), value.AsRegisterPairLow());
1467 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh());
1468 } else {
1469 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset),
1470 value.AsRegisterPairLow());
1471 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset + kX86WordSize),
1472 value.AsRegisterPairHigh());
1473 }
1474 break;
1475 }
1476
1477 case Primitive::kPrimFloat:
1478 case Primitive::kPrimDouble:
1479 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1480
1481 case Primitive::kPrimVoid:
1482 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1483 }
1484}
1485
1486void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
1487 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1488 locations->SetInAt(0, Location::RequiresRegister());
1489 locations->SetOut(Location::RequiresRegister());
1490 instruction->SetLocations(locations);
1491}
1492
1493void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
1494 LocationSummary* locations = instruction->GetLocations();
1495 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1496 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1497 Register out = locations->Out().AsX86().AsCpuRegister();
1498 __ movl(out, Address(obj, offset));
1499}
1500
1501void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001502 LocationSummary* locations =
1503 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001504 locations->SetInAt(0, Location::RequiresRegister());
1505 locations->SetInAt(1, Location::RequiresRegister());
1506 // TODO: Have a normalization phase that makes this instruction never used.
1507 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001508}
1509
1510void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
1511 LocationSummary* locations = instruction->GetLocations();
1512 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01001513 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001514 codegen_->AddSlowPath(slow_path);
1515
1516 Register index = locations->InAt(0).AsX86().AsCpuRegister();
1517 Register length = locations->InAt(1).AsX86().AsCpuRegister();
1518
1519 __ cmpl(index, length);
1520 __ j(kAboveEqual, slow_path->GetEntryLabel());
1521}
1522
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001523void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
1524 temp->SetLocations(nullptr);
1525}
1526
1527void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
1528 // Nothing to do, this is driven by the code generator.
1529}
1530
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001531void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001532 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001533}
1534
1535void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001536 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
1537}
1538
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001539void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
1540 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
1541}
1542
1543void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
1544 SuspendCheckSlowPathX86* slow_path =
1545 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction);
1546 codegen_->AddSlowPath(slow_path);
1547 __ fs()->cmpl(Address::Absolute(
1548 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
1549 __ j(kNotEqual, slow_path->GetEntryLabel());
1550 __ Bind(slow_path->GetReturnLabel());
1551}
1552
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001553X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
1554 return codegen_->GetAssembler();
1555}
1556
1557void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
1558 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001559 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001560 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1561 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
1562 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
1563}
1564
1565void ParallelMoveResolverX86::EmitMove(size_t index) {
1566 MoveOperands* move = moves_.Get(index);
1567 Location source = move->GetSource();
1568 Location destination = move->GetDestination();
1569
1570 if (source.IsRegister()) {
1571 if (destination.IsRegister()) {
1572 __ movl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister());
1573 } else {
1574 DCHECK(destination.IsStackSlot());
1575 __ movl(Address(ESP, destination.GetStackIndex()), source.AsX86().AsCpuRegister());
1576 }
1577 } else if (source.IsStackSlot()) {
1578 if (destination.IsRegister()) {
1579 __ movl(destination.AsX86().AsCpuRegister(), Address(ESP, source.GetStackIndex()));
1580 } else {
1581 DCHECK(destination.IsStackSlot());
1582 MoveMemoryToMemory(destination.GetStackIndex(),
1583 source.GetStackIndex());
1584 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001585 } else if (source.IsConstant()) {
1586 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
1587 Immediate imm(instruction->AsIntConstant()->GetValue());
1588 if (destination.IsRegister()) {
1589 __ movl(destination.AsX86().AsCpuRegister(), imm);
1590 } else {
1591 __ movl(Address(ESP, destination.GetStackIndex()), imm);
1592 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001593 } else {
1594 LOG(FATAL) << "Unimplemented";
1595 }
1596}
1597
1598void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001599 Register suggested_scratch = reg == EAX ? EBX : EAX;
1600 ScratchRegisterScope ensure_scratch(
1601 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
1602
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001603 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1604 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
1605 __ movl(Address(ESP, mem + stack_offset), reg);
1606 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
1607}
1608
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001609void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
1610 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001611 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
1612
1613 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001614 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001615 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
1616
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001617 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
1618 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
1619 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
1620 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
1621 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
1622 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
1623}
1624
1625void ParallelMoveResolverX86::EmitSwap(size_t index) {
1626 MoveOperands* move = moves_.Get(index);
1627 Location source = move->GetSource();
1628 Location destination = move->GetDestination();
1629
1630 if (source.IsRegister() && destination.IsRegister()) {
1631 __ xchgl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister());
1632 } else if (source.IsRegister() && destination.IsStackSlot()) {
1633 Exchange(source.AsX86().AsCpuRegister(), destination.GetStackIndex());
1634 } else if (source.IsStackSlot() && destination.IsRegister()) {
1635 Exchange(destination.AsX86().AsCpuRegister(), source.GetStackIndex());
1636 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
1637 Exchange(destination.GetStackIndex(), source.GetStackIndex());
1638 } else {
1639 LOG(FATAL) << "Unimplemented";
1640 }
1641}
1642
1643void ParallelMoveResolverX86::SpillScratch(int reg) {
1644 __ pushl(static_cast<Register>(reg));
1645}
1646
1647void ParallelMoveResolverX86::RestoreScratch(int reg) {
1648 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001649}
1650
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001651} // namespace x86
1652} // namespace art