blob: 6791003bf8e425b13e57d331513165bd9acd0dc5 [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()) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +0100566 locations->SetInAt(0, Location::Any(), Location::kDiesAtEntry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100567 }
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 Geoffray9ae0daa2014-09-30 22:40:23 +0100653 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
654 locations->SetInAt(1, Location::Any(), Location::kDiesAtEntry);
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 Geoffray9ae0daa2014-09-30 22:40:23 +01001097 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
1098 locations->SetInAt(1, Location::Any(), Location::kDiesAtEntry);
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 Geoffray9ae0daa2014-09-30 22:40:23 +01001162 bool is_object_type = field_type == Primitive::kPrimNot;
1163 bool dies_at_entry = !is_object_type;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001164 if (field_type == Primitive::kPrimBoolean || field_type == Primitive::kPrimByte) {
1165 // Ensure the value is in a byte register.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001166 locations->SetInAt(1, X86CpuLocation(EAX), dies_at_entry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001167 } else {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001168 locations->SetInAt(1, Location::RequiresRegister(), dies_at_entry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001169 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001170 // Temporary registers for the write barrier.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001171 if (is_object_type) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001172 locations->AddTemp(Location::RequiresRegister());
1173 // Ensure the card is in a byte register.
1174 locations->AddTemp(X86CpuLocation(ECX));
1175 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001176}
1177
1178void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1179 LocationSummary* locations = instruction->GetLocations();
1180 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1181 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001182 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001183
1184 switch (field_type) {
1185 case Primitive::kPrimBoolean:
1186 case Primitive::kPrimByte: {
1187 ByteRegister value = locations->InAt(1).AsX86().AsByteRegister();
1188 __ movb(Address(obj, offset), value);
1189 break;
1190 }
1191
1192 case Primitive::kPrimShort:
1193 case Primitive::kPrimChar: {
1194 Register value = locations->InAt(1).AsX86().AsCpuRegister();
1195 __ movw(Address(obj, offset), value);
1196 break;
1197 }
1198
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001199 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001200 case Primitive::kPrimNot: {
1201 Register value = locations->InAt(1).AsX86().AsCpuRegister();
1202 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001203
1204 if (field_type == Primitive::kPrimNot) {
1205 Register temp = locations->GetTemp(0).AsX86().AsCpuRegister();
1206 Register card = locations->GetTemp(1).AsX86().AsCpuRegister();
1207 codegen_->MarkGCCard(temp, card, obj, value);
1208 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001209 break;
1210 }
1211
1212 case Primitive::kPrimLong: {
1213 X86ManagedRegister value = locations->InAt(1).AsX86();
1214 __ movl(Address(obj, offset), value.AsRegisterPairLow());
1215 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh());
1216 break;
1217 }
1218
1219 case Primitive::kPrimFloat:
1220 case Primitive::kPrimDouble:
1221 LOG(FATAL) << "Unimplemented register type " << field_type;
1222
1223 case Primitive::kPrimVoid:
1224 LOG(FATAL) << "Unreachable type " << field_type;
1225 }
1226}
1227
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001228void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
1229 Label is_null;
1230 __ testl(value, value);
1231 __ j(kEqual, &is_null);
1232 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
1233 __ movl(temp, object);
1234 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
1235 __ movb(Address(temp, card, TIMES_1, 0),
1236 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
1237 __ Bind(&is_null);
1238}
1239
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001240void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001241 LocationSummary* locations =
1242 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001243 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001244 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001245}
1246
1247void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1248 LocationSummary* locations = instruction->GetLocations();
1249 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1250 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
1251
1252 switch (instruction->GetType()) {
1253 case Primitive::kPrimBoolean: {
1254 Register out = locations->Out().AsX86().AsCpuRegister();
1255 __ movzxb(out, Address(obj, offset));
1256 break;
1257 }
1258
1259 case Primitive::kPrimByte: {
1260 Register out = locations->Out().AsX86().AsCpuRegister();
1261 __ movsxb(out, Address(obj, offset));
1262 break;
1263 }
1264
1265 case Primitive::kPrimShort: {
1266 Register out = locations->Out().AsX86().AsCpuRegister();
1267 __ movsxw(out, Address(obj, offset));
1268 break;
1269 }
1270
1271 case Primitive::kPrimChar: {
1272 Register out = locations->Out().AsX86().AsCpuRegister();
1273 __ movzxw(out, Address(obj, offset));
1274 break;
1275 }
1276
1277 case Primitive::kPrimInt:
1278 case Primitive::kPrimNot: {
1279 Register out = locations->Out().AsX86().AsCpuRegister();
1280 __ movl(out, Address(obj, offset));
1281 break;
1282 }
1283
1284 case Primitive::kPrimLong: {
1285 // TODO: support volatile.
1286 X86ManagedRegister out = locations->Out().AsX86();
1287 __ movl(out.AsRegisterPairLow(), Address(obj, offset));
1288 __ movl(out.AsRegisterPairHigh(), Address(obj, kX86WordSize + offset));
1289 break;
1290 }
1291
1292 case Primitive::kPrimFloat:
1293 case Primitive::kPrimDouble:
1294 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1295
1296 case Primitive::kPrimVoid:
1297 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1298 }
1299}
1300
1301void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001302 LocationSummary* locations =
1303 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001304 locations->SetInAt(0, Location::Any());
1305 // TODO: Have a normalization phase that makes this instruction never used.
1306 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001307}
1308
1309void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001310 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001311 codegen_->AddSlowPath(slow_path);
1312
1313 LocationSummary* locations = instruction->GetLocations();
1314 Location obj = locations->InAt(0);
1315 DCHECK(obj.Equals(locations->Out()));
1316
1317 if (obj.IsRegister()) {
1318 __ cmpl(obj.AsX86().AsCpuRegister(), Immediate(0));
1319 } else {
1320 DCHECK(locations->InAt(0).IsStackSlot());
1321 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
1322 }
1323 __ j(kEqual, slow_path->GetEntryLabel());
1324}
1325
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001326void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001327 LocationSummary* locations =
1328 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001329 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
1330 locations->SetInAt(
1331 1, Location::RegisterOrConstant(instruction->InputAt(1)), Location::kDiesAtEntry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001332 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001333}
1334
1335void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
1336 LocationSummary* locations = instruction->GetLocations();
1337 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1338 Location index = locations->InAt(1);
1339
1340 switch (instruction->GetType()) {
1341 case Primitive::kPrimBoolean: {
1342 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1343 Register out = locations->Out().AsX86().AsCpuRegister();
1344 if (index.IsConstant()) {
1345 __ movzxb(out, Address(obj,
1346 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1347 } else {
1348 __ movzxb(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_1, data_offset));
1349 }
1350 break;
1351 }
1352
1353 case Primitive::kPrimByte: {
1354 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1355 Register out = locations->Out().AsX86().AsCpuRegister();
1356 if (index.IsConstant()) {
1357 __ movsxb(out, Address(obj,
1358 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1359 } else {
1360 __ movsxb(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_1, data_offset));
1361 }
1362 break;
1363 }
1364
1365 case Primitive::kPrimShort: {
1366 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1367 Register out = locations->Out().AsX86().AsCpuRegister();
1368 if (index.IsConstant()) {
1369 __ movsxw(out, Address(obj,
1370 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1371 } else {
1372 __ movsxw(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_2, data_offset));
1373 }
1374 break;
1375 }
1376
1377 case Primitive::kPrimChar: {
1378 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1379 Register out = locations->Out().AsX86().AsCpuRegister();
1380 if (index.IsConstant()) {
1381 __ movzxw(out, Address(obj,
1382 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1383 } else {
1384 __ movzxw(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_2, data_offset));
1385 }
1386 break;
1387 }
1388
1389 case Primitive::kPrimInt:
1390 case Primitive::kPrimNot: {
1391 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1392 Register out = locations->Out().AsX86().AsCpuRegister();
1393 if (index.IsConstant()) {
1394 __ movl(out, Address(obj,
1395 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1396 } else {
1397 __ movl(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_4, data_offset));
1398 }
1399 break;
1400 }
1401
1402 case Primitive::kPrimLong: {
1403 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1404 X86ManagedRegister out = locations->Out().AsX86();
1405 if (index.IsConstant()) {
1406 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1407 __ movl(out.AsRegisterPairLow(), Address(obj, offset));
1408 __ movl(out.AsRegisterPairHigh(), Address(obj, offset + kX86WordSize));
1409 } else {
1410 __ movl(out.AsRegisterPairLow(),
1411 Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset));
1412 __ movl(out.AsRegisterPairHigh(),
1413 Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset + kX86WordSize));
1414 }
1415 break;
1416 }
1417
1418 case Primitive::kPrimFloat:
1419 case Primitive::kPrimDouble:
1420 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1421
1422 case Primitive::kPrimVoid:
1423 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1424 }
1425}
1426
1427void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001428 Primitive::Type value_type = instruction->GetComponentType();
1429 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1430 instruction,
1431 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
1432
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001433 if (value_type == Primitive::kPrimNot) {
1434 InvokeRuntimeCallingConvention calling_convention;
1435 locations->SetInAt(0, X86CpuLocation(calling_convention.GetRegisterAt(0)));
1436 locations->SetInAt(1, X86CpuLocation(calling_convention.GetRegisterAt(1)));
1437 locations->SetInAt(2, X86CpuLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001438 } else {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001439 // We need the inputs to be different than the output in case of long operation.
1440 bool dies_at_entry = value_type != Primitive::kPrimLong;
1441 locations->SetInAt(0, Location::RequiresRegister(), dies_at_entry);
1442 locations->SetInAt(
1443 1, Location::RegisterOrConstant(instruction->InputAt(1)), dies_at_entry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001444 if (value_type == Primitive::kPrimBoolean || value_type == Primitive::kPrimByte) {
1445 // Ensure the value is in a byte register.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001446 locations->SetInAt(2, X86CpuLocation(EAX), dies_at_entry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001447 } else {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001448 locations->SetInAt(2, Location::RequiresRegister(), dies_at_entry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001449 }
1450 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001451}
1452
1453void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
1454 LocationSummary* locations = instruction->GetLocations();
1455 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1456 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001457 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001458
1459 switch (value_type) {
1460 case Primitive::kPrimBoolean:
1461 case Primitive::kPrimByte: {
1462 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1463 ByteRegister value = locations->InAt(2).AsX86().AsByteRegister();
1464 if (index.IsConstant()) {
1465 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1466 __ movb(Address(obj, offset), value);
1467 } else {
1468 __ movb(Address(obj, index.AsX86().AsCpuRegister(), TIMES_1, data_offset), value);
1469 }
1470 break;
1471 }
1472
1473 case Primitive::kPrimShort:
1474 case Primitive::kPrimChar: {
1475 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1476 Register value = locations->InAt(2).AsX86().AsCpuRegister();
1477 if (index.IsConstant()) {
1478 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1479 __ movw(Address(obj, offset), value);
1480 } else {
1481 __ movw(Address(obj, index.AsX86().AsCpuRegister(), TIMES_2, data_offset), value);
1482 }
1483 break;
1484 }
1485
1486 case Primitive::kPrimInt: {
1487 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1488 Register value = locations->InAt(2).AsX86().AsCpuRegister();
1489 if (index.IsConstant()) {
1490 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1491 __ movl(Address(obj, offset), value);
1492 } else {
1493 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_4, data_offset), value);
1494 }
1495 break;
1496 }
1497
1498 case Primitive::kPrimNot: {
1499 DCHECK(!codegen_->IsLeafMethod());
1500 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001501 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001502 break;
1503 }
1504
1505 case Primitive::kPrimLong: {
1506 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1507 X86ManagedRegister value = locations->InAt(2).AsX86();
1508 if (index.IsConstant()) {
1509 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1510 __ movl(Address(obj, offset), value.AsRegisterPairLow());
1511 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh());
1512 } else {
1513 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset),
1514 value.AsRegisterPairLow());
1515 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset + kX86WordSize),
1516 value.AsRegisterPairHigh());
1517 }
1518 break;
1519 }
1520
1521 case Primitive::kPrimFloat:
1522 case Primitive::kPrimDouble:
1523 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1524
1525 case Primitive::kPrimVoid:
1526 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1527 }
1528}
1529
1530void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
1531 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001532 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001533 locations->SetOut(Location::RequiresRegister());
1534 instruction->SetLocations(locations);
1535}
1536
1537void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
1538 LocationSummary* locations = instruction->GetLocations();
1539 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1540 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1541 Register out = locations->Out().AsX86().AsCpuRegister();
1542 __ movl(out, Address(obj, offset));
1543}
1544
1545void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001546 LocationSummary* locations =
1547 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001548 locations->SetInAt(0, Location::RequiresRegister());
1549 locations->SetInAt(1, Location::RequiresRegister());
1550 // TODO: Have a normalization phase that makes this instruction never used.
1551 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001552}
1553
1554void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
1555 LocationSummary* locations = instruction->GetLocations();
1556 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01001557 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001558 codegen_->AddSlowPath(slow_path);
1559
1560 Register index = locations->InAt(0).AsX86().AsCpuRegister();
1561 Register length = locations->InAt(1).AsX86().AsCpuRegister();
1562
1563 __ cmpl(index, length);
1564 __ j(kAboveEqual, slow_path->GetEntryLabel());
1565}
1566
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001567void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
1568 temp->SetLocations(nullptr);
1569}
1570
1571void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
1572 // Nothing to do, this is driven by the code generator.
1573}
1574
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001575void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001576 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001577}
1578
1579void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001580 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
1581}
1582
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001583void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
1584 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
1585}
1586
1587void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001588 HBasicBlock* block = instruction->GetBlock();
1589 if (block->GetLoopInformation() != nullptr) {
1590 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
1591 // The back edge will generate the suspend check.
1592 return;
1593 }
1594 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
1595 // The goto will generate the suspend check.
1596 return;
1597 }
1598 GenerateSuspendCheck(instruction, nullptr);
1599}
1600
1601void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
1602 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001603 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001604 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001605 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001606 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001607 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001608 if (successor == nullptr) {
1609 __ j(kNotEqual, slow_path->GetEntryLabel());
1610 __ Bind(slow_path->GetReturnLabel());
1611 } else {
1612 __ j(kEqual, codegen_->GetLabelOf(successor));
1613 __ jmp(slow_path->GetEntryLabel());
1614 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001615}
1616
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001617X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
1618 return codegen_->GetAssembler();
1619}
1620
1621void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
1622 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001623 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001624 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1625 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
1626 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
1627}
1628
1629void ParallelMoveResolverX86::EmitMove(size_t index) {
1630 MoveOperands* move = moves_.Get(index);
1631 Location source = move->GetSource();
1632 Location destination = move->GetDestination();
1633
1634 if (source.IsRegister()) {
1635 if (destination.IsRegister()) {
1636 __ movl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister());
1637 } else {
1638 DCHECK(destination.IsStackSlot());
1639 __ movl(Address(ESP, destination.GetStackIndex()), source.AsX86().AsCpuRegister());
1640 }
1641 } else if (source.IsStackSlot()) {
1642 if (destination.IsRegister()) {
1643 __ movl(destination.AsX86().AsCpuRegister(), Address(ESP, source.GetStackIndex()));
1644 } else {
1645 DCHECK(destination.IsStackSlot());
1646 MoveMemoryToMemory(destination.GetStackIndex(),
1647 source.GetStackIndex());
1648 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001649 } else if (source.IsConstant()) {
1650 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
1651 Immediate imm(instruction->AsIntConstant()->GetValue());
1652 if (destination.IsRegister()) {
1653 __ movl(destination.AsX86().AsCpuRegister(), imm);
1654 } else {
1655 __ movl(Address(ESP, destination.GetStackIndex()), imm);
1656 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001657 } else {
1658 LOG(FATAL) << "Unimplemented";
1659 }
1660}
1661
1662void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001663 Register suggested_scratch = reg == EAX ? EBX : EAX;
1664 ScratchRegisterScope ensure_scratch(
1665 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
1666
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001667 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1668 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
1669 __ movl(Address(ESP, mem + stack_offset), reg);
1670 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
1671}
1672
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001673void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
1674 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001675 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
1676
1677 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001678 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001679 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
1680
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001681 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
1682 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
1683 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
1684 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
1685 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
1686 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
1687}
1688
1689void ParallelMoveResolverX86::EmitSwap(size_t index) {
1690 MoveOperands* move = moves_.Get(index);
1691 Location source = move->GetSource();
1692 Location destination = move->GetDestination();
1693
1694 if (source.IsRegister() && destination.IsRegister()) {
1695 __ xchgl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister());
1696 } else if (source.IsRegister() && destination.IsStackSlot()) {
1697 Exchange(source.AsX86().AsCpuRegister(), destination.GetStackIndex());
1698 } else if (source.IsStackSlot() && destination.IsRegister()) {
1699 Exchange(destination.AsX86().AsCpuRegister(), source.GetStackIndex());
1700 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
1701 Exchange(destination.GetStackIndex(), source.GetStackIndex());
1702 } else {
1703 LOG(FATAL) << "Unimplemented";
1704 }
1705}
1706
1707void ParallelMoveResolverX86::SpillScratch(int reg) {
1708 __ pushl(static_cast<Register>(reg));
1709}
1710
1711void ParallelMoveResolverX86::RestoreScratch(int reg) {
1712 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001713}
1714
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001715} // namespace x86
1716} // namespace art