blob: ea67dfda3213d577d12d77ae777c11bde0a8a769 [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:
Roland Levillain5799fc02014-09-25 12:15:20 +010095 BoundsCheckSlowPathX86(HBoundsCheck* instruction,
96 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:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100120 explicit SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
121 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000122
123 virtual void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
124 __ Bind(GetEntryLabel());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100125 codegen->SaveLiveRegisters(instruction_->GetLocations());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000126 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pTestSuspend)));
127 codegen->RecordPcInfo(instruction_, instruction_->GetDexPc());
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100128 codegen->RestoreLiveRegisters(instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100129 if (successor_ == nullptr) {
130 __ jmp(GetReturnLabel());
131 } else {
132 __ jmp(codegen->GetLabelOf(successor_));
133 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000134 }
135
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100136 Label* GetReturnLabel() {
137 DCHECK(successor_ == nullptr);
138 return &return_label_;
139 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000140
141 private:
142 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100143 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000144 Label return_label_;
145
146 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
147};
148
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100149#undef __
150#define __ reinterpret_cast<X86Assembler*>(GetAssembler())->
151
Dave Allison20dfc792014-06-16 20:44:29 -0700152inline Condition X86Condition(IfCondition cond) {
153 switch (cond) {
154 case kCondEQ: return kEqual;
155 case kCondNE: return kNotEqual;
156 case kCondLT: return kLess;
157 case kCondLE: return kLessEqual;
158 case kCondGT: return kGreater;
159 case kCondGE: return kGreaterEqual;
160 default:
161 LOG(FATAL) << "Unknown if condition";
162 }
163 return kEqual;
164}
165
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100166void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
167 stream << X86ManagedRegister::FromCpuRegister(Register(reg));
168}
169
170void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
171 stream << X86ManagedRegister::FromXmmRegister(XmmRegister(reg));
172}
173
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100174void CodeGeneratorX86::SaveCoreRegister(Location stack_location, uint32_t reg_id) {
175 __ movl(Address(ESP, stack_location.GetStackIndex()), static_cast<Register>(reg_id));
176}
177
178void CodeGeneratorX86::RestoreCoreRegister(Location stack_location, uint32_t reg_id) {
179 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_location.GetStackIndex()));
180}
181
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100182CodeGeneratorX86::CodeGeneratorX86(HGraph* graph)
183 : CodeGenerator(graph, kNumberOfRegIds),
184 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100185 instruction_visitor_(graph, this),
186 move_resolver_(graph->GetArena(), this) {}
187
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100188size_t CodeGeneratorX86::FrameEntrySpillSize() const {
189 return kNumberOfPushedRegistersAtEntry * kX86WordSize;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100190}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100191
192static bool* GetBlockedRegisterPairs(bool* blocked_registers) {
193 return blocked_registers + kNumberOfAllocIds;
194}
195
196ManagedRegister CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type,
197 bool* blocked_registers) const {
198 switch (type) {
199 case Primitive::kPrimLong: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100200 bool* blocked_register_pairs = GetBlockedRegisterPairs(blocked_registers);
201 size_t reg = AllocateFreeRegisterInternal(blocked_register_pairs, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100202 X86ManagedRegister pair =
203 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
204 blocked_registers[pair.AsRegisterPairLow()] = true;
205 blocked_registers[pair.AsRegisterPairHigh()] = true;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100206 // Block all other register pairs that share a register with `pair`.
207 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
208 X86ManagedRegister current =
209 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
210 if (current.AsRegisterPairLow() == pair.AsRegisterPairLow()
211 || current.AsRegisterPairLow() == pair.AsRegisterPairHigh()
212 || current.AsRegisterPairHigh() == pair.AsRegisterPairLow()
213 || current.AsRegisterPairHigh() == pair.AsRegisterPairHigh()) {
214 blocked_register_pairs[i] = true;
215 }
216 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100217 return pair;
218 }
219
220 case Primitive::kPrimByte:
221 case Primitive::kPrimBoolean:
222 case Primitive::kPrimChar:
223 case Primitive::kPrimShort:
224 case Primitive::kPrimInt:
225 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100226 Register reg = static_cast<Register>(
227 AllocateFreeRegisterInternal(blocked_registers, kNumberOfCpuRegisters));
228 // Block all register pairs that contain `reg`.
229 bool* blocked_register_pairs = GetBlockedRegisterPairs(blocked_registers);
230 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
231 X86ManagedRegister current =
232 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
233 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
234 blocked_register_pairs[i] = true;
235 }
236 }
237 return X86ManagedRegister::FromCpuRegister(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100238 }
239
240 case Primitive::kPrimFloat:
241 case Primitive::kPrimDouble:
242 LOG(FATAL) << "Unimplemented register type " << type;
243
244 case Primitive::kPrimVoid:
245 LOG(FATAL) << "Unreachable type " << type;
246 }
247
248 return ManagedRegister::NoRegister();
249}
250
251void CodeGeneratorX86::SetupBlockedRegisters(bool* blocked_registers) const {
252 bool* blocked_register_pairs = GetBlockedRegisterPairs(blocked_registers);
253
254 // Don't allocate the dalvik style register pair passing.
255 blocked_register_pairs[ECX_EDX] = true;
256
257 // Stack register is always reserved.
258 blocked_registers[ESP] = true;
259
260 // TODO: We currently don't use Quick's callee saved registers.
261 blocked_registers[EBP] = true;
262 blocked_registers[ESI] = true;
263 blocked_registers[EDI] = true;
264 blocked_register_pairs[EAX_EDI] = true;
265 blocked_register_pairs[EDX_EDI] = true;
266 blocked_register_pairs[ECX_EDI] = true;
267 blocked_register_pairs[EBX_EDI] = true;
268}
269
270size_t CodeGeneratorX86::GetNumberOfRegisters() const {
271 return kNumberOfRegIds;
272}
273
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100274InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
275 : HGraphVisitor(graph),
276 assembler_(codegen->GetAssembler()),
277 codegen_(codegen) {}
278
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000279void CodeGeneratorX86::GenerateFrameEntry() {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000280 // Create a fake register to mimic Quick.
281 static const int kFakeReturnRegister = 8;
282 core_spill_mask_ |= (1 << kFakeReturnRegister);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000283
Dave Allison648d7112014-07-25 16:15:27 -0700284 bool skip_overflow_check = IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100285 if (!skip_overflow_check && !kExplicitStackOverflowCheck) {
286 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100287 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100288 }
289
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100290 // The return PC has already been pushed on the stack.
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100291 __ subl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100292
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100293 if (!skip_overflow_check && kExplicitStackOverflowCheck) {
294 SlowPathCode* slow_path = new (GetGraph()->GetArena()) StackOverflowCheckSlowPathX86();
295 AddSlowPath(slow_path);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100296
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100297 __ fs()->cmpl(ESP, Address::Absolute(Thread::StackEndOffset<kX86WordSize>()));
298 __ j(kLess, slow_path->GetEntryLabel());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100299 }
300
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100301 __ movl(Address(ESP, kCurrentMethodStackOffset), EAX);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000302}
303
304void CodeGeneratorX86::GenerateFrameExit() {
Nicolas Geoffray707c8092014-04-04 10:50:14 +0100305 __ addl(ESP, Immediate(GetFrameSize() - kNumberOfPushedRegistersAtEntry * kX86WordSize));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000306}
307
308void CodeGeneratorX86::Bind(Label* label) {
309 __ Bind(label);
310}
311
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000312void InstructionCodeGeneratorX86::LoadCurrentMethod(Register reg) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100313 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000314}
315
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100316Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
317 switch (load->GetType()) {
318 case Primitive::kPrimLong:
319 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
320 break;
321
322 case Primitive::kPrimInt:
323 case Primitive::kPrimNot:
324 return Location::StackSlot(GetStackSlot(load->GetLocal()));
325
326 case Primitive::kPrimFloat:
327 case Primitive::kPrimDouble:
328 LOG(FATAL) << "Unimplemented type " << load->GetType();
329
330 case Primitive::kPrimBoolean:
331 case Primitive::kPrimByte:
332 case Primitive::kPrimChar:
333 case Primitive::kPrimShort:
334 case Primitive::kPrimVoid:
335 LOG(FATAL) << "Unexpected type " << load->GetType();
336 }
337
338 LOG(FATAL) << "Unreachable";
339 return Location();
340}
341
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100342Location InvokeDexCallingConventionVisitor::GetNextLocation(Primitive::Type type) {
343 switch (type) {
344 case Primitive::kPrimBoolean:
345 case Primitive::kPrimByte:
346 case Primitive::kPrimChar:
347 case Primitive::kPrimShort:
348 case Primitive::kPrimInt:
349 case Primitive::kPrimNot: {
350 uint32_t index = gp_index_++;
351 if (index < calling_convention.GetNumberOfRegisters()) {
352 return X86CpuLocation(calling_convention.GetRegisterAt(index));
353 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100354 return Location::StackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100355 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100356 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100357
358 case Primitive::kPrimLong: {
359 uint32_t index = gp_index_;
360 gp_index_ += 2;
361 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
362 return Location::RegisterLocation(X86ManagedRegister::FromRegisterPair(
363 calling_convention.GetRegisterPairAt(index)));
364 } else if (index + 1 == calling_convention.GetNumberOfRegisters()) {
365 return Location::QuickParameter(index);
366 } else {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100367 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100368 }
369 }
370
371 case Primitive::kPrimDouble:
372 case Primitive::kPrimFloat:
373 LOG(FATAL) << "Unimplemented parameter type " << type;
374 break;
375
376 case Primitive::kPrimVoid:
377 LOG(FATAL) << "Unexpected parameter type " << type;
378 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100379 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100380 return Location();
381}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100382
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100383void CodeGeneratorX86::Move32(Location destination, Location source) {
384 if (source.Equals(destination)) {
385 return;
386 }
387 if (destination.IsRegister()) {
388 if (source.IsRegister()) {
389 __ movl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister());
390 } else {
391 DCHECK(source.IsStackSlot());
392 __ movl(destination.AsX86().AsCpuRegister(), Address(ESP, source.GetStackIndex()));
393 }
394 } else {
395 if (source.IsRegister()) {
396 __ movl(Address(ESP, destination.GetStackIndex()), source.AsX86().AsCpuRegister());
397 } else {
398 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100399 __ pushl(Address(ESP, source.GetStackIndex()));
400 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100401 }
402 }
403}
404
405void CodeGeneratorX86::Move64(Location destination, Location source) {
406 if (source.Equals(destination)) {
407 return;
408 }
409 if (destination.IsRegister()) {
410 if (source.IsRegister()) {
411 __ movl(destination.AsX86().AsRegisterPairLow(), source.AsX86().AsRegisterPairLow());
412 __ movl(destination.AsX86().AsRegisterPairHigh(), source.AsX86().AsRegisterPairHigh());
413 } else if (source.IsQuickParameter()) {
414 uint32_t argument_index = source.GetQuickParameterIndex();
415 InvokeDexCallingConvention calling_convention;
416 __ movl(destination.AsX86().AsRegisterPairLow(),
417 calling_convention.GetRegisterAt(argument_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100418 __ movl(destination.AsX86().AsRegisterPairHigh(), Address(ESP,
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100419 calling_convention.GetStackOffsetOf(argument_index + 1) + GetFrameSize()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100420 } else {
421 DCHECK(source.IsDoubleStackSlot());
422 __ movl(destination.AsX86().AsRegisterPairLow(), Address(ESP, source.GetStackIndex()));
423 __ movl(destination.AsX86().AsRegisterPairHigh(),
424 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
425 }
426 } else if (destination.IsQuickParameter()) {
427 InvokeDexCallingConvention calling_convention;
428 uint32_t argument_index = destination.GetQuickParameterIndex();
429 if (source.IsRegister()) {
430 __ movl(calling_convention.GetRegisterAt(argument_index), source.AsX86().AsRegisterPairLow());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100431 __ movl(Address(ESP, calling_convention.GetStackOffsetOf(argument_index + 1)),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100432 source.AsX86().AsRegisterPairHigh());
433 } else {
434 DCHECK(source.IsDoubleStackSlot());
435 __ movl(calling_convention.GetRegisterAt(argument_index),
436 Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100437 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100438 __ popl(Address(ESP, calling_convention.GetStackOffsetOf(argument_index + 1)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100439 }
440 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100441 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100442 if (source.IsRegister()) {
443 __ movl(Address(ESP, destination.GetStackIndex()), source.AsX86().AsRegisterPairLow());
444 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
445 source.AsX86().AsRegisterPairHigh());
446 } else if (source.IsQuickParameter()) {
447 InvokeDexCallingConvention calling_convention;
448 uint32_t argument_index = source.GetQuickParameterIndex();
449 __ movl(Address(ESP, destination.GetStackIndex()),
450 calling_convention.GetRegisterAt(argument_index));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100451 __ pushl(Address(ESP,
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100452 calling_convention.GetStackOffsetOf(argument_index + 1) + GetFrameSize()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100453 __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100454 } else {
455 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100456 __ pushl(Address(ESP, source.GetStackIndex()));
457 __ popl(Address(ESP, destination.GetStackIndex()));
458 __ pushl(Address(ESP, source.GetHighStackIndex(kX86WordSize)));
459 __ popl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100460 }
461 }
462}
463
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100464void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
465 if (instruction->AsIntConstant() != nullptr) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100466 Immediate imm(instruction->AsIntConstant()->GetValue());
467 if (location.IsRegister()) {
468 __ movl(location.AsX86().AsCpuRegister(), imm);
469 } else {
470 __ movl(Address(ESP, location.GetStackIndex()), imm);
471 }
472 } else if (instruction->AsLongConstant() != nullptr) {
473 int64_t value = instruction->AsLongConstant()->GetValue();
474 if (location.IsRegister()) {
475 __ movl(location.AsX86().AsRegisterPairLow(), Immediate(Low32Bits(value)));
476 __ movl(location.AsX86().AsRegisterPairHigh(), Immediate(High32Bits(value)));
477 } else {
478 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
479 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
480 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100481 } else if (instruction->AsLoadLocal() != nullptr) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100482 switch (instruction->GetType()) {
483 case Primitive::kPrimBoolean:
484 case Primitive::kPrimByte:
485 case Primitive::kPrimChar:
486 case Primitive::kPrimShort:
487 case Primitive::kPrimInt:
488 case Primitive::kPrimNot:
489 Move32(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
490 break;
491
492 case Primitive::kPrimLong:
493 Move64(location, Location::DoubleStackSlot(
494 GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
495 break;
496
497 default:
498 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
499 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000500 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100501 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100502 switch (instruction->GetType()) {
503 case Primitive::kPrimBoolean:
504 case Primitive::kPrimByte:
505 case Primitive::kPrimChar:
506 case Primitive::kPrimShort:
507 case Primitive::kPrimInt:
508 case Primitive::kPrimNot:
509 Move32(location, instruction->GetLocations()->Out());
510 break;
511
512 case Primitive::kPrimLong:
513 Move64(location, instruction->GetLocations()->Out());
514 break;
515
516 default:
517 LOG(FATAL) << "Unimplemented type " << instruction->GetType();
518 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000519 }
520}
521
522void LocationsBuilderX86::VisitGoto(HGoto* got) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000523 got->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000524}
525
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000526void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000527 HBasicBlock* successor = got->GetSuccessor();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100528 DCHECK(!successor->IsExitBlock());
529
530 HBasicBlock* block = got->GetBlock();
531 HInstruction* previous = got->GetPrevious();
532
533 HLoopInformation* info = block->GetLoopInformation();
534 if (info != nullptr && info->IsBackEdge(block) && info->HasSuspendCheck()) {
535 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
536 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
537 return;
538 }
539
540 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
541 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
542 }
543 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000544 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000545 }
546}
547
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000548void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000549 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000550}
551
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000552void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000553 if (kIsDebugBuild) {
554 __ Comment("Unreachable");
555 __ int3();
556 }
557}
558
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000559void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100560 LocationSummary* locations =
561 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100562 HInstruction* cond = if_instr->InputAt(0);
563 DCHECK(cond->IsCondition());
564 HCondition* condition = cond->AsCondition();
565 if (condition->NeedsMaterialization()) {
566 locations->SetInAt(0, Location::Any());
567 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000568}
569
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000570void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
Dave Allison20dfc792014-06-16 20:44:29 -0700571 HInstruction* cond = if_instr->InputAt(0);
572 DCHECK(cond->IsCondition());
573 HCondition* condition = cond->AsCondition();
574 if (condition->NeedsMaterialization()) {
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100575 // Moves do not affect the eflags register, so if the condition is evaluated
576 // just before the if, we don't need to evaluate it again.
577 if (!condition->IsBeforeWhenDisregardMoves(if_instr)) {
578 // Materialized condition, compare against 0
579 Location lhs = if_instr->GetLocations()->InAt(0);
580 if (lhs.IsRegister()) {
581 __ cmpl(lhs.AsX86().AsCpuRegister(), Immediate(0));
582 } else {
583 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
584 }
Dave Allison20dfc792014-06-16 20:44:29 -0700585 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100586 __ j(kNotEqual, codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100587 } else {
Dave Allison20dfc792014-06-16 20:44:29 -0700588 Location lhs = condition->GetLocations()->InAt(0);
589 Location rhs = condition->GetLocations()->InAt(1);
590 // LHS is guaranteed to be in a register (see LocationsBuilderX86::VisitCondition).
591 if (rhs.IsRegister()) {
592 __ cmpl(lhs.AsX86().AsCpuRegister(), rhs.AsX86().AsCpuRegister());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100593 } else if (rhs.IsConstant()) {
594 HIntConstant* instruction = rhs.GetConstant()->AsIntConstant();
595 Immediate imm(instruction->AsIntConstant()->GetValue());
596 __ cmpl(lhs.AsX86().AsCpuRegister(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700597 } else {
598 __ cmpl(lhs.AsX86().AsCpuRegister(), Address(ESP, rhs.GetStackIndex()));
599 }
600 __ j(X86Condition(condition->GetCondition()),
601 codegen_->GetLabelOf(if_instr->IfTrueSuccessor()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100602 }
Dave Allison20dfc792014-06-16 20:44:29 -0700603 if (!codegen_->GoesToNextBlock(if_instr->GetBlock(), if_instr->IfFalseSuccessor())) {
604 __ jmp(codegen_->GetLabelOf(if_instr->IfFalseSuccessor()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000605 }
606}
607
608void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000609 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000610}
611
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000612void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
613 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000614}
615
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000616void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100617 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000618}
619
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000620void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100621 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000622}
623
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100624void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100625 LocationSummary* locations =
626 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100627 switch (store->InputAt(1)->GetType()) {
628 case Primitive::kPrimBoolean:
629 case Primitive::kPrimByte:
630 case Primitive::kPrimChar:
631 case Primitive::kPrimShort:
632 case Primitive::kPrimInt:
633 case Primitive::kPrimNot:
634 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
635 break;
636
637 case Primitive::kPrimLong:
638 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
639 break;
640
641 default:
642 LOG(FATAL) << "Unimplemented local type " << store->InputAt(1)->GetType();
643 }
644 store->SetLocations(locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000645}
646
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000647void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000648}
649
Dave Allison20dfc792014-06-16 20:44:29 -0700650void LocationsBuilderX86::VisitCondition(HCondition* comp) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100651 LocationSummary* locations =
652 new (GetGraph()->GetArena()) LocationSummary(comp, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100653 locations->SetInAt(0, Location::RequiresRegister());
654 locations->SetInAt(1, Location::Any());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100655 if (comp->NeedsMaterialization()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100656 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100657 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000658}
659
Dave Allison20dfc792014-06-16 20:44:29 -0700660void InstructionCodeGeneratorX86::VisitCondition(HCondition* comp) {
661 if (comp->NeedsMaterialization()) {
662 LocationSummary* locations = comp->GetLocations();
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100663 Register reg = locations->Out().AsX86().AsCpuRegister();
664 // Clear register: setcc only sets the low byte.
665 __ xorl(reg, reg);
Dave Allison20dfc792014-06-16 20:44:29 -0700666 if (locations->InAt(1).IsRegister()) {
667 __ cmpl(locations->InAt(0).AsX86().AsCpuRegister(),
668 locations->InAt(1).AsX86().AsCpuRegister());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100669 } else if (locations->InAt(1).IsConstant()) {
670 HConstant* instruction = locations->InAt(1).GetConstant();
671 Immediate imm(instruction->AsIntConstant()->GetValue());
672 __ cmpl(locations->InAt(0).AsX86().AsCpuRegister(), imm);
Dave Allison20dfc792014-06-16 20:44:29 -0700673 } else {
674 __ cmpl(locations->InAt(0).AsX86().AsCpuRegister(),
675 Address(ESP, locations->InAt(1).GetStackIndex()));
676 }
Nicolas Geoffray18efde52014-09-22 15:51:11 +0100677 __ setb(X86Condition(comp->GetCondition()), reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100678 }
Dave Allison20dfc792014-06-16 20:44:29 -0700679}
680
681void LocationsBuilderX86::VisitEqual(HEqual* comp) {
682 VisitCondition(comp);
683}
684
685void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
686 VisitCondition(comp);
687}
688
689void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
690 VisitCondition(comp);
691}
692
693void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
694 VisitCondition(comp);
695}
696
697void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
698 VisitCondition(comp);
699}
700
701void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
702 VisitCondition(comp);
703}
704
705void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
706 VisitCondition(comp);
707}
708
709void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
710 VisitCondition(comp);
711}
712
713void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
714 VisitCondition(comp);
715}
716
717void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
718 VisitCondition(comp);
719}
720
721void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
722 VisitCondition(comp);
723}
724
725void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
726 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000727}
728
729void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100730 LocationSummary* locations =
731 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100732 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000733}
734
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000735void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000736}
737
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100738void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100739 LocationSummary* locations =
740 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100741 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100742}
743
744void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
745 // Will be generated at use site.
746}
747
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000748void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000749 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000750}
751
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000752void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
753 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000754 __ ret();
755}
756
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000757void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100758 LocationSummary* locations =
759 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100760 switch (ret->InputAt(0)->GetType()) {
761 case Primitive::kPrimBoolean:
762 case Primitive::kPrimByte:
763 case Primitive::kPrimChar:
764 case Primitive::kPrimShort:
765 case Primitive::kPrimInt:
766 case Primitive::kPrimNot:
767 locations->SetInAt(0, X86CpuLocation(EAX));
768 break;
769
770 case Primitive::kPrimLong:
771 locations->SetInAt(
772 0, Location::RegisterLocation(X86ManagedRegister::FromRegisterPair(EAX_EDX)));
773 break;
774
775 default:
776 LOG(FATAL) << "Unimplemented return type " << ret->InputAt(0)->GetType();
777 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000778}
779
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000780void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100781 if (kIsDebugBuild) {
782 switch (ret->InputAt(0)->GetType()) {
783 case Primitive::kPrimBoolean:
784 case Primitive::kPrimByte:
785 case Primitive::kPrimChar:
786 case Primitive::kPrimShort:
787 case Primitive::kPrimInt:
788 case Primitive::kPrimNot:
789 DCHECK_EQ(ret->GetLocations()->InAt(0).AsX86().AsCpuRegister(), EAX);
790 break;
791
792 case Primitive::kPrimLong:
793 DCHECK_EQ(ret->GetLocations()->InAt(0).AsX86().AsRegisterPair(), EAX_EDX);
794 break;
795
796 default:
797 LOG(FATAL) << "Unimplemented return type " << ret->InputAt(0)->GetType();
798 }
799 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000800 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000801 __ ret();
802}
803
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000804void LocationsBuilderX86::VisitInvokeStatic(HInvokeStatic* invoke) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100805 HandleInvoke(invoke);
806}
807
808void InstructionCodeGeneratorX86::VisitInvokeStatic(HInvokeStatic* invoke) {
809 Register temp = invoke->GetLocations()->GetTemp(0).AsX86().AsCpuRegister();
810 uint32_t heap_reference_size = sizeof(mirror::HeapReference<mirror::Object>);
811 size_t index_in_cache = mirror::Array::DataOffset(heap_reference_size).Int32Value() +
812 invoke->GetIndexInDexCache() * kX86WordSize;
813
814 // TODO: Implement all kinds of calls:
815 // 1) boot -> boot
816 // 2) app -> boot
817 // 3) app -> app
818 //
819 // Currently we implement the app -> app logic, which looks up in the resolve cache.
820
821 // temp = method;
822 LoadCurrentMethod(temp);
823 // temp = temp->dex_cache_resolved_methods_;
824 __ movl(temp, Address(temp, mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value()));
825 // temp = temp[index_in_cache]
826 __ movl(temp, Address(temp, index_in_cache));
827 // (temp + offset_of_quick_compiled_code)()
828 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
829
830 DCHECK(!codegen_->IsLeafMethod());
831 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
832}
833
834void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
835 HandleInvoke(invoke);
836}
837
838void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100839 LocationSummary* locations =
840 new (GetGraph()->GetArena()) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100841 locations->AddTemp(X86CpuLocation(EAX));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100842
843 InvokeDexCallingConventionVisitor calling_convention_visitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100844 for (size_t i = 0; i < invoke->InputCount(); i++) {
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100845 HInstruction* input = invoke->InputAt(i);
846 locations->SetInAt(i, calling_convention_visitor.GetNextLocation(input->GetType()));
847 }
848
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100849 switch (invoke->GetType()) {
850 case Primitive::kPrimBoolean:
851 case Primitive::kPrimByte:
852 case Primitive::kPrimChar:
853 case Primitive::kPrimShort:
854 case Primitive::kPrimInt:
855 case Primitive::kPrimNot:
856 locations->SetOut(X86CpuLocation(EAX));
857 break;
858
859 case Primitive::kPrimLong:
860 locations->SetOut(Location::RegisterLocation(X86ManagedRegister::FromRegisterPair(EAX_EDX)));
861 break;
862
863 case Primitive::kPrimVoid:
864 break;
865
866 case Primitive::kPrimDouble:
867 case Primitive::kPrimFloat:
868 LOG(FATAL) << "Unimplemented return type " << invoke->GetType();
869 break;
870 }
871
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000872 invoke->SetLocations(locations);
873}
874
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100875void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100876 Register temp = invoke->GetLocations()->GetTemp(0).AsX86().AsCpuRegister();
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100877 uint32_t method_offset = mirror::Class::EmbeddedVTableOffset().Uint32Value() +
878 invoke->GetVTableIndex() * sizeof(mirror::Class::VTableEntry);
879 LocationSummary* locations = invoke->GetLocations();
880 Location receiver = locations->InAt(0);
881 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
882 // temp = object->GetClass();
883 if (receiver.IsStackSlot()) {
884 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
885 __ movl(temp, Address(temp, class_offset));
886 } else {
887 __ movl(temp, Address(receiver.AsX86().AsCpuRegister(), class_offset));
888 }
889 // temp = temp->GetMethodAt(method_offset);
890 __ movl(temp, Address(temp, method_offset));
891 // call temp->GetEntryPoint();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000892 __ call(Address(temp, mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value()));
893
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100894 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +0100895 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000896}
897
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000898void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100899 LocationSummary* locations =
900 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000901 switch (add->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100902 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100903 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100904 locations->SetInAt(0, Location::RequiresRegister());
905 locations->SetInAt(1, Location::Any());
906 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100907 break;
908 }
909
910 case Primitive::kPrimBoolean:
911 case Primitive::kPrimByte:
912 case Primitive::kPrimChar:
913 case Primitive::kPrimShort:
914 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
915 break;
916
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000917 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100918 LOG(FATAL) << "Unimplemented add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000919 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000920}
921
922void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
923 LocationSummary* locations = add->GetLocations();
924 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100925 case Primitive::kPrimInt: {
926 DCHECK_EQ(locations->InAt(0).AsX86().AsCpuRegister(),
927 locations->Out().AsX86().AsCpuRegister());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100928 if (locations->InAt(1).IsRegister()) {
929 __ addl(locations->InAt(0).AsX86().AsCpuRegister(),
930 locations->InAt(1).AsX86().AsCpuRegister());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100931 } else if (locations->InAt(1).IsConstant()) {
932 HConstant* instruction = locations->InAt(1).GetConstant();
933 Immediate imm(instruction->AsIntConstant()->GetValue());
934 __ addl(locations->InAt(0).AsX86().AsCpuRegister(), imm);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100935 } else {
936 __ addl(locations->InAt(0).AsX86().AsCpuRegister(),
937 Address(ESP, locations->InAt(1).GetStackIndex()));
938 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000939 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100940 }
941
942 case Primitive::kPrimLong: {
943 DCHECK_EQ(locations->InAt(0).AsX86().AsRegisterPair(),
944 locations->Out().AsX86().AsRegisterPair());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100945 if (locations->InAt(1).IsRegister()) {
946 __ addl(locations->InAt(0).AsX86().AsRegisterPairLow(),
947 locations->InAt(1).AsX86().AsRegisterPairLow());
948 __ adcl(locations->InAt(0).AsX86().AsRegisterPairHigh(),
949 locations->InAt(1).AsX86().AsRegisterPairHigh());
950 } else {
951 __ addl(locations->InAt(0).AsX86().AsRegisterPairLow(),
952 Address(ESP, locations->InAt(1).GetStackIndex()));
953 __ adcl(locations->InAt(0).AsX86().AsRegisterPairHigh(),
954 Address(ESP, locations->InAt(1).GetHighStackIndex(kX86WordSize)));
955 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100956 break;
957 }
958
959 case Primitive::kPrimBoolean:
960 case Primitive::kPrimByte:
961 case Primitive::kPrimChar:
962 case Primitive::kPrimShort:
963 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
964 break;
965
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000966 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100967 LOG(FATAL) << "Unimplemented add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000968 }
969}
970
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100971void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100972 LocationSummary* locations =
973 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100974 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100975 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100976 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100977 locations->SetInAt(0, Location::RequiresRegister());
978 locations->SetInAt(1, Location::Any());
979 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100980 break;
981 }
982
983 case Primitive::kPrimBoolean:
984 case Primitive::kPrimByte:
985 case Primitive::kPrimChar:
986 case Primitive::kPrimShort:
987 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
988 break;
989
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100990 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100991 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100992 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100993}
994
995void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
996 LocationSummary* locations = sub->GetLocations();
997 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100998 case Primitive::kPrimInt: {
999 DCHECK_EQ(locations->InAt(0).AsX86().AsCpuRegister(),
1000 locations->Out().AsX86().AsCpuRegister());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001001 if (locations->InAt(1).IsRegister()) {
1002 __ subl(locations->InAt(0).AsX86().AsCpuRegister(),
1003 locations->InAt(1).AsX86().AsCpuRegister());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001004 } else if (locations->InAt(1).IsConstant()) {
1005 HConstant* instruction = locations->InAt(1).GetConstant();
1006 Immediate imm(instruction->AsIntConstant()->GetValue());
1007 __ subl(locations->InAt(0).AsX86().AsCpuRegister(), imm);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001008 } else {
1009 __ subl(locations->InAt(0).AsX86().AsCpuRegister(),
1010 Address(ESP, locations->InAt(1).GetStackIndex()));
1011 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001012 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001013 }
1014
1015 case Primitive::kPrimLong: {
1016 DCHECK_EQ(locations->InAt(0).AsX86().AsRegisterPair(),
1017 locations->Out().AsX86().AsRegisterPair());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001018 if (locations->InAt(1).IsRegister()) {
1019 __ subl(locations->InAt(0).AsX86().AsRegisterPairLow(),
1020 locations->InAt(1).AsX86().AsRegisterPairLow());
1021 __ sbbl(locations->InAt(0).AsX86().AsRegisterPairHigh(),
1022 locations->InAt(1).AsX86().AsRegisterPairHigh());
1023 } else {
1024 __ subl(locations->InAt(0).AsX86().AsRegisterPairLow(),
1025 Address(ESP, locations->InAt(1).GetStackIndex()));
1026 __ sbbl(locations->InAt(0).AsX86().AsRegisterPairHigh(),
1027 Address(ESP, locations->InAt(1).GetHighStackIndex(kX86WordSize)));
1028 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001029 break;
1030 }
1031
1032 case Primitive::kPrimBoolean:
1033 case Primitive::kPrimByte:
1034 case Primitive::kPrimChar:
1035 case Primitive::kPrimShort:
1036 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
1037 break;
1038
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001039 default:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001040 LOG(FATAL) << "Unimplemented sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001041 }
1042}
1043
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001044void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001045 LocationSummary* locations =
1046 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001047 locations->SetOut(X86CpuLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001048 InvokeRuntimeCallingConvention calling_convention;
1049 locations->AddTemp(X86CpuLocation(calling_convention.GetRegisterAt(0)));
1050 locations->AddTemp(X86CpuLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001051}
1052
1053void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
1054 InvokeRuntimeCallingConvention calling_convention;
1055 LoadCurrentMethod(calling_convention.GetRegisterAt(1));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001056 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001057
Nicolas Geoffray707c8092014-04-04 10:50:14 +01001058 __ fs()->call(
1059 Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAllocObjectWithAccessCheck)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001060
Nicolas Geoffray39468442014-09-02 15:17:15 +01001061 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001062 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001063}
1064
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001065void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001066 LocationSummary* locations =
1067 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001068 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
1069 if (location.IsStackSlot()) {
1070 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
1071 } else if (location.IsDoubleStackSlot()) {
1072 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001073 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001074 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001075}
1076
1077void InstructionCodeGeneratorX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001078}
1079
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001080void LocationsBuilderX86::VisitNot(HNot* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001081 LocationSummary* locations =
1082 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001083 locations->SetInAt(0, Location::RequiresRegister());
1084 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001085}
1086
1087void InstructionCodeGeneratorX86::VisitNot(HNot* instruction) {
1088 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001089 Location out = locations->Out();
1090 DCHECK_EQ(locations->InAt(0).AsX86().AsCpuRegister(), out.AsX86().AsCpuRegister());
1091 __ xorl(out.AsX86().AsCpuRegister(), Immediate(1));
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001092}
1093
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001094void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001095 LocationSummary* locations =
1096 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001097 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001098 locations->SetInAt(1, Location::Any());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001099 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001100}
1101
1102void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
1103 Label greater, done;
1104 LocationSummary* locations = compare->GetLocations();
1105 switch (compare->InputAt(0)->GetType()) {
1106 case Primitive::kPrimLong: {
1107 Label less, greater, done;
1108 Register output = locations->Out().AsX86().AsCpuRegister();
1109 X86ManagedRegister left = locations->InAt(0).AsX86();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001110 Location right = locations->InAt(1);
1111 if (right.IsRegister()) {
1112 __ cmpl(left.AsRegisterPairHigh(), right.AsX86().AsRegisterPairHigh());
1113 } else {
1114 DCHECK(right.IsDoubleStackSlot());
1115 __ cmpl(left.AsRegisterPairHigh(), Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1116 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001117 __ j(kLess, &less); // Signed compare.
1118 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001119 if (right.IsRegister()) {
1120 __ cmpl(left.AsRegisterPairLow(), right.AsX86().AsRegisterPairLow());
1121 } else {
1122 DCHECK(right.IsDoubleStackSlot());
1123 __ cmpl(left.AsRegisterPairLow(), Address(ESP, right.GetStackIndex()));
1124 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001125 __ movl(output, Immediate(0));
1126 __ j(kEqual, &done);
1127 __ j(kBelow, &less); // Unsigned compare.
1128
1129 __ Bind(&greater);
1130 __ movl(output, Immediate(1));
1131 __ jmp(&done);
1132
1133 __ Bind(&less);
1134 __ movl(output, Immediate(-1));
1135
1136 __ Bind(&done);
1137 break;
1138 }
1139 default:
1140 LOG(FATAL) << "Unimplemented compare type " << compare->InputAt(0)->GetType();
1141 }
1142}
1143
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001144void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001145 LocationSummary* locations =
1146 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001147 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
1148 locations->SetInAt(i, Location::Any());
1149 }
1150 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001151}
1152
1153void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001154 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001155}
1156
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001157void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001158 LocationSummary* locations =
1159 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001160 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001161 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001162 if (field_type == Primitive::kPrimBoolean || field_type == Primitive::kPrimByte) {
1163 // Ensure the value is in a byte register.
1164 locations->SetInAt(1, X86CpuLocation(EAX));
1165 } else {
1166 locations->SetInAt(1, Location::RequiresRegister());
1167 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001168 // Temporary registers for the write barrier.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001169 if (field_type == Primitive::kPrimNot) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001170 locations->AddTemp(Location::RequiresRegister());
1171 // Ensure the card is in a byte register.
1172 locations->AddTemp(X86CpuLocation(ECX));
1173 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001174}
1175
1176void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1177 LocationSummary* locations = instruction->GetLocations();
1178 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1179 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001180 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001181
1182 switch (field_type) {
1183 case Primitive::kPrimBoolean:
1184 case Primitive::kPrimByte: {
1185 ByteRegister value = locations->InAt(1).AsX86().AsByteRegister();
1186 __ movb(Address(obj, offset), value);
1187 break;
1188 }
1189
1190 case Primitive::kPrimShort:
1191 case Primitive::kPrimChar: {
1192 Register value = locations->InAt(1).AsX86().AsCpuRegister();
1193 __ movw(Address(obj, offset), value);
1194 break;
1195 }
1196
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001197 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001198 case Primitive::kPrimNot: {
1199 Register value = locations->InAt(1).AsX86().AsCpuRegister();
1200 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001201
1202 if (field_type == Primitive::kPrimNot) {
1203 Register temp = locations->GetTemp(0).AsX86().AsCpuRegister();
1204 Register card = locations->GetTemp(1).AsX86().AsCpuRegister();
1205 codegen_->MarkGCCard(temp, card, obj, value);
1206 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001207 break;
1208 }
1209
1210 case Primitive::kPrimLong: {
1211 X86ManagedRegister value = locations->InAt(1).AsX86();
1212 __ movl(Address(obj, offset), value.AsRegisterPairLow());
1213 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh());
1214 break;
1215 }
1216
1217 case Primitive::kPrimFloat:
1218 case Primitive::kPrimDouble:
1219 LOG(FATAL) << "Unimplemented register type " << field_type;
1220
1221 case Primitive::kPrimVoid:
1222 LOG(FATAL) << "Unreachable type " << field_type;
1223 }
1224}
1225
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001226void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
1227 Label is_null;
1228 __ testl(value, value);
1229 __ j(kEqual, &is_null);
1230 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
1231 __ movl(temp, object);
1232 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
1233 __ movb(Address(temp, card, TIMES_1, 0),
1234 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
1235 __ Bind(&is_null);
1236}
1237
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001238void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001239 LocationSummary* locations =
1240 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001241 locations->SetInAt(0, Location::RequiresRegister());
1242 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001243}
1244
1245void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1246 LocationSummary* locations = instruction->GetLocations();
1247 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1248 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
1249
1250 switch (instruction->GetType()) {
1251 case Primitive::kPrimBoolean: {
1252 Register out = locations->Out().AsX86().AsCpuRegister();
1253 __ movzxb(out, Address(obj, offset));
1254 break;
1255 }
1256
1257 case Primitive::kPrimByte: {
1258 Register out = locations->Out().AsX86().AsCpuRegister();
1259 __ movsxb(out, Address(obj, offset));
1260 break;
1261 }
1262
1263 case Primitive::kPrimShort: {
1264 Register out = locations->Out().AsX86().AsCpuRegister();
1265 __ movsxw(out, Address(obj, offset));
1266 break;
1267 }
1268
1269 case Primitive::kPrimChar: {
1270 Register out = locations->Out().AsX86().AsCpuRegister();
1271 __ movzxw(out, Address(obj, offset));
1272 break;
1273 }
1274
1275 case Primitive::kPrimInt:
1276 case Primitive::kPrimNot: {
1277 Register out = locations->Out().AsX86().AsCpuRegister();
1278 __ movl(out, Address(obj, offset));
1279 break;
1280 }
1281
1282 case Primitive::kPrimLong: {
1283 // TODO: support volatile.
1284 X86ManagedRegister out = locations->Out().AsX86();
1285 __ movl(out.AsRegisterPairLow(), Address(obj, offset));
1286 __ movl(out.AsRegisterPairHigh(), Address(obj, kX86WordSize + offset));
1287 break;
1288 }
1289
1290 case Primitive::kPrimFloat:
1291 case Primitive::kPrimDouble:
1292 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1293
1294 case Primitive::kPrimVoid:
1295 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1296 }
1297}
1298
1299void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001300 LocationSummary* locations =
1301 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001302 locations->SetInAt(0, Location::Any());
1303 // TODO: Have a normalization phase that makes this instruction never used.
1304 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001305}
1306
1307void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001308 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001309 codegen_->AddSlowPath(slow_path);
1310
1311 LocationSummary* locations = instruction->GetLocations();
1312 Location obj = locations->InAt(0);
1313 DCHECK(obj.Equals(locations->Out()));
1314
1315 if (obj.IsRegister()) {
1316 __ cmpl(obj.AsX86().AsCpuRegister(), Immediate(0));
1317 } else {
1318 DCHECK(locations->InAt(0).IsStackSlot());
1319 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
1320 }
1321 __ j(kEqual, slow_path->GetEntryLabel());
1322}
1323
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001324void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001325 LocationSummary* locations =
1326 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001327 locations->SetInAt(0, Location::RequiresRegister());
1328 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1329 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001330}
1331
1332void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
1333 LocationSummary* locations = instruction->GetLocations();
1334 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1335 Location index = locations->InAt(1);
1336
1337 switch (instruction->GetType()) {
1338 case Primitive::kPrimBoolean: {
1339 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1340 Register out = locations->Out().AsX86().AsCpuRegister();
1341 if (index.IsConstant()) {
1342 __ movzxb(out, Address(obj,
1343 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1344 } else {
1345 __ movzxb(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_1, data_offset));
1346 }
1347 break;
1348 }
1349
1350 case Primitive::kPrimByte: {
1351 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1352 Register out = locations->Out().AsX86().AsCpuRegister();
1353 if (index.IsConstant()) {
1354 __ movsxb(out, Address(obj,
1355 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1356 } else {
1357 __ movsxb(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_1, data_offset));
1358 }
1359 break;
1360 }
1361
1362 case Primitive::kPrimShort: {
1363 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1364 Register out = locations->Out().AsX86().AsCpuRegister();
1365 if (index.IsConstant()) {
1366 __ movsxw(out, Address(obj,
1367 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1368 } else {
1369 __ movsxw(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_2, data_offset));
1370 }
1371 break;
1372 }
1373
1374 case Primitive::kPrimChar: {
1375 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1376 Register out = locations->Out().AsX86().AsCpuRegister();
1377 if (index.IsConstant()) {
1378 __ movzxw(out, Address(obj,
1379 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1380 } else {
1381 __ movzxw(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_2, data_offset));
1382 }
1383 break;
1384 }
1385
1386 case Primitive::kPrimInt:
1387 case Primitive::kPrimNot: {
1388 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1389 Register out = locations->Out().AsX86().AsCpuRegister();
1390 if (index.IsConstant()) {
1391 __ movl(out, Address(obj,
1392 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1393 } else {
1394 __ movl(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_4, data_offset));
1395 }
1396 break;
1397 }
1398
1399 case Primitive::kPrimLong: {
1400 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1401 X86ManagedRegister out = locations->Out().AsX86();
1402 if (index.IsConstant()) {
1403 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1404 __ movl(out.AsRegisterPairLow(), Address(obj, offset));
1405 __ movl(out.AsRegisterPairHigh(), Address(obj, offset + kX86WordSize));
1406 } else {
1407 __ movl(out.AsRegisterPairLow(),
1408 Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset));
1409 __ movl(out.AsRegisterPairHigh(),
1410 Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset + kX86WordSize));
1411 }
1412 break;
1413 }
1414
1415 case Primitive::kPrimFloat:
1416 case Primitive::kPrimDouble:
1417 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1418
1419 case Primitive::kPrimVoid:
1420 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1421 }
1422}
1423
1424void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001425 Primitive::Type value_type = instruction->GetComponentType();
1426 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1427 instruction,
1428 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
1429
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001430 if (value_type == Primitive::kPrimNot) {
1431 InvokeRuntimeCallingConvention calling_convention;
1432 locations->SetInAt(0, X86CpuLocation(calling_convention.GetRegisterAt(0)));
1433 locations->SetInAt(1, X86CpuLocation(calling_convention.GetRegisterAt(1)));
1434 locations->SetInAt(2, X86CpuLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001435 } else {
1436 locations->SetInAt(0, Location::RequiresRegister());
1437 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1438 if (value_type == Primitive::kPrimBoolean || value_type == Primitive::kPrimByte) {
1439 // Ensure the value is in a byte register.
1440 locations->SetInAt(2, X86CpuLocation(EAX));
1441 } else {
1442 locations->SetInAt(2, Location::RequiresRegister());
1443 }
1444 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001445}
1446
1447void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
1448 LocationSummary* locations = instruction->GetLocations();
1449 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1450 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001451 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001452
1453 switch (value_type) {
1454 case Primitive::kPrimBoolean:
1455 case Primitive::kPrimByte: {
1456 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1457 ByteRegister value = locations->InAt(2).AsX86().AsByteRegister();
1458 if (index.IsConstant()) {
1459 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1460 __ movb(Address(obj, offset), value);
1461 } else {
1462 __ movb(Address(obj, index.AsX86().AsCpuRegister(), TIMES_1, data_offset), value);
1463 }
1464 break;
1465 }
1466
1467 case Primitive::kPrimShort:
1468 case Primitive::kPrimChar: {
1469 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1470 Register value = locations->InAt(2).AsX86().AsCpuRegister();
1471 if (index.IsConstant()) {
1472 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1473 __ movw(Address(obj, offset), value);
1474 } else {
1475 __ movw(Address(obj, index.AsX86().AsCpuRegister(), TIMES_2, data_offset), value);
1476 }
1477 break;
1478 }
1479
1480 case Primitive::kPrimInt: {
1481 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1482 Register value = locations->InAt(2).AsX86().AsCpuRegister();
1483 if (index.IsConstant()) {
1484 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1485 __ movl(Address(obj, offset), value);
1486 } else {
1487 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_4, data_offset), value);
1488 }
1489 break;
1490 }
1491
1492 case Primitive::kPrimNot: {
1493 DCHECK(!codegen_->IsLeafMethod());
1494 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001495 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001496 break;
1497 }
1498
1499 case Primitive::kPrimLong: {
1500 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1501 X86ManagedRegister value = locations->InAt(2).AsX86();
1502 if (index.IsConstant()) {
1503 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1504 __ movl(Address(obj, offset), value.AsRegisterPairLow());
1505 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh());
1506 } else {
1507 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset),
1508 value.AsRegisterPairLow());
1509 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset + kX86WordSize),
1510 value.AsRegisterPairHigh());
1511 }
1512 break;
1513 }
1514
1515 case Primitive::kPrimFloat:
1516 case Primitive::kPrimDouble:
1517 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1518
1519 case Primitive::kPrimVoid:
1520 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1521 }
1522}
1523
1524void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
1525 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1526 locations->SetInAt(0, Location::RequiresRegister());
1527 locations->SetOut(Location::RequiresRegister());
1528 instruction->SetLocations(locations);
1529}
1530
1531void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
1532 LocationSummary* locations = instruction->GetLocations();
1533 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1534 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1535 Register out = locations->Out().AsX86().AsCpuRegister();
1536 __ movl(out, Address(obj, offset));
1537}
1538
1539void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001540 LocationSummary* locations =
1541 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001542 locations->SetInAt(0, Location::RequiresRegister());
1543 locations->SetInAt(1, Location::RequiresRegister());
1544 // TODO: Have a normalization phase that makes this instruction never used.
1545 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001546}
1547
1548void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
1549 LocationSummary* locations = instruction->GetLocations();
1550 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01001551 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001552 codegen_->AddSlowPath(slow_path);
1553
1554 Register index = locations->InAt(0).AsX86().AsCpuRegister();
1555 Register length = locations->InAt(1).AsX86().AsCpuRegister();
1556
1557 __ cmpl(index, length);
1558 __ j(kAboveEqual, slow_path->GetEntryLabel());
1559}
1560
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001561void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
1562 temp->SetLocations(nullptr);
1563}
1564
1565void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
1566 // Nothing to do, this is driven by the code generator.
1567}
1568
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001569void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001570 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001571}
1572
1573void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001574 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
1575}
1576
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001577void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
1578 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
1579}
1580
1581void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001582 HBasicBlock* block = instruction->GetBlock();
1583 if (block->GetLoopInformation() != nullptr) {
1584 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
1585 // The back edge will generate the suspend check.
1586 return;
1587 }
1588 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
1589 // The goto will generate the suspend check.
1590 return;
1591 }
1592 GenerateSuspendCheck(instruction, nullptr);
1593}
1594
1595void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
1596 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001597 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001598 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001599 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001600 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001601 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001602 if (successor == nullptr) {
1603 __ j(kNotEqual, slow_path->GetEntryLabel());
1604 __ Bind(slow_path->GetReturnLabel());
1605 } else {
1606 __ j(kEqual, codegen_->GetLabelOf(successor));
1607 __ jmp(slow_path->GetEntryLabel());
1608 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001609}
1610
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001611X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
1612 return codegen_->GetAssembler();
1613}
1614
1615void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
1616 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001617 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001618 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1619 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
1620 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
1621}
1622
1623void ParallelMoveResolverX86::EmitMove(size_t index) {
1624 MoveOperands* move = moves_.Get(index);
1625 Location source = move->GetSource();
1626 Location destination = move->GetDestination();
1627
1628 if (source.IsRegister()) {
1629 if (destination.IsRegister()) {
1630 __ movl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister());
1631 } else {
1632 DCHECK(destination.IsStackSlot());
1633 __ movl(Address(ESP, destination.GetStackIndex()), source.AsX86().AsCpuRegister());
1634 }
1635 } else if (source.IsStackSlot()) {
1636 if (destination.IsRegister()) {
1637 __ movl(destination.AsX86().AsCpuRegister(), Address(ESP, source.GetStackIndex()));
1638 } else {
1639 DCHECK(destination.IsStackSlot());
1640 MoveMemoryToMemory(destination.GetStackIndex(),
1641 source.GetStackIndex());
1642 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001643 } else if (source.IsConstant()) {
1644 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
1645 Immediate imm(instruction->AsIntConstant()->GetValue());
1646 if (destination.IsRegister()) {
1647 __ movl(destination.AsX86().AsCpuRegister(), imm);
1648 } else {
1649 __ movl(Address(ESP, destination.GetStackIndex()), imm);
1650 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001651 } else {
1652 LOG(FATAL) << "Unimplemented";
1653 }
1654}
1655
1656void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001657 Register suggested_scratch = reg == EAX ? EBX : EAX;
1658 ScratchRegisterScope ensure_scratch(
1659 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
1660
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001661 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1662 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
1663 __ movl(Address(ESP, mem + stack_offset), reg);
1664 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
1665}
1666
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001667void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
1668 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001669 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
1670
1671 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001672 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001673 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
1674
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001675 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
1676 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
1677 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
1678 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
1679 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
1680 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
1681}
1682
1683void ParallelMoveResolverX86::EmitSwap(size_t index) {
1684 MoveOperands* move = moves_.Get(index);
1685 Location source = move->GetSource();
1686 Location destination = move->GetDestination();
1687
1688 if (source.IsRegister() && destination.IsRegister()) {
1689 __ xchgl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister());
1690 } else if (source.IsRegister() && destination.IsStackSlot()) {
1691 Exchange(source.AsX86().AsCpuRegister(), destination.GetStackIndex());
1692 } else if (source.IsStackSlot() && destination.IsRegister()) {
1693 Exchange(destination.AsX86().AsCpuRegister(), source.GetStackIndex());
1694 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
1695 Exchange(destination.GetStackIndex(), source.GetStackIndex());
1696 } else {
1697 LOG(FATAL) << "Unimplemented";
1698 }
1699}
1700
1701void ParallelMoveResolverX86::SpillScratch(int reg) {
1702 __ pushl(static_cast<Register>(reg));
1703}
1704
1705void ParallelMoveResolverX86::RestoreScratch(int reg) {
1706 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001707}
1708
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001709} // namespace x86
1710} // namespace art