blob: 9fb4cc236f2ddf118792980d63e86a72a6968306 [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;
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001163 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
1164 || (field_type == Primitive::kPrimByte);
1165 // The register allocator does not support multiple
1166 // inputs that die at entry with one in a specific register.
1167 bool dies_at_entry = !is_object_type && !is_byte_type;
1168 if (is_byte_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001169 // Ensure the value is in a byte register.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001170 locations->SetInAt(1, X86CpuLocation(EAX), dies_at_entry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001171 } else {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001172 locations->SetInAt(1, Location::RequiresRegister(), dies_at_entry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001173 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001174 // Temporary registers for the write barrier.
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001175 if (is_object_type) {
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01001176 locations->AddTemp(Location::RequiresRegister());
1177 // Ensure the card is in a byte register.
1178 locations->AddTemp(X86CpuLocation(ECX));
1179 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001180}
1181
1182void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
1183 LocationSummary* locations = instruction->GetLocations();
1184 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1185 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
Nicolas Geoffray39468442014-09-02 15:17:15 +01001186 Primitive::Type field_type = instruction->GetFieldType();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001187
1188 switch (field_type) {
1189 case Primitive::kPrimBoolean:
1190 case Primitive::kPrimByte: {
1191 ByteRegister value = locations->InAt(1).AsX86().AsByteRegister();
1192 __ movb(Address(obj, offset), value);
1193 break;
1194 }
1195
1196 case Primitive::kPrimShort:
1197 case Primitive::kPrimChar: {
1198 Register value = locations->InAt(1).AsX86().AsCpuRegister();
1199 __ movw(Address(obj, offset), value);
1200 break;
1201 }
1202
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001203 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001204 case Primitive::kPrimNot: {
1205 Register value = locations->InAt(1).AsX86().AsCpuRegister();
1206 __ movl(Address(obj, offset), value);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001207
1208 if (field_type == Primitive::kPrimNot) {
1209 Register temp = locations->GetTemp(0).AsX86().AsCpuRegister();
1210 Register card = locations->GetTemp(1).AsX86().AsCpuRegister();
1211 codegen_->MarkGCCard(temp, card, obj, value);
1212 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001213 break;
1214 }
1215
1216 case Primitive::kPrimLong: {
1217 X86ManagedRegister value = locations->InAt(1).AsX86();
1218 __ movl(Address(obj, offset), value.AsRegisterPairLow());
1219 __ movl(Address(obj, kX86WordSize + offset), value.AsRegisterPairHigh());
1220 break;
1221 }
1222
1223 case Primitive::kPrimFloat:
1224 case Primitive::kPrimDouble:
1225 LOG(FATAL) << "Unimplemented register type " << field_type;
1226
1227 case Primitive::kPrimVoid:
1228 LOG(FATAL) << "Unreachable type " << field_type;
1229 }
1230}
1231
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001232void CodeGeneratorX86::MarkGCCard(Register temp, Register card, Register object, Register value) {
1233 Label is_null;
1234 __ testl(value, value);
1235 __ j(kEqual, &is_null);
1236 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
1237 __ movl(temp, object);
1238 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
1239 __ movb(Address(temp, card, TIMES_1, 0),
1240 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
1241 __ Bind(&is_null);
1242}
1243
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001244void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001245 LocationSummary* locations =
1246 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001247 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001248 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001249}
1250
1251void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
1252 LocationSummary* locations = instruction->GetLocations();
1253 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1254 uint32_t offset = instruction->GetFieldOffset().Uint32Value();
1255
1256 switch (instruction->GetType()) {
1257 case Primitive::kPrimBoolean: {
1258 Register out = locations->Out().AsX86().AsCpuRegister();
1259 __ movzxb(out, Address(obj, offset));
1260 break;
1261 }
1262
1263 case Primitive::kPrimByte: {
1264 Register out = locations->Out().AsX86().AsCpuRegister();
1265 __ movsxb(out, Address(obj, offset));
1266 break;
1267 }
1268
1269 case Primitive::kPrimShort: {
1270 Register out = locations->Out().AsX86().AsCpuRegister();
1271 __ movsxw(out, Address(obj, offset));
1272 break;
1273 }
1274
1275 case Primitive::kPrimChar: {
1276 Register out = locations->Out().AsX86().AsCpuRegister();
1277 __ movzxw(out, Address(obj, offset));
1278 break;
1279 }
1280
1281 case Primitive::kPrimInt:
1282 case Primitive::kPrimNot: {
1283 Register out = locations->Out().AsX86().AsCpuRegister();
1284 __ movl(out, Address(obj, offset));
1285 break;
1286 }
1287
1288 case Primitive::kPrimLong: {
1289 // TODO: support volatile.
1290 X86ManagedRegister out = locations->Out().AsX86();
1291 __ movl(out.AsRegisterPairLow(), Address(obj, offset));
1292 __ movl(out.AsRegisterPairHigh(), Address(obj, kX86WordSize + offset));
1293 break;
1294 }
1295
1296 case Primitive::kPrimFloat:
1297 case Primitive::kPrimDouble:
1298 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1299
1300 case Primitive::kPrimVoid:
1301 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1302 }
1303}
1304
1305void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001306 LocationSummary* locations =
1307 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001308 locations->SetInAt(0, Location::Any());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001309 if (instruction->HasUses()) {
1310 locations->SetOut(Location::SameAsFirstInput());
1311 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001312}
1313
1314void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001315 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001316 codegen_->AddSlowPath(slow_path);
1317
1318 LocationSummary* locations = instruction->GetLocations();
1319 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001320
1321 if (obj.IsRegister()) {
1322 __ cmpl(obj.AsX86().AsCpuRegister(), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001323 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001324 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001325 } else {
1326 DCHECK(obj.IsConstant()) << obj;
1327 DCHECK_EQ(obj.GetConstant()->AsIntConstant()->GetValue(), 0);
1328 __ jmp(slow_path->GetEntryLabel());
1329 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001330 }
1331 __ j(kEqual, slow_path->GetEntryLabel());
1332}
1333
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001334void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001335 LocationSummary* locations =
1336 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001337 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
1338 locations->SetInAt(
1339 1, Location::RegisterOrConstant(instruction->InputAt(1)), Location::kDiesAtEntry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001340 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001341}
1342
1343void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
1344 LocationSummary* locations = instruction->GetLocations();
1345 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1346 Location index = locations->InAt(1);
1347
1348 switch (instruction->GetType()) {
1349 case Primitive::kPrimBoolean: {
1350 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1351 Register out = locations->Out().AsX86().AsCpuRegister();
1352 if (index.IsConstant()) {
1353 __ movzxb(out, Address(obj,
1354 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1355 } else {
1356 __ movzxb(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_1, data_offset));
1357 }
1358 break;
1359 }
1360
1361 case Primitive::kPrimByte: {
1362 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1363 Register out = locations->Out().AsX86().AsCpuRegister();
1364 if (index.IsConstant()) {
1365 __ movsxb(out, Address(obj,
1366 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
1367 } else {
1368 __ movsxb(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_1, data_offset));
1369 }
1370 break;
1371 }
1372
1373 case Primitive::kPrimShort: {
1374 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1375 Register out = locations->Out().AsX86().AsCpuRegister();
1376 if (index.IsConstant()) {
1377 __ movsxw(out, Address(obj,
1378 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1379 } else {
1380 __ movsxw(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_2, data_offset));
1381 }
1382 break;
1383 }
1384
1385 case Primitive::kPrimChar: {
1386 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1387 Register out = locations->Out().AsX86().AsCpuRegister();
1388 if (index.IsConstant()) {
1389 __ movzxw(out, Address(obj,
1390 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
1391 } else {
1392 __ movzxw(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_2, data_offset));
1393 }
1394 break;
1395 }
1396
1397 case Primitive::kPrimInt:
1398 case Primitive::kPrimNot: {
1399 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1400 Register out = locations->Out().AsX86().AsCpuRegister();
1401 if (index.IsConstant()) {
1402 __ movl(out, Address(obj,
1403 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
1404 } else {
1405 __ movl(out, Address(obj, index.AsX86().AsCpuRegister(), TIMES_4, data_offset));
1406 }
1407 break;
1408 }
1409
1410 case Primitive::kPrimLong: {
1411 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1412 X86ManagedRegister out = locations->Out().AsX86();
1413 if (index.IsConstant()) {
1414 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1415 __ movl(out.AsRegisterPairLow(), Address(obj, offset));
1416 __ movl(out.AsRegisterPairHigh(), Address(obj, offset + kX86WordSize));
1417 } else {
1418 __ movl(out.AsRegisterPairLow(),
1419 Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset));
1420 __ movl(out.AsRegisterPairHigh(),
1421 Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset + kX86WordSize));
1422 }
1423 break;
1424 }
1425
1426 case Primitive::kPrimFloat:
1427 case Primitive::kPrimDouble:
1428 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1429
1430 case Primitive::kPrimVoid:
1431 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1432 }
1433}
1434
1435void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001436 Primitive::Type value_type = instruction->GetComponentType();
1437 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1438 instruction,
1439 value_type == Primitive::kPrimNot ? LocationSummary::kCall : LocationSummary::kNoCall);
1440
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001441 if (value_type == Primitive::kPrimNot) {
1442 InvokeRuntimeCallingConvention calling_convention;
1443 locations->SetInAt(0, X86CpuLocation(calling_convention.GetRegisterAt(0)));
1444 locations->SetInAt(1, X86CpuLocation(calling_convention.GetRegisterAt(1)));
1445 locations->SetInAt(2, X86CpuLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001446 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001447 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
1448 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001449 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001450 // In case of a byte operation, the register allocator does not support multiple
1451 // inputs that die at entry with one in a specific register.
1452 bool dies_at_entry = value_type != Primitive::kPrimLong && !is_byte_type;
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001453 locations->SetInAt(0, Location::RequiresRegister(), dies_at_entry);
1454 locations->SetInAt(
1455 1, Location::RegisterOrConstant(instruction->InputAt(1)), dies_at_entry);
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01001456 if (is_byte_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001457 // Ensure the value is in a byte register.
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001458 locations->SetInAt(2, Location::ByteRegisterOrConstant(
1459 X86ManagedRegister::FromCpuRegister(EAX), instruction->InputAt(2)), dies_at_entry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001460 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001461 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)), dies_at_entry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001462 }
1463 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001464}
1465
1466void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
1467 LocationSummary* locations = instruction->GetLocations();
1468 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1469 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001470 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01001471 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001472
1473 switch (value_type) {
1474 case Primitive::kPrimBoolean:
1475 case Primitive::kPrimByte: {
1476 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001477 if (index.IsConstant()) {
1478 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001479 if (value.IsRegister()) {
1480 __ movb(Address(obj, offset), value.AsX86().AsByteRegister());
1481 } else {
1482 __ movb(Address(obj, offset),
1483 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1484 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001485 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001486 if (value.IsRegister()) {
1487 __ movb(Address(obj, index.AsX86().AsCpuRegister(), TIMES_1, data_offset),
1488 value.AsX86().AsByteRegister());
1489 } else {
1490 __ movb(Address(obj, index.AsX86().AsCpuRegister(), TIMES_1, data_offset),
1491 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1492 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001493 }
1494 break;
1495 }
1496
1497 case Primitive::kPrimShort:
1498 case Primitive::kPrimChar: {
1499 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001500 if (index.IsConstant()) {
1501 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001502 if (value.IsRegister()) {
1503 __ movw(Address(obj, offset), value.AsX86().AsCpuRegister());
1504 } else {
1505 __ movw(Address(obj, offset),
1506 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1507 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001508 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001509 if (value.IsRegister()) {
1510 __ movw(Address(obj, index.AsX86().AsCpuRegister(), TIMES_2, data_offset),
1511 value.AsX86().AsCpuRegister());
1512 } else {
1513 __ movw(Address(obj, index.AsX86().AsCpuRegister(), TIMES_2, data_offset),
1514 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1515 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001516 }
1517 break;
1518 }
1519
1520 case Primitive::kPrimInt: {
1521 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001522 if (index.IsConstant()) {
1523 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001524 if (value.IsRegister()) {
1525 __ movl(Address(obj, offset), value.AsX86().AsCpuRegister());
1526 } else {
1527 __ movl(Address(obj, offset), Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1528 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001529 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001530 if (value.IsRegister()) {
1531 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_4, data_offset),
1532 value.AsX86().AsCpuRegister());
1533 } else {
1534 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_4, data_offset),
1535 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
1536 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001537 }
1538 break;
1539 }
1540
1541 case Primitive::kPrimNot: {
1542 DCHECK(!codegen_->IsLeafMethod());
1543 __ fs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86WordSize, pAputObject)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001544 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001545 break;
1546 }
1547
1548 case Primitive::kPrimLong: {
1549 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001550 if (index.IsConstant()) {
1551 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001552 if (value.IsRegister()) {
1553 __ movl(Address(obj, offset), value.AsX86().AsRegisterPairLow());
1554 __ movl(Address(obj, offset + kX86WordSize), value.AsX86().AsRegisterPairHigh());
1555 } else {
1556 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
1557 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
1558 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
1559 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001560 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001561 if (value.IsRegister()) {
1562 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset),
1563 value.AsX86().AsRegisterPairLow());
1564 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset + kX86WordSize),
1565 value.AsX86().AsRegisterPairHigh());
1566 } else {
1567 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
1568 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset),
1569 Immediate(Low32Bits(val)));
1570 __ movl(Address(obj, index.AsX86().AsCpuRegister(), TIMES_8, data_offset + kX86WordSize),
1571 Immediate(High32Bits(val)));
1572 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001573 }
1574 break;
1575 }
1576
1577 case Primitive::kPrimFloat:
1578 case Primitive::kPrimDouble:
1579 LOG(FATAL) << "Unimplemented register type " << instruction->GetType();
1580
1581 case Primitive::kPrimVoid:
1582 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1583 }
1584}
1585
1586void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
1587 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01001588 locations->SetInAt(0, Location::RequiresRegister(), Location::kDiesAtEntry);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001589 locations->SetOut(Location::RequiresRegister());
1590 instruction->SetLocations(locations);
1591}
1592
1593void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
1594 LocationSummary* locations = instruction->GetLocations();
1595 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1596 Register obj = locations->InAt(0).AsX86().AsCpuRegister();
1597 Register out = locations->Out().AsX86().AsCpuRegister();
1598 __ movl(out, Address(obj, offset));
1599}
1600
1601void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001602 LocationSummary* locations =
1603 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001604 locations->SetInAt(0, Location::RequiresRegister());
1605 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01001606 if (instruction->HasUses()) {
1607 locations->SetOut(Location::SameAsFirstInput());
1608 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001609}
1610
1611void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
1612 LocationSummary* locations = instruction->GetLocations();
1613 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(
Nicolas Geoffray39468442014-09-02 15:17:15 +01001614 instruction, locations->InAt(0), locations->InAt(1));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001615 codegen_->AddSlowPath(slow_path);
1616
1617 Register index = locations->InAt(0).AsX86().AsCpuRegister();
1618 Register length = locations->InAt(1).AsX86().AsCpuRegister();
1619
1620 __ cmpl(index, length);
1621 __ j(kAboveEqual, slow_path->GetEntryLabel());
1622}
1623
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001624void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
1625 temp->SetLocations(nullptr);
1626}
1627
1628void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
1629 // Nothing to do, this is driven by the code generator.
1630}
1631
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001632void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001633 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001634}
1635
1636void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001637 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
1638}
1639
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001640void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
1641 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
1642}
1643
1644void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001645 HBasicBlock* block = instruction->GetBlock();
1646 if (block->GetLoopInformation() != nullptr) {
1647 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
1648 // The back edge will generate the suspend check.
1649 return;
1650 }
1651 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
1652 // The goto will generate the suspend check.
1653 return;
1654 }
1655 GenerateSuspendCheck(instruction, nullptr);
1656}
1657
1658void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
1659 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001660 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001661 new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001662 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001663 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001664 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001665 if (successor == nullptr) {
1666 __ j(kNotEqual, slow_path->GetEntryLabel());
1667 __ Bind(slow_path->GetReturnLabel());
1668 } else {
1669 __ j(kEqual, codegen_->GetLabelOf(successor));
1670 __ jmp(slow_path->GetEntryLabel());
1671 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001672}
1673
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001674X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
1675 return codegen_->GetAssembler();
1676}
1677
1678void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src) {
1679 ScratchRegisterScope ensure_scratch(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001680 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001681 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1682 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, src + stack_offset));
1683 __ movl(Address(ESP, dst + stack_offset), static_cast<Register>(ensure_scratch.GetRegister()));
1684}
1685
1686void ParallelMoveResolverX86::EmitMove(size_t index) {
1687 MoveOperands* move = moves_.Get(index);
1688 Location source = move->GetSource();
1689 Location destination = move->GetDestination();
1690
1691 if (source.IsRegister()) {
1692 if (destination.IsRegister()) {
1693 __ movl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister());
1694 } else {
1695 DCHECK(destination.IsStackSlot());
1696 __ movl(Address(ESP, destination.GetStackIndex()), source.AsX86().AsCpuRegister());
1697 }
1698 } else if (source.IsStackSlot()) {
1699 if (destination.IsRegister()) {
1700 __ movl(destination.AsX86().AsCpuRegister(), Address(ESP, source.GetStackIndex()));
1701 } else {
1702 DCHECK(destination.IsStackSlot());
1703 MoveMemoryToMemory(destination.GetStackIndex(),
1704 source.GetStackIndex());
1705 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001706 } else if (source.IsConstant()) {
1707 HIntConstant* instruction = source.GetConstant()->AsIntConstant();
1708 Immediate imm(instruction->AsIntConstant()->GetValue());
1709 if (destination.IsRegister()) {
1710 __ movl(destination.AsX86().AsCpuRegister(), imm);
1711 } else {
1712 __ movl(Address(ESP, destination.GetStackIndex()), imm);
1713 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001714 } else {
1715 LOG(FATAL) << "Unimplemented";
1716 }
1717}
1718
1719void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001720 Register suggested_scratch = reg == EAX ? EBX : EAX;
1721 ScratchRegisterScope ensure_scratch(
1722 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
1723
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001724 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
1725 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
1726 __ movl(Address(ESP, mem + stack_offset), reg);
1727 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
1728}
1729
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001730void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
1731 ScratchRegisterScope ensure_scratch1(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001732 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
1733
1734 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001735 ScratchRegisterScope ensure_scratch2(
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001736 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
1737
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001738 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
1739 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
1740 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
1741 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
1742 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
1743 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
1744}
1745
1746void ParallelMoveResolverX86::EmitSwap(size_t index) {
1747 MoveOperands* move = moves_.Get(index);
1748 Location source = move->GetSource();
1749 Location destination = move->GetDestination();
1750
1751 if (source.IsRegister() && destination.IsRegister()) {
1752 __ xchgl(destination.AsX86().AsCpuRegister(), source.AsX86().AsCpuRegister());
1753 } else if (source.IsRegister() && destination.IsStackSlot()) {
1754 Exchange(source.AsX86().AsCpuRegister(), destination.GetStackIndex());
1755 } else if (source.IsStackSlot() && destination.IsRegister()) {
1756 Exchange(destination.AsX86().AsCpuRegister(), source.GetStackIndex());
1757 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
1758 Exchange(destination.GetStackIndex(), source.GetStackIndex());
1759 } else {
1760 LOG(FATAL) << "Unimplemented";
1761 }
1762}
1763
1764void ParallelMoveResolverX86::SpillScratch(int reg) {
1765 __ pushl(static_cast<Register>(reg));
1766}
1767
1768void ParallelMoveResolverX86::RestoreScratch(int reg) {
1769 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001770}
1771
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001772} // namespace x86
1773} // namespace art