blob: f7b849564dbaf0848317f6da8dbcb2a9a6a248d7 [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()) {
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100544 // Moves do not affect the eflags register, so if the condition is evaluated
545 // just before the if, we don't need to evaluate it again.
546 if (!condition->IsBeforeWhenDisregardMoves(if_instr)) {
547 // Materialized condition, compare against 0
548 Location lhs = if_instr->GetLocations()->InAt(0);
549 if (lhs.IsRegister()) {
550 __ cmpl(lhs.AsX86().AsCpuRegister(), Immediate(0));
551 } else {
552 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
553 }
Dave Allison20dfc792014-06-16 20:44:29 -0700554 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100555 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100556 } else {
Dave Allison20dfc792014-06-16 20:44:29 -0700557 Location lhs = condition->GetLocations()->InAt(0);
558 Location rhs = condition->GetLocations()->InAt(1);
559 // LHS is guaranteed to be in a register (see LocationsBuilderX86::VisitCondition).
560 if (rhs.IsRegister()) {
561 __ cmpl(lhs.AsX86().AsCpuRegister(), rhs.AsX86().AsCpuRegister());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100562 } else if (rhs.IsConstant()) {
563 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
564 Immediate imm(instruction->AsIntConstant()->GetValue());
565 __ cmpl(lhs.AsX86().AsCpuRegister(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700566 } else {
567 __ cmpl(lhs.AsX86().AsCpuRegister(), Address(ESP, rhs.GetStackIndex()));
568 }
569 __ j(X86Condition(condition->GetCondition()),
570 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100571 }
Dave Allison20dfc792014-06-16 20:44:29 -0700572 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfFalseSuccessor())) {
573 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000574 }
575}
576
577void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000578 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000579}
580
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000581void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
582 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000583}
584
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000585void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100586 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000587}
588
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000589void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100590 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000591}
592
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100593void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100594 LocationSummary* locations =
595 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100596 switch (store->InputAt(1)->GetType()) {
597 case Primitive::kPrimBoolean:
598 case Primitive::kPrimByte:
599 case Primitive::kPrimChar:
600 case Primitive::kPrimShort:
601 case Primitive::kPrimInt:
602 case Primitive::kPrimNot:
603 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
604 break;
605
606 case Primitive::kPrimLong:
607 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
608 break;
609
610 default:
611 LOG(FATAL) << "Unimplemented local type " << store->InputAt(1)->GetType();
612 }
613 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000614}
615
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000616void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000617}
618
Dave Allison20dfc792014-06-16 20:44:29 -0700619void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100620 LocationSummary* locations =
621 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100622 locations->SetInAt(0, Location::RequiresRegister());
623 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100624 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100625 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100626 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000627}
628
Dave Allison20dfc792014-06-16 20:44:29 -0700629void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
630 if (comp->NeedsMaterialization()) {
631 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100632 Register reg = locations->Out().AsX86().AsCpuRegister();
633 // Clear register: setcc only sets the low byte.
634 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700635 if (locations->InAt(1).IsRegister()) {
636 __ cmpl(locations->InAt(0).AsX86().AsCpuRegister(),
637 locations->InAt(1).AsX86().AsCpuRegister());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100638 } else if (locations->InAt(1).IsConstant()) {
639 HConstant* instruction = locations->InAt(1).GetConstant();
640 Immediate imm(instruction->AsIntConstant()->GetValue());
641 __ cmpl(locations->InAt(0).AsX86().AsCpuRegister(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700642 } else {
643 __ cmpl(locations->InAt(0).AsX86().AsCpuRegister(),
644 Address(ESP, locations->InAt(1).GetStackIndex()));
645 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100646 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100647 }
Dave Allison20dfc792014-06-16 20:44:29 -0700648}
649
650void LocationsBuilderX86::VisitEqual(HEqual* comp) {
651 VisitCondition(comp);
652}
653
654void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
655 VisitCondition(comp);
656}
657
658void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
659 VisitCondition(comp);
660}
661
662void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
663 VisitCondition(comp);
664}
665
666void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
667 VisitCondition(comp);
668}
669
670void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
671 VisitCondition(comp);
672}
673
674void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
675 VisitCondition(comp);
676}
677
678void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
679 VisitCondition(comp);
680}
681
682void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
683 VisitCondition(comp);
684}
685
686void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
687 VisitCondition(comp);
688}
689
690void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
691 VisitCondition(comp);
692}
693
694void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
695 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000696}
697
698void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100699 LocationSummary* locations =
700 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100701 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000702}
703
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000704void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000705}
706
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100707void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100708 LocationSummary* locations =
709 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100710 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100711}
712
713void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
714 // Will be generated at use site.
715}
716
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000717void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000718 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000719}
720
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000721void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
722 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000723 __ ret();
724}
725
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000726void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100727 LocationSummary* locations =
728 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100729 switch (ret->InputAt(0)->GetType()) {
730 case Primitive::kPrimBoolean:
731 case Primitive::kPrimByte:
732 case Primitive::kPrimChar:
733 case Primitive::kPrimShort:
734 case Primitive::kPrimInt:
735 case Primitive::kPrimNot:
736 locations->SetInAt(0, X86CpuLocation(EAX));
737 break;
738
739 case Primitive::kPrimLong:
740 locations->SetInAt(
741 0, Location::RegisterLocation(X86ManagedRegister::FromRegisterPair(EAX_EDX)));
742 break;
743
744 default:
745 LOG(FATAL) << "Unimplemented return type " << ret->InputAt(0)->GetType();
746 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000747}
748
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000749void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100750 if (kIsDebugBuild) {
751 switch (ret->InputAt(0)->GetType()) {
752 case Primitive::kPrimBoolean:
753 case Primitive::kPrimByte:
754 case Primitive::kPrimChar:
755 case Primitive::kPrimShort:
756 case Primitive::kPrimInt:
757 case Primitive::kPrimNot:
758 DCHECK_EQ(ret->GetLocations()->InAt(0).AsX86().AsCpuRegister(), EAX);
759 break;
760
761 case Primitive::kPrimLong:
762 DCHECK_EQ(ret->GetLocations()->InAt(0).AsX86().AsRegisterPair(), EAX_EDX);
763 break;
764
765 default:
766 LOG(FATAL) << "Unimplemented return type " << ret->InputAt(0)->GetType();
767 }
768 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000769 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000770 __ ret();
771}
772
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000773void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100774 HandleInvoke(invoke);
775}
776
777void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
778 Register temp = invoke->GetLocations()->GetTemp(0).AsX86().AsCpuRegister();
779 uint32_t heap_reference_size = sizeof(mirror::HeapReference<mirror::Object>);
780 size_t index_in_cache = mirror::Array::DataOffset(heap_reference_size).Int32Value() +
781 invoke->GetIndexInDexCache() * kX86WordSize;
782
783 // TODO: Implement all kinds of calls:
784 // 1) boot -> boot
785 // 2) app -> boot
786 // 3) app -> app
787 //
788 // Currently we implement the app -> app logic, which looks up in the resolve cache.
789
790 // temp = method;
791 LoadCurrentMethod(temp);
792 // temp = temp->dex_cache_resolved_methods_;
793 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
794 // temp = temp[index_in_cache]
795 __ movl(temp, Address(temp, index_in_cache));
796 // (temp + offset_of_quick_compiled_code)()
797 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
798
799 DCHECK(!codegen_->IsLeafMethod());
800 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
801}
802
803void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
804 HandleInvoke(invoke);
805}
806
807void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100808 LocationSummary* locations =
809 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100810 locations->AddTemp(X86CpuLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100811
812 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100813 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100814 HInstruction* input = invoke->InputAt(i);
815 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
816 }
817
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100818 switch (invoke->GetType()) {
819 case Primitive::kPrimBoolean:
820 case Primitive::kPrimByte:
821 case Primitive::kPrimChar:
822 case Primitive::kPrimShort:
823 case Primitive::kPrimInt:
824 case Primitive::kPrimNot:
825 locations->SetOut(X86CpuLocation(EAX));
826 break;
827
828 case Primitive::kPrimLong:
829 locations->SetOut(Location::RegisterLocation(X86ManagedRegister::FromRegisterPair(EAX_EDX)));
830 break;
831
832 case Primitive::kPrimVoid:
833 break;
834
835 case Primitive::kPrimDouble:
836 case Primitive::kPrimFloat:
837 LOG(FATAL) << "Unimplemented return type " << invoke->GetType();
838 break;
839 }
840
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000841 invoke->SetLocations(locations);
842}
843
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100844void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100845 Register temp = invoke->GetLocations()->GetTemp(0).AsX86().AsCpuRegister();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100846 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
847 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
848 LocationSummary* locations = invoke->GetLocations();
849 Location receiver = locations->InAt(0);
850 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
851 // temp = object->GetClass();
852 if (receiver.IsStackSlot()) {
853 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
854 __ movl(temp, Address(temp, class_offset));
855 } else {
856 __ movl(temp, Address(receiver.AsX86().AsCpuRegister(), class_offset));
857 }
858 // temp = temp->GetMethodAt(method_offset);
859 __ movl(temp, Address(temp, method_offset));
860 // call temp->GetEntryPoint();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000861 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
862
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100863 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100864 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000865}
866
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000867void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100868 LocationSummary* locations =
869 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000870 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100871 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100872 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100873 locations->SetInAt(0, Location::RequiresRegister());
874 locations->SetInAt(1, Location::Any());
875 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100876 break;
877 }
878
879 case Primitive::kPrimBoolean:
880 case Primitive::kPrimByte:
881 case Primitive::kPrimChar:
882 case Primitive::kPrimShort:
883 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
884 break;
885
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000886 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100887 LOG(FATAL) << "Unimplemented add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000888 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000889}
890
891void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
892 LocationSummary* locations = add->GetLocations();
893 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100894 case Primitive::kPrimInt: {
895 DCHECK_EQ(locations->InAt(0).AsX86().AsCpuRegister(),
896 locations->Out().AsX86().AsCpuRegister());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100897 if (locations->InAt(1).IsRegister()) {
898 __ addl(locations->InAt(0).AsX86().AsCpuRegister(),
899 locations->InAt(1).AsX86().AsCpuRegister());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100900 } else if (locations->InAt(1).IsConstant()) {
901 HConstant* instruction = locations->InAt(1).GetConstant();
902 Immediate imm(instruction->AsIntConstant()->GetValue());
903 __ addl(locations->InAt(0).AsX86().AsCpuRegister(), imm);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100904 } else {
905 __ addl(locations->InAt(0).AsX86().AsCpuRegister(),
906 Address(ESP, locations->InAt(1).GetStackIndex()));
907 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000908 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100909 }
910
911 case Primitive::kPrimLong: {
912 DCHECK_EQ(locations->InAt(0).AsX86().AsRegisterPair(),
913 locations->Out().AsX86().AsRegisterPair());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100914 if (locations->InAt(1).IsRegister()) {
915 __ addl(locations->InAt(0).AsX86().AsRegisterPairLow(),
916 locations->InAt(1).AsX86().AsRegisterPairLow());
917 __ adcl(locations->InAt(0).AsX86().AsRegisterPairHigh(),
918 locations->InAt(1).AsX86().AsRegisterPairHigh());
919 } else {
920 __ addl(locations->InAt(0).AsX86().AsRegisterPairLow(),
921 Address(ESP, locations->InAt(1).GetStackIndex()));
922 __ adcl(locations->InAt(0).AsX86().AsRegisterPairHigh(),
923 Address(ESP, locations->InAt(1).GetHighStackIndex(kX86WordSize)));
924 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100925 break;
926 }
927
928 case Primitive::kPrimBoolean:
929 case Primitive::kPrimByte:
930 case Primitive::kPrimChar:
931 case Primitive::kPrimShort:
932 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
933 break;
934
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000935 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100936 LOG(FATAL) << "Unimplemented add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000937 }
938}
939
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100940void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100941 LocationSummary* locations =
942 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100943 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100944 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100945 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100946 locations->SetInAt(0, Location::RequiresRegister());
947 locations->SetInAt(1, Location::Any());
948 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100949 break;
950 }
951
952 case Primitive::kPrimBoolean:
953 case Primitive::kPrimByte:
954 case Primitive::kPrimChar:
955 case Primitive::kPrimShort:
956 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
957 break;
958
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100959 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100960 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100961 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100962}
963
964void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
965 LocationSummary* locations = sub->GetLocations();
966 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100967 case Primitive::kPrimInt: {
968 DCHECK_EQ(locations->InAt(0).AsX86().AsCpuRegister(),
969 locations->Out().AsX86().AsCpuRegister());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100970 if (locations->InAt(1).IsRegister()) {
971 __ subl(locations->InAt(0).AsX86().AsCpuRegister(),
972 locations->InAt(1).AsX86().AsCpuRegister());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100973 } else if (locations->InAt(1).IsConstant()) {
974 HConstant* instruction = locations->InAt(1).GetConstant();
975 Immediate imm(instruction->AsIntConstant()->GetValue());
976 __ subl(locations->InAt(0).AsX86().AsCpuRegister(), imm);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100977 } else {
978 __ subl(locations->InAt(0).AsX86().AsCpuRegister(),
979 Address(ESP, locations->InAt(1).GetStackIndex()));
980 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100981 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100982 }
983
984 case Primitive::kPrimLong: {
985 DCHECK_EQ(locations->InAt(0).AsX86().AsRegisterPair(),
986 locations->Out().AsX86().AsRegisterPair());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100987 if (locations->InAt(1).IsRegister()) {
988 __ subl(locations->InAt(0).AsX86().AsRegisterPairLow(),
989 locations->InAt(1).AsX86().AsRegisterPairLow());
990 __ sbbl(locations->InAt(0).AsX86().AsRegisterPairHigh(),
991 locations->InAt(1).AsX86().AsRegisterPairHigh());
992 } else {
993 __ subl(locations->InAt(0).AsX86().AsRegisterPairLow(),
994 Address(ESP, locations->InAt(1).GetStackIndex()));
995 __ sbbl(locations->InAt(0).AsX86().AsRegisterPairHigh(),
996 Address(ESP, locations->InAt(1).GetHighStackIndex(kX86WordSize)));
997 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100998 break;
999 }
1000
1001 case Primitive::kPrimBoolean:
1002 case Primitive::kPrimByte:
1003 case Primitive::kPrimChar:
1004 case Primitive::kPrimShort:
1005 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
1006 break;
1007
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001008 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001009 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001010 }
1011}
1012
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001013void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001014 LocationSummary* locations =
1015 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001016 locations->SetOut(X86CpuLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001017 InvokeRuntimeCallingConvention calling_convention;
1018 locations->AddTemp(X86CpuLocation(calling_convention.GetRegisterAt(0)));
1019 locations->AddTemp(X86CpuLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001020}
1021
1022void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
1023 InvokeRuntimeCallingConvention calling_convention;
1024 LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001025 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001026
Nicolas Geoffray707c8092014-04-04 10:50:14 +01001027 __ fs()->call(
1028 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001029
Nicolas Geoffray39468442014-09-02 15:17:15 +01001030 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001031 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001032}
1033
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001034void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001035 LocationSummary* locations =
1036 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001037 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1038 if (location.IsStackSlot()) {
1039 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1040 } else if (location.IsDoubleStackSlot()) {
1041 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001042 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001043 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001044}
1045
1046void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001047}
1048
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001049void LocationsBuilderX86::VisitNot(HNot* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001050 LocationSummary* locations =
1051 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001052 locations->SetInAt(0, Location::RequiresRegister());
1053 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001054}
1055
1056void InstructionCodeGeneratorX86::VisitNot(HNot* instruction) {
1057 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001058 Location out = locations->Out();
1059 DCHECK_EQ(locations->InAt(0).AsX86().AsCpuRegister(), out.AsX86().AsCpuRegister());
1060 __ xorl(out.AsX86().AsCpuRegister(), Immediate(1));
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001061}
1062
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001063void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001064 LocationSummary* locations =
1065 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001066 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001067 locations->SetInAt(1, Location::Any());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001068 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001069}
1070
1071void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
1072 Label greater, done;
1073 LocationSummary* locations = compare->GetLocations();
1074 switch (compare->InputAt(0)->GetType()) {
1075 case Primitive::kPrimLong: {
1076 Label less, greater, done;
1077 Register output = locations->Out().AsX86().AsCpuRegister();
1078 X86ManagedRegister left = locations->InAt(0).AsX86();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001079 Location right = locations->InAt(1);
1080 if (right.IsRegister()) {
1081 __ cmpl(left.AsRegisterPairHigh(), right.AsX86().AsRegisterPairHigh());
1082 } else {
1083 DCHECK(right.IsDoubleStackSlot());
1084 __ cmpl(left.AsRegisterPairHigh(), Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1085 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001086 __ j(kLess, &less); // Signed compare.
1087 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001088 if (right.IsRegister()) {
1089 __ cmpl(left.AsRegisterPairLow(), right.AsX86().AsRegisterPairLow());
1090 } else {
1091 DCHECK(right.IsDoubleStackSlot());
1092 __ cmpl(left.AsRegisterPairLow(), Address(ESP, right.GetStackIndex()));
1093 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001094 __ movl(output, Immediate(0));
1095 __ j(kEqual, &done);
1096 __ j(kBelow, &less); // Unsigned compare.
1097
1098 __ Bind(&greater);
1099 __ movl(output, Immediate(1));
1100 __ jmp(&done);
1101
1102 __ Bind(&less);
1103 __ movl(output, Immediate(-1));
1104
1105 __ Bind(&done);
1106 break;
1107 }
1108 default:
1109 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
1110 }
1111}
1112
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001113void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001114 LocationSummary* locations =
1115 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001116 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1117 locations->SetInAt(i, Location::Any());
1118 }
1119 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001120}
1121
1122void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001123 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001124}
1125
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001126void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001127 LocationSummary* locations =
1128 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001129 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001130 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001131 if (field_type == Primitive::kPrimBoolean || field_type == Primitive::kPrimByte) {
1132 // Ensure the value is in a byte register.
1133 locations->SetInAt(1, X86CpuLocation(EAX));
1134 } else {
1135 locations->SetInAt(1, Location::RequiresRegister());
1136 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001137 // Temporary registers for the write barrier.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001138 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001139 locations->AddTemp(Location::RequiresRegister());
1140 // Ensure the card is in a byte register.
1141 locations->AddTemp(X86CpuLocation(ECX));
1142 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001143}
1144
1145void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1146 LocationSummary* locations = instruction->GetLocations();
1147 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1148 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001149 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001150
1151 switch (field_type) {
1152 case Primitive::kPrimBoolean:
1153 case Primitive::kPrimByte: {
1154 ByteRegister value = locations->InAt(1).AsX86().AsByteRegister();
1155 __ movb(Address(obj, offset), value);
1156 break;
1157 }
1158
1159 case Primitive::kPrimShort:
1160 case Primitive::kPrimChar: {
1161 Register value = locations->InAt(1).AsX86().AsCpuRegister();
1162 __ movw(Address(obj, offset), value);
1163 break;
1164 }
1165
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001166 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001167 case Primitive::kPrimNot: {
1168 Register value = locations->InAt(1).AsX86().AsCpuRegister();
1169 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001170
1171 if (field_type == Primitive::kPrimNot) {
1172 Register temp = locations->GetTemp(0).AsX86().AsCpuRegister();
1173 Register card = locations->GetTemp(1).AsX86().AsCpuRegister();
1174 codegen_->MarkGCCard(temp, card, obj, value);
1175 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001176 break;
1177 }
1178
1179 case Primitive::kPrimLong: {
1180 X86ManagedRegister value = locations->InAt(1).AsX86();
1181 __ movl(Address(obj, offset), value.AsRegisterPairLow());
1182 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh());
1183 break;
1184 }
1185
1186 case Primitive::kPrimFloat:
1187 case Primitive::kPrimDouble:
1188 LOG(FATAL) << "Unimplemented register type " << field_type;
1189
1190 case Primitive::kPrimVoid:
1191 LOG(FATAL) << "Unreachable type " << field_type;
1192 }
1193}
1194
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001195void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
1196 Label is_null;
1197 __ testl(value, value);
1198 __ j(kEqual, &is_null);
1199 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
1200 __ movl(temp, object);
1201 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
1202 __ movb(Address(temp, card, TIMES_1, 0),
1203 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
1204 __ Bind(&is_null);
1205}
1206
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001207void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001208 LocationSummary* locations =
1209 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001210 locations->SetInAt(0, Location::RequiresRegister());
1211 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001212}
1213
1214void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1215 LocationSummary* locations = instruction->GetLocations();
1216 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1217 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
1218
1219 switch (instruction->GetType()) {
1220 case Primitive::kPrimBoolean: {
1221 Register out = locations->Out().AsX86().AsCpuRegister();
1222 __ movzxb(out, Address(obj, offset));
1223 break;
1224 }
1225
1226 case Primitive::kPrimByte: {
1227 Register out = locations->Out().AsX86().AsCpuRegister();
1228 __ movsxb(out, Address(obj, offset));
1229 break;
1230 }
1231
1232 case Primitive::kPrimShort: {
1233 Register out = locations->Out().AsX86().AsCpuRegister();
1234 __ movsxw(out, Address(obj, offset));
1235 break;
1236 }
1237
1238 case Primitive::kPrimChar: {
1239 Register out = locations->Out().AsX86().AsCpuRegister();
1240 __ movzxw(out, Address(obj, offset));
1241 break;
1242 }
1243
1244 case Primitive::kPrimInt:
1245 case Primitive::kPrimNot: {
1246 Register out = locations->Out().AsX86().AsCpuRegister();
1247 __ movl(out, Address(obj, offset));
1248 break;
1249 }
1250
1251 case Primitive::kPrimLong: {
1252 // TODO: support volatile.
1253 X86ManagedRegister out = locations->Out().AsX86();
1254 __ movl(out.AsRegisterPairLow(), Address(obj, offset));
1255 __ movl(out.AsRegisterPairHigh(), Address(obj, kX86WordSize + offset));
1256 break;
1257 }
1258
1259 case Primitive::kPrimFloat:
1260 case Primitive::kPrimDouble:
1261 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1262
1263 case Primitive::kPrimVoid:
1264 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1265 }
1266}
1267
1268void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001269 LocationSummary* locations =
1270 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001271 locations->SetInAt(0, Location::Any());
1272 // TODO: Have a normalization phase that makes this instruction never used.
1273 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001274}
1275
1276void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001277 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001278 codegen_->AddSlowPath(slow_path);
1279
1280 LocationSummary* locations = instruction->GetLocations();
1281 Location obj = locations->InAt(0);
1282 DCHECK(obj.Equals(locations->Out()));
1283
1284 if (obj.IsRegister()) {
1285 __ cmpl(obj.AsX86().AsCpuRegister(), Immediate(0));
1286 } else {
1287 DCHECK(locations->InAt(0).IsStackSlot());
1288 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
1289 }
1290 __ j(kEqual, slow_path->GetEntryLabel());
1291}
1292
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001293void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001294 LocationSummary* locations =
1295 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001296 locations->SetInAt(0, Location::RequiresRegister());
1297 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1298 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001299}
1300
1301void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
1302 LocationSummary* locations = instruction->GetLocations();
1303 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1304 Location index = locations->InAt(1);
1305
1306 switch (instruction->GetType()) {
1307 case Primitive::kPrimBoolean: {
1308 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1309 Register out = locations->Out().AsX86().AsCpuRegister();
1310 if (index.IsConstant()) {
1311 __ movzxb(out, Address(obj,
1312 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1313 } else {
1314 __ movzxb(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_1, data_offset));
1315 }
1316 break;
1317 }
1318
1319 case Primitive::kPrimByte: {
1320 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1321 Register out = locations->Out().AsX86().AsCpuRegister();
1322 if (index.IsConstant()) {
1323 __ movsxb(out, Address(obj,
1324 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1325 } else {
1326 __ movsxb(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_1, data_offset));
1327 }
1328 break;
1329 }
1330
1331 case Primitive::kPrimShort: {
1332 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1333 Register out = locations->Out().AsX86().AsCpuRegister();
1334 if (index.IsConstant()) {
1335 __ movsxw(out, Address(obj,
1336 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1337 } else {
1338 __ movsxw(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_2, data_offset));
1339 }
1340 break;
1341 }
1342
1343 case Primitive::kPrimChar: {
1344 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1345 Register out = locations->Out().AsX86().AsCpuRegister();
1346 if (index.IsConstant()) {
1347 __ movzxw(out, Address(obj,
1348 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1349 } else {
1350 __ movzxw(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_2, data_offset));
1351 }
1352 break;
1353 }
1354
1355 case Primitive::kPrimInt:
1356 case Primitive::kPrimNot: {
1357 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1358 Register out = locations->Out().AsX86().AsCpuRegister();
1359 if (index.IsConstant()) {
1360 __ movl(out, Address(obj,
1361 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1362 } else {
1363 __ movl(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_4, data_offset));
1364 }
1365 break;
1366 }
1367
1368 case Primitive::kPrimLong: {
1369 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1370 X86ManagedRegister out = locations->Out().AsX86();
1371 if (index.IsConstant()) {
1372 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1373 __ movl(out.AsRegisterPairLow(), Address(obj, offset));
1374 __ movl(out.AsRegisterPairHigh(), Address(obj, offset + kX86WordSize));
1375 } else {
1376 __ movl(out.AsRegisterPairLow(),
1377 Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset));
1378 __ movl(out.AsRegisterPairHigh(),
1379 Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset + kX86WordSize));
1380 }
1381 break;
1382 }
1383
1384 case Primitive::kPrimFloat:
1385 case Primitive::kPrimDouble:
1386 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1387
1388 case Primitive::kPrimVoid:
1389 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1390 }
1391}
1392
1393void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001394 Primitive::Type value_type = instruction->GetComponentType();
1395 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1396 instruction,
1397 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
1398
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001399 if (value_type == Primitive::kPrimNot) {
1400 InvokeRuntimeCallingConvention calling_convention;
1401 locations->SetInAt(0, X86CpuLocation(calling_convention.GetRegisterAt(0)));
1402 locations->SetInAt(1, X86CpuLocation(calling_convention.GetRegisterAt(1)));
1403 locations->SetInAt(2, X86CpuLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001404 } else {
1405 locations->SetInAt(0, Location::RequiresRegister());
1406 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1407 if (value_type == Primitive::kPrimBoolean || value_type == Primitive::kPrimByte) {
1408 // Ensure the value is in a byte register.
1409 locations->SetInAt(2, X86CpuLocation(EAX));
1410 } else {
1411 locations->SetInAt(2, Location::RequiresRegister());
1412 }
1413 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001414}
1415
1416void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
1417 LocationSummary* locations = instruction->GetLocations();
1418 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1419 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001420 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001421
1422 switch (value_type) {
1423 case Primitive::kPrimBoolean:
1424 case Primitive::kPrimByte: {
1425 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1426 ByteRegister value = locations->InAt(2).AsX86().AsByteRegister();
1427 if (index.IsConstant()) {
1428 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1429 __ movb(Address(obj, offset), value);
1430 } else {
1431 __ movb(Address(obj, index.AsX86().AsCpuRegister(), TIMES_1, data_offset), value);
1432 }
1433 break;
1434 }
1435
1436 case Primitive::kPrimShort:
1437 case Primitive::kPrimChar: {
1438 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1439 Register value = locations->InAt(2).AsX86().AsCpuRegister();
1440 if (index.IsConstant()) {
1441 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1442 __ movw(Address(obj, offset), value);
1443 } else {
1444 __ movw(Address(obj, index.AsX86().AsCpuRegister(), TIMES_2, data_offset), value);
1445 }
1446 break;
1447 }
1448
1449 case Primitive::kPrimInt: {
1450 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1451 Register value = locations->InAt(2).AsX86().AsCpuRegister();
1452 if (index.IsConstant()) {
1453 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1454 __ movl(Address(obj, offset), value);
1455 } else {
1456 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_4, data_offset), value);
1457 }
1458 break;
1459 }
1460
1461 case Primitive::kPrimNot: {
1462 DCHECK(!codegen_->IsLeafMethod());
1463 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001464 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001465 break;
1466 }
1467
1468 case Primitive::kPrimLong: {
1469 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1470 X86ManagedRegister value = locations->InAt(2).AsX86();
1471 if (index.IsConstant()) {
1472 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1473 __ movl(Address(obj, offset), value.AsRegisterPairLow());
1474 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh());
1475 } else {
1476 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset),
1477 value.AsRegisterPairLow());
1478 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset + kX86WordSize),
1479 value.AsRegisterPairHigh());
1480 }
1481 break;
1482 }
1483
1484 case Primitive::kPrimFloat:
1485 case Primitive::kPrimDouble:
1486 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1487
1488 case Primitive::kPrimVoid:
1489 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1490 }
1491}
1492
1493void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
1494 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1495 locations->SetInAt(0, Location::RequiresRegister());
1496 locations->SetOut(Location::RequiresRegister());
1497 instruction->SetLocations(locations);
1498}
1499
1500void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
1501 LocationSummary* locations = instruction->GetLocations();
1502 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1503 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1504 Register out = locations->Out().AsX86().AsCpuRegister();
1505 __ movl(out, Address(obj, offset));
1506}
1507
1508void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001509 LocationSummary* locations =
1510 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001511 locations->SetInAt(0, Location::RequiresRegister());
1512 locations->SetInAt(1, Location::RequiresRegister());
1513 // TODO: Have a normalization phase that makes this instruction never used.
1514 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001515}
1516
1517void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
1518 LocationSummary* locations = instruction->GetLocations();
1519 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01001520 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001521 codegen_->AddSlowPath(slow_path);
1522
1523 Register index = locations->InAt(0).AsX86().AsCpuRegister();
1524 Register length = locations->InAt(1).AsX86().AsCpuRegister();
1525
1526 __ cmpl(index, length);
1527 __ j(kAboveEqual, slow_path->GetEntryLabel());
1528}
1529
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001530void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
1531 temp->SetLocations(nullptr);
1532}
1533
1534void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
1535 // Nothing to do, this is driven by the code generator.
1536}
1537
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001538void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001539 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001540}
1541
1542void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001543 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
1544}
1545
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001546void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
1547 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
1548}
1549
1550void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
1551 SuspendCheckSlowPathX86* slow_path =
1552 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction);
1553 codegen_->AddSlowPath(slow_path);
1554 __ fs()->cmpl(Address::Absolute(
1555 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
1556 __ j(kNotEqual, slow_path->GetEntryLabel());
1557 __ Bind(slow_path->GetReturnLabel());
1558}
1559
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001560X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
1561 return codegen_->GetAssembler();
1562}
1563
1564void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
1565 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001566 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001567 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1568 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
1569 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
1570}
1571
1572void ParallelMoveResolverX86::EmitMove(size_t index) {
1573 MoveOperands* move = moves_.Get(index);
1574 Location source = move->GetSource();
1575 Location destination = move->GetDestination();
1576
1577 if (source.IsRegister()) {
1578 if (destination.IsRegister()) {
1579 __ movl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister());
1580 } else {
1581 DCHECK(destination.IsStackSlot());
1582 __ movl(Address(ESP, destination.GetStackIndex()), source.AsX86().AsCpuRegister());
1583 }
1584 } else if (source.IsStackSlot()) {
1585 if (destination.IsRegister()) {
1586 __ movl(destination.AsX86().AsCpuRegister(), Address(ESP, source.GetStackIndex()));
1587 } else {
1588 DCHECK(destination.IsStackSlot());
1589 MoveMemoryToMemory(destination.GetStackIndex(),
1590 source.GetStackIndex());
1591 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001592 } else if (source.IsConstant()) {
1593 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
1594 Immediate imm(instruction->AsIntConstant()->GetValue());
1595 if (destination.IsRegister()) {
1596 __ movl(destination.AsX86().AsCpuRegister(), imm);
1597 } else {
1598 __ movl(Address(ESP, destination.GetStackIndex()), imm);
1599 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001600 } else {
1601 LOG(FATAL) << "Unimplemented";
1602 }
1603}
1604
1605void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001606 Register suggested_scratch = reg == EAX ? EBX : EAX;
1607 ScratchRegisterScope ensure_scratch(
1608 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
1609
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001610 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1611 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
1612 __ movl(Address(ESP, mem + stack_offset), reg);
1613 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
1614}
1615
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001616void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
1617 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001618 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
1619
1620 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001621 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001622 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
1623
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001624 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
1625 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
1626 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
1627 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
1628 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
1629 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
1630}
1631
1632void ParallelMoveResolverX86::EmitSwap(size_t index) {
1633 MoveOperands* move = moves_.Get(index);
1634 Location source = move->GetSource();
1635 Location destination = move->GetDestination();
1636
1637 if (source.IsRegister() && destination.IsRegister()) {
1638 __ xchgl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister());
1639 } else if (source.IsRegister() && destination.IsStackSlot()) {
1640 Exchange(source.AsX86().AsCpuRegister(), destination.GetStackIndex());
1641 } else if (source.IsStackSlot() && destination.IsRegister()) {
1642 Exchange(destination.AsX86().AsCpuRegister(), source.GetStackIndex());
1643 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
1644 Exchange(destination.GetStackIndex(), source.GetStackIndex());
1645 } else {
1646 LOG(FATAL) << "Unimplemented";
1647 }
1648}
1649
1650void ParallelMoveResolverX86::SpillScratch(int reg) {
1651 __ pushl(static_cast<Register>(reg));
1652}
1653
1654void ParallelMoveResolverX86::RestoreScratch(int reg) {
1655 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001656}
1657
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001658} // namespace x86
1659} // namespace art